/rust/registry/src/index.crates.io-1949cf8c6b5b557f/openssl-0.10.62/src/cipher.rs
Line | Count | Source |
1 | | //! Symmetric ciphers. |
2 | | |
3 | | #[cfg(ossl300)] |
4 | | use crate::cvt_p; |
5 | | #[cfg(ossl300)] |
6 | | use crate::error::ErrorStack; |
7 | | #[cfg(ossl300)] |
8 | | use crate::lib_ctx::LibCtxRef; |
9 | | use crate::nid::Nid; |
10 | | use cfg_if::cfg_if; |
11 | | use foreign_types::{ForeignTypeRef, Opaque}; |
12 | | use openssl_macros::corresponds; |
13 | | #[cfg(ossl300)] |
14 | | use std::ffi::CString; |
15 | | use std::ops::{Deref, DerefMut}; |
16 | | #[cfg(ossl300)] |
17 | | use std::ptr; |
18 | | |
19 | | cfg_if! { |
20 | | if #[cfg(any(boringssl, ossl110, libressl273))] { |
21 | | use ffi::{EVP_CIPHER_block_size, EVP_CIPHER_iv_length, EVP_CIPHER_key_length}; |
22 | | } else { |
23 | | use libc::c_int; |
24 | | |
25 | | #[allow(bad_style)] |
26 | | pub unsafe fn EVP_CIPHER_iv_length(ptr: *const ffi::EVP_CIPHER) -> c_int { |
27 | | (*ptr).iv_len |
28 | | } |
29 | | |
30 | | #[allow(bad_style)] |
31 | | pub unsafe fn EVP_CIPHER_block_size(ptr: *const ffi::EVP_CIPHER) -> c_int { |
32 | | (*ptr).block_size |
33 | | } |
34 | | |
35 | | #[allow(bad_style)] |
36 | | pub unsafe fn EVP_CIPHER_key_length(ptr: *const ffi::EVP_CIPHER) -> c_int { |
37 | | (*ptr).key_len |
38 | | } |
39 | | } |
40 | | } |
41 | | |
42 | | cfg_if! { |
43 | | if #[cfg(ossl300)] { |
44 | | use foreign_types::ForeignType; |
45 | | |
46 | | type Inner = *mut ffi::EVP_CIPHER; |
47 | | |
48 | | impl Drop for Cipher { |
49 | | #[inline] |
50 | | fn drop(&mut self) { |
51 | | unsafe { |
52 | | ffi::EVP_CIPHER_free(self.as_ptr()); |
53 | | } |
54 | | } |
55 | | } |
56 | | |
57 | | impl ForeignType for Cipher { |
58 | | type CType = ffi::EVP_CIPHER; |
59 | | type Ref = CipherRef; |
60 | | |
61 | | #[inline] |
62 | | unsafe fn from_ptr(ptr: *mut Self::CType) -> Self { |
63 | | Cipher(ptr) |
64 | | } |
65 | | |
66 | | #[inline] |
67 | | fn as_ptr(&self) -> *mut Self::CType { |
68 | | self.0 |
69 | | } |
70 | | } |
71 | | |
72 | | impl Deref for Cipher { |
73 | | type Target = CipherRef; |
74 | | |
75 | | #[inline] |
76 | | fn deref(&self) -> &Self::Target { |
77 | | unsafe { |
78 | | CipherRef::from_ptr(self.as_ptr()) |
79 | | } |
80 | | } |
81 | | } |
82 | | |
83 | | impl DerefMut for Cipher { |
84 | | #[inline] |
85 | | fn deref_mut(&mut self) -> &mut Self::Target { |
86 | | unsafe { |
87 | | CipherRef::from_ptr_mut(self.as_ptr()) |
88 | | } |
89 | | } |
90 | | } |
91 | | } else { |
92 | | enum Inner {} |
93 | | |
94 | | impl Deref for Cipher { |
95 | | type Target = CipherRef; |
96 | | |
97 | | #[inline] |
98 | | fn deref(&self) -> &Self::Target { |
99 | | match self.0 {} |
100 | | } |
101 | | } |
102 | | |
103 | | impl DerefMut for Cipher { |
104 | | #[inline] |
105 | | fn deref_mut(&mut self) -> &mut Self::Target { |
106 | | match self.0 {} |
107 | | } |
108 | | } |
109 | | } |
110 | | } |
111 | | |
112 | | /// A symmetric cipher. |
113 | | pub struct Cipher(Inner); |
114 | | |
115 | | unsafe impl Sync for Cipher {} |
116 | | unsafe impl Send for Cipher {} |
117 | | |
118 | | impl Cipher { |
119 | | /// Looks up the cipher for a certain nid. |
120 | | #[corresponds(EVP_get_cipherbynid)] |
121 | 0 | pub fn from_nid(nid: Nid) -> Option<&'static CipherRef> { |
122 | | unsafe { |
123 | 0 | let ptr = ffi::EVP_get_cipherbyname(ffi::OBJ_nid2sn(nid.as_raw())); |
124 | 0 | if ptr.is_null() { |
125 | 0 | None |
126 | | } else { |
127 | 0 | Some(CipherRef::from_ptr(ptr as *mut _)) |
128 | | } |
129 | | } |
130 | 0 | } |
131 | | |
132 | | /// Fetches a cipher object corresponding to the specified algorithm name and properties. |
133 | | /// |
134 | | /// Requires OpenSSL 3.0.0 or newer. |
135 | | #[corresponds(EVP_CIPHER_fetch)] |
136 | | #[cfg(ossl300)] |
137 | | pub fn fetch( |
138 | | ctx: Option<&LibCtxRef>, |
139 | | algorithm: &str, |
140 | | properties: Option<&str>, |
141 | | ) -> Result<Self, ErrorStack> { |
142 | | let algorithm = CString::new(algorithm).unwrap(); |
143 | | let properties = properties.map(|s| CString::new(s).unwrap()); |
144 | | |
145 | | unsafe { |
146 | | let ptr = cvt_p(ffi::EVP_CIPHER_fetch( |
147 | | ctx.map_or(ptr::null_mut(), ForeignTypeRef::as_ptr), |
148 | | algorithm.as_ptr(), |
149 | | properties.map_or(ptr::null_mut(), |s| s.as_ptr()), |
150 | | ))?; |
151 | | |
152 | | Ok(Cipher::from_ptr(ptr)) |
153 | | } |
154 | | } |
155 | | |
156 | 0 | pub fn aes_128_ecb() -> &'static CipherRef { |
157 | 0 | unsafe { CipherRef::from_ptr(ffi::EVP_aes_128_ecb() as *mut _) } |
158 | 0 | } |
159 | | |
160 | 0 | pub fn aes_128_cbc() -> &'static CipherRef { |
161 | 0 | unsafe { CipherRef::from_ptr(ffi::EVP_aes_128_cbc() as *mut _) } |
162 | 0 | } |
163 | | |
164 | | #[cfg(not(boringssl))] |
165 | 0 | pub fn aes_128_xts() -> &'static CipherRef { |
166 | 0 | unsafe { CipherRef::from_ptr(ffi::EVP_aes_128_xts() as *mut _) } |
167 | 0 | } |
168 | | |
169 | | #[cfg(not(boringssl))] |
170 | 0 | pub fn aes_128_ctr() -> &'static CipherRef { |
171 | 0 | unsafe { CipherRef::from_ptr(ffi::EVP_aes_128_ctr() as *mut _) } |
172 | 0 | } |
173 | | |
174 | | #[cfg(not(boringssl))] |
175 | 0 | pub fn aes_128_cfb1() -> &'static CipherRef { |
176 | 0 | unsafe { CipherRef::from_ptr(ffi::EVP_aes_128_cfb1() as *mut _) } |
177 | 0 | } |
178 | | |
179 | | #[cfg(not(boringssl))] |
180 | 0 | pub fn aes_128_cfb128() -> &'static CipherRef { |
181 | 0 | unsafe { CipherRef::from_ptr(ffi::EVP_aes_128_cfb128() as *mut _) } |
182 | 0 | } |
183 | | |
184 | | #[cfg(not(boringssl))] |
185 | 0 | pub fn aes_128_cfb8() -> &'static CipherRef { |
186 | 0 | unsafe { CipherRef::from_ptr(ffi::EVP_aes_128_cfb8() as *mut _) } |
187 | 0 | } |
188 | | |
189 | 0 | pub fn aes_128_gcm() -> &'static CipherRef { |
190 | 0 | unsafe { CipherRef::from_ptr(ffi::EVP_aes_128_gcm() as *mut _) } |
191 | 0 | } |
192 | | |
193 | | #[cfg(not(boringssl))] |
194 | 0 | pub fn aes_128_ccm() -> &'static CipherRef { |
195 | 0 | unsafe { CipherRef::from_ptr(ffi::EVP_aes_128_ccm() as *mut _) } |
196 | 0 | } |
197 | | |
198 | | #[cfg(not(boringssl))] |
199 | 0 | pub fn aes_128_ofb() -> &'static CipherRef { |
200 | 0 | unsafe { CipherRef::from_ptr(ffi::EVP_aes_128_ofb() as *mut _) } |
201 | 0 | } |
202 | | |
203 | | /// Requires OpenSSL 1.1.0 or newer. |
204 | | #[cfg(all(ossl110, not(osslconf = "OPENSSL_NO_OCB")))] |
205 | 0 | pub fn aes_128_ocb() -> &'static CipherRef { |
206 | 0 | unsafe { CipherRef::from_ptr(ffi::EVP_aes_128_ocb() as *mut _) } |
207 | 0 | } |
208 | | |
209 | | /// Requires OpenSSL 1.0.2 or newer. |
210 | | #[cfg(ossl102)] |
211 | 0 | pub fn aes_128_wrap() -> &'static CipherRef { |
212 | 0 | unsafe { CipherRef::from_ptr(ffi::EVP_aes_128_wrap() as *mut _) } |
213 | 0 | } |
214 | | |
215 | | /// Requires OpenSSL 1.1.0 or newer. |
216 | | #[cfg(ossl110)] |
217 | 0 | pub fn aes_128_wrap_pad() -> &'static CipherRef { |
218 | 0 | unsafe { CipherRef::from_ptr(ffi::EVP_aes_128_wrap_pad() as *mut _) } |
219 | 0 | } |
220 | | |
221 | 0 | pub fn aes_192_ecb() -> &'static CipherRef { |
222 | 0 | unsafe { CipherRef::from_ptr(ffi::EVP_aes_192_ecb() as *mut _) } |
223 | 0 | } |
224 | | |
225 | 0 | pub fn aes_192_cbc() -> &'static CipherRef { |
226 | 0 | unsafe { CipherRef::from_ptr(ffi::EVP_aes_192_cbc() as *mut _) } |
227 | 0 | } |
228 | | |
229 | 0 | pub fn aes_192_ctr() -> &'static CipherRef { |
230 | 0 | unsafe { CipherRef::from_ptr(ffi::EVP_aes_192_ctr() as *mut _) } |
231 | 0 | } |
232 | | |
233 | | #[cfg(not(boringssl))] |
234 | 0 | pub fn aes_192_cfb1() -> &'static CipherRef { |
235 | 0 | unsafe { CipherRef::from_ptr(ffi::EVP_aes_192_cfb1() as *mut _) } |
236 | 0 | } |
237 | | |
238 | 0 | pub fn aes_192_cfb128() -> &'static CipherRef { |
239 | 0 | unsafe { CipherRef::from_ptr(ffi::EVP_aes_192_cfb128() as *mut _) } |
240 | 0 | } |
241 | | |
242 | | #[cfg(not(boringssl))] |
243 | 0 | pub fn aes_192_cfb8() -> &'static CipherRef { |
244 | 0 | unsafe { CipherRef::from_ptr(ffi::EVP_aes_192_cfb8() as *mut _) } |
245 | 0 | } |
246 | | |
247 | 0 | pub fn aes_192_gcm() -> &'static CipherRef { |
248 | 0 | unsafe { CipherRef::from_ptr(ffi::EVP_aes_192_gcm() as *mut _) } |
249 | 0 | } |
250 | | |
251 | | #[cfg(not(boringssl))] |
252 | 0 | pub fn aes_192_ccm() -> &'static CipherRef { |
253 | 0 | unsafe { CipherRef::from_ptr(ffi::EVP_aes_192_ccm() as *mut _) } |
254 | 0 | } |
255 | | |
256 | 0 | pub fn aes_192_ofb() -> &'static CipherRef { |
257 | 0 | unsafe { CipherRef::from_ptr(ffi::EVP_aes_192_ofb() as *mut _) } |
258 | 0 | } |
259 | | |
260 | | /// Requires OpenSSL 1.1.0 or newer. |
261 | | #[cfg(all(ossl110, not(osslconf = "OPENSSL_NO_OCB")))] |
262 | 0 | pub fn aes_192_ocb() -> &'static CipherRef { |
263 | 0 | unsafe { CipherRef::from_ptr(ffi::EVP_aes_192_ocb() as *mut _) } |
264 | 0 | } |
265 | | |
266 | | /// Requires OpenSSL 1.0.2 or newer. |
267 | | #[cfg(ossl102)] |
268 | 0 | pub fn aes_192_wrap() -> &'static CipherRef { |
269 | 0 | unsafe { CipherRef::from_ptr(ffi::EVP_aes_192_wrap() as *mut _) } |
270 | 0 | } |
271 | | |
272 | | /// Requires OpenSSL 1.1.0 or newer. |
273 | | #[cfg(ossl110)] |
274 | 0 | pub fn aes_192_wrap_pad() -> &'static CipherRef { |
275 | 0 | unsafe { CipherRef::from_ptr(ffi::EVP_aes_192_wrap_pad() as *mut _) } |
276 | 0 | } |
277 | | |
278 | 0 | pub fn aes_256_ecb() -> &'static CipherRef { |
279 | 0 | unsafe { CipherRef::from_ptr(ffi::EVP_aes_256_ecb() as *mut _) } |
280 | 0 | } |
281 | | |
282 | 0 | pub fn aes_256_cbc() -> &'static CipherRef { |
283 | 0 | unsafe { CipherRef::from_ptr(ffi::EVP_aes_256_cbc() as *mut _) } |
284 | 0 | } |
285 | | |
286 | 0 | pub fn aes_256_ctr() -> &'static CipherRef { |
287 | 0 | unsafe { CipherRef::from_ptr(ffi::EVP_aes_256_ctr() as *mut _) } |
288 | 0 | } |
289 | | |
290 | | #[cfg(not(boringssl))] |
291 | 0 | pub fn aes_256_cfb1() -> &'static CipherRef { |
292 | 0 | unsafe { CipherRef::from_ptr(ffi::EVP_aes_256_cfb1() as *mut _) } |
293 | 0 | } |
294 | | |
295 | 0 | pub fn aes_256_cfb128() -> &'static CipherRef { |
296 | 0 | unsafe { CipherRef::from_ptr(ffi::EVP_aes_256_cfb128() as *mut _) } |
297 | 0 | } |
298 | | |
299 | | #[cfg(not(boringssl))] |
300 | 0 | pub fn aes_256_cfb8() -> &'static CipherRef { |
301 | 0 | unsafe { CipherRef::from_ptr(ffi::EVP_aes_256_cfb8() as *mut _) } |
302 | 0 | } |
303 | | |
304 | 0 | pub fn aes_256_gcm() -> &'static CipherRef { |
305 | 0 | unsafe { CipherRef::from_ptr(ffi::EVP_aes_256_gcm() as *mut _) } |
306 | 0 | } |
307 | | |
308 | | #[cfg(not(boringssl))] |
309 | 0 | pub fn aes_256_ccm() -> &'static CipherRef { |
310 | 0 | unsafe { CipherRef::from_ptr(ffi::EVP_aes_256_ccm() as *mut _) } |
311 | 0 | } |
312 | | |
313 | 0 | pub fn aes_256_ofb() -> &'static CipherRef { |
314 | 0 | unsafe { CipherRef::from_ptr(ffi::EVP_aes_256_ofb() as *mut _) } |
315 | 0 | } |
316 | | |
317 | | /// Requires OpenSSL 1.1.0 or newer. |
318 | | #[cfg(all(ossl110, not(osslconf = "OPENSSL_NO_OCB")))] |
319 | 0 | pub fn aes_256_ocb() -> &'static CipherRef { |
320 | 0 | unsafe { CipherRef::from_ptr(ffi::EVP_aes_256_ocb() as *mut _) } |
321 | 0 | } |
322 | | |
323 | | /// Requires OpenSSL 1.0.2 or newer. |
324 | | #[cfg(ossl102)] |
325 | 0 | pub fn aes_256_wrap() -> &'static CipherRef { |
326 | 0 | unsafe { CipherRef::from_ptr(ffi::EVP_aes_256_wrap() as *mut _) } |
327 | 0 | } |
328 | | |
329 | | /// Requires OpenSSL 1.1.0 or newer. |
330 | | #[cfg(ossl110)] |
331 | 0 | pub fn aes_256_wrap_pad() -> &'static CipherRef { |
332 | 0 | unsafe { CipherRef::from_ptr(ffi::EVP_aes_256_wrap_pad() as *mut _) } |
333 | 0 | } |
334 | | |
335 | | #[cfg(not(osslconf = "OPENSSL_NO_BF"))] |
336 | 0 | pub fn bf_cbc() -> &'static CipherRef { |
337 | 0 | unsafe { CipherRef::from_ptr(ffi::EVP_bf_cbc() as *mut _) } |
338 | 0 | } |
339 | | |
340 | | #[cfg(not(osslconf = "OPENSSL_NO_BF"))] |
341 | 0 | pub fn bf_ecb() -> &'static CipherRef { |
342 | 0 | unsafe { CipherRef::from_ptr(ffi::EVP_bf_ecb() as *mut _) } |
343 | 0 | } |
344 | | |
345 | | #[cfg(not(osslconf = "OPENSSL_NO_BF"))] |
346 | 0 | pub fn bf_cfb64() -> &'static CipherRef { |
347 | 0 | unsafe { CipherRef::from_ptr(ffi::EVP_bf_cfb64() as *mut _) } |
348 | 0 | } |
349 | | |
350 | | #[cfg(not(osslconf = "OPENSSL_NO_BF"))] |
351 | 0 | pub fn bf_ofb() -> &'static CipherRef { |
352 | 0 | unsafe { CipherRef::from_ptr(ffi::EVP_bf_ofb() as *mut _) } |
353 | 0 | } |
354 | | |
355 | 0 | pub fn des_cbc() -> &'static CipherRef { |
356 | 0 | unsafe { CipherRef::from_ptr(ffi::EVP_des_cbc() as *mut _) } |
357 | 0 | } |
358 | | |
359 | 0 | pub fn des_ecb() -> &'static CipherRef { |
360 | 0 | unsafe { CipherRef::from_ptr(ffi::EVP_des_ecb() as *mut _) } |
361 | 0 | } |
362 | | |
363 | 0 | pub fn des_ede3() -> &'static CipherRef { |
364 | 0 | unsafe { CipherRef::from_ptr(ffi::EVP_des_ede3() as *mut _) } |
365 | 0 | } |
366 | | |
367 | 0 | pub fn des_ede3_cbc() -> &'static CipherRef { |
368 | 0 | unsafe { CipherRef::from_ptr(ffi::EVP_des_ede3_cbc() as *mut _) } |
369 | 0 | } |
370 | | |
371 | | #[cfg(not(boringssl))] |
372 | 0 | pub fn des_ede3_cfb64() -> &'static CipherRef { |
373 | 0 | unsafe { CipherRef::from_ptr(ffi::EVP_des_ede3_cfb64() as *mut _) } |
374 | 0 | } |
375 | | |
376 | | #[cfg(not(osslconf = "OPENSSL_NO_RC4"))] |
377 | 0 | pub fn rc4() -> &'static CipherRef { |
378 | 0 | unsafe { CipherRef::from_ptr(ffi::EVP_rc4() as *mut _) } |
379 | 0 | } |
380 | | |
381 | | #[cfg(not(osslconf = "OPENSSL_NO_CAMELLIA"))] |
382 | 0 | pub fn camellia128_cfb128() -> &'static CipherRef { |
383 | 0 | unsafe { CipherRef::from_ptr(ffi::EVP_camellia_128_cfb128() as *mut _) } |
384 | 0 | } |
385 | | |
386 | | #[cfg(not(osslconf = "OPENSSL_NO_CAMELLIA"))] |
387 | 0 | pub fn camellia128_ecb() -> &'static CipherRef { |
388 | 0 | unsafe { CipherRef::from_ptr(ffi::EVP_camellia_128_ecb() as *mut _) } |
389 | 0 | } |
390 | | |
391 | | #[cfg(not(osslconf = "OPENSSL_NO_CAMELLIA"))] |
392 | 0 | pub fn camellia128_cbc() -> &'static CipherRef { |
393 | 0 | unsafe { CipherRef::from_ptr(ffi::EVP_camellia_128_cbc() as *mut _) } |
394 | 0 | } |
395 | | |
396 | | #[cfg(not(osslconf = "OPENSSL_NO_CAMELLIA"))] |
397 | 0 | pub fn camellia192_cfb128() -> &'static CipherRef { |
398 | 0 | unsafe { CipherRef::from_ptr(ffi::EVP_camellia_192_cfb128() as *mut _) } |
399 | 0 | } |
400 | | |
401 | | #[cfg(not(osslconf = "OPENSSL_NO_CAMELLIA"))] |
402 | 0 | pub fn camellia192_ecb() -> &'static CipherRef { |
403 | 0 | unsafe { CipherRef::from_ptr(ffi::EVP_camellia_192_ecb() as *mut _) } |
404 | 0 | } |
405 | | |
406 | | #[cfg(not(osslconf = "OPENSSL_NO_CAMELLIA"))] |
407 | 0 | pub fn camellia192_cbc() -> &'static CipherRef { |
408 | 0 | unsafe { CipherRef::from_ptr(ffi::EVP_camellia_192_cbc() as *mut _) } |
409 | 0 | } |
410 | | |
411 | | #[cfg(not(osslconf = "OPENSSL_NO_CAMELLIA"))] |
412 | 0 | pub fn camellia256_cfb128() -> &'static CipherRef { |
413 | 0 | unsafe { CipherRef::from_ptr(ffi::EVP_camellia_256_cfb128() as *mut _) } |
414 | 0 | } |
415 | | |
416 | | #[cfg(not(osslconf = "OPENSSL_NO_CAMELLIA"))] |
417 | 0 | pub fn camellia256_ecb() -> &'static CipherRef { |
418 | 0 | unsafe { CipherRef::from_ptr(ffi::EVP_camellia_256_ecb() as *mut _) } |
419 | 0 | } |
420 | | |
421 | | #[cfg(not(osslconf = "OPENSSL_NO_CAMELLIA"))] |
422 | 0 | pub fn camellia256_cbc() -> &'static CipherRef { |
423 | 0 | unsafe { CipherRef::from_ptr(ffi::EVP_camellia_256_cbc() as *mut _) } |
424 | 0 | } |
425 | | |
426 | | #[cfg(not(osslconf = "OPENSSL_NO_CAST"))] |
427 | 0 | pub fn cast5_cfb64() -> &'static CipherRef { |
428 | 0 | unsafe { CipherRef::from_ptr(ffi::EVP_cast5_cfb64() as *mut _) } |
429 | 0 | } |
430 | | |
431 | | #[cfg(not(osslconf = "OPENSSL_NO_CAST"))] |
432 | 0 | pub fn cast5_ecb() -> &'static CipherRef { |
433 | 0 | unsafe { CipherRef::from_ptr(ffi::EVP_cast5_ecb() as *mut _) } |
434 | 0 | } |
435 | | |
436 | | #[cfg(not(osslconf = "OPENSSL_NO_CAST"))] |
437 | 0 | pub fn cast5_cbc() -> &'static CipherRef { |
438 | 0 | unsafe { CipherRef::from_ptr(ffi::EVP_cast5_cbc() as *mut _) } |
439 | 0 | } |
440 | | |
441 | | #[cfg(not(osslconf = "OPENSSL_NO_IDEA"))] |
442 | | pub fn idea_cfb64() -> &'static CipherRef { |
443 | | unsafe { CipherRef::from_ptr(ffi::EVP_idea_cfb64() as *mut _) } |
444 | | } |
445 | | |
446 | | #[cfg(not(osslconf = "OPENSSL_NO_IDEA"))] |
447 | | pub fn idea_ecb() -> &'static CipherRef { |
448 | | unsafe { CipherRef::from_ptr(ffi::EVP_idea_ecb() as *mut _) } |
449 | | } |
450 | | |
451 | | #[cfg(not(osslconf = "OPENSSL_NO_IDEA"))] |
452 | | pub fn idea_cbc() -> &'static CipherRef { |
453 | | unsafe { CipherRef::from_ptr(ffi::EVP_idea_cbc() as *mut _) } |
454 | | } |
455 | | |
456 | | #[cfg(all(any(ossl110, libressl310), not(osslconf = "OPENSSL_NO_CHACHA")))] |
457 | 0 | pub fn chacha20() -> &'static CipherRef { |
458 | 0 | unsafe { CipherRef::from_ptr(ffi::EVP_chacha20() as *mut _) } |
459 | 0 | } |
460 | | |
461 | | #[cfg(all(any(ossl110, libressl360), not(osslconf = "OPENSSL_NO_CHACHA")))] |
462 | 0 | pub fn chacha20_poly1305() -> &'static CipherRef { |
463 | 0 | unsafe { CipherRef::from_ptr(ffi::EVP_chacha20_poly1305() as *mut _) } |
464 | 0 | } |
465 | | |
466 | | #[cfg(not(osslconf = "OPENSSL_NO_SEED"))] |
467 | 0 | pub fn seed_cbc() -> &'static CipherRef { |
468 | 0 | unsafe { CipherRef::from_ptr(ffi::EVP_seed_cbc() as *mut _) } |
469 | 0 | } |
470 | | |
471 | | #[cfg(not(osslconf = "OPENSSL_NO_SEED"))] |
472 | 0 | pub fn seed_cfb128() -> &'static CipherRef { |
473 | 0 | unsafe { CipherRef::from_ptr(ffi::EVP_seed_cfb128() as *mut _) } |
474 | 0 | } |
475 | | |
476 | | #[cfg(not(osslconf = "OPENSSL_NO_SEED"))] |
477 | 0 | pub fn seed_ecb() -> &'static CipherRef { |
478 | 0 | unsafe { CipherRef::from_ptr(ffi::EVP_seed_ecb() as *mut _) } |
479 | 0 | } |
480 | | |
481 | | #[cfg(not(osslconf = "OPENSSL_NO_SEED"))] |
482 | 0 | pub fn seed_ofb() -> &'static CipherRef { |
483 | 0 | unsafe { CipherRef::from_ptr(ffi::EVP_seed_ofb() as *mut _) } |
484 | 0 | } |
485 | | |
486 | | #[cfg(all(any(ossl111, libressl291), not(osslconf = "OPENSSL_NO_SM4")))] |
487 | 0 | pub fn sm4_ecb() -> &'static CipherRef { |
488 | 0 | unsafe { CipherRef::from_ptr(ffi::EVP_sm4_ecb() as *mut _) } |
489 | 0 | } |
490 | | |
491 | | #[cfg(all(any(ossl111, libressl291), not(osslconf = "OPENSSL_NO_SM4")))] |
492 | 0 | pub fn sm4_cbc() -> &'static CipherRef { |
493 | 0 | unsafe { CipherRef::from_ptr(ffi::EVP_sm4_cbc() as *mut _) } |
494 | 0 | } |
495 | | |
496 | | #[cfg(all(any(ossl111, libressl291), not(osslconf = "OPENSSL_NO_SM4")))] |
497 | 0 | pub fn sm4_ctr() -> &'static CipherRef { |
498 | 0 | unsafe { CipherRef::from_ptr(ffi::EVP_sm4_ctr() as *mut _) } |
499 | 0 | } |
500 | | |
501 | | #[cfg(all(any(ossl111, libressl291), not(osslconf = "OPENSSL_NO_SM4")))] |
502 | 0 | pub fn sm4_cfb128() -> &'static CipherRef { |
503 | 0 | unsafe { CipherRef::from_ptr(ffi::EVP_sm4_cfb128() as *mut _) } |
504 | 0 | } |
505 | | |
506 | | #[cfg(all(any(ossl111, libressl291), not(osslconf = "OPENSSL_NO_SM4")))] |
507 | 0 | pub fn sm4_ofb() -> &'static CipherRef { |
508 | 0 | unsafe { CipherRef::from_ptr(ffi::EVP_sm4_ofb() as *mut _) } |
509 | 0 | } |
510 | | } |
511 | | |
512 | | /// A reference to a [`Cipher`]. |
513 | | pub struct CipherRef(Opaque); |
514 | | |
515 | | impl ForeignTypeRef for CipherRef { |
516 | | type CType = ffi::EVP_CIPHER; |
517 | | } |
518 | | |
519 | | unsafe impl Sync for CipherRef {} |
520 | | unsafe impl Send for CipherRef {} |
521 | | |
522 | | impl CipherRef { |
523 | | /// Returns the cipher's Nid. |
524 | | #[corresponds(EVP_CIPHER_nid)] |
525 | 0 | pub fn nid(&self) -> Nid { |
526 | 0 | let nid = unsafe { ffi::EVP_CIPHER_nid(self.as_ptr()) }; |
527 | 0 | Nid::from_raw(nid) |
528 | 0 | } |
529 | | |
530 | | /// Returns the length of keys used with this cipher. |
531 | | #[corresponds(EVP_CIPHER_key_length)] |
532 | 0 | pub fn key_length(&self) -> usize { |
533 | 0 | unsafe { EVP_CIPHER_key_length(self.as_ptr()) as usize } |
534 | 0 | } |
535 | | |
536 | | /// Returns the length of the IV used with this cipher. |
537 | | /// |
538 | | /// # Note |
539 | | /// |
540 | | /// Ciphers that do not use an IV have an IV length of 0. |
541 | | #[corresponds(EVP_CIPHER_iv_length)] |
542 | 0 | pub fn iv_length(&self) -> usize { |
543 | 0 | unsafe { EVP_CIPHER_iv_length(self.as_ptr()) as usize } |
544 | 0 | } |
545 | | |
546 | | /// Returns the block size of the cipher. |
547 | | /// |
548 | | /// # Note |
549 | | /// |
550 | | /// Stream ciphers have a block size of 1. |
551 | | #[corresponds(EVP_CIPHER_block_size)] |
552 | 0 | pub fn block_size(&self) -> usize { |
553 | 0 | unsafe { EVP_CIPHER_block_size(self.as_ptr()) as usize } |
554 | 0 | } |
555 | | } |