-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.py
225 lines (172 loc) · 24.9 KB
/
test.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
# -*- coding: utf-8 -*-
# This file is copyright (c) 2023 by the AUTHORS under the LICENSE
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
import binascii
import mprsa
# The typing module is helpful during development, but isn't supported by MicroPython, so wrap import in try/except
try:
import typing
except:
pass
# If True, debug messages will be printed
DEBUG = False
# Define some key data. All these keys have the same "guts", but are represented here in various formats and structures.
# Because these keys are publicly available, they are ONLY FOR TESTING. Don't use them for anything but testing!
PRIVATE_PKCS1_DER = b'0\x82\x04\xa3\x02\x01\x00\x02\x82\x01\x01\x00\xaflJ\x13\xb7\xeeO\xf5U\xd8\xb9;\x81\xf0\xa4\x03\x8cNM\xc1WY\xba\x13\'\xa6F\xc7\\\xd6\x08\xff\x1d\xd6\x969\x99\x83\xf1\xfd\xd4\xd4\x80;T\x8d\xa6\x06!\x12r\xe8o1`\xb6o\x8fm\x9acy\x7f\r\xd7\xe0-\xf98\x06\xbfdj\xd7\xb11)\x8aG@\xbd\x85PV\r\xb0\xa4\xe9\x13\x8a\xf5\xda\x84^\xec\xc6\x9a\x95\xe1}\xc6,\xff\xd7K\xd8\xdf\x82\xa6\xd8\xecKs\xdc\x06<l\x95\xc0I\x85C:\xd22\xd5\xfb]\xcd\xa3\xdeC\x07\r_\xd4\xf0\x84ot\r}\xfcqT\x11\x18/;\xf5\xdb\x18\x1a}(\xda\n\x8f\xaec\xf0T\x80\xa6\xe6Ss\x8e\xe9mp\xfb\xcc\xfc\xd7\xbb~\x9f]f\xb9\xfa\x1b\x08zr\xc7\x97\xfd\xc7\xb3\xba\xea\x01\x1cO\xc6\x13\xe8[X\x10\xf4\x98U\xbcO\xa9{\xde\x05\x96\x95\x01G\xb4\xb8\xa9ow\xbc\x99\x13\x96@\x7f"\x87,\xdaS\xf3\x96\xb2\xff\'\xc0\xe8\xee\x01\xf6\x1c,\x94\xcd\x84\x97\x98\x1e\xdcR\x8b\xe7x\xd5\x15\x02\x03\x01\x00\x01\x02\x82\x01\x00*\x03\xf9\x0b\x17\x80\xf5\xf88\x1f@ [[8a\\+\xce\x0eJ~de\x9d\x97\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x02\x82\x01\x01\x00\xaflJ\x13\xb7\xeeO\xf5U\xd8\xb9;\x81\xf0\xa4\x03\x8cNM\xc1WY\xba\x13\'\xa6F\xc7\\\xd6\x08\xff\x1d\xd6\x969\x99\x83\xf1\xfd\xd4\xd4\x80;T\x8d\xa6\x06!\x12r\xe8o1`\xb6o\x8fm\x9acy\x7f\r\xd7\xe0-\xf98\x06\xbfdj\xd7\xb11)\x8aG@\xbd\x85PV\r\xb0\xa4\xe9\x13\x8a\xf5\xda\x84^\xec\xc6\x9a\x95\xe1}\xc6,\xff\xd7K\xd8\xdf\x82\xa6\xd8\xecKs\xdc\x06<l\x95\xc0I\x85C:\xd22\xd5\xfb]\xcd\xa3\xdeC\x07\r_\xd4\xf0\x84ot\r}\xfcqT\x11\x18/;\xf5\xdb\x18\x1a}(\xda\n\x8f\xaec\xf0T\x80\xa6\xe6Ss\x8e\xe9mp\xfb\xcc\xfc\xd7\xbb~\x9f]f\xb9\xfa\x1b\x08zr\xc7\x97\xfd\xc7\xb3\xba\xea\x01\x1cO\xc6\x13\xe8[X\x10\xf4\x98U\xbcO\xa9{\xde\x05\x96\x95\x01G\xb4\xb8\xa9ow\xbc\x99\x13\x96@\x7f"\x87,\xdaS\xf3\x96\xb2\xff\'\xc0\xe8\xee\x01\xf6\x1c,\x94\xcd\x84\x97\x98\x1e\xdcR\x8b\xe7x\xd5\x15\x02\x03\x01\x00\x01\x02\x82\x01\x00*\x03\xf9\x0b\x17\x80\xf5\xf88\x1f@ [[8a\\+\xce\x0eJ~de\x9d\x97\xc2\xf8\x91\x88\xf7\x1f)u\xa1\xc5\xfay\xa2\x8em\xf9\x16\x9c\x7f@,\xfdgR\xec\xa04\xaf\xa2/\x17\xf1\xc5\xa9\xd6|\xfd\\"\xb2 8\xc2}E>\x87#\x91\x9eI\x89\x83\xce\xe3\x05\xa8\xeb\xef{\xf9G\x94\xe5q\xc4\xacs2\xe9O\x91\xa9\xf0.\xb4\xcc\xf33H\xa6\xa1\xfb\x95\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xd8\xdf\x82\xa6\xd8\xecKs\xdc\x06<l\x95\xc0I\x85C:\xd22\xd5\xfb]\xcd\xa3\xdeC\x07\r_\xd4\xf0\x84ot\r}\xfcqT\x11\x18/;\xf5\xdb\x18\x1a}(\xda\n\x8f\xaec\xf0T\x80\xa6\xe6Ss\x8e\xe9mp\xfb\xcc\xfc\xd7\xbb~\x9f]f\xb9\xfa\x1b\x08zr\xc7\x97\xfd\xc7\xb3\xba\xea\x01\x1cO\xc6\x13\xe8[X\x10\xf4\x98U\xbcO\xa9{\xde\x05\x96\x95\x01G\xb4\xb8\xa9ow\xbc\x99\x13\x96@\x7f"\x87,\xdaS\xf3\x96\xb2\xff\'\xc0\xe8\xee\x01\xf6\x1c,\x94\xcd\x84\x97\x98\x1e\xdcR\x8b\xe7x\xd5\x15\x02\x03\x01\x00\x01'
PUBLIC_PKCS1_PEM = b'-----BEGIN RSA PUBLIC KEY-----\nMIIBCgKCAQEAr2xKE7fuT/VV2Lk7gfCkA4xOTcFXWboTJ6ZGx1zWCP8d1pY5mYPx\n/dTUgDtUjaYGIRJy6G8xYLZvj22aY3l/DdfgLfk4Br9katexMSmKR0C9hVBWDbCk\n6ROK9dqEXuzGmpXhfcYs/9dL2N+CptjsS3PcBjxslcBJhUM60jLV+13No95DBw1f\n1PCEb3QNffxxVBEYLzv12xgafSjaCo+uY/BUgKbmU3OO6W1w+8z817t+n11mufob\nCHpyx5f9x7O66gEcT8YT6FtYEPSYVbxPqXveBZaVAUe0uKlvd7yZE5ZAfyKHLNpT\n85ay/yfA6O4B9hwslM2El5ge3FKL53jVFQIDAQAB\n-----END RSA PUBLIC KEY-----\n'
PUBLIC_X509_SPKI_DER = b'0\x82\x01"0\r\x06\t*\x86H\x86\xf7\r\x01\x01\x01\x05\x00\x03\x82\x01\x0f\x000\x82\x01\n\x02\x82\x01\x01\x00\xaflJ\x13\xb7\xeeO\xf5U\xd8\xb9;\x81\xf0\xa4\x03\x8cNM\xc1WY\xba\x13\'\xa6F\xc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T85ay/yfA6O4B9hwslM2El5ge3FKL53jV\nFQIDAQAB\n-----END PUBLIC KEY-----\n'
# 277 bytes of Lorem Ipsum
LOREM_IPSUM = b'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Facilisis leo vel fringilla est ullamcorper eget. Imperdiet nulla malesuada pellentesque elit. Dui nunc mattis enim ut. Mi in nulla posuere sollicitudin.'
# Hashes of the above snippet of Lorem Ipsum
LOREM_IPSUM_HASHES = {
'SHA-1': b'258eae8bb8d0a0993ab7e4c76435cab169ee4acc',
'SHA-256': b'92e4077e45c9a8c1a9f54505c4431bcab8130276b7bc2f1ff538d48683657877'
}
def debug_print(s: str):
"""
Print messages when `DEBUG` is set to `True`.
Args:
s (str): The string to print.
"""
if DEBUG:
print(s)
def test_key_importing_and_exporting():
"""
Test key importing and exporting.
"""
debug_print('Loading PRIVATE_PKCS1_DER')
private_pkcs1_der_key = mprsa.PrivateKey.load_pkcs1_der_data(PRIVATE_PKCS1_DER)
debug_print('Loading PRIVATE_PKCS1_PEM')
private_pkcs1_pem_key = mprsa.PrivateKey.load_pkcs1_pem_data(PRIVATE_PKCS1_PEM)
debug_print('Loading PRIVATE_PKCS8_DER')
private_pkcs8_der_key = mprsa.PrivateKey.load_pkcs8_der_data(PRIVATE_PKCS8_DER)
debug_print('Loading PRIVATE_PKCS8_PEM')
private_pkcs8_pem_key = mprsa.PrivateKey.load_pkcs8_pem_data(PRIVATE_PKCS8_PEM)
debug_print('Loading PUBLIC_PKCS1_DER')
public_pkcs1_der_key = mprsa.PublicKey.load_pkcs1_der_data(PUBLIC_PKCS1_DER)
debug_print('Loading PUBLIC_PKCS1_PEM')
public_pkcs1_pem_key = mprsa.PublicKey.load_pkcs1_pem_data(PUBLIC_PKCS1_PEM)
debug_print('Loading PUBLIC_X509_SPKI_DER')
public_x509_spki_der_key = mprsa.PublicKey.load_x509_spki_der_data(PUBLIC_X509_SPKI_DER)
debug_print('Loading PUBLIC_X509_SPKI_PEM')
public_x509_spki_pem_key = mprsa.PublicKey.load_x509_spki_pem_data(PUBLIC_X509_SPKI_PEM)
debug_print('Verifying all the PrivateKey objects are identical')
assert private_pkcs1_der_key == private_pkcs1_pem_key and private_pkcs1_pem_key == private_pkcs8_der_key and private_pkcs8_der_key == private_pkcs8_pem_key, 'Test failure: Private keys do not match'
debug_print('Verifying all the PublicKey objects are identical')
assert public_pkcs1_der_key == public_pkcs1_pem_key and public_pkcs1_pem_key == public_x509_spki_der_key and public_x509_spki_der_key == public_x509_spki_pem_key, 'Test failure: Public keys do not match'
debug_print('Verifying PrivateKey.get_pkcs1_der_data')
assert private_pkcs1_der_key.get_pkcs1_der_data() == PRIVATE_PKCS1_DER, 'Test failure: The function "PrivateKey.get_pkcs1_der_data" did not return the expected data'
debug_print('Verifying PrivateKey.get_pkcs1_pem_data')
assert private_pkcs1_der_key.get_pkcs1_pem_data() == PRIVATE_PKCS1_PEM, 'Test failure: The function "PrivateKey.get_pkcs1_pem_data" did not return the expected data'
debug_print('Verifying PrivateKey.get_pkcs8_der_data')
assert private_pkcs1_der_key.get_pkcs8_der_data() == PRIVATE_PKCS8_DER, 'Test failure: The function "PrivateKey.get_pkcs8_der_data" did not return the expected data'
debug_print('Verifying PrivateKey.get_pkcs8_pem_data')
assert private_pkcs1_der_key.get_pkcs8_pem_data() == PRIVATE_PKCS8_PEM, 'Test failure: The function "PrivateKey.get_pkcs8_pem_data" did not return the expected data'
debug_print('Verifying PublicKey.get_pkcs1_der_data')
assert public_pkcs1_der_key.get_pkcs1_der_data() == PUBLIC_PKCS1_DER, 'Test failure: The function "PublicKey.get_pkcs1_der_data" did not return the expected data'
debug_print('Verifying PublicKey.get_pkcs1_pem_data')
assert public_pkcs1_der_key.get_pkcs1_pem_data() == PUBLIC_PKCS1_PEM, 'Test failure: The function "PublicKey.get_pkcs1_pem_data" did not return the expected data'
debug_print('Verifying PublicKey.get_x509_spki_der_data')
assert public_pkcs1_der_key.get_x509_spki_der_data() == PUBLIC_X509_SPKI_DER, 'Test failure: The function "PublicKey.get_x509_spki_der_data" did not return the expected data'
debug_print('Verifying PublicKey.get_x509_spki_pem_data')
assert public_pkcs1_der_key.get_x509_spki_pem_data() == PUBLIC_X509_SPKI_PEM, 'Test failure: The function "PublicKey.get_x509_spki_pem_data" did not return the expected data'
def test_key_generation(bit_cnt: int, accurate_modulus: bool, safe_primes: bool) -> typing.Tuple[mprsa.PublicKey, mprsa.PrivateKey]:
"""
Test key generation.
Args:
bit_cnt (int): The size to use during key generation.
accurate_modulus (bool): A boolean used to determine if a modulus "n" must match the exact number of bits
specified during key generation.
safe_primes (bool): A boolean used to determine if prime numbers "p" and "q" must be safe primes during key
generation.
Returns:
result (typing.Tuple[mprsa.PublicKey, mprsa.PrivateKey]): A two item tuple where the first item is an instance
of `mprsa.PublicKey` and the second item is an instance of `mprsa.PrivateKey`.
"""
debug_print('Generate a new {bit_cnt} bit key pair. Be patient, this may take a while.'.format(bit_cnt=bit_cnt))
return mprsa.gen_keys(bit_cnt, accurate_modulus=accurate_modulus, safe_primes=safe_primes)
def test_encryption_and_decryption(public_key: mprsa.PublicKey, private_key: mprsa.PrivateKey):
"""
Test encryption and decryption.
Args:
public_key (mprsa.PublicKey): An instance of `mprsa.PublicKey`.
private_key (mprsa.PrivateKey): An instance of `mprsa.PrivateKey`.
"""
byte_cnt = 2048 // 8 - 11
debug_print('Encrypting {byte_cnt} bytes (the max that can be encrypted by a 2048 bit key) of Lorem Ipsum'.format(byte_cnt=byte_cnt))
encrypted_msg = public_key.encrypt(LOREM_IPSUM[0: byte_cnt])
debug_print('Decrypting previously encrypted message')
decrypted_msg = private_key.decrypt(encrypted_msg)
debug_print('Verifying encryption/decryption were successful')
assert decrypted_msg[0], 'Test failure: Message decryption was unsuccessful. This could result from a problem with encryption, decryption, or both.'
assert decrypted_msg[1] == LOREM_IPSUM[0: byte_cnt], 'Test failure: Decrypted message does not match original input. This could result from a problem with encryption, decryption, or both.'
def test_signing_and_verification(public_key: mprsa.PublicKey, private_key: mprsa.PrivateKey, hashing_algorithm: str):
"""
Test signing and verification.
Args:
public_key (mprsa.PublicKey): An instance of `mprsa.PublicKey`.
private_key (mprsa.PrivateKey): An instance of `mprsa.PrivateKey`.
hashing_algorithm (str): The hashing algorithm to use. Current options are "SHA-1" and "SHA-256".
"""
debug_print('Signing {lorem_ipsum_len} bytes of Lorem Ipsum using {hashing_algorithm} for hashing'.format(lorem_ipsum_len=len(LOREM_IPSUM), hashing_algorithm=hashing_algorithm))
signature_encrypted = private_key.sign(LOREM_IPSUM, hashing_algorithm)
debug_print('Verifying (without pre-computed hash) the signature of the previously signed message')
verification_result = public_key.verify(LOREM_IPSUM, signature_encrypted)
debug_print('Verifying signing/verification were successful')
assert verification_result[0], 'Test failure: Signature verification (without pre-computed hash) with {hashing_algorithm} hashing was unsuccessful. This could result from a problem with signing, verification, or both.'.format(hashing_algorithm=hashing_algorithm)
assert verification_result[1] == hashing_algorithm, 'Test failure: Signature verification hashing function does not match original input of {hashing_algorithm}. This could result from a problem with signing, verification, or both.'.format(hashing_algorithm=hashing_algorithm)
debug_print('Pre-computing {hashing_algorithm} hash on {lorem_ipsum_len} bytes of Lorem Ipsum'.format(hashing_algorithm=hashing_algorithm, lorem_ipsum_len=len(LOREM_IPSUM)))
lorem_ipsum_hash = mprsa.compute_hash(LOREM_IPSUM, hashing_algorithm)
debug_print('Verifying (with pre-computed hash) the signature of the previously signed message')
verification_result = public_key.verify_hash(lorem_ipsum_hash, signature_encrypted)
debug_print('Verifying signing/verification were successful')
assert verification_result[0], 'Test failure: Signature verification (with pre-computed hash) with {hashing_algorithm} hashing was unsuccessful. This could result from a problem with signing, verification, or both.'.format(hashing_algorithm=hashing_algorithm)
assert verification_result[1] == hashing_algorithm, 'Test failure: Signature verification hashing function does not match original input of {hashing_algorithm}. This could result from a problem with signing, verification, or both.'.format(hashing_algorithm=hashing_algorithm)
debug_print('Verifying the {hashing_algorithm} hashing function used by signing/verification produces the expected result'.format(hashing_algorithm=hashing_algorithm))
assert binascii.hexlify(lorem_ipsum_hash) == LOREM_IPSUM_HASHES[hashing_algorithm]
def main(test_loops: int = 1, debug: bool = False):
"""
Test all functions of the mprsa module.
Args:
test_loops (int): Number of times to run the tests. Defaults to 1.
debug (bool): A boolean which when `True` causes debug messages to be printed. Defaults to `False`.
"""
global DEBUG
DEBUG = debug
for test_loop in range(0, test_loops):
print('Test loop {test_loop}... '.format(test_loop=test_loop), end='\n' if DEBUG else '')
try:
test_key_importing_and_exporting()
test_key_generation(bit_cnt=64, accurate_modulus=True, safe_primes=False)
test_key_generation(bit_cnt=64, accurate_modulus=False, safe_primes=True)
test_key_generation(bit_cnt=64, accurate_modulus=True, safe_primes=True)
public_key, private_key = test_key_generation(bit_cnt=2048, accurate_modulus=False, safe_primes=False)
test_encryption_and_decryption(public_key, private_key)
test_signing_and_verification(public_key, private_key, 'SHA-1')
test_signing_and_verification(public_key, private_key, 'SHA-256')
print('SUCCESS!')
except Exception as e:
print('FAIL!')
print(e)