Skip to main content

variant_ssl_sys/
evp.rs

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