提交 aa42af14 编写于 作者: N nozomi

fix bug

Signed-off-by: Nnozomi <zhanghanlin9@huawei.com>
上级 5e6727d2
......@@ -64,6 +64,7 @@ ohos_moduletest_suite("ActsHuksNAPITest") {
"src/huks_signverify_sm2_test.cpp",
"src/huks_three_stage_test_common.cpp",
"src/huks_warpped_test.cpp",
"src/huks_wrapped_test_common.cpp",
]
include_dirs = [
......
......@@ -30,6 +30,14 @@ int32_t HksMemCmp(const void *ptr1, const void *ptr2, uint32_t size);
#define OH_HUKS_KEY_BYTES(keySize) (((keySize) + 7) / 8)
#define HUKS_FREE_BLOB(blob) do { \
if ((blob).data != nullptr) { \
HksFree((blob).data); \
(blob).data = nullptr; \
} \
(blob).size = 0; \
} while (0)
#ifdef __cplusplus
}
#endif
......
......@@ -87,6 +87,11 @@ extern "C"
const struct OH_Huks_Blob *srcData,
struct OH_Huks_Blob *hash);
OH_Huks_Result HuksAgreeKey(
const struct OH_Huks_ParamSet *paramSet,
const struct OH_Huks_Blob *keyAlias,
const struct OH_Huks_Blob *peerPublicKey,
struct OH_Huks_Blob *agreedKey);
#ifdef __cplusplus
}
#endif
......
......@@ -13,6 +13,7 @@
* limitations under the License.
*/
#include "huks_three_stage_test_common.h"
#include "huks_wrapped_test_common.h"
#ifndef HUKS_WARPPED_TEST_H
#define HUKS_WARPPED_TEST_H
namespace Unittest::ImportWrapped {
......
/*
* Copyright (c) 2022 Huawei Device 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.
*/
#ifndef HUKS_WRAPPED_TEST_COMMON_H
#define HUKS_WRAPPED_TEST_COMMON_H
#include "huks_three_stage_test_common.h"
namespace Unittest::ImportWrapped {
struct HksImportWrappedKeyTestParams {
struct OH_Huks_Blob *wrappingKeyAlias;
struct OH_Huks_Blob *agreeKeyAlgName;
struct OH_Huks_ParamSet *genWrappingKeyParamSet;
uint32_t publicKeySize;
struct OH_Huks_Blob *callerKeyAlias;
struct OH_Huks_ParamSet *genCallerKeyParamSet;
struct OH_Huks_Blob *callerKekAlias;
struct OH_Huks_Blob *callerKek;
struct OH_Huks_ParamSet *importCallerKekParamSet;
struct OH_Huks_Blob *callerAgreeKeyAlias;
struct OH_Huks_ParamSet *agreeParamSet;
struct OH_Huks_ParamSet *importWrappedKeyParamSet;
struct OH_Huks_Blob *importedKeyAlias;
struct OH_Huks_Blob *importedPlainKey;
uint32_t keyMaterialLen;
};
static const uint32_t IV_SIZE = 16;
static uint8_t IV[IV_SIZE] = "bababababababab";
static const uint32_t AAD_SIZE = 16;
static uint8_t AAD[AAD_SIZE] = "abababababababa";
static const uint32_t NONCE_SIZE = 12;
static uint8_t NONCE[NONCE_SIZE] = "hahahahahah";
static const uint32_t AEAD_TAG_SIZE = 16;
static const uint32_t X25519_256_SIZE = 256;
void HksImportWrappedKeyTestCommonCase(const struct HksImportWrappedKeyTestParams *params);
void HksClearKeysForWrappedKeyTest(const struct HksImportWrappedKeyTestParams *params);
} //namespace Unittest::ImportWrapped
#endif // HUKS_WRAPPED_TEST_COMMON_H
......@@ -56,10 +56,10 @@ static struct OH_Huks_Param g_genParamsTest001[] = {
.uint32Param = OH_HUKS_KEY_PURPOSE_SIGN | OH_HUKS_KEY_PURPOSE_VERIFY
}, {
.tag = OH_HUKS_TAG_KEY_SIZE,
.uint32Param = OH_HUKS_ECC_KEY_SIZE_224
.uint32Param = OH_HUKS_ECC_KEY_SIZE_256
}, {
.tag = OH_HUKS_TAG_DIGEST,
.uint32Param = OH_HUKS_DIGEST_NONE
.uint32Param = OH_HUKS_DIGEST_SHA256
}
};
static struct OH_Huks_Param g_signParamsTest001[] = {
......@@ -71,10 +71,10 @@ static struct OH_Huks_Param g_signParamsTest001[] = {
.uint32Param = OH_HUKS_KEY_PURPOSE_SIGN
}, {
.tag = OH_HUKS_TAG_KEY_SIZE,
.uint32Param = OH_HUKS_ECC_KEY_SIZE_224
.uint32Param = OH_HUKS_ECC_KEY_SIZE_256
}, {
.tag = OH_HUKS_TAG_DIGEST,
.uint32Param = OH_HUKS_DIGEST_NONE
.uint32Param = OH_HUKS_DIGEST_SHA256
}
};
static struct OH_Huks_Param g_verifyParamsTest001[] = {
......@@ -86,10 +86,10 @@ static struct OH_Huks_Param g_verifyParamsTest001[] = {
.uint32Param = OH_HUKS_KEY_PURPOSE_VERIFY
}, {
.tag = OH_HUKS_TAG_KEY_SIZE,
.uint32Param = OH_HUKS_ECC_KEY_SIZE_224
.uint32Param = OH_HUKS_ECC_KEY_SIZE_256
}, {
.tag = OH_HUKS_TAG_DIGEST,
.uint32Param = OH_HUKS_DIGEST_NONE
.uint32Param = OH_HUKS_DIGEST_SHA256
}
};
......
......@@ -334,7 +334,6 @@ static OH_Huks_Result CreateImportKeyAlias(struct OH_Huks_Blob *importKeyAlias,
ret.errorCode=OH_HUKS_ERR_CODE_ILLEGAL_ARGUMENT;
return ret;
}
return ret;
}
......
......@@ -145,9 +145,9 @@ OH_Huks_Result HksTestUpdate(
}
OH_Huks_Result TestUpdateLoopFinish(const struct OH_Huks_Blob *handle,
const struct OH_Huks_ParamSet *paramSet,
const struct OH_Huks_Blob *inData,
struct OH_Huks_Blob *outData)
const struct OH_Huks_ParamSet *paramSet,
const struct OH_Huks_Blob *inData,
struct OH_Huks_Blob *outData)
{
struct OH_Huks_Result ret;
ret.errorCode = OH_HUKS_SUCCESS;
......@@ -374,19 +374,18 @@ OH_Huks_Result TestCmpKeyAliasHash(
}
static OH_Huks_Result HksEncryptLoopUpdate(const struct OH_Huks_Blob *handle, const struct OH_Huks_ParamSet *paramSet,
const struct OH_Huks_Blob *inData)
const struct OH_Huks_Blob *inData, struct OH_Huks_Blob *outData)
{
struct OH_Huks_Blob inDataSeg = *inData;
inDataSeg.size = MAX_UPDATE_SIZE;
struct OH_Huks_Result ret;
ret.errorCode = OH_HUKS_SUCCESS;
struct OH_Huks_Blob inDataSeg = *inData;
uint8_t *lastPtr = inData->data + inData->size - 1;
struct OH_Huks_Blob outDataSeg = {
.size = MAX_OUTDATA_SIZE,
.data = NULL};
struct OH_Huks_Blob outDataSeg = {MAX_OUTDATA_SIZE, NULL};
uint8_t *cur = outData->data;
outData->size = 0;
inDataSeg.size = MAX_UPDATE_SIZE;
bool isFinished = false;
while (inDataSeg.data <= lastPtr)
......@@ -399,54 +398,69 @@ static OH_Huks_Result HksEncryptLoopUpdate(const struct OH_Huks_Blob *handle, co
{
isFinished = true;
inDataSeg.size = lastPtr - inDataSeg.data + 1;
outDataSeg.size = inDataSeg.size + MAX_UPDATE_SIZE;
break;
}
if (MallocAndCheckBlobData(&outDataSeg, outDataSeg.size).errorCode != (int32_t)OH_HUKS_SUCCESS)
{
ret.errorCode = OH_HUKS_ERR_CODE_INTERNAL_ERROR;
return ret;
}
struct OH_Huks_Result result = OH_Huks_UpdateSession(handle, paramSet, &inDataSeg, &outDataSeg);
if (result.errorCode != (int32_t)OH_HUKS_SUCCESS)
ret = OH_Huks_UpdateSession(handle, paramSet, &inDataSeg, &outDataSeg);
if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS)
{
free(outDataSeg.data);
return result;
return ret;
}
(void)memcpy_s(cur, outDataSeg.size, outDataSeg.data, outDataSeg.size);
cur += outDataSeg.size;
outData->size += outDataSeg.size;
free(outDataSeg.data);
if ((isFinished == false) && (inDataSeg.data + MAX_UPDATE_SIZE > lastPtr))
{
ret.errorCode = OH_HUKS_ERR_CODE_INTERNAL_ERROR;
return ret;
}
inDataSeg.data += MAX_UPDATE_SIZE;
}
struct OH_Huks_Blob outDataFinish = {inDataSeg.size * TIMES, NULL};
if (MallocAndCheckBlobData(&outDataFinish, outDataFinish.size).errorCode != (int32_t)OH_HUKS_SUCCESS)
{
ret.errorCode = OH_HUKS_ERR_CODE_INTERNAL_ERROR;
return ret;
}
ret = OH_Huks_FinishSession(handle, paramSet, &inDataSeg, &outDataFinish);
if (ret.errorCode != OH_HUKS_SUCCESS)
{
free(outDataFinish.data);
return ret;
}
(void)memcpy_s(cur, outDataFinish.size, outDataFinish.data, outDataFinish.size);
outData->size += outDataFinish.size;
free(outDataFinish.data);
return ret;
}
OH_Huks_Result HuksEncrypt(const struct OH_Huks_Blob *key, const struct OH_Huks_ParamSet *paramSet,
const struct OH_Huks_Blob *plainText, struct OH_Huks_Blob *cipherText)
const struct OH_Huks_Blob *plainText, struct OH_Huks_Blob *cipherText)
{
uint8_t handle[sizeof(uint64_t)] = {0};
struct OH_Huks_Blob handleBlob = {sizeof(uint64_t), handle};
struct OH_Huks_Result result = OH_Huks_InitSession(key, paramSet, &handleBlob, nullptr);
if (result.errorCode != OH_HUKS_SUCCESS)
{
return result;
}
OH_Huks_Result ret = HksEncryptLoopUpdate(&handleBlob, paramSet, plainText);
if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS)
OH_Huks_Result ret = OH_Huks_InitSession(key, paramSet, &handleBlob, nullptr);
if (ret.errorCode != OH_HUKS_SUCCESS)
{
return ret;
}
struct OH_Huks_Blob finishData = {0, nullptr};
result = OH_Huks_FinishSession(&handleBlob, paramSet, &finishData, cipherText);
return result;
ret = HksEncryptLoopUpdate(&handleBlob, paramSet, plainText, cipherText);
return ret;
}
static OH_Huks_Result HksDecryptLoopUpdate(const struct OH_Huks_Blob *handle, const struct OH_Huks_ParamSet *paramSet,
const struct OH_Huks_Blob *inData, struct OH_Huks_Blob *outData)
const struct OH_Huks_Blob *inData, struct OH_Huks_Blob *outData)
{
struct OH_Huks_Result ret;
ret.errorCode = OH_HUKS_SUCCESS;
......@@ -517,7 +531,7 @@ static OH_Huks_Result HksDecryptLoopUpdate(const struct OH_Huks_Blob *handle, co
}
OH_Huks_Result HuksDecrypt(const struct OH_Huks_Blob *key, const struct OH_Huks_ParamSet *paramSet,
const struct OH_Huks_Blob *cipherText, struct OH_Huks_Blob *plainText)
const struct OH_Huks_Blob *cipherText, struct OH_Huks_Blob *plainText)
{
uint8_t handle[sizeof(uint64_t)] = {0};
......@@ -534,92 +548,100 @@ OH_Huks_Result HuksDecrypt(const struct OH_Huks_Blob *key, const struct OH_Huks_
static int32_t HksGetDigestLen(uint32_t digest, uint32_t *digestLen)
{
switch (digest) {
case OH_HUKS_DIGEST_MD5:
*digestLen = HKS_DIGEST_MD5_LEN;
break;
case OH_HUKS_DIGEST_SHA1:
*digestLen = HKS_DIGEST_SHA1_LEN;
break;
case OH_HUKS_DIGEST_SHA224:
*digestLen = HKS_DIGEST_SHA224_LEN;
break;
case OH_HUKS_DIGEST_SHA256:
*digestLen = HKS_DIGEST_SHA256_LEN;
break;
case OH_HUKS_DIGEST_SHA384:
*digestLen = HKS_DIGEST_SHA384_LEN;
break;
case OH_HUKS_DIGEST_SHA512:
*digestLen = HKS_DIGEST_SHA512_LEN;
break;
case OH_HUKS_DIGEST_SM3:
*digestLen = HKS_DIGEST_SM3_LEN;
break;
default:
return OH_HUKS_ERR_CODE_ILLEGAL_ARGUMENT;
switch (digest)
{
case OH_HUKS_DIGEST_MD5:
*digestLen = HKS_DIGEST_MD5_LEN;
break;
case OH_HUKS_DIGEST_SHA1:
*digestLen = HKS_DIGEST_SHA1_LEN;
break;
case OH_HUKS_DIGEST_SHA224:
*digestLen = HKS_DIGEST_SHA224_LEN;
break;
case OH_HUKS_DIGEST_SHA256:
*digestLen = HKS_DIGEST_SHA256_LEN;
break;
case OH_HUKS_DIGEST_SHA384:
*digestLen = HKS_DIGEST_SHA384_LEN;
break;
case OH_HUKS_DIGEST_SHA512:
*digestLen = HKS_DIGEST_SHA512_LEN;
break;
case OH_HUKS_DIGEST_SM3:
*digestLen = HKS_DIGEST_SM3_LEN;
break;
default:
return OH_HUKS_ERR_CODE_ILLEGAL_ARGUMENT;
}
return OH_HUKS_SUCCESS;
}
static const EVP_MD *GetOpensslAlg(uint32_t alg)
{
switch (alg) {
case OH_HUKS_DIGEST_MD5:
return EVP_md5();
case OH_HUKS_DIGEST_SHA1:
return EVP_sha1();
case OH_HUKS_DIGEST_SHA224:
return EVP_sha224();
case OH_HUKS_DIGEST_SHA256:
return EVP_sha256();
case OH_HUKS_DIGEST_SHA384:
return EVP_sha384();
case OH_HUKS_DIGEST_SHA512:
return EVP_sha512();
default:
return NULL;
switch (alg)
{
case OH_HUKS_DIGEST_MD5:
return EVP_md5();
case OH_HUKS_DIGEST_SHA1:
return EVP_sha1();
case OH_HUKS_DIGEST_SHA224:
return EVP_sha224();
case OH_HUKS_DIGEST_SHA256:
return EVP_sha256();
case OH_HUKS_DIGEST_SHA384:
return EVP_sha384();
case OH_HUKS_DIGEST_SHA512:
return EVP_sha512();
default:
return NULL;
}
}
static int32_t HksOpensslHash(uint32_t alg, const struct OH_Huks_Blob *msg, struct OH_Huks_Blob *hash)
{
const EVP_MD *opensslAlg = GetOpensslAlg(alg);
if (opensslAlg == NULL) {
if (opensslAlg == NULL)
{
return OH_HUKS_ERR_CODE_ILLEGAL_ARGUMENT;
}
int32_t ret = EVP_Digest(msg->data, msg->size, hash->data, &hash->size, opensslAlg, NULL);
if (ret != 1) {
if (ret != 1)
{
return OH_HUKS_ERR_CODE_ILLEGAL_ARGUMENT;
}
return (int32_t)OH_HUKS_SUCCESS;
}
OH_Huks_Result HuksHash(const struct OH_Huks_ParamSet *paramSet,
const struct OH_Huks_Blob *srcData, struct OH_Huks_Blob *hash)
const struct OH_Huks_Blob *srcData, struct OH_Huks_Blob *hash)
{
OH_Huks_Result ret;
if ((paramSet == NULL) || (srcData == NULL) || (hash == NULL)) {
if ((paramSet == NULL) || (srcData == NULL) || (hash == NULL))
{
ret.errorCode = OH_HUKS_ERR_CODE_ILLEGAL_ARGUMENT;
return ret;
}
struct OH_Huks_Param *digestAlg = NULL;
ret = OH_Huks_GetParam(paramSet, OH_HUKS_TAG_DIGEST, &digestAlg);
if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS) {
if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS)
{
return ret;
}
uint32_t digestLen;
int32_t ret1 = HksGetDigestLen(digestAlg->uint32Param, &digestLen);
ret.errorCode = ret1;
if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS) {
if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS)
{
return ret;
}
if (hash->size < digestLen) {
if (hash->size < digestLen)
{
ret.errorCode = OH_HUKS_ERR_CODE_ILLEGAL_ARGUMENT;
return ret;
}
......@@ -628,3 +650,34 @@ OH_Huks_Result HuksHash(const struct OH_Huks_ParamSet *paramSet,
return ret;
}
OH_Huks_Result HuksAgreeKey(const struct OH_Huks_ParamSet *paramSet, const struct OH_Huks_Blob *keyAlias,
const struct OH_Huks_Blob *peerPublicKey, struct OH_Huks_Blob *agreedKey)
{
uint8_t temp[10] = {0};
struct OH_Huks_Blob inData = {
sizeof(temp),
temp};
uint8_t handleU[sizeof(uint64_t)] = {0};
struct OH_Huks_Blob handle = {sizeof(uint64_t), handleU};
OH_Huks_Result ret = OH_Huks_InitSession(keyAlias, paramSet, &handle, nullptr);
if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS)
{
return ret;
}
uint8_t outDataU[1024] = {0};
struct OH_Huks_Blob outDataUpdate = {1024, outDataU};
ret = OH_Huks_UpdateSession(&handle, paramSet, peerPublicKey, &outDataUpdate);
if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS)
{
return ret;
}
ret = OH_Huks_FinishSession(&handle, paramSet, &inData, agreedKey);
if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS)
{
return ret;
}
return ret;
}
\ No newline at end of file
......@@ -17,12 +17,15 @@
#include "huks_warpped_test.h"
#include "huks_three_stage_test_common.h"
#include "huks_wrapped_test_common.h"
#include "huks_mem.h"
#include "native_huks_type.h"
using namespace testing::ext;
namespace Unittest::ImportWrappedKey {
class HuksImportWrappedX25519Test : public testing::Test {
namespace Unittest::ImportWrapped
{
class HuksImportWrappedX25519Test : public testing::Test
{
public:
static void SetUpTestCase(void);
......@@ -43,85 +46,135 @@ namespace Unittest::ImportWrappedKey {
void HuksImportWrappedX25519Test::SetUp()
{
}
void HuksImportWrappedX25519Test::TearDown() {
void HuksImportWrappedX25519Test::TearDown()
{
}
static struct OH_Huks_Param g_genImportECCOption01[] = {
{
.tag = OH_HUKS_TAG_ALGORITHM,
.uint32Param = OH_HUKS_ALG_ECC
}, {
.tag = OH_HUKS_TAG_KEY_SIZE,
.uint32Param = OH_HUKS_ECC_KEY_SIZE_256
}, {
.tag = OH_HUKS_TAG_PURPOSE,
.uint32Param = OH_HUKS_KEY_PURPOSE_UNWRAP
}, {
.tag = OH_HUKS_TAG_DIGEST,
.uint32Param = OH_HUKS_DIGEST_SHA256
}, {
.tag = OH_HUKS_TAG_IMPORT_KEY_TYPE,
.uint32Param = OH_HUKS_KEY_TYPE_KEY_PAIR
},
struct TestImportKeyData {
struct OH_Huks_Blob x509PublicKey;
struct OH_Huks_Blob publicOrXData;
struct OH_Huks_Blob privateOrYData;
struct OH_Huks_Blob zData;
};
static const uint8_t inputECCPair[] = {
0x02, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
0x20, 0x00, 0x00, 0x00, 0xa5, 0xb8, 0xa3, 0x78, 0x1d, 0x6d, 0x76, 0xe0, 0xb3, 0xf5, 0x6f, 0x43,
0x9d, 0xcf, 0x60, 0xf6, 0x0b, 0x3f, 0x64, 0x45, 0xa8, 0x3f, 0x1a, 0x96, 0xf1, 0xa1, 0xa4, 0x5d,
0x3e, 0x2c, 0x3f, 0x13, 0xd7, 0x81, 0xf7, 0x2a, 0xb5, 0x8d, 0x19, 0x3d, 0x9b, 0x96, 0xc7, 0x6a,
0x10, 0xf0, 0xaa, 0xbc, 0x91, 0x6f, 0x4d, 0xa7, 0x09, 0xb3, 0x57, 0x88, 0x19, 0x6f, 0x00, 0x4b,
0xad, 0xee, 0x34, 0x35, 0xfb, 0x8b, 0x9f, 0x12, 0xa0, 0x83, 0x19, 0xbe, 0x6a, 0x6f, 0x63, 0x2a,
0x7c, 0x86, 0xba, 0xca, 0x64, 0x0b, 0x88, 0x96, 0xe2, 0xfa, 0x77, 0xbc, 0x71, 0xe3, 0x0f, 0x0f,
0x9e, 0x3c, 0xe5, 0xf9
static struct OH_Huks_Blob g_wrappingKeyAliasAes256 = {
.size = strlen("test_wrappingKey_x25519_aes256"),
.data = (uint8_t *)"test_wrappingKey_x25519_aes256"};
static struct OH_Huks_Blob g_callerKeyAliasAes256 = {
.size = strlen("test_caller_key_x25519_aes256"),
.data = (uint8_t *)"test_caller_key_x25519_aes256"};
static struct OH_Huks_Blob g_callerKekAliasAes256 = {
.size = strlen("test_caller_kek_x25519_aes256"),
.data = (uint8_t *)"test_caller_kek_x25519_aes256"};
static struct OH_Huks_Blob g_callerAes256Kek = {
.size = strlen("This is kek to encrypt plain key"),
.data = (uint8_t *)"This is kek to encrypt plain key"};
static struct OH_Huks_Blob g_callerAgreeKeyAliasAes256 = {
.size = strlen("test_caller_agree_key_x25519_aes256"),
.data = (uint8_t *)"test_caller_agree_key_x25519_aes256"};
static struct OH_Huks_Blob g_importedKeyAliasAes256 = {
.size = strlen("test_import_key_x25519_aes256"),
.data = (uint8_t *)"test_import_key_x25519_aes256"};
static struct OH_Huks_Blob g_importedAes256PlainKey = {
.size = strlen("This is plain key to be imported"),
.data = (uint8_t *)"This is plain key to be imported"};
static struct OH_Huks_Param g_importWrappedAes256Params[] = {
{.tag = OH_HUKS_TAG_ALGORITHM, .uint32Param = OH_HUKS_ALG_AES},
{.tag = OH_HUKS_TAG_PURPOSE, .uint32Param = OH_HUKS_KEY_PURPOSE_ENCRYPT | OH_HUKS_KEY_PURPOSE_DECRYPT},
{.tag = OH_HUKS_TAG_KEY_SIZE, .uint32Param = OH_HUKS_AES_KEY_SIZE_256},
{.tag = OH_HUKS_TAG_PADDING, .uint32Param = OH_HUKS_PADDING_NONE},
{.tag = OH_HUKS_TAG_BLOCK_MODE, .uint32Param = OH_HUKS_MODE_GCM},
{.tag = OH_HUKS_TAG_DIGEST, .uint32Param = OH_HUKS_DIGEST_NONE},
{.tag = OH_HUKS_TAG_UNWRAP_ALGORITHM_SUITE, .uint32Param = OH_HUKS_UNWRAP_SUITE_X25519_AES_256_GCM_NOPADDING},
{.tag = OH_HUKS_TAG_ASSOCIATED_DATA, .blob = {.size = Unittest::ImportWrapped::AAD_SIZE, .data = (uint8_t *)Unittest::ImportWrapped::AAD}},
{.tag = OH_HUKS_TAG_NONCE, .blob = {.size = Unittest::ImportWrapped::NONCE_SIZE, .data = (uint8_t *)Unittest::ImportWrapped::NONCE}}};
static char g_agreeKeyAlgName[] = "X25519";
static struct OH_Huks_Blob g_agreeKeyAlgNameBlob = {
.size = sizeof(g_agreeKeyAlgName),
.data = (uint8_t *)g_agreeKeyAlgName};
static const uint32_t g_x25519PubKeySize = 32;
static struct OH_Huks_Param g_genWrappingKeyParams[] = {
{.tag = OH_HUKS_TAG_ALGORITHM, .uint32Param = OH_HUKS_ALG_X25519},
{.tag = OH_HUKS_TAG_PURPOSE, .uint32Param = OH_HUKS_KEY_PURPOSE_UNWRAP},
{.tag = OH_HUKS_TAG_KEY_SIZE, .uint32Param = OH_HUKS_CURVE25519_KEY_SIZE_256}};
static struct OH_Huks_Param g_genCallerX25519Params[] = {
{.tag = OH_HUKS_TAG_ALGORITHM, .uint32Param = OH_HUKS_ALG_X25519},
{.tag = OH_HUKS_TAG_PURPOSE, .uint32Param = OH_HUKS_KEY_PURPOSE_AGREE},
{.tag = OH_HUKS_TAG_KEY_SIZE, .uint32Param = OH_HUKS_CURVE25519_KEY_SIZE_256}};
static struct OH_Huks_Param g_importParamsCallerKek[] = {
{.tag = OH_HUKS_TAG_ALGORITHM, .uint32Param = OH_HUKS_ALG_AES},
{.tag = OH_HUKS_TAG_PURPOSE, .uint32Param = OH_HUKS_KEY_PURPOSE_ENCRYPT},
{.tag = OH_HUKS_TAG_KEY_SIZE, .uint32Param = OH_HUKS_AES_KEY_SIZE_256},
{.tag = OH_HUKS_TAG_PADDING, .uint32Param = OH_HUKS_PADDING_NONE},
{.tag = OH_HUKS_TAG_BLOCK_MODE, .uint32Param = OH_HUKS_MODE_GCM},
{.tag = OH_HUKS_TAG_DIGEST, .uint32Param = OH_HUKS_DIGEST_NONE},
{.tag = OH_HUKS_TAG_IV, .blob = {.size = Unittest::ImportWrapped::IV_SIZE, .data = (uint8_t *)Unittest::ImportWrapped::IV}}};
static struct OH_Huks_Param g_callerAgreeParams[] = {
{.tag = OH_HUKS_TAG_ALGORITHM, .uint32Param = OH_HUKS_ALG_X25519},
{.tag = OH_HUKS_TAG_PURPOSE, .uint32Param = OH_HUKS_KEY_PURPOSE_AGREE},
{.tag = OH_HUKS_TAG_KEY_SIZE, .uint32Param = OH_HUKS_CURVE25519_KEY_SIZE_256}
};
static struct OH_Huks_Param g_ImportECCOption02[] = {
static void InitCommonTestParamsAndDoImport(
struct HksImportWrappedKeyTestParams *importWrappedKeyTestParams,
const struct OH_Huks_Param *importedKeyParamSetArray,
uint32_t arraySize)
{
.tag = OH_HUKS_TAG_ALGORITHM,
.uint32Param = OH_HUKS_ALG_AES
}, {
.tag = OH_HUKS_TAG_KEY_SIZE,
.uint32Param = OH_HUKS_AES_KEY_SIZE_256
}, {
.tag = OH_HUKS_TAG_PURPOSE,
.uint32Param = OH_HUKS_KEY_PURPOSE_ENCRYPT | OH_HUKS_KEY_PURPOSE_DECRYPT
}, {
.tag = OH_HUKS_TAG_BLOCK_MODE,
.uint32Param = OH_HUKS_MODE_CBC
}, {
.tag = OH_HUKS_TAG_PADDING,
.uint32Param = OH_HUKS_PADDING_NONE
}, {
.tag = OH_HUKS_TAG_UNWRAP_ALGORITHM_SUITE,
.uint32Param = OH_HUKS_UNWRAP_SUITE_ECDH_AES_256_GCM_NOPADDING
},
};
static const uint8_t inputECCKey[] = {
0x5b, 0x00, 0x00, 0x00, 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02,
0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0xc0,
0xfe, 0x1c, 0x67, 0xde, 0x86, 0x0e, 0xfb, 0xaf, 0xb5, 0x85, 0x52, 0xb4, 0x0e, 0x1f, 0x6c, 0x6c,
0xaa, 0xc5, 0xd9, 0xd2, 0x4d, 0xb0, 0x8a, 0x72, 0x24, 0xa1, 0x99, 0xaf, 0xfc, 0x3e, 0x55, 0x5a,
0xac, 0x99, 0x3d, 0xe8, 0x34, 0x72, 0xb9, 0x47, 0x9c, 0xa6, 0xd8, 0xfb, 0x00, 0xa0, 0x1f, 0x9f,
0x7a, 0x41, 0xe5, 0x44, 0x3e, 0xb2, 0x76, 0x08, 0xa2, 0xbd, 0xe9, 0x41, 0xd5, 0x2b, 0x9e, 0x10,
0x00, 0x00, 0x00, 0xbf, 0xf9, 0x69, 0x41, 0xf5, 0x49, 0x85, 0x31, 0x35, 0x14, 0x69, 0x12, 0x57,
0x9c, 0xc8, 0xb7, 0x10, 0x00, 0x00, 0x00, 0x2d, 0xb7, 0xf1, 0x5a, 0x0f, 0xb8, 0x20, 0xc5, 0x90,
0xe5, 0xca, 0x45, 0x84, 0x5c, 0x08, 0x08, 0x10, 0x00, 0x00, 0x00, 0x43, 0x25, 0x1b, 0x2f, 0x5b,
0x86, 0xd8, 0x87, 0x04, 0x4d, 0x38, 0xc2, 0x65, 0xcc, 0x9e, 0xb7, 0x20, 0x00, 0x00, 0x00, 0xf4,
0xe8, 0x93, 0x28, 0x0c, 0xfa, 0x4e, 0x11, 0x6b, 0xe8, 0xbd, 0xa8, 0xe9, 0x3f, 0xa7, 0x8f, 0x2f,
0xe3, 0xb3, 0xbf, 0xaf, 0xce, 0xe5, 0x06, 0x2d, 0xe6, 0x45, 0x5d, 0x19, 0x26, 0x09, 0xe7, 0x10,
0x00, 0x00, 0x00, 0xf4, 0x1e, 0x7b, 0x01, 0x7a, 0x84, 0x36, 0xa4, 0xa8, 0x1c, 0x0d, 0x3d, 0xde,
0x57, 0x66, 0x73, 0x10, 0x00, 0x00, 0x00, 0xe3, 0xff, 0x29, 0x97, 0xad, 0xb3, 0x4a, 0x2c, 0x50,
0x08, 0xb5, 0x68, 0xe1, 0x90, 0x5a, 0xdc, 0x10, 0x00, 0x00, 0x00, 0x26, 0xae, 0xdc, 0x4e, 0xa5,
0x6e, 0xb1, 0x38, 0x14, 0x24, 0x47, 0x1c, 0x41, 0x89, 0x63, 0x11, 0x04, 0x00, 0x00, 0x00, 0x20,
0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x0b, 0xcb, 0xa9, 0xa8, 0x5f, 0x5a, 0x9d, 0xbf, 0xa1,
0xfc, 0x72, 0x74, 0x87, 0x79, 0xf2, 0xf4, 0x22, 0x0c, 0x8a, 0x4d, 0xd8, 0x7e, 0x10, 0xc8, 0x44,
0x17, 0x95, 0xab, 0x3b, 0xd2, 0x8f, 0x0a
};
importWrappedKeyTestParams->agreeKeyAlgName = &g_agreeKeyAlgNameBlob;
struct OH_Huks_ParamSet *genX25519KeyParamSet = nullptr;
OH_Huks_Result ret = InitParamSet(&genX25519KeyParamSet, g_genWrappingKeyParams,
sizeof(g_genWrappingKeyParams) / sizeof(OH_Huks_Param));
EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "InitParamSet(gen huks x25519) failed.";
importWrappedKeyTestParams->genWrappingKeyParamSet = genX25519KeyParamSet;
importWrappedKeyTestParams->publicKeySize = g_x25519PubKeySize;
struct OH_Huks_ParamSet *genCallerKeyParamSet = nullptr;
ret = InitParamSet(&genCallerKeyParamSet, g_genCallerX25519Params,
sizeof(g_genCallerX25519Params) / sizeof(OH_Huks_Param));
EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "InitParamSet(gen caller x25519) failed.";
importWrappedKeyTestParams->genCallerKeyParamSet = genCallerKeyParamSet;
struct OH_Huks_ParamSet *callerImportParamsKek = nullptr;
ret = InitParamSet(&callerImportParamsKek, g_importParamsCallerKek,
sizeof(g_importParamsCallerKek) / sizeof(OH_Huks_Param));
EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "InitParamSet(import call kek) failed.";
importWrappedKeyTestParams->importCallerKekParamSet = callerImportParamsKek;
struct OH_Huks_ParamSet *agreeParamSet = nullptr;
ret = InitParamSet(&agreeParamSet, g_callerAgreeParams,
sizeof(g_callerAgreeParams) / sizeof(OH_Huks_Param));
EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "InitParamSet(agreeParamSet) failed.";
importWrappedKeyTestParams->agreeParamSet = agreeParamSet;
struct OH_Huks_ParamSet *importPlainKeyParams = nullptr;
ret = InitParamSet(&importPlainKeyParams, importedKeyParamSetArray, arraySize);
EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "InitParamSet(import plain key) failed.";
importWrappedKeyTestParams->importWrappedKeyParamSet = importPlainKeyParams;
HksImportWrappedKeyTestCommonCase(importWrappedKeyTestParams);
OH_Huks_FreeParamSet(&genX25519KeyParamSet);
OH_Huks_FreeParamSet(&genCallerKeyParamSet);
OH_Huks_FreeParamSet(&callerImportParamsKek);
OH_Huks_FreeParamSet(&importPlainKeyParams);
}
/**
* @tc.name: HuksImportWrappedX25519Test.Security_HUKS_NAPI_Wrapped_0100
......@@ -131,33 +184,18 @@ namespace Unittest::ImportWrappedKey {
*/
HWTEST_F(HuksImportWrappedX25519Test, Security_HUKS_NAPI_Wrapped_0100, TestSize.Level0)
{
char tmpKey1[] = "Security_HUKS_NAPI_Wrapped_0100";
struct OH_Huks_Blob newKeyAlias1 = { .size = strlen(tmpKey1), .data = (uint8_t *)tmpKey1 };
char tmpKey2[] = "Security_HUKS_NAPI_Wrapped_0100Wrap";
struct OH_Huks_Blob newKeyAlias2 = { .size = strlen(tmpKey2), .data = (uint8_t *)tmpKey2 };
struct OH_Huks_Blob inputECCPairBlob = {sizeof(inputECCPair), (uint8_t *) inputECCPair};
struct OH_Huks_Blob inputECCKeyBlob = {sizeof(inputECCKey), (uint8_t *) inputECCKey};
struct OH_Huks_ParamSet *genParamSetOption01 = nullptr;
struct OH_Huks_ParamSet *genParamSetOption02 = nullptr;
OH_Huks_Result ret = InitParamSet(&genParamSetOption01, g_genImportECCOption01, sizeof(g_genImportECCOption01) / sizeof(OH_Huks_Param));
EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "InitParamSet failed.";
ret = InitParamSet(&genParamSetOption02, g_ImportECCOption02, sizeof(g_ImportECCOption02) / sizeof(OH_Huks_Param));
EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "InitParamSet failed.";
ret = OH_Huks_ImportKeyItem(&newKeyAlias1, genParamSetOption01, &inputECCPairBlob);
EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "OH_Huks_ImportKeyItem failed.";
ret = OH_Huks_ImportWrappedKeyItem(&newKeyAlias2, &newKeyAlias1,
genParamSetOption02, &inputECCKeyBlob);
EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "OH_Huks_ImportWrappedKeyItem failed.";
ret = OH_Huks_DeleteKeyItem(&newKeyAlias1, genParamSetOption01);
EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "OH_Huks_DeleteKeyItem failed.";
ret = OH_Huks_DeleteKeyItem(&newKeyAlias2, genParamSetOption02);
EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "OH_Huks_DeleteKeyItem failed.";
OH_Huks_FreeParamSet(&genParamSetOption01);
OH_Huks_FreeParamSet(&genParamSetOption02);
struct HksImportWrappedKeyTestParams importWrappedKeyTestParams001 = {0};
importWrappedKeyTestParams001.wrappingKeyAlias = &g_wrappingKeyAliasAes256;
importWrappedKeyTestParams001.keyMaterialLen = g_importedAes256PlainKey.size;
importWrappedKeyTestParams001.callerKeyAlias = &g_callerKeyAliasAes256;
importWrappedKeyTestParams001.callerKekAlias = &g_callerKekAliasAes256;
importWrappedKeyTestParams001.callerKek = &g_callerAes256Kek;
importWrappedKeyTestParams001.callerAgreeKeyAlias = &g_callerAgreeKeyAliasAes256;
importWrappedKeyTestParams001.importedKeyAlias = &g_importedKeyAliasAes256;
importWrappedKeyTestParams001.importedPlainKey = &g_importedAes256PlainKey;
InitCommonTestParamsAndDoImport(&importWrappedKeyTestParams001, g_importWrappedAes256Params,
sizeof(g_importWrappedAes256Params) / sizeof(struct OH_Huks_Param));
HksClearKeysForWrappedKeyTest(&importWrappedKeyTestParams001);
}
}
/*
* Copyright (c) 2022 Huawei Device 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 <gtest/gtest.h>
#include "huks_three_stage_test_common.h"
#include "huks_wrapped_test_common.h"
using namespace testing::ext;
namespace Unittest::ImportWrapped
{
static struct OH_Huks_Param g_aesKekEncryptParams[] = {
{.tag = OH_HUKS_TAG_ALGORITHM, .uint32Param = OH_HUKS_ALG_AES},
{.tag = OH_HUKS_TAG_PURPOSE, .uint32Param = OH_HUKS_KEY_PURPOSE_ENCRYPT},
{.tag = OH_HUKS_TAG_KEY_SIZE, .uint32Param = OH_HUKS_AES_KEY_SIZE_256},
{.tag = OH_HUKS_TAG_PADDING, .uint32Param = OH_HUKS_PADDING_NONE},
{.tag = OH_HUKS_TAG_BLOCK_MODE, .uint32Param = OH_HUKS_MODE_GCM},
{.tag = OH_HUKS_TAG_DIGEST, .uint32Param = OH_HUKS_DIGEST_NONE},
{.tag = OH_HUKS_TAG_ASSOCIATED_DATA, .blob = {.size = Unittest::ImportWrapped::AAD_SIZE, .data = (uint8_t *)Unittest::ImportWrapped::AAD}},
{.tag = OH_HUKS_TAG_NONCE, .blob = {.size = Unittest::ImportWrapped::NONCE_SIZE, .data = (uint8_t *)Unittest::ImportWrapped::NONCE}}};
static struct OH_Huks_Param g_importAgreeKeyParams[] = {
{.tag = OH_HUKS_TAG_ALGORITHM, .uint32Param = OH_HUKS_ALG_AES},
{.tag = OH_HUKS_TAG_PURPOSE, .uint32Param = OH_HUKS_KEY_PURPOSE_ENCRYPT},
{.tag = OH_HUKS_TAG_KEY_SIZE, .uint32Param = OH_HUKS_AES_KEY_SIZE_256},
{.tag = OH_HUKS_TAG_PADDING, .uint32Param = OH_HUKS_PADDING_NONE},
{.tag = OH_HUKS_TAG_BLOCK_MODE, .uint32Param = OH_HUKS_MODE_GCM},
{.tag = OH_HUKS_TAG_DIGEST, .uint32Param = OH_HUKS_DIGEST_NONE},
{.tag = OH_HUKS_TAG_IV, .blob = {.size = Unittest::ImportWrapped::IV_SIZE, .data = (uint8_t *)Unittest::ImportWrapped::IV}}};
static OH_Huks_Result BuildWrappedKeyData(struct OH_Huks_Blob **blobArray, uint32_t size, struct OH_Huks_Blob *outData)
{
uint32_t totalLength = size * sizeof(uint32_t);
struct OH_Huks_Result ret;
ret.errorCode = OH_HUKS_SUCCESS;
/* counter size */
for (uint32_t i = 0; i < size; ++i)
{
totalLength += blobArray[i]->size;
}
struct OH_Huks_Blob outBlob = {0, nullptr};
outBlob.size = totalLength;
(void)MallocAndCheckBlobData(&outBlob, outBlob.size);
uint32_t offset = 0;
/* copy data */
for (uint32_t i = 0; i < size; ++i)
{
if (memcpy_s(outBlob.data + offset, totalLength - offset, reinterpret_cast<uint8_t *>(&blobArray[i]->size),
sizeof(blobArray[i]->size)) != EOK)
{
ret.errorCode = OH_HUKS_ERR_CODE_INTERNAL_ERROR;
return ret;
}
offset += sizeof(blobArray[i]->size);
if (memcpy_s(outBlob.data + offset, totalLength - offset, blobArray[i]->data,
blobArray[i]->size) != EOK)
{
ret.errorCode = OH_HUKS_ERR_CODE_INTERNAL_ERROR;
return ret;
}
offset += blobArray[i]->size;
}
outData->size = outBlob.size;
outData->data = outBlob.data;
return ret;
}
static OH_Huks_Result CheckParamsValid(const struct HksImportWrappedKeyTestParams *params)
{
struct OH_Huks_Result ret;
ret.errorCode = OH_HUKS_SUCCESS;
if (params == nullptr)
{
ret.errorCode = OH_HUKS_ERR_CODE_ILLEGAL_ARGUMENT;
return ret;
}
if (params->wrappingKeyAlias == nullptr || params->genWrappingKeyParamSet == nullptr ||
params->agreeKeyAlgName == nullptr || params->callerKeyAlias == nullptr ||
params->genCallerKeyParamSet == nullptr || params->callerKekAlias == nullptr ||
params->callerKek == nullptr || params->importCallerKekParamSet == nullptr ||
params->callerAgreeKeyAlias == nullptr || params->agreeParamSet == nullptr ||
params->importWrappedKeyParamSet == nullptr || params->importedKeyAlias == nullptr ||
params->importedPlainKey == nullptr)
{
ret.errorCode = OH_HUKS_ERR_CODE_ILLEGAL_ARGUMENT;
return ret;
}
return ret;
}
static void GenerateAndExportHuksPublicKey(const struct HksImportWrappedKeyTestParams *params,
struct OH_Huks_Blob *huksPublicKey)
{
OH_Huks_Result ret = OH_Huks_GenerateKeyItem(params->wrappingKeyAlias, params->genWrappingKeyParamSet, nullptr);
EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Generate huks key failed.";
huksPublicKey->size = params->publicKeySize;
EXPECT_EQ(MallocAndCheckBlobData(huksPublicKey, huksPublicKey->size).errorCode,
(int32_t)OH_HUKS_SUCCESS)
<< "Malloc pub key failed.";
ret = OH_Huks_ExportPublicKeyItem(params->wrappingKeyAlias, nullptr, huksPublicKey);
EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Export huks public key failed.";
}
static void GenerateAndExportCallerPublicKey(const struct HksImportWrappedKeyTestParams *params,
struct OH_Huks_Blob *callerSelfPublicKey)
{
OH_Huks_Result ret = OH_Huks_GenerateKeyItem(params->callerKeyAlias, params->genCallerKeyParamSet, nullptr);
EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Generate caller key failed.";
callerSelfPublicKey->size = params->publicKeySize;
EXPECT_EQ(MallocAndCheckBlobData(callerSelfPublicKey, callerSelfPublicKey->size).errorCode,
(int32_t)OH_HUKS_SUCCESS)
<< "malloc fail";
ret = OH_Huks_ExportPublicKeyItem(params->callerKeyAlias, params->genWrappingKeyParamSet, callerSelfPublicKey);
EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Export caller public key failed.";
}
static void ImportKekAndAgreeSharedSecret(const struct HksImportWrappedKeyTestParams *params,
const struct OH_Huks_Blob *huksPublicKey, struct OH_Huks_Blob *outSharedKey)
{
OH_Huks_Result ret = OH_Huks_ImportKeyItem(params->callerKekAlias,
params->importCallerKekParamSet, params->callerKek);
EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "ImportCallerSelfKek failed.";
EXPECT_EQ(MallocAndCheckBlobData(outSharedKey, outSharedKey->size).errorCode,
(int32_t)OH_HUKS_SUCCESS)
<< "Malloc sharedKey failed.";
ret = HuksAgreeKey(params->agreeParamSet, params->callerKeyAlias, huksPublicKey, outSharedKey);
EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS)
<< "HuksAgreeKey with huks public key and caller private key failed.";
struct OH_Huks_ParamSet *importAgreeKeyParams = nullptr;
ret = InitParamSet(&importAgreeKeyParams, g_importAgreeKeyParams,
sizeof(g_importAgreeKeyParams) / sizeof(OH_Huks_Param));
EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "InitParamSet(importAgreeKey) failed.";
ret = OH_Huks_ImportKeyItem(params->callerAgreeKeyAlias, importAgreeKeyParams, outSharedKey);
EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "import agree shared key failed.";
OH_Huks_FreeParamSet(&importAgreeKeyParams);
}
static void EncryptImportedPlainKeyAndKek(const struct HksImportWrappedKeyTestParams *params,
struct OH_Huks_Blob *plainCipherText, struct OH_Huks_Blob *kekCipherText)
{
struct OH_Huks_ParamSet *encryptParamSet = nullptr;
OH_Huks_Result ret = InitParamSet(&encryptParamSet, g_aesKekEncryptParams,
sizeof(g_aesKekEncryptParams) / sizeof(OH_Huks_Param));
EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "InitParamSet(aesKekEnc) failed.";
ret = HuksEncrypt(params->callerKekAlias, encryptParamSet, params->importedPlainKey, plainCipherText);
EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "HuksEncrypt plain key to be imported failed.";
ret = HuksEncrypt(params->callerAgreeKeyAlias, encryptParamSet, params->callerKek, kekCipherText);
EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Kek encrypt failed.";
OH_Huks_FreeParamSet(&encryptParamSet);
}
static void ImportWrappedKey(const struct HksImportWrappedKeyTestParams *params, struct OH_Huks_Blob *plainCipher,
struct OH_Huks_Blob *kekCipherText, struct OH_Huks_Blob *peerPublicKey, struct OH_Huks_Blob *wrappedKeyData)
{
struct OH_Huks_Blob commonAad = {.size = Unittest::ImportWrapped::AAD_SIZE,
.data = reinterpret_cast<uint8_t *>(Unittest::ImportWrapped::AAD)};
struct OH_Huks_Blob commonNonce = {.size = Unittest::ImportWrapped::NONCE_SIZE,
.data = reinterpret_cast<uint8_t *>(Unittest::ImportWrapped::NONCE)};
struct OH_Huks_Blob keyMaterialLen = {.size = sizeof(uint32_t), .data = (uint8_t *)&params->keyMaterialLen};
/* copy AEAD tag from cipher text and decrease its size */
const uint32_t tagSize = Unittest::ImportWrapped::AEAD_TAG_SIZE;
uint8_t kekTagBuf[tagSize] = {0};
struct OH_Huks_Blob kekTag = {.size = tagSize, .data = kekTagBuf};
if (memcpy_s(kekTag.data, tagSize, plainCipher->data + (plainCipher->size - tagSize), tagSize) != EOK)
{
EXPECT_EQ(OH_HUKS_ERR_CODE_ILLEGAL_ARGUMENT, EOK) << "memcpy kek tag failed.";
}
plainCipher->size -= tagSize;
/* copy AEAD tag from kek cipher text and decrease its size */
uint8_t agreeKeyTagBuf[tagSize] = {0};
struct OH_Huks_Blob agreeKeyTag = {.size = tagSize, .data = agreeKeyTagBuf};
if (memcpy_s(agreeKeyTagBuf, tagSize, kekCipherText->data + (kekCipherText->size - tagSize), tagSize) != EOK)
{
EXPECT_EQ(OH_HUKS_ERR_CODE_ILLEGAL_ARGUMENT, EOK) << "memcpy agreekey tag failed.";
}
kekCipherText->size -= tagSize;
struct OH_Huks_Blob *blobArray[] = {peerPublicKey, &commonAad, &commonNonce, &agreeKeyTag, kekCipherText,
&commonAad, &commonNonce, &kekTag, &keyMaterialLen, plainCipher};
OH_Huks_Result ret = BuildWrappedKeyData(blobArray, OH_HUKS_IMPORT_WRAPPED_KEY_TOTAL_BLOBS, wrappedKeyData);
EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "BuildWrappedKeyData failed.";
struct OH_Huks_Param *purpose = nullptr;
ret = OH_Huks_GetParam(params->importWrappedKeyParamSet, OH_HUKS_TAG_PURPOSE, &purpose);
EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Get wrapped purpose param failed.";
ret = OH_Huks_ImportWrappedKeyItem(params->importedKeyAlias, params->wrappingKeyAlias,
params->importWrappedKeyParamSet, wrappedKeyData);
if (purpose->uint32Param == (uint32_t)OH_HUKS_KEY_PURPOSE_UNWRAP)
{
EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_ERR_CODE_INVALID_CRYPTO_ALG_ARGUMENT) << "Import unwrap purpose wrapped key shouldn't be success.";
}
else
{
EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "OH_Huks_ImportWrappedKeyItem failed.";
}
}
void HksImportWrappedKeyTestCommonCase(const struct HksImportWrappedKeyTestParams *params)
{
OH_Huks_Result ret = CheckParamsValid(params);
EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "CheckParamsValid failed.";
if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS)
{
return;
}
struct OH_Huks_Blob huksPublicKey = {0, nullptr};
struct OH_Huks_Blob callerSelfPublicKey = {0, nullptr};
struct OH_Huks_Blob outSharedKey = {.size = OH_HUKS_KEY_BYTES(OH_HUKS_AES_KEY_SIZE_256), .data = nullptr};
struct OH_Huks_Blob wrappedKeyData = {0, nullptr};
uint8_t plainKeyCipherBuffer[OH_HUKS_MAX_KEY_SIZE] = {0};
struct OH_Huks_Blob plainCipherText = {OH_HUKS_MAX_KEY_SIZE, plainKeyCipherBuffer};
uint8_t kekCipherTextBuffer[OH_HUKS_MAX_KEY_SIZE] = {0};
struct OH_Huks_Blob kekCipherText = {OH_HUKS_MAX_KEY_SIZE, kekCipherTextBuffer};
GenerateAndExportHuksPublicKey(params, &huksPublicKey);
GenerateAndExportCallerPublicKey(params, &callerSelfPublicKey);
ImportKekAndAgreeSharedSecret(params, &huksPublicKey, &outSharedKey);
EncryptImportedPlainKeyAndKek(params, &plainCipherText, &kekCipherText);
ImportWrappedKey(params, &plainCipherText, &kekCipherText, &callerSelfPublicKey, &wrappedKeyData);
HUKS_FREE_BLOB(huksPublicKey);
HUKS_FREE_BLOB(callerSelfPublicKey);
HUKS_FREE_BLOB(outSharedKey);
HUKS_FREE_BLOB(wrappedKeyData);
}
void HksClearKeysForWrappedKeyTest(const struct HksImportWrappedKeyTestParams *params)
{
OH_Huks_Result ret = CheckParamsValid(params);
EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "CheckParamsValid failed.";
if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS)
{
return;
}
(void)OH_Huks_DeleteKeyItem(params->wrappingKeyAlias, nullptr);
(void)OH_Huks_DeleteKeyItem(params->callerKeyAlias, nullptr);
(void)OH_Huks_DeleteKeyItem(params->callerKekAlias, nullptr);
(void)OH_Huks_DeleteKeyItem(params->callerAgreeKeyAlias, nullptr);
(void)OH_Huks_DeleteKeyItem(params->importedKeyAlias, nullptr);
}
}
......@@ -19,6 +19,10 @@ import { stringToUint8Array } from '../../../../../../utils/param/publicFunc';
import { publicSignVerifyFunc } from '../../../../../../utils/param/signverify/publicSignverifyPromise.js';
let srcData63 = Data.Data63b;
let srcData63Kb = stringToUint8Array(srcData63);
let srcData63AfterSha256 = new Uint8Array([
0xD2, 0xE1, 0x24, 0x8F, 0x0D, 0xBB, 0x2F, 0x98, 0x64, 0x13, 0x27, 0xE0, 0x0D, 0xEA, 0x34, 0xBB,
0x1D, 0x1C, 0xC2, 0xA1, 0xBE, 0x10, 0xF0, 0xB1, 0x6F, 0x8E, 0xDF, 0x9B, 0x42, 0xBA, 0x38, 0x5D,
]);
export default function SecurityHuksECCBasicAbort63KBCallbackJsunit() {
describe('SecurityHuksECCBasicAbort63KBCallbackJsunit', function () {
it('testSignVerifyECC002', 0, async function (done) {
......@@ -30,9 +34,9 @@ describe('SecurityHuksECCBasicAbort63KBCallbackJsunit', function () {
HuksSignVerifyECC.HuksTagECCDigestNONE,
HuksSignVerifyECC.HuksKeyECCSize224
),
inData: srcData63Kb,
inData: srcData63AfterSha256,
};
await publicSignVerifyFunc(srcKeyAlies, HuksOptions, 'abort', true, srcData63Kb);
await publicSignVerifyFunc(srcKeyAlies, HuksOptions, 'abort', true, srcData63AfterSha256);
done();
});
});
......
......@@ -19,6 +19,10 @@ import { stringToUint8Array } from '../../../../../../utils/param/publicFunc';
import { publicSignVerifyFunc } from '../../../../../../utils/param/signverify/publicSignverifyPromise.js';
let srcData65 = Data.Data65b;
let srcData65Kb = stringToUint8Array(srcData65);
const srcData65Sha256 = new Uint8Array([
0x23, 0xF8, 0x94, 0xEA, 0xEE, 0x1C, 0x70, 0xA9, 0x33, 0x82, 0x6C, 0xCF, 0xEF, 0x2D, 0x41, 0x6F,
0x64, 0xF6, 0x46, 0xD4, 0xCD, 0xD4, 0x5C, 0x33, 0x84, 0x4D, 0x53, 0x4A, 0x1D, 0xF0, 0xE7, 0xDF,
]);
export default function SecurityHuksECCBasicAbort65KBCallbackJsunit() {
describe('SecurityHuksECCBasicAbort65KBCallbackJsunit', function () {
it('testSignVerifyECC004', 0, async function (done) {
......@@ -30,9 +34,9 @@ describe('SecurityHuksECCBasicAbort65KBCallbackJsunit', function () {
HuksSignVerifyECC.HuksTagECCDigestNONE,
HuksSignVerifyECC.HuksKeyECCSize224
),
inData: srcData65Kb,
inData: srcData65Sha256,
};
await publicSignVerifyFunc(srcKeyAlies, HuksOptions, 'abort', true, srcData65Kb);
await publicSignVerifyFunc(srcKeyAlies, HuksOptions, 'abort', true, srcData65Sha256);
done();
});
});
......
......@@ -20,6 +20,10 @@ import { publicSignVerifyFunc } from '../../../../../../utils/param/signverify/p
let srcData63 = Data.Data63b;
let srcData63Kb = stringToUint8Array(srcData63);
let finishOutData;
let srcData63AfterSha256 = new Uint8Array([
0xD2, 0xE1, 0x24, 0x8F, 0x0D, 0xBB, 0x2F, 0x98, 0x64, 0x13, 0x27, 0xE0, 0x0D, 0xEA, 0x34, 0xBB,
0x1D, 0x1C, 0xC2, 0xA1, 0xBE, 0x10, 0xF0, 0xB1, 0x6F, 0x8E, 0xDF, 0x9B, 0x42, 0xBA, 0x38, 0x5D,
]);
export default function SecurityHuksECCBasicFinish63KBCallbackJsunit() {
describe('SecurityHuksECCBasicFinish63KBCallbackJsunit', function () {
it('testSignVerifyECC001', 0, async function (done) {
......@@ -31,9 +35,9 @@ describe('SecurityHuksECCBasicFinish63KBCallbackJsunit', function () {
HuksSignVerifyECC.HuksTagECCDigestNONE,
HuksSignVerifyECC.HuksKeyECCSize224
),
inData: srcData63Kb,
inData: srcData63AfterSha256,
};
finishOutData = await publicSignVerifyFunc(srcKeyAlies, HuksOptions, 'finish', true, srcData63Kb);
finishOutData = await publicSignVerifyFunc(srcKeyAlies, HuksOptions, 'finish', true, srcData63AfterSha256);
HuksOptions = {
properties: new Array(
HuksSignVerifyECC.HuksKeyAlgECC,
......@@ -43,7 +47,7 @@ describe('SecurityHuksECCBasicFinish63KBCallbackJsunit', function () {
),
inData: finishOutData,
};
await publicSignVerifyFunc(srcKeyAlies, HuksOptions, 'finish', false, srcData63Kb);
await publicSignVerifyFunc(srcKeyAlies, HuksOptions, 'finish', false, srcData63AfterSha256);
done();
});
});
......
......@@ -20,6 +20,10 @@ import { publicSignVerifyFunc } from '../../../../../../utils/param/signverify/p
let srcData65 = Data.Data65b;
let srcData65Kb = stringToUint8Array(srcData65);
let finishOutData;
const srcData65Sha256 = new Uint8Array([
0x23, 0xF8, 0x94, 0xEA, 0xEE, 0x1C, 0x70, 0xA9, 0x33, 0x82, 0x6C, 0xCF, 0xEF, 0x2D, 0x41, 0x6F,
0x64, 0xF6, 0x46, 0xD4, 0xCD, 0xD4, 0x5C, 0x33, 0x84, 0x4D, 0x53, 0x4A, 0x1D, 0xF0, 0xE7, 0xDF,
]);
export default function SecurityHuksECCBasicFinish65KBCallbackJsunit() {
describe('SecurityHuksECCBasicFinish65KBCallbackJsunit', function () {
it('testSignVerifyECC003', 0, async function (done) {
......@@ -31,9 +35,9 @@ describe('SecurityHuksECCBasicFinish65KBCallbackJsunit', function () {
HuksSignVerifyECC.HuksTagECCDigestNONE,
HuksSignVerifyECC.HuksKeyECCSize224
),
inData: srcData65Kb,
inData: srcData65Sha256,
};
finishOutData = await publicSignVerifyFunc(srcKeyAlies, HuksOptions, 'finish', true, srcData65Kb);
finishOutData = await publicSignVerifyFunc(srcKeyAlies, HuksOptions, 'finish', true, srcData65Sha256);
HuksOptions = {
properties: new Array(
HuksSignVerifyECC.HuksKeyAlgECC,
......@@ -43,7 +47,7 @@ describe('SecurityHuksECCBasicFinish65KBCallbackJsunit', function () {
),
inData: finishOutData,
};
await publicSignVerifyFunc(srcKeyAlies, HuksOptions, 'finish', false, srcData65Kb);
await publicSignVerifyFunc(srcKeyAlies, HuksOptions, 'finish', false, srcData65Sha256);
done();
});
});
......
......@@ -19,6 +19,10 @@ import { stringToUint8Array } from '../../../../../../utils/param/publicFunc';
import { publicSignVerifyFunc } from '../../../../../../utils/param/signverify/publicSignverifyPromise.js';
let srcData63 = Data.Data63b;
let srcData63Kb = stringToUint8Array(srcData63);
let srcData63AfterSha256 = new Uint8Array([
0xD2, 0xE1, 0x24, 0x8F, 0x0D, 0xBB, 0x2F, 0x98, 0x64, 0x13, 0x27, 0xE0, 0x0D, 0xEA, 0x34, 0xBB,
0x1D, 0x1C, 0xC2, 0xA1, 0xBE, 0x10, 0xF0, 0xB1, 0x6F, 0x8E, 0xDF, 0x9B, 0x42, 0xBA, 0x38, 0x5D,
]);
export default function SecurityHuksECCBasicAbort63KBPromiseJsunit() {
describe('SecurityHuksECCBasicAbort63KBPromiseJsunit', function () {
it('testSignVerifyECC102', 0, async function (done) {
......@@ -30,9 +34,9 @@ describe('SecurityHuksECCBasicAbort63KBPromiseJsunit', function () {
HuksSignVerifyECC.HuksTagECCDigestNONE,
HuksSignVerifyECC.HuksKeyECCSize224
),
inData: srcData63Kb,
inData: srcData63AfterSha256,
};
await publicSignVerifyFunc(srcKeyAlies, HuksOptions, 'abort', true, srcData63Kb);
await publicSignVerifyFunc(srcKeyAlies, HuksOptions, 'abort', true, srcData63AfterSha256);
done();
});
});
......
......@@ -19,6 +19,10 @@ import { stringToUint8Array } from '../../../../../../utils/param/publicFunc';
import { publicSignVerifyFunc } from '../../../../../../utils/param/signverify/publicSignverifyPromise.js';
let srcData65 = Data.Data65b;
let srcData65Kb = stringToUint8Array(srcData65);
const srcData65Sha256 = new Uint8Array([
0x23, 0xF8, 0x94, 0xEA, 0xEE, 0x1C, 0x70, 0xA9, 0x33, 0x82, 0x6C, 0xCF, 0xEF, 0x2D, 0x41, 0x6F,
0x64, 0xF6, 0x46, 0xD4, 0xCD, 0xD4, 0x5C, 0x33, 0x84, 0x4D, 0x53, 0x4A, 0x1D, 0xF0, 0xE7, 0xDF,
]);
export default function SecurityHuksECCBasicAbort65KBPromiseJsunit() {
describe('SecurityHuksECCBasicAbort65KBPromiseJsunit', function () {
it('testSignVerifyECC104', 0, async function (done) {
......@@ -30,9 +34,9 @@ describe('SecurityHuksECCBasicAbort65KBPromiseJsunit', function () {
HuksSignVerifyECC.HuksTagECCDigestNONE,
HuksSignVerifyECC.HuksKeyECCSize224
),
inData: srcData65Kb,
inData: srcData65Sha256,
};
await publicSignVerifyFunc(srcKeyAlies, HuksOptions, 'abort', true, srcData65Kb);
await publicSignVerifyFunc(srcKeyAlies, HuksOptions, 'abort', true, srcData65Sha256);
done();
});
});
......
......@@ -20,6 +20,10 @@ import { publicSignVerifyFunc } from '../../../../../../utils/param/signverify/p
let srcData63 = Data.Data63b;
let srcData63Kb = stringToUint8Array(srcData63);
let finishOutData;
let srcData63AfterSha256 = new Uint8Array([
0xD2, 0xE1, 0x24, 0x8F, 0x0D, 0xBB, 0x2F, 0x98, 0x64, 0x13, 0x27, 0xE0, 0x0D, 0xEA, 0x34, 0xBB,
0x1D, 0x1C, 0xC2, 0xA1, 0xBE, 0x10, 0xF0, 0xB1, 0x6F, 0x8E, 0xDF, 0x9B, 0x42, 0xBA, 0x38, 0x5D,
]);
export default function SecurityHuksECCBasicFinish63KBPromiseJsunit() {
describe('SecurityHuksECCBasicFinish63KBPromiseJsunit', function () {
it('testSignVerifyECC101', 0, async function (done) {
......@@ -31,9 +35,9 @@ describe('SecurityHuksECCBasicFinish63KBPromiseJsunit', function () {
HuksSignVerifyECC.HuksTagECCDigestNONE,
HuksSignVerifyECC.HuksKeyECCSize224
),
inData: srcData63Kb,
inData: srcData63AfterSha256,
};
finishOutData = await publicSignVerifyFunc(srcKeyAlies, HuksOptions, 'finish', true, srcData63Kb);
finishOutData = await publicSignVerifyFunc(srcKeyAlies, HuksOptions, 'finish', true, srcData63AfterSha256);
HuksOptions = {
properties: new Array(
HuksSignVerifyECC.HuksKeyAlgECC,
......@@ -43,7 +47,7 @@ describe('SecurityHuksECCBasicFinish63KBPromiseJsunit', function () {
),
inData: finishOutData,
};
await publicSignVerifyFunc(srcKeyAlies, HuksOptions, 'finish', false, srcData63Kb);
await publicSignVerifyFunc(srcKeyAlies, HuksOptions, 'finish', false, srcData63AfterSha256);
done();
});
});
......
......@@ -20,6 +20,10 @@ import { publicSignVerifyFunc } from '../../../../../../utils/param/signverify/p
let srcData65 = Data.Data65b;
let srcData65Kb = stringToUint8Array(srcData65);
let finishOutData;
const srcData65Sha256 = new Uint8Array([
0x23, 0xF8, 0x94, 0xEA, 0xEE, 0x1C, 0x70, 0xA9, 0x33, 0x82, 0x6C, 0xCF, 0xEF, 0x2D, 0x41, 0x6F,
0x64, 0xF6, 0x46, 0xD4, 0xCD, 0xD4, 0x5C, 0x33, 0x84, 0x4D, 0x53, 0x4A, 0x1D, 0xF0, 0xE7, 0xDF,
]);
export default function SecurityHuksECCBasicFinish65KBPromiseJsunit() {
describe('SecurityHuksECCBasicFinish65KBPromiseJsunit', function () {
it('testSignVerifyECC103', 0, async function (done) {
......@@ -31,9 +35,9 @@ describe('SecurityHuksECCBasicFinish65KBPromiseJsunit', function () {
HuksSignVerifyECC.HuksTagECCDigestNONE,
HuksSignVerifyECC.HuksKeyECCSize224
),
inData: srcData65Kb,
inData: srcData65Sha256,
};
finishOutData = await publicSignVerifyFunc(srcKeyAlies, HuksOptions, 'finish', true, srcData65Kb);
finishOutData = await publicSignVerifyFunc(srcKeyAlies, HuksOptions, 'finish', true, srcData65Sha256);
HuksOptions = {
properties: new Array(
HuksSignVerifyECC.HuksKeyAlgECC,
......@@ -43,7 +47,7 @@ describe('SecurityHuksECCBasicFinish65KBPromiseJsunit', function () {
),
inData: finishOutData,
};
await publicSignVerifyFunc(srcKeyAlies, HuksOptions, 'finish', false, srcData65Kb);
await publicSignVerifyFunc(srcKeyAlies, HuksOptions, 'finish', false, srcData65Sha256);
done();
});
});
......
......@@ -128,42 +128,43 @@ function init(srcKeyAlies, HuksOptions) {
}
async function publicUpdateFunc(HuksOptions) {
let dateSize = 64;
let tempHuksOptionsInData = HuksOptions.inData;
let inDataArray = HuksOptions.inData;
if (uint8ArrayToString(inDataArray).length < dateSize) {
await update(handle, HuksOptions);
HuksOptions.inData = tempHuksOptionsInData;
} else {
let count = Math.floor(uint8ArrayToString(inDataArray).length / dateSize);
let remainder = uint8ArrayToString(inDataArray).length % dateSize;
for (let i = 0; i < count; i++) {
HuksOptions.inData = stringToUint8Array(
uint8ArrayToString(tempHuksOptionsInData).slice(dateSize * i, dateSize * (i + 1))
);
await update(handle, HuksOptions);
HuksOptions.inData = tempHuksOptionsInData;
}
if (remainder !== 0) {
HuksOptions.inData = stringToUint8Array(
uint8ArrayToString(tempHuksOptionsInData).slice(dateSize * count, uint8ArrayToString(inDataArray).length)
);
await update(handle, HuksOptions);
HuksOptions.inData = tempHuksOptionsInData;
}
}
}
const maxUpdateSize = 64;
const inData = HuksOptions.inData;
const lastInDataPosition = inData.length - 1;
let inDataSegSize = maxUpdateSize;
let inDataSegPosition = 0;
let isFinished = false;
let outData = [];
async function update(handle, HuksOptions) {
await updateCallback(handle, HuksOptions)
.then(async (data) => {
while (inDataSegPosition <= lastInDataPosition) {
HuksOptions.inData = new Uint8Array(
Array.from(inData).slice(inDataSegPosition, inDataSegPosition + inDataSegSize)
);
console.error(`enter promise doUpdate`);
await updateCallback(handle, HuksOptions).then(async (data) => {
console.log(`test update data ${JSON.stringify(data)}`);
outData = outData.concat(Array.from(data.outData));
expect(data.errorCode == 0).assertTrue();
})
.catch((err) => {
console.log('test update err information: ' + err);
.catch((err) => {
console.log('test update err information: ' + err);
expect(null).assertFail();
});
if (inDataSegPosition + maxUpdateSize > lastInDataPosition) {
isFinished = true;
inDataSegSize = lastInDataPosition - inDataSegPosition + 1;
console.error(`enter promise doUpdate`);
break;
}
if ((!isFinished) && (inDataSegPosition + maxUpdateSize > lastInDataPosition)) {
console.log(`update size invalid isFinished = ${isFinished}`);
console.log(`inDataSegPosition = ${inDataSegPosition}`);
console.log(`lastInDataPosition = ${lastInDataPosition}`);
expect(null).assertFail();
});
return;
}
inDataSegPosition += maxUpdateSize;
}
}
function updateCallback(handle, HuksOptions) {
......
......@@ -77,45 +77,46 @@ async function publicInitFunc(keyAlias, HuksOptions) {
}
async function publicUpdateFunc(HuksOptions) {
let dateSize = 64;
let tempHuksOptionsInData = HuksOptions.inData;
let inDataArray = HuksOptions.inData;
if (uint8ArrayToString(inDataArray).length < dateSize) {
await update(handle, HuksOptions);
HuksOptions.inData = tempHuksOptionsInData;
} else {
let count = Math.floor(uint8ArrayToString(inDataArray).length / dateSize);
let remainder = uint8ArrayToString(inDataArray).length % dateSize;
for (let i = 0; i < count; i++) {
HuksOptions.inData = stringToUint8Array(
uint8ArrayToString(tempHuksOptionsInData).slice(dateSize * i, dateSize * (i + 1))
);
await update(handle, HuksOptions);
HuksOptions.inData = tempHuksOptionsInData;
const maxUpdateSize = 64;
const inData = HuksOptions.inData;
const lastInDataPosition = inData.length - 1;
let inDataSegSize = maxUpdateSize;
let inDataSegPosition = 0;
let isFinished = false;
let outData = [];
while (inDataSegPosition <= lastInDataPosition) {
HuksOptions.inData = new Uint8Array(
Array.from(inData).slice(inDataSegPosition, inDataSegPosition + inDataSegSize)
);
console.error(`enter promise doUpdate`);
await huks.update(handle, HuksOptions)
.then((data) => {
console.log(`test update data ${JSON.stringify(data)}`);
outData = outData.concat(Array.from(data.outData));
expect(data.errorCode == 0).assertTrue();
})
.catch((err) => {
console.log('test update err information: ' + err);
expect(null).assertFail();
});
if (inDataSegPosition + maxUpdateSize > lastInDataPosition) {
isFinished = true;
inDataSegSize = lastInDataPosition - inDataSegPosition + 1;
console.error(`enter promise doUpdate`);
break;
}
if (remainder !== 0) {
HuksOptions.inData = stringToUint8Array(
uint8ArrayToString(tempHuksOptionsInData).slice(dateSize * count, uint8ArrayToString(inDataArray).length)
);
await update(handle, HuksOptions);
HuksOptions.inData = tempHuksOptionsInData;
if ((!isFinished) && (inDataSegPosition + maxUpdateSize > lastInDataPosition)) {
console.log(`update size invalid isFinished = ${isFinished}`);
console.log(`inDataSegPosition = ${inDataSegPosition}`);
console.log(`lastInDataPosition = ${lastInDataPosition}`);
expect(null).assertFail();
return;
}
inDataSegPosition += maxUpdateSize;
}
}
async function update(handle, HuksOptions) {
await huks
.update(handle, HuksOptions)
.then(async (data) => {
console.log(`test update data ${JSON.stringify(data)}`);
expect(data.errorCode == 0).assertTrue();
})
.catch((err) => {
console.log('test update err information: ' + err);
expect(null).assertFail();
});
}
async function publicFinishFunc(HuksOptions) {
await huks
.finish(handle, HuksOptions)
......
......@@ -16,16 +16,16 @@ group("security_huks_reformed_test") {
testonly = true
if (is_standard_system) {
deps = [
"huks_agree_callback_BasicTest:huksagree_callback_basic_js_test",
"huks_agree_promise_BasicTest:huksagree_promise_basic_js_test",
"huks_cipher_callback_BasicTest:hukscipher_callback_basic_js_test",
"huks_cipher_promise_BasicTest:hukscipher_promise_basic_js_test",
"huks_derive_callback_BasicTest:huksderive_callback_basic_js_test",
"huks_derive_promise_BasicTest:huksderive_promise_basic_js_test",
"huks_hmac_callback_BasicTest:hukshmac_callback_basic_js_test",
"huks_hmac_promise_BasicTest:hukshmac_promise_basic_js_test",
"huks_signverify_callback_BasicTest:hukssignverify_callback_basic_js_test",
"huks_signverify_promise_BasicTest:hukssignverify_promise_basic_js_test",
"huks_agree_callback_BasicTest:huksagree_callback_basic_js_reformed_test",
"huks_agree_promise_BasicTest:huksagree_promise_basic_js_reformed_test",
"huks_cipher_callback_BasicTest:hukscipher_callback_basic_js_reformed_test",
"huks_cipher_promise_BasicTest:hukscipher_promise_basic_js_reformed_test",
"huks_derive_callback_BasicTest:huksderive_callback_basic_js_reformed_test",
"huks_derive_promise_BasicTest:huksderive_promise_basic_js_reformed_test",
"huks_hmac_callback_BasicTest:hukshmac_callback_basic_js_reformed_test",
"huks_hmac_promise_BasicTest:hukshmac_promise_basic_js_reformed_test",
"huks_signverify_callback_BasicTest:hukssignverify_callback_basic_js_reformed_test",
"huks_signverify_promise_BasicTest:hukssignverify_promise_basic_js_reformed_test",
]
}
}
......@@ -12,7 +12,7 @@
# limitations under the License.
import("//test/xts/tools/build/suite.gni")
ohos_js_hap_suite("huksagree_callback_basic_js_test") {
ohos_js_hap_suite("huksagree_callback_basic_js_reformed_test") {
hap_profile = "./src/main/config.json"
deps = [
":huks_js_assets",
......
......@@ -12,7 +12,7 @@
# limitations under the License.
import("//test/xts/tools/build/suite.gni")
ohos_js_hap_suite("huksagree_promise_basic_js_test") {
ohos_js_hap_suite("huksagree_promise_basic_js_reformed_test") {
hap_profile = "./src/main/config.json"
deps = [
":huks_js_assets",
......
......@@ -12,7 +12,7 @@
# limitations under the License.
import("//test/xts/tools/build/suite.gni")
ohos_js_hap_suite("hukscipher_callback_basic_js_test") {
ohos_js_hap_suite("hukscipher_callback_basic_js_reformed_test") {
hap_profile = "./src/main/config.json"
deps = [
":huks_js_assets",
......
......@@ -12,7 +12,7 @@
# limitations under the License.
import("//test/xts/tools/build/suite.gni")
ohos_js_hap_suite("hukscipher_promise_basic_js_test") {
ohos_js_hap_suite("hukscipher_promise_basic_js_reformed_test") {
hap_profile = "./src/main/config.json"
deps = [
":huks_js_assets",
......
......@@ -12,7 +12,7 @@
# limitations under the License.
import("//test/xts/tools/build/suite.gni")
ohos_js_hap_suite("huksderive_callback_basic_js_test") {
ohos_js_hap_suite("huksderive_callback_basic_js_reformed_test") {
hap_profile = "./src/main/config.json"
deps = [
":huks_js_assets",
......
......@@ -12,7 +12,7 @@
# limitations under the License.
import("//test/xts/tools/build/suite.gni")
ohos_js_hap_suite("huksderive_promise_basic_js_test") {
ohos_js_hap_suite("huksderive_promise_basic_js_reformed_test") {
hap_profile = "./src/main/config.json"
deps = [
":huks_js_assets",
......
......@@ -12,7 +12,7 @@
# limitations under the License.
import("//test/xts/tools/build/suite.gni")
ohos_js_hap_suite("hukshmac_callback_basic_js_test") {
ohos_js_hap_suite("hukshmac_callback_basic_js_reformed_test") {
hap_profile = "./src/main/config.json"
deps = [
":huks_js_assets",
......
......@@ -12,7 +12,7 @@
# limitations under the License.
import("//test/xts/tools/build/suite.gni")
ohos_js_hap_suite("hukshmac_promise_basic_js_test") {
ohos_js_hap_suite("hukshmac_promise_basic_js_reformed_test") {
hap_profile = "./src/main/config.json"
deps = [
":huks_js_assets",
......
......@@ -12,7 +12,7 @@
# limitations under the License.
import("//test/xts/tools/build/suite.gni")
ohos_js_hap_suite("hukssignverify_callback_basic_js_test") {
ohos_js_hap_suite("hukssignverify_callback_basic_js_reformed_test") {
hap_profile = "./src/main/config.json"
deps = [
":huks_js_assets",
......
......@@ -20,31 +20,35 @@ import { publicSignVerifyFunc } from '../../../../../../utils/param/signverify/p
let srcData63 = Data.Data63b;
let srcData63Kb = stringToUint8Array(srcData63);
let finishOutData;
let srcData63AfterSha256 = new Uint8Array([
0xD2, 0xE1, 0x24, 0x8F, 0x0D, 0xBB, 0x2F, 0x98, 0x64, 0x13, 0x27, 0xE0, 0x0D, 0xEA, 0x34, 0xBB,
0x1D, 0x1C, 0xC2, 0xA1, 0xBE, 0x10, 0xF0, 0xB1, 0x6F, 0x8E, 0xDF, 0x9B, 0x42, 0xBA, 0x38, 0x5D,
]);
export default function SecurityHuksECCBasicFinish63KBCallbackJsunit() {
describe('SecurityHuksECCBasicFinish63KBCallbackJsunit', function () {
it('testReformedSignVerifyECC001', 0, async function (done) {
const srcKeyAlies = 'testSignVerifyECCSize224SIGNNONEKeyAlias001';
let HuksOptions = {
properties: new Array(
HuksSignVerifyECC.HuksKeyAlgECC,
HuksSignVerifyECC.HuksKeyECCPurposeSIGN,
HuksSignVerifyECC.HuksTagECCDigestNONE,
HuksSignVerifyECC.HuksKeyECCSize224
),
inData: srcData63Kb,
};
finishOutData = await publicSignVerifyFunc(srcKeyAlies, HuksOptions, 'finish', true, srcData63Kb);
HuksOptions = {
properties: new Array(
HuksSignVerifyECC.HuksKeyAlgECC,
HuksSignVerifyECC.HuksKeyECCPurposeVERIFY,
HuksSignVerifyECC.HuksTagECCDigestNONE,
HuksSignVerifyECC.HuksKeyECCSize224
),
inData: finishOutData,
};
await publicSignVerifyFunc(srcKeyAlies, HuksOptions, 'finish', false, srcData63Kb);
done();
describe('SecurityHuksECCBasicFinish63KBCallbackJsunit', function () {
it('testReformedSignVerifyECC001', 0, async function (done) {
const srcKeyAlies = 'testSignVerifyECCSize224SIGNNONEKeyAlias001';
let HuksOptions = {
properties: new Array(
HuksSignVerifyECC.HuksKeyAlgECC,
HuksSignVerifyECC.HuksKeyECCPurposeSIGN,
HuksSignVerifyECC.HuksTagECCDigestNONE,
HuksSignVerifyECC.HuksKeyECCSize224
),
inData: srcData63AfterSha256,
};
finishOutData = await publicSignVerifyFunc(srcKeyAlies, HuksOptions, 'finish', true, srcData63AfterSha256);
HuksOptions = {
properties: new Array(
HuksSignVerifyECC.HuksKeyAlgECC,
HuksSignVerifyECC.HuksKeyECCPurposeVERIFY,
HuksSignVerifyECC.HuksTagECCDigestNONE,
HuksSignVerifyECC.HuksKeyECCSize224
),
inData: finishOutData,
};
await publicSignVerifyFunc(srcKeyAlies, HuksOptions, 'finish', false, srcData63AfterSha256);
done();
});
});
});
}
......@@ -18,33 +18,36 @@ import { HuksSignVerifyECC } from '../../../../../../utils/param/signverify/publ
import { stringToUint8Array } from '../../../../../../utils/param/publicFunc';
import { publicSignVerifyFunc } from '../../../../../../utils/param/signverify/publicSignverifyPromise.js';
let srcData65 = Data.Data65b;
let srcData65Kb = stringToUint8Array(srcData65);
const srcData65Sha256 = new Uint8Array([
0x23, 0xF8, 0x94, 0xEA, 0xEE, 0x1C, 0x70, 0xA9, 0x33, 0x82, 0x6C, 0xCF, 0xEF, 0x2D, 0x41, 0x6F,
0x64, 0xF6, 0x46, 0xD4, 0xCD, 0xD4, 0x5C, 0x33, 0x84, 0x4D, 0x53, 0x4A, 0x1D, 0xF0, 0xE7, 0xDF,
]);
let finishOutData;
export default function SecurityHuksECCBasicFinish65KBCallbackJsunit() {
describe('SecurityHuksECCBasicFinish65KBCallbackJsunit', function () {
it('testReformedSignVerifyECC003', 0, async function (done) {
const srcKeyAlies = 'testSignVerifyECCSize224SIGNNONEKeyAlias003';
let HuksOptions = {
properties: new Array(
HuksSignVerifyECC.HuksKeyAlgECC,
HuksSignVerifyECC.HuksKeyECCPurposeSIGN,
HuksSignVerifyECC.HuksTagECCDigestNONE,
HuksSignVerifyECC.HuksKeyECCSize224
),
inData: srcData65Kb,
};
finishOutData = await publicSignVerifyFunc(srcKeyAlies, HuksOptions, 'finish', true, srcData65Kb);
HuksOptions = {
properties: new Array(
HuksSignVerifyECC.HuksKeyAlgECC,
HuksSignVerifyECC.HuksKeyECCPurposeVERIFY,
HuksSignVerifyECC.HuksTagECCDigestNONE,
HuksSignVerifyECC.HuksKeyECCSize224
),
inData: finishOutData,
};
await publicSignVerifyFunc(srcKeyAlies, HuksOptions, 'finish', false, srcData65Kb);
done();
describe('SecurityHuksECCBasicFinish65KBCallbackJsunit', function () {
it('testReformedSignVerifyECC003', 0, async function (done) {
const srcKeyAlies = 'testSignVerifyECCSize224SIGNNONEKeyAlias003';
let HuksOptions = {
properties: new Array(
HuksSignVerifyECC.HuksKeyAlgECC,
HuksSignVerifyECC.HuksKeyECCPurposeSIGN,
HuksSignVerifyECC.HuksTagECCDigestNONE,
HuksSignVerifyECC.HuksKeyECCSize224
),
inData: srcData65Sha256,
};
finishOutData = await publicSignVerifyFunc(srcKeyAlies, HuksOptions, 'finish', true, srcData65Sha256);
HuksOptions = {
properties: new Array(
HuksSignVerifyECC.HuksKeyAlgECC,
HuksSignVerifyECC.HuksKeyECCPurposeVERIFY,
HuksSignVerifyECC.HuksTagECCDigestNONE,
HuksSignVerifyECC.HuksKeyECCSize224
),
inData: finishOutData,
};
await publicSignVerifyFunc(srcKeyAlies, HuksOptions, 'finish', false, srcData65Sha256);
done();
});
});
});
}
......@@ -12,7 +12,7 @@
# limitations under the License.
import("//test/xts/tools/build/suite.gni")
ohos_js_hap_suite("hukssignverify_promise_basic_js_test") {
ohos_js_hap_suite("hukssignverify_promise_basic_js_reformed_test") {
hap_profile = "./src/main/config.json"
deps = [
":huks_js_assets",
......
......@@ -19,6 +19,10 @@ import { stringToUint8Array } from '../../../../../../utils/param/publicFunc';
import { publicSignVerifyFunc } from '../../../../../../utils/param/signverify/publicSignverifyPromise.js';
let srcData63 = Data.Data63b;
let srcData63Kb = stringToUint8Array(srcData63);
let srcData63AfterSha256 = new Uint8Array([
0xD2, 0xE1, 0x24, 0x8F, 0x0D, 0xBB, 0x2F, 0x98, 0x64, 0x13, 0x27, 0xE0, 0x0D, 0xEA, 0x34, 0xBB,
0x1D, 0x1C, 0xC2, 0xA1, 0xBE, 0x10, 0xF0, 0xB1, 0x6F, 0x8E, 0xDF, 0x9B, 0x42, 0xBA, 0x38, 0x5D,
])
export default function SecurityHuksECCBasicAbort63KBPromiseJsunit() {
describe('SecurityHuksECCBasicAbort63KBPromiseJsunit', function () {
it('testReformedSignVerifyECC102', 0, async function (done) {
......@@ -30,9 +34,9 @@ describe('SecurityHuksECCBasicAbort63KBPromiseJsunit', function () {
HuksSignVerifyECC.HuksTagECCDigestNONE,
HuksSignVerifyECC.HuksKeyECCSize224
),
inData: srcData63Kb,
inData: srcData63AfterSha256,
};
await publicSignVerifyFunc(srcKeyAlies, HuksOptions, 'abort', true, srcData63Kb);
await publicSignVerifyFunc(srcKeyAlies, HuksOptions, 'abort', true, srcData63AfterSha256);
done();
});
});
......
......@@ -19,6 +19,10 @@ import { stringToUint8Array } from '../../../../../../utils/param/publicFunc';
import { publicSignVerifyFunc } from '../../../../../../utils/param/signverify/publicSignverifyPromise.js';
let srcData65 = Data.Data65b;
let srcData65Kb = stringToUint8Array(srcData65);
const srcData65Sha256 = new Uint8Array([
0x23, 0xF8, 0x94, 0xEA, 0xEE, 0x1C, 0x70, 0xA9, 0x33, 0x82, 0x6C, 0xCF, 0xEF, 0x2D, 0x41, 0x6F,
0x64, 0xF6, 0x46, 0xD4, 0xCD, 0xD4, 0x5C, 0x33, 0x84, 0x4D, 0x53, 0x4A, 0x1D, 0xF0, 0xE7, 0xDF,
]);
export default function SecurityHuksECCBasicAbort65KBPromiseJsunit() {
describe('SecurityHuksECCBasicAbort65KBPromiseJsunit', function () {
it('testReformedSignVerifyECC104', 0, async function (done) {
......@@ -30,9 +34,9 @@ describe('SecurityHuksECCBasicAbort65KBPromiseJsunit', function () {
HuksSignVerifyECC.HuksTagECCDigestNONE,
HuksSignVerifyECC.HuksKeyECCSize224
),
inData: srcData65Kb,
inData: srcData65Sha256,
};
await publicSignVerifyFunc(srcKeyAlies, HuksOptions, 'abort', true, srcData65Kb);
await publicSignVerifyFunc(srcKeyAlies, HuksOptions, 'abort', true, srcData65Sha256);
done();
});
});
......
......@@ -20,6 +20,10 @@ import { publicSignVerifyFunc } from '../../../../../../utils/param/signverify/p
let srcData63 = Data.Data63b;
let srcData63Kb = stringToUint8Array(srcData63);
let finishOutData;
let srcData63AfterSha256 = new Uint8Array([
0xD2, 0xE1, 0x24, 0x8F, 0x0D, 0xBB, 0x2F, 0x98, 0x64, 0x13, 0x27, 0xE0, 0x0D, 0xEA, 0x34, 0xBB,
0x1D, 0x1C, 0xC2, 0xA1, 0xBE, 0x10, 0xF0, 0xB1, 0x6F, 0x8E, 0xDF, 0x9B, 0x42, 0xBA, 0x38, 0x5D,
]);
export default function SecurityHuksECCBasicFinish63KBPromiseJsunit() {
describe('SecurityHuksECCBasicFinish63KBPromiseJsunit', function () {
it('testReformedSignVerifyECC101', 0, async function (done) {
......@@ -31,9 +35,9 @@ describe('SecurityHuksECCBasicFinish63KBPromiseJsunit', function () {
HuksSignVerifyECC.HuksTagECCDigestNONE,
HuksSignVerifyECC.HuksKeyECCSize224
),
inData: srcData63Kb,
inData: srcData63AfterSha256,
};
finishOutData = await publicSignVerifyFunc(srcKeyAlies, HuksOptions, 'finish', true, srcData63Kb);
finishOutData = await publicSignVerifyFunc(srcKeyAlies, HuksOptions, 'finish', true, srcData63AfterSha256);
HuksOptions = {
properties: new Array(
HuksSignVerifyECC.HuksKeyAlgECC,
......@@ -43,7 +47,7 @@ describe('SecurityHuksECCBasicFinish63KBPromiseJsunit', function () {
),
inData: finishOutData,
};
await publicSignVerifyFunc(srcKeyAlies, HuksOptions, 'finish', false, srcData63Kb);
await publicSignVerifyFunc(srcKeyAlies, HuksOptions, 'finish', false, srcData63AfterSha256);
done();
});
});
......
......@@ -20,6 +20,10 @@ import { publicSignVerifyFunc } from '../../../../../../utils/param/signverify/p
let srcData65 = Data.Data65b;
let srcData65Kb = stringToUint8Array(srcData65);
let finishOutData;
const srcData65Sha256 = new Uint8Array([
0x23, 0xF8, 0x94, 0xEA, 0xEE, 0x1C, 0x70, 0xA9, 0x33, 0x82, 0x6C, 0xCF, 0xEF, 0x2D, 0x41, 0x6F,
0x64, 0xF6, 0x46, 0xD4, 0xCD, 0xD4, 0x5C, 0x33, 0x84, 0x4D, 0x53, 0x4A, 0x1D, 0xF0, 0xE7, 0xDF,
]);
export default function SecurityHuksECCBasicFinish65KBPromiseJsunit() {
describe('SecurityHuksECCBasicFinish65KBPromiseJsunit', function () {
it('testReformedSignVerifyECC103', 0, async function (done) {
......@@ -31,9 +35,9 @@ describe('SecurityHuksECCBasicFinish65KBPromiseJsunit', function () {
HuksSignVerifyECC.HuksTagECCDigestNONE,
HuksSignVerifyECC.HuksKeyECCSize224
),
inData: srcData65Kb,
inData: srcData65Sha256,
};
finishOutData = await publicSignVerifyFunc(srcKeyAlies, HuksOptions, 'finish', true, srcData65Kb);
finishOutData = await publicSignVerifyFunc(srcKeyAlies, HuksOptions, 'finish', true, srcData65Sha256);
HuksOptions = {
properties: new Array(
HuksSignVerifyECC.HuksKeyAlgECC,
......@@ -43,7 +47,7 @@ describe('SecurityHuksECCBasicFinish65KBPromiseJsunit', function () {
),
inData: finishOutData,
};
await publicSignVerifyFunc(srcKeyAlies, HuksOptions, 'finish', false, srcData65Kb);
await publicSignVerifyFunc(srcKeyAlies, HuksOptions, 'finish', false, srcData65Sha256);
done();
});
});
......
......@@ -21,10 +21,10 @@ import { HuksSignVerifyRSA } from '../../../../../../utils/param/signverify/publ
let inDataString = "Hks_RSA_Sign_Verify_Test_0000000000000000000000000000000000000000000000000000000" +
"00000000000000000000000000000000000000000000000000000000000000000000000000000000" +
"0000000000000000000000000000000000000000000000000000000000000000000000000_string";
let inDataAfterSha512 = new Uint8Array(
let inDataAfterSha512 = new Uint8Array([
0x68, 0xD1, 0x5F, 0xE4, 0x46, 0xF7, 0xBF, 0x8D, 0x30, 0x91, 0x46, 0x53, 0x81, 0x49, 0xA0, 0xD5,
0x39, 0xC7, 0xDC, 0x59, 0xE7, 0xF6, 0x42, 0x5D, 0x86, 0x09, 0x30, 0xD0, 0x3F, 0x4D, 0x91, 0x85,
);
]);
let srcData64 = Data.Data64b;
let srcData64Kb = stringToUint8Array(srcData64);
let signedResult;
......
......@@ -159,46 +159,47 @@ function initSession(srcKeyAlies, HuksOptions) {
}
async function publicUpdateSessionFunc(HuksOptions) {
let dateSize = 64;
let tempHuksOptionsInData = HuksOptions.inData;
let inDataArray = HuksOptions.inData;
if (uint8ArrayToString(inDataArray).length < dateSize) {
await publicUpdateSession(handle, HuksOptions);
HuksOptions.inData = tempHuksOptionsInData;
} else {
let count = Math.floor(uint8ArrayToString(inDataArray).length / dateSize);
let remainder = uint8ArrayToString(inDataArray).length % dateSize;
for (let i = 0; i < count; i++) {
HuksOptions.inData = stringToUint8Array(
uint8ArrayToString(tempHuksOptionsInData).slice(dateSize * i, dateSize * (i + 1))
);
await publicUpdateSession(handle, HuksOptions);
HuksOptions.inData = tempHuksOptionsInData;
const maxUpdateSize = 64;
const inData = HuksOptions.inData;
const lastInDataPosition = inData.length - 1;
let inDataSegSize = maxUpdateSize;
let inDataSegPosition = 0;
let isFinished = false;
let outData = [];
while (inDataSegPosition <= lastInDataPosition) {
HuksOptions.inData = new Uint8Array(
Array.from(inData).slice(inDataSegPosition, inDataSegPosition + inDataSegSize)
);
console.error(`enter promise doUpdate`);
try {
await updateSession(handle, HuksOptions)
.then((data) => {
console.error(`promise: doUpdate success, data = ${JSON.stringify(data)}`);
outData = outData.concat(Array.from(data.outData));
})
.catch(error => {
console.error(`promise: doUpdate failed, code: ${error.code}, msg: ${error.message}`);
expect(null).assertFail();
});
} catch (error) {
console.error(`promise: doUpdate input arg invalid, code: ${error.code}, msg: ${error.message}`);
expect(null).assertFail();
}
if (remainder !== 0) {
HuksOptions.inData = stringToUint8Array(
uint8ArrayToString(tempHuksOptionsInData).slice(dateSize * count, uint8ArrayToString(inDataArray).length)
);
await publicUpdateSession(handle, HuksOptions);
HuksOptions.inData = tempHuksOptionsInData;
if (inDataSegPosition + maxUpdateSize > lastInDataPosition) {
isFinished = true;
inDataSegSize = lastInDataPosition - inDataSegPosition + 1;
console.error(`enter promise doUpdate`);
break;
}
}
}
async function publicUpdateSession(handle, HuksOptions) {
console.info(`enter callback doUpdate`);
try {
await updateSession(handle, HuksOptions)
.then ((data) => {
console.info(`callback: doUpdate success, data = ${JSON.stringify(data)}`);
})
.catch(error => {
console.error(`callback: doUpdate failed, code: ${error.code}, msg: ${error.message}`);
expect(null).assertFail();
});
} catch (error) {
console.error(`callback: doUpdate input arg invalid, code: ${error.code}, msg: ${error.message}`);
expect(null).assertFail();
if ((!isFinished) && (inDataSegPosition + maxUpdateSize > lastInDataPosition)) {
console.log(`update size invalid isFinished = ${isFinished}`);
console.log(`inDataSegPosition = ${inDataSegPosition}`);
console.log(`lastInDataPosition = ${lastInDataPosition}`);
expect(null).assertFail();
return;
}
inDataSegPosition += maxUpdateSize;
}
}
......@@ -222,7 +223,7 @@ async function publicFinishSession(HuksOptionsFinish) {
console.info(`enter callback doFinish`);
try {
await finishSession(handle, HuksOptionsFinish)
.then ((data) => {
.then((data) => {
if (data !== null && data.outData !== null) {
finishOutData = data.outData;
}
......@@ -258,7 +259,7 @@ async function publicAbortSession(HuksOptionsAbort) {
console.info(`enter callback doAbort`);
try {
await abortSession(handle, HuksOptionsAbort)
.then ((data) => {
.then((data) => {
console.info(`callback: doAbort success, data = ${JSON.stringify(data)}`);
})
.catch(error => {
......@@ -289,7 +290,7 @@ async function publicDeleteKeyItem(KeyAlias, HuksOptions) {
console.info(`enter callback deleteKeyItem`);
try {
await deleteKeyItem(KeyAlias, HuksOptions)
.then ((data) => {
.then((data) => {
console.info(`callback: deleteKeyItem key success, data = ${JSON.stringify(data)}`);
})
.catch(error => {
......
......@@ -44,7 +44,7 @@ async function publicImportKeyFunc(keyAlias, HuksOptions) {
console.info(`enter promise importKeyItem`);
try {
await huks.importKeyItem(keyAlias, HuksOptions)
.then ((data) => {
.then((data) => {
console.info(`promise: importKeyItem success, data = ${JSON.stringify(data)}`);
})
.catch(error => {
......@@ -62,7 +62,7 @@ async function publicExportKeyFunc(keyAlias, HuksOptions) {
console.info(`enter promise export`);
try {
await huks.exportKeyItem(keyAlias, HuksOptions)
.then ((data) => {
.then((data) => {
console.info(`promise: exportKeyItem success, data = ${JSON.stringify(data)}`);
exportKey = data.outData;
})
......@@ -80,7 +80,7 @@ async function publicInitSessionFunc(keyAlias, HuksOptions) {
console.info(`enter promise doInit`);
try {
await huks.initSession(keyAlias, HuksOptions)
.then ((data) => {
.then((data) => {
console.info(`promise: doInit success, data = ${JSON.stringify(data)}`);
handle = data.handle;
})
......@@ -94,55 +94,56 @@ async function publicInitSessionFunc(keyAlias, HuksOptions) {
}
}
async function publicUpdateSessionFunc(HuksOptions){
let dateSize = 64;
let tempHuksOptionsInData = HuksOptions.inData;
let inDataArray = HuksOptions.inData;
if (uint8ArrayToString(inDataArray).length < dateSize) {
await publicUpdateSession(handle, HuksOptions);
HuksOptions.inData = tempHuksOptionsInData;
} else {
let count = Math.floor(uint8ArrayToString(inDataArray).length / dateSize);
let remainder = uint8ArrayToString(inDataArray).length % dateSize;
for (let i = 0; i < count; i++) {
HuksOptions.inData = stringToUint8Array(
uint8ArrayToString(tempHuksOptionsInData).slice(dateSize * i, dateSize * (i + 1))
);
await publicUpdateSession(handle, HuksOptions);
HuksOptions.inData = tempHuksOptionsInData;
async function publicUpdateSessionFunc(HuksOptions) {
const maxUpdateSize = 64;
const inData = HuksOptions.inData;
const lastInDataPosition = inData.length - 1;
let inDataSegSize = maxUpdateSize;
let inDataSegPosition = 0;
let isFinished = false;
let outData = [];
while (inDataSegPosition <= lastInDataPosition) {
HuksOptions.inData = new Uint8Array(
Array.from(inData).slice(inDataSegPosition, inDataSegPosition + inDataSegSize)
);
console.error(`enter promise doUpdate`);
try {
await huks.updateSession(handle, HuksOptions)
.then((data) => {
console.error(`promise: doUpdate success, data = ${JSON.stringify(data)}`);
outData = outData.concat(Array.from(data.outData));
})
.catch(error => {
console.error(`promise: doUpdate failed, code: ${error.code}, msg: ${error.message}`);
expect(null).assertFail();
});
} catch (error) {
console.error(`promise: doUpdate input arg invalid, code: ${error.code}, msg: ${error.message}`);
expect(null).assertFail();
}
if (remainder !== 0) {
HuksOptions.inData = stringToUint8Array(
uint8ArrayToString(tempHuksOptionsInData).slice(dateSize * count, uint8ArrayToString(inDataArray).length)
);
await publicUpdateSession(handle, HuksOptions);
HuksOptions.inData = tempHuksOptionsInData;
if (inDataSegPosition + maxUpdateSize > lastInDataPosition) {
isFinished = true;
inDataSegSize = lastInDataPosition - inDataSegPosition + 1;
console.error(`enter promise doUpdate`);
break;
}
}
}
async function publicUpdateSession(handle, HuksOptions) {
console.info(`enter promise doUpdate`);
try {
await huks.updateSession(handle, HuksOptions)
.then ((data) => {
console.info(`promise: doUpdate success, data = ${JSON.stringify(data)}`);
})
.catch(error => {
console.error(`promise: doUpdate failed, code: ${error.code}, msg: ${error.message}`);
expect(null).assertFail();
});
} catch (error) {
console.error(`promise: doUpdate input arg invalid, code: ${error.code}, msg: ${error.message}`);
expect(null).assertFail();
if ((!isFinished) && (inDataSegPosition + maxUpdateSize > lastInDataPosition)) {
console.log(`update size invalid isFinished = ${isFinished}`);
console.log(`inDataSegPosition = ${inDataSegPosition}`);
console.log(`lastInDataPosition = ${lastInDataPosition}`);
expect(null).assertFail();
return;
}
inDataSegPosition += maxUpdateSize;
}
}
async function publicFinishSession(HuksOptions) {
console.info(`enter promise doFinish`);
console.info(`enter promise doFinish:${JSON.stringify(HuksOptions)}`);
try {
await huks.finishSession(handle, HuksOptions)
.then ((data) => {
.then((data) => {
console.info(`promise: doFinish success, data = ${JSON.stringify(data)}`);
if (data !== null && data.outData !== null) {
finishOutData = data.outData;
......@@ -162,7 +163,7 @@ async function publicAbortSession(HuksOptions) {
console.info(`enter promise doAbort`);
try {
await huks.abortSession(handle, HuksOptions)
.then ((data) => {
.then((data) => {
console.info(`promise: doAbort success, data = ${JSON.stringify(data)}`);
})
.catch(error => {
......@@ -179,7 +180,7 @@ async function publicDeleteKeyItem(KeyAlias, HuksOptions) {
console.info(`enter promise deleteKeyItem`);
try {
await huks.deleteKeyItem(KeyAlias, HuksOptions)
.then ((data) => {
.then((data) => {
console.info(`promise: deleteKeyItem key success, data = ${JSON.stringify(data)}`);
})
.catch(error => {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册