1use super::*;
2use libc::*;
3
4pub const EVP_MAX_MD_SIZE: c_uint = 64;
5
6pub const PKCS5_SALT_LEN: c_int = 8;
7pub const PKCS12_DEFAULT_ITER: c_int = 2048;
8
9pub const EVP_PKEY_RSA: c_int = NID_rsaEncryption;
10#[cfg(any(ossl111, libressl, boringssl, awslc))]
11pub const EVP_PKEY_RSA_PSS: c_int = NID_rsassaPss;
12pub const EVP_PKEY_DSA: c_int = NID_dsa;
13pub const EVP_PKEY_DH: c_int = NID_dhKeyAgreement;
14#[cfg(ossl110)]
15pub const EVP_PKEY_DHX: c_int = NID_dhpublicnumber;
16pub const EVP_PKEY_EC: c_int = NID_X9_62_id_ecPublicKey;
17#[cfg(ossl111)]
18pub const EVP_PKEY_SM2: c_int = NID_sm2;
19#[cfg(any(ossl111, libressl370))]
20pub const EVP_PKEY_X25519: c_int = NID_X25519;
21#[cfg(any(ossl111, libressl370))]
22pub const EVP_PKEY_ED25519: c_int = NID_ED25519;
23#[cfg(ossl111)]
24pub const EVP_PKEY_X448: c_int = NID_X448;
25#[cfg(ossl111)]
26pub const EVP_PKEY_ED448: c_int = NID_ED448;
27pub const EVP_PKEY_HMAC: c_int = NID_hmac;
28pub const EVP_PKEY_CMAC: c_int = NID_cmac;
29#[cfg(ossl111)]
30pub const EVP_PKEY_POLY1305: c_int = NID_poly1305;
31#[cfg(any(ossl110, libressl360))]
32pub const EVP_PKEY_HKDF: c_int = NID_hkdf;
33
34#[cfg(ossl110)]
35pub const EVP_CIPHER_CTX_FLAG_WRAP_ALLOW: c_int = 0x1;
36
37pub const EVP_CTRL_GCM_SET_IVLEN: c_int = 0x9;
38pub const EVP_CTRL_GCM_GET_TAG: c_int = 0x10;
39pub const EVP_CTRL_GCM_SET_TAG: c_int = 0x11;
40
41cfg_if! {
42 if #[cfg(ossl300)] {
43 pub const EVP_PKEY_KEY_PARAMETERS: c_int = OSSL_KEYMGMT_SELECT_ALL_PARAMETERS;
44 pub const EVP_PKEY_PRIVATE_KEY: c_int = EVP_PKEY_KEY_PARAMETERS | OSSL_KEYMGMT_SELECT_PRIVATE_KEY;
45 pub const EVP_PKEY_PUBLIC_KEY: c_int = EVP_PKEY_KEY_PARAMETERS | OSSL_KEYMGMT_SELECT_PUBLIC_KEY;
46 pub const EVP_PKEY_KEYPAIR: c_int = EVP_PKEY_PUBLIC_KEY | OSSL_KEYMGMT_SELECT_PRIVATE_KEY;
47 pub const EVP_KDF_HKDF_MODE_EXTRACT_AND_EXPAND: c_int = 0;
48 pub const EVP_KDF_HKDF_MODE_EXTRACT_ONLY: c_int = 1;
49 pub const EVP_KDF_HKDF_MODE_EXPAND_ONLY: c_int = 2;
50 }
51}
52
53pub unsafe fn EVP_get_digestbynid(type_: c_int) -> *const EVP_MD {
54 EVP_get_digestbyname(OBJ_nid2sn(type_))
55}
56
57cfg_if! {
58 if #[cfg(ossl300)] {
59 #[inline]
60 pub unsafe fn EVP_MD_CTX_md(ctx: *const EVP_MD_CTX) -> *const EVP_MD {
61 EVP_MD_CTX_get0_md(ctx)
62 }
63
64 #[inline]
65 pub unsafe fn EVP_MD_CTX_get_size(ctx: *const EVP_MD_CTX) -> c_int {
66 EVP_MD_get_size(EVP_MD_CTX_get0_md(ctx))
67 }
68
69 #[inline]
70 pub unsafe fn EVP_MD_CTX_size(ctx: *const EVP_MD_CTX) -> c_int {
71 EVP_MD_CTX_get_size(ctx)
72 }
73
74 #[inline]
75 pub unsafe fn EVP_MD_block_size(md: *const EVP_MD) -> c_int {
76 EVP_MD_get_block_size(md)
77 }
78
79 #[inline]
80 pub unsafe fn EVP_MD_size(md: *const EVP_MD) -> c_int {
81 EVP_MD_get_size(md)
82 }
83
84 #[inline]
85 pub unsafe fn EVP_MD_type(md: *const EVP_MD) -> c_int {
86 EVP_MD_get_type(md)
87 }
88
89 #[inline]
90 pub unsafe fn EVP_CIPHER_key_length(cipher: *const EVP_CIPHER) -> c_int {
91 EVP_CIPHER_get_key_length(cipher)
92 }
93
94 #[inline]
95 pub unsafe fn EVP_CIPHER_block_size(cipher: *const EVP_CIPHER) -> c_int {
96 EVP_CIPHER_get_block_size(cipher)
97 }
98
99 #[inline]
100 pub unsafe fn EVP_CIPHER_iv_length(cipher: *const EVP_CIPHER) -> c_int {
101 EVP_CIPHER_get_iv_length(cipher)
102 }
103
104 #[inline]
105 pub unsafe fn EVP_CIPHER_nid(cipher: *const EVP_CIPHER) -> c_int {
106 EVP_CIPHER_get_nid(cipher)
107 }
108
109 #[inline]
110 pub unsafe fn EVP_CIPHER_CTX_block_size(ctx: *const EVP_CIPHER_CTX) -> c_int {
111 EVP_CIPHER_CTX_get_block_size(ctx)
112 }
113
114 #[inline]
115 pub unsafe fn EVP_CIPHER_CTX_key_length(ctx: *const EVP_CIPHER_CTX) -> c_int {
116 EVP_CIPHER_CTX_get_key_length(ctx)
117 }
118
119 #[inline]
120 pub unsafe fn EVP_CIPHER_CTX_iv_length(ctx: *const EVP_CIPHER_CTX) -> c_int {
121 EVP_CIPHER_CTX_get_iv_length(ctx)
122 }
123
124 #[inline]
125 pub unsafe fn EVP_CIPHER_CTX_num(ctx: *const EVP_CIPHER_CTX) -> c_int {
126 EVP_CIPHER_CTX_get_num(ctx)
127 }
128 } else {
129 pub unsafe fn EVP_MD_CTX_size(ctx: *const EVP_MD_CTX) -> c_int {
130 EVP_MD_size(EVP_MD_CTX_md(ctx))
131 }
132 }
133}
134#[cfg(not(ossl300))]
135#[inline]
136pub unsafe fn EVP_DigestSignUpdate(
137 ctx: *mut EVP_MD_CTX,
138 data: *const c_void,
139 dsize: size_t,
140) -> c_int {
141 EVP_DigestUpdate(ctx, data, dsize)
142}
143#[cfg(not(ossl300))]
144#[inline]
145pub unsafe fn EVP_DigestVerifyUpdate(
146 ctx: *mut EVP_MD_CTX,
147 data: *const c_void,
148 dsize: size_t,
149) -> c_int {
150 EVP_DigestUpdate(ctx, data, dsize)
151}
152#[cfg(ossl300)]
153#[inline]
154pub unsafe fn EVP_PKEY_size(pkey: *const EVP_PKEY) -> c_int {
155 EVP_PKEY_get_size(pkey)
156}
157
158cfg_if! {
159 if #[cfg(ossl300)] {
160 #[inline]
161 pub unsafe fn EVP_PKEY_id(pkey: *const EVP_PKEY) -> c_int {
162 EVP_PKEY_get_id(pkey)
163 }
164
165 #[inline]
166 pub unsafe fn EVP_PKEY_bits(pkey: *const EVP_PKEY) -> c_int {
167 EVP_PKEY_get_bits(pkey)
168 }
169
170 #[inline]
171 pub unsafe fn EVP_PKEY_security_bits(pkey: *const EVP_PKEY) -> c_int {
172 EVP_PKEY_get_security_bits(pkey)
173 }
174 }
175}
176
177pub const EVP_PKEY_OP_PARAMGEN: c_int = 1 << 1;
178pub const EVP_PKEY_OP_KEYGEN: c_int = 1 << 2;
179cfg_if! {
180 if #[cfg(ossl300)] {
181 pub const EVP_PKEY_OP_SIGN: c_int = 1 << 4;
182 pub const EVP_PKEY_OP_VERIFY: c_int = 1 << 5;
183 pub const EVP_PKEY_OP_VERIFYRECOVER: c_int = 1 << 6;
184 pub const EVP_PKEY_OP_SIGNCTX: c_int = 1 << 7;
185 pub const EVP_PKEY_OP_VERIFYCTX: c_int = 1 << 8;
186 pub const EVP_PKEY_OP_ENCRYPT: c_int = 1 << 9;
187 pub const EVP_PKEY_OP_DECRYPT: c_int = 1 << 10;
188 pub const EVP_PKEY_OP_DERIVE: c_int = 1 << 11;
189 } else {
190 pub const EVP_PKEY_OP_SIGN: c_int = 1 << 3;
191 pub const EVP_PKEY_OP_VERIFY: c_int = 1 << 4;
192 pub const EVP_PKEY_OP_VERIFYRECOVER: c_int = 1 << 5;
193 pub const EVP_PKEY_OP_SIGNCTX: c_int = 1 << 6;
194 pub const EVP_PKEY_OP_VERIFYCTX: c_int = 1 << 7;
195 pub const EVP_PKEY_OP_ENCRYPT: c_int = 1 << 8;
196 pub const EVP_PKEY_OP_DECRYPT: c_int = 1 << 9;
197 pub const EVP_PKEY_OP_DERIVE: c_int = 1 << 10;
198 }
199}
200#[cfg(ossl340)]
201pub const EVP_PKEY_OP_SIGNMSG: c_int = 1 << 14;
202#[cfg(ossl340)]
203pub const EVP_PKEY_OP_VERIFYMSG: c_int = 1 << 15;
204
205cfg_if! {
206 if #[cfg(ossl340)] {
207 pub const EVP_PKEY_OP_TYPE_SIG: c_int = EVP_PKEY_OP_SIGN
208 | EVP_PKEY_OP_SIGNMSG
209 | EVP_PKEY_OP_VERIFY
210 | EVP_PKEY_OP_VERIFYMSG
211 | EVP_PKEY_OP_VERIFYRECOVER
212 | EVP_PKEY_OP_SIGNCTX
213 | EVP_PKEY_OP_VERIFYCTX;
214 } else {
215 pub const EVP_PKEY_OP_TYPE_SIG: c_int = EVP_PKEY_OP_SIGN
216 | EVP_PKEY_OP_VERIFY
217 | EVP_PKEY_OP_VERIFYRECOVER
218 | EVP_PKEY_OP_SIGNCTX
219 | EVP_PKEY_OP_VERIFYCTX;
220 }
221}
222
223pub const EVP_PKEY_OP_TYPE_CRYPT: c_int = EVP_PKEY_OP_ENCRYPT | EVP_PKEY_OP_DECRYPT;
224
225pub const EVP_PKEY_CTRL_MD: c_int = 1;
226
227pub const EVP_PKEY_CTRL_SET_MAC_KEY: c_int = 6;
228
229pub const EVP_PKEY_CTRL_CIPHER: c_int = 12;
230
231pub const EVP_PKEY_ALG_CTRL: c_int = 0x1000;
232
233#[cfg(any(ossl111, libressl360))]
234pub const EVP_PKEY_HKDEF_MODE_EXTRACT_AND_EXPAND: c_int = 0;
235
236#[cfg(any(ossl111, libressl360))]
237pub const EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY: c_int = 1;
238
239#[cfg(any(ossl111, libressl360))]
240pub const EVP_PKEY_HKDEF_MODE_EXPAND_ONLY: c_int = 2;
241
242#[cfg(any(ossl110, libressl360))]
243pub const EVP_PKEY_CTRL_HKDF_MD: c_int = EVP_PKEY_ALG_CTRL + 3;
244
245#[cfg(any(ossl110, libressl360))]
246pub const EVP_PKEY_CTRL_HKDF_SALT: c_int = EVP_PKEY_ALG_CTRL + 4;
247
248#[cfg(any(ossl110, libressl360))]
249pub const EVP_PKEY_CTRL_HKDF_KEY: c_int = EVP_PKEY_ALG_CTRL + 5;
250
251#[cfg(any(ossl110, libressl360))]
252pub const EVP_PKEY_CTRL_HKDF_INFO: c_int = EVP_PKEY_ALG_CTRL + 6;
253
254#[cfg(any(ossl111, libressl360))]
255pub const EVP_PKEY_CTRL_HKDF_MODE: c_int = EVP_PKEY_ALG_CTRL + 7;
256
257#[cfg(any(all(ossl111, not(ossl300)), libressl360))]
258pub unsafe fn EVP_PKEY_CTX_set_hkdf_mode(ctx: *mut EVP_PKEY_CTX, mode: c_int) -> c_int {
259 EVP_PKEY_CTX_ctrl(
260 ctx,
261 -1,
262 EVP_PKEY_OP_DERIVE,
263 EVP_PKEY_CTRL_HKDF_MODE,
264 mode,
265 std::ptr::null_mut(),
266 )
267}
268
269#[cfg(any(all(ossl110, not(ossl300)), libressl360))]
270pub unsafe fn EVP_PKEY_CTX_set_hkdf_md(ctx: *mut EVP_PKEY_CTX, md: *const EVP_MD) -> c_int {
271 EVP_PKEY_CTX_ctrl(
272 ctx,
273 -1,
274 EVP_PKEY_OP_DERIVE,
275 EVP_PKEY_CTRL_HKDF_MD,
276 0,
277 md as *mut c_void,
278 )
279}
280
281#[cfg(any(all(ossl110, not(ossl300)), libressl360))]
282pub unsafe fn EVP_PKEY_CTX_set1_hkdf_salt(
283 ctx: *mut EVP_PKEY_CTX,
284 salt: *const u8,
285 saltlen: c_int,
286) -> c_int {
287 EVP_PKEY_CTX_ctrl(
288 ctx,
289 -1,
290 EVP_PKEY_OP_DERIVE,
291 EVP_PKEY_CTRL_HKDF_SALT,
292 saltlen,
293 salt as *mut c_void,
294 )
295}
296
297#[cfg(any(all(ossl110, not(ossl300)), libressl360))]
298pub unsafe fn EVP_PKEY_CTX_set1_hkdf_key(
299 ctx: *mut EVP_PKEY_CTX,
300 key: *const u8,
301 keylen: c_int,
302) -> c_int {
303 EVP_PKEY_CTX_ctrl(
304 ctx,
305 -1,
306 EVP_PKEY_OP_DERIVE,
307 EVP_PKEY_CTRL_HKDF_KEY,
308 keylen,
309 key as *mut c_void,
310 )
311}
312
313#[cfg(any(all(ossl110, not(ossl300)), libressl360))]
314pub unsafe fn EVP_PKEY_CTX_add1_hkdf_info(
315 ctx: *mut EVP_PKEY_CTX,
316 info: *const u8,
317 infolen: c_int,
318) -> c_int {
319 EVP_PKEY_CTX_ctrl(
320 ctx,
321 -1,
322 EVP_PKEY_OP_DERIVE,
323 EVP_PKEY_CTRL_HKDF_INFO,
324 infolen,
325 info as *mut c_void,
326 )
327}
328
329#[cfg(not(any(ossl300, boringssl, awslc)))]
330pub unsafe fn EVP_PKEY_CTX_set_signature_md(cxt: *mut EVP_PKEY_CTX, md: *mut EVP_MD) -> c_int {
331 EVP_PKEY_CTX_ctrl(
332 cxt,
333 -1,
334 EVP_PKEY_OP_TYPE_SIG,
335 EVP_PKEY_CTRL_MD,
336 0,
337 md as *mut c_void,
338 )
339}
340
341#[cfg(not(osslconf = "OPENSSL_NO_DEPRECATED_3_0"))]
342pub unsafe fn EVP_PKEY_assign_RSA(pkey: *mut EVP_PKEY, rsa: *mut RSA) -> c_int {
343 EVP_PKEY_assign(pkey, EVP_PKEY_RSA, rsa as *mut c_void)
344}
345
346#[cfg(not(osslconf = "OPENSSL_NO_DEPRECATED_3_0"))]
347pub unsafe fn EVP_PKEY_assign_DSA(pkey: *mut EVP_PKEY, dsa: *mut DSA) -> c_int {
348 EVP_PKEY_assign(pkey, EVP_PKEY_DSA, dsa as *mut c_void)
349}
350
351#[cfg(not(osslconf = "OPENSSL_NO_DEPRECATED_3_0"))]
352pub unsafe fn EVP_PKEY_assign_DH(pkey: *mut EVP_PKEY, dh: *mut DH) -> c_int {
353 EVP_PKEY_assign(pkey, EVP_PKEY_DH, dh as *mut c_void)
354}
355
356#[cfg(not(osslconf = "OPENSSL_NO_DEPRECATED_3_0"))]
357pub unsafe fn EVP_PKEY_assign_EC_KEY(pkey: *mut EVP_PKEY, ec_key: *mut EC_KEY) -> c_int {
358 EVP_PKEY_assign(pkey, EVP_PKEY_EC, ec_key as *mut c_void)
359}