| 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.SelfTest.loader import load_test_vectors_wycheproof |
| 36 |
from Crypto.Util.py3compat import tobytes, bchr |
| 37 |
from Crypto.Cipher import AES |
| 38 |
from Crypto.Hash import SHAKE128 |
| 39 |
|
| 40 |
from Crypto.Util.strxor import strxor |
| 41 |
|
| 42 |
|
| 43 |
def get_tag_random(tag, length): |
| 44 |
return SHAKE128.new(data=tobytes(tag)).read(length) |
| 45 |
|
| 46 |
|
| 47 |
class CcmTests(unittest.TestCase): |
| 48 |
|
| 49 |
key_128 = get_tag_random("key_128", 16) |
| 50 |
nonce_96 = get_tag_random("nonce_128", 12) |
| 51 |
data = get_tag_random("data", 128) |
| 52 |
|
| 53 |
def test_loopback_128(self): |
| 54 |
cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96) |
| 55 |
pt = get_tag_random("plaintext", 16 * 100) |
| 56 |
ct = cipher.encrypt(pt) |
| 57 |
|
| 58 |
cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96) |
| 59 |
pt2 = cipher.decrypt(ct) |
| 60 |
self.assertEqual(pt, pt2) |
| 61 |
|
| 62 |
def test_nonce(self): |
| 63 |
|
| 64 |
cipher = AES.new(self.key_128, AES.MODE_CCM) |
| 65 |
nonce1 = cipher.nonce |
| 66 |
cipher = AES.new(self.key_128, AES.MODE_CCM) |
| 67 |
nonce2 = cipher.nonce |
| 68 |
self.assertEqual(len(nonce1), 11) |
| 69 |
self.assertNotEqual(nonce1, nonce2) |
| 70 |
|
| 71 |
cipher = AES.new(self.key_128, AES.MODE_CCM, self.nonce_96) |
| 72 |
ct = cipher.encrypt(self.data) |
| 73 |
|
| 74 |
cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96) |
| 75 |
self.assertEqual(ct, cipher.encrypt(self.data)) |
| 76 |
|
| 77 |
def test_nonce_must_be_bytes(self): |
| 78 |
self.assertRaises(TypeError, AES.new, self.key_128, AES.MODE_CCM, |
| 79 |
nonce=u'test12345678') |
| 80 |
|
| 81 |
def test_nonce_length(self): |
| 82 |
self.assertRaises(ValueError, AES.new, self.key_128, AES.MODE_CCM, |
| 83 |
nonce=b"") |
| 84 |
self.assertRaises(ValueError, AES.new, self.key_128, AES.MODE_CCM, |
| 85 |
nonce=bchr(1) * 6) |
| 86 |
self.assertRaises(ValueError, AES.new, self.key_128, AES.MODE_CCM, |
| 87 |
nonce=bchr(1) * 14) |
| 88 |
for x in range(7, 13 + 1): |
| 89 |
AES.new(self.key_128, AES.MODE_CCM, nonce=bchr(1) * x) |
| 90 |
|
| 91 |
def test_block_size(self): |
| 92 |
cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96) |
| 93 |
self.assertEqual(cipher.block_size, AES.block_size) |
| 94 |
|
| 95 |
def test_nonce_attribute(self): |
| 96 |
cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96) |
| 97 |
self.assertEqual(cipher.nonce, self.nonce_96) |
| 98 |
|
| 99 |
|
| 100 |
nonce1 = AES.new(self.key_128, AES.MODE_CCM).nonce |
| 101 |
nonce2 = AES.new(self.key_128, AES.MODE_CCM).nonce |
| 102 |
self.assertEqual(len(nonce1), 11) |
| 103 |
self.assertNotEqual(nonce1, nonce2) |
| 104 |
|
| 105 |
def test_unknown_parameters(self): |
| 106 |
self.assertRaises(TypeError, AES.new, self.key_128, AES.MODE_CCM, |
| 107 |
self.nonce_96, 7) |
| 108 |
self.assertRaises(TypeError, AES.new, self.key_128, AES.MODE_CCM, |
| 109 |
nonce=self.nonce_96, unknown=7) |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96, |
| 114 |
use_aesni=False) |
| 115 |
|
| 116 |
def test_null_encryption_decryption(self): |
| 117 |
for func in "encrypt", "decrypt": |
| 118 |
cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96) |
| 119 |
result = getattr(cipher, func)(b"") |
| 120 |
self.assertEqual(result, b"") |
| 121 |
|
| 122 |
def test_either_encrypt_or_decrypt(self): |
| 123 |
cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96) |
| 124 |
cipher.encrypt(b"") |
| 125 |
self.assertRaises(TypeError, cipher.decrypt, b"") |
| 126 |
|
| 127 |
cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96) |
| 128 |
cipher.decrypt(b"") |
| 129 |
self.assertRaises(TypeError, cipher.encrypt, b"") |
| 130 |
|
| 131 |
def test_data_must_be_bytes(self): |
| 132 |
cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96) |
| 133 |
self.assertRaises(TypeError, cipher.encrypt, u'test1234567890-*') |
| 134 |
|
| 135 |
cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96) |
| 136 |
self.assertRaises(TypeError, cipher.decrypt, u'test1234567890-*') |
| 137 |
|
| 138 |
def test_mac_len(self): |
| 139 |
|
| 140 |
for mac_len in range(3, 17 + 1, 2): |
| 141 |
self.assertRaises(ValueError, AES.new, self.key_128, AES.MODE_CCM, |
| 142 |
nonce=self.nonce_96, mac_len=mac_len) |
| 143 |
|
| 144 |
|
| 145 |
for mac_len in range(4, 16 + 1, 2): |
| 146 |
cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96, |
| 147 |
mac_len=mac_len) |
| 148 |
_, mac = cipher.encrypt_and_digest(self.data) |
| 149 |
self.assertEqual(len(mac), mac_len) |
| 150 |
|
| 151 |
|
| 152 |
cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96) |
| 153 |
_, mac = cipher.encrypt_and_digest(self.data) |
| 154 |
self.assertEqual(len(mac), 16) |
| 155 |
|
| 156 |
def test_invalid_mac(self): |
| 157 |
from Crypto.Util.strxor import strxor_c |
| 158 |
cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96) |
| 159 |
ct, mac = cipher.encrypt_and_digest(self.data) |
| 160 |
|
| 161 |
invalid_mac = strxor_c(mac, 0x01) |
| 162 |
|
| 163 |
cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96) |
| 164 |
self.assertRaises(ValueError, cipher.decrypt_and_verify, ct, |
| 165 |
invalid_mac) |
| 166 |
|
| 167 |
def test_hex_mac(self): |
| 168 |
cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96) |
| 169 |
mac_hex = cipher.hexdigest() |
| 170 |
self.assertEqual(cipher.digest(), unhexlify(mac_hex)) |
| 171 |
|
| 172 |
cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96) |
| 173 |
cipher.hexverify(mac_hex) |
| 174 |
|
| 175 |
def test_longer_assoc_data_than_declared(self): |
| 176 |
|
| 177 |
cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96, |
| 178 |
assoc_len=0) |
| 179 |
self.assertRaises(ValueError, cipher.update, b"1") |
| 180 |
|
| 181 |
|
| 182 |
cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96, |
| 183 |
assoc_len=15) |
| 184 |
self.assertRaises(ValueError, cipher.update, self.data) |
| 185 |
|
| 186 |
def test_shorter_assoc_data_than_expected(self): |
| 187 |
DATA_LEN = len(self.data) |
| 188 |
|
| 189 |
|
| 190 |
cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96, |
| 191 |
assoc_len=DATA_LEN + 1) |
| 192 |
cipher.update(self.data) |
| 193 |
self.assertRaises(ValueError, cipher.encrypt, self.data) |
| 194 |
|
| 195 |
|
| 196 |
cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96, |
| 197 |
assoc_len=DATA_LEN + 1) |
| 198 |
cipher.update(self.data) |
| 199 |
self.assertRaises(ValueError, cipher.digest) |
| 200 |
|
| 201 |
|
| 202 |
cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96, |
| 203 |
assoc_len=DATA_LEN + 1) |
| 204 |
cipher.update(self.data) |
| 205 |
self.assertRaises(ValueError, cipher.decrypt, self.data) |
| 206 |
|
| 207 |
|
| 208 |
cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96) |
| 209 |
cipher.update(self.data) |
| 210 |
mac = cipher.digest() |
| 211 |
|
| 212 |
cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96, |
| 213 |
assoc_len=DATA_LEN + 1) |
| 214 |
cipher.update(self.data) |
| 215 |
self.assertRaises(ValueError, cipher.verify, mac) |
| 216 |
|
| 217 |
def test_shorter_and_longer_plaintext_than_declared(self): |
| 218 |
DATA_LEN = len(self.data) |
| 219 |
|
| 220 |
cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96, |
| 221 |
msg_len=DATA_LEN + 1) |
| 222 |
cipher.encrypt(self.data) |
| 223 |
self.assertRaises(ValueError, cipher.digest) |
| 224 |
|
| 225 |
cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96, |
| 226 |
msg_len=DATA_LEN - 1) |
| 227 |
self.assertRaises(ValueError, cipher.encrypt, self.data) |
| 228 |
|
| 229 |
def test_shorter_ciphertext_than_declared(self): |
| 230 |
DATA_LEN = len(self.data) |
| 231 |
|
| 232 |
cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96) |
| 233 |
ct, mac = cipher.encrypt_and_digest(self.data) |
| 234 |
|
| 235 |
cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96, |
| 236 |
msg_len=DATA_LEN + 1) |
| 237 |
cipher.decrypt(ct) |
| 238 |
self.assertRaises(ValueError, cipher.verify, mac) |
| 239 |
|
| 240 |
cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96, |
| 241 |
msg_len=DATA_LEN - 1) |
| 242 |
self.assertRaises(ValueError, cipher.decrypt, ct) |
| 243 |
|
| 244 |
def test_message_chunks(self): |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
auth_data = get_tag_random("authenticated data", 127) |
| 249 |
plaintext = get_tag_random("plaintext", 127) |
| 250 |
|
| 251 |
cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96) |
| 252 |
cipher.update(auth_data) |
| 253 |
ciphertext, ref_mac = cipher.encrypt_and_digest(plaintext) |
| 254 |
|
| 255 |
def break_up(data, chunk_length): |
| 256 |
return [data[i:i+chunk_length] for i in range(0, len(data), |
| 257 |
chunk_length)] |
| 258 |
|
| 259 |
|
| 260 |
for chunk_length in 1, 2, 3, 7, 10, 13, 16, 40, 80, 128: |
| 261 |
|
| 262 |
cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96, |
| 263 |
msg_len=127, assoc_len=127) |
| 264 |
|
| 265 |
for chunk in break_up(auth_data, chunk_length): |
| 266 |
cipher.update(chunk) |
| 267 |
pt2 = b"" |
| 268 |
for chunk in break_up(ciphertext, chunk_length): |
| 269 |
pt2 += cipher.decrypt(chunk) |
| 270 |
self.assertEqual(plaintext, pt2) |
| 271 |
cipher.verify(ref_mac) |
| 272 |
|
| 273 |
|
| 274 |
for chunk_length in 1, 2, 3, 7, 10, 13, 16, 40, 80, 128: |
| 275 |
|
| 276 |
cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96, |
| 277 |
msg_len=127, assoc_len=127) |
| 278 |
|
| 279 |
for chunk in break_up(auth_data, chunk_length): |
| 280 |
cipher.update(chunk) |
| 281 |
ct2 = b"" |
| 282 |
for chunk in break_up(plaintext, chunk_length): |
| 283 |
ct2 += cipher.encrypt(chunk) |
| 284 |
self.assertEqual(ciphertext, ct2) |
| 285 |
self.assertEqual(cipher.digest(), ref_mac) |
| 286 |
|
| 287 |
def test_bytearray(self): |
| 288 |
|
| 289 |
|
| 290 |
key_ba = bytearray(self.key_128) |
| 291 |
nonce_ba = bytearray(self.nonce_96) |
| 292 |
header_ba = bytearray(self.data) |
| 293 |
data_ba = bytearray(self.data) |
| 294 |
|
| 295 |
cipher1 = AES.new(self.key_128, |
| 296 |
AES.MODE_CCM, |
| 297 |
nonce=self.nonce_96) |
| 298 |
cipher1.update(self.data) |
| 299 |
ct = cipher1.encrypt(self.data) |
| 300 |
tag = cipher1.digest() |
| 301 |
|
| 302 |
cipher2 = AES.new(key_ba, |
| 303 |
AES.MODE_CCM, |
| 304 |
nonce=nonce_ba) |
| 305 |
key_ba[:3] = b"\xFF\xFF\xFF" |
| 306 |
nonce_ba[:3] = b"\xFF\xFF\xFF" |
| 307 |
cipher2.update(header_ba) |
| 308 |
header_ba[:3] = b"\xFF\xFF\xFF" |
| 309 |
ct_test = cipher2.encrypt(data_ba) |
| 310 |
data_ba[:3] = b"\xFF\xFF\xFF" |
| 311 |
tag_test = cipher2.digest() |
| 312 |
|
| 313 |
self.assertEqual(ct, ct_test) |
| 314 |
self.assertEqual(tag, tag_test) |
| 315 |
self.assertEqual(cipher1.nonce, cipher2.nonce) |
| 316 |
|
| 317 |
|
| 318 |
key_ba = bytearray(self.key_128) |
| 319 |
nonce_ba = bytearray(self.nonce_96) |
| 320 |
header_ba = bytearray(self.data) |
| 321 |
del data_ba |
| 322 |
|
| 323 |
cipher4 = AES.new(key_ba, |
| 324 |
AES.MODE_CCM, |
| 325 |
nonce=nonce_ba) |
| 326 |
key_ba[:3] = b"\xFF\xFF\xFF" |
| 327 |
nonce_ba[:3] = b"\xFF\xFF\xFF" |
| 328 |
cipher4.update(header_ba) |
| 329 |
header_ba[:3] = b"\xFF\xFF\xFF" |
| 330 |
pt_test = cipher4.decrypt_and_verify(bytearray(ct_test), bytearray(tag_test)) |
| 331 |
|
| 332 |
self.assertEqual(self.data, pt_test) |
| 333 |
|
| 334 |
def test_memoryview(self): |
| 335 |
|
| 336 |
|
| 337 |
key_mv = memoryview(bytearray(self.key_128)) |
| 338 |
nonce_mv = memoryview(bytearray(self.nonce_96)) |
| 339 |
header_mv = memoryview(bytearray(self.data)) |
| 340 |
data_mv = memoryview(bytearray(self.data)) |
| 341 |
|
| 342 |
cipher1 = AES.new(self.key_128, |
| 343 |
AES.MODE_CCM, |
| 344 |
nonce=self.nonce_96) |
| 345 |
cipher1.update(self.data) |
| 346 |
ct = cipher1.encrypt(self.data) |
| 347 |
tag = cipher1.digest() |
| 348 |
|
| 349 |
cipher2 = AES.new(key_mv, |
| 350 |
AES.MODE_CCM, |
| 351 |
nonce=nonce_mv) |
| 352 |
key_mv[:3] = b"\xFF\xFF\xFF" |
| 353 |
nonce_mv[:3] = b"\xFF\xFF\xFF" |
| 354 |
cipher2.update(header_mv) |
| 355 |
header_mv[:3] = b"\xFF\xFF\xFF" |
| 356 |
ct_test = cipher2.encrypt(data_mv) |
| 357 |
data_mv[:3] = b"\xFF\xFF\xFF" |
| 358 |
tag_test = cipher2.digest() |
| 359 |
|
| 360 |
self.assertEqual(ct, ct_test) |
| 361 |
self.assertEqual(tag, tag_test) |
| 362 |
self.assertEqual(cipher1.nonce, cipher2.nonce) |
| 363 |
|
| 364 |
|
| 365 |
key_mv = memoryview(bytearray(self.key_128)) |
| 366 |
nonce_mv = memoryview(bytearray(self.nonce_96)) |
| 367 |
header_mv = memoryview(bytearray(self.data)) |
| 368 |
del data_mv |
| 369 |
|
| 370 |
cipher4 = AES.new(key_mv, |
| 371 |
AES.MODE_CCM, |
| 372 |
nonce=nonce_mv) |
| 373 |
key_mv[:3] = b"\xFF\xFF\xFF" |
| 374 |
nonce_mv[:3] = b"\xFF\xFF\xFF" |
| 375 |
cipher4.update(header_mv) |
| 376 |
header_mv[:3] = b"\xFF\xFF\xFF" |
| 377 |
pt_test = cipher4.decrypt_and_verify(memoryview(ct_test), memoryview(tag_test)) |
| 378 |
|
| 379 |
self.assertEqual(self.data, pt_test) |
| 380 |
|
| 381 |
def test_output_param(self): |
| 382 |
|
| 383 |
pt = b'5' * 128 |
| 384 |
cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96) |
| 385 |
ct = cipher.encrypt(pt) |
| 386 |
tag = cipher.digest() |
| 387 |
|
| 388 |
output = bytearray(128) |
| 389 |
cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96) |
| 390 |
res = cipher.encrypt(pt, output=output) |
| 391 |
self.assertEqual(ct, output) |
| 392 |
self.assertEqual(res, None) |
| 393 |
|
| 394 |
cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96) |
| 395 |
res = cipher.decrypt(ct, output=output) |
| 396 |
self.assertEqual(pt, output) |
| 397 |
self.assertEqual(res, None) |
| 398 |
|
| 399 |
cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96) |
| 400 |
res, tag_out = cipher.encrypt_and_digest(pt, output=output) |
| 401 |
self.assertEqual(ct, output) |
| 402 |
self.assertEqual(res, None) |
| 403 |
self.assertEqual(tag, tag_out) |
| 404 |
|
| 405 |
cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96) |
| 406 |
res = cipher.decrypt_and_verify(ct, tag, output=output) |
| 407 |
self.assertEqual(pt, output) |
| 408 |
self.assertEqual(res, None) |
| 409 |
|
| 410 |
def test_output_param_memoryview(self): |
| 411 |
|
| 412 |
pt = b'5' * 128 |
| 413 |
cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96) |
| 414 |
ct = cipher.encrypt(pt) |
| 415 |
|
| 416 |
output = memoryview(bytearray(128)) |
| 417 |
cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96) |
| 418 |
cipher.encrypt(pt, output=output) |
| 419 |
self.assertEqual(ct, output) |
| 420 |
|
| 421 |
cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96) |
| 422 |
cipher.decrypt(ct, output=output) |
| 423 |
self.assertEqual(pt, output) |
| 424 |
|
| 425 |
def test_output_param_neg(self): |
| 426 |
|
| 427 |
pt = b'5' * 16 |
| 428 |
cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96) |
| 429 |
ct = cipher.encrypt(pt) |
| 430 |
|
| 431 |
cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96) |
| 432 |
self.assertRaises(TypeError, cipher.encrypt, pt, output=b'0'*16) |
| 433 |
|
| 434 |
cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96) |
| 435 |
self.assertRaises(TypeError, cipher.decrypt, ct, output=b'0'*16) |
| 436 |
|
| 437 |
shorter_output = bytearray(15) |
| 438 |
cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96) |
| 439 |
self.assertRaises(ValueError, cipher.encrypt, pt, output=shorter_output) |
| 440 |
cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96) |
| 441 |
self.assertRaises(ValueError, cipher.decrypt, ct, output=shorter_output) |
| 442 |
|
| 443 |
|
| 444 |
class CcmFSMTests(unittest.TestCase): |
| 445 |
|
| 446 |
key_128 = get_tag_random("key_128", 16) |
| 447 |
nonce_96 = get_tag_random("nonce_128", 12) |
| 448 |
data = get_tag_random("data", 16) |
| 449 |
|
| 450 |
def test_valid_init_encrypt_decrypt_digest_verify(self): |
| 451 |
|
| 452 |
for assoc_len in (None, 0): |
| 453 |
for msg_len in (None, len(self.data)): |
| 454 |
|
| 455 |
cipher = AES.new(self.key_128, AES.MODE_CCM, |
| 456 |
nonce=self.nonce_96, |
| 457 |
assoc_len=assoc_len, |
| 458 |
msg_len=msg_len) |
| 459 |
ct = cipher.encrypt(self.data) |
| 460 |
mac = cipher.digest() |
| 461 |
|
| 462 |
|
| 463 |
cipher = AES.new(self.key_128, AES.MODE_CCM, |
| 464 |
nonce=self.nonce_96, |
| 465 |
assoc_len=assoc_len, |
| 466 |
msg_len=msg_len) |
| 467 |
cipher.decrypt(ct) |
| 468 |
cipher.verify(mac) |
| 469 |
|
| 470 |
def test_valid_init_update_digest_verify(self): |
| 471 |
|
| 472 |
for assoc_len in (None, len(self.data)): |
| 473 |
for msg_len in (None, 0): |
| 474 |
|
| 475 |
cipher = AES.new(self.key_128, AES.MODE_CCM, |
| 476 |
nonce=self.nonce_96, |
| 477 |
assoc_len=assoc_len, |
| 478 |
msg_len=msg_len) |
| 479 |
cipher.update(self.data) |
| 480 |
mac = cipher.digest() |
| 481 |
|
| 482 |
|
| 483 |
cipher = AES.new(self.key_128, AES.MODE_CCM, |
| 484 |
nonce=self.nonce_96, |
| 485 |
assoc_len=assoc_len, |
| 486 |
msg_len=msg_len) |
| 487 |
cipher.update(self.data) |
| 488 |
cipher.verify(mac) |
| 489 |
|
| 490 |
def test_valid_full_path(self): |
| 491 |
|
| 492 |
for assoc_len in (None, len(self.data)): |
| 493 |
for msg_len in (None, len(self.data)): |
| 494 |
|
| 495 |
cipher = AES.new(self.key_128, AES.MODE_CCM, |
| 496 |
nonce=self.nonce_96, |
| 497 |
assoc_len=assoc_len, |
| 498 |
msg_len=msg_len) |
| 499 |
cipher.update(self.data) |
| 500 |
ct = cipher.encrypt(self.data) |
| 501 |
mac = cipher.digest() |
| 502 |
|
| 503 |
|
| 504 |
cipher = AES.new(self.key_128, AES.MODE_CCM, |
| 505 |
nonce=self.nonce_96, |
| 506 |
assoc_len=assoc_len, |
| 507 |
msg_len=msg_len) |
| 508 |
cipher.update(self.data) |
| 509 |
cipher.decrypt(ct) |
| 510 |
cipher.verify(mac) |
| 511 |
|
| 512 |
def test_valid_init_digest(self): |
| 513 |
|
| 514 |
cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96) |
| 515 |
cipher.digest() |
| 516 |
|
| 517 |
def test_valid_init_verify(self): |
| 518 |
|
| 519 |
cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96) |
| 520 |
mac = cipher.digest() |
| 521 |
|
| 522 |
cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96) |
| 523 |
cipher.verify(mac) |
| 524 |
|
| 525 |
def test_valid_multiple_encrypt_or_decrypt(self): |
| 526 |
|
| 527 |
for method_name in "encrypt", "decrypt": |
| 528 |
for auth_data in (None, b"333", self.data, |
| 529 |
self.data + b"3"): |
| 530 |
if auth_data is None: |
| 531 |
assoc_len = None |
| 532 |
else: |
| 533 |
assoc_len = len(auth_data) |
| 534 |
cipher = AES.new(self.key_128, AES.MODE_CCM, |
| 535 |
nonce=self.nonce_96, |
| 536 |
msg_len=64, |
| 537 |
assoc_len=assoc_len) |
| 538 |
if auth_data is not None: |
| 539 |
cipher.update(auth_data) |
| 540 |
method = getattr(cipher, method_name) |
| 541 |
method(self.data) |
| 542 |
method(self.data) |
| 543 |
method(self.data) |
| 544 |
method(self.data) |
| 545 |
|
| 546 |
def test_valid_multiple_digest_or_verify(self): |
| 547 |
|
| 548 |
cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96) |
| 549 |
cipher.update(self.data) |
| 550 |
first_mac = cipher.digest() |
| 551 |
for x in range(4): |
| 552 |
self.assertEqual(first_mac, cipher.digest()) |
| 553 |
|
| 554 |
|
| 555 |
cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96) |
| 556 |
cipher.update(self.data) |
| 557 |
for x in range(5): |
| 558 |
cipher.verify(first_mac) |
| 559 |
|
| 560 |
def test_valid_encrypt_and_digest_decrypt_and_verify(self): |
| 561 |
|
| 562 |
cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96) |
| 563 |
cipher.update(self.data) |
| 564 |
ct, mac = cipher.encrypt_and_digest(self.data) |
| 565 |
|
| 566 |
|
| 567 |
cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96) |
| 568 |
cipher.update(self.data) |
| 569 |
pt = cipher.decrypt_and_verify(ct, mac) |
| 570 |
self.assertEqual(self.data, pt) |
| 571 |
|
| 572 |
def test_invalid_multiple_encrypt_decrypt_without_msg_len(self): |
| 573 |
|
| 574 |
for method_name in "encrypt", "decrypt": |
| 575 |
for assoc_data_present in (True, False): |
| 576 |
cipher = AES.new(self.key_128, AES.MODE_CCM, |
| 577 |
nonce=self.nonce_96) |
| 578 |
if assoc_data_present: |
| 579 |
cipher.update(self.data) |
| 580 |
method = getattr(cipher, method_name) |
| 581 |
method(self.data) |
| 582 |
self.assertRaises(TypeError, method, self.data) |
| 583 |
|
| 584 |
def test_invalid_mixing_encrypt_decrypt(self): |
| 585 |
|
| 586 |
for method1_name, method2_name in (("encrypt", "decrypt"), |
| 587 |
("decrypt", "encrypt")): |
| 588 |
for assoc_data_present in (True, False): |
| 589 |
cipher = AES.new(self.key_128, AES.MODE_CCM, |
| 590 |
nonce=self.nonce_96, |
| 591 |
msg_len=32) |
| 592 |
if assoc_data_present: |
| 593 |
cipher.update(self.data) |
| 594 |
getattr(cipher, method1_name)(self.data) |
| 595 |
self.assertRaises(TypeError, getattr(cipher, method2_name), |
| 596 |
self.data) |
| 597 |
|
| 598 |
def test_invalid_encrypt_or_update_after_digest(self): |
| 599 |
for method_name in "encrypt", "update": |
| 600 |
cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96) |
| 601 |
cipher.encrypt(self.data) |
| 602 |
cipher.digest() |
| 603 |
self.assertRaises(TypeError, getattr(cipher, method_name), |
| 604 |
self.data) |
| 605 |
|
| 606 |
cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96) |
| 607 |
cipher.encrypt_and_digest(self.data) |
| 608 |
|
| 609 |
def test_invalid_decrypt_or_update_after_verify(self): |
| 610 |
cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96) |
| 611 |
ct = cipher.encrypt(self.data) |
| 612 |
mac = cipher.digest() |
| 613 |
|
| 614 |
for method_name in "decrypt", "update": |
| 615 |
cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96) |
| 616 |
cipher.decrypt(ct) |
| 617 |
cipher.verify(mac) |
| 618 |
self.assertRaises(TypeError, getattr(cipher, method_name), |
| 619 |
self.data) |
| 620 |
|
| 621 |
cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96) |
| 622 |
cipher.decrypt_and_verify(ct, mac) |
| 623 |
self.assertRaises(TypeError, getattr(cipher, method_name), |
| 624 |
self.data) |
| 625 |
|
| 626 |
|
| 627 |
class TestVectors(unittest.TestCase): |
| 628 |
"""Class exercising the CCM test vectors found in Appendix C |
| 629 |
of NIST SP 800-38C and in RFC 3610""" |
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
test_vectors_hex = [ |
| 639 |
|
| 640 |
( '0001020304050607', |
| 641 |
'20212223', |
| 642 |
'7162015b', |
| 643 |
'4dac255d', |
| 644 |
'404142434445464748494a4b4c4d4e4f', |
| 645 |
'10111213141516'), |
| 646 |
( '000102030405060708090a0b0c0d0e0f', |
| 647 |
'202122232425262728292a2b2c2d2e2f', |
| 648 |
'd2a1f0e051ea5f62081a7792073d593d', |
| 649 |
'1fc64fbfaccd', |
| 650 |
'404142434445464748494a4b4c4d4e4f', |
| 651 |
'1011121314151617'), |
| 652 |
( '000102030405060708090a0b0c0d0e0f10111213', |
| 653 |
'202122232425262728292a2b2c2d2e2f3031323334353637', |
| 654 |
'e3b201a9f5b71a7a9b1ceaeccd97e70b6176aad9a4428aa5', |
| 655 |
'484392fbc1b09951', |
| 656 |
'404142434445464748494a4b4c4d4e4f', |
| 657 |
'101112131415161718191a1b'), |
| 658 |
( (''.join(["%02X" % (x*16+y) for x in range(0,16) for y in range(0,16)]))*256, |
| 659 |
'202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f', |
| 660 |
'69915dad1e84c6376a68c2967e4dab615ae0fd1faec44cc484828529463ccf72', |
| 661 |
'b4ac6bec93e8598e7f0dadbcea5b', |
| 662 |
'404142434445464748494a4b4c4d4e4f', |
| 663 |
'101112131415161718191a1b1c'), |
| 664 |
|
| 665 |
( '0001020304050607', |
| 666 |
'08090a0b0c0d0e0f101112131415161718191a1b1c1d1e', |
| 667 |
'588c979a61c663d2f066d0c2c0f989806d5f6b61dac384', |
| 668 |
'17e8d12cfdf926e0', |
| 669 |
'c0c1c2c3c4c5c6c7c8c9cacbcccdcecf', |
| 670 |
'00000003020100a0a1a2a3a4a5'), |
| 671 |
( |
| 672 |
'0001020304050607', |
| 673 |
'08090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f', |
| 674 |
'72c91a36e135f8cf291ca894085c87e3cc15c439c9e43a3b', |
| 675 |
'a091d56e10400916', |
| 676 |
'c0c1c2c3c4c5c6c7c8c9cacbcccdcecf', |
| 677 |
'00000004030201a0a1a2a3a4a5'), |
| 678 |
( '0001020304050607', |
| 679 |
'08090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20', |
| 680 |
'51b1e5f44a197d1da46b0f8e2d282ae871e838bb64da859657', |
| 681 |
'4adaa76fbd9fb0c5', |
| 682 |
'c0c1c2c3c4c5c6c7c8c9cacbcccdcecf', |
| 683 |
'00000005040302A0A1A2A3A4A5'), |
| 684 |
( '000102030405060708090a0b', |
| 685 |
'0c0d0e0f101112131415161718191a1b1c1d1e', |
| 686 |
'a28c6865939a9a79faaa5c4c2a9d4a91cdac8c', |
| 687 |
'96c861b9c9e61ef1', |
| 688 |
'c0c1c2c3c4c5c6c7c8c9cacbcccdcecf', |
| 689 |
'00000006050403a0a1a2a3a4a5'), |
| 690 |
( '000102030405060708090a0b', |
| 691 |
'0c0d0e0f101112131415161718191a1b1c1d1e1f', |
| 692 |
'dcf1fb7b5d9e23fb9d4e131253658ad86ebdca3e', |
| 693 |
'51e83f077d9c2d93', |
| 694 |
'c0c1c2c3c4c5c6c7c8c9cacbcccdcecf', |
| 695 |
'00000007060504a0a1a2a3a4a5'), |
| 696 |
( '000102030405060708090a0b', |
| 697 |
'0c0d0e0f101112131415161718191a1b1c1d1e1f20', |
| 698 |
'6fc1b011f006568b5171a42d953d469b2570a4bd87', |
| 699 |
'405a0443ac91cb94', |
| 700 |
'c0c1c2c3c4c5c6c7c8c9cacbcccdcecf', |
| 701 |
'00000008070605a0a1a2a3a4a5'), |
| 702 |
( '0001020304050607', |
| 703 |
'08090a0b0c0d0e0f101112131415161718191a1b1c1d1e', |
| 704 |
'0135d1b2c95f41d5d1d4fec185d166b8094e999dfed96c', |
| 705 |
'048c56602c97acbb7490', |
| 706 |
'c0c1c2c3c4c5c6c7c8c9cacbcccdcecf', |
| 707 |
'00000009080706a0a1a2a3a4a5'), |
| 708 |
( '0001020304050607', |
| 709 |
'08090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f', |
| 710 |
'7b75399ac0831dd2f0bbd75879a2fd8f6cae6b6cd9b7db24', |
| 711 |
'c17b4433f434963f34b4', |
| 712 |
'c0c1c2c3c4c5c6c7c8c9cacbcccdcecf', |
| 713 |
'0000000a090807a0a1a2a3a4a5'), |
| 714 |
( '0001020304050607', |
| 715 |
'08090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20', |
| 716 |
'82531a60cc24945a4b8279181ab5c84df21ce7f9b73f42e197', |
| 717 |
'ea9c07e56b5eb17e5f4e', |
| 718 |
'c0c1c2c3c4c5c6c7c8c9cacbcccdcecf', |
| 719 |
'0000000b0a0908a0a1a2a3a4a5'), |
| 720 |
( '000102030405060708090a0b', |
| 721 |
'0c0d0e0f101112131415161718191a1b1c1d1e', |
| 722 |
'07342594157785152b074098330abb141b947b', |
| 723 |
'566aa9406b4d999988dd', |
| 724 |
'c0c1c2c3c4c5c6c7c8c9cacbcccdcecf', |
| 725 |
'0000000c0b0a09a0a1a2a3a4a5'), |
| 726 |
( '000102030405060708090a0b', |
| 727 |
'0c0d0e0f101112131415161718191a1b1c1d1e1f', |
| 728 |
'676bb20380b0e301e8ab79590a396da78b834934', |
| 729 |
'f53aa2e9107a8b6c022c', |
| 730 |
'c0c1c2c3c4c5c6c7c8c9cacbcccdcecf', |
| 731 |
'0000000d0c0b0aa0a1a2a3a4a5'), |
| 732 |
( '000102030405060708090a0b', |
| 733 |
'0c0d0e0f101112131415161718191a1b1c1d1e1f20', |
| 734 |
'c0ffa0d6f05bdb67f24d43a4338d2aa4bed7b20e43', |
| 735 |
'cd1aa31662e7ad65d6db', |
| 736 |
'c0c1c2c3c4c5c6c7c8c9cacbcccdcecf', |
| 737 |
'0000000e0d0c0ba0a1a2a3a4a5'), |
| 738 |
( '0be1a88bace018b1', |
| 739 |
'08e8cf97d820ea258460e96ad9cf5289054d895ceac47c', |
| 740 |
'4cb97f86a2a4689a877947ab8091ef5386a6ffbdd080f8', |
| 741 |
'e78cf7cb0cddd7b3', |
| 742 |
'd7828d13b2b0bdc325a76236df93cc6b', |
| 743 |
'00412b4ea9cdbe3c9696766cfa'), |
| 744 |
( '63018f76dc8a1bcb', |
| 745 |
'9020ea6f91bdd85afa0039ba4baff9bfb79c7028949cd0ec', |
| 746 |
'4ccb1e7ca981befaa0726c55d378061298c85c92814abc33', |
| 747 |
'c52ee81d7d77c08a', |
| 748 |
'd7828d13b2b0bdc325a76236df93cc6b', |
| 749 |
'0033568ef7b2633c9696766cfa'), |
| 750 |
( 'aa6cfa36cae86b40', |
| 751 |
'b916e0eacc1c00d7dcec68ec0b3bbb1a02de8a2d1aa346132e', |
| 752 |
'b1d23a2220ddc0ac900d9aa03c61fcf4a559a4417767089708', |
| 753 |
'a776796edb723506', |
| 754 |
'd7828d13b2b0bdc325a76236df93cc6b', |
| 755 |
'00103fe41336713c9696766cfa'), |
| 756 |
( 'd0d0735c531e1becf049c244', |
| 757 |
'12daac5630efa5396f770ce1a66b21f7b2101c', |
| 758 |
'14d253c3967b70609b7cbb7c49916028324526', |
| 759 |
'9a6f49975bcadeaf', |
| 760 |
'd7828d13b2b0bdc325a76236df93cc6b', |
| 761 |
'00764c63b8058e3c9696766cfa'), |
| 762 |
( '77b60f011c03e1525899bcae', |
| 763 |
'e88b6a46c78d63e52eb8c546efb5de6f75e9cc0d', |
| 764 |
'5545ff1a085ee2efbf52b2e04bee1e2336c73e3f', |
| 765 |
'762c0c7744fe7e3c', |
| 766 |
'd7828d13b2b0bdc325a76236df93cc6b', |
| 767 |
'00f8b678094e3b3c9696766cfa'), |
| 768 |
( 'cd9044d2b71fdb8120ea60c0', |
| 769 |
'6435acbafb11a82e2f071d7ca4a5ebd93a803ba87f', |
| 770 |
'009769ecabdf48625594c59251e6035722675e04c8', |
| 771 |
'47099e5ae0704551', |
| 772 |
'd7828d13b2b0bdc325a76236df93cc6b', |
| 773 |
'00d560912d3f703c9696766cfa'), |
| 774 |
( 'd85bc7e69f944fb8', |
| 775 |
'8a19b950bcf71a018e5e6701c91787659809d67dbedd18', |
| 776 |
'bc218daa947427b6db386a99ac1aef23ade0b52939cb6a', |
| 777 |
'637cf9bec2408897c6ba', |
| 778 |
'd7828d13b2b0bdc325a76236df93cc6b', |
| 779 |
'0042fff8f1951c3c9696766cfa'), |
| 780 |
( '74a0ebc9069f5b37', |
| 781 |
'1761433c37c5a35fc1f39f406302eb907c6163be38c98437', |
| 782 |
'5810e6fd25874022e80361a478e3e9cf484ab04f447efff6', |
| 783 |
'f0a477cc2fc9bf548944', |
| 784 |
'd7828d13b2b0bdc325a76236df93cc6b', |
| 785 |
'00920f40e56cdc3c9696766cfa'), |
| 786 |
( '44a3aa3aae6475ca', |
| 787 |
'a434a8e58500c6e41530538862d686ea9e81301b5ae4226bfa', |
| 788 |
'f2beed7bc5098e83feb5b31608f8e29c38819a89c8e776f154', |
| 789 |
'4d4151a4ed3a8b87b9ce', |
| 790 |
'd7828d13b2b0bdc325a76236df93cc6b', |
| 791 |
'0027ca0c7120bc3c9696766cfa'), |
| 792 |
( 'ec46bb63b02520c33c49fd70', |
| 793 |
'b96b49e21d621741632875db7f6c9243d2d7c2', |
| 794 |
'31d750a09da3ed7fddd49a2032aabf17ec8ebf', |
| 795 |
'7d22c8088c666be5c197', |
| 796 |
'd7828d13b2b0bdc325a76236df93cc6b', |
| 797 |
'005b8ccbcd9af83c9696766cfa'), |
| 798 |
( '47a65ac78b3d594227e85e71', |
| 799 |
'e2fcfbb880442c731bf95167c8ffd7895e337076', |
| 800 |
'e882f1dbd38ce3eda7c23f04dd65071eb41342ac', |
| 801 |
'df7e00dccec7ae52987d', |
| 802 |
'd7828d13b2b0bdc325a76236df93cc6b', |
| 803 |
'003ebe94044b9a3c9696766cfa'), |
| 804 |
( '6e37a6ef546d955d34ab6059', |
| 805 |
'abf21c0b02feb88f856df4a37381bce3cc128517d4', |
| 806 |
'f32905b88a641b04b9c9ffb58cc390900f3da12ab1', |
| 807 |
'6dce9e82efa16da62059', |
| 808 |
'd7828d13b2b0bdc325a76236df93cc6b', |
| 809 |
'008d493b30ae8b3c9696766cfa'), |
| 810 |
] |
| 811 |
|
| 812 |
test_vectors = [[unhexlify(x) for x in tv] for tv in test_vectors_hex] |
| 813 |
|
| 814 |
def runTest(self): |
| 815 |
for assoc_data, pt, ct, mac, key, nonce in self.test_vectors: |
| 816 |
|
| 817 |
cipher = AES.new(key, AES.MODE_CCM, nonce, mac_len=len(mac)) |
| 818 |
cipher.update(assoc_data) |
| 819 |
ct2, mac2 = cipher.encrypt_and_digest(pt) |
| 820 |
self.assertEqual(ct, ct2) |
| 821 |
self.assertEqual(mac, mac2) |
| 822 |
|
| 823 |
|
| 824 |
cipher = AES.new(key, AES.MODE_CCM, nonce, mac_len=len(mac)) |
| 825 |
cipher.update(assoc_data) |
| 826 |
pt2 = cipher.decrypt_and_verify(ct, mac) |
| 827 |
self.assertEqual(pt, pt2) |
| 828 |
|
| 829 |
|
| 830 |
class TestVectorsWycheproof(unittest.TestCase): |
| 831 |
|
| 832 |
def __init__(self, wycheproof_warnings, **extra_params): |
| 833 |
unittest.TestCase.__init__(self) |
| 834 |
self._wycheproof_warnings = wycheproof_warnings |
| 835 |
self._extra_params = extra_params |
| 836 |
self._id = "None" |
| 837 |
|
| 838 |
def setUp(self): |
| 839 |
|
| 840 |
def filter_tag(group): |
| 841 |
return group['tagSize'] // 8 |
| 842 |
|
| 843 |
self.tv = load_test_vectors_wycheproof(("Cipher", "wycheproof"), |
| 844 |
"aes_ccm_test.json", |
| 845 |
"Wycheproof AES CCM", |
| 846 |
group_tag={'tag_size': filter_tag}) |
| 847 |
|
| 848 |
def shortDescription(self): |
| 849 |
return self._id |
| 850 |
|
| 851 |
def warn(self, tv): |
| 852 |
if tv.warning and self._wycheproof_warnings: |
| 853 |
import warnings |
| 854 |
warnings.warn("Wycheproof warning: %s (%s)" % (self._id, tv.comment)) |
| 855 |
|
| 856 |
def test_encrypt(self, tv): |
| 857 |
self._id = "Wycheproof Encrypt CCM Test #" + str(tv.id) |
| 858 |
|
| 859 |
try: |
| 860 |
cipher = AES.new(tv.key, AES.MODE_CCM, tv.iv, mac_len=tv.tag_size, |
| 861 |
**self._extra_params) |
| 862 |
except ValueError as e: |
| 863 |
if len(tv.iv) not in range(7, 13 + 1, 2) and "Length of parameter 'nonce'" in str(e): |
| 864 |
assert not tv.valid |
| 865 |
return |
| 866 |
if tv.tag_size not in range(4, 16 + 1, 2) and "Parameter 'mac_len'" in str(e): |
| 867 |
assert not tv.valid |
| 868 |
return |
| 869 |
raise e |
| 870 |
|
| 871 |
cipher.update(tv.aad) |
| 872 |
ct, tag = cipher.encrypt_and_digest(tv.msg) |
| 873 |
if tv.valid: |
| 874 |
self.assertEqual(ct, tv.ct) |
| 875 |
self.assertEqual(tag, tv.tag) |
| 876 |
self.warn(tv) |
| 877 |
|
| 878 |
def test_decrypt(self, tv): |
| 879 |
self._id = "Wycheproof Decrypt CCM Test #" + str(tv.id) |
| 880 |
|
| 881 |
try: |
| 882 |
cipher = AES.new(tv.key, AES.MODE_CCM, tv.iv, mac_len=tv.tag_size, |
| 883 |
**self._extra_params) |
| 884 |
except ValueError as e: |
| 885 |
if len(tv.iv) not in range(7, 13 + 1, 2) and "Length of parameter 'nonce'" in str(e): |
| 886 |
assert not tv.valid |
| 887 |
return |
| 888 |
if tv.tag_size not in range(4, 16 + 1, 2) and "Parameter 'mac_len'" in str(e): |
| 889 |
assert not tv.valid |
| 890 |
return |
| 891 |
raise e |
| 892 |
|
| 893 |
cipher.update(tv.aad) |
| 894 |
try: |
| 895 |
pt = cipher.decrypt_and_verify(tv.ct, tv.tag) |
| 896 |
except ValueError: |
| 897 |
assert not tv.valid |
| 898 |
else: |
| 899 |
assert tv.valid |
| 900 |
self.assertEqual(pt, tv.msg) |
| 901 |
self.warn(tv) |
| 902 |
|
| 903 |
def test_corrupt_decrypt(self, tv): |
| 904 |
self._id = "Wycheproof Corrupt Decrypt CCM Test #" + str(tv.id) |
| 905 |
if len(tv.iv) not in range(7, 13 + 1, 2) or len(tv.ct) == 0: |
| 906 |
return |
| 907 |
cipher = AES.new(tv.key, AES.MODE_CCM, tv.iv, mac_len=tv.tag_size, |
| 908 |
**self._extra_params) |
| 909 |
cipher.update(tv.aad) |
| 910 |
ct_corrupt = strxor(tv.ct, b"\x00" * (len(tv.ct) - 1) + b"\x01") |
| 911 |
self.assertRaises(ValueError, cipher.decrypt_and_verify, ct_corrupt, tv.tag) |
| 912 |
|
| 913 |
def runTest(self): |
| 914 |
|
| 915 |
for tv in self.tv: |
| 916 |
self.test_encrypt(tv) |
| 917 |
self.test_decrypt(tv) |
| 918 |
self.test_corrupt_decrypt(tv) |
| 919 |
|
| 920 |
|
| 921 |
def get_tests(config={}): |
| 922 |
wycheproof_warnings = config.get('wycheproof_warnings') |
| 923 |
|
| 924 |
tests = [] |
| 925 |
tests += list_test_cases(CcmTests) |
| 926 |
tests += list_test_cases(CcmFSMTests) |
| 927 |
tests += [TestVectors()] |
| 928 |
tests += [TestVectorsWycheproof(wycheproof_warnings)] |
| 929 |
|
| 930 |
return tests |
| 931 |
|
| 932 |
|
| 933 |
if __name__ == '__main__': |
| 934 |
def suite(): |
| 935 |
unittest.TestSuite(get_tests()) |
| 936 |
unittest.main(defaultTest='suite') |