1f6603c60Sopenharmony_ci/* 2f6603c60Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 3f6603c60Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4f6603c60Sopenharmony_ci * you may not use this file except in compliance with the License. 5f6603c60Sopenharmony_ci * You may obtain a copy of the License at 6f6603c60Sopenharmony_ci * 7f6603c60Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8f6603c60Sopenharmony_ci * 9f6603c60Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10f6603c60Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11f6603c60Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12f6603c60Sopenharmony_ci * See the License for the specific language governing permissions and 13f6603c60Sopenharmony_ci * limitations under the License. 14f6603c60Sopenharmony_ci */ 15f6603c60Sopenharmony_ci 16f6603c60Sopenharmony_ci#include "hks_test_curve25519.h" 17f6603c60Sopenharmony_ci#include "hks_api.h" 18f6603c60Sopenharmony_ci#include "hks_param.h" 19f6603c60Sopenharmony_ci#include "hks_test_log.h" 20f6603c60Sopenharmony_ci#include "hks_type.h" 21f6603c60Sopenharmony_ci#include "securec.h" 22f6603c60Sopenharmony_ci 23f6603c60Sopenharmony_ci#define TEST_ALIAS_ED25519 "test_ed25519" 24f6603c60Sopenharmony_ci#define TEST_PLAIN_TEST_ED25519 "This is a plain text! Hello world and thanks for watching ED25519~" 25f6603c60Sopenharmony_ci#define TEST_CURVE_256 256 26f6603c60Sopenharmony_ci#define TEST_CURVE_512 512 27f6603c60Sopenharmony_ci 28f6603c60Sopenharmony_cistatic uint8_t g_buffer[TEST_CURVE_256]; 29f6603c60Sopenharmony_cistatic uint32_t g_bufferSize = TEST_CURVE_256; 30f6603c60Sopenharmony_cistatic uint8_t g_pubKey[TEST_CURVE_512] = {0}; 31f6603c60Sopenharmony_cistatic uint32_t g_pubKeyLen = TEST_CURVE_512; 32f6603c60Sopenharmony_ci 33f6603c60Sopenharmony_ciint32_t TestGenerateEd25519Key(struct HksBlob alias) 34f6603c60Sopenharmony_ci{ 35f6603c60Sopenharmony_ci HKS_TEST_LOG_I("Test_GenerateEd25519!\n"); 36f6603c60Sopenharmony_ci struct HksParamSet *paramSet = NULL; 37f6603c60Sopenharmony_ci int32_t ret = HksInitParamSet(¶mSet); 38f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 39f6603c60Sopenharmony_ci struct HksParam algParam = {0}; 40f6603c60Sopenharmony_ci algParam.tag = HKS_TAG_ALGORITHM; 41f6603c60Sopenharmony_ci algParam.uint32Param = HKS_ALG_ED25519; 42f6603c60Sopenharmony_ci ret = HksAddParams(paramSet, &algParam, 1); 43f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 44f6603c60Sopenharmony_ci struct HksParam keySizeParam = {0}; 45f6603c60Sopenharmony_ci keySizeParam.tag = HKS_TAG_KEY_SIZE; 46f6603c60Sopenharmony_ci keySizeParam.uint32Param = TEST_CURVE_256; 47f6603c60Sopenharmony_ci ret = HksAddParams(paramSet, &keySizeParam, 1); 48f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 49f6603c60Sopenharmony_ci struct HksParam purposeParam = {0}; 50f6603c60Sopenharmony_ci purposeParam.tag = HKS_TAG_PURPOSE; 51f6603c60Sopenharmony_ci purposeParam.uint32Param = HKS_KEY_PURPOSE_SIGN | HKS_KEY_PURPOSE_VERIFY; 52f6603c60Sopenharmony_ci ret = HksAddParams(paramSet, &purposeParam, 1); 53f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 54f6603c60Sopenharmony_ci struct HksParam digestParam = {0}; 55f6603c60Sopenharmony_ci digestParam.tag = HKS_TAG_DIGEST; 56f6603c60Sopenharmony_ci digestParam.uint32Param = HKS_DIGEST_SHA512; 57f6603c60Sopenharmony_ci ret = HksAddParams(paramSet, &digestParam, 1); 58f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 59f6603c60Sopenharmony_ci struct HksParam paddingParam = {0}; 60f6603c60Sopenharmony_ci paddingParam.tag = HKS_TAG_PADDING; 61f6603c60Sopenharmony_ci paddingParam.uint32Param = HKS_PADDING_NONE; 62f6603c60Sopenharmony_ci ret = HksAddParams(paramSet, &paddingParam, 1); 63f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 64f6603c60Sopenharmony_ci ret = HksBuildParamSet(¶mSet); 65f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 66f6603c60Sopenharmony_ci ret = HksGenerateKey(&alias, paramSet, NULL); 67f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 68f6603c60Sopenharmony_ci HksFreeParamSet(¶mSet); 69f6603c60Sopenharmony_ci return ret; 70f6603c60Sopenharmony_ci} 71f6603c60Sopenharmony_ci 72f6603c60Sopenharmony_cistatic int32_t TestSignEd25519(struct HksBlob alias) 73f6603c60Sopenharmony_ci{ 74f6603c60Sopenharmony_ci struct HksBlob msg = {strlen(TEST_PLAIN_TEST_ED25519), (uint8_t *)TEST_PLAIN_TEST_ED25519}; 75f6603c60Sopenharmony_ci 76f6603c60Sopenharmony_ci struct HksParamSet *paramSet = NULL; 77f6603c60Sopenharmony_ci int32_t ret = HksInitParamSet(¶mSet); 78f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 79f6603c60Sopenharmony_ci struct HksParam algParam = {0}; 80f6603c60Sopenharmony_ci algParam.tag = HKS_TAG_ALGORITHM; 81f6603c60Sopenharmony_ci algParam.uint32Param = HKS_ALG_ED25519; 82f6603c60Sopenharmony_ci ret = HksAddParams(paramSet, &algParam, 1); 83f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 84f6603c60Sopenharmony_ci struct HksParam purposeParam = {0}; 85f6603c60Sopenharmony_ci purposeParam.tag = HKS_TAG_PURPOSE; 86f6603c60Sopenharmony_ci purposeParam.uint32Param = HKS_KEY_PURPOSE_SIGN; 87f6603c60Sopenharmony_ci ret = HksAddParams(paramSet, &purposeParam, 1); 88f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 89f6603c60Sopenharmony_ci struct HksParam digestParam = {0}; 90f6603c60Sopenharmony_ci digestParam.tag = HKS_TAG_DIGEST; 91f6603c60Sopenharmony_ci digestParam.uint32Param = HKS_DIGEST_SHA512; 92f6603c60Sopenharmony_ci ret = HksAddParams(paramSet, &digestParam, 1); 93f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 94f6603c60Sopenharmony_ci struct HksParam paddingParam = {0}; 95f6603c60Sopenharmony_ci paddingParam.tag = HKS_TAG_PADDING; 96f6603c60Sopenharmony_ci paddingParam.uint32Param = HKS_PADDING_NONE; 97f6603c60Sopenharmony_ci ret = HksAddParams(paramSet, &paddingParam, 1); 98f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 99f6603c60Sopenharmony_ci ret = HksBuildParamSet(¶mSet); 100f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 101f6603c60Sopenharmony_ci struct HksBlob signature = { TEST_CURVE_256, g_buffer }; 102f6603c60Sopenharmony_ci ret = HksSign(&alias, paramSet, &msg, &signature); 103f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 104f6603c60Sopenharmony_ci g_bufferSize = signature.size; 105f6603c60Sopenharmony_ci HKS_TEST_LOG_I("TestSignEd25519 signature size is %u", signature.size); 106f6603c60Sopenharmony_ci HksFreeParamSet(¶mSet); 107f6603c60Sopenharmony_ci return ret; 108f6603c60Sopenharmony_ci} 109f6603c60Sopenharmony_ci 110f6603c60Sopenharmony_cistatic int32_t TestVerifyEd25519(struct HksBlob alias) 111f6603c60Sopenharmony_ci{ 112f6603c60Sopenharmony_ci struct HksBlob msg = {strlen(TEST_PLAIN_TEST_ED25519), (uint8_t *)TEST_PLAIN_TEST_ED25519}; 113f6603c60Sopenharmony_ci 114f6603c60Sopenharmony_ci HKS_TEST_LOG_I("TestVerifyEd25519!\n"); 115f6603c60Sopenharmony_ci struct HksParamSet *paramSet = NULL; 116f6603c60Sopenharmony_ci int32_t ret = HksInitParamSet(¶mSet); 117f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 118f6603c60Sopenharmony_ci 119f6603c60Sopenharmony_ci struct HksParam algParam = {0}; 120f6603c60Sopenharmony_ci algParam.tag = HKS_TAG_ALGORITHM; 121f6603c60Sopenharmony_ci algParam.uint32Param = HKS_ALG_ED25519; 122f6603c60Sopenharmony_ci ret = HksAddParams(paramSet, &algParam, 1); 123f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 124f6603c60Sopenharmony_ci 125f6603c60Sopenharmony_ci struct HksParam purposeParam = {0}; 126f6603c60Sopenharmony_ci purposeParam.tag = HKS_TAG_PURPOSE; 127f6603c60Sopenharmony_ci purposeParam.uint32Param = HKS_KEY_PURPOSE_VERIFY; 128f6603c60Sopenharmony_ci ret = HksAddParams(paramSet, &purposeParam, 1); 129f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 130f6603c60Sopenharmony_ci 131f6603c60Sopenharmony_ci struct HksParam digestParam = {0}; 132f6603c60Sopenharmony_ci digestParam.tag = HKS_TAG_DIGEST; 133f6603c60Sopenharmony_ci digestParam.uint32Param = HKS_DIGEST_SHA512; 134f6603c60Sopenharmony_ci ret = HksAddParams(paramSet, &digestParam, 1); 135f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 136f6603c60Sopenharmony_ci 137f6603c60Sopenharmony_ci struct HksParam paddingParam = {0}; 138f6603c60Sopenharmony_ci paddingParam.tag = HKS_TAG_PADDING; 139f6603c60Sopenharmony_ci paddingParam.uint32Param = HKS_PADDING_NONE; 140f6603c60Sopenharmony_ci ret = HksAddParams(paramSet, &paddingParam, 1); 141f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 142f6603c60Sopenharmony_ci 143f6603c60Sopenharmony_ci ret = HksBuildParamSet(¶mSet); 144f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 145f6603c60Sopenharmony_ci 146f6603c60Sopenharmony_ci struct HksBlob signature = { g_bufferSize, g_buffer }; 147f6603c60Sopenharmony_ci ret = HksVerify(&alias, paramSet, &msg, &signature); 148f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 149f6603c60Sopenharmony_ci HksFreeParamSet(¶mSet); 150f6603c60Sopenharmony_ci return ret; 151f6603c60Sopenharmony_ci} 152f6603c60Sopenharmony_ci 153f6603c60Sopenharmony_ciint32_t TestImportEd25519(struct HksBlob alias, struct HksBlob *pubKeyInfo) 154f6603c60Sopenharmony_ci{ 155f6603c60Sopenharmony_ci HKS_TEST_LOG_I("TestImportEd25519!\n"); 156f6603c60Sopenharmony_ci struct HksParamSet *paramSet = NULL; 157f6603c60Sopenharmony_ci int32_t ret = HksInitParamSet(¶mSet); 158f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 159f6603c60Sopenharmony_ci 160f6603c60Sopenharmony_ci struct HksParam algParam = {0}; 161f6603c60Sopenharmony_ci algParam.tag = HKS_TAG_ALGORITHM; 162f6603c60Sopenharmony_ci algParam.uint32Param = HKS_ALG_ED25519; 163f6603c60Sopenharmony_ci ret = HksAddParams(paramSet, &algParam, 1); 164f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 165f6603c60Sopenharmony_ci 166f6603c60Sopenharmony_ci struct HksParam keySizeParam = {0}; 167f6603c60Sopenharmony_ci keySizeParam.tag = HKS_TAG_KEY_SIZE; 168f6603c60Sopenharmony_ci keySizeParam.uint32Param = TEST_CURVE_256; 169f6603c60Sopenharmony_ci ret = HksAddParams(paramSet, &keySizeParam, 1); 170f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 171f6603c60Sopenharmony_ci 172f6603c60Sopenharmony_ci struct HksParam purposeParam = {0}; 173f6603c60Sopenharmony_ci purposeParam.tag = HKS_TAG_PURPOSE; 174f6603c60Sopenharmony_ci purposeParam.uint32Param = HKS_KEY_PURPOSE_VERIFY; 175f6603c60Sopenharmony_ci ret = HksAddParams(paramSet, &purposeParam, 1); 176f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 177f6603c60Sopenharmony_ci 178f6603c60Sopenharmony_ci struct HksParam digestParam = {0}; 179f6603c60Sopenharmony_ci digestParam.tag = HKS_TAG_DIGEST; 180f6603c60Sopenharmony_ci digestParam.uint32Param = HKS_DIGEST_SHA512; 181f6603c60Sopenharmony_ci ret = HksAddParams(paramSet, &digestParam, 1); 182f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 183f6603c60Sopenharmony_ci 184f6603c60Sopenharmony_ci struct HksParam paddingParam = {0}; 185f6603c60Sopenharmony_ci paddingParam.tag = HKS_TAG_PADDING; 186f6603c60Sopenharmony_ci paddingParam.uint32Param = HKS_PADDING_NONE; 187f6603c60Sopenharmony_ci ret = HksAddParams(paramSet, &paddingParam, 1); 188f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 189f6603c60Sopenharmony_ci 190f6603c60Sopenharmony_ci ret = HksBuildParamSet(¶mSet); 191f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 192f6603c60Sopenharmony_ci 193f6603c60Sopenharmony_ci ret = HksImportKey(&alias, paramSet, pubKeyInfo); 194f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 195f6603c60Sopenharmony_ci HksFreeParamSet(¶mSet); 196f6603c60Sopenharmony_ci return ret; 197f6603c60Sopenharmony_ci} 198f6603c60Sopenharmony_ci 199f6603c60Sopenharmony_cistatic int32_t TestExportImportEd25519SignVerify(struct HksBlob alias) 200f6603c60Sopenharmony_ci{ 201f6603c60Sopenharmony_ci uint8_t pubKey[32] = {0}; 202f6603c60Sopenharmony_ci uint32_t pubKeyLen = 32; 203f6603c60Sopenharmony_ci struct HksBlob pubKeyInfo = { pubKeyLen, pubKey }; 204f6603c60Sopenharmony_ci int32_t ret = TestGenerateEd25519Key(alias); 205f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 206f6603c60Sopenharmony_ci 207f6603c60Sopenharmony_ci ret = HksExportPublicKey(&alias, NULL, &pubKeyInfo); 208f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 209f6603c60Sopenharmony_ci 210f6603c60Sopenharmony_ci ret = TestSignEd25519(alias); 211f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 212f6603c60Sopenharmony_ci 213f6603c60Sopenharmony_ci ret = HksDeleteKey(&alias, NULL); 214f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 215f6603c60Sopenharmony_ci 216f6603c60Sopenharmony_ci struct HksBlob newAlias = { strlen("test_ed25519_2"), (uint8_t *)"test_ed25519_2" }; 217f6603c60Sopenharmony_ci ret = TestImportEd25519(newAlias, &pubKeyInfo); 218f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 219f6603c60Sopenharmony_ci ret = TestVerifyEd25519(newAlias); 220f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 221f6603c60Sopenharmony_ci 222f6603c60Sopenharmony_ci ret = HksDeleteKey(&newAlias, NULL); 223f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 224f6603c60Sopenharmony_ci return ret; 225f6603c60Sopenharmony_ci} 226f6603c60Sopenharmony_ci 227f6603c60Sopenharmony_ciint32_t TestCurve25519All() 228f6603c60Sopenharmony_ci{ 229f6603c60Sopenharmony_ci struct HksBlob ed25519Alias = { strlen(TEST_ALIAS_ED25519), (uint8_t *)TEST_ALIAS_ED25519 }; 230f6603c60Sopenharmony_ci int32_t ret = TestGenerateEd25519Key(ed25519Alias); 231f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 232f6603c60Sopenharmony_ci ret = TestSignEd25519(ed25519Alias); 233f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 234f6603c60Sopenharmony_ci ret = TestVerifyEd25519(ed25519Alias); 235f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 236f6603c60Sopenharmony_ci ret = HksDeleteKey(&ed25519Alias, NULL); 237f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 238f6603c60Sopenharmony_ci 239f6603c60Sopenharmony_ci ret = TestExportImportEd25519SignVerify(ed25519Alias); 240f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 241f6603c60Sopenharmony_ci return ret; 242f6603c60Sopenharmony_ci} 243f6603c60Sopenharmony_ci 244f6603c60Sopenharmony_cistatic int32_t BuildTeeSignParamSet(struct HksParamSet **paramSet) 245f6603c60Sopenharmony_ci{ 246f6603c60Sopenharmony_ci int32_t ret = HksInitParamSet(paramSet); 247f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 248f6603c60Sopenharmony_ci 249f6603c60Sopenharmony_ci struct HksParam algParam = {0}; 250f6603c60Sopenharmony_ci algParam.tag = HKS_TAG_ALGORITHM; 251f6603c60Sopenharmony_ci algParam.uint32Param = HKS_ALG_ED25519; 252f6603c60Sopenharmony_ci ret = HksAddParams(*paramSet, &algParam, 1); 253f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 254f6603c60Sopenharmony_ci 255f6603c60Sopenharmony_ci struct HksParam purposeParam = {0}; 256f6603c60Sopenharmony_ci purposeParam.tag = HKS_TAG_PURPOSE; 257f6603c60Sopenharmony_ci purposeParam.uint32Param = HKS_KEY_PURPOSE_SIGN; 258f6603c60Sopenharmony_ci ret = HksAddParams(*paramSet, &purposeParam, 1); 259f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 260f6603c60Sopenharmony_ci 261f6603c60Sopenharmony_ci struct HksParam digestParam = {0}; 262f6603c60Sopenharmony_ci digestParam.tag = HKS_TAG_DIGEST; 263f6603c60Sopenharmony_ci digestParam.uint32Param = HKS_DIGEST_SHA512; 264f6603c60Sopenharmony_ci ret = HksAddParams(*paramSet, &digestParam, 1); 265f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 266f6603c60Sopenharmony_ci 267f6603c60Sopenharmony_ci struct HksParam paddingParam = {0}; 268f6603c60Sopenharmony_ci paddingParam.tag = HKS_TAG_PADDING; 269f6603c60Sopenharmony_ci paddingParam.uint32Param = HKS_PADDING_NONE; 270f6603c60Sopenharmony_ci ret = HksAddParams(*paramSet, &paddingParam, 1); 271f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 272f6603c60Sopenharmony_ci 273f6603c60Sopenharmony_ci ret = HksBuildParamSet(paramSet); 274f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 275f6603c60Sopenharmony_ci return ret; 276f6603c60Sopenharmony_ci} 277f6603c60Sopenharmony_ci 278f6603c60Sopenharmony_cistatic int32_t BuildLocalVerifyParamSet(struct HksParamSet **paramSet) 279f6603c60Sopenharmony_ci{ 280f6603c60Sopenharmony_ci int32_t ret = HksInitParamSet(paramSet); 281f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 282f6603c60Sopenharmony_ci 283f6603c60Sopenharmony_ci struct HksParam algParam = {0}; 284f6603c60Sopenharmony_ci algParam.tag = HKS_TAG_ALGORITHM; 285f6603c60Sopenharmony_ci algParam.uint32Param = HKS_ALG_ED25519; 286f6603c60Sopenharmony_ci ret = HksAddParams(*paramSet, &algParam, 1); 287f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 288f6603c60Sopenharmony_ci 289f6603c60Sopenharmony_ci struct HksParam isKeyAlias = {0}; 290f6603c60Sopenharmony_ci isKeyAlias.tag = HKS_TAG_IS_KEY_ALIAS; 291f6603c60Sopenharmony_ci isKeyAlias.boolParam = false; 292f6603c60Sopenharmony_ci ret = HksAddParams(*paramSet, &isKeyAlias, 1); 293f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 294f6603c60Sopenharmony_ci 295f6603c60Sopenharmony_ci struct HksParam keySizeParam = {0}; 296f6603c60Sopenharmony_ci keySizeParam.tag = HKS_TAG_KEY_SIZE; 297f6603c60Sopenharmony_ci keySizeParam.uint32Param = TEST_CURVE_256; 298f6603c60Sopenharmony_ci ret = HksAddParams(*paramSet, &keySizeParam, 1); 299f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 300f6603c60Sopenharmony_ci 301f6603c60Sopenharmony_ci struct HksParam purposeParam = {0}; 302f6603c60Sopenharmony_ci purposeParam.tag = HKS_TAG_PURPOSE; 303f6603c60Sopenharmony_ci purposeParam.uint32Param = HKS_KEY_PURPOSE_VERIFY; 304f6603c60Sopenharmony_ci ret = HksAddParams(*paramSet, &purposeParam, 1); 305f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 306f6603c60Sopenharmony_ci 307f6603c60Sopenharmony_ci struct HksParam digestParam = {0}; 308f6603c60Sopenharmony_ci digestParam.tag = HKS_TAG_DIGEST; 309f6603c60Sopenharmony_ci digestParam.uint32Param = HKS_DIGEST_SHA512; 310f6603c60Sopenharmony_ci ret = HksAddParams(*paramSet, &digestParam, 1); 311f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 312f6603c60Sopenharmony_ci 313f6603c60Sopenharmony_ci ret = HksBuildParamSet(paramSet); 314f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 315f6603c60Sopenharmony_ci return ret; 316f6603c60Sopenharmony_ci} 317f6603c60Sopenharmony_ci 318f6603c60Sopenharmony_ciint32_t TestEd25519SignTeeVerifyLocal() 319f6603c60Sopenharmony_ci{ 320f6603c60Sopenharmony_ci HKS_TEST_LOG_D("TestEd25519SignTeeVerifyLocal enter!"); 321f6603c60Sopenharmony_ci 322f6603c60Sopenharmony_ci struct HksBlob ed25519Alias = { strlen(TEST_ALIAS_ED25519), (uint8_t *)TEST_ALIAS_ED25519 }; 323f6603c60Sopenharmony_ci struct HksBlob msg = {strlen(TEST_PLAIN_TEST_ED25519), (uint8_t *)TEST_PLAIN_TEST_ED25519}; 324f6603c60Sopenharmony_ci struct HksBlob signature = { TEST_CURVE_256, g_buffer }; 325f6603c60Sopenharmony_ci struct HksParamSet *paramSetSign = NULL; 326f6603c60Sopenharmony_ci struct HksParamSet *paramSetVerify = NULL; 327f6603c60Sopenharmony_ci struct HksBlob pubKeyInfo = { g_pubKeyLen, g_pubKey }; 328f6603c60Sopenharmony_ci 329f6603c60Sopenharmony_ci int32_t ret = TestGenerateEd25519Key(ed25519Alias); 330f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 331f6603c60Sopenharmony_ci 332f6603c60Sopenharmony_ci ret = HksExportPublicKey(&ed25519Alias, NULL, &pubKeyInfo); 333f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 334f6603c60Sopenharmony_ci HKS_TEST_LOG_I("HksExportPublicKey puKey size is %u", pubKeyInfo.size); 335f6603c60Sopenharmony_ci 336f6603c60Sopenharmony_ci ret = BuildTeeSignParamSet(¶mSetSign); 337f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 338f6603c60Sopenharmony_ci ret = HksSign(&ed25519Alias, paramSetSign, &msg, &signature); 339f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 340f6603c60Sopenharmony_ci HKS_TEST_LOG_I("Test_Ed25519_Sign_TEE signature size is %u", signature.size); 341f6603c60Sopenharmony_ci 342f6603c60Sopenharmony_ci ret = BuildLocalVerifyParamSet(¶mSetVerify); 343f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 344f6603c60Sopenharmony_ci ret = HksVerify(&pubKeyInfo, paramSetVerify, &msg, &signature); 345f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 346f6603c60Sopenharmony_ci HKS_TEST_LOG_I("Test_Ed25519_Verify_Local Success"); 347f6603c60Sopenharmony_ci 348f6603c60Sopenharmony_ci ret = HksDeleteKey(&ed25519Alias, NULL); 349f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 350f6603c60Sopenharmony_ci 351f6603c60Sopenharmony_ci HksFreeParamSet(¶mSetSign); 352f6603c60Sopenharmony_ci HksFreeParamSet(¶mSetVerify); 353f6603c60Sopenharmony_ci HKS_TEST_LOG_D("TestEd25519SignTeeVerifyLocal End!\n"); 354f6603c60Sopenharmony_ci return ret; 355f6603c60Sopenharmony_ci} 356f6603c60Sopenharmony_ci 357f6603c60Sopenharmony_cistatic int32_t TestSignEd25519Wrong(struct HksBlob alias) 358f6603c60Sopenharmony_ci{ 359f6603c60Sopenharmony_ci struct HksBlob msg = {strlen(TEST_PLAIN_TEST_ED25519), (uint8_t *)TEST_PLAIN_TEST_ED25519}; 360f6603c60Sopenharmony_ci 361f6603c60Sopenharmony_ci struct HksParamSet *paramSet = NULL; 362f6603c60Sopenharmony_ci int32_t ret = HksInitParamSet(¶mSet); 363f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 364f6603c60Sopenharmony_ci struct HksParam algParam = {0}; 365f6603c60Sopenharmony_ci algParam.tag = HKS_TAG_ALGORITHM; 366f6603c60Sopenharmony_ci algParam.uint32Param = HKS_ALG_ED25519; 367f6603c60Sopenharmony_ci ret = HksAddParams(paramSet, &algParam, 1); 368f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 369f6603c60Sopenharmony_ci struct HksParam purposeParam = {0}; 370f6603c60Sopenharmony_ci purposeParam.tag = HKS_TAG_PURPOSE; 371f6603c60Sopenharmony_ci purposeParam.uint32Param = HKS_KEY_PURPOSE_VERIFY; 372f6603c60Sopenharmony_ci ret = HksAddParams(paramSet, &purposeParam, 1); 373f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 374f6603c60Sopenharmony_ci struct HksParam digestParam = {0}; 375f6603c60Sopenharmony_ci digestParam.tag = HKS_TAG_DIGEST; 376f6603c60Sopenharmony_ci digestParam.uint32Param = HKS_DIGEST_SHA256; 377f6603c60Sopenharmony_ci ret = HksAddParams(paramSet, &digestParam, 1); 378f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 379f6603c60Sopenharmony_ci struct HksParam paddingParam = {0}; 380f6603c60Sopenharmony_ci paddingParam.tag = HKS_TAG_PADDING; 381f6603c60Sopenharmony_ci paddingParam.uint32Param = HKS_PADDING_PKCS5; 382f6603c60Sopenharmony_ci ret = HksAddParams(paramSet, &paddingParam, 1); 383f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 384f6603c60Sopenharmony_ci ret = HksBuildParamSet(¶mSet); 385f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 386f6603c60Sopenharmony_ci struct HksBlob signature = { TEST_CURVE_256, g_buffer }; 387f6603c60Sopenharmony_ci ret = HksSign(&alias, paramSet, &msg, &signature); 388f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret != 0); 389f6603c60Sopenharmony_ci g_bufferSize = signature.size; 390f6603c60Sopenharmony_ci HKS_TEST_LOG_I("TestSignEd25519 signature size is %u", signature.size); 391f6603c60Sopenharmony_ci HksFreeParamSet(¶mSet); 392f6603c60Sopenharmony_ci return ret; 393f6603c60Sopenharmony_ci} 394f6603c60Sopenharmony_ci 395f6603c60Sopenharmony_ciint32_t TestCurve25519SignWrong() 396f6603c60Sopenharmony_ci{ 397f6603c60Sopenharmony_ci struct HksBlob ed25519Alias = { strlen(TEST_ALIAS_ED25519), (uint8_t *)TEST_ALIAS_ED25519 }; 398f6603c60Sopenharmony_ci int32_t ret = TestGenerateEd25519Key(ed25519Alias); 399f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 400f6603c60Sopenharmony_ci ret = TestSignEd25519Wrong(ed25519Alias); 401f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret != 0); 402f6603c60Sopenharmony_ci int32_t retTwo = HksDeleteKey(&ed25519Alias, NULL); 403f6603c60Sopenharmony_ci HKS_TEST_ASSERT(retTwo == 0); 404f6603c60Sopenharmony_ci if ((ret != 0) && (retTwo == 0)) { 405f6603c60Sopenharmony_ci return 0; 406f6603c60Sopenharmony_ci } 407f6603c60Sopenharmony_ci return 1; 408f6603c60Sopenharmony_ci} 409f6603c60Sopenharmony_ci 410f6603c60Sopenharmony_cistatic int32_t TestVerifyEd25519Wrong(struct HksBlob alias) 411f6603c60Sopenharmony_ci{ 412f6603c60Sopenharmony_ci struct HksBlob msg = {strlen(TEST_PLAIN_TEST_ED25519), (uint8_t *)TEST_PLAIN_TEST_ED25519}; 413f6603c60Sopenharmony_ci 414f6603c60Sopenharmony_ci HKS_TEST_LOG_I("TestVerifyEd25519!\n"); 415f6603c60Sopenharmony_ci struct HksParamSet *paramSet = NULL; 416f6603c60Sopenharmony_ci int32_t ret = HksInitParamSet(¶mSet); 417f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 418f6603c60Sopenharmony_ci 419f6603c60Sopenharmony_ci struct HksParam algParam = {0}; 420f6603c60Sopenharmony_ci algParam.tag = HKS_TAG_ALGORITHM; 421f6603c60Sopenharmony_ci algParam.uint32Param = HKS_ALG_ED25519; 422f6603c60Sopenharmony_ci ret = HksAddParams(paramSet, &algParam, 1); 423f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 424f6603c60Sopenharmony_ci 425f6603c60Sopenharmony_ci struct HksParam purposeParam = {0}; 426f6603c60Sopenharmony_ci purposeParam.tag = HKS_TAG_PURPOSE; 427f6603c60Sopenharmony_ci purposeParam.uint32Param = HKS_KEY_PURPOSE_SIGN; 428f6603c60Sopenharmony_ci ret = HksAddParams(paramSet, &purposeParam, 1); 429f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 430f6603c60Sopenharmony_ci 431f6603c60Sopenharmony_ci struct HksParam digestParam = {0}; 432f6603c60Sopenharmony_ci digestParam.tag = HKS_TAG_DIGEST; 433f6603c60Sopenharmony_ci digestParam.uint32Param = HKS_DIGEST_SHA256; 434f6603c60Sopenharmony_ci ret = HksAddParams(paramSet, &digestParam, 1); 435f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 436f6603c60Sopenharmony_ci 437f6603c60Sopenharmony_ci struct HksParam paddingParam = {0}; 438f6603c60Sopenharmony_ci paddingParam.tag = HKS_TAG_PADDING; 439f6603c60Sopenharmony_ci paddingParam.uint32Param = HKS_PADDING_PKCS5; 440f6603c60Sopenharmony_ci ret = HksAddParams(paramSet, &paddingParam, 1); 441f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 442f6603c60Sopenharmony_ci 443f6603c60Sopenharmony_ci ret = HksBuildParamSet(¶mSet); 444f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 445f6603c60Sopenharmony_ci 446f6603c60Sopenharmony_ci struct HksBlob signature = { g_bufferSize, g_buffer }; 447f6603c60Sopenharmony_ci ret = HksVerify(&alias, paramSet, &msg, &signature); 448f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret != 0); 449f6603c60Sopenharmony_ci HksFreeParamSet(¶mSet); 450f6603c60Sopenharmony_ci return ret; 451f6603c60Sopenharmony_ci} 452f6603c60Sopenharmony_ci 453f6603c60Sopenharmony_ciint32_t TestCurve25519verifyWrong() 454f6603c60Sopenharmony_ci{ 455f6603c60Sopenharmony_ci struct HksBlob ed25519Alias = { strlen(TEST_ALIAS_ED25519), (uint8_t *)TEST_ALIAS_ED25519 }; 456f6603c60Sopenharmony_ci int32_t ret = TestGenerateEd25519Key(ed25519Alias); 457f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 458f6603c60Sopenharmony_ci ret = TestSignEd25519(ed25519Alias); 459f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret == 0); 460f6603c60Sopenharmony_ci ret = TestVerifyEd25519Wrong(ed25519Alias); 461f6603c60Sopenharmony_ci HKS_TEST_ASSERT(ret != 0); 462f6603c60Sopenharmony_ci int32_t retTwo = HksDeleteKey(&ed25519Alias, NULL); 463f6603c60Sopenharmony_ci HKS_TEST_ASSERT(retTwo == 0); 464f6603c60Sopenharmony_ci if ((ret != 0) && (retTwo == 0)) { 465f6603c60Sopenharmony_ci return 0; 466f6603c60Sopenharmony_ci } 467f6603c60Sopenharmony_ci return 1; 468f6603c60Sopenharmony_ci}