├── .github └── workflows │ └── ci.yml ├── .gitignore ├── CHANGES ├── Cargo.lock ├── Cargo.toml ├── LICENSE-APACHE ├── LICENSE-CC0 ├── LICENSE-MIT0 ├── README ├── benches ├── bench.rs ├── bench_classic.rs └── bench_generic.rs ├── src ├── classic.rs ├── dit.rs ├── generic.rs ├── lib.rs ├── neon.rs └── sse2.rs └── tests ├── count_instructions.rs ├── count_instructions_classic.rs ├── count_instructions_generic.rs └── exhaustive.rs /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: tests 2 | 3 | on: [push, pull_request] 4 | permissions: 5 | contents: read 6 | 7 | jobs: 8 | tests: 9 | name: Run tests 10 | strategy: 11 | matrix: 12 | toolchain: [1.85.0, stable, beta, nightly] 13 | os: [ubuntu-latest, macos-latest, windows-latest, ubuntu-24.04-arm] 14 | runs-on: ${{ matrix.os }} 15 | steps: 16 | - uses: actions/checkout@v4 17 | - uses: actions/cache@v4 18 | with: 19 | path: | 20 | ~/.cargo/registry/index/ 21 | ~/.cargo/registry/cache/ 22 | ~/.cargo/git/db/ 23 | target/ 24 | key: ${{ matrix.os }}-cargo-${{ matrix.toolchain }}-${{ hashFiles('**/Cargo.lock','**/Cargo.toml') }} 25 | - run: rustup toolchain install ${{ matrix.toolchain }} --profile=minimal --no-self-update 26 | - run: rustup default ${{ matrix.toolchain }} 27 | - run: rustup override set ${{ matrix.toolchain }} 28 | - run: rustc --verbose --version 29 | - run: cargo --verbose --version 30 | - run: cargo build --verbose 31 | - run: cargo build --verbose --no-default-features 32 | - run: cargo build --verbose --release 33 | - run: cargo build --verbose --release --no-default-features 34 | - run: cargo test --verbose 35 | - run: cargo test --verbose --no-default-features 36 | - run: cargo test --verbose --release 37 | - run: cargo test --verbose --release --no-default-features 38 | - name: Cross-compile x86_64-unknown-linux-gnu 39 | env: 40 | TARGET: x86_64-unknown-linux-gnu 41 | run: | 42 | rustup target add ${{ env.TARGET }} 43 | cargo build --verbose --target ${{ env.TARGET }} 44 | cargo build --verbose --target ${{ env.TARGET }} --no-default-features 45 | cargo build --verbose --release --target ${{ env.TARGET }} 46 | cargo build --verbose --release --target ${{ env.TARGET }} --no-default-features 47 | - name: Cross-compile i686-unknown-linux-gnu 48 | env: 49 | TARGET: i686-unknown-linux-gnu 50 | run: | 51 | rustup target add ${{ env.TARGET }} 52 | cargo build --verbose --target ${{ env.TARGET }} 53 | cargo build --verbose --target ${{ env.TARGET }} --no-default-features 54 | cargo build --verbose --release --target ${{ env.TARGET }} 55 | cargo build --verbose --release --target ${{ env.TARGET }} --no-default-features 56 | - name: Cross-compile aarch64-unknown-linux-gnu 57 | env: 58 | TARGET: aarch64-unknown-linux-gnu 59 | run: | 60 | rustup target add ${{ env.TARGET }} 61 | cargo build --verbose --target ${{ env.TARGET }} 62 | cargo build --verbose --target ${{ env.TARGET }} --no-default-features 63 | cargo build --verbose --release --target ${{ env.TARGET }} 64 | cargo build --verbose --release --target ${{ env.TARGET }} --no-default-features 65 | - name: Cross-compile armv7-unknown-linux-gnueabihf 66 | env: 67 | TARGET: armv7-unknown-linux-gnueabihf 68 | run: | 69 | rustup target add ${{ env.TARGET }} 70 | cargo build --verbose --target ${{ env.TARGET }} 71 | cargo build --verbose --target ${{ env.TARGET }} --no-default-features 72 | cargo build --verbose --release --target ${{ env.TARGET }} 73 | cargo build --verbose --release --target ${{ env.TARGET }} --no-default-features 74 | - name: Cross-compile aarch64-apple-darwin 75 | env: 76 | TARGET: aarch64-apple-darwin 77 | run: | 78 | rustup target add ${{ env.TARGET }} 79 | cargo build --verbose --target ${{ env.TARGET }} 80 | cargo build --verbose --target ${{ env.TARGET }} --no-default-features 81 | cargo build --verbose --release --target ${{ env.TARGET }} 82 | cargo build --verbose --release --target ${{ env.TARGET }} --no-default-features 83 | - name: Cross-compile riscv64gc-unknown-linux-gnu 84 | env: 85 | TARGET: riscv64gc-unknown-linux-gnu 86 | run: | 87 | rustup target add ${{ env.TARGET }} 88 | cargo build --verbose --target ${{ env.TARGET }} 89 | cargo build --verbose --target ${{ env.TARGET }} --no-default-features 90 | cargo build --verbose --release --target ${{ env.TARGET }} 91 | cargo build --verbose --release --target ${{ env.TARGET }} --no-default-features 92 | - name: Cross-compile riscv32imafc-unknown-none-elf 93 | env: 94 | TARGET: riscv32imafc-unknown-none-elf 95 | run: | 96 | rustup target add ${{ env.TARGET }} 97 | cargo build --verbose --target ${{ env.TARGET }} --no-default-features 98 | cargo build --verbose --release --target ${{ env.TARGET }} --no-default-features 99 | - name: Cross-compile loongarch64-unknown-linux-gnu 100 | env: 101 | TARGET: loongarch64-unknown-linux-gnu 102 | run: | 103 | rustup target add ${{ env.TARGET }} 104 | cargo build --verbose --target ${{ env.TARGET }} 105 | cargo build --verbose --target ${{ env.TARGET }} --no-default-features 106 | cargo build --verbose --release --target ${{ env.TARGET }} 107 | cargo build --verbose --release --target ${{ env.TARGET }} --no-default-features 108 | - name: Cross-compile s390x-unknown-linux-gnu 109 | env: 110 | TARGET: s390x-unknown-linux-gnu 111 | run: | 112 | rustup target add ${{ env.TARGET }} 113 | cargo build --verbose --target ${{ env.TARGET }} 114 | cargo build --verbose --target ${{ env.TARGET }} --no-default-features 115 | cargo build --verbose --release --target ${{ env.TARGET }} 116 | cargo build --verbose --release --target ${{ env.TARGET }} --no-default-features 117 | - name: Cross-compile wasm32-unknown-unknown 118 | env: 119 | TARGET: wasm32-unknown-unknown 120 | run: | 121 | rustup target add ${{ env.TARGET }} 122 | cargo build --verbose --target ${{ env.TARGET }} 123 | cargo build --verbose --target ${{ env.TARGET }} --no-default-features 124 | cargo build --verbose --release --target ${{ env.TARGET }} 125 | cargo build --verbose --release --target ${{ env.TARGET }} --no-default-features 126 | - name: Cross-compile wasm32-wasip1 127 | env: 128 | TARGET: wasm32-wasip1 129 | run: | 130 | rustup target add ${{ env.TARGET }} 131 | cargo build --verbose --target ${{ env.TARGET }} 132 | cargo build --verbose --target ${{ env.TARGET }} --no-default-features 133 | cargo build --verbose --release --target ${{ env.TARGET }} 134 | cargo build --verbose --release --target ${{ env.TARGET }} --no-default-features 135 | - name: Count instructions 136 | if: ${{ runner.os == 'Linux' }} 137 | run: | 138 | cargo test --verbose --release --features count_instructions_test 139 | cargo test --verbose --release --no-default-features --features count_instructions_test 140 | 141 | miri: 142 | name: Run tests under Miri 143 | runs-on: ubuntu-latest 144 | steps: 145 | - uses: actions/checkout@v4 146 | - uses: actions/cache@v4 147 | with: 148 | path: | 149 | ~/.cargo/registry/index/ 150 | ~/.cargo/registry/cache/ 151 | ~/.cargo/git/db/ 152 | target/ 153 | key: ubuntu-latest-cargo-nightly-${{ hashFiles('**/Cargo.lock','**/Cargo.toml') }}-miri 154 | - run: rustup toolchain install nightly --profile=minimal --no-self-update 155 | - run: rustup default nightly 156 | - run: rustup override set nightly 157 | - run: rustup component add miri 158 | - run: rustc --verbose --version 159 | - run: cargo --verbose --version 160 | - run: cargo miri test --verbose 161 | - run: cargo miri test --verbose --no-default-features 162 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | -------------------------------------------------------------------------------- /CHANGES: -------------------------------------------------------------------------------- 1 | # 0.4.2 2 | 3 | * Refactor detection of the DIT feature on ARM. For builds without the 4 | "std" feature, there's now an undocumented function to force the use 5 | of the DIT bit. 6 | 7 | # 0.4.1 8 | 9 | * Simplify code to make it easier to review. 10 | 11 | # 0.4.0 12 | 13 | BREAKING CHANGE: this crate now has a "std" feature which is enabled by 14 | default. Disable it if you need to use this crate as a no_std crate. 15 | 16 | Previous versions of this crate protected against the optimizer doing an 17 | early exit when the accumulator becomes non-zero (found a difference), 18 | but not against a sufficiently smart optimizer doing an early exit when 19 | the accumulator has all bits set (the accumulator never clears a bit, so 20 | having all bits set means it will no longer change). 21 | 22 | Protecting against that also prevents autovectorization, so this release 23 | does manual vectorization to recover most of the speed lost. Where there 24 | is enough compiler support (stable vector intrinsics), it uses a mix of 25 | vector intrinsics and inline assembly for inputs which are a multiple of 26 | the vector size, while for other architectures and for the remainder of 27 | an input which is not a multiple of the vector size, it uses a generic 28 | word-at-a-time implementation with the native word size. 29 | 30 | Some newer implementations of the ARM architecture do not guarantee the 31 | timing of instructions unless the DIT bit is set. Fortunately, that bit 32 | can be set on all privilege levels; unfortunately, that bit only exists 33 | on these newer implementations of the ARM architecture, and the flag to 34 | detect whether it exists is not accessible on all privilege levels. How 35 | to obtain that flag varies depending on the operating system, but Rust 36 | has a good implementation of that on its standard library. This means 37 | that runtime detection introduces a dependency on std (enabled by the 38 | "std" feature, which is enabled by default); compile-time detection is 39 | always available. 40 | 41 | This release is a candidate for becoming the 1.0 release. In preparation 42 | for that, it uses the 2024 edition, which enables the new resolver which 43 | will allow future updates to the set of architectures which can use the 44 | inline assembly implementation of optimizer_hide(), without breaking 45 | downstream crates (for instance, s390x and arm64ec were stabilized in 46 | Rust 1.84.0). 47 | 48 | * Rewrite the generic implementation to process one word at a time, 49 | instead of byte by byte. Depending on the architecture, this means 50 | 8 bytes or 4 bytes processed on each loop iteration. 51 | * Use optimizer_hide() after each step, instead of just at the end. 52 | * Since optimizer_hide() now works on words, it no longer neeeds the 53 | special case for byte sub-registers on x86 and x86_64. 54 | * Manual implementation for SSE2/AVX (x86 and x86_64), using 128-bit 55 | vectors, processing up to 32 bytes on each loop iteration. 56 | * Manual implementation for NEON (aarch64 only for now), also using 57 | 128-bit vectors and processing up to 32 bytes on each loop iteration. 58 | * On AArch64 with FEAT_DIT (like modern Apple devices), try to set the 59 | DIT flag to ensure data independent timing. 60 | 61 | # 0.3.1 62 | 63 | * Use the portable optimizer_hide() when running under Miri. 64 | 65 | # 0.3.0 66 | 67 | * Use black_box instead of volatile read when inline assembly is not 68 | available. 69 | * Increase minimum Rust version to 1.66, which is when black_box was 70 | stabilized. 71 | 72 | # 0.2.6 73 | 74 | * New tests using the count_instructions crate; no functional changes. 75 | 76 | # 0.2.5 77 | 78 | * Add #[must_use] to all functions. 79 | 80 | # 0.2.4 81 | 82 | * Since CC0 is no longer accepted as a license for code by Fedora, also 83 | allow MIT-0 or Apache-2.0 as options. No code changes. 84 | 85 | # 0.2.3 86 | 87 | * Add fixed-size variant for arrays of any size (using const generics). 88 | 89 | # 0.2.2 90 | 91 | * Set rust-version in Cargo.toml to 1.59. 92 | 93 | # 0.2.1 94 | 95 | * Reduce inlining of variable-size variant. In 0.1.5, the loop was not 96 | inlined, and it can be a bit large due to the auto-vectorization. Go 97 | back to how it was in 0.1.5, but allowing the compiler to inline if 98 | it believes it would be a speed gain. 99 | 100 | # 0.2.0 101 | 102 | * Use inline assembly when available to hide from the optimizer. 103 | * When inline assembly is not available, use both a volatile read and 104 | disabled inlining. 105 | * Increase minimum Rust version to 1.59, which is the first with inline 106 | assembly. 107 | 108 | # 0.1.5 109 | 110 | * Add fixed-size variant for arrays with sizes 16 bytes, 32 bytes, and 111 | 64 bytes. 112 | -------------------------------------------------------------------------------- /Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 4 4 | 5 | [[package]] 6 | name = "aho-corasick" 7 | version = "1.1.3" 8 | source = "registry+https://github.com/rust-lang/crates.io-index" 9 | checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" 10 | dependencies = [ 11 | "memchr", 12 | ] 13 | 14 | [[package]] 15 | name = "anes" 16 | version = "0.1.6" 17 | source = "registry+https://github.com/rust-lang/crates.io-index" 18 | checksum = "4b46cbb362ab8752921c97e041f5e366ee6297bd428a31275b9fcf1e380f7299" 19 | 20 | [[package]] 21 | name = "anstyle" 22 | version = "1.0.10" 23 | source = "registry+https://github.com/rust-lang/crates.io-index" 24 | checksum = "55cc3b69f167a1ef2e161439aa98aed94e6028e5f9a59be9a6ffb47aef1651f9" 25 | 26 | [[package]] 27 | name = "autocfg" 28 | version = "1.4.0" 29 | source = "registry+https://github.com/rust-lang/crates.io-index" 30 | checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" 31 | 32 | [[package]] 33 | name = "bitflags" 34 | version = "2.9.0" 35 | source = "registry+https://github.com/rust-lang/crates.io-index" 36 | checksum = "5c8214115b7bf84099f1309324e63141d4c5d7cc26862f97a0a857dbefe165bd" 37 | 38 | [[package]] 39 | name = "bumpalo" 40 | version = "3.17.0" 41 | source = "registry+https://github.com/rust-lang/crates.io-index" 42 | checksum = "1628fb46dfa0b37568d12e5edd512553eccf6a22a78e8bde00bb4aed84d5bdbf" 43 | 44 | [[package]] 45 | name = "cast" 46 | version = "0.3.0" 47 | source = "registry+https://github.com/rust-lang/crates.io-index" 48 | checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5" 49 | 50 | [[package]] 51 | name = "cfg-if" 52 | version = "1.0.0" 53 | source = "registry+https://github.com/rust-lang/crates.io-index" 54 | checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" 55 | 56 | [[package]] 57 | name = "ciborium" 58 | version = "0.2.2" 59 | source = "registry+https://github.com/rust-lang/crates.io-index" 60 | checksum = "42e69ffd6f0917f5c029256a24d0161db17cea3997d185db0d35926308770f0e" 61 | dependencies = [ 62 | "ciborium-io", 63 | "ciborium-ll", 64 | "serde", 65 | ] 66 | 67 | [[package]] 68 | name = "ciborium-io" 69 | version = "0.2.2" 70 | source = "registry+https://github.com/rust-lang/crates.io-index" 71 | checksum = "05afea1e0a06c9be33d539b876f1ce3692f4afea2cb41f740e7743225ed1c757" 72 | 73 | [[package]] 74 | name = "ciborium-ll" 75 | version = "0.2.2" 76 | source = "registry+https://github.com/rust-lang/crates.io-index" 77 | checksum = "57663b653d948a338bfb3eeba9bb2fd5fcfaecb9e199e87e1eda4d9e8b240fd9" 78 | dependencies = [ 79 | "ciborium-io", 80 | "half", 81 | ] 82 | 83 | [[package]] 84 | name = "clap" 85 | version = "4.5.32" 86 | source = "registry+https://github.com/rust-lang/crates.io-index" 87 | checksum = "6088f3ae8c3608d19260cd7445411865a485688711b78b5be70d78cd96136f83" 88 | dependencies = [ 89 | "clap_builder", 90 | ] 91 | 92 | [[package]] 93 | name = "clap_builder" 94 | version = "4.5.32" 95 | source = "registry+https://github.com/rust-lang/crates.io-index" 96 | checksum = "22a7ef7f676155edfb82daa97f99441f3ebf4a58d5e32f295a56259f1b6facc8" 97 | dependencies = [ 98 | "anstyle", 99 | "clap_lex", 100 | ] 101 | 102 | [[package]] 103 | name = "clap_lex" 104 | version = "0.7.4" 105 | source = "registry+https://github.com/rust-lang/crates.io-index" 106 | checksum = "f46ad14479a25103f283c0f10005961cf086d8dc42205bb44c46ac563475dca6" 107 | 108 | [[package]] 109 | name = "constant_time_eq" 110 | version = "0.4.2" 111 | dependencies = [ 112 | "count_instructions", 113 | "criterion", 114 | ] 115 | 116 | [[package]] 117 | name = "count_instructions" 118 | version = "0.2.0" 119 | source = "registry+https://github.com/rust-lang/crates.io-index" 120 | checksum = "d27c934c1e8025234223056530ea5087cb01fc7e4cfcda01978e83e89f69b3b0" 121 | dependencies = [ 122 | "libc", 123 | "rustix", 124 | ] 125 | 126 | [[package]] 127 | name = "criterion" 128 | version = "0.5.1" 129 | source = "registry+https://github.com/rust-lang/crates.io-index" 130 | checksum = "f2b12d017a929603d80db1831cd3a24082f8137ce19c69e6447f54f5fc8d692f" 131 | dependencies = [ 132 | "anes", 133 | "cast", 134 | "ciborium", 135 | "clap", 136 | "criterion-plot", 137 | "is-terminal", 138 | "itertools", 139 | "num-traits", 140 | "once_cell", 141 | "oorandom", 142 | "plotters", 143 | "rayon", 144 | "regex", 145 | "serde", 146 | "serde_derive", 147 | "serde_json", 148 | "tinytemplate", 149 | "walkdir", 150 | ] 151 | 152 | [[package]] 153 | name = "criterion-plot" 154 | version = "0.5.0" 155 | source = "registry+https://github.com/rust-lang/crates.io-index" 156 | checksum = "6b50826342786a51a89e2da3a28f1c32b06e387201bc2d19791f622c673706b1" 157 | dependencies = [ 158 | "cast", 159 | "itertools", 160 | ] 161 | 162 | [[package]] 163 | name = "crossbeam-deque" 164 | version = "0.8.6" 165 | source = "registry+https://github.com/rust-lang/crates.io-index" 166 | checksum = "9dd111b7b7f7d55b72c0a6ae361660ee5853c9af73f70c3c2ef6858b950e2e51" 167 | dependencies = [ 168 | "crossbeam-epoch", 169 | "crossbeam-utils", 170 | ] 171 | 172 | [[package]] 173 | name = "crossbeam-epoch" 174 | version = "0.9.18" 175 | source = "registry+https://github.com/rust-lang/crates.io-index" 176 | checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e" 177 | dependencies = [ 178 | "crossbeam-utils", 179 | ] 180 | 181 | [[package]] 182 | name = "crossbeam-utils" 183 | version = "0.8.21" 184 | source = "registry+https://github.com/rust-lang/crates.io-index" 185 | checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28" 186 | 187 | [[package]] 188 | name = "crunchy" 189 | version = "0.2.3" 190 | source = "registry+https://github.com/rust-lang/crates.io-index" 191 | checksum = "43da5946c66ffcc7745f48db692ffbb10a83bfe0afd96235c5c2a4fb23994929" 192 | 193 | [[package]] 194 | name = "either" 195 | version = "1.15.0" 196 | source = "registry+https://github.com/rust-lang/crates.io-index" 197 | checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719" 198 | 199 | [[package]] 200 | name = "errno" 201 | version = "0.3.10" 202 | source = "registry+https://github.com/rust-lang/crates.io-index" 203 | checksum = "33d852cb9b869c2a9b3df2f71a3074817f01e1844f839a144f5fcef059a4eb5d" 204 | dependencies = [ 205 | "libc", 206 | "windows-sys", 207 | ] 208 | 209 | [[package]] 210 | name = "half" 211 | version = "2.5.0" 212 | source = "registry+https://github.com/rust-lang/crates.io-index" 213 | checksum = "7db2ff139bba50379da6aa0766b52fdcb62cb5b263009b09ed58ba604e14bbd1" 214 | dependencies = [ 215 | "cfg-if", 216 | "crunchy", 217 | ] 218 | 219 | [[package]] 220 | name = "hermit-abi" 221 | version = "0.5.0" 222 | source = "registry+https://github.com/rust-lang/crates.io-index" 223 | checksum = "fbd780fe5cc30f81464441920d82ac8740e2e46b29a6fad543ddd075229ce37e" 224 | 225 | [[package]] 226 | name = "is-terminal" 227 | version = "0.4.16" 228 | source = "registry+https://github.com/rust-lang/crates.io-index" 229 | checksum = "e04d7f318608d35d4b61ddd75cbdaee86b023ebe2bd5a66ee0915f0bf93095a9" 230 | dependencies = [ 231 | "hermit-abi", 232 | "libc", 233 | "windows-sys", 234 | ] 235 | 236 | [[package]] 237 | name = "itertools" 238 | version = "0.10.5" 239 | source = "registry+https://github.com/rust-lang/crates.io-index" 240 | checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473" 241 | dependencies = [ 242 | "either", 243 | ] 244 | 245 | [[package]] 246 | name = "itoa" 247 | version = "1.0.15" 248 | source = "registry+https://github.com/rust-lang/crates.io-index" 249 | checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c" 250 | 251 | [[package]] 252 | name = "js-sys" 253 | version = "0.3.77" 254 | source = "registry+https://github.com/rust-lang/crates.io-index" 255 | checksum = "1cfaf33c695fc6e08064efbc1f72ec937429614f25eef83af942d0e227c3a28f" 256 | dependencies = [ 257 | "once_cell", 258 | "wasm-bindgen", 259 | ] 260 | 261 | [[package]] 262 | name = "libc" 263 | version = "0.2.171" 264 | source = "registry+https://github.com/rust-lang/crates.io-index" 265 | checksum = "c19937216e9d3aa9956d9bb8dfc0b0c8beb6058fc4f7a4dc4d850edf86a237d6" 266 | 267 | [[package]] 268 | name = "linux-raw-sys" 269 | version = "0.9.3" 270 | source = "registry+https://github.com/rust-lang/crates.io-index" 271 | checksum = "fe7db12097d22ec582439daf8618b8fdd1a7bef6270e9af3b1ebcd30893cf413" 272 | 273 | [[package]] 274 | name = "log" 275 | version = "0.4.26" 276 | source = "registry+https://github.com/rust-lang/crates.io-index" 277 | checksum = "30bde2b3dc3671ae49d8e2e9f044c7c005836e7a023ee57cffa25ab82764bb9e" 278 | 279 | [[package]] 280 | name = "memchr" 281 | version = "2.7.4" 282 | source = "registry+https://github.com/rust-lang/crates.io-index" 283 | checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" 284 | 285 | [[package]] 286 | name = "num-traits" 287 | version = "0.2.19" 288 | source = "registry+https://github.com/rust-lang/crates.io-index" 289 | checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" 290 | dependencies = [ 291 | "autocfg", 292 | ] 293 | 294 | [[package]] 295 | name = "once_cell" 296 | version = "1.21.1" 297 | source = "registry+https://github.com/rust-lang/crates.io-index" 298 | checksum = "d75b0bedcc4fe52caa0e03d9f1151a323e4aa5e2d78ba3580400cd3c9e2bc4bc" 299 | 300 | [[package]] 301 | name = "oorandom" 302 | version = "11.1.5" 303 | source = "registry+https://github.com/rust-lang/crates.io-index" 304 | checksum = "d6790f58c7ff633d8771f42965289203411a5e5c68388703c06e14f24770b41e" 305 | 306 | [[package]] 307 | name = "plotters" 308 | version = "0.3.7" 309 | source = "registry+https://github.com/rust-lang/crates.io-index" 310 | checksum = "5aeb6f403d7a4911efb1e33402027fc44f29b5bf6def3effcc22d7bb75f2b747" 311 | dependencies = [ 312 | "num-traits", 313 | "plotters-backend", 314 | "plotters-svg", 315 | "wasm-bindgen", 316 | "web-sys", 317 | ] 318 | 319 | [[package]] 320 | name = "plotters-backend" 321 | version = "0.3.7" 322 | source = "registry+https://github.com/rust-lang/crates.io-index" 323 | checksum = "df42e13c12958a16b3f7f4386b9ab1f3e7933914ecea48da7139435263a4172a" 324 | 325 | [[package]] 326 | name = "plotters-svg" 327 | version = "0.3.7" 328 | source = "registry+https://github.com/rust-lang/crates.io-index" 329 | checksum = "51bae2ac328883f7acdfea3d66a7c35751187f870bc81f94563733a154d7a670" 330 | dependencies = [ 331 | "plotters-backend", 332 | ] 333 | 334 | [[package]] 335 | name = "proc-macro2" 336 | version = "1.0.94" 337 | source = "registry+https://github.com/rust-lang/crates.io-index" 338 | checksum = "a31971752e70b8b2686d7e46ec17fb38dad4051d94024c88df49b667caea9c84" 339 | dependencies = [ 340 | "unicode-ident", 341 | ] 342 | 343 | [[package]] 344 | name = "quote" 345 | version = "1.0.40" 346 | source = "registry+https://github.com/rust-lang/crates.io-index" 347 | checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d" 348 | dependencies = [ 349 | "proc-macro2", 350 | ] 351 | 352 | [[package]] 353 | name = "rayon" 354 | version = "1.10.0" 355 | source = "registry+https://github.com/rust-lang/crates.io-index" 356 | checksum = "b418a60154510ca1a002a752ca9714984e21e4241e804d32555251faf8b78ffa" 357 | dependencies = [ 358 | "either", 359 | "rayon-core", 360 | ] 361 | 362 | [[package]] 363 | name = "rayon-core" 364 | version = "1.12.1" 365 | source = "registry+https://github.com/rust-lang/crates.io-index" 366 | checksum = "1465873a3dfdaa8ae7cb14b4383657caab0b3e8a0aa9ae8e04b044854c8dfce2" 367 | dependencies = [ 368 | "crossbeam-deque", 369 | "crossbeam-utils", 370 | ] 371 | 372 | [[package]] 373 | name = "regex" 374 | version = "1.11.1" 375 | source = "registry+https://github.com/rust-lang/crates.io-index" 376 | checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191" 377 | dependencies = [ 378 | "aho-corasick", 379 | "memchr", 380 | "regex-automata", 381 | "regex-syntax", 382 | ] 383 | 384 | [[package]] 385 | name = "regex-automata" 386 | version = "0.4.9" 387 | source = "registry+https://github.com/rust-lang/crates.io-index" 388 | checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908" 389 | dependencies = [ 390 | "aho-corasick", 391 | "memchr", 392 | "regex-syntax", 393 | ] 394 | 395 | [[package]] 396 | name = "regex-syntax" 397 | version = "0.8.5" 398 | source = "registry+https://github.com/rust-lang/crates.io-index" 399 | checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" 400 | 401 | [[package]] 402 | name = "rustix" 403 | version = "1.0.2" 404 | source = "registry+https://github.com/rust-lang/crates.io-index" 405 | checksum = "f7178faa4b75a30e269c71e61c353ce2748cf3d76f0c44c393f4e60abf49b825" 406 | dependencies = [ 407 | "bitflags", 408 | "errno", 409 | "libc", 410 | "linux-raw-sys", 411 | "windows-sys", 412 | ] 413 | 414 | [[package]] 415 | name = "rustversion" 416 | version = "1.0.20" 417 | source = "registry+https://github.com/rust-lang/crates.io-index" 418 | checksum = "eded382c5f5f786b989652c49544c4877d9f015cc22e145a5ea8ea66c2921cd2" 419 | 420 | [[package]] 421 | name = "ryu" 422 | version = "1.0.20" 423 | source = "registry+https://github.com/rust-lang/crates.io-index" 424 | checksum = "28d3b2b1366ec20994f1fd18c3c594f05c5dd4bc44d8bb0c1c632c8d6829481f" 425 | 426 | [[package]] 427 | name = "same-file" 428 | version = "1.0.6" 429 | source = "registry+https://github.com/rust-lang/crates.io-index" 430 | checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" 431 | dependencies = [ 432 | "winapi-util", 433 | ] 434 | 435 | [[package]] 436 | name = "serde" 437 | version = "1.0.219" 438 | source = "registry+https://github.com/rust-lang/crates.io-index" 439 | checksum = "5f0e2c6ed6606019b4e29e69dbaba95b11854410e5347d525002456dbbb786b6" 440 | dependencies = [ 441 | "serde_derive", 442 | ] 443 | 444 | [[package]] 445 | name = "serde_derive" 446 | version = "1.0.219" 447 | source = "registry+https://github.com/rust-lang/crates.io-index" 448 | checksum = "5b0276cf7f2c73365f7157c8123c21cd9a50fbbd844757af28ca1f5925fc2a00" 449 | dependencies = [ 450 | "proc-macro2", 451 | "quote", 452 | "syn", 453 | ] 454 | 455 | [[package]] 456 | name = "serde_json" 457 | version = "1.0.140" 458 | source = "registry+https://github.com/rust-lang/crates.io-index" 459 | checksum = "20068b6e96dc6c9bd23e01df8827e6c7e1f2fddd43c21810382803c136b99373" 460 | dependencies = [ 461 | "itoa", 462 | "memchr", 463 | "ryu", 464 | "serde", 465 | ] 466 | 467 | [[package]] 468 | name = "syn" 469 | version = "2.0.100" 470 | source = "registry+https://github.com/rust-lang/crates.io-index" 471 | checksum = "b09a44accad81e1ba1cd74a32461ba89dee89095ba17b32f5d03683b1b1fc2a0" 472 | dependencies = [ 473 | "proc-macro2", 474 | "quote", 475 | "unicode-ident", 476 | ] 477 | 478 | [[package]] 479 | name = "tinytemplate" 480 | version = "1.2.1" 481 | source = "registry+https://github.com/rust-lang/crates.io-index" 482 | checksum = "be4d6b5f19ff7664e8c98d03e2139cb510db9b0a60b55f8e8709b689d939b6bc" 483 | dependencies = [ 484 | "serde", 485 | "serde_json", 486 | ] 487 | 488 | [[package]] 489 | name = "unicode-ident" 490 | version = "1.0.18" 491 | source = "registry+https://github.com/rust-lang/crates.io-index" 492 | checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512" 493 | 494 | [[package]] 495 | name = "walkdir" 496 | version = "2.5.0" 497 | source = "registry+https://github.com/rust-lang/crates.io-index" 498 | checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b" 499 | dependencies = [ 500 | "same-file", 501 | "winapi-util", 502 | ] 503 | 504 | [[package]] 505 | name = "wasm-bindgen" 506 | version = "0.2.100" 507 | source = "registry+https://github.com/rust-lang/crates.io-index" 508 | checksum = "1edc8929d7499fc4e8f0be2262a241556cfc54a0bea223790e71446f2aab1ef5" 509 | dependencies = [ 510 | "cfg-if", 511 | "once_cell", 512 | "rustversion", 513 | "wasm-bindgen-macro", 514 | ] 515 | 516 | [[package]] 517 | name = "wasm-bindgen-backend" 518 | version = "0.2.100" 519 | source = "registry+https://github.com/rust-lang/crates.io-index" 520 | checksum = "2f0a0651a5c2bc21487bde11ee802ccaf4c51935d0d3d42a6101f98161700bc6" 521 | dependencies = [ 522 | "bumpalo", 523 | "log", 524 | "proc-macro2", 525 | "quote", 526 | "syn", 527 | "wasm-bindgen-shared", 528 | ] 529 | 530 | [[package]] 531 | name = "wasm-bindgen-macro" 532 | version = "0.2.100" 533 | source = "registry+https://github.com/rust-lang/crates.io-index" 534 | checksum = "7fe63fc6d09ed3792bd0897b314f53de8e16568c2b3f7982f468c0bf9bd0b407" 535 | dependencies = [ 536 | "quote", 537 | "wasm-bindgen-macro-support", 538 | ] 539 | 540 | [[package]] 541 | name = "wasm-bindgen-macro-support" 542 | version = "0.2.100" 543 | source = "registry+https://github.com/rust-lang/crates.io-index" 544 | checksum = "8ae87ea40c9f689fc23f209965b6fb8a99ad69aeeb0231408be24920604395de" 545 | dependencies = [ 546 | "proc-macro2", 547 | "quote", 548 | "syn", 549 | "wasm-bindgen-backend", 550 | "wasm-bindgen-shared", 551 | ] 552 | 553 | [[package]] 554 | name = "wasm-bindgen-shared" 555 | version = "0.2.100" 556 | source = "registry+https://github.com/rust-lang/crates.io-index" 557 | checksum = "1a05d73b933a847d6cccdda8f838a22ff101ad9bf93e33684f39c1f5f0eece3d" 558 | dependencies = [ 559 | "unicode-ident", 560 | ] 561 | 562 | [[package]] 563 | name = "web-sys" 564 | version = "0.3.77" 565 | source = "registry+https://github.com/rust-lang/crates.io-index" 566 | checksum = "33b6dd2ef9186f1f2072e409e99cd22a975331a6b3591b12c764e0e55c60d5d2" 567 | dependencies = [ 568 | "js-sys", 569 | "wasm-bindgen", 570 | ] 571 | 572 | [[package]] 573 | name = "winapi-util" 574 | version = "0.1.9" 575 | source = "registry+https://github.com/rust-lang/crates.io-index" 576 | checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb" 577 | dependencies = [ 578 | "windows-sys", 579 | ] 580 | 581 | [[package]] 582 | name = "windows-sys" 583 | version = "0.59.0" 584 | source = "registry+https://github.com/rust-lang/crates.io-index" 585 | checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" 586 | dependencies = [ 587 | "windows-targets", 588 | ] 589 | 590 | [[package]] 591 | name = "windows-targets" 592 | version = "0.52.6" 593 | source = "registry+https://github.com/rust-lang/crates.io-index" 594 | checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" 595 | dependencies = [ 596 | "windows_aarch64_gnullvm", 597 | "windows_aarch64_msvc", 598 | "windows_i686_gnu", 599 | "windows_i686_gnullvm", 600 | "windows_i686_msvc", 601 | "windows_x86_64_gnu", 602 | "windows_x86_64_gnullvm", 603 | "windows_x86_64_msvc", 604 | ] 605 | 606 | [[package]] 607 | name = "windows_aarch64_gnullvm" 608 | version = "0.52.6" 609 | source = "registry+https://github.com/rust-lang/crates.io-index" 610 | checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" 611 | 612 | [[package]] 613 | name = "windows_aarch64_msvc" 614 | version = "0.52.6" 615 | source = "registry+https://github.com/rust-lang/crates.io-index" 616 | checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" 617 | 618 | [[package]] 619 | name = "windows_i686_gnu" 620 | version = "0.52.6" 621 | source = "registry+https://github.com/rust-lang/crates.io-index" 622 | checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" 623 | 624 | [[package]] 625 | name = "windows_i686_gnullvm" 626 | version = "0.52.6" 627 | source = "registry+https://github.com/rust-lang/crates.io-index" 628 | checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" 629 | 630 | [[package]] 631 | name = "windows_i686_msvc" 632 | version = "0.52.6" 633 | source = "registry+https://github.com/rust-lang/crates.io-index" 634 | checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" 635 | 636 | [[package]] 637 | name = "windows_x86_64_gnu" 638 | version = "0.52.6" 639 | source = "registry+https://github.com/rust-lang/crates.io-index" 640 | checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" 641 | 642 | [[package]] 643 | name = "windows_x86_64_gnullvm" 644 | version = "0.52.6" 645 | source = "registry+https://github.com/rust-lang/crates.io-index" 646 | checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" 647 | 648 | [[package]] 649 | name = "windows_x86_64_msvc" 650 | version = "0.52.6" 651 | source = "registry+https://github.com/rust-lang/crates.io-index" 652 | checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" 653 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "constant_time_eq" 3 | version = "0.4.2" 4 | edition = "2024" 5 | authors = ["Cesar Eduardo Barros "] 6 | description = "Compares two equal-sized byte strings in constant time." 7 | documentation = "https://docs.rs/constant_time_eq" 8 | repository = "https://github.com/cesarb/constant_time_eq" 9 | readme = "README" 10 | keywords = ["constant_time"] 11 | categories = ["cryptography", "no-std"] 12 | license = "CC0-1.0 OR MIT-0 OR Apache-2.0" 13 | rust-version = "1.85.0" 14 | 15 | [dev-dependencies] 16 | criterion = { version = "0.5.1", features = ["cargo_bench_support", "html_reports"] } 17 | count_instructions = "0.2.0" 18 | 19 | [features] 20 | default = ["std"] 21 | 22 | # Necessary to detect at runtime whether DIT is available on aarch64. 23 | std = [] 24 | 25 | # Enables tests which depend on the count_instructions crate. 26 | count_instructions_test = [] 27 | 28 | [[bench]] 29 | name = "bench" 30 | harness = false 31 | 32 | [[bench]] 33 | name = "bench_generic" 34 | harness = false 35 | 36 | [[bench]] 37 | name = "bench_classic" 38 | harness = false 39 | -------------------------------------------------------------------------------- /LICENSE-APACHE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | -------------------------------------------------------------------------------- /LICENSE-CC0: -------------------------------------------------------------------------------- 1 | Creative Commons Legal Code 2 | 3 | CC0 1.0 Universal 4 | 5 | CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE 6 | LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN 7 | ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS 8 | INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES 9 | REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS 10 | PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM 11 | THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED 12 | HEREUNDER. 13 | 14 | Statement of Purpose 15 | 16 | The laws of most jurisdictions throughout the world automatically confer 17 | exclusive Copyright and Related Rights (defined below) upon the creator 18 | and subsequent owner(s) (each and all, an "owner") of an original work of 19 | authorship and/or a database (each, a "Work"). 20 | 21 | Certain owners wish to permanently relinquish those rights to a Work for 22 | the purpose of contributing to a commons of creative, cultural and 23 | scientific works ("Commons") that the public can reliably and without fear 24 | of later claims of infringement build upon, modify, incorporate in other 25 | works, reuse and redistribute as freely as possible in any form whatsoever 26 | and for any purposes, including without limitation commercial purposes. 27 | These owners may contribute to the Commons to promote the ideal of a free 28 | culture and the further production of creative, cultural and scientific 29 | works, or to gain reputation or greater distribution for their Work in 30 | part through the use and efforts of others. 31 | 32 | For these and/or other purposes and motivations, and without any 33 | expectation of additional consideration or compensation, the person 34 | associating CC0 with a Work (the "Affirmer"), to the extent that he or she 35 | is an owner of Copyright and Related Rights in the Work, voluntarily 36 | elects to apply CC0 to the Work and publicly distribute the Work under its 37 | terms, with knowledge of his or her Copyright and Related Rights in the 38 | Work and the meaning and intended legal effect of CC0 on those rights. 39 | 40 | 1. Copyright and Related Rights. A Work made available under CC0 may be 41 | protected by copyright and related or neighboring rights ("Copyright and 42 | Related Rights"). Copyright and Related Rights include, but are not 43 | limited to, the following: 44 | 45 | i. the right to reproduce, adapt, distribute, perform, display, 46 | communicate, and translate a Work; 47 | ii. moral rights retained by the original author(s) and/or performer(s); 48 | iii. publicity and privacy rights pertaining to a person's image or 49 | likeness depicted in a Work; 50 | iv. rights protecting against unfair competition in regards to a Work, 51 | subject to the limitations in paragraph 4(a), below; 52 | v. rights protecting the extraction, dissemination, use and reuse of data 53 | in a Work; 54 | vi. database rights (such as those arising under Directive 96/9/EC of the 55 | European Parliament and of the Council of 11 March 1996 on the legal 56 | protection of databases, and under any national implementation 57 | thereof, including any amended or successor version of such 58 | directive); and 59 | vii. other similar, equivalent or corresponding rights throughout the 60 | world based on applicable law or treaty, and any national 61 | implementations thereof. 62 | 63 | 2. Waiver. To the greatest extent permitted by, but not in contravention 64 | of, applicable law, Affirmer hereby overtly, fully, permanently, 65 | irrevocably and unconditionally waives, abandons, and surrenders all of 66 | Affirmer's Copyright and Related Rights and associated claims and causes 67 | of action, whether now known or unknown (including existing as well as 68 | future claims and causes of action), in the Work (i) in all territories 69 | worldwide, (ii) for the maximum duration provided by applicable law or 70 | treaty (including future time extensions), (iii) in any current or future 71 | medium and for any number of copies, and (iv) for any purpose whatsoever, 72 | including without limitation commercial, advertising or promotional 73 | purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each 74 | member of the public at large and to the detriment of Affirmer's heirs and 75 | successors, fully intending that such Waiver shall not be subject to 76 | revocation, rescission, cancellation, termination, or any other legal or 77 | equitable action to disrupt the quiet enjoyment of the Work by the public 78 | as contemplated by Affirmer's express Statement of Purpose. 79 | 80 | 3. Public License Fallback. Should any part of the Waiver for any reason 81 | be judged legally invalid or ineffective under applicable law, then the 82 | Waiver shall be preserved to the maximum extent permitted taking into 83 | account Affirmer's express Statement of Purpose. In addition, to the 84 | extent the Waiver is so judged Affirmer hereby grants to each affected 85 | person a royalty-free, non transferable, non sublicensable, non exclusive, 86 | irrevocable and unconditional license to exercise Affirmer's Copyright and 87 | Related Rights in the Work (i) in all territories worldwide, (ii) for the 88 | maximum duration provided by applicable law or treaty (including future 89 | time extensions), (iii) in any current or future medium and for any number 90 | of copies, and (iv) for any purpose whatsoever, including without 91 | limitation commercial, advertising or promotional purposes (the 92 | "License"). The License shall be deemed effective as of the date CC0 was 93 | applied by Affirmer to the Work. Should any part of the License for any 94 | reason be judged legally invalid or ineffective under applicable law, such 95 | partial invalidity or ineffectiveness shall not invalidate the remainder 96 | of the License, and in such case Affirmer hereby affirms that he or she 97 | will not (i) exercise any of his or her remaining Copyright and Related 98 | Rights in the Work or (ii) assert any associated claims and causes of 99 | action with respect to the Work, in either case contrary to Affirmer's 100 | express Statement of Purpose. 101 | 102 | 4. Limitations and Disclaimers. 103 | 104 | a. No trademark or patent rights held by Affirmer are waived, abandoned, 105 | surrendered, licensed or otherwise affected by this document. 106 | b. Affirmer offers the Work as-is and makes no representations or 107 | warranties of any kind concerning the Work, express, implied, 108 | statutory or otherwise, including without limitation warranties of 109 | title, merchantability, fitness for a particular purpose, non 110 | infringement, or the absence of latent or other defects, accuracy, or 111 | the present or absence of errors, whether or not discoverable, all to 112 | the greatest extent permissible under applicable law. 113 | c. Affirmer disclaims responsibility for clearing rights of other persons 114 | that may apply to the Work or any use thereof, including without 115 | limitation any person's Copyright and Related Rights in the Work. 116 | Further, Affirmer disclaims responsibility for obtaining any necessary 117 | consents, permissions or other rights required for any use of the 118 | Work. 119 | d. Affirmer understands and acknowledges that Creative Commons is not a 120 | party to this document and has no duty or obligation with respect to 121 | this CC0 or use of the Work. 122 | -------------------------------------------------------------------------------- /LICENSE-MIT0: -------------------------------------------------------------------------------- 1 | Permission is hereby granted, free of charge, to any person obtaining a 2 | copy of this software and associated documentation files (the 3 | "Software"), to deal in the Software without restriction, including 4 | without limitation the rights to use, copy, modify, merge, publish, 5 | distribute, sublicense, and/or sell copies of the Software, and to 6 | permit persons to whom the Software is furnished to do so. 7 | 8 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 9 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 10 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 11 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 12 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 13 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 14 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 15 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | Compares two equal-sized byte strings in constant time. 2 | 3 | Inspired by the Linux kernel's crypto_memneq. 4 | 5 | Licensed under either of 6 | 7 | * Apache License, Version 2.0 (LICENSE-APACHE) 8 | * MIT No Attribution License (LICENSE-MIT0) 9 | * CC0 1.0 Universal (LICENSE-CC0) 10 | 11 | at your option. 12 | -------------------------------------------------------------------------------- /benches/bench.rs: -------------------------------------------------------------------------------- 1 | use constant_time_eq::{constant_time_eq, constant_time_eq_n}; 2 | use core::hint::black_box; 3 | use criterion::{ 4 | BenchmarkGroup, BenchmarkId, Criterion, Throughput, criterion_group, criterion_main, 5 | measurement::WallTime, 6 | }; 7 | 8 | fn bench_array(c: &mut Criterion) { 9 | let mut group = c.benchmark_group("constant_time_eq_n"); 10 | 11 | fn bench_array_n(group: &mut BenchmarkGroup) { 12 | let input = (&[1; N], &[2; N]); 13 | group.throughput(Throughput::Bytes(N as u64)); 14 | group.bench_with_input(BenchmarkId::from_parameter(N), &input, |b, &(x, y)| { 15 | b.iter(|| constant_time_eq_n(black_box(x), black_box(y))) 16 | }); 17 | } 18 | 19 | bench_array_n::<8>(&mut group); 20 | bench_array_n::<16>(&mut group); 21 | bench_array_n::<20>(&mut group); 22 | bench_array_n::<32>(&mut group); 23 | bench_array_n::<64>(&mut group); 24 | bench_array_n::<96>(&mut group); 25 | bench_array_n::<128>(&mut group); 26 | 27 | group.finish(); 28 | } 29 | 30 | fn bench_slice(c: &mut Criterion) { 31 | let mut group = c.benchmark_group("constant_time_eq"); 32 | 33 | let input = (&[1; 65536], &[2; 65536]); 34 | for &size in &[8, 16, 20, 32, 64, 96, 128, 4 * 1024, 16 * 1024, 64 * 1024] { 35 | let input = (&input.0[..size], &input.1[..size]); 36 | group.throughput(Throughput::Bytes(size as u64)); 37 | group.bench_with_input(BenchmarkId::from_parameter(size), &input, |b, &(x, y)| { 38 | b.iter(|| constant_time_eq(black_box(x), black_box(y))) 39 | }); 40 | } 41 | 42 | group.finish(); 43 | } 44 | 45 | criterion_group!(benches, bench_array, bench_slice); 46 | criterion_main!(benches); 47 | -------------------------------------------------------------------------------- /benches/bench_classic.rs: -------------------------------------------------------------------------------- 1 | use constant_time_eq::classic::{constant_time_eq, constant_time_eq_n}; 2 | use core::hint::black_box; 3 | use criterion::{ 4 | BenchmarkGroup, BenchmarkId, Criterion, Throughput, criterion_group, criterion_main, 5 | measurement::WallTime, 6 | }; 7 | 8 | fn bench_array(c: &mut Criterion) { 9 | let mut group = c.benchmark_group("classic::constant_time_eq_n"); 10 | 11 | fn bench_array_n(group: &mut BenchmarkGroup) { 12 | let input = (&[1; N], &[2; N]); 13 | group.throughput(Throughput::Bytes(N as u64)); 14 | group.bench_with_input(BenchmarkId::from_parameter(N), &input, |b, &(x, y)| { 15 | b.iter(|| constant_time_eq_n(black_box(x), black_box(y))) 16 | }); 17 | } 18 | 19 | bench_array_n::<8>(&mut group); 20 | bench_array_n::<16>(&mut group); 21 | bench_array_n::<20>(&mut group); 22 | bench_array_n::<32>(&mut group); 23 | bench_array_n::<64>(&mut group); 24 | bench_array_n::<96>(&mut group); 25 | bench_array_n::<128>(&mut group); 26 | 27 | group.finish(); 28 | } 29 | 30 | fn bench_slice(c: &mut Criterion) { 31 | let mut group = c.benchmark_group("classic::constant_time_eq"); 32 | 33 | let input = (&[1; 65536], &[2; 65536]); 34 | for &size in &[8, 16, 20, 32, 64, 96, 128, 4 * 1024, 16 * 1024, 64 * 1024] { 35 | let input = (&input.0[..size], &input.1[..size]); 36 | group.throughput(Throughput::Bytes(size as u64)); 37 | group.bench_with_input(BenchmarkId::from_parameter(size), &input, |b, &(x, y)| { 38 | b.iter(|| constant_time_eq(black_box(x), black_box(y))) 39 | }); 40 | } 41 | 42 | group.finish(); 43 | } 44 | 45 | criterion_group!(benches, bench_array, bench_slice); 46 | criterion_main!(benches); 47 | -------------------------------------------------------------------------------- /benches/bench_generic.rs: -------------------------------------------------------------------------------- 1 | use constant_time_eq::generic::{constant_time_eq, constant_time_eq_n}; 2 | use core::hint::black_box; 3 | use criterion::{ 4 | BenchmarkGroup, BenchmarkId, Criterion, Throughput, criterion_group, criterion_main, 5 | measurement::WallTime, 6 | }; 7 | 8 | fn bench_array(c: &mut Criterion) { 9 | let mut group = c.benchmark_group("generic::constant_time_eq_n"); 10 | 11 | fn bench_array_n(group: &mut BenchmarkGroup) { 12 | let input = (&[1; N], &[2; N]); 13 | group.throughput(Throughput::Bytes(N as u64)); 14 | group.bench_with_input(BenchmarkId::from_parameter(N), &input, |b, &(x, y)| { 15 | b.iter(|| constant_time_eq_n(black_box(x), black_box(y))) 16 | }); 17 | } 18 | 19 | bench_array_n::<8>(&mut group); 20 | bench_array_n::<16>(&mut group); 21 | bench_array_n::<20>(&mut group); 22 | bench_array_n::<32>(&mut group); 23 | bench_array_n::<64>(&mut group); 24 | bench_array_n::<96>(&mut group); 25 | bench_array_n::<128>(&mut group); 26 | 27 | group.finish(); 28 | } 29 | 30 | fn bench_slice(c: &mut Criterion) { 31 | let mut group = c.benchmark_group("generic::constant_time_eq"); 32 | 33 | let input = (&[1; 65536], &[2; 65536]); 34 | for &size in &[8, 16, 20, 32, 64, 96, 128, 4 * 1024, 16 * 1024, 64 * 1024] { 35 | let input = (&input.0[..size], &input.1[..size]); 36 | group.throughput(Throughput::Bytes(size as u64)); 37 | group.bench_with_input(BenchmarkId::from_parameter(size), &input, |b, &(x, y)| { 38 | b.iter(|| constant_time_eq(black_box(x), black_box(y))) 39 | }); 40 | } 41 | 42 | group.finish(); 43 | } 44 | 45 | criterion_group!(benches, bench_array, bench_slice); 46 | criterion_main!(benches); 47 | -------------------------------------------------------------------------------- /src/classic.rs: -------------------------------------------------------------------------------- 1 | //! Classic implementation of constant_time_eq and constant_time_eq_n 2 | //! 3 | //! This is the implementation from classic versions of this crate. Kept for benchmarking purposes 4 | //! only, since its protection against the optimizer is weaker: a sufficiently smart optimizer 5 | //! might notice that the accumulator will not change anymore once it becomes all-ones, and 6 | //! introduce an early exit. On the other hand, this weaker protection allows the optimizer to use 7 | //! vector registers to compare many bytes in parallel, without having to write explicit SIMD code. 8 | 9 | use crate::with_dit; 10 | 11 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] 12 | #[cfg(not(miri))] 13 | #[inline] 14 | #[must_use] 15 | fn optimizer_hide(mut value: u8) -> u8 { 16 | // SAFETY: the input value is passed unchanged to the output, the inline assembly does nothing. 17 | unsafe { 18 | core::arch::asm!("/* {0} */", inout(reg_byte) value, options(pure, nomem, nostack, preserves_flags)); 19 | value 20 | } 21 | } 22 | 23 | #[cfg(any( 24 | target_arch = "arm", 25 | target_arch = "aarch64", 26 | target_arch = "riscv32", 27 | target_arch = "riscv64" 28 | ))] 29 | #[cfg(not(miri))] 30 | #[inline] 31 | #[must_use] 32 | #[allow(asm_sub_register)] 33 | fn optimizer_hide(mut value: u8) -> u8 { 34 | // SAFETY: the input value is passed unchanged to the output, the inline assembly does nothing. 35 | unsafe { 36 | core::arch::asm!("/* {0} */", inout(reg) value, options(pure, nomem, nostack, preserves_flags)); 37 | value 38 | } 39 | } 40 | 41 | #[cfg(any( 42 | not(any( 43 | target_arch = "x86", 44 | target_arch = "x86_64", 45 | target_arch = "arm", 46 | target_arch = "aarch64", 47 | target_arch = "riscv32", 48 | target_arch = "riscv64", 49 | )), 50 | miri, 51 | ))] 52 | #[inline(never)] 53 | #[must_use] 54 | fn optimizer_hide(value: u8) -> u8 { 55 | // The current implementation of black_box in the main codegen backends is similar to 56 | // { 57 | // let result = value; 58 | // asm!("", in(reg) &result); 59 | // result 60 | // } 61 | // which round-trips the value through the stack, instead of leaving it in a register. 62 | // Experimental codegen backends might implement black_box as a pure identity function, 63 | // without the expected optimization barrier, so it's less guaranteed than inline asm. 64 | // For that reason, we also use the #[inline(never)] hint, which makes it harder for an 65 | // optimizer to look inside this function. 66 | core::hint::black_box(value) 67 | } 68 | 69 | #[inline] 70 | #[must_use] 71 | fn constant_time_ne(a: &[u8], b: &[u8]) -> u8 { 72 | assert!(a.len() == b.len()); 73 | 74 | // These useless slices make the optimizer elide the bounds checks. 75 | // See the comment in clone_from_slice() added on Rust commit 6a7bc47. 76 | let len = a.len(); 77 | let a = &a[..len]; 78 | let b = &b[..len]; 79 | 80 | let mut tmp = 0; 81 | for i in 0..len { 82 | tmp |= a[i] ^ b[i]; 83 | } 84 | 85 | // The compare with 0 must happen outside this function. 86 | optimizer_hide(tmp) 87 | } 88 | 89 | /// Compares two equal-sized byte strings in constant time. 90 | /// 91 | /// # Examples 92 | /// 93 | /// ``` 94 | /// use constant_time_eq::constant_time_eq; 95 | /// 96 | /// assert!(constant_time_eq(b"foo", b"foo")); 97 | /// assert!(!constant_time_eq(b"foo", b"bar")); 98 | /// assert!(!constant_time_eq(b"bar", b"baz")); 99 | /// # assert!(constant_time_eq(b"", b"")); 100 | /// 101 | /// // Not equal-sized, so won't take constant time. 102 | /// assert!(!constant_time_eq(b"foo", b"")); 103 | /// assert!(!constant_time_eq(b"foo", b"quux")); 104 | /// ``` 105 | #[must_use] 106 | pub fn constant_time_eq(a: &[u8], b: &[u8]) -> bool { 107 | with_dit(|| a.len() == b.len() && constant_time_ne(a, b) == 0) 108 | } 109 | 110 | // Fixed-size array variant. 111 | 112 | #[inline] 113 | #[must_use] 114 | fn constant_time_ne_n(a: &[u8; N], b: &[u8; N]) -> u8 { 115 | let mut tmp = 0; 116 | for i in 0..N { 117 | tmp |= a[i] ^ b[i]; 118 | } 119 | 120 | // The compare with 0 must happen outside this function. 121 | optimizer_hide(tmp) 122 | } 123 | 124 | /// Compares two fixed-size byte strings in constant time. 125 | /// 126 | /// # Examples 127 | /// 128 | /// ``` 129 | /// use constant_time_eq::constant_time_eq_n; 130 | /// 131 | /// assert!(constant_time_eq_n(&[3; 20], &[3; 20])); 132 | /// assert!(!constant_time_eq_n(&[3; 20], &[7; 20])); 133 | /// ``` 134 | #[must_use] 135 | pub fn constant_time_eq_n(a: &[u8; N], b: &[u8; N]) -> bool { 136 | with_dit(|| constant_time_ne_n(a, b) == 0) 137 | } 138 | 139 | // Fixed-size variants for the most common sizes. 140 | 141 | /// Compares two 128-bit byte strings in constant time. 142 | /// 143 | /// # Examples 144 | /// 145 | /// ``` 146 | /// use constant_time_eq::constant_time_eq_16; 147 | /// 148 | /// assert!(constant_time_eq_16(&[3; 16], &[3; 16])); 149 | /// assert!(!constant_time_eq_16(&[3; 16], &[7; 16])); 150 | /// ``` 151 | #[inline] 152 | #[must_use] 153 | pub fn constant_time_eq_16(a: &[u8; 16], b: &[u8; 16]) -> bool { 154 | constant_time_eq_n(a, b) 155 | } 156 | 157 | /// Compares two 256-bit byte strings in constant time. 158 | /// 159 | /// # Examples 160 | /// 161 | /// ``` 162 | /// use constant_time_eq::constant_time_eq_32; 163 | /// 164 | /// assert!(constant_time_eq_32(&[3; 32], &[3; 32])); 165 | /// assert!(!constant_time_eq_32(&[3; 32], &[7; 32])); 166 | /// ``` 167 | #[inline] 168 | #[must_use] 169 | pub fn constant_time_eq_32(a: &[u8; 32], b: &[u8; 32]) -> bool { 170 | constant_time_eq_n(a, b) 171 | } 172 | 173 | /// Compares two 512-bit byte strings in constant time. 174 | /// 175 | /// # Examples 176 | /// 177 | /// ``` 178 | /// use constant_time_eq::constant_time_eq_64; 179 | /// 180 | /// assert!(constant_time_eq_64(&[3; 64], &[3; 64])); 181 | /// assert!(!constant_time_eq_64(&[3; 64], &[7; 64])); 182 | /// ``` 183 | #[inline] 184 | #[must_use] 185 | pub fn constant_time_eq_64(a: &[u8; 64], b: &[u8; 64]) -> bool { 186 | constant_time_eq_n(a, b) 187 | } 188 | 189 | #[cfg(test)] 190 | mod tests { 191 | #[cfg(feature = "count_instructions_test")] 192 | extern crate std; 193 | 194 | #[cfg(feature = "count_instructions_test")] 195 | #[test] 196 | fn count_optimizer_hide_instructions() -> std::io::Result<()> { 197 | use super::optimizer_hide; 198 | use count_instructions::count_instructions; 199 | 200 | fn count() -> std::io::Result { 201 | // If optimizer_hide does not work, constant propagation and folding 202 | // will make this identical to count_optimized() below. 203 | let mut count = 0; 204 | assert_eq!( 205 | 10u8, 206 | count_instructions( 207 | || optimizer_hide(1) 208 | + optimizer_hide(2) 209 | + optimizer_hide(3) 210 | + optimizer_hide(4), 211 | |_| count += 1 212 | )? 213 | ); 214 | Ok(count) 215 | } 216 | 217 | fn count_optimized() -> std::io::Result { 218 | #[inline] 219 | fn inline_identity(value: u8) -> u8 { 220 | value 221 | } 222 | 223 | let mut count = 0; 224 | assert_eq!( 225 | 10u8, 226 | count_instructions( 227 | || inline_identity(1) 228 | + inline_identity(2) 229 | + inline_identity(3) 230 | + inline_identity(4), 231 | |_| count += 1 232 | )? 233 | ); 234 | Ok(count) 235 | } 236 | 237 | assert!(count()? > count_optimized()?); 238 | Ok(()) 239 | } 240 | } 241 | -------------------------------------------------------------------------------- /src/dit.rs: -------------------------------------------------------------------------------- 1 | //! Runs code with the hardware DIT feature enabled when possible. 2 | //! 3 | //! With the "std" feature, detects at compilation time and runtime whether FEAT_DIT is available, 4 | //! and uses it (together with FEAT_SB when available) to enable the data independent timing mode 5 | //! of the processor. 6 | //! 7 | //! Without the "std" feature, this detection is done at compilation time only, which is enough for 8 | //! some targets like aarch64-apple-darwin which is known to always have these features. 9 | 10 | use core::arch::asm; 11 | 12 | /// Describes whether `FEAT_DIT` and `FEAT_SB` are known to be implemented. 13 | #[repr(u8)] 14 | #[derive(Clone, Copy)] 15 | enum Features { 16 | // Unknown = 0, 17 | #[allow(dead_code)] 18 | Neither = 1, 19 | #[allow(dead_code)] 20 | DitOnly = 2, 21 | DitSb = 3, 22 | } 23 | 24 | #[cfg(not(all(target_feature = "dit", target_feature = "sb")))] 25 | mod detect { 26 | use super::Features; 27 | use core::mem::transmute; 28 | use core::sync::atomic::{AtomicU8, Ordering}; 29 | 30 | static FEATURES: AtomicU8 = AtomicU8::new(0); 31 | 32 | /// Determines whether `FEAT_DIT` and `FEAT_SB` are known to be implemented. 33 | #[inline] 34 | pub fn get_aarch64_dit_sb_features() -> Features { 35 | let features = FEATURES.load(Ordering::Relaxed); 36 | if features > 0 { 37 | // SAFETY: a non-zero value is a valid discriminant from Features 38 | unsafe { transmute::(features) } 39 | } else { 40 | detect_aarch64_dit_sb_features() 41 | } 42 | } 43 | 44 | /// Records whether `FEAT_DIT` and `FEAT_SB` are known to be implemented. 45 | /// 46 | /// # Safety 47 | /// 48 | /// Either parameter must not be set to true if the corresponding feature 49 | /// is not implemented. 50 | pub unsafe fn set_aarch64_dit_sb_features(dit: bool, sb: bool) -> Features { 51 | let dit = dit || cfg!(target_feature = "dit"); 52 | let sb = sb || cfg!(target_feature = "sb"); 53 | let features = match (dit, sb) { 54 | (true, true) => Features::DitSb, 55 | (true, false) => Features::DitOnly, 56 | _ => Features::Neither, 57 | }; 58 | let _ = FEATURES.compare_exchange(0, features as u8, Ordering::Relaxed, Ordering::Relaxed); 59 | features 60 | } 61 | 62 | /// Detects whether `FEAT_DIT` and `FEAT_SB` are known to be implemented. 63 | #[cfg(feature = "std")] 64 | #[cold] 65 | fn detect_aarch64_dit_sb_features() -> Features { 66 | use std::arch::is_aarch64_feature_detected; 67 | // SAFETY: each parameter is true only if the feature is implemented 68 | unsafe { 69 | set_aarch64_dit_sb_features( 70 | is_aarch64_feature_detected!("dit"), 71 | is_aarch64_feature_detected!("sb"), 72 | ) 73 | } 74 | } 75 | 76 | /// Detects whether `FEAT_DIT` and `FEAT_SB` are known to be implemented. 77 | #[cfg(not(feature = "std"))] 78 | #[cold] 79 | fn detect_aarch64_dit_sb_features() -> Features { 80 | // It might or might not be possible to read the system registers 81 | // AA64PFR0_EL1 and AA64ISAR1_EL1 here; they might even be available 82 | // at EL0 if HWCAP_CPUID is set in AT_HWCAP, but being no_std means 83 | // this code might be called in a context where we cannot call into 84 | // the libc to obtain the auxv (and if we could, we could read from 85 | // AT_HWCAP the HWCAP_DIT and HWCAP_SB bits directly). 86 | // 87 | // The best that can be done, without adding several ARM-specific 88 | // features to specify "this code will run at EL1" or "this code 89 | // will run under a Linux kernel greater than 4.11", is to use what's 90 | // known to be implemented at compile time, and allow an override 91 | // through the undocumented `set_aarch64_dit_sb_features` function. 92 | 93 | // SAFETY: each parameter is true only if the feature is implemented 94 | unsafe { 95 | set_aarch64_dit_sb_features(cfg!(target_feature = "dit"), cfg!(target_feature = "sb")) 96 | } 97 | } 98 | } 99 | 100 | /// Overrides the runtime detection of `FEAT_DIT` and `FEAT_SB`. 101 | /// 102 | /// This must be called before other threads are created, and before 103 | /// any other code in this `constant_time_eq` crate is called. 104 | /// 105 | /// # Safety 106 | /// 107 | /// Either parameter must not be set to true if the corresponding feature 108 | /// is not implemented. 109 | pub unsafe fn set_aarch64_dit_sb_features(_dit: bool, _sb: bool) { 110 | // SAFETY: the safety requirements are the same as this function 111 | #[cfg(not(all(target_feature = "dit", target_feature = "sb")))] 112 | unsafe { 113 | detect::set_aarch64_dit_sb_features(_dit, _sb); 114 | } 115 | } 116 | 117 | #[cfg(not(all(target_feature = "dit", target_feature = "sb")))] 118 | use detect::get_aarch64_dit_sb_features; 119 | 120 | /// Determines whether `FEAT_DIT` and `FEAT_SB` are known to be implemented. 121 | #[cfg(all(target_feature = "dit", target_feature = "sb"))] 122 | #[inline(always)] 123 | fn get_aarch64_dit_sb_features() -> Features { 124 | // Both are known to be implemented at compile time, skip the detection. 125 | Features::DitSb 126 | } 127 | 128 | /// Equivalent to `__arm_rsr64("dit")`. 129 | /// 130 | /// # Safety 131 | /// 132 | /// Must be called only when `FEAT_DIT` is implemented. 133 | #[inline] 134 | #[target_feature(enable = "dit")] 135 | unsafe fn rsr64_dit() -> u64 { 136 | let mut value; 137 | // SAFETY: called only when `FEAT_DIT` is implemented 138 | unsafe { 139 | asm!("mrs {}, dit", lateout(reg) value, options(nomem, preserves_flags, nostack)); 140 | } 141 | value 142 | } 143 | 144 | /// Equivalent to `__arm_wsr64("dit", value)`. 145 | /// 146 | /// # Safety 147 | /// 148 | /// Must be called only when `FEAT_DIT` is implemented. 149 | #[inline] 150 | #[target_feature(enable = "dit")] 151 | unsafe fn wsr64_dit(value: u64) { 152 | // SAFETY: called only when `FEAT_DIT` is implemented 153 | unsafe { 154 | // The compiler must not cache values or flags across this instruction. 155 | asm!("msr dit, {}", in(reg) value, options(nostack)); 156 | } 157 | } 158 | 159 | /// Equivalent to `__arm_wsr64("dit", 1 << 24)`. 160 | /// 161 | /// # Safety 162 | /// 163 | /// Must be called only when `FEAT_DIT` is implemented. 164 | #[inline] 165 | #[target_feature(enable = "dit")] 166 | unsafe fn enable_dit() { 167 | // SAFETY: called only when `FEAT_DIT` is implemented 168 | unsafe { 169 | // The compiler must not cache values or flags across this instruction. 170 | asm!("msr dit, #{}", const 1, options(nostack)); 171 | } 172 | } 173 | 174 | /// Equivalent to `__asm__ __volatile__("sb" ::: "memory")`. 175 | /// 176 | /// # Safety 177 | /// 178 | /// Must be called only when `FEAT_SB` is implemented. 179 | #[inline] 180 | #[target_feature(enable = "sb")] 181 | unsafe fn speculation_barrier() { 182 | // SAFETY: called only when `FEAT_SB` is implemented 183 | unsafe { 184 | // The compiler must not cache values or flags across this instruction. 185 | asm!("sb", options(nostack)); 186 | } 187 | } 188 | 189 | /// Synchronization barrier for when `FEAT_SB` is not available. 190 | /// 191 | /// This heavier alternative is recommended by Apple when SB is not available, see: 192 | /// 193 | #[inline] 194 | fn synchronization_barrier() { 195 | // SAFETY: these instructions are always available, and have no effects 196 | // other than being a data barrier and flushing the pipeline 197 | unsafe { 198 | // The compiler must not cache values or flags across this instruction. 199 | asm!("dsb nsh", "isb sy", options(nostack)); 200 | } 201 | } 202 | 203 | /// Wraps code with DIT when `FEAT_DIT` and `FEAT_SB` were detected. 204 | /// 205 | /// # Safety 206 | /// 207 | /// `FEAT_DIT` and `FEAT_SB` must have been detected. 208 | #[inline] 209 | #[target_feature(enable = "dit,sb")] 210 | unsafe fn with_feat_dit_sb(f: F) -> T 211 | where 212 | F: FnOnce() -> T, 213 | { 214 | struct Guard { 215 | dit: u64, 216 | } 217 | 218 | impl Drop for Guard { 219 | #[inline] 220 | fn drop(&mut self) { 221 | // SAFETY: called only when `FEAT_DIT` is implemented 222 | unsafe { wsr64_dit(self.dit) }; 223 | } 224 | } 225 | 226 | let _guard = Guard { 227 | // SAFETY: called only when `FEAT_DIT` is implemented 228 | dit: unsafe { rsr64_dit() }, 229 | }; 230 | 231 | // SAFETY: called only when `FEAT_DIT` is implemented 232 | unsafe { enable_dit() }; 233 | 234 | // SAFETY: called only when `FEAT_SB` is implemented 235 | unsafe { speculation_barrier() }; 236 | 237 | f() 238 | } 239 | 240 | /// Wraps code with DIT when `FEAT_DIT` was detected but not `FEAT_SB`. 241 | /// 242 | /// # Safety 243 | /// 244 | /// `FEAT_DIT` must have been detected. 245 | #[inline] 246 | #[target_feature(enable = "dit")] 247 | unsafe fn with_feat_dit(f: F) -> T 248 | where 249 | F: FnOnce() -> T, 250 | { 251 | struct Guard { 252 | dit: u64, 253 | } 254 | 255 | impl Drop for Guard { 256 | #[inline] 257 | fn drop(&mut self) { 258 | // SAFETY: called only when `FEAT_DIT` is implemented 259 | unsafe { wsr64_dit(self.dit) }; 260 | } 261 | } 262 | 263 | let _guard = Guard { 264 | // SAFETY: called only when `FEAT_DIT` is implemented 265 | dit: unsafe { rsr64_dit() }, 266 | }; 267 | 268 | // SAFETY: called only when `FEAT_DIT` is implemented 269 | unsafe { enable_dit() }; 270 | 271 | synchronization_barrier(); 272 | 273 | f() 274 | } 275 | 276 | /// Runs code with the hardware DIT feature enabled when possible. 277 | #[inline] 278 | pub(crate) fn with_dit(f: F) -> T 279 | where 280 | F: FnOnce() -> T, 281 | { 282 | // The use of #[target_feature] disables inlining in some cases. 283 | // Repeating the code three times with different #[target_feature] 284 | // generates better code. 285 | match get_aarch64_dit_sb_features() { 286 | Features::DitSb => { 287 | // SAFETY: both `FEAT_DIT` and `FEAT_SB` were detected 288 | unsafe { with_feat_dit_sb(f) } 289 | } 290 | Features::DitOnly => { 291 | // SAFETY: `FEAT_DIT` was detected 292 | unsafe { with_feat_dit(f) } 293 | } 294 | Features::Neither => f(), 295 | } 296 | } 297 | 298 | #[cfg(test)] 299 | mod tests { 300 | extern crate std; 301 | 302 | use super::{rsr64_dit, with_dit}; 303 | use std::arch::is_aarch64_feature_detected; 304 | 305 | #[test] 306 | fn dit_is_restored_after_with_dit() { 307 | if is_aarch64_feature_detected!("dit") { 308 | // SAFETY: `FEAT_DIT` was detected 309 | unsafe { 310 | let saved = rsr64_dit(); 311 | with_dit(|| assert_ne!(rsr64_dit(), 0)); 312 | assert_eq!(rsr64_dit(), saved); 313 | } 314 | } 315 | } 316 | } 317 | -------------------------------------------------------------------------------- /src/generic.rs: -------------------------------------------------------------------------------- 1 | //! Generic implementation of `constant_time_eq` and `constant_time_eq_n`. 2 | //! 3 | //! This implementation does SIMD in general-purpose registers instead of vector registers, and 4 | //! uses inline assembly only to hide the dependencies and comparisons from the optimizer, to 5 | //! prevent it from returning early when the accumulator becomes non-zero (found a difference) or 6 | //! all-ones (the accumulator can no longer change). 7 | //! 8 | //! This generic implementation is also used for suffixes smaller than one vector from the 9 | //! architecture-specific vector implementations. This is simpler and often faster than trying to 10 | //! load a partial vector register. 11 | 12 | use core::mem::size_of; 13 | use core::ops::BitXor; 14 | use core::ptr::read_unaligned; 15 | 16 | use crate::with_dit; 17 | 18 | /// The natural word type for this architecture. All bit patterns must be valid for this type. 19 | #[cfg(all( 20 | target_pointer_width = "64", 21 | any(not(target_arch = "riscv64"), target_feature = "unaligned-scalar-mem") 22 | ))] 23 | pub(crate) type Word = u64; 24 | 25 | /// The natural word type for this architecture. All bit patterns must be valid for this type. 26 | #[cfg(all( 27 | target_pointer_width = "32", 28 | any(not(target_arch = "riscv32"), target_feature = "unaligned-scalar-mem") 29 | ))] 30 | pub(crate) type Word = u32; 31 | 32 | /// The natural word type for this architecture. All bit patterns must be valid for this type. 33 | #[cfg(target_pointer_width = "16")] 34 | pub(crate) type Word = u16; 35 | 36 | /// The natural word type for this architecture. All bit patterns must be valid for this type. 37 | #[cfg(not(any( 38 | target_pointer_width = "64", 39 | target_pointer_width = "32", 40 | target_pointer_width = "16" 41 | )))] 42 | pub(crate) type Word = usize; 43 | 44 | // RISC-V without unaligned-scalar-mem generates worse code for unaligned word reads. 45 | /// The natural word type for this architecture. All bit patterns must be valid for this type. 46 | #[cfg(all( 47 | any(target_arch = "riscv64", target_arch = "riscv32"), 48 | not(target_feature = "unaligned-scalar-mem") 49 | ))] 50 | pub(crate) type Word = u8; 51 | 52 | /// Hides a value from the optimizer. 53 | #[cfg(all( 54 | not(miri), 55 | any( 56 | target_arch = "x86", 57 | target_arch = "x86_64", 58 | target_arch = "arm", 59 | target_arch = "aarch64", 60 | target_arch = "arm64ec", 61 | target_arch = "riscv32", 62 | target_arch = "riscv64", 63 | target_arch = "loongarch64", 64 | target_arch = "s390x", 65 | ) 66 | ))] 67 | #[must_use] 68 | #[inline(always)] 69 | fn optimizer_hide(mut value: Word) -> Word { 70 | // SAFETY: the input value is passed unchanged to the output, the inline assembly does nothing. 71 | unsafe { 72 | core::arch::asm!("/* {0} */", inlateout(reg) value, options(pure, nomem, preserves_flags, nostack)); 73 | } 74 | value 75 | } 76 | 77 | /// Attempts to hide a value from the optimizer. 78 | #[cfg(any( 79 | miri, 80 | not(any( 81 | target_arch = "x86", 82 | target_arch = "x86_64", 83 | target_arch = "arm", 84 | target_arch = "aarch64", 85 | target_arch = "arm64ec", 86 | target_arch = "riscv32", 87 | target_arch = "riscv64", 88 | target_arch = "loongarch64", 89 | target_arch = "s390x", 90 | )) 91 | ))] 92 | #[must_use] 93 | #[inline(never)] 94 | fn optimizer_hide(value: Word) -> Word { 95 | // The current implementation of black_box in the main codegen backends is similar to 96 | // { 97 | // let result = value; 98 | // asm!("", in(reg) &result); 99 | // result 100 | // } 101 | // which round-trips the value through the stack, instead of leaving it in a register. 102 | // Experimental codegen backends might implement black_box as a pure identity function, 103 | // without the expected optimization barrier, so it's less guaranteed than inline asm. 104 | // For that reason, we also use the #[inline(never)] hint, which makes it harder for an 105 | // optimizer to look inside this function. 106 | core::hint::black_box(value) 107 | } 108 | 109 | /// Equivalent to `read_unaligned` for byte slices. 110 | /// 111 | /// # Safety 112 | /// 113 | /// All bit patterns must be valid for type T. 114 | #[must_use] 115 | #[inline(always)] 116 | unsafe fn read_unaligned_from_slice(src: &[u8]) -> T { 117 | assert_eq!(src.len(), size_of::()); 118 | 119 | // SAFETY: the slice has enough bytes for type T 120 | // SAFETY: all bit patterns are valid for type T 121 | unsafe { read_unaligned(src.as_ptr().cast::()) } 122 | } 123 | 124 | /// Generic implementation of `constant_time_eq` and `constant_time_eq_n`. 125 | #[must_use] 126 | #[inline(always)] 127 | pub(crate) fn constant_time_eq_impl(mut a: &[u8], mut b: &[u8], mut tmp: Word) -> bool { 128 | if a.len() != b.len() { 129 | return false; 130 | } 131 | 132 | // This statement does nothing, because a.len() == b.len() here, 133 | // but it makes the optimizer elide some useless bounds checks. 134 | b = &b[..a.len()]; 135 | 136 | // Early exit for the common case when called by the SIMD code. 137 | if a.is_empty() { 138 | return tmp == 0; 139 | } 140 | 141 | /// Reads and compares a single word from the input, adjusting the slices. 142 | /// Returns zero if both words are equal, non-zero if any byte is different. 143 | /// 144 | /// # Safety 145 | /// 146 | /// All bit patterns must be valid for type T. 147 | #[must_use] 148 | #[inline(always)] 149 | unsafe fn cmp_step>(a: &mut &[u8], b: &mut &[u8]) -> T { 150 | // SAFETY: all bit patterns are valid for type T 151 | let tmpa = unsafe { read_unaligned_from_slice::(&a[..size_of::()]) }; 152 | // SAFETY: all bit patterns are valid for type T 153 | let tmpb = unsafe { read_unaligned_from_slice::(&b[..size_of::()]) }; 154 | 155 | *a = &a[size_of::()..]; 156 | *b = &b[size_of::()..]; 157 | 158 | tmpa ^ tmpb 159 | } 160 | 161 | // The optimizer is not allowed to assume anything about the value of tmp after each iteration, 162 | // which prevents it from terminating the loop early if the value becomes non-zero or all-ones. 163 | 164 | // Do most of the work using the natural word size; the other blocks clean up the leftovers. 165 | while a.len() >= size_of::() { 166 | // SAFETY: all bit patterns are valid for Word 167 | let cmp = optimizer_hide(unsafe { cmp_step::(&mut a, &mut b) }); 168 | tmp = optimizer_hide(tmp | cmp); 169 | } 170 | 171 | // These first two blocks would only be necessary for architectures with usize > 64 bits. 172 | // They are kept here for future-proofing, so that everything still works in that case. 173 | // The optimizer tracks the range of len and will not generate any code for these blocks. 174 | while a.len() >= size_of::() { 175 | // SAFETY: all bit patterns are valid for u128 176 | let cmp = optimizer_hide(unsafe { cmp_step::(&mut a, &mut b) } as Word); 177 | tmp = optimizer_hide(tmp | cmp); 178 | } 179 | if a.len() >= size_of::() { 180 | // SAFETY: all bit patterns are valid for u64 181 | let cmp = optimizer_hide(unsafe { cmp_step::(&mut a, &mut b) } as Word); 182 | tmp = optimizer_hide(tmp | cmp); 183 | } 184 | if a.len() >= size_of::() { 185 | // SAFETY: all bit patterns are valid for u32 186 | let cmp = optimizer_hide(unsafe { cmp_step::(&mut a, &mut b) } as Word); 187 | tmp = optimizer_hide(tmp | cmp); 188 | } 189 | if a.len() >= size_of::() { 190 | // SAFETY: all bit patterns are valid for u16 191 | let cmp = optimizer_hide(unsafe { cmp_step::(&mut a, &mut b) } as Word); 192 | tmp = optimizer_hide(tmp | cmp); 193 | } 194 | if a.len() >= size_of::() { 195 | // SAFETY: all bit patterns are valid for u8 196 | let cmp = optimizer_hide(unsafe { cmp_step::(&mut a, &mut b) } as Word); 197 | tmp = optimizer_hide(tmp | cmp); 198 | } 199 | 200 | tmp == 0 201 | } 202 | 203 | /// Compares two equal-sized byte strings in constant time. 204 | /// 205 | /// # Examples 206 | /// 207 | /// ``` 208 | /// use constant_time_eq::constant_time_eq; 209 | /// 210 | /// assert!(constant_time_eq(b"foo", b"foo")); 211 | /// assert!(!constant_time_eq(b"foo", b"bar")); 212 | /// assert!(!constant_time_eq(b"bar", b"baz")); 213 | /// # assert!(constant_time_eq(b"", b"")); 214 | /// 215 | /// // Not equal-sized, so won't take constant time. 216 | /// assert!(!constant_time_eq(b"foo", b"")); 217 | /// assert!(!constant_time_eq(b"foo", b"quux")); 218 | /// ``` 219 | #[must_use] 220 | pub fn constant_time_eq(a: &[u8], b: &[u8]) -> bool { 221 | with_dit(|| constant_time_eq_impl(a, b, 0)) 222 | } 223 | 224 | /// Compares two fixed-size byte strings in constant time. 225 | /// 226 | /// # Examples 227 | /// 228 | /// ``` 229 | /// use constant_time_eq::constant_time_eq_n; 230 | /// 231 | /// assert!(constant_time_eq_n(&[3; 20], &[3; 20])); 232 | /// assert!(!constant_time_eq_n(&[3; 20], &[7; 20])); 233 | /// ``` 234 | #[must_use] 235 | pub fn constant_time_eq_n(a: &[u8; N], b: &[u8; N]) -> bool { 236 | with_dit(|| constant_time_eq_impl(&a[..], &b[..], 0)) 237 | } 238 | 239 | #[cfg(test)] 240 | mod tests { 241 | #[cfg(feature = "count_instructions_test")] 242 | extern crate std; 243 | 244 | #[cfg(feature = "count_instructions_test")] 245 | #[test] 246 | fn count_optimizer_hide_instructions() -> std::io::Result<()> { 247 | use super::{Word, optimizer_hide}; 248 | use count_instructions::count_instructions; 249 | 250 | fn count() -> std::io::Result { 251 | // If optimizer_hide does not work, constant propagation and folding 252 | // will make this identical to count_optimized() below. 253 | let mut count = 0; 254 | assert_eq!( 255 | 10 as Word, 256 | count_instructions( 257 | || optimizer_hide(1) 258 | + optimizer_hide(2) 259 | + optimizer_hide(3) 260 | + optimizer_hide(4), 261 | |_| count += 1 262 | )? 263 | ); 264 | Ok(count) 265 | } 266 | 267 | fn count_optimized() -> std::io::Result { 268 | #[inline(always)] 269 | fn inline_identity(value: Word) -> Word { 270 | value 271 | } 272 | 273 | let mut count = 0; 274 | assert_eq!( 275 | 10 as Word, 276 | count_instructions( 277 | || inline_identity(1) 278 | + inline_identity(2) 279 | + inline_identity(3) 280 | + inline_identity(4), 281 | |_| count += 1 282 | )? 283 | ); 284 | Ok(count) 285 | } 286 | 287 | assert!(count()? > count_optimized()?); 288 | Ok(()) 289 | } 290 | } 291 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | //! Compares two equal-sized byte strings in constant time. 2 | //! 3 | //! The time of the comparison does not depend on: 4 | //! 5 | //! * The contents of the inputs; 6 | //! * The position of the difference(s) between the inputs. 7 | //! 8 | //! The time of the comparison can depend on: 9 | //! 10 | //! * The memory addresses of the inputs; 11 | //! * The length of the inputs. 12 | 13 | #![cfg_attr(not(feature = "std"), no_std)] 14 | #![deny(clippy::undocumented_unsafe_blocks)] 15 | 16 | #[doc(hidden)] 17 | pub mod classic; 18 | 19 | #[doc(hidden)] 20 | pub mod generic; 21 | 22 | #[cfg(all( 23 | any(target_arch = "x86", target_arch = "x86_64"), 24 | target_feature = "sse2", 25 | not(miri) 26 | ))] 27 | mod sse2; 28 | 29 | #[cfg(all( 30 | any(target_arch = "x86", target_arch = "x86_64"), 31 | target_feature = "sse2", 32 | not(miri) 33 | ))] 34 | use sse2 as simd; 35 | 36 | #[cfg(all(target_arch = "aarch64", target_feature = "neon", not(miri)))] 37 | mod neon; 38 | 39 | #[cfg(all(target_arch = "aarch64", target_feature = "neon", not(miri)))] 40 | use neon as simd; 41 | 42 | #[cfg(not(any( 43 | all( 44 | any(target_arch = "x86", target_arch = "x86_64"), 45 | target_feature = "sse2", 46 | not(miri) 47 | ), 48 | all(target_arch = "aarch64", target_feature = "neon", not(miri)) 49 | )))] 50 | use generic as simd; 51 | 52 | #[cfg(all(target_arch = "aarch64", not(miri)))] 53 | #[doc(hidden)] 54 | pub mod dit; 55 | 56 | #[cfg(all(target_arch = "aarch64", not(miri)))] 57 | use dit::with_dit; 58 | 59 | /// Runs code with the hardware DIT feature or equivalent enabled when possible. 60 | #[cfg(any(not(target_arch = "aarch64"), miri))] 61 | #[inline(always)] 62 | pub(crate) fn with_dit(f: F) -> T 63 | where 64 | F: FnOnce() -> T, 65 | { 66 | f() 67 | } 68 | 69 | /// Compares two equal-sized byte strings in constant time. 70 | /// 71 | /// # Examples 72 | /// 73 | /// ``` 74 | /// use constant_time_eq::constant_time_eq; 75 | /// 76 | /// assert!(constant_time_eq(b"foo", b"foo")); 77 | /// assert!(!constant_time_eq(b"foo", b"bar")); 78 | /// assert!(!constant_time_eq(b"bar", b"baz")); 79 | /// # assert!(constant_time_eq(b"", b"")); 80 | /// 81 | /// // Not equal-sized, so won't take constant time. 82 | /// assert!(!constant_time_eq(b"foo", b"")); 83 | /// assert!(!constant_time_eq(b"foo", b"quux")); 84 | /// ``` 85 | #[must_use] 86 | pub fn constant_time_eq(a: &[u8], b: &[u8]) -> bool { 87 | simd::constant_time_eq(a, b) 88 | } 89 | 90 | // Fixed-size array variant. 91 | 92 | /// Compares two fixed-size byte strings in constant time. 93 | /// 94 | /// # Examples 95 | /// 96 | /// ``` 97 | /// use constant_time_eq::constant_time_eq_n; 98 | /// 99 | /// assert!(constant_time_eq_n(&[3; 20], &[3; 20])); 100 | /// assert!(!constant_time_eq_n(&[3; 20], &[7; 20])); 101 | /// ``` 102 | #[must_use] 103 | pub fn constant_time_eq_n(a: &[u8; N], b: &[u8; N]) -> bool { 104 | simd::constant_time_eq_n(a, b) 105 | } 106 | 107 | // Fixed-size variants for the most common sizes. 108 | 109 | /// Compares two 128-bit byte strings in constant time. 110 | /// 111 | /// # Examples 112 | /// 113 | /// ``` 114 | /// use constant_time_eq::constant_time_eq_16; 115 | /// 116 | /// assert!(constant_time_eq_16(&[3; 16], &[3; 16])); 117 | /// assert!(!constant_time_eq_16(&[3; 16], &[7; 16])); 118 | /// ``` 119 | #[inline] 120 | #[must_use] 121 | pub fn constant_time_eq_16(a: &[u8; 16], b: &[u8; 16]) -> bool { 122 | constant_time_eq_n(a, b) 123 | } 124 | 125 | /// Compares two 256-bit byte strings in constant time. 126 | /// 127 | /// # Examples 128 | /// 129 | /// ``` 130 | /// use constant_time_eq::constant_time_eq_32; 131 | /// 132 | /// assert!(constant_time_eq_32(&[3; 32], &[3; 32])); 133 | /// assert!(!constant_time_eq_32(&[3; 32], &[7; 32])); 134 | /// ``` 135 | #[inline] 136 | #[must_use] 137 | pub fn constant_time_eq_32(a: &[u8; 32], b: &[u8; 32]) -> bool { 138 | constant_time_eq_n(a, b) 139 | } 140 | 141 | /// Compares two 512-bit byte strings in constant time. 142 | /// 143 | /// # Examples 144 | /// 145 | /// ``` 146 | /// use constant_time_eq::constant_time_eq_64; 147 | /// 148 | /// assert!(constant_time_eq_64(&[3; 64], &[3; 64])); 149 | /// assert!(!constant_time_eq_64(&[3; 64], &[7; 64])); 150 | /// ``` 151 | #[inline] 152 | #[must_use] 153 | pub fn constant_time_eq_64(a: &[u8; 64], b: &[u8; 64]) -> bool { 154 | constant_time_eq_n(a, b) 155 | } 156 | -------------------------------------------------------------------------------- /src/neon.rs: -------------------------------------------------------------------------------- 1 | //! NEON implementation of `constant_time_eq` and `constant_time_eq_n`. 2 | 3 | use core::arch::asm; 4 | use core::mem::size_of; 5 | 6 | #[cfg(target_arch = "aarch64")] 7 | use core::arch::aarch64::*; 8 | 9 | use crate::with_dit; 10 | 11 | /// Equivalent to `vceqq_u8`, but hidden from the compiler. 12 | /// 13 | /// The use of inline assembly instead of an intrinsic prevents a sufficiently 14 | /// smart compiler from computing the mask in other ways which might not be 15 | /// constant time (for instance, looping through the input and using branching 16 | /// to set the vector elements). 17 | #[must_use] 18 | #[inline(always)] 19 | fn vceqq_u8_hide(a: uint8x16_t, b: uint8x16_t) -> uint8x16_t { 20 | let mut c; 21 | // SAFETY: this file is compiled only when NEON is available 22 | // SAFETY: assembly instruction touches only these registers 23 | #[cfg(target_arch = "aarch64")] 24 | unsafe { 25 | asm!("cmeq {c:v}.16b, {a:v}.16b, {b:v}.16b", 26 | c = lateout(vreg) c, 27 | a = in(vreg) a, 28 | b = in(vreg) b, 29 | options(pure, nomem, preserves_flags, nostack)); 30 | } 31 | c 32 | } 33 | 34 | /// Equivalent to `vandq_u8`, but hidden from the compiler. 35 | /// 36 | /// The use of inline assembly instead of an intrinsic prevents a sufficiently 37 | /// smart compiler from short circuiting the computation once the mask becomes 38 | /// all zeros. 39 | #[must_use] 40 | #[inline(always)] 41 | fn vandq_u8_hide(a: uint8x16_t, b: uint8x16_t) -> uint8x16_t { 42 | let mut c; 43 | // SAFETY: this file is compiled only when NEON is available 44 | // SAFETY: assembly instruction touches only these registers 45 | #[cfg(target_arch = "aarch64")] 46 | unsafe { 47 | asm!("and {c:v}.16b, {a:v}.16b, {b:v}.16b", 48 | c = lateout(vreg) c, 49 | a = in(vreg) a, 50 | b = in(vreg) b, 51 | options(pure, nomem, preserves_flags, nostack)); 52 | } 53 | c 54 | } 55 | 56 | /// Equivalent to `vshrn_n_u16(..., 4)`, but hidden from the compiler. 57 | /// 58 | /// The use of inline assembly instead of an intrinsic prevents a sufficiently 59 | /// smart compiler from extracting the mask in other ways which might not be 60 | /// constant time (for instance, looping through the elements of the vector). 61 | #[must_use] 62 | #[inline(always)] 63 | fn vshrn_n_u16_4_hide(a: uint16x8_t) -> uint8x8_t { 64 | let mut mask; 65 | // SAFETY: this file is compiled only when NEON is available 66 | // SAFETY: assembly instruction touches only these registers 67 | #[cfg(target_arch = "aarch64")] 68 | unsafe { 69 | asm!("shrn {mask:v}.8b, {a:v}.8h, #{n}", 70 | mask = lateout(vreg) mask, 71 | a = in(vreg) a, 72 | n = const 4, 73 | options(pure, nomem, preserves_flags, nostack)); 74 | } 75 | mask 76 | } 77 | 78 | /// Moves a mask created by `vceqq_u8` to a `u64` register, with each all-zero or 79 | /// all-ones mask byte represented as an all-zero or all-ones half-byte. 80 | #[must_use] 81 | #[inline(always)] 82 | fn get_mask_u64(mask: uint8x16_t) -> u64 { 83 | // SAFETY: this file is compiled only when NEON is available 84 | unsafe { 85 | let mask = vshrn_n_u16_4_hide(vreinterpretq_u16_u8(mask)); 86 | vget_lane_u64(vreinterpret_u64_u8(mask), 0) 87 | } 88 | } 89 | 90 | /// Safe equivalent to `vld1q_u8` for byte slices. 91 | #[must_use] 92 | #[inline(always)] 93 | fn vld1q_u8_safe(src: &[u8]) -> uint8x16_t { 94 | assert_eq!(src.len(), size_of::()); 95 | 96 | // SAFETY: this file is compiled only when NEON is available 97 | // SAFETY: the slice has enough bytes for a `uint8x16_t` 98 | unsafe { vld1q_u8(src.as_ptr()) } 99 | } 100 | 101 | /// Safe equivalent to `vld1q_u8_x2` for byte slices. 102 | #[must_use] 103 | #[inline(always)] 104 | fn vld1q_u8_x2_safe(src: &[u8]) -> uint8x16x2_t { 105 | assert_eq!(src.len(), size_of::()); 106 | 107 | // SAFETY: this file is compiled only when NEON is available 108 | // SAFETY: the slice has enough bytes for a `uint8x16x2_t` 109 | unsafe { vld1q_u8_x2(src.as_ptr()) } 110 | } 111 | 112 | /// NEON implementation of `constant_time_eq` and `constant_time_eq_n`. 113 | #[must_use] 114 | #[inline(always)] 115 | fn constant_time_eq_neon(mut a: &[u8], mut b: &[u8]) -> bool { 116 | if a.len() != b.len() { 117 | return false; 118 | } 119 | 120 | // This statement does nothing, because a.len() == b.len() here, 121 | // but it makes the optimizer elide some useless bounds checks. 122 | b = &b[..a.len()]; 123 | 124 | const LANES: usize = 16; 125 | 126 | let tmp = if a.len() >= LANES * 2 { 127 | let tmpa = vld1q_u8_x2_safe(&a[..LANES * 2]); 128 | let tmpb = vld1q_u8_x2_safe(&b[..LANES * 2]); 129 | 130 | a = &a[LANES * 2..]; 131 | b = &b[LANES * 2..]; 132 | 133 | let mut mask0 = vceqq_u8_hide(tmpa.0, tmpb.0); 134 | let mut mask1 = vceqq_u8_hide(tmpa.1, tmpb.1); 135 | 136 | while a.len() >= LANES * 2 { 137 | let tmpa = vld1q_u8_x2_safe(&a[..LANES * 2]); 138 | let tmpb = vld1q_u8_x2_safe(&b[..LANES * 2]); 139 | 140 | a = &a[LANES * 2..]; 141 | b = &b[LANES * 2..]; 142 | 143 | let tmp0 = vceqq_u8_hide(tmpa.0, tmpb.0); 144 | let tmp1 = vceqq_u8_hide(tmpa.1, tmpb.1); 145 | 146 | mask0 = vandq_u8_hide(mask0, tmp0); 147 | mask1 = vandq_u8_hide(mask1, tmp1); 148 | } 149 | 150 | if a.len() >= LANES { 151 | let tmpa = vld1q_u8_safe(&a[..LANES]); 152 | let tmpb = vld1q_u8_safe(&b[..LANES]); 153 | 154 | a = &a[LANES..]; 155 | b = &b[LANES..]; 156 | 157 | let tmp = vceqq_u8_hide(tmpa, tmpb); 158 | 159 | mask0 = vandq_u8_hide(mask0, tmp); 160 | } 161 | 162 | let mask = vandq_u8_hide(mask0, mask1); 163 | get_mask_u64(mask) ^ !0 164 | } else if a.len() >= LANES { 165 | let tmpa = vld1q_u8_safe(&a[..LANES]); 166 | let tmpb = vld1q_u8_safe(&b[..LANES]); 167 | 168 | a = &a[LANES..]; 169 | b = &b[LANES..]; 170 | 171 | let mask = vceqq_u8_hide(tmpa, tmpb); 172 | 173 | get_mask_u64(mask) ^ !0 174 | } else { 175 | 0 176 | }; 177 | 178 | // Note: be careful to not short-circuit ("tmp == 0 &&") the comparison here 179 | crate::generic::constant_time_eq_impl(a, b, tmp) 180 | } 181 | 182 | #[must_use] 183 | pub fn constant_time_eq(a: &[u8], b: &[u8]) -> bool { 184 | with_dit(|| constant_time_eq_neon(a, b)) 185 | } 186 | 187 | #[must_use] 188 | pub fn constant_time_eq_n(a: &[u8; N], b: &[u8; N]) -> bool { 189 | with_dit(|| constant_time_eq_neon(&a[..], &b[..])) 190 | } 191 | -------------------------------------------------------------------------------- /src/sse2.rs: -------------------------------------------------------------------------------- 1 | //! SSE2/AVX implementation of `constant_time_eq` and `constant_time_eq_n`. 2 | //! 3 | //! Note: some microarchitectures split vector operations and/or vector registers larger than 4 | //! 128-bit, and might have optimizations for when one of the halves is all-zeros. To protect 5 | //! against that, only 128-bit vectors are used, even though larger vectors might be faster. 6 | 7 | use core::arch::asm; 8 | use core::mem::size_of; 9 | 10 | #[cfg(target_arch = "x86")] 11 | use core::arch::x86::*; 12 | 13 | #[cfg(target_arch = "x86_64")] 14 | use core::arch::x86_64::*; 15 | 16 | use crate::with_dit; 17 | 18 | /// Equivalent to `_mm_cmpeq_epi8`, but hidden from the compiler. 19 | /// 20 | /// The use of inline assembly instead of an intrinsic prevents a sufficiently 21 | /// smart compiler from computing the mask in other ways which might not be 22 | /// constant time (for instance, looping through the input and using branching 23 | /// to set the vector elements). 24 | #[must_use] 25 | #[inline(always)] 26 | fn cmpeq_epi8(a: __m128i, b: __m128i) -> __m128i { 27 | let mut c; 28 | // When AVX is available, the compiler will use the VEX prefix for all 29 | // SIMD instructions; do the same for this inline assembly. 30 | if cfg!(target_feature = "avx") { 31 | // SAFETY: used only when AVX is available 32 | // SAFETY: assembly instruction touches only these registers 33 | unsafe { 34 | asm!("vpcmpeqb {c}, {a}, {b}", 35 | c = lateout(xmm_reg) c, 36 | a = in(xmm_reg) a, 37 | b = in(xmm_reg) b, 38 | options(pure, nomem, preserves_flags, nostack)); 39 | } 40 | } else { 41 | // SAFETY: this file is compiled only when SSE2 is available 42 | // SAFETY: assembly instruction touches only these registers 43 | unsafe { 44 | asm!("pcmpeqb {a}, {b}", 45 | a = inlateout(xmm_reg) a => c, 46 | b = in(xmm_reg) b, 47 | options(pure, nomem, preserves_flags, nostack)); 48 | } 49 | } 50 | c 51 | } 52 | 53 | /// Equivalent to `_mm_and_si128`, but hidden from the compiler. 54 | /// 55 | /// The use of inline assembly instead of an intrinsic prevents a sufficiently 56 | /// smart compiler from short circuiting the computation once the mask becomes 57 | /// all zeros. 58 | #[must_use] 59 | #[inline(always)] 60 | fn and_si128(a: __m128i, b: __m128i) -> __m128i { 61 | let mut c; 62 | // When AVX is available, the compiler will use the VEX prefix for all 63 | // SIMD instructions; do the same for this inline assembly. 64 | if cfg!(target_feature = "avx") { 65 | // SAFETY: used only when AVX is available 66 | // SAFETY: assembly instruction touches only these registers 67 | unsafe { 68 | asm!("vpand {c}, {a}, {b}", 69 | c = lateout(xmm_reg) c, 70 | a = in(xmm_reg) a, 71 | b = in(xmm_reg) b, 72 | options(pure, nomem, preserves_flags, nostack)); 73 | } 74 | } else { 75 | // SAFETY: this file is compiled only when SSE2 is available 76 | // SAFETY: assembly instruction touches only these registers 77 | unsafe { 78 | asm!("pand {a}, {b}", 79 | a = inlateout(xmm_reg) a => c, 80 | b = in(xmm_reg) b, 81 | options(pure, nomem, preserves_flags, nostack)); 82 | } 83 | } 84 | c 85 | } 86 | 87 | /// Equivalent to `_mm_movemask_epi8`, but hidden from the compiler. 88 | /// 89 | /// The use of inline assembly instead of an intrinsic prevents a sufficiently 90 | /// smart compiler from extracting the mask in other ways which might not be 91 | /// constant time (for instance, looping through the elements of the vector). 92 | #[must_use] 93 | #[inline(always)] 94 | fn movemask_epi8(a: __m128i) -> u32 { 95 | let mut mask; 96 | // When AVX is available, the compiler will use the VEX prefix for all 97 | // SIMD instructions; do the same for this inline assembly. 98 | if cfg!(target_feature = "avx") { 99 | // SAFETY: used only when AVX is available 100 | // SAFETY: assembly instruction touches only these registers 101 | // SAFETY: 32-bit operations zero-extend the 64-bit register 102 | unsafe { 103 | asm!("vpmovmskb {mask:e}, {a}", 104 | mask = lateout(reg) mask, 105 | a = in(xmm_reg) a, 106 | options(pure, nomem, preserves_flags, nostack)); 107 | } 108 | } else { 109 | // SAFETY: this file is compiled only when SSE2 is available 110 | // SAFETY: assembly instruction touches only these registers 111 | // SAFETY: 32-bit operations zero-extend the 64-bit register 112 | unsafe { 113 | asm!("pmovmskb {mask:e}, {a}", 114 | mask = lateout(reg) mask, 115 | a = in(xmm_reg) a, 116 | options(pure, nomem, preserves_flags, nostack)); 117 | } 118 | } 119 | // The return type is u32 instead of i32 to avoid a sign extension. 120 | mask 121 | } 122 | 123 | /// Safe equivalent to `_mm_loadu_si128` for byte slices. 124 | #[must_use] 125 | #[inline(always)] 126 | fn loadu_si128(src: &[u8]) -> __m128i { 127 | assert_eq!(src.len(), size_of::<__m128i>()); 128 | 129 | // SAFETY: this file is compiled only when SSE2 is available 130 | // SAFETY: the slice has enough bytes for a __m128i 131 | unsafe { _mm_loadu_si128(src.as_ptr().cast::<__m128i>()) } 132 | } 133 | 134 | /// SSE2/AVX implementation of `constant_time_eq` and `constant_time_eq_n`. 135 | #[must_use] 136 | #[inline(always)] 137 | fn constant_time_eq_sse2(mut a: &[u8], mut b: &[u8]) -> bool { 138 | if a.len() != b.len() { 139 | return false; 140 | } 141 | 142 | // This statement does nothing, because a.len() == b.len() here, 143 | // but it makes the optimizer elide some useless bounds checks. 144 | b = &b[..a.len()]; 145 | 146 | const LANES: usize = size_of::<__m128i>(); 147 | 148 | let tmp = if a.len() >= LANES * 2 { 149 | let tmpa0 = loadu_si128(&a[..LANES]); 150 | let tmpb0 = loadu_si128(&b[..LANES]); 151 | let tmpa1 = loadu_si128(&a[LANES..LANES * 2]); 152 | let tmpb1 = loadu_si128(&b[LANES..LANES * 2]); 153 | 154 | a = &a[LANES * 2..]; 155 | b = &b[LANES * 2..]; 156 | 157 | let mut mask0 = cmpeq_epi8(tmpa0, tmpb0); 158 | let mut mask1 = cmpeq_epi8(tmpa1, tmpb1); 159 | 160 | while a.len() >= LANES * 2 { 161 | let tmpa0 = loadu_si128(&a[..LANES]); 162 | let tmpb0 = loadu_si128(&b[..LANES]); 163 | let tmpa1 = loadu_si128(&a[LANES..LANES * 2]); 164 | let tmpb1 = loadu_si128(&b[LANES..LANES * 2]); 165 | 166 | a = &a[LANES * 2..]; 167 | b = &b[LANES * 2..]; 168 | 169 | let tmp0 = cmpeq_epi8(tmpa0, tmpb0); 170 | let tmp1 = cmpeq_epi8(tmpa1, tmpb1); 171 | 172 | mask0 = and_si128(mask0, tmp0); 173 | mask1 = and_si128(mask1, tmp1); 174 | } 175 | 176 | if a.len() >= LANES { 177 | let tmpa = loadu_si128(&a[..LANES]); 178 | let tmpb = loadu_si128(&b[..LANES]); 179 | 180 | a = &a[LANES..]; 181 | b = &b[LANES..]; 182 | 183 | let tmp = cmpeq_epi8(tmpa, tmpb); 184 | 185 | mask0 = and_si128(mask0, tmp); 186 | } 187 | 188 | let mask = and_si128(mask0, mask1); 189 | movemask_epi8(mask) ^ 0xFFFF 190 | } else if a.len() >= LANES { 191 | let tmpa = loadu_si128(&a[..LANES]); 192 | let tmpb = loadu_si128(&b[..LANES]); 193 | 194 | a = &a[LANES..]; 195 | b = &b[LANES..]; 196 | 197 | let mask = cmpeq_epi8(tmpa, tmpb); 198 | 199 | movemask_epi8(mask) ^ 0xFFFF 200 | } else { 201 | 0 202 | }; 203 | 204 | // Note: be careful to not short-circuit ("tmp == 0 &&") the comparison here 205 | crate::generic::constant_time_eq_impl(a, b, tmp.into()) 206 | } 207 | 208 | #[must_use] 209 | pub fn constant_time_eq(a: &[u8], b: &[u8]) -> bool { 210 | with_dit(|| constant_time_eq_sse2(a, b)) 211 | } 212 | 213 | #[must_use] 214 | pub fn constant_time_eq_n(a: &[u8; N], b: &[u8; N]) -> bool { 215 | with_dit(|| constant_time_eq_sse2(&a[..], &b[..])) 216 | } 217 | -------------------------------------------------------------------------------- /tests/count_instructions.rs: -------------------------------------------------------------------------------- 1 | #[cfg(feature = "count_instructions_test")] 2 | mod tests { 3 | use std::io::Result; 4 | 5 | use constant_time_eq::{constant_time_eq, constant_time_eq_n}; 6 | use count_instructions::{Address, count_instructions}; 7 | 8 | fn detect_features() { 9 | // The first time with_dit() is called, the feature detection will run. 10 | // That will lead to a difference in the number of instructions, which 11 | // is unrelated to the data being compared. 12 | // Run a dummy comparison at least once before each test to avoid this. 13 | let _ = constant_time_eq(b"", b""); 14 | } 15 | 16 | #[inline(never)] 17 | fn count(l: &[u8], r: &[u8], capacity: usize) -> Result> { 18 | let mut addresses = Vec::with_capacity(capacity); 19 | assert!(!count_instructions( 20 | || constant_time_eq(l, r), 21 | |instruction| addresses.push(instruction.address()) 22 | )?); 23 | Ok(addresses) 24 | } 25 | 26 | #[inline(never)] 27 | fn count_n(l: &[u8; N], r: &[u8; N], capacity: usize) -> Result> { 28 | let mut addresses = Vec::with_capacity(capacity); 29 | assert!(!count_instructions( 30 | || constant_time_eq_n(l, r), 31 | |instruction| addresses.push(instruction.address()) 32 | )?); 33 | Ok(addresses) 34 | } 35 | 36 | fn test(a: u8, b: u8) -> Result<()> { 37 | detect_features(); 38 | 39 | const N: usize = 64; 40 | let l = vec![a; N]; 41 | let r = vec![b; N]; 42 | let baseline = count(&l, &r, 0)?; 43 | 44 | let mut t = r.clone(); 45 | for n in 0..(N - 1) { 46 | t[n] = a; 47 | assert_eq!(count(&l, &t, baseline.len())?, baseline); 48 | } 49 | 50 | t[N - 1] = a; 51 | assert!(constant_time_eq(&l, &t)); 52 | 53 | let mut t = r.clone(); 54 | for n in 1..N { 55 | t[N - n] = a; 56 | assert_eq!(count(&l, &t, baseline.len())?, baseline); 57 | } 58 | 59 | t[0] = a; 60 | assert!(constant_time_eq(&l, &t)); 61 | 62 | Ok(()) 63 | } 64 | 65 | fn test_n(a: u8, b: u8) -> Result<()> { 66 | detect_features(); 67 | 68 | let l = [a; N]; 69 | let r = [b; N]; 70 | let baseline = count_n(&l, &r, 0)?; 71 | 72 | let mut t = r.clone(); 73 | for n in 0..(N - 1) { 74 | t[n] = a; 75 | assert_eq!(count_n(&l, &t, baseline.len())?, baseline); 76 | } 77 | 78 | t[N - 1] = a; 79 | assert!(constant_time_eq_n(&l, &t)); 80 | 81 | let mut t = r.clone(); 82 | for n in 1..N { 83 | t[N - n] = a; 84 | assert_eq!(count_n(&l, &t, baseline.len())?, baseline); 85 | } 86 | 87 | t[0] = a; 88 | assert!(constant_time_eq_n(&l, &t)); 89 | 90 | Ok(()) 91 | } 92 | 93 | #[test] 94 | fn count_instructions_test() -> Result<()> { 95 | test(b'A', b'B')?; 96 | test(0x55, 0xAA)?; 97 | Ok(()) 98 | } 99 | 100 | fn count_instructions_test_n() -> Result<()> { 101 | test_n::(b'A', b'B')?; 102 | test_n::(0x55, 0xAA)?; 103 | Ok(()) 104 | } 105 | 106 | #[test] 107 | fn count_instructions_test_n_16() -> Result<()> { 108 | count_instructions_test_n::<16>() 109 | } 110 | 111 | #[test] 112 | fn count_instructions_test_n_20() -> Result<()> { 113 | count_instructions_test_n::<20>() 114 | } 115 | 116 | #[test] 117 | fn count_instructions_test_n_24() -> Result<()> { 118 | count_instructions_test_n::<24>() 119 | } 120 | 121 | #[test] 122 | fn count_instructions_test_n_32() -> Result<()> { 123 | count_instructions_test_n::<32>() 124 | } 125 | 126 | #[test] 127 | fn count_instructions_test_n_48() -> Result<()> { 128 | count_instructions_test_n::<48>() 129 | } 130 | 131 | #[test] 132 | fn count_instructions_test_n_64() -> Result<()> { 133 | count_instructions_test_n::<64>() 134 | } 135 | 136 | // This silly test shows that count_instructions() can detect early returns. 137 | #[test] 138 | fn count_instructions_test_variable() -> Result<()> { 139 | #[inline(never)] 140 | fn variable_time_eq(a: &[u8], b: &[u8]) -> bool { 141 | if a.len() != b.len() { 142 | false 143 | } else { 144 | for i in 0..a.len() { 145 | if a[i] != b[i] { 146 | return false; 147 | } 148 | } 149 | true 150 | } 151 | } 152 | 153 | #[inline(never)] 154 | fn count_variable(l: &[u8], r: &[u8], capacity: usize) -> Result> { 155 | let mut addresses = Vec::with_capacity(capacity); 156 | assert!(!count_instructions( 157 | || variable_time_eq(l, r), 158 | |instruction| addresses.push(instruction.address()) 159 | )?); 160 | Ok(addresses) 161 | } 162 | 163 | const N: usize = 64; 164 | let l = vec![b'A'; N]; 165 | let r = vec![b'B'; N]; 166 | 167 | let mut t = r.clone(); 168 | t[0] = b'A'; 169 | let short = count_variable(&l, &t, 0)?; 170 | 171 | let mut t = l.clone(); 172 | t[N - 1] = b'B'; 173 | let long = count_variable(&l, &t, short.len())?; 174 | 175 | assert_ne!(short, long); 176 | Ok(()) 177 | } 178 | } 179 | -------------------------------------------------------------------------------- /tests/count_instructions_classic.rs: -------------------------------------------------------------------------------- 1 | #[cfg(feature = "count_instructions_test")] 2 | mod tests { 3 | use std::io::Result; 4 | 5 | use constant_time_eq::classic::{constant_time_eq, constant_time_eq_n}; 6 | use count_instructions::{Address, count_instructions}; 7 | 8 | fn detect_features() { 9 | // The first time with_dit() is called, the feature detection will run. 10 | // That will lead to a difference in the number of instructions, which 11 | // is unrelated to the data being compared. 12 | // Run a dummy comparison at least once before each test to avoid this. 13 | let _ = constant_time_eq(b"", b""); 14 | } 15 | 16 | #[inline(never)] 17 | fn count(l: &[u8], r: &[u8], capacity: usize) -> Result> { 18 | let mut addresses = Vec::with_capacity(capacity); 19 | assert!(!count_instructions( 20 | || constant_time_eq(l, r), 21 | |instruction| addresses.push(instruction.address()) 22 | )?); 23 | Ok(addresses) 24 | } 25 | 26 | #[inline(never)] 27 | fn count_n(l: &[u8; N], r: &[u8; N], capacity: usize) -> Result> { 28 | let mut addresses = Vec::with_capacity(capacity); 29 | assert!(!count_instructions( 30 | || constant_time_eq_n(l, r), 31 | |instruction| addresses.push(instruction.address()) 32 | )?); 33 | Ok(addresses) 34 | } 35 | 36 | fn test(a: u8, b: u8) -> Result<()> { 37 | detect_features(); 38 | 39 | const N: usize = 64; 40 | let l = vec![a; N]; 41 | let r = vec![b; N]; 42 | let baseline = count(&l, &r, 0)?; 43 | 44 | let mut t = r.clone(); 45 | for n in 0..(N - 1) { 46 | t[n] = a; 47 | assert_eq!(count(&l, &t, baseline.len())?, baseline); 48 | } 49 | 50 | t[N - 1] = a; 51 | assert!(constant_time_eq(&l, &t)); 52 | 53 | let mut t = r.clone(); 54 | for n in 1..N { 55 | t[N - n] = a; 56 | assert_eq!(count(&l, &t, baseline.len())?, baseline); 57 | } 58 | 59 | t[0] = a; 60 | assert!(constant_time_eq(&l, &t)); 61 | 62 | Ok(()) 63 | } 64 | 65 | fn test_n(a: u8, b: u8) -> Result<()> { 66 | detect_features(); 67 | 68 | let l = [a; N]; 69 | let r = [b; N]; 70 | let baseline = count_n(&l, &r, 0)?; 71 | 72 | let mut t = r.clone(); 73 | for n in 0..(N - 1) { 74 | t[n] = a; 75 | assert_eq!(count_n(&l, &t, baseline.len())?, baseline); 76 | } 77 | 78 | t[N - 1] = a; 79 | assert!(constant_time_eq_n(&l, &t)); 80 | 81 | let mut t = r.clone(); 82 | for n in 1..N { 83 | t[N - n] = a; 84 | assert_eq!(count_n(&l, &t, baseline.len())?, baseline); 85 | } 86 | 87 | t[0] = a; 88 | assert!(constant_time_eq_n(&l, &t)); 89 | 90 | Ok(()) 91 | } 92 | 93 | #[test] 94 | fn count_instructions_test() -> Result<()> { 95 | test(b'A', b'B')?; 96 | test(0x55, 0xAA)?; 97 | Ok(()) 98 | } 99 | 100 | fn count_instructions_test_n() -> Result<()> { 101 | test_n::(b'A', b'B')?; 102 | test_n::(0x55, 0xAA)?; 103 | Ok(()) 104 | } 105 | 106 | #[test] 107 | fn count_instructions_test_n_16() -> Result<()> { 108 | count_instructions_test_n::<16>() 109 | } 110 | 111 | #[test] 112 | fn count_instructions_test_n_20() -> Result<()> { 113 | count_instructions_test_n::<20>() 114 | } 115 | 116 | #[test] 117 | fn count_instructions_test_n_24() -> Result<()> { 118 | count_instructions_test_n::<24>() 119 | } 120 | 121 | #[test] 122 | fn count_instructions_test_n_32() -> Result<()> { 123 | count_instructions_test_n::<32>() 124 | } 125 | 126 | #[test] 127 | fn count_instructions_test_n_48() -> Result<()> { 128 | count_instructions_test_n::<48>() 129 | } 130 | 131 | #[test] 132 | fn count_instructions_test_n_64() -> Result<()> { 133 | count_instructions_test_n::<64>() 134 | } 135 | 136 | // This silly test shows that count_instructions() can detect early returns. 137 | #[test] 138 | fn count_instructions_test_variable() -> Result<()> { 139 | #[inline(never)] 140 | fn variable_time_eq(a: &[u8], b: &[u8]) -> bool { 141 | if a.len() != b.len() { 142 | false 143 | } else { 144 | for i in 0..a.len() { 145 | if a[i] != b[i] { 146 | return false; 147 | } 148 | } 149 | true 150 | } 151 | } 152 | 153 | #[inline(never)] 154 | fn count_variable(l: &[u8], r: &[u8], capacity: usize) -> Result> { 155 | let mut addresses = Vec::with_capacity(capacity); 156 | assert!(!count_instructions( 157 | || variable_time_eq(l, r), 158 | |instruction| addresses.push(instruction.address()) 159 | )?); 160 | Ok(addresses) 161 | } 162 | 163 | const N: usize = 64; 164 | let l = vec![b'A'; N]; 165 | let r = vec![b'B'; N]; 166 | 167 | let mut t = r.clone(); 168 | t[0] = b'A'; 169 | let short = count_variable(&l, &t, 0)?; 170 | 171 | let mut t = l.clone(); 172 | t[N - 1] = b'B'; 173 | let long = count_variable(&l, &t, short.len())?; 174 | 175 | assert_ne!(short, long); 176 | Ok(()) 177 | } 178 | } 179 | -------------------------------------------------------------------------------- /tests/count_instructions_generic.rs: -------------------------------------------------------------------------------- 1 | #[cfg(feature = "count_instructions_test")] 2 | mod tests { 3 | use std::io::Result; 4 | 5 | use constant_time_eq::generic::{constant_time_eq, constant_time_eq_n}; 6 | use count_instructions::{Address, count_instructions}; 7 | 8 | fn detect_features() { 9 | // The first time with_dit() is called, the feature detection will run. 10 | // That will lead to a difference in the number of instructions, which 11 | // is unrelated to the data being compared. 12 | // Run a dummy comparison at least once before each test to avoid this. 13 | let _ = constant_time_eq(b"", b""); 14 | } 15 | 16 | #[inline(never)] 17 | fn count(l: &[u8], r: &[u8], capacity: usize) -> Result> { 18 | let mut addresses = Vec::with_capacity(capacity); 19 | assert!(!count_instructions( 20 | || constant_time_eq(l, r), 21 | |instruction| addresses.push(instruction.address()) 22 | )?); 23 | Ok(addresses) 24 | } 25 | 26 | #[inline(never)] 27 | fn count_n(l: &[u8; N], r: &[u8; N], capacity: usize) -> Result> { 28 | let mut addresses = Vec::with_capacity(capacity); 29 | assert!(!count_instructions( 30 | || constant_time_eq_n(l, r), 31 | |instruction| addresses.push(instruction.address()) 32 | )?); 33 | Ok(addresses) 34 | } 35 | 36 | fn test(a: u8, b: u8) -> Result<()> { 37 | detect_features(); 38 | 39 | const N: usize = 64; 40 | let l = vec![a; N]; 41 | let r = vec![b; N]; 42 | let baseline = count(&l, &r, 0)?; 43 | 44 | let mut t = r.clone(); 45 | for n in 0..(N - 1) { 46 | t[n] = a; 47 | assert_eq!(count(&l, &t, baseline.len())?, baseline); 48 | } 49 | 50 | t[N - 1] = a; 51 | assert!(constant_time_eq(&l, &t)); 52 | 53 | let mut t = r.clone(); 54 | for n in 1..N { 55 | t[N - n] = a; 56 | assert_eq!(count(&l, &t, baseline.len())?, baseline); 57 | } 58 | 59 | t[0] = a; 60 | assert!(constant_time_eq(&l, &t)); 61 | 62 | Ok(()) 63 | } 64 | 65 | fn test_n(a: u8, b: u8) -> Result<()> { 66 | detect_features(); 67 | 68 | let l = [a; N]; 69 | let r = [b; N]; 70 | let baseline = count_n(&l, &r, 0)?; 71 | 72 | let mut t = r.clone(); 73 | for n in 0..(N - 1) { 74 | t[n] = a; 75 | assert_eq!(count_n(&l, &t, baseline.len())?, baseline); 76 | } 77 | 78 | t[N - 1] = a; 79 | assert!(constant_time_eq_n(&l, &t)); 80 | 81 | let mut t = r.clone(); 82 | for n in 1..N { 83 | t[N - n] = a; 84 | assert_eq!(count_n(&l, &t, baseline.len())?, baseline); 85 | } 86 | 87 | t[0] = a; 88 | assert!(constant_time_eq_n(&l, &t)); 89 | 90 | Ok(()) 91 | } 92 | 93 | #[test] 94 | fn count_instructions_test() -> Result<()> { 95 | test(b'A', b'B')?; 96 | test(0x55, 0xAA)?; 97 | Ok(()) 98 | } 99 | 100 | fn count_instructions_test_n() -> Result<()> { 101 | test_n::(b'A', b'B')?; 102 | test_n::(0x55, 0xAA)?; 103 | Ok(()) 104 | } 105 | 106 | #[test] 107 | fn count_instructions_test_n_16() -> Result<()> { 108 | count_instructions_test_n::<16>() 109 | } 110 | 111 | #[test] 112 | fn count_instructions_test_n_20() -> Result<()> { 113 | count_instructions_test_n::<20>() 114 | } 115 | 116 | #[test] 117 | fn count_instructions_test_n_24() -> Result<()> { 118 | count_instructions_test_n::<24>() 119 | } 120 | 121 | #[test] 122 | fn count_instructions_test_n_32() -> Result<()> { 123 | count_instructions_test_n::<32>() 124 | } 125 | 126 | #[test] 127 | fn count_instructions_test_n_48() -> Result<()> { 128 | count_instructions_test_n::<48>() 129 | } 130 | 131 | #[test] 132 | fn count_instructions_test_n_64() -> Result<()> { 133 | count_instructions_test_n::<64>() 134 | } 135 | 136 | // This silly test shows that count_instructions() can detect early returns. 137 | #[test] 138 | fn count_instructions_test_variable() -> Result<()> { 139 | #[inline(never)] 140 | fn variable_time_eq(a: &[u8], b: &[u8]) -> bool { 141 | if a.len() != b.len() { 142 | false 143 | } else { 144 | for i in 0..a.len() { 145 | if a[i] != b[i] { 146 | return false; 147 | } 148 | } 149 | true 150 | } 151 | } 152 | 153 | #[inline(never)] 154 | fn count_variable(l: &[u8], r: &[u8], capacity: usize) -> Result> { 155 | let mut addresses = Vec::with_capacity(capacity); 156 | assert!(!count_instructions( 157 | || variable_time_eq(l, r), 158 | |instruction| addresses.push(instruction.address()) 159 | )?); 160 | Ok(addresses) 161 | } 162 | 163 | const N: usize = 64; 164 | let l = vec![b'A'; N]; 165 | let r = vec![b'B'; N]; 166 | 167 | let mut t = r.clone(); 168 | t[0] = b'A'; 169 | let short = count_variable(&l, &t, 0)?; 170 | 171 | let mut t = l.clone(); 172 | t[N - 1] = b'B'; 173 | let long = count_variable(&l, &t, short.len())?; 174 | 175 | assert_ne!(short, long); 176 | Ok(()) 177 | } 178 | } 179 | -------------------------------------------------------------------------------- /tests/exhaustive.rs: -------------------------------------------------------------------------------- 1 | #![cfg(not(miri))] 2 | 3 | use core::mem::size_of_val; 4 | use core::slice::from_raw_parts_mut; 5 | 6 | /// Misaligns the slice by one byte, to ensure no SIMD load instructions require alignment. 7 | fn misalign_slice(buf: &mut [u128]) -> &mut [u8] { 8 | let ptr = buf.as_mut_ptr() as *mut u8; 9 | let len = size_of_val(buf); 10 | unsafe { from_raw_parts_mut(ptr.add(1), len - 1) } 11 | } 12 | 13 | /// Confirms that all bit positions are being used for comparison, for a given length. 14 | fn test_one_length(a: &mut [u8], b: &mut [u8], n: usize, cteq: &CTEQ) 15 | where 16 | CTEQ: Fn(&[u8], &[u8]) -> bool, 17 | { 18 | let a = &mut a[..n]; 19 | let b = &mut b[..n]; 20 | 21 | assert!(cteq(a, b)); 22 | for i in 0..n { 23 | for m in [0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80] { 24 | a[i] ^= m; 25 | assert!(!cteq(a, b), "len={} a[{}] mask 0x{:02x}", n, i, m); 26 | a[i] ^= m; 27 | 28 | b[i] ^= m; 29 | assert!(!cteq(a, b), "len={} b[{}] mask 0x{:02x}", n, i, m); 30 | b[i] ^= m; 31 | } 32 | } 33 | assert!(cteq(a, b)); 34 | } 35 | 36 | /// Confirms that all bit positions are being used for comparison, for all lengths up to 1024 bits. 37 | fn test_all_lengths(fill: F, cteq: &CTEQ) 38 | where 39 | CTEQ: Fn(&[u8], &[u8]) -> bool, 40 | { 41 | let mut a = [0u128; 9]; 42 | let mut b = [0u128; 9]; 43 | 44 | let a = misalign_slice(&mut a); 45 | let b = misalign_slice(&mut b); 46 | 47 | fill(a); 48 | b.copy_from_slice(a); 49 | 50 | // Note: this is quadratic; do not increase the maximum length too much. 51 | for n in 0..=128 { 52 | test_one_length(a, b, n, cteq); 53 | } 54 | } 55 | 56 | fn exhaustive_test_zeros(cteq: &CTEQ) 57 | where 58 | CTEQ: Fn(&[u8], &[u8]) -> bool, 59 | { 60 | test_all_lengths(|buf| buf.fill(0), cteq); 61 | } 62 | 63 | fn exhaustive_test_ones(cteq: &CTEQ) 64 | where 65 | CTEQ: Fn(&[u8], &[u8]) -> bool, 66 | { 67 | test_all_lengths(|buf| buf.fill(!0), cteq); 68 | } 69 | 70 | fn exhaustive_test_random(cteq: &CTEQ) 71 | where 72 | CTEQ: Fn(&[u8], &[u8]) -> bool, 73 | { 74 | // Simple xorshift PRNG, from https://www.jstatsoft.org/article/view/v008i14 75 | let mut state: u32 = 2463534242; 76 | let xorshift32 = || { 77 | state ^= state << 13; 78 | state ^= state >> 17; 79 | state ^= state << 5; 80 | state as u8 81 | }; 82 | 83 | test_all_lengths(|buf| buf.fill_with(xorshift32), cteq); 84 | } 85 | 86 | #[test] 87 | fn exhaustive_test_zeros_simd() { 88 | use constant_time_eq::constant_time_eq; 89 | exhaustive_test_zeros(&constant_time_eq); 90 | } 91 | 92 | #[test] 93 | fn exhaustive_test_ones_simd() { 94 | use constant_time_eq::constant_time_eq; 95 | exhaustive_test_ones(&constant_time_eq); 96 | } 97 | 98 | #[test] 99 | fn exhaustive_test_random_simd() { 100 | use constant_time_eq::constant_time_eq; 101 | exhaustive_test_random(&constant_time_eq); 102 | } 103 | 104 | #[test] 105 | fn exhaustive_test_zeros_generic() { 106 | use constant_time_eq::generic::constant_time_eq; 107 | exhaustive_test_zeros(&constant_time_eq); 108 | } 109 | 110 | #[test] 111 | fn exhaustive_test_ones_generic() { 112 | use constant_time_eq::generic::constant_time_eq; 113 | exhaustive_test_ones(&constant_time_eq); 114 | } 115 | 116 | #[test] 117 | fn exhaustive_test_random_generic() { 118 | use constant_time_eq::generic::constant_time_eq; 119 | exhaustive_test_random(&constant_time_eq); 120 | } 121 | --------------------------------------------------------------------------------