Debian Codegraph @

dkim/tests/test_crypto.py

raw symbols SCIP
1 # This software is provided 'as-is', without any express or implied
2 # warranty. In no event will the author be held liable for any damages
3 # arising from the use of this software.
4 #
5 # Permission is granted to anyone to use this software for any purpose,
6 # including commercial applications, and to alter it and redistribute it
7 # freely, subject to the following restrictions:
8 #
9 # 1. The origin of this software must not be misrepresented; you must not
10 # claim that you wrote the original software. If you use this software
11 # in a product, an acknowledgment in the product documentation would be
12 # appreciated but is not required.
13 # 2. Altered source versions must be plainly marked as such, and must not be
14 # misrepresented as being the original software.
15 # 3. This notice may not be removed or altered from any source distribution.
16 #
17 # Copyright (c) 2011 William Grant <[email protected]>
18
19 import base64
20 import binascii
21 import hashlib
22 import unittest
23
24 from dkim.crypto import (
25 DigestTooLargeError,
26 UnparsableKeyError,
27 EMSA_PKCS1_v1_5_encode,
28 int2str,
29 parse_pem_private_key,
30 parse_public_key,
31 RSASSA_PKCS1_v1_5_sign,
32 RSASSA_PKCS1_v1_5_verify,
33 str2int,
34 )
35 from dkim.tests.test_dkim import read_test_data
36 from dkim.util import parse_tag_value
37
38
39 # These are extracted from dkim/tests/data/test.private.
40 TEST_KEY_MODULUS = int(
41 '160190232090260054474895273563294777865179886824815261110923286158270437'
42 '657769966074370477716411064825849317279563494735400250019233722215662302'
43 '997403060159149904218292658425241195497467863155064737257198115261596066'
44 '733086923624062366294295557722551666415445482671442053150678674937682352'
45 '837105556539434741981')
46 TEST_KEY_PUBLIC_EXPONENT = 65537
47 TEST_KEY_PRIVATE_EXPONENT = int(
48 '219642251791061057038224045690185219631125389170665415924249912174530136'
49 '074693824121380763959239792563755125360354847443780863736947713174228520'
50 '489900956461640273471526152019568303807247290486052565153701534491987040'
51 '131529720476525111651818771481293273124837542067061293644354088836358900'
52 '29771161475005043329')
53 TEST_KEY_PRIME1 = int(
54 '127343333492908149956322715568115237787784712176275919666517073343689103'
55 '280591709737233188193431204382936008602497360201661766158158969883295914'
56 '16266272177')
57 TEST_KEY_PRIME2 = int(
58 '125793967926229270607412639516115399484604596465353856808629588968254772'
59 '302339293254103556785310783521521266982500068526354237606773478050287350'
60 '33316975853')
61 TEST_KEY_EXPONENT1 = int(
62 '971401692373919639404678505179789291960987093676634885925231250693661495'
63 '080125935714710587508461815572290443270923375888685273287584323569222368'
64 '5450962737')
65 TEST_KEY_EXPONENT2 = int(
66 '405135004809332318340885085107137607293826268763328174261828392259785080'
67 '028911220030572618988900118679333717167345003034279703551607153395397272'
68 '3014807045')
69 TEST_KEY_COEFFICIENT = int(
70 '933140693852464192207530806898449261372116224159220632563973880414444021'
71 '989007318611849609226428922185905596238131661588470844906391982906126973'
72 '1282880267')
73 TEST_PK = {
74 'version': 0,
75 'modulus': TEST_KEY_MODULUS,
76 'publicExponent': TEST_KEY_PUBLIC_EXPONENT,
77 'privateExponent': TEST_KEY_PRIVATE_EXPONENT,
78 'prime1': TEST_KEY_PRIME1,
79 'prime2': TEST_KEY_PRIME2,
80 'exponent1': TEST_KEY_EXPONENT1,
81 'exponent2': TEST_KEY_EXPONENT2,
82 'coefficient': TEST_KEY_COEFFICIENT,
83 }
84
85
86 class TestStrIntConversion(unittest.TestCase):
87
88 def test_str2int(self):
89 self.assertEqual(1234, str2int(b'\x04\xd2'))
90
91 def test_int2str(self):
92 self.assertEqual(b'\x04\xd2', int2str(1234))
93
94 def test_int2str_with_length(self):
95 self.assertEqual(b'\x00\x00\x04\xd2', int2str(1234, 4))
96
97 def test_int2str_fails_on_negative(self):
98 self.assertRaises(AssertionError, int2str, -1)
99
100
101 class TestParseKeys(unittest.TestCase):
102
103 def test_parse_pem_private_key(self):
104 key = parse_pem_private_key(read_test_data('test.private'))
105 self.assertEqual(key, TEST_PK)
106
107 def test_parse_public_key(self):
108 data = read_test_data('test.txt')
109 key = parse_public_key(base64.b64decode(parse_tag_value(data)[b'p']))
110 self.assertEqual(key['modulus'], TEST_KEY_MODULUS)
111 self.assertEqual(key['publicExponent'], TEST_KEY_PUBLIC_EXPONENT)
112 try:
113 data = read_test_data('test_bad.txt')
114 key = parse_public_key(base64.b64decode(parse_tag_value(data)[b'p']))
115 except UnparsableKeyError: return
116 self.fail("failed to reject invalid public key")
117
118
119 class TestEMSA_PKCS1_v1_5(unittest.TestCase):
120
121 def test_encode_sha256(self):
122 hash = hashlib.sha256(b'message')
123 self.assertEqual(
124 b'\x00\x01\xff\xff\xff\xff\xff\xff\xff\xff\x00'
125 b'010\x0d\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x01\x05\x00\x04'
126 b' ' + hash.digest(),
127 EMSA_PKCS1_v1_5_encode(hash, 62))
128
129 def test_encode_sha1(self):
130 hash = hashlib.sha1(b'message')
131 self.assertEqual(
132 b'\x00\x01\xff\xff\xff\xff\xff\xff\xff\xff\x00'
133 b'0!0\x09\x06\x05\x2b\x0e\x03\x02\x1a\x05\x00\x04\x14'
134 + hash.digest(),
135 EMSA_PKCS1_v1_5_encode(hash, 46))
136
137 def test_encode_forbids_too_short(self):
138 # PKCS#1 requires at least 8 bytes of padding, so there must be
139 # at least that much space.
140 hash = hashlib.sha1(b'message')
141 self.assertRaises(
142 DigestTooLargeError,
143 EMSA_PKCS1_v1_5_encode, hash, 45)
144
145
146 class TestRSASSA(unittest.TestCase):
147
148 def setUp(self):
149 self.key = parse_pem_private_key(read_test_data('test.private'))
150 self.hash = hashlib.sha1(self.test_digest)
151
152 test_digest = b'0123456789abcdef0123'
153 test_signature = binascii.unhexlify(
154 b'cc8d3647d64dd3bc12984947a27bdfbb565041fcc9db781afb4b60d29d288d8d60d'
155 b'e9e1916d6f81569c3e72af442538dd6aecb50a6de9a14565fdd679c46ff7842482e'
156 b'15e5aa078549621b6f12ca8cd57ecfad95b18e53581e131c6c3c7cd01cb153adeb4'
157 b'39d2d6ab8b215b19be0e69ef490885004a474eb26d747a219693e8c')
158
159 def test_sign_and_verify(self):
160 signature = RSASSA_PKCS1_v1_5_sign(self.hash, TEST_PK)
161 self.assertEqual(self.test_signature, signature)
162 self.assertTrue(
163 RSASSA_PKCS1_v1_5_verify(
164 self.hash, signature, TEST_PK))
165
166 def test_invalid_signature(self):
167 invalid_key = TEST_PK.copy()
168 invalid_key['modulus'] += 1
169 self.assertFalse(
170 RSASSA_PKCS1_v1_5_verify(
171 self.hash, self.test_signature, invalid_key))
172
173
174 def test_suite():
175 from unittest import TestLoader
176 return TestLoader().loadTestsFromName(__name__)