| 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 json |
| 32 |
import unittest |
| 33 |
from binascii import unhexlify |
| 34 |
|
| 35 |
from Crypto.SelfTest.st_common import list_test_cases |
| 36 |
from Crypto.SelfTest.loader import load_test_vectors_wycheproof |
| 37 |
|
| 38 |
from Crypto.Util.py3compat import tobytes, bchr |
| 39 |
from Crypto.Cipher import AES |
| 40 |
from Crypto.Hash import SHAKE128 |
| 41 |
|
| 42 |
from Crypto.Util.strxor import strxor |
| 43 |
|
| 44 |
|
| 45 |
def get_tag_random(tag, length): |
| 46 |
return SHAKE128.new(data=tobytes(tag)).read(length) |
| 47 |
|
| 48 |
|
| 49 |
class SivTests(unittest.TestCase): |
| 50 |
|
| 51 |
key_256 = get_tag_random("key_256", 32) |
| 52 |
key_384 = get_tag_random("key_384", 48) |
| 53 |
key_512 = get_tag_random("key_512", 64) |
| 54 |
nonce_96 = get_tag_random("nonce_128", 12) |
| 55 |
data = get_tag_random("data", 128) |
| 56 |
|
| 57 |
def test_loopback_128(self): |
| 58 |
for key in self.key_256, self.key_384, self.key_512: |
| 59 |
cipher = AES.new(key, AES.MODE_SIV, nonce=self.nonce_96) |
| 60 |
pt = get_tag_random("plaintext", 16 * 100) |
| 61 |
ct, mac = cipher.encrypt_and_digest(pt) |
| 62 |
|
| 63 |
cipher = AES.new(key, AES.MODE_SIV, nonce=self.nonce_96) |
| 64 |
pt2 = cipher.decrypt_and_verify(ct, mac) |
| 65 |
self.assertEqual(pt, pt2) |
| 66 |
|
| 67 |
def test_nonce(self): |
| 68 |
|
| 69 |
AES.new(self.key_256, AES.MODE_SIV) |
| 70 |
|
| 71 |
cipher = AES.new(self.key_256, AES.MODE_SIV, self.nonce_96) |
| 72 |
ct1, tag1 = cipher.encrypt_and_digest(self.data) |
| 73 |
|
| 74 |
cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96) |
| 75 |
ct2, tag2 = cipher.encrypt_and_digest(self.data) |
| 76 |
self.assertEqual(ct1 + tag1, ct2 + tag2) |
| 77 |
|
| 78 |
def test_nonce_must_be_bytes(self): |
| 79 |
self.assertRaises(TypeError, AES.new, self.key_256, AES.MODE_SIV, |
| 80 |
nonce=u'test12345678') |
| 81 |
|
| 82 |
def test_nonce_length(self): |
| 83 |
|
| 84 |
self.assertRaises(ValueError, AES.new, self.key_256, AES.MODE_SIV, |
| 85 |
nonce=b"") |
| 86 |
|
| 87 |
for x in range(1, 128): |
| 88 |
cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=bchr(1) * x) |
| 89 |
cipher.encrypt_and_digest(b'\x01') |
| 90 |
|
| 91 |
def test_block_size_128(self): |
| 92 |
cipher = AES.new(self.key_256, AES.MODE_SIV, 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_256, AES.MODE_SIV, nonce=self.nonce_96) |
| 97 |
self.assertEqual(cipher.nonce, self.nonce_96) |
| 98 |
|
| 99 |
|
| 100 |
self.assertFalse(hasattr(AES.new(self.key_256, AES.MODE_SIV), "nonce")) |
| 101 |
|
| 102 |
def test_unknown_parameters(self): |
| 103 |
self.assertRaises(TypeError, AES.new, self.key_256, AES.MODE_SIV, |
| 104 |
self.nonce_96, 7) |
| 105 |
self.assertRaises(TypeError, AES.new, self.key_256, AES.MODE_SIV, |
| 106 |
nonce=self.nonce_96, unknown=7) |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96, |
| 111 |
use_aesni=False) |
| 112 |
|
| 113 |
def test_encrypt_excludes_decrypt(self): |
| 114 |
cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96) |
| 115 |
cipher.encrypt_and_digest(self.data) |
| 116 |
self.assertRaises(TypeError, cipher.decrypt, self.data) |
| 117 |
|
| 118 |
cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96) |
| 119 |
cipher.encrypt_and_digest(self.data) |
| 120 |
self.assertRaises(TypeError, cipher.decrypt_and_verify, |
| 121 |
self.data, self.data) |
| 122 |
|
| 123 |
def test_data_must_be_bytes(self): |
| 124 |
cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96) |
| 125 |
self.assertRaises(TypeError, cipher.encrypt, u'test1234567890-*') |
| 126 |
|
| 127 |
cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96) |
| 128 |
self.assertRaises(TypeError, cipher.decrypt_and_verify, |
| 129 |
u'test1234567890-*', b"xxxx") |
| 130 |
|
| 131 |
def test_mac_len(self): |
| 132 |
cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96) |
| 133 |
_, mac = cipher.encrypt_and_digest(self.data) |
| 134 |
self.assertEqual(len(mac), 16) |
| 135 |
|
| 136 |
def test_invalid_mac(self): |
| 137 |
from Crypto.Util.strxor import strxor_c |
| 138 |
cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96) |
| 139 |
ct, mac = cipher.encrypt_and_digest(self.data) |
| 140 |
|
| 141 |
invalid_mac = strxor_c(mac, 0x01) |
| 142 |
|
| 143 |
cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96) |
| 144 |
self.assertRaises(ValueError, cipher.decrypt_and_verify, ct, |
| 145 |
invalid_mac) |
| 146 |
|
| 147 |
def test_hex_mac(self): |
| 148 |
cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96) |
| 149 |
mac_hex = cipher.hexdigest() |
| 150 |
self.assertEqual(cipher.digest(), unhexlify(mac_hex)) |
| 151 |
|
| 152 |
cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96) |
| 153 |
cipher.hexverify(mac_hex) |
| 154 |
|
| 155 |
def test_bytearray(self): |
| 156 |
|
| 157 |
|
| 158 |
key = bytearray(self.key_256) |
| 159 |
nonce = bytearray(self.nonce_96) |
| 160 |
data = bytearray(self.data) |
| 161 |
header = bytearray(self.data) |
| 162 |
|
| 163 |
cipher1 = AES.new(self.key_256, |
| 164 |
AES.MODE_SIV, |
| 165 |
nonce=self.nonce_96) |
| 166 |
cipher1.update(self.data) |
| 167 |
ct, tag = cipher1.encrypt_and_digest(self.data) |
| 168 |
|
| 169 |
cipher2 = AES.new(key, |
| 170 |
AES.MODE_SIV, |
| 171 |
nonce=nonce) |
| 172 |
key[:3] = b'\xFF\xFF\xFF' |
| 173 |
nonce[:3] = b'\xFF\xFF\xFF' |
| 174 |
cipher2.update(header) |
| 175 |
header[:3] = b'\xFF\xFF\xFF' |
| 176 |
ct_test, tag_test = cipher2.encrypt_and_digest(data) |
| 177 |
|
| 178 |
self.assertEqual(ct, ct_test) |
| 179 |
self.assertEqual(tag, tag_test) |
| 180 |
self.assertEqual(cipher1.nonce, cipher2.nonce) |
| 181 |
|
| 182 |
|
| 183 |
key = bytearray(self.key_256) |
| 184 |
nonce = bytearray(self.nonce_96) |
| 185 |
header = bytearray(self.data) |
| 186 |
ct_ba = bytearray(ct) |
| 187 |
tag_ba = bytearray(tag) |
| 188 |
|
| 189 |
cipher3 = AES.new(key, |
| 190 |
AES.MODE_SIV, |
| 191 |
nonce=nonce) |
| 192 |
key[:3] = b'\xFF\xFF\xFF' |
| 193 |
nonce[:3] = b'\xFF\xFF\xFF' |
| 194 |
cipher3.update(header) |
| 195 |
header[:3] = b'\xFF\xFF\xFF' |
| 196 |
pt_test = cipher3.decrypt_and_verify(ct_ba, tag_ba) |
| 197 |
|
| 198 |
self.assertEqual(self.data, pt_test) |
| 199 |
|
| 200 |
def test_memoryview(self): |
| 201 |
|
| 202 |
|
| 203 |
key = memoryview(bytearray(self.key_256)) |
| 204 |
nonce = memoryview(bytearray(self.nonce_96)) |
| 205 |
data = memoryview(bytearray(self.data)) |
| 206 |
header = memoryview(bytearray(self.data)) |
| 207 |
|
| 208 |
cipher1 = AES.new(self.key_256, |
| 209 |
AES.MODE_SIV, |
| 210 |
nonce=self.nonce_96) |
| 211 |
cipher1.update(self.data) |
| 212 |
ct, tag = cipher1.encrypt_and_digest(self.data) |
| 213 |
|
| 214 |
cipher2 = AES.new(key, |
| 215 |
AES.MODE_SIV, |
| 216 |
nonce=nonce) |
| 217 |
key[:3] = b'\xFF\xFF\xFF' |
| 218 |
nonce[:3] = b'\xFF\xFF\xFF' |
| 219 |
cipher2.update(header) |
| 220 |
header[:3] = b'\xFF\xFF\xFF' |
| 221 |
ct_test, tag_test= cipher2.encrypt_and_digest(data) |
| 222 |
|
| 223 |
self.assertEqual(ct, ct_test) |
| 224 |
self.assertEqual(tag, tag_test) |
| 225 |
self.assertEqual(cipher1.nonce, cipher2.nonce) |
| 226 |
|
| 227 |
|
| 228 |
key = memoryview(bytearray(self.key_256)) |
| 229 |
nonce = memoryview(bytearray(self.nonce_96)) |
| 230 |
header = memoryview(bytearray(self.data)) |
| 231 |
ct_ba = memoryview(bytearray(ct)) |
| 232 |
tag_ba = memoryview(bytearray(tag)) |
| 233 |
|
| 234 |
cipher3 = AES.new(key, |
| 235 |
AES.MODE_SIV, |
| 236 |
nonce=nonce) |
| 237 |
key[:3] = b'\xFF\xFF\xFF' |
| 238 |
nonce[:3] = b'\xFF\xFF\xFF' |
| 239 |
cipher3.update(header) |
| 240 |
header[:3] = b'\xFF\xFF\xFF' |
| 241 |
pt_test = cipher3.decrypt_and_verify(ct_ba, tag_ba) |
| 242 |
|
| 243 |
self.assertEqual(self.data, pt_test) |
| 244 |
|
| 245 |
def test_output_param(self): |
| 246 |
|
| 247 |
pt = b'5' * 128 |
| 248 |
cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96) |
| 249 |
ct, tag = cipher.encrypt_and_digest(pt) |
| 250 |
|
| 251 |
output = bytearray(128) |
| 252 |
cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96) |
| 253 |
res, tag_out = cipher.encrypt_and_digest(pt, output=output) |
| 254 |
self.assertEqual(ct, output) |
| 255 |
self.assertEqual(res, None) |
| 256 |
self.assertEqual(tag, tag_out) |
| 257 |
|
| 258 |
cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96) |
| 259 |
res = cipher.decrypt_and_verify(ct, tag, output=output) |
| 260 |
self.assertEqual(pt, output) |
| 261 |
self.assertEqual(res, None) |
| 262 |
|
| 263 |
def test_output_param_memoryview(self): |
| 264 |
|
| 265 |
pt = b'5' * 128 |
| 266 |
cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96) |
| 267 |
ct, tag = cipher.encrypt_and_digest(pt) |
| 268 |
|
| 269 |
output = memoryview(bytearray(128)) |
| 270 |
cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96) |
| 271 |
cipher.encrypt_and_digest(pt, output=output) |
| 272 |
self.assertEqual(ct, output) |
| 273 |
|
| 274 |
cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96) |
| 275 |
cipher.decrypt_and_verify(ct, tag, output=output) |
| 276 |
self.assertEqual(pt, output) |
| 277 |
|
| 278 |
def test_output_param_neg(self): |
| 279 |
LEN_PT = 128 |
| 280 |
|
| 281 |
pt = b'5' * LEN_PT |
| 282 |
cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96) |
| 283 |
ct, tag = cipher.encrypt_and_digest(pt) |
| 284 |
|
| 285 |
cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96) |
| 286 |
self.assertRaises(TypeError, cipher.encrypt_and_digest, pt, output=b'0' * LEN_PT) |
| 287 |
|
| 288 |
cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96) |
| 289 |
self.assertRaises(TypeError, cipher.decrypt_and_verify, ct, tag, output=b'0' * LEN_PT) |
| 290 |
|
| 291 |
shorter_output = bytearray(LEN_PT - 1) |
| 292 |
cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96) |
| 293 |
self.assertRaises(ValueError, cipher.encrypt_and_digest, pt, output=shorter_output) |
| 294 |
cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96) |
| 295 |
self.assertRaises(ValueError, cipher.decrypt_and_verify, ct, tag, output=shorter_output) |
| 296 |
|
| 297 |
|
| 298 |
class SivFSMTests(unittest.TestCase): |
| 299 |
|
| 300 |
key_256 = get_tag_random("key_256", 32) |
| 301 |
nonce_96 = get_tag_random("nonce_96", 12) |
| 302 |
data = get_tag_random("data", 128) |
| 303 |
|
| 304 |
def test_invalid_init_encrypt(self): |
| 305 |
|
| 306 |
cipher = AES.new(self.key_256, AES.MODE_SIV, |
| 307 |
nonce=self.nonce_96) |
| 308 |
self.assertRaises(TypeError, cipher.encrypt, b"xxx") |
| 309 |
|
| 310 |
def test_invalid_init_decrypt(self): |
| 311 |
|
| 312 |
cipher = AES.new(self.key_256, AES.MODE_SIV, |
| 313 |
nonce=self.nonce_96) |
| 314 |
self.assertRaises(TypeError, cipher.decrypt, b"xxx") |
| 315 |
|
| 316 |
def test_valid_init_update_digest_verify(self): |
| 317 |
|
| 318 |
|
| 319 |
cipher = AES.new(self.key_256, AES.MODE_SIV, |
| 320 |
nonce=self.nonce_96) |
| 321 |
cipher.update(self.data) |
| 322 |
mac = cipher.digest() |
| 323 |
|
| 324 |
|
| 325 |
cipher = AES.new(self.key_256, AES.MODE_SIV, |
| 326 |
nonce=self.nonce_96) |
| 327 |
cipher.update(self.data) |
| 328 |
cipher.verify(mac) |
| 329 |
|
| 330 |
def test_valid_init_digest(self): |
| 331 |
|
| 332 |
cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96) |
| 333 |
cipher.digest() |
| 334 |
|
| 335 |
def test_valid_init_verify(self): |
| 336 |
|
| 337 |
cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96) |
| 338 |
mac = cipher.digest() |
| 339 |
|
| 340 |
cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96) |
| 341 |
cipher.verify(mac) |
| 342 |
|
| 343 |
def test_valid_multiple_digest_or_verify(self): |
| 344 |
|
| 345 |
cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96) |
| 346 |
cipher.update(self.data) |
| 347 |
first_mac = cipher.digest() |
| 348 |
for x in range(4): |
| 349 |
self.assertEqual(first_mac, cipher.digest()) |
| 350 |
|
| 351 |
|
| 352 |
cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96) |
| 353 |
cipher.update(self.data) |
| 354 |
for x in range(5): |
| 355 |
cipher.verify(first_mac) |
| 356 |
|
| 357 |
def test_valid_encrypt_and_digest_decrypt_and_verify(self): |
| 358 |
|
| 359 |
cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96) |
| 360 |
cipher.update(self.data) |
| 361 |
ct, mac = cipher.encrypt_and_digest(self.data) |
| 362 |
|
| 363 |
|
| 364 |
cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96) |
| 365 |
cipher.update(self.data) |
| 366 |
pt = cipher.decrypt_and_verify(ct, mac) |
| 367 |
self.assertEqual(self.data, pt) |
| 368 |
|
| 369 |
def test_invalid_multiple_encrypt_and_digest(self): |
| 370 |
cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96) |
| 371 |
ct, tag = cipher.encrypt_and_digest(self.data) |
| 372 |
self.assertRaises(TypeError, cipher.encrypt_and_digest, b'') |
| 373 |
|
| 374 |
def test_invalid_multiple_decrypt_and_verify(self): |
| 375 |
cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96) |
| 376 |
ct, tag = cipher.encrypt_and_digest(self.data) |
| 377 |
|
| 378 |
cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96) |
| 379 |
cipher.decrypt_and_verify(ct, tag) |
| 380 |
self.assertRaises(TypeError, cipher.decrypt_and_verify, ct, tag) |
| 381 |
|
| 382 |
|
| 383 |
def transform(tv): |
| 384 |
new_tv = [[unhexlify(x) for x in tv[0].split("-")]] |
| 385 |
new_tv += [ unhexlify(x) for x in tv[1:5]] |
| 386 |
if tv[5]: |
| 387 |
nonce = unhexlify(tv[5]) |
| 388 |
else: |
| 389 |
nonce = None |
| 390 |
new_tv += [ nonce ] |
| 391 |
return new_tv |
| 392 |
|
| 393 |
|
| 394 |
class TestVectors(unittest.TestCase): |
| 395 |
"""Class exercising the SIV test vectors found in RFC5297""" |
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
test_vectors_hex = [ |
| 409 |
( |
| 410 |
'101112131415161718191a1b1c1d1e1f2021222324252627', |
| 411 |
'112233445566778899aabbccddee', |
| 412 |
'40c02b9690c4dc04daef7f6afe5c', |
| 413 |
'85632d07c6e8f37f950acd320a2ecc93', |
| 414 |
'fffefdfcfbfaf9f8f7f6f5f4f3f2f1f0f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff', |
| 415 |
None |
| 416 |
), |
| 417 |
( |
| 418 |
'00112233445566778899aabbccddeeffdeaddadadeaddadaffeeddccbbaa9988' + |
| 419 |
'7766554433221100-102030405060708090a0', |
| 420 |
'7468697320697320736f6d6520706c61696e7465787420746f20656e63727970' + |
| 421 |
'74207573696e67205349562d414553', |
| 422 |
'cb900f2fddbe404326601965c889bf17dba77ceb094fa663b7a3f748ba8af829' + |
| 423 |
'ea64ad544a272e9c485b62a3fd5c0d', |
| 424 |
'7bdb6e3b432667eb06f4d14bff2fbd0f', |
| 425 |
'7f7e7d7c7b7a79787776757473727170404142434445464748494a4b4c4d4e4f', |
| 426 |
'09f911029d74e35bd84156c5635688c0' |
| 427 |
), |
| 428 |
] |
| 429 |
|
| 430 |
test_vectors = [ transform(tv) for tv in test_vectors_hex ] |
| 431 |
|
| 432 |
def runTest(self): |
| 433 |
for assoc_data, pt, ct, mac, key, nonce in self.test_vectors: |
| 434 |
|
| 435 |
|
| 436 |
cipher = AES.new(key, AES.MODE_SIV, nonce=nonce) |
| 437 |
for x in assoc_data: |
| 438 |
cipher.update(x) |
| 439 |
ct2, mac2 = cipher.encrypt_and_digest(pt) |
| 440 |
self.assertEqual(ct, ct2) |
| 441 |
self.assertEqual(mac, mac2) |
| 442 |
|
| 443 |
|
| 444 |
cipher = AES.new(key, AES.MODE_SIV, nonce=nonce) |
| 445 |
for x in assoc_data: |
| 446 |
cipher.update(x) |
| 447 |
pt2 = cipher.decrypt_and_verify(ct, mac) |
| 448 |
self.assertEqual(pt, pt2) |
| 449 |
|
| 450 |
|
| 451 |
class TestVectorsWycheproof(unittest.TestCase): |
| 452 |
|
| 453 |
def __init__(self): |
| 454 |
unittest.TestCase.__init__(self) |
| 455 |
self._id = "None" |
| 456 |
|
| 457 |
def setUp(self): |
| 458 |
self.tv = load_test_vectors_wycheproof(("Cipher", "wycheproof"), |
| 459 |
"aes_siv_cmac_test.json", |
| 460 |
"Wycheproof AES SIV") |
| 461 |
|
| 462 |
def shortDescription(self): |
| 463 |
return self._id |
| 464 |
|
| 465 |
def test_encrypt(self, tv): |
| 466 |
self._id = "Wycheproof Encrypt AES-SIV Test #" + str(tv.id) |
| 467 |
|
| 468 |
cipher = AES.new(tv.key, AES.MODE_SIV) |
| 469 |
cipher.update(tv.aad) |
| 470 |
ct, tag = cipher.encrypt_and_digest(tv.msg) |
| 471 |
if tv.valid: |
| 472 |
self.assertEqual(tag + ct, tv.ct) |
| 473 |
|
| 474 |
def test_decrypt(self, tv): |
| 475 |
self._id = "Wycheproof Decrypt AES_SIV Test #" + str(tv.id) |
| 476 |
|
| 477 |
cipher = AES.new(tv.key, AES.MODE_SIV) |
| 478 |
cipher.update(tv.aad) |
| 479 |
try: |
| 480 |
pt = cipher.decrypt_and_verify(tv.ct[16:], tv.ct[:16]) |
| 481 |
except ValueError: |
| 482 |
assert not tv.valid |
| 483 |
else: |
| 484 |
assert tv.valid |
| 485 |
self.assertEqual(pt, tv.msg) |
| 486 |
|
| 487 |
def runTest(self): |
| 488 |
|
| 489 |
for tv in self.tv: |
| 490 |
self.test_encrypt(tv) |
| 491 |
self.test_decrypt(tv) |
| 492 |
|
| 493 |
|
| 494 |
class TestVectorsWycheproof2(unittest.TestCase): |
| 495 |
|
| 496 |
def __init__(self): |
| 497 |
unittest.TestCase.__init__(self) |
| 498 |
self._id = "None" |
| 499 |
|
| 500 |
def setUp(self): |
| 501 |
self.tv = load_test_vectors_wycheproof(("Cipher", "wycheproof"), |
| 502 |
"aead_aes_siv_cmac_test.json", |
| 503 |
"Wycheproof AEAD SIV") |
| 504 |
|
| 505 |
def shortDescription(self): |
| 506 |
return self._id |
| 507 |
|
| 508 |
def test_encrypt(self, tv): |
| 509 |
self._id = "Wycheproof Encrypt AEAD-AES-SIV Test #" + str(tv.id) |
| 510 |
|
| 511 |
cipher = AES.new(tv.key, AES.MODE_SIV, nonce=tv.iv) |
| 512 |
cipher.update(tv.aad) |
| 513 |
ct, tag = cipher.encrypt_and_digest(tv.msg) |
| 514 |
if tv.valid: |
| 515 |
self.assertEqual(ct, tv.ct) |
| 516 |
self.assertEqual(tag, tv.tag) |
| 517 |
|
| 518 |
def test_decrypt(self, tv): |
| 519 |
self._id = "Wycheproof Decrypt AEAD-AES-SIV Test #" + str(tv.id) |
| 520 |
|
| 521 |
cipher = AES.new(tv.key, AES.MODE_SIV, nonce=tv.iv) |
| 522 |
cipher.update(tv.aad) |
| 523 |
try: |
| 524 |
pt = cipher.decrypt_and_verify(tv.ct, tv.tag) |
| 525 |
except ValueError: |
| 526 |
assert not tv.valid |
| 527 |
else: |
| 528 |
assert tv.valid |
| 529 |
self.assertEqual(pt, tv.msg) |
| 530 |
|
| 531 |
def runTest(self): |
| 532 |
|
| 533 |
for tv in self.tv: |
| 534 |
self.test_encrypt(tv) |
| 535 |
self.test_decrypt(tv) |
| 536 |
|
| 537 |
|
| 538 |
def get_tests(config={}): |
| 539 |
wycheproof_warnings = config.get('wycheproof_warnings') |
| 540 |
|
| 541 |
tests = [] |
| 542 |
tests += list_test_cases(SivTests) |
| 543 |
tests += list_test_cases(SivFSMTests) |
| 544 |
tests += [ TestVectors() ] |
| 545 |
tests += [ TestVectorsWycheproof() ] |
| 546 |
tests += [ TestVectorsWycheproof2() ] |
| 547 |
return tests |
| 548 |
|
| 549 |
|
| 550 |
if __name__ == '__main__': |
| 551 |
suite = lambda: unittest.TestSuite(get_tests()) |
| 552 |
unittest.main(defaultTest='suite') |