OLD | NEW |
(Empty) | |
| 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 class PKCS1_15_Tests(unittest.TestCase): |
| 56 |
| 57 # List of tuples with test data for PKCS#1 v1.5. |
| 58 # Each tuple is made up by: |
| 59 # Item #0: dictionary with RSA key component, or key to import |
| 60 # Item #1: data to hash and sign |
| 61 # Item #2: signature of the data #1, done with the key #0, after |
| 62 # hashing it with #3 |
| 63 # Item #3: hash object generator |
| 64 |
| 65 _testData = ( |
| 66 |
| 67 # |
| 68 # Taken from ftp://ftp.rsa.com/pub/pkcs/ascii/examples.asc |
| 69 # "Some Examples of the PKCS Standards", 1999 |
| 70 # |
| 71 ( |
| 72 |
| 73 # Private key, from 2.1 |
| 74 { |
| 75 'n':'''0a 66 79 1d c6 98 81 68 de 7a b7 74 19 bb 7f b0 c0 01 c6 |
| 76 27 10 27 00 75 14 29 42 e1 9a 8d 8c 51 d0 53 b3 e3 78 2a 1d |
| 77 e5 dc 5a f4 eb e9 94 68 17 01 14 a1 df e6 7c dc 9a 9a f5 5d |
| 78 65 56 20 bb ab''', |
| 79 'e':'''01 00 |
| 80 01''', |
| 81 'd':'''01 23 c5 b6 1b a3 6e db 1d 36 79 90 41 99 a8 9e a8 0c 09 |
| 82 b9 12 2e 14 00 c0 9a dc f7 78 46 76 d0 1d 23 35 6a 7d 44 d6 |
| 83 bd 8b d5 0e 94 bf c7 23 fa 87 d8 86 2b 75 17 76 91 c1 1d 75 |
| 84 76 92 df 88 81''' |
| 85 }, |
| 86 # Data to sign, from 3.1 |
| 87 '''30 81 a4 02 01 00 30 42 31 0b 30 09 06 |
| 88 03 55 04 06 13 02 55 53 31 1d 30 1b 06 03 55 04 0a 13 14 |
| 89 45 78 61 6d 70 6c 65 20 4f 72 67 61 6e 69 7a 61 74 69 6f |
| 90 6e 31 14 30 12 06 03 55 04 03 13 0b 54 65 73 74 20 55 73 |
| 91 65 72 20 31 30 5b 30 0d 06 09 2a 86 48 86 f7 0d 01 01 01 |
| 92 05 00 03 4a 00 30 47 02 40 |
| 93 0a 66 79 1d c6 98 81 68 de 7a b7 74 19 bb 7f b0 |
| 94 c0 01 c6 27 10 27 00 75 14 29 42 e1 9a 8d 8c 51 |
| 95 d0 53 b3 e3 78 2a 1d e5 dc 5a f4 eb e9 94 68 17 |
| 96 01 14 a1 df e6 7c dc 9a 9a f5 5d 65 56 20 bb ab |
| 97 02 03 01 00 01''', |
| 98 # Signature, from 3.2 (at the very end) |
| 99 '''06 db 36 cb 18 d3 47 5b 9c 01 db 3c 78 95 28 08 |
| 100 02 79 bb ae ff 2b 7d 55 8e d6 61 59 87 c8 51 86 |
| 101 3f 8a 6c 2c ff bc 89 c3 f7 5a 18 d9 6b 12 7c 71 |
| 102 7d 54 d0 d8 04 8d a8 a0 54 46 26 d1 7a 2a 8f be''', |
| 103 MD2 |
| 104 ), |
| 105 |
| 106 # |
| 107 # RSA keypair generated with openssl |
| 108 # |
| 109 ( |
| 110 """-----BEGIN RSA PRIVATE KEY----- |
| 111 MIIBOwIBAAJBAL8eJ5AKoIsjURpcEoGubZMxLD7+kT+TLr7UkvEtFrRhDDKMtuII |
| 112 q19FrL4pUIMymPMSLBn3hJLe30Dw48GQM4UCAwEAAQJACUSDEp8RTe32ftq8IwG8 |
| 113 Wojl5mAd1wFiIOrZ/Uv8b963WJOJiuQcVN29vxU5+My9GPZ7RA3hrDBEAoHUDPrI |
| 114 OQIhAPIPLz4dphiD9imAkivY31Rc5AfHJiQRA7XixTcjEkojAiEAyh/pJHks/Mlr |
| 115 +rdPNEpotBjfV4M4BkgGAA/ipcmaAjcCIQCHvhwwKVBLzzTscT2HeUdEeBMoiXXK |
| 116 JACAr3sJQJGxIQIgarRp+m1WSKV1MciwMaTOnbU7wxFs9DP1pva76lYBzgUCIQC9 |
| 117 n0CnZCJ6IZYqSt0H5N7+Q+2Ro64nuwV/OSQfM6sBwQ== |
| 118 -----END RSA PRIVATE KEY-----""", |
| 119 "This is a test\x0a", |
| 120 # |
| 121 # PKCS#1 signature computed with openssl |
| 122 # |
| 123 '''4a700a16432a291a3194646952687d5316458b8b86fb0a25aa30e0dcecdb |
| 124 442676759ac63d56ec1499c3ae4c0013c2053cabd5b5804848994541ac16 |
| 125 fa243a4d''', |
| 126 SHA |
| 127 ), |
| 128 |
| 129 # |
| 130 # Test vector from http://www.di-mgt.com.au/rsa_alg.html#signpkc
s1 |
| 131 # |
| 132 ( |
| 133 { |
| 134 'n':'''E08973398DD8F5F5E88776397F4EB005BB5383DE0FB7ABDC7DC77
5290D052E6D |
| 135 12DFA68626D4D26FAA5829FC97ECFA82510F3080BEB1509E4644F12CBBD8
32CF |
| 136 C6686F07D9B060ACBEEE34096A13F5F7050593DF5EBA3556D961FF197FC9
81E6 |
| 137 F86CEA874070EFAC6D2C749F2DFA553AB9997702A648528C4EF357385774
575F''', |
| 138 'e':'''010001''', |
| 139 'd':'''00A403C327477634346CA686B57949014B2E8AD2C862B2C7D7480
96A8B91F736 |
| 140 F275D6E8CD15906027314735644D95CD6763CEB49F56AC2F376E1CEE0EBF
282D |
| 141 F439906F34D86E085BD5656AD841F313D72D395EFE33CBFF29E4030B3D05
A28F |
| 142 B7F18EA27637B07957D32F2BDE8706227D04665EC91BAF8B1AC3EC9144AB
7F21''' |
| 143 }, |
| 144 "abc", |
| 145 '''60AD5A78FB4A4030EC542C8974CD15F55384E836554CEDD9A322D5F4135C6
267 |
| 146 A9D20970C54E6651070B0144D43844C899320DD8FA7819F7EBC6A7715287332E |
| 147 C8675C136183B3F8A1F81EF969418267130A756FDBB2C71D9A667446E34E0EAD |
| 148 9CF31BFB66F816F319D0B7E430A5F2891553986E003720261C7E9022C0D9F11F
''', |
| 149 SHA |
| 150 ) |
| 151 |
| 152 ) |
| 153 |
| 154 def testSign1(self): |
| 155 for i in range(len(self._testData)): |
| 156 row = self._testData[i] |
| 157 # Build the key |
| 158 if isStr(row[0]): |
| 159 key = RSA.importKey(row[0]) |
| 160 else: |
| 161 comps = [ long(rws(row[0][x]),16) for x in ('n',
'e','d') ] |
| 162 key = RSA.construct(comps) |
| 163 h = row[3].new() |
| 164 # Data to sign can either be in hex form or not |
| 165 try: |
| 166 h.update(t2b(row[1])) |
| 167 except: |
| 168 h.update(b(row[1])) |
| 169 # The real test |
| 170 signer = PKCS.new(key) |
| 171 self.failUnless(signer.can_sign()) |
| 172 s = signer.sign(h) |
| 173 self.assertEqual(s, t2b(row[2])) |
| 174 |
| 175 def testVerify1(self): |
| 176 for i in range(len(self._testData)): |
| 177 row = self._testData[i] |
| 178 # Build the key |
| 179 if isStr(row[0]): |
| 180 key = RSA.importKey(row[0]).publickey() |
| 181 else: |
| 182 comps = [ long(rws(row[0][x]),16) for x in ('n',
'e') ] |
| 183 key = RSA.construct(comps) |
| 184 h = row[3].new() |
| 185 # Data to sign can either be in hex form or not |
| 186 try: |
| 187 h.update(t2b(row[1])) |
| 188 except: |
| 189 h.update(b(row[1])) |
| 190 # The real test |
| 191 verifier = PKCS.new(key) |
| 192 self.failIf(verifier.can_sign()) |
| 193 result = verifier.verify(h, t2b(row[2])) |
| 194 self.failUnless(result) |
| 195 |
| 196 def testSignVerify(self): |
| 197 rng = Random.new().read |
| 198 key = RSA.generate(1024, rng) |
| 199 |
| 200 for hashmod in (MD2,MD5,SHA,SHA224,SHA256,SHA384,SHA512,
RIPEMD): |
| 201 h = hashmod.new() |
| 202 h.update(b('blah blah blah')) |
| 203 |
| 204 signer = PKCS.new(key) |
| 205 s = signer.sign(h) |
| 206 result = signer.verify(h, s) |
| 207 self.failUnless(result) |
| 208 |
| 209 |
| 210 def get_tests(config={}): |
| 211 tests = [] |
| 212 tests += list_test_cases(PKCS1_15_Tests) |
| 213 return tests |
| 214 |
| 215 if __name__ == '__main__': |
| 216 suite = lambda: unittest.TestSuite(get_tests()) |
| 217 unittest.main(defaultTest='suite') |
| 218 |
| 219 # vim:set ts=4 sw=4 sts=4 expandtab: |
OLD | NEW |