From: Philipp Gesang Date: Tue, 28 Feb 2017 13:34:59 +0000 (+0100) Subject: add unit test module for encryption layer X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=67c721bae212647620145910982b181b3c499196;p=python-delta-tar add unit test module for encryption layer --- diff --git a/testing/test_crypto.py b/testing/test_crypto.py new file mode 100644 index 0000000..9406c9a --- /dev/null +++ b/testing/test_crypto.py @@ -0,0 +1,82 @@ +import binascii +import os +import pylibscrypt +import unittest + +import deltatar.crypto as crypto + +def b(s): + return s.encode("UTF-8") + +TEST_PLAINTEXT = b("gentlemen don’t read each other’s mail") +TEST_PASSPHRASE = b"test1234" +TEST_AES_GCM_AAD = b"authenticated plain text" + +CRYPTO_NACL_SIZE = 12 +CRYPTO_KEY_SIZE = 16 +CRYPTO_TAG_SIZE = 16 + +def faux_hdr (ctsize=1337, iv=None): + return \ + { "version" : 42 + , "paramversion" : 2187 + , "nacl" : binascii.unhexlify(b"0011223344556677" + b"8899aabbccddeeff") + , "iv" : iv or binascii.unhexlify(b"0011223344556677" + b"8899aabb") + , "ctsize" : ctsize + } + + +def faux_payload (): + return "abcd" * 42 + +class CryptoLayerTest (unittest.TestCase): + + def test_crypto_aes_gcm_enc_simple (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) + + def test_crypto_aes_gcm_enc_tag_retrieve (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) + ok, ct = enc.encrypt_chunk (TEST_PLAINTEXT) + if ok is False or ct is None: + raise "error encrypting chunk [%s]" % TEST_PLAINTEXT + ok, ct, tag = enc.done () + if ok is False or ct is None: + raise "error finalizing encryption" + if not tag: + raise "no tag received upon completing the encryption" + + def test_crypto_aes_gcm_enc_tag_size (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) + ok, ct = enc.encrypt_chunk (TEST_PLAINTEXT) + if ok is False or ct is None: + raise "error encrypting chunk [%s]" % TEST_PLAINTEXT + ok, ct, tag = enc.done () + if ok is False or ct is None: + raise "error finalizing encryption" + if not tag: + raise "no tag received upon completing the encryption" + assert len (tag) == CRYPTO_TAG_SIZE + + def test_crypto_aes_gcm_enc_chunk_size (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) + ok, ct = enc.encrypt_chunk (TEST_PLAINTEXT) + if ok is False or ct is None: + raise "error encrypting chunk [%s]" % TEST_PLAINTEXT + assert len (ct) == len (TEST_PLAINTEXT) + ok, ct, tag = enc.done () + if ok is False or ct is None: + raise "error finalizing encryption" + if not tag: + raise "no tag received upon completing the encryption" + assert len (ct) == 0 +