unit test auth tag handling
authorPhilipp Gesang <philipp.gesang@intra2net.com>
Tue, 28 Feb 2017 14:58:20 +0000 (15:58 +0100)
committerThomas Jarosch <thomas.jarosch@intra2net.com>
Mon, 2 Apr 2018 11:34:08 +0000 (13:34 +0200)
testing/test_crypto.py

index 3381160..559c06b 100644 (file)
@@ -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)