--- /dev/null
+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
+