1 /*
2  * Copyright (C) 2022-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 #ifndef HCF_ASY_KEY_GENERATOR_H
17 #define HCF_ASY_KEY_GENERATOR_H
18 
19 #include <stdint.h>
20 #include "algorithm_parameter.h"
21 #include "asy_key_params.h"
22 #include "result.h"
23 #include "key_pair.h"
24 
25 enum HcfRsaKeySize {
26     HCF_RSA_KEY_SIZE_512 = 512,
27     HCF_RSA_KEY_SIZE_768 = 768,
28     HCF_RSA_KEY_SIZE_1024 = 1024,
29     HCF_RSA_KEY_SIZE_2048 = 2048,
30     HCF_RSA_KEY_SIZE_3072 = 3072,
31     HCF_RSA_KEY_SIZE_4096 = 4096,
32     HCF_RSA_KEY_SIZE_8192 = 8192,
33 };
34 
35 enum HcfDhNamedGroupId {
36     HCF_DH_MODP_SIZE_1536 = 0,
37     HCF_DH_MODP_SIZE_2048,
38     HCF_DH_MODP_SIZE_3072,
39     HCF_DH_MODP_SIZE_4096,
40     HCF_DH_MODP_SIZE_6144,
41     HCF_DH_MODP_SIZE_8192,
42     HCF_DH_FFDHE_SIZE_2048,
43     HCF_DH_FFDHE_SIZE_3072,
44     HCF_DH_FFDHE_SIZE_4096,
45     HCF_DH_FFDHE_SIZE_6144,
46     HCF_DH_FFDHE_SIZE_8192,
47 };
48 
49 enum HcfDsaKeySize {
50     HCF_DSA_KEY_SIZE_1024 = 1024,
51     HCF_DSA_KEY_SIZE_2048 = 2048,
52     HCF_DSA_KEY_SIZE_3072 = 3072,
53 };
54 
55 enum HcfRsaPrimesSize {
56     HCF_RSA_PRIMES_SIZE_2 = 2,
57     HCF_RSA_PRIMES_SIZE_3 = 3,
58     HCF_RSA_PRIMES_SIZE_4 = 4,
59     HCF_RSA_PRIMES_SIZE_5 = 5,
60 };
61 
62 typedef struct HcfAsyKeyGenerator HcfAsyKeyGenerator;
63 
64 struct HcfAsyKeyGenerator {
65     HcfObjectBase base;
66 
67     HcfResult (*generateKeyPair)(HcfAsyKeyGenerator *self, HcfParamsSpec *params,
68         HcfKeyPair **returnKeyPair);
69 
70     HcfResult (*convertKey)(HcfAsyKeyGenerator *self, HcfParamsSpec *params, HcfBlob *pubKeyBlob,
71         HcfBlob *priKeyBlob, HcfKeyPair **returnKeyPair);
72 
73     HcfResult (*convertPemKey)(HcfAsyKeyGenerator *self, HcfParamsSpec *params, const char *pubKeyStr,
74         const char *priKeyStr, HcfKeyPair **returnKeyPair);
75 
76     const char *(*getAlgoName)(HcfAsyKeyGenerator *self);
77 };
78 
79 typedef struct HcfAsyKeyGeneratorBySpec HcfAsyKeyGeneratorBySpec;
80 
81 struct HcfAsyKeyGeneratorBySpec {
82     HcfObjectBase base;
83 
84     HcfResult (*generateKeyPair)(const HcfAsyKeyGeneratorBySpec *self, HcfKeyPair **returnKeyPair);
85 
86     HcfResult (*generatePubKey)(const HcfAsyKeyGeneratorBySpec *self, HcfPubKey **returnPubKey);
87 
88     HcfResult (*generatePriKey)(const HcfAsyKeyGeneratorBySpec *self, HcfPriKey **returnPriKey);
89 
90     const char *(*getAlgName)(const HcfAsyKeyGeneratorBySpec *self);
91 };
92 
93 #ifdef __cplusplus
94 extern "C" {
95 #endif
96 
97 HcfResult HcfAsyKeyGeneratorCreate(const char *algoName, HcfAsyKeyGenerator **returnObj);
98 
99 HcfResult HcfAsyKeyGeneratorBySpecCreate(const HcfAsyKeyParamsSpec *paramsSpec, HcfAsyKeyGeneratorBySpec **returnObj);
100 
101 #ifdef __cplusplus
102 }
103 #endif
104 
105 #endif
106