1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
|
/** @file
Arm Firmware TRNG interface library.
Copyright (c) 2021 - 2022, Arm Limited. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
@par Reference(s):
- [1] Arm True Random Number Generator Firmware, Interface 1.0,
Platform Design Document.
(https://developer.arm.com/documentation/den0098/latest/)
- [2] NIST Special Publication 800-90B, Recommendation for the Entropy
Sources Used for Random Bit Generation.
(https://csrc.nist.gov/publications/detail/sp/800-90b/final)
@par Glossary:
- TRNG - True Random Number Generator
- FID - Function ID
**/
#include <Base.h>
#include <Library/ArmLib.h>
#include <Library/ArmMonitorLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/DebugLib.h>
#include "ArmTrngDefs.h"
/** Convert TRNG status codes to RETURN status codes.
@param [in] TrngStatus TRNG status code.
@retval RETURN_SUCCESS Success.
@retval RETURN_UNSUPPORTED Function not implemented or
negative return code.
@retval RETURN_INVALID_PARAMETER A parameter is invalid.
@retval RETURN_NOT_READY No Entropy available.
**/
STATIC
RETURN_STATUS
TrngStatusToReturnStatus (
IN INT32 TrngStatus
)
{
switch (TrngStatus) {
case TRNG_STATUS_NOT_SUPPORTED:
return RETURN_UNSUPPORTED;
case TRNG_STATUS_INVALID_PARAMETER:
return RETURN_INVALID_PARAMETER;
case TRNG_STATUS_NO_ENTROPY:
return RETURN_NOT_READY;
case TRNG_STATUS_SUCCESS:
return RETURN_SUCCESS;
default:
if (TrngStatus < 0) {
return RETURN_UNSUPPORTED;
}
return RETURN_SUCCESS;
}
}
/** Get the version of the Arm TRNG backend.
A TRNG may be implemented by the system firmware, in which case this
function shall return the version of the Arm TRNG backend.
The implementation must return NOT_SUPPORTED if a Back end is not present.
@param [out] MajorRevision Major revision.
@param [out] MinorRevision Minor revision.
@retval RETURN_SUCCESS The function completed successfully.
@retval RETURN_INVALID_PARAMETER Invalid parameter.
@retval RETURN_UNSUPPORTED Backend not present.
**/
RETURN_STATUS
EFIAPI
GetArmTrngVersion (
OUT UINT16 *MajorRevision,
OUT UINT16 *MinorRevision
)
{
RETURN_STATUS Status;
ARM_MONITOR_ARGS Parameters;
INT32 Revision;
if ((MajorRevision == NULL) || (MinorRevision == NULL)) {
return RETURN_INVALID_PARAMETER;
}
ZeroMem (&Parameters, sizeof (Parameters));
Parameters.Arg0 = ARM_SMC_ID_TRNG_VERSION;
ArmMonitorCall (&Parameters);
Revision = (INT32)Parameters.Arg0;
Status = TrngStatusToReturnStatus (Revision);
if (RETURN_ERROR (Status)) {
return Status;
}
*MinorRevision = (Revision & TRNG_REV_MINOR_MASK);
*MajorRevision = ((Revision >> TRNG_REV_MAJOR_SHIFT) & TRNG_REV_MAJOR_MASK);
return RETURN_SUCCESS;
}
/** Get the features supported by the Arm TRNG backend.
The caller can determine if functions defined in the Arm TRNG ABI are
present in the ABI implementation.
@param [in] FunctionId Function Id.
@param [out] Capability Function specific capability if present.
@retval RETURN_SUCCESS The function completed successfully.
@retval RETURN_INVALID_PARAMETER Invalid parameter.
@retval RETURN_UNSUPPORTED Function not implemented.
**/
STATIC
RETURN_STATUS
EFIAPI
GetArmTrngFeatures (
IN CONST UINT32 FunctionId,
OUT UINT32 *Capability OPTIONAL
)
{
ARM_MONITOR_ARGS Parameters;
RETURN_STATUS Status;
ZeroMem (&Parameters, sizeof (Parameters));
Parameters.Arg0 = ARM_SMC_ID_TRNG_FEATURES;
Parameters.Arg1 = FunctionId;
ArmMonitorCall (&Parameters);
Status = TrngStatusToReturnStatus (Parameters.Arg0);
if (RETURN_ERROR (Status)) {
return Status;
}
if (Capability != NULL) {
*Capability = (UINT32)Parameters.Arg0;
}
return RETURN_SUCCESS;
}
/** Get the UUID of the Arm TRNG backend.
A TRNG may be implemented by the system firmware, in which case this
function shall return the UUID of the TRNG backend.
Returning the Arm TRNG UUID is optional and if not implemented,
RETURN_UNSUPPORTED shall be returned.
Note: The caller must not rely on the returned UUID as a trustworthy Arm TRNG
Back end identity
@param [out] Guid UUID of the Arm TRNG backend.
@retval RETURN_SUCCESS The function completed successfully.
@retval RETURN_INVALID_PARAMETER Invalid parameter.
@retval RETURN_UNSUPPORTED Function not implemented.
**/
RETURN_STATUS
EFIAPI
GetArmTrngUuid (
OUT GUID *Guid
)
{
ARM_MONITOR_ARGS Parameters;
if (Guid == NULL) {
return RETURN_INVALID_PARAMETER;
}
ZeroMem (&Parameters, sizeof (Parameters));
Parameters.Arg0 = ARM_SMC_ID_TRNG_GET_UUID;
ArmMonitorCall (&Parameters);
// Only invalid value is TRNG_STATUS_NOT_SUPPORTED (-1).
if ((INT32)Parameters.Arg0 == TRNG_STATUS_NOT_SUPPORTED) {
return TrngStatusToReturnStatus ((INT32)Parameters.Arg0);
}
Guid->Data1 = (Parameters.Arg0 & MAX_UINT32);
Guid->Data2 = (Parameters.Arg1 & MAX_UINT16);
Guid->Data3 = ((Parameters.Arg1 >> 16) & MAX_UINT16);
Guid->Data4[0] = (Parameters.Arg2 & MAX_UINT8);
Guid->Data4[1] = ((Parameters.Arg2 >> 8) & MAX_UINT8);
Guid->Data4[2] = ((Parameters.Arg2 >> 16) & MAX_UINT8);
Guid->Data4[3] = ((Parameters.Arg2 >> 24) & MAX_UINT8);
Guid->Data4[4] = (Parameters.Arg3 & MAX_UINT8);
Guid->Data4[5] = ((Parameters.Arg3 >> 8) & MAX_UINT8);
Guid->Data4[6] = ((Parameters.Arg3 >> 16) & MAX_UINT8);
Guid->Data4[7] = ((Parameters.Arg3 >> 24) & MAX_UINT8);
DEBUG ((DEBUG_INFO, "FW-TRNG: UUID %g\n", Guid));
return RETURN_SUCCESS;
}
/** Returns maximum number of entropy bits that can be returned in a single
call.
@return Returns the maximum number of Entropy bits that can be returned
in a single call to GetArmTrngEntropy().
**/
UINTN
EFIAPI
GetArmTrngMaxSupportedEntropyBits (
VOID
)
{
return MAX_ENTROPY_BITS;
}
/** Returns N bits of conditioned entropy.
See [2] Section 2.3.1 GetEntropy: An Interface to the Entropy Source
GetEntropy
Input:
bits_of_entropy: the requested amount of entropy
Output:
entropy_bitstring: The string that provides the requested entropy.
status: A Boolean value that is TRUE if the request has been satisfied,
and is FALSE otherwise.
@param [in] EntropyBits Number of entropy bits requested.
@param [in] BufferSize Size of the Buffer in bytes.
@param [out] Buffer Buffer to return the entropy bits.
@retval RETURN_SUCCESS The function completed successfully.
@retval RETURN_INVALID_PARAMETER Invalid parameter.
@retval RETURN_UNSUPPORTED Function not implemented.
@retval RETURN_BAD_BUFFER_SIZE Buffer size is too small.
@retval RETURN_NOT_READY No Entropy available.
**/
RETURN_STATUS
EFIAPI
GetArmTrngEntropy (
IN UINTN EntropyBits,
IN UINTN BufferSize,
OUT UINT8 *Buffer
)
{
RETURN_STATUS Status;
ARM_MONITOR_ARGS Parameters;
UINTN EntropyBytes;
UINTN LastValidBits;
UINTN BytesToClear;
UINTN EntropyData[3];
if ((EntropyBits == 0) ||
(EntropyBits > MAX_ENTROPY_BITS) ||
(Buffer == NULL))
{
return RETURN_INVALID_PARAMETER;
}
EntropyBytes = (EntropyBits + 7) >> 3;
if (EntropyBytes > BufferSize) {
return RETURN_BAD_BUFFER_SIZE;
}
ZeroMem (Buffer, BufferSize);
ZeroMem (&Parameters, sizeof (Parameters));
Parameters.Arg0 = ARM_SMC_ID_TRNG_RND;
Parameters.Arg1 = EntropyBits;
ArmMonitorCall (&Parameters);
Status = TrngStatusToReturnStatus ((INT32)Parameters.Arg0);
if (RETURN_ERROR (Status)) {
return Status;
}
// The entropy data is returned in the Parameters.Arg<3..1>
// With the lower order bytes in Parameters.Arg3 and the higher
// order bytes being stored in Parameters.Arg1.
EntropyData[0] = Parameters.Arg3;
EntropyData[1] = Parameters.Arg2;
EntropyData[2] = Parameters.Arg1;
CopyMem (Buffer, EntropyData, EntropyBytes);
// Mask off any unused top bytes, in accordance with specification.
BytesToClear = BufferSize - EntropyBytes;
if (BytesToClear != 0) {
ZeroMem (&Buffer[EntropyBytes], BytesToClear);
}
// Clear the unused MSB bits of the last byte.
LastValidBits = EntropyBits & 0x7;
if (LastValidBits != 0) {
Buffer[EntropyBytes - 1] &= (0xFF >> (8 - LastValidBits));
}
return Status;
}
/** The constructor checks that the FW-TRNG interface is supported
by the host firmware.
It will ASSERT() if FW-TRNG is not supported.
It will always return RETURN_SUCCESS.
@retval RETURN_SUCCESS The constructor always returns RETURN_SUCCESS.
**/
RETURN_STATUS
EFIAPI
ArmTrngLibConstructor (
VOID
)
{
ARM_MONITOR_ARGS Parameters;
RETURN_STATUS Status;
UINT16 MajorRev;
UINT16 MinorRev;
GUID Guid;
ZeroMem (&Parameters, sizeof (Parameters));
Parameters.Arg0 = SMCCC_VERSION;
ArmMonitorCall (&Parameters);
Status = TrngStatusToReturnStatus ((INT32)Parameters.Arg0);
if (RETURN_ERROR (Status)) {
goto ErrorHandler;
}
// Cf [1] s2.1.3 'Caller responsibilities',
// SMCCC version must be greater or equal than 1.1
if ((INT32)Parameters.Arg0 < 0x10001) {
goto ErrorHandler;
}
Status = GetArmTrngVersion (&MajorRev, &MinorRev);
if (RETURN_ERROR (Status)) {
goto ErrorHandler;
}
// Check that the required features are present.
Status = GetArmTrngFeatures (ARM_SMC_ID_TRNG_RND, NULL);
if (RETURN_ERROR (Status)) {
goto ErrorHandler;
}
// Check if TRNG UUID is supported and if so trace the GUID.
Status = GetArmTrngFeatures (ARM_SMC_ID_TRNG_GET_UUID, NULL);
if (RETURN_ERROR (Status)) {
goto ErrorHandler;
}
DEBUG_CODE_BEGIN ();
Status = GetArmTrngUuid (&Guid);
if (RETURN_ERROR (Status)) {
goto ErrorHandler;
}
DEBUG ((
DEBUG_INFO,
"FW-TRNG: Version %d.%d, GUID {%g}\n",
MajorRev,
MinorRev,
&Guid
));
DEBUG_CODE_END ();
return RETURN_SUCCESS;
ErrorHandler:
DEBUG ((DEBUG_ERROR, "ArmTrngLib could not be correctly initialized.\n"));
return RETURN_SUCCESS;
}
|