From: Philipp Gesang Date: Tue, 28 Feb 2017 14:45:16 +0000 (+0100) Subject: unit test crypto handling of data spanning multiple chunks X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=6d42f226c68014a4c26ad529310a093e84de41bd;p=python-delta-tar unit test crypto handling of data spanning multiple chunks --- diff --git a/deltatar/crypto.py b/deltatar/crypto.py index ff36690..18cc9ca 100755 --- a/deltatar/crypto.py +++ b/deltatar/crypto.py @@ -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; +# fn hdr_make (f : handle, h : hdrinfo) -> IOResult; # fn hdr_fmt (h : hdrinfo) -> String; # diff --git a/testing/test_crypto.py b/testing/test_crypto.py index 51c66d0..3381160 100644 --- a/testing/test_crypto.py +++ b/testing/test_crypto.py @@ -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)