boring_imp/
lib.rs

1//! Bindings to BoringSSL
2//!
3//! This crate provides a safe interface to the BoringSSL cryptography library.
4//!
5//! # Versioning
6//!
7//! ## Crate versioning
8//!
9//! The crate and all the related crates (FFI bindings, etc.) are released simultaneously and all
10//! bumped to the same version disregard whether particular crate has any API changes or not.
11//! However, semantic versioning guarantees still hold, as all the crate versions will be updated
12//! based on the crate with most significant changes.
13//!
14//! ## BoringSSL version
15//!
16//! By default, the crate aims to statically link with the latest BoringSSL master branch.
17//! *Note*: any BoringSSL revision bumps will be released as a major version update of all crates.
18//!
19//! # Compilation and linking options
20//!
21//! ## Environment variables
22//!
23//! This crate uses various environment variables to tweak how boring is built. The variables
24//! are all prefixed by `BORING_BSSL_` for non-FIPS builds, and by `BORING_BSSL_FIPS_` for FIPS builds.
25//!
26//! ## Support for pre-built binaries or custom source
27//!
28//! While this crate can build BoringSSL on its own, you may want to provide pre-built binaries instead.
29//! To do so, specify the environment variable `BORING_BSSL{,_FIPS}_PATH` with the path to the binaries.
30//!
31//! You can also provide specific headers by setting `BORING_BSSL{,_FIPS}_INCLUDE_PATH`.
32//!
33//! _Notes_: The crate will look for headers in the`$BORING_BSSL{,_FIPS}_INCLUDE_PATH/openssl/`
34//! folder, make sure to place your headers there.
35//!
36//! In alternative a different path for the BoringSSL source code directory can be specified by setting
37//! `BORING_BSSL{,_FIPS}_SOURCE_PATH` which will automatically be compiled during the build process.
38//!
39//! _Warning_: When providing a different version of BoringSSL make sure to use a compatible one, the
40//! crate relies on the presence of certain functions.
41//!
42//! ## Building with a FIPS-validated module
43//!
44//! Only BoringCrypto module version `853ca1ea1168dff08011e5d42d94609cc0ca2e27`, as certified with
45//! [FIPS 140-2 certificate 4407](https://csrc.nist.gov/projects/cryptographic-module-validation-program/certificate/4407)
46//! is supported by this crate. Support is enabled by this crate's `fips` feature.
47//!
48//! `boring-sys` comes with a test that FIPS is enabled/disabled depending on the feature flag. You can run it as follows:
49//!
50//! ```bash
51//! $ cargo test --features fips fips::is_enabled
52//! ```
53//!
54//! ## Linking current BoringSSL version with precompiled FIPS-validated module (`bcm.o`)
55//!
56//! It's possible to link latest supported version of BoringSSL with FIPS-validated crypto module
57//! (`bcm.o`). To enable this compilation option one should enable `fips-link-precompiled`
58//! compilation feature and provide a `BORING_BSSL_FIPS_PRECOMPILED_BCM_O` env variable with a path to the
59//! precompiled FIPS-validated `bcm.o` module.
60//!
61//! Note that `BORING_BSSL_PRECOMPILED_BCM_O` is never used, as linking BoringSSL with precompiled non-FIPS
62//! module is not supported.
63//!
64//! # Optional patches
65//!
66//! ## Raw Public Key
67//!
68//! The crate can be compiled with [RawPublicKey](https://datatracker.ietf.org/doc/html/rfc7250)
69//! support by turning on `rpk` compilation feature.
70//!
71//! ## Experimental post-quantum cryptography
72//!
73//! The crate can be compiled with [post-quantum cryptography](https://blog.cloudflare.com/post-quantum-for-all/)
74//! support by turning on `post-quantum` compilation feature.
75//!
76//! Upstream BoringSSL support the post-quantum hybrid key agreement `X25519Kyber768Draft00`. Most
77//! users should stick to that one. Enabling this feature, adds a few other post-quantum key
78//! agreements:
79//!
80//! - `X25519Kyber768Draft00Old` is the same as `X25519Kyber768Draft00`, but under its old codepoint.
81//! - `X25519Kyber512Draft00`. Similar to `X25519Kyber768Draft00`, but uses level 1 parameter set for
82//!    Kyber. Not recommended. It's useful to test whether the shorter ClientHello upsets fewer middle
83//!    boxes.
84//! - `P256Kyber768Draft00`. Similar again to `X25519Kyber768Draft00`, but uses P256 as classical
85//!    part. It uses a non-standard codepoint. Not recommended.
86//! - `IPDWing`. A preliminary version of
87//!    [X-Wing](https://datatracker.ietf.org/doc/draft-connolly-cfrg-xwing-kem/02/).
88//!    Similar to `X25519Kyber768Draft00Old`, but uses a newer (but not yet final) version of Kyber
89//!    called ML-KEM-ipd. Not recommended.
90//!
91//! Presently all these key agreements are deployed by Cloudflare, but we do not guarantee continued
92//! support for them.
93
94#![cfg_attr(docsrs, feature(doc_auto_cfg))]
95
96#[macro_use]
97extern crate bitflags;
98#[macro_use]
99extern crate foreign_types;
100extern crate boring_sys as ffi;
101extern crate libc;
102
103#[cfg(test)]
104extern crate hex;
105
106#[doc(inline)]
107pub use crate::ffi::init;
108
109use libc::{c_int, size_t};
110
111use crate::error::ErrorStack;
112
113#[macro_use]
114mod macros;
115
116mod bio;
117#[macro_use]
118mod util;
119pub mod aes;
120pub mod asn1;
121pub mod base64;
122pub mod bn;
123pub mod conf;
124pub mod derive;
125pub mod dh;
126pub mod dsa;
127pub mod ec;
128pub mod ecdsa;
129pub mod error;
130pub mod ex_data;
131pub mod fips;
132pub mod hash;
133pub mod memcmp;
134pub mod nid;
135pub mod pkcs12;
136pub mod pkcs5;
137pub mod pkey;
138pub mod rand;
139pub mod rsa;
140pub mod sha;
141pub mod sign;
142pub mod srtp;
143pub mod ssl;
144pub mod stack;
145pub mod string;
146pub mod symm;
147pub mod version;
148pub mod x509;
149
150fn cvt_p<T>(r: *mut T) -> Result<*mut T, ErrorStack> {
151    if r.is_null() {
152        Err(ErrorStack::get())
153    } else {
154        Ok(r)
155    }
156}
157
158fn cvt_0(r: size_t) -> Result<size_t, ErrorStack> {
159    if r == 0 {
160        Err(ErrorStack::get())
161    } else {
162        Ok(r)
163    }
164}
165
166fn cvt_0i(r: c_int) -> Result<c_int, ErrorStack> {
167    if r == 0 {
168        Err(ErrorStack::get())
169    } else {
170        Ok(r)
171    }
172}
173
174fn cvt(r: c_int) -> Result<c_int, ErrorStack> {
175    if r <= 0 {
176        Err(ErrorStack::get())
177    } else {
178        Ok(r)
179    }
180}
181
182fn cvt_n(r: c_int) -> Result<c_int, ErrorStack> {
183    if r < 0 {
184        Err(ErrorStack::get())
185    } else {
186        Ok(r)
187    }
188}