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
16 #ifdef HKS_CONFIG_FILE
17 #include HKS_CONFIG_FILE
18 #else
19 #include "hks_config.h"
20 #endif
21
22 #ifdef _CUT_AUTHENTICATE_
23 #undef HKS_SUPPORT_ECDH_C
24 #endif /* _CUT_AUTHENTICATE_ */
25
26 #ifdef HKS_SUPPORT_ECDH_C
27
28 #include "hks_mbedtls_ecdh.h"
29
30 #include <mbedtls/bignum.h>
31 #include <mbedtls/ctr_drbg.h>
32 #include <mbedtls/ecdh.h>
33 #include <mbedtls/ecp.h>
34 #include <mbedtls/entropy.h>
35 #include <securec.h>
36
37 #include "hks_log.h"
38 #include "hks_mbedtls_common.h"
39 #include "hks_mbedtls_ecc.h"
40 #include "hks_template.h"
41
42 #ifdef HKS_SUPPORT_ECDH_AGREE_KEY
EccKeyMaterialToCtx(const struct HksBlob * nativeKey,const struct HksBlob * pubKey,mbedtls_ecdh_context * ctx)43 static int32_t EccKeyMaterialToCtx(const struct HksBlob *nativeKey,
44 const struct HksBlob *pubKey, mbedtls_ecdh_context *ctx)
45 {
46 int32_t ret = HksEccKeyMaterialToPub(pubKey, &(ctx->MBEDTLS_PRIVATE(Qp)));
47 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "Ecc keyMaterial to public key failed! ret = 0x%" LOG_PUBLIC "X", ret)
48
49 ret = HksEccKeyMaterialToPri(nativeKey, &(ctx->MBEDTLS_PRIVATE(d)));
50 HKS_IF_NOT_SUCC_LOGE(ret, "Ecc keyMaterial to private key failed! ret = 0x%" LOG_PUBLIC "X", ret)
51
52 return ret;
53 }
54
HksMbedtlsEcdh(const struct HksBlob * nativeKey,const struct HksBlob * pubKey,const struct HksKeySpec * spec,struct HksBlob * sharedKey)55 int32_t HksMbedtlsEcdh(const struct HksBlob *nativeKey,
56 const struct HksBlob *pubKey, const struct HksKeySpec *spec, struct HksBlob *sharedKey)
57 {
58 int32_t ret = EccKeyCheck(pubKey);
59 HKS_IF_NOT_SUCC_RETURN(ret, ret)
60
61 mbedtls_ecp_group_id mbedtlsCurveNist = MBEDTLS_ECP_DP_NONE;
62 ret = HksMbedtlsEccGetKeyCurveNist((struct KeyMaterialEcc *)(nativeKey->data), &mbedtlsCurveNist);
63 HKS_IF_NOT_SUCC_RETURN(ret, ret)
64
65 mbedtls_ctr_drbg_context ctrDrbg;
66 mbedtls_entropy_context entropy;
67 (void)memset_s(&entropy, sizeof(mbedtls_entropy_context), 0, sizeof(mbedtls_entropy_context));
68 (void)memset_s(&ctrDrbg, sizeof(mbedtls_ctr_drbg_context), 0, sizeof(mbedtls_ctr_drbg_context));
69 ret = HksCtrDrbgSeed(&ctrDrbg, &entropy);
70 HKS_IF_NOT_SUCC_RETURN(ret, ret)
71
72 mbedtls_ecdh_context ctx;
73 (void)memset_s(&ctx, sizeof(mbedtls_ecdh_context), 0, sizeof(mbedtls_ecdh_context));
74 mbedtls_ecdh_init(&ctx);
75
76 do {
77 ret = mbedtls_ecp_group_load(&(ctx.MBEDTLS_PRIVATE(grp)), mbedtlsCurveNist);
78 if (ret != HKS_MBEDTLS_SUCCESS) {
79 HKS_LOG_E("Mbedtls ecdh load group failed! mbedtls ret = 0x%" LOG_PUBLIC "X", ret);
80 break;
81 }
82
83 ret = EccKeyMaterialToCtx(nativeKey, pubKey, &ctx);
84 HKS_IF_NOT_SUCC_BREAK(ret)
85
86 ret = mbedtls_ecdh_compute_shared(&(ctx.MBEDTLS_PRIVATE(grp)), &(ctx.MBEDTLS_PRIVATE(z)),
87 &(ctx.MBEDTLS_PRIVATE(Qp)), &(ctx.MBEDTLS_PRIVATE(d)), mbedtls_ctr_drbg_random, &ctrDrbg);
88 if (ret != HKS_MBEDTLS_SUCCESS) {
89 HKS_LOG_E("Mbedtls ecdh shared key failed! mbedtls ret = 0x%" LOG_PUBLIC "X", ret);
90 break;
91 }
92
93 const uint32_t keyByteLen = HKS_KEY_BYTES(spec->keyLen);
94 ret = mbedtls_mpi_write_binary(&(ctx.MBEDTLS_PRIVATE(z)), sharedKey->data, keyByteLen);
95 if (ret != HKS_MBEDTLS_SUCCESS) {
96 HKS_LOG_E("Mbedtls ecdh mpi write to sharedKey failed! mbedtls ret = 0x%" LOG_PUBLIC "X", ret);
97 (void)memset_s(sharedKey->data, sharedKey->size, 0, sharedKey->size);
98 break;
99 }
100 sharedKey->size = keyByteLen;
101 } while (0);
102
103 mbedtls_ecdh_free(&ctx);
104 mbedtls_ctr_drbg_free(&ctrDrbg);
105 mbedtls_entropy_free(&entropy);
106 return ret;
107 }
108 #endif /* HKS_SUPPORT_ECDH_AGREE_KEY */
109 #endif /* HKS_SUPPORT_ECDH_C */
110