YACA: Add GCM/CCM tag length combination tests 60/84060/9
authorMateusz Forc <m.forc@samsung.com>
Tue, 16 Aug 2016 09:56:44 +0000 (11:56 +0200)
committerDariusz Michaluk <d.michaluk@samsung.com>
Thu, 8 Sep 2016 15:03:04 +0000 (17:03 +0200)
Change-Id: I91d45e95f1e9b6b85bec8977dd2c9b67fcc70fb7

src/yaca/test-vectors/encrypt_aes_ccm_tag_len.txt [new file with mode: 0644]
src/yaca/test-vectors/encrypt_aes_gcm_tag_len.txt [new file with mode: 0644]
src/yaca/tools/containers.py
src/yaca/tools/encrypt_param_combinations.py
src/yaca/yaca-test-encrypt.cpp

diff --git a/src/yaca/test-vectors/encrypt_aes_ccm_tag_len.txt b/src/yaca/test-vectors/encrypt_aes_ccm_tag_len.txt
new file mode 100644 (file)
index 0000000..a8d8bcf
--- /dev/null
@@ -0,0 +1,118 @@
+################## Check tag_len for algo and bcm ##################
+
+################## AES ##################
+
+################## CCM ##################
+
+algo=AES
+bcm=CCM
+key_len=128
+iv_len=64
+tag_len=3
+valid=0
+
+algo=AES
+bcm=CCM
+key_len=128
+iv_len=64
+tag_len=4
+valid=1
+
+algo=AES
+bcm=CCM
+key_len=128
+iv_len=64
+tag_len=5
+valid=0
+
+algo=AES
+bcm=CCM
+key_len=128
+iv_len=64
+tag_len=6
+valid=1
+
+algo=AES
+bcm=CCM
+key_len=128
+iv_len=64
+tag_len=7
+valid=0
+
+algo=AES
+bcm=CCM
+key_len=128
+iv_len=64
+tag_len=8
+valid=1
+
+algo=AES
+bcm=CCM
+key_len=128
+iv_len=64
+tag_len=9
+valid=0
+
+algo=AES
+bcm=CCM
+key_len=128
+iv_len=64
+tag_len=10
+valid=1
+
+algo=AES
+bcm=CCM
+key_len=128
+iv_len=64
+tag_len=11
+valid=0
+
+algo=AES
+bcm=CCM
+key_len=128
+iv_len=64
+tag_len=12
+valid=1
+
+algo=AES
+bcm=CCM
+key_len=128
+iv_len=64
+tag_len=13
+valid=0
+
+algo=AES
+bcm=CCM
+key_len=128
+iv_len=64
+tag_len=14
+valid=1
+
+algo=AES
+bcm=CCM
+key_len=128
+iv_len=64
+tag_len=15
+valid=0
+
+algo=AES
+bcm=CCM
+key_len=128
+iv_len=64
+tag_len=16
+valid=1
+
+algo=AES
+bcm=CCM
+key_len=128
+iv_len=64
+tag_len=17
+valid=0
+
+algo=AES
+bcm=CCM
+key_len=128
+iv_len=64
+tag_len=18
+valid=0
+
diff --git a/src/yaca/test-vectors/encrypt_aes_gcm_tag_len.txt b/src/yaca/test-vectors/encrypt_aes_gcm_tag_len.txt
new file mode 100644 (file)
index 0000000..c67b9f9
--- /dev/null
@@ -0,0 +1,118 @@
+################## Check tag_len for algo and bcm ##################
+
+################## AES ##################
+
+################## GCM ##################
+
+algo=AES
+bcm=GCM
+key_len=128
+iv_len=64
+tag_len=3
+valid=0
+
+algo=AES
+bcm=GCM
+key_len=128
+iv_len=64
+tag_len=4
+valid=1
+
+algo=AES
+bcm=GCM
+key_len=128
+iv_len=64
+tag_len=5
+valid=0
+
+algo=AES
+bcm=GCM
+key_len=128
+iv_len=64
+tag_len=6
+valid=0
+
+algo=AES
+bcm=GCM
+key_len=128
+iv_len=64
+tag_len=7
+valid=0
+
+algo=AES
+bcm=GCM
+key_len=128
+iv_len=64
+tag_len=8
+valid=1
+
+algo=AES
+bcm=GCM
+key_len=128
+iv_len=64
+tag_len=9
+valid=0
+
+algo=AES
+bcm=GCM
+key_len=128
+iv_len=64
+tag_len=10
+valid=0
+
+algo=AES
+bcm=GCM
+key_len=128
+iv_len=64
+tag_len=11
+valid=0
+
+algo=AES
+bcm=GCM
+key_len=128
+iv_len=64
+tag_len=12
+valid=1
+
+algo=AES
+bcm=GCM
+key_len=128
+iv_len=64
+tag_len=13
+valid=1
+
+algo=AES
+bcm=GCM
+key_len=128
+iv_len=64
+tag_len=14
+valid=1
+
+algo=AES
+bcm=GCM
+key_len=128
+iv_len=64
+tag_len=15
+valid=1
+
+algo=AES
+bcm=GCM
+key_len=128
+iv_len=64
+tag_len=16
+valid=1
+
+algo=AES
+bcm=GCM
+key_len=128
+iv_len=64
+tag_len=17
+valid=0
+
+algo=AES
+bcm=GCM
+key_len=128
+iv_len=64
+tag_len=18
+valid=0
+
index 59d8d49..1100f16 100644 (file)
@@ -11,6 +11,8 @@ key_length_list = [8, 40, 64, 80, 128,
 
 iv_length_list = [0, 56, 64, 96, 104, 128, 256]
 
+tag_len_list = range(3, 18 + 1, 1)
+
 openssl_cmd_params = [
                     "-aes-128-ecb",
                     "-aes-192-ecb",
@@ -92,8 +94,12 @@ class Algorithm:
         self.bcm_list = bcm_list
 
 class Bcm:
-    def __init__(self, iv_len_list):
+    def __init__(self, iv_len_list, tag_len_list = None):
         self.iv_len_list = iv_len_list
+        if tag_len_list != None:
+            self.tag_len_list = tag_len_list
+        else:
+            tag_len_list = []
 
     def set_valid_iv_len(self, iv_len_list):
         self.iv_len_list = iv_len_list
@@ -107,12 +113,11 @@ aes.set_bcm(OrderedDict([
                         ('CFB1', Bcm([128])),
                         ('CFB8', Bcm([128])),
                         ('ECB',  Bcm([0])),
-                        ('GCM',  Bcm(range(24, 256 + 8, 8))),
-                        ('CCM',  Bcm(range(56, 104 + 8, 8))),
+                        ('GCM',  Bcm(range(24, 256 + 8, 8), [4, 8, 12, 13, 14, 15, 16])),
+                        ('CCM',  Bcm(range(56, 104 + 8, 8), range(4, 16 + 2, 2))),
                         ('CTR',  Bcm([128])),
                         ('WRAP', Bcm([64])),
                         ]))
-
 des = Algorithm([64])
 des.set_bcm(OrderedDict([
                         ('CBC',  Bcm([64])),
index 833f802..228bd93 100755 (executable)
@@ -6,11 +6,13 @@ def add_description(out_file, string):
     out_file.write(containers.separator + ' ' + string + ' ' + containers.separator +\
                    os.linesep + os.linesep)
 
-def write_set(algo_name, bcm, key_len, iv_len, out_file, valid=None):
+def write_set(algo_name, bcm, key_len, iv_len, out_file, valid = None, tag_len = None):
     out_file.write('algo=' + algo_name + os.linesep)
     out_file.write('bcm=' + bcm + os.linesep)
     out_file.write('key_len=' + str(key_len) + os.linesep)
     out_file.write('iv_len=' + str(iv_len) + os.linesep)
+    if tag_len != None:
+        out_file.write('tag_len=' + str(tag_len) + os.linesep)
     if valid != None:
         out_file.write('valid=' + valid + os.linesep)
     out_file.write(os.linesep)
@@ -73,6 +75,24 @@ def generate_combs(out_file):
     check_key_len_for_algo(out_file)
     check_iv_len_for_algo_bcm(out_file)
 
+def generate_tag_len_comb_for_algo_bcm(out_file, algo_name, bcm_name):
+    add_description(out_file, 'Check tag_len for algo and bcm')
+    algo = containers.valid_database[algo_name]
+    bcm = algo.bcm_list[bcm_name]
+    key_len = algo.key_len_list[0]
+
+    add_description(out_file, algo_name)
+    add_description(out_file, bcm_name)
+    iv_len = containers.default_iv_len(algo, bcm)
+    for tag_len in containers.tag_len_list:
+        valid = str(int(tag_len in bcm.tag_len_list))
+        write_set(algo_name, bcm_name, key_len, iv_len, out_file, valid, tag_len)
+
+def generate_combs(out_file):
+    check_bcm_for_algo(out_file)
+    check_key_len_for_algo(out_file)
+    check_iv_len_for_algo_bcm(out_file)
+
 def main():
     file_name = 'encrypt_param_comb.txt'
     out_file = open(file_name, 'w')
@@ -85,5 +105,15 @@ def main():
     generate_valid_combs_only(out_file, exclude_gcm_ccm_wrap)
     out_file.close()
 
+    file_name = 'encrypt_aes_gcm_tag_len.txt'
+    out_file = open(file_name, 'w')
+    generate_tag_len_comb_for_algo_bcm(out_file, 'AES', 'GCM')
+    out_file.close()
+
+    file_name = 'encrypt_aes_ccm_tag_len.txt'
+    out_file = open(file_name, 'w')
+    generate_tag_len_comb_for_algo_bcm(out_file, 'AES', 'CCM')
+    out_file.close()
+
 if __name__ == "__main__":
     main()
index 48a8b84..8f18940 100644 (file)
@@ -102,6 +102,21 @@ public:
         YACA_SUCCESS(yaca_context_set_property(m_decCtxPtr.get(), YACA_PROPERTY_RC2_EFFECTIVE_KEY_BITS,
                                                (char*)(&key_bits), sizeof(key_bits)));
     }
+
+    int set_tag_len(yaca_block_cipher_mode_e bcm, size_t tag_len)
+    {
+        switch (bcm) {
+        case YACA_BCM_GCM:
+            return yaca_context_set_property(m_enCtxPtr.get(), YACA_PROPERTY_GCM_TAG_LEN,
+                                             (void*)&tag_len, sizeof(tag_len));
+        case YACA_BCM_CCM:
+            return yaca_context_set_property(m_enCtxPtr.get(), YACA_PROPERTY_CCM_TAG_LEN,
+                                             (void*)&tag_len, sizeof(tag_len));
+        default:
+            return YACA_ERROR_INVALID_PARAMETER;
+        }
+    }
+
 private:
     CtxPtr m_enCtxPtr;
     CtxPtr m_decCtxPtr;
@@ -303,6 +318,41 @@ void test_encryption_decryption(std::string filename)
     }
 }
 
+void test_vector_gcm_tag_len(size_t key_len,
+                             size_t iv_len,
+                             size_t tag_len,
+                             bool valid)
+{
+    auto key_ptr = generate_key(YACA_KEY_TYPE_SYMMETRIC, key_len);
+    auto iv_ptr  = generate_key(YACA_KEY_TYPE_IV, iv_len);
+
+    int expected = YACA_ERROR_NONE;
+    if (!valid)
+        expected = YACA_ERROR_INVALID_PARAMETER;
+
+    Encryptor encryptor(YACA_ENCRYPT_AES, YACA_BCM_GCM, key_ptr, iv_ptr);
+    encryptor.encrypt(DATA);
+
+    YACA_RESULT(expected, encryptor.set_tag_len(YACA_BCM_GCM, tag_len));
+}
+
+void test_vector_ccm_tag_len(size_t key_len,
+                             size_t iv_len,
+                             size_t tag_len,
+                             bool valid)
+{
+    auto key_ptr = generate_key(YACA_KEY_TYPE_SYMMETRIC, key_len);
+    auto iv_ptr  = generate_key(YACA_KEY_TYPE_IV, iv_len);
+
+    int expected = YACA_ERROR_NONE;
+    if (!valid)
+        expected = YACA_ERROR_INVALID_PARAMETER;
+
+    Encryptor encryptor(YACA_ENCRYPT_AES, YACA_BCM_CCM, key_ptr, iv_ptr);
+
+    YACA_RESULT(expected, encryptor.set_tag_len(YACA_BCM_CCM, tag_len));
+}
+
 }//namespace anonymous
 
 RUNNER_TEST_GROUP_INIT(T3000_YACA_ENCRYPT);
@@ -785,3 +835,41 @@ RUNNER_TEST(T3120_yaca_aes_ccm_call_order_invalid_param, YacaTest)
     YACA_ASSERT_MSG(DATA.size() == decrypt_output.size(), "Size after encrypt-decrypt differs\n");
     YACA_ASSERT_MSG(DATA == decrypt_output, "Text after encrypt-decrypt has changed\n");
 }
