| 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 |
| 37 |
from Crypto.Cipher import ChaCha20_Poly1305 |
| 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 ChaCha20Poly1305Tests(unittest.TestCase): |
| 48 |
|
| 49 |
key_256 = get_tag_random("key_256", 32) |
| 50 |
nonce_96 = get_tag_random("nonce_96", 12) |
| 51 |
data_128 = get_tag_random("data_128", 16) |
| 52 |
|
| 53 |
def test_loopback(self): |
| 54 |
cipher = ChaCha20_Poly1305.new(key=self.key_256, |
| 55 |
nonce=self.nonce_96) |
| 56 |
pt = get_tag_random("plaintext", 16 * 100) |
| 57 |
ct = cipher.encrypt(pt) |
| 58 |
|
| 59 |
cipher = ChaCha20_Poly1305.new(key=self.key_256, |
| 60 |
nonce=self.nonce_96) |
| 61 |
pt2 = cipher.decrypt(ct) |
| 62 |
self.assertEqual(pt, pt2) |
| 63 |
|
| 64 |
def test_nonce(self): |
| 65 |
|
| 66 |
cipher = ChaCha20_Poly1305.new(key=self.key_256, |
| 67 |
nonce=b'H' * 8) |
| 68 |
self.assertEqual(len(cipher.nonce), 8) |
| 69 |
cipher = ChaCha20_Poly1305.new(key=self.key_256, |
| 70 |
nonce=b'H' * 12) |
| 71 |
self.assertEqual(len(cipher.nonce), 12) |
| 72 |
|
| 73 |
|
| 74 |
cipher = ChaCha20_Poly1305.new(key=self.key_256) |
| 75 |
nonce1 = cipher.nonce |
| 76 |
cipher = ChaCha20_Poly1305.new(key=self.key_256) |
| 77 |
nonce2 = cipher.nonce |
| 78 |
self.assertEqual(len(nonce1), 12) |
| 79 |
self.assertNotEqual(nonce1, nonce2) |
| 80 |
|
| 81 |
cipher = ChaCha20_Poly1305.new(key=self.key_256, |
| 82 |
nonce=self.nonce_96) |
| 83 |
ct = cipher.encrypt(self.data_128) |
| 84 |
|
| 85 |
cipher = ChaCha20_Poly1305.new(key=self.key_256, |
| 86 |
nonce=self.nonce_96) |
| 87 |
self.assertEqual(ct, cipher.encrypt(self.data_128)) |
| 88 |
|
| 89 |
def test_nonce_must_be_bytes(self): |
| 90 |
self.assertRaises(TypeError, |
| 91 |
ChaCha20_Poly1305.new, |
| 92 |
key=self.key_256, |
| 93 |
nonce=u'test12345678') |
| 94 |
|
| 95 |
def test_nonce_length(self): |
| 96 |
|
| 97 |
self.assertRaises(ValueError, |
| 98 |
ChaCha20_Poly1305.new, |
| 99 |
key=self.key_256, |
| 100 |
nonce=b'0' * 7) |
| 101 |
self.assertRaises(ValueError, |
| 102 |
ChaCha20_Poly1305.new, |
| 103 |
key=self.key_256, |
| 104 |
nonce=b'') |
| 105 |
|
| 106 |
def test_block_size(self): |
| 107 |
|
| 108 |
cipher = ChaCha20_Poly1305.new(key=self.key_256, |
| 109 |
nonce=self.nonce_96) |
| 110 |
self.assertFalse(hasattr(cipher, 'block_size')) |
| 111 |
|
| 112 |
def test_nonce_attribute(self): |
| 113 |
cipher = ChaCha20_Poly1305.new(key=self.key_256, |
| 114 |
nonce=self.nonce_96) |
| 115 |
self.assertEqual(cipher.nonce, self.nonce_96) |
| 116 |
|
| 117 |
|
| 118 |
nonce1 = ChaCha20_Poly1305.new(key=self.key_256).nonce |
| 119 |
nonce2 = ChaCha20_Poly1305.new(key=self.key_256).nonce |
| 120 |
self.assertEqual(len(nonce1), 12) |
| 121 |
self.assertNotEqual(nonce1, nonce2) |
| 122 |
|
| 123 |
def test_unknown_parameters(self): |
| 124 |
self.assertRaises(TypeError, |
| 125 |
ChaCha20_Poly1305.new, |
| 126 |
key=self.key_256, |
| 127 |
param=9) |
| 128 |
|
| 129 |
def test_null_encryption_decryption(self): |
| 130 |
for func in "encrypt", "decrypt": |
| 131 |
cipher = ChaCha20_Poly1305.new(key=self.key_256, |
| 132 |
nonce=self.nonce_96) |
| 133 |
result = getattr(cipher, func)(b"") |
| 134 |
self.assertEqual(result, b"") |
| 135 |
|
| 136 |
def test_either_encrypt_or_decrypt(self): |
| 137 |
cipher = ChaCha20_Poly1305.new(key=self.key_256, |
| 138 |
nonce=self.nonce_96) |
| 139 |
cipher.encrypt(b"") |
| 140 |
self.assertRaises(TypeError, cipher.decrypt, b"") |
| 141 |
|
| 142 |
cipher = ChaCha20_Poly1305.new(key=self.key_256, |
| 143 |
nonce=self.nonce_96) |
| 144 |
cipher.decrypt(b"") |
| 145 |
self.assertRaises(TypeError, cipher.encrypt, b"") |
| 146 |
|
| 147 |
def test_data_must_be_bytes(self): |
| 148 |
cipher = ChaCha20_Poly1305.new(key=self.key_256, |
| 149 |
nonce=self.nonce_96) |
| 150 |
self.assertRaises(TypeError, cipher.encrypt, u'test1234567890-*') |
| 151 |
|
| 152 |
cipher = ChaCha20_Poly1305.new(key=self.key_256, |
| 153 |
nonce=self.nonce_96) |
| 154 |
self.assertRaises(TypeError, cipher.decrypt, u'test1234567890-*') |
| 155 |
|
| 156 |
def test_mac_len(self): |
| 157 |
cipher = ChaCha20_Poly1305.new(key=self.key_256, |
| 158 |
nonce=self.nonce_96) |
| 159 |
_, mac = cipher.encrypt_and_digest(self.data_128) |
| 160 |
self.assertEqual(len(mac), 16) |
| 161 |
|
| 162 |
def test_invalid_mac(self): |
| 163 |
from Crypto.Util.strxor import strxor_c |
| 164 |
cipher = ChaCha20_Poly1305.new(key=self.key_256, |
| 165 |
nonce=self.nonce_96) |
| 166 |
ct, mac = cipher.encrypt_and_digest(self.data_128) |
| 167 |
|
| 168 |
invalid_mac = strxor_c(mac, 0x01) |
| 169 |
|
| 170 |
cipher = ChaCha20_Poly1305.new(key=self.key_256, |
| 171 |
nonce=self.nonce_96) |
| 172 |
self.assertRaises(ValueError, cipher.decrypt_and_verify, ct, |
| 173 |
invalid_mac) |
| 174 |
|
| 175 |
def test_hex_mac(self): |
| 176 |
cipher = ChaCha20_Poly1305.new(key=self.key_256, |
| 177 |
nonce=self.nonce_96) |
| 178 |
mac_hex = cipher.hexdigest() |
| 179 |
self.assertEqual(cipher.digest(), unhexlify(mac_hex)) |
| 180 |
|
| 181 |
cipher = ChaCha20_Poly1305.new(key=self.key_256, |
| 182 |
nonce=self.nonce_96) |
| 183 |
cipher.hexverify(mac_hex) |
| 184 |
|
| 185 |
def test_message_chunks(self): |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
auth_data = get_tag_random("authenticated data", 127) |
| 190 |
plaintext = get_tag_random("plaintext", 127) |
| 191 |
|
| 192 |
cipher = ChaCha20_Poly1305.new(key=self.key_256, |
| 193 |
nonce=self.nonce_96) |
| 194 |
cipher.update(auth_data) |
| 195 |
ciphertext, ref_mac = cipher.encrypt_and_digest(plaintext) |
| 196 |
|
| 197 |
def break_up(data, chunk_length): |
| 198 |
return [data[i:i+chunk_length] for i in range(0, len(data), |
| 199 |
chunk_length)] |
| 200 |
|
| 201 |
|
| 202 |
for chunk_length in 1, 2, 3, 7, 10, 13, 16, 40, 80, 128: |
| 203 |
|
| 204 |
cipher = ChaCha20_Poly1305.new(key=self.key_256, |
| 205 |
nonce=self.nonce_96) |
| 206 |
|
| 207 |
for chunk in break_up(auth_data, chunk_length): |
| 208 |
cipher.update(chunk) |
| 209 |
pt2 = b"" |
| 210 |
for chunk in break_up(ciphertext, chunk_length): |
| 211 |
pt2 += cipher.decrypt(chunk) |
| 212 |
self.assertEqual(plaintext, pt2) |
| 213 |
cipher.verify(ref_mac) |
| 214 |
|
| 215 |
|
| 216 |
for chunk_length in 1, 2, 3, 7, 10, 13, 16, 40, 80, 128: |
| 217 |
|
| 218 |
cipher = ChaCha20_Poly1305.new(key=self.key_256, |
| 219 |
nonce=self.nonce_96) |
| 220 |
|
| 221 |
for chunk in break_up(auth_data, chunk_length): |
| 222 |
cipher.update(chunk) |
| 223 |
ct2 = b"" |
| 224 |
for chunk in break_up(plaintext, chunk_length): |
| 225 |
ct2 += cipher.encrypt(chunk) |
| 226 |
self.assertEqual(ciphertext, ct2) |
| 227 |
self.assertEqual(cipher.digest(), ref_mac) |
| 228 |
|
| 229 |
def test_bytearray(self): |
| 230 |
|
| 231 |
|
| 232 |
key_ba = bytearray(self.key_256) |
| 233 |
nonce_ba = bytearray(self.nonce_96) |
| 234 |
header_ba = bytearray(self.data_128) |
| 235 |
data_ba = bytearray(self.data_128) |
| 236 |
|
| 237 |
cipher1 = ChaCha20_Poly1305.new(key=self.key_256, |
| 238 |
nonce=self.nonce_96) |
| 239 |
cipher1.update(self.data_128) |
| 240 |
ct = cipher1.encrypt(self.data_128) |
| 241 |
tag = cipher1.digest() |
| 242 |
|
| 243 |
cipher2 = ChaCha20_Poly1305.new(key=self.key_256, |
| 244 |
nonce=self.nonce_96) |
| 245 |
key_ba[:3] = b'\xFF\xFF\xFF' |
| 246 |
nonce_ba[:3] = b'\xFF\xFF\xFF' |
| 247 |
cipher2.update(header_ba) |
| 248 |
header_ba[:3] = b'\xFF\xFF\xFF' |
| 249 |
ct_test = cipher2.encrypt(data_ba) |
| 250 |
data_ba[:3] = b'\x99\x99\x99' |
| 251 |
tag_test = cipher2.digest() |
| 252 |
|
| 253 |
self.assertEqual(ct, ct_test) |
| 254 |
self.assertEqual(tag, tag_test) |
| 255 |
self.assertEqual(cipher1.nonce, cipher2.nonce) |
| 256 |
|
| 257 |
|
| 258 |
key_ba = bytearray(self.key_256) |
| 259 |
nonce_ba = bytearray(self.nonce_96) |
| 260 |
header_ba = bytearray(self.data_128) |
| 261 |
ct_ba = bytearray(ct) |
| 262 |
tag_ba = bytearray(tag) |
| 263 |
del data_ba |
| 264 |
|
| 265 |
cipher3 = ChaCha20_Poly1305.new(key=self.key_256, |
| 266 |
nonce=self.nonce_96) |
| 267 |
key_ba[:3] = b'\xFF\xFF\xFF' |
| 268 |
nonce_ba[:3] = b'\xFF\xFF\xFF' |
| 269 |
cipher3.update(header_ba) |
| 270 |
header_ba[:3] = b'\xFF\xFF\xFF' |
| 271 |
pt_test = cipher3.decrypt(ct_ba) |
| 272 |
ct_ba[:3] = b'\xFF\xFF\xFF' |
| 273 |
cipher3.verify(tag_ba) |
| 274 |
|
| 275 |
self.assertEqual(pt_test, self.data_128) |
| 276 |
|
| 277 |
def test_memoryview(self): |
| 278 |
|
| 279 |
|
| 280 |
key_mv = memoryview(bytearray(self.key_256)) |
| 281 |
nonce_mv = memoryview(bytearray(self.nonce_96)) |
| 282 |
header_mv = memoryview(bytearray(self.data_128)) |
| 283 |
data_mv = memoryview(bytearray(self.data_128)) |
| 284 |
|
| 285 |
cipher1 = ChaCha20_Poly1305.new(key=self.key_256, |
| 286 |
nonce=self.nonce_96) |
| 287 |
cipher1.update(self.data_128) |
| 288 |
ct = cipher1.encrypt(self.data_128) |
| 289 |
tag = cipher1.digest() |
| 290 |
|
| 291 |
cipher2 = ChaCha20_Poly1305.new(key=self.key_256, |
| 292 |
nonce=self.nonce_96) |
| 293 |
key_mv[:3] = b'\xFF\xFF\xFF' |
| 294 |
nonce_mv[:3] = b'\xFF\xFF\xFF' |
| 295 |
cipher2.update(header_mv) |
| 296 |
header_mv[:3] = b'\xFF\xFF\xFF' |
| 297 |
ct_test = cipher2.encrypt(data_mv) |
| 298 |
data_mv[:3] = b'\x99\x99\x99' |
| 299 |
tag_test = cipher2.digest() |
| 300 |
|
| 301 |
self.assertEqual(ct, ct_test) |
| 302 |
self.assertEqual(tag, tag_test) |
| 303 |
self.assertEqual(cipher1.nonce, cipher2.nonce) |
| 304 |
|
| 305 |
|
| 306 |
key_mv = memoryview(bytearray(self.key_256)) |
| 307 |
nonce_mv = memoryview(bytearray(self.nonce_96)) |
| 308 |
header_mv = memoryview(bytearray(self.data_128)) |
| 309 |
ct_mv = memoryview(bytearray(ct)) |
| 310 |
tag_mv = memoryview(bytearray(tag)) |
| 311 |
del data_mv |
| 312 |
|
| 313 |
cipher3 = ChaCha20_Poly1305.new(key=self.key_256, |
| 314 |
nonce=self.nonce_96) |
| 315 |
key_mv[:3] = b'\xFF\xFF\xFF' |
| 316 |
nonce_mv[:3] = b'\xFF\xFF\xFF' |
| 317 |
cipher3.update(header_mv) |
| 318 |
header_mv[:3] = b'\xFF\xFF\xFF' |
| 319 |
pt_test = cipher3.decrypt(ct_mv) |
| 320 |
ct_mv[:3] = b'\x99\x99\x99' |
| 321 |
cipher3.verify(tag_mv) |
| 322 |
|
| 323 |
self.assertEqual(pt_test, self.data_128) |
| 324 |
|
| 325 |
|
| 326 |
class XChaCha20Poly1305Tests(unittest.TestCase): |
| 327 |
|
| 328 |
def test_nonce(self): |
| 329 |
|
| 330 |
cipher = ChaCha20_Poly1305.new(key=b'Y' * 32, |
| 331 |
nonce=b'H' * 24) |
| 332 |
self.assertEqual(len(cipher.nonce), 24) |
| 333 |
self.assertEqual(cipher.nonce, b'H' * 24) |
| 334 |
|
| 335 |
def test_encrypt(self): |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
pt = b""" |
| 340 |
4c616469657320616e642047656e746c656d656e206f662074686520636c6173 |
| 341 |
73206f66202739393a204966204920636f756c64206f6666657220796f75206f |
| 342 |
6e6c79206f6e652074697020666f7220746865206675747572652c2073756e73 |
| 343 |
637265656e20776f756c642062652069742e""" |
| 344 |
pt = unhexlify(pt.replace(b"\n", b"").replace(b" ", b"")) |
| 345 |
|
| 346 |
aad = unhexlify(b"50515253c0c1c2c3c4c5c6c7") |
| 347 |
key = unhexlify(b"808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9f") |
| 348 |
iv = unhexlify(b"404142434445464748494a4b4c4d4e4f5051525354555657") |
| 349 |
|
| 350 |
ct = b""" |
| 351 |
bd6d179d3e83d43b9576579493c0e939572a1700252bfaccbed2902c21396cbb |
| 352 |
731c7f1b0b4aa6440bf3a82f4eda7e39ae64c6708c54c216cb96b72e1213b452 |
| 353 |
2f8c9ba40db5d945b11b69b982c1bb9e3f3fac2bc369488f76b2383565d3fff9 |
| 354 |
21f9664c97637da9768812f615c68b13b52e""" |
| 355 |
ct = unhexlify(ct.replace(b"\n", b"").replace(b" ", b"")) |
| 356 |
|
| 357 |
tag = unhexlify(b"c0875924c1c7987947deafd8780acf49") |
| 358 |
|
| 359 |
cipher = ChaCha20_Poly1305.new(key=key, nonce=iv) |
| 360 |
cipher.update(aad) |
| 361 |
ct_test, tag_test = cipher.encrypt_and_digest(pt) |
| 362 |
|
| 363 |
self.assertEqual(ct, ct_test) |
| 364 |
self.assertEqual(tag, tag_test) |
| 365 |
|
| 366 |
cipher = ChaCha20_Poly1305.new(key=key, nonce=iv) |
| 367 |
cipher.update(aad) |
| 368 |
cipher.decrypt_and_verify(ct, tag) |
| 369 |
|
| 370 |
|
| 371 |
class ChaCha20Poly1305FSMTests(unittest.TestCase): |
| 372 |
|
| 373 |
key_256 = get_tag_random("key_256", 32) |
| 374 |
nonce_96 = get_tag_random("nonce_96", 12) |
| 375 |
data_128 = get_tag_random("data_128", 16) |
| 376 |
|
| 377 |
def test_valid_init_encrypt_decrypt_digest_verify(self): |
| 378 |
|
| 379 |
|
| 380 |
cipher = ChaCha20_Poly1305.new(key=self.key_256, |
| 381 |
nonce=self.nonce_96) |
| 382 |
ct = cipher.encrypt(self.data_128) |
| 383 |
mac = cipher.digest() |
| 384 |
|
| 385 |
|
| 386 |
cipher = ChaCha20_Poly1305.new(key=self.key_256, |
| 387 |
nonce=self.nonce_96) |
| 388 |
cipher.decrypt(ct) |
| 389 |
cipher.verify(mac) |
| 390 |
|
| 391 |
def test_valid_init_update_digest_verify(self): |
| 392 |
|
| 393 |
|
| 394 |
cipher = ChaCha20_Poly1305.new(key=self.key_256, |
| 395 |
nonce=self.nonce_96) |
| 396 |
cipher.update(self.data_128) |
| 397 |
mac = cipher.digest() |
| 398 |
|
| 399 |
|
| 400 |
cipher = ChaCha20_Poly1305.new(key=self.key_256, |
| 401 |
nonce=self.nonce_96) |
| 402 |
cipher.update(self.data_128) |
| 403 |
cipher.verify(mac) |
| 404 |
|
| 405 |
def test_valid_full_path(self): |
| 406 |
|
| 407 |
|
| 408 |
cipher = ChaCha20_Poly1305.new(key=self.key_256, |
| 409 |
nonce=self.nonce_96) |
| 410 |
cipher.update(self.data_128) |
| 411 |
ct = cipher.encrypt(self.data_128) |
| 412 |
mac = cipher.digest() |
| 413 |
|
| 414 |
|
| 415 |
cipher = ChaCha20_Poly1305.new(key=self.key_256, |
| 416 |
nonce=self.nonce_96) |
| 417 |
cipher.update(self.data_128) |
| 418 |
cipher.decrypt(ct) |
| 419 |
cipher.verify(mac) |
| 420 |
|
| 421 |
def test_valid_init_digest(self): |
| 422 |
|
| 423 |
cipher = ChaCha20_Poly1305.new(key=self.key_256, |
| 424 |
nonce=self.nonce_96) |
| 425 |
cipher.digest() |
| 426 |
|
| 427 |
def test_valid_init_verify(self): |
| 428 |
|
| 429 |
cipher = ChaCha20_Poly1305.new(key=self.key_256, |
| 430 |
nonce=self.nonce_96) |
| 431 |
mac = cipher.digest() |
| 432 |
|
| 433 |
cipher = ChaCha20_Poly1305.new(key=self.key_256, |
| 434 |
nonce=self.nonce_96) |
| 435 |
cipher.verify(mac) |
| 436 |
|
| 437 |
def test_valid_multiple_encrypt_or_decrypt(self): |
| 438 |
for method_name in "encrypt", "decrypt": |
| 439 |
for auth_data in (None, b"333", self.data_128, |
| 440 |
self.data_128 + b"3"): |
| 441 |
cipher = ChaCha20_Poly1305.new(key=self.key_256, |
| 442 |
nonce=self.nonce_96) |
| 443 |
if auth_data is not None: |
| 444 |
cipher.update(auth_data) |
| 445 |
method = getattr(cipher, method_name) |
| 446 |
method(self.data_128) |
| 447 |
method(self.data_128) |
| 448 |
method(self.data_128) |
| 449 |
method(self.data_128) |
| 450 |
|
| 451 |
def test_valid_multiple_digest_or_verify(self): |
| 452 |
|
| 453 |
cipher = ChaCha20_Poly1305.new(key=self.key_256, |
| 454 |
nonce=self.nonce_96) |
| 455 |
cipher.update(self.data_128) |
| 456 |
first_mac = cipher.digest() |
| 457 |
for x in range(4): |
| 458 |
self.assertEqual(first_mac, cipher.digest()) |
| 459 |
|
| 460 |
|
| 461 |
cipher = ChaCha20_Poly1305.new(key=self.key_256, |
| 462 |
nonce=self.nonce_96) |
| 463 |
cipher.update(self.data_128) |
| 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 = ChaCha20_Poly1305.new(key=self.key_256, |
| 470 |
nonce=self.nonce_96) |
| 471 |
cipher.update(self.data_128) |
| 472 |
ct, mac = cipher.encrypt_and_digest(self.data_128) |
| 473 |
|
| 474 |
|
| 475 |
cipher = ChaCha20_Poly1305.new(key=self.key_256, |
| 476 |
nonce=self.nonce_96) |
| 477 |
cipher.update(self.data_128) |
| 478 |
pt = cipher.decrypt_and_verify(ct, mac) |
| 479 |
self.assertEqual(self.data_128, pt) |
| 480 |
|
| 481 |
def test_invalid_mixing_encrypt_decrypt(self): |
| 482 |
|
| 483 |
for method1_name, method2_name in (("encrypt", "decrypt"), |
| 484 |
("decrypt", "encrypt")): |
| 485 |
for assoc_data_present in (True, False): |
| 486 |
cipher = ChaCha20_Poly1305.new(key=self.key_256, |
| 487 |
nonce=self.nonce_96) |
| 488 |
if assoc_data_present: |
| 489 |
cipher.update(self.data_128) |
| 490 |
getattr(cipher, method1_name)(self.data_128) |
| 491 |
self.assertRaises(TypeError, getattr(cipher, method2_name), |
| 492 |
self.data_128) |
| 493 |
|
| 494 |
def test_invalid_encrypt_or_update_after_digest(self): |
| 495 |
for method_name in "encrypt", "update": |
| 496 |
cipher = ChaCha20_Poly1305.new(key=self.key_256, |
| 497 |
nonce=self.nonce_96) |
| 498 |
cipher.encrypt(self.data_128) |
| 499 |
cipher.digest() |
| 500 |
self.assertRaises(TypeError, getattr(cipher, method_name), |
| 501 |
self.data_128) |
| 502 |
|
| 503 |
cipher = ChaCha20_Poly1305.new(key=self.key_256, |
| 504 |
nonce=self.nonce_96) |
| 505 |
cipher.encrypt_and_digest(self.data_128) |
| 506 |
|
| 507 |
def test_invalid_decrypt_or_update_after_verify(self): |
| 508 |
cipher = ChaCha20_Poly1305.new(key=self.key_256, |
| 509 |
nonce=self.nonce_96) |
| 510 |
ct = cipher.encrypt(self.data_128) |
| 511 |
mac = cipher.digest() |
| 512 |
|
| 513 |
for method_name in "decrypt", "update": |
| 514 |
cipher = ChaCha20_Poly1305.new(key=self.key_256, |
| 515 |
nonce=self.nonce_96) |
| 516 |
cipher.decrypt(ct) |
| 517 |
cipher.verify(mac) |
| 518 |
self.assertRaises(TypeError, getattr(cipher, method_name), |
| 519 |
self.data_128) |
| 520 |
|
| 521 |
cipher = ChaCha20_Poly1305.new(key=self.key_256, |
| 522 |
nonce=self.nonce_96) |
| 523 |
cipher.decrypt(ct) |
| 524 |
cipher.verify(mac) |
| 525 |
self.assertRaises(TypeError, getattr(cipher, method_name), |
| 526 |
self.data_128) |
| 527 |
|
| 528 |
cipher = ChaCha20_Poly1305.new(key=self.key_256, |
| 529 |
nonce=self.nonce_96) |
| 530 |
cipher.decrypt_and_verify(ct, mac) |
| 531 |
self.assertRaises(TypeError, getattr(cipher, method_name), |
| 532 |
self.data_128) |
| 533 |
|
| 534 |
|
| 535 |
def compact(x): |
| 536 |
return unhexlify(x.replace(" ", "").replace(":", "")) |
| 537 |
|
| 538 |
|
| 539 |
class TestVectorsRFC(unittest.TestCase): |
| 540 |
"""Test cases from RFC7539""" |
| 541 |
|
| 542 |
|
| 543 |
test_vectors_hex = [ |
| 544 |
( '50 51 52 53 c0 c1 c2 c3 c4 c5 c6 c7', |
| 545 |
'4c 61 64 69 65 73 20 61 6e 64 20 47 65 6e 74 6c' |
| 546 |
'65 6d 65 6e 20 6f 66 20 74 68 65 20 63 6c 61 73' |
| 547 |
'73 20 6f 66 20 27 39 39 3a 20 49 66 20 49 20 63' |
| 548 |
'6f 75 6c 64 20 6f 66 66 65 72 20 79 6f 75 20 6f' |
| 549 |
'6e 6c 79 20 6f 6e 65 20 74 69 70 20 66 6f 72 20' |
| 550 |
'74 68 65 20 66 75 74 75 72 65 2c 20 73 75 6e 73' |
| 551 |
'63 72 65 65 6e 20 77 6f 75 6c 64 20 62 65 20 69' |
| 552 |
'74 2e', |
| 553 |
'd3 1a 8d 34 64 8e 60 db 7b 86 af bc 53 ef 7e c2' |
| 554 |
'a4 ad ed 51 29 6e 08 fe a9 e2 b5 a7 36 ee 62 d6' |
| 555 |
'3d be a4 5e 8c a9 67 12 82 fa fb 69 da 92 72 8b' |
| 556 |
'1a 71 de 0a 9e 06 0b 29 05 d6 a5 b6 7e cd 3b 36' |
| 557 |
'92 dd bd 7f 2d 77 8b 8c 98 03 ae e3 28 09 1b 58' |
| 558 |
'fa b3 24 e4 fa d6 75 94 55 85 80 8b 48 31 d7 bc' |
| 559 |
'3f f4 de f0 8e 4b 7a 9d e5 76 d2 65 86 ce c6 4b' |
| 560 |
'61 16', |
| 561 |
'1a:e1:0b:59:4f:09:e2:6a:7e:90:2e:cb:d0:60:06:91', |
| 562 |
'80 81 82 83 84 85 86 87 88 89 8a 8b 8c 8d 8e 8f' |
| 563 |
'90 91 92 93 94 95 96 97 98 99 9a 9b 9c 9d 9e 9f', |
| 564 |
'07 00 00 00' + '40 41 42 43 44 45 46 47', |
| 565 |
), |
| 566 |
( 'f3 33 88 86 00 00 00 00 00 00 4e 91', |
| 567 |
'49 6e 74 65 72 6e 65 74 2d 44 72 61 66 74 73 20' |
| 568 |
'61 72 65 20 64 72 61 66 74 20 64 6f 63 75 6d 65' |
| 569 |
'6e 74 73 20 76 61 6c 69 64 20 66 6f 72 20 61 20' |
| 570 |
'6d 61 78 69 6d 75 6d 20 6f 66 20 73 69 78 20 6d' |
| 571 |
'6f 6e 74 68 73 20 61 6e 64 20 6d 61 79 20 62 65' |
| 572 |
'20 75 70 64 61 74 65 64 2c 20 72 65 70 6c 61 63' |
| 573 |
'65 64 2c 20 6f 72 20 6f 62 73 6f 6c 65 74 65 64' |
| 574 |
'20 62 79 20 6f 74 68 65 72 20 64 6f 63 75 6d 65' |
| 575 |
'6e 74 73 20 61 74 20 61 6e 79 20 74 69 6d 65 2e' |
| 576 |
'20 49 74 20 69 73 20 69 6e 61 70 70 72 6f 70 72' |
| 577 |
'69 61 74 65 20 74 6f 20 75 73 65 20 49 6e 74 65' |
| 578 |
'72 6e 65 74 2d 44 72 61 66 74 73 20 61 73 20 72' |
| 579 |
'65 66 65 72 65 6e 63 65 20 6d 61 74 65 72 69 61' |
| 580 |
'6c 20 6f 72 20 74 6f 20 63 69 74 65 20 74 68 65' |
| 581 |
'6d 20 6f 74 68 65 72 20 74 68 61 6e 20 61 73 20' |
| 582 |
'2f e2 80 9c 77 6f 72 6b 20 69 6e 20 70 72 6f 67' |
| 583 |
'72 65 73 73 2e 2f e2 80 9d', |
| 584 |
'64 a0 86 15 75 86 1a f4 60 f0 62 c7 9b e6 43 bd' |
| 585 |
'5e 80 5c fd 34 5c f3 89 f1 08 67 0a c7 6c 8c b2' |
| 586 |
'4c 6c fc 18 75 5d 43 ee a0 9e e9 4e 38 2d 26 b0' |
| 587 |
'bd b7 b7 3c 32 1b 01 00 d4 f0 3b 7f 35 58 94 cf' |
| 588 |
'33 2f 83 0e 71 0b 97 ce 98 c8 a8 4a bd 0b 94 81' |
| 589 |
'14 ad 17 6e 00 8d 33 bd 60 f9 82 b1 ff 37 c8 55' |
| 590 |
'97 97 a0 6e f4 f0 ef 61 c1 86 32 4e 2b 35 06 38' |
| 591 |
'36 06 90 7b 6a 7c 02 b0 f9 f6 15 7b 53 c8 67 e4' |
| 592 |
'b9 16 6c 76 7b 80 4d 46 a5 9b 52 16 cd e7 a4 e9' |
| 593 |
'90 40 c5 a4 04 33 22 5e e2 82 a1 b0 a0 6c 52 3e' |
| 594 |
'af 45 34 d7 f8 3f a1 15 5b 00 47 71 8c bc 54 6a' |
| 595 |
'0d 07 2b 04 b3 56 4e ea 1b 42 22 73 f5 48 27 1a' |
| 596 |
'0b b2 31 60 53 fa 76 99 19 55 eb d6 31 59 43 4e' |
| 597 |
'ce bb 4e 46 6d ae 5a 10 73 a6 72 76 27 09 7a 10' |
| 598 |
'49 e6 17 d9 1d 36 10 94 fa 68 f0 ff 77 98 71 30' |
| 599 |
'30 5b ea ba 2e da 04 df 99 7b 71 4d 6c 6f 2c 29' |
| 600 |
'a6 ad 5c b4 02 2b 02 70 9b', |
| 601 |
'ee ad 9d 67 89 0c bb 22 39 23 36 fe a1 85 1f 38', |
| 602 |
'1c 92 40 a5 eb 55 d3 8a f3 33 88 86 04 f6 b5 f0' |
| 603 |
'47 39 17 c1 40 2b 80 09 9d ca 5c bc 20 70 75 c0', |
| 604 |
'00 00 00 00 01 02 03 04 05 06 07 08', |
| 605 |
) |
| 606 |
] |
| 607 |
|
| 608 |
test_vectors = [[unhexlify(x.replace(" ", "").replace(":", "")) for x in tv] for tv in test_vectors_hex] |
| 609 |
|
| 610 |
def runTest(self): |
| 611 |
for assoc_data, pt, ct, mac, key, nonce in self.test_vectors: |
| 612 |
|
| 613 |
cipher = ChaCha20_Poly1305.new(key=key, nonce=nonce) |
| 614 |
cipher.update(assoc_data) |
| 615 |
ct2, mac2 = cipher.encrypt_and_digest(pt) |
| 616 |
self.assertEqual(ct, ct2) |
| 617 |
self.assertEqual(mac, mac2) |
| 618 |
|
| 619 |
|
| 620 |
cipher = ChaCha20_Poly1305.new(key=key, nonce=nonce) |
| 621 |
cipher.update(assoc_data) |
| 622 |
pt2 = cipher.decrypt_and_verify(ct, mac) |
| 623 |
self.assertEqual(pt, pt2) |
| 624 |
|
| 625 |
|
| 626 |
class TestVectorsWycheproof(unittest.TestCase): |
| 627 |
|
| 628 |
def __init__(self, wycheproof_warnings): |
| 629 |
unittest.TestCase.__init__(self) |
| 630 |
self._wycheproof_warnings = wycheproof_warnings |
| 631 |
self._id = "None" |
| 632 |
|
| 633 |
def load_tests(self, filename): |
| 634 |
|
| 635 |
def filter_tag(group): |
| 636 |
return group['tagSize'] // 8 |
| 637 |
|
| 638 |
def filter_algo(root): |
| 639 |
return root['algorithm'] |
| 640 |
|
| 641 |
result = load_test_vectors_wycheproof(("Cipher", "wycheproof"), |
| 642 |
filename, |
| 643 |
"Wycheproof ChaCha20-Poly1305", |
| 644 |
root_tag={'algo': filter_algo}, |
| 645 |
group_tag={'tag_size': filter_tag}) |
| 646 |
return result |
| 647 |
|
| 648 |
def setUp(self): |
| 649 |
self.tv = [] |
| 650 |
self.tv.extend(self.load_tests("chacha20_poly1305_test.json")) |
| 651 |
self.tv.extend(self.load_tests("xchacha20_poly1305_test.json")) |
| 652 |
|
| 653 |
def shortDescription(self): |
| 654 |
return self._id |
| 655 |
|
| 656 |
def warn(self, tv): |
| 657 |
if tv.warning and self._wycheproof_warnings: |
| 658 |
import warnings |
| 659 |
warnings.warn("Wycheproof warning: %s (%s)" % (self._id, tv.comment)) |
| 660 |
|
| 661 |
def test_encrypt(self, tv): |
| 662 |
self._id = "Wycheproof Encrypt %s Test #%s" % (tv.algo, tv.id) |
| 663 |
|
| 664 |
try: |
| 665 |
cipher = ChaCha20_Poly1305.new(key=tv.key, nonce=tv.iv) |
| 666 |
except ValueError as e: |
| 667 |
assert len(tv.iv) not in (8, 12) and "Nonce must be" in str(e) |
| 668 |
return |
| 669 |
|
| 670 |
cipher.update(tv.aad) |
| 671 |
ct, tag = cipher.encrypt_and_digest(tv.msg) |
| 672 |
if tv.valid: |
| 673 |
self.assertEqual(ct, tv.ct) |
| 674 |
self.assertEqual(tag, tv.tag) |
| 675 |
self.warn(tv) |
| 676 |
|
| 677 |
def test_decrypt(self, tv): |
| 678 |
self._id = "Wycheproof Decrypt %s Test #%s" % (tv.algo, tv.id) |
| 679 |
|
| 680 |
try: |
| 681 |
cipher = ChaCha20_Poly1305.new(key=tv.key, nonce=tv.iv) |
| 682 |
except ValueError as e: |
| 683 |
assert len(tv.iv) not in (8, 12) and "Nonce must be" in str(e) |
| 684 |
return |
| 685 |
|
| 686 |
cipher.update(tv.aad) |
| 687 |
try: |
| 688 |
pt = cipher.decrypt_and_verify(tv.ct, tv.tag) |
| 689 |
except ValueError: |
| 690 |
assert not tv.valid |
| 691 |
else: |
| 692 |
assert tv.valid |
| 693 |
self.assertEqual(pt, tv.msg) |
| 694 |
self.warn(tv) |
| 695 |
|
| 696 |
def test_corrupt_decrypt(self, tv): |
| 697 |
self._id = "Wycheproof Corrupt Decrypt ChaCha20-Poly1305 Test #" + str(tv.id) |
| 698 |
if len(tv.iv) == 0 or len(tv.ct) < 1: |
| 699 |
return |
| 700 |
cipher = ChaCha20_Poly1305.new(key=tv.key, nonce=tv.iv) |
| 701 |
cipher.update(tv.aad) |
| 702 |
ct_corrupt = strxor(tv.ct, b"\x00" * (len(tv.ct) - 1) + b"\x01") |
| 703 |
self.assertRaises(ValueError, cipher.decrypt_and_verify, ct_corrupt, tv.tag) |
| 704 |
|
| 705 |
def runTest(self): |
| 706 |
|
| 707 |
for tv in self.tv: |
| 708 |
self.test_encrypt(tv) |
| 709 |
self.test_decrypt(tv) |
| 710 |
self.test_corrupt_decrypt(tv) |
| 711 |
|
| 712 |
|
| 713 |
class TestOutput(unittest.TestCase): |
| 714 |
|
| 715 |
def runTest(self): |
| 716 |
|
| 717 |
|
| 718 |
key = b'4' * 32 |
| 719 |
nonce = b'5' * 12 |
| 720 |
cipher = ChaCha20_Poly1305.new(key=key, nonce=nonce) |
| 721 |
|
| 722 |
pt = b'5' * 16 |
| 723 |
ct = cipher.encrypt(pt) |
| 724 |
|
| 725 |
output = bytearray(16) |
| 726 |
cipher = ChaCha20_Poly1305.new(key=key, nonce=nonce) |
| 727 |
res = cipher.encrypt(pt, output=output) |
| 728 |
self.assertEqual(ct, output) |
| 729 |
self.assertEqual(res, None) |
| 730 |
|
| 731 |
cipher = ChaCha20_Poly1305.new(key=key, nonce=nonce) |
| 732 |
res = cipher.decrypt(ct, output=output) |
| 733 |
self.assertEqual(pt, output) |
| 734 |
self.assertEqual(res, None) |
| 735 |
|
| 736 |
output = memoryview(bytearray(16)) |
| 737 |
cipher = ChaCha20_Poly1305.new(key=key, nonce=nonce) |
| 738 |
cipher.encrypt(pt, output=output) |
| 739 |
self.assertEqual(ct, output) |
| 740 |
|
| 741 |
cipher = ChaCha20_Poly1305.new(key=key, nonce=nonce) |
| 742 |
cipher.decrypt(ct, output=output) |
| 743 |
self.assertEqual(pt, output) |
| 744 |
|
| 745 |
cipher = ChaCha20_Poly1305.new(key=key, nonce=nonce) |
| 746 |
self.assertRaises(TypeError, cipher.encrypt, pt, output=b'0'*16) |
| 747 |
|
| 748 |
cipher = ChaCha20_Poly1305.new(key=key, nonce=nonce) |
| 749 |
self.assertRaises(TypeError, cipher.decrypt, ct, output=b'0'*16) |
| 750 |
|
| 751 |
shorter_output = bytearray(7) |
| 752 |
|
| 753 |
cipher = ChaCha20_Poly1305.new(key=key, nonce=nonce) |
| 754 |
self.assertRaises(ValueError, cipher.encrypt, pt, output=shorter_output) |
| 755 |
|
| 756 |
cipher = ChaCha20_Poly1305.new(key=key, nonce=nonce) |
| 757 |
self.assertRaises(ValueError, cipher.decrypt, ct, output=shorter_output) |
| 758 |
|
| 759 |
|
| 760 |
def get_tests(config={}): |
| 761 |
wycheproof_warnings = config.get('wycheproof_warnings') |
| 762 |
|
| 763 |
tests = [] |
| 764 |
tests += list_test_cases(ChaCha20Poly1305Tests) |
| 765 |
tests += list_test_cases(XChaCha20Poly1305Tests) |
| 766 |
tests += list_test_cases(ChaCha20Poly1305FSMTests) |
| 767 |
tests += [TestVectorsRFC()] |
| 768 |
tests += [TestVectorsWycheproof(wycheproof_warnings)] |
| 769 |
tests += [TestOutput()] |
| 770 |
return tests |
| 771 |
|
| 772 |
|
| 773 |
if __name__ == '__main__': |
| 774 |
def suite(): |
| 775 |
unittest.TestSuite(get_tests()) |
| 776 |
unittest.main(defaultTest='suite') |