1 /*
2 * Copyright (c) 2020-2022 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 HKS_SUPPORT_BN_C
24
25 #include "hks_mbedtls_bn.h"
26
27 #include <mbedtls/bignum.h>
28
29 #include "hks_log.h"
30 #include "hks_mbedtls_common.h"
31 #include "hks_template.h"
32
CheckBnExpModNx(const struct HksBlob * n,const struct HksBlob * x)33 static int32_t CheckBnExpModNx(const struct HksBlob *n, const struct HksBlob *x)
34 {
35 /* zero is even number, so doesn't have to be checked */
36 if ((n->data[n->size - 1] & 0x1) == 0x0) {
37 HKS_LOG_E("The param n(modular) must be odd!");
38 return HKS_ERROR_INVALID_ARGUMENT;
39 }
40
41 if (x->size < n->size) {
42 HKS_LOG_E("The param x's size is too samll! x size = 0x%" LOG_PUBLIC "X", x->size);
43 return HKS_ERROR_BUFFER_TOO_SMALL;
44 }
45
46 return HKS_SUCCESS;
47 }
48
HksMbedtlsBnExpMod(struct HksBlob * x,const struct HksBlob * a,const struct HksBlob * e,const struct HksBlob * n)49 int32_t HksMbedtlsBnExpMod(struct HksBlob *x, const struct HksBlob *a,
50 const struct HksBlob *e, const struct HksBlob *n)
51 {
52 int32_t ret = CheckBnExpModNx(n, x);
53 HKS_IF_NOT_SUCC_RETURN(ret, ret)
54
55 mbedtls_mpi bnX;
56 mbedtls_mpi bnA;
57 mbedtls_mpi bnE;
58 mbedtls_mpi bnN;
59
60 mbedtls_mpi_init(&bnX);
61 mbedtls_mpi_init(&bnA);
62 mbedtls_mpi_init(&bnE);
63 mbedtls_mpi_init(&bnN);
64
65 do {
66 ret = mbedtls_mpi_read_binary(&bnA, a->data, a->size);
67 if (ret != HKS_MBEDTLS_SUCCESS) {
68 HKS_LOG_E("Mbedtls mpi read a failed! mbedtls ret = 0x%" LOG_PUBLIC "X", ret);
69 break;
70 }
71 ret = mbedtls_mpi_read_binary(&bnE, e->data, e->size);
72 if (ret != HKS_MBEDTLS_SUCCESS) {
73 HKS_LOG_E("Mbedtls mpi read e failed! mbedtls ret = 0x%" LOG_PUBLIC "X", ret);
74 break;
75 }
76 ret = mbedtls_mpi_read_binary(&bnN, n->data, n->size);
77 if (ret != HKS_MBEDTLS_SUCCESS) {
78 HKS_LOG_E("Mbedtls mpi read n failed! mbedtls ret = 0x%" LOG_PUBLIC "X", ret);
79 break;
80 }
81
82 ret = mbedtls_mpi_exp_mod(&bnX, &bnA, &bnE, &bnN, NULL);
83 if (ret != HKS_MBEDTLS_SUCCESS) {
84 HKS_LOG_E("Mbedtls exp mod failed! mbedtls ret = 0x%" LOG_PUBLIC "X", ret);
85 break;
86 }
87
88 ret = mbedtls_mpi_write_binary(&bnX, x->data, x->size);
89 if (ret != HKS_MBEDTLS_SUCCESS) {
90 HKS_LOG_E("Mbedtls mpi write x failed! mbedtls ret = 0x%" LOG_PUBLIC "X", ret);
91 }
92 } while (0);
93
94 mbedtls_mpi_free(&bnX);
95 mbedtls_mpi_free(&bnA);
96 mbedtls_mpi_free(&bnE);
97 mbedtls_mpi_free(&bnN);
98 return ret;
99 }
100 #endif /* HKS_SUPPORT_BN_C */
101