rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | # -*- coding: utf-8 -*- |
| 2 | # |
| 3 | # SelfTest/Signature/test_pkcs1_15.py: Self-test for PKCS#1 v1.5 signatures |
| 4 | # |
| 5 | # =================================================================== |
| 6 | # The contents of this file are dedicated to the public domain. To |
| 7 | # the extent that dedication to the public domain is not available, |
| 8 | # everyone is granted a worldwide, perpetual, royalty-free, |
| 9 | # non-exclusive license to exercise all rights associated with the |
| 10 | # contents of this file for any purpose whatsoever. |
| 11 | # No rights are reserved. |
| 12 | # |
| 13 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 14 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 15 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 16 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 17 | # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 18 | # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 19 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | # SOFTWARE. |
| 21 | # =================================================================== |
| 22 | |
| 23 | __revision__ = "$Id$" |
| 24 | |
| 25 | import unittest |
| 26 | |
| 27 | from Crypto.PublicKey import RSA |
| 28 | from Crypto.SelfTest.st_common import list_test_cases, a2b_hex, b2a_hex |
| 29 | from Crypto.Hash import * |
| 30 | from Crypto import Random |
| 31 | from Crypto.Signature import PKCS1_v1_5 as PKCS |
| 32 | from Crypto.Util.py3compat import * |
| 33 | |
| 34 | def isStr(s): |
| 35 | t = '' |
| 36 | try: |
| 37 | t += s |
| 38 | except TypeError: |
| 39 | return 0 |
| 40 | return 1 |
| 41 | |
| 42 | def rws(t): |
| 43 | """Remove white spaces, tabs, and new lines from a string""" |
| 44 | for c in ['\n', '\t', ' ']: |
| 45 | t = t.replace(c,'') |
| 46 | return t |
| 47 | |
| 48 | def t2b(t): |
| 49 | """Convert a text string with bytes in hex form to a byte string""" |
| 50 | clean = b(rws(t)) |
| 51 | if len(clean)%2 == 1: |
| 52 | raise ValueError("Even number of characters expected") |
| 53 | return a2b_hex(clean) |
| 54 | |
| 55 | # dump str to bytes |
| 56 | def rsa_dump(data): |
| 57 | print("-----------pkcs1v15_mytest dump:----------") |
| 58 | for i in range(0, len(data)): |
| 59 | print ("0x%02x,"%ord(data[i])), |
| 60 | if(((i+1)%16) == 0): |
| 61 | print("") |
| 62 | print("-----------pkcs1v15_mytest dump end----------") |
| 63 | |
| 64 | |
| 65 | class PKCS1_15_Tests(unittest.TestCase): |
| 66 | |
| 67 | # List of tuples with test data for PKCS#1 v1.5. |
| 68 | # Each tuple is made up by: |
| 69 | # Item #0: dictionary with RSA key component, or key to import |
| 70 | # Item #1: data to hash and sign |
| 71 | # Item #2: signature of the data #1, done with the key #0, after |
| 72 | # hashing it with #3 |
| 73 | # Item #3: hash object generator |
| 74 | |
| 75 | _testData = ( |
| 76 | |
| 77 | # |
| 78 | # Taken from ftp://ftp.rsa.com/pub/pkcs/ascii/examples.asc |
| 79 | # "Some Examples of the PKCS Standards", 1999 |
| 80 | # |
| 81 | ( |
| 82 | |
| 83 | # Private key, from 2.1 |
| 84 | { |
| 85 | 'n':'''0a 66 79 1d c6 98 81 68 de 7a b7 74 19 bb 7f b0 c0 01 c6 |
| 86 | 27 10 27 00 75 14 29 42 e1 9a 8d 8c 51 d0 53 b3 e3 78 2a 1d |
| 87 | e5 dc 5a f4 eb e9 94 68 17 01 14 a1 df e6 7c dc 9a 9a f5 5d |
| 88 | 65 56 20 bb ab''', |
| 89 | 'e':'''01 00 |
| 90 | 01''', |
| 91 | 'd':'''01 23 c5 b6 1b a3 6e db 1d 36 79 90 41 99 a8 9e a8 0c 09 |
| 92 | b9 12 2e 14 00 c0 9a dc f7 78 46 76 d0 1d 23 35 6a 7d 44 d6 |
| 93 | bd 8b d5 0e 94 bf c7 23 fa 87 d8 86 2b 75 17 76 91 c1 1d 75 |
| 94 | 76 92 df 88 81''' |
| 95 | }, |
| 96 | # Data to sign, from 3.1 |
| 97 | '''30 81 a4 02 01 00 30 42 31 0b 30 09 06 |
| 98 | 03 55 04 06 13 02 55 53 31 1d 30 1b 06 03 55 04 0a 13 14 |
| 99 | 45 78 61 6d 70 6c 65 20 4f 72 67 61 6e 69 7a 61 74 69 6f |
| 100 | 6e 31 14 30 12 06 03 55 04 03 13 0b 54 65 73 74 20 55 73 |
| 101 | 65 72 20 31 30 5b 30 0d 06 09 2a 86 48 86 f7 0d 01 01 01 |
| 102 | 05 00 03 4a 00 30 47 02 40 |
| 103 | 0a 66 79 1d c6 98 81 68 de 7a b7 74 19 bb 7f b0 |
| 104 | c0 01 c6 27 10 27 00 75 14 29 42 e1 9a 8d 8c 51 |
| 105 | d0 53 b3 e3 78 2a 1d e5 dc 5a f4 eb e9 94 68 17 |
| 106 | 01 14 a1 df e6 7c dc 9a 9a f5 5d 65 56 20 bb ab |
| 107 | 02 03 01 00 01''', |
| 108 | # Signature, from 3.2 (at the very end) |
| 109 | '''06 db 36 cb 18 d3 47 5b 9c 01 db 3c 78 95 28 08 |
| 110 | 02 79 bb ae ff 2b 7d 55 8e d6 61 59 87 c8 51 86 |
| 111 | 3f 8a 6c 2c ff bc 89 c3 f7 5a 18 d9 6b 12 7c 71 |
| 112 | 7d 54 d0 d8 04 8d a8 a0 54 46 26 d1 7a 2a 8f be''', |
| 113 | MD2 |
| 114 | ), |
| 115 | |
| 116 | # |
| 117 | # RSA keypair generated with openssl |
| 118 | # |
| 119 | ( |
| 120 | """-----BEGIN RSA PRIVATE KEY----- |
| 121 | MIIBOwIBAAJBAL8eJ5AKoIsjURpcEoGubZMxLD7+kT+TLr7UkvEtFrRhDDKMtuII |
| 122 | q19FrL4pUIMymPMSLBn3hJLe30Dw48GQM4UCAwEAAQJACUSDEp8RTe32ftq8IwG8 |
| 123 | Wojl5mAd1wFiIOrZ/Uv8b963WJOJiuQcVN29vxU5+My9GPZ7RA3hrDBEAoHUDPrI |
| 124 | OQIhAPIPLz4dphiD9imAkivY31Rc5AfHJiQRA7XixTcjEkojAiEAyh/pJHks/Mlr |
| 125 | +rdPNEpotBjfV4M4BkgGAA/ipcmaAjcCIQCHvhwwKVBLzzTscT2HeUdEeBMoiXXK |
| 126 | JACAr3sJQJGxIQIgarRp+m1WSKV1MciwMaTOnbU7wxFs9DP1pva76lYBzgUCIQC9 |
| 127 | n0CnZCJ6IZYqSt0H5N7+Q+2Ro64nuwV/OSQfM6sBwQ== |
| 128 | -----END RSA PRIVATE KEY-----""", |
| 129 | "This is a test\x0a", |
| 130 | # |
| 131 | # PKCS#1 signature computed with openssl |
| 132 | # |
| 133 | '''4a700a16432a291a3194646952687d5316458b8b86fb0a25aa30e0dcecdb |
| 134 | 442676759ac63d56ec1499c3ae4c0013c2053cabd5b5804848994541ac16 |
| 135 | fa243a4d''', |
| 136 | SHA |
| 137 | ), |
| 138 | |
| 139 | # |
| 140 | # Test vector from http://www.di-mgt.com.au/rsa_alg.html#signpkcs1 |
| 141 | # |
| 142 | ( |
| 143 | { |
| 144 | 'n':'''E08973398DD8F5F5E88776397F4EB005BB5383DE0FB7ABDC7DC775290D052E6D |
| 145 | 12DFA68626D4D26FAA5829FC97ECFA82510F3080BEB1509E4644F12CBBD832CF |
| 146 | C6686F07D9B060ACBEEE34096A13F5F7050593DF5EBA3556D961FF197FC981E6 |
| 147 | F86CEA874070EFAC6D2C749F2DFA553AB9997702A648528C4EF357385774575F''', |
| 148 | 'e':'''010001''', |
| 149 | 'd':'''00A403C327477634346CA686B57949014B2E8AD2C862B2C7D748096A8B91F736 |
| 150 | F275D6E8CD15906027314735644D95CD6763CEB49F56AC2F376E1CEE0EBF282D |
| 151 | F439906F34D86E085BD5656AD841F313D72D395EFE33CBFF29E4030B3D05A28F |
| 152 | B7F18EA27637B07957D32F2BDE8706227D04665EC91BAF8B1AC3EC9144AB7F21''' |
| 153 | }, |
| 154 | "abc", |
| 155 | '''60AD5A78FB4A4030EC542C8974CD15F55384E836554CEDD9A322D5F4135C6267 |
| 156 | A9D20970C54E6651070B0144D43844C899320DD8FA7819F7EBC6A7715287332E |
| 157 | C8675C136183B3F8A1F81EF969418267130A756FDBB2C71D9A667446E34E0EAD |
| 158 | 9CF31BFB66F816F319D0B7E430A5F2891553986E003720261C7E9022C0D9F11F''', |
| 159 | SHA |
| 160 | ) |
| 161 | |
| 162 | ) |
| 163 | |
| 164 | def testSign1(self): |
| 165 | print("ttest") |
| 166 | for i in range(len(self._testData)): |
| 167 | row = self._testData[i] |
| 168 | # Build the key |
| 169 | if isStr(row[0]): |
| 170 | key = RSA.importKey(row[0]) |
| 171 | else: |
| 172 | comps = [ long(rws(row[0][x]),16) for x in ('n','e','d') ] |
| 173 | key = RSA.construct(comps) |
| 174 | h = row[3].new() |
| 175 | # Data to sign can either be in hex form or not |
| 176 | try: |
| 177 | h.update(t2b(row[1])) |
| 178 | except: |
| 179 | h.update(b(row[1])) |
| 180 | # The real test |
| 181 | signer = PKCS.new(key) |
| 182 | self.failUnless(signer.can_sign()) |
| 183 | s = signer.sign(h) |
| 184 | self.assertEqual(s, t2b(row[2])) |
| 185 | |
| 186 | def testVerify1(self): |
| 187 | for i in range(len(self._testData)): |
| 188 | row = self._testData[i] |
| 189 | # Build the key |
| 190 | if isStr(row[0]): |
| 191 | key = RSA.importKey(row[0]).publickey() |
| 192 | else: |
| 193 | comps = [ long(rws(row[0][x]),16) for x in ('n','e') ] |
| 194 | key = RSA.construct(comps) |
| 195 | h = row[3].new() |
| 196 | # Data to sign can either be in hex form or not |
| 197 | try: |
| 198 | h.update(t2b(row[1])) |
| 199 | except: |
| 200 | h.update(b(row[1])) |
| 201 | # The real test |
| 202 | verifier = PKCS.new(key) |
| 203 | self.failIf(verifier.can_sign()) |
| 204 | result = verifier.verify(h, t2b(row[2])) |
| 205 | self.failUnless(result) |
| 206 | |
| 207 | def testSignVerify(self): |
| 208 | rng = Random.new().read |
| 209 | key = RSA.generate(1024, rng) |
| 210 | |
| 211 | for hashmod in (MD2,MD5,SHA,SHA224,SHA256,SHA384,SHA512,RIPEMD): |
| 212 | h = hashmod.new() |
| 213 | h.update(b('blah blah blah')) |
| 214 | |
| 215 | signer = PKCS.new(key) |
| 216 | s = signer.sign(h) |
| 217 | result = signer.verify(h, s) |
| 218 | self.failUnless(result) |
| 219 | |
| 220 | |
| 221 | |
| 222 | class testSign1_mytest(): |
| 223 | # List of tuples with test data for PKCS#1 v1.5. |
| 224 | # Each tuple is made up by: |
| 225 | # Item #0: dictionary with RSA key component, or key to import |
| 226 | # Item #1: data to hash and sign |
| 227 | # Item #2: signature of the data #1, done with the key #0, after |
| 228 | # hashing it with #3 |
| 229 | # Item #3: hash object generator |
| 230 | |
| 231 | def __init__(self): |
| 232 | _testData = ( |
| 233 | |
| 234 | # |
| 235 | # Taken from ftp://ftp.rsa.com/pub/pkcs/ascii/examples.asc |
| 236 | # "Some Examples of the PKCS Standards", 1999 |
| 237 | # |
| 238 | ( |
| 239 | |
| 240 | # Private key, from 2.1 |
| 241 | { |
| 242 | 'n':'''0a 66 79 1d c6 98 81 68 de 7a b7 74 19 bb 7f b0 c0 01 c6 |
| 243 | 27 10 27 00 75 14 29 42 e1 9a 8d 8c 51 d0 53 b3 e3 78 2a 1d |
| 244 | e5 dc 5a f4 eb e9 94 68 17 01 14 a1 df e6 7c dc 9a 9a f5 5d |
| 245 | 65 56 20 bb ab''', |
| 246 | 'e':'''01 00 |
| 247 | 01''', |
| 248 | 'd':'''01 23 c5 b6 1b a3 6e db 1d 36 79 90 41 99 a8 9e a8 0c 09 |
| 249 | b9 12 2e 14 00 c0 9a dc f7 78 46 76 d0 1d 23 35 6a 7d 44 d6 |
| 250 | bd 8b d5 0e 94 bf c7 23 fa 87 d8 86 2b 75 17 76 91 c1 1d 75 |
| 251 | 76 92 df 88 81''' |
| 252 | }, |
| 253 | # Data to sign, from 3.1 |
| 254 | '''30 81 a4 02 01 00 30 42 31 0b 30 09 06 |
| 255 | 03 55 04 06 13 02 55 53 31 1d 30 1b 06 03 55 04 0a 13 14 |
| 256 | 45 78 61 6d 70 6c 65 20 4f 72 67 61 6e 69 7a 61 74 69 6f |
| 257 | 6e 31 14 30 12 06 03 55 04 03 13 0b 54 65 73 74 20 55 73 |
| 258 | 65 72 20 31 30 5b 30 0d 06 09 2a 86 48 86 f7 0d 01 01 01 |
| 259 | 05 00 03 4a 00 30 47 02 40 |
| 260 | 0a 66 79 1d c6 98 81 68 de 7a b7 74 19 bb 7f b0 |
| 261 | c0 01 c6 27 10 27 00 75 14 29 42 e1 9a 8d 8c 51 |
| 262 | d0 53 b3 e3 78 2a 1d e5 dc 5a f4 eb e9 94 68 17 |
| 263 | 01 14 a1 df e6 7c dc 9a 9a f5 5d 65 56 20 bb ab |
| 264 | 02 03 01 00 01''', |
| 265 | # Signature, from 3.2 (at the very end) |
| 266 | '''06 db 36 cb 18 d3 47 5b 9c 01 db 3c 78 95 28 08 |
| 267 | 02 79 bb ae ff 2b 7d 55 8e d6 61 59 87 c8 51 86 |
| 268 | 3f 8a 6c 2c ff bc 89 c3 f7 5a 18 d9 6b 12 7c 71 |
| 269 | 7d 54 d0 d8 04 8d a8 a0 54 46 26 d1 7a 2a 8f be''', |
| 270 | MD2 |
| 271 | ), |
| 272 | |
| 273 | # |
| 274 | # RSA keypair generated with openssl |
| 275 | # |
| 276 | ( |
| 277 | """-----BEGIN RSA PRIVATE KEY----- |
| 278 | MIIBOwIBAAJBAL8eJ5AKoIsjURpcEoGubZMxLD7+kT+TLr7UkvEtFrRhDDKMtuII |
| 279 | q19FrL4pUIMymPMSLBn3hJLe30Dw48GQM4UCAwEAAQJACUSDEp8RTe32ftq8IwG8 |
| 280 | Wojl5mAd1wFiIOrZ/Uv8b963WJOJiuQcVN29vxU5+My9GPZ7RA3hrDBEAoHUDPrI |
| 281 | OQIhAPIPLz4dphiD9imAkivY31Rc5AfHJiQRA7XixTcjEkojAiEAyh/pJHks/Mlr |
| 282 | +rdPNEpotBjfV4M4BkgGAA/ipcmaAjcCIQCHvhwwKVBLzzTscT2HeUdEeBMoiXXK |
| 283 | JACAr3sJQJGxIQIgarRp+m1WSKV1MciwMaTOnbU7wxFs9DP1pva76lYBzgUCIQC9 |
| 284 | n0CnZCJ6IZYqSt0H5N7+Q+2Ro64nuwV/OSQfM6sBwQ== |
| 285 | -----END RSA PRIVATE KEY-----""", |
| 286 | "This is a test\x0a", |
| 287 | # |
| 288 | # PKCS#1 signature computed with openssl |
| 289 | # |
| 290 | '''4a700a16432a291a3194646952687d5316458b8b86fb0a25aa30e0dcecdb |
| 291 | 442676759ac63d56ec1499c3ae4c0013c2053cabd5b5804848994541ac16 |
| 292 | fa243a4d''', |
| 293 | SHA |
| 294 | ), |
| 295 | |
| 296 | # |
| 297 | # Test vector from http://www.di-mgt.com.au/rsa_alg.html#signpkcs1 |
| 298 | # |
| 299 | ( |
| 300 | { |
| 301 | 'n':'''E08973398DD8F5F5E88776397F4EB005BB5383DE0FB7ABDC7DC775290D052E6D |
| 302 | 12DFA68626D4D26FAA5829FC97ECFA82510F3080BEB1509E4644F12CBBD832CF |
| 303 | C6686F07D9B060ACBEEE34096A13F5F7050593DF5EBA3556D961FF197FC981E6 |
| 304 | F86CEA874070EFAC6D2C749F2DFA553AB9997702A648528C4EF357385774575F''', |
| 305 | 'e':'''010001''', |
| 306 | 'd':'''00A403C327477634346CA686B57949014B2E8AD2C862B2C7D748096A8B91F736 |
| 307 | F275D6E8CD15906027314735644D95CD6763CEB49F56AC2F376E1CEE0EBF282D |
| 308 | F439906F34D86E085BD5656AD841F313D72D395EFE33CBFF29E4030B3D05A28F |
| 309 | B7F18EA27637B07957D32F2BDE8706227D04665EC91BAF8B1AC3EC9144AB7F21''' |
| 310 | }, |
| 311 | "abc", |
| 312 | '''60AD5A78FB4A4030EC542C8974CD15F55384E836554CEDD9A322D5F4135C6267 |
| 313 | A9D20970C54E6651070B0144D43844C899320DD8FA7819F7EBC6A7715287332E |
| 314 | C8675C136183B3F8A1F81EF969418267130A756FDBB2C71D9A667446E34E0EAD |
| 315 | 9CF31BFB66F816F319D0B7E430A5F2891553986E003720261C7E9022C0D9F11F''', |
| 316 | SHA |
| 317 | ), |
| 318 | |
| 319 | #RSA 2048 + SHA1 + py_rsakey_golden1.pem (generate by pycrypto_rsa_sign_pkca1v15_sha1) |
| 320 | ( |
| 321 | { |
| 322 | 'n':'''b9bd2e2ed0f531dc00a0f44bf36caa1c2358d3c48645cb51bec95a3a38fb7f99 |
| 323 | fc646814da5f6b410ea9897fa0d8bfa8a1bd21065a66e105918175248a6b089c |
| 324 | 39dc9805f03ab4f9a3f43684c8f9b8cd7fbe2ab120eeda08200c370cb51fe725 |
| 325 | 8f72130a962e551581157aec40bc99435d4cc50e74a878a428b0dc739cd518b9 |
| 326 | 8089b162ca609ce84bce7cf303a0174d1520505775e57f685b63b8e16646486d |
| 327 | 131582b08be3b7b379a7e076791fe32537bc464e847dcb4aed95286a3b70af32 |
| 328 | 0f30e9ac44c26ce8cf093e3f851a9d96ebd6876f93f330df94c4cf07b1dddab1 |
| 329 | 60d287aebe0fa4cf050fe9fb5cb04f8f85c5d3454ef6bc17581a41793359c61b''', |
| 330 | 'e':'''010001''', |
| 331 | 'd':'''3a44ef4820a5cb8e8963f5401e8de8900b46c2a8d4b6f0a224886695fd6a690d |
| 332 | 95f0a9f660cbae8a2a5f659374928b0e0c2f993cb4ffb1785dbd8f52775a3cc9 |
| 333 | 461dbe539e99cc4cc4f2c867bfe517e4e03e7886391eb5a7f6e5de1f4e7343c0 |
| 334 | b4cfc4c97d73eb9d6371a9ae946096745bc0c9d14f27cb3134d2c7563dcd5c83 |
| 335 | cc2c817446175812cb4040275e6b4c91d3bad61de64a3cc14973e11108170f18 |
| 336 | 6ce1b124457e7bcc28768d69ae60bd4e2e275d72b6eedb157cdc1bbd2e802c86 |
| 337 | b478c0c76cd2fa2e4ed54f274a84f57a50716c0ff8df37a2ad409b1a3335332c |
| 338 | d4225868dd4cfd4304b3eb4884630e1977fad20c7ce9ed9467632658296a0601''' |
| 339 | }, |
| 340 | "abc", |
| 341 | '''7b80ab542c939b7f8b042b2ac653f2792d139caf83bd612a8a29685ac52fd8ff |
| 342 | faa2ddb09547f4719ec60c8f9942c3356d36d087d869ffa84384246076017dcc |
| 343 | abe38d4cf6f00155216a29aad02673f61dfbac98869be64e40ffd888975e6203 |
| 344 | ef5dca5f82f28deb02ff1406cc079173309becb97b00c867007aaf2be0e9d355 |
| 345 | 64c03df71c50ec5132ac61ceade753c9ecce164ae0d4315f0fa308fbe900e75d |
| 346 | 51130992df550f7732fbc4c849e1f3c1a13927fe2c73f450cb33496ef1213567 |
| 347 | 6cbfa1d0c39dd9137582807aae36c88e556a6255b7499f22bf4ef03371514ca6 |
| 348 | 23d2ccf1d6b0896ac0572d175e92c9e47699f0962ef2c4a924a7f300aff847ac''', |
| 349 | SHA |
| 350 | ) |
| 351 | ) |
| 352 | |
| 353 | self._testData = _testData |
| 354 | |
| 355 | def testsign1_mytest(self): |
| 356 | print("mytest rsa sign") |
| 357 | for i in range(len(self._testData)): |
| 358 | row = self._testData[i] |
| 359 | # Build the key |
| 360 | if isStr(row[0]): |
| 361 | print("testData[%d]" %i) |
| 362 | key = RSA.importKey(row[0]) |
| 363 | else: |
| 364 | print("e testData[%d]" %i) |
| 365 | comps = [ long(rws(row[0][x]),16) for x in ('n','e','d') ] |
| 366 | key = RSA.construct(comps) |
| 367 | h = row[3].new() |
| 368 | # Data to sign can either be in hex form or not |
| 369 | try: |
| 370 | print("try") |
| 371 | h.update(t2b(row[1])) |
| 372 | except: |
| 373 | print("except") |
| 374 | h.update(b(row[1])) |
| 375 | # The real test |
| 376 | signer = PKCS.new(key) |
| 377 | t_flag = signer.can_sign() |
| 378 | if t_flag: |
| 379 | print("mytest: can sign") |
| 380 | else: |
| 381 | print("mytest: can't sign") |
| 382 | s = signer.sign(h) |
| 383 | rsa_dump(s) |
| 384 | if s == t2b(row[2]): |
| 385 | print("signature compare pass") |
| 386 | else: |
| 387 | print("signature compare fail") |
| 388 | |
| 389 | def testverify1_mytest(self): |
| 390 | print("mytest rsa verify") |
| 391 | for i in range(len(self._testData)): |
| 392 | row = self._testData[i] |
| 393 | # Build the key |
| 394 | if isStr(row[0]): |
| 395 | key = RSA.importKey(row[0]).publickey() |
| 396 | else: |
| 397 | comps = [ long(rws(row[0][x]),16) for x in ('n','e') ] |
| 398 | key = RSA.construct(comps) |
| 399 | h = row[3].new() |
| 400 | # Data to sign can either be in hex form or not |
| 401 | try: |
| 402 | h.update(t2b(row[1])) |
| 403 | except: |
| 404 | h.update(b(row[1])) |
| 405 | # The real test |
| 406 | verifier = PKCS.new(key) |
| 407 | t_flag = verifier.can_sign() |
| 408 | if t_flag: |
| 409 | print("mytest: can't verify") |
| 410 | else: |
| 411 | print("mytest: can verify") |
| 412 | result = verifier.verify(h, t2b(row[2])) |
| 413 | if result: |
| 414 | print("verify pass") |
| 415 | else: |
| 416 | print("verify fail") |
| 417 | |
| 418 | |
| 419 | class sbc_rsa_sign1_mytest(): |
| 420 | # List of tuples with test data for PKCS#1 v1.5. |
| 421 | # Each tuple is made up by: |
| 422 | # Item #0: dictionary with RSA key component, or key to import |
| 423 | # Item #1: data to hash and sign |
| 424 | # Item #2: signature of the data #1, done with the key #0, after |
| 425 | # hashing it with #3 |
| 426 | # Item #3: hash object generator |
| 427 | |
| 428 | def __init__(self, rsa_parse, msg): |
| 429 | _testData = ( |
| 430 | |
| 431 | # |
| 432 | # RSA keypair generated with openssl |
| 433 | # |
| 434 | |
| 435 | |
| 436 | # |
| 437 | # Test vector from http://www.di-mgt.com.au/rsa_alg.html#signpkcs1 |
| 438 | # |
| 439 | ( |
| 440 | { |
| 441 | 'n':'''E08973398DD8F5F5E88776397F4EB005BB5383DE0FB7ABDC7DC775290D052E6D |
| 442 | 12DFA68626D4D26FAA5829FC97ECFA82510F3080BEB1509E4644F12CBBD832CF |
| 443 | C6686F07D9B060ACBEEE34096A13F5F7050593DF5EBA3556D961FF197FC981E6 |
| 444 | F86CEA874070EFAC6D2C749F2DFA553AB9997702A648528C4EF357385774575F''', |
| 445 | 'e':'''010001''', |
| 446 | 'd':'''00A403C327477634346CA686B57949014B2E8AD2C862B2C7D748096A8B91F736 |
| 447 | F275D6E8CD15906027314735644D95CD6763CEB49F56AC2F376E1CEE0EBF282D |
| 448 | F439906F34D86E085BD5656AD841F313D72D395EFE33CBFF29E4030B3D05A28F |
| 449 | B7F18EA27637B07957D32F2BDE8706227D04665EC91BAF8B1AC3EC9144AB7F21''' |
| 450 | }, |
| 451 | "abc", |
| 452 | '''60AD5A78FB4A4030EC542C8974CD15F55384E836554CEDD9A322D5F4135C6267 |
| 453 | A9D20970C54E6651070B0144D43844C899320DD8FA7819F7EBC6A7715287332E |
| 454 | C8675C136183B3F8A1F81EF969418267130A756FDBB2C71D9A667446E34E0EAD |
| 455 | 9CF31BFB66F816F319D0B7E430A5F2891553986E003720261C7E9022C0D9F11F''', |
| 456 | SHA |
| 457 | ), |
| 458 | |
| 459 | #RSA 2048 + SHA1 + py_rsakey_golden1.pem (generate by pycrypto_rsa_sign_pkca1v15_sha1) |
| 460 | ( |
| 461 | { |
| 462 | 'n':'''b9bd2e2ed0f531dc00a0f44bf36caa1c2358d3c48645cb51bec95a3a38fb7f99 |
| 463 | fc646814da5f6b410ea9897fa0d8bfa8a1bd21065a66e105918175248a6b089c |
| 464 | 39dc9805f03ab4f9a3f43684c8f9b8cd7fbe2ab120eeda08200c370cb51fe725 |
| 465 | 8f72130a962e551581157aec40bc99435d4cc50e74a878a428b0dc739cd518b9 |
| 466 | 8089b162ca609ce84bce7cf303a0174d1520505775e57f685b63b8e16646486d |
| 467 | 131582b08be3b7b379a7e076791fe32537bc464e847dcb4aed95286a3b70af32 |
| 468 | 0f30e9ac44c26ce8cf093e3f851a9d96ebd6876f93f330df94c4cf07b1dddab1 |
| 469 | 60d287aebe0fa4cf050fe9fb5cb04f8f85c5d3454ef6bc17581a41793359c61b''', |
| 470 | 'e':'''010001''', |
| 471 | 'd':'''3a44ef4820a5cb8e8963f5401e8de8900b46c2a8d4b6f0a224886695fd6a690d |
| 472 | 95f0a9f660cbae8a2a5f659374928b0e0c2f993cb4ffb1785dbd8f52775a3cc9 |
| 473 | 461dbe539e99cc4cc4f2c867bfe517e4e03e7886391eb5a7f6e5de1f4e7343c0 |
| 474 | b4cfc4c97d73eb9d6371a9ae946096745bc0c9d14f27cb3134d2c7563dcd5c83 |
| 475 | cc2c817446175812cb4040275e6b4c91d3bad61de64a3cc14973e11108170f18 |
| 476 | 6ce1b124457e7bcc28768d69ae60bd4e2e275d72b6eedb157cdc1bbd2e802c86 |
| 477 | b478c0c76cd2fa2e4ed54f274a84f57a50716c0ff8df37a2ad409b1a3335332c |
| 478 | d4225868dd4cfd4304b3eb4884630e1977fad20c7ce9ed9467632658296a0601''' |
| 479 | }, |
| 480 | "abc", |
| 481 | '''7b80ab542c939b7f8b042b2ac653f2792d139caf83bd612a8a29685ac52fd8ff |
| 482 | faa2ddb09547f4719ec60c8f9942c3356d36d087d869ffa84384246076017dcc |
| 483 | abe38d4cf6f00155216a29aad02673f61dfbac98869be64e40ffd888975e6203 |
| 484 | ef5dca5f82f28deb02ff1406cc079173309becb97b00c867007aaf2be0e9d355 |
| 485 | 64c03df71c50ec5132ac61ceade753c9ecce164ae0d4315f0fa308fbe900e75d |
| 486 | 51130992df550f7732fbc4c849e1f3c1a13927fe2c73f450cb33496ef1213567 |
| 487 | 6cbfa1d0c39dd9137582807aae36c88e556a6255b7499f22bf4ef03371514ca6 |
| 488 | 23d2ccf1d6b0896ac0572d175e92c9e47699f0962ef2c4a924a7f300aff847ac''', |
| 489 | SHA |
| 490 | ) |
| 491 | ) |
| 492 | |
| 493 | self._testData = _testData |
| 494 | self.rsa_parse = rsa_parse |
| 495 | self.msg = msg |
| 496 | |
| 497 | def sbc_rsa_sign1(self): |
| 498 | print("mytest rsa sign") |
| 499 | for i in range(len(self._testData)): |
| 500 | row = self._testData[i] |
| 501 | # Build the key |
| 502 | if isStr(row[0]): |
| 503 | print("testData[%d]" %i) |
| 504 | key = RSA.importKey(row[0]) |
| 505 | else: |
| 506 | print("e testData[%d]" %i) |
| 507 | comps = [ long(rws(row[0][x]),16) for x in ('n','e','d') ] |
| 508 | key = RSA.construct(comps) |
| 509 | h = row[3].new() |
| 510 | # Data to sign can either be in hex form or not |
| 511 | try: |
| 512 | print("try") |
| 513 | h.update(t2b(row[1])) |
| 514 | except: |
| 515 | print("except") |
| 516 | h.update(b(row[1])) |
| 517 | # The real test |
| 518 | signer = PKCS.new(key) |
| 519 | t_flag = signer.can_sign() |
| 520 | if t_flag: |
| 521 | print("mytest: can sign") |
| 522 | else: |
| 523 | print("mytest: can't sign") |
| 524 | s = signer.sign(h) |
| 525 | rsa_dump(s) |
| 526 | if s == t2b(row[2]): |
| 527 | print("signature compare pass") |
| 528 | else: |
| 529 | print("signature compare fail") |
| 530 | return s |
| 531 | |
| 532 | def sbc_rsa_sign1_json(self): |
| 533 | print("mytest rsa sign json") |
| 534 | row = self.rsa_parse |
| 535 | #print(self.rsa_parse) |
| 536 | # Build the key |
| 537 | if isStr(row[0]): |
| 538 | #print("testData[%d]" %i) |
| 539 | key = RSA.importKey(row[0]) |
| 540 | else: |
| 541 | #print("e testData[%d]" %i) |
| 542 | comps = [ long(rws(row[0][x]),16) for x in ('n','e','d') ] |
| 543 | key = RSA.construct(comps) |
| 544 | |
| 545 | if(row[3] == "SHA"): |
| 546 | h = SHA.new() |
| 547 | elif(row[3] == "SHA256"): |
| 548 | h = SHA256.new() |
| 549 | elif(row[3] == "SHA384"): |
| 550 | h = SHA384.new() |
| 551 | elif(row[3] == "SHA512"): |
| 552 | h = SHA512.new() |
| 553 | else: |
| 554 | h = SHA256.new() |
| 555 | # Data to sign can either be in hex form or not |
| 556 | if(row[4] == 1): |
| 557 | try: |
| 558 | print("try") |
| 559 | h.update(t2b(row[1])) |
| 560 | except: |
| 561 | print("except") |
| 562 | h.update(b(row[1])) |
| 563 | else: |
| 564 | h.update(self.msg) |
| 565 | rsa_dump(h.digest()) |
| 566 | |
| 567 | # The real test |
| 568 | signer = PKCS.new(key) |
| 569 | t_flag = signer.can_sign() |
| 570 | if t_flag: |
| 571 | print("mytest: can sign") |
| 572 | else: |
| 573 | print("mytest: can't sign") |
| 574 | s = signer.sign(h) |
| 575 | rsa_dump(s) |
| 576 | if s == t2b(row[2]): |
| 577 | print("signature compare pass") |
| 578 | else: |
| 579 | print("signature compare fail") |
| 580 | return s |
| 581 | |
| 582 | def sbc_rsa_verify1(self): |
| 583 | print("mytest rsa verify") |
| 584 | for i in range(len(self._testData)): |
| 585 | row = self._testData[i] |
| 586 | # Build the key |
| 587 | if isStr(row[0]): |
| 588 | key = RSA.importKey(row[0]).publickey() |
| 589 | else: |
| 590 | comps = [ long(rws(row[0][x]),16) for x in ('n','e') ] |
| 591 | key = RSA.construct(comps) |
| 592 | h = row[3].new() |
| 593 | # Data to sign can either be in hex form or not |
| 594 | try: |
| 595 | h.update(t2b(row[1])) |
| 596 | except: |
| 597 | h.update(b(row[1])) |
| 598 | # The real test |
| 599 | verifier = PKCS.new(key) |
| 600 | t_flag = verifier.can_sign() |
| 601 | if t_flag: |
| 602 | print("mytest: can't verify") |
| 603 | else: |
| 604 | print("mytest: can verify") |
| 605 | result = verifier.verify(h, t2b(row[2])) |
| 606 | if result: |
| 607 | print("verify pass") |
| 608 | else: |
| 609 | print("verify fail") |
| 610 | |
| 611 | def get_tests(config={}): |
| 612 | tests = [] |
| 613 | tests += list_test_cases(PKCS1_15_Tests) |
| 614 | return tests |
| 615 | |
| 616 | if __name__ == '__main__': |
| 617 | suite = lambda: unittest.TestSuite(get_tests()) |
| 618 | unittest.main(defaultTest='suite') |
| 619 | |
| 620 | # vim:set ts=4 sw=4 sts=4 expandtab: |