Skip to main content

boring_sys/
lib.rs

1#![allow(
2    clippy::missing_safety_doc,
3    clippy::redundant_static_lifetimes,
4    clippy::too_many_arguments,
5    clippy::unreadable_literal,
6    clippy::upper_case_acronyms,
7    improper_ctypes,
8    non_camel_case_types,
9    non_snake_case,
10    non_upper_case_globals,
11    unused_imports
12)]
13
14use std::convert::TryInto;
15use std::ffi::c_void;
16use std::os::raw::{c_char, c_int, c_uint, c_ulong};
17
18#[allow(
19    clippy::useless_transmute,
20    clippy::derive_partial_eq_without_eq,
21    clippy::ptr_offset_with_cast,
22    dead_code,
23    unnecessary_transmutes
24)]
25mod generated {
26    include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
27}
28
29// explicitly require presence of some symbols to check if the bindings worked
30pub use generated::{ssl_compliance_policy_t, ERR_add_error_data, SSL_set1_groups}; // if these are missing, your include path is incorrect or has a wrong version of boringssl
31pub use generated::{BIO_new, OPENSSL_free, SSL_ERROR_NONE}; // if these are missing, your include path is incorrect
32#[cfg(feature = "fips")]
33pub use generated::{FIPS_mode, SSL_CTX_set_compliance_policy}; // your include path is incorrect or has a version of boringssl without FIPS support
34#[cfg(feature = "mlkem")]
35pub use generated::{MLKEM768_encap, MLKEM768_private_key_from_seed}; // your include path is incorrect or has a version of boringssl without mlkem support
36#[cfg(feature = "rpk")]
37pub use generated::{SSL_CREDENTIAL_new_raw_public_key_empty, SSL_CREDENTIAL_set1_spki}; // your include path is incorrect or has a version of boringssl without rpk support
38
39pub use generated::*;
40
41#[cfg(target_pointer_width = "64")]
42pub type BN_ULONG = u64;
43#[cfg(target_pointer_width = "32")]
44pub type BN_ULONG = u32;
45
46#[must_use]
47pub const fn ERR_PACK(l: c_int, f: c_int, r: c_int) -> c_ulong {
48    ((l as c_ulong & 0x0FF) << 24) | ((f as c_ulong & 0xFFF) << 12) | (r as c_ulong & 0xFFF)
49}
50
51#[must_use]
52pub const fn ERR_GET_LIB(l: c_uint) -> c_int {
53    ((l >> 24) & 0x0FF) as c_int
54}
55
56#[must_use]
57pub const fn ERR_GET_FUNC(l: c_uint) -> c_int {
58    ((l >> 12) & 0xFFF) as c_int
59}
60
61#[must_use]
62pub const fn ERR_GET_REASON(l: c_uint) -> c_int {
63    (l & 0xFFF) as c_int
64}
65
66pub fn init() {
67    unsafe {
68        CRYPTO_library_init();
69    }
70}
71
72// CBS_init is inline in BoringSSL, so bindgen can't generate bindings for it.
73#[inline]
74#[must_use]
75pub fn cbs_init(data: &[u8]) -> CBS {
76    CBS {
77        data: data.as_ptr(),
78        len: data.len(),
79    }
80}
81
82pub mod internal {
83    use super::EVP_MD;
84    use std::os::raw::c_int;
85
86    extern "C" {
87        /// Calculates `out_len` bytes of the TLS 1.2 PRF using `digest` and writes
88        /// them to `out`.
89        ///
90        /// This symbol is exported by BoringSSL, but it is declared in an internal
91        /// header (`crypto/fipsmodule/tls/internal.h`) and is therefore not present
92        /// in generated bindgen output.
93        pub fn CRYPTO_tls1_prf(
94            digest: *const EVP_MD,
95            out: *mut u8,
96            out_len: usize,
97            secret: *const u8,
98            secret_len: usize,
99            label: *const u8,
100            label_len: usize,
101            seed1: *const u8,
102            seed1_len: usize,
103            seed2: *const u8,
104            seed2_len: usize,
105        ) -> c_int;
106    }
107}