| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 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 |
|
| 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 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 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 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 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') |