YACA: simple sign/verify/hmac/cmac vector tests 85/77385/6
authorLukasz Pawelczyk <l.pawelczyk@samsung.com>
Wed, 29 Jun 2016 12:45:26 +0000 (14:45 +0200)
committerLukasz Pawelczyk <l.pawelczyk@samsung.com>
Thu, 30 Jun 2016 09:18:05 +0000 (11:18 +0200)
Change-Id: Ia8a3ca01f86dddf8ac32e62327019f839acfb171

src/yaca/yaca-test-simple.cpp

index 138208b..190a55c 100644 (file)
 
 namespace {
 
-void check_test_vector(yaca_digest_algorithm_e algo,
-                       size_t repeats,
-                       const Buffer &single_input,
-                       const Buffer &expected)
+void digest_check_test_vector(yaca_digest_algorithm_e algo,
+                              size_t repeats,
+                              const Buffer &single_input,
+                              const Buffer &expected)
 {
     /* concatenate input if needed */
     Buffer input(single_input);
@@ -57,7 +57,7 @@ void check_test_vector(yaca_digest_algorithm_e algo,
                     digest_len * 8 <<" bits long. Expected " <<
                     expected.size() * 8 << " bits");
 
-    int ret = yaca_memcmp(digest_ptr.get(), expected.data(), digest_len);
+    int ret = yaca_memcmp(digest, expected.data(), digest_len);
     YACA_ASSERT_MSG(YACA_ERROR_NONE == ret,
                     "Digest calculated for \"" <<
                     truncate_str(std::string(input.data(), input.size()), 16)
@@ -118,7 +118,7 @@ RUNNER_TEST(T7020_yaca_simple_calculate_digest_ascii_vectors, YacaTest)
         tv.get("input", input);
         tv.get("output", expected);
 
-        check_test_vector(algo, repeats, Buffer(input.begin(), input.end()), expected);
+        digest_check_test_vector(algo, repeats, Buffer(input.begin(), input.end()), expected);
     }
 }
 
@@ -137,7 +137,7 @@ RUNNER_TEST(T7030_yaca_simple_calculate_digest_binary_vectors, YacaTest)
         tv.get("input", input);
         tv.get("output", expected);
 
-        check_test_vector(algo, repeats, input, expected);
+        digest_check_test_vector(algo, repeats, input, expected);
     }
 }
 
@@ -376,3 +376,224 @@ RUNNER_TEST(T7070_yaca_simple_calculate_cmac_invalid_param, YacaTest)
                                                   &mac,
                                                   nullptr));
 }
