Skip to main content

aws_lc_rs/
lib.rs

1// Copyright 2015-2016 Brian Smith.
2// SPDX-License-Identifier: ISC
3// Modifications copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
4// SPDX-License-Identifier: Apache-2.0 OR ISC
5#![cfg_attr(not(clippy), allow(unexpected_cfgs))]
6#![cfg_attr(not(clippy), allow(unknown_lints))]
7#![allow(clippy::doc_markdown)]
8//! A [*ring*](https://github.com/briansmith/ring)-compatible crypto library using the cryptographic
9//! operations provided by [*AWS-LC*](https://github.com/aws/aws-lc). It uses either the
10//! auto-generated [*aws-lc-sys*](https://crates.io/crates/aws-lc-sys) or
11//! [*aws-lc-fips-sys*](https://crates.io/crates/aws-lc-fips-sys)
12//! Foreign Function Interface (FFI) crates found in this repository for invoking *AWS-LC*.
13//!
14//! # Build
15//!
16//! `aws-lc-rs` is available through [crates.io](https://crates.io/crates/aws-lc-rs). It can
17//! be added to your project in the [standard way](https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html)
18//! using `Cargo.toml`:
19//!
20//! ```toml
21//! [dependencies]
22//! aws-lc-rs = "1"
23//! ```
24//! Consuming projects will need a C/C++ compiler to build.
25//!
26//! **Non-FIPS builds (default):**
27//! * CMake is **never** required
28//! * Bindgen is **never** required (pre-generated bindings are provided)
29//! * Go is **never** required
30//!
31//! **FIPS builds:** Require **CMake**, **Go**, and potentially **bindgen** depending on the target platform.
32//!
33//! See our [User Guide](https://aws.github.io/aws-lc-rs/) for guidance on installing build requirements.
34//!
35//! # Feature Flags
36//!
37//! #### alloc (default)
38//!
39//! Allows implementation to allocate values of arbitrary size. (The meaning of this feature differs
40//! from the "alloc" feature of *ring*.) Currently, this is required by the `io::writer` module.
41//!
42//! #### ring-io (default)
43//!
44//! Enable feature to access the  `io`  module.
45//!
46//! #### ring-sig-verify (default)
47//!
48//! Enable feature to preserve compatibility with ring's `signature::VerificationAlgorithm::verify`
49//! function. This adds a requirement on `untrusted = "0.7.1"`.
50//!
51//! #### fips
52//!
53//! Enable this feature to have aws-lc-rs use the [*aws-lc-fips-sys*](https://crates.io/crates/aws-lc-fips-sys)
54//! crate for the cryptographic implementations. The aws-lc-fips-sys crate provides bindings to the
55//! latest version of the AWS-LC-FIPS module that has completed FIPS validation testing by an
56//! accredited lab and has been submitted to NIST for certification. This will continue to be the
57//! case as we periodically submit new versions of the AWS-LC-FIPS module to NIST for certification.
58//! Currently, aws-lc-fips-sys binds to
59//! [AWS-LC-FIPS 3.0.x](https://github.com/aws/aws-lc/tree/fips-2024-09-27).
60//!
61//! Consult with your local FIPS compliance team to determine the version of AWS-LC-FIPS module that you require. Consumers
62//! needing to remain on a previous version of the AWS-LC-FIPS module should pin to specific versions of aws-lc-rs to avoid
63//! automatically being upgraded to a newer module version.
64//! (See [cargo's documentation](https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html)
65//! on how to specify dependency versions.)
66//!
67//! | AWS-LC-FIPS module | aws-lc-rs |
68//! |--------------------|-----------|
69//! | 2.0.x              | \<1.12.0  |
70//! | 3.0.x              | *latest*  |
71//!
72//! Refer to the
73//! [NIST Cryptographic Module Validation Program's Modules In Progress List](https://csrc.nist.gov/Projects/cryptographic-module-validation-program/modules-in-process/Modules-In-Process-List)
74//! for the latest status of the static or dynamic AWS-LC Cryptographic Module. Please see the
75//! [FIPS.md in the aws-lc repository](https://github.com/aws/aws-lc/blob/main/crypto/fipsmodule/FIPS.md)
76//! for relevant security policies and information on supported operating environments.
77//! We will also update our release notes and documentation to reflect any changes in FIPS certification status.
78//!
79//! #### non-fips
80//!
81//! Enable this feature to guarantee that the non-FIPS [*aws-lc-sys*](https://crates.io/crates/aws-lc-sys)
82//! crate is used for cryptographic implementations. This feature is mutually exclusive with the `fips`
83//! feature - enabling both will result in a compile-time error. Use this feature when you need a
84//! compile-time guarantee that your build is using the non-FIPS cryptographic module.
85//!
86//! #### asan
87//!
88//! Performs an "address sanitizer" build. This can be used to help detect memory leaks. See the
89//! ["Address Sanitizer" section](https://doc.rust-lang.org/beta/unstable-book/compiler-flags/sanitizer.html#addresssanitizer)
90//! of the [Rust Unstable Book](https://doc.rust-lang.org/beta/unstable-book/).
91//!
92//! **Preferred alternative:** Instead of the `asan` feature flag, you can set the
93//! `AWS_LC_SYS_SANITIZER` environment variable (or `AWS_LC_FIPS_SYS_SANITIZER` for FIPS builds)
94//! to one of: `asan`, `msan`, `tsan`. This approach does not require forwarding a feature through
95//! the dependency graph and also supports MemorySanitizer and ThreadSanitizer.
96//! MSAN and TSAN require the standard library to be rebuilt with sanitizer instrumentation, so
97//! you must install the `rust-src` component and pass `-Zbuild-std` to Cargo. For example:
98//!
99//! ```bash
100//! rustup component add rust-src --toolchain nightly
101//! AWS_LC_SYS_SANITIZER=msan RUSTFLAGS="-Zsanitizer=memory -Zsanitizer-memory-track-origins" \
102//!     cargo +nightly test -Zbuild-std --target x86_64-unknown-linux-gnu
103//! ```
104//!
105//! **Note:** MSAN is not currently supported for FIPS builds due to a missing preprocessor guard
106//! in the upstream AWS-LC `bcm.c` integrity check.
107//!
108//! #### bindgen
109//!
110//! Causes `aws-lc-sys` or `aws-lc-fips-sys` to generates fresh bindings for AWS-LC instead of using
111//! the pre-generated bindings. This feature requires `libclang` to be installed. See the
112//! [requirements](https://rust-lang.github.io/rust-bindgen/requirements.html)
113//! for [rust-bindgen](https://github.com/rust-lang/rust-bindgen)
114//!
115//! #### prebuilt-nasm
116//!
117//! Enables the use of crate provided prebuilt NASM objects under certain conditions. This only affects builds for
118//! Windows x86-64 platforms. This feature is ignored if the "fips" feature is also enabled.
119//!
120//! Use of prebuilt NASM objects is prevented if either of the following conditions are true:
121//! * The NASM assembler is detected in the build environment
122//! * `AWS_LC_SYS_PREBUILT_NASM` environment variable is set with a value of `0`
123//!
124//! Be aware that [features are additive](https://doc.rust-lang.org/cargo/reference/features.html#feature-unification);
125//! by enabling this feature, it is enabled for all crates within the same build.
126//!
127//! #### dev-tests-only
128//!
129//! Enables the `rand::unsealed` module, which re-exports the normally sealed `SecureRandom` trait.
130//! This allows consumers to provide their own implementations of `SecureRandom` (e.g., a
131//! deterministic RNG) for testing purposes. When enabled, a `mut_fill` method is also available on
132//! `SecureRandom`.
133//!
134//! This feature is restricted to **dev/debug profile builds only** — attempting to use it in a
135//! release build will result in a compile-time error.
136//!
137//! It can be enabled in two ways:
138//! * **Feature flag:** `cargo test --features dev-tests-only`
139//! * **Environment variable:** `AWS_LC_RS_DEV_TESTS_ONLY=1 cargo test`
140//!
141//! **⚠️ Warning:** This feature is intended **only** for development and testing. It must not be
142//! used in production builds. The `rand::unsealed` module and `mut_fill` method are not part of the
143//! stable public API and may change without notice.
144//!
145//! #### legacy-des
146//!
147//! Enables single DES and Triple DES as opt-in symmetric ciphers under the
148//! [`cipher`] module, exposing `DES_FOR_LEGACY_USE_ONLY` (single DES),
149//! `DES_EDE_FOR_LEGACY_USE_ONLY` (2-key Triple DES) and
150//! `DES_EDE3_FOR_LEGACY_USE_ONLY` (3-key Triple DES), together with the
151//! supporting key-length and IV-length constants. Only CBC and ECB operating
152//! modes are supported.
153//!
154//! **⚠️ Warning:** Single DES and Triple DES are legacy algorithms. Single DES
155//! provides only 56 bits of effective security and has been considered insecure
156//! for decades. Triple DES has been disallowed for encryption by
157//! [NIST SP 800-131A Rev. 2](https://csrc.nist.gov/publications/detail/sp/800-131a/rev-2/final):
158//! 2-key Triple DES after 2015, and 3-key Triple DES after 2023. This feature
159//! exists solely to support interoperability. All exposed items are marked
160//! `#[deprecated]` so that any use site produces a compiler warning. **Do not
161//! use single DES or Triple DES in new designs.** If you only need
162//! confidentiality, prefer AES-GCM or another AEAD from the [`aead`] module;
163//! if you specifically need a block cipher, prefer one of the AES algorithms
164//! in [`cipher`].
165//!
166//! **Build-time impact:** The default `aws-lc-sys` bindings do not expose the
167//! low-level `DES_*` symbols this feature relies on, so enabling `legacy-des`
168//! also enables `aws-lc-sys?/all-bindings`. Concretely:
169//!
170//! * On platforms with pre-generated per-target bindings, the build switches
171//!   from the universal bindings to the per-target bindings (no extra
172//!   tooling required).
173//! * On platforms *without* pre-generated bindings for the selected target,
174//!   `bindgen` is invoked at build time, which requires `libclang` to be
175//!   installed in the build environment.
176//!
177//! Because [features are
178//! additive](https://doc.rust-lang.org/cargo/reference/features.html#feature-unification),
179//! enabling `legacy-des` anywhere in your dependency graph applies the
180//! above to the entire build.
181//!
182//! # Use of prebuilt NASM objects
183//!
184//! Prebuilt NASM objects are **only** applicable to Windows x86-64 platforms. They are **never** used on any other platform (Linux, macOS, etc.).
185//!
186//! For Windows x86 and x86-64, NASM is required for assembly code compilation. On these platforms,
187//! we recommend that you install [the NASM assembler](https://www.nasm.us/). **If NASM is
188//! detected in the build environment, it is always used** to compile the assembly files. Prebuilt NASM objects are only used as a fallback.
189//!
190//! If a NASM assembler is not available, and the "fips" feature is not enabled, then the build fails unless one of the following conditions are true:
191//!
192//! * You are building for `x86-64` and either:
193//!    * The `AWS_LC_SYS_PREBUILT_NASM` environment variable is found and has a value of "1"; OR
194//!    * `AWS_LC_SYS_PREBUILT_NASM` is *not found* in the environment AND the "prebuilt-nasm" feature has been enabled.
195//!
196//! If the above cases apply, then the crate provided prebuilt NASM objects will be used for the build. To prevent usage of prebuilt NASM
197//! objects, install NASM in the build environment and/or set the variable `AWS_LC_SYS_PREBUILT_NASM` to `0` in the build environment to prevent their use.
198//!
199//! ## About prebuilt NASM objects
200//!
201//! Prebuilt NASM objects are generated using automation similar to the crate provided pregenerated bindings. See the repository's
202//! [GitHub workflow configuration](https://github.com/aws/aws-lc-rs/blob/main/.github/workflows/sys-bindings-generator.yml) for more information.
203//! The prebuilt NASM objects are checked into the repository
204//! and are [available for inspection](https://github.com/aws/aws-lc-rs/tree/main/aws-lc-sys/builder/prebuilt-nasm).
205//! For each PR submitted,
206//! [CI verifies](https://github.com/aws/aws-lc-rs/blob/main/.github/workflows/tests.yml)
207//! that the NASM objects newly built from source match the NASM objects currently in the repository.
208//!
209//! # *ring*-compatibility
210//!
211//! Although this library attempts to be fully compatible with *ring* (v0.16.x), there are a few places where our
212//! behavior is observably different.
213//!
214//! * Our implementation requires the `std` library. We currently do not support a
215//!   [`#![no_std]`](https://docs.rust-embedded.org/book/intro/no-std.html) build.
216//! * `aws-lc-rs` supports the platforms supported by `aws-lc-sys` and AWS-LC. See the
217//!   [Platform Support](https://aws.github.io/aws-lc-rs/platform_support.html) page in our User Guide.
218//! * `Ed25519KeyPair::from_pkcs8` and `Ed25519KeyPair::from_pkcs8_maybe_unchecked` both support
219//!   parsing of v1 or v2 PKCS#8 documents. If a v2 encoded key is provided to either function,
220//!   public key component, if present, will be verified to match the one derived from the encoded
221//!   private key.
222//!
223//! # Post-Quantum Cryptography
224//!
225//! Details on the post-quantum algorithms supported by aws-lc-rs can be found at
226//! [PQREADME](https://github.com/aws/aws-lc/tree/main/crypto/fipsmodule/PQREADME.md).
227//!
228//! # Motivation
229//!
230//! Rust developers increasingly need to deploy applications that meet US and Canadian government
231//! cryptographic requirements. We evaluated how to deliver FIPS validated cryptography in idiomatic
232//! and performant Rust, built around our AWS-LC offering. We found that the popular ring (v0.16)
233//! library fulfilled much of the cryptographic needs in the Rust community, but it did not meet the
234//! needs of developers with FIPS requirements. Our intention is to contribute a drop-in replacement
235//! for ring that provides FIPS support and is compatible with the ring API. Rust developers with
236//! prescribed cryptographic requirements can seamlessly integrate aws-lc-rs into their applications
237//! and deploy them into AWS Regions.
238
239#![warn(missing_docs)]
240#![warn(clippy::exhaustive_enums)]
241#![cfg_attr(aws_lc_rs_docsrs, feature(doc_cfg))]
242
243extern crate alloc;
244#[cfg(feature = "fips")]
245extern crate aws_lc_fips_sys as aws_lc;
246#[cfg(not(feature = "fips"))]
247extern crate aws_lc_sys as aws_lc;
248
249pub mod aead;
250pub mod agreement;
251pub mod cmac;
252pub mod constant_time;
253pub mod digest;
254pub mod error;
255pub mod hkdf;
256pub mod hmac;
257#[cfg(feature = "ring-io")]
258pub mod io;
259pub mod key_wrap;
260pub mod pbkdf2;
261pub mod pkcs8;
262pub mod rand;
263pub mod signature;
264pub mod test;
265
266mod bn;
267mod buffer;
268mod cbb;
269mod cbs;
270pub mod cipher;
271mod debug;
272mod ec;
273mod ed25519;
274pub mod encoding;
275mod endian;
276mod evp_pkey;
277mod fips;
278mod hex;
279pub mod iv;
280pub mod kdf;
281#[allow(clippy::module_name_repetitions)]
282pub mod kem;
283#[cfg(all(feature = "unstable", not(feature = "fips")))]
284mod pqdsa;
285mod ptr;
286pub mod rsa;
287pub mod tls_prf;
288pub mod unstable;
289
290pub(crate) use debug::derive_debug_via_id;
291// TODO: Uncomment when MSRV >= 1.64
292// use core::ffi::CStr;
293use std::ffi::CStr;
294
295use crate::aws_lc::{
296    CRYPTO_library_init, ERR_error_string, ERR_get_error, FIPS_mode, OpenSSL_version, ERR_GET_FUNC,
297    ERR_GET_LIB, ERR_GET_REASON, OPENSSL_VERSION,
298};
299use std::sync::Once;
300
301static START: Once = Once::new();
302
303#[inline]
304/// Initialize the *AWS-LC* library. (This should generally not be needed.)
305pub fn init() {
306    START.call_once(|| unsafe {
307        CRYPTO_library_init();
308    });
309}
310
311#[cfg(feature = "fips")]
312/// Panics if the underlying implementation is not FIPS, otherwise it returns.
313///
314/// # Panics
315/// Panics if the underlying implementation is not FIPS.
316pub fn fips_mode() {
317    try_fips_mode().unwrap();
318}
319
320/// Indicates whether the underlying implementation is FIPS.
321///
322/// # Errors
323/// Return an error if the underlying implementation is not FIPS, otherwise Ok.
324pub fn try_fips_mode() -> Result<(), &'static str> {
325    init();
326    match unsafe { FIPS_mode() } {
327        1 => Ok(()),
328        _ => Err("FIPS mode not enabled!"),
329    }
330}
331
332/// The version number of the linked AWS-LC library (e.g. `"5.1.0"`).
333///
334/// This identifies the exact release you are running against. For FIPS builds it
335/// can disambiguate builds that share a FIPS module number, but it must not be
336/// used to infer FIPS certification status directly.
337///
338/// # Panics
339/// Panics if AWS-LC returns a version string that is not valid UTF-8.
340#[must_use]
341pub fn awslc_version() -> &'static str {
342    init();
343    let full = unsafe { CStr::from_ptr(OpenSSL_version(OPENSSL_VERSION)) }
344        .to_str()
345        .expect("AWS-LC version string is not valid UTF-8");
346    // "AWS-LC 5.1.0" / "AWS-LC FIPS 3.4.0" -> the trailing version token.
347    full.rsplit_once(' ').map_or(full, |(_, version)| version)
348}
349
350/// The FIPS module version this build corresponds to, or `None` if the build
351/// does not correspond to an AWS-LC FIPS release branch.
352///
353/// Unlike [`awslc_version()`], this is a build-time constant resolved from
354/// the AWS-LC headers; the library linked at runtime is not consulted. It is
355/// also not equivalent to [`try_fips_mode()`] and does not by itself imply
356/// FIPS certification status. Returns `None` when the system-library version
357/// check is skipped.
358// TODO: Resolve at runtime via the `FIPS_version()` C API once
359// aws-lc-fips-sys tracks a FIPS 5+ branch.
360#[must_use]
361pub fn fips_version() -> Option<u32> {
362    let version = aws_lc::fips_version();
363    (version != 0).then_some(version)
364}
365
366#[cfg(feature = "fips")]
367/// Panics if the underlying implementation is not using CPU jitter entropy, otherwise it returns.
368///
369/// # Panics
370/// Panics if the underlying implementation is not using CPU jitter entropy.
371pub fn fips_cpu_jitter_entropy() {
372    try_fips_cpu_jitter_entropy().unwrap();
373}
374
375/// Indicates whether the underlying implementation is FIPS.
376///
377/// # Errors
378/// Return an error if the underlying implementation is not using CPU jitter entropy, otherwise Ok.
379pub fn try_fips_cpu_jitter_entropy() -> Result<(), &'static str> {
380    init();
381    // TODO: Delete once FIPS_is_entropy_cpu_jitter() available on FIPS branch
382    // https://github.com/aws/aws-lc/pull/2088
383    #[cfg(feature = "fips")]
384    if aws_lc::CFG_CPU_JITTER_ENTROPY() {
385        Ok(())
386    } else {
387        Err("FIPS CPU Jitter Entropy not enabled!")
388    }
389    #[cfg(not(feature = "fips"))]
390    match unsafe { aws_lc::FIPS_is_entropy_cpu_jitter() } {
391        1 => Ok(()),
392        _ => Err("FIPS CPU Jitter Entropy not enabled!"),
393    }
394}
395
396#[allow(dead_code)]
397unsafe fn dump_error() {
398    let err = ERR_get_error();
399    let lib = ERR_GET_LIB(err);
400    let reason = ERR_GET_REASON(err);
401    let func = ERR_GET_FUNC(err);
402    let mut buffer = [0u8; 256];
403    ERR_error_string(err, buffer.as_mut_ptr().cast());
404    let error_msg = CStr::from_bytes_with_nul_unchecked(&buffer);
405    eprintln!("Raw Error -- {error_msg:?}\nErr: {err}, Lib: {lib}, Reason: {reason}, Func: {func}");
406}
407
408mod sealed {
409    /// Traits that are designed to only be implemented internally in *aws-lc-rs*.
410    //
411    // Usage:
412    // ```
413    // use crate::sealed;
414    //
415    // pub trait MyType: sealed::Sealed {
416    //     // [...]
417    // }
418    //
419    // impl sealed::Sealed for MyType {}
420    // ```
421    pub trait Sealed {}
422}
423
424#[cfg(test)]
425mod tests {
426    use crate::{dump_error, init};
427
428    #[test]
429    fn test_init() {
430        init();
431    }
432
433    #[test]
434    fn test_dump() {
435        unsafe {
436            dump_error();
437        }
438    }
439
440    #[cfg(not(feature = "fips"))]
441    #[test]
442    fn test_fips() {
443        assert!({ crate::try_fips_mode().is_err() });
444        // Re-enable with fixed test after upstream has merged RAGDOLL
445        //assert!({ crate::try_fips_cpu_jitter_entropy().is_ok() });
446    }
447
448    #[test]
449    // FIPS mode is disabled for an ASAN build
450    #[cfg(feature = "fips")]
451    fn test_fips() {
452        #[cfg(not(feature = "asan"))]
453        crate::fips_mode();
454        if aws_lc::CFG_CPU_JITTER_ENTROPY() {
455            crate::fips_cpu_jitter_entropy();
456        }
457    }
458
459    #[test]
460    fn test_awslc_version() {
461        let version = crate::awslc_version();
462        let major = version
463            .split('.')
464            .next()
465            .and_then(|major| major.parse::<u32>().ok())
466            .expect("AWS-LC version should start with a numeric major version");
467        assert!(major > 0);
468    }
469
470    #[cfg(not(feature = "fips"))]
471    #[test]
472    fn test_fips_version() {
473        assert_eq!(crate::fips_version(), None);
474    }
475
476    #[cfg(feature = "fips")]
477    #[test]
478    fn test_fips_version() {
479        // Module versions are monotonic across FIPS branches; the pinned branch is 3.
480        assert!(crate::fips_version().unwrap() >= 3);
481    }
482}