• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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_OPENSSL_ADAPTER_H
17 #define HCF_OPENSSL_ADAPTER_H
18 
19 #include <openssl/ec.h>
20 #include <openssl/evp.h>
21 #include <openssl/pem.h>
22 #include <openssl/x509.h>
23 #include <openssl/evp.h>
24 #include <openssl/hmac.h>
25 #include <openssl/rand.h>
26 #include <openssl/des.h>
27 #include <crypto/sm2.h>
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 BIGNUM *Openssl_BN_dup(const BIGNUM *a);
34 void Openssl_BN_clear(BIGNUM *a);
35 void Openssl_BN_clear_free(BIGNUM *a);
36 BIGNUM *Openssl_BN_new(void);
37 void Openssl_BN_free(BIGNUM *a);
38 BIGNUM *Openssl_BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret);
39 BIGNUM *Openssl_BN_lebin2bn(const unsigned char *s, int len, BIGNUM *ret);
40 int Openssl_BN_bn2binpad(const BIGNUM *a, unsigned char *to, int toLen);
41 int Openssl_BN_bn2lebinpad(const BIGNUM *a, unsigned char *to, int toLen);
42 BN_CTX *Openssl_BN_CTX_new(void);
43 void Openssl_BN_CTX_free(BN_CTX *ctx);
44 int Openssl_BN_num_bytes(const BIGNUM *a);
45 int Openssl_BN_set_word(BIGNUM *a, unsigned int w);
46 unsigned int Openssl_BN_get_word(const BIGNUM *a);
47 int Openssl_BN_num_bits(const BIGNUM *a);
48 int Openssl_BN_hex2bn(BIGNUM **a, const char *str);
49 int Openssl_BN_cmp(const BIGNUM *a, const BIGNUM *b);
50 
51 EC_KEY *Openssl_EC_KEY_new_by_curve_name(int nid);
52 EC_POINT *Openssl_EC_POINT_dup(const EC_POINT *src, const EC_GROUP *group);
53 int Openssl_EC_KEY_generate_key(EC_KEY *ecKey);
54 int Openssl_EC_KEY_set_public_key(EC_KEY *key, const EC_POINT *pub);
55 int Openssl_EC_KEY_set_private_key(EC_KEY *key, const BIGNUM *priv_key);
56 int Openssl_EC_KEY_check_key(const EC_KEY *key);
57 const EC_POINT *Openssl_EC_KEY_get0_public_key(const EC_KEY *key);
58 const BIGNUM *Openssl_EC_KEY_get0_private_key(const EC_KEY *key);
59 const EC_GROUP *Openssl_EC_KEY_get0_group(const EC_KEY *key);
60 int Openssl_i2d_EC_PUBKEY(EC_KEY *a, unsigned char **pp);
61 int Openssl_i2d_ECPrivateKey(EC_KEY *key, unsigned char **out);
62 EC_KEY *Openssl_d2i_EC_PUBKEY(EC_KEY **a, const unsigned char **pp, long length);
63 EC_KEY *Openssl_d2i_ECPrivateKey(EC_KEY **key, const unsigned char **in, long len);
64 void Openssl_EC_KEY_set_asn1_flag(EC_KEY *key, int flag);
65 void Openssl_EC_KEY_set_enc_flags(EC_KEY *ecKey, unsigned int flags);
66 void Openssl_EC_KEY_free(EC_KEY *key);
67 void Openssl_EC_POINT_free(EC_POINT *point);
68 EC_GROUP *Openssl_EC_GROUP_new_curve_GFp(const BIGNUM *p, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx);
69 void Openssl_EC_GROUP_free(EC_GROUP *group);
70 EC_POINT *Openssl_EC_POINT_new(const EC_GROUP *group);
71 int Openssl_EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group, EC_POINT *point, const BIGNUM *x,
72     const BIGNUM *y, BN_CTX *ctx);
73 int Openssl_EC_GROUP_set_generator(EC_GROUP *group, const EC_POINT *generator,
74     const BIGNUM *order, const BIGNUM *cofactor);
75 EC_KEY *Openssl_EC_KEY_new(void);
76 EC_KEY *Openssl_EC_KEY_dup(const EC_KEY *ecKey);
77 int Openssl_EC_KEY_set_group(EC_KEY *key, const EC_GROUP *group);
78 int Openssl_EC_GROUP_get_curve_GFp(const EC_GROUP *group, BIGNUM *p, BIGNUM *a, BIGNUM *b, BN_CTX *ctx);
79 const EC_POINT *Openssl_EC_GROUP_get0_generator(const EC_GROUP *group);
80 int Openssl_EC_POINT_get_affine_coordinates_GFp(const EC_GROUP *group, const EC_POINT *point, BIGNUM *x,
81     BIGNUM *y, BN_CTX *ctx);
82 int Openssl_EC_GROUP_get_order(const EC_GROUP *group, BIGNUM *order, BN_CTX *ctx);
83 int Openssl_EC_GROUP_get_cofactor(const EC_GROUP *group, BIGNUM *cofactor, BN_CTX *ctx);
84 int Openssl_EC_GROUP_get_degree(const EC_GROUP *group);
85 EC_GROUP *Openssl_EC_GROUP_dup(const EC_GROUP *a);
86 void Openssl_EC_GROUP_set_curve_name(EC_GROUP *group, int nid);
87 int Openssl_EC_GROUP_get_curve_name(const EC_GROUP *group);
88 int Openssl_EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar, const EC_POINT *point,
89     const BIGNUM *p_scalar, BN_CTX *ctx);
90 
91 EVP_MD_CTX *Openssl_EVP_MD_CTX_new(void);
92 void Openssl_EVP_MD_CTX_free(EVP_MD_CTX *ctx);
93 void Openssl_EVP_MD_CTX_set_pkey_ctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX *pctx);
94 int Openssl_EVP_DigestSignInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx, const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey);
95 int Openssl_EVP_DigestSignUpdate(EVP_MD_CTX *ctx, const void *data, size_t count);
96 int Openssl_EVP_DigestSignFinal(EVP_MD_CTX *ctx, unsigned char *sigret, size_t *siglen);
97 int Openssl_EVP_DigestVerifyInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx, const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey);
98 int Openssl_EVP_DigestVerifyUpdate(EVP_MD_CTX *ctx, const void *data, size_t count);
99 int Openssl_EVP_DigestVerifyFinal(EVP_MD_CTX *ctx, const unsigned char *sig, size_t siglen);
100 int Openssl_EVP_PKEY_sign_init(EVP_PKEY_CTX *ctx);
101 int Openssl_EVP_PKEY_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen, const unsigned char *tbs,
102     size_t tbslen);
103 int Openssl_EVP_PKEY_verify_init(EVP_PKEY_CTX *ctx);
104 int Openssl_EVP_PKEY_verify(EVP_PKEY_CTX *ctx, const unsigned char *sig, size_t siglen, const unsigned char *tbs,
105     size_t tbslen);
106 
107 EVP_PKEY *Openssl_EVP_PKEY_new(void);
108 int Openssl_EVP_PKEY_assign_EC_KEY(EVP_PKEY *pkey, EC_KEY *key);
109 void Openssl_EVP_PKEY_free(EVP_PKEY *pkey);
110 
111 EVP_PKEY_CTX *Openssl_EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e);
112 int Openssl_EVP_PKEY_derive_init(EVP_PKEY_CTX *ctx);
113 int Openssl_EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer);
114 int Openssl_EVP_PKEY_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen);
115 void Openssl_EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx);
116 
117 // new added
118 int Openssl_EVP_PKEY_encrypt(EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen,
119     const unsigned char *in, size_t inlen);
120 int Openssl_EVP_PKEY_decrypt(EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen,
121     const unsigned char *in, size_t inlen);
122 int Openssl_EVP_PKEY_encrypt_init(EVP_PKEY_CTX *ctx);
123 int Openssl_EVP_PKEY_decrypt_init(EVP_PKEY_CTX *ctx);
124 
125 EVP_PKEY_CTX *Openssl_EVP_PKEY_CTX_new_id(int id, ENGINE *e);
126 int Openssl_EVP_PKEY_CTX_set1_id(EVP_PKEY_CTX *ctx, const void *id, int id_len);
127 int Openssl_EVP_PKEY_paramgen_init(EVP_PKEY_CTX *ctx);
128 int Openssl_EVP_PKEY_CTX_set_dsa_paramgen_bits(EVP_PKEY_CTX *ctx, int nbits);
129 int Openssl_EVP_PKEY_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey);
130 int Openssl_EVP_PKEY_keygen_init(EVP_PKEY_CTX *ctx);
131 int Openssl_EVP_PKEY_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey);
132 int Openssl_EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key);
133 DSA *Openssl_EVP_PKEY_get1_DSA(EVP_PKEY *pkey);
134 DSA *Openssl_DSA_new(void);
135 void Openssl_DSA_free(DSA *dsa);
136 int Openssl_DSA_up_ref(DSA *dsa);
137 int Openssl_DSA_set0_pqg(DSA *dsa, BIGNUM *p, BIGNUM *q, BIGNUM *g);
138 int Openssl_DSA_set0_key(DSA *dsa, BIGNUM *pub_key, BIGNUM *pri_key);
139 const BIGNUM *Openssl_DSA_get0_p(const DSA *dsa);
140 const BIGNUM *Openssl_DSA_get0_q(const DSA *dsa);
141 const BIGNUM *Openssl_DSA_get0_g(const DSA *dsa);
142 const BIGNUM *Openssl_DSA_get0_pub_key(const DSA *dsa);
143 const BIGNUM *Openssl_DSA_get0_priv_key(const DSA *dsa);
144 int Openssl_DSA_generate_key(DSA *a);
145 DSA *Openssl_d2i_DSA_PUBKEY(DSA **dsa, const unsigned char **ppin, long length);
146 DSA *Openssl_d2i_DSAPrivateKey(DSA **dsa, const unsigned char **ppin, long length);
147 int Openssl_i2d_DSA_PUBKEY(DSA *dsa, unsigned char **ppout);
148 int Openssl_i2d_DSAPrivateKey(DSA *dsa, unsigned char **ppout);
149 
150 RSA *Openssl_RSA_new(void);
151 void Openssl_RSA_free(RSA *rsa);
152 int Openssl_RSA_generate_multi_prime_key(RSA *rsa, int bits, int primes,
153     BIGNUM *e, BN_GENCB *cb);
154 int Openssl_RSA_generate_key_ex(RSA *rsa, int bits, BIGNUM *e, BN_GENCB *cb);
155 int Openssl_RSA_bits(const RSA *rsa);
156 int Openssl_RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d);
157 void Openssl_RSA_get0_key(const RSA *r, const BIGNUM **n, const BIGNUM **e, const BIGNUM **d);
158 const BIGNUM *Openssl_RSA_get0_n(const RSA *d);
159 const BIGNUM *Openssl_RSA_get0_e(const RSA *d);
160 const BIGNUM *Openssl_RSA_get0_d(const RSA *d);
161 void Openssl_RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q);
162 RSA *Openssl_RSAPublicKey_dup(RSA *rsa);
163 RSA *Openssl_RSAPrivateKey_dup(RSA *rsa);
164 RSA *Openssl_d2i_RSA_PUBKEY(RSA **a, const unsigned char **pp, long length);
165 int Openssl_i2d_RSA_PUBKEY(RSA *a, unsigned char **pp);
166 int Openssl_EVP_PKEY_CTX_set_rsa_pss_saltlen(EVP_PKEY_CTX *ctx, int saltlen);
167 int Openssl_EVP_PKEY_CTX_get_rsa_pss_saltlen(EVP_PKEY_CTX *ctx, int *saltlen);
168 int Openssl_EVP_PKEY_CTX_set_rsa_padding(EVP_PKEY_CTX *ctx, int pad);
169 int Openssl_EVP_PKEY_CTX_set_rsa_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD *md);
170 int Openssl_EVP_PKEY_CTX_set_rsa_oaep_md(EVP_PKEY_CTX *ctx, const EVP_MD *md);
171 int Openssl_EVP_PKEY_CTX_set0_rsa_oaep_label(EVP_PKEY_CTX *ctx, void *label, int len);
172 int Openssl_EVP_PKEY_CTX_get0_rsa_oaep_label(EVP_PKEY_CTX *ctx, unsigned char **label);
173 EVP_PKEY *Openssl_d2i_AutoPrivateKey(EVP_PKEY **a, const unsigned char **pp, long length);
174 struct rsa_st *Openssl_EVP_PKEY_get1_RSA(EVP_PKEY *pkey);
175 int Openssl_EVP_PKEY_set1_RSA(EVP_PKEY *pkey, struct rsa_st *key);
176 int Openssl_EVP_PKEY_assign_RSA(EVP_PKEY *pkey, struct rsa_st *key);
177 int Openssl_i2d_PKCS8PrivateKey_bio(BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc,
178     char *kstr, int klen, pem_password_cb *cb, void *u);
179 // BIO
180 BIO *Openssl_BIO_new(const BIO_METHOD *type);
181 const BIO_METHOD *Openssl_BIO_s_mem(void);
182 int Openssl_BIO_read(BIO *b, void *data, int dlen);
183 void Openssl_BIO_free_all(BIO *a);
184 
185 int Openssl_RAND_priv_bytes(unsigned char *buf, int num);
186 void Openssl_RAND_seed(const void *buf, int num);
187 
188 const EVP_MD *Openssl_EVP_sha1(void);
189 const EVP_MD *Openssl_EVP_sha224(void);
190 const EVP_MD *Openssl_EVP_sha256(void);
191 const EVP_MD *Openssl_EVP_sha384(void);
192 const EVP_MD *Openssl_EVP_sha512(void);
193 const EVP_MD *Openssl_EVP_md5(void);
194 const EVP_MD *Openssl_EVP_sm3(void);
195 int Openssl_EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *size);
196 int Openssl_EVP_MD_CTX_size(const EVP_MD_CTX *ctx);
197 int Openssl_EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl);
198 
199 int Openssl_HMAC_Init_ex(HMAC_CTX *ctx, const void *key, int len, const EVP_MD *md, ENGINE *impl);
200 int Openssl_HMAC_Final(HMAC_CTX *ctx, unsigned char *md, unsigned int *len);
201 size_t Openssl_HMAC_size(const HMAC_CTX *ctx);
202 void Openssl_HMAC_CTX_free(HMAC_CTX *ctx);
203 HMAC_CTX *Openssl_HMAC_CTX_new(void);
204 
205 void Openssl_EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx);
206 const EVP_CIPHER *Openssl_EVP_aes_128_ecb(void);
207 const EVP_CIPHER *Openssl_EVP_aes_192_ecb(void);
208 const EVP_CIPHER *Openssl_EVP_aes_256_ecb(void);
209 const EVP_CIPHER *Openssl_EVP_aes_128_cbc(void);
210 const EVP_CIPHER *Openssl_EVP_aes_192_cbc(void);
211 const EVP_CIPHER *Openssl_EVP_aes_256_cbc(void);
212 const EVP_CIPHER *Openssl_EVP_aes_128_ctr(void);
213 const EVP_CIPHER *Openssl_EVP_aes_192_ctr(void);
214 const EVP_CIPHER *Openssl_EVP_aes_256_ctr(void);
215 const EVP_CIPHER *Openssl_EVP_aes_128_ofb(void);
216 const EVP_CIPHER *Openssl_EVP_aes_192_ofb(void);
217 const EVP_CIPHER *Openssl_EVP_aes_256_ofb(void);
218 const EVP_CIPHER *Openssl_EVP_aes_128_cfb(void);
219 const EVP_CIPHER *Openssl_EVP_aes_192_cfb(void);
220 const EVP_CIPHER *Openssl_EVP_aes_256_cfb(void);
221 const EVP_CIPHER *Openssl_EVP_aes_128_cfb1(void);
222 const EVP_CIPHER *Openssl_EVP_aes_192_cfb1(void);
223 const EVP_CIPHER *Openssl_EVP_aes_256_cfb1(void);
224 const EVP_CIPHER *Openssl_EVP_aes_128_cfb128(void);
225 const EVP_CIPHER *Openssl_EVP_aes_192_cfb128(void);
226 const EVP_CIPHER *Openssl_EVP_aes_256_cfb128(void);
227 const EVP_CIPHER *Openssl_EVP_aes_128_cfb8(void);
228 const EVP_CIPHER *Openssl_EVP_aes_192_cfb8(void);
229 const EVP_CIPHER *Openssl_EVP_aes_256_cfb8(void);
230 const EVP_CIPHER *Openssl_EVP_aes_128_ccm(void);
231 const EVP_CIPHER *Openssl_EVP_aes_192_ccm(void);
232 const EVP_CIPHER *Openssl_EVP_aes_256_ccm(void);
233 const EVP_CIPHER *Openssl_EVP_aes_128_gcm(void);
234 const EVP_CIPHER *Openssl_EVP_aes_192_gcm(void);
235 const EVP_CIPHER *Openssl_EVP_aes_256_gcm(void);
236 const EVP_CIPHER *Openssl_EVP_sm4_ecb(void);
237 const EVP_CIPHER *Openssl_EVP_sm4_cbc(void);
238 const EVP_CIPHER *Openssl_EVP_sm4_cfb(void);
239 const EVP_CIPHER *Openssl_EVP_sm4_cfb128(void);
240 const EVP_CIPHER *Openssl_EVP_sm4_ctr(void);
241 const EVP_CIPHER *Openssl_EVP_sm4_ofb(void);
242 const EVP_CIPHER *Openssl_EVP_des_ede3_ecb(void);
243 const EVP_CIPHER *Openssl_EVP_des_ede3_cbc(void);
244 const EVP_CIPHER *Openssl_EVP_des_ede3_ofb(void);
245 const EVP_CIPHER *Openssl_EVP_des_ede3_cfb64(void);
246 const EVP_CIPHER *Openssl_EVP_des_ede3_cfb1(void);
247 const EVP_CIPHER *Openssl_EVP_des_ede3_cfb8(void);
248 EVP_CIPHER_CTX *Openssl_EVP_CIPHER_CTX_new(void);
249 int Openssl_EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
250                            const unsigned char *key, const unsigned char *iv, int enc);
251 int Openssl_EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *ctx, int pad);
252 int Openssl_EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr);
253 int Openssl_EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl);
254 int Openssl_EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl, const unsigned char *in, int inl);
255 
256 int Openssl_sm2_ciphertext_size(const EC_KEY *key, const EVP_MD *digest, size_t msgLen, size_t *cipherTextSize);
257 int Openssl_sm2_plaintext_size(const unsigned char *cipherText, size_t cipherTextSize, size_t *plainTextSize);
258 int Openssl_sm2_encrypt(const EC_KEY *key, const EVP_MD *digest, const uint8_t *msg,
259                         size_t msgLen, uint8_t *cipherTextBuf, size_t *cipherTextLen);
260 
261 int Openssl_sm2_decrypt(const EC_KEY *key, const EVP_MD *digest, const uint8_t *cipherText,
262                         size_t cipherTextLen, uint8_t *plainTextBuf, size_t *plainTextLen);
263 
264 #ifdef __cplusplus
265 }
266 #endif
267 
268 #endif