Select one of the symbols to view example projects that use it.
 
Outline
...
...
...
...
#include "nx_crypto_pkcs1_v1.5.h"
_NX_CRYPTO_DER_OID_SHA_1
_NX_CRYPTO_DER_OID_SHA_224
_NX_CRYPTO_DER_OID_SHA_256
_NX_CRYPTO_DER_OID_SHA_384
_NX_CRYPTO_DER_OID_SHA_512
_NX_CRYPTO_DER_OID_SHA_512_224
_NX_CRYPTO_DER_OID_SHA_512_256
...
...
_nx_crypto_pkcs1_v1_5_sign(UCHAR *, UINT, UCHAR *, UINT, UCHAR *, UCHAR *, UINT)
...
...
_nx_crypto_pkcs1_v1_5_verify(UCHAR *, UINT, UCHAR *, UINT, UCHAR *, UINT, UCHAR *)
...
...
_nx_crypto_pkcs1_v1_5_encode(UCHAR *, UINT, NX_CRYPTO_METHOD *, UCHAR *, UINT, UCHAR *, UINT)
...
...
_nx_crypto_method_pkcs1_v1_5_init(struct NX_CRYPTO_METHOD_STRUCT *, UCHAR *, NX_CRYPTO_KEY_SIZE, void **, void *, ULONG)
...
...
_nx_crypto_method_pkcs1_v1_5_cleanup(void *)
...
_nx_crypto_method_pkcs1_v1_5_operation(UINT, void *, struct NX_CRYPTO_METHOD_STRUCT *, UCHAR *, NX_CRYPTO_KEY_SIZE, UCHAR *, ULONG, UCHAR *, UCHAR *, ULONG, void *, ULONG, void *, void (*)(void *, UINT))
Files
loading...
CodeScopeSTM32 Libraries and Samplesnetxduocrypto_libraries/src/nx_crypto_pkcs1_v1.5.c
 
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/**************************************************************************/ /* */ /* Copyright (c) Microsoft Corporation. All rights reserved. */ /* */ /* This software is licensed under the Microsoft Software License */ /* Terms for Microsoft Azure RTOS. Full text of the license can be */ /* found in the LICENSE file at https://aka.ms/AzureRTOS_EULA */ /* and in the root directory of this software. */ /* */... /**************************************************************************/ ... /**************************************************************************/ /**************************************************************************/ /** */ /** NetX Crypto Component */ /** */ /** Transport Layer Security (TLS) - PKCS#1 v1.5 functions */ /** */... /**************************************************************************/ /**************************************************************************/ #include "nx_crypto_pkcs1_v1.5.h" /* DER encodings (with OIDs for common algorithms) from RFC 8017. * NOTE: This is the equivalent DER-encoding for the value "T" described in RFC 8017 section 9.2. *//* ... */ static const UCHAR _NX_CRYPTO_DER_OID_SHA_1[] = {0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14}; static const UCHAR _NX_CRYPTO_DER_OID_SHA_224[] = {0x30, 0x2d, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x04, 0x05, 0x00, 0x04, 0x1c}; static const UCHAR _NX_CRYPTO_DER_OID_SHA_256[] = {0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20}; static const UCHAR _NX_CRYPTO_DER_OID_SHA_384[] = {0x30, 0x41, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02, 0x05, 0x00, 0x04, 0x30}; static const UCHAR _NX_CRYPTO_DER_OID_SHA_512[] = {0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03, 0x05, 0x00, 0x04, 0x40}; static const UCHAR _NX_CRYPTO_DER_OID_SHA_512_224[] = {0x30, 0x2d, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x05, 0x05, 0x00, 0x04, 0x1c}; static const UCHAR _NX_CRYPTO_DER_OID_SHA_512_256[] = {0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x06, 0x05, 0x00, 0x04, 0x20}; ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_crypto_pkcs1_v1_5_sign PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function generates an encoded signature using PKCS#1v1.5 */ /* formatting. */ /* */ /* INPUT */ /* */ /* input Data to sign */ /* */ /* OUTPUT */ /* */ /* status Completion status */ /* */ /* CALLS */ /* */ /* [nx_crypto_operation] Crypto operations */ /* */ /* CALLED BY */ /* */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 Timothy Stapko Initial Version 6.0 */ /* 09-30-2020 Timothy Stapko Modified comment(s), */ /* resulting in version 6.1 */ /* */... /**************************************************************************/ NX_CRYPTO_KEEP UINT _nx_crypto_pkcs1_v1_5_sign(UCHAR *input, UINT input_length, UCHAR *private_key, UINT private_key_size, UCHAR *metadata_area, UCHAR *output, UINT output_size) { NX_CRYPTO_PKCS1 *ctx = (NX_CRYPTO_PKCS1 *)metadata_area; UINT status; /* From RFC 8017 Section 9.2, EMSA-PKCS1-v1_5-ENCODE (M, emLen): * * 1. Apply the hash function to the message M to produce a hash * value H: * * H = Hash(M). * * If the hash function outputs "message too long", output * "message too long" and stop. * * 2. Encode the algorithm ID for the hash function and the hash * value into an ASN.1 value of type DigestInfo (see * Appendix A.2.4) with the DER, where the type DigestInfo has * the syntax * * DigestInfo ::= SEQUENCE { * digestAlgorithm AlgorithmIdentifier, * digest OCTET STRING * } * * The first field identifies the hash function and the second * contains the hash value. Let T be the DER encoding of the * DigestInfo value (see the notes below), and let tLen be the * length in octets of T. * * 3. If emLen < tLen + 11, output "intended encoded message length * too short" and stop. * * 4. Generate an octet string PS consisting of emLen - tLen - 3 * octets with hexadecimal value 0xff. The length of PS will be * at least 8 octets. * * 5. Concatenate PS, the DER encoding T, and other padding to form * the encoded message EM as * * EM = 0x00 || 0x01 || PS || 0x00 || T. * *//* ... */ /* Make sure we found a supported version (essentially an assertion check). */ if (ctx -> hash_method == NX_CRYPTO_NULL || ctx -> public_cipher_method == NX_CRYPTO_NULL || (ctx -> public_cipher_method) -> nx_crypto_init == NX_CRYPTO_NULL || (ctx -> public_cipher_method) -> nx_crypto_operation == NX_CRYPTO_NULL) { return(NX_CRYPTO_PTR_ERROR); }if (ctx -> hash_method == NX_CRYPTO_NULL || ctx -> public_cipher_method == NX_CRYPTO_NULL || (ctx -> public_cipher_method) -> nx_crypto_init == NX_CRYPTO_NULL || (ctx -> public_cipher_method) -> nx_crypto_operation == NX_CRYPTO_NULL) { ... } if (output_size < ctx -> modulus_size) { return(NX_CRYPTO_NOT_SUCCESSFUL); }if (output_size < ctx -> modulus_size) { ... } status = _nx_crypto_pkcs1_v1_5_encode(input, input_length, ctx -> hash_method, ctx -> hash_metadata, ctx -> hash_metadata_size, output, private_key_size); if(status != NX_CRYPTO_SUCCESS) { return(status); }if (status != NX_CRYPTO_SUCCESS) { ... } /* Initialize the crypto method with the modulus. */ status = (ctx -> public_cipher_method) -> nx_crypto_init(ctx -> public_cipher_method, ctx -> modulus, (NX_CRYPTO_KEY_SIZE)(ctx -> modulus_size << 3), NX_CRYPTO_NULL, ctx -> public_cipher_metadata, ctx -> public_cipher_metadata_size); if (status) { return(status); }if (status) { ... } /* Sign the hash we just generated using our local RSA private key (associated with our local cert). */ status = (ctx -> public_cipher_method) -> nx_crypto_operation(NX_CRYPTO_ENCRYPT, NX_CRYPTO_NULL, ctx -> public_cipher_method, private_key, (NX_CRYPTO_KEY_SIZE)(private_key_size << 3), output, (NX_CRYPTO_KEY_SIZE)(private_key_size), NX_CRYPTO_NULL, output, output_size, ctx -> public_cipher_metadata, ctx -> public_cipher_metadata_size, NX_CRYPTO_NULL, NX_CRYPTO_NULL); if(status != NX_CRYPTO_SUCCESS) { return(status); }if (status != NX_CRYPTO_SUCCESS) { ... } if ((ctx -> public_cipher_method) -> nx_crypto_cleanup) { status = (ctx -> public_cipher_method) -> nx_crypto_cleanup(ctx -> public_cipher_metadata); if(status != NX_CRYPTO_SUCCESS) { return(status); }if (status != NX_CRYPTO_SUCCESS) { ... } }if ((ctx -> public_cipher_method) -> nx_crypto_cleanup) { ... } return(NX_CRYPTO_SUCCESS); }{ ... } .../**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_crypto_pkcs1_v1_5_verify PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function verifies an encoded signature using PKCS#1v1.5 */ /* formatting. */ /* */ /* INPUT */ /* */ /* input Data to sign */ /* */ /* OUTPUT */ /* */ /* status Completion status */ /* */ /* CALLS */ /* */ /* [nx_crypto_operation] Crypto operations */ /* */ /* CALLED BY */ /* */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 Timothy Stapko Initial Version 6.0 */ /* 09-30-2020 Timothy Stapko Modified comment(s), */ /* resulting in version 6.1 */ /* */... /**************************************************************************/ NX_CRYPTO_KEEP UINT _nx_crypto_pkcs1_v1_5_verify(UCHAR *message, UINT message_length, UCHAR *signature, UINT signature_length, UCHAR *public_key, UINT public_key_size, UCHAR *metadata_area) { UCHAR *EM1, *EM2; NX_CRYPTO_PKCS1 *ctx = (NX_CRYPTO_PKCS1 *)metadata_area; UINT status; /* Make sure we found a supported version (essentially an assertion check). */ if (ctx -> hash_method == NX_CRYPTO_NULL || ctx -> public_cipher_method == NX_CRYPTO_NULL || (ctx -> public_cipher_method) -> nx_crypto_init == NX_CRYPTO_NULL || (ctx -> public_cipher_method) -> nx_crypto_operation == NX_CRYPTO_NULL) { return(NX_CRYPTO_PTR_ERROR); }if (ctx -> hash_method == NX_CRYPTO_NULL || ctx -> public_cipher_method == NX_CRYPTO_NULL || (ctx -> public_cipher_method) -> nx_crypto_init == NX_CRYPTO_NULL || (ctx -> public_cipher_method) -> nx_crypto_operation == NX_CRYPTO_NULL) { ... } if (sizeof(ctx -> scratch_buffer) < (2 * (ctx -> modulus_size))) { return(NX_CRYPTO_NOT_SUCCESSFUL); }if (sizeof(ctx -> scratch_buffer) < (2 * (ctx -> modulus_size))) { ... } /* Allocate space for encoded messages need to be compared. */ EM1 = ctx -> scratch_buffer; EM2 = ctx -> scratch_buffer + ctx -> modulus_size; /* Initialize the crypto method with the modulus. */ status = (ctx -> public_cipher_method) -> nx_crypto_init(ctx -> public_cipher_method, ctx -> modulus, (NX_CRYPTO_KEY_SIZE)(ctx -> modulus_size << 3), NX_CRYPTO_NULL, ctx -> public_cipher_metadata, ctx -> public_cipher_metadata_size); if (status) { return status; }if (status) { ... } /* Decrypt the signature by the public key to get EM1 */ status = (ctx -> public_cipher_method) -> nx_crypto_operation(NX_CRYPTO_ENCRYPT, NX_CRYPTO_NULL, ctx -> public_cipher_method, public_key, (NX_CRYPTO_KEY_SIZE)(public_key_size << 3), signature, signature_length, NX_CRYPTO_NULL, EM1, ctx -> modulus_size, ctx -> public_cipher_metadata, ctx -> public_cipher_metadata_size, NX_CRYPTO_NULL, NX_CRYPTO_NULL); if (status) { return status; }if (status) { ... } if ((ctx -> public_cipher_method) -> nx_crypto_cleanup) { status = (ctx -> public_cipher_method) -> nx_crypto_cleanup(ctx -> public_cipher_metadata); if(status != NX_CRYPTO_SUCCESS) { return(status); }if (status != NX_CRYPTO_SUCCESS) { ... } }if ((ctx -> public_cipher_method) -> nx_crypto_cleanup) { ... } /* Encoded the original message to get EM2. */ status = _nx_crypto_pkcs1_v1_5_encode(message, message_length, ctx -> hash_method, ctx -> hash_metadata, ctx -> hash_metadata_size, EM2, ctx -> modulus_size); if(status != NX_CRYPTO_SUCCESS) { return(status); }if (status != NX_CRYPTO_SUCCESS) { ... } if (NX_CRYPTO_MEMCMP(EM1, EM2, ctx -> modulus_size)) { return(NX_CRYPTO_NOT_SUCCESSFUL); }if (NX_CRYPTO_MEMCMP(EM1, EM2, ctx -> modulus_size)) { ... } return(NX_CRYPTO_SUCCESS); }{ ... } .../**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_crypto_pkcs1_v1_5_encode PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function encodes a message with PKCS#1v1.5. */ /* */ /* INPUT */ /* */ /* input Data to sign */ /* */ /* OUTPUT */ /* */ /* status Completion status */ /* */ /* CALLS */ /* */ /* [nx_crypto_operation] Crypto operations */ /* */ /* CALLED BY */ /* */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 Timothy Stapko Initial Version 6.0 */ /* 09-30-2020 Timothy Stapko Modified comment(s), improved */ /* buffer length verification, */ /* verified memcpy use cases, */ /* resulting in version 6.1 */ /* */... /**************************************************************************/ NX_CRYPTO_KEEP UINT _nx_crypto_pkcs1_v1_5_encode(UCHAR *input, UINT input_length, NX_CRYPTO_METHOD *hash_method, UCHAR *metadata_area, UINT metadata_size, UCHAR *output, UINT expected_output_length) { const UCHAR *der_encoding; UINT der_encoding_length; UINT signature_length = 0; UINT hash_length; UCHAR *working_ptr; UINT status; /* PKCS-1 Signature padding. The scheme is to start with the block type (0x00, 0x01 for signing) then pad with 0xFF bytes (for signing) followed with a single 0 byte right before the payload, which comes at the end of the RSA block. *//* ... */ /* Start with a clear buffer. */ NX_CRYPTO_MEMSET(output, 0xff, expected_output_length); output[0] = 0x0; output[1] = 0x1; /* Get the size of the hash output. */ hash_length = (hash_method -> nx_crypto_ICV_size_in_bits) >> 3; /* Figure out which hash method we are using to get our DER-encoding. */ switch(hash_method -> nx_crypto_algorithm) { case NX_CRYPTO_HASH_SHA1: der_encoding = _NX_CRYPTO_DER_OID_SHA_1; der_encoding_length = sizeof(_NX_CRYPTO_DER_OID_SHA_1); break;case NX_CRYPTO_HASH_SHA1: case NX_CRYPTO_HASH_SHA224: der_encoding = _NX_CRYPTO_DER_OID_SHA_224; der_encoding_length = sizeof(_NX_CRYPTO_DER_OID_SHA_224); break;case NX_CRYPTO_HASH_SHA224: case NX_CRYPTO_HASH_SHA256: der_encoding = _NX_CRYPTO_DER_OID_SHA_256; der_encoding_length = sizeof(_NX_CRYPTO_DER_OID_SHA_256); break;case NX_CRYPTO_HASH_SHA256: case NX_CRYPTO_HASH_SHA384: der_encoding = _NX_CRYPTO_DER_OID_SHA_384; der_encoding_length = sizeof(_NX_CRYPTO_DER_OID_SHA_384); break;case NX_CRYPTO_HASH_SHA384: case NX_CRYPTO_HASH_SHA512: der_encoding = _NX_CRYPTO_DER_OID_SHA_512; der_encoding_length = sizeof(_NX_CRYPTO_DER_OID_SHA_512); break;case NX_CRYPTO_HASH_SHA512: case NX_CRYPTO_HASH_SHA512_224: der_encoding = _NX_CRYPTO_DER_OID_SHA_512_224; der_encoding_length = sizeof(_NX_CRYPTO_DER_OID_SHA_512_224); break;case NX_CRYPTO_HASH_SHA512_224: case NX_CRYPTO_HASH_SHA512_256: der_encoding = _NX_CRYPTO_DER_OID_SHA_512_256; der_encoding_length = sizeof(_NX_CRYPTO_DER_OID_SHA_512_256); break;case NX_CRYPTO_HASH_SHA512_256: default: return(NX_CRYPTO_AUTHENTICATION_FAILED);default }switch (hash_method -> nx_crypto_algorithm) { ... } /* Encoded signature in RSA buffer (plaintext): * signature_length = der_encoding_length + hash_length * x = data_size - (der_encoding_length + hash_length) * Length: | x | der_encoding_length | hash_length | * Field: | Padding | DER encoding header | Hash value | * Value: | 0x0,0x1,... | <ASN.1 sequence, OID> | <hash output> | *//* ... */ /* Calculate our final signature length for later offset calculations. */ signature_length = der_encoding_length + hash_length; /* DER encoding size + hash size = plaintext encoded signature length */ if (signature_length + 1 > expected_output_length) { return(NX_CRYPTO_SIZE_ERROR); }if (signature_length + 1 > expected_output_length) { ... } output[expected_output_length - signature_length - 1] = 0; /* Get a working pointer into the padded signature buffer. All PKCS-1 encoded data comes at the end of the RSA encrypted block. *//* ... */ working_ptr = &output[expected_output_length - signature_length]; /* Copy in the DER encoding. */ NX_CRYPTO_MEMCPY(working_ptr, der_encoding, der_encoding_length); /* Use case of memcpy is verified. */ /* Move the working pointer to the end of the DER encoding. */ working_ptr += der_encoding_length; /* Generate hash of input data using supplied hash method, placing it in the output buffer at the end * of the RSA block (data_size). *//* ... */ status = hash_method -> nx_crypto_operation(NX_CRYPTO_AUTHENTICATE, NX_CRYPTO_NULL, hash_method, NX_CRYPTO_NULL, 0, input, input_length, NX_CRYPTO_NULL, working_ptr, hash_length, metadata_area, metadata_size, NX_CRYPTO_NULL, NX_CRYPTO_NULL); return (status); }{ ... } .../**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_crypto_method_pkcs1_v1_5_init PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function is the common crypto method init callback for */ /* Microsoft supported PKCS#1v1.5 cryptographic algorithm. */ /* */ /* INPUT */ /* */ /* method Pointer to crypto method */ /* key Pointer to key */ /* key_size_in_bits Length of key size in bits */ /* handler Returned crypto handler */ /* crypto_metadata Metadata area */ /* crypto_metadata_size Size of the metadata area */ /* */ /* OUTPUT */ /* */ /* status Completion status */ /* */ /* CALLS */ /* */ /* None */ /* */ /* CALLED BY */ /* */ /* Application Code */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 Timothy Stapko Initial Version 6.0 */ /* 09-30-2020 Timothy Stapko Modified comment(s), */ /* resulting in version 6.1 */ /* */... /**************************************************************************/ NX_CRYPTO_KEEP UINT _nx_crypto_method_pkcs1_v1_5_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_PARAMETER_NOT_USED(key); NX_CRYPTO_PARAMETER_NOT_USED(key_size_in_bits); NX_CRYPTO_PARAMETER_NOT_USED(handle); NX_CRYPTO_STATE_CHECK if ((method == NX_CRYPTO_NULL) || (crypto_metadata == NX_CRYPTO_NULL)) { return(NX_CRYPTO_PTR_ERROR); }if ((method == NX_CRYPTO_NULL) || (crypto_metadata == NX_CRYPTO_NULL)) { ... } /* Verify the metadata addrsss is 4-byte aligned. */ if((((ULONG)crypto_metadata) & 0x3) != 0) { return(NX_CRYPTO_PTR_ERROR); }if ((((ULONG)crypto_metadata) & 0x3) != 0) { ... } if(crypto_metadata_size < sizeof(NX_CRYPTO_PKCS1)) { return(NX_CRYPTO_PTR_ERROR); }if (crypto_metadata_size < sizeof(NX_CRYPTO_PKCS1)) { ... } return(NX_CRYPTO_SUCCESS); }{ ... } ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_crypto_method_pkcs1_v1_5_cleanup PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function cleans up the crypto metadata. */ /* */ /* INPUT */ /* */ /* crypto_metadata Crypto metadata */ /* */ /* OUTPUT */ /* */ /* status Completion status */ /* */ /* CALLS */ /* */ /* NX_CRYPTO_MEMSET Set the memory */ /* */ /* CALLED BY */ /* */ /* Application Code */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 Timothy Stapko Initial Version 6.0 */ /* 09-30-2020 Timothy Stapko Modified comment(s), */ /* resulting in version 6.1 */ /* */... /**************************************************************************/ NX_CRYPTO_KEEP UINT _nx_crypto_method_pkcs1_v1_5_cleanup(VOID *crypto_metadata) { NX_CRYPTO_STATE_CHECK #ifdef NX_SECURE_KEY_CLEAR if (!crypto_metadata) return (NX_CRYPTO_SUCCESS); /* Clean up the crypto metadata. */ NX_CRYPTO_MEMSET(crypto_metadata, 0, sizeof(NX_CRYPTO_PKCS1));/* ... */ #else NX_CRYPTO_PARAMETER_NOT_USED(crypto_metadata); #endif/* NX_SECURE_KEY_CLEAR */ return(NX_CRYPTO_SUCCESS); }{ ... } .../**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_crypto_method_pkcs1_operation PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function performs an PKCS#1v1.5 operation. */ /* */ /* INPUT */ /* */ /* op PKCS#1v1.5 operation */ /* handle Crypto handle */ /* method Cryption Method Object */ /* key Encryption Key */ /* key_size_in_bits Key size in bits */ /* input Input data */ /* input_length_in_byte Input data size */ /* iv_ptr Initial vector */ /* output Output buffer */ /* output_length_in_byte Output buffer size */ /* crypto_metadata Metadata area */ /* crypto_metadata_size Metadata area size */ /* packet_ptr Pointer to packet */ /* nx_crypto_hw_process_callback Callback function pointer */ /* */ /* OUTPUT */ /* */ /* status Completion status */ /* */ /* CALLS */ /* */ /* _nx_crypto_pkcs1_v1_5_sign Sign using PKCS#1v1.5 */ /* _nx_crypto_pkcs1_v1_5_verify Verify PKCS#1v1.5 signature */ /* */ /* CALLED BY */ /* */ /* Application Code */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 Timothy Stapko Initial Version 6.0 */ /* 09-30-2020 Timothy Stapko Modified comment(s), */ /* resulting in version 6.1 */ /* */... /**************************************************************************/ NX_CRYPTO_KEEP UINT _nx_crypto_method_pkcs1_v1_5_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 *, UINT)) { NX_CRYPTO_PKCS1 *ctx; NX_CRYPTO_PKCS1_OPTIONS *options; UINT status = 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 /* Verify the metadata addrsss is 4-byte aligned. */ 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_PKCS1)) { return(NX_CRYPTO_PTR_ERROR); }if (crypto_metadata_size < sizeof(NX_CRYPTO_PKCS1)) { ... } ctx = (NX_CRYPTO_PKCS1 *)(crypto_metadata); if (op == NX_CRYPTO_SET_ADDITIONAL_DATA) { options = (NX_CRYPTO_PKCS1_OPTIONS *)input; if ((options -> public_cipher_metadata_size < (options -> public_cipher_method) -> nx_crypto_metadata_area_size) || (options -> hash_metadata_size < (options -> hash_method) -> nx_crypto_metadata_area_size)) { return(NX_CRYPTO_NOT_SUCCESSFUL); }if ((options -> public_cipher_metadata_size < (options -> public_cipher_method) -> nx_crypto_metadata_area_size) || (options -> hash_metadata_size < (options -> hash_method) -> nx_crypto_metadata_area_size)) { ... } ctx -> public_cipher_method = options -> public_cipher_method; ctx -> public_cipher_metadata = options -> public_cipher_metadata; ctx -> public_cipher_metadata_size = options -> public_cipher_metadata_size; ctx -> hash_method = options -> hash_method; ctx -> hash_metadata = options -> hash_metadata; ctx -> hash_metadata_size = options -> hash_metadata_size; ctx -> modulus = key; ctx -> modulus_size = (key_size_in_bits >> 3); }if (op == NX_CRYPTO_SET_ADDITIONAL_DATA) { ... } else if (op == NX_CRYPTO_AUTHENTICATE) { status = _nx_crypto_pkcs1_v1_5_sign(input, input_length_in_byte, key, (key_size_in_bits >> 3), crypto_metadata, output, output_length_in_byte); }else if (op == NX_CRYPTO_AUTHENTICATE) { ... } else if (op == NX_CRYPTO_VERIFY) { status = _nx_crypto_pkcs1_v1_5_verify(input, input_length_in_byte, output, output_length_in_byte, key, (key_size_in_bits >> 3), crypto_metadata); }else if (op == NX_CRYPTO_VERIFY) { ... } else { status = NX_CRYPTO_NOT_SUCCESSFUL; }else { ... } return(status); }{ ... }