| 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.Util.py3compat import b, tobytes, bchr |
| 35 |
from Crypto.Util.number import long_to_bytes |
| 36 |
from Crypto.SelfTest.loader import load_test_vectors |
| 37 |
from Crypto.SelfTest.st_common import list_test_cases |
| 38 |
|
| 39 |
from Crypto.Cipher import AES |
| 40 |
from Crypto.Hash import SHAKE128 |
| 41 |
|
| 42 |
|
| 43 |
def get_tag_random(tag, length): |
| 44 |
return SHAKE128.new(data=tobytes(tag)).read(length) |
| 45 |
|
| 46 |
|
| 47 |
class OcbTests(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_OCB, nonce=self.nonce_96) |
| 55 |
pt = get_tag_random("plaintext", 16 * 100) |
| 56 |
ct, mac = cipher.encrypt_and_digest(pt) |
| 57 |
|
| 58 |
cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96) |
| 59 |
pt2 = cipher.decrypt_and_verify(ct, mac) |
| 60 |
self.assertEqual(pt, pt2) |
| 61 |
|
| 62 |
def test_nonce(self): |
| 63 |
|
| 64 |
AES.new(self.key_128, AES.MODE_OCB) |
| 65 |
|
| 66 |
cipher = AES.new(self.key_128, AES.MODE_OCB, self.nonce_96) |
| 67 |
ct = cipher.encrypt(self.data) |
| 68 |
|
| 69 |
cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96) |
| 70 |
self.assertEqual(ct, cipher.encrypt(self.data)) |
| 71 |
|
| 72 |
def test_nonce_must_be_bytes(self): |
| 73 |
self.assertRaises(TypeError, AES.new, self.key_128, AES.MODE_OCB, |
| 74 |
nonce=u'test12345678') |
| 75 |
|
| 76 |
def test_nonce_length(self): |
| 77 |
|
| 78 |
self.assertRaises(ValueError, AES.new, self.key_128, AES.MODE_OCB, |
| 79 |
nonce=b("")) |
| 80 |
|
| 81 |
|
| 82 |
for length in range(1, 16): |
| 83 |
AES.new(self.key_128, AES.MODE_OCB, nonce=self.data[:length]) |
| 84 |
|
| 85 |
self.assertRaises(ValueError, AES.new, self.key_128, AES.MODE_OCB, |
| 86 |
nonce=self.data) |
| 87 |
|
| 88 |
def test_block_size_128(self): |
| 89 |
cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96) |
| 90 |
self.assertEqual(cipher.block_size, AES.block_size) |
| 91 |
|
| 92 |
|
| 93 |
nonce1 = AES.new(self.key_128, AES.MODE_OCB).nonce |
| 94 |
nonce2 = AES.new(self.key_128, AES.MODE_OCB).nonce |
| 95 |
self.assertEqual(len(nonce1), 15) |
| 96 |
self.assertNotEqual(nonce1, nonce2) |
| 97 |
|
| 98 |
def test_nonce_attribute(self): |
| 99 |
cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96) |
| 100 |
self.assertEqual(cipher.nonce, self.nonce_96) |
| 101 |
|
| 102 |
|
| 103 |
nonce1 = AES.new(self.key_128, AES.MODE_OCB).nonce |
| 104 |
nonce2 = AES.new(self.key_128, AES.MODE_OCB).nonce |
| 105 |
self.assertEqual(len(nonce1), 15) |
| 106 |
self.assertNotEqual(nonce1, nonce2) |
| 107 |
|
| 108 |
def test_unknown_parameters(self): |
| 109 |
self.assertRaises(TypeError, AES.new, self.key_128, AES.MODE_OCB, |
| 110 |
self.nonce_96, 7) |
| 111 |
self.assertRaises(TypeError, AES.new, self.key_128, AES.MODE_OCB, |
| 112 |
nonce=self.nonce_96, unknown=7) |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96, |
| 117 |
use_aesni=False) |
| 118 |
|
| 119 |
def test_null_encryption_decryption(self): |
| 120 |
for func in "encrypt", "decrypt": |
| 121 |
cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96) |
| 122 |
result = getattr(cipher, func)(b("")) |
| 123 |
self.assertEqual(result, b("")) |
| 124 |
|
| 125 |
def test_either_encrypt_or_decrypt(self): |
| 126 |
cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96) |
| 127 |
cipher.encrypt(b("xyz")) |
| 128 |
self.assertRaises(TypeError, cipher.decrypt, b("xyz")) |
| 129 |
|
| 130 |
cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96) |
| 131 |
cipher.decrypt(b("xyz")) |
| 132 |
self.assertRaises(TypeError, cipher.encrypt, b("xyz")) |
| 133 |
|
| 134 |
def test_data_must_be_bytes(self): |
| 135 |
cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96) |
| 136 |
self.assertRaises(TypeError, cipher.encrypt, u'test1234567890-*') |
| 137 |
|
| 138 |
cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96) |
| 139 |
self.assertRaises(TypeError, cipher.decrypt, u'test1234567890-*') |
| 140 |
|
| 141 |
def test_mac_len(self): |
| 142 |
|
| 143 |
self.assertRaises(ValueError, AES.new, self.key_128, AES.MODE_OCB, |
| 144 |
nonce=self.nonce_96, mac_len=7) |
| 145 |
self.assertRaises(ValueError, AES.new, self.key_128, AES.MODE_OCB, |
| 146 |
nonce=self.nonce_96, mac_len=16+1) |
| 147 |
|
| 148 |
|
| 149 |
for mac_len in range(8, 16 + 1): |
| 150 |
cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96, |
| 151 |
mac_len=mac_len) |
| 152 |
_, mac = cipher.encrypt_and_digest(self.data) |
| 153 |
self.assertEqual(len(mac), mac_len) |
| 154 |
|
| 155 |
|
| 156 |
cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96) |
| 157 |
_, mac = cipher.encrypt_and_digest(self.data) |
| 158 |
self.assertEqual(len(mac), 16) |
| 159 |
|
| 160 |
def test_invalid_mac(self): |
| 161 |
from Crypto.Util.strxor import strxor_c |
| 162 |
cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96) |
| 163 |
ct, mac = cipher.encrypt_and_digest(self.data) |
| 164 |
|
| 165 |
invalid_mac = strxor_c(mac, 0x01) |
| 166 |
|
| 167 |
cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96) |
| 168 |
self.assertRaises(ValueError, cipher.decrypt_and_verify, ct, |
| 169 |
invalid_mac) |
| 170 |
|
| 171 |
def test_hex_mac(self): |
| 172 |
cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96) |
| 173 |
mac_hex = cipher.hexdigest() |
| 174 |
self.assertEqual(cipher.digest(), unhexlify(mac_hex)) |
| 175 |
|
| 176 |
cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96) |
| 177 |
cipher.hexverify(mac_hex) |
| 178 |
|
| 179 |
def test_message_chunks(self): |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
auth_data = get_tag_random("authenticated data", 127) |
| 184 |
plaintext = get_tag_random("plaintext", 127) |
| 185 |
|
| 186 |
cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96) |
| 187 |
cipher.update(auth_data) |
| 188 |
ciphertext, ref_mac = cipher.encrypt_and_digest(plaintext) |
| 189 |
|
| 190 |
def break_up(data, chunk_length): |
| 191 |
return [data[i:i+chunk_length] for i in range(0, len(data), |
| 192 |
chunk_length)] |
| 193 |
|
| 194 |
|
| 195 |
for chunk_length in 1, 2, 3, 7, 10, 13, 16, 40, 80, 128: |
| 196 |
|
| 197 |
cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96) |
| 198 |
|
| 199 |
for chunk in break_up(auth_data, chunk_length): |
| 200 |
cipher.update(chunk) |
| 201 |
pt2 = b("") |
| 202 |
for chunk in break_up(ciphertext, chunk_length): |
| 203 |
pt2 += cipher.decrypt(chunk) |
| 204 |
pt2 += cipher.decrypt() |
| 205 |
self.assertEqual(plaintext, pt2) |
| 206 |
cipher.verify(ref_mac) |
| 207 |
|
| 208 |
|
| 209 |
for chunk_length in 1, 2, 3, 7, 10, 13, 16, 40, 80, 128: |
| 210 |
|
| 211 |
cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96) |
| 212 |
|
| 213 |
for chunk in break_up(auth_data, chunk_length): |
| 214 |
cipher.update(chunk) |
| 215 |
ct2 = b("") |
| 216 |
for chunk in break_up(plaintext, chunk_length): |
| 217 |
ct2 += cipher.encrypt(chunk) |
| 218 |
ct2 += cipher.encrypt() |
| 219 |
self.assertEqual(ciphertext, ct2) |
| 220 |
self.assertEqual(cipher.digest(), ref_mac) |
| 221 |
|
| 222 |
def test_bytearray(self): |
| 223 |
|
| 224 |
|
| 225 |
key_ba = bytearray(self.key_128) |
| 226 |
nonce_ba = bytearray(self.nonce_96) |
| 227 |
header_ba = bytearray(self.data) |
| 228 |
data_ba = bytearray(self.data) |
| 229 |
|
| 230 |
cipher1 = AES.new(self.key_128, |
| 231 |
AES.MODE_OCB, |
| 232 |
nonce=self.nonce_96) |
| 233 |
cipher1.update(self.data) |
| 234 |
ct = cipher1.encrypt(self.data) + cipher1.encrypt() |
| 235 |
tag = cipher1.digest() |
| 236 |
|
| 237 |
cipher2 = AES.new(key_ba, |
| 238 |
AES.MODE_OCB, |
| 239 |
nonce=nonce_ba) |
| 240 |
key_ba[:3] = b"\xFF\xFF\xFF" |
| 241 |
nonce_ba[:3] = b"\xFF\xFF\xFF" |
| 242 |
cipher2.update(header_ba) |
| 243 |
header_ba[:3] = b"\xFF\xFF\xFF" |
| 244 |
ct_test = cipher2.encrypt(data_ba) + cipher2.encrypt() |
| 245 |
data_ba[:3] = b"\xFF\xFF\xFF" |
| 246 |
tag_test = cipher2.digest() |
| 247 |
|
| 248 |
self.assertEqual(ct, ct_test) |
| 249 |
self.assertEqual(tag, tag_test) |
| 250 |
self.assertEqual(cipher1.nonce, cipher2.nonce) |
| 251 |
|
| 252 |
|
| 253 |
key_ba = bytearray(self.key_128) |
| 254 |
nonce_ba = bytearray(self.nonce_96) |
| 255 |
header_ba = bytearray(self.data) |
| 256 |
del data_ba |
| 257 |
|
| 258 |
cipher4 = AES.new(key_ba, |
| 259 |
AES.MODE_OCB, |
| 260 |
nonce=nonce_ba) |
| 261 |
key_ba[:3] = b"\xFF\xFF\xFF" |
| 262 |
nonce_ba[:3] = b"\xFF\xFF\xFF" |
| 263 |
cipher4.update(header_ba) |
| 264 |
header_ba[:3] = b"\xFF\xFF\xFF" |
| 265 |
pt_test = cipher4.decrypt_and_verify(bytearray(ct_test), bytearray(tag_test)) |
| 266 |
|
| 267 |
self.assertEqual(self.data, pt_test) |
| 268 |
|
| 269 |
def test_memoryview(self): |
| 270 |
|
| 271 |
|
| 272 |
key_mv = memoryview(bytearray(self.key_128)) |
| 273 |
nonce_mv = memoryview(bytearray(self.nonce_96)) |
| 274 |
header_mv = memoryview(bytearray(self.data)) |
| 275 |
data_mv = memoryview(bytearray(self.data)) |
| 276 |
|
| 277 |
cipher1 = AES.new(self.key_128, |
| 278 |
AES.MODE_OCB, |
| 279 |
nonce=self.nonce_96) |
| 280 |
cipher1.update(self.data) |
| 281 |
ct = cipher1.encrypt(self.data) + cipher1.encrypt() |
| 282 |
tag = cipher1.digest() |
| 283 |
|
| 284 |
cipher2 = AES.new(key_mv, |
| 285 |
AES.MODE_OCB, |
| 286 |
nonce=nonce_mv) |
| 287 |
key_mv[:3] = b"\xFF\xFF\xFF" |
| 288 |
nonce_mv[:3] = b"\xFF\xFF\xFF" |
| 289 |
cipher2.update(header_mv) |
| 290 |
header_mv[:3] = b"\xFF\xFF\xFF" |
| 291 |
ct_test = cipher2.encrypt(data_mv) + cipher2.encrypt() |
| 292 |
data_mv[:3] = b"\xFF\xFF\xFF" |
| 293 |
tag_test = cipher2.digest() |
| 294 |
|
| 295 |
self.assertEqual(ct, ct_test) |
| 296 |
self.assertEqual(tag, tag_test) |
| 297 |
self.assertEqual(cipher1.nonce, cipher2.nonce) |
| 298 |
|
| 299 |
|
| 300 |
key_mv = memoryview(bytearray(self.key_128)) |
| 301 |
nonce_mv = memoryview(bytearray(self.nonce_96)) |
| 302 |
header_mv = memoryview(bytearray(self.data)) |
| 303 |
del data_mv |
| 304 |
|
| 305 |
cipher4 = AES.new(key_mv, |
| 306 |
AES.MODE_OCB, |
| 307 |
nonce=nonce_mv) |
| 308 |
key_mv[:3] = b"\xFF\xFF\xFF" |
| 309 |
nonce_mv[:3] = b"\xFF\xFF\xFF" |
| 310 |
cipher4.update(header_mv) |
| 311 |
header_mv[:3] = b"\xFF\xFF\xFF" |
| 312 |
pt_test = cipher4.decrypt_and_verify(memoryview(ct_test), memoryview(tag_test)) |
| 313 |
|
| 314 |
self.assertEqual(self.data, pt_test) |
| 315 |
|
| 316 |
|
| 317 |
class OcbFSMTests(unittest.TestCase): |
| 318 |
|
| 319 |
key_128 = get_tag_random("key_128", 16) |
| 320 |
nonce_96 = get_tag_random("nonce_128", 12) |
| 321 |
data = get_tag_random("data", 128) |
| 322 |
|
| 323 |
def test_valid_init_encrypt_decrypt_digest_verify(self): |
| 324 |
|
| 325 |
|
| 326 |
cipher = AES.new(self.key_128, AES.MODE_OCB, |
| 327 |
nonce=self.nonce_96) |
| 328 |
ct = cipher.encrypt(self.data) |
| 329 |
ct += cipher.encrypt() |
| 330 |
mac = cipher.digest() |
| 331 |
|
| 332 |
|
| 333 |
cipher = AES.new(self.key_128, AES.MODE_OCB, |
| 334 |
nonce=self.nonce_96) |
| 335 |
cipher.decrypt(ct) |
| 336 |
cipher.decrypt() |
| 337 |
cipher.verify(mac) |
| 338 |
|
| 339 |
def test_invalid_init_encrypt_decrypt_digest_verify(self): |
| 340 |
|
| 341 |
|
| 342 |
cipher = AES.new(self.key_128, AES.MODE_OCB, |
| 343 |
nonce=self.nonce_96) |
| 344 |
ct = cipher.encrypt(self.data) |
| 345 |
self.assertRaises(TypeError, cipher.digest) |
| 346 |
|
| 347 |
|
| 348 |
cipher = AES.new(self.key_128, AES.MODE_OCB, |
| 349 |
nonce=self.nonce_96) |
| 350 |
cipher.decrypt(ct) |
| 351 |
self.assertRaises(TypeError, cipher.verify) |
| 352 |
|
| 353 |
def test_valid_init_update_digest_verify(self): |
| 354 |
|
| 355 |
|
| 356 |
cipher = AES.new(self.key_128, AES.MODE_OCB, |
| 357 |
nonce=self.nonce_96) |
| 358 |
cipher.update(self.data) |
| 359 |
mac = cipher.digest() |
| 360 |
|
| 361 |
|
| 362 |
cipher = AES.new(self.key_128, AES.MODE_OCB, |
| 363 |
nonce=self.nonce_96) |
| 364 |
cipher.update(self.data) |
| 365 |
cipher.verify(mac) |
| 366 |
|
| 367 |
def test_valid_full_path(self): |
| 368 |
|
| 369 |
|
| 370 |
cipher = AES.new(self.key_128, AES.MODE_OCB, |
| 371 |
nonce=self.nonce_96) |
| 372 |
cipher.update(self.data) |
| 373 |
ct = cipher.encrypt(self.data) |
| 374 |
ct += cipher.encrypt() |
| 375 |
mac = cipher.digest() |
| 376 |
|
| 377 |
|
| 378 |
cipher = AES.new(self.key_128, AES.MODE_OCB, |
| 379 |
nonce=self.nonce_96) |
| 380 |
cipher.update(self.data) |
| 381 |
cipher.decrypt(ct) |
| 382 |
cipher.decrypt() |
| 383 |
cipher.verify(mac) |
| 384 |
|
| 385 |
|
| 386 |
cipher = AES.new(self.key_128, AES.MODE_OCB, |
| 387 |
nonce=self.nonce_96) |
| 388 |
cipher.update(self.data) |
| 389 |
ct1 = cipher.encrypt(self.data[:2]) |
| 390 |
ct2, mac = cipher.encrypt_and_digest(self.data[2:]) |
| 391 |
|
| 392 |
|
| 393 |
cipher = AES.new(self.key_128, AES.MODE_OCB, |
| 394 |
nonce=self.nonce_96) |
| 395 |
cipher.update(self.data) |
| 396 |
cipher.decrypt(ct1) |
| 397 |
cipher.decrypt_and_verify(ct2, mac) |
| 398 |
|
| 399 |
def test_invalid_encrypt_after_final(self): |
| 400 |
cipher = AES.new(self.key_128, AES.MODE_OCB, |
| 401 |
nonce=self.nonce_96) |
| 402 |
cipher.update(self.data) |
| 403 |
cipher.encrypt(self.data) |
| 404 |
cipher.encrypt() |
| 405 |
self.assertRaises(TypeError, cipher.encrypt, self.data) |
| 406 |
|
| 407 |
def test_invalid_decrypt_after_final(self): |
| 408 |
cipher = AES.new(self.key_128, AES.MODE_OCB, |
| 409 |
nonce=self.nonce_96) |
| 410 |
cipher.update(self.data) |
| 411 |
cipher.decrypt(self.data) |
| 412 |
cipher.decrypt() |
| 413 |
self.assertRaises(TypeError, cipher.decrypt, self.data) |
| 414 |
|
| 415 |
def test_valid_init_digest(self): |
| 416 |
|
| 417 |
cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96) |
| 418 |
cipher.digest() |
| 419 |
|
| 420 |
def test_valid_init_verify(self): |
| 421 |
|
| 422 |
cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96) |
| 423 |
mac = cipher.digest() |
| 424 |
|
| 425 |
cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96) |
| 426 |
cipher.verify(mac) |
| 427 |
|
| 428 |
def test_valid_multiple_encrypt_or_decrypt(self): |
| 429 |
for method_name in "encrypt", "decrypt": |
| 430 |
for auth_data in (None, b("333"), self.data, |
| 431 |
self.data + b("3")): |
| 432 |
cipher = AES.new(self.key_128, AES.MODE_OCB, |
| 433 |
nonce=self.nonce_96) |
| 434 |
if auth_data is not None: |
| 435 |
cipher.update(auth_data) |
| 436 |
method = getattr(cipher, method_name) |
| 437 |
method(self.data) |
| 438 |
method(self.data) |
| 439 |
method(self.data) |
| 440 |
method(self.data) |
| 441 |
method() |
| 442 |
|
| 443 |
def test_valid_multiple_digest_or_verify(self): |
| 444 |
|
| 445 |
cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96) |
| 446 |
cipher.update(self.data) |
| 447 |
first_mac = cipher.digest() |
| 448 |
for x in range(4): |
| 449 |
self.assertEqual(first_mac, cipher.digest()) |
| 450 |
|
| 451 |
|
| 452 |
cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96) |
| 453 |
cipher.update(self.data) |
| 454 |
for x in range(5): |
| 455 |
cipher.verify(first_mac) |
| 456 |
|
| 457 |
def test_valid_encrypt_and_digest_decrypt_and_verify(self): |
| 458 |
|
| 459 |
cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96) |
| 460 |
cipher.update(self.data) |
| 461 |
ct, mac = cipher.encrypt_and_digest(self.data) |
| 462 |
|
| 463 |
|
| 464 |
cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96) |
| 465 |
cipher.update(self.data) |
| 466 |
pt = cipher.decrypt_and_verify(ct, mac) |
| 467 |
self.assertEqual(self.data, pt) |
| 468 |
|
| 469 |
def test_invalid_mixing_encrypt_decrypt(self): |
| 470 |
|
| 471 |
for method1_name, method2_name in (("encrypt", "decrypt"), |
| 472 |
("decrypt", "encrypt")): |
| 473 |
for assoc_data_present in (True, False): |
| 474 |
cipher = AES.new(self.key_128, AES.MODE_OCB, |
| 475 |
nonce=self.nonce_96) |
| 476 |
if assoc_data_present: |
| 477 |
cipher.update(self.data) |
| 478 |
getattr(cipher, method1_name)(self.data) |
| 479 |
self.assertRaises(TypeError, getattr(cipher, method2_name), |
| 480 |
self.data) |
| 481 |
|
| 482 |
def test_invalid_encrypt_or_update_after_digest(self): |
| 483 |
for method_name in "encrypt", "update": |
| 484 |
cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96) |
| 485 |
cipher.encrypt(self.data) |
| 486 |
cipher.encrypt() |
| 487 |
cipher.digest() |
| 488 |
self.assertRaises(TypeError, getattr(cipher, method_name), |
| 489 |
self.data) |
| 490 |
|
| 491 |
cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96) |
| 492 |
cipher.encrypt_and_digest(self.data) |
| 493 |
|
| 494 |
def test_invalid_decrypt_or_update_after_verify(self): |
| 495 |
cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96) |
| 496 |
ct = cipher.encrypt(self.data) |
| 497 |
ct += cipher.encrypt() |
| 498 |
mac = cipher.digest() |
| 499 |
|
| 500 |
for method_name in "decrypt", "update": |
| 501 |
cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96) |
| 502 |
cipher.decrypt(ct) |
| 503 |
cipher.decrypt() |
| 504 |
cipher.verify(mac) |
| 505 |
self.assertRaises(TypeError, getattr(cipher, method_name), |
| 506 |
self.data) |
| 507 |
|
| 508 |
cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96) |
| 509 |
cipher.decrypt_and_verify(ct, mac) |
| 510 |
self.assertRaises(TypeError, getattr(cipher, method_name), |
| 511 |
self.data) |
| 512 |
|
| 513 |
|
| 514 |
def algo_rfc7253(keylen, taglen, noncelen): |
| 515 |
"""Implement the algorithm at page 18 of RFC 7253""" |
| 516 |
|
| 517 |
key = bchr(0) * (keylen // 8 - 1) + bchr(taglen) |
| 518 |
C = b"" |
| 519 |
|
| 520 |
for i in range(128): |
| 521 |
S = bchr(0) * i |
| 522 |
|
| 523 |
N = long_to_bytes(3 * i + 1, noncelen // 8) |
| 524 |
cipher = AES.new(key, AES.MODE_OCB, nonce=N, mac_len=taglen // 8) |
| 525 |
cipher.update(S) |
| 526 |
C += cipher.encrypt(S) + cipher.encrypt() + cipher.digest() |
| 527 |
|
| 528 |
N = long_to_bytes(3 * i + 2, noncelen // 8) |
| 529 |
cipher = AES.new(key, AES.MODE_OCB, nonce=N, mac_len=taglen // 8) |
| 530 |
C += cipher.encrypt(S) + cipher.encrypt() + cipher.digest() |
| 531 |
|
| 532 |
N = long_to_bytes(3 * i + 3, noncelen // 8) |
| 533 |
cipher = AES.new(key, AES.MODE_OCB, nonce=N, mac_len=taglen // 8) |
| 534 |
cipher.update(S) |
| 535 |
C += cipher.encrypt() + cipher.digest() |
| 536 |
|
| 537 |
N = long_to_bytes(385, noncelen // 8) |
| 538 |
cipher = AES.new(key, AES.MODE_OCB, nonce=N, mac_len=taglen // 8) |
| 539 |
cipher.update(C) |
| 540 |
return cipher.encrypt() + cipher.digest() |
| 541 |
|
| 542 |
|
| 543 |
class OcbRfc7253Test(unittest.TestCase): |
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
tv1_key = "000102030405060708090A0B0C0D0E0F" |
| 551 |
tv1 = ( |
| 552 |
( |
| 553 |
"BBAA99887766554433221100", |
| 554 |
"", |
| 555 |
"", |
| 556 |
"785407BFFFC8AD9EDCC5520AC9111EE6" |
| 557 |
), |
| 558 |
( |
| 559 |
"BBAA99887766554433221101", |
| 560 |
"0001020304050607", |
| 561 |
"0001020304050607", |
| 562 |
"6820B3657B6F615A5725BDA0D3B4EB3A257C9AF1F8F03009" |
| 563 |
), |
| 564 |
( |
| 565 |
"BBAA99887766554433221102", |
| 566 |
"0001020304050607", |
| 567 |
"", |
| 568 |
"81017F8203F081277152FADE694A0A00" |
| 569 |
), |
| 570 |
( |
| 571 |
"BBAA99887766554433221103", |
| 572 |
"", |
| 573 |
"0001020304050607", |
| 574 |
"45DD69F8F5AAE72414054CD1F35D82760B2CD00D2F99BFA9" |
| 575 |
), |
| 576 |
( |
| 577 |
"BBAA99887766554433221104", |
| 578 |
"000102030405060708090A0B0C0D0E0F", |
| 579 |
"000102030405060708090A0B0C0D0E0F", |
| 580 |
"571D535B60B277188BE5147170A9A22C3AD7A4FF3835B8C5" |
| 581 |
"701C1CCEC8FC3358" |
| 582 |
), |
| 583 |
( |
| 584 |
"BBAA99887766554433221105", |
| 585 |
"000102030405060708090A0B0C0D0E0F", |
| 586 |
"", |
| 587 |
"8CF761B6902EF764462AD86498CA6B97" |
| 588 |
), |
| 589 |
( |
| 590 |
"BBAA99887766554433221106", |
| 591 |
"", |
| 592 |
"000102030405060708090A0B0C0D0E0F", |
| 593 |
"5CE88EC2E0692706A915C00AEB8B2396F40E1C743F52436B" |
| 594 |
"DF06D8FA1ECA343D" |
| 595 |
), |
| 596 |
( |
| 597 |
"BBAA99887766554433221107", |
| 598 |
"000102030405060708090A0B0C0D0E0F1011121314151617", |
| 599 |
"000102030405060708090A0B0C0D0E0F1011121314151617", |
| 600 |
"1CA2207308C87C010756104D8840CE1952F09673A448A122" |
| 601 |
"C92C62241051F57356D7F3C90BB0E07F" |
| 602 |
), |
| 603 |
( |
| 604 |
"BBAA99887766554433221108", |
| 605 |
"000102030405060708090A0B0C0D0E0F1011121314151617", |
| 606 |
"", |
| 607 |
"6DC225A071FC1B9F7C69F93B0F1E10DE" |
| 608 |
), |
| 609 |
( |
| 610 |
"BBAA99887766554433221109", |
| 611 |
"", |
| 612 |
"000102030405060708090A0B0C0D0E0F1011121314151617", |
| 613 |
"221BD0DE7FA6FE993ECCD769460A0AF2D6CDED0C395B1C3C" |
| 614 |
"E725F32494B9F914D85C0B1EB38357FF" |
| 615 |
), |
| 616 |
( |
| 617 |
"BBAA9988776655443322110A", |
| 618 |
"000102030405060708090A0B0C0D0E0F1011121314151617" |
| 619 |
"18191A1B1C1D1E1F", |
| 620 |
"000102030405060708090A0B0C0D0E0F1011121314151617" |
| 621 |
"18191A1B1C1D1E1F", |
| 622 |
"BD6F6C496201C69296C11EFD138A467ABD3C707924B964DE" |
| 623 |
"AFFC40319AF5A48540FBBA186C5553C68AD9F592A79A4240" |
| 624 |
), |
| 625 |
( |
| 626 |
"BBAA9988776655443322110B", |
| 627 |
"000102030405060708090A0B0C0D0E0F1011121314151617" |
| 628 |
"18191A1B1C1D1E1F", |
| 629 |
"", |
| 630 |
"FE80690BEE8A485D11F32965BC9D2A32" |
| 631 |
), |
| 632 |
( |
| 633 |
"BBAA9988776655443322110C", |
| 634 |
"", |
| 635 |
"000102030405060708090A0B0C0D0E0F1011121314151617" |
| 636 |
"18191A1B1C1D1E1F", |
| 637 |
"2942BFC773BDA23CABC6ACFD9BFD5835BD300F0973792EF4" |
| 638 |
"6040C53F1432BCDFB5E1DDE3BC18A5F840B52E653444D5DF" |
| 639 |
), |
| 640 |
( |
| 641 |
"BBAA9988776655443322110D", |
| 642 |
"000102030405060708090A0B0C0D0E0F1011121314151617" |
| 643 |
"18191A1B1C1D1E1F2021222324252627", |
| 644 |
"000102030405060708090A0B0C0D0E0F1011121314151617" |
| 645 |
"18191A1B1C1D1E1F2021222324252627", |
| 646 |
"D5CA91748410C1751FF8A2F618255B68A0A12E093FF45460" |
| 647 |
"6E59F9C1D0DDC54B65E8628E568BAD7AED07BA06A4A69483" |
| 648 |
"A7035490C5769E60" |
| 649 |
), |
| 650 |
( |
| 651 |
"BBAA9988776655443322110E", |
| 652 |
"000102030405060708090A0B0C0D0E0F1011121314151617" |
| 653 |
"18191A1B1C1D1E1F2021222324252627", |
| 654 |
"", |
| 655 |
"C5CD9D1850C141E358649994EE701B68" |
| 656 |
), |
| 657 |
( |
| 658 |
"BBAA9988776655443322110F", |
| 659 |
"", |
| 660 |
"000102030405060708090A0B0C0D0E0F1011121314151617" |
| 661 |
"18191A1B1C1D1E1F2021222324252627", |
| 662 |
"4412923493C57D5DE0D700F753CCE0D1D2D95060122E9F15" |
| 663 |
"A5DDBFC5787E50B5CC55EE507BCB084E479AD363AC366B95" |
| 664 |
"A98CA5F3000B1479" |
| 665 |
) |
| 666 |
) |
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
tv2 = ( |
| 675 |
"0F0E0D0C0B0A09080706050403020100", |
| 676 |
"BBAA9988776655443322110D", |
| 677 |
"000102030405060708090A0B0C0D0E0F1011121314151617" |
| 678 |
"18191A1B1C1D1E1F2021222324252627", |
| 679 |
"000102030405060708090A0B0C0D0E0F1011121314151617" |
| 680 |
"18191A1B1C1D1E1F2021222324252627", |
| 681 |
"1792A4E31E0755FB03E31B22116E6C2DDF9EFD6E33D536F1" |
| 682 |
"A0124B0A55BAE884ED93481529C76B6AD0C515F4D1CDD4FD" |
| 683 |
"AC4F02AA" |
| 684 |
) |
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
tv3 = ( |
| 691 |
(128, 128, "67E944D23256C5E0B6C61FA22FDF1EA2"), |
| 692 |
(192, 128, "F673F2C3E7174AAE7BAE986CA9F29E17"), |
| 693 |
(256, 128, "D90EB8E9C977C88B79DD793D7FFA161C"), |
| 694 |
(128, 96, "77A3D8E73589158D25D01209"), |
| 695 |
(192, 96, "05D56EAD2752C86BE6932C5E"), |
| 696 |
(256, 96, "5458359AC23B0CBA9E6330DD"), |
| 697 |
(128, 64, "192C9B7BD90BA06A"), |
| 698 |
(192, 64, "0066BC6E0EF34E24"), |
| 699 |
(256, 64, "7D4EA5D445501CBE"), |
| 700 |
) |
| 701 |
|
| 702 |
def test1(self): |
| 703 |
key = unhexlify(b(self.tv1_key)) |
| 704 |
for tv in self.tv1: |
| 705 |
nonce, aad, pt, ct = [unhexlify(b(x)) for x in tv] |
| 706 |
ct, mac_tag = ct[:-16], ct[-16:] |
| 707 |
|
| 708 |
cipher = AES.new(key, AES.MODE_OCB, nonce=nonce) |
| 709 |
cipher.update(aad) |
| 710 |
ct2 = cipher.encrypt(pt) + cipher.encrypt() |
| 711 |
self.assertEqual(ct, ct2) |
| 712 |
self.assertEqual(mac_tag, cipher.digest()) |
| 713 |
|
| 714 |
cipher = AES.new(key, AES.MODE_OCB, nonce=nonce) |
| 715 |
cipher.update(aad) |
| 716 |
pt2 = cipher.decrypt(ct) + cipher.decrypt() |
| 717 |
self.assertEqual(pt, pt2) |
| 718 |
cipher.verify(mac_tag) |
| 719 |
|
| 720 |
def test2(self): |
| 721 |
|
| 722 |
key, nonce, aad, pt, ct = [unhexlify(b(x)) for x in self.tv2] |
| 723 |
ct, mac_tag = ct[:-12], ct[-12:] |
| 724 |
|
| 725 |
cipher = AES.new(key, AES.MODE_OCB, nonce=nonce, mac_len=12) |
| 726 |
cipher.update(aad) |
| 727 |
ct2 = cipher.encrypt(pt) + cipher.encrypt() |
| 728 |
self.assertEqual(ct, ct2) |
| 729 |
self.assertEqual(mac_tag, cipher.digest()) |
| 730 |
|
| 731 |
cipher = AES.new(key, AES.MODE_OCB, nonce=nonce, mac_len=12) |
| 732 |
cipher.update(aad) |
| 733 |
pt2 = cipher.decrypt(ct) + cipher.decrypt() |
| 734 |
self.assertEqual(pt, pt2) |
| 735 |
cipher.verify(mac_tag) |
| 736 |
|
| 737 |
def test3(self): |
| 738 |
for keylen, taglen, result in self.tv3: |
| 739 |
result2 = algo_rfc7253(keylen, taglen, 96) |
| 740 |
self.assertEqual(unhexlify(b(result)), result2) |
| 741 |
|
| 742 |
|
| 743 |
class OcbDkgTest(unittest.TestCase): |
| 744 |
"""Test vectors from https://gitlab.com/dkg/ocb-test-vectors""" |
| 745 |
|
| 746 |
def test_1_2(self): |
| 747 |
tvs = [] |
| 748 |
for fi in (1, 2): |
| 749 |
for nb in (104, 112, 120): |
| 750 |
tv_file = load_test_vectors(("Cipher", "AES"), |
| 751 |
"test-vector-%d-nonce%d.txt" % (fi, nb), |
| 752 |
"DKG tests, %d, %d bits" % (fi, nb), |
| 753 |
{}) |
| 754 |
if tv_file is None: |
| 755 |
break |
| 756 |
key = tv_file[0].k |
| 757 |
for tv in tv_file[1:]: |
| 758 |
tv.k = key |
| 759 |
tvs.append(tv) |
| 760 |
|
| 761 |
for tv in tvs: |
| 762 |
k, n, a, p, c = tv.k, tv.n, tv.a, tv.p, tv.c |
| 763 |
mac_len = len(c) - len(p) |
| 764 |
cipher = AES.new(k, AES.MODE_OCB, nonce=n, mac_len=mac_len) |
| 765 |
cipher.update(a) |
| 766 |
c_out, tag_out = cipher.encrypt_and_digest(p) |
| 767 |
self.assertEqual(c, c_out + tag_out) |
| 768 |
|
| 769 |
def test_3(self): |
| 770 |
|
| 771 |
def check(keylen, taglen, noncelen, exp): |
| 772 |
result = algo_rfc7253(keylen, taglen, noncelen) |
| 773 |
self.assertEqual(result, unhexlify(exp)) |
| 774 |
|
| 775 |
|
| 776 |
check(128, 128, 104, "C47F5F0341E15326D4D1C46F47F05062") |
| 777 |
check(192, 128, 104, "95B9167A38EB80495DFC561A8486E109") |
| 778 |
check(256, 128, 104, "AFE1CDDB97028FD92F8FB3C8CFBA7D83") |
| 779 |
check(128, 96, 104, "F471B4983BA80946DF217A54") |
| 780 |
check(192, 96, 104, "5AE828BC51C24D85FA5CC7B2") |
| 781 |
check(256, 96, 104, "8C8335982E2B734616CAD14C") |
| 782 |
check(128, 64, 104, "B553F74B85FD1E5B") |
| 783 |
check(192, 64, 104, "3B49D20E513531F9") |
| 784 |
check(256, 64, 104, "ED6DA5B1216BF8BB") |
| 785 |
|
| 786 |
|
| 787 |
check(128, 128, 112, "CA8AFCA031BAC3F480A583BD6C50A547") |
| 788 |
check(192, 128, 112, "D170C1DF356308079DA9A3F619147148") |
| 789 |
check(256, 128, 112, "57F94381F2F9231EFB04AECD323757C3") |
| 790 |
check(128, 96, 112, "3A618B2531ED39F260C750DC") |
| 791 |
check(192, 96, 112, "9071EB89FEDBADDA88FD286E") |
| 792 |
check(256, 96, 112, "FDF0EFB97F21A39AC4BAB5AC") |
| 793 |
check(128, 64, 112, "FAB2FF3A8DD82A13") |
| 794 |
check(192, 64, 112, "AC01D912BD0737D3") |
| 795 |
check(256, 64, 112, "9D1FD0B500EA4ECF") |
| 796 |
|
| 797 |
|
| 798 |
check(128, 128, 120, "9E043A7140A25FB91F43BCC9DD7E0F46") |
| 799 |
check(192, 128, 120, "680000E53908323A7F396B955B8EC641") |
| 800 |
check(256, 128, 120, "8304B97FAACDA56E676602E1878A7E6F") |
| 801 |
check(128, 96, 120, "81F978AC9867E825D339847D") |
| 802 |
check(192, 96, 120, "EFCF2D60B24926ADA48CF5B1") |
| 803 |
check(256, 96, 120, "84961DC56E917B165E58C174") |
| 804 |
check(128, 64, 120, "227AEE6C9D905A61") |
| 805 |
check(192, 64, 120, "541DE691B9E1A2F9") |
| 806 |
check(256, 64, 120, "B0E761381C7129FC") |
| 807 |
|
| 808 |
def test_2_bugfix(self): |
| 809 |
nonce = unhexlify("EEDDCCBBAA9988776655443322110D") |
| 810 |
key = unhexlify("0F0E0D0C0B0A09080706050403020100") |
| 811 |
A = unhexlify("000102030405060708090A0B0C0D0E0F1011121314151617" |
| 812 |
"18191A1B1C1D1E1F2021222324252627") |
| 813 |
P = unhexlify("000102030405060708090A0B0C0D0E0F1011121314151617" |
| 814 |
"18191A1B1C1D1E1F2021222324252627") |
| 815 |
C = unhexlify("07E903BFC49552411ABC865F5ECE60F6FAD1F5A9F14D3070" |
| 816 |
"FA2F1308A563207FFE14C1EEA44B22059C7484319D8A2C53" |
| 817 |
"C236A7B3") |
| 818 |
mac_len = len(C) - len(P) |
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
buggy_result = unhexlify("BA015C4E5AE54D76C890AE81BD40DC57" |
| 824 |
"03EDC30E8AC2A58BC5D8FA4D61C5BAE6" |
| 825 |
"C39BEAC435B2FD56A2A5085C1B135D77" |
| 826 |
"0C8264B7") |
| 827 |
cipher = AES.new(key, AES.MODE_OCB, nonce=nonce[:-1], mac_len=mac_len) |
| 828 |
cipher.update(A) |
| 829 |
C_out2, tag_out2 = cipher.encrypt_and_digest(P) |
| 830 |
self.assertEqual(buggy_result, C_out2 + tag_out2) |
| 831 |
|
| 832 |
|
| 833 |
def get_tests(config={}): |
| 834 |
tests = [] |
| 835 |
tests += list_test_cases(OcbTests) |
| 836 |
tests += list_test_cases(OcbFSMTests) |
| 837 |
tests += list_test_cases(OcbRfc7253Test) |
| 838 |
tests += list_test_cases(OcbDkgTest) |
| 839 |
return tests |
| 840 |
|
| 841 |
|
| 842 |
if __name__ == '__main__': |
| 843 |
def suite(): |
| 844 |
return unittest.TestSuite(get_tests()) |
| 845 |
unittest.main(defaultTest='suite') |