1
10
13
14
20
21
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
152
153
154
155
156
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
232
233
234
238
239
243
244
245
246
247
248
249
250
251
252
253
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
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
402
403
407
408
409
410
411
416
421
422
423
424
428
429
430
431
432
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
...
...
...
#include "nx_crypto_rsa.h"
#include "nx_crypto_huge_number.h"
...
...
NX_CRYPTO_KEEP UINT _nx_crypto_rsa_operation(const UCHAR *exponent, UINT exponent_length, const UCHAR *modulus, UINT modulus_length,
const UCHAR *p, UINT p_length, UCHAR *q, UINT q_length,
const UCHAR *input, UINT input_length, UCHAR *output,
USHORT *scratch_buf_ptr, UINT scratch_buf_length)
{
UCHAR *scratch;
UINT mod_length;
NX_CRYPTO_HUGE_NUMBER modulus_hn, exponent_hn, input_hn, output_hn, p_hn, q_hn;
NX_CRYPTO_PARAMETER_NOT_USED(scratch_buf_length);
scratch = (UCHAR *)scratch_buf_ptr;
modulus_hn.nx_crypto_huge_number_data = (HN_UBASE *)scratch;
scratch += modulus_length;
modulus_hn.nx_crypto_huge_buffer_size = modulus_length;
input_hn.nx_crypto_huge_number_data = (HN_UBASE *)scratch;
scratch += modulus_length;
input_hn.nx_crypto_huge_buffer_size = modulus_length;
exponent_hn.nx_crypto_huge_number_data = (HN_UBASE *)scratch;
scratch += modulus_length;
exponent_hn.nx_crypto_huge_buffer_size = modulus_length;
output_hn.nx_crypto_huge_number_data = (HN_UBASE *)scratch;
scratch += modulus_length * 2;
output_hn.nx_crypto_huge_buffer_size = modulus_length * 2;
_nx_crypto_huge_number_setup(&exponent_hn, exponent, exponent_length);
_nx_crypto_huge_number_setup(&input_hn, input, input_length);
_nx_crypto_huge_number_setup(&modulus_hn, modulus, modulus_length);
if (p && q)
{
p_hn.nx_crypto_huge_number_data = (HN_UBASE *)scratch;
scratch += (modulus_length >> 1);
p_hn.nx_crypto_huge_buffer_size = (modulus_length >> 1);
q_hn.nx_crypto_huge_number_data = (HN_UBASE *)scratch;
scratch += (modulus_length >> 1);
q_hn.nx_crypto_huge_buffer_size = (modulus_length >> 1);
_nx_crypto_huge_number_setup(&p_hn, p, p_length);
_nx_crypto_huge_number_setup(&q_hn, q, q_length);
/* ... */
_nx_crypto_huge_number_crt_power_modulus(&input_hn, &exponent_hn, &p_hn, &q_hn,
&modulus_hn, &output_hn,
(HN_UBASE *)scratch);
}if (p && q) { ... }
else
{
/* ... */
_nx_crypto_huge_number_mont_power_modulus(&input_hn, &exponent_hn, &modulus_hn,
&output_hn, (HN_UBASE *)scratch);
}else { ... }
_nx_crypto_huge_number_extract(&output_hn, output, modulus_length, &mod_length);
return(NX_CRYPTO_SUCCESS);
}{ ... }
...
...
NX_CRYPTO_KEEP UINT _nx_crypto_method_rsa_init(struct NX_CRYPTO_METHOD_STRUCT *method,
UCHAR *key, NX_CRYPTO_KEY_SIZE key_size_in_bits,
VOID **handle,
VOID *crypto_metadata,
ULONG crypto_metadata_size)
{
NX_CRYPTO_RSA *ctx;
NX_CRYPTO_PARAMETER_NOT_USED(handle);
NX_CRYPTO_STATE_CHECK
if ((method == NX_CRYPTO_NULL) || (key == NX_CRYPTO_NULL) || (crypto_metadata == NX_CRYPTO_NULL))
{
return(NX_CRYPTO_PTR_ERROR);
}if ((method == NX_CRYPTO_NULL) || (key == NX_CRYPTO_NULL) || (crypto_metadata == NX_CRYPTO_NULL)) { ... }
if((((ULONG)crypto_metadata) & 0x3) != 0)
{
return(NX_CRYPTO_PTR_ERROR);
}if ((((ULONG)crypto_metadata) & 0x3) != 0) { ... }
if(crypto_metadata_size < sizeof(NX_CRYPTO_RSA))
{
return(NX_CRYPTO_PTR_ERROR);
}if (crypto_metadata_size < sizeof(NX_CRYPTO_RSA)) { ... }
ctx = (NX_CRYPTO_RSA *)crypto_metadata;
ctx -> nx_crypto_rsa_modulus = key;
ctx -> nx_crypto_rsa_modulus_length = key_size_in_bits >> 3;
ctx -> nx_crypto_rsa_prime_p = NX_CRYPTO_NULL;
ctx -> nx_crypto_rsa_prime_p_length = 0;
ctx -> nx_crypto_rsa_prime_q = NX_CRYPTO_NULL;
ctx -> nx_crypto_rsa_prime_q_length = 0;
/* ... */
return(NX_CRYPTO_SUCCESS);
}{ ... }
...
...
NX_CRYPTO_KEEP UINT _nx_crypto_method_rsa_cleanup(VOID *crypto_metadata)
{
NX_CRYPTO_STATE_CHECK
#ifdef NX_SECURE_KEY_CLEAR
if (!crypto_metadata)
return (NX_CRYPTO_SUCCESS);
NX_CRYPTO_MEMSET(crypto_metadata, 0, sizeof(NX_CRYPTO_RSA));/* ... */
#else
NX_CRYPTO_PARAMETER_NOT_USED(crypto_metadata);
#endif
return(NX_CRYPTO_SUCCESS);
}{ ... }
...
...
NX_CRYPTO_KEEP UINT _nx_crypto_method_rsa_operation(UINT op,
VOID *handle,
struct NX_CRYPTO_METHOD_STRUCT *method,
UCHAR *key,
NX_CRYPTO_KEY_SIZE key_size_in_bits,
UCHAR *input,
ULONG input_length_in_byte,
UCHAR *iv_ptr,
UCHAR *output,
ULONG output_length_in_byte,
VOID *crypto_metadata,
ULONG crypto_metadata_size,
VOID *packet_ptr,
VOID (*nx_crypto_hw_process_callback)(VOID *packet_ptr, UINT status))
{
NX_CRYPTO_RSA *ctx;
UINT return_value = NX_CRYPTO_SUCCESS;
NX_CRYPTO_PARAMETER_NOT_USED(handle);
NX_CRYPTO_PARAMETER_NOT_USED(iv_ptr);
NX_CRYPTO_PARAMETER_NOT_USED(packet_ptr);
NX_CRYPTO_PARAMETER_NOT_USED(nx_crypto_hw_process_callback);
NX_CRYPTO_STATE_CHECK
if((method == NX_CRYPTO_NULL) || (crypto_metadata == NX_CRYPTO_NULL) || ((((ULONG)crypto_metadata) & 0x3) != 0))
{
return(NX_CRYPTO_PTR_ERROR);
}if ((method == NX_CRYPTO_NULL) || (crypto_metadata == NX_CRYPTO_NULL) || ((((ULONG)crypto_metadata) & 0x3) != 0)) { ... }
if(crypto_metadata_size < sizeof(NX_CRYPTO_RSA))
{
return(NX_CRYPTO_PTR_ERROR);
}if (crypto_metadata_size < sizeof(NX_CRYPTO_RSA)) { ... }
ctx = (NX_CRYPTO_RSA *)crypto_metadata;
if (op == NX_CRYPTO_SET_PRIME_P)
{
ctx -> nx_crypto_rsa_prime_p = input;
ctx -> nx_crypto_rsa_prime_p_length = input_length_in_byte;
}if (op == NX_CRYPTO_SET_PRIME_P) { ... }
else if (op == NX_CRYPTO_SET_PRIME_Q)
{
ctx -> nx_crypto_rsa_prime_q = input;
ctx -> nx_crypto_rsa_prime_q_length = input_length_in_byte;
}else if (op == NX_CRYPTO_SET_PRIME_Q) { ... }
else
{
if (key == NX_CRYPTO_NULL)
{
return(NX_CRYPTO_PTR_ERROR);
}if (key == NX_CRYPTO_NULL) { ... }
if(output_length_in_byte < (key_size_in_bits >> 3))
return(NX_CRYPTO_INVALID_BUFFER_SIZE);
if (input_length_in_byte > (ctx -> nx_crypto_rsa_modulus_length))
{
return(NX_CRYPTO_PTR_ERROR);
}if (input_length_in_byte > (ctx -> nx_crypto_rsa_modulus_length)) { ... }
return_value = _nx_crypto_rsa_operation(key,
key_size_in_bits >> 3,
ctx -> nx_crypto_rsa_modulus,
ctx -> nx_crypto_rsa_modulus_length,
ctx -> nx_crypto_rsa_prime_p,
ctx -> nx_crypto_rsa_prime_p_length,
ctx -> nx_crypto_rsa_prime_q,
ctx -> nx_crypto_rsa_prime_q_length,
input, input_length_in_byte,
output,
ctx -> nx_crypto_rsa_scratch_buffer,
NX_CRYPTO_RSA_SCRATCH_BUFFER_SIZE);
}else { ... }
return(return_value);
}{ ... }