pyhsslms 2.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
pyhsslms/__init__.py ADDED
@@ -0,0 +1,60 @@
1
+ # https://www.python.org/dev/peps/pep-0396/
2
+ __version__ = '2.0.1'
3
+
4
+ from .pyhsslms import lmots_sha256_n32_w1
5
+ from .pyhsslms import lmots_sha256_n32_w2
6
+ from .pyhsslms import lmots_sha256_n32_w4
7
+ from .pyhsslms import lmots_sha256_n32_w8
8
+ from .pyhsslms import lmots_sha256_n24_w1
9
+ from .pyhsslms import lmots_sha256_n24_w2
10
+ from .pyhsslms import lmots_sha256_n24_w4
11
+ from .pyhsslms import lmots_sha256_n24_w8
12
+ from .pyhsslms import lmots_shake_n32_w1
13
+ from .pyhsslms import lmots_shake_n32_w2
14
+ from .pyhsslms import lmots_shake_n32_w4
15
+ from .pyhsslms import lmots_shake_n32_w8
16
+ from .pyhsslms import lmots_shake_n24_w1
17
+ from .pyhsslms import lmots_shake_n24_w2
18
+ from .pyhsslms import lmots_shake_n24_w4
19
+ from .pyhsslms import lmots_shake_n24_w8
20
+
21
+ from .pyhsslms import lms_sha256_m32_h5
22
+ from .pyhsslms import lms_sha256_m32_h10
23
+ from .pyhsslms import lms_sha256_m32_h15
24
+ from .pyhsslms import lms_sha256_m32_h20
25
+ from .pyhsslms import lms_sha256_m32_h25
26
+ from .pyhsslms import lms_sha256_m24_h5
27
+ from .pyhsslms import lms_sha256_m24_h10
28
+ from .pyhsslms import lms_sha256_m24_h15
29
+ from .pyhsslms import lms_sha256_m24_h20
30
+ from .pyhsslms import lms_sha256_m24_h25
31
+ from .pyhsslms import lms_shake_m32_h5
32
+ from .pyhsslms import lms_shake_m32_h10
33
+ from .pyhsslms import lms_shake_m32_h15
34
+ from .pyhsslms import lms_shake_m32_h20
35
+ from .pyhsslms import lms_shake_m32_h25
36
+ from .pyhsslms import lms_shake_m24_h5
37
+ from .pyhsslms import lms_shake_m24_h10
38
+ from .pyhsslms import lms_shake_m24_h15
39
+ from .pyhsslms import lms_shake_m24_h20
40
+ from .pyhsslms import lms_shake_m24_h25
41
+
42
+ from .pyhsslms import coef
43
+ from .pyhsslms import checksum
44
+ from .pyhsslms import serialize_list
45
+
46
+ from .pyhsslms import LmotsSignature
47
+ from .pyhsslms import LmotsPrivateKey
48
+ from .pyhsslms import LmotsPublicKey
49
+
50
+ from .pyhsslms import LmsSignature
51
+ from .pyhsslms import LmsPrivateKey
52
+ from .pyhsslms import LmsPublicKey
53
+
54
+ from .pyhsslms import HssSignature
55
+ from .pyhsslms import HssPrivateKey
56
+ from .pyhsslms import HssPublicKey
57
+
58
+ from .pyhsslms import HssLmsSignature
59
+ from .pyhsslms import HssLmsPrivateKey
60
+ from .pyhsslms import HssLmsPublicKey
pyhsslms/compat.py ADDED
@@ -0,0 +1,94 @@
1
+ # compat.py
2
+ #
3
+ # For compatibility between Python2 and Python3 within the routines
4
+ # for HSS/LMS Hash-based Signatures as defined in RFC 8554.
5
+ #
6
+ #
7
+ # Copyright (c) 2020-2026, Vigil Security, LLC
8
+ # All rights reserved.
9
+ #
10
+ # Redistribution and use, with or without modification, are permitted
11
+ # provided that the following conditions are met:
12
+ #
13
+ # (1) Redistributions must retain the above copyright notice, this
14
+ # list of conditions, and the following disclaimer.
15
+ #
16
+ # (2) Redistributions in binary form must reproduce the above
17
+ # copyright notice, this list of conditions and the following
18
+ # disclaimer in the documentation and/or other materials provided
19
+ # with the distribution.
20
+ #
21
+ # (3) Neither the name of the Vigil Security, LLC nor the names of the
22
+ # contributors to this code may be used to endorse or promote any
23
+ # products derived from this software without specific prior written
24
+ # permission.
25
+ #
26
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27
+ # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28
+ # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
29
+ # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
30
+ # COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31
+ # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
32
+ # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
33
+ # OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) REGARDLESS OF THE
34
+ # CAUSE AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35
+ # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
36
+ # WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37
+ # POSSIBILITY OF SUCH DAMAGE.
38
+
39
+ from sys import version_info
40
+
41
+ if version_info[0] == 3 and version_info[1] < 6:
42
+ from Cryptodome.Hash import SHAKE256
43
+
44
+ class crypto_hash_shake256():
45
+ """
46
+ Put a wrapper around Crypto.Hash.SHAKE256 with an interface similar
47
+ to hashlib for use with older versions of Python.
48
+ """
49
+ def __init__(self):
50
+ self.name = 'shake_256'
51
+ self.handle = SHAKE256.new()
52
+
53
+ def update(self, buf):
54
+ self.handle.update(buf)
55
+
56
+ def digest(self, rvlen):
57
+ return self.handle.read(rvlen)
58
+
59
+
60
+ if version_info[0] <= 2:
61
+ raise RuntimeError("Unsupported python version")
62
+
63
+ elif version_info[0] == 3 and version_info[1] < 6:
64
+ import os
65
+
66
+ u32 = lambda i: i.to_bytes(4, byteorder='big', signed=False)
67
+ u16 = lambda i: i.to_bytes(2, byteorder='big', signed=False)
68
+ u8 = lambda i: i.to_bytes(1, byteorder='big', signed=False)
69
+ fromHex = bytes.fromhex
70
+ toHex = lambda x: x.hex()
71
+ toBytes = lambda x: x.encode()
72
+ randBytes = os.urandom
73
+ int32 = lambda x: int.from_bytes(x, byteorder='big')
74
+ charNum = lambda x: x
75
+ shake256 = crypto_hash_shake256
76
+ NoFileError = FileNotFoundError
77
+ FoundFileError = FileExistsError
78
+
79
+ else:
80
+ import hashlib
81
+ from secrets import token_bytes as random_bytes
82
+
83
+ u32 = lambda i: i.to_bytes(4, byteorder='big', signed=False)
84
+ u16 = lambda i: i.to_bytes(2, byteorder='big', signed=False)
85
+ u8 = lambda i: i.to_bytes(1, byteorder='big', signed=False)
86
+ fromHex = bytes.fromhex
87
+ toHex = lambda x: x.hex()
88
+ toBytes = lambda x: x.encode()
89
+ randBytes = random_bytes
90
+ int32 = lambda x: int.from_bytes(x, byteorder='big')
91
+ charNum = lambda x: x
92
+ shake256 = hashlib.shake_256
93
+ NoFileError = FileNotFoundError
94
+ FoundFileError = FileExistsError
pyhsslms/hsslms.py ADDED
@@ -0,0 +1,263 @@
1
+ #!/usr/bin/env python
2
+
3
+ # hsslms.py
4
+ #
5
+ # This provides a command line interface for the pyhsslms.py
6
+ # implementation of HSS/LMS Hash-based Signatures as defined
7
+ # in RFC 8554.
8
+ #
9
+ #
10
+ # Copyright (c) 2020-2026, Vigil Security, LLC
11
+ # All rights reserved.
12
+ #
13
+ # Redistribution and use, with or without modification, are permitted
14
+ # provided that the following conditions are met:
15
+ #
16
+ # (1) Redistributions must retain the above copyright notice, this
17
+ # list of conditions, and the following disclaimer.
18
+ #
19
+ # (2) Redistributions in binary form must reproduce the above
20
+ # copyright notice, this list of conditions and the following
21
+ # disclaimer in the documentation and/or other materials provided
22
+ # with the distribution.
23
+ #
24
+ # (3) Neither the name of the Vigil Security, LLC nor the names of the
25
+ # contributors to this code may be used to endorse or promote any
26
+ # products derived from this software without specific prior written
27
+ # permission.
28
+ #
29
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30
+ # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31
+ # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
32
+ # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
33
+ # COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
34
+ # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
35
+ # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
36
+ # OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) REGARDLESS OF THE
37
+ # CAUSE AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38
+ # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
39
+ # WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40
+ # POSSIBILITY OF SUCH DAMAGE.
41
+
42
+ import sys
43
+ import os.path
44
+ import argparse
45
+ import pyhsslms
46
+ from .__init__ import __version__ as VERSION
47
+
48
+
49
+ def usage(name):
50
+ """
51
+ Display usage information and then exit.
52
+ """
53
+ cmd_name = os.path.basename(name)
54
+ print("commands:")
55
+ print(cmd_name + " genkey <keyname> [<genparms>]")
56
+ print(" creates <keyname>.prv and <keyname>.pub")
57
+ print(" ")
58
+ print(cmd_name + " sign <keyname> <filename>")
59
+ print(" updates <keyname>.prv and makes the signature in <filename>.sig")
60
+ print(" ")
61
+ print(cmd_name + " verify <keyname> <filename>")
62
+ print(" verifies the signature in <filename>.sig with <keyname>.pub")
63
+ print(" ")
64
+ print(cmd_name + " showprv <keyname>")
65
+ print(" display <keyname>.prv")
66
+ print(" ")
67
+ print(cmd_name + " showpub <keyname>")
68
+ print(" display <keyname>.pub")
69
+ print(" ")
70
+ print(cmd_name + " showsig <filename>")
71
+ print(" display <filename>.sig")
72
+ print(" ")
73
+ print("optional <genparms> for the genkey command:")
74
+ print(" -l LEVELS, --levels LEVELS")
75
+ print(" Number of levels in HSS heirarchy")
76
+ print(" -s LMS_TYPE, --lms LMS_TYPE")
77
+ print(" Height of the LMS trees")
78
+ print(" -w LMOTS_TYPE, --lmots LMOTS_TYPE")
79
+ print(" Winternitz number")
80
+ print(" -a HASH_ALG, --alg HASH_ALG")
81
+ print(" Hash algorithm (sha256 or shake)")
82
+ print(" -t TRUNC, --trunc TRUNC")
83
+ print(" Hash algorithm truncation size")
84
+ print(" ")
85
+ print("optional command arguments:")
86
+ print(" -h, --help")
87
+ print(" Provides this information")
88
+ print(" -v, --version")
89
+ print(" Provids the program version number")
90
+ sys.exit(1)
91
+
92
+
93
+ def main():
94
+ """
95
+ Command line interface for pyhsslms.py.
96
+ """
97
+ cmds = ['genkey', 'keygen', 'sign', 'verify', \
98
+ 'showprv', 'showpub', 'showsig', \
99
+ '--version', '-v', 'version', '--help', '-h', 'help']
100
+
101
+ if len(sys.argv) < 2 or sys.argv[1] not in cmds:
102
+ print("error: first argument must be a command")
103
+ usage(sys.argv[0])
104
+ sys.exit(1)
105
+
106
+ if sys.argv[1] == 'help' or '--help' in sys.argv or '-h' in sys.argv:
107
+ usage(sys.argv[0])
108
+ sys.exit(1)
109
+
110
+ if sys.argv[1] == 'version' or '--version' in sys.argv or '-v' in sys.argv:
111
+ print(os.path.basename(sys.argv[0]) + " " + VERSION)
112
+ sys.exit(1)
113
+
114
+ if sys.argv[1] in ['genkey', 'keygen']:
115
+ if len(sys.argv) < 3:
116
+ print("error: second argument must be a keyname")
117
+ usage(sys.argv[0])
118
+ sys.exit(1)
119
+
120
+ keyname = sys.argv[2]
121
+ levels = 2
122
+ lms_type = pyhsslms.lms_sha256_m32_h5
123
+ lmots_type = pyhsslms.lmots_sha256_n32_w8
124
+ if len(sys.argv) > 3:
125
+ parser = argparse.ArgumentParser()
126
+ parser.add_argument('-l', '--levels', dest='levels', default=2,
127
+ type=int, choices=[1, 2, 3, 4, 5, 6, 7, 8],
128
+ metavar='LEVELS', help='Number of levels in HSS heirarchy')
129
+ parser.add_argument("-s", "--lms", dest='lms', default=5,
130
+ type=int, choices=[5, 10, 15, 20, 25],
131
+ metavar='LMS_TYPE', help='Height of the LMS trees')
132
+ parser.add_argument('-w', '--lmots', dest='lmots', default=8,
133
+ type=int, choices=[1, 2, 4, 8],
134
+ metavar='LMOTS_TYPE', help='Winternitz number')
135
+ parser.add_argument('-a', '--alg', dest='alg', default='sha256',
136
+ type=str, choices=['sha256', 'shake'],
137
+ metavar='HASH_ALG', help='Hash algorithm (sha256 or shake)')
138
+ parser.add_argument('-t', '--trunc', dest='trunc', default='32',
139
+ type=int, choices=[32, 24],
140
+ metavar='TRUNC', help='Hash algorithm truncation size')
141
+ args = parser.parse_args(sys.argv[3:])
142
+
143
+ levels = args.levels
144
+ if args.alg == 'sha256':
145
+ if args.trunc == 32:
146
+ if args.lms == 5: lms_type = pyhsslms.lms_sha256_m32_h5
147
+ if args.lms == 10: lms_type = pyhsslms.lms_sha256_m32_h10
148
+ if args.lms == 15: lms_type = pyhsslms.lms_sha256_m32_h15
149
+ if args.lms == 20: lms_type = pyhsslms.lms_sha256_m32_h20
150
+ if args.lms == 25: lms_type = pyhsslms.lms_sha256_m32_h25
151
+ if args.lmots == 1: lmots_type = pyhsslms.lmots_sha256_n32_w1
152
+ if args.lmots == 2: lmots_type = pyhsslms.lmots_sha256_n32_w2
153
+ if args.lmots == 4: lmots_type = pyhsslms.lmots_sha256_n32_w4
154
+ if args.lmots == 8: lmots_type = pyhsslms.lmots_sha256_n32_w8
155
+ else: # args.trunc == 24
156
+ if args.lms == 5: lms_type = pyhsslms.lms_sha256_m24_h5
157
+ if args.lms == 10: lms_type = pyhsslms.lms_sha256_m24_h10
158
+ if args.lms == 15: lms_type = pyhsslms.lms_sha256_m24_h15
159
+ if args.lms == 20: lms_type = pyhsslms.lms_sha256_m24_h20
160
+ if args.lms == 25: lms_type = pyhsslms.lms_sha256_m24_h25
161
+ if args.lmots == 1: lmots_type = pyhsslms.lmots_sha256_n24_w1
162
+ if args.lmots == 2: lmots_type = pyhsslms.lmots_sha256_n24_w2
163
+ if args.lmots == 4: lmots_type = pyhsslms.lmots_sha256_n24_w4
164
+ if args.lmots == 8: lmots_type = pyhsslms.lmots_sha256_n24_w8
165
+ else: # args.alg == 'shake'
166
+ if args.trunc == 32:
167
+ if args.lms == 5: lms_type = pyhsslms.lms_shake_m32_h5
168
+ if args.lms == 10: lms_type = pyhsslms.lms_shake_m32_h10
169
+ if args.lms == 15: lms_type = pyhsslms.lms_shake_m32_h15
170
+ if args.lms == 20: lms_type = pyhsslms.lms_shake_m32_h20
171
+ if args.lms == 25: lms_type = pyhsslms.lms_shake_m32_h25
172
+ if args.lmots == 1: lmots_type = pyhsslms.lmots_shake_n32_w1
173
+ if args.lmots == 2: lmots_type = pyhsslms.lmots_shake_n32_w2
174
+ if args.lmots == 4: lmots_type = pyhsslms.lmots_shake_n32_w4
175
+ if args.lmots == 8: lmots_type = pyhsslms.lmots_shake_n32_w8
176
+ else: # args.trunc == 24
177
+ if args.lms == 5: lms_type = pyhsslms.lms_shake_m24_h5
178
+ if args.lms == 10: lms_type = pyhsslms.lms_shake_m24_h10
179
+ if args.lms == 15: lms_type = pyhsslms.lms_shake_m24_h15
180
+ if args.lms == 20: lms_type = pyhsslms.lms_shake_m24_h20
181
+ if args.lms == 25: lms_type = pyhsslms.lms_shake_m24_h25
182
+ if args.lmots == 1: lmots_type = pyhsslms.lmots_shake_n24_w1
183
+ if args.lmots == 2: lmots_type = pyhsslms.lmots_shake_n24_w2
184
+ if args.lmots == 4: lmots_type = pyhsslms.lmots_shake_n24_w4
185
+ if args.lmots == 8: lmots_type = pyhsslms.lmots_shake_n24_w8
186
+
187
+ pyhsslms.HssLmsPrivateKey.genkey(keyname, levels=levels,
188
+ lms_type=lms_type, lmots_type=lmots_type)
189
+
190
+ if sys.argv[1] == 'sign':
191
+ if len(sys.argv) < 3:
192
+ print("error: second argument must be a keyname")
193
+ usage(sys.argv[0])
194
+ sys.exit(1)
195
+
196
+ if len(sys.argv) < 4:
197
+ print("error: third argument must be a file name")
198
+ usage(sys.argv[0])
199
+ sys.exit(1)
200
+
201
+ keyname = sys.argv[2]
202
+ filename = sys.argv[3]
203
+ print("Signing " + filename + " ...")
204
+ prv = pyhsslms.HssLmsPrivateKey(keyname)
205
+ if prv.signFile(filename):
206
+ print(" ... Success. Signature saved in " + filename + ".sig")
207
+ else:
208
+ print(" ... Failed!")
209
+
210
+ if sys.argv[1] == 'verify':
211
+ if len(sys.argv) < 3:
212
+ print("error: second argument must be a keyname")
213
+ usage(sys.argv[0])
214
+ sys.exit(1)
215
+
216
+ if len(sys.argv) < 4:
217
+ print("error: third argument must be a file name")
218
+ usage(sys.argv[0])
219
+ sys.exit(1)
220
+
221
+ keyname = sys.argv[2]
222
+ filename = sys.argv[3]
223
+ pub = pyhsslms.HssLmsPublicKey(keyname)
224
+ if pub.verifyFile(filename):
225
+ print("Signature in " + filename + ".sig is valid.")
226
+ else:
227
+ print("Signature verification failed!")
228
+
229
+ if sys.argv[1] == 'showprv':
230
+ if len(sys.argv) < 3:
231
+ print("error: second argument must be a keyname")
232
+ usage(sys.argv[0])
233
+ sys.exit(1)
234
+
235
+ keyname = sys.argv[2]
236
+ prv = pyhsslms.HssLmsPrivateKey(keyname)
237
+ print("Private Key: " + keyname + ".prv")
238
+ print(prv.hss_prv.prettyPrint())
239
+
240
+ if sys.argv[1] == 'showpub':
241
+ if len(sys.argv) < 3:
242
+ print("error: second argument must be a keyname")
243
+ usage(sys.argv[0])
244
+ sys.exit(1)
245
+
246
+ keyname = sys.argv[2]
247
+ pub = pyhsslms.HssLmsPublicKey(keyname)
248
+ print("Public Key: " + keyname + ".pub")
249
+ print(pub.hss_pub.prettyPrint())
250
+
251
+ if sys.argv[1] == 'showsig':
252
+ if len(sys.argv) < 3:
253
+ print("error: second argument must be a file name")
254
+ usage(sys.argv[0])
255
+ sys.exit(1)
256
+
257
+ filename = sys.argv[2]
258
+ sig = pyhsslms.HssLmsSignature(filename)
259
+ print("Signature: " + filename + ".sig")
260
+ print(sig.hss_sig.prettyPrint())
261
+
262
+ if __name__ == "__main__":
263
+ main()