• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022-2023 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 "params_parser.h"
17 
18 #include <stdbool.h>
19 #include <stddef.h>
20 #include <string.h>
21 #include "hcf_string.h"
22 #include "log.h"
23 
24 static const HcfParaConfig PARAM_CONFIG[] = {
25     {"ECC224",       HCF_ALG_KEY_TYPE,       HCF_ALG_ECC_224},
26     {"ECC256",       HCF_ALG_KEY_TYPE,       HCF_ALG_ECC_256},
27     {"ECC384",       HCF_ALG_KEY_TYPE,       HCF_ALG_ECC_384},
28     {"ECC521",       HCF_ALG_KEY_TYPE,       HCF_ALG_ECC_521},
29 
30     {"AES128",       HCF_ALG_KEY_TYPE,       HCF_ALG_AES_128},
31     {"AES192",       HCF_ALG_KEY_TYPE,       HCF_ALG_AES_192},
32     {"AES256",       HCF_ALG_KEY_TYPE,       HCF_ALG_AES_256},
33     {"SM4_128",      HCF_ALG_KEY_TYPE,       HCF_ALG_SM4_128},
34     {"3DES192",      HCF_ALG_KEY_TYPE,       HCF_ALG_3DES_192},
35 
36     {"ECB",          HCF_ALG_MODE,           HCF_ALG_MODE_ECB},
37     {"CBC",          HCF_ALG_MODE,           HCF_ALG_MODE_CBC},
38     {"CTR",          HCF_ALG_MODE,           HCF_ALG_MODE_CTR},
39     {"OFB",          HCF_ALG_MODE,           HCF_ALG_MODE_OFB},
40     {"CFB",          HCF_ALG_MODE,           HCF_ALG_MODE_CFB},
41     {"CFB1",         HCF_ALG_MODE,           HCF_ALG_MODE_CFB1},
42     {"CFB8",         HCF_ALG_MODE,           HCF_ALG_MODE_CFB8},
43     {"CFB64",        HCF_ALG_MODE,           HCF_ALG_MODE_CFB64},
44     {"CFB128",       HCF_ALG_MODE,           HCF_ALG_MODE_CFB128},
45     {"CCM",          HCF_ALG_MODE,           HCF_ALG_MODE_CCM},
46     {"GCM",          HCF_ALG_MODE,           HCF_ALG_MODE_GCM},
47 
48     {"NoPadding",    HCF_ALG_PADDING_TYPE,   HCF_ALG_NOPADDING},
49     {"PKCS5",        HCF_ALG_PADDING_TYPE,   HCF_ALG_PADDING_PKCS5},
50     {"PKCS7",        HCF_ALG_PADDING_TYPE,   HCF_ALG_PADDING_PKCS7},
51 
52     {"RSA512",        HCF_ALG_KEY_TYPE,       HCF_OPENSSL_RSA_512},
53     {"RSA768",        HCF_ALG_KEY_TYPE,       HCF_OPENSSL_RSA_768},
54     {"RSA1024",       HCF_ALG_KEY_TYPE,       HCF_OPENSSL_RSA_1024},
55     {"RSA2048",       HCF_ALG_KEY_TYPE,       HCF_OPENSSL_RSA_2048},
56     {"RSA3072",       HCF_ALG_KEY_TYPE,       HCF_OPENSSL_RSA_3072},
57     {"RSA4096",       HCF_ALG_KEY_TYPE,       HCF_OPENSSL_RSA_4096},
58     {"RSA8192",       HCF_ALG_KEY_TYPE,       HCF_OPENSSL_RSA_8192},
59 
60     {"PKCS1",        HCF_ALG_PADDING_TYPE,   HCF_OPENSSL_RSA_PKCS1_PADDING},
61     {"PKCS1_OAEP",   HCF_ALG_PADDING_TYPE,   HCF_OPENSSL_RSA_PKCS1_OAEP_PADDING},
62     {"PSS",          HCF_ALG_PADDING_TYPE,   HCF_OPENSSL_RSA_PSS_PADDING},
63 
64     {"NoHash",       HCF_ALG_DIGEST,         HCF_OPENSSL_DIGEST_NONE},
65     {"MD5",          HCF_ALG_DIGEST,         HCF_OPENSSL_DIGEST_MD5},
66     {"SM3",          HCF_ALG_DIGEST,         HCF_OPENSSL_DIGEST_SM3},
67     {"SHA1",         HCF_ALG_DIGEST,         HCF_OPENSSL_DIGEST_SHA1},
68     {"SHA224",       HCF_ALG_DIGEST,         HCF_OPENSSL_DIGEST_SHA224},
69     {"SHA256",       HCF_ALG_DIGEST,         HCF_OPENSSL_DIGEST_SHA256},
70     {"SHA384",       HCF_ALG_DIGEST,         HCF_OPENSSL_DIGEST_SHA384},
71     {"SHA512",       HCF_ALG_DIGEST,         HCF_OPENSSL_DIGEST_SHA512},
72 
73     {"MGF1_MD5",          HCF_ALG_MGF1_DIGEST,         HCF_OPENSSL_DIGEST_MD5},
74     {"MGF1_SHA1",         HCF_ALG_MGF1_DIGEST,         HCF_OPENSSL_DIGEST_SHA1},
75     {"MGF1_SHA224",       HCF_ALG_MGF1_DIGEST,         HCF_OPENSSL_DIGEST_SHA224},
76     {"MGF1_SHA256",       HCF_ALG_MGF1_DIGEST,         HCF_OPENSSL_DIGEST_SHA256},
77     {"MGF1_SHA384",       HCF_ALG_MGF1_DIGEST,         HCF_OPENSSL_DIGEST_SHA384},
78     {"MGF1_SHA512",       HCF_ALG_MGF1_DIGEST,         HCF_OPENSSL_DIGEST_SHA512},
79 
80     {"PRIMES_2",          HCF_ALG_PRIMES,              HCF_OPENSSL_PRIMES_2},
81     {"PRIMES_3",          HCF_ALG_PRIMES,              HCF_OPENSSL_PRIMES_3},
82     {"PRIMES_4",          HCF_ALG_PRIMES,              HCF_OPENSSL_PRIMES_4},
83     {"PRIMES_5",          HCF_ALG_PRIMES,              HCF_OPENSSL_PRIMES_5},
84 
85     {"DSA1024",       HCF_ALG_KEY_TYPE,       HCF_ALG_DSA_1024},
86     {"DSA2048",       HCF_ALG_KEY_TYPE,       HCF_ALG_DSA_2048},
87     {"DSA3072",       HCF_ALG_KEY_TYPE,       HCF_ALG_DSA_3072},
88 
89     {"SM2_256",       HCF_ALG_KEY_TYPE,       HCF_ALG_SM2_256},
90 
91     {"RSA",       HCF_ALG_TYPE,       HCF_ALG_RSA_DEFAULT},
92     {"DSA",       HCF_ALG_TYPE,       HCF_ALG_DSA_DEFAULT},
93     {"ECC",       HCF_ALG_TYPE,       HCF_ALG_ECC_DEFAULT},
94     {"SM2",       HCF_ALG_TYPE,       HCF_ALG_SM2_DEFAULT},
95     {"AES",       HCF_ALG_TYPE,       HCF_ALG_AES_DEFAULT},
96     {"SM4",       HCF_ALG_TYPE,       HCF_ALG_SM4_DEFAULT},
97     {"3DES",      HCF_ALG_TYPE,       HCF_ALG_3DES_DEFAULT},
98 
99     {"C1C3C2",    HCF_ALG_TEXT_FORMAT, HCF_ALG_TEXT_FORMAT_C1C3C2},
100     {"C1C2C3",    HCF_ALG_TEXT_FORMAT, HCF_ALG_TEXT_FORMAT_C1C2C3},
101 };
102 
103 static const HcfAlgMap ALG_MAP[] = {
104     {"DSA", HCF_ALG_DSA},
105     {"RSA", HCF_ALG_RSA},
106     {"ECC", HCF_ALG_ECC},
107     {"SM2", HCF_ALG_SM2},
108 };
109 
FindConfig(const HcString * tag)110 static const HcfParaConfig *FindConfig(const HcString* tag)
111 {
112     if (tag == NULL) {
113         return NULL;
114     }
115 
116     for (uint32_t i = 0; i < sizeof(PARAM_CONFIG) / sizeof(HcfParaConfig); ++i) {
117         if (StringCompare(tag, PARAM_CONFIG[i].tag) == 0) {
118             return &PARAM_CONFIG[i];
119         }
120     }
121     return NULL;
122 }
123 
ParseAndSetParameter(const char * paramsStr,void * params,SetParameterFunc setFunc)124 HcfResult ParseAndSetParameter(const char *paramsStr, void *params, SetParameterFunc setFunc)
125 {
126     if (paramsStr == NULL || setFunc == NULL) {
127         return HCF_INVALID_PARAMS;
128     }
129     HcString str = CreateString();
130     HcString subStr = CreateString();
131     if (!StringSetPointer(&str, paramsStr)) {
132         DeleteString(&subStr);
133         DeleteString(&str);
134         return HCF_INVALID_PARAMS;
135     }
136     HcfResult ret = HCF_INVALID_PARAMS;
137     uint32_t pos = 0;
138     do {
139         int findPos = StringFind(&str, '|', pos);
140         if (findPos >= 0) {
141             if (!StringSubString(&str, pos, findPos - pos, &subStr)) {
142                 LOGE("StringSubString failed!");
143                 break;
144             }
145             ret = (*setFunc)(FindConfig(&subStr), params);
146             if (ret != HCF_SUCCESS) {
147                 break;
148             }
149             pos = findPos + 1;
150         } else {
151             uint32_t strLen = StringLength(&str);
152             if (strLen < pos) {
153                 break;
154             }
155             if (!StringSubString(&str, pos, strLen - pos, &subStr)) {
156                 LOGE("get last string failed!");
157                 break;
158             }
159             ret = (*setFunc)(FindConfig(&subStr), params);
160             break;
161         }
162     } while (true);
163 
164     DeleteString(&subStr);
165     DeleteString(&str);
166     return ret;
167 }
168 
ParseAlgNameToParams(const char * algNameStr,HcfAsyKeyGenParams * params)169 HcfResult ParseAlgNameToParams(const char *algNameStr, HcfAsyKeyGenParams *params)
170 {
171     if (algNameStr == NULL || params == NULL) {
172         return HCF_INVALID_PARAMS;
173     }
174     for (uint32_t i = 0; i < sizeof(ALG_MAP) / sizeof(HcfAlgMap); ++i) {
175         if (strcmp(algNameStr, ALG_MAP[i].algNameStr) == 0) {
176             params->algo = ALG_MAP[i].algValue;
177             params->bits = 0;
178             return HCF_SUCCESS;
179         }
180     }
181     LOGE("Not support algorithm name: %s", algNameStr);
182     return HCF_INVALID_PARAMS;
183 }
184