+
+RUNNER_TEST(T7080_yaca_simple_calculate_signature_vectors, YacaTest)
+{
+    auto tvv = loadTestVector("sign_ascii.txt");
+
+    for (const auto &tv : tvv) {
+        yaca_digest_algorithm_e algo;
+        yaca_key_type_e key_type;
+        Buffer key_data;
+        size_t repeats;
+        std::string single_input;
+        Buffer expected;
+
+        tv.get("algo", algo);
+        tv.get("key_type", key_type);
+        tv.get("key", key_data);
+        tv.get("repeats", repeats);
+        tv.get("input", single_input);
+        tv.get("output", expected);
+
+        KeyPtr key = import_key(key_type, nullptr, key_data.data(), key_data.size());
+
+        /* concatenate input if needed */
+        Buffer input(single_input.begin(), single_input.end());
+        for (size_t i = 1; i < repeats; i++)
+            input.insert(input.end(), single_input.begin(), single_input.end());
+
+        char *signature;
+        size_t signature_len;
+        YACA_SUCCESS(yaca_simple_calculate_signature(algo,
+                                                     key.get(),
+                                                     input.data(),
+                                                     input.size(),
+                                                     &signature,
+                                                     &signature_len));
+        RUNNER_ASSERT_MSG(signature != NULL, "NULL signature calculated");
+
+        ChrPtr signature_ptr = wrap_ptr(signature);
+
+        YACA_ASSERT_MSG(signature_len == expected.size(),
+                        "Signature calculated for \""  <<
+                        truncate_str(std::string(input.data(), input.size()), 16)
+                        << "\" with " << digest2str(algo) << " and " << keytype2str(key_type)
+                        << " key is " << signature_len * 8
+                        <<" bits long. Expected " << expected.size() * 8 << " bits");
+
+        int ret = yaca_memcmp(signature, expected.data(), signature_len);
+        YACA_ASSERT_MSG(YACA_ERROR_NONE == ret,
+                        "Signature calculated for \"" <<
+                        truncate_str(std::string(input.data(), input.size()), 16) << "\" with "
+                        << digest2str(algo) << " and " << keytype2str(key_type)
+                        << "key is different than expected");
+    }
+}
+
+RUNNER_TEST(T7090_yaca_simple_calculate_signature_verify_vectors, YacaTest)
+{
+    auto tvv = loadTestVector("sign_verify_ascii.txt");
+
+    for (const auto &tv : tvv) {
+        yaca_digest_algorithm_e algo;
+        yaca_key_type_e key_type;
+        Buffer key_data;
+        size_t repeats;
+        std::string single_input;
+
+        tv.get("algo", algo);
+        tv.get("key_type", key_type);
+        tv.get("key", key_data);
+        tv.get("repeats", repeats);
+        tv.get("input", single_input);
+
+        KeyPtr key_prv = import_key(key_type, nullptr, key_data.data(), key_data.size());
+        KeyPtr key_pub = extract_public_key(key_prv);
+
+        /* concatenate input if needed */
+        Buffer input(single_input.begin(), single_input.end());
+        for (size_t i = 1; i < repeats; i++)
+            input.insert(input.end(), single_input.begin(), single_input.end());
+
+        char *signature;
+        size_t signature_len;
+        YACA_SUCCESS(yaca_simple_calculate_signature(algo,
+                                                     key_prv.get(),
+                                                     input.data(),
+                                                     input.size(),
+                                                     &signature,
+                                                     &signature_len));
+        RUNNER_ASSERT_MSG(signature != NULL, "NULL signature calculated");
+
+        ChrPtr signature_ptr = wrap_ptr(signature);
+
+        int ret = yaca_simple_verify_signature(algo,
+                                               key_pub.get(),
+                                               input.data(),
+                                               input.size(),
+                                               signature,
+                                               signature_len);
+
+        YACA_ASSERT_MSG(YACA_ERROR_NONE == ret,
+                        "Verification failed for \"" <<
+                        truncate_str(std::string(input.data(), input.size()), 16) << "\" with "
+                        << digest2str(algo) << " and " << keytype2str(key_type) << " key");
+
+        Buffer input_wrong = input;
+        input_wrong.push_back('!');
+
+        ret = yaca_simple_verify_signature(algo,
+                                           key_pub.get(),
+                                           input_wrong.data(),
+                                           input_wrong.size(),
+                                           signature,
+                                           signature_len);
+
+        YACA_ASSERT_MSG(YACA_ERROR_DATA_MISMATCH == ret,
+                        "Verification succeeded for \"" <<
+                        truncate_str(std::string(input.data(), input.size()), 16) << "\" with "
+                        << digest2str(algo) << " and " << keytype2str(key_type)
+                        << " key while the input has been tampered with");
+    }
+}
+
+RUNNER_TEST(T7100_yaca_simple_calculate_hmac_vectors, YacaTest)
+{
+    auto tvv = loadTestVector("sign_hmac_ascii.txt");
+
+    for (const auto &tv : tvv) {
+        yaca_digest_algorithm_e algo;
+        Buffer key_data;
+        size_t repeats;
+        std::string single_input;
+        Buffer expected;
+
+        tv.get("algo", algo);
+        tv.get("key", key_data);
+        tv.get("repeats", repeats);
+        tv.get("input", single_input);
+        tv.get("output", expected);
+
+        KeyPtr key = import_key(YACA_KEY_TYPE_SYMMETRIC, nullptr, key_data.data(), key_data.size());
+
+        /* concatenate input if needed */
+        Buffer input(single_input.begin(), single_input.end());
+        for (size_t i = 1; i < repeats; i++)
+            input.insert(input.end(), single_input.begin(), single_input.end());
+
+        char *mac;
+        size_t mac_len;
+        YACA_SUCCESS(yaca_simple_calculate_hmac(algo,
+                                                key.get(),
+                                                input.data(),
+                                                input.size(),
+                                                &mac,
+                                                &mac_len));
+        RUNNER_ASSERT_MSG(mac != NULL, "NULL signature calculated");
+
+        ChrPtr mac_ptr = wrap_ptr(mac);
+
+        YACA_ASSERT_MSG(mac_len == expected.size(),
+                        "HMAC calculated for \""  <<
+                        truncate_str(std::string(input.data(), input.size()), 16)
+                        << "\" with " << digest2str(algo) << " is " << mac_len * 8
+                        <<" bits long. Expected " << expected.size() * 8 << " bits");
+
+        int ret = yaca_memcmp(mac, expected.data(), mac_len);
+        YACA_ASSERT_MSG(YACA_ERROR_NONE == ret,
+                        "HMAC calculated for \"" <<
+                        truncate_str(std::string(input.data(), input.size()), 16) << "\" with "
+                        << digest2str(algo) << " is different than expected");
+    }
+}
+
+RUNNER_TEST(T7110_yaca_simple_calculate_cmac_vectors, YacaTest)
+{
+    auto tvv = loadTestVector("sign_cmac_ascii.txt");
+
+    for (const auto &tv : tvv) {
+        yaca_encrypt_algorithm_e algo;
+        Buffer key_data;
+        size_t repeats;
+        std::string single_input;
+        Buffer expected;
+
+        tv.get("algo", algo);
+        tv.get("key", key_data);
+        tv.get("repeats", repeats);
+        tv.get("input", single_input);
+        tv.get("output", expected);
+
+        KeyPtr key = import_key(YACA_KEY_TYPE_SYMMETRIC, nullptr, key_data.data(), key_data.size());
+
+        /* concatenate input if needed */
+        Buffer input(single_input.begin(), single_input.end());
+        for (size_t i = 1; i < repeats; i++)
+            input.insert(input.end(), single_input.begin(), single_input.end());
+
+        char *mac;
+        size_t mac_len;
+        YACA_SUCCESS(yaca_simple_calculate_cmac(algo,
+                                                key.get(),
+                                                input.data(),
+                                                input.size(),
+                                                &mac,
+                                                &mac_len));
+        RUNNER_ASSERT_MSG(mac != NULL, "NULL signature calculated");
+
+        ChrPtr mac_ptr = wrap_ptr(mac);
+
+        YACA_ASSERT_MSG(mac_len == expected.size(),
+                        "CMAC calculated for \""  <<
+                        truncate_str(std::string(input.data(), input.size()), 16)
+                        << "\" with " << encrypt2str(algo) << " is " << mac_len * 8
+                        <<" bits long. Expected " << expected.size() * 8 << " bits");
+
+        int ret = yaca_memcmp(mac, expected.data(), mac_len);
+        YACA_ASSERT_MSG(YACA_ERROR_NONE == ret,
+                        "CMAC calculated for \"" <<
+                        truncate_str(std::string(input.data(), input.size()), 16) << "\" with "
+                        << encrypt2str(algo) << " is different than expected");
+    }
+}