Encrypted initial values test
[platform/core/test/security-tests.git] / src / ckm / privileged / initial-values.cpp
index 34638fc..168d5ca 100644 (file)
@@ -23,6 +23,7 @@
 #include <ckm-common.h>
 #include <ckm-privileged-common.h>
 #include <ckm/ckm-control.h>
+#include <ckm/ckm-manager.h>
 #include <ckmc/ckmc-manager.h>
 #include <access_provider2.h>
 #include <fstream>
@@ -107,6 +108,25 @@ void test_exists(const std::string& name, bool expected) {
 
 }
 
+int hexToBin(char h) {
+    if (h >= '0' && h <= '9')
+        return h - '0';
+    if (h >= 'a' && h <= 'f')
+        return h - 'a' + 10;
+    if (h >= 'A' && h <= 'F')
+        return h - 'A' + 10;
+    RUNNER_ASSERT_MSG(false, "Input out of scope");
+}
+
+CKM::RawBuffer hexToBin(std::string &hex) {
+    CKM::RawBuffer output;
+    output.resize(hex.size()/2);
+    for (size_t i=0; i<output.size(); ++i) {
+        output[i] = hexToBin(hex[i*2])*16 +
+                    hexToBin(hex[i*2 + 1]);
+    }
+    return output;
+}
 
 RUNNER_TEST_GROUP_INIT(T60_INITIAL_VALUES);
 
@@ -342,3 +362,25 @@ RUNNER_TEST(T6999_deinit)
 {
     remove_user_data(0);
 }
+
+RUNNER_TEST(T7000_Encrypted_initial_values)
+{
+    int temp;
+    std::string message  = "16c9efbc342777c0e36d59019582d59be8385bdea5497cf092f99ce5430498e9";
+    std::string iv       = "6162636465666768696a6b6c6d6e6f70";
+
+    std::string expected = "ShortTestMessage";
+
+    CKM::CryptoAlgorithm algo;
+    CKM::RawBuffer messageBin = hexToBin(message);
+    CKM::RawBuffer ivBin = hexToBin(iv);
+    CKM::RawBuffer decrypted;
+
+    algo.setParam(CKM::ParamName::ALGO_TYPE, CKM::AlgoType::AES_CBC);
+    algo.setParam(CKM::ParamName::ED_IV, ivBin);
+
+    auto mgr = CKM::Manager::create();
+    RUNNER_ASSERT_MSG(CKM_API_SUCCESS == (temp = mgr->decrypt(algo, "/System TEI_0", CKM::Password(), messageBin, decrypted)), "Failed to decrypt " << CKMErrorToString(temp));
+    RUNNER_ASSERT_MSG(std::string(decrypted.begin(), decrypted.end()) == expected, "Data does not match");
+}
+