From: Philipp Gesang Date: Tue, 28 Feb 2017 14:58:20 +0000 (+0100) Subject: unit test auth tag handling X-Git-Tag: v2.2~7^2~242 X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=7c32c1762f0ca80d53d52d84ef23d97a6e6aca2e;p=python-delta-tar unit test auth tag handling --- diff --git a/testing/test_crypto.py b/testing/test_crypto.py index 3381160..559c06b 100644 --- a/testing/test_crypto.py +++ b/testing/test_crypto.py @@ -110,6 +110,19 @@ class CryptoLayerTest (unittest.TestCase): assert pt == TEST_PLAINTEXT + def test_crypto_aes_gcm_dec_missing_tag (self): + NaCl = os.urandom (CRYPTO_NACL_SIZE) + key = os.urandom (CRYPTO_KEY_SIZE) + enc = crypto.AES_GCM_context (crypto.ENCRYPT, key, TEST_AES_GCM_AAD) + iv = enc.iv + dec = crypto.AES_GCM_context (crypto.DECRYPT, key, TEST_AES_GCM_AAD, iv = iv) + ok, ct = enc.process_chunk (TEST_PLAINTEXT) + ok, _, tag = enc.done () + ok, pt = dec.process_chunk (ct) + with pytest.raises (ValueError): + ok, _, _ = dec.done () + + def test_crypto_aes_gcm_dec_bad_tag (self): NaCl = os.urandom (CRYPTO_NACL_SIZE) key = os.urandom (CRYPTO_KEY_SIZE) @@ -177,6 +190,67 @@ class CryptoLayerTest (unittest.TestCase): assert pt == orig_pt + def test_crypto_aes_gcm_dec_multicnk_bad_tag (self): + cnksiz = 1 << 10 + orig_pt = fill_mod (1 << 14) + NaCl = os.urandom (CRYPTO_NACL_SIZE) + key = os.urandom (CRYPTO_KEY_SIZE) + enc = crypto.AES_GCM_context (crypto.ENCRYPT, key, TEST_AES_GCM_AAD) + iv = enc.iv + dec = crypto.AES_GCM_context (crypto.DECRYPT, key, TEST_AES_GCM_AAD, iv = iv) + + off = 0 + ct = b"" + while off < len (orig_pt): + upto = min (off + cnksiz, len (orig_pt)) + ok, cnk = enc.process_chunk (orig_pt [off:upto]) + ct += cnk + off += cnksiz + ok, _, tag = enc.done () + + off = 0 + pt = b"" + while off < len (orig_pt): + upto = min (off + cnksiz, len (orig_pt)) + ok, cnk = dec.process_chunk (ct [off:upto]) + pt += cnk + off += cnksiz + + with pytest.raises (cryptography.exceptions.InvalidTag): + tag = b"Y" + tag[:-1] + ok, _, _ = dec.done (tag) + + + def test_crypto_aes_gcm_dec_multicnk_missing_tag (self): + cnksiz = 1 << 10 + orig_pt = fill_mod (1 << 14) + NaCl = os.urandom (CRYPTO_NACL_SIZE) + key = os.urandom (CRYPTO_KEY_SIZE) + enc = crypto.AES_GCM_context (crypto.ENCRYPT, key, TEST_AES_GCM_AAD) + iv = enc.iv + dec = crypto.AES_GCM_context (crypto.DECRYPT, key, TEST_AES_GCM_AAD, iv = iv) + + off = 0 + ct = b"" + while off < len (orig_pt): + upto = min (off + cnksiz, len (orig_pt)) + ok, cnk = enc.process_chunk (orig_pt [off:upto]) + ct += cnk + off += cnksiz + ok, _, tag = enc.done () + + off = 0 + pt = b"" + while off < len (orig_pt): + upto = min (off + cnksiz, len (orig_pt)) + ok, cnk = dec.process_chunk (ct [off:upto]) + pt += cnk + off += cnksiz + + with pytest.raises (ValueError): + ok, _, _ = dec.done () + + def test_crypto_fmt_hdr_make (self): meta = faux_hdr() ok, hdr = crypto.hdr_make (meta)