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`, MSRV 1.61+)
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//! with the following `RUSTFLAGS`:
35//!
36//! ```text
37//! --cfg polyval_armv8
38//! ```
39//!
40//! On Linux and macOS when the ARMv8 features are enabled, support for `PMULL`
41//! intrinsics is autodetected at runtime. On other platforms the `crypto`
42//! target feature must be enabled via RUSTFLAGS.
43//!
44//! ## `x86`/`x86_64` intrinsics (`CMLMUL`)
45//! By default this crate uses runtime detection on `i686`/`x86_64` targets
46//! in order to determine if `CLMUL` is available, and if it is not, it will
47//! fallback to using a constant-time software implementation.
48//!
49//! For optimal performance, set `target-cpu` in `RUSTFLAGS` to `sandybridge`
50//! or newer:
51//!
52//! Example:
53//!
54//! ```text
55//! $ RUSTFLAGS="-Ctarget-cpu=sandybridge" cargo bench
56//! ```
57//!
58//! # Relationship to GHASH
59//! POLYVAL can be thought of as the little endian equivalent of GHASH, which
60//! affords it a small performance advantage over GHASH when used on little
61//! endian architectures.
62//!
63//! It has also been designed so it can also be used to compute GHASH and with
64//! it GMAC, the Message Authentication Code (MAC) used by AES-GCM.
65//!
66//! From [RFC 8452 Appendix A]:
67//!
68//! > "GHASH and POLYVAL both operate in GF(2^128), although with different
69//! > irreducible polynomials: POLYVAL works modulo x^128 + x^127 + x^126 +
70//! > x^121 + 1 and GHASH works modulo x^128 + x^7 + x^2 + x + 1. Note
71//! > that these irreducible polynomials are the 'reverse' of each other."
72//!
73//! [AES-GCM-SIV]: https://en.wikipedia.org/wiki/AES-GCM-SIV
74//! [AES-GCM/GMAC]: https://en.wikipedia.org/wiki/Galois/Counter_Mode
75//! [BearSSL]: https://www.bearssl.org/constanttime.html#ghash-for-gcm
76//! [RFC 8452 Section 3]: https://tools.ietf.org/html/rfc8452#section-3
77//! [RFC 8452 Appendix A]: https://tools.ietf.org/html/rfc8452#appendix-A
78
79#![no_std]
80#![cfg_attr(docsrs, feature(doc_cfg))]
81#![doc(
82 html_logo_url = "https://raw.githubusercontent.com/RustCrypto/media/8f1a9894/logo.svg",
83 html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/8f1a9894/logo.svg"
84)]
85#![warn(missing_docs, rust_2018_idioms)]
86
87mod backend;
88mod mulx;
89
90pub use crate::{backend::Polyval, mulx::mulx};
91pub use universal_hash;
92
93opaque_debug::implement!(Polyval);
94
95/// Size of a POLYVAL block in bytes
96pub const BLOCK_SIZE: usize = 16;
97
98/// Size of a POLYVAL key in bytes
99pub const KEY_SIZE: usize = 16;
100
101/// POLYVAL keys (16-bytes)
102pub type Key = universal_hash::Key<Polyval>;
103
104/// POLYVAL blocks (16-bytes)
105pub type Block = universal_hash::Block<Polyval>;
106
107/// POLYVAL tags (16-bytes)
108pub type Tag = universal_hash::Block<Polyval>;