forked from wolfSSL/wolfssl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwc_lms_impl.c
More file actions
3735 lines (3443 loc) · 123 KB
/
Copy pathwc_lms_impl.c
File metadata and controls
3735 lines (3443 loc) · 123 KB
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
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* wc_lms_impl.c
*
* Copyright (C) 2006-2026 wolfSSL Inc.
*
* This file is part of wolfSSL.
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
/* Implementation based on:
* RFC 8554: Leighton-Micali Hash-Based Signatures
* https://datatracker.ietf.org/doc/html/rfc8554
* Implementation by Sean Parkinson.
*/
/* Possible LMS options:
*
* WC_LMS_FULL_HASH Default: OFF
* Performs a full hash instead of assuming internals.
* Enable when using hardware SHA-256.
* WOLFSSL_LMS_VERIFY_ONLY Default: OFF
* Only compiles in verification code.
* WOLFSSL_WC_LMS_SMALL Default: OFF
* Implementation is smaller code size with slow signing.
* Enable when memory is limited.
*/
#include <wolfssl/wolfcrypt/libwolfssl_sources.h>
#include <wolfssl/wolfcrypt/wc_lms.h>
#ifdef NO_INLINE
#include <wolfssl/wolfcrypt/misc.h>
#else
#define WOLFSSL_MISC_INCLUDED
#include <wolfcrypt/src/misc.c>
#endif
#if defined(WOLFSSL_HAVE_LMS) && defined(WOLFSSL_WC_LMS)
/* Length of R in bytes. */
#define LMS_R_LEN 4
/* Length of D in bytes. */
#define LMS_D_LEN 2
/* Length of checksum in bytes. */
#define LMS_CKSM_LEN 2
/* Predefined values used in hashes to make them unique. */
/* Fixed value for calculating x. */
#define LMS_D_FIXED 0xff
/* D value when computing public key. */
#define LMS_D_PBLC 0x8080
/* D value when computing message. */
#define LMS_D_MESG 0x8181
/* D value when computing leaf node. */
#define LMS_D_LEAF 0x8282
/* D value when computing interior node. */
#define LMS_D_INTR 0x8383
/* D value when computing C, randomizer value. */
#define LMS_D_C 0xfffd
/* D value when computing child SEED for private key. */
#define LMS_D_CHILD_SEED 0xfffe
/* D value when computing child I for private key. */
#define LMS_D_CHILD_I 0xffff
/* Length of data to hash when computing seed:
* 16 + 4 + 2 + 32/24 = 54/46 */
#define LMS_SEED_HASH_LEN(hLen) \
(LMS_I_LEN + LMS_R_LEN + LMS_D_LEN + (hLen))
/* Length of data to hash when computing a node:
* 16 + 4 + 2 + 32/24 + 32/24 = 86/70 */
#define LMS_NODE_HASH_LEN(hLen) \
(LMS_I_LEN + LMS_R_LEN + LMS_D_LEN + 2 * (hLen))
/* Length of data to hash when computing most results:
* 16 + 4 + 2 + 1 + 32/24 = 55/47 */
#define LMS_HASH_BUFFER_LEN(hLen) \
(LMS_I_LEN + LMS_Q_LEN + LMS_P_LEN + LMS_W_LEN + (hLen))
/* Length of preliminary data to hash when computing K:
* 16 + 4 + 2 = 22 */
#define LMS_K_PRE_LEN (LMS_I_LEN + LMS_Q_LEN + LMS_P_LEN)
/* Length of preliminary data to hash when computing message hash:
* 16 + 4 + 2 = 22 */
#define LMS_MSG_PRE_LEN (LMS_I_LEN + LMS_Q_LEN + LMS_P_LEN)
#ifdef WC_LMS_DEBUG_PRINT_DATA
/* Print data when debugging implementation.
*
* @param [in] name String to print before data.
* @param [in] data Array of bytes.
* @param [in] len Length of data in array.
*/
static void print_data(const char* name, const byte* data, int len)
{
int i;
fprintf(stderr, "%6s: ", name);
for (i = 0; i < len; i++) {
fprintf(stderr, "%02x", data[i]);
}
fprintf(stderr, "\n");
}
#endif
/***************************************
* Index APIs
**************************************/
#ifndef WOLFSSL_LMS_VERIFY_ONLY
/* Zero index.
*
* @param [out] a Byte array. Big-endian encoding.
* @param [in] len Length of array in bytes.
*/
static WC_INLINE void wc_lms_idx_zero(unsigned char* a, int len)
{
XMEMSET(a, 0, len);
}
/* Increment big-endian value.
*
* @param [in, out] a Byte array. Big-endian encoding.
* @param [in] len Length of array in bytes.
*/
static WC_INLINE void wc_lms_idx_inc(unsigned char* a, int len)
{
int i;
/* Starting at least-significant byte up to most. */
for (i = len - 1; i >= 0; i--) {
/* Add one/carry to byte. */
if ((++a[i]) != 0) {
/* No more carry. */
break;
}
}
}
#endif /* !WOLFSSL_LMS_VERIFY_ONLY */
/***************************************
* Hash APIs
**************************************/
/* Set hash data and length into SHA-256 digest.
*
* @param [in, out] state SHA-256 digest object.
* @param [in] data Data to add to hash.
* @param [in] len Number of bytes in data. Must be less than a block.
*/
#define LMS_SHA256_SET_DATA(sha256, data, len) \
do { \
XMEMCPY((sha256)->buffer, (data), (len)); \
(sha256)->buffLen = (len); \
(sha256)->loLen = (len); \
} while (0)
/* Add hash data and length into SHA-256 digest.
*
* @param [in, out] state SHA-256 digest object.
* @param [in] data Data to add to hash.
* @param [in] len Number of bytes in data. Must be less than a block.
*/
#define LMS_SHA256_ADD_DATA(sha256, data, len) \
do { \
XMEMCPY((byte*)(sha256)->buffer + (sha256)->buffLen, (data), (len)); \
(sha256)->buffLen += (len); \
(sha256)->loLen += (len); \
} while (0)
/* Set the length of 54 bytes in buffer as per SHA-256 final operation.
*
* @param [in, out] buffer Hash data buffer to add length to.
*/
#define LMS_SHA256_SET_LEN_54(buffer) \
do { \
(buffer)[54] = 0x80; \
(buffer)[55] = 0x00; \
(buffer)[56] = 0x00; \
(buffer)[57] = 0x00; \
(buffer)[58] = 0x00; \
(buffer)[59] = 0x00; \
(buffer)[60] = 0x00; \
(buffer)[61] = 0x00; \
(buffer)[62] = 0x01; \
(buffer)[63] = 0xb0; \
} while (0)
/* Set the length of 55 bytes in buffer as per SHA-256 final operation.
*
* @param [in, out] buffer Hash data buffer to add length to.
*/
#define LMS_SHA256_SET_LEN_55(buffer) \
do { \
(buffer)[55] = 0x80; \
(buffer)[56] = 0x00; \
(buffer)[57] = 0x00; \
(buffer)[58] = 0x00; \
(buffer)[59] = 0x00; \
(buffer)[60] = 0x00; \
(buffer)[61] = 0x00; \
(buffer)[62] = 0x01; \
(buffer)[63] = 0xb8; \
} while (0)
#ifndef WOLFSSL_NO_LMS_SHA256_256
#ifndef WC_LMS_FULL_HASH
/* Hash one full block of data and compute result.
*
* @param [in] sha256 SHA-256 hash object.
* @param [in] data Data to hash.
* @param [out] hash Hash output.
* @return 0 on success.
*/
static WC_INLINE int wc_lms_hash_block(wc_Sha256* sha256, const byte* data,
byte* hash)
{
/* Hash the block and reset SHA-256 state. */
return wc_Sha256HashBlock(sha256, data, hash);
}
#endif /* !WC_LMS_FULL_HASH */
/* Hash data and compute result.
*
* @param [in] sha256 SHA-256 hash object.
* @param [in] data Data to hash.
* @param [in] len Length of data to hash.
* @param [out] hash Hash output.
* @return 0 on success.
*/
static WC_INLINE int wc_lms_hash(wc_Sha256* sha256, byte* data, word32 len,
byte* hash)
{
int ret;
#ifndef WC_LMS_FULL_HASH
if (len < WC_SHA256_BLOCK_SIZE) {
/* Store data into SHA-256 object's buffer. */
LMS_SHA256_SET_DATA(sha256, data, len);
ret = wc_Sha256Final(sha256, hash);
}
else if (len < WC_SHA256_BLOCK_SIZE + WC_SHA256_PAD_SIZE) {
ret = wc_Sha256HashBlock(sha256, data, NULL);
if (ret == 0) {
byte* buffer = (byte*)sha256->buffer;
int rem = len - WC_SHA256_BLOCK_SIZE;
XMEMCPY(buffer, data + WC_SHA256_BLOCK_SIZE, rem);
buffer[rem++] = 0x80;
XMEMSET(buffer + rem, 0, WC_SHA256_BLOCK_SIZE - 2 - rem);
buffer[WC_SHA256_BLOCK_SIZE - 2] = (byte)(len >> 5);
buffer[WC_SHA256_BLOCK_SIZE - 1] = (byte)(len << 3);
ret = wc_Sha256HashBlock(sha256, buffer, hash);
}
}
else {
ret = wc_Sha256Update(sha256, data, len);
if (ret == 0) {
ret = wc_Sha256Final(sha256, hash);
}
}
#else
ret = wc_Sha256Update(sha256, data, len);
if (ret == 0) {
ret = wc_Sha256Final(sha256, hash);
}
#endif /* !WC_LMS_FULL_HASH */
return ret;
}
#endif /* !WOLFSSL_NO_LMS_SHA256_256 */
/* Update hash with first data.
*
* Sets the data directly into SHA-256's buffer if valid.
*
* @param [in] sha256 SHA-256 hash object.
* @param [in] data Data to hash.
* @param [in] len Length of data to hash.
* @return 0 on success.
*/
static WC_INLINE int wc_lms_hash_first(wc_Sha256* sha256, const byte* data,
word32 len)
{
int ret = 0;
#ifndef WC_LMS_FULL_HASH
if (len < WC_SHA256_BLOCK_SIZE) {
/* Store data into SHA-256 object's buffer. */
LMS_SHA256_SET_DATA(sha256, data, len);
}
else
#endif /* !WC_LMS_FULL_HASH */
{
ret = wc_Sha256Update(sha256, data, len);
}
return ret;
}
/* Update hash with further data.
*
* Adds the data directly into SHA-256's buffer if valid.
*
* @param [in] sha256 SHA-256 hash object.
* @param [in] data Data to hash.
* @param [in] len Length of data to hash.
* @return 0 on success.
*/
static WC_INLINE int wc_lms_hash_update(wc_Sha256* sha256, const byte* data,
word32 len)
{
int ret = 0;
#ifndef WC_LMS_FULL_HASH
if (sha256->buffLen + len < WC_SHA256_BLOCK_SIZE) {
/* Add data to SHA-256 object's buffer. */
LMS_SHA256_ADD_DATA(sha256, data, len);
}
else if (sha256->buffLen + len < 2 * WC_SHA256_BLOCK_SIZE) {
byte* buffer = (byte*)sha256->buffer;
XMEMCPY(buffer + sha256->buffLen, data,
WC_SHA256_BLOCK_SIZE - sha256->buffLen);
ret = wc_Sha256HashBlock(sha256, buffer, NULL);
if (ret == 0) {
int rem = len - (WC_SHA256_BLOCK_SIZE - sha256->buffLen);
XMEMCPY(buffer, data + WC_SHA256_BLOCK_SIZE - sha256->buffLen, rem);
sha256->buffLen = rem;
sha256->loLen += len;
}
}
else {
ret = wc_Sha256Update(sha256, data, len);
}
#else
ret = wc_Sha256Update(sha256, data, len);
#endif /* !WC_LMS_FULL_HASH */
return ret;
}
#ifndef WOLFSSL_NO_LMS_SHA256_256
/* Finalize hash.
*
* @param [in] sha256 SHA-256 hash object.
* @param [out] hash Hash output.
* @return 0 on success.
*/
static WC_INLINE int wc_lms_hash_final(wc_Sha256* sha256, byte* hash)
{
#ifndef WC_LMS_FULL_HASH
int ret = 0;
byte* buffer = (byte*)sha256->buffer;
buffer[sha256->buffLen++] = 0x80;
if (sha256->buffLen > WC_SHA256_PAD_SIZE) {
XMEMSET(buffer + sha256->buffLen, 0,
WC_SHA256_BLOCK_SIZE - sha256->buffLen);
ret = wc_Sha256HashBlock(sha256, buffer, NULL);
sha256->buffLen = 0;
}
if (ret == 0) {
XMEMSET(buffer + sha256->buffLen, 0,
WC_SHA256_BLOCK_SIZE - 8 - sha256->buffLen);
sha256->hiLen = (sha256->hiLen << 3) + (sha256->loLen >> 29);
sha256->loLen = sha256->loLen << 3;
#ifdef LITTLE_ENDIAN_ORDER
sha256->buffer[14] = ByteReverseWord32(sha256->hiLen);
sha256->buffer[15] = ByteReverseWord32(sha256->loLen);
#else
sha256->buffer[14] = sha256->hiLen;
sha256->buffer[15] = sha256->loLen;
#endif
ret = wc_Sha256HashBlock(sha256, buffer, hash);
sha256->buffLen = 0;
sha256->hiLen = 0;
sha256->loLen = 0;
}
return ret;
#else
return wc_Sha256Final(sha256, hash);
#endif
}
#endif /* !WOLFSSL_NO_LMS_SHA256_256 */
#ifdef WOLFSSL_LMS_SHA256_192
/* Set the length of 46 bytes in buffer as per SHA-256 final operation.
*
* @param [in, out] buffer Hash data buffer to add length to.
*/
#define LMS_SHA256_SET_LEN_46(buffer) \
do { \
(buffer)[46] = 0x80; \
(buffer)[47] = 0x00; \
(buffer)[48] = 0x00; \
(buffer)[49] = 0x00; \
(buffer)[50] = 0x00; \
(buffer)[51] = 0x00; \
(buffer)[52] = 0x00; \
(buffer)[53] = 0x00; \
(buffer)[54] = 0x00; \
(buffer)[55] = 0x00; \
(buffer)[56] = 0x00; \
(buffer)[57] = 0x00; \
(buffer)[58] = 0x00; \
(buffer)[59] = 0x00; \
(buffer)[60] = 0x00; \
(buffer)[61] = 0x00; \
(buffer)[62] = 0x01; \
(buffer)[63] = 0x70; \
} while (0)
/* Set the length of 47 bytes in buffer as per SHA-256 final operation.
*
* @param [in, out] buffer Hash data buffer to add length to.
*/
#define LMS_SHA256_SET_LEN_47(buffer) \
do { \
(buffer)[47] = 0x80; \
(buffer)[48] = 0x00; \
(buffer)[49] = 0x00; \
(buffer)[50] = 0x00; \
(buffer)[51] = 0x00; \
(buffer)[52] = 0x00; \
(buffer)[53] = 0x00; \
(buffer)[54] = 0x00; \
(buffer)[55] = 0x00; \
(buffer)[56] = 0x00; \
(buffer)[57] = 0x00; \
(buffer)[58] = 0x00; \
(buffer)[59] = 0x00; \
(buffer)[60] = 0x00; \
(buffer)[61] = 0x00; \
(buffer)[62] = 0x01; \
(buffer)[63] = 0x78; \
} while (0)
#ifndef WC_LMS_FULL_HASH
/* Hash one full block of data and compute result.
*
* @param [in] sha256 SHA-256 hash object.
* @param [in] data Data to hash.
* @param [out] hash Hash output.
* @return 0 on success.
*/
static WC_INLINE int wc_lms_sha256_192_hash_block(wc_Sha256* sha256,
const byte* data, byte* hash)
{
int ret;
unsigned char output[WC_SHA256_DIGEST_SIZE];
/* Hash the block and reset SHA-256 state. */
ret = wc_Sha256HashBlock(sha256, data, output);
if (ret == 0) {
XMEMCPY(hash, output, WC_SHA256_192_DIGEST_SIZE);
}
return ret;
}
#endif /* !WC_LMS_FULL_HASH */
/* Hash data and compute result.
*
* @param [in] sha256 SHA-256 hash object.
* @param [in] data Data to hash.
* @param [in] len Length of data to hash.
* @param [out] hash Hash output.
* @return 0 on success.
*/
static WC_INLINE int wc_lms_hash_sha256_192(wc_Sha256* sha256, byte* data,
word32 len, byte* hash)
{
int ret;
unsigned char output[WC_SHA256_DIGEST_SIZE];
#ifndef WC_LMS_FULL_HASH
if (len < WC_SHA256_BLOCK_SIZE) {
/* Store data into SHA-256 object's buffer. */
LMS_SHA256_SET_DATA(sha256, data, len);
ret = wc_Sha256Final(sha256, output);
if (ret == 0) {
XMEMCPY(hash, output, WC_SHA256_192_DIGEST_SIZE);
}
}
else if (len < WC_SHA256_BLOCK_SIZE + WC_SHA256_PAD_SIZE) {
ret = wc_Sha256HashBlock(sha256, data, NULL);
if (ret == 0) {
byte* buffer = (byte*)sha256->buffer;
int rem = len - WC_SHA256_BLOCK_SIZE;
XMEMCPY(buffer, data + WC_SHA256_BLOCK_SIZE, rem);
buffer[rem++] = 0x80;
XMEMSET(buffer + rem, 0, WC_SHA256_BLOCK_SIZE - 2 - rem);
buffer[WC_SHA256_BLOCK_SIZE - 2] = (byte)(len >> 5);
buffer[WC_SHA256_BLOCK_SIZE - 1] = (byte)(len << 3);
ret = wc_Sha256HashBlock(sha256, buffer, output);
if (ret == 0) {
XMEMCPY(hash, output, WC_SHA256_192_DIGEST_SIZE);
}
}
}
else {
ret = wc_Sha256Update(sha256, data, len);
if (ret == 0) {
ret = wc_Sha256Final(sha256, output);
if (ret == 0) {
XMEMCPY(hash, output, WC_SHA256_192_DIGEST_SIZE);
}
}
}
#else
ret = wc_Sha256Update(sha256, data, len);
if (ret == 0) {
ret = wc_Sha256Final(sha256, output);
if (ret == 0) {
XMEMCPY(hash, output, WC_SHA256_192_DIGEST_SIZE);
}
}
#endif /* !WC_LMS_FULL_HASH */
return ret;
}
/* Finalize hash.
*
* @param [in] sha256 SHA-256 hash object.
* @param [out] hash Hash output.
* @return 0 on success.
*/
static WC_INLINE int wc_lms_hash_sha256_192_final(wc_Sha256* sha256, byte* hash)
{
#ifndef WC_LMS_FULL_HASH
int ret = 0;
byte* buffer = (byte*)sha256->buffer;
unsigned char output[WC_SHA256_DIGEST_SIZE];
buffer[sha256->buffLen++] = 0x80;
if (sha256->buffLen > WC_SHA256_PAD_SIZE) {
XMEMSET(buffer + sha256->buffLen, 0,
WC_SHA256_BLOCK_SIZE - sha256->buffLen);
ret = wc_Sha256HashBlock(sha256, buffer, NULL);
sha256->buffLen = 0;
}
if (ret == 0) {
XMEMSET(buffer + sha256->buffLen, 0,
WC_SHA256_BLOCK_SIZE - 8 - sha256->buffLen);
sha256->hiLen = (sha256->hiLen << 3) + (sha256->loLen >> 29);
sha256->loLen = sha256->loLen << 3;
#ifdef LITTLE_ENDIAN_ORDER
sha256->buffer[14] = ByteReverseWord32(sha256->hiLen);
sha256->buffer[15] = ByteReverseWord32(sha256->loLen);
#else
sha256->buffer[14] = sha256->hiLen;
sha256->buffer[15] = sha256->loLen;
#endif
ret = wc_Sha256HashBlock(sha256, buffer, output);
if (ret == 0) {
XMEMCPY(hash, output, WC_SHA256_192_DIGEST_SIZE);
}
sha256->buffLen = 0;
sha256->hiLen = 0;
sha256->loLen = 0;
}
return ret;
#else
int ret;
unsigned char output[WC_SHA256_DIGEST_SIZE];
ret = wc_Sha256Final(sha256, output);
if (ret == 0) {
XMEMCPY(hash, output, WC_SHA256_192_DIGEST_SIZE);
}
return ret;
#endif
}
#endif /* WOLFSSL_LMS_SHA256_192 */
/***************************************
* LM-OTS APIs
**************************************/
/* Expand Q to and array of Winternitz width bits values plus checksum.
*
* Supported Winternitz widths: 8, 4, 2, 1.
*
* Algorithm 2: Checksum Calculation
* sum = 0
* for ( i = 0; i < (n*8/w); i = i + 1 ) {
* sum = sum + (2^w - 1) - coef(S, i, w)
* }
* return (sum << ls)
* Section 3.1.3: Strings of w-Bit Elements
* coef(S, i, w) = (2^w - 1) AND
* ( byte(S, floor(i * w / 8)) >>
* (8 - (w * (i % (8 / w)) + w)) )
* Combine coefficient expansion with checksum calculation.
*
* @param [in] q Q array of bytes.
* @param [in] n Number of bytes in Q.
* @param [in] w Winternitz width in bits.
* @param [in] ls Left shift of checksum.
* @param [out] qe Expanded Q with checksum.
* @return 0 on success.
* @return BAD_FUNC_ARG when Winternitz width is not supported.
*/
static WC_INLINE int wc_lmots_q_expand(byte* q, word8 n, word8 w, word8 ls,
byte* qe)
{
int ret = 0;
word16 sum;
unsigned int i;
#ifndef WOLFSSL_WC_LMS_SMALL
switch (w) {
/* Winternitz width of 8. */
case 8:
/* No expansion required, just copy. */
XMEMCPY(qe, q, n);
/* Start sum with all 2^w - 1s and subtract from that. */
sum = 0xff * n;
/* For each byte of the hash. */
for (i = 0; i < n; i++) {
/* Subtract coefficient from sum. */
sum -= q[i];
}
/* Put coefficients of checksum on the end. */
qe[n + 0] = (word8)(sum >> 8);
qe[n + 1] = (word8)(sum );
break;
/* Winternitz width of 4. */
case 4:
sum = 2 * 0xf * n;
/* For each byte of the hash. */
for (i = 0; i < n; i++) {
/* Get coefficient. */
qe[0] = (q[i] >> 4) ;
qe[1] = (q[i] ) & 0xf;
/* Subtract coefficients from sum. */
sum -= qe[0];
sum -= qe[1];
/* Move to next coefficients. */
qe += 2;
}
/* Put coefficients of checksum on the end. */
qe[0] = (word8)((sum >> 8) & 0xf);
qe[1] = (word8)((sum >> 4) & 0xf);
qe[2] = (word8)((sum ) & 0xf);
break;
/* Winternitz width of 2. */
case 2:
sum = 4 * 0x3 * n;
/* For each byte of the hash. */
for (i = 0; i < n; i++) {
/* Get coefficients. */
qe[0] = (q[i] >> 6) ;
qe[1] = (q[i] >> 4) & 0x3;
qe[2] = (q[i] >> 2) & 0x3;
qe[3] = (q[i] ) & 0x3;
/* Subtract coefficients from sum. */
sum -= qe[0];
sum -= qe[1];
sum -= qe[2];
sum -= qe[3];
/* Move to next coefficients. */
qe += 4;
}
/* Put coefficients of checksum on the end. */
qe[0] = (word8)((sum >> 8) & 0x3);
qe[1] = (word8)((sum >> 6) & 0x3);
qe[2] = (word8)((sum >> 4) & 0x3);
qe[3] = (word8)((sum >> 2) & 0x3);
qe[4] = (word8)((sum ) & 0x3);
break;
/* Winternitz width of 1. */
case 1:
sum = 8 * 0x01 * n;
/* For each byte of the hash. */
for (i = 0; i < n; i++) {
/* Get coefficients. */
qe[0] = (q[i] >> 7) ;
qe[1] = (q[i] >> 6) & 0x1;
qe[2] = (q[i] >> 5) & 0x1;
qe[3] = (q[i] >> 4) & 0x1;
qe[4] = (q[i] >> 3) & 0x1;
qe[5] = (q[i] >> 2) & 0x1;
qe[6] = (q[i] >> 1) & 0x1;
qe[7] = (q[i] ) & 0x1;
/* Subtract coefficients from sum. */
sum -= qe[0];
sum -= qe[1];
sum -= qe[2];
sum -= qe[3];
sum -= qe[4];
sum -= qe[5];
sum -= qe[6];
sum -= qe[7];
/* Move to next coefficients. */
qe += 8;
}
/* Put coefficients of checksum on the end. */
#ifdef WOLFSSL_LMS_SHA256_192
if (ls == 7)
#endif
{
qe[0] = (word8)((sum >> 8) );
qe++;
}
qe[0] = (word8)((sum >> 7) & 0x1);
qe[1] = (word8)((sum >> 6) & 0x1);
qe[2] = (word8)((sum >> 5) & 0x1);
qe[3] = (word8)((sum >> 4) & 0x1);
qe[4] = (word8)((sum >> 3) & 0x1);
qe[5] = (word8)((sum >> 2) & 0x1);
qe[6] = (word8)((sum >> 1) & 0x1);
qe[7] = (word8)((sum ) & 0x1);
break;
default:
ret = BAD_FUNC_ARG;
break;
}
(void)ls;
#else
int j;
if ((w != 8) && (w != 4) && (w != 2) && (w != 1)) {
ret = BAD_FUNC_ARG;
}
if (ret == 0) {
/* Start sum with all 2^w - 1s and subtract from that. */
sum = (((word16)1 << w) - 1) * ((n * 8) / w);
/* For each byte of the hash. */
for (i = 0; i < n; i++) {
/* Get next byte. */
byte a = *(q++);
/* For each width bits of byte. */
for (j = 8 - w; j >= 0; j -= w) {
/* Get coefficient. */
*qe = a >> (8 - w);
/* Subtract coefficient from sum. */
sum -= *qe;
/* Move to next coefficient. */
qe++;
/* Remove width bits. */
a <<= w;
}
}
/* Shift sum up as required to pack it on the end of hash. */
sum <<= ls;
/* For each width bit of checksum. */
for (j = 16 - w; j >= ls; j--) {
/* Get coefficient. */
*(qe++) = sum >> (16 - w);
/* Remove width bits. */
sum <<= w;
}
}
#endif /* !WOLFSSL_WC_LMS_SMALL */
return ret;
}
/* Calculate the hash for the message.
*
* Algorithm 3: Generating a One-Time Signature From a Private Key and a
* Message
* ...
* 5. Compute the array y as follows:
* Q = H(I || u32str(q) || u16str(D_MESG) || C || message)
* Algorithm 4b: Computing a Public Key Candidate Kc from a Signature,
* Message, Signature Typecode pubtype, and Identifiers I, q
* ...
* 3. Compute the string Kc as follows:
* Q = H(I || u32str(q) || u16str(D_MESG) || C || message)
*
* @param [in, out] state LMS state.
* @param [in] msg Message to hash.
* @param [in] msgSz Length of message in bytes.
* @param [in] c C or randomizer value.
* @param [out] q Computed Q value.
* @return 0 on success.
*/
static int wc_lmots_msg_hash(LmsState* state, const byte* msg, word32 msgSz,
const byte* c, byte* q)
{
int ret;
byte* buffer = state->buffer;
byte* ip = buffer + LMS_I_LEN + LMS_Q_LEN;
/* I || u32str(q) || u16str(D_MESG) */
c16toa(LMS_D_MESG, ip);
/* H(I || u32str(q) || u16str(D_MESG) || ...) */
ret = wc_lms_hash_first(&state->hash, buffer, LMS_MSG_PRE_LEN);
if (ret == 0) {
/* H(... || C || ...) */
ret = wc_lms_hash_update(&state->hash, c, state->params->hash_len);
}
if (ret == 0) {
/* H(... || message) */
ret = wc_lms_hash_update(&state->hash, msg, msgSz);
}
#ifdef WOLFSSL_LMS_SHA256_192
if ((ret == 0) &&
((state->params->lmOtsType & LMS_HASH_MASK) == LMS_SHA256_192)) {
/* Q = H(...) */
ret = wc_lms_hash_sha256_192_final(&state->hash, q);
}
else
#endif
#ifndef WOLFSSL_NO_LMS_SHA256_256
if (ret == 0) {
/* Q = H(...) */
ret = wc_lms_hash_final(&state->hash, q);
}
else
#endif
{
ret = NOT_COMPILED_IN;
}
return ret;
}
#ifndef WOLFSSL_LMS_VERIFY_ONLY
/* Compute array y, intermediates of public key calculation, for signature.
*
* Verification will perform the remaining iterations of hashing.
*
* Algorithm 3: Generating a One-Time Signature From a Private Key and a
* Message
* ...
* 5. Compute the array y as follows:
* Q = H(I || u32str(q) || u16str(D_MESG) || C || message)
* for ( i = 0; i < p; i = i + 1 ) {
* a = coef(Q || Cksm(Q), i, w)
* tmp = x[i]
* for ( j = 0; j < a; j = j + 1 ) {
* tmp = H(I || u32str(q) || u16str(i) || u8str(j) || tmp)
* }
* y[i] = tmp
* }
* x[i] can be calculated on the fly using pseudo key generation in Appendix A.
* Appendix A, The elements of the LM-OTS private keys are computed as:
* x_q[i] = H(I || u32str(q) || u16str(i) || u8str(0xff) || SEED).
*
* @param [in, out] state LMS state.
* @param [in] seed Seed to hash.
* @param [in] msg Message to sign.
* @param [in] msgSZ Length of message in bytes.
* @param [in] c C or randomizer value to hash.
* @param [out] y Calculated intermediate hashes.
* @return 0 on success.
*/
static int wc_lmots_compute_y_from_seed(LmsState* state, const byte* seed,
const byte* msg, word32 msgSz, const byte* c, byte* y)
{
const LmsParams* params = state->params;
int ret;
word16 i;
byte q[LMS_MAX_NODE_LEN + LMS_CKSM_LEN];
#ifdef WOLFSSL_SMALL_STACK
byte* a = state->a;
#else
byte a[LMS_MAX_P];
#endif /* WOLFSSL_SMALL_STACK */
byte* buffer = state->buffer;
byte* ip = buffer + LMS_I_LEN + LMS_Q_LEN;
byte* jp = ip + LMS_P_LEN;
byte* tmp = jp + LMS_W_LEN;
/* Q = H(I || u32str(q) || u16str(D_MESG) || C || message) */
ret = wc_lmots_msg_hash(state, msg, msgSz, c, q);
if (ret == 0) {
/* Calculate checksum list all coefficients. */
ret = wc_lmots_q_expand(q, (word8)params->hash_len, params->width,
params->ls, a);
}
#ifndef WC_LMS_FULL_HASH
if (ret == 0) {
#ifdef WOLFSSL_LMS_SHA256_192
if ((params->lmOtsType & LMS_HASH_MASK) == LMS_SHA256_192) {
/* Put in padding for final block. */
LMS_SHA256_SET_LEN_47(buffer);
}
else
#endif
{
#ifndef WOLFSSL_NO_LMS_SHA256_256
/* Put in padding for final block. */
LMS_SHA256_SET_LEN_55(buffer);
#endif
}
}
#endif /* !WC_LMS_FULL_HASH */
/* Compute y for each coefficient. */
for (i = 0; (ret == 0) && (i < params->p); i++) {
unsigned int j;
/* tmp = x[i]
* = H(I || u32str(q) || u16str(i) || u8str(0xff) || SEED). */
c16toa(i, ip);
*jp = LMS_D_FIXED;
#ifndef WC_LMS_FULL_HASH
#ifdef WOLFSSL_LMS_SHA256_192
if ((params->lmOtsType & LMS_HASH_MASK) == LMS_SHA256_192) {
XMEMCPY(tmp, seed, WC_SHA256_192_DIGEST_SIZE);
ret = wc_lms_sha256_192_hash_block(&state->hash, buffer, tmp);
}
else
#endif
{
#ifndef WOLFSSL_NO_LMS_SHA256_256
XMEMCPY(tmp, seed, WC_SHA256_DIGEST_SIZE);
ret = wc_lms_hash_block(&state->hash, buffer, tmp);
#else
ret = NOT_COMPILED_IN;
#endif
}
#else
#ifdef WOLFSSL_LMS_SHA256_192
if ((params->lmOtsType & LMS_HASH_MASK) == LMS_SHA256_192) {
XMEMCPY(tmp, seed, WC_SHA256_192_DIGEST_SIZE);
ret = wc_lms_hash_sha256_192(&state->hash, buffer,
LMS_HASH_BUFFER_LEN(WC_SHA256_192_DIGEST_SIZE), tmp);
}
else
#endif
{
#ifndef WOLFSSL_NO_LMS_SHA256_256
XMEMCPY(tmp, seed, WC_SHA256_DIGEST_SIZE);
ret = wc_lms_hash(&state->hash, buffer,
LMS_HASH_BUFFER_LEN(WC_SHA256_DIGEST_SIZE), tmp);
#else
ret = NOT_COMPILED_IN;
#endif
}
#endif /* !WC_LMS_FULL_HASH */
/* Apply the hash function coefficient number of times. */
for (j = 0; (ret == 0) && (j < a[i]); j++) {
/* I || u32str(q) || u16str(i) || u8str(j) || tmp */
*jp = j;
/* tmp = H(I || u32str(q) || u16str(i) || u8str(j) || tmp) */
#ifndef WC_LMS_FULL_HASH
#ifdef WOLFSSL_LMS_SHA256_192
if ((params->lmOtsType & LMS_HASH_MASK) == LMS_SHA256_192) {
ret = wc_lms_sha256_192_hash_block(&state->hash, buffer, tmp);
}
else
#endif
{
#ifndef WOLFSSL_NO_LMS_SHA256_256
ret = wc_lms_hash_block(&state->hash, buffer, tmp);
#else
ret = NOT_COMPILED_IN;
#endif
}
#else
#ifdef WOLFSSL_LMS_SHA256_192
if ((params->lmOtsType & LMS_HASH_MASK) == LMS_SHA256_192) {
ret = wc_lms_hash_sha256_192(&state->hash, buffer,
LMS_HASH_BUFFER_LEN(WC_SHA256_192_DIGEST_SIZE), tmp);
}
else
#endif
{
#ifndef WOLFSSL_NO_LMS_SHA256_256
ret = wc_lms_hash(&state->hash, buffer,
LMS_HASH_BUFFER_LEN(WC_SHA256_DIGEST_SIZE), tmp);
#else
ret = NOT_COMPILED_IN;
#endif
}
#endif /* !WC_LMS_FULL_HASH */
}
if (ret == 0) {
/* y[i] = tmp */
XMEMCPY(y, tmp, params->hash_len);