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
|
/** @file
RSA Asymmetric Cipher Wrapper Implementation over MbedTLS.
This file implements following APIs which provide more capabilities for RSA:
1) RsaGetKey
2) RsaGenerateKey
3) RsaCheckKey
4) RsaPkcs1Sign
RFC 8017 - PKCS #1: RSA Cryptography Specifications Version 2.2
Copyright (c) 2024, Intel Corporation. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#include "InternalCryptLib.h"
#include <mbedtls/rsa.h>
/**
Gets the tag-designated RSA key component from the established RSA context.
This function retrieves the tag-designated RSA key component from the
established RSA context as a non-negative integer (octet string format
represented in RSA PKCS#1).
If specified key component has not been set or has been cleared, then returned
BnSize is set to 0.
If the BigNumber buffer is too small to hold the contents of the key, FALSE
is returned and BnSize is set to the required buffer size to obtain the key.
If RsaContext is NULL, then return FALSE.
If BnSize is NULL, then return FALSE.
If BnSize is large enough but BigNumber is NULL, then return FALSE.
@param[in, out] RsaContext Pointer to RSA context being set.
@param[in] KeyTag Tag of RSA key component being set.
@param[out] BigNumber Pointer to octet integer buffer.
@param[in, out] BnSize On input, the size of big number buffer in bytes.
On output, the size of data returned in big number buffer in bytes.
@retval TRUE RSA key component was retrieved successfully.
@retval FALSE Invalid RSA key component tag.
@retval FALSE BnSize is too small.
**/
BOOLEAN
EFIAPI
RsaGetKey (
IN OUT VOID *RsaContext,
IN RSA_KEY_TAG KeyTag,
OUT UINT8 *BigNumber,
IN OUT UINTN *BnSize
)
{
mbedtls_rsa_context *RsaKey;
INT32 Ret;
mbedtls_mpi Value;
UINTN Size;
//
// Check input parameters.
//
if ((RsaContext == NULL) || (*BnSize > INT_MAX)) {
return FALSE;
}
//
// Init mbedtls_mpi
//
mbedtls_mpi_init (&Value);
Size = *BnSize;
*BnSize = 0;
RsaKey = (mbedtls_rsa_context *)RsaContext;
switch (KeyTag) {
case RsaKeyN:
Ret = mbedtls_rsa_export (RsaKey, &Value, NULL, NULL, NULL, NULL);
break;
case RsaKeyE:
Ret = mbedtls_rsa_export (RsaKey, NULL, NULL, NULL, NULL, &Value);
break;
case RsaKeyD:
Ret = mbedtls_rsa_export (RsaKey, NULL, NULL, NULL, &Value, NULL);
break;
case RsaKeyQ:
Ret = mbedtls_rsa_export (RsaKey, NULL, NULL, &Value, NULL, NULL);
break;
case RsaKeyP:
Ret = mbedtls_rsa_export (RsaKey, NULL, &Value, NULL, NULL, NULL);
break;
case RsaKeyDp:
case RsaKeyDq:
case RsaKeyQInv:
default:
Ret = -1;
break;
}
if (Ret != 0) {
goto End;
}
if (mbedtls_mpi_size (&Value) == 0) {
Ret = 0;
goto End;
}
*BnSize = Size;
Size = mbedtls_mpi_size (&Value);
if (*BnSize < Size) {
Ret = 1;
*BnSize = Size;
goto End;
}
if (BigNumber == NULL) {
Ret = 0;
*BnSize = Size;
goto End;
}
if ((BigNumber != NULL) && (Ret == 0)) {
Ret = mbedtls_mpi_write_binary (&Value, BigNumber, Size);
*BnSize = Size;
}
End:
mbedtls_mpi_free (&Value);
return Ret == 0;
}
/**
Generates RSA key components.
This function generates RSA key components. It takes RSA public exponent Pe and
length in bits of RSA modulus N as input, and generates all key components.
If PublicExponent is NULL, the default RSA public exponent (0x10001) will be used.
Before this function can be invoked, pseudorandom number generator must be correctly
initialized by RandomSeed().
If RsaContext is NULL, then return FALSE.
@param[in, out] RsaContext Pointer to RSA context being set.
@param[in] ModulusLength Length of RSA modulus N in bits.
@param[in] PublicExponent Pointer to RSA public exponent.
@param[in] PublicExponentSize Size of RSA public exponent buffer in bytes.
@retval TRUE RSA key component was generated successfully.
@retval FALSE Invalid RSA key component tag.
**/
BOOLEAN
EFIAPI
RsaGenerateKey (
IN OUT VOID *RsaContext,
IN UINTN ModulusLength,
IN CONST UINT8 *PublicExponent,
IN UINTN PublicExponentSize
)
{
INT32 Ret;
mbedtls_rsa_context *Rsa;
INT32 Pe;
//
// Check input parameters.
//
if ((RsaContext == NULL) || (ModulusLength > INT_MAX) || (PublicExponentSize > INT_MAX)) {
return FALSE;
}
Rsa = (mbedtls_rsa_context *)RsaContext;
if (PublicExponent == NULL) {
Pe = 0x10001;
} else {
if (PublicExponentSize == 0) {
return FALSE;
}
switch (PublicExponentSize) {
case 1:
Pe = PublicExponent[0];
break;
case 2:
Pe = PublicExponent[0] << 8 | PublicExponent[1];
break;
case 3:
Pe = PublicExponent[0] << 16 | PublicExponent[1] << 8 |
PublicExponent[2];
break;
case 4:
Pe = PublicExponent[0] << 24 | PublicExponent[1] << 16 |
PublicExponent[2] << 8 | PublicExponent[3];
break;
default:
return FALSE;
}
}
Ret = mbedtls_rsa_gen_key (
Rsa,
MbedtlsRand,
NULL,
(UINT32)ModulusLength,
Pe
);
return Ret == 0;
}
/**
Validates key components of RSA context.
NOTE: This function performs integrity checks on all the RSA key material, so
the RSA key structure must contain all the private key data.
This function validates key components of RSA context in following aspects:
- Whether p is a prime
- Whether q is a prime
- Whether n = p * q
- Whether d*e = 1 mod lcm(p-1,q-1)
If RsaContext is NULL, then return FALSE.
@param[in] RsaContext Pointer to RSA context to check.
@retval TRUE RSA key components are valid.
@retval FALSE RSA key components are not valid.
**/
BOOLEAN
EFIAPI
RsaCheckKey (
IN VOID *RsaContext
)
{
if (RsaContext == NULL) {
return FALSE;
}
UINT32 Ret;
Ret = mbedtls_rsa_complete (RsaContext);
if (Ret == 0) {
Ret = mbedtls_rsa_check_privkey (RsaContext);
}
return Ret == 0;
}
/**
Carries out the RSA-SSA signature generation with EMSA-PKCS1-v1_5 encoding scheme.
This function carries out the RSA-SSA signature generation with EMSA-PKCS1-v1_5 encoding scheme defined in
RSA PKCS#1.
If the Signature buffer is too small to hold the contents of signature, FALSE
is returned and SigSize is set to the required buffer size to obtain the signature.
If RsaContext is NULL, then return FALSE.
If MessageHash is NULL, then return FALSE.
If HashSize is not equal to the size of MD5, SHA-1, SHA-256, SHA-384 or SHA-512 digest, then return FALSE.
If SigSize is large enough but Signature is NULL, then return FALSE.
@param[in] RsaContext Pointer to RSA context for signature generation.
@param[in] MessageHash Pointer to octet message hash to be signed.
@param[in] HashSize Size of the message hash in bytes.
@param[out] Signature Pointer to buffer to receive RSA PKCS1-v1_5 signature.
@param[in, out] SigSize On input, the size of Signature buffer in bytes.
On output, the size of data returned in Signature buffer in bytes.
@retval TRUE Signature successfully generated in PKCS1-v1_5.
@retval FALSE Signature generation failed.
@retval FALSE SigSize is too small.
**/
BOOLEAN
EFIAPI
RsaPkcs1Sign (
IN VOID *RsaContext,
IN CONST UINT8 *MessageHash,
IN UINTN HashSize,
OUT UINT8 *Signature,
IN OUT UINTN *SigSize
)
{
INT32 Ret;
mbedtls_md_type_t MdAlg;
if ((RsaContext == NULL) || (MessageHash == NULL)) {
return FALSE;
}
if (mbedtls_rsa_complete ((mbedtls_rsa_context *)RsaContext) != 0) {
return FALSE;
}
switch (HashSize) {
#ifndef DISABLE_SHA1_DEPRECATED_INTERFACES
case SHA1_DIGEST_SIZE:
MdAlg = MBEDTLS_MD_SHA1;
break;
#endif
case SHA256_DIGEST_SIZE:
MdAlg = MBEDTLS_MD_SHA256;
break;
case SHA384_DIGEST_SIZE:
MdAlg = MBEDTLS_MD_SHA384;
break;
case SHA512_DIGEST_SIZE:
MdAlg = MBEDTLS_MD_SHA512;
break;
default:
return FALSE;
}
if (mbedtls_rsa_get_len (RsaContext) > *SigSize) {
*SigSize = mbedtls_rsa_get_len (RsaContext);
return FALSE;
}
if (Signature == NULL) {
return FALSE;
}
Ret = mbedtls_rsa_set_padding (RsaContext, MBEDTLS_RSA_PKCS_V15, MdAlg);
if (Ret != 0) {
return FALSE;
}
Ret = mbedtls_rsa_pkcs1_sign (
RsaContext,
MbedtlsRand,
NULL,
MdAlg,
(UINT32)HashSize,
MessageHash,
Signature
);
if (Ret != 0) {
return FALSE;
}
*SigSize = mbedtls_rsa_get_len (RsaContext);
return TRUE;
}
|