-
Notifications
You must be signed in to change notification settings - Fork 4
/
hss.py
executable file
·1292 lines (1127 loc) · 43.6 KB
/
hss.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
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
#!/usr/bin/python
"""
Copyright (c) 2016 Cisco Systems, Inc.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
Neither the name of the Cisco Systems, Inc. nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
OF THE POSSIBILITY OF SUCH DAMAGE.
hss.py
Reference implementation for Leighton-Micali Hash Based Signatures
(HBS) and Hierarchical Signature System (HSS), as per the Internet
Draft draft-mcgrew-hash-sigs-05.txt.
"""
import sys
import binascii
import struct
import os.path
from Crypto.Hash import SHA256
from Crypto import Random
# error codes
#
err_private_key_exhausted = 'error: attempted overuse of private key'
err_unknown_typecode = 'error: unrecognized typecode'
err_bad_length = 'error: parameter has wrong length'
err_bad_value = 'error: parameter has inadmissable value'
err_list = [
err_private_key_exhausted,
err_unknown_typecode,
err_bad_length,
err_bad_value
]
def err_handle(err):
if err.args[0] in err_list:
print str(err.args)
else:
raise
# return codes
#
VALID = 0
INVALID = 1
# informative return codes for debugging
#
INVALID_LMS_TYPE_ERR = 2
INVALID_LMS_PUB_ERR = 3
INVALID_HSS_LEVEL_ERR = 4
INVALID_WITH_REASON = 5
retcode_dict = {
INVALID_LMS_TYPE_ERR: "error: LMS typecode mismatch",
INVALID_LMS_PUB_ERR: "error: LMS public key mismatch",
INVALID_HSS_LEVEL_ERR: "error: HSS level mismatch",
INVALID_WITH_REASON: "error: exception"
}
def retcode_get_string(x):
if x in retcode_dict:
return retcode_dict[x]
else:
return "unknown error"
# Diversification constants
#
D_ITER = chr(0x00) # in the iterations of the LM-OTS algorithms
D_PBLC = chr(0x01) # when computing the hash of all of the iterates in the LM-OTS algorithm
D_MESG = chr(0x02) # when computing the hash of the message in the LMOTS algorithms
D_LEAF = chr(0x03) # when computing the hash of the leaf of an LMS tree
D_INTR = chr(0x04) # when computing the hash of an interior node of an LMS tree
D_PRG = chr(0x05) # when computing LMS private keys pseudorandomly
# LMOTS typecodes and parameters
#
lmots_sha256_n32_w1 = 0x00000001
lmots_sha256_n32_w2 = 0x00000002
lmots_sha256_n32_w4 = 0x00000003
lmots_sha256_n32_w8 = 0x00000004
lmots_params = {
# n p w ls
lmots_sha256_n32_w1: (32, 265, 1, 7),
lmots_sha256_n32_w2: (32, 133, 2, 6),
lmots_sha256_n32_w4: (32, 67, 4, 4),
lmots_sha256_n32_w8: (32, 34, 8, 0)
}
lmots_name = {
lmots_sha256_n32_w1: "LMOTS_SHA256_N32_W1",
lmots_sha256_n32_w2: "LMOTS_SHA256_N32_W2",
lmots_sha256_n32_w4: "LMOTS_SHA256_N32_W4",
lmots_sha256_n32_w8: "LMOTS_SHA256_N32_W8"
}
# LMS typecodes and parameters
#
lms_sha256_m32_h5 = 0x00000005
lms_sha256_m32_h10 = 0x00000006
lms_sha256_m32_h15 = 0x00000007
lms_sha256_m32_h20 = 0x00000008
lms_sha256_m32_h25 = 0x00000009
lms_params = {
# m, h, LenI
lms_sha256_m32_h5: (32, 5, 64),
lms_sha256_m32_h10: (32, 10, 64),
lms_sha256_m32_h15: (32, 15, 64),
lms_sha256_m32_h20: (32, 20, 64),
lms_sha256_m32_h25: (32, 25, 64)
}
lms_name = {
lms_sha256_m32_h5: "LMS_SHA256_M32_H5",
lms_sha256_m32_h10: "LMS_SHA256_M32_H10",
lms_sha256_m32_h15: "LMS_SHA256_M32_H15",
lms_sha256_m32_h20: "LMS_SHA256_M32_H20",
lms_sha256_m32_h25: "LMS_SHA256_M32_H25"
}
# ***************************************************************
# |
# Utilities |
# |
# ***************************************************************
def H(x):
"""
SHA256 hash function
:param x: input that will be hashed
:return: list of 32 bytes, hash digest
"""
h = SHA256.new()
h.update(x)
return h.digest()
def u32str(x):
"""
Integer to 4-byte string conversion
:param x: integer that will be converted
:return: 4-byte string representing integer
"""
return struct.pack('>I', x)
def u16str(x):
"""
Integer to 2-byte string conversion
:param x: integer that will be converted
:return: 2-byte string representing integer
"""
return struct.pack('>H', x)
def u8str(x):
"""
Integer to 1-byte string conversion
:param x: integer that will be converted
:return: 1-byte representing integer
"""
return chr(x)
def deserialize_u32(buffer):
if (len(buffer) != 4):
raise ValueError(err_bad_length, str(len(buffer)))
return int(buffer.encode('hex'), 16)
def typecode_peek(buffer):
if (len(buffer) != 4):
raise ValueError(err_bad_length, str(len(buffer)))
return int(buffer.encode('hex'), 16)
def serialize_array(array):
result = ""
for e in array:
result = result + e
return result
def string_to_hex(x):
return binascii.hexlify(bytearray(x))
class PrintUtl(object):
margin = 12
width = 16
@classmethod
def print_hex(cls, LHS, RHS, comment=""):
s = RHS
LHS = LHS + (" " * (cls.margin - len(LHS)))
if len(s) < cls.width and comment != "":
comment = " " * 2 * (cls.width - len(s)) + " # " + comment
print LHS + string_to_hex(s[0:cls.width]) + comment
s = s[cls.width:]
LHS = " " * cls.margin
while len(s) is not 0:
print LHS + string_to_hex(s[0:cls.width])
s = s[cls.width:]
@classmethod
def print_line(cls):
print "-" * (cls.margin + 2*cls.width)
# ***************************************************************
# |
# LM-OTS functionality |
# |
# ***************************************************************
entropySource = Random.new()
class LmotsSignature():
"""
Leighton-Micali One Time Signature
"""
def __init__(self, C, y, typecode=lmots_sha256_n32_w8):
self.C = C
self.y = y
self.type = typecode
def serialize(self):
return u32str(self.type) + self.C + serialize_array(self.y)
@classmethod
def deserialize(cls, buffer):
lmots_type = typecode_peek(buffer[0:4])
if lmots_type in lmots_params:
n, p, w, ls = lmots_params[lmots_type]
else:
raise ValueError(err_unknown_typecode, str(lmots_type))
if (len(buffer) != cls.bytes(lmots_type)):
raise ValueError(err_bad_length)
C = buffer[4:n+4]
y = list()
pos = n+4
for i in xrange(0, p):
y.append(buffer[pos:pos+n])
pos = pos + n
return cls(C, y, lmots_type)
@classmethod
def bytes(cls, lmots_type):
n, p, w, ls = lmots_params[lmots_type]
return 4 + n*(p+1)
def print_hex(self):
PrintUtl.print_line()
print "LMOTS signature"
PrintUtl.print_hex("LMOTS type", u32str(self.type), lmots_name[self.type])
PrintUtl.print_hex("C", self.C)
for i, e in enumerate(self.y):
PrintUtl.print_hex("y[" + str(i) + "]", e)
PrintUtl.print_line()
class LmotsPrivateKey:
"""
Leighton-Micali One Time Signature Private Key
"""
# Algorithm 0: Generating an LMOTS Private Key
#
def __init__(self, S=None, SEED=None, lmots_type=lmots_sha256_n32_w8):
n, p, w, ls = lmots_params[lmots_type]
if S is None:
self.S = entropySource.read(n)
else:
self.S = S
self.x = list()
if SEED is None:
for i in xrange(0, p):
self.x.append(entropySource.read(n))
else:
for i in xrange(0, p):
self.x.append(H(self.S + SEED + u16str(i+1) + D_PRG))
self.type = lmots_type
self._signatures_remaining = 1
def num_signatures_remaining(self):
return self._signatures_remaining
# Algorithm 1: Generating a Public Key From a Private Key
#
def get_public_key(self):
n, p, w, ls = lmots_params[self.type]
hash = SHA256.new()
hash.update(self.S)
for i, x in enumerate(self.x):
tmp = x
for j in xrange(0, 2**w - 1):
tmp = H(self.S + tmp + u16str(i) + u8str(j) + D_ITER)
hash.update(tmp)
hash.update(D_PBLC)
return LmotsPublicKey(self.S, hash.digest(), self.type)
# Algorithm 3: Generating a Signature From a Private Key and a Message
#
def sign(self, message):
if self._signatures_remaining != 1:
raise ValueError(err_private_key_exhausted)
n, p, w, ls = lmots_params[self.type]
C = entropySource.read(n)
hashQ = H(self.S + C + message + D_MESG)
V = hashQ + checksum(hashQ, w, ls)
y = list()
for i, x in enumerate(self.x):
tmp = x
for j in xrange(0, coef(V, i, w)):
tmp = H(self.S + tmp + u16str(i) + u8str(j) + D_ITER)
y.append(tmp)
self._signatures_remaining = 0
return LmotsSignature(C, y, self.type).serialize()
def print_hex(self):
PrintUtl.print_line()
print "LMOTS private key"
PrintUtl.print_hex("LMOTS type", u32str(self.type), lmots_name[self.type])
PrintUtl.print_hex("S", self.S)
for i, x in enumerate(self.x):
PrintUtl.print_hex("x[" + str(i) + "]", x)
PrintUtl.print_line()
@classmethod
def get_param_list(cls):
param_list = list()
for t in lmots_params.keys():
param_list.append({'lmots_type':t})
return param_list
@classmethod
def get_public_key_class(cls):
return LmotsPublicKey
class LmotsPublicKey:
"""
Leighton-Micali One Time Signature Public Key
"""
def __init__(self, S, K, lmots_type):
self.S = S
self.K = K
self.type = lmots_type
# Algorithm 4: Verifying a Signature and Message Using a Public Key
#
def verify(self, message, sig):
if self.K == lmots_sig_to_pub(sig, self.S, self.type, message):
return VALID
else:
return INVALID
def serialize(self):
return u32str(self.type) + self.S + self.K
@classmethod
def deserialize(cls, buffer):
lmots_type = typecode_peek(buffer[0:4])
if lmots_type in lmots_params:
n, p, w, ls = lmots_params[lmots_type]
else:
raise ValueError(err_unknown_typecode)
if len(buffer) != 4+2*n:
raise ValueError(err_bad_length)
S = buffer[4:4+n]
K = buffer[4+n:4+2*n]
return cls(S, K, lmots_type)
def print_hex(self):
PrintUtl.print_line()
print "LMOTS public key"
PrintUtl.print_hex("LMOTS type", u32str(self.type), lmots_name[self.type])
PrintUtl.print_hex("S", self.S)
PrintUtl.print_hex("K", self.K)
PrintUtl.print_line()
# Algorithm 2: Merkle Checksum Calculation
#
def coef(S, i, w):
return (2**w - 1) & (ord(S[i*w/8]) >> (8 - (w*(i%(8/w)) + w)))
def checksum(x, w, ls):
sum = 0
num_coefs = len(x)*(8/w)
for i in xrange(0, num_coefs):
sum = sum + (2**w - 1) - coef(x, i, w)
return u16str(sum << ls)
def lmots_sig_to_pub(sig, S, lmots_type, message):
signature = LmotsSignature.deserialize(sig)
if (signature.type != lmots_type):
raise ValueError(err_unknown_typecode)
n, p, w, ls = lmots_params[lmots_type]
hashQ = H(S + signature.C + message + D_MESG)
V = hashQ + checksum(hashQ, w, ls)
hash = SHA256.new()
hash.update(S)
for i, y in enumerate(signature.y):
tmp = y
for j in xrange(coef(V, i, w), 2**w - 1):
tmp = H(S + tmp + u16str(i) + u8str(j) + D_ITER)
hash.update(tmp)
hash.update(D_PBLC)
return hash.digest()
# ***************************************************************
# |
# LMS N-time signatures functions |
# |
# ***************************************************************
def serialize_lms_sig(typecode, q, lmots_sig, path):
return u32str(q) + lmots_sig + u32str(typecode) + serialize_array(path)
def deserialize_lms_sig(buffer):
q = deserialize_u32(buffer[0:4])
# print "q: " + str(q)
lmots_type = typecode_peek(buffer[4:8])
# print "lmots_type: " + str(lmots_type)
if lmots_type in lmots_params:
pos = 4 + LmotsSignature.bytes(lmots_type)
else:
raise ValueError(err_unknown_typecode, str(lmots_type))
lmots_sig = buffer[4:pos]
lms_type = typecode_peek(buffer[pos:pos+4])
if lms_type in lms_params:
m, h, LenI = lms_params[lms_type]
else:
raise ValueError(err_unknown_typecode, str(lms_type))
if (q >= 2**h):
raise ValueError(err_bad_value)
pos = pos + 4
path = list()
for i in xrange(0, h):
path.append(buffer[pos:pos+m])
pos = pos + m
# PrintUtl.print_hex("buffer tail", buffer[pos:])
return lms_type, q, lmots_sig, path
def parse_lms_sig(buffer):
lmots_type = typecode_peek(buffer[4:8])
if lmots_type in lmots_params:
pos = 4 + LmotsSignature.bytes(lmots_type)
else:
raise ValueError(err_unknown_typecode)
lms_type = typecode_peek(buffer[pos:pos+4])
if lms_type in lms_params:
m, h, LenI = lms_params[lms_type]
else:
raise ValueError(err_unknown_typecode)
pos = pos + 4 + h*m
return buffer[0:pos], buffer[pos:]
def print_lms_sig(sig):
PrintUtl.print_line()
print "LMS signature"
lms_type, q, lmots_sig, path = deserialize_lms_sig(sig)
PrintUtl.print_hex("q", u32str(q))
LmotsSignature.deserialize(lmots_sig).print_hex()
PrintUtl.print_hex("LMS type", u32str(lms_type), lms_name[lms_type])
for i, e in enumerate(path):
PrintUtl.print_hex("path[" + str(i) + "]", e)
class LmsPrivateKey(object):
"""
Leighton-Micali Signature Private Key
"""
def __init__(self, lms_type=lms_sha256_m32_h10, lmots_type=lmots_sha256_n32_w8,
SEED=None, I=None, qinit=0, nodes=None, pub=None):
n, p, w, ls = lmots_params[lmots_type]
m, h, LenI = lms_params[lms_type]
self.lms_type = lms_type
self.lmots_type = lmots_type
self.priv = list()
self.pub = list()
if I is None:
self.I = entropySource.read(LenI)
else:
if (len(I) != LenI):
raise ValueError(err_bad_length, str(len(I)))
self.I = I
if SEED is None:
SEED = entropySource.read(n)
else:
if (len(SEED) != n):
raise ValueError(err_bad_length, str(len(SEED)))
self.SEED = SEED
if nodes is None:
for q in xrange(0, 2**h):
S = self.I + u32str(q)
ots_priv = LmotsPrivateKey(S=S, SEED=SEED, lmots_type=lmots_type)
ots_pub = ots_priv.get_public_key()
self.priv.append(ots_priv)
self.pub.append(ots_pub)
else:
self.nodes = nodes
self.pub = pub
self.leaf_num = qinit
self.nodes = {}
self.lms_pub_value = self.T(1)
def get_path(self, node_num):
path = list()
while node_num > 1:
if (node_num % 2):
path.append(self.nodes[node_num - 1])
else:
path.append(self.nodes[node_num + 1])
node_num = node_num/2
return path
def get_next_ots_priv_key(self):
return self.priv[self.leaf_num]
def sign(self, message):
m, h, LenI = lms_params[self.lms_type]
if (self.leaf_num >= 2**h):
raise ValueError(err_private_key_exhausted)
ots_sig = self.get_next_ots_priv_key().sign(message)
path = self.get_path(self.leaf_num + 2**h)
leaf_num = self.leaf_num
self.leaf_num = self.leaf_num + 1
return serialize_lms_sig(self.lms_type, leaf_num, ots_sig, path)
# Algorithm for computing root and other nodes (alternative to Algorithm 6)
#
def T(self, r):
m, h, LenI = lms_params[self.lms_type]
if (r >= 2**h):
self.nodes[r] = H(self.I + self.pub[r - 2**h].K + u32str(r) + D_LEAF)
return self.nodes[r]
else:
self.nodes[r] = H(self.I + self.T(2*r) + self.T(2*r+1) + u32str(r) + D_INTR)
return self.nodes[r]
def num_signatures_remaining(self):
m, h, LenI = lms_params[self.lms_type]
return 2**h - self.leaf_num
def is_exhausted(self):
return (0 == self.num_signatures_remaining())
def max_signatures(self):
m, h, LenI = lms_params[self.lms_type]
return 2**h
def print_hex(self):
PrintUtl.print_line()
print "LMS private key"
PrintUtl.print_hex("LMS type", u32str(self.lms_type), lms_name[self.lms_type])
PrintUtl.print_hex("LMOTS_type", u32str(self.lmots_type), lmots_name[self.lmots_type])
PrintUtl.print_hex("I", self.I)
PrintUtl.print_hex("SEED", self.SEED)
PrintUtl.print_hex("q", u32str(self.leaf_num))
PrintUtl.print_hex("pub", self.lms_pub_value)
def get_public_key(self):
return LmsPublicKey(self.I, self.lms_pub_value, self.lms_type, self.lmots_type)
@classmethod
def get_param_list(cls):
param_list = list()
for x in lmots_params.keys():
for y in lms_params.keys():
param_list.append({'lmots_type':x, 'lms_type':y})
return param_list
@classmethod
def get_public_key_class(cls):
return LmsPublicKey
def serialize(self):
return u32str(self.lms_type) + u32str(self.lmots_type) + self.SEED + self.I + u32str(self.leaf_num)
@classmethod
def deserialize(cls, buffer):
lms_type = deserialize_u32(buffer[0:4])
lmots_type = deserialize_u32(buffer[4:8])
n, p, w, ls = lmots_params[lmots_type]
m, h, LenI = lms_params[lms_type]
SEED = buffer[8:8+n]
I = buffer[8+n:8+n+LenI]
q = deserialize_u32(buffer[8+n+LenI:8+n+LenI+4])
return cls(lms_type, lmots_type, SEED, I, q)
@classmethod
def deserialize_print_hex(cls, buffer):
PrintUtl.print_line()
print "LMS private key"
lms_type = deserialize_u32(buffer[0:4])
lmots_type = deserialize_u32(buffer[4:8])
n, p, w, ls = lmots_params[lmots_type]
m, h, LenI = lms_params[lms_type]
SEED = buffer[8:8+n]
I = buffer[8+n:8+n+LenI]
q = deserialize_u32(buffer[8+n+LenI:8+n+LenI+4])
PrintUtl.print_hex("lms_type", u32str(lms_type))
PrintUtl.print_hex("lmots_type", u32str(lmots_type))
PrintUtl.print_hex("SEED", SEED)
PrintUtl.print_hex("I", I)
PrintUtl.print_hex("leaf_num", u32str(q))
PrintUtl.print_line()
@classmethod
def parse(cls, buffer):
lms_type = deserialize_u32(buffer[0:4])
lmots_type = deserialize_u32(buffer[4:8])
n, p, w, ls = lmots_params[lmots_type]
m, h, LenI = lms_params[lms_type]
return buffer[:8+n+LenI], buffer[8+n+LenI:]
class LmsPrivateKeyNonvolatile(LmsPrivateKey):
def get_next_ots_priv_key(self):
return self.priv[self.leaf_num]
class LmsPublicKey(object):
"""
Leighton-Micali Signature Public Key
"""
def __init__(self, I, value, lms_type, lmots_type):
self.I = I
self.value = value
self.lms_type = lms_type
self.lmots_type = lmots_type
def verify(self, message, sig):
m, h, LenI = lms_params[self.lms_type]
lms_type, q, lmots_sig, path = deserialize_lms_sig(sig)
node_num = q + 2**h
if lms_type != self.lms_type:
return INVALID_LMS_TYPE_ERR
pathvalue = iter(path)
tmp = lmots_sig_to_pub(lmots_sig, self.I + u32str(q), self.lmots_type, message)
tmp = H(self.I + tmp + u32str(node_num) + D_LEAF)
while node_num > 1:
if (node_num % 2):
tmp = H(self.I + pathvalue.next() + tmp + u32str(node_num/2) + D_INTR)
else:
tmp = H(self.I + tmp + pathvalue.next() + u32str(node_num/2) + D_INTR)
node_num = node_num/2
if (tmp == self.value):
return VALID
else:
return INVALID_LMS_PUB_ERR
def serialize(self):
return u32str(self.lms_type) + u32str(self.lmots_type) + self.I + self.value
@classmethod
def parse(cls, buffer):
lms_type = typecode_peek(buffer[0:4])
if lms_type in lms_params:
m, h, LenI = lms_params[lms_type]
else:
raise ValueError(err_unknown_typecode)
return buffer[0:4+4+LenI+m], buffer[4+4+LenI+m:]
@classmethod
def deserialize(cls, buffer):
lms_type = typecode_peek(buffer[0:4])
if lms_type in lms_params:
m, h, LenI = lms_params[lms_type]
else:
raise ValueError(err_unknown_typecode)
lmots_type = typecode_peek(buffer[4:8])
if lmots_type not in lmots_params:
raise ValueError(err_unknown_typecode)
I = buffer[8:8+LenI]
K = buffer[8+LenI:8+LenI+m]
return cls(I, K, lms_type, lmots_type)
def print_hex(self):
PrintUtl.print_line()
print "LMS public key"
PrintUtl.print_hex("LMS type", u32str(self.lms_type), lms_name[self.lms_type])
PrintUtl.print_hex("LMOTS_type", u32str(self.lmots_type), lmots_name[self.lmots_type])
PrintUtl.print_hex("I", self.I)
PrintUtl.print_hex("K", self.value)
PrintUtl.print_line()
# ***************************************************************
# |
# Hierarchical Signature System (HSS) |
# |
# HSS signature format: |
# (l=number of signed_public_keys) |
# array of l-2 signed_public_keys |
# signature |
# ***************************************************************
def serialize_hss_sig(levels_minus_one, publist, siglist, msg_sig):
result = u32str(levels_minus_one)
for i in xrange(0, levels_minus_one):
result = result + siglist[i]
result = result + publist[i+1].serialize()
result = result + msg_sig
return result
def deserialize_hss_sig(buffer):
hss_max_levels = 8
levels = deserialize_u32(buffer[0:4]) + 1
if (levels > hss_max_levels):
raise ValueError(err_bad_value)
siglist = list()
publist = list()
tmp = buffer[4:]
for i in xrange(0, levels-1):
lms_sig, tmp = parse_lms_sig(tmp)
siglist.append(lms_sig)
lms_pub, tmp = LmsPublicKey.parse(tmp)
publist.append(lms_pub)
msg_sig = tmp
return levels, publist, siglist, msg_sig
def print_hss_sig(sig):
levels, publist, siglist, lms_sig = deserialize_hss_sig(sig)
PrintUtl.print_line()
print "HSS signature"
PrintUtl.print_hex("Nspk", u32str(levels-1))
for i in xrange(0, levels-1):
print "sig[" + str(i) + "]: "
print_lms_sig(siglist[i])
print "pub[" + str(i) + "]: "
LmsPublicKey.deserialize(publist[i]).print_hex()
print "final_signature: "
print_lms_sig(lms_sig)
class HssPrivateKey(object):
"""
Hierarchical Signature System Private Key
"""
def __init__(self, levels=2, lms_type=lms_sha256_m32_h5, lmots_type=lmots_sha256_n32_w8, prv0=None):
self.levels = levels
self.prv = list()
self.pub = list()
self.sig = list()
if prv0 is None:
prv0 = LmsPrivateKey(lms_type=lms_type, lmots_type=lmots_type)
self.prv.append(prv0)
self.pub.append(self.prv[0].get_public_key())
for i in xrange(1, self.levels):
self.prv.append(LmsPrivateKey(lms_type=lms_type, lmots_type=lmots_type))
self.pub.append(self.prv[-1].get_public_key())
self.sig.append(self.prv[-2].sign(self.pub[-1].serialize()))
def sign(self, message):
while (self.prv[-1].is_exhausted()):
print "level " + str(len(self.prv)) + " is exhausted"
if (len(self.prv) == 1):
raise ValueError(err_private_key_exhausted)
self.prv.pop()
self.pub.pop()
self.sig.pop()
while (len(self.prv) < self.levels):
print "refreshing level " + str(len(self.prv))
self.prv.append(LmsPrivateKey(lms_type=self.prv[0].lms_type, lmots_type=self.prv[0].lmots_type))
self.pub.append(self.prv[-1].get_public_key())
self.sig.append(self.prv[-2].sign(self.pub[-1].serialize()))
# sign message
lms_sig = self.prv[-1].sign(message)
return serialize_hss_sig(self.levels-1, self.pub, self.sig, lms_sig)
def get_public_key(self):
return HssPublicKey(self.prv[0].get_public_key(), self.levels)
def num_signatures_remaining(self):
unused = self.prv[0].num_signatures_remaining()
for i in xrange(1,self.levels):
unused = unused * self.prv[i].max_signatures() + self.prv[i].num_signatures_remaining()
return unused
def serialize(self):
return u32str(self.levels) + self.prv[0].serialize()
@classmethod
def deserialize(cls, buffer):
levels = deserialize_u32(buffer[0:4])
prv = LmsPrivateKey.deserialize(buffer[4:])
return cls(levels, lms_type=prv.lms_type, lmots_type=prv.lmots_type, prv0=prv)
@classmethod
def deserialize_print_hex(cls, buffer):
"""
Parse all of the data elements of an HSS private key out of the string buffer.
Does not initialize an hss_private_key (as that initialization computes at least one
LMS public/private keypair, which can take a long time)
:param buffer: string representing HSS private key
:return:
"""
PrintUtl.print_line()
print "HSS private key"
levels = deserialize_u32(buffer[0:4])
PrintUtl.print_hex("levels", u32str(levels))
print "prv[0]:"
LmsPrivateKey.deserialize_print_hex(buffer[4:])
PrintUtl.print_line()
def print_hex(self):
PrintUtl.print_line()
print "HSS private key"
PrintUtl.print_hex("levels", u32str(self.levels))
for prv in self.prv:
prv.print_hex()
PrintUtl.print_line()
@classmethod
def get_param_list(cls):
param_list = list()
for x in [ lmots_sha256_n32_w1 ]: # lmots_params.keys():
for y in [ lms_sha256_m32_h5 ]: # lms_params.keys():
for l in [2,3]:
param_list.append({'lmots_type':x, 'lms_type':y, 'levels':l})
return param_list
@classmethod
def get_public_key_class(cls):
return HssPublicKey
class HssPublicKey(object):
"""
Hierarchical Signature System Public Key
"""
def __init__(self, rootpub, levels):
self.pub1 = rootpub
self.levels = levels
def verify(self, message, sig):
try:
levels, publist, siglist, lms_sig = deserialize_hss_sig(sig)
if levels != self.levels:
return INVALID_HSS_LEVEL_ERR
# verify the chain of signed public keys
key = self.pub1
for i in xrange(0, self.levels-1):
sig = siglist[i]
msg = publist[i]
result = key.verify(msg, sig)
if (result != VALID):
return result
key = LmsPublicKey.deserialize(msg)
return key.verify(message, lms_sig)
except ValueError as err:
if err.args[0] in err_list:
return INVALID_WITH_REASON
def serialize(self):
return u32str(self.levels) + self.pub1.serialize()
@classmethod
def deserialize(cls, buffer):
levels = deserialize_u32(buffer[0:4])
rootpub = LmsPublicKey.deserialize(buffer[4:])
return cls(rootpub, levels)
def print_hex(self):
PrintUtl.print_line()
print "HSS public key"
PrintUtl.print_hex("levels", u32str(self.levels))
self.pub1.print_hex()
PrintUtl.print_line()
# ***************************************************************
# |
# Test Functions |
# |
# ***************************************************************
def checksum_test():
for typecode in [2]:
n, p, w, ls = lmots_params[typecode]
for j in xrange(0, n):
x = ""
for i in xrange(0,n):
if i == j:
x = x + chr(0)
else:
x = x + chr(0)
y = x + checksum(x, w, ls)
print "w: " + str(w) + "\tp: " + str(p) + "\tcksm: " + string_to_hex(checksum(x, w, ls))
print "x + checksum(x): "
print_as_coefs(y,w,p)
print ""
def print_as_coefs(x, w, p):
num_coefs = len(x)*(8/w)
if (p > num_coefs):
raise ValueError(err_bad_length)
for i in xrange(0, p):
print str(coef(x, i, w))
print "\n"
# Message used in tests
#
testmessage = "Hello, world!"
class byte_flip_mangler:
def __init__(self, value):
self.value = value
self.i = 0
def __iter__(self):
return self
def next(self):
if self.i < len(self.value):
i = self.i
self.i += 1
tmp = entropySource.read(1)
while tmp == self.value[i]:
tmp = entropySource.read(1)
return self.value[:i] + tmp + self.value[i+1:]
else:
raise StopIteration()
class byte_snip_mangler:
def __init__(self, value):
self.value = value
self.i = 0
def __iter__(self):
return self
def next(self):
if self.i < len(self.value):
i = self.i
self.i += 1
return self.value[:i] + self.value[i+1:]
else:
raise StopIteration()
class mangler:
def __init__(self, value):
self.byte_flip = byte_flip_mangler(value)
self.byte_snip = byte_snip_mangler(value)
def __iter__(self):
return self
def next(self):
try:
return self.byte_flip.next()
except StopIteration:
return self.byte_snip.next()
def ntimesig_test(private_key_class, verbose=False):
paramlist = private_key_class.get_param_list()
for param in paramlist:
ntimesig_test_param(private_key_class, param, verbose)
def ntimesig_test_param(private_key_class, param, verbose=False):
"""
Unit test for N-time signatures
:param param: dictionary containing private key parameters
:param verbose: boolean that determines verbosity of output
:return:
"""
print "N-time signature test"
public_key_class = private_key_class.get_public_key_class()
private_key = private_key_class(**param)
public_key_buffer = private_key.get_public_key().serialize()