| 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 |
from __future__ import print_function |
| 32 |
|
| 33 |
import unittest |
| 34 |
from binascii import unhexlify |
| 35 |
|
| 36 |
from Crypto.SelfTest.st_common import list_test_cases |
| 37 |
from Crypto.SelfTest.loader import load_test_vectors, load_test_vectors_wycheproof |
| 38 |
|
| 39 |
from Crypto.Util.py3compat import tobytes, bchr |
| 40 |
from Crypto.Cipher import AES |
| 41 |
from Crypto.Hash import SHAKE128, SHA256 |
| 42 |
|
| 43 |
from Crypto.Util.strxor import strxor |
| 44 |
|
| 45 |
|
| 46 |
def get_tag_random(tag, length): |
| 47 |
return SHAKE128.new(data=tobytes(tag)).read(length) |
| 48 |
|
| 49 |
|
| 50 |
class GcmTests(unittest.TestCase): |
| 51 |
|
| 52 |
key_128 = get_tag_random("key_128", 16) |
| 53 |
nonce_96 = get_tag_random("nonce_128", 12) |
| 54 |
data = get_tag_random("data", 128) |
| 55 |
|
| 56 |
def test_loopback_128(self): |
| 57 |
cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96) |
| 58 |
pt = get_tag_random("plaintext", 16 * 100) |
| 59 |
ct = cipher.encrypt(pt) |
| 60 |
|
| 61 |
cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96) |
| 62 |
pt2 = cipher.decrypt(ct) |
| 63 |
self.assertEqual(pt, pt2) |
| 64 |
|
| 65 |
def test_nonce(self): |
| 66 |
|
| 67 |
AES.new(self.key_128, AES.MODE_GCM) |
| 68 |
|
| 69 |
cipher = AES.new(self.key_128, AES.MODE_GCM, self.nonce_96) |
| 70 |
ct = cipher.encrypt(self.data) |
| 71 |
|
| 72 |
cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96) |
| 73 |
self.assertEqual(ct, cipher.encrypt(self.data)) |
| 74 |
|
| 75 |
def test_nonce_must_be_bytes(self): |
| 76 |
self.assertRaises(TypeError, AES.new, self.key_128, AES.MODE_GCM, |
| 77 |
nonce=u'test12345678') |
| 78 |
|
| 79 |
def test_nonce_length(self): |
| 80 |
|
| 81 |
self.assertRaises(ValueError, AES.new, self.key_128, AES.MODE_GCM, |
| 82 |
nonce=b"") |
| 83 |
|
| 84 |
for x in range(1, 128): |
| 85 |
cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=bchr(1) * x) |
| 86 |
cipher.encrypt(bchr(1)) |
| 87 |
|
| 88 |
def test_block_size_128(self): |
| 89 |
cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96) |
| 90 |
self.assertEqual(cipher.block_size, AES.block_size) |
| 91 |
|
| 92 |
def test_nonce_attribute(self): |
| 93 |
cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96) |
| 94 |
self.assertEqual(cipher.nonce, self.nonce_96) |
| 95 |
|
| 96 |
|
| 97 |
nonce1 = AES.new(self.key_128, AES.MODE_GCM).nonce |
| 98 |
nonce2 = AES.new(self.key_128, AES.MODE_GCM).nonce |
| 99 |
self.assertEqual(len(nonce1), 16) |
| 100 |
self.assertNotEqual(nonce1, nonce2) |
| 101 |
|
| 102 |
def test_unknown_parameters(self): |
| 103 |
self.assertRaises(TypeError, AES.new, self.key_128, AES.MODE_GCM, |
| 104 |
self.nonce_96, 7) |
| 105 |
self.assertRaises(TypeError, AES.new, self.key_128, AES.MODE_GCM, |
| 106 |
nonce=self.nonce_96, unknown=7) |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96, |
| 111 |
use_aesni=False) |
| 112 |
|
| 113 |
def test_null_encryption_decryption(self): |
| 114 |
for func in "encrypt", "decrypt": |
| 115 |
cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96) |
| 116 |
result = getattr(cipher, func)(b"") |
| 117 |
self.assertEqual(result, b"") |
| 118 |
|
| 119 |
def test_either_encrypt_or_decrypt(self): |
| 120 |
cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96) |
| 121 |
cipher.encrypt(b"") |
| 122 |
self.assertRaises(TypeError, cipher.decrypt, b"") |
| 123 |
|
| 124 |
cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96) |
| 125 |
cipher.decrypt(b"") |
| 126 |
self.assertRaises(TypeError, cipher.encrypt, b"") |
| 127 |
|
| 128 |
def test_data_must_be_bytes(self): |
| 129 |
cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96) |
| 130 |
self.assertRaises(TypeError, cipher.encrypt, u'test1234567890-*') |
| 131 |
|
| 132 |
cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96) |
| 133 |
self.assertRaises(TypeError, cipher.decrypt, u'test1234567890-*') |
| 134 |
|
| 135 |
def test_mac_len(self): |
| 136 |
|
| 137 |
self.assertRaises(ValueError, AES.new, self.key_128, AES.MODE_GCM, |
| 138 |
nonce=self.nonce_96, mac_len=3) |
| 139 |
self.assertRaises(ValueError, AES.new, self.key_128, AES.MODE_GCM, |
| 140 |
nonce=self.nonce_96, mac_len=16+1) |
| 141 |
|
| 142 |
|
| 143 |
for mac_len in range(5, 16 + 1): |
| 144 |
cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96, |
| 145 |
mac_len=mac_len) |
| 146 |
_, mac = cipher.encrypt_and_digest(self.data) |
| 147 |
self.assertEqual(len(mac), mac_len) |
| 148 |
|
| 149 |
|
| 150 |
cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96) |
| 151 |
_, mac = cipher.encrypt_and_digest(self.data) |
| 152 |
self.assertEqual(len(mac), 16) |
| 153 |
|
| 154 |
def test_invalid_mac(self): |
| 155 |
from Crypto.Util.strxor import strxor_c |
| 156 |
cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96) |
| 157 |
ct, mac = cipher.encrypt_and_digest(self.data) |
| 158 |
|
| 159 |
invalid_mac = strxor_c(mac, 0x01) |
| 160 |
|
| 161 |
cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96) |
| 162 |
self.assertRaises(ValueError, cipher.decrypt_and_verify, ct, |
| 163 |
invalid_mac) |
| 164 |
|
| 165 |
def test_hex_mac(self): |
| 166 |
cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96) |
| 167 |
mac_hex = cipher.hexdigest() |
| 168 |
self.assertEqual(cipher.digest(), unhexlify(mac_hex)) |
| 169 |
|
| 170 |
cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96) |
| 171 |
cipher.hexverify(mac_hex) |
| 172 |
|
| 173 |
def test_message_chunks(self): |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
auth_data = get_tag_random("authenticated data", 127) |
| 178 |
plaintext = get_tag_random("plaintext", 127) |
| 179 |
|
| 180 |
cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96) |
| 181 |
cipher.update(auth_data) |
| 182 |
ciphertext, ref_mac = cipher.encrypt_and_digest(plaintext) |
| 183 |
|
| 184 |
def break_up(data, chunk_length): |
| 185 |
return [data[i:i+chunk_length] for i in range(0, len(data), |
| 186 |
chunk_length)] |
| 187 |
|
| 188 |
|
| 189 |
for chunk_length in 1, 2, 3, 7, 10, 13, 16, 40, 80, 128: |
| 190 |
|
| 191 |
cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96) |
| 192 |
|
| 193 |
for chunk in break_up(auth_data, chunk_length): |
| 194 |
cipher.update(chunk) |
| 195 |
pt2 = b"" |
| 196 |
for chunk in break_up(ciphertext, chunk_length): |
| 197 |
pt2 += cipher.decrypt(chunk) |
| 198 |
self.assertEqual(plaintext, pt2) |
| 199 |
cipher.verify(ref_mac) |
| 200 |
|
| 201 |
|
| 202 |
for chunk_length in 1, 2, 3, 7, 10, 13, 16, 40, 80, 128: |
| 203 |
|
| 204 |
cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96) |
| 205 |
|
| 206 |
for chunk in break_up(auth_data, chunk_length): |
| 207 |
cipher.update(chunk) |
| 208 |
ct2 = b"" |
| 209 |
for chunk in break_up(plaintext, chunk_length): |
| 210 |
ct2 += cipher.encrypt(chunk) |
| 211 |
self.assertEqual(ciphertext, ct2) |
| 212 |
self.assertEqual(cipher.digest(), ref_mac) |
| 213 |
|
| 214 |
def test_bytearray(self): |
| 215 |
|
| 216 |
|
| 217 |
key_ba = bytearray(self.key_128) |
| 218 |
nonce_ba = bytearray(self.nonce_96) |
| 219 |
header_ba = bytearray(self.data) |
| 220 |
data_ba = bytearray(self.data) |
| 221 |
|
| 222 |
cipher1 = AES.new(self.key_128, |
| 223 |
AES.MODE_GCM, |
| 224 |
nonce=self.nonce_96) |
| 225 |
cipher1.update(self.data) |
| 226 |
ct = cipher1.encrypt(self.data) |
| 227 |
tag = cipher1.digest() |
| 228 |
|
| 229 |
cipher2 = AES.new(key_ba, |
| 230 |
AES.MODE_GCM, |
| 231 |
nonce=nonce_ba) |
| 232 |
key_ba[:3] = b"\xFF\xFF\xFF" |
| 233 |
nonce_ba[:3] = b"\xFF\xFF\xFF" |
| 234 |
cipher2.update(header_ba) |
| 235 |
header_ba[:3] = b"\xFF\xFF\xFF" |
| 236 |
ct_test = cipher2.encrypt(data_ba) |
| 237 |
data_ba[:3] = b"\xFF\xFF\xFF" |
| 238 |
tag_test = cipher2.digest() |
| 239 |
|
| 240 |
self.assertEqual(ct, ct_test) |
| 241 |
self.assertEqual(tag, tag_test) |
| 242 |
self.assertEqual(cipher1.nonce, cipher2.nonce) |
| 243 |
|
| 244 |
|
| 245 |
key_ba = bytearray(self.key_128) |
| 246 |
nonce_ba = bytearray(self.nonce_96) |
| 247 |
header_ba = bytearray(self.data) |
| 248 |
del data_ba |
| 249 |
|
| 250 |
cipher4 = AES.new(key_ba, |
| 251 |
AES.MODE_GCM, |
| 252 |
nonce=nonce_ba) |
| 253 |
key_ba[:3] = b"\xFF\xFF\xFF" |
| 254 |
nonce_ba[:3] = b"\xFF\xFF\xFF" |
| 255 |
cipher4.update(header_ba) |
| 256 |
header_ba[:3] = b"\xFF\xFF\xFF" |
| 257 |
pt_test = cipher4.decrypt_and_verify(bytearray(ct_test), bytearray(tag_test)) |
| 258 |
|
| 259 |
self.assertEqual(self.data, pt_test) |
| 260 |
|
| 261 |
def test_memoryview(self): |
| 262 |
|
| 263 |
|
| 264 |
key_mv = memoryview(bytearray(self.key_128)) |
| 265 |
nonce_mv = memoryview(bytearray(self.nonce_96)) |
| 266 |
header_mv = memoryview(bytearray(self.data)) |
| 267 |
data_mv = memoryview(bytearray(self.data)) |
| 268 |
|
| 269 |
cipher1 = AES.new(self.key_128, |
| 270 |
AES.MODE_GCM, |
| 271 |
nonce=self.nonce_96) |
| 272 |
cipher1.update(self.data) |
| 273 |
ct = cipher1.encrypt(self.data) |
| 274 |
tag = cipher1.digest() |
| 275 |
|
| 276 |
cipher2 = AES.new(key_mv, |
| 277 |
AES.MODE_GCM, |
| 278 |
nonce=nonce_mv) |
| 279 |
key_mv[:3] = b"\xFF\xFF\xFF" |
| 280 |
nonce_mv[:3] = b"\xFF\xFF\xFF" |
| 281 |
cipher2.update(header_mv) |
| 282 |
header_mv[:3] = b"\xFF\xFF\xFF" |
| 283 |
ct_test = cipher2.encrypt(data_mv) |
| 284 |
data_mv[:3] = b"\xFF\xFF\xFF" |
| 285 |
tag_test = cipher2.digest() |
| 286 |
|
| 287 |
self.assertEqual(ct, ct_test) |
| 288 |
self.assertEqual(tag, tag_test) |
| 289 |
self.assertEqual(cipher1.nonce, cipher2.nonce) |
| 290 |
|
| 291 |
|
| 292 |
key_mv = memoryview(bytearray(self.key_128)) |
| 293 |
nonce_mv = memoryview(bytearray(self.nonce_96)) |
| 294 |
header_mv = memoryview(bytearray(self.data)) |
| 295 |
del data_mv |
| 296 |
|
| 297 |
cipher4 = AES.new(key_mv, |
| 298 |
AES.MODE_GCM, |
| 299 |
nonce=nonce_mv) |
| 300 |
key_mv[:3] = b"\xFF\xFF\xFF" |
| 301 |
nonce_mv[:3] = b"\xFF\xFF\xFF" |
| 302 |
cipher4.update(header_mv) |
| 303 |
header_mv[:3] = b"\xFF\xFF\xFF" |
| 304 |
pt_test = cipher4.decrypt_and_verify(memoryview(ct_test), memoryview(tag_test)) |
| 305 |
|
| 306 |
self.assertEqual(self.data, pt_test) |
| 307 |
|
| 308 |
def test_output_param(self): |
| 309 |
|
| 310 |
pt = b'5' * 128 |
| 311 |
cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96) |
| 312 |
ct = cipher.encrypt(pt) |
| 313 |
tag = cipher.digest() |
| 314 |
|
| 315 |
output = bytearray(128) |
| 316 |
cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96) |
| 317 |
res = cipher.encrypt(pt, output=output) |
| 318 |
self.assertEqual(ct, output) |
| 319 |
self.assertEqual(res, None) |
| 320 |
|
| 321 |
cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96) |
| 322 |
res = cipher.decrypt(ct, output=output) |
| 323 |
self.assertEqual(pt, output) |
| 324 |
self.assertEqual(res, None) |
| 325 |
|
| 326 |
cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96) |
| 327 |
res, tag_out = cipher.encrypt_and_digest(pt, output=output) |
| 328 |
self.assertEqual(ct, output) |
| 329 |
self.assertEqual(res, None) |
| 330 |
self.assertEqual(tag, tag_out) |
| 331 |
|
| 332 |
cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96) |
| 333 |
res = cipher.decrypt_and_verify(ct, tag, output=output) |
| 334 |
self.assertEqual(pt, output) |
| 335 |
self.assertEqual(res, None) |
| 336 |
|
| 337 |
def test_output_param_memoryview(self): |
| 338 |
|
| 339 |
pt = b'5' * 128 |
| 340 |
cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96) |
| 341 |
ct = cipher.encrypt(pt) |
| 342 |
|
| 343 |
output = memoryview(bytearray(128)) |
| 344 |
cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96) |
| 345 |
cipher.encrypt(pt, output=output) |
| 346 |
self.assertEqual(ct, output) |
| 347 |
|
| 348 |
cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96) |
| 349 |
cipher.decrypt(ct, output=output) |
| 350 |
self.assertEqual(pt, output) |
| 351 |
|
| 352 |
def test_output_param_neg(self): |
| 353 |
LEN_PT = 128 |
| 354 |
|
| 355 |
pt = b'5' * LEN_PT |
| 356 |
cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96) |
| 357 |
ct = cipher.encrypt(pt) |
| 358 |
|
| 359 |
cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96) |
| 360 |
self.assertRaises(TypeError, cipher.encrypt, pt, output=b'0' * LEN_PT) |
| 361 |
|
| 362 |
cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96) |
| 363 |
self.assertRaises(TypeError, cipher.decrypt, ct, output=b'0' * LEN_PT) |
| 364 |
|
| 365 |
shorter_output = bytearray(LEN_PT - 1) |
| 366 |
cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96) |
| 367 |
self.assertRaises(ValueError, cipher.encrypt, pt, output=shorter_output) |
| 368 |
cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96) |
| 369 |
self.assertRaises(ValueError, cipher.decrypt, ct, output=shorter_output) |
| 370 |
|
| 371 |
|
| 372 |
class GcmFSMTests(unittest.TestCase): |
| 373 |
|
| 374 |
key_128 = get_tag_random("key_128", 16) |
| 375 |
nonce_96 = get_tag_random("nonce_128", 12) |
| 376 |
data = get_tag_random("data", 128) |
| 377 |
|
| 378 |
def test_valid_init_encrypt_decrypt_digest_verify(self): |
| 379 |
|
| 380 |
|
| 381 |
cipher = AES.new(self.key_128, AES.MODE_GCM, |
| 382 |
nonce=self.nonce_96) |
| 383 |
ct = cipher.encrypt(self.data) |
| 384 |
mac = cipher.digest() |
| 385 |
|
| 386 |
|
| 387 |
cipher = AES.new(self.key_128, AES.MODE_GCM, |
| 388 |
nonce=self.nonce_96) |
| 389 |
cipher.decrypt(ct) |
| 390 |
cipher.verify(mac) |
| 391 |
|
| 392 |
def test_valid_init_update_digest_verify(self): |
| 393 |
|
| 394 |
|
| 395 |
cipher = AES.new(self.key_128, AES.MODE_GCM, |
| 396 |
nonce=self.nonce_96) |
| 397 |
cipher.update(self.data) |
| 398 |
mac = cipher.digest() |
| 399 |
|
| 400 |
|
| 401 |
cipher = AES.new(self.key_128, AES.MODE_GCM, |
| 402 |
nonce=self.nonce_96) |
| 403 |
cipher.update(self.data) |
| 404 |
cipher.verify(mac) |
| 405 |
|
| 406 |
def test_valid_full_path(self): |
| 407 |
|
| 408 |
|
| 409 |
cipher = AES.new(self.key_128, AES.MODE_GCM, |
| 410 |
nonce=self.nonce_96) |
| 411 |
cipher.update(self.data) |
| 412 |
ct = cipher.encrypt(self.data) |
| 413 |
mac = cipher.digest() |
| 414 |
|
| 415 |
|
| 416 |
cipher = AES.new(self.key_128, AES.MODE_GCM, |
| 417 |
nonce=self.nonce_96) |
| 418 |
cipher.update(self.data) |
| 419 |
cipher.decrypt(ct) |
| 420 |
cipher.verify(mac) |
| 421 |
|
| 422 |
def test_valid_init_digest(self): |
| 423 |
|
| 424 |
cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96) |
| 425 |
cipher.digest() |
| 426 |
|
| 427 |
def test_valid_init_verify(self): |
| 428 |
|
| 429 |
cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96) |
| 430 |
mac = cipher.digest() |
| 431 |
|
| 432 |
cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96) |
| 433 |
cipher.verify(mac) |
| 434 |
|
| 435 |
def test_valid_multiple_encrypt_or_decrypt(self): |
| 436 |
for method_name in "encrypt", "decrypt": |
| 437 |
for auth_data in (None, b"333", self.data, |
| 438 |
self.data + b"3"): |
| 439 |
if auth_data is None: |
| 440 |
assoc_len = None |
| 441 |
else: |
| 442 |
assoc_len = len(auth_data) |
| 443 |
cipher = AES.new(self.key_128, AES.MODE_GCM, |
| 444 |
nonce=self.nonce_96) |
| 445 |
if auth_data is not None: |
| 446 |
cipher.update(auth_data) |
| 447 |
method = getattr(cipher, method_name) |
| 448 |
method(self.data) |
| 449 |
method(self.data) |
| 450 |
method(self.data) |
| 451 |
method(self.data) |
| 452 |
|
| 453 |
def test_valid_multiple_digest_or_verify(self): |
| 454 |
|
| 455 |
cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96) |
| 456 |
cipher.update(self.data) |
| 457 |
first_mac = cipher.digest() |
| 458 |
for x in range(4): |
| 459 |
self.assertEqual(first_mac, cipher.digest()) |
| 460 |
|
| 461 |
|
| 462 |
cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96) |
| 463 |
cipher.update(self.data) |
| 464 |
for x in range(5): |
| 465 |
cipher.verify(first_mac) |
| 466 |
|
| 467 |
def test_valid_encrypt_and_digest_decrypt_and_verify(self): |
| 468 |
|
| 469 |
cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96) |
| 470 |
cipher.update(self.data) |
| 471 |
ct, mac = cipher.encrypt_and_digest(self.data) |
| 472 |
|
| 473 |
|
| 474 |
cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96) |
| 475 |
cipher.update(self.data) |
| 476 |
pt = cipher.decrypt_and_verify(ct, mac) |
| 477 |
self.assertEqual(self.data, pt) |
| 478 |
|
| 479 |
def test_invalid_mixing_encrypt_decrypt(self): |
| 480 |
|
| 481 |
for method1_name, method2_name in (("encrypt", "decrypt"), |
| 482 |
("decrypt", "encrypt")): |
| 483 |
for assoc_data_present in (True, False): |
| 484 |
cipher = AES.new(self.key_128, AES.MODE_GCM, |
| 485 |
nonce=self.nonce_96) |
| 486 |
if assoc_data_present: |
| 487 |
cipher.update(self.data) |
| 488 |
getattr(cipher, method1_name)(self.data) |
| 489 |
self.assertRaises(TypeError, getattr(cipher, method2_name), |
| 490 |
self.data) |
| 491 |
|
| 492 |
def test_invalid_encrypt_or_update_after_digest(self): |
| 493 |
for method_name in "encrypt", "update": |
| 494 |
cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96) |
| 495 |
cipher.encrypt(self.data) |
| 496 |
cipher.digest() |
| 497 |
self.assertRaises(TypeError, getattr(cipher, method_name), |
| 498 |
self.data) |
| 499 |
|
| 500 |
cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96) |
| 501 |
cipher.encrypt_and_digest(self.data) |
| 502 |
|
| 503 |
def test_invalid_decrypt_or_update_after_verify(self): |
| 504 |
cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96) |
| 505 |
ct = cipher.encrypt(self.data) |
| 506 |
mac = cipher.digest() |
| 507 |
|
| 508 |
for method_name in "decrypt", "update": |
| 509 |
cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96) |
| 510 |
cipher.decrypt(ct) |
| 511 |
cipher.verify(mac) |
| 512 |
self.assertRaises(TypeError, getattr(cipher, method_name), |
| 513 |
self.data) |
| 514 |
|
| 515 |
cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96) |
| 516 |
cipher.decrypt_and_verify(ct, mac) |
| 517 |
self.assertRaises(TypeError, getattr(cipher, method_name), |
| 518 |
self.data) |
| 519 |
|
| 520 |
|
| 521 |
class TestVectors(unittest.TestCase): |
| 522 |
"""Class exercising the GCM test vectors found in |
| 523 |
http://csrc.nist.gov/groups/ST/toolkit/BCM/documents/proposedmodes/gcm/gcm-revised-spec.pdf""" |
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
test_vectors_hex = [ |
| 533 |
( |
| 534 |
'', |
| 535 |
'', |
| 536 |
'', |
| 537 |
'58e2fccefa7e3061367f1d57a4e7455a', |
| 538 |
'00000000000000000000000000000000', |
| 539 |
'000000000000000000000000' |
| 540 |
), |
| 541 |
( |
| 542 |
'', |
| 543 |
'00000000000000000000000000000000', |
| 544 |
'0388dace60b6a392f328c2b971b2fe78', |
| 545 |
'ab6e47d42cec13bdf53a67b21257bddf', |
| 546 |
'00000000000000000000000000000000', |
| 547 |
'000000000000000000000000' |
| 548 |
), |
| 549 |
( |
| 550 |
'', |
| 551 |
'd9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a72' + |
| 552 |
'1c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b391aafd255', |
| 553 |
'42831ec2217774244b7221b784d0d49ce3aa212f2c02a4e035c17e2329aca12e' + |
| 554 |
'21d514b25466931c7d8f6a5aac84aa051ba30b396a0aac973d58e091473f5985', |
| 555 |
'4d5c2af327cd64a62cf35abd2ba6fab4', |
| 556 |
'feffe9928665731c6d6a8f9467308308', |
| 557 |
'cafebabefacedbaddecaf888' |
| 558 |
), |
| 559 |
( |
| 560 |
'feedfacedeadbeeffeedfacedeadbeefabaddad2', |
| 561 |
'd9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a72' + |
| 562 |
'1c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39', |
| 563 |
'42831ec2217774244b7221b784d0d49ce3aa212f2c02a4e035c17e2329aca12e' + |
| 564 |
'21d514b25466931c7d8f6a5aac84aa051ba30b396a0aac973d58e091', |
| 565 |
'5bc94fbc3221a5db94fae95ae7121a47', |
| 566 |
'feffe9928665731c6d6a8f9467308308', |
| 567 |
'cafebabefacedbaddecaf888' |
| 568 |
), |
| 569 |
( |
| 570 |
'feedfacedeadbeeffeedfacedeadbeefabaddad2', |
| 571 |
'd9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a72' + |
| 572 |
'1c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39', |
| 573 |
'61353b4c2806934a777ff51fa22a4755699b2a714fcdc6f83766e5f97b6c7423' + |
| 574 |
'73806900e49f24b22b097544d4896b424989b5e1ebac0f07c23f4598', |
| 575 |
'3612d2e79e3b0785561be14aaca2fccb', |
| 576 |
'feffe9928665731c6d6a8f9467308308', |
| 577 |
'cafebabefacedbad' |
| 578 |
), |
| 579 |
( |
| 580 |
'feedfacedeadbeeffeedfacedeadbeefabaddad2', |
| 581 |
'd9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a72' + |
| 582 |
'1c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39', |
| 583 |
'8ce24998625615b603a033aca13fb894be9112a5c3a211a8ba262a3cca7e2ca7' + |
| 584 |
'01e4a9a4fba43c90ccdcb281d48c7c6fd62875d2aca417034c34aee5', |
| 585 |
'619cc5aefffe0bfa462af43c1699d050', |
| 586 |
'feffe9928665731c6d6a8f9467308308', |
| 587 |
'9313225df88406e555909c5aff5269aa' + |
| 588 |
'6a7a9538534f7da1e4c303d2a318a728c3c0c95156809539fcf0e2429a6b5254' + |
| 589 |
'16aedbf5a0de6a57a637b39b' |
| 590 |
), |
| 591 |
( |
| 592 |
'', |
| 593 |
'', |
| 594 |
'', |
| 595 |
'cd33b28ac773f74ba00ed1f312572435', |
| 596 |
'000000000000000000000000000000000000000000000000', |
| 597 |
'000000000000000000000000' |
| 598 |
), |
| 599 |
( |
| 600 |
'', |
| 601 |
'00000000000000000000000000000000', |
| 602 |
'98e7247c07f0fe411c267e4384b0f600', |
| 603 |
'2ff58d80033927ab8ef4d4587514f0fb', |
| 604 |
'000000000000000000000000000000000000000000000000', |
| 605 |
'000000000000000000000000' |
| 606 |
), |
| 607 |
( |
| 608 |
'', |
| 609 |
'd9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a72' + |
| 610 |
'1c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b391aafd255', |
| 611 |
'3980ca0b3c00e841eb06fac4872a2757859e1ceaa6efd984628593b40ca1e19c' + |
| 612 |
'7d773d00c144c525ac619d18c84a3f4718e2448b2fe324d9ccda2710acade256', |
| 613 |
'9924a7c8587336bfb118024db8674a14', |
| 614 |
'feffe9928665731c6d6a8f9467308308feffe9928665731c', |
| 615 |
'cafebabefacedbaddecaf888' |
| 616 |
), |
| 617 |
( |
| 618 |
'feedfacedeadbeeffeedfacedeadbeefabaddad2', |
| 619 |
'd9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a72' + |
| 620 |
'1c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39', |
| 621 |
'3980ca0b3c00e841eb06fac4872a2757859e1ceaa6efd984628593b40ca1e19c' + |
| 622 |
'7d773d00c144c525ac619d18c84a3f4718e2448b2fe324d9ccda2710', |
| 623 |
'2519498e80f1478f37ba55bd6d27618c', |
| 624 |
'feffe9928665731c6d6a8f9467308308feffe9928665731c', |
| 625 |
'cafebabefacedbaddecaf888' |
| 626 |
), |
| 627 |
( |
| 628 |
'feedfacedeadbeeffeedfacedeadbeefabaddad2', |
| 629 |
'd9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a72' + |
| 630 |
'1c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39', |
| 631 |
'0f10f599ae14a154ed24b36e25324db8c566632ef2bbb34f8347280fc4507057' + |
| 632 |
'fddc29df9a471f75c66541d4d4dad1c9e93a19a58e8b473fa0f062f7', |
| 633 |
'65dcc57fcf623a24094fcca40d3533f8', |
| 634 |
'feffe9928665731c6d6a8f9467308308feffe9928665731c', |
| 635 |
'cafebabefacedbad' |
| 636 |
), |
| 637 |
( |
| 638 |
'feedfacedeadbeeffeedfacedeadbeefabaddad2', |
| 639 |
'd9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a72' + |
| 640 |
'1c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39', |
| 641 |
'd27e88681ce3243c4830165a8fdcf9ff1de9a1d8e6b447ef6ef7b79828666e45' + |
| 642 |
'81e79012af34ddd9e2f037589b292db3e67c036745fa22e7e9b7373b', |
| 643 |
'dcf566ff291c25bbb8568fc3d376a6d9', |
| 644 |
'feffe9928665731c6d6a8f9467308308feffe9928665731c', |
| 645 |
'9313225df88406e555909c5aff5269aa' + |
| 646 |
'6a7a9538534f7da1e4c303d2a318a728c3c0c95156809539fcf0e2429a6b5254' + |
| 647 |
'16aedbf5a0de6a57a637b39b' |
| 648 |
), |
| 649 |
( |
| 650 |
'', |
| 651 |
'', |
| 652 |
'', |
| 653 |
'530f8afbc74536b9a963b4f1c4cb738b', |
| 654 |
'0000000000000000000000000000000000000000000000000000000000000000', |
| 655 |
'000000000000000000000000' |
| 656 |
), |
| 657 |
( |
| 658 |
'', |
| 659 |
'00000000000000000000000000000000', |
| 660 |
'cea7403d4d606b6e074ec5d3baf39d18', |
| 661 |
'd0d1c8a799996bf0265b98b5d48ab919', |
| 662 |
'0000000000000000000000000000000000000000000000000000000000000000', |
| 663 |
'000000000000000000000000' |
| 664 |
), |
| 665 |
( '', |
| 666 |
'd9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a72' + |
| 667 |
'1c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b391aafd255', |
| 668 |
'522dc1f099567d07f47f37a32a84427d643a8cdcbfe5c0c97598a2bd2555d1aa' + |
| 669 |
'8cb08e48590dbb3da7b08b1056828838c5f61e6393ba7a0abcc9f662898015ad', |
| 670 |
'b094dac5d93471bdec1a502270e3cc6c', |
| 671 |
'feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308', |
| 672 |
'cafebabefacedbaddecaf888' |
| 673 |
), |
| 674 |
( |
| 675 |
'feedfacedeadbeeffeedfacedeadbeefabaddad2', |
| 676 |
'd9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a72' + |
| 677 |
'1c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39', |
| 678 |
'522dc1f099567d07f47f37a32a84427d643a8cdcbfe5c0c97598a2bd2555d1aa' + |
| 679 |
'8cb08e48590dbb3da7b08b1056828838c5f61e6393ba7a0abcc9f662', |
| 680 |
'76fc6ece0f4e1768cddf8853bb2d551b', |
| 681 |
'feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308', |
| 682 |
'cafebabefacedbaddecaf888' |
| 683 |
), |
| 684 |
( |
| 685 |
'feedfacedeadbeeffeedfacedeadbeefabaddad2', |
| 686 |
'd9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a72' + |
| 687 |
'1c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39', |
| 688 |
'c3762df1ca787d32ae47c13bf19844cbaf1ae14d0b976afac52ff7d79bba9de0' + |
| 689 |
'feb582d33934a4f0954cc2363bc73f7862ac430e64abe499f47c9b1f', |
| 690 |
'3a337dbf46a792c45e454913fe2ea8f2', |
| 691 |
'feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308', |
| 692 |
'cafebabefacedbad' |
| 693 |
), |
| 694 |
( |
| 695 |
'feedfacedeadbeeffeedfacedeadbeefabaddad2', |
| 696 |
'd9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a72' + |
| 697 |
'1c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39', |
| 698 |
'5a8def2f0c9e53f1f75d7853659e2a20eeb2b22aafde6419a058ab4f6f746bf4' + |
| 699 |
'0fc0c3b780f244452da3ebf1c5d82cdea2418997200ef82e44ae7e3f', |
| 700 |
'a44a8266ee1c8eb0c8b5d4cf5ae9f19a', |
| 701 |
'feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308', |
| 702 |
'9313225df88406e555909c5aff5269aa' + |
| 703 |
'6a7a9538534f7da1e4c303d2a318a728c3c0c95156809539fcf0e2429a6b5254' + |
| 704 |
'16aedbf5a0de6a57a637b39b' |
| 705 |
) |
| 706 |
] |
| 707 |
|
| 708 |
test_vectors = [[unhexlify(x) for x in tv] for tv in test_vectors_hex] |
| 709 |
|
| 710 |
def runTest(self): |
| 711 |
for assoc_data, pt, ct, mac, key, nonce in self.test_vectors: |
| 712 |
|
| 713 |
|
| 714 |
cipher = AES.new(key, AES.MODE_GCM, nonce, mac_len=len(mac)) |
| 715 |
cipher.update(assoc_data) |
| 716 |
ct2, mac2 = cipher.encrypt_and_digest(pt) |
| 717 |
self.assertEqual(ct, ct2) |
| 718 |
self.assertEqual(mac, mac2) |
| 719 |
|
| 720 |
|
| 721 |
cipher = AES.new(key, AES.MODE_GCM, nonce, mac_len=len(mac)) |
| 722 |
cipher.update(assoc_data) |
| 723 |
pt2 = cipher.decrypt_and_verify(ct, mac) |
| 724 |
self.assertEqual(pt, pt2) |
| 725 |
|
| 726 |
|
| 727 |
class TestVectorsGueronKrasnov(unittest.TestCase): |
| 728 |
"""Class exercising the GCM test vectors found in |
| 729 |
'The fragility of AES-GCM authentication algorithm', Gueron, Krasnov |
| 730 |
https://eprint.iacr.org/2013/157.pdf""" |
| 731 |
|
| 732 |
def test_1(self): |
| 733 |
key = unhexlify("3da6c536d6295579c0959a7043efb503") |
| 734 |
iv = unhexlify("2b926197d34e091ef722db94") |
| 735 |
aad = unhexlify("00000000000000000000000000000000" + |
| 736 |
"000102030405060708090a0b0c0d0e0f" + |
| 737 |
"101112131415161718191a1b1c1d1e1f" + |
| 738 |
"202122232425262728292a2b2c2d2e2f" + |
| 739 |
"303132333435363738393a3b3c3d3e3f") |
| 740 |
digest = unhexlify("69dd586555ce3fcc89663801a71d957b") |
| 741 |
|
| 742 |
cipher = AES.new(key, AES.MODE_GCM, iv).update(aad) |
| 743 |
self.assertEqual(digest, cipher.digest()) |
| 744 |
|
| 745 |
def test_2(self): |
| 746 |
key = unhexlify("843ffcf5d2b72694d19ed01d01249412") |
| 747 |
iv = unhexlify("dbcca32ebf9b804617c3aa9e") |
| 748 |
aad = unhexlify("00000000000000000000000000000000" + |
| 749 |
"101112131415161718191a1b1c1d1e1f") |
| 750 |
pt = unhexlify("000102030405060708090a0b0c0d0e0f" + |
| 751 |
"101112131415161718191a1b1c1d1e1f" + |
| 752 |
"202122232425262728292a2b2c2d2e2f" + |
| 753 |
"303132333435363738393a3b3c3d3e3f" + |
| 754 |
"404142434445464748494a4b4c4d4e4f") |
| 755 |
ct = unhexlify("6268c6fa2a80b2d137467f092f657ac0" + |
| 756 |
"4d89be2beaa623d61b5a868c8f03ff95" + |
| 757 |
"d3dcee23ad2f1ab3a6c80eaf4b140eb0" + |
| 758 |
"5de3457f0fbc111a6b43d0763aa422a3" + |
| 759 |
"013cf1dc37fe417d1fbfc449b75d4cc5") |
| 760 |
digest = unhexlify("3b629ccfbc1119b7319e1dce2cd6fd6d") |
| 761 |
|
| 762 |
cipher = AES.new(key, AES.MODE_GCM, iv).update(aad) |
| 763 |
ct2, digest2 = cipher.encrypt_and_digest(pt) |
| 764 |
|
| 765 |
self.assertEqual(ct, ct2) |
| 766 |
self.assertEqual(digest, digest2) |
| 767 |
|
| 768 |
|
| 769 |
class NISTTestVectorsGCM(unittest.TestCase): |
| 770 |
|
| 771 |
def __init__(self, a): |
| 772 |
self.use_clmul = True |
| 773 |
unittest.TestCase.__init__(self, a) |
| 774 |
|
| 775 |
|
| 776 |
class NISTTestVectorsGCM_no_clmul(unittest.TestCase): |
| 777 |
|
| 778 |
def __init__(self, a): |
| 779 |
self.use_clmul = False |
| 780 |
unittest.TestCase.__init__(self, a) |
| 781 |
|
| 782 |
|
| 783 |
test_vectors_nist = load_test_vectors( |
| 784 |
("Cipher", "AES"), |
| 785 |
"gcmDecrypt128.rsp", |
| 786 |
"GCM decrypt", |
| 787 |
{"count": lambda x: int(x)}) or [] |
| 788 |
|
| 789 |
test_vectors_nist += load_test_vectors( |
| 790 |
("Cipher", "AES"), |
| 791 |
"gcmEncryptExtIV128.rsp", |
| 792 |
"GCM encrypt", |
| 793 |
{"count": lambda x: int(x)}) or [] |
| 794 |
|
| 795 |
for idx, tv in enumerate(test_vectors_nist): |
| 796 |
|
| 797 |
|
| 798 |
if isinstance(tv, str): |
| 799 |
continue |
| 800 |
|
| 801 |
def single_test(self, tv=tv): |
| 802 |
|
| 803 |
self.description = tv.desc |
| 804 |
cipher = AES.new(tv.key, AES.MODE_GCM, nonce=tv.iv, |
| 805 |
mac_len=len(tv.tag), use_clmul=self.use_clmul) |
| 806 |
cipher.update(tv.aad) |
| 807 |
if "FAIL" in tv.others: |
| 808 |
self.assertRaises(ValueError, cipher.decrypt_and_verify, |
| 809 |
tv.ct, tv.tag) |
| 810 |
else: |
| 811 |
pt = cipher.decrypt_and_verify(tv.ct, tv.tag) |
| 812 |
self.assertEqual(pt, tv.pt) |
| 813 |
|
| 814 |
setattr(NISTTestVectorsGCM, "test_%d" % idx, single_test) |
| 815 |
setattr(NISTTestVectorsGCM_no_clmul, "test_%d" % idx, single_test) |
| 816 |
|
| 817 |
|
| 818 |
class TestVectorsWycheproof(unittest.TestCase): |
| 819 |
|
| 820 |
def __init__(self, wycheproof_warnings, **extra_params): |
| 821 |
unittest.TestCase.__init__(self) |
| 822 |
self._wycheproof_warnings = wycheproof_warnings |
| 823 |
self._extra_params = extra_params |
| 824 |
self._id = "None" |
| 825 |
|
| 826 |
def setUp(self): |
| 827 |
|
| 828 |
def filter_tag(group): |
| 829 |
return group['tagSize'] // 8 |
| 830 |
|
| 831 |
self.tv = load_test_vectors_wycheproof(("Cipher", "wycheproof"), |
| 832 |
"aes_gcm_test.json", |
| 833 |
"Wycheproof GCM", |
| 834 |
group_tag={'tag_size': filter_tag}) |
| 835 |
|
| 836 |
def shortDescription(self): |
| 837 |
return self._id |
| 838 |
|
| 839 |
def warn(self, tv): |
| 840 |
if tv.warning and self._wycheproof_warnings: |
| 841 |
import warnings |
| 842 |
warnings.warn("Wycheproof warning: %s (%s)" % (self._id, tv.comment)) |
| 843 |
|
| 844 |
def test_encrypt(self, tv): |
| 845 |
self._id = "Wycheproof Encrypt GCM Test #" + str(tv.id) |
| 846 |
|
| 847 |
try: |
| 848 |
cipher = AES.new(tv.key, AES.MODE_GCM, tv.iv, mac_len=tv.tag_size, |
| 849 |
**self._extra_params) |
| 850 |
except ValueError as e: |
| 851 |
if len(tv.iv) == 0 and "Nonce cannot be empty" in str(e): |
| 852 |
return |
| 853 |
raise e |
| 854 |
|
| 855 |
cipher.update(tv.aad) |
| 856 |
ct, tag = cipher.encrypt_and_digest(tv.msg) |
| 857 |
if tv.valid: |
| 858 |
self.assertEqual(ct, tv.ct) |
| 859 |
self.assertEqual(tag, tv.tag) |
| 860 |
self.warn(tv) |
| 861 |
|
| 862 |
def test_decrypt(self, tv): |
| 863 |
self._id = "Wycheproof Decrypt GCM Test #" + str(tv.id) |
| 864 |
|
| 865 |
try: |
| 866 |
cipher = AES.new(tv.key, AES.MODE_GCM, tv.iv, mac_len=tv.tag_size, |
| 867 |
**self._extra_params) |
| 868 |
except ValueError as e: |
| 869 |
if len(tv.iv) == 0 and "Nonce cannot be empty" in str(e): |
| 870 |
return |
| 871 |
raise e |
| 872 |
|
| 873 |
cipher.update(tv.aad) |
| 874 |
try: |
| 875 |
pt = cipher.decrypt_and_verify(tv.ct, tv.tag) |
| 876 |
except ValueError: |
| 877 |
assert not tv.valid |
| 878 |
else: |
| 879 |
assert tv.valid |
| 880 |
self.assertEqual(pt, tv.msg) |
| 881 |
self.warn(tv) |
| 882 |
|
| 883 |
def test_corrupt_decrypt(self, tv): |
| 884 |
self._id = "Wycheproof Corrupt Decrypt GCM Test #" + str(tv.id) |
| 885 |
if len(tv.iv) == 0 or len(tv.ct) < 1: |
| 886 |
return |
| 887 |
cipher = AES.new(tv.key, AES.MODE_GCM, tv.iv, mac_len=tv.tag_size, |
| 888 |
**self._extra_params) |
| 889 |
cipher.update(tv.aad) |
| 890 |
ct_corrupt = strxor(tv.ct, b"\x00" * (len(tv.ct) - 1) + b"\x01") |
| 891 |
self.assertRaises(ValueError, cipher.decrypt_and_verify, ct_corrupt, tv.tag) |
| 892 |
|
| 893 |
def runTest(self): |
| 894 |
|
| 895 |
for tv in self.tv: |
| 896 |
self.test_encrypt(tv) |
| 897 |
self.test_decrypt(tv) |
| 898 |
self.test_corrupt_decrypt(tv) |
| 899 |
|
| 900 |
|
| 901 |
class TestVariableLength(unittest.TestCase): |
| 902 |
|
| 903 |
def __init__(self, **extra_params): |
| 904 |
unittest.TestCase.__init__(self) |
| 905 |
self._extra_params = extra_params |
| 906 |
|
| 907 |
def runTest(self): |
| 908 |
key = b'0' * 16 |
| 909 |
h = SHA256.new() |
| 910 |
|
| 911 |
for length in range(160): |
| 912 |
nonce = '{0:04d}'.format(length).encode('utf-8') |
| 913 |
data = bchr(length) * length |
| 914 |
cipher = AES.new(key, AES.MODE_GCM, nonce=nonce, **self._extra_params) |
| 915 |
ct, tag = cipher.encrypt_and_digest(data) |
| 916 |
h.update(ct) |
| 917 |
h.update(tag) |
| 918 |
|
| 919 |
self.assertEqual(h.hexdigest(), "7b7eb1ffbe67a2e53a912067c0ec8e62ebc7ce4d83490ea7426941349811bdf4") |
| 920 |
|
| 921 |
|
| 922 |
def get_tests(config={}): |
| 923 |
from Crypto.Util import _cpu_features |
| 924 |
|
| 925 |
wycheproof_warnings = config.get('wycheproof_warnings') |
| 926 |
|
| 927 |
tests = [] |
| 928 |
tests += list_test_cases(GcmTests) |
| 929 |
tests += list_test_cases(GcmFSMTests) |
| 930 |
tests += [TestVectors()] |
| 931 |
tests += [TestVectorsWycheproof(wycheproof_warnings)] |
| 932 |
tests += list_test_cases(TestVectorsGueronKrasnov) |
| 933 |
tests += [TestVariableLength()] |
| 934 |
if config.get('slow_tests'): |
| 935 |
tests += list_test_cases(NISTTestVectorsGCM) |
| 936 |
|
| 937 |
if _cpu_features.have_clmul(): |
| 938 |
tests += [TestVectorsWycheproof(wycheproof_warnings, use_clmul=False)] |
| 939 |
tests += [TestVariableLength(use_clmul=False)] |
| 940 |
if config.get('slow_tests'): |
| 941 |
tests += list_test_cases(NISTTestVectorsGCM_no_clmul) |
| 942 |
else: |
| 943 |
print("Skipping test of PCLMULDQD in AES GCM") |
| 944 |
|
| 945 |
return tests |
| 946 |
|
| 947 |
|
| 948 |
if __name__ == '__main__': |
| 949 |
def suite(): |
| 950 |
unittest.TestSuite(get_tests()) |
| 951 |
unittest.main(defaultTest='suite') |