1 /*
2 * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #define HUKS_DISABLE_LOG_AT_FILE_TO_REDUCE_ROM_SIZE
16
17 #ifdef HKS_CONFIG_FILE
18 #include HKS_CONFIG_FILE
19 #else
20 #include "hks_config.h"
21 #endif
22
23 #ifdef _CUT_AUTHENTICATE_
24 #undef HKS_SUPPORT_ECDH_C
25 #endif /* _CUT_AUTHENTICATE_ */
26
27 #ifdef HKS_SUPPORT_ECDH_C
28
29 #include "hks_mbedtls_ecdh.h"
30
31 #include <mbedtls/bignum.h>
32 #include <mbedtls/ctr_drbg.h>
33 #include <mbedtls/ecdh.h>
34 #include <mbedtls/ecp.h>
35 #include <mbedtls/entropy.h>
36 #include <securec.h>
37
38 #include "hks_log.h"
39 #include "hks_mbedtls_common.h"
40 #include "hks_mbedtls_ecc.h"
41 #include "hks_template.h"
42
43 #ifdef HKS_SUPPORT_ECDH_AGREE_KEY
EccKeyMaterialToCtx(const struct HksBlob * nativeKey,const struct HksBlob * pubKey,mbedtls_ecdh_context * ctx)44 static int32_t EccKeyMaterialToCtx(const struct HksBlob *nativeKey,
45 const struct HksBlob *pubKey, mbedtls_ecdh_context *ctx)
46 {
47 int32_t ret = HksEccKeyMaterialToPub(pubKey, &(ctx->MBEDTLS_PRIVATE(Qp)));
48 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "Ecc keyMaterial to public key failed! ret = 0x%" LOG_PUBLIC "X", ret)
49
50 ret = HksEccKeyMaterialToPri(nativeKey, &(ctx->MBEDTLS_PRIVATE(d)));
51 HKS_IF_NOT_SUCC_LOGE(ret, "Ecc keyMaterial to private key failed! ret = 0x%" LOG_PUBLIC "X", ret)
52
53 return ret;
54 }
55
HksMbedtlsEcdh(const struct HksBlob * nativeKey,const struct HksBlob * pubKey,const struct HksKeySpec * spec,struct HksBlob * sharedKey)56 int32_t HksMbedtlsEcdh(const struct HksBlob *nativeKey,
57 const struct HksBlob *pubKey, const struct HksKeySpec *spec, struct HksBlob *sharedKey)
58 {
59 int32_t ret = EccKeyCheck(pubKey);
60 HKS_IF_NOT_SUCC_RETURN(ret, ret)
61
62 mbedtls_ecp_group_id mbedtlsCurveNist = MBEDTLS_ECP_DP_NONE;
63 ret = HksMbedtlsEccGetKeyCurveNist((struct KeyMaterialEcc *)(nativeKey->data), &mbedtlsCurveNist);
64 HKS_IF_NOT_SUCC_RETURN(ret, ret)
65
66 mbedtls_ctr_drbg_context ctrDrbg;
67 mbedtls_entropy_context entropy;
68 (void)memset_s(&entropy, sizeof(mbedtls_entropy_context), 0, sizeof(mbedtls_entropy_context));
69 (void)memset_s(&ctrDrbg, sizeof(mbedtls_ctr_drbg_context), 0, sizeof(mbedtls_ctr_drbg_context));
70 ret = HksCtrDrbgSeed(&ctrDrbg, &entropy);
71 HKS_IF_NOT_SUCC_RETURN(ret, ret)
72
73 mbedtls_ecdh_context ctx;
74 (void)memset_s(&ctx, sizeof(mbedtls_ecdh_context), 0, sizeof(mbedtls_ecdh_context));
75 mbedtls_ecdh_init(&ctx);
76
77 do {
78 ret = mbedtls_ecp_group_load(&(ctx.MBEDTLS_PRIVATE(grp)), mbedtlsCurveNist);
79 if (ret != HKS_MBEDTLS_SUCCESS) {
80 HKS_LOG_E("Mbedtls ecdh load group failed! mbedtls ret = 0x%" LOG_PUBLIC "X", ret);
81 break;
82 }
83
84 ret = EccKeyMaterialToCtx(nativeKey, pubKey, &ctx);
85 HKS_IF_NOT_SUCC_BREAK(ret)
86
87 ret = mbedtls_ecdh_compute_shared(&(ctx.MBEDTLS_PRIVATE(grp)), &(ctx.MBEDTLS_PRIVATE(z)),
88 &(ctx.MBEDTLS_PRIVATE(Qp)), &(ctx.MBEDTLS_PRIVATE(d)), mbedtls_ctr_drbg_random, &ctrDrbg);
89 if (ret != HKS_MBEDTLS_SUCCESS) {
90 HKS_LOG_E("Mbedtls ecdh shared key failed! mbedtls ret = 0x%" LOG_PUBLIC "X", ret);
91 break;
92 }
93
94 const uint32_t keyByteLen = HKS_KEY_BYTES(spec->keyLen);
95 ret = mbedtls_mpi_write_binary(&(ctx.MBEDTLS_PRIVATE(z)), sharedKey->data, keyByteLen);
96 if (ret != HKS_MBEDTLS_SUCCESS) {
97 HKS_LOG_E("Mbedtls ecdh mpi write to sharedKey failed! mbedtls ret = 0x%" LOG_PUBLIC "X", ret);
98 (void)memset_s(sharedKey->data, sharedKey->size, 0, sharedKey->size);
99 break;
100 }
101 sharedKey->size = keyByteLen;
102 } while (0);
103
104 mbedtls_ecdh_free(&ctx);
105 mbedtls_ctr_drbg_free(&ctrDrbg);
106 mbedtls_entropy_free(&entropy);
107 return ret;
108 }
109 #endif /* HKS_SUPPORT_ECDH_AGREE_KEY */
110 #endif /* HKS_SUPPORT_ECDH_C */
111