--- /dev/null
+//
+// Copyright (c) 2024 Samsung Electronics Co., Ltd.
+//
+// Licensed under the Apache License, Version 2.0 (the License);
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+#include "assert.h"
+
+#include <ckmc/ckmc-manager.h>
+#include <ckmc/ckmc-extended.h>
+#include <ckmc/ckmc-control.h>
+#include <ckmc/ckmc-type.h>
+#include <ckmc/ckmc-error.h>
+
+#include <string.h>
+#include <stdio.h>
+#include <stddef.h>
+#include <stdlib.h>
+
+#define PASS "test-pass-1"
+
+const char* prvAlias = "RSA-prv-test";
+const char* pubAlias = "RSA-pub-test";
+const char* aliasAES = "AES-test";
+const char* AES_KEY_IMP = "AES_KEY_IMP";
+
+const ckmc_policy_s policyExp = {NULL, 1};
+const ckmc_policy_s policyExpPass = {(char*)PASS, 1};
+const ckmc_policy_s policyUnexp = {NULL, 0};
+
+ckmc_param_list_h params = NULL;
+ckmc_raw_buffer_s *data = NULL;
+ckmc_raw_buffer_s *dataImp = NULL;
+ckmc_key_s *wrappedKey = NULL;
+
+/**
+ * @function utc_key_manager_extended_startup
+ * @description Called before each test
+ * @parameter NA
+ * @return NA
+ */
+void utc_key_manager_extended_startup(void)
+{
+ ckmc_generate_new_params(CKMC_ALGO_RSA_OAEP, ¶ms);
+ ckmc_param_list_set_integer(params, CKMC_PARAM_ED_OAEP_HASH, CKMC_HASH_SHA384);
+ ckmc_buffer_new((unsigned char*)"somedata", 9, &data);
+}
+
+/**
+ * @function utc_key_manager_extended_cleanup
+ * @description Called after each test
+ * @parameter NA
+ * @return NA
+ */
+void utc_key_manager_extended_cleanup(void)
+{
+ ckmc_remove_key(prvAlias);
+ ckmc_remove_key(pubAlias);
+ ckmc_remove_key(aliasAES);
+ ckmc_param_list_free(params);
+ ckmc_buffer_free(data);
+ ckmc_remove_alias(AES_KEY_IMP);
+}
+
+/**
+ * @testcase utc_ckmc_wrap_unwrap_concatenated_data_p1
+ * @since_tizen 7.0
+ * @description Wrap and unwrap exportable concatenated data.
+ * @scenario Wrap an AES key, unwrap it and compare the result with the original key.
+ */
+int utc_ckmc_wrap_unwrap_concatenated_data_p1(void)
+{
+ ckmc_key_s *aesKey = NULL;
+ ckmc_key_s *aesKeyImp = NULL;
+
+ int temp = ckmc_create_key_pair_rsa(4096, prvAlias, pubAlias, policyExp, policyExp);
+ assert_eq(temp, CKMC_ERROR_NONE);
+ temp = ckmc_create_key_aes(256, aliasAES, policyExp);
+ assert_eq(temp, CKMC_ERROR_NONE);
+
+ temp = ckmc_wrap_concatenated_data(params,
+ pubAlias,
+ NULL,
+ aliasAES,
+ NULL,
+ data,
+ &wrappedKey);
+ assert_eq(temp, CKMC_ERROR_NONE);
+
+ temp = ckmc_unwrap_concatenated_data(params,
+ prvAlias,
+ NULL,
+ wrappedKey,
+ AES_KEY_IMP,
+ 256,
+ policyExp,
+ &dataImp);
+ assert_eq(temp, CKMC_ERROR_NONE);
+
+ assert_eq(data->size, dataImp->size);
+ assert_eq(memcmp(data->data, dataImp->data, data->size), 0);
+
+ temp = ckmc_get_key(aliasAES, NULL, &aesKey);
+ assert_eq(temp, CKMC_ERROR_NONE);
+ temp = ckmc_get_key(AES_KEY_IMP, NULL, &aesKeyImp);
+ assert_eq(temp, CKMC_ERROR_NONE);
+
+ assert_eq(aesKey->key_size, aesKeyImp->key_size);
+ assert_eq(0, memcmp(aesKey->raw_key, aesKeyImp->raw_key, aesKey->key_size));
+
+ ckmc_buffer_free(dataImp);
+ ckmc_key_free(wrappedKey);
+ ckmc_key_free(aesKey);
+ ckmc_key_free(aesKeyImp);
+
+ return 0;
+}
+
+/**
+ * @testcase utc_ckmc_wrap_unwrap_concatenated_data_p2
+ * @since_tizen 7.0
+ * @description Wrap and unwrap unexportable concatenated data.
+ * @scenario Wrap an AES key, unwrap it and indirectly compare the result with the original
+ * key by encrypting some data with one and decrypting with the other, then
+ * comparing the decrypted data with the original.
+ */
+int utc_ckmc_wrap_unwrap_concatenated_data_p2(void)
+{
+ ckmc_raw_buffer_s *encrypted = NULL;
+ ckmc_raw_buffer_s *decrypted = NULL;
+ ckmc_param_list_h paramsAES = NULL;
+ ckmc_raw_buffer_s *iv = NULL;
+ char ivData[16] = {0};
+
+ int temp = ckmc_create_key_pair_rsa(2048, prvAlias, pubAlias, policyUnexp, policyUnexp);
+ assert_eq(temp, CKMC_ERROR_NONE);
+ temp = ckmc_create_key_aes(128, aliasAES, policyUnexp);
+ assert_eq(temp, CKMC_ERROR_NONE);
+
+ temp = ckmc_wrap_concatenated_data(params,
+ pubAlias,
+ NULL,
+ aliasAES,
+ NULL,
+ data,
+ &wrappedKey);
+ assert_eq(temp, CKMC_ERROR_NONE);
+
+ temp = ckmc_unwrap_concatenated_data(params,
+ prvAlias,
+ NULL,
+ wrappedKey,
+ AES_KEY_IMP,
+ 128,
+ policyUnexp,
+ &dataImp);
+ assert_eq(temp, CKMC_ERROR_NONE);
+
+ assert_eq(data->size, dataImp->size);
+ assert_eq(memcmp(data->data, dataImp->data, data->size), 0);
+
+ temp = ckmc_generate_new_params(CKMC_ALGO_AES_CBC, ¶msAES);
+ assert_eq(temp, CKMC_ERROR_NONE);
+
+ ckmc_buffer_new((unsigned char*)ivData, 16, &iv);
+ assert_eq(temp, CKMC_ERROR_NONE);
+
+ temp = ckmc_param_list_set_buffer(paramsAES, CKMC_PARAM_ED_IV, iv);
+ assert_eq(temp, CKMC_ERROR_NONE);
+
+ temp = ckmc_encrypt_data(paramsAES,
+ aliasAES,
+ NULL,
+ *data,
+ &encrypted);
+ assert_eq(temp, CKMC_ERROR_NONE);
+ temp = ckmc_decrypt_data(paramsAES,
+ AES_KEY_IMP,
+ NULL,
+ *encrypted,
+ &decrypted);
+ assert_eq(temp, CKMC_ERROR_NONE);
+
+ assert_eq(data->size, decrypted->size);
+ assert_eq(memcmp(data->data, decrypted->data, data->size), 0);
+
+ ckmc_buffer_free(dataImp);
+ ckmc_buffer_free(encrypted);
+ ckmc_buffer_free(decrypted);
+ ckmc_buffer_free(iv);
+ ckmc_param_list_free(paramsAES);
+ ckmc_key_free(wrappedKey);
+
+ return 0;
+}
+
+/**
+ * @testcase utc_ckmc_wrap_concatenated_data_n1
+ * @since_tizen 7.0
+ * @description Wrap concatenated data with an invalid alias.
+ * @scenario Wrap an AES key while providing either an invalid private RSA key alias or an
+ * invalid AES key alias.
+ */
+int utc_ckmc_wrap_concatenated_data_n1(void)
+{
+ int temp = ckmc_create_key_pair_rsa(3072, prvAlias, pubAlias, policyExp, policyExp);
+ assert_eq(temp, CKMC_ERROR_NONE);
+ temp = ckmc_create_key_aes(128, aliasAES, policyExp);
+ assert_eq(temp, CKMC_ERROR_NONE);
+
+ temp = ckmc_wrap_concatenated_data(params,
+ pubAlias,
+ NULL,
+ "invalid_alias",
+ NULL,
+ data,
+ &wrappedKey);
+ assert_eq(temp, CKMC_ERROR_DB_ALIAS_UNKNOWN);
+
+ temp = ckmc_wrap_concatenated_data(params,
+ "invalid_alias",
+ NULL,
+ aliasAES,
+ NULL,
+ data,
+ &wrappedKey);
+ assert_eq(temp, CKMC_ERROR_DB_ALIAS_UNKNOWN);
+
+ return 0;
+}
+
+/**
+ * @testcase utc_ckmc_wrap_concatenated_data_n2
+ * @since_tizen 7.0
+ * @description Wrap concatenated data with invalid parameters.
+ * @scenario Wrap an AES key while providing NULL parameters.
+ */
+int utc_ckmc_wrap_concatenated_data_n2(void)
+{
+ int temp = ckmc_create_key_pair_rsa(3072, prvAlias, pubAlias, policyExp, policyExp);
+ assert_eq(temp, CKMC_ERROR_NONE);
+ temp = ckmc_create_key_aes(128, aliasAES, policyExp);
+ assert_eq(temp, CKMC_ERROR_NONE);
+
+ ckmc_raw_buffer_s *wrongSizeData = NULL;
+ char* buf = (char*)malloc(1024*sizeof(char));
+ temp = ckmc_buffer_new((unsigned char*)buf, 1024, &wrongSizeData);
+ assert_eq(temp, CKMC_ERROR_NONE);
+
+ temp = ckmc_wrap_concatenated_data(params,
+ pubAlias,
+ NULL,
+ aliasAES,
+ NULL,
+ data,
+ &wrappedKey);
+ assert_eq(temp, CKMC_ERROR_NONE);
+
+ temp = ckmc_wrap_concatenated_data(params,
+ NULL,
+ NULL,
+ aliasAES,
+ NULL,
+ data,
+ &wrappedKey);
+ assert_eq(temp, CKMC_ERROR_INVALID_PARAMETER);
+
+ temp = ckmc_wrap_concatenated_data(params,
+ pubAlias,
+ NULL,
+ NULL,
+ NULL,
+ data,
+ &wrappedKey);
+ assert_eq(temp, CKMC_ERROR_INVALID_PARAMETER);
+
+ temp = ckmc_wrap_concatenated_data(params,
+ pubAlias,
+ NULL,
+ aliasAES,
+ NULL,
+ data,
+ NULL);
+ assert_eq(temp, CKMC_ERROR_INVALID_PARAMETER);
+
+ temp = ckmc_wrap_concatenated_data(params,
+ pubAlias,
+ NULL,
+ aliasAES,
+ NULL,
+ wrongSizeData,
+ &wrappedKey);
+ assert_eq(temp, CKMC_ERROR_INVALID_PARAMETER);
+
+ temp = ckmc_wrap_concatenated_data(NULL,
+ pubAlias,
+ NULL,
+ aliasAES,
+ NULL,
+ data,
+ &wrappedKey);
+ assert_eq(temp, CKMC_ERROR_INVALID_PARAMETER);
+
+ ckmc_buffer_free(wrongSizeData);
+ free(buf);
+
+ return 0;
+}
+
+/**
+ * @testcase utc_ckmc_wrap_concatenated_data_n3
+ * @since_tizen 7.0
+ * @description Wrap concatenated data with failed authentication.
+ * @scenario Wrap an AES key while providing either an RSA passowrd or an AES password when
+ * neither ot them is requierd due to the policy.
+ */
+int utc_ckmc_wrap_concatenated_data_n3(void)
+{
+ int temp = ckmc_create_key_pair_rsa(2048, prvAlias, pubAlias, policyExp, policyExp);
+ assert_eq(temp, CKMC_ERROR_NONE);
+ temp = ckmc_create_key_aes(192, aliasAES, policyExp);
+ assert_eq(temp, CKMC_ERROR_NONE);
+
+ const char* passwordRSA = "unrequired RSA password";
+ temp = ckmc_wrap_concatenated_data(params,
+ pubAlias,
+ passwordRSA,
+ aliasAES,
+ NULL,
+ data,
+ &wrappedKey);
+ assert_eq(temp, CKMC_ERROR_AUTHENTICATION_FAILED);
+
+ const char* passwordAES = "unrequired AES password";
+ temp = ckmc_wrap_concatenated_data(params,
+ pubAlias,
+ NULL,
+ aliasAES,
+ passwordAES,
+ data,
+ &wrappedKey);
+ assert_eq(temp, CKMC_ERROR_AUTHENTICATION_FAILED);
+
+ return 0;
+}
+
+/**
+ * @testcase utc_ckmc_unwrap_concatenated_data_n1
+ * @since_tizen 7.0
+ * @description Unwrap concatenated data with an unknown alias.
+ * @scenario Wrap an AES key and then try to unwrap it while providing an invalid RSA alias.
+ */
+int utc_ckmc_unwrap_concatenated_data_n1(void)
+{
+ int temp = ckmc_create_key_pair_rsa(3072, prvAlias, pubAlias, policyExp, policyExp);
+ assert_eq(temp, CKMC_ERROR_NONE);
+ temp = ckmc_create_key_aes(192, aliasAES, policyExp);
+ assert_eq(temp, CKMC_ERROR_NONE);
+
+ temp = ckmc_wrap_concatenated_data(params,
+ pubAlias,
+ NULL,
+ aliasAES,
+ NULL,
+ data,
+ &wrappedKey);
+ assert_eq(temp, CKMC_ERROR_NONE);
+
+ temp = ckmc_unwrap_concatenated_data(params,
+ "invalid alias",
+ NULL,
+ wrappedKey,
+ AES_KEY_IMP,
+ 192,
+ policyExp,
+ &dataImp);
+ assert_eq(temp, CKMC_ERROR_DB_ALIAS_UNKNOWN);
+
+ ckmc_key_free(wrappedKey);
+
+ return 0;
+}
+
+/**
+ * @testcase utc_ckmc_unwrap_concatenated_data_n2
+ * @since_tizen 7.0
+ * @description Unwrap concatenated data while an AES alias already exists.
+ * @scenario Wrap an AES key and then try to unwrap it while an AES alias already exists.
+ */
+int utc_ckmc_unwrap_concatenated_data_n2(void)
+{
+ int temp = ckmc_create_key_pair_rsa(3072, prvAlias, pubAlias, policyExp, policyExp);
+ assert_eq(temp, CKMC_ERROR_NONE);
+ temp = ckmc_create_key_aes(192, aliasAES, policyExp);
+ assert_eq(temp, CKMC_ERROR_NONE);
+
+ temp = ckmc_wrap_concatenated_data(params,
+ pubAlias,
+ NULL,
+ aliasAES,
+ NULL,
+ data,
+ &wrappedKey);
+ assert_eq(temp, CKMC_ERROR_NONE);
+
+ temp = ckmc_create_key_aes(192, AES_KEY_IMP, policyExp);
+ assert_eq(temp, CKMC_ERROR_NONE);
+
+ temp = ckmc_unwrap_concatenated_data(params,
+ prvAlias,
+ NULL,
+ wrappedKey,
+ AES_KEY_IMP,
+ 192,
+ policyExp,
+ &dataImp);
+ assert_eq(temp, CKMC_ERROR_DB_ALIAS_EXISTS);
+
+ ckmc_key_free(wrappedKey);
+
+ return 0;
+}
+
+/**
+ * @testcase utc_ckmc_unwrap_concatenated_data_n3
+ * @since_tizen 7.0
+ * @description Unwrap concatenated data with invalid parameters.
+ * @scenario Wrap an AES key and try to unwrap it while providing NULL parameters.
+ */
+int utc_ckmc_unwrap_concatenated_data_n3(void)
+{
+ int temp = ckmc_create_key_pair_rsa(3072, prvAlias, pubAlias, policyExp, policyExp);
+ assert_eq(temp, CKMC_ERROR_NONE);
+ temp = ckmc_create_key_aes(192, aliasAES, policyExp);
+ assert_eq(temp, CKMC_ERROR_NONE);
+
+ temp = ckmc_wrap_concatenated_data(params,
+ pubAlias,
+ NULL,
+ aliasAES,
+ NULL,
+ data,
+ &wrappedKey);
+ assert_eq(temp, CKMC_ERROR_NONE);
+
+ temp = ckmc_unwrap_concatenated_data(params,
+ NULL,
+ NULL,
+ wrappedKey,
+ AES_KEY_IMP,
+ 192,
+ policyExp,
+ &dataImp);
+ assert_eq(temp, CKMC_ERROR_INVALID_PARAMETER);
+
+ temp = ckmc_unwrap_concatenated_data(params,
+ prvAlias,
+ NULL,
+ NULL,
+ AES_KEY_IMP,
+ 192,
+ policyExp,
+ &dataImp);
+ assert_eq(temp, CKMC_ERROR_INVALID_PARAMETER);
+
+ temp = ckmc_unwrap_concatenated_data(params,
+ prvAlias,
+ NULL,
+ wrappedKey,
+ NULL,
+ 192,
+ policyExp,
+ &dataImp);
+ assert_eq(temp, CKMC_ERROR_INVALID_PARAMETER);
+
+ temp = ckmc_unwrap_concatenated_data(params,
+ prvAlias,
+ NULL,
+ wrappedKey,
+ AES_KEY_IMP,
+ 1024,
+ policyExp,
+ &dataImp);
+ assert_eq(temp, CKMC_ERROR_INVALID_PARAMETER);
+
+ temp = ckmc_unwrap_concatenated_data(NULL,
+ prvAlias,
+ NULL,
+ wrappedKey,
+ AES_KEY_IMP,
+ 192,
+ policyExp,
+ &dataImp);
+ assert_eq(temp, CKMC_ERROR_INVALID_PARAMETER);
+
+ ckmc_key_free(wrappedKey);
+
+ return 0;
+}
+
+/**
+ * @testcase utc_ckmc_unwrap_concatenated_data_n4
+ * @since_tizen 7.0
+ * @description Unwrap concatenated data with failed authentication.
+ * @scenario Wrap an AES key and try to unwrap it while providing an incorrect RSA key
+ * password.
+ */
+int utc_ckmc_unwrap_concatenated_data_n4(void)
+{
+ int temp = ckmc_create_key_pair_rsa(3072, prvAlias, pubAlias,
+ policyExpPass, policyExpPass);
+ assert_eq(temp, CKMC_ERROR_NONE);
+ temp = ckmc_create_key_aes(192, aliasAES, policyExpPass);
+ assert_eq(temp, CKMC_ERROR_NONE);
+
+ temp = ckmc_wrap_concatenated_data(params,
+ pubAlias,
+ PASS,
+ aliasAES,
+ PASS,
+ data,
+ &wrappedKey);
+ assert_eq(temp, CKMC_ERROR_NONE);
+
+ const char* incorrectPasswordRSA = "incorrect RSA password";
+ temp = ckmc_unwrap_concatenated_data(params,
+ prvAlias,
+ incorrectPasswordRSA,
+ wrappedKey,
+ "AES_KEY_IMP",
+ 192,
+ policyExpPass,
+ &dataImp
+ );
+ assert_eq(temp, CKMC_ERROR_AUTHENTICATION_FAILED);
+
+ ckmc_key_free(wrappedKey);
+
+ return 0;
+}
\ No newline at end of file