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, ERR_GET_FUNC, ERR_GET_LIB,
297 ERR_GET_REASON,
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#[cfg(feature = "fips")]
333/// Panics if the underlying implementation is not using CPU jitter entropy, otherwise it returns.
334///
335/// # Panics
336/// Panics if the underlying implementation is not using CPU jitter entropy.
337pub fn fips_cpu_jitter_entropy() {
338 try_fips_cpu_jitter_entropy().unwrap();
339}
340
341/// Indicates whether the underlying implementation is FIPS.
342///
343/// # Errors
344/// Return an error if the underlying implementation is not using CPU jitter entropy, otherwise Ok.
345pub fn try_fips_cpu_jitter_entropy() -> Result<(), &'static str> {
346 init();
347 // TODO: Delete once FIPS_is_entropy_cpu_jitter() available on FIPS branch
348 // https://github.com/aws/aws-lc/pull/2088
349 #[cfg(feature = "fips")]
350 if aws_lc::CFG_CPU_JITTER_ENTROPY() {
351 Ok(())
352 } else {
353 Err("FIPS CPU Jitter Entropy not enabled!")
354 }
355 #[cfg(not(feature = "fips"))]
356 match unsafe { aws_lc::FIPS_is_entropy_cpu_jitter() } {
357 1 => Ok(()),
358 _ => Err("FIPS CPU Jitter Entropy not enabled!"),
359 }
360}
361
362#[allow(dead_code)]
363unsafe fn dump_error() {
364 let err = ERR_get_error();
365 let lib = ERR_GET_LIB(err);
366 let reason = ERR_GET_REASON(err);
367 let func = ERR_GET_FUNC(err);
368 let mut buffer = [0u8; 256];
369 ERR_error_string(err, buffer.as_mut_ptr().cast());
370 let error_msg = CStr::from_bytes_with_nul_unchecked(&buffer);
371 eprintln!("Raw Error -- {error_msg:?}\nErr: {err}, Lib: {lib}, Reason: {reason}, Func: {func}");
372}
373
374mod sealed {
375 /// Traits that are designed to only be implemented internally in *aws-lc-rs*.
376 //
377 // Usage:
378 // ```
379 // use crate::sealed;
380 //
381 // pub trait MyType: sealed::Sealed {
382 // // [...]
383 // }
384 //
385 // impl sealed::Sealed for MyType {}
386 // ```
387 pub trait Sealed {}
388}
389
390#[cfg(test)]
391mod tests {
392 use crate::{dump_error, init};
393
394 #[test]
395 fn test_init() {
396 init();
397 }
398
399 #[test]
400 fn test_dump() {
401 unsafe {
402 dump_error();
403 }
404 }
405
406 #[cfg(not(feature = "fips"))]
407 #[test]
408 fn test_fips() {
409 assert!({ crate::try_fips_mode().is_err() });
410 // Re-enable with fixed test after upstream has merged RAGDOLL
411 //assert!({ crate::try_fips_cpu_jitter_entropy().is_ok() });
412 }
413
414 #[test]
415 // FIPS mode is disabled for an ASAN build
416 #[cfg(feature = "fips")]
417 fn test_fips() {
418 #[cfg(not(feature = "asan"))]
419 crate::fips_mode();
420 if aws_lc::CFG_CPU_JITTER_ENTROPY() {
421 crate::fips_cpu_jitter_entropy();
422 }
423 }
424}