polyval/
lib.rs

1//! **POLYVAL** is a GHASH-like universal hash over GF(2^128) useful for
2//! implementing [AES-GCM-SIV] or [AES-GCM/GMAC].
3//!
4//! From [RFC 8452 Section 3] which defines POLYVAL for use in AES-GCM-SIV:
5//!
6//! > "POLYVAL, like GHASH (the authenticator in AES-GCM; ...), operates in a
7//! > binary field of size 2^128.  The field is defined by the irreducible
8//! > polynomial x^128 + x^127 + x^126 + x^121 + 1."
9//!
10//! By multiplying (in the finite field sense) a sequence of 128-bit blocks of
11//! input data data by a field element `H`, POLYVAL can be used to authenticate
12//! the message sequence as powers (in the finite field sense) of `H`.
13//!
14//! # Minimum Supported Rust Version
15//! Rust **1.56** or higher.
16//!
17//! In the future the minimum supported Rust version may be changed, but it
18//! be will be accompanied with a minor version bump.
19//!
20//! # Supported backends
21//! This crate provides multiple backends including a portable pure Rust
22//! backend as well as ones based on CPU intrinsics.
23//!
24//! ## "soft" portable backend
25//! As a baseline implementation, this crate provides a constant-time pure Rust
26//! implementation based on [BearSSL], which is a straightforward and
27//! compact implementation which uses a clever but simple technique to avoid
28//! carry-spilling.
29//!
30//! ## ARMv8 intrinsics (`PMULL`, nightly-only)
31//! On `aarch64` targets including `aarch64-apple-darwin` (Apple M1) and Linux
32//! targets such as `aarch64-unknown-linux-gnu` and `aarch64-unknown-linux-musl`,
33//! support for using the `PMULL` instructions in ARMv8's Cryptography Extensions
34//! is available when using the nightly compiler, and can be enabled using the
35//! `armv8` crate feature.
36//!
37//! On Linux and macOS, when the `armv8` feature is enabled support for AES
38//! intrinsics is autodetected at runtime. On other platforms the `crypto`
39//! target feature must be enabled via RUSTFLAGS.
40//!
41//! ## `x86`/`x86_64` intrinsics (`CMLMUL`)
42//! By default this crate uses runtime detection on `i686`/`x86_64` targets
43//! in order to determine if `CLMUL` is available, and if it is not, it will
44//! fallback to using a constant-time software implementation.
45//!
46//! For optimal performance, set `target-cpu` in `RUSTFLAGS` to `sandybridge`
47//! or newer:
48//!
49//! Example:
50//!
51//! ```text
52//! $ RUSTFLAGS="-Ctarget-cpu=sandybridge" cargo bench
53//! ```
54//!
55//! # Relationship to GHASH
56//! POLYVAL can be thought of as the little endian equivalent of GHASH, which
57//! affords it a small performance advantage over GHASH when used on little
58//! endian architectures.
59//!
60//! It has also been designed so it can also be used to compute GHASH and with
61//! it GMAC, the Message Authentication Code (MAC) used by AES-GCM.
62//!
63//! From [RFC 8452 Appendix A]:
64//!
65//! > "GHASH and POLYVAL both operate in GF(2^128), although with different
66//! > irreducible polynomials: POLYVAL works modulo x^128 + x^127 + x^126 +
67//! > x^121 + 1 and GHASH works modulo x^128 + x^7 + x^2 + x + 1.  Note
68//! > that these irreducible polynomials are the 'reverse' of each other."
69//!
70//! [AES-GCM-SIV]: https://en.wikipedia.org/wiki/AES-GCM-SIV
71//! [AES-GCM/GMAC]: https://en.wikipedia.org/wiki/Galois/Counter_Mode
72//! [BearSSL]: https://www.bearssl.org/constanttime.html#ghash-for-gcm
73//! [RFC 8452 Section 3]: https://tools.ietf.org/html/rfc8452#section-3
74//! [RFC 8452 Appendix A]: https://tools.ietf.org/html/rfc8452#appendix-A
75
76#![no_std]
77#![cfg_attr(docsrs, feature(doc_cfg))]
78#![cfg_attr(all(polyval_armv8, target_arch = "aarch64"), feature(stdsimd))]
79#![doc(
80    html_logo_url = "https://raw.githubusercontent.com/RustCrypto/media/8f1a9894/logo.svg",
81    html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/8f1a9894/logo.svg"
82)]
83#![warn(missing_docs, rust_2018_idioms)]
84
85mod backend;
86mod mulx;
87
88pub use crate::{backend::Polyval, mulx::mulx};
89pub use universal_hash;
90
91opaque_debug::implement!(Polyval);
92
93/// Size of a POLYVAL block in bytes
94pub const BLOCK_SIZE: usize = 16;
95
96/// Size of a POLYVAL key in bytes
97pub const KEY_SIZE: usize = 16;
98
99/// POLYVAL keys (16-bytes)
100pub type Key = universal_hash::Key<Polyval>;
101
102/// POLYVAL blocks (16-bytes)
103pub type Block = universal_hash::Block<Polyval>;
104
105/// POLYVAL tags (16-bytes)
106pub type Tag = universal_hash::Block<Polyval>;