| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
from __future__ import print_function |
| 24 |
|
| 25 |
import unittest |
| 26 |
|
| 27 |
from Crypto.PublicKey import RSA |
| 28 |
from Crypto.SelfTest.st_common import list_test_cases, a2b_hex |
| 29 |
from Crypto import Random |
| 30 |
from Crypto.Cipher import PKCS1_v1_5 as PKCS |
| 31 |
from Crypto.Util.py3compat import b |
| 32 |
from Crypto.Util.number import bytes_to_long, long_to_bytes |
| 33 |
from Crypto.SelfTest.loader import load_test_vectors_wycheproof |
| 34 |
|
| 35 |
|
| 36 |
def rws(t): |
| 37 |
"""Remove white spaces, tabs, and new lines from a string""" |
| 38 |
for c in ['\n', '\t', ' ']: |
| 39 |
t = t.replace(c, '') |
| 40 |
return t |
| 41 |
|
| 42 |
|
| 43 |
def t2b(t): |
| 44 |
"""Convert a text string with bytes in hex form to a byte string""" |
| 45 |
clean = b(rws(t)) |
| 46 |
if len(clean) % 2 == 1: |
| 47 |
raise ValueError("Even number of characters expected") |
| 48 |
return a2b_hex(clean) |
| 49 |
|
| 50 |
|
| 51 |
class PKCS1_15_Tests(unittest.TestCase): |
| 52 |
|
| 53 |
def setUp(self): |
| 54 |
self.rng = Random.new().read |
| 55 |
self.key1024 = RSA.generate(1024, self.rng) |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
_testData = ( |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
( |
| 70 |
|
| 71 |
'''-----BEGIN RSA PRIVATE KEY----- |
| 72 |
MIICXAIBAAKBgQDAiAnvIAOvqVwJTaYzsKnefZftgtXGE2hPJppGsWl78yz9jeXY |
| 73 |
W/FxX/gTPURArNhdnhP6n3p2ZaDIBrO2zizbgIXs0IsljTTcr4vnI8fMXzyNUOjA |
| 74 |
zP3nzMqZDZK6757XQAobOssMkBFqRWwilT/3DsBhRpl3iMUhF+wvpTSHewIDAQAB |
| 75 |
AoGAC4HV/inOrpgTvSab8Wj0riyZgQOZ3U3ZpSlsfR8ra9Ib9Uee3jCYnKscu6Gk |
| 76 |
y6zI/cdt8EPJ4PuwAWSNJzbpbVaDvUq25OD+CX8/uRT08yBS4J8TzBitZJTD4lS7 |
| 77 |
atdTnKT0Wmwk+u8tDbhvMKwnUHdJLcuIsycts9rwJVapUtkCQQDvDpx2JMun0YKG |
| 78 |
uUttjmL8oJ3U0m3ZvMdVwBecA0eebZb1l2J5PvI3EJD97eKe91Nsw8T3lwpoN40k |
| 79 |
IocSVDklAkEAzi1HLHE6EzVPOe5+Y0kGvrIYRRhncOb72vCvBZvD6wLZpQgqo6c4 |
| 80 |
d3XHFBBQWA6xcvQb5w+VVEJZzw64y25sHwJBAMYReRl6SzL0qA0wIYrYWrOt8JeQ |
| 81 |
8mthulcWHXmqTgC6FEXP9Es5GD7/fuKl4wqLKZgIbH4nqvvGay7xXLCXD/ECQH9a |
| 82 |
1JYNMtRen5unSAbIOxRcKkWz92F0LKpm9ZW/S9vFHO+mBcClMGoKJHiuQxLBsLbT |
| 83 |
NtEZfSJZAeS2sUtn3/0CQDb2M2zNBTF8LlM0nxmh0k9VGm5TVIyBEMcipmvOgqIs |
| 84 |
HKukWBcq9f/UOmS0oEhai/6g+Uf7VHJdWaeO5LzuvwU= |
| 85 |
-----END RSA PRIVATE KEY-----''', |
| 86 |
|
| 87 |
'''THIS IS PLAINTEXT\x0A''', |
| 88 |
|
| 89 |
'''3f dc fd 3c cd 5c 9b 12 af 65 32 e3 f7 d0 da 36 |
| 90 |
8f 8f d9 e3 13 1c 7f c8 b3 f9 c1 08 e4 eb 79 9c |
| 91 |
91 89 1f 96 3b 94 77 61 99 a4 b1 ee 5d e6 17 c9 |
| 92 |
5d 0a b5 63 52 0a eb 00 45 38 2a fb b0 71 3d 11 |
| 93 |
f7 a1 9e a7 69 b3 af 61 c0 bb 04 5b 5d 4b 27 44 |
| 94 |
1f 5b 97 89 ba 6a 08 95 ee 4f a2 eb 56 64 e5 0f |
| 95 |
da 7c f9 9a 61 61 06 62 ed a0 bc 5f aa 6c 31 78 |
| 96 |
70 28 1a bb 98 3c e3 6a 60 3c d1 0b 0f 5a f4 75''', |
| 97 |
|
| 98 |
'''eb d7 7d 86 a4 35 23 a3 54 7e 02 0b 42 1d |
| 99 |
61 6c af 67 b8 4e 17 56 80 66 36 04 64 34 26 8a |
| 100 |
47 dd 44 b3 1a b2 17 60 f4 91 2e e2 b5 95 64 cc |
| 101 |
f9 da c8 70 94 54 86 4c ef 5b 08 7d 18 c4 ab 8d |
| 102 |
04 06 33 8f ca 15 5f 52 60 8a a1 0c f5 08 b5 4c |
| 103 |
bb 99 b8 94 25 04 9c e6 01 75 e6 f9 63 7a 65 61 |
| 104 |
13 8a a7 47 77 81 ae 0d b8 2c 4d 50 a5''' |
| 105 |
), |
| 106 |
) |
| 107 |
|
| 108 |
def testEncrypt1(self): |
| 109 |
for test in self._testData: |
| 110 |
|
| 111 |
key = RSA.importKey(test[0]) |
| 112 |
|
| 113 |
|
| 114 |
class randGen: |
| 115 |
def __init__(self, data): |
| 116 |
self.data = data |
| 117 |
self.idx = 0 |
| 118 |
def __call__(self, N): |
| 119 |
r = self.data[self.idx:self.idx+N] |
| 120 |
self.idx += N |
| 121 |
return r |
| 122 |
|
| 123 |
cipher = PKCS.new(key, randfunc=randGen(t2b(test[3]))) |
| 124 |
ct = cipher.encrypt(b(test[1])) |
| 125 |
self.assertEqual(ct, t2b(test[2])) |
| 126 |
|
| 127 |
def testEncrypt2(self): |
| 128 |
|
| 129 |
pt = '\x00'*(128-11+1) |
| 130 |
cipher = PKCS.new(self.key1024) |
| 131 |
self.assertRaises(ValueError, cipher.encrypt, pt) |
| 132 |
|
| 133 |
def testVerify1(self): |
| 134 |
for test in self._testData: |
| 135 |
key = RSA.importKey(test[0]) |
| 136 |
expected_pt = b(test[1]) |
| 137 |
ct = t2b(test[2]) |
| 138 |
cipher = PKCS.new(key) |
| 139 |
|
| 140 |
|
| 141 |
pt = cipher.decrypt(ct, None) |
| 142 |
self.assertEqual(pt, expected_pt) |
| 143 |
|
| 144 |
pt = cipher.decrypt(ct, b'\xFF' * len(expected_pt)) |
| 145 |
self.assertEqual(pt, expected_pt) |
| 146 |
|
| 147 |
def testVerify2(self): |
| 148 |
|
| 149 |
|
| 150 |
cipher = PKCS.new(self.key1024) |
| 151 |
self.assertRaises(ValueError, cipher.decrypt, '\x00'*127, "---") |
| 152 |
self.assertRaises(ValueError, cipher.decrypt, '\x00'*129, "---") |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
pt = b('\x00\x02' + '\xFF'*7 + '\x00' + '\x45'*118) |
| 157 |
pt_int = bytes_to_long(pt) |
| 158 |
ct_int = self.key1024._encrypt(pt_int) |
| 159 |
ct = long_to_bytes(ct_int, 128) |
| 160 |
self.assertEqual(b"---", cipher.decrypt(ct, b"---")) |
| 161 |
|
| 162 |
def testEncryptVerify1(self): |
| 163 |
|
| 164 |
|
| 165 |
for pt_len in range(0, 128 - 11 + 1): |
| 166 |
pt = self.rng(pt_len) |
| 167 |
cipher = PKCS.new(self.key1024) |
| 168 |
ct = cipher.encrypt(pt) |
| 169 |
pt2 = cipher.decrypt(ct, b'\xAA' * pt_len) |
| 170 |
self.assertEqual(pt, pt2) |
| 171 |
|
| 172 |
def test_encrypt_verify_exp_pt_len(self): |
| 173 |
|
| 174 |
cipher = PKCS.new(self.key1024) |
| 175 |
pt = b'5' * 16 |
| 176 |
ct = cipher.encrypt(pt) |
| 177 |
sentinel = b'\xAA' * 16 |
| 178 |
|
| 179 |
pt_A = cipher.decrypt(ct, sentinel, 16) |
| 180 |
self.assertEqual(pt, pt_A) |
| 181 |
|
| 182 |
pt_B = cipher.decrypt(ct, sentinel, 15) |
| 183 |
self.assertEqual(sentinel, pt_B) |
| 184 |
|
| 185 |
pt_C = cipher.decrypt(ct, sentinel, 17) |
| 186 |
self.assertEqual(sentinel, pt_C) |
| 187 |
|
| 188 |
def testByteArray(self): |
| 189 |
pt = b"XER" |
| 190 |
cipher = PKCS.new(self.key1024) |
| 191 |
ct = cipher.encrypt(bytearray(pt)) |
| 192 |
pt2 = cipher.decrypt(bytearray(ct), '\xFF' * len(pt)) |
| 193 |
self.assertEqual(pt, pt2) |
| 194 |
|
| 195 |
def testMemoryview(self): |
| 196 |
pt = b"XER" |
| 197 |
cipher = PKCS.new(self.key1024) |
| 198 |
ct = cipher.encrypt(memoryview(bytearray(pt))) |
| 199 |
pt2 = cipher.decrypt(memoryview(bytearray(ct)), b'\xFF' * len(pt)) |
| 200 |
self.assertEqual(pt, pt2) |
| 201 |
|
| 202 |
def test_return_type(self): |
| 203 |
pt = b"XYZ" |
| 204 |
cipher = PKCS.new(self.key1024) |
| 205 |
ct = cipher.encrypt(pt) |
| 206 |
self.assertTrue(isinstance(ct, bytes)) |
| 207 |
pt2 = cipher.decrypt(ct, b'\xAA' * 3) |
| 208 |
self.assertTrue(isinstance(pt2, bytes)) |
| 209 |
|
| 210 |
|
| 211 |
class TestVectorsWycheproof(unittest.TestCase): |
| 212 |
|
| 213 |
def __init__(self, wycheproof_warnings, skip_slow_tests): |
| 214 |
unittest.TestCase.__init__(self) |
| 215 |
self._wycheproof_warnings = wycheproof_warnings |
| 216 |
self._skip_slow_tests = skip_slow_tests |
| 217 |
self._id = "None" |
| 218 |
|
| 219 |
def load_tests(self, filename): |
| 220 |
|
| 221 |
def filter_rsa(group): |
| 222 |
return RSA.import_key(group['privateKeyPem']) |
| 223 |
|
| 224 |
result = load_test_vectors_wycheproof(("Cipher", "wycheproof"), |
| 225 |
filename, |
| 226 |
"Wycheproof PKCS#1v1.5 (%s)" % filename, |
| 227 |
group_tag={'rsa_key': filter_rsa} |
| 228 |
) |
| 229 |
return result |
| 230 |
|
| 231 |
def setUp(self): |
| 232 |
self.tv = [] |
| 233 |
self.tv.extend(self.load_tests("rsa_pkcs1_2048_test.json")) |
| 234 |
if not self._skip_slow_tests: |
| 235 |
self.tv.extend(self.load_tests("rsa_pkcs1_3072_test.json")) |
| 236 |
self.tv.extend(self.load_tests("rsa_pkcs1_4096_test.json")) |
| 237 |
|
| 238 |
def shortDescription(self): |
| 239 |
return self._id |
| 240 |
|
| 241 |
def warn(self, tv): |
| 242 |
if tv.warning and self._wycheproof_warnings: |
| 243 |
import warnings |
| 244 |
warnings.warn("Wycheproof warning: %s (%s)" % (self._id, tv.comment)) |
| 245 |
|
| 246 |
def test_decrypt(self, tv): |
| 247 |
self._id = "Wycheproof Decrypt PKCS#1v1.5 Test #%s" % tv.id |
| 248 |
sentinel = b'\xAA' * max(3, len(tv.msg)) |
| 249 |
cipher = PKCS.new(tv.rsa_key) |
| 250 |
try: |
| 251 |
pt = cipher.decrypt(tv.ct, sentinel=sentinel) |
| 252 |
except ValueError: |
| 253 |
assert not tv.valid |
| 254 |
else: |
| 255 |
if pt == sentinel: |
| 256 |
assert not tv.valid |
| 257 |
else: |
| 258 |
assert tv.valid |
| 259 |
self.assertEqual(pt, tv.msg) |
| 260 |
self.warn(tv) |
| 261 |
|
| 262 |
def runTest(self): |
| 263 |
|
| 264 |
for tv in self.tv: |
| 265 |
self.test_decrypt(tv) |
| 266 |
|
| 267 |
|
| 268 |
def get_tests(config={}): |
| 269 |
skip_slow_tests = not config.get('slow_tests') |
| 270 |
wycheproof_warnings = config.get('wycheproof_warnings') |
| 271 |
|
| 272 |
tests = [] |
| 273 |
tests += list_test_cases(PKCS1_15_Tests) |
| 274 |
tests += [TestVectorsWycheproof(wycheproof_warnings, skip_slow_tests)] |
| 275 |
return tests |
| 276 |
|
| 277 |
|
| 278 |
if __name__ == '__main__': |
| 279 |
def suite(): |
| 280 |
return unittest.TestSuite(get_tests()) |
| 281 |
unittest.main(defaultTest='suite') |
| 282 |
|
| 283 |
|