Debian Codegraph @

lib_pypy/_cffi_ssl/_cffi_src/openssl/evp.py

raw symbols SCIP
1 # This file is dual licensed under the terms of the Apache License, Version
2 # 2.0, and the BSD License. See the LICENSE file in the root of this repository
3 # for complete details.
4
5 from __future__ import absolute_import, division, print_function
6
7 INCLUDES = """
8 #include <openssl/evp.h>
9 """
10
11 TYPES = """
12 typedef ... EVP_CIPHER;
13 typedef ... EVP_CIPHER_CTX;
14 typedef ... EVP_MD;
15 typedef ... EVP_MD_CTX;
16
17 typedef ... EVP_PKEY;
18 typedef ... EVP_PKEY_CTX;
19 static const int EVP_PKEY_RSA;
20 static const int EVP_PKEY_DSA;
21 static const int EVP_PKEY_DH;
22 static const int EVP_PKEY_DHX;
23 static const int EVP_PKEY_EC;
24 static const int EVP_PKEY_X25519;
25 static const int EVP_PKEY_ED25519;
26 static const int EVP_PKEY_X448;
27 static const int EVP_PKEY_ED448;
28 static const int EVP_PKEY_POLY1305;
29 static const int EVP_MAX_MD_SIZE;
30 static const int EVP_CTRL_AEAD_SET_IVLEN;
31 static const int EVP_CTRL_AEAD_GET_TAG;
32 static const int EVP_CTRL_AEAD_SET_TAG;
33 // only on OpenSSL 1.1.1+
34 static const int EVP_MD_CTX_FLAG_NON_FIPS_ALLOW;
35
36 static const int Cryptography_HAS_SCRYPT;
37 static const int Cryptography_HAS_EVP_PKEY_DHX;
38 static const int Cryptography_HAS_EVP_PKEY_get_set_tls_encodedpoint;
39 static const int Cryptography_HAS_ONESHOT_EVP_DIGEST_SIGN_VERIFY;
40 static const long Cryptography_HAS_RAW_KEY;
41 static const long Cryptography_HAS_EVP_DIGESTFINAL_XOF;
42 """
43
44 FUNCTIONS = """
45 const EVP_CIPHER *EVP_get_cipherbyname(const char *);
46 EVP_CIPHER *EVP_CIPHER_fetch(OSSL_LIB_CTX *, const char *, const char *);
47 void EVP_CIPHER_free(EVP_CIPHER *);
48
49 int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *, int);
50 int EVP_CipherInit_ex(EVP_CIPHER_CTX *, const EVP_CIPHER *, ENGINE *,
51 const unsigned char *, const unsigned char *, int);
52 int EVP_CipherUpdate(EVP_CIPHER_CTX *, unsigned char *, int *,
53 const unsigned char *, int);
54 int EVP_CipherFinal_ex(EVP_CIPHER_CTX *, unsigned char *, int *);
55 int EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *);
56 EVP_CIPHER_CTX *EVP_CIPHER_CTX_new(void);
57 void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *);
58 int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *, int);
59 const EVP_CIPHER *EVP_CIPHER_CTX_cipher(const EVP_CIPHER_CTX *);
60
61 int EVP_MD_CTX_copy_ex(EVP_MD_CTX *, const EVP_MD_CTX *);
62 int EVP_DigestInit_ex(EVP_MD_CTX *, const EVP_MD *, ENGINE *);
63 int EVP_DigestUpdate(EVP_MD_CTX *, const void *, size_t);
64 int EVP_DigestFinal_ex(EVP_MD_CTX *, unsigned char *, unsigned int *);
65 int EVP_Digest(const void *, size_t, unsigned char *, unsigned int *, const EVP_MD *, ENGINE *);
66 int EVP_DigestFinalXOF(EVP_MD_CTX *, unsigned char *, size_t);
67 const EVP_MD *EVP_get_digestbyname(const char *);
68 int EVP_MD_size(const EVP_MD *);
69 void EVP_MD_do_all(void (*) (const EVP_MD *, const char *, const char *, void *), void *);
70 void EVP_MD_do_all_provided(OSSL_LIB_CTX *, void (*)(EVP_MD *, void *), void *);
71 int EVP_MD_nid(const EVP_MD *);
72
73 EVP_PKEY *EVP_PKEY_new(void);
74 void EVP_PKEY_free(EVP_PKEY *);
75 int EVP_PKEY_type(int);
76 int EVP_PKEY_size(EVP_PKEY *);
77 RSA *EVP_PKEY_get1_RSA(EVP_PKEY *);
78 DSA *EVP_PKEY_get1_DSA(EVP_PKEY *);
79 DH *EVP_PKEY_get1_DH(EVP_PKEY *);
80
81 int EVP_PKEY_encrypt(EVP_PKEY_CTX *, unsigned char *, size_t *,
82 const unsigned char *, size_t);
83 int EVP_PKEY_decrypt(EVP_PKEY_CTX *, unsigned char *, size_t *,
84 const unsigned char *, size_t);
85
86 int EVP_SignInit(EVP_MD_CTX *, const EVP_MD *);
87 int EVP_SignUpdate(EVP_MD_CTX *, const void *, size_t);
88 int EVP_SignFinal(EVP_MD_CTX *, unsigned char *, unsigned int *, EVP_PKEY *);
89
90 int EVP_VerifyInit(EVP_MD_CTX *, const EVP_MD *);
91 int EVP_VerifyUpdate(EVP_MD_CTX *, const void *, size_t);
92 int EVP_VerifyFinal(EVP_MD_CTX *, const unsigned char *, unsigned int,
93 EVP_PKEY *);
94
95 int EVP_DigestSignInit(EVP_MD_CTX *, EVP_PKEY_CTX **, const EVP_MD *,
96 ENGINE *, EVP_PKEY *);
97 int EVP_DigestSignUpdate(EVP_MD_CTX *, const void *, size_t);
98 int EVP_DigestSignFinal(EVP_MD_CTX *, unsigned char *, size_t *);
99 int EVP_DigestVerifyInit(EVP_MD_CTX *, EVP_PKEY_CTX **, const EVP_MD *,
100 ENGINE *, EVP_PKEY *);
101
102 int PKCS5_PBKDF2_HMAC_SHA1(const char *, int, const unsigned char *, int, int,
103 int, unsigned char *);
104
105 EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *, ENGINE *);
106 EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int, ENGINE *);
107 EVP_PKEY_CTX *EVP_PKEY_CTX_dup(EVP_PKEY_CTX *);
108 void EVP_PKEY_CTX_free(EVP_PKEY_CTX *);
109 int EVP_PKEY_sign_init(EVP_PKEY_CTX *);
110 int EVP_PKEY_sign(EVP_PKEY_CTX *, unsigned char *, size_t *,
111 const unsigned char *, size_t);
112 int EVP_PKEY_verify_init(EVP_PKEY_CTX *);
113 int EVP_PKEY_verify(EVP_PKEY_CTX *, const unsigned char *, size_t,
114 const unsigned char *, size_t);
115 int EVP_PKEY_encrypt_init(EVP_PKEY_CTX *);
116 int EVP_PKEY_decrypt_init(EVP_PKEY_CTX *);
117
118 int EVP_PKEY_set1_RSA(EVP_PKEY *, RSA *);
119 int EVP_PKEY_set1_DSA(EVP_PKEY *, DSA *);
120 int EVP_PKEY_set1_DH(EVP_PKEY *, DH *);
121
122 int EVP_PKEY_cmp(const EVP_PKEY *, const EVP_PKEY *);
123
124 int EVP_PKEY_keygen_init(EVP_PKEY_CTX *);
125 int EVP_PKEY_keygen(EVP_PKEY_CTX *, EVP_PKEY **);
126 int EVP_PKEY_derive_init(EVP_PKEY_CTX *);
127 int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *, EVP_PKEY *);
128 int EVP_PKEY_derive(EVP_PKEY_CTX *, unsigned char *, size_t *);
129 int EVP_PKEY_set_type(EVP_PKEY *, int);
130
131 EVP_MD_CTX *EVP_MD_CTX_new(void);
132 void EVP_MD_CTX_free(EVP_MD_CTX *);
133 /* Added in 1.1.1 */
134 int EVP_DigestSign(EVP_MD_CTX *, unsigned char *, size_t *,
135 const unsigned char *, size_t);
136 int EVP_DigestVerify(EVP_MD_CTX *, const unsigned char *, size_t,
137 const unsigned char *, size_t);
138 /* Added in 1.1.0 */
139 size_t EVP_PKEY_get1_tls_encodedpoint(EVP_PKEY *, unsigned char **);
140 int EVP_PKEY_set1_tls_encodedpoint(EVP_PKEY *, const unsigned char *,
141 size_t);
142
143 /* EVP_PKEY * became const in 1.1.0 */
144 // int EVP_PKEY_bits(EVP_PKEY *);
145
146 void OpenSSL_add_all_algorithms(void);
147 int EVP_PKEY_assign_RSA(EVP_PKEY *, RSA *);
148
149 EC_KEY *EVP_PKEY_get1_EC_KEY(EVP_PKEY *);
150 int EVP_PKEY_set1_EC_KEY(EVP_PKEY *, EC_KEY *);
151
152 int EVP_MD_CTX_block_size(const EVP_MD_CTX *);
153 int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *, int, int, void *);
154
155 int PKCS5_PBKDF2_HMAC(const char *, int, const unsigned char *, int, int,
156 const EVP_MD *, int, unsigned char *);
157
158 int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX *, const EVP_MD *);
159
160 int EVP_PBE_scrypt(const char *, size_t, const unsigned char *, size_t,
161 uint64_t, uint64_t, uint64_t, uint64_t, unsigned char *,
162 size_t);
163
164 EVP_PKEY *EVP_PKEY_new_raw_private_key(int, ENGINE *, const unsigned char *,
165 size_t);
166 EVP_PKEY *EVP_PKEY_new_raw_public_key(int, ENGINE *, const unsigned char *,
167 size_t);
168 int EVP_PKEY_get_raw_private_key(const EVP_PKEY *, unsigned char *, size_t *);
169 int EVP_PKEY_get_raw_public_key(const EVP_PKEY *, unsigned char *, size_t *);
170 /* OpenSSL 1.1.1+ */
171 void EVP_MD_CTX_set_flags(EVP_MD_CTX *ctx, int flags);
172 int EVP_MD_block_size(const EVP_MD *md);
173 // OpenSSL 3.0.0+
174 int EVP_default_properties_is_fips_enabled(OSSL_LIB_CTX *libctx);
175
176 // locally defined for _hashlib
177 int EVP_hash(EVP_MD_CTX *ctx, const void *vp, ssize_t len, ssize_t munch);
178 EVP_MD *EVP_MD_fetch(OSSL_LIB_CTX *ctx, const char *algorithm,
179 const char *properties);
180 void EVP_MD_free(EVP_MD *md);
181 unsigned char *HMAC(const EVP_MD *evp_md, const void *key, int key_len,
182 const unsigned char *data, size_t data_len,
183 unsigned char *md, unsigned int *md_len);
184 """
185
186 CUSTOMIZATIONS = """
187 #ifdef EVP_PKEY_DHX
188 const long Cryptography_HAS_EVP_PKEY_DHX = 1;
189 #else
190 const long Cryptography_HAS_EVP_PKEY_DHX = 0;
191 const long EVP_PKEY_DHX = -1;
192 #endif
193
194 #if CRYPTOGRAPHY_OPENSSL_LESS_THAN_110 || defined(OPENSSL_NO_SCRYPT)
195 static const long Cryptography_HAS_SCRYPT = 0;
196 int (*EVP_PBE_scrypt)(const char *, size_t, const unsigned char *, size_t,
197 uint64_t, uint64_t, uint64_t, uint64_t, unsigned char *,
198 size_t) = NULL;
199 #else
200 static const long Cryptography_HAS_SCRYPT = 1;
201 #endif
202
203 #if CRYPTOGRAPHY_OPENSSL_110_OR_GREATER
204 static const long Cryptography_HAS_EVP_PKEY_get_set_tls_encodedpoint = 1;
205 #else
206 static const long Cryptography_HAS_EVP_PKEY_get_set_tls_encodedpoint = 0;
207 size_t (*EVP_PKEY_get1_tls_encodedpoint)(EVP_PKEY *, unsigned char **) = NULL;
208 int (*EVP_PKEY_set1_tls_encodedpoint)(EVP_PKEY *, const unsigned char *,
209 size_t) = NULL;
210 #endif
211
212 #if CRYPTOGRAPHY_OPENSSL_LESS_THAN_111
213 static const long Cryptography_HAS_ONESHOT_EVP_DIGEST_SIGN_VERIFY = 0;
214 static const long Cryptography_HAS_RAW_KEY = 0;
215 static const long Cryptography_HAS_EVP_DIGESTFINAL_XOF = 0;
216 int (*EVP_DigestFinalXOF)(EVP_MD_CTX *, unsigned char *, size_t) = NULL;
217 int (*EVP_DigestSign)(EVP_MD_CTX *, unsigned char *, size_t *,
218 const unsigned char *tbs, size_t) = NULL;
219 int (*EVP_DigestVerify)(EVP_MD_CTX *, const unsigned char *, size_t,
220 const unsigned char *, size_t) = NULL;
221 EVP_PKEY *(*EVP_PKEY_new_raw_private_key)(int, ENGINE *, const unsigned char *,
222 size_t) = NULL;
223 EVP_PKEY *(*EVP_PKEY_new_raw_public_key)(int, ENGINE *, const unsigned char *,
224 size_t) = NULL;
225 int (*EVP_PKEY_get_raw_private_key)(const EVP_PKEY *, unsigned char *,
226 size_t *) = NULL;
227 int (*EVP_PKEY_get_raw_public_key)(const EVP_PKEY *, unsigned char *,
228 size_t *) = NULL;
229 #else
230 static const long Cryptography_HAS_ONESHOT_EVP_DIGEST_SIGN_VERIFY = 1;
231 static const long Cryptography_HAS_RAW_KEY = 1;
232 static const long Cryptography_HAS_EVP_DIGESTFINAL_XOF = 1;
233 #endif
234
235 /* OpenSSL 1.1.0+ does this define for us, but if not present we'll do it */
236 #if !defined(EVP_CTRL_AEAD_SET_IVLEN)
237 # define EVP_CTRL_AEAD_SET_IVLEN EVP_CTRL_GCM_SET_IVLEN
238 #endif
239 #if !defined(EVP_CTRL_AEAD_GET_TAG)
240 # define EVP_CTRL_AEAD_GET_TAG EVP_CTRL_GCM_GET_TAG
241 #endif
242 #if !defined(EVP_CTRL_AEAD_SET_TAG)
243 # define EVP_CTRL_AEAD_SET_TAG EVP_CTRL_GCM_SET_TAG
244 #endif
245
246 /* This is tied to X25519 support so we reuse the Cryptography_HAS_X25519
247 conditional to remove it. OpenSSL 1.1.0 didn't have this define, but
248 1.1.1 will when it is released. We can remove this in the distant
249 future when we drop 1.1.0 support. */
250 #ifndef EVP_PKEY_X25519
251 #define EVP_PKEY_X25519 NID_X25519
252 #endif
253
254 /* This is tied to X448 support so we reuse the Cryptography_HAS_X448
255 conditional to remove it. OpenSSL 1.1.1 adds this define. We can remove
256 this in the distant future when we drop 1.1.0 support. */
257 #ifndef EVP_PKEY_X448
258 #define EVP_PKEY_X448 NID_X448
259 #endif
260
261 /* This is tied to ED25519 support so we reuse the Cryptography_HAS_ED25519
262 conditional to remove it. */
263 #ifndef EVP_PKEY_ED25519
264 #define EVP_PKEY_ED25519 NID_ED25519
265 #endif
266
267 /* This is tied to ED448 support so we reuse the Cryptography_HAS_ED448
268 conditional to remove it. */
269 #ifndef EVP_PKEY_ED448
270 #define EVP_PKEY_ED448 NID_ED448
271 #endif
272
273 /* This is tied to poly1305 support so we reuse the Cryptography_HAS_POLY1305
274 conditional to remove it. */
275 #ifndef EVP_PKEY_POLY1305
276 #define EVP_PKEY_POLY1305 NID_poly1305
277 #endif
278
279 #if CRYPTOGRAPHY_OPENSSL_300_OR_GREATER
280 static const long Cryptography_HAS_300_FIPS = 1;
281 static const long Cryptography_HAS_300_EVP_CIPHER = 1;
282 #else
283 static const long Cryptography_HAS_300_FIPS = 0;
284 static const long Cryptography_HAS_300_EVP_CIPHER = 0;
285 void (*EVP_MD_do_all_provided)(OSSL_LIB_CTX *, void (*)(EVP_MD *, void *), void *) = NULL;
286 int (*EVP_default_properties_is_fips_enabled)(OSSL_LIB_CTX *) = NULL;
287 int (*EVP_default_properties_enable_fips)(OSSL_LIB_CTX *, int) = NULL;
288 EVP_CIPHER * (*EVP_CIPHER_fetch)(OSSL_LIB_CTX *, const char *,
289 const char *) = NULL;
290 void (*EVP_CIPHER_free)(EVP_CIPHER *) = NULL;
291 EVP_MD *(*EVP_MD_fetch)(OSSL_LIB_CTX *ctx, const char *algorithm,
292 const char *properties) = NULL;
293 void (*EVP_MD_free)(EVP_MD *md) = NULL;
294 #endif
295
296 int
297 EVP_hash(EVP_MD_CTX *ctx, const void *vp, ssize_t len, ssize_t munch)
298 {
299 unsigned int process;
300 const unsigned char *cp = (const unsigned char *)vp;
301 while (0 < len) {
302 if (len > munch)
303 process = (unsigned int)munch;
304 else
305 process = (unsigned int)len;
306 if (!EVP_DigestUpdate(ctx, (const void*)cp, process)) {
307 return -1;
308 }
309 len -= process;
310 cp += process;
311 }
312 return 0;
313 }
314 """