Debian Codegraph @

lib/Crypto/SelfTest/Cipher/test_OpenPGP.py

raw symbols SCIP
1 # ===================================================================
2 #
3 # Copyright (c) 2015, Legrandin <[email protected]>
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 #
10 # 1. Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # 2. Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in
14 # the documentation and/or other materials provided with the
15 # distribution.
16 #
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 # POSSIBILITY OF SUCH DAMAGE.
29 # ===================================================================
30
31 import unittest
32 from binascii import unhexlify
33
34 from Crypto.SelfTest.st_common import list_test_cases
35 from Crypto.Util.py3compat import tobytes
36 from Crypto.Cipher import AES, DES3, DES
37 from Crypto.Hash import SHAKE128
38
39 def get_tag_random(tag, length):
40 return SHAKE128.new(data=tobytes(tag)).read(length)
41
42
43 from Crypto.SelfTest.Cipher.test_CBC import BlockChainingTests
44
45 class OpenPGPTests(BlockChainingTests):
46
47 aes_mode = AES.MODE_OPENPGP
48 des3_mode = DES3.MODE_OPENPGP
49
50 # Redefine test_unaligned_data_128/64
51
52 key_128 = get_tag_random("key_128", 16)
53 key_192 = get_tag_random("key_192", 24)
54 iv_128 = get_tag_random("iv_128", 16)
55 iv_64 = get_tag_random("iv_64", 8)
56 data_128 = get_tag_random("data_128", 16)
57
58 def test_loopback_128(self):
59 cipher = AES.new(self.key_128, AES.MODE_OPENPGP, self.iv_128)
60 pt = get_tag_random("plaintext", 16 * 100)
61 ct = cipher.encrypt(pt)
62
63 eiv, ct = ct[:18], ct[18:]
64
65 cipher = AES.new(self.key_128, AES.MODE_OPENPGP, eiv)
66 pt2 = cipher.decrypt(ct)
67 self.assertEqual(pt, pt2)
68
69 def test_loopback_64(self):
70 cipher = DES3.new(self.key_192, DES3.MODE_OPENPGP, self.iv_64)
71 pt = get_tag_random("plaintext", 8 * 100)
72 ct = cipher.encrypt(pt)
73
74 eiv, ct = ct[:10], ct[10:]
75
76 cipher = DES3.new(self.key_192, DES3.MODE_OPENPGP, eiv)
77 pt2 = cipher.decrypt(ct)
78 self.assertEqual(pt, pt2)
79
80 def test_IV_iv_attributes(self):
81 cipher = AES.new(self.key_128, AES.MODE_OPENPGP, self.iv_128)
82 eiv = cipher.encrypt(b"")
83 self.assertEqual(cipher.iv, self.iv_128)
84
85 cipher = AES.new(self.key_128, AES.MODE_OPENPGP, eiv)
86 self.assertEqual(cipher.iv, self.iv_128)
87
88 def test_null_encryption_decryption(self):
89 cipher = AES.new(self.key_128, AES.MODE_OPENPGP, self.iv_128)
90 eiv = cipher.encrypt(b"")
91
92 cipher = AES.new(self.key_128, AES.MODE_OPENPGP, eiv)
93 self.assertEqual(cipher.decrypt(b""), b"")
94
95 def test_either_encrypt_or_decrypt(self):
96 cipher = AES.new(self.key_128, AES.MODE_OPENPGP, self.iv_128)
97 eiv = cipher.encrypt(b"")
98 self.assertRaises(TypeError, cipher.decrypt, b"")
99
100 cipher = AES.new(self.key_128, AES.MODE_OPENPGP, eiv)
101 cipher.decrypt(b"")
102 self.assertRaises(TypeError, cipher.encrypt, b"")
103
104 def test_unaligned_data_128(self):
105 plaintexts = [ b"7777777" ] * 100
106
107 cipher = AES.new(self.key_128, AES.MODE_OPENPGP, self.iv_128)
108 ciphertexts = [ cipher.encrypt(x) for x in plaintexts ]
109 cipher = AES.new(self.key_128, AES.MODE_OPENPGP, self.iv_128)
110 self.assertEqual(b"".join(ciphertexts), cipher.encrypt(b"".join(plaintexts)))
111
112 def test_unaligned_data_64(self):
113 plaintexts = [ b"7777777" ] * 100
114
115 cipher = DES3.new(self.key_192, DES3.MODE_OPENPGP, self.iv_64)
116 ciphertexts = [ cipher.encrypt(x) for x in plaintexts ]
117 cipher = DES3.new(self.key_192, DES3.MODE_OPENPGP, self.iv_64)
118 self.assertEqual(b"".join(ciphertexts), cipher.encrypt(b"".join(plaintexts)))
119
120 def test_output_param(self):
121 pass
122
123 def test_output_param_same_buffer(self):
124 pass
125
126 def test_output_param_memoryview(self):
127 pass
128
129 def test_output_param_neg(self):
130 pass
131
132
133 class TestVectors(unittest.TestCase):
134
135 def test_aes(self):
136 # The following test vectors have been generated with gpg v1.4.0.
137 # The command line used was:
138 #
139 # gpg -c -z 0 --cipher-algo AES --passphrase secret_passphrase \
140 # --disable-mdc --s2k-mode 0 --output ct pt
141 #
142 # As result, the content of the file 'pt' is encrypted with a key derived
143 # from 'secret_passphrase' and written to file 'ct'.
144 # Test vectors must be extracted from 'ct', which is a collection of
145 # TLVs (see RFC4880 for all details):
146 # - the encrypted data (with the encrypted IV as prefix) is the payload
147 # of the TLV with tag 9 (Symmetrical Encrypted Data Packet).
148 # This is the ciphertext in the test vector.
149 # - inside the encrypted part, there is a further layer of TLVs. One must
150 # look for tag 11 (Literal Data Packet); in its payload, after a short
151 # but time dependent header, there is the content of file 'pt'.
152 # In the test vector, the plaintext is the complete set of TLVs that gets
153 # encrypted. It is not just the content of 'pt'.
154 # - the key is the leftmost 16 bytes of the SHA1 digest of the password.
155 # The test vector contains such shortened digest.
156 #
157 # Note that encryption uses a clear IV, and decryption an encrypted IV
158
159 plaintext = 'ac18620270744fb4f647426c61636b4361745768697465436174'
160 ciphertext = 'dc6b9e1f095de609765c59983db5956ae4f63aea7405389d2ebb'
161 key = '5baa61e4c9b93f3f0682250b6cf8331b'
162 iv = '3d7d3e62282add7eb203eeba5c800733'
163 encrypted_iv='fd934601ef49cb58b6d9aebca6056bdb96ef'
164
165 plaintext = unhexlify(plaintext)
166 ciphertext = unhexlify(ciphertext)
167 key = unhexlify(key)
168 iv = unhexlify(iv)
169 encrypted_iv = unhexlify(encrypted_iv)
170
171 cipher = AES.new(key, AES.MODE_OPENPGP, iv)
172 ct = cipher.encrypt(plaintext)
173 self.assertEqual(ct[:18], encrypted_iv)
174 self.assertEqual(ct[18:], ciphertext)
175
176 cipher = AES.new(key, AES.MODE_OPENPGP, encrypted_iv)
177 pt = cipher.decrypt(ciphertext)
178 self.assertEqual(pt, plaintext)
179
180 def test_des3(self):
181 # The following test vectors have been generated with gpg v1.4.0.
182 # The command line used was:
183 # gpg -c -z 0 --cipher-algo 3DES --passphrase secret_passphrase \
184 # --disable-mdc --s2k-mode 0 --output ct pt
185 # For an explanation, see test_AES.py .
186
187 plaintext = 'ac1762037074324fb53ba3596f73656d69746556616c6c6579'
188 ciphertext = '9979238528357b90e2e0be549cb0b2d5999b9a4a447e5c5c7d'
189 key = '7ade65b460f5ea9be35f9e14aa883a2048e3824aa616c0b2'
190 iv='cd47e2afb8b7e4b0'
191 encrypted_iv='6a7eef0b58050e8b904a'
192
193 plaintext = unhexlify(plaintext)
194 ciphertext = unhexlify(ciphertext)
195 key = unhexlify(key)
196 iv = unhexlify(iv)
197 encrypted_iv = unhexlify(encrypted_iv)
198
199 cipher = DES3.new(key, DES3.MODE_OPENPGP, iv)
200 ct = cipher.encrypt(plaintext)
201 self.assertEqual(ct[:10], encrypted_iv)
202 self.assertEqual(ct[10:], ciphertext)
203
204 cipher = DES3.new(key, DES3.MODE_OPENPGP, encrypted_iv)
205 pt = cipher.decrypt(ciphertext)
206 self.assertEqual(pt, plaintext)
207
208
209 def get_tests(config={}):
210 tests = []
211 tests += list_test_cases(OpenPGPTests)
212 tests += list_test_cases(TestVectors)
213 return tests
214
215
216 if __name__ == '__main__':
217 suite = lambda: unittest.TestSuite(get_tests())
218 unittest.main(defaultTest='suite')