1 /*
2  * Copyright (C) 2024 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 #include "cipher_sm2_crypto_util_openssl.h"
17 #include <stdbool.h>
18 #include <string.h>
19 #include "log.h"
20 #include "memory.h"
21 #include "openssl_adapter.h"
22 #include "openssl_common.h"
23 #include "securec.h"
24 #include "utils.h"
25 
BuildSm2Ciphertext(const Sm2CipherTextSpec * spec,struct Sm2CipherTextSt * sm2Text)26 static HcfResult BuildSm2Ciphertext(const Sm2CipherTextSpec *spec, struct Sm2CipherTextSt *sm2Text)
27 {
28     if (BigIntegerToBigNum(&(spec->xCoordinate), &(sm2Text->c1X)) != HCF_SUCCESS) {
29         LOGE("Build x failed.");
30         return HCF_ERR_CRYPTO_OPERATION;
31     }
32     if (BigIntegerToBigNum(&(spec->yCoordinate), &(sm2Text->c1Y)) != HCF_SUCCESS) {
33         LOGE("Build y failed.");
34         return HCF_ERR_CRYPTO_OPERATION;
35     }
36     if (sm2Text->c3 == NULL || sm2Text->c2 == NULL) {
37         LOGE("SM2 openssl [ASN1_OCTET_STRING_new] c3 c2 fail");
38         HcfPrintOpensslError();
39         return HCF_ERR_CRYPTO_OPERATION;
40     }
41     if (OpensslAsn1OctetStringSet(sm2Text->c3, spec->hashData.data, spec->hashData.len) != HCF_OPENSSL_SUCCESS) {
42         LOGE("SM2 openssl [ASN1_OCTET_STRING_set] c3 error");
43         HcfPrintOpensslError();
44         return HCF_ERR_CRYPTO_OPERATION;
45     }
46     if (OpensslAsn1OctetStringSet(sm2Text->c2, spec->cipherTextData.data,
47         spec->cipherTextData.len) != HCF_OPENSSL_SUCCESS) {
48         LOGE("SM2 openssl [ASN1_OCTET_STRING_set] c2 error");
49         HcfPrintOpensslError();
50         return HCF_ERR_CRYPTO_OPERATION;
51     }
52     return HCF_SUCCESS;
53 }
54 
HcfSm2SpecToAsn1(Sm2CipherTextSpec * spec,HcfBlob * output)55 HcfResult HcfSm2SpecToAsn1(Sm2CipherTextSpec *spec, HcfBlob *output)
56 {
57     struct Sm2CipherTextSt *sm2Text = OpensslSm2CipherTextNew();
58     if (sm2Text == NULL) {
59         LOGE("SM2 openssl [SM2_Ciphertext_new] failed");
60         HcfPrintOpensslError();
61         return HCF_ERR_CRYPTO_OPERATION;
62     }
63     HcfResult res = BuildSm2Ciphertext(spec, sm2Text);
64     if (res != HCF_SUCCESS) {
65         OpensslSm2CipherTextFree(sm2Text);
66         LOGE("SM2 build SM2Ciphertext fail");
67         return res;
68     }
69     unsigned char *returnData = NULL;
70     int returnDataLen = OpensslI2dSm2CipherText(sm2Text, &returnData);
71     OpensslSm2CipherTextFree(sm2Text);
72     if (returnData == NULL || returnDataLen < 0) {
73         LOGE("SM2 openssl [i2d_SM2_Ciphertext] error");
74         HcfPrintOpensslError();
75         return HCF_ERR_CRYPTO_OPERATION;
76     }
77     output->data = returnData;
78     output->len = (size_t)returnDataLen;
79     return HCF_SUCCESS;
80 }
81 
BuildSm2CiphertextSpec(struct Sm2CipherTextSt * sm2Text,Sm2CipherTextSpec * tempSpec)82 static HcfResult BuildSm2CiphertextSpec(struct Sm2CipherTextSt *sm2Text, Sm2CipherTextSpec *tempSpec)
83 {
84     if (BigNumToBigInteger(sm2Text->c1X, &(tempSpec->xCoordinate)) != HCF_SUCCESS) {
85         LOGE("BigNumToBigInteger xCoordinate failed.");
86         return HCF_ERR_CRYPTO_OPERATION;
87     }
88     if (BigNumToBigInteger(sm2Text->c1Y, &(tempSpec->yCoordinate)) != HCF_SUCCESS) {
89         LOGE("BigNumToBigInteger yCoordinate failed.");
90         return HCF_ERR_CRYPTO_OPERATION;
91     }
92     const unsigned char *c2Data = OpensslAsn1StringGet0Data(sm2Text->c2);
93     int c2Len = OpensslAsn1StringLength(sm2Text->c2);
94     if (c2Data == NULL || c2Len <= 0) {
95         LOGE("SM2 openssl [OpensslAsn1StringGet0Data] error.");
96         return HCF_ERR_CRYPTO_OPERATION;
97     }
98     const unsigned char *c3Data = OpensslAsn1StringGet0Data(sm2Text->c3);
99     int c3Len = OpensslAsn1StringLength(sm2Text->c3);
100     if (c3Data == NULL || c3Len <= 0) {
101         LOGE("SM2 openssl [OpensslAsn1StringGet0Data] error.");
102         return HCF_ERR_CRYPTO_OPERATION;
103     }
104 
105     tempSpec->cipherTextData.data = (unsigned char *)HcfMalloc(c2Len, 0);
106     if (tempSpec->cipherTextData.data == NULL) {
107         LOGE("Failed to allocate cipherTextData.data memory");
108         return HCF_ERR_MALLOC;
109     }
110     tempSpec->hashData.data = (unsigned char *)HcfMalloc(c3Len, 0);
111     if (tempSpec->hashData.data == NULL) {
112         LOGE("Failed to allocate hashData.data memory");
113         return HCF_ERR_MALLOC;
114     }
115     (void)memcpy_s(tempSpec->cipherTextData.data, c2Len, c2Data, c2Len);
116     (void)memcpy_s(tempSpec->hashData.data, c3Len, c3Data, c3Len);
117     tempSpec->cipherTextData.len = c2Len;
118     tempSpec->hashData.len = c3Len;
119     return HCF_SUCCESS;
120 }
121 
HcfAsn1ToSm2Spec(HcfBlob * input,Sm2CipherTextSpec ** returnSpec)122 HcfResult HcfAsn1ToSm2Spec(HcfBlob *input, Sm2CipherTextSpec **returnSpec)
123 {
124     struct Sm2CipherTextSt *sm2Text = OpensslD2iSm2CipherText(input->data, input->len);
125     if (sm2Text == NULL) {
126         LOGE("SM2 openssl [d2i_SM2_Ciphertext] error");
127         return HCF_ERR_CRYPTO_OPERATION;
128     }
129     Sm2CipherTextSpec *tempSpec = (Sm2CipherTextSpec *)(HcfMalloc(sizeof(Sm2CipherTextSpec), 0));
130     if (tempSpec == NULL) {
131         LOGE("Failed to allocate Sm2CipherTextSpec memory");
132         OpensslSm2CipherTextFree(sm2Text);
133         return HCF_ERR_MALLOC;
134     }
135     HcfResult res = BuildSm2CiphertextSpec(sm2Text, tempSpec);
136     if (res != HCF_SUCCESS) {
137         LOGE("SM2 build SM2Ciphertext fail");
138         DestroySm2CipherTextSpec(tempSpec);
139         OpensslSm2CipherTextFree(sm2Text);
140         return res;
141     }
142     *returnSpec = tempSpec;
143     OpensslSm2CipherTextFree(sm2Text);
144     return HCF_SUCCESS;
145 }