unit test crypto handling of data spanning multiple chunks
authorPhilipp Gesang <philipp.gesang@intra2net.com>
Tue, 28 Feb 2017 14:45:16 +0000 (15:45 +0100)
committerThomas Jarosch <thomas.jarosch@intra2net.com>
Mon, 2 Apr 2018 11:34:08 +0000 (13:34 +0200)
deltatar/crypto.py
testing/test_crypto.py

index ff36690..18cc9ca 100755 (executable)
@@ -53,7 +53,7 @@ from cryptography.hazmat.backends import default_backend
 
 __all__ = [ "ENCRYPT", "DECRYPT"
           , "AES_GCM_context"
-          , "hdr_make", "hdr_read", "hdr_write"
+          , "hdr_make", "hdr_read", "hdr_fmt", "hdr_fmt_pretty"
           , "I2N_HDR_SIZE" ]
 
 ###############################################################################
@@ -118,7 +118,7 @@ SCRYPT_NaCl_LEN  = 16
 #    tag : [u8; 16]
 #
 #    fn hdr_read (f : handle) -> hdrinfo;
-#    fn hdr_write (f : handle, h : hdrinfo) -> IOResult<usize>;
+#    fn hdr_make (f : handle, h : hdrinfo) -> IOResult<usize>;
 #    fn hdr_fmt (h : hdrinfo) -> String;
 #
 
index 51c66d0..3381160 100644 (file)
@@ -2,6 +2,7 @@ import binascii
 import os
 import pylibscrypt
 import pytest
+import struct
 import unittest
 
 import deltatar.crypto as crypto
@@ -31,6 +32,15 @@ def faux_hdr (ctsize=1337, iv=None):
         }
 
 
+def fill_mod (n):
+    buf = bytearray (n)
+    bufv = memoryview (buf)
+    for i in range (n):
+        c = i % 64 + 32
+        struct.pack_into ("c", bufv, i, chr(c).encode("UTF-8"))
+    return bytes (buf)
+
+
 def faux_payload ():
     return "abcd" * 42
 
@@ -114,6 +124,59 @@ class CryptoLayerTest (unittest.TestCase):
             ok, _, _ = dec.done (tag)
 
 
+    def test_crypto_aes_gcm_enc_multicnk (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])
+            assert ok
+            ct += cnk
+            off += cnksiz
+        ok, _, tag = enc.done ()
+        assert ok
+        assert tag
+        assert len (ct) == len (orig_pt)
+
+
+    def test_crypto_aes_gcm_dec_multicnk (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
+        ok, _, _ = dec.done (tag)
+
+        assert pt == orig_pt
+
+
     def test_crypto_fmt_hdr_make (self):
         meta = faux_hdr()
         ok, hdr = crypto.hdr_make (meta)