| 1 |
# subtle [](https://crates.io/crates/subtle) [](https://doc.dalek.rs/subtle) [](https://travis-ci.org/dalek-cryptography/subtle) |
| 2 |
|
| 3 |
**Pure-Rust traits and utilities for constant-time cryptographic implementations.** |
| 4 |
|
| 5 |
It consists of a `Choice` type, and a collection of traits using `Choice` |
| 6 |
instead of `bool` which are intended to execute in constant-time. The `Choice` |
| 7 |
type is a wrapper around a `u8` that holds a `0` or `1`. |
| 8 |
|
| 9 |
```toml |
| 10 |
subtle = "2.5" |
| 11 |
``` |
| 12 |
|
| 13 |
This crate represents a “best-effort” attempt, since side-channels |
| 14 |
are ultimately a property of a deployed cryptographic system |
| 15 |
including the hardware it runs on, not just of software. |
| 16 |
|
| 17 |
The traits are implemented using bitwise operations, and should execute in |
| 18 |
constant time provided that a) the bitwise operations are constant-time and |
| 19 |
b) the bitwise operations are not recognized as a conditional assignment and |
| 20 |
optimized back into a branch. |
| 21 |
|
| 22 |
For a compiler to recognize that bitwise operations represent a conditional |
| 23 |
assignment, it needs to know that the value used to generate the bitmasks is |
| 24 |
really a boolean `i1` rather than an `i8` byte value. In an attempt to |
| 25 |
prevent this refinement, the crate tries to hide the value of a `Choice`'s |
| 26 |
inner `u8` by passing it through a volatile read. For more information, see |
| 27 |
the _About_ section below. |
| 28 |
|
| 29 |
Rust versions from 1.66 or higher support a new best-effort optimization |
| 30 |
barrier ([`core::hint::black_box`]). To use the new optimization barrier, |
| 31 |
enable the `core_hint_black_box` feature. |
| 32 |
|
| 33 |
Rust versions from 1.51 or higher have const generics support. You may enable |
| 34 |
`const-generics` feautre to have `subtle` traits implemented for arrays `[T; N]`. |
| 35 |
|
| 36 |
Versions prior to `2.2` recommended use of the `nightly` feature to enable an |
| 37 |
optimization barrier; this is not required in versions `2.2` and above. |
| 38 |
|
| 39 |
Note: the `subtle` crate contains `debug_assert`s to check invariants during |
| 40 |
debug builds. These invariant checks involve secret-dependent branches, and |
| 41 |
are not present when compiled in release mode. This crate is intended to be |
| 42 |
used in release mode. |
| 43 |
|
| 44 |
## Documentation |
| 45 |
|
| 46 |
Documentation is available [here][docs]. |
| 47 |
|
| 48 |
## Minimum Supported Rust Version |
| 49 |
|
| 50 |
Rust **1.41** or higher. |
| 51 |
|
| 52 |
Minimum supported Rust version can be changed in the future, but it will be done with a minor version bump. |
| 53 |
|
| 54 |
## About |
| 55 |
|
| 56 |
This library aims to be the Rust equivalent of Go’s `crypto/subtle` module. |
| 57 |
|
| 58 |
Old versions of the optimization barrier in `impl From<u8> for Choice` were |
| 59 |
based on Tim Maclean's [work on `rust-timing-shield`][rust-timing-shield], |
| 60 |
which attempts to provide a more comprehensive approach for preventing |
| 61 |
software side-channels in Rust code. |
| 62 |
|
| 63 |
From version `2.2`, it was based on Diane Hosfelt and Amber Sprenkels' work on |
| 64 |
"Secret Types in Rust". Version `2.5` adds the `core_hint_black_box` feature, |
| 65 |
which uses the original method through the [`core::hint::black_box`] function |
| 66 |
from the Rust standard library. |
| 67 |
|
| 68 |
`subtle` is authored by isis agora lovecruft and Henry de Valence. |
| 69 |
|
| 70 |
## Warning |
| 71 |
|
| 72 |
This code is a low-level library, intended for specific use-cases implementing |
| 73 |
cryptographic protocols. It represents a best-effort attempt to protect |
| 74 |
against some software side-channels. Because side-channel resistance is not a |
| 75 |
property of software alone, but of software together with hardware, any such |
| 76 |
effort is fundamentally limited. |
| 77 |
|
| 78 |
**USE AT YOUR OWN RISK** |
| 79 |
|
| 80 |
[docs]: https://docs.rs/subtle |
| 81 |
[`core::hint::black_box`]: https://doc.rust-lang.org/core/hint/fn.black_box.html |
| 82 |
[rust-timing-shield]: https://www.chosenplaintext.ca/open-source/rust-timing-shield/security |