• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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_openssl_bn.h"
25 
26 #include <openssl/bn.h>
27 #include <stddef.h>
28 
29 #include "hks_log.h"
30 #include "hks_mem.h"
31 #include "hks_openssl_engine.h"
32 #include "hks_template.h"
33 #include "securec.h"
34 
BnFreeParams(struct HksBnExpModParams * bnParams)35 static void BnFreeParams(struct HksBnExpModParams *bnParams)
36 {
37     BN_free(bnParams->bnX);
38     BN_free(bnParams->bnA);
39     BN_free(bnParams->bnE);
40     BN_free(bnParams->bnN);
41     BN_CTX_free(bnParams->ctx);
42 
43     bnParams->bnX = NULL;
44     bnParams->bnA = NULL;
45     bnParams->bnE = NULL;
46     bnParams->bnN = NULL;
47     bnParams->ctx = NULL;
48 }
49 
BnBuildParams(struct HksBnExpModParams * bnParams,const struct HksBlob * a,const struct HksBlob * e,const struct HksBlob * n)50 static int32_t BnBuildParams(
51     struct HksBnExpModParams *bnParams, const struct HksBlob *a, const struct HksBlob *e, const struct HksBlob *n)
52 {
53     bnParams->ctx = BN_CTX_new();
54     bnParams->bnX = BN_new();
55     bnParams->bnA = BN_bin2bn(a->data, a->size, NULL);
56     bnParams->bnE = BN_bin2bn(e->data, e->size, NULL);
57     bnParams->bnN = BN_bin2bn(n->data, n->size, NULL);
58     if ((bnParams->ctx == NULL) || (bnParams->bnX == NULL) || (bnParams->bnA == NULL) || (bnParams->bnE == NULL) ||
59         (bnParams->bnN == NULL)) {
60         BnFreeParams(bnParams);
61         return HKS_ERROR_CRYPTO_ENGINE_ERROR;
62     }
63     return HKS_SUCCESS;
64 }
65 
BnExpModExport(BIGNUM * bnX,struct HksBlob * x)66 static int32_t BnExpModExport(BIGNUM *bnX, struct HksBlob *x)
67 {
68     int32_t outLen = BN_num_bytes(bnX);
69     if ((outLen < 0) || (x->size < (uint32_t)outLen)) {
70         return HKS_ERROR_BUFFER_TOO_SMALL;
71     }
72 
73     (void)memset_s(x->data, x->size, 0, x->size);
74     if (outLen == 0) {
75         return HKS_SUCCESS;
76     }
77 
78     uint8_t *bnOutput = (uint8_t *)HksMalloc(outLen);
79     HKS_IF_NULL_LOGE_RETURN(bnOutput, HKS_ERROR_MALLOC_FAIL, "malloc fail")
80 
81     int32_t ret = HKS_SUCCESS;
82     do {
83         int32_t realOutLen = BN_bn2bin(bnX, bnOutput);
84         if (realOutLen != outLen) {
85             HKS_LOG_E("BN_bn2bin fail");
86             ret = HKS_ERROR_CRYPTO_ENGINE_ERROR;
87             break;
88         }
89 
90         int32_t i = (int32_t)(x->size - 1);
91         int32_t j = realOutLen - 1; /* realOutLen is greater than 0; x->size is no less than realOutLen */
92         for (; j >= 0; --i, --j) {  /* i is no less than j */
93             x->data[i] = bnOutput[j];
94         }
95     } while (0);
96 
97     HksFree(bnOutput);
98     return ret;
99 }
100 
HksOpensslBnExpMod(struct HksBlob * x,const struct HksBlob * a,const struct HksBlob * e,const struct HksBlob * n)101 int32_t HksOpensslBnExpMod(struct HksBlob *x, const struct HksBlob *a,
102     const struct HksBlob *e, const struct HksBlob *n)
103 {
104     struct HksBnExpModParams bnParams;
105     (void)memset_s(&bnParams, sizeof(bnParams), 0, sizeof(bnParams));
106     int32_t ret = BnBuildParams(&bnParams, a, e, n);
107     HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "BnInitParams fail")
108 
109     do {
110         /* mod 0 is not supported */
111         if (BN_is_zero(bnParams.bnN)) {
112             HKS_LOG_E("not support mod 0 operation.");
113             ret = HKS_ERROR_INVALID_ARGUMENT;
114             break;
115         }
116 
117         ret = BN_mod_exp(bnParams.bnX, bnParams.bnA, bnParams.bnE, bnParams.bnN, bnParams.ctx);
118         if (ret != HKS_OPENSSL_SUCCESS) {
119             HKS_LOG_E("BN_mod_exp fail, ret = %" LOG_PUBLIC "d", ret);
120             ret = HKS_ERROR_CRYPTO_ENGINE_ERROR;
121             break;
122         }
123 
124         ret = BnExpModExport(bnParams.bnX, x);
125         HKS_IF_NOT_SUCC_LOGE_BREAK(ret, "BnExpModExport fail")
126     } while (0);
127 
128     BnFreeParams(&bnParams);
129     return ret;
130 }
131 #endif /* HKS_SUPPORT_BN_C */
132