• 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) || x->size > INT32_MAX) {
70         return HKS_ERROR_BUFFER_TOO_SMALL;
71     }
72 
73     if (memset_s(x->data, x->size, 0, x->size) != EOK) {
74         HKS_LOG_E("memset x data failed!");
75         return HKS_ERROR_INSUFFICIENT_MEMORY;
76     }
77     if (outLen == 0) {
78         return HKS_SUCCESS;
79     }
80 
81     uint8_t *bnOutput = (uint8_t *)HksMalloc(outLen);
82     HKS_IF_NULL_LOGE_RETURN(bnOutput, HKS_ERROR_MALLOC_FAIL, "malloc fail")
83 
84     int32_t ret = HKS_SUCCESS;
85     do {
86         int32_t realOutLen = BN_bn2bin(bnX, bnOutput);
87         if (realOutLen != outLen) {
88             HKS_LOG_E("BN_bn2bin fail");
89             ret = HKS_ERROR_CRYPTO_ENGINE_ERROR;
90             break;
91         }
92 
93         int32_t i = (int32_t)(x->size - 1);
94         int32_t j = realOutLen - 1; /* realOutLen is greater than 0; x->size is no less than realOutLen */
95         for (; j >= 0; --i, --j) {  /* i is no less than j */
96             x->data[i] = bnOutput[j];
97         }
98     } while (0);
99 
100     HKS_FREE(bnOutput);
101     return ret;
102 }
103 
HksOpensslBnExpMod(struct HksBlob * x,const struct HksBlob * a,const struct HksBlob * e,const struct HksBlob * n)104 int32_t HksOpensslBnExpMod(struct HksBlob *x, const struct HksBlob *a,
105     const struct HksBlob *e, const struct HksBlob *n)
106 {
107     struct HksBnExpModParams bnParams;
108     (void)memset_s(&bnParams, sizeof(bnParams), 0, sizeof(bnParams));
109     int32_t ret = BnBuildParams(&bnParams, a, e, n);
110     HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "BnInitParams fail")
111 
112     do {
113         /* mod 0 is not supported */
114         if (BN_is_zero(bnParams.bnN)) {
115             HKS_LOG_E("not support mod 0 operation.");
116             ret = HKS_ERROR_INVALID_ARGUMENT;
117             break;
118         }
119 
120         ret = BN_mod_exp(bnParams.bnX, bnParams.bnA, bnParams.bnE, bnParams.bnN, bnParams.ctx);
121         if (ret != HKS_OPENSSL_SUCCESS) {
122             HKS_LOG_E("BN_mod_exp fail, ret = %" LOG_PUBLIC "d", ret);
123             ret = HKS_ERROR_CRYPTO_ENGINE_ERROR;
124             break;
125         }
126 
127         ret = BnExpModExport(bnParams.bnX, x);
128         HKS_IF_NOT_SUCC_LOGE_BREAK(ret, "BnExpModExport fail")
129     } while (0);
130 
131     BnFreeParams(&bnParams);
132     return ret;
133 }
134 #endif /* HKS_SUPPORT_BN_C */
135