-
Notifications
You must be signed in to change notification settings - Fork 4
/
fips205.py
743 lines (631 loc) · 27.9 KB
/
fips205.py
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
# fips205.py
# 2023-11-24 Markku-Juhani O. Saarinen < mjos@iki.fi>. See LICENSE
# === FIPS 205 implementation https://doi.org/10.6028/NIST.FIPS.205
# SLH-DSA / Stateless Hash-Based Digital Signature Standard
# test_slhdsa is only used by the unit test in the end
from test_slhdsa import test_slhdsa
# hashes
from Crypto.Hash import SHAKE128, SHAKE256, SHA256, SHA512
# A class for handling Addresses (Section 4.2.)
class ADRS:
# type constants
WOTS_HASH = 0
WOTS_PK = 1
TREE = 2
FORS_TREE = 3
FORS_ROOTS = 4
WOTS_PRF = 5
FORS_PRF = 6
def __init__(self, a=32):
"""Initialize."""
self.a = bytearray(a)
def copy(self):
""" Make a copy of self."""
return ADRS(self.a)
def set_layer_address(self, x):
""" Set layer address."""
self.a[ 0: 4] = x.to_bytes(4, byteorder='big')
def set_tree_address(self, x):
""" Set tree address."""
self.a[ 4:16] = x.to_bytes(12, byteorder='big')
def set_key_pair_address(self, x):
""" Set key pair Address."""
self.a[20:24] = x.to_bytes(4, byteorder='big')
def get_key_pair_address(self):
""" Get key pair Address."""
return int.from_bytes(self.a[20:24], byteorder='big')
def set_tree_height(self, x):
""" Set FORS tree height."""
self.a[24:28] = x.to_bytes(4, byteorder='big')
def set_chain_address(self, x):
""" Set WOTS+ chain address."""
self.a[24:28] = x.to_bytes(4, byteorder='big')
def set_tree_index(self, x):
""" Set FORS tree index."""
self.a[28:32] = x.to_bytes(4, byteorder='big')
def get_tree_index(self):
""" Get FORS tree index."""
return int.from_bytes(self.a[28:32], byteorder='big')
def set_hash_address(self, x):
""" Set WOTS+ hash address."""
self.a[28:32] = x.to_bytes(4, byteorder='big')
def set_type_and_clear(self, t):
""" The member function ADRS.setTypeAndClear(Y) for addresses sets
the type of the ADRS to Y and sets the fnal 12 bytes of the ADRS
to zero."""
self.a[16:20] = t.to_bytes(4, byteorder='big')
for i in range(12):
self.a[20 + i] = 0
def adrs(self):
""" Return the ADRS as bytes."""
return self.a
def adrsc(self):
""" Compressed address ADRDc used with SHA-2."""
return self.a[3:4] + self.a[8 : 16] + self.a[19:20] + self.a[20:32]
# SLH-DSA Implementation
class SLH_DSA:
# initialize
def __init__(self, hashname='SHAKE', n=16, h=66,
d=22, hp=3, a=6, k=33, lg_w=4, m=34,
rbg=None):
self.hashname = hashname
self.n = n
self.h = h
self.d = d
self.hp = hp
self.a = a
self.k = k
self.lg_w = lg_w
self.m = m
# instantiate hash functions
if hashname == 'SHAKE':
self.h_msg = self.shake_h_msg
self.prf = self.shake_prf
self.prf_msg = self.shake_prf_msg
self.h_f = self.shake_f
self.h_h = self.shake_f
self.h_t = self.shake_f
elif hashname == 'SHA2' and self.n == 16:
self.h_msg = self.sha256_h_msg
self.prf = self.sha256_prf
self.prf_msg = self.sha256_prf_msg
self.h_f = self.sha256_f
self.h_h = self.sha256_f
self.h_t = self.sha256_f
elif hashname == 'SHA2' and self.n > 16:
self.h_msg = self.sha512_h_msg
self.prf = self.sha256_prf
self.prf_msg = self.sha512_prf_msg
self.h_f = self.sha256_f
self.h_h = self.sha512_h
self.h_t = self.sha512_h
# equations 5.1 - 5.4
self.w = 2**self.lg_w
self.len1 = (8 * self.n + (self.lg_w - 1)) // self.lg_w
self.len2 = (self.len1 *
(self.w - 1)).bit_length() // self.lg_w + 1
self.len = self.len1 + self.len2
# external parameter sizes
self.pk_sz = 2 * self.n
self.sk_sz = 4 * self.n
self.sig_sz = (1 + self.k*(1 + self.a) + self.h +
self.d * self.len) * self.n
# rbg
self.rbg = rbg
# 10.1. SLH-DSA Using SHAKE
def shake256(self, x, l):
"""SHAKE256(x, l): Internal hook."""
return SHAKE256.new(x).read(l)
def shake_h_msg(self, r, pk_seed, pk_root, m):
return self.shake256(r + pk_seed + pk_root + m, self.m)
def shake_prf(self, pk_seed, sk_seed, adrs):
return self.shake256(pk_seed + adrs.adrs() + sk_seed, self.n)
def shake_prf_msg(self, sk_prf, opt_rand, m):
return self.shake256(sk_prf + opt_rand + m, self.n)
def shake_f(self, pk_seed, adrs, m1):
return self.shake256(pk_seed + adrs.adrs() + m1, self.n)
# Various constructions required for SHA-2 variants.
def sha256(self, x, n=32):
"""Tranc_n(SHA2-256(x))."""
return SHA256.new(x).digest()[0:n]
def sha512(self, x, n=64):
"""Tranc_n(SHA2-512(x))."""
return SHA512.new(x).digest()[0:n]
def mgf(self, hash_f, hash_l, mgf_seed, mask_len):
"""NIST SP 800-56B REV. 2 / The Mask Generation Function (MGF)."""
t = b''
for c in range((mask_len + hash_l - 1) // hash_l):
t += hash_f(mgf_seed + c.to_bytes(4, byteorder='big'))
return t[0:mask_len]
def mgf_sha256(self, mgf_seed, mask_len):
"""MGF1-SHA1-256(mgfSeed, maskLen)."""
return self.mgf(self.sha256, 32, mgf_seed, mask_len)
def mgf_sha512(self, mgf_seed, mask_len):
"""MGF1-SHA1-512(mgfSeed, maskLen)."""
return self.mgf(self.sha512, 64, mgf_seed, mask_len)
def hmac(self, hash_f, hash_l, hash_b, k, text):
"""FIPS PUB 198-1 HMAC."""
if len(k) > hash_b:
k = hash_f(k)
ipad = bytearray(hash_b)
ipad[0:len(k)] = k
opad = bytearray(ipad)
for i in range(hash_b):
ipad[i] ^= 0x36
opad[i] ^= 0x5C
return hash_f(opad + hash_f(ipad + text))
def hmac_sha256(self, k, text, n=32):
"""Trunc_n(HMAC-SHA-256(k, text)): Internal hook."""
return self.hmac(self.sha256, 32, 64, k, text)[0:n]
def hmac_sha512(self, k, text, n=64):
"""Trunc_n(HMAC-SHA-256(k, text)): Internal hook."""
return self.hmac(self.sha512, 64, 128, k, text)[0:n]
# 10.2 SLH-DSA Using SHA2 for Security Category 1
def sha256_h_msg(self, r, pk_seed, pk_root, m):
return self.mgf_sha256( r + pk_seed +
self.sha256(r + pk_seed + pk_root + m), self.m)
def sha256_prf(self, pk_seed, sk_seed, adrs):
return self.sha256(pk_seed + bytes(64 - self.n) +
adrs.adrsc() + sk_seed, self.n)
def sha256_prf_msg(self, sk_prf, opt_rand, m):
return self.hmac_sha256(sk_prf, opt_rand + m, self.n)
def sha256_f(self, pk_seed, adrs, m1):
return self.sha256(pk_seed + bytes(64 - self.n) +
adrs.adrsc() + m1, self.n)
# 10.3 SLH-DSA Using SHA2 for Security Categories 3 and 5
def sha512_h_msg(self, r, pk_seed, pk_root, m):
return self.mgf_sha512( r + pk_seed +
self.sha512(r + pk_seed + pk_root + m), self.m)
def sha512_prf_msg(self, sk_prf, opt_rand, m):
return self.hmac_sha512(sk_prf, opt_rand + m, self.n)
def sha512_h(self, pk_seed, adrs, m2):
return self.sha512(pk_seed + bytes(128 - self.n) +
adrs.adrsc() + m2, self.n)
# --- FIPS 205 Algorithms
def to_int(self, s, n):
""" Algorithm 2: toInt(X, n). Convert a byte string to an integer."""
t = 0
for i in range(n):
t = (t << 8) + int(s[i])
return t
def to_byte(self, x, n):
""" Algorithm 3: toByte(x, n). Convert an integer to a byte string."""
t = x
s = bytearray(n)
for i in range(n):
s[n - 1 - i] = t & 0xFF
t >>= 8
return s
def base_2b(self, s, b, out_len):
""" Algorithm 4: base_2b (X, b, out_len).
Compute the base 2**b representation of X."""
i = 0 # in
c = 0 # bits
t = 0 # total
v = [] # baseb
m = (1 << b) - 1 # mask
for j in range(out_len):
while c < b:
t = (t << 8) + int(s[i])
i += 1
c += 8
c -= b
v += [ (t >> c) & m ]
return v
def chain(self, x, i, s, pk_seed, adrs):
""" Algorithm 5: chain(X, i, s, PK.seed, ADRS).
Chaining function used in WOTS+."""
if i + s >= self.w:
return None
t = x
for j in range(i, i + s):
adrs.set_hash_address(j)
t = self.h_f(pk_seed, adrs, t)
return t
def wots_pkgen(self, sk_seed, pk_seed, adrs):
""" Algorithm 6: wots_PKgen(SK.seed, PK.seed, ADRS).
Generate a WOTS+ public key."""
sk_adrs = adrs.copy()
sk_adrs.set_type_and_clear(ADRS.WOTS_PRF)
sk_adrs.set_key_pair_address(adrs.get_key_pair_address())
tmp = b''
for i in range(self.len):
sk_adrs.set_chain_address(i)
sk = self.prf(pk_seed, sk_seed, sk_adrs)
adrs.set_chain_address(i)
tmp += self.chain(sk, 0, self.w - 1, pk_seed, adrs)
wotspk_adrs = adrs.copy()
wotspk_adrs.set_type_and_clear(ADRS.WOTS_PK)
wotspk_adrs.set_key_pair_address(adrs.get_key_pair_address())
pk = self.h_t(pk_seed, wotspk_adrs, tmp)
return pk
def wots_sign(self, m, sk_seed, pk_seed, adrs):
""" Algorithm 7: wots_sign(M, SK.seed, PK.seed, ADRS).
Generate a WOTS+ signature on an n-byte message."""
csum = 0
msg = self.base_2b(m, self.lg_w, self.len1)
for i in range(self.len1):
csum += self.w - 1 - msg[i]
csum <<= ((8 - ((self.len2 * self.lg_w) % 8)) % 8)
msg += self.base_2b(self.to_byte(csum,
(self.len2 * self.lg_w + 7) // 8), self.lg_w, self.len2)
sk_adrs = adrs.copy()
sk_adrs.set_type_and_clear(ADRS.WOTS_PRF)
sk_adrs.set_key_pair_address(adrs.get_key_pair_address())
sig = b''
for i in range(self.len):
sk_adrs.set_chain_address(i)
sk = self.prf(pk_seed, sk_seed, sk_adrs)
adrs.set_chain_address(i)
sig += self.chain(sk, 0, msg[i], pk_seed, adrs)
return sig
def wots_pk_from_sig(self, sig, m, pk_seed, adrs):
""" Algorithm 8: wots_PKFromSig(sig, M, PK.seed, ADRS).
Compute a WOTS+ public key from a message and its signature."""
csum = 0
msg = self.base_2b(m, self.lg_w, self.len1)
for i in range(self.len1):
csum += self.w - 1 - msg[i]
csum <<= ((8 - ((self.len2 * self.lg_w) % 8)) % 8)
msg += self.base_2b(self.to_byte(csum,
(self.len2 * self.lg_w + 7) // 8), self.lg_w, self.len2)
tmp = b''
for i in range(self.len):
adrs.set_chain_address(i)
tmp += self.chain(sig[i*self.n:(i+1)*self.n],
msg[i], self.w - 1 - msg[i],
pk_seed, adrs)
wots_pk_adrs = adrs.copy()
wots_pk_adrs.set_type_and_clear(ADRS.WOTS_PK)
wots_pk_adrs.set_key_pair_address(adrs.get_key_pair_address())
pk_sig = self.h_t(pk_seed, wots_pk_adrs, tmp)
return pk_sig
def xmss_node(self, sk_seed, i, z, pk_seed, adrs):
""" Algorithm 9: xmss_node(SK.seed, i, z, PK.seed, ADRS).
Compute the root of a Merkle subtree of WOTS+ public keys."""
if z > self.hp or i >= 2**(self.hp - z):
return None
if z == 0:
adrs.set_type_and_clear(ADRS.WOTS_HASH)
adrs.set_key_pair_address(i)
node = self.wots_pkgen(sk_seed, pk_seed, adrs)
else:
lnode = self.xmss_node(sk_seed, 2 * i, z - 1, pk_seed, adrs)
rnode = self.xmss_node(sk_seed, 2 * i + 1, z - 1, pk_seed, adrs)
adrs.set_type_and_clear(ADRS.TREE)
adrs.set_tree_height(z)
adrs.set_tree_index(i)
node = self.h_h(pk_seed, adrs, lnode + rnode)
return node
def xmss_sign(self, m, sk_seed, idx, pk_seed, adrs):
""" Algorithm 10: xmss_sign(M, SK.seed, idx, PK.seed, ADRS).
Generate an XMSS signature."""
auth = b''
for j in range(self.hp):
k = (idx >> j) ^ 1
auth += self.xmss_node(sk_seed, k, j, pk_seed, adrs)
adrs.set_type_and_clear(ADRS.WOTS_HASH)
adrs.set_key_pair_address(idx)
sig = self.wots_sign(m, sk_seed, pk_seed, adrs)
sig_xmss = sig + auth
return sig_xmss
def xmss_pk_from_sig(self, idx, sig_xmss, m, pk_seed, adrs):
""" Algorithm 11: xmss_PKFromSig(idx, SIG_XMSS, M, PK.seed, ADRS).
Compute an XMSS public key from an XMSS signature."""
adrs.set_type_and_clear(ADRS.WOTS_HASH)
adrs.set_key_pair_address(idx)
sig = sig_xmss[0:self.len*self.n]
auth = sig_xmss[self.len*self.n:]
node_0 = self.wots_pk_from_sig(sig, m, pk_seed, adrs)
adrs.set_type_and_clear(ADRS.TREE)
adrs.set_tree_index(idx)
for k in range(self.hp):
adrs.set_tree_height(k + 1)
auth_k = auth[k*self.n:(k+1)*self.n]
if (idx >> k) & 1 == 0:
adrs.set_tree_index(adrs.get_tree_index() // 2)
node_1 = self.h_h(pk_seed, adrs, node_0 + auth_k)
else:
adrs.set_tree_index((adrs.get_tree_index() - 1) // 2)
node_1 = self.h_h(pk_seed, adrs, auth_k + node_0)
node_0 = node_1
return node_0
def ht_sign(self, m, sk_seed, pk_seed, i_tree, i_leaf):
""" Algorithm 12: ht_sign(M, SK.seed, PK.seed, idx_tree, idx_leaf).
Generate a hypertree signature."""
adrs = ADRS()
adrs.set_tree_address(i_tree)
sig_tmp = self.xmss_sign(m, sk_seed, i_leaf, pk_seed, adrs)
sig_ht = sig_tmp
root = self.xmss_pk_from_sig(i_leaf, sig_tmp, m, pk_seed, adrs)
hp_m = ((1 << self.hp) - 1)
for j in range(1, self.d):
i_leaf = i_tree & hp_m
i_tree = i_tree >> self.hp
adrs.set_layer_address(j)
adrs.set_tree_address(i_tree)
sig_tmp = self.xmss_sign(root, sk_seed, i_leaf, pk_seed, adrs)
sig_ht += sig_tmp
if j < self.d - 1:
root = self.xmss_pk_from_sig(i_leaf, sig_tmp, root,
pk_seed, adrs)
return sig_ht
def ht_verify(self, m, sig_ht, pk_seed, i_tree, i_leaf, pk_root):
""" Algorithm 13: ht_verify(M, SIG_HT, PK.seed, idx_tree, idx_leaf,
PK.root). Verify a hypertree signature."""
adrs = ADRS()
adrs.set_tree_address(i_tree)
sig_tmp = sig_ht[0:(self.hp + self.len)*self.n]
node = self.xmss_pk_from_sig(i_leaf, sig_tmp, m, pk_seed, adrs)
hp_m = ((1 << self.hp) - 1)
for j in range(1, self.d):
i_leaf = i_tree & hp_m
i_tree = i_tree >> self.hp
adrs.set_layer_address(j)
adrs.set_tree_address(i_tree)
sig_tmp = sig_ht[j*(self.hp + self.len)*self.n:
(j+1)*(self.hp + self.len)*self.n]
node = self.xmss_pk_from_sig(i_leaf, sig_tmp, node,
pk_seed, adrs)
return node == pk_root
def fors_sk_gen(self, sk_seed, pk_seed, adrs, idx):
""" Algorithm 14: fors_SKgen(SK.seed, PK.seed, ADRS, idx).
Generate a FORS private-key value."""
sk_adrs = adrs.copy()
sk_adrs.set_type_and_clear(ADRS.FORS_PRF)
sk_adrs.set_key_pair_address(adrs.get_key_pair_address())
sk_adrs.set_tree_index(idx)
return self.prf(pk_seed, sk_seed, sk_adrs)
def fors_node(self, sk_seed, i, z, pk_seed, adrs):
""" Algorithm 15: fors_node(SK.seed, i, z, PK.seed, ADRS).
Compute the root of a Merkle subtree of FORS public values."""
if z > self.a or i >= (self.k << (self.a - z)):
return None
if z == 0:
sk = self.fors_sk_gen(sk_seed, pk_seed, adrs, i)
adrs.set_tree_height(0)
adrs.set_tree_index(i)
node = self.h_f(pk_seed, adrs, sk)
else:
lnode = self.fors_node(sk_seed, 2 * i, z - 1, pk_seed, adrs)
rnode = self.fors_node(sk_seed, 2 * i + 1, z - 1, pk_seed, adrs)
adrs.set_tree_height(z)
adrs.set_tree_index(i)
node = self.h_h(pk_seed, adrs, lnode + rnode)
return node
def fors_sign(self, md, sk_seed, pk_seed, adrs):
""" Algorithm 16: fors_sign(md, SK.seed, PK.seed, ADRS).
Generate a FORS signature."""
sig_fors = b''
indices = self.base_2b(md, self.a, self.k)
for i in range(self.k):
sig_fors += self.fors_sk_gen(sk_seed, pk_seed, adrs,
(i << self.a) + indices[i])
for j in range(self.a):
s = (indices[i] >> j) ^ 1
sig_fors += self.fors_node(sk_seed,
(i << (self.a - j)) + s, j,
pk_seed, adrs)
return sig_fors
def fors_pk_from_sig(self, sig_fors, md, pk_seed, adrs):
""" Algorithm 17: fors_pkFromSig(SIG_FORS, md, PK.seed, ADRS).
Compute a FORS public key from a FORS signature."""
def get_sk(sig_fors, i):
return sig_fors[i*(self.a+1)*self.n:(i*(self.a+1)+1)*self.n]
def get_auth(sig_fors, i):
return sig_fors[(i*(self.a+1)+1)*self.n:(i+1)*(self.a+1)*self.n]
indices = self.base_2b(md, self.a, self.k)
root = b''
for i in range(self.k):
sk = get_sk(sig_fors, i)
adrs.set_tree_height(0)
adrs.set_tree_index((i << self.a) + indices[i])
node_0 = self.h_f(pk_seed, adrs, sk)
auth = get_auth(sig_fors, i)
for j in range(self.a):
auth_j = auth[j*self.n:(j+1)*self.n]
adrs.set_tree_height(j + 1)
if (indices[i] >> j) & 1 == 0:
adrs.set_tree_index(adrs.get_tree_index() // 2)
node_1 = self.h_h(pk_seed, adrs, node_0 + auth_j)
else:
adrs.set_tree_index((adrs.get_tree_index() - 1) // 2)
node_1 = self.h_h(pk_seed, adrs, auth_j + node_0)
node_0 = node_1
root += node_0
fors_pk_adrs = adrs.copy()
fors_pk_adrs.set_type_and_clear(ADRS.FORS_ROOTS)
fors_pk_adrs.set_key_pair_address(adrs.get_key_pair_address())
pk = self.h_t(pk_seed, fors_pk_adrs, root)
return pk
def slh_keygen_internal(self, sk_seed, sk_prf, pk_seed):
""" Algorithm 18: slh_keygen_internal()."""
# The behavior is different if one performs three distinct
adrs = ADRS()
adrs.set_layer_address(self.d - 1)
pk_root = self.xmss_node(sk_seed, 0, self.hp, pk_seed, adrs)
sk = sk_seed + sk_prf + pk_seed + pk_root
pk = pk_seed + pk_root
return (pk, sk) # Alg 17 has (sk, pk)
def split_digest(self, digest):
""" Helper: Lines 11-16 of Alg 18 / Lines 10-15 of Alg 19."""
ka1 = (self.k * self.a + 7) // 8
md = digest[0:ka1]
hd = self.h // self.d
hhd = self.h - hd
ka2 = ka1 + ((hhd + 7) // 8)
i_tree = self.to_int( digest[ka1:ka2], (hhd + 7) // 8) % (2 ** hhd)
ka3 = ka2 + ((hd + 7) // 8)
i_leaf = self.to_int( digest[ka2:ka3], (hd + 7) // 8) % (2 ** hd)
return (md, i_tree, i_leaf)
def slh_sign_internal(self, m, sk, addrnd):
""" Algorithm 19: slh_sign_internal(M, SK). """
adrs = ADRS()
sk_seed = sk[ 0: self.n]
sk_prf = sk[ self.n:2*self.n]
pk_seed = sk[2*self.n:3*self.n]
pk_root = sk[3*self.n:]
if addrnd == None:
addrnd = pk_seed
r = self.prf_msg(sk_prf, addrnd, m)
sig = r
digest = self.h_msg(r, pk_seed, pk_root, m)
(md, i_tree, i_leaf) = self.split_digest(digest)
adrs.set_tree_address(i_tree)
adrs.set_type_and_clear(ADRS.FORS_TREE)
adrs.set_key_pair_address(i_leaf)
sig_fors = self.fors_sign(md, sk_seed, pk_seed, adrs)
sig += sig_fors
pk_fors = self.fors_pk_from_sig(sig_fors, md, pk_seed, adrs)
sig_ht = self.ht_sign(pk_fors, sk_seed, pk_seed, i_tree, i_leaf)
sig += sig_ht
return sig
def slh_verify_internal(self, m, sig, pk):
""" Algorithm 20: slh_verify_internal(M, SIG, PK)."""
if len(sig) != self.sig_sz or len(pk) != self.pk_sz:
return False
pk_seed = pk[:self.n]
pk_root = pk[self.n:]
adrs = ADRS()
r = sig[0:self.n]
sig_fors = sig[self.n:(1+self.k*(1+self.a))*self.n]
sig_ht = sig[(1 + self.k*(1 + self.a))*self.n:]
digest = self.h_msg(r, pk_seed, pk_root, m)
(md, i_tree, i_leaf) = self.split_digest(digest)
adrs.set_tree_address(i_tree)
adrs.set_type_and_clear(ADRS.FORS_TREE)
adrs.set_key_pair_address(i_leaf)
pk_fors = self.fors_pk_from_sig(sig_fors, md, pk_seed, adrs)
return self.ht_verify(pk_fors, sig_ht, pk_seed,
i_tree, i_leaf, pk_root)
# XXX Note 2024-11-09: Not covered by test vectors.
def slh_keygen(self):
""" Algorithm 21, Algorithm 21 slh_keygen()."""
if param != None:
self.__init__(param)
sk_seed = self.rbg(self.n)
sk_prf = self.rbg(self.n)
pk_seed = self.rbg(self.n)
return slh.slh_keygen_internal(sk_seed, sk_prf, pk_seed)
# XXX Note 2024-11-09: Not covered by test vectors.
def slh_sign(self, m, ctx, sk, addrnd=None, param=None):
""" Algorithm 22, slh_sign(M, ctx, SK)."""
if param != None:
self.__init__(param)
if len(ctx) > 255:
return None
mp = ( self.integer_to_bytes(0, 1) +
self.integer_to_bytes(len(ctx), 1) + ctx + m )
sig = self.slh_sign_internal(mp, sk, addrnd)
return sig
# XXX Note 2024-11-09: Not covered by test vectors.
def hash_slh_sign(self, m, ctx, ph, sk, addrnd=None, param=None):
""" Algorithm 23, hash_slh_sign(M, ctx, PH, SK). """
if param != None:
self.__init__(param)
if len(ctx) > 255:
return None
if ph == 'SHA-256':
oid = bytes([ 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
0x04, 0x02, 0x01])
phm = SHA256.new(m).digest()
elif ph == 'SHA-512':
oid = bytes([ 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
0x04, 0x02, 0x03])
phm = SHA512.new(m).digest()
elif ph == 'SHAKE128':
oid = bytes([ 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
0x04, 0x02, 0x0B])
phm = SHAKE128.new(m).read(256 // 8)
elif ph == 'SHAKE256':
oid = bytes([ 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
0x04, 0x02, 0x0C])
phm = SHAKE256.new(m).read(512 // 8)
else:
return None
mp = ( self.integer_to_bytes(1, 1) +
self.integer_to_bytes(len(ctx), 1) +
ctx + oid + phm )
sig = self.slh_sign_internal(sk, mp, rnd)
return sig
# XXX Note 2024-11-09: Not covered by test vectors.
def slh_verify(self, m, sig, ctx, pk, param=None):
""" Algorithm 24, slh_verify(M, SIG, ctx, PK)."""
if param != None:
self.__init__(param)
if len(ctx) > 255:
return False
mp = ( self.integer_to_bytes(0, 1) +
self.integer_to_bytes(len(ctx), 1) + ctx + m)
return self.slh_verify_internal(mp, sig, pk)
# XXX Note 2024-11-09: Not covered by test vectors.
def hash_ml_dsa_verify(self, pk, m, sig, ctx, ph, param=None):
""" Algorithm 25, hash_slh_verify(M, SIG, ctx, PH, PK)."""
if param != None:
self.__init__(param)
if len(ctx) > 255:
return None
if ph == 'SHA-256':
oid = bytes([ 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
0x04, 0x02, 0x01])
phm = SHA256.new(m).digest()
elif ph == 'SHA-512':
oid = bytes([ 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
0x04, 0x02, 0x03])
phm = SHA512.new(m).digest()
elif ph == 'SHAKE128':
oid = bytes([ 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
0x04, 0x02, 0x0B])
phm = SHAKE128.new(m).read(256 // 8)
elif ph == 'SHAKE256':
oid = bytes([ 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
0x04, 0x02, 0x0C])
phm = SHAKE256.new(m).read(512 // 8)
else:
return None
mp = ( self.integer_to_bytes(1, 1) +
self.integer_to_bytes(len(ctx), 1) +
ctx + oid + phm )
return self.slh_verify_internal(mp, sig, pk)
# Section 11: Table 2. SLH-DSA parameter sets
SLH_DSA_PARAMS = {
'SLH-DSA-SHA2-128s': SLH_DSA(hashname='SHA2',
n=16, h=63, d=7, hp=9, a=12, k=14, lg_w=4, m=30),
'SLH-DSA-SHAKE-128s': SLH_DSA(hashname='SHAKE',
n=16, h=63, d=7, hp=9, a=12, k=14, lg_w=4, m=30),
'SLH-DSA-SHA2-128f': SLH_DSA(hashname='SHA2',
n=16, h=66, d=22, hp=3, a=6, k=33, lg_w=4, m=34),
'SLH-DSA-SHAKE-128f': SLH_DSA(hashname='SHAKE',
n=16, h=66, d=22, hp=3, a=6, k=33, lg_w=4, m=34),
'SLH-DSA-SHA2-192s': SLH_DSA(hashname='SHA2',
n=24, h=63, d=7, hp=9, a=14, k=17, lg_w=4, m=39),
'SLH-DSA-SHAKE-192s': SLH_DSA(hashname='SHAKE',
n=24, h=63, d=7, hp=9, a=14, k=17, lg_w=4, m=39),
'SLH-DSA-SHA2-192f': SLH_DSA(hashname='SHA2',
n=24, h=66, d=22, hp=3, a=8, k=33, lg_w=4, m=42),
'SLH-DSA-SHAKE-192f': SLH_DSA(hashname='SHAKE',
n=24, h=66, d=22, hp=3, a=8, k=33, lg_w=4, m=42),
'SLH-DSA-SHA2-256s': SLH_DSA(hashname='SHA2',
n=32, h=64, d=8, hp=8, a=14, k=22, lg_w=4, m=47),
'SLH-DSA-SHAKE-256s': SLH_DSA(hashname='SHAKE',
n=32, h=64, d=8, hp=8, a=14, k=22, lg_w=4, m=47),
'SLH-DSA-SHA2-256f': SLH_DSA(hashname='SHA2',
n=32, h=68, d=17, hp=4, a=9, k=35, lg_w=4, m=49),
'SLH-DSA-SHAKE-256f': SLH_DSA(hashname='SHAKE',
n=32, h=68, d=17, hp=4, a=9, k=35, lg_w=4, m=49)
}
def param_keygen( sk_seed, sk_prf, pk_seed, param):
slh = SLH_DSA_PARAMS[param]
return slh.slh_keygen_internal(sk_seed, sk_prf, pk_seed)
def param_sign( msg, sk, addrnd, param):
slh = SLH_DSA_PARAMS[param]
return slh.slh_sign_internal(msg, sk, addrnd)
def param_verify( msg, sig, pk, param):
slh = SLH_DSA_PARAMS[param]
return slh.slh_verify_internal( msg, sig, pk )
# run the test on these functions
if __name__ == '__main__':
test_slhdsa(param_keygen,
param_sign,
param_verify,
'(fips205.py)')