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 "sm2_crypto_util.h"
17 #include <securec.h>
18 #include "cipher_sm2_crypto_util_openssl.h"
19 #include "log.h"
20 #include "memory.h"
21 #include "utils.h"
22
23 #define HCF_SM2_C3_LEN 32
24
25 typedef HcfResult (*HcfSm2SpecToASN1CreateFunc)(Sm2CipherTextSpec *spec, HcfBlob *output);
26
27 typedef struct {
28 char *mode;
29 HcfSm2SpecToASN1CreateFunc createFunc;
30 } HcfSm2UtilAbility;
31
32 static const HcfSm2UtilAbility CONVERT_ABILITY_SET[] = {
33 { "C1C3C2", HcfSm2SpecToAsn1 },
34 };
35
FindAbility(const char * mode)36 static HcfSm2SpecToASN1CreateFunc FindAbility(const char *mode)
37 {
38 // mode default C1C3C2
39 if (HcfStrlen(mode) == 0) {
40 return CONVERT_ABILITY_SET[0].createFunc;
41 }
42 for (uint32_t i = 0; i < sizeof(CONVERT_ABILITY_SET) / sizeof(HcfSm2UtilAbility); i++) {
43 if (strcmp(mode, CONVERT_ABILITY_SET[i].mode) == 0) {
44 return CONVERT_ABILITY_SET[i].createFunc;
45 }
46 }
47 return NULL;
48 }
49
CheckMode(const char * mode)50 static bool CheckMode(const char *mode)
51 {
52 if (HcfStrlen(mode) == 0) {
53 return true;
54 }
55 for (uint32_t i = 0; i < sizeof(CONVERT_ABILITY_SET) / sizeof(HcfSm2UtilAbility); i++) {
56 if (strcmp(mode, CONVERT_ABILITY_SET[i].mode) == 0) {
57 return true;
58 }
59 }
60 LOGE("Invalid param mode");
61 return false;
62 }
63
CheckSm2CipherTextSpec(Sm2CipherTextSpec * spec)64 static bool CheckSm2CipherTextSpec(Sm2CipherTextSpec *spec)
65 {
66 if (spec == NULL) {
67 LOGE("Spec is null");
68 return false;
69 }
70 if ((spec->xCoordinate.data == NULL) || (spec->xCoordinate.len == 0)) {
71 LOGE("Spec.xCoordinate is null");
72 return false;
73 }
74 if ((spec->yCoordinate.data == NULL) || (spec->yCoordinate.len == 0)) {
75 LOGE("Spec.yCoordinate is null");
76 return false;
77 }
78 if ((spec->hashData.data == NULL) || (spec->hashData.len == 0)) {
79 LOGE("Spec.hashData is null");
80 return false;
81 }
82 if ((spec->cipherTextData.data == NULL) || (spec->cipherTextData.len == 0)) {
83 LOGE("Spec.cipherTextData is null");
84 return false;
85 }
86 if (spec->hashData.len != HCF_SM2_C3_LEN) {
87 LOGE("Invalid param hashData");
88 return false;
89 }
90 return true;
91 }
92
HcfGenCipherTextBySpec(Sm2CipherTextSpec * spec,const char * mode,HcfBlob * output)93 HcfResult HcfGenCipherTextBySpec(Sm2CipherTextSpec *spec, const char *mode, HcfBlob *output)
94 {
95 if (!CheckMode(mode)) {
96 LOGE("Invalid param mode!");
97 return HCF_INVALID_PARAMS;
98 }
99 if (output == NULL) {
100 LOGE("Invalid param output!");
101 return HCF_INVALID_PARAMS;
102 }
103 if (!CheckSm2CipherTextSpec(spec)) {
104 LOGE("Invalid param spec!");
105 return HCF_INVALID_PARAMS;
106 }
107 HcfSm2SpecToASN1CreateFunc createFunc = FindAbility(mode);
108 HcfResult res = createFunc(spec, output);
109 if (res != HCF_SUCCESS) {
110 LOGE("Failed to convert construct to asn1!");
111 }
112 return res;
113 }
114
HcfGetCipherTextSpec(HcfBlob * input,const char * mode,Sm2CipherTextSpec ** returnSpc)115 HcfResult HcfGetCipherTextSpec(HcfBlob *input, const char *mode, Sm2CipherTextSpec **returnSpc)
116 {
117 if (!CheckMode(mode)) {
118 LOGE("Invalid param mode!");
119 return HCF_INVALID_PARAMS;
120 }
121 if (input == NULL) {
122 LOGE("Invalid param input!");
123 return HCF_INVALID_PARAMS;
124 }
125 if (returnSpc == NULL) {
126 LOGE("Invalid param returnSpc!");
127 return HCF_INVALID_PARAMS;
128 }
129 HcfResult res = HcfAsn1ToSm2Spec(input, returnSpc);
130 if (res != HCF_SUCCESS) {
131 LOGE("Failed to convert asn1 to construct!");
132 return res;
133 }
134 return HCF_SUCCESS;
135 }