+
+RUNNER_TEST(T3130_yaca_aes_gcm_tag_len_combs_invalid_param, YacaTest)
+{
+    auto tvv = loadTestVector("encrypt_aes_gcm_tag_len.txt");
+
+    for (const auto& tv : tvv) {
+        size_t key_len;
+        size_t iv_len;
+        size_t tag_len;
+        bool valid;
+
+        tv.get("key_len", key_len);
+        tv.get("iv_len", iv_len);
+        tv.get("tag_len", tag_len);
+        tv.get("valid", valid);
+
+        test_vector_gcm_tag_len(key_len, iv_len, tag_len, valid);
+    }
+}
+
+RUNNER_TEST(T3140_yaca_aes_ccm_tag_len_combs_invalid_param, YacaTest)
+{
+    auto tvv = loadTestVector("encrypt_aes_ccm_tag_len.txt");
+
+    for (const auto& tv : tvv) {
+        size_t key_len;
+        size_t iv_len;
+        size_t tag_len;
+        bool valid;
+
+        tv.get("key_len", key_len);
+        tv.get("iv_len", iv_len);
+        tv.get("tag_len", tag_len);
+        tv.get("valid", valid);
+
+        test_vector_ccm_tag_len(key_len, iv_len, tag_len, valid);
+    }
+}