• 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 #ifndef HCF_PARAMS_PARSER_H
17 #define HCF_PARAMS_PARSER_H
18 
19 #include <stdint.h>
20 #include "result.h"
21 
22 typedef enum {
23     HCF_ALG_TYPE = 1,
24     HCF_ALG_KEY_TYPE,
25     HCF_ALG_MODE,
26     HCF_ALG_PADDING_TYPE,
27     HCF_ALG_PRIMES,
28     HCF_ALG_DIGEST,
29     HCF_ALG_MGF1_DIGEST,
30     HCF_ALG_TEXT_FORMAT,
31 } HcfAlgParaType;
32 
33 typedef enum {
34     HCF_ALG_AES = 1,
35     HCF_ALG_DES,
36     HCF_ALG_RSA,
37     HCF_ALG_ECC,
38     HCF_ALG_DSA,
39     HCF_ALG_SM2,
40     HCF_ALG_SM4,
41 } HcfAlgValue;
42 
43 typedef enum {
44     HCF_ALG_ECC_224 = 1,
45     HCF_ALG_ECC_256,
46     HCF_ALG_ECC_384,
47     HCF_ALG_ECC_521,
48 
49     HCF_ALG_AES_128,
50     HCF_ALG_AES_192,
51     HCF_ALG_AES_256,
52     HCF_ALG_SM4_128,
53     HCF_ALG_3DES_192,
54 
55     HCF_ALG_MODE_NONE,
56     HCF_ALG_MODE_ECB,
57     HCF_ALG_MODE_CBC,
58     HCF_ALG_MODE_CTR,
59     HCF_ALG_MODE_OFB,
60     HCF_ALG_MODE_CFB,
61     HCF_ALG_MODE_CFB1,
62     HCF_ALG_MODE_CFB8,
63     HCF_ALG_MODE_CFB64,
64     HCF_ALG_MODE_CFB128,
65     HCF_ALG_MODE_CCM,
66     HCF_ALG_MODE_GCM,
67 
68     HCF_ALG_NOPADDING,
69     HCF_ALG_PADDING_PKCS5,
70     HCF_ALG_PADDING_PKCS7,
71 
72     // rsa keysize
73     HCF_OPENSSL_RSA_512,
74     HCF_OPENSSL_RSA_768,
75     HCF_OPENSSL_RSA_1024,
76     HCF_OPENSSL_RSA_2048,
77     HCF_OPENSSL_RSA_3072,
78     HCF_OPENSSL_RSA_4096,
79     HCF_OPENSSL_RSA_8192,
80 
81     // rsa cipher padding,
82     HCF_OPENSSL_RSA_PKCS1_PADDING,
83     HCF_OPENSSL_RSA_PKCS1_OAEP_PADDING,
84     HCF_OPENSSL_RSA_PSS_PADDING,
85 
86     // digest
87     HCF_OPENSSL_DIGEST_NONE,
88     HCF_OPENSSL_DIGEST_MD5,
89     HCF_OPENSSL_DIGEST_SM3,
90     HCF_OPENSSL_DIGEST_SHA1,
91     HCF_OPENSSL_DIGEST_SHA224,
92     HCF_OPENSSL_DIGEST_SHA256,
93     HCF_OPENSSL_DIGEST_SHA384,
94     HCF_OPENSSL_DIGEST_SHA512,
95 
96     // primes
97     HCF_OPENSSL_PRIMES_2,
98     HCF_OPENSSL_PRIMES_3,
99     HCF_OPENSSL_PRIMES_4,
100     HCF_OPENSSL_PRIMES_5,
101 
102     // dsa
103     HCF_ALG_DSA_1024,
104     HCF_ALG_DSA_2048,
105     HCF_ALG_DSA_3072,
106 
107     // sm2
108     HCF_ALG_SM2_256,
109 
110     // 4.0 added: only for algName(NO SIZE)
111     HCF_ALG_DSA_DEFAULT,
112     HCF_ALG_RSA_DEFAULT,
113     HCF_ALG_ECC_DEFAULT,
114     HCF_ALG_SM2_DEFAULT,
115     HCF_ALG_AES_DEFAULT,
116     HCF_ALG_SM4_DEFAULT,
117     HCF_ALG_3DES_DEFAULT,
118 
119     // sm2 text format
120     HCF_ALG_TEXT_FORMAT_C1C3C2,
121     HCF_ALG_TEXT_FORMAT_C1C2C3,
122 } HcfAlgParaValue;
123 
124 typedef struct {
125     const char* tag;
126     HcfAlgParaType paraType;
127     HcfAlgParaValue paraValue;
128 } HcfParaConfig;
129 
130 typedef struct {
131     const char* algNameStr;
132     HcfAlgValue algValue;
133 } HcfAlgMap;
134 
135 typedef struct {
136     HcfAlgValue algo;
137     HcfAlgParaValue keySize;
138     HcfAlgParaValue mode;
139     HcfAlgParaValue paddingMode;
140     HcfAlgParaValue md;
141     HcfAlgParaValue mgf1md;
142 } CipherAttr;
143 
144 typedef struct {
145     HcfAlgValue algo; // algType
146     int32_t bits; // keyLen
147     int32_t primes; // number of primes
148 } HcfAsyKeyGenParams;
149 
150 typedef struct {
151     HcfAlgValue algo; // algType
152     HcfAlgParaValue padding;
153     HcfAlgParaValue md;
154     HcfAlgParaValue mgf1md;
155 } HcfSignatureParams;
156 
157 typedef struct {
158     HcfAlgValue algo;
159 } HcfKeyAgreementParams;
160 
161 typedef HcfResult (*SetParameterFunc) (const HcfParaConfig* config, void *params);
162 
163 #ifdef __cplusplus
164 extern "C" {
165 #endif
166 
167 HcfResult ParseAndSetParameter(const char *paramsStr, void *params, SetParameterFunc setFunc);
168 
169 HcfResult ParseAlgNameToParams(const char *algNameStr, HcfAsyKeyGenParams *params);
170 
171 #ifdef __cplusplus
172 }
173 #endif
174 #endif
175