YACA: Add simple encrypt/decrypt tests. 23/84423/6
authorDariusz Michaluk <d.michaluk@samsung.com>
Thu, 18 Aug 2016 11:48:13 +0000 (13:48 +0200)
committerKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Mon, 29 Aug 2016 14:50:06 +0000 (07:50 -0700)
Change-Id: I361190fd81762cc1ad2ba55f2ee1a5ff79842087

src/yaca/yaca-test-common.cpp
src/yaca/yaca-test-common.h
src/yaca/yaca-test-simple.cpp

index a028fcf..4a71b3b 100644 (file)
@@ -208,6 +208,38 @@ ChrPtr simple_sign(yaca_digest_algorithm_e algo,
     return wrap_ptr(signature);
 }
 
+ChrPtr simple_encrypt(yaca_encrypt_algorithm_e algo,
+                      yaca_block_cipher_mode_e bcm,
+                      const KeyPtr &key,
+                      const KeyPtr &iv,
+                      const char *plaintext,
+                      size_t plaintext_len,
+                      size_t *ciphertext_len)
+{
+    char *ciphertext;
+
+    YACA_SUCCESS(yaca_simple_encrypt(algo, bcm, key.get(), iv.get(), plaintext, plaintext_len,
+                                     &ciphertext, ciphertext_len));
+
+    return wrap_ptr(ciphertext);
+}
+
+ChrPtr simple_decrypt(yaca_encrypt_algorithm_e algo,
+                      yaca_block_cipher_mode_e bcm,
+                      const KeyPtr &key,
+                      const KeyPtr &iv,
+                      const char *ciphertext,
+                      size_t ciphertext_len,
+                      size_t *plaintext_len)
+{
+    char *plaintext;
+
+    YACA_SUCCESS(yaca_simple_decrypt(algo, bcm, key.get(), iv.get(), ciphertext, ciphertext_len,
+                                     &plaintext, plaintext_len));
+
+    return wrap_ptr(plaintext);
+}
+
 CtxPtr encrypt_init(yaca_encrypt_algorithm_e algo,
                     yaca_block_cipher_mode_e bcm,
                     const KeyPtr &sym_key,
index 3fe2629..6e100ad 100644 (file)
@@ -168,6 +168,23 @@ ChrPtr simple_sign(yaca_digest_algorithm_e algo,
                    size_t data_len,
                    size_t *signature_len);
 
+ChrPtr simple_encrypt(yaca_encrypt_algorithm_e algo,
+                      yaca_block_cipher_mode_e bcm,
+                      const KeyPtr &key,
+                      const KeyPtr &iv,
+                      const char *plaintext,
+                      size_t plaintext_len,
+                      size_t *ciphertext_len);
+
+
+ChrPtr simple_decrypt(yaca_encrypt_algorithm_e algo,
+                      yaca_block_cipher_mode_e bcm,
+                      const KeyPtr &key,
+                      const KeyPtr &iv,
+                      const char *ciphertext,
+                      size_t ciphertext_len,
+                      size_t *plaintext_len);
+
 CtxPtr seal_init(const KeyPtr &key_pub,
                  yaca_encrypt_algorithm_e algo,
                  yaca_block_cipher_mode_e bcm,
index e7d5883..a15c034 100644 (file)
@@ -65,6 +65,106 @@ void digest_check_test_vector(yaca_digest_algorithm_e algo,
                     " is different than expected");
 }
 
+void test_vector_simple_encrypt_decrypt(const Buffer &input,
+                                        yaca_encrypt_algorithm_e algo,
+                                        yaca_block_cipher_mode_e bcm,
+                                        size_t key_len,
+                                        size_t iv_len)
+{
+    KeyPtr key = generate_key(YACA_KEY_TYPE_SYMMETRIC, key_len);
+    KeyPtr iv = (iv_len > 0) ? generate_key(YACA_KEY_TYPE_IV, iv_len)
+                             : null_key();
+
+    size_t ciphertext_len = 0;
+    size_t plaintext_len = 0;
+    ChrPtr ciphertext = simple_encrypt(algo, bcm, key, iv, input.data(), input.size(), &ciphertext_len);
+    ChrPtr plaintext = simple_decrypt(algo, bcm, key, iv, ciphertext.get(), ciphertext_len, &plaintext_len);
+
+    YACA_ASSERT_MSG(input.size() == plaintext_len, "Size after encrypt-decrypt differs\n");
+    YACA_ASSERT_MSG(yaca_memcmp(plaintext.get(), input.data(), plaintext_len) == YACA_ERROR_NONE,
+                    "Text after encrypt-decrypt has changed\n");
+}
+
+void test_vector_simple_encrypt_decrypt_output(const Buffer &input,
+                                               yaca_encrypt_algorithm_e algo,
+                                               yaca_block_cipher_mode_e bcm,
+                                               const Buffer &key,
+                                               const Buffer &iv,
+                                               size_t repeats,
+                                               const Buffer &expected)
+{
+    Buffer input_repeated;
+    for (size_t i = 0; i < repeats; ++i)
+        std::copy(input.begin(), input.end(), std::back_inserter(input_repeated));
+
+    KeyPtr key_ptr = import_key(YACA_KEY_TYPE_SYMMETRIC, nullptr, key.data(), key.size());
+    KeyPtr iv_ptr = (iv.size() > 0) ? import_key(YACA_KEY_TYPE_IV, nullptr, iv.data(), iv.size())
+                                    : null_key();
+
+    size_t ciphertext_len = 0;
+    ChrPtr ciphertext = simple_encrypt(algo, bcm, key_ptr, iv_ptr,
+                                       input_repeated.data(), input_repeated.size(), &ciphertext_len);
+
+    YACA_ASSERT_MSG(expected.size() == ciphertext_len, "Size after encrypt differs\n");
+    YACA_ASSERT_MSG(yaca_memcmp(ciphertext.get(), expected.data(), ciphertext_len) == YACA_ERROR_NONE,
+                    "Text after encrypt differs\n");
+}
+
+void test_simple_encryption_output(std::string filename)
+{
+    auto tvv = loadTestVector(filename);
+
+    for (const auto& tv : tvv) {
+        Buffer input;
+        yaca_encrypt_algorithm_e algo;
+        yaca_block_cipher_mode_e bcm;
+        Buffer key;
+        Buffer iv;
+        size_t repeats;
+        Buffer expected;
+
+        tv.get("input", input);
+        tv.get("algo", algo);
+        tv.get("bcm", bcm);
+        tv.get("key", key);
+        tv.get("iv", iv);
+        tv.get("repeats", repeats);
+        tv.get("output", expected);
+
+        test_vector_simple_encrypt_decrypt_output(input, algo, bcm, key, iv, repeats, expected);
+    }
+}
+
+void test_simple_encryption_decryption(std::string filename)
+{
+    Buffer input;
+    std::string s = "abcdefghijklmnoprstuvwxyz0123456789";
+
+    auto tvv = loadTestVector(filename);
+
+    for (const auto& tv : tvv) {
+        yaca_encrypt_algorithm_e algo;
+        yaca_block_cipher_mode_e bcm;
+        size_t key_len;
+        size_t iv_len;
+        size_t key_data_len = 0;
+
+        tv.get("algo", algo);
+        tv.get("bcm", bcm);
+        tv.get("key_len", key_len);
+        tv.get("iv_len", iv_len);
+        if (bcm == YACA_BCM_WRAP)
+            tv.get("key_data_len", key_data_len);
+
+        if (key_data_len > 0)
+            input = random_buffer(key_data_len / 8);
+        else
+            std::copy(s.begin(), s.end(), std::back_inserter(input));
+
+        test_vector_simple_encrypt_decrypt(input, algo, bcm, key_len, iv_len);
+    }
+}
+
 } // anonymous namespace
 
 
@@ -741,3 +841,16 @@ RUNNER_TEST(T7130_yaca_simple_decrypt_invalid_param, YacaTest)
     YACA_INVALID_PARAM(yaca_simple_decrypt(algo, bcm, sym_key.get(), iv.get(),
                                            ciphertext.data(), ciphertext.size(), &plaintext, nullptr));
 }
+
+RUNNER_TEST(T7150_yaca_simple_encrypt_decrypt_output, YacaTest)
+{
+     test_simple_encryption_output("encrypt_output_comparison.txt");
+     test_simple_encryption_output("encrypt_output_comparison_rc4.txt");
+     test_simple_encryption_output("encrypt_output_comparison_wrap.txt");
+}
+
+RUNNER_TEST(T7160_yaca_simple_encrypt_decrypt_comparison, YacaTest)
+{
+    test_simple_encryption_decryption("encrypt_valid_param.txt");
+    test_simple_encryption_decryption("encrypt_valid_param_wrap.txt");
+}