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