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