Vendor dependencies

This commit is contained in:
2026-08-01 16:11:49 +03:00
parent 7f139a0241
commit 6b5e7f0f8b
29706 changed files with 9575646 additions and 0 deletions
+1
View File
@@ -0,0 +1 @@
{"$comment":"This file only protects against accidental modifications. It is not a security mechanism and does not protect against malicious changes.","files":{".cargo_vcs_info.json":"f6520937d67a831a7347d7a9e7b08e303c85ec834fda9d8fc55e2fda81fb9d75","CHANGELOG.md":"25c1eb1c81cb523e41bf5f8e72c5b46f2d41503248e73e88af233df6daa58c47","Cargo.lock":"5396a7cd8857d2d4ff651dc626b447c2eb99fd4768ac005f0303ad5bf21926e1","Cargo.toml":"0d220dd1cd83df095dbc0bd7a6b738cc79f68c40d92a76f953f5a101a294844c","Cargo.toml.orig":"ecbabba1ab31ebb18ec635d14ce0dbd913c88fc5faa871510b1db65552aff15c","LICENSE-APACHE":"a9040321c3712d8fd0b09cf52b17445de04a23a10165049ae187cd39e5c86be5","LICENSE-MIT":"b8c6939380a400f53e11923d50fcc4dd2fa1ba8339fd9d04cda38a0251b6c9b0","README.md":"e5495c78bf5270bcca880e0f68c552d435dabd92764c9cc2f62b574b2d75588d","benches/mod.rs":"93ca43362e31d6707f131e8c4484cfb3aaa209058510b40ff7805601f57c68ab","src/backends.rs":"a5b51ab7b20fdb7d99c29b231bfbc8fc643f1002ad2155f4e6757be9cc990105","src/backends/avx2.rs":"c44c34d86ab9d896ac8b863e611f496cda31b5d731b1c860f70c0772426da918","src/backends/avx512.rs":"b4e945d887f0fb11e2dc6a757f7dc01e7553d486ae8d381964defaf8857d9ab6","src/backends/neon.rs":"5d29e3c2da18ea649f6e19fc339bb6253611db18cafcba4993155b70d096f102","src/backends/soft.rs":"120bcf4bc535c3fbcc3b4e2bdc0f021e44fae553bf5bb3a532271b2a08dc2149","src/backends/sse2.rs":"04c4ff27778e6123a0669b8975376c7aed705b783a17525db9552962896eae39","src/chacha.rs":"323b76530ad49db28cb1764dbee7843908af21ae85918e4ade04abad8a4614bb","src/legacy.rs":"f5e9dc27cc2c911d087f198bcdbbbb9a1f55c873c5bbd6e79280d1628f72ab49","src/lib.rs":"b539477230a58abfa2e2ab8dd62c017ffc84f3a7680971c5496973185c543c20","src/rng.rs":"8f1367fed752857f0c6ec9c7a7bd154417a80b780b342c9346af4f16b3089bde","src/variants.rs":"79bb423dd0ef0ac690334915baea48db17f055797175c22b1bcbed0714dd9f88","src/xchacha.rs":"86de92973ea8701003c97994fb95fe33099dc0fe0938c27d10f20e95d4b94000","tests/data/chacha20-legacy.blb":"2405a7f849b5db6ae8f6ba79dd943632f7ba3f9839a9c4db86a79a1f069e5e30","tests/data/chacha20.blb":"2686172dba8dd9a5f20d37881b3bf8ef68a9a5106cb8178628ca892e2a5d9052","tests/kats.rs":"ea16474f269f1c5edb5551ecfbfb40bc0fac06b3f293c2365b1442b5338c684f","tests/rng.rs":"eb59c35b32f11e72a6dc04ca790b5efc35331c8315b4039222847eb3c6abcfe9"},"package":"d524456ba66e72eb8b115ff89e01e497f8e6d11d78b70b1aa13c0fbd97540a81"}
+6
View File
@@ -0,0 +1,6 @@
{
"git": {
"sha1": "5f3430b7531a33aa14957b6bd407b46687635124"
},
"path_in_vcs": "chacha20"
}
+299
View File
@@ -0,0 +1,299 @@
# Changelog
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## 0.10.1 (2026-06-24)
### Added
- `ChaCha20LegacyCore` type and `Nonce` type alias ([#570])
[#570]: https://github.com/RustCrypto/stream-ciphers/pull/570
## 0.10.0 (2026-02-07)
### Added
- `rand_core` v0.10 support ([#333], [#513])
- 64-bit counter support ([#439])
- `{get,set}_block_pos` inherent methods ([#516])
- AVX-512 backend - requires `--cfg chacha20_avx512` to enable ([#477])
- Inherent methods for RNG state (de)serialization ([#541])
### Changed
- Bump `cipher` from `0.4` to `0.5` - replaces `generic-array` with `hybrid-array` ([#338], [#521])
- Edition changed to 2024 and MSRV bumped to 1.85 ([#397])
- Relax MSRV policy and allow MSRV bumps in patch releases
- Bump `cpufeatures` to v0.3 ([#530])
- `--cfg chacha20_backend="..."` replaces previous `chacha20_force*` ([#520])
- `chacha20_force_avx2` => `chacha20_backend="avx2"`
- `chacha20_force_avx512` => `chacha20_backend="avx512"`
- `chacha20_force_soft` => `chacha20_backend="soft"`
- `chacha20_force_sse2` => `chacha20_backend="sse2"`
### Removed
- `chacha20_force_neon` cfg attribute - now on-by-default for supported targets ([#361])
- `std` feature ([#397])
- `Clone` impls ([#462])
[#333]: https://github.com/RustCrypto/stream-ciphers/pull/333
[#338]: https://github.com/RustCrypto/stream-ciphers/pull/338
[#361]: https://github.com/RustCrypto/stream-ciphers/pull/361
[#397]: https://github.com/RustCrypto/stream-ciphers/pull/397
[#439]: https://github.com/RustCrypto/stream-ciphers/pull/439
[#462]: https://github.com/RustCrypto/stream-ciphers/pull/462
[#477]: https://github.com/RustCrypto/stream-ciphers/pull/477
[#513]: https://github.com/RustCrypto/stream-ciphers/pull/513
[#516]: https://github.com/RustCrypto/stream-ciphers/pull/516
[#520]: https://github.com/RustCrypto/stream-ciphers/pull/520
[#521]: https://github.com/RustCrypto/stream-ciphers/pull/521
[#530]: https://github.com/RustCrypto/stream-ciphers/pull/530
[#541]: https://github.com/RustCrypto/stream-ciphers/pull/541
## 0.9.1 (2023-04-01)
### Added
- NEON support via `chacha20_force_neon` cfg attribute ([#310], [#317])
[#310]: https://github.com/RustCrypto/stream-ciphers/pull/310
[#317]: https://github.com/RustCrypto/stream-ciphers/pull/317
## 0.9.0 (2022-02-21)
### Added
- `chacha20_force_soft`, `chacha20_force_sse2`, and `chacha20_force_avx2`
configuration flags ([#293])
### Changed
- Bump `cipher` dependency to v0.4 ([#276])
### Fixed
- Minimal versions build ([#290])
### Removed
- `neon`, `force-soft`, `expose-core`, `hchacha`, `legacy`, and `rng` features ([#276], [#293])
[#276]: https://github.com/RustCrypto/stream-ciphers/pull/276
[#290]: https://github.com/RustCrypto/stream-ciphers/pull/290
[#293]: https://github.com/RustCrypto/stream-ciphers/pull/293
## 0.8.2 (2022-07-07)
### Changed
- Unpin `zeroize` dependency ([#301])
[#301]: https://github.com/RustCrypto/stream-ciphers/pull/301
## 0.8.1 (2021-08-30)
### Added
- NEON implementation for aarch64 ([#274])
[#274]: https://github.com/RustCrypto/stream-ciphers/pull/274
## 0.8.0 (2021-08-29)
### Added
- SSE2 autodetection support ([#270])
### Changed
- AVX2 performance improvements ([#267], [#267])
- MSRV 1.51+ ([#267])
- Lock to `zeroize` <1.5 ([#269])
### Removed
- `xchacha` feature: all `XChaCha*` types are now available by-default ([#271])
[#267]: https://github.com/RustCrypto/stream-ciphers/pull/267
[#269]: https://github.com/RustCrypto/stream-ciphers/pull/269
[#270]: https://github.com/RustCrypto/stream-ciphers/pull/270
[#271]: https://github.com/RustCrypto/stream-ciphers/pull/271
## 0.7.3 (2021-08-27)
### Changed
- Improve AVX2 performance ([#261])
- Bump `cpufeatures` to v0.2 ([#265])
[#261]: https://github.com/RustCrypto/stream-ciphers/pull/261
[#265]: https://github.com/RustCrypto/stream-ciphers/pull/265
## 0.7.2 (2021-07-20)
### Changed
- Pin `zeroize` dependency to v1.3 ([#256])
[#256]: https://github.com/RustCrypto/stream-ciphers/pull/256
## 0.7.1 (2021-04-29)
### Added
- `hchacha` feature ([#234])
[#234]: https://github.com/RustCrypto/stream-ciphers/pull/234
## 0.7.0 (2021-04-29) [YANKED]
### Added
- AVX2 detection; MSRV 1.49+ ([#200], [#212])
- `XChaCha8` and `XChaCha12` ([#215])
### Changed
- Full 64-bit counters ([#217])
- Bump `cipher` crate dependency to v0.3 release ([#226])
### Fixed
- `rng` feature on big endian platforms ([#202])
- Stream-length overflow check ([#216])
### Removed
- `Clone` impls on RNGs ([#220])
[#200]: https://github.com/RustCrypto/stream-ciphers/pull/200
[#202]: https://github.com/RustCrypto/stream-ciphers/pull/202
[#212]: https://github.com/RustCrypto/stream-ciphers/pull/212
[#215]: https://github.com/RustCrypto/stream-ciphers/pull/215
[#216]: https://github.com/RustCrypto/stream-ciphers/pull/216
[#217]: https://github.com/RustCrypto/stream-ciphers/pull/217
[#220]: https://github.com/RustCrypto/stream-ciphers/pull/220
[#226]: https://github.com/RustCrypto/stream-ciphers/pull/226
## 0.6.0 (2020-10-16)
### Changed
- Rename `Cipher` to `ChaCha` ([#177])
- Replace `block-cipher`/`stream-cipher` with `cipher` crate ([#177])
[#177]: https://github.com/RustCrypto/stream-ciphers/pull/177
## 0.5.0 (2020-08-25)
### Changed
- Bump `stream-cipher` dependency to v0.7 ([#161], [#164])
[#161]: https://github.com/RustCrypto/stream-ciphers/pull/161
[#164]: https://github.com/RustCrypto/stream-ciphers/pull/164
## 0.4.3 (2020-06-11)
### Changed
- Documentation improvements ([#153], [#154], [#155])
[#153]: https://github.com/RustCrypto/stream-ciphers/pull/155
[#154]: https://github.com/RustCrypto/stream-ciphers/pull/155
[#155]: https://github.com/RustCrypto/stream-ciphers/pull/155
## 0.4.2 (2020-06-11)
### Added
- Documentation improvements ([#149])
- `Key`, `Nonce`, `XNonce`, and `LegacyNonce` type aliases ([#147])
[#149]: https://github.com/RustCrypto/stream-ciphers/pull/149
[#147]: https://github.com/RustCrypto/stream-ciphers/pull/147
## 0.4.1 (2020-06-06)
### Fixed
- Links in documentation ([#142])
[#142]: https://github.com/RustCrypto/stream-ciphers/pull/142
## 0.4.0 (2020-06-06)
### Changed
- Upgrade to the `stream-cipher` v0.4 crate ([#121], [#138])
[#138]: https://github.com/RustCrypto/stream-ciphers/pull/138
[#121]: https://github.com/RustCrypto/stream-ciphers/pull/121
## 0.3.4 (2020-03-02)
### Fixed
- Avoid accidental `alloc` and `std` linking ([#105])
[#105]: https://github.com/RustCrypto/stream-ciphers/pull/105
## 0.3.3 (2020-01-18)
### Changed
- Replace macros with `Rounds` trait + generics ([#100])
### Fixed
- Fix warnings when building with `rng` feature alone ([#99])
[#99]: https://github.com/RustCrypto/stream-ciphers/pull/99
[#100]: https://github.com/RustCrypto/stream-ciphers/pull/100
## 0.3.2 (2020-01-17)
### Added
- `CryptoRng` marker on all `ChaCha*Rng` types ([#91])
[#91]: https://github.com/RustCrypto/stream-ciphers/pull/91
## 0.3.1 (2020-01-16)
### Added
- Parallelize AVX2 backend ([#87])
- Benchmark for `ChaCha20Rng` ([#87])
### Fixed
- Fix broken buffering logic ([#86])
[#86]: https://github.com/RustCrypto/stream-ciphers/pull/86
[#87]: https://github.com/RustCrypto/stream-ciphers/pull/87
## 0.3.0 (2020-01-15) [YANKED]
NOTE: This release was yanked due to a showstopper bug in the newly added
buffering logic which when seeking in the keystream could result in plaintexts
being clobbered with the keystream instead of XOR'd correctly.
The bug was addressed in v0.3.1 ([#86]).
### Added
- AVX2 accelerated implementation ([#83])
- ChaCha8 and ChaCha20 reduced round variants ([#84])
### Changed
- Simplify portable implementation ([#76])
- Make 2018 edition crate; MSRV 1.34+ ([#77])
- Replace `salsa20-core` dependency with `ctr`-derived buffering ([#81])
### Removed
- `byteorder` dependency ([#80])
[#76]: https://github.com/RustCrypto/stream-ciphers/pull/76
[#77]: https://github.com/RustCrypto/stream-ciphers/pull/77
[#80]: https://github.com/RustCrypto/stream-ciphers/pull/80
[#81]: https://github.com/RustCrypto/stream-ciphers/pull/81
[#83]: https://github.com/RustCrypto/stream-ciphers/pull/83
[#84]: https://github.com/RustCrypto/stream-ciphers/pull/84
## 0.2.3 (2019-10-23)
### Security
- Ensure block counter < MAX_BLOCKS ([#68])
[#68]: https://github.com/RustCrypto/stream-ciphers/pull/68
## 0.2.2 (2019-10-22)
### Added
- SSE2 accelerated implementation ([#61])
[#61]: https://github.com/RustCrypto/stream-ciphers/pull/61
## 0.2.1 (2019-08-19)
### Added
- Add `MAX_BLOCKS` and `BLOCK_SIZE` constants ([#47])
[#47]: https://github.com/RustCrypto/stream-ciphers/pull/47
## 0.2.0 (2019-08-18)
### Added
- `impl SyncStreamCipher` ([#39])
- `XChaCha20` ([#36])
- Support for 12-byte nonces ala RFC 8439 ([#19])
### Changed
- Refactor around a `ctr`-like type ([#44])
- Extract and encapsulate `Cipher` type ([#43])
- Switch tests to use `new_sync_test!` ([#42])
- Refactor into `ChaCha20` and `ChaCha20Legacy` ([#25])
### Fixed
- Fix `zeroize` cargo feature ([#21])
- Fix broken Cargo feature attributes ([#21])
[#44]: https://github.com/RustCrypto/stream-ciphers/pull/44
[#43]: https://github.com/RustCrypto/stream-ciphers/pull/43
[#42]: https://github.com/RustCrypto/stream-ciphers/pull/42
[#39]: https://github.com/RustCrypto/stream-ciphers/pull/39
[#36]: https://github.com/RustCrypto/stream-ciphers/pull/36
[#25]: https://github.com/RustCrypto/stream-ciphers/pull/25
[#21]: https://github.com/RustCrypto/stream-ciphers/pull/21
[#19]: https://github.com/RustCrypto/stream-ciphers/pull/19
## 0.1.0 (2019-06-24)
- Initial release
+114
View File
@@ -0,0 +1,114 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "blobby"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "89af0b093cc13baa4e51e64e65ec2422f7e73aea0e612e5ad3872986671622f1"
[[package]]
name = "block-buffer"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2f6c7dbe95a6ed67ad9f18e57daf93a2f034c524b99fd2b76d18fdfeb6660aa"
dependencies = [
"hybrid-array",
]
[[package]]
name = "cfg-if"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"
[[package]]
name = "chacha20"
version = "0.10.1"
dependencies = [
"cfg-if",
"cipher",
"cpufeatures",
"hex-literal",
"rand_core",
"zeroize",
]
[[package]]
name = "cipher"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e8cf2a2c93cd704877c0858356ed03480ff301ee950b43f1cbe4573b088bfa6c"
dependencies = [
"blobby",
"block-buffer",
"crypto-common",
"inout",
]
[[package]]
name = "cpufeatures"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b2a41393f66f16b0823bb79094d54ac5fbd34ab292ddafb9a0456ac9f87d201"
dependencies = [
"libc",
]
[[package]]
name = "crypto-common"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ce6e4c961d6cd6c9a86db418387425e8bdeaf05b3c8bc1411e6dca4c252f1453"
dependencies = [
"hybrid-array",
]
[[package]]
name = "hex-literal"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e712f64ec3850b98572bffac52e2c6f282b29fe6c5fa6d42334b30be438d95c1"
[[package]]
name = "hybrid-array"
version = "0.4.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9155a582abd142abc056962c29e3ce5ff2ad5469f4246b537ed42c5deba857da"
dependencies = [
"typenum",
]
[[package]]
name = "inout"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4250ce6452e92010fdf7268ccc5d14faa80bb12fc741938534c58f16804e03c7"
dependencies = [
"hybrid-array",
]
[[package]]
name = "libc"
version = "0.2.186"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "68ab91017fe16c622486840e4c83c9a37afeff978bd239b5293d61ece587de66"
[[package]]
name = "rand_core"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "63b8176103e19a2643978565ca18b50549f6101881c443590420e4dc998a3c69"
[[package]]
name = "typenum"
version = "1.20.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6f5e870be6c3b371b77fe0ee0bafb859fa4964b4404c27de1d380043c4dda20"
[[package]]
name = "zeroize"
version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e13c156562582aa81c60cb29407084cdb54c4164760106ab78e6c5b0858cf64e"
+147
View File
@@ -0,0 +1,147 @@
# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
#
# When uploading crates to the registry Cargo will automatically
# "normalize" Cargo.toml files for maximal compatibility
# with all versions of Cargo and also rewrite `path` dependencies
# to registry (e.g., crates.io) dependencies.
#
# If you are reading this file be aware that the original Cargo.toml
# will likely look very different (and much more reasonable).
# See Cargo.toml.orig for the original contents.
[package]
edition = "2024"
rust-version = "1.85"
name = "chacha20"
version = "0.10.1"
authors = ["RustCrypto Developers"]
build = false
autolib = false
autobins = false
autoexamples = false
autotests = false
autobenches = false
description = """
The ChaCha20 stream cipher (RFC 8439) implemented in pure Rust using traits
from the RustCrypto `cipher` crate, with optional architecture-specific
hardware acceleration (AVX2, SSE2). Additionally provides the ChaCha8, ChaCha12,
XChaCha20, XChaCha12 and XChaCha8 stream ciphers, and also optional
rand_core-compatible RNGs based on those ciphers.
"""
documentation = "https://docs.rs/chacha20"
readme = "README.md"
keywords = [
"crypto",
"stream-cipher",
"chacha8",
"chacha12",
"xchacha20",
]
categories = [
"cryptography",
"no-std",
]
license = "MIT OR Apache-2.0"
repository = "https://github.com/RustCrypto/stream-ciphers"
resolver = "2"
[package.metadata.docs.rs]
all-features = true
[features]
default = ["cipher"]
legacy = ["cipher"]
rng = ["dep:rand_core"]
xchacha = ["cipher"]
[lib]
name = "chacha20"
path = "src/lib.rs"
[[test]]
name = "kats"
path = "tests/kats.rs"
[[test]]
name = "rng"
path = "tests/rng.rs"
[[bench]]
name = "mod"
path = "benches/mod.rs"
[dependencies.cfg-if]
version = "1"
[dependencies.cipher]
version = "0.5"
features = ["stream-wrapper"]
optional = true
[dependencies.rand_core]
version = "0.10"
optional = true
default-features = false
[dependencies.zeroize]
version = "1.8.1"
optional = true
default-features = false
[dev-dependencies.cipher]
version = "0.5"
features = ["dev"]
[dev-dependencies.hex-literal]
version = "1"
[target.'cfg(any(target_arch = "x86_64", target_arch = "x86"))'.dependencies.cpufeatures]
version = "0.3"
[lints.clippy]
borrow_as_ptr = "warn"
cast_lossless = "warn"
cast_possible_truncation = "warn"
cast_possible_wrap = "warn"
cast_precision_loss = "warn"
cast_sign_loss = "warn"
checked_conversions = "warn"
from_iter_instead_of_collect = "warn"
implicit_saturating_sub = "warn"
manual_assert = "warn"
map_unwrap_or = "warn"
missing_errors_doc = "warn"
missing_panics_doc = "warn"
mod_module_files = "warn"
must_use_candidate = "warn"
needless_range_loop = "allow"
ptr_as_ptr = "warn"
redundant_closure_for_method_calls = "warn"
ref_as_ptr = "warn"
return_self_not_must_use = "warn"
semicolon_if_nothing_returned = "warn"
std_instead_of_alloc = "warn"
std_instead_of_core = "warn"
trivially_copy_pass_by_ref = "warn"
undocumented_unsafe_blocks = "warn"
unnecessary_safety_comment = "warn"
unwrap_in_result = "warn"
unwrap_used = "warn"
[lints.rust]
missing_copy_implementations = "warn"
missing_debug_implementations = "warn"
missing_docs = "warn"
trivial_casts = "warn"
trivial_numeric_casts = "warn"
unreachable_pub = "warn"
unused_lifetimes = "warn"
unused_qualifications = "warn"
[lints.rust.unexpected_cfgs]
level = "warn"
priority = 0
check-cfg = [
"cfg(chacha20_avx512)",
'cfg(chacha20_backend, values("avx2", "avx512", "soft", "sse2"))',
]
+90
View File
@@ -0,0 +1,90 @@
[package]
name = "chacha20"
version = "0.10.1"
authors = ["RustCrypto Developers"]
edition = "2024"
rust-version = "1.85"
documentation = "https://docs.rs/chacha20"
readme = "README.md"
repository = "https://github.com/RustCrypto/stream-ciphers"
license = "MIT OR Apache-2.0"
keywords = ["crypto", "stream-cipher", "chacha8", "chacha12", "xchacha20"]
categories = ["cryptography", "no-std"]
description = """
The ChaCha20 stream cipher (RFC 8439) implemented in pure Rust using traits
from the RustCrypto `cipher` crate, with optional architecture-specific
hardware acceleration (AVX2, SSE2). Additionally provides the ChaCha8, ChaCha12,
XChaCha20, XChaCha12 and XChaCha8 stream ciphers, and also optional
rand_core-compatible RNGs based on those ciphers.
"""
[dependencies]
cfg-if = "1"
cipher = { version = "0.5", optional = true, features = ["stream-wrapper"] }
rand_core = { version = "0.10", optional = true, default-features = false }
# `zeroize` is an explicit dependency because this crate may be used without the `cipher` crate
zeroize = { version = "1.8.1", optional = true, default-features = false }
[target.'cfg(any(target_arch = "x86_64", target_arch = "x86"))'.dependencies]
cpufeatures = "0.3"
[dev-dependencies]
cipher = { version = "0.5", features = ["dev"] }
hex-literal = "1"
[features]
default = ["cipher"]
legacy = ["cipher"]
rng = ["dep:rand_core"]
xchacha = ["cipher"]
[lints.rust]
missing_copy_implementations = "warn"
missing_debug_implementations = "warn"
missing_docs = "warn"
trivial_casts = "warn"
trivial_numeric_casts = "warn"
unreachable_pub = "warn"
unused_lifetimes = "warn"
unused_qualifications = "warn"
[lints.rust.unexpected_cfgs]
level = "warn"
check-cfg = [
'cfg(chacha20_avx512)',
'cfg(chacha20_backend, values("avx2", "avx512", "soft", "sse2"))'
]
[lints.clippy]
borrow_as_ptr = "warn"
cast_lossless = "warn"
cast_possible_truncation = "warn"
cast_possible_wrap = "warn"
cast_precision_loss = "warn"
cast_sign_loss = "warn"
checked_conversions = "warn"
from_iter_instead_of_collect = "warn"
implicit_saturating_sub = "warn"
manual_assert = "warn"
map_unwrap_or = "warn"
missing_errors_doc = "warn"
missing_panics_doc = "warn"
mod_module_files = "warn"
must_use_candidate = "warn"
needless_range_loop = "allow"
ptr_as_ptr = "warn"
redundant_closure_for_method_calls = "warn"
ref_as_ptr = "warn"
return_self_not_must_use = "warn"
semicolon_if_nothing_returned = "warn"
trivially_copy_pass_by_ref = "warn"
std_instead_of_alloc = "warn"
std_instead_of_core = "warn"
undocumented_unsafe_blocks = "warn"
unnecessary_safety_comment = "warn"
unwrap_in_result = "warn"
unwrap_used = "warn"
[package.metadata.docs.rs]
all-features = true
+201
View File
@@ -0,0 +1,201 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
+25
View File
@@ -0,0 +1,25 @@
Copyright (c) 2019-2026 The RustCrypto Project Developers
Permission is hereby granted, free of charge, to any
person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the
Software without restriction, including without
limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software
is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice
shall be included in all copies or substantial portions
of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
+145
View File
@@ -0,0 +1,145 @@
# RustCrypto: ChaCha20
[![Crate][crate-image]][crate-link]
[![Docs][docs-image]][docs-link]
[![Build Status][build-image]][build-link]
![Apache2/MIT licensed][license-image]
![Rust Version][rustc-image]
[![Project Chat][chat-image]][chat-link]
[![HAZMAT][hazmat-image]][hazmat-link]
Implementation of the [ChaCha] family of stream ciphers.
ChaCha improves upon the previous [Salsa] family of stream ciphers
with increased per-round diffusion at no cost to performance.
This crate also contains an implementation of the [XChaCha] family of stream ciphers
with an extended 192-bit (24-byte) nonce, gated under the `xchacha` Cargo feature,
and "legacy" (a.k.a "djb") variant with 64-bit nonce, gated under the `legacy` crate feature.
**WARNING:** This implementation internally uses 32-bit counter,
while the original "legacy" variant implementation uses 64-bit counter.
In other words, it does not allow encryption of more than 256 GiB of data.
## Security
### ⚠️ Warning: [Hazmat!][hazmat-link]
This crate does not ensure ciphertexts are authentic (i.e. by using a MAC to
verify ciphertext integrity), which can lead to serious vulnerabilities
if used incorrectly!
To avoid this, use an [AEAD] mode based on ChaCha20, e.g. [`chacha20poly1305`].
See the [RustCrypto/AEADs] repository for more information.
USE AT YOUR OWN RISK!
### Notes
This crate has received one [security audit by NCC Group][NCC-AUDIT], with no significant
findings. We would like to thank [MobileCoin] for funding the audit.
All implementations contained in the crate (along with the underlying ChaCha20
stream cipher itself) are designed to execute in constant time.
## Examples
```rust
// This example requires `cipher` crate feature
#[cfg(feature = "cipher")] {
use chacha20::ChaCha20;
use chacha20::cipher::{KeyIvInit, StreamCipher, StreamCipherSeek};
use hex_literal::hex;
let key = [0x42; 32];
let nonce = [0x24; 12];
let plaintext = hex!("000102030405060708090A0B0C0D0E0F");
let ciphertext = hex!("e405626e4f1236b3670ee428332ea20e");
// Key and IV must be references to the `Array` type.
// Here we use the `Into` trait to convert arrays into it.
let mut cipher = ChaCha20::new(&key.into(), &nonce.into());
let mut buffer = plaintext;
// apply keystream (encrypt)
cipher.apply_keystream(&mut buffer);
assert_eq!(buffer, ciphertext);
let ciphertext = buffer;
// ChaCha ciphers support seeking
cipher.seek(0u32);
// decrypt ciphertext by applying keystream again
cipher.apply_keystream(&mut buffer);
assert_eq!(buffer, plaintext);
// stream ciphers can be used with streaming messages
cipher.seek(0u32);
for chunk in buffer.chunks_mut(3) {
cipher.apply_keystream(chunk);
}
assert_eq!(buffer, ciphertext);
}
```
## Configuration Flags
You can modify crate using the following configuration flags:
- `chacha20_backend="avx2"`: force AVX2 backend on x86/x86_64 targets.
Requires enabled AVX2 target feature. Ignored on non-x86(_64) targets.
- `chacha20_backend="avx512"`: force AVX-512 backend on x86/x86_64 targets.
Requires enabled AVX-512 target feature (MSRV 1.89). Ignored on non-x86(_64) targets.
- `chacha20_backend="soft"`: force software backend.
- `chacha20_backend="sse2"`: force SSE2 backend on x86/x86_64 targets.
Requires enabled SSE2 target feature. Ignored on non-x86(-64) targets.
To use the MSRV 1.89 AVX-512 support with autodetection, you must enable it using
`chacha20_avx512` configuration flag.
The flags can be enabled using `RUSTFLAGS` environmental variable
(e.g. `RUSTFLAGS='--cfg chacha20_backend="avx2"'`) or by modifying `.cargo/config.toml`.
## License
Licensed under either of:
- [Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0)
- [MIT license](http://opensource.org/licenses/MIT)
at your option.
### Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted
for inclusion in the work by you, as defined in the Apache-2.0 license, shall be
dual licensed as above, without any additional terms or conditions.
[//]: # (badges)
[crate-image]: https://img.shields.io/crates/v/chacha20.svg
[crate-link]: https://crates.io/crates/chacha20
[docs-image]: https://docs.rs/chacha20/badge.svg
[docs-link]: https://docs.rs/chacha20/
[build-image]: https://github.com/RustCrypto/stream-ciphers/actions/workflows/chacha20.yml/badge.svg
[build-link]: https://github.com/RustCrypto/stream-ciphers/actions/workflows/chacha20.yml
[license-image]: https://img.shields.io/badge/license-Apache2.0/MIT-blue.svg
[rustc-image]: https://img.shields.io/badge/rustc-1.85+-blue.svg
[chat-image]: https://img.shields.io/badge/zulip-join_chat-blue.svg
[chat-link]: https://rustcrypto.zulipchat.com/#narrow/stream/260049-stream-ciphers
[hazmat-image]: https://img.shields.io/badge/crypto-hazmat%E2%9A%A0-red.svg
[hazmat-link]: https://github.com/RustCrypto/meta/blob/master/HAZMAT.md
[//]: # (footnotes)
[ChaCha]: https://en.wikipedia.org/wiki/Salsa20#ChaCha_variant
[Salsa]: https://en.wikipedia.org/wiki/Salsa20
[XChaCha]: https://tools.ietf.org/html/draft-arciszewski-xchacha-02
[AEAD]: https://en.wikipedia.org/wiki/Authenticated_encryption
[`chacha20poly1305`]: https://docs.rs/chacha20poly1305
[RustCrypto/AEADs]: https://github.com/RustCrypto/AEADs
[NCC-AUDIT]: https://web.archive.org/web/20240108154854/https://research.nccgroup.com/wp-content/uploads/2020/02/NCC_Group_MobileCoin_RustCrypto_AESGCM_ChaCha20Poly1305_Implementation_Review_2020-02-12_v1.0.pdf
[MobileCoin]: https://www.mobilecoin.com/
+27
View File
@@ -0,0 +1,27 @@
//! Basic benchmarks
#![feature(test)]
extern crate test;
cipher::stream_cipher_bench!(
chacha20::ChaCha8;
chacha8_bench1_16b 16;
chacha8_bench2_256b 256;
chacha8_bench3_1kib 1024;
chacha8_bench4_16kib 16384;
);
cipher::stream_cipher_bench!(
chacha20::ChaCha12;
chacha12_bench1_16b 16;
chacha12_bench2_256b 256;
chacha12_bench3_1kib 1024;
chacha12_bench4_16kib 16384;
);
cipher::stream_cipher_bench!(
chacha20::ChaCha20;
chacha20_bench1_16b 16;
chacha20_bench2_256b 256;
chacha20_bench3_1kib 1024;
chacha20_bench4_16kib 16384;
);
+32
View File
@@ -0,0 +1,32 @@
#![cfg(any(feature = "cipher", feature = "rng"))]
use cfg_if::cfg_if;
cfg_if! {
if #[cfg(chacha20_backend = "soft")] {
pub(crate) mod soft;
} else if #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] {
cfg_if! {
if #[cfg(all(chacha20_avx512, chacha20_backend = "avx512"))] {
pub(crate) mod avx512;
// AVX-2 backend needed for RNG if enabled
#[cfg(feature = "rng")]
pub(crate) mod avx2;
} else if #[cfg(chacha20_backend = "avx2")] {
pub(crate) mod avx2;
} else if #[cfg(chacha20_backend = "sse2")] {
pub(crate) mod sse2;
} else {
pub(crate) mod soft;
#[cfg(chacha20_avx512)]
pub(crate) mod avx512;
pub(crate) mod avx2;
pub(crate) mod sse2;
}
}
} else if #[cfg(all(target_arch = "aarch64", target_feature = "neon"))] {
pub(crate) mod neon;
} else {
pub(crate) mod soft;
}
}
+354
View File
@@ -0,0 +1,354 @@
//! AVX2 backend.
#![allow(unsafe_op_in_unsafe_fn, reason = "needs triage")]
#![allow(clippy::cast_possible_truncation, reason = "needs triage")]
#![allow(clippy::cast_possible_wrap, reason = "needs triage")]
#![allow(clippy::cast_sign_loss, reason = "needs triage")]
#![allow(clippy::undocumented_unsafe_blocks, reason = "TODO")]
use crate::{Rounds, Variant};
use core::marker::PhantomData;
#[cfg(feature = "rng")]
use crate::ChaChaCore;
#[cfg(feature = "cipher")]
use crate::{STATE_WORDS, chacha::Block};
#[cfg(feature = "cipher")]
use cipher::{
BlockSizeUser, ParBlocks, ParBlocksSizeUser, StreamCipherBackend, StreamCipherClosure,
consts::{U4, U64},
};
#[cfg(target_arch = "x86")]
use core::arch::x86::*;
#[cfg(target_arch = "x86_64")]
use core::arch::x86_64::*;
/// Number of blocks processed in parallel.
const PAR_BLOCKS: usize = 4;
/// Number of `__m256i` to store parallel blocks.
const N: usize = PAR_BLOCKS / 2;
#[inline]
#[target_feature(enable = "avx2")]
#[cfg(feature = "cipher")]
#[cfg_attr(chacha20_backend = "avx512", expect(unused))]
pub(crate) unsafe fn inner<R, F, V>(state: &mut [u32; STATE_WORDS], f: F)
where
R: Rounds,
F: StreamCipherClosure<BlockSize = U64>,
V: Variant,
{
let state_ptr = state.as_ptr().cast::<__m128i>();
let v = [
_mm256_broadcastsi128_si256(_mm_loadu_si128(state_ptr.add(0))),
_mm256_broadcastsi128_si256(_mm_loadu_si128(state_ptr.add(1))),
_mm256_broadcastsi128_si256(_mm_loadu_si128(state_ptr.add(2))),
];
let mut c = _mm256_broadcastsi128_si256(_mm_loadu_si128(state_ptr.add(3)));
c = match size_of::<V::Counter>() {
4 => _mm256_add_epi32(c, _mm256_set_epi32(0, 0, 0, 1, 0, 0, 0, 0)),
8 => _mm256_add_epi64(c, _mm256_set_epi64x(0, 1, 0, 0)),
_ => unreachable!(),
};
let mut ctr = [c; N];
for i in 0..N {
ctr[i] = c;
c = match size_of::<V::Counter>() {
4 => _mm256_add_epi32(c, _mm256_set_epi32(0, 0, 0, 2, 0, 0, 0, 2)),
8 => _mm256_add_epi64(c, _mm256_set_epi64x(0, 2, 0, 2)),
_ => unreachable!(),
};
}
let mut backend = Backend::<R, V> {
v,
ctr,
_pd: PhantomData,
};
f.call(&mut backend);
state[12] = _mm256_extract_epi32(backend.ctr[0], 0) as u32;
match size_of::<V::Counter>() {
4 => {}
8 => state[13] = _mm256_extract_epi32(backend.ctr[0], 1) as u32,
_ => unreachable!(),
}
}
#[inline]
#[target_feature(enable = "avx2")]
#[cfg(feature = "rng")]
pub(crate) unsafe fn rng_inner<R, V>(core: &mut ChaChaCore<R, V>, buffer: &mut [u32; 64])
where
R: Rounds,
V: Variant,
{
let state_ptr = core.state.as_ptr().cast::<__m128i>();
let v = [
_mm256_broadcastsi128_si256(_mm_loadu_si128(state_ptr.add(0))),
_mm256_broadcastsi128_si256(_mm_loadu_si128(state_ptr.add(1))),
_mm256_broadcastsi128_si256(_mm_loadu_si128(state_ptr.add(2))),
];
let mut c = _mm256_broadcastsi128_si256(_mm_loadu_si128(state_ptr.add(3)));
c = _mm256_add_epi64(c, _mm256_set_epi64x(0, 1, 0, 0));
let mut ctr = [c; N];
for i in 0..N {
ctr[i] = c;
c = _mm256_add_epi64(c, _mm256_set_epi64x(0, 2, 0, 2));
}
let mut backend = Backend::<R, V> {
v,
ctr,
_pd: PhantomData,
};
backend.rng_gen_par_ks_blocks(buffer);
core.state[12] = _mm256_extract_epi32(backend.ctr[0], 0) as u32;
core.state[13] = _mm256_extract_epi32(backend.ctr[0], 1) as u32;
}
struct Backend<R: Rounds, V: Variant> {
v: [__m256i; 3],
ctr: [__m256i; N],
_pd: PhantomData<(R, V)>,
}
#[cfg(feature = "cipher")]
impl<R: Rounds, V: Variant> BlockSizeUser for Backend<R, V> {
type BlockSize = U64;
}
#[cfg(feature = "cipher")]
impl<R: Rounds, V: Variant> ParBlocksSizeUser for Backend<R, V> {
type ParBlocksSize = U4;
}
#[cfg(feature = "cipher")]
impl<R: Rounds, V: Variant> StreamCipherBackend for Backend<R, V> {
#[inline(always)]
fn gen_ks_block(&mut self, block: &mut Block) {
unsafe {
let res = rounds::<R>(&self.v, &self.ctr);
for c in self.ctr.iter_mut() {
*c = match size_of::<V::Counter>() {
4 => _mm256_add_epi32(*c, _mm256_set_epi32(0, 0, 0, 1, 0, 0, 0, 1)),
8 => _mm256_add_epi64(*c, _mm256_set_epi64x(0, 1, 0, 1)),
_ => unreachable!(),
};
}
let res0: [__m128i; 8] = core::mem::transmute(res[0]);
let block_ptr = block.as_mut_ptr().cast::<__m128i>();
for i in 0..4 {
_mm_storeu_si128(block_ptr.add(i), res0[2 * i]);
}
}
}
#[inline(always)]
fn gen_par_ks_blocks(&mut self, blocks: &mut ParBlocks<Self>) {
unsafe {
let vs = rounds::<R>(&self.v, &self.ctr);
let pb = PAR_BLOCKS as i32;
for c in self.ctr.iter_mut() {
*c = match size_of::<V::Counter>() {
4 => _mm256_add_epi32(*c, _mm256_set_epi32(0, 0, 0, pb, 0, 0, 0, pb)),
8 => {
_mm256_add_epi64(*c, _mm256_set_epi64x(0, i64::from(pb), 0, i64::from(pb)))
}
_ => unreachable!(),
}
}
let mut block_ptr = blocks.as_mut_ptr().cast::<__m128i>();
for v in vs {
let t: [__m128i; 8] = core::mem::transmute(v);
for i in 0..4 {
_mm_storeu_si128(block_ptr.add(i), t[2 * i]);
_mm_storeu_si128(block_ptr.add(4 + i), t[2 * i + 1]);
}
block_ptr = block_ptr.add(8);
}
}
}
}
#[cfg(feature = "rng")]
impl<R: Rounds, V: Variant> Backend<R, V> {
#[inline(always)]
fn rng_gen_par_ks_blocks(&mut self, blocks: &mut [u32; 64]) {
unsafe {
let vs = rounds::<R>(&self.v, &self.ctr);
let pb = PAR_BLOCKS as i32;
for c in self.ctr.iter_mut() {
*c = _mm256_add_epi64(*c, _mm256_set_epi64x(0, i64::from(pb), 0, i64::from(pb)));
}
let mut block_ptr = blocks.as_mut_ptr().cast::<__m128i>();
for v in vs {
let t: [__m128i; 8] = core::mem::transmute(v);
for i in 0..4 {
_mm_storeu_si128(block_ptr.add(i), t[2 * i]);
_mm_storeu_si128(block_ptr.add(4 + i), t[2 * i + 1]);
}
block_ptr = block_ptr.add(8);
}
}
}
}
#[inline]
#[target_feature(enable = "avx2")]
unsafe fn rounds<R: Rounds>(v: &[__m256i; 3], c: &[__m256i; N]) -> [[__m256i; 4]; N] {
let mut vs: [[__m256i; 4]; N] = [[_mm256_setzero_si256(); 4]; N];
for i in 0..N {
vs[i] = [v[0], v[1], v[2], c[i]];
}
for _ in 0..R::COUNT {
double_quarter_round(&mut vs);
}
for i in 0..N {
for j in 0..3 {
vs[i][j] = _mm256_add_epi32(vs[i][j], v[j]);
}
vs[i][3] = _mm256_add_epi32(vs[i][3], c[i]);
}
vs
}
#[inline]
#[target_feature(enable = "avx2")]
unsafe fn double_quarter_round(v: &mut [[__m256i; 4]; N]) {
add_xor_rot(v);
rows_to_cols(v);
add_xor_rot(v);
cols_to_rows(v);
}
/// The goal of this function is to transform the state words from:
/// ```text
/// [a0, a1, a2, a3] [ 0, 1, 2, 3]
/// [b0, b1, b2, b3] == [ 4, 5, 6, 7]
/// [c0, c1, c2, c3] [ 8, 9, 10, 11]
/// [d0, d1, d2, d3] [12, 13, 14, 15]
/// ```
///
/// to:
/// ```text
/// [a0, a1, a2, a3] [ 0, 1, 2, 3]
/// [b1, b2, b3, b0] == [ 5, 6, 7, 4]
/// [c2, c3, c0, c1] [10, 11, 8, 9]
/// [d3, d0, d1, d2] [15, 12, 13, 14]
/// ```
///
/// so that we can apply [`add_xor_rot`] to the resulting columns, and have it compute the
/// "diagonal rounds" (as defined in RFC 7539) in parallel. In practice, this shuffle is
/// non-optimal: the last state word to be altered in `add_xor_rot` is `b`, so the shuffle
/// blocks on the result of `b` being calculated.
///
/// We can optimize this by observing that the four quarter rounds in `add_xor_rot` are
/// data-independent: they only access a single column of the state, and thus the order of
/// the columns does not matter. We therefore instead shuffle the other three state words,
/// to obtain the following equivalent layout:
/// ```text
/// [a3, a0, a1, a2] [ 3, 0, 1, 2]
/// [b0, b1, b2, b3] == [ 4, 5, 6, 7]
/// [c1, c2, c3, c0] [ 9, 10, 11, 8]
/// [d2, d3, d0, d1] [14, 15, 12, 13]
/// ```
///
/// See https://github.com/sneves/blake2-avx2/pull/4 for additional details. The earliest
/// known occurrence of this optimization is in floodyberry's SSE4 ChaCha code from 2014:
/// - https://github.com/floodyberry/chacha-opt/blob/0ab65cb99f5016633b652edebaf3691ceb4ff753/chacha_blocks_ssse3-64.S#L639-L643
#[inline]
#[target_feature(enable = "avx2")]
unsafe fn rows_to_cols(vs: &mut [[__m256i; 4]; N]) {
// c >>>= 32; d >>>= 64; a >>>= 96;
for [a, _, c, d] in vs {
*c = _mm256_shuffle_epi32(*c, 0b_00_11_10_01); // _MM_SHUFFLE(0, 3, 2, 1)
*d = _mm256_shuffle_epi32(*d, 0b_01_00_11_10); // _MM_SHUFFLE(1, 0, 3, 2)
*a = _mm256_shuffle_epi32(*a, 0b_10_01_00_11); // _MM_SHUFFLE(2, 1, 0, 3)
}
}
/// The goal of this function is to transform the state words from:
/// ```text
/// [a3, a0, a1, a2] [ 3, 0, 1, 2]
/// [b0, b1, b2, b3] == [ 4, 5, 6, 7]
/// [c1, c2, c3, c0] [ 9, 10, 11, 8]
/// [d2, d3, d0, d1] [14, 15, 12, 13]
/// ```
///
/// to:
/// ```text
/// [a0, a1, a2, a3] [ 0, 1, 2, 3]
/// [b0, b1, b2, b3] == [ 4, 5, 6, 7]
/// [c0, c1, c2, c3] [ 8, 9, 10, 11]
/// [d0, d1, d2, d3] [12, 13, 14, 15]
/// ```
///
/// reversing the transformation of [`rows_to_cols`].
#[inline]
#[target_feature(enable = "avx2")]
unsafe fn cols_to_rows(vs: &mut [[__m256i; 4]; N]) {
// c <<<= 32; d <<<= 64; a <<<= 96;
for [a, _, c, d] in vs {
*c = _mm256_shuffle_epi32(*c, 0b_10_01_00_11); // _MM_SHUFFLE(2, 1, 0, 3)
*d = _mm256_shuffle_epi32(*d, 0b_01_00_11_10); // _MM_SHUFFLE(1, 0, 3, 2)
*a = _mm256_shuffle_epi32(*a, 0b_00_11_10_01); // _MM_SHUFFLE(0, 3, 2, 1)
}
}
#[inline]
#[target_feature(enable = "avx2")]
unsafe fn add_xor_rot(vs: &mut [[__m256i; 4]; N]) {
let rol16_mask = _mm256_set_epi64x(
0x0d0c_0f0e_0908_0b0a,
0x0504_0706_0100_0302,
0x0d0c_0f0e_0908_0b0a,
0x0504_0706_0100_0302,
);
let rol8_mask = _mm256_set_epi64x(
0x0e0d_0c0f_0a09_080b,
0x0605_0407_0201_0003,
0x0e0d_0c0f_0a09_080b,
0x0605_0407_0201_0003,
);
// a += b; d ^= a; d <<<= (16, 16, 16, 16);
for [a, b, _, d] in vs.iter_mut() {
*a = _mm256_add_epi32(*a, *b);
*d = _mm256_xor_si256(*d, *a);
*d = _mm256_shuffle_epi8(*d, rol16_mask);
}
// c += d; b ^= c; b <<<= (12, 12, 12, 12);
for [_, b, c, d] in vs.iter_mut() {
*c = _mm256_add_epi32(*c, *d);
*b = _mm256_xor_si256(*b, *c);
*b = _mm256_xor_si256(_mm256_slli_epi32(*b, 12), _mm256_srli_epi32(*b, 20));
}
// a += b; d ^= a; d <<<= (8, 8, 8, 8);
for [a, b, _, d] in vs.iter_mut() {
*a = _mm256_add_epi32(*a, *b);
*d = _mm256_xor_si256(*d, *a);
*d = _mm256_shuffle_epi8(*d, rol8_mask);
}
// c += d; b ^= c; b <<<= (7, 7, 7, 7);
for [_, b, c, d] in vs.iter_mut() {
*c = _mm256_add_epi32(*c, *d);
*b = _mm256_xor_si256(*b, *c);
*b = _mm256_xor_si256(_mm256_slli_epi32(*b, 7), _mm256_srli_epi32(*b, 25));
}
}
+625
View File
@@ -0,0 +1,625 @@
#![allow(unsafe_op_in_unsafe_fn)]
use crate::{Rounds, Variant};
use core::marker::PhantomData;
#[cfg(feature = "cipher")]
use crate::{STATE_WORDS, chacha::Block};
#[cfg(feature = "cipher")]
use cipher::{
BlockSizeUser, ParBlocks, ParBlocksSizeUser, StreamCipherBackend, StreamCipherClosure,
consts::{U16, U64},
};
#[cfg(target_arch = "x86")]
use core::arch::x86::*;
#[cfg(target_arch = "x86_64")]
use core::arch::x86_64::*;
/// Maximum number of blocks processed in parallel.
/// We also support 8 and 4 in gen_tail_blocks.
const MAX_PAR_BLOCKS: usize = 16;
/// Divisor to compute `N`, the number of __m512i needed
/// to represent a number of parallel blocks.
const BLOCKS_PER_VECTOR: usize = 4;
const MAX_N: usize = MAX_PAR_BLOCKS / BLOCKS_PER_VECTOR;
#[inline]
#[target_feature(enable = "avx512f")]
#[cfg(feature = "cipher")]
pub(crate) unsafe fn inner<R, F, V>(state: &mut [u32; STATE_WORDS], f: F)
where
R: Rounds,
F: StreamCipherClosure<BlockSize = U64>,
V: Variant,
{
let simd_state = state.as_mut_ptr().cast::<i32>();
let mut backend = Backend::<R, V> {
state: [
_mm_loadu_epi32(simd_state),
_mm_loadu_epi32(simd_state.add(4)),
_mm_loadu_epi32(simd_state.add(8)),
],
ctr: _mm_loadu_epi32(simd_state.add(12)),
_pd: PhantomData,
};
f.call(&mut backend);
// Update counter in the persistent state
state[12] = _mm_extract_epi32::<0>(backend.ctr) as u32;
if size_of::<V::Counter>() == 8 {
state[13] = _mm_extract_epi32::<1>(backend.ctr) as u32;
}
}
struct Backend<R: Rounds, V: Variant> {
state: [__m128i; 3],
ctr: __m128i,
_pd: PhantomData<(R, V)>,
}
#[cfg(feature = "cipher")]
impl<R: Rounds, V: Variant> Backend<R, V> {
#[inline]
#[target_feature(enable = "avx512f", enable = "avx512vl")]
unsafe fn increment_ctr(&mut self, amount: usize) {
match size_of::<V::Counter>() {
4 => {
self.ctr = _mm_add_epi32(self.ctr, _mm_set_epi32(0, 0, 0, amount as i32));
}
8 => {
self.ctr = _mm_add_epi64(self.ctr, _mm_set_epi64x(0, amount as i64));
}
_ => unreachable!(),
}
}
/// Generates blocks using the 512-bit-wide dispatch
/// with up to `N` vectors processed in parallel, producing
/// `N * BLOCKS_PER_VECTOR` blocks.
#[inline]
#[target_feature(enable = "avx512f", enable = "avx512vl")]
unsafe fn gen_blocks_fullwidth<const N: usize>(&mut self, blocks: &mut [Block]) {
let par_blocks = N * BLOCKS_PER_VECTOR;
assert!(blocks.len() <= par_blocks);
let mut ctrs = [_mm512_broadcast_i32x4(self.ctr); N];
for i in 0..ctrs.len() {
match size_of::<V::Counter>() {
4 => {
ctrs[i] = _mm512_add_epi32(
ctrs[i],
_mm512_set_epi32(
0,
0,
0,
(i * BLOCKS_PER_VECTOR + 3) as i32,
0,
0,
0,
(i * BLOCKS_PER_VECTOR + 2) as i32,
0,
0,
0,
(i * BLOCKS_PER_VECTOR + 1) as i32,
0,
0,
0,
(i * BLOCKS_PER_VECTOR) as i32,
),
);
}
8 => {
ctrs[i] = _mm512_add_epi64(
ctrs[i],
_mm512_set_epi64(
0,
(i * BLOCKS_PER_VECTOR + 3) as i64,
0,
(i * BLOCKS_PER_VECTOR + 2) as i64,
0,
(i * BLOCKS_PER_VECTOR + 1) as i64,
0,
(i * BLOCKS_PER_VECTOR) as i64,
),
);
}
_ => unreachable!(),
}
}
self.increment_ctr(blocks.len());
let result = rounds::<N, R>(&self.state.map(|v| _mm512_broadcast_i32x4(v)), &ctrs);
for i in 0..N {
let result_vectors = result[i];
// We have our data in SIMD vectors in the following layout
// (using a, b, c, and d to indicate the resp. 4 rows of each block,
// and Bn to denote the nth block):
// result_vectors[0]:
// B0a0 B0a1 B0a2 B0a3
// B1a0 B1a1 B1a2 B1a3
// ...
// B3a0 B3a1 B3a2 B3a2
//
// result_vectors[1]:
// B0b0 B0b1 B0b2 B0b3
// B1b0 B1b1 B1b2 B1b3
// ...
// B3b0 B3b1 B3b2 B3b2
//
// and so on for result_vectors[2] (storing c values) and result_vectors[3] (storing d values).
//
// To store to memory, we need to transpose to the following format:
// transposed[0]:
// B0a0 B0a1 B0a2 B0a3
// B0b0 B0b1 B0b2 B0b3
// B0c0 B0c1 B0c2 B0c3
// B0d0 B0d1 B0d2 B0d3
//
// and so on, such that each 512-bit SIMD vector
// contains a single contiguous block.
//
// We achieve this transposition using the following
// sequence of shuffles.
let temp_abab_block01 = _mm512_permutex2var_epi64(
result_vectors[0],
_mm512_setr_epi64(0, 1, 8, 9, 2, 3, 10, 11),
result_vectors[1],
);
let temp_abab_block23 = _mm512_permutex2var_epi64(
result_vectors[0],
_mm512_setr_epi64(4, 5, 12, 13, 6, 7, 14, 15),
result_vectors[1],
);
let temp_cdcd_block01 = _mm512_permutex2var_epi64(
result_vectors[2],
_mm512_setr_epi64(0, 1, 8, 9, 2, 3, 10, 11),
result_vectors[3],
);
let temp_cdcd_block23 = _mm512_permutex2var_epi64(
result_vectors[2],
_mm512_setr_epi64(4, 5, 12, 13, 6, 7, 14, 15),
result_vectors[3],
);
let block0 =
_mm512_shuffle_i32x4::<0b01_00_01_00>(temp_abab_block01, temp_cdcd_block01);
let block1 =
_mm512_shuffle_i32x4::<0b11_10_11_10>(temp_abab_block01, temp_cdcd_block01);
let block2 =
_mm512_shuffle_i32x4::<0b01_00_01_00>(temp_abab_block23, temp_cdcd_block23);
let block3 =
_mm512_shuffle_i32x4::<0b11_10_11_10>(temp_abab_block23, temp_cdcd_block23);
for (j, src_block) in [block0, block1, block2, block3].into_iter().enumerate() {
let dst_index = i * BLOCKS_PER_VECTOR + j;
if dst_index < blocks.len() {
_mm512_storeu_si512((&raw mut blocks[dst_index]).cast(), src_block);
}
}
}
}
/// Generates up to 2 blocks using 256-bit vectors.
#[inline]
#[target_feature(enable = "avx512f", enable = "avx512vl")]
unsafe fn gen_blocks_halfwidth(&mut self, blocks: &mut [Block]) {
assert!(blocks.len() <= 2);
let mut ctr = _mm256_broadcast_i32x4(self.ctr);
match size_of::<V::Counter>() {
4 => {
ctr = _mm256_add_epi32(ctr, _mm256_set_epi32(0, 0, 0, 1, 0, 0, 0, 0));
}
8 => {
ctr = _mm256_add_epi64(ctr, _mm256_set_epi64x(0, 1, 0, 0));
}
_ => unreachable!(),
}
self.increment_ctr(blocks.len());
let block_vectors = rounds_halfwide::<R>([
_mm256_broadcast_i32x4(self.state[0]),
_mm256_broadcast_i32x4(self.state[1]),
_mm256_broadcast_i32x4(self.state[2]),
ctr,
]);
// Similar transpose operation as
// in gen_blocks_fullwidth.
let block0_ab = _mm256_permutex2var_epi64(
block_vectors[0],
_mm256_setr_epi64x(0, 1, 4, 5),
block_vectors[1],
);
let block0_cd = _mm256_permutex2var_epi64(
block_vectors[2],
_mm256_setr_epi64x(0, 1, 4, 5),
block_vectors[3],
);
let block1_ab = _mm256_permutex2var_epi64(
block_vectors[0],
_mm256_setr_epi64x(2, 3, 6, 7),
block_vectors[1],
);
let block1_cd = _mm256_permutex2var_epi64(
block_vectors[2],
_mm256_setr_epi64x(2, 3, 6, 7),
block_vectors[3],
);
for (i, (block_part_ab, block_part_cd)) in [(block0_ab, block0_cd), (block1_ab, block1_cd)]
.into_iter()
.enumerate()
{
if i < blocks.len() {
let dst = (&raw mut blocks[i]).cast::<i32>();
_mm256_storeu_epi32(dst, block_part_ab);
_mm256_storeu_epi32(
dst.add(size_of::<Block>() / 2 / size_of::<i32>()),
block_part_cd,
);
}
}
}
}
#[cfg(feature = "cipher")]
impl<R: Rounds, V: Variant> BlockSizeUser for Backend<R, V> {
type BlockSize = U64;
}
#[cfg(feature = "cipher")]
impl<R: Rounds, V: Variant> ParBlocksSizeUser for Backend<R, V> {
type ParBlocksSize = U16;
}
#[cfg(feature = "cipher")]
impl<R: Rounds, V: Variant> StreamCipherBackend for Backend<R, V> {
#[inline]
fn gen_par_ks_blocks(&mut self, blocks: &mut ParBlocks<Self>) {
unsafe { self.gen_blocks_fullwidth::<MAX_N>(blocks) }
}
#[inline(always)]
fn gen_ks_block(&mut self, block: &mut Block) {
// Fallback for generating a single block using quarter-width vectors
// (128).
unsafe {
let state = [self.state[0], self.state[1], self.state[2], self.ctr];
self.increment_ctr(1);
let result = rounds_quarterwide::<R>(state);
for row in 0..4 {
let dst = block.as_mut_ptr().cast::<i32>().add(row * 4);
_mm_storeu_epi32(dst, result[row]);
}
}
}
#[inline]
fn gen_tail_blocks(&mut self, blocks: &mut [cipher::Block<Self>]) {
assert!(blocks.len() < MAX_PAR_BLOCKS);
if blocks.is_empty() {
return;
}
// Fallback for generating a number of blocks less than
// MAX_PAR_BLOCKS.
unsafe {
if blocks.len() == 1 {
self.gen_ks_block(&mut blocks[0]);
} else if blocks.len() == 2 {
self.gen_blocks_halfwidth(blocks);
} else if blocks.len() <= 4 {
self.gen_blocks_fullwidth::<1>(blocks);
} else if blocks.len() <= 8 {
self.gen_blocks_fullwidth::<2>(blocks);
} else {
self.gen_blocks_fullwidth::<MAX_N>(blocks);
}
}
}
}
#[inline]
#[target_feature(enable = "avx512f")]
unsafe fn rounds<const N: usize, R: Rounds>(
v: &[__m512i; 3],
c: &[__m512i; N],
) -> [[__m512i; 4]; N] {
let mut vs: [[__m512i; 4]; N] = [[_mm512_setzero_si512(); 4]; N];
for i in 0..N {
vs[i] = [v[0], v[1], v[2], c[i]];
}
for _ in 0..R::COUNT {
double_quarter_round(&mut vs);
}
for i in 0..N {
for j in 0..3 {
vs[i][j] = _mm512_add_epi32(vs[i][j], v[j]);
}
vs[i][3] = _mm512_add_epi32(vs[i][3], c[i]);
}
vs
}
#[inline]
#[target_feature(enable = "avx512f")]
unsafe fn double_quarter_round<const N: usize>(v: &mut [[__m512i; 4]; N]) {
add_xor_rot(v);
rows_to_cols(v);
add_xor_rot(v);
cols_to_rows(v);
}
/// The goal of this function is to transform the state words from:
/// ```text
/// [a0, a1, a2, a3] [ 0, 1, 2, 3]
/// [b0, b1, b2, b3] == [ 4, 5, 6, 7]
/// [c0, c1, c2, c3] [ 8, 9, 10, 11]
/// [d0, d1, d2, d3] [12, 13, 14, 15]
/// ```
///
/// to:
/// ```text
/// [a0, a1, a2, a3] [ 0, 1, 2, 3]
/// [b1, b2, b3, b0] == [ 5, 6, 7, 4]
/// [c2, c3, c0, c1] [10, 11, 8, 9]
/// [d3, d0, d1, d2] [15, 12, 13, 14]
/// ```
///
/// so that we can apply [`add_xor_rot`] to the resulting columns, and have it compute the
/// "diagonal rounds" (as defined in RFC 7539) in parallel. In practice, this shuffle is
/// non-optimal: the last state word to be altered in `add_xor_rot` is `b`, so the shuffle
/// blocks on the result of `b` being calculated.
///
/// We can optimize this by observing that the four quarter rounds in `add_xor_rot` are
/// data-independent: they only access a single column of the state, and thus the order of
/// the columns does not matter. We therefore instead shuffle the other three state words,
/// to obtain the following equivalent layout:
/// ```text
/// [a3, a0, a1, a2] [ 3, 0, 1, 2]
/// [b0, b1, b2, b3] == [ 4, 5, 6, 7]
/// [c1, c2, c3, c0] [ 9, 10, 11, 8]
/// [d2, d3, d0, d1] [14, 15, 12, 13]
/// ```
///
/// See https://github.com/sneves/blake2-avx2/pull/4 for additional details. The earliest
/// known occurrence of this optimization is in floodyberry's SSE4 ChaCha code from 2014:
/// - https://github.com/floodyberry/chacha-opt/blob/0ab65cb99f5016633b652edebaf3691ceb4ff753/chacha_blocks_ssse3-64.S#L639-L643
#[inline]
#[target_feature(enable = "avx512f")]
unsafe fn rows_to_cols<const N: usize>(vs: &mut [[__m512i; 4]; N]) {
// c >>>= 32; d >>>= 64; a >>>= 96;
for [a, _, c, d] in vs {
*c = _mm512_shuffle_epi32::<0b_00_11_10_01>(*c); // _MM_SHUFFLE(0, 3, 2, 1)
*d = _mm512_shuffle_epi32::<0b_01_00_11_10>(*d); // _MM_SHUFFLE(1, 0, 3, 2)
*a = _mm512_shuffle_epi32::<0b_10_01_00_11>(*a); // _MM_SHUFFLE(2, 1, 0, 3)
}
}
/// The goal of this function is to transform the state words from:
/// ```text
/// [a3, a0, a1, a2] [ 3, 0, 1, 2]
/// [b0, b1, b2, b3] == [ 4, 5, 6, 7]
/// [c1, c2, c3, c0] [ 9, 10, 11, 8]
/// [d2, d3, d0, d1] [14, 15, 12, 13]
/// ```
///
/// to:
/// ```text
/// [a0, a1, a2, a3] [ 0, 1, 2, 3]
/// [b0, b1, b2, b3] == [ 4, 5, 6, 7]
/// [c0, c1, c2, c3] [ 8, 9, 10, 11]
/// [d0, d1, d2, d3] [12, 13, 14, 15]
/// ```
///
/// reversing the transformation of [`rows_to_cols`].
#[inline]
#[target_feature(enable = "avx512f")]
unsafe fn cols_to_rows<const N: usize>(vs: &mut [[__m512i; 4]; N]) {
// c <<<= 32; d <<<= 64; a <<<= 96;
for [a, _, c, d] in vs {
*c = _mm512_shuffle_epi32::<0b_10_01_00_11>(*c); // _MM_SHUFFLE(2, 1, 0, 3)
*d = _mm512_shuffle_epi32::<0b_01_00_11_10>(*d); // _MM_SHUFFLE(1, 0, 3, 2)
*a = _mm512_shuffle_epi32::<0b_00_11_10_01>(*a); // _MM_SHUFFLE(0, 3, 2, 1)
}
}
#[inline]
#[target_feature(enable = "avx512f")]
unsafe fn add_xor_rot<const N: usize>(vs: &mut [[__m512i; 4]; N]) {
// a += b; d ^= a; d <<<= (16, 16, 16, 16);
for [a, b, _, d] in vs.iter_mut() {
*a = _mm512_add_epi32(*a, *b);
*d = _mm512_xor_si512(*d, *a);
*d = _mm512_rol_epi32::<16>(*d);
}
// c += d; b ^= c; b <<<= (12, 12, 12, 12);
for [_, b, c, d] in vs.iter_mut() {
*c = _mm512_add_epi32(*c, *d);
*b = _mm512_xor_si512(*b, *c);
*b = _mm512_rol_epi32::<12>(*b);
}
// a += b; d ^= a; d <<<= (8, 8, 8, 8);
for [a, b, _, d] in vs.iter_mut() {
*a = _mm512_add_epi32(*a, *b);
*d = _mm512_xor_si512(*d, *a);
*d = _mm512_rol_epi32::<8>(*d);
}
// c += d; b ^= c; b <<<= (7, 7, 7, 7);
for [_, b, c, d] in vs.iter_mut() {
*c = _mm512_add_epi32(*c, *d);
*b = _mm512_xor_si512(*b, *c);
*b = _mm512_rol_epi32::<7>(*b);
}
}
// Below is another implementation of the round application
// that uses 256-bit vectors instead of 512-bit (but, unlike
// the avx2 module, can use new AVX-512 instructions like rotates).
// It is used for tail processing of shorter outputs,
// since 256-bit instructions can be faster and lower latency
// than 512-bit instructions on certain microarchitectures (e.g. Zen 4).
#[inline]
#[target_feature(enable = "avx512f", enable = "avx512vl")]
unsafe fn rounds_halfwide<R: Rounds>(v_in: [__m256i; 4]) -> [__m256i; 4] {
let mut v = v_in;
for _ in 0..R::COUNT {
double_quarter_round_halfwide(&mut v);
}
for (a, b) in v.iter_mut().zip(v_in) {
*a = _mm256_add_epi32(*a, b);
}
v
}
#[inline]
#[target_feature(enable = "avx512f", enable = "avx512vl")]
unsafe fn double_quarter_round_halfwide(v: &mut [__m256i; 4]) {
add_xor_rot_halfwide(v);
rows_to_cols_halfwide(v);
add_xor_rot_halfwide(v);
cols_to_rows_halfwide(v);
}
#[inline]
#[target_feature(enable = "avx512f", enable = "avx512vl")]
unsafe fn rows_to_cols_halfwide(v: &mut [__m256i; 4]) {
// c >>>= 32; d >>>= 64; a >>>= 96;
let [a, _, c, d] = v;
*c = _mm256_shuffle_epi32::<0b_00_11_10_01>(*c); // _MM_SHUFFLE(0, 3, 2, 1)
*d = _mm256_shuffle_epi32::<0b_01_00_11_10>(*d); // _MM_SHUFFLE(1, 0, 3, 2)
*a = _mm256_shuffle_epi32::<0b_10_01_00_11>(*a); // _MM_SHUFFLE(2, 1, 0, 3)
}
#[inline]
#[target_feature(enable = "avx512f", enable = "avx512vl")]
unsafe fn cols_to_rows_halfwide(v: &mut [__m256i; 4]) {
// c <<<= 32; d <<<= 64; a <<<= 96;
let [a, _, c, d] = v;
*c = _mm256_shuffle_epi32::<0b_10_01_00_11>(*c); // _MM_SHUFFLE(2, 1, 0, 3)
*d = _mm256_shuffle_epi32::<0b_01_00_11_10>(*d); // _MM_SHUFFLE(1, 0, 3, 2)
*a = _mm256_shuffle_epi32::<0b_00_11_10_01>(*a); // _MM_SHUFFLE(0, 3, 2, 1)
}
#[inline]
#[target_feature(enable = "avx512f", enable = "avx512vl")]
unsafe fn add_xor_rot_halfwide(v: &mut [__m256i; 4]) {
let [a, b, c, d] = v;
// a += b; d ^= a; d <<<= (16, 16, 16, 16);
*a = _mm256_add_epi32(*a, *b);
*d = _mm256_xor_si256(*d, *a);
*d = _mm256_rol_epi32::<16>(*d);
// c += d; b ^= c; b <<<= (12, 12, 12, 12);
*c = _mm256_add_epi32(*c, *d);
*b = _mm256_xor_si256(*b, *c);
*b = _mm256_rol_epi32::<12>(*b);
// a += b; d ^= a; d <<<= (8, 8, 8, 8);
*a = _mm256_add_epi32(*a, *b);
*d = _mm256_xor_si256(*d, *a);
*d = _mm256_rol_epi32::<8>(*d);
// c += d; b ^= c; b <<<= (7, 7, 7, 7);
*c = _mm256_add_epi32(*c, *d);
*b = _mm256_xor_si256(*b, *c);
*b = _mm256_rol_epi32::<7>(*b);
}
// Finally, below is an implementation using 128-bit vectors
// for the case of generating a single block.
#[inline(always)]
unsafe fn rounds_quarterwide<R: Rounds>(v_in: [__m128i; 4]) -> [__m128i; 4] {
let mut v = v_in;
for _ in 0..R::COUNT {
double_quarter_round_quarterwide(&mut v);
}
for (a, b) in v.iter_mut().zip(v_in) {
*a = _mm_add_epi32(*a, b);
}
v
}
#[inline(always)]
unsafe fn double_quarter_round_quarterwide(v: &mut [__m128i; 4]) {
add_xor_rot_quarterwide(v);
rows_to_cols_quarterwide(v);
add_xor_rot_quarterwide(v);
cols_to_rows_quarterwide(v);
}
#[inline(always)]
unsafe fn rows_to_cols_quarterwide(v: &mut [__m128i; 4]) {
// c >>>= 32; d >>>= 64; a >>>= 96;
let [a, _, c, d] = v;
*c = _mm_shuffle_epi32::<0b_00_11_10_01>(*c); // _MM_SHUFFLE(0, 3, 2, 1)
*d = _mm_shuffle_epi32::<0b_01_00_11_10>(*d); // _MM_SHUFFLE(1, 0, 3, 2)
*a = _mm_shuffle_epi32::<0b_10_01_00_11>(*a); // _MM_SHUFFLE(2, 1, 0, 3)
}
#[inline(always)]
unsafe fn cols_to_rows_quarterwide(v: &mut [__m128i; 4]) {
// c <<<= 32; d <<<= 64; a <<<= 96;
let [a, _, c, d] = v;
*c = _mm_shuffle_epi32::<0b_10_01_00_11>(*c); // _MM_SHUFFLE(2, 1, 0, 3)
*d = _mm_shuffle_epi32::<0b_01_00_11_10>(*d); // _MM_SHUFFLE(1, 0, 3, 2)
*a = _mm_shuffle_epi32::<0b_00_11_10_01>(*a); // _MM_SHUFFLE(0, 3, 2, 1)
}
#[inline(always)]
unsafe fn add_xor_rot_quarterwide(v: &mut [__m128i; 4]) {
let [a, b, c, d] = v;
// a += b; d ^= a; d <<<= (16, 16, 16, 16);
*a = _mm_add_epi32(*a, *b);
*d = _mm_xor_si128(*d, *a);
*d = _mm_rol_epi32::<16>(*d);
// c += d; b ^= c; b <<<= (12, 12, 12, 12);
*c = _mm_add_epi32(*c, *d);
*b = _mm_xor_si128(*b, *c);
*b = _mm_rol_epi32::<12>(*b);
// a += b; d ^= a; d <<<= (8, 8, 8, 8);
*a = _mm_add_epi32(*a, *b);
*d = _mm_xor_si128(*d, *a);
*d = _mm_rol_epi32::<8>(*d);
// c += d; b ^= c; b <<<= (7, 7, 7, 7);
*c = _mm_add_epi32(*c, *d);
*b = _mm_xor_si128(*b, *c);
*b = _mm_rol_epi32::<7>(*b);
}
+364
View File
@@ -0,0 +1,364 @@
//! NEON-optimized implementation for aarch64 CPUs.
//!
//! Adapted from the Crypto++ `chacha_simd` implementation by Jack Lloyd and
//! Jeffrey Walton (public domain), but uses **8-block** outer batches for
//! bulk paths (see `ChaCha20_512_neon` in OpenSSL).
#![allow(unsafe_op_in_unsafe_fn, reason = "needs triage")]
use crate::{Rounds, STATE_WORDS, Variant};
use core::{arch::aarch64::*, marker::PhantomData};
#[cfg(feature = "rng")]
use crate::ChaChaCore;
#[cfg(feature = "cipher")]
use crate::chacha::Block;
#[cfg(feature = "cipher")]
use cipher::{
BlockSizeUser, ParBlocks, ParBlocksSizeUser, StreamCipherBackend, StreamCipherClosure,
consts::{U8, U64},
};
struct Backend<R: Rounds, V: Variant> {
state: [uint32x4_t; 4],
ctrs: [uint32x4_t; 8],
_pd: PhantomData<(R, V)>,
}
macro_rules! add_counter {
($a:expr, $b:expr, $variant:ty) => {
match size_of::<<$variant>::Counter>() {
4 => vaddq_u32($a, $b),
8 => vreinterpretq_u32_u64(vaddq_u64(
vreinterpretq_u64_u32($a),
vreinterpretq_u64_u32($b),
)),
_ => unreachable!(),
}
};
}
impl<R: Rounds, V: Variant> Backend<R, V> {
#[inline]
unsafe fn new(state: &mut [u32; STATE_WORDS]) -> Self {
let state = [
vld1q_u32(state.as_ptr().offset(0)),
vld1q_u32(state.as_ptr().offset(4)),
vld1q_u32(state.as_ptr().offset(8)),
vld1q_u32(state.as_ptr().offset(12)),
];
let ctrs = [
vld1q_u32([1, 0, 0, 0].as_ptr()),
vld1q_u32([2, 0, 0, 0].as_ptr()),
vld1q_u32([3, 0, 0, 0].as_ptr()),
vld1q_u32([4, 0, 0, 0].as_ptr()),
vld1q_u32([5, 0, 0, 0].as_ptr()),
vld1q_u32([6, 0, 0, 0].as_ptr()),
vld1q_u32([7, 0, 0, 0].as_ptr()),
vld1q_u32([8, 0, 0, 0].as_ptr()),
];
Backend::<R, V> {
state,
ctrs,
_pd: PhantomData,
}
}
}
#[inline]
#[cfg(feature = "cipher")]
#[target_feature(enable = "neon")]
pub(crate) unsafe fn inner<R, F, V>(state: &mut [u32; STATE_WORDS], f: F)
where
R: Rounds,
F: StreamCipherClosure<BlockSize = U64>,
V: Variant,
{
let mut backend = Backend::<R, V>::new(state);
f.call(&mut backend);
match size_of::<V::Counter>() {
4 => state[12] = vgetq_lane_u32(backend.state[3], 0),
8 => vst1q_u64(
state.as_mut_ptr().offset(12).cast::<u64>(),
vreinterpretq_u64_u32(backend.state[3]),
),
_ => unreachable!(),
}
}
/// Sets up backend and blindly writes 4 blocks to dest_ptr.
#[cfg(feature = "rng")]
#[inline]
#[target_feature(enable = "neon")]
pub(crate) unsafe fn rng_inner<R, V>(core: &mut ChaChaCore<R, V>, buffer: &mut [u32; 64])
where
R: Rounds,
V: Variant,
{
let mut backend = Backend::<R, V>::new(&mut core.state);
backend.write_par_ks_blocks(buffer);
vst1q_u64(
core.state.as_mut_ptr().offset(12).cast::<u64>(),
vreinterpretq_u64_u32(backend.state[3]),
);
}
#[cfg(feature = "cipher")]
impl<R: Rounds, V: Variant> BlockSizeUser for Backend<R, V> {
type BlockSize = U64;
}
#[cfg(feature = "cipher")]
impl<R: Rounds, V: Variant> ParBlocksSizeUser for Backend<R, V> {
/// Match OpenSSL's large-buffer path: 8 blocks
type ParBlocksSize = U8;
}
/// Evaluates to `a = a + b`, where the operands are u32x4s
macro_rules! add_assign_vec {
($a:expr, $b:expr) => {
$a = vaddq_u32($a, $b)
};
}
#[cfg(feature = "cipher")]
impl<R: Rounds, V: Variant> StreamCipherBackend for Backend<R, V> {
#[inline(always)]
fn gen_ks_block(&mut self, block: &mut Block) {
let state3 = self.state[3];
let mut par = ParBlocks::<Self>::default();
self.gen_par_ks_blocks(&mut par);
*block = par[0];
// SAFETY: we have used conditional compilation to ensure NEON is available
unsafe {
self.state[3] = add_counter!(state3, vld1q_u32([1, 0, 0, 0].as_ptr()), V);
}
}
#[inline(always)]
fn gen_par_ks_blocks(&mut self, dest: &mut ParBlocks<Self>) {
// SAFETY: we have used conditional compilation to ensure NEON is available
unsafe {
let mut blocks = [
[self.state[0], self.state[1], self.state[2], self.state[3]],
[
self.state[0],
self.state[1],
self.state[2],
add_counter!(self.state[3], self.ctrs[0], V),
],
[
self.state[0],
self.state[1],
self.state[2],
add_counter!(self.state[3], self.ctrs[1], V),
],
[
self.state[0],
self.state[1],
self.state[2],
add_counter!(self.state[3], self.ctrs[2], V),
],
[
self.state[0],
self.state[1],
self.state[2],
add_counter!(self.state[3], self.ctrs[3], V),
],
[
self.state[0],
self.state[1],
self.state[2],
add_counter!(self.state[3], self.ctrs[4], V),
],
[
self.state[0],
self.state[1],
self.state[2],
add_counter!(self.state[3], self.ctrs[5], V),
],
[
self.state[0],
self.state[1],
self.state[2],
add_counter!(self.state[3], self.ctrs[6], V),
],
];
for _ in 0..R::COUNT {
double_quarter_round(&mut blocks);
}
for block in 0..8 {
// add state to block
for state_row in 0..3 {
add_assign_vec!(blocks[block][state_row], self.state[state_row]);
}
if block > 0 {
add_assign_vec!(
blocks[block][3],
add_counter!(self.state[3], self.ctrs[block - 1], V)
);
} else {
add_assign_vec!(blocks[block][3], self.state[3]);
}
// write blocks to dest
for state_row in 0..4 {
vst1q_u8(
dest[block].as_mut_ptr().add(state_row << 4),
vreinterpretq_u8_u32(blocks[block][state_row]),
);
}
}
self.state[3] = add_counter!(self.state[3], self.ctrs[7], V);
}
}
}
macro_rules! rotate_left {
($v:expr, 8) => {{
let maskb = [3u8, 0, 1, 2, 7, 4, 5, 6, 11, 8, 9, 10, 15, 12, 13, 14];
let mask = vld1q_u8(maskb.as_ptr());
$v = vreinterpretq_u32_u8(vqtbl1q_u8(vreinterpretq_u8_u32($v), mask))
}};
($v:expr, 16) => {
$v = vreinterpretq_u32_u16(vrev32q_u16(vreinterpretq_u16_u32($v)))
};
($v:expr, $r:literal) => {
$v = vorrq_u32(vshlq_n_u32($v, $r), vshrq_n_u32($v, 32 - $r))
};
}
macro_rules! extract {
($v:expr, $s:literal) => {
$v = vextq_u32($v, $v, $s)
};
}
impl<R: Rounds, V: Variant> Backend<R, V> {
#[inline(always)]
/// Generates `num_blocks` blocks and blindly writes them to `dest_ptr`
///
/// `num_blocks` must be greater than 0, and less than or equal to 4.
///
/// # Safety
/// `dest_ptr` must have at least `64 * num_blocks` bytes available to be
/// overwritten, or else it could produce undefined behavior
#[cfg(feature = "rng")]
unsafe fn write_par_ks_blocks(&mut self, buffer: &mut [u32; 64]) {
let mut blocks = [
[self.state[0], self.state[1], self.state[2], self.state[3]],
[
self.state[0],
self.state[1],
self.state[2],
add_counter!(self.state[3], self.ctrs[0], V),
],
[
self.state[0],
self.state[1],
self.state[2],
add_counter!(self.state[3], self.ctrs[1], V),
],
[
self.state[0],
self.state[1],
self.state[2],
add_counter!(self.state[3], self.ctrs[2], V),
],
];
for _ in 0..R::COUNT {
double_quarter_round(&mut blocks);
}
let mut dest_ptr = buffer.as_mut_ptr().cast::<u8>();
for block in 0..4 {
// add state to block
for state_row in 0..3 {
add_assign_vec!(blocks[block][state_row], self.state[state_row]);
}
if block > 0 {
add_assign_vec!(
blocks[block][3],
add_counter!(self.state[3], self.ctrs[block - 1], V)
);
} else {
add_assign_vec!(blocks[block][3], self.state[3]);
}
// write blocks to buffer
for state_row in 0..4 {
#[allow(clippy::cast_sign_loss)]
vst1q_u8(
dest_ptr.offset(state_row << 4),
vreinterpretq_u8_u32(blocks[block][state_row as usize]),
);
}
dest_ptr = dest_ptr.add(64);
}
self.state[3] = add_counter!(self.state[3], self.ctrs[3], V);
}
}
#[inline]
unsafe fn double_quarter_round<const N: usize>(blocks: &mut [[uint32x4_t; 4]; N]) {
add_xor_rot(blocks);
rows_to_cols(blocks);
add_xor_rot(blocks);
cols_to_rows(blocks);
}
#[inline]
unsafe fn add_xor_rot<const N: usize>(blocks: &mut [[uint32x4_t; 4]; N]) {
/// Evaluates to `a = a ^ b`, where the operands are u32x4s
macro_rules! xor_assign_vec {
($a:expr, $b:expr) => {
$a = veorq_u32($a, $b)
};
}
for block in blocks.iter_mut() {
// this part of the code cannot be reduced much more without having
// to deal with some problems regarding `rotate_left` requiring the second
// argument to be a const, and const arrays cannot be indexed by non-consts
add_assign_vec!(block[0], block[1]);
xor_assign_vec!(block[3], block[0]);
rotate_left!(block[3], 16);
add_assign_vec!(block[2], block[3]);
xor_assign_vec!(block[1], block[2]);
rotate_left!(block[1], 12);
add_assign_vec!(block[0], block[1]);
xor_assign_vec!(block[3], block[0]);
rotate_left!(block[3], 8);
add_assign_vec!(block[2], block[3]);
xor_assign_vec!(block[1], block[2]);
rotate_left!(block[1], 7);
}
}
#[inline]
unsafe fn rows_to_cols<const N: usize>(blocks: &mut [[uint32x4_t; 4]; N]) {
for block in blocks.iter_mut() {
extract!(block[1], 1);
extract!(block[2], 2);
extract!(block[3], 3);
}
}
#[inline]
unsafe fn cols_to_rows<const N: usize>(blocks: &mut [[uint32x4_t; 4]; N]) {
for block in blocks.iter_mut() {
extract!(block[1], 3);
extract!(block[2], 2);
extract!(block[3], 1);
}
}
+87
View File
@@ -0,0 +1,87 @@
//! Portable implementation which does not rely on architecture-specific intrinsics.
#![allow(clippy::cast_possible_truncation)]
use crate::{ChaChaCore, Rounds, STATE_WORDS, Variant, quarter_round};
#[cfg(feature = "cipher")]
use crate::chacha::Block;
#[cfg(feature = "cipher")]
use cipher::{
BlockSizeUser, ParBlocksSizeUser, StreamCipherBackend,
consts::{U1, U64},
};
#[cfg(feature = "rng")]
use crate::rng::BLOCK_WORDS;
pub(crate) struct Backend<'a, R: Rounds, V: Variant>(pub(crate) &'a mut ChaChaCore<R, V>);
#[cfg(feature = "cipher")]
impl<R: Rounds, V: Variant> BlockSizeUser for Backend<'_, R, V> {
type BlockSize = U64;
}
#[cfg(feature = "cipher")]
impl<R: Rounds, V: Variant> ParBlocksSizeUser for Backend<'_, R, V> {
type ParBlocksSize = U1;
}
#[cfg(feature = "cipher")]
impl<R: Rounds, V: Variant> StreamCipherBackend for Backend<'_, R, V> {
#[inline(always)]
fn gen_ks_block(&mut self, block: &mut Block) {
let res = run_rounds::<R>(&self.0.state);
let mut ctr = (u64::from(self.0.state[13]) << 32) | u64::from(self.0.state[12]);
ctr = ctr.wrapping_add(1);
self.0.state[12] = ctr as u32;
if size_of::<V::Counter>() == 8 {
self.0.state[13] = (ctr >> 32) as u32;
}
for (chunk, val) in block.chunks_exact_mut(4).zip(res.iter()) {
chunk.copy_from_slice(&val.to_le_bytes());
}
}
}
#[cfg(feature = "rng")]
impl<R: Rounds, V: Variant> Backend<'_, R, V> {
#[inline(always)]
pub(crate) fn gen_ks_blocks(&mut self, buffer: &mut [u32; 64]) {
for block in 0..4 {
let res = run_rounds::<R>(&self.0.state);
let mut ctr = (u64::from(self.0.state[13]) << 32) | u64::from(self.0.state[12]);
ctr = ctr.wrapping_add(1);
self.0.state[12] = ctr as u32;
self.0.state[13] = (ctr >> 32) as u32;
buffer[block * BLOCK_WORDS as usize..(block + 1) * BLOCK_WORDS as usize]
.copy_from_slice(&res);
}
}
}
#[inline(always)]
fn run_rounds<R: Rounds>(state: &[u32; STATE_WORDS]) -> [u32; STATE_WORDS] {
let mut res = *state;
for _ in 0..R::COUNT {
// column rounds
quarter_round(0, 4, 8, 12, &mut res);
quarter_round(1, 5, 9, 13, &mut res);
quarter_round(2, 6, 10, 14, &mut res);
quarter_round(3, 7, 11, 15, &mut res);
// diagonal rounds
quarter_round(0, 5, 10, 15, &mut res);
quarter_round(1, 6, 11, 12, &mut res);
quarter_round(2, 7, 8, 13, &mut res);
quarter_round(3, 4, 9, 14, &mut res);
}
for (s1, s0) in res.iter_mut().zip(state.iter()) {
*s1 = s1.wrapping_add(*s0);
}
res
}
+293
View File
@@ -0,0 +1,293 @@
//! SSE2 backend.
#![allow(unsafe_op_in_unsafe_fn, reason = "needs triage")]
#![allow(clippy::cast_possible_truncation, reason = "needs triage")]
#![allow(clippy::cast_possible_wrap, reason = "needs triage")]
#![allow(clippy::cast_sign_loss, reason = "needs triage")]
#![allow(clippy::undocumented_unsafe_blocks, reason = "TODO")]
use crate::{Rounds, Variant};
#[cfg(feature = "rng")]
use crate::ChaChaCore;
#[cfg(feature = "cipher")]
use crate::{STATE_WORDS, chacha::Block};
#[cfg(feature = "cipher")]
use cipher::{
BlockSizeUser, ParBlocksSizeUser, StreamCipherBackend, StreamCipherClosure,
consts::{U4, U64},
};
use core::marker::PhantomData;
#[cfg(target_arch = "x86")]
use core::arch::x86::*;
#[cfg(target_arch = "x86_64")]
use core::arch::x86_64::*;
const PAR_BLOCKS: usize = 4;
#[inline]
#[target_feature(enable = "sse2")]
#[cfg(feature = "cipher")]
pub(crate) unsafe fn inner<R, F, V>(state: &mut [u32; STATE_WORDS], f: F)
where
R: Rounds,
F: StreamCipherClosure<BlockSize = U64>,
V: Variant,
{
let state_ptr = state.as_ptr().cast::<__m128i>();
let mut backend = Backend::<R, V> {
v: [
_mm_loadu_si128(state_ptr.add(0)),
_mm_loadu_si128(state_ptr.add(1)),
_mm_loadu_si128(state_ptr.add(2)),
_mm_loadu_si128(state_ptr.add(3)),
],
_pd: PhantomData,
};
f.call(&mut backend);
state[12] = _mm_cvtsi128_si32(backend.v[3]) as u32;
if size_of::<V::Counter>() == 8 {
state[13] = _mm_extract_epi32(backend.v[3], 1) as u32;
}
}
struct Backend<R: Rounds, V: Variant> {
v: [__m128i; 4],
_pd: PhantomData<(R, V)>,
}
#[cfg(feature = "cipher")]
impl<R: Rounds, V: Variant> BlockSizeUser for Backend<R, V> {
type BlockSize = U64;
}
#[cfg(feature = "cipher")]
impl<R: Rounds, V: Variant> ParBlocksSizeUser for Backend<R, V> {
type ParBlocksSize = U4;
}
#[cfg(feature = "cipher")]
impl<R: Rounds, V: Variant> StreamCipherBackend for Backend<R, V> {
#[inline(always)]
fn gen_ks_block(&mut self, block: &mut Block) {
unsafe {
let res = rounds::<R, V>(&self.v);
self.v[3] = match size_of::<V::Counter>() {
4 => _mm_add_epi32(self.v[3], _mm_set_epi32(0, 0, 0, 1)),
8 => _mm_add_epi64(self.v[3], _mm_set_epi64x(0, 1)),
_ => unreachable!(),
};
let block_ptr = block.as_mut_ptr().cast::<__m128i>();
for i in 0..4 {
_mm_storeu_si128(block_ptr.add(i), res[0][i]);
}
}
}
#[inline(always)]
fn gen_par_ks_blocks(&mut self, blocks: &mut cipher::ParBlocks<Self>) {
unsafe {
let res = rounds::<R, V>(&self.v);
self.v[3] = match size_of::<V::Counter>() {
4 => _mm_add_epi32(self.v[3], _mm_set_epi32(0, 0, 0, PAR_BLOCKS as i32)),
8 => _mm_add_epi64(self.v[3], _mm_set_epi64x(0, PAR_BLOCKS as i64)),
_ => unreachable!(),
};
let blocks_ptr = blocks.as_mut_ptr().cast::<__m128i>();
for block in 0..PAR_BLOCKS {
for i in 0..4 {
_mm_storeu_si128(blocks_ptr.add(i + block * PAR_BLOCKS), res[block][i]);
}
}
}
}
}
#[inline]
#[target_feature(enable = "sse2")]
#[cfg(feature = "rng")]
pub(crate) unsafe fn rng_inner<R, V>(core: &mut ChaChaCore<R, V>, buffer: &mut [u32; 64])
where
R: Rounds,
V: Variant,
{
let state_ptr = core.state.as_ptr().cast::<__m128i>();
let mut backend = Backend::<R, V> {
v: [
_mm_loadu_si128(state_ptr.add(0)),
_mm_loadu_si128(state_ptr.add(1)),
_mm_loadu_si128(state_ptr.add(2)),
_mm_loadu_si128(state_ptr.add(3)),
],
_pd: PhantomData,
};
backend.gen_ks_blocks(buffer);
core.state[12] = _mm_cvtsi128_si32(backend.v[3]) as u32;
core.state[13] = _mm_extract_epi32(backend.v[3], 1) as u32;
}
#[cfg(feature = "rng")]
impl<R: Rounds, V: Variant> Backend<R, V> {
#[inline(always)]
fn gen_ks_blocks(&mut self, block: &mut [u32; 64]) {
const _: () = assert!(4 * PAR_BLOCKS * size_of::<__m128i>() == size_of::<[u32; 64]>());
unsafe {
let res = rounds::<R, V>(&self.v);
self.v[3] = _mm_add_epi64(self.v[3], _mm_set_epi64x(0, PAR_BLOCKS as i64));
let blocks_ptr = block.as_mut_ptr().cast::<__m128i>();
for block in 0..PAR_BLOCKS {
for i in 0..4 {
_mm_storeu_si128(blocks_ptr.add(i + block * PAR_BLOCKS), res[block][i]);
}
}
}
}
}
#[inline]
#[target_feature(enable = "sse2")]
unsafe fn rounds<R: Rounds, V: Variant>(v: &[__m128i; 4]) -> [[__m128i; 4]; PAR_BLOCKS] {
let mut res = [*v; 4];
for block in 1..PAR_BLOCKS {
res[block][3] = match size_of::<V::Counter>() {
4 => _mm_add_epi32(res[block][3], _mm_set_epi32(0, 0, 0, block as i32)),
8 => _mm_add_epi64(res[block][3], _mm_set_epi64x(0, block as i64)),
_ => unreachable!(),
}
}
for _ in 0..R::COUNT {
double_quarter_round(&mut res);
}
for block in 0..PAR_BLOCKS {
for i in 0..3 {
res[block][i] = _mm_add_epi32(res[block][i], v[i]);
}
let ctr = match size_of::<V::Counter>() {
4 => _mm_add_epi32(v[3], _mm_set_epi32(0, 0, 0, block as i32)),
8 => _mm_add_epi64(v[3], _mm_set_epi64x(0, block as i64)),
_ => unreachable!(),
};
res[block][3] = _mm_add_epi32(res[block][3], ctr);
}
res
}
#[inline]
#[target_feature(enable = "sse2")]
unsafe fn double_quarter_round(v: &mut [[__m128i; 4]; PAR_BLOCKS]) {
add_xor_rot(v);
rows_to_cols(v);
add_xor_rot(v);
cols_to_rows(v);
}
/// The goal of this function is to transform the state words from:
/// ```text
/// [a0, a1, a2, a3] [ 0, 1, 2, 3]
/// [b0, b1, b2, b3] == [ 4, 5, 6, 7]
/// [c0, c1, c2, c3] [ 8, 9, 10, 11]
/// [d0, d1, d2, d3] [12, 13, 14, 15]
/// ```
///
/// to:
/// ```text
/// [a0, a1, a2, a3] [ 0, 1, 2, 3]
/// [b1, b2, b3, b0] == [ 5, 6, 7, 4]
/// [c2, c3, c0, c1] [10, 11, 8, 9]
/// [d3, d0, d1, d2] [15, 12, 13, 14]
/// ```
///
/// so that we can apply [`add_xor_rot`] to the resulting columns, and have it compute the
/// "diagonal rounds" (as defined in RFC 7539) in parallel. In practice, this shuffle is
/// non-optimal: the last state word to be altered in `add_xor_rot` is `b`, so the shuffle
/// blocks on the result of `b` being calculated.
///
/// We can optimize this by observing that the four quarter rounds in `add_xor_rot` are
/// data-independent: they only access a single column of the state, and thus the order of
/// the columns does not matter. We therefore instead shuffle the other three state words,
/// to obtain the following equivalent layout:
/// ```text
/// [a3, a0, a1, a2] [ 3, 0, 1, 2]
/// [b0, b1, b2, b3] == [ 4, 5, 6, 7]
/// [c1, c2, c3, c0] [ 9, 10, 11, 8]
/// [d2, d3, d0, d1] [14, 15, 12, 13]
/// ```
///
/// See https://github.com/sneves/blake2-avx2/pull/4 for additional details. The earliest
/// known occurrence of this optimization is in floodyberry's SSE4 ChaCha code from 2014:
/// - https://github.com/floodyberry/chacha-opt/blob/0ab65cb99f5016633b652edebaf3691ceb4ff753/chacha_blocks_ssse3-64.S#L639-L643
#[inline]
#[target_feature(enable = "sse2")]
unsafe fn rows_to_cols(blocks: &mut [[__m128i; 4]; PAR_BLOCKS]) {
for [a, _, c, d] in blocks.iter_mut() {
// c >>>= 32; d >>>= 64; a >>>= 96;
*c = _mm_shuffle_epi32(*c, 0b_00_11_10_01); // _MM_SHUFFLE(0, 3, 2, 1)
*d = _mm_shuffle_epi32(*d, 0b_01_00_11_10); // _MM_SHUFFLE(1, 0, 3, 2)
*a = _mm_shuffle_epi32(*a, 0b_10_01_00_11); // _MM_SHUFFLE(2, 1, 0, 3)
}
}
/// The goal of this function is to transform the state words from:
/// ```text
/// [a3, a0, a1, a2] [ 3, 0, 1, 2]
/// [b0, b1, b2, b3] == [ 4, 5, 6, 7]
/// [c1, c2, c3, c0] [ 9, 10, 11, 8]
/// [d2, d3, d0, d1] [14, 15, 12, 13]
/// ```
///
/// to:
/// ```text
/// [a0, a1, a2, a3] [ 0, 1, 2, 3]
/// [b0, b1, b2, b3] == [ 4, 5, 6, 7]
/// [c0, c1, c2, c3] [ 8, 9, 10, 11]
/// [d0, d1, d2, d3] [12, 13, 14, 15]
/// ```
///
/// reversing the transformation of [`rows_to_cols`].
#[inline]
#[target_feature(enable = "sse2")]
unsafe fn cols_to_rows(blocks: &mut [[__m128i; 4]; PAR_BLOCKS]) {
for [a, _, c, d] in blocks.iter_mut() {
// c <<<= 32; d <<<= 64; a <<<= 96;
*c = _mm_shuffle_epi32(*c, 0b_10_01_00_11); // _MM_SHUFFLE(2, 1, 0, 3)
*d = _mm_shuffle_epi32(*d, 0b_01_00_11_10); // _MM_SHUFFLE(1, 0, 3, 2)
*a = _mm_shuffle_epi32(*a, 0b_00_11_10_01); // _MM_SHUFFLE(0, 3, 2, 1)
}
}
#[inline]
#[target_feature(enable = "sse2")]
unsafe fn add_xor_rot(blocks: &mut [[__m128i; 4]; PAR_BLOCKS]) {
for [a, b, c, d] in blocks.iter_mut() {
// a += b; d ^= a; d <<<= (16, 16, 16, 16);
*a = _mm_add_epi32(*a, *b);
*d = _mm_xor_si128(*d, *a);
*d = _mm_xor_si128(_mm_slli_epi32(*d, 16), _mm_srli_epi32(*d, 16));
// c += d; b ^= c; b <<<= (12, 12, 12, 12);
*c = _mm_add_epi32(*c, *d);
*b = _mm_xor_si128(*b, *c);
*b = _mm_xor_si128(_mm_slli_epi32(*b, 12), _mm_srli_epi32(*b, 20));
// a += b; d ^= a; d <<<= (8, 8, 8, 8);
*a = _mm_add_epi32(*a, *b);
*d = _mm_xor_si128(*d, *a);
*d = _mm_xor_si128(_mm_slli_epi32(*d, 8), _mm_srli_epi32(*d, 24));
// c += d; b ^= c; b <<<= (7, 7, 7, 7);
*c = _mm_add_epi32(*c, *d);
*b = _mm_xor_si128(*b, *c);
*b = _mm_xor_si128(_mm_slli_epi32(*b, 7), _mm_srli_epi32(*b, 25));
}
}
+38
View File
@@ -0,0 +1,38 @@
use cipher::{
IvSizeUser, KeyIvInit, KeySizeUser, StreamCipherCoreWrapper,
array::Array,
consts::{U12, U32, U64},
};
use crate::{ChaChaCore, R8, R12, R20, Rounds, variants::Ietf};
/// Key type used by all ChaCha variants.
pub type Key = Array<u8, U32>;
/// Nonce type used by ChaCha variants.
pub type Nonce = Array<u8, U12>;
/// ChaCha8 stream cipher (reduced-round variant of [`ChaCha20`] with 8 rounds)
pub type ChaCha8 = StreamCipherCoreWrapper<ChaChaCore<R8, Ietf>>;
/// ChaCha12 stream cipher (reduced-round variant of [`ChaCha20`] with 12 rounds)
pub type ChaCha12 = StreamCipherCoreWrapper<ChaChaCore<R12, Ietf>>;
/// ChaCha20 stream cipher (RFC 8439 version with 96-bit nonce)
pub type ChaCha20 = StreamCipherCoreWrapper<ChaChaCore<R20, Ietf>>;
pub(crate) type Block = Array<u8, U64>;
impl<R: Rounds> KeySizeUser for ChaChaCore<R, Ietf> {
type KeySize = U32;
}
impl<R: Rounds> IvSizeUser for ChaChaCore<R, Ietf> {
type IvSize = U12;
}
impl<R: Rounds> KeyIvInit for ChaChaCore<R, Ietf> {
#[inline]
fn new(key: &Key, iv: &Nonce) -> Self {
ChaChaCore::<R, Ietf>::new_internal(key.as_ref(), iv.as_ref())
}
}
+32
View File
@@ -0,0 +1,32 @@
//! Legacy version of ChaCha20 with a 64-bit nonce
use crate::{ChaChaCore, Key, R20, variants::Legacy};
use cipher::{
IvSizeUser, KeyIvInit, KeySizeUser, StreamCipherCoreWrapper,
array::Array,
consts::{U8, U32},
};
/// Nonce type used by [`ChaCha20Legacy`].
pub type LegacyNonce = Array<u8, U8>;
/// The ChaCha20 stream cipher (legacy "djb" construction with 64-bit nonce).
pub type ChaCha20Legacy = StreamCipherCoreWrapper<ChaCha20LegacyCore>;
/// The ChaCha20 stream cipher (legacy "djb" construction with 64-bit nonce).
pub type ChaCha20LegacyCore = ChaChaCore<R20, Legacy>;
impl KeySizeUser for ChaCha20LegacyCore {
type KeySize = U32;
}
impl IvSizeUser for ChaCha20LegacyCore {
type IvSize = U8;
}
impl KeyIvInit for ChaCha20LegacyCore {
#[inline(always)]
fn new(key: &Key, iv: &LegacyNonce) -> Self {
ChaChaCore::<R20, Legacy>::new_internal(key.as_ref(), iv.as_ref())
}
}
+343
View File
@@ -0,0 +1,343 @@
#![no_std]
#![doc = include_str!("../README.md")]
#![doc(
html_logo_url = "https://raw.githubusercontent.com/RustCrypto/media/8f1a9894/logo.svg",
html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/8f1a9894/logo.svg"
)]
#![cfg_attr(docsrs, feature(doc_cfg))]
pub mod variants;
mod backends;
#[cfg(feature = "cipher")]
mod chacha;
#[cfg(feature = "legacy")]
mod legacy;
#[cfg(feature = "rng")]
mod rng;
#[cfg(feature = "xchacha")]
mod xchacha;
#[cfg(feature = "cipher")]
pub use chacha::{ChaCha8, ChaCha12, ChaCha20, Key, Nonce};
#[cfg(feature = "cipher")]
pub use cipher;
#[cfg(feature = "cipher")]
pub use cipher::KeyIvInit;
#[cfg(feature = "legacy")]
pub use legacy::{ChaCha20Legacy, ChaCha20LegacyCore, LegacyNonce};
#[cfg(feature = "rng")]
pub use rand_core;
#[cfg(feature = "rng")]
pub use rng::{ChaCha8Rng, ChaCha12Rng, ChaCha20Rng, Seed, SerializedRngState};
#[cfg(feature = "xchacha")]
pub use xchacha::{XChaCha8, XChaCha12, XChaCha20, XNonce, hchacha};
use cfg_if::cfg_if;
use core::{fmt, marker::PhantomData};
use variants::Variant;
#[cfg(feature = "cipher")]
use cipher::{BlockSizeUser, StreamCipherCore, StreamCipherSeekCore, consts::U64};
#[cfg(feature = "zeroize")]
use zeroize::{Zeroize, ZeroizeOnDrop};
/// State initialization constant ("expand 32-byte k")
#[cfg(any(feature = "cipher", feature = "rng"))]
const CONSTANTS: [u32; 4] = [0x6170_7865, 0x3320_646e, 0x7962_2d32, 0x6b20_6574];
/// Number of 32-bit words in the ChaCha state
const STATE_WORDS: usize = 16;
/// Marker type for a number of ChaCha rounds to perform.
pub trait Rounds: Copy {
/// The amount of rounds to perform
const COUNT: usize;
}
/// 8-rounds
#[derive(Copy, Clone, Debug)]
pub struct R8;
impl Rounds for R8 {
const COUNT: usize = 4;
}
/// 12-rounds
#[derive(Copy, Clone, Debug)]
pub struct R12;
impl Rounds for R12 {
const COUNT: usize = 6;
}
/// 20-rounds
#[derive(Copy, Clone, Debug)]
pub struct R20;
impl Rounds for R20 {
const COUNT: usize = 10;
}
cfg_if! {
if #[cfg(chacha20_backend = "soft")] {
type Tokens = ();
} else if #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] {
cfg_if! {
if #[cfg(all(chacha20_avx512, chacha20_backend = "avx512"))] {
#[cfg(not(all(target_feature = "avx512f", target_feature = "avx512vl")))]
compile_error!("You must enable `avx512f` and `avx512vl` target features with \
`chacha20_backend = "avx512"` configuration option");
type Tokens = ();
} else if #[cfg(chacha20_backend = "avx2")] {
#[cfg(not(target_feature = "avx2"))]
compile_error!("You must enable `avx2` target feature with \
`chacha20_backend = "avx2"` configuration option");
type Tokens = ();
} else if #[cfg(chacha20_backend = "sse2")] {
#[cfg(not(target_feature = "sse2"))]
compile_error!("You must enable `sse2` target feature with \
`chacha20_backend = "sse2"` configuration option");
type Tokens = ();
} else {
#[cfg(chacha20_avx512)]
cpufeatures::new!(avx512_cpuid, "avx512f", "avx512vl");
cpufeatures::new!(avx2_cpuid, "avx2");
cpufeatures::new!(sse2_cpuid, "sse2");
#[cfg(chacha20_avx512)]
type Tokens = (avx512_cpuid::InitToken, avx2_cpuid::InitToken, sse2_cpuid::InitToken);
#[cfg(not(chacha20_avx512))]
type Tokens = (avx2_cpuid::InitToken, sse2_cpuid::InitToken);
}
}
} else {
type Tokens = ();
}
}
/// The ChaCha core function.
pub struct ChaChaCore<R: Rounds, V: Variant> {
/// Internal state of the core function
state: [u32; STATE_WORDS],
/// CPU target feature tokens
#[allow(dead_code)]
tokens: Tokens,
/// Number of rounds to perform and the cipher variant
_pd: PhantomData<(R, V)>,
}
impl<R: Rounds, V: Variant> ChaChaCore<R, V> {
/// Constructs a ChaChaCore with the specified `key` and `iv`.
///
/// You must ensure that the iv is of the correct size when using this method
/// directly.
///
/// # Panics
/// If `iv.len()` is not equal to 4, 8, or 12.
#[must_use]
#[cfg(any(feature = "cipher", feature = "rng"))]
fn new_internal(key: &[u8; 32], iv: &[u8]) -> Self {
assert!(matches!(iv.len(), 4 | 8 | 12));
let mut state = [0u32; STATE_WORDS];
let ctr_size = size_of::<V::Counter>() / size_of::<u32>();
let (const_dst, state_rem) = state.split_at_mut(4);
let (key_dst, state_rem) = state_rem.split_at_mut(8);
let (_ctr_dst, iv_dst) = state_rem.split_at_mut(ctr_size);
const_dst.copy_from_slice(&CONSTANTS);
// TODO(tarcieri): when MSRV 1.88, use `[T]::as_chunks` to avoid panic
#[allow(clippy::unwrap_used, reason = "MSRV TODO")]
{
for (src, dst) in key.chunks_exact(4).zip(key_dst) {
*dst = u32::from_le_bytes(src.try_into().unwrap());
}
assert_eq!(size_of_val(iv_dst), size_of_val(iv));
for (src, dst) in iv.chunks_exact(4).zip(iv_dst) {
*dst = u32::from_le_bytes(src.try_into().unwrap());
}
}
cfg_if! {
if #[cfg(chacha20_backend = "soft")] {
let tokens = ();
} else if #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] {
cfg_if! {
if #[cfg(chacha20_backend = "avx512")] {
let tokens = ();
} else if #[cfg(chacha20_backend = "avx2")] {
let tokens = ();
} else if #[cfg(chacha20_backend = "sse2")] {
let tokens = ();
} else if #[cfg(chacha20_avx512)] {
let tokens = (avx512_cpuid::init(), avx2_cpuid::init(), sse2_cpuid::init());
} else {
let tokens = (avx2_cpuid::init(), sse2_cpuid::init());
}
}
} else {
let tokens = ();
}
}
Self {
state,
tokens,
_pd: PhantomData,
}
}
/// Get the current block position.
#[inline(always)]
#[must_use]
pub fn get_block_pos(&self) -> V::Counter {
V::get_block_pos(&self.state[12..])
}
/// Set the block position.
#[inline(always)]
pub fn set_block_pos(&mut self, pos: V::Counter) {
V::set_block_pos(&mut self.state[12..], pos);
}
}
impl<R: Rounds, V: Variant> fmt::Debug for ChaChaCore<R, V> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"ChaChaCore<R: {}, V: {}-bit)> {{ ... }}",
R::COUNT,
size_of::<V::Counter>() * 8
)
}
}
#[cfg(feature = "cipher")]
impl<R: Rounds, V: Variant> StreamCipherSeekCore for ChaChaCore<R, V> {
type Counter = V::Counter;
#[inline(always)]
fn get_block_pos(&self) -> Self::Counter {
self.get_block_pos()
}
#[inline(always)]
fn set_block_pos(&mut self, pos: Self::Counter) {
self.set_block_pos(pos);
}
}
#[cfg(feature = "cipher")]
impl<R: Rounds, V: Variant> StreamCipherCore for ChaChaCore<R, V> {
#[inline(always)]
fn remaining_blocks(&self) -> Option<usize> {
V::remaining_blocks(self.get_block_pos())
}
fn process_with_backend(
&mut self,
f: impl cipher::StreamCipherClosure<BlockSize = Self::BlockSize>,
) {
cfg_if! {
if #[cfg(chacha20_backend = "soft")] {
f.call(&mut backends::soft::Backend(self));
} else if #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] {
cfg_if! {
if #[cfg(all(chacha20_avx512, chacha20_backend = "avx512"))] {
unsafe {
backends::avx512::inner::<R, _, V>(&mut self.state, f);
}
} else if #[cfg(chacha20_backend = "avx2")] {
unsafe {
backends::avx2::inner::<R, _, V>(&mut self.state, f);
}
} else if #[cfg(chacha20_backend = "sse2")] {
unsafe {
backends::sse2::inner::<R, _, V>(&mut self.state, f);
}
} else {
#[cfg(chacha20_avx512)]
let (avx512_token, avx2_token, sse2_token) = self.tokens;
#[cfg(not(chacha20_avx512))]
let (avx2_token, sse2_token) = self.tokens;
#[cfg(chacha20_avx512)]
if avx512_token.get() {
// SAFETY: runtime CPU feature detection above ensures this is valid
unsafe {
backends::avx512::inner::<R, _, V>(&mut self.state, f);
}
return;
}
if avx2_token.get() {
// SAFETY: runtime CPU feature detection above ensures this is valid
unsafe {
backends::avx2::inner::<R, _, V>(&mut self.state, f);
}
} else if sse2_token.get() {
// SAFETY: runtime CPU feature detection above ensures this is valid
unsafe {
backends::sse2::inner::<R, _, V>(&mut self.state, f);
}
} else {
f.call(&mut backends::soft::Backend(self));
}
}
}
} else if #[cfg(all(target_arch = "aarch64", target_feature = "neon"))] {
// SAFETY: we have used conditional compilation to ensure NEON is available
unsafe {
backends::neon::inner::<R, _, V>(&mut self.state, f);
}
} else {
f.call(&mut backends::soft::Backend(self));
}
}
}
}
#[cfg(feature = "cipher")]
impl<R: Rounds, V: Variant> BlockSizeUser for ChaChaCore<R, V> {
type BlockSize = U64;
}
#[cfg(feature = "zeroize")]
impl<R: Rounds, V: Variant> Drop for ChaChaCore<R, V> {
fn drop(&mut self) {
self.state.zeroize();
}
}
#[cfg(feature = "zeroize")]
impl<R: Rounds, V: Variant> ZeroizeOnDrop for ChaChaCore<R, V> {}
/// The ChaCha20 quarter round function
///
/// We located this function in the root of the crate as we want it to be available
/// for the soft backend and for xchacha.
#[allow(dead_code)]
pub(crate) fn quarter_round(
a: usize,
b: usize,
c: usize,
d: usize,
state: &mut [u32; STATE_WORDS],
) {
state[a] = state[a].wrapping_add(state[b]);
state[d] ^= state[a];
state[d] = state[d].rotate_left(16);
state[c] = state[c].wrapping_add(state[d]);
state[b] ^= state[c];
state[b] = state[b].rotate_left(12);
state[a] = state[a].wrapping_add(state[b]);
state[d] ^= state[a];
state[d] = state[d].rotate_left(8);
state[c] = state[c].wrapping_add(state[d]);
state[b] ^= state[c];
state[b] = state[b].rotate_left(7);
}
+344
View File
@@ -0,0 +1,344 @@
#![allow(clippy::cast_possible_truncation, reason = "needs triage")]
#![allow(clippy::undocumented_unsafe_blocks, reason = "TODO")]
use core::fmt;
use rand_core::{
Infallible, SeedableRng, TryCryptoRng, TryRng,
block::{BlockRng, Generator},
};
#[cfg(feature = "zeroize")]
use zeroize::{Zeroize, ZeroizeOnDrop};
use crate::{
ChaChaCore, R8, R12, R20, Rounds, backends,
variants::{Legacy, Variant},
};
use cfg_if::cfg_if;
/// Seed value used to initialize ChaCha-based RNGs.
pub type Seed = [u8; 32];
/// Serialized RNG state.
pub type SerializedRngState = [u8; 49];
/// Number of 32-bit words per ChaCha block (fixed by algorithm definition).
pub(crate) const BLOCK_WORDS: u8 = 16;
/// Number of blocks generated by RNG core.
const BUF_BLOCKS: u8 = 4;
/// Buffer size in words used by buffered RNG.
const BUFFER_SIZE: usize = (BLOCK_WORDS * BUF_BLOCKS) as usize;
impl<R: Rounds, V: Variant> SeedableRng for ChaChaCore<R, V> {
type Seed = Seed;
#[inline]
fn from_seed(seed: Self::Seed) -> Self {
ChaChaCore::new_internal(&seed, &[0u8; 8])
}
}
impl<R: Rounds, V: Variant> Generator for ChaChaCore<R, V> {
type Output = [u32; BUFFER_SIZE];
/// Generates 4 blocks in parallel with avx2 & neon, but merely fills
/// 4 blocks with sse2 & soft
fn generate(&mut self, buffer: &mut [u32; BUFFER_SIZE]) {
cfg_if! {
if #[cfg(chacha20_backend = "soft")] {
backends::soft::Backend(self).gen_ks_blocks(buffer);
} else if #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] {
cfg_if! {
// AVX-512 doesn't support RNG, so use AVX-2 instead
if #[cfg(any(chacha20_backend = "avx2", chacha20_backend = "avx512"))] {
unsafe {
backends::avx2::rng_inner::<R, V>(self, buffer);
}
} else if #[cfg(chacha20_backend = "sse2")] {
unsafe {
backends::sse2::rng_inner::<R, V>(self, buffer);
}
} else {
#[cfg(chacha20_avx512)]
let (_avx512_token, avx2_token, sse2_token) = self.tokens;
#[cfg(not(chacha20_avx512))]
let (avx2_token, sse2_token) = self.tokens;
if avx2_token.get() {
unsafe {
backends::avx2::rng_inner::<R, V>(self, buffer);
}
} else if sse2_token.get() {
unsafe {
backends::sse2::rng_inner::<R, V>(self, buffer);
}
} else {
backends::soft::Backend(self).gen_ks_blocks(buffer);
}
}
}
} else if #[cfg(all(target_arch = "aarch64", target_feature = "neon"))] {
// SAFETY: we have used conditional compilation to ensure NEON is available
unsafe {
backends::neon::rng_inner::<R, V>(self, buffer);
}
} else {
backends::soft::Backend(self).gen_ks_blocks(buffer);
}
}
}
// `Drop` impl of `BlockRng` calls this method and passes reference to
// its internal buffer in `output`. So we zeroize its contents here.
#[cfg(feature = "zeroize")]
fn drop(&mut self, output: &mut Self::Output) {
output.zeroize();
}
}
macro_rules! impl_chacha_rng {
($Rng:ident, $rounds:ident) => {
/// A cryptographically secure random number generator that uses the ChaCha stream cipher.
///
/// See the [crate docs][crate] for more information about the underlying stream cipher.
///
/// This RNG implementation uses a 64-bit counter and 64-bit stream identifier (a.k.a nonce).
/// A 64-bit counter over 64-byte (16 word) blocks allows 1 ZiB of output before cycling,
/// and the stream identifier allows 2<sup>64</sup> unique streams of output per seed.
/// Both counter and stream are initialized to zero but may be set via the [`set_word_pos`]
/// and [`set_stream`] methods.
///
/// [`set_word_pos`]: Self::set_word_pos
/// [`set_stream`]: Self::set_stream
///
/// # Example
///
/// ```rust
#[doc = concat!("use chacha20::", stringify!($Rng), ";")]
/// use rand_core::{SeedableRng, Rng};
///
/// let seed = [42u8; 32];
#[doc = concat!("let mut rng = ", stringify!($Rng), "::from_seed(seed);")]
///
/// let random_u32 = rng.next_u32();
/// let random_u64 = rng.next_u64();
///
/// let mut random_bytes = [0u8; 3];
/// rng.fill_bytes(&mut random_bytes);
/// ```
///
/// See the [`rand`](https://docs.rs/rand/) crate for more advanced RNG functionality.
pub struct $Rng {
core: BlockRng<ChaChaCore<$rounds, Legacy>>,
}
impl SeedableRng for $Rng {
type Seed = Seed;
#[inline]
fn from_seed(seed: Self::Seed) -> Self {
let core = ChaChaCore::new_internal(&seed, &[0u8; 8]);
Self {
core: BlockRng::new(core),
}
}
}
impl TryRng for $Rng {
type Error = Infallible;
#[inline]
fn try_next_u32(&mut self) -> Result<u32, Self::Error> {
Ok(self.core.next_word())
}
#[inline]
fn try_next_u64(&mut self) -> Result<u64, Self::Error> {
Ok(self.core.next_u64_from_u32())
}
#[inline]
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Self::Error> {
self.core.fill_bytes(dest);
Ok(())
}
}
impl TryCryptoRng for $Rng {}
#[cfg(feature = "zeroize")]
impl ZeroizeOnDrop for $Rng {}
// We use custom implementation of `PartialEq` because RNG states
// may buffer different parts of the same keystream, while keeping
// buffer cursor pointing towards the same keystream point.
impl PartialEq<$Rng> for $Rng {
fn eq(&self, rhs: &$Rng) -> bool {
(self.get_seed() == rhs.get_seed())
&& (self.get_stream() == rhs.get_stream())
&& (self.get_word_pos() == rhs.get_word_pos())
}
}
impl Eq for $Rng {}
// Custom Debug implementation that does not expose the internal state
impl fmt::Debug for $Rng {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, concat!(stringify!($Rng), " {{ ... }}"))
}
}
impl $Rng {
/// Get the offset from the start of the stream, in 32-bit words.
///
/// Since the generated blocks are 16 words (2<sup>4</sup>) long and the
/// counter is 64-bits, the offset is a 68-bit number. Sub-word offsets are
/// not supported, hence the result can simply be multiplied by 4 to get a
/// byte-offset.
#[inline]
#[must_use]
pub fn get_word_pos(&self) -> u128 {
let mut block_counter = (u64::from(self.core.core.state[13]) << 32)
| u64::from(self.core.core.state[12]);
if self.core.word_offset() != 0 {
block_counter = block_counter.wrapping_sub(u64::from(BUF_BLOCKS));
}
let word_pos = u128::from(block_counter) * u128::from(BLOCK_WORDS)
+ self.core.word_offset() as u128;
// eliminate bits above the 68th bit
word_pos & ((1 << 68) - 1)
}
/// Set the offset from the start of the stream, in 32-bit words.
///
/// **This value will be erased when calling `set_stream()`,
/// so call `set_stream()` before calling `set_word_pos()`**
/// if you intend on using both of them together.
///
/// As with `get_word_pos`, we use a 68-bit number. Since the generator
/// simply cycles at the end of its period (1 ZiB), we ignore the upper
/// 60 bits.
#[inline]
pub fn set_word_pos(&mut self, word_offset: u128) {
let index = (word_offset % u128::from(BLOCK_WORDS)) as usize;
let counter = word_offset / u128::from(BLOCK_WORDS);
//self.set_block_pos(counter as u64);
self.core.core.state[12] = counter as u32;
self.core.core.state[13] = (counter >> 32) as u32;
self.core.reset_and_skip(index);
}
/// Sets the block pos and resets the RNG's index.
///
/// **This value will be erased when calling `set_stream()`,
/// so call `set_stream()` before calling `set_block_pos()`**
/// if you intend on using both of them together.
///
/// The word pos will be equal to `block_pos * 16 words per block`.
#[inline]
#[allow(unused)]
pub fn set_block_pos(&mut self, block_pos: u64) {
self.core.reset_and_skip(0);
self.core.core.set_block_pos(block_pos);
}
/// Get the block pos.
#[inline]
#[allow(unused)]
#[must_use]
pub fn get_block_pos(&self) -> u64 {
let counter = self.core.core.get_block_pos();
let offset = self.core.word_offset();
if offset != 0 {
counter - u64::from(BUF_BLOCKS) + offset as u64 / u64::from(BLOCK_WORDS)
} else {
counter
}
}
/// Set the stream ID and reset the `word_pos` to 0.
#[inline]
pub fn set_stream(&mut self, stream: u64) {
self.core.core.state[14] = stream as u32;
self.core.core.state[15] = (stream >> 32) as u32;
self.set_block_pos(0);
}
/// Get the stream number (nonce).
#[inline]
#[must_use]
pub fn get_stream(&self) -> u64 {
let mut result = [0u8; 8];
result[..4].copy_from_slice(&self.core.core.state[14].to_le_bytes());
result[4..].copy_from_slice(&self.core.core.state[15].to_le_bytes());
u64::from_le_bytes(result)
}
/// Get the RNG seed.
#[inline]
#[must_use]
pub fn get_seed(&self) -> [u8; 32] {
let seed = &self.core.core.state[4..12];
let mut result = [0u8; 32];
for (src, dst) in seed.iter().zip(result.chunks_exact_mut(4)) {
dst.copy_from_slice(&src.to_le_bytes())
}
result
}
/// Serialize RNG state.
///
/// # Warning
/// Leaking serialized RNG state to an attacker defeats security properties
/// provided by the RNG.
#[inline]
pub fn serialize_state(&self) -> SerializedRngState {
let seed = self.get_seed();
let stream = self.get_stream().to_le_bytes();
let word_pos = self.get_word_pos().to_le_bytes();
let mut res = [0u8; 49];
let (seed_dst, res_rem) = res.split_at_mut(32);
let (stream_dst, word_pos_dst) = res_rem.split_at_mut(8);
seed_dst.copy_from_slice(&seed);
stream_dst.copy_from_slice(&stream);
word_pos_dst.copy_from_slice(&word_pos[..9]);
debug_assert_eq!(&word_pos[9..], &[0u8; 7]);
res
}
/// Deserialize RNG state.
#[inline]
pub fn deserialize_state(state: &SerializedRngState) -> Self {
let (seed, state_rem) = state.split_at(32);
let (stream, word_pos_raw) = state_rem.split_at(8);
let seed: &[u8; 32] = seed.try_into().expect("seed.len() is equal to 32");
let stream: &[u8; 8] = stream.try_into().expect("stream.len() is equal to 8");
// Note that we use only 68 bits from `word_pos_raw`, i.e. 4 remaining bits
// get ignored and should be equal to zero in practice.
let mut word_pos_buf = [0u8; 16];
word_pos_buf[..9].copy_from_slice(word_pos_raw);
let word_pos = u128::from_le_bytes(word_pos_buf);
let core = ChaChaCore::new_internal(seed, stream);
let mut res = Self {
core: BlockRng::new(core),
};
res.set_word_pos(word_pos);
res
}
}
};
}
impl_chacha_rng!(ChaCha8Rng, R8);
impl_chacha_rng!(ChaCha12Rng, R12);
impl_chacha_rng!(ChaCha20Rng, R20);
+84
View File
@@ -0,0 +1,84 @@
//! ChaCha variant-specific configurations.
mod sealed {
pub trait Sealed {}
}
/// A trait that distinguishes some ChaCha variants. Contains configurations
/// for "Legacy" DJB variant and the IETF variant.
pub trait Variant: sealed::Sealed {
/// The counter's type.
#[cfg(not(feature = "cipher"))]
type Counter: Copy;
/// The counter's type.
#[cfg(feature = "cipher")]
type Counter: cipher::StreamCipherCounter;
/// Takes a slice of `state[12..NONCE_INDEX]` to convert it into
/// `Self::Counter`.
fn get_block_pos(row: &[u32]) -> Self::Counter;
/// Breaks down the `Self::Counter` type into a u32 array for setting the
/// block pos.
fn set_block_pos(row: &mut [u32], pos: Self::Counter);
/// A helper method for calculating the remaining blocks using these types
fn remaining_blocks(block_pos: Self::Counter) -> Option<usize>;
}
/// IETF ChaCha configuration to use a 32-bit counter and 96-bit nonce.
#[derive(Clone, Copy, Debug)]
pub enum Ietf {}
impl sealed::Sealed for Ietf {}
impl Variant for Ietf {
type Counter = u32;
#[inline(always)]
fn get_block_pos(row: &[u32]) -> u32 {
row[0]
}
#[inline(always)]
fn set_block_pos(row: &mut [u32], pos: u32) {
row[0] = pos;
}
#[inline(always)]
fn remaining_blocks(block_pos: u32) -> Option<usize> {
let remaining = u32::MAX - block_pos;
remaining.try_into().ok()
}
}
/// DJB variant specific features: 64-bit counter and 64-bit nonce.
#[cfg(any(feature = "legacy", feature = "rng"))]
#[derive(Clone, Copy, Debug)]
pub enum Legacy {}
#[cfg(any(feature = "legacy", feature = "rng"))]
impl sealed::Sealed for Legacy {}
#[cfg(any(feature = "legacy", feature = "rng"))]
impl Variant for Legacy {
type Counter = u64;
#[inline(always)]
fn get_block_pos(row: &[u32]) -> u64 {
(u64::from(row[1]) << 32) | u64::from(row[0])
}
#[inline(always)]
fn set_block_pos(row: &mut [u32], pos: u64) {
row[0] = (pos & 0xFFFF_FFFF) as u32;
row[1] = (pos >> 32) as u32;
}
#[inline(always)]
fn remaining_blocks(block_pos: u64) -> Option<usize> {
let remaining = u64::MAX - block_pos;
remaining.try_into().ok()
}
}
+179
View File
@@ -0,0 +1,179 @@
//! XChaCha is an extended nonce variant of ChaCha
use crate::{
CONSTANTS, ChaChaCore, Key, R8, R12, R20, Rounds, STATE_WORDS, quarter_round, variants::Ietf,
};
use cipher::{
BlockSizeUser, IvSizeUser, KeyIvInit, KeySizeUser, StreamCipherClosure, StreamCipherCore,
StreamCipherCoreWrapper, StreamCipherSeekCore,
array::Array,
consts::{U4, U16, U24, U32, U64},
};
#[cfg(feature = "zeroize")]
use zeroize::ZeroizeOnDrop;
/// Nonce type used by XChaCha variants.
pub type XNonce = Array<u8, U24>;
/// XChaCha is a ChaCha20 variant with an extended 192-bit (24-byte) nonce.
///
/// The construction is an adaptation of the same techniques used by
/// XChaCha as described in the paper "Extending the Salsa20 Nonce",
/// applied to the 96-bit nonce variant of ChaCha20, and derive a
/// separate subkey/nonce for each extended nonce:
///
/// <https://cr.yp.to/snuffle/xsalsa-20081128.pdf>
///
/// No authoritative specification exists for XChaCha20, however the
/// construction has "rough consensus and running code" in the form of
/// several interoperable libraries and protocols (e.g. libsodium, WireGuard)
/// and is documented in an (expired) IETF draft:
///
/// <https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-xchacha>
pub type XChaCha20 = StreamCipherCoreWrapper<XChaChaCore<R20>>;
/// XChaCha12 stream cipher (reduced-round variant of [`XChaCha20`] with 12 rounds)
pub type XChaCha12 = StreamCipherCoreWrapper<XChaChaCore<R12>>;
/// XChaCha8 stream cipher (reduced-round variant of [`XChaCha20`] with 8 rounds)
pub type XChaCha8 = StreamCipherCoreWrapper<XChaChaCore<R8>>;
/// The XChaCha core function.
#[derive(Debug)]
pub struct XChaChaCore<R: Rounds>(ChaChaCore<R, Ietf>);
impl<R: Rounds> KeySizeUser for XChaChaCore<R> {
type KeySize = U32;
}
impl<R: Rounds> IvSizeUser for XChaChaCore<R> {
type IvSize = U24;
}
impl<R: Rounds> BlockSizeUser for XChaChaCore<R> {
type BlockSize = U64;
}
impl<R: Rounds> KeyIvInit for XChaChaCore<R> {
fn new(key: &Key, iv: &XNonce) -> Self {
#[allow(clippy::unwrap_used)]
let subkey = hchacha::<R>(key, iv[..16].as_ref().try_into().unwrap());
let mut nonce = [0u8; 12];
// first 4 bytes are 0, last 8 bytes are last 8 from the iv
// according to draft-arciszewski-xchacha-03
nonce[4..].copy_from_slice(&iv[16..]);
Self(ChaChaCore::<R, Ietf>::new_internal(subkey.as_ref(), &nonce))
}
}
impl<R: Rounds> StreamCipherCore for XChaChaCore<R> {
#[inline(always)]
fn remaining_blocks(&self) -> Option<usize> {
self.0.remaining_blocks()
}
#[inline(always)]
fn process_with_backend(&mut self, f: impl StreamCipherClosure<BlockSize = Self::BlockSize>) {
self.0.process_with_backend(f);
}
}
impl<R: Rounds> StreamCipherSeekCore for XChaChaCore<R> {
type Counter = u32;
#[inline(always)]
fn get_block_pos(&self) -> u32 {
self.0.get_block_pos()
}
#[inline(always)]
fn set_block_pos(&mut self, pos: u32) {
self.0.set_block_pos(pos);
}
}
#[cfg(feature = "zeroize")]
impl<R: Rounds> ZeroizeOnDrop for XChaChaCore<R> {}
/// The HChaCha function: adapts the ChaCha core function in the same
/// manner that HSalsa adapts the Salsa function.
///
/// HChaCha takes 512-bits of input:
///
/// - Constants: `u32` x 4
/// - Key: `u32` x 8
/// - Nonce: `u32` x 4
///
/// It produces 256-bits of output suitable for use as a ChaCha key
///
/// For more information on HSalsa on which HChaCha is based, see:
///
/// <http://cr.yp.to/snuffle/xsalsa-20110204.pdf>
#[must_use]
pub fn hchacha<R: Rounds>(key: &Key, input: &Array<u8, U16>) -> Array<u8, U32> {
let mut state = [0u32; STATE_WORDS];
state[..4].copy_from_slice(&CONSTANTS);
// TODO(tarcieri): use `[T]::as_chunks` when MSRV 1.88
let key_chunks = Array::<u8, U4>::slice_as_chunks(key).0;
for (v, chunk) in state[4..12].iter_mut().zip(key_chunks) {
*v = u32::from_le_bytes(chunk.0);
}
let input_chunks = Array::<u8, U4>::slice_as_chunks(input).0;
for (v, chunk) in state[12..16].iter_mut().zip(input_chunks) {
*v = u32::from_le_bytes(chunk.0);
}
// R rounds consisting of R/2 column rounds and R/2 diagonal rounds
for _ in 0..R::COUNT {
// column rounds
quarter_round(0, 4, 8, 12, &mut state);
quarter_round(1, 5, 9, 13, &mut state);
quarter_round(2, 6, 10, 14, &mut state);
quarter_round(3, 7, 11, 15, &mut state);
// diagonal rounds
quarter_round(0, 5, 10, 15, &mut state);
quarter_round(1, 6, 11, 12, &mut state);
quarter_round(2, 7, 8, 13, &mut state);
quarter_round(3, 4, 9, 14, &mut state);
}
let mut output = Array::default();
for (chunk, val) in output[..16].chunks_exact_mut(4).zip(&state[..4]) {
chunk.copy_from_slice(&val.to_le_bytes());
}
for (chunk, val) in output[16..].chunks_exact_mut(4).zip(&state[12..]) {
chunk.copy_from_slice(&val.to_le_bytes());
}
output
}
#[cfg(test)]
mod hchacha20_tests {
use super::*;
use hex_literal::hex;
/// Test vectors from:
/// https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-xchacha#section-2.2.1
#[test]
fn test_vector() {
const KEY: [u8; 32] = hex!(
"000102030405060708090a0b0c0d0e0f"
"101112131415161718191a1b1c1d1e1f"
);
const INPUT: [u8; 16] = hex!("000000090000004a0000000031415927");
const OUTPUT: [u8; 32] = hex!(
"82413b4227b27bfed30e42508a877d73"
"a0f9e4d58a74a853c12ec41326d3ecdc"
);
let actual = hchacha::<R20>(&KEY.into(), &INPUT.into());
assert_eq!(actual.as_slice(), &OUTPUT);
}
}
Binary file not shown.
Binary file not shown.
+236
View File
@@ -0,0 +1,236 @@
//! Tests for ChaCha20 (IETF and "djb" versions) as well as XChaCha20
#[cfg(feature = "cipher")]
use chacha20::ChaCha20;
#[cfg(feature = "legacy")]
use chacha20::ChaCha20Legacy;
#[cfg(feature = "xchacha")]
use chacha20::XChaCha20;
// IETF version of ChaCha20 (96-bit nonce)
#[cfg(feature = "cipher")]
cipher::stream_cipher_test!(chacha20_core, "chacha20", ChaCha20);
#[cfg(feature = "cipher")]
cipher::stream_cipher_seek_test!(chacha20_seek, ChaCha20);
#[cfg(feature = "xchacha")]
cipher::stream_cipher_seek_test!(xchacha20_seek, XChaCha20);
#[cfg(feature = "legacy")]
cipher::stream_cipher_seek_test!(chacha20legacy_seek, ChaCha20Legacy);
#[cfg(feature = "cipher")]
mod chacha20test {
use chacha20::{ChaCha20, KeyIvInit};
use cipher::StreamCipher;
use hex_literal::hex;
//
// ChaCha20 test vectors from:
// <https://datatracker.ietf.org/doc/html/rfc8439#section-2.4.2>
//
const KEY: [u8; 32] = hex!("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f");
const IV: [u8; 12] = hex!("000000000000004a00000000");
const PLAINTEXT: [u8; 114] = hex!(
"
4c616469657320616e642047656e746c
656d656e206f662074686520636c6173
73206f66202739393a20496620492063
6f756c64206f6666657220796f75206f
6e6c79206f6e652074697020666f7220
746865206675747572652c2073756e73
637265656e20776f756c642062652069
742e
"
);
const KEYSTREAM: [u8; 114] = hex!(
"
224f51f3401bd9e12fde276fb8631ded8c131f823d2c06
e27e4fcaec9ef3cf788a3b0aa372600a92b57974cded2b
9334794cba40c63e34cdea212c4cf07d41b769a6749f3f
630f4122cafe28ec4dc47e26d4346d70b98c73f3e9c53a
c40c5945398b6eda1a832c89c167eacd901d7e2bf363
"
);
const CIPHERTEXT: [u8; 114] = hex!(
"
6e2e359a2568f98041ba0728dd0d6981
e97e7aec1d4360c20a27afccfd9fae0b
f91b65c5524733ab8f593dabcd62b357
1639d624e65152ab8f530c359f0861d8
07ca0dbf500d6a6156a38e088a22b65e
52bc514d16ccf806818ce91ab7793736
5af90bbf74a35be6b40b8eedf2785e42
874d
"
);
#[test]
fn chacha20_keystream() {
let mut cipher = ChaCha20::new(&KEY.into(), &IV.into());
// The test vectors omit the first 64-bytes of the keystream
let mut prefix = [0u8; 64];
cipher.apply_keystream(&mut prefix);
let mut buf = [0u8; 114];
cipher.apply_keystream(&mut buf);
assert_eq!(&buf[..], &KEYSTREAM[..]);
}
#[test]
fn chacha20_encryption() {
let mut cipher = ChaCha20::new(&KEY.into(), &IV.into());
let mut buf = PLAINTEXT;
// The test vectors omit the first 64-bytes of the keystream
let mut prefix = [0u8; 64];
cipher.apply_keystream(&mut prefix);
cipher.apply_keystream(&mut buf);
assert_eq!(&buf[..], &CIPHERTEXT[..]);
}
}
#[rustfmt::skip]
#[cfg(feature = "xchacha")]
mod xchacha20 {
use chacha20::{Key, XChaCha20, XNonce};
use cipher::{KeyIvInit, StreamCipher};
use hex_literal::hex;
cipher::stream_cipher_seek_test!(xchacha20_seek, XChaCha20);
//
// XChaCha20 test vectors from:
// <https://datatracker.ietf.org/doc/html/draft-arciszewski-xchacha-03#appendix-A.2>
//
const KEY: [u8; 32] = hex!("
808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9f
");
const IV: [u8; 24] = hex!("
404142434445464748494a4b4c4d4e4f5051525354555658
");
const PLAINTEXT: [u8; 304] = hex!("
5468652064686f6c65202870726f6e6f756e6365642022646f6c652229206973
20616c736f206b6e6f776e2061732074686520417369617469632077696c6420
646f672c2072656420646f672c20616e642077686973746c696e6720646f672e
2049742069732061626f7574207468652073697a65206f662061204765726d61
6e20736865706865726420627574206c6f6f6b73206d6f7265206c696b652061
206c6f6e672d6c656767656420666f782e205468697320686967686c7920656c
757369766520616e6420736b696c6c6564206a756d70657220697320636c6173
736966696564207769746820776f6c7665732c20636f796f7465732c206a6163
6b616c732c20616e6420666f78657320696e20746865207461786f6e6f6d6963
2066616d696c792043616e696461652e
");
const KEYSTREAM: [u8; 304] = hex!("
29624b4b1b140ace53740e405b2168540fd7d630c1f536fecd722fc3cddba7f4
cca98cf9e47e5e64d115450f9b125b54449ff76141ca620a1f9cfcab2a1a8a25
5e766a5266b878846120ea64ad99aa479471e63befcbd37cd1c22a221fe46221
5cf32c74895bf505863ccddd48f62916dc6521f1ec50a5ae08903aa259d9bf60
7cd8026fba548604f1b6072d91bc91243a5b845f7fd171b02edc5a0a84cf28dd
241146bc376e3f48df5e7fee1d11048c190a3d3deb0feb64b42d9c6fdeee290f
a0e6ae2c26c0249ea8c181f7e2ffd100cbe5fd3c4f8271d62b15330cb8fdcf00
b3df507ca8c924f7017b7e712d15a2eb5c50484451e54e1b4b995bd8fdd94597
bb94d7af0b2c04df10ba0890899ed9293a0f55b8bafa999264035f1d4fbe7fe0
aafa109a62372027e50e10cdfecca127
");
const CIPHERTEXT: [u8; 304] = hex!("
7d0a2e6b7f7c65a236542630294e063b7ab9b555a5d5149aa21e4ae1e4fbce87
ecc8e08a8b5e350abe622b2ffa617b202cfad72032a3037e76ffdcdc4376ee05
3a190d7e46ca1de04144850381b9cb29f051915386b8a710b8ac4d027b8b050f
7cba5854e028d564e453b8a968824173fc16488b8970cac828f11ae53cabd201
12f87107df24ee6183d2274fe4c8b1485534ef2c5fbc1ec24bfc3663efaa08bc
047d29d25043532db8391a8a3d776bf4372a6955827ccb0cdd4af403a7ce4c63
d595c75a43e045f0cce1f29c8b93bd65afc5974922f214a40b7c402cdb91ae73
c0b63615cdad0480680f16515a7ace9d39236464328a37743ffc28f4ddb324f4
d0f5bbdc270c65b1749a6efff1fbaa09536175ccd29fb9e6057b307320d31683
8a9c71f70b5b5907a66f7ea49aadc409
");
#[test]
fn xchacha20_keystream() {
let mut cipher = XChaCha20::new(&Key::from(KEY), &XNonce::from(IV));
// The test vectors omit the first 64-bytes of the keystream
let mut prefix = [0u8; 64];
cipher.apply_keystream(&mut prefix);
let mut buf = [0u8; 304];
cipher.apply_keystream(&mut buf);
assert_eq!(&buf[..], &KEYSTREAM[..]);
}
#[test]
fn xchacha20_encryption() {
let mut cipher = XChaCha20::new(&Key::from(KEY), &XNonce::from(IV));
let mut buf = PLAINTEXT;
// The test vectors omit the first 64-bytes of the keystream
let mut prefix = [0u8; 64];
cipher.apply_keystream(&mut prefix);
cipher.apply_keystream(&mut buf);
assert_eq!(&buf[..], &CIPHERTEXT[..]);
}
}
// Legacy "djb" version of ChaCha20 (64-bit nonce)
#[cfg(feature = "legacy")]
#[rustfmt::skip]
mod legacy {
use chacha20::{ChaCha20Legacy, LegacyNonce};
use cipher::{StreamCipher, StreamCipherSeek, KeyIvInit};
use hex_literal::hex;
cipher::stream_cipher_test!(chacha20_legacy_core, "chacha20-legacy", ChaCha20Legacy);
cipher::stream_cipher_seek_test!(chacha20_legacy_seek, ChaCha20Legacy);
const KEY_LONG: [u8; 32] = hex!("
0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20
");
const IV_LONG: [u8; 8] = hex!("0301040105090206");
const EXPECTED_LONG: [u8; 256] = hex!("
deeb6b9d06dff3e091bf3ad4f4d492b6dd98246f69691802e466e03bad235787
0f1c6c010b6c2e650c4bf58d2d35c72ab639437069a384e03100078cc1d735a0
db4e8f474ee6291460fd9197c77ed87b4c64e0d9ac685bd1c56cce021f3819cd
13f49c9a3053603602582a060e59c2fbee90ab0bf7bb102d819ced03969d3bae
71034fe598246583336aa744d8168e5dfff5c6d10270f125a4130e719717e783
c0858b6f7964437173ea1d7556c158bc7a99e74a34d93da6bf72ac9736a215ac
aefd4ec031f3f13f099e3d811d83a2cf1d544a68d2752409cc6be852b0511a2e
32f69aa0be91b30981584a1c56ce7546cca24d8cfdfca525d6b15eea83b6b686
");
#[test]
#[ignore]
fn chacha20_offsets() {
for idx in 0..256 {
for middle in idx..256 {
for last in middle..256 {
let mut cipher =
ChaCha20Legacy::new(&KEY_LONG.into(), &LegacyNonce::from(IV_LONG));
let mut buf = [0; 256];
cipher.seek(idx as u64);
cipher.apply_keystream(&mut buf[idx..middle]);
cipher.apply_keystream(&mut buf[middle..last]);
for k in idx..last {
assert_eq!(buf[k], EXPECTED_LONG[k]);
}
}
}
}
}
}
+552
View File
@@ -0,0 +1,552 @@
//! Random number generator tests.
#![cfg(feature = "rng")]
use chacha20::{
ChaCha8Rng, ChaCha12Rng, ChaCha20Rng, SerializedRngState,
rand_core::{Rng, SeedableRng},
};
use hex_literal::hex;
const KEY: [u8; 32] = hex!("0102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F20");
const STREAM: u64 = 0xF0F1F2F3_F4F5F6F7;
const BLOCK_WORDS: u8 = 16;
#[test]
fn test_rng_output() {
let mut rng = ChaCha20Rng::from_seed(KEY);
let mut bytes = [0u8; 13];
rng.fill_bytes(&mut bytes);
let expected = hex!("B1697E9FC6461E1983D131CF69");
assert_eq!(bytes, expected);
rng.fill_bytes(&mut bytes);
let expected = hex!("A7A3FC134F149880E8BB2B5D23");
assert_eq!(bytes, expected);
}
#[test]
fn test_chacha_true_values_a() {
// Test vectors 1 and 2 from
// https://tools.ietf.org/html/draft-nir-cfrg-chacha20-poly1305-04
let seed = [0u8; 32];
let mut rng = ChaCha20Rng::from_seed(seed);
let mut results = [0u32; 16];
for i in results.iter_mut() {
*i = rng.next_u32();
}
let expected = [
0xade0b876, 0x903df1a0, 0xe56a5d40, 0x28bd8653, 0xb819d2bd, 0x1aed8da0, 0xccef36a8,
0xc70d778b, 0x7c5941da, 0x8d485751, 0x3fe02477, 0x374ad8b8, 0xf4b8436a, 0x1ca11815,
0x69b687c3, 0x8665eeb2,
];
assert_eq!(results, expected);
for i in results.iter_mut() {
*i = rng.next_u32();
}
let expected = [
0xbee7079f, 0x7a385155, 0x7c97ba98, 0x0d082d73, 0xa0290fcb, 0x6965e348, 0x3e53c612,
0xed7aee32, 0x7621b729, 0x434ee69c, 0xb03371d5, 0xd539d874, 0x281fed31, 0x45fb0a51,
0x1f0ae1ac, 0x6f4d794b,
];
assert_eq!(results, expected);
}
#[test]
fn test_chacha_true_values_b() {
// Test vector 3 from
// https://tools.ietf.org/html/draft-nir-cfrg-chacha20-poly1305-04
let seed = hex!("0000000000000000000000000000000000000000000000000000000000000001");
let mut rng = ChaCha20Rng::from_seed(seed);
// Skip block 0
for _ in 0..16 {
rng.next_u32();
}
let mut results = [0u32; 16];
for i in results.iter_mut() {
*i = rng.next_u32();
}
let expected = [
0x2452eb3a, 0x9249f8ec, 0x8d829d9b, 0xddd4ceb1, 0xe8252083, 0x60818b01, 0xf38422b8,
0x5aaa49c9, 0xbb00ca8e, 0xda3ba7b4, 0xc4b592d1, 0xfdf2732f, 0x4436274e, 0x2561b3c8,
0xebdd4aa6, 0xa0136c00,
];
assert_eq!(results, expected);
}
#[test]
fn test_chacha_true_values_c() {
// Test vector 4 from
// https://tools.ietf.org/html/draft-nir-cfrg-chacha20-poly1305-04
let seed = hex!("00FF000000000000000000000000000000000000000000000000000000000000");
let expected = [
0xfb4dd572, 0x4bc42ef1, 0xdf922636, 0x327f1394, 0xa78dea8f, 0x5e269039, 0xa1bebbc1,
0xcaf09aae, 0xa25ab213, 0x48a6b46c, 0x1b9d9bcb, 0x092c5be6, 0x546ca624, 0x1bec45d5,
0x87f47473, 0x96f0992e,
];
let expected_end = 3 * 16;
let mut results = [0u32; 16];
// Test block 2 by skipping block 0 and 1
let mut rng1 = ChaCha20Rng::from_seed(seed);
for _ in 0..32 {
rng1.next_u32();
}
for i in results.iter_mut() {
*i = rng1.next_u32();
}
assert_eq!(results, expected);
assert_eq!(rng1.get_word_pos(), expected_end);
// Test block 2 by using `set_word_pos`
let mut rng2 = ChaCha20Rng::from_seed(seed);
rng2.set_word_pos(2 * 16);
for i in results.iter_mut() {
*i = rng2.next_u32();
}
assert_eq!(results, expected);
assert_eq!(rng2.get_word_pos(), expected_end);
// Test block 2 by using `set_block_pos` and u32
let mut rng3 = ChaCha20Rng::from_seed(seed);
rng3.set_block_pos(2);
results = [0u32; 16];
for i in results.iter_mut() {
*i = rng3.next_u32();
}
assert_eq!(results, expected);
assert_eq!(rng3.get_word_pos(), expected_end);
// Test block 2 by using `set_block_pos` and [u8; 8]
let mut rng4 = ChaCha20Rng::from_seed(seed);
rng4.set_block_pos(2);
let mut buf = [0u8; 8];
for chunk in expected.chunks_exact(2) {
rng4.fill_bytes(&mut buf);
assert_eq!(buf[..4], chunk[0].to_le_bytes());
assert_eq!(buf[4..], chunk[1].to_le_bytes());
}
assert_eq!(rng4.get_word_pos(), expected_end);
// Test skipping behaviour with other types
let mut buf = [0u8; 32];
rng2.fill_bytes(&mut buf[..]);
assert_eq!(rng2.get_word_pos(), expected_end + 8);
rng2.fill_bytes(&mut buf[0..25]);
assert_eq!(rng2.get_word_pos(), expected_end + 15);
rng2.next_u64();
assert_eq!(rng2.get_word_pos(), expected_end + 17);
rng2.next_u32();
rng2.next_u64();
assert_eq!(rng2.get_word_pos(), expected_end + 20);
rng2.fill_bytes(&mut buf[0..1]);
assert_eq!(rng2.get_word_pos(), expected_end + 21);
}
#[test]
fn test_chacha_multiple_blocks() {
let seed = hex!("0000000001000000020000000300000004000000050000000600000007000000");
let mut rng = ChaCha20Rng::from_seed(seed);
// Store the 17*i-th 32-bit word,
// i.e., the i-th word of the i-th 16-word block
let mut results = [0u32; 16];
for i in results.iter_mut() {
*i = rng.next_u32();
for _ in 0..16 {
rng.next_u32();
}
}
let expected = [
0xf225c81a, 0x6ab1be57, 0x04d42951, 0x70858036, 0x49884684, 0x64efec72, 0x4be2d186,
0x3615b384, 0x11cfa18e, 0xd3c50049, 0x75c775f6, 0x434c6530, 0x2c5bad8f, 0x898881dc,
0x5f1c86d9, 0xc1f8e7f4,
];
assert_eq!(results, expected);
}
#[test]
fn test_chacha_true_bytes() {
let seed = [0u8; 32];
let mut rng = ChaCha20Rng::from_seed(seed);
let mut results = [0u8; 32];
rng.fill_bytes(&mut results);
let expected = hex!("76B8E0ADA0F13D90405D6AE55386BD28BDD219B8A08DED1AA836EFCC8B770DC7");
assert_eq!(results, expected);
}
#[test]
fn test_chacha_construction() {
let seed = hex!("0000000000000000010000000000000002000000000000000300000000000000");
let mut rng1 = ChaCha20Rng::from_seed(seed);
assert_eq!(rng1.next_u32(), 137206642);
assert_eq!(rng1.get_seed(), seed);
let mut rng2 = ChaCha20Rng::from_rng(&mut rng1);
assert_eq!(rng2.next_u32(), 1325750369);
}
#[test]
fn test_chacha_nonce() {
use hex_literal::hex;
// Test vector 5 from
// https://www.rfc-editor.org/rfc/rfc8439#section-2.3.2
let seed = hex!("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f");
let mut rng = ChaCha20Rng::from_seed(seed);
rng.set_stream(0x000000004a000000);
rng.set_block_pos(u64::from_le_bytes(hex!("0000000000000009")));
// The test vectors omit the first 64-bytes of the keystream
let mut discard_first_64 = [0u8; 64];
rng.fill_bytes(&mut discard_first_64);
let mut results = [0u32; 16];
for i in results.iter_mut() {
*i = rng.next_u32();
}
let expected = [
0xe4e7f110, 0x15593bd1, 0x1fdd0f50, 0xc47120a3, 0xc7f4d1c7, 0x0368c033, 0x9aaa2204,
0x4e6cd4c3, 0x466482d2, 0x09aa9f07, 0x05d7c214, 0xa2028bd9, 0xd19c12b5, 0xb94e16de,
0xe883d0cb, 0x4e3c50a2,
];
assert_eq!(results, expected);
}
#[test]
fn test_chacha_nonce_2() {
// Test vector 5 from
// https://tools.ietf.org/html/draft-nir-cfrg-chacha20-poly1305-04
// Although we do not support setting a nonce, we try it here anyway so
// we can use this test vector.
let seed = [0u8; 32];
let mut rng = ChaCha20Rng::from_seed(seed);
// 96-bit nonce in LE order is: 0,0,0,0, 0,0,0,0, 0,0,0,2
rng.set_stream(2u64 << (24 + 32));
let mut results = [0u32; 16];
for i in results.iter_mut() {
*i = rng.next_u32();
}
let expected = [
0x374dc6c2, 0x3736d58c, 0xb904e24a, 0xcd3f93ef, 0x88228b1a, 0x96a4dfb3, 0x5b76ab72,
0xc727ee54, 0x0e0e978a, 0xf3145c95, 0x1b748ea8, 0xf786c297, 0x99c28f5f, 0x628314e8,
0x398a19fa, 0x6ded1b53,
];
assert_eq!(results, expected);
}
/// Test vector 9 from https://github.com/pyca/cryptography/blob/main/vectors/cryptography_vectors/ciphers/ChaCha20/counter-overflow.txt
#[test]
fn counter_wrap_1() {
let mut rng = ChaCha20Rng::from_seed([0u8; 32]);
let block_pos = 18446744073709551615;
assert_eq!(block_pos, u64::MAX);
rng.set_block_pos(block_pos);
let mut output = [0u8; 64 * 3];
rng.fill_bytes(&mut output);
let expected = hex!(
"d7918cd8620cf832532652c04c01a553092cfb32e7b3f2f5467ae9674a2e9eec17368e"
"c8027a357c0c51e6ea747121fec45284be0f099d2b3328845607b1768976b8e0ada0f1"
"3d90405d6ae55386bd28bdd219b8a08ded1aa836efcc8b770dc7da41597c5157488d77"
"24e03fb8d84a376a43b8f41518a11cc387b669b2ee65869f07e7be5551387a98ba977c"
"732d080dcb0f29a048e3656912c6533e32ee7aed29b721769ce64e43d57133b074d839"
"d531ed1f28510afb45ace10a1f4b794d6f"
);
assert_eq!(expected, output);
}
/// Counts how many bytes were incorrect, and returns:
///
/// (`index_of_first_incorrect_word`, `num_incorrect_bytes`)
fn count_incorrect_bytes(expected: &[u8], output: &[u8]) -> (Option<usize>, u32) {
assert_eq!(expected.len(), output.len());
let mut num_incorrect_bytes = 0;
let mut index_of_first_incorrect_word = None;
expected
.iter()
.enumerate()
.zip(output.iter())
.for_each(|((i, a), b)| {
if a.ne(b) {
if index_of_first_incorrect_word.is_none() {
index_of_first_incorrect_word = Some(i / 4);
}
num_incorrect_bytes += 1;
}
});
(index_of_first_incorrect_word, num_incorrect_bytes)
}
/// Test vector 8 from https://github.com/pyca/cryptography/blob/main/vectors/cryptography_vectors/ciphers/ChaCha20/counter-overflow.txt
#[test]
fn counter_overflow_and_diagnostics() {
let mut rng = ChaCha20Rng::from_seed([0u8; 32]);
let block_pos = 4294967295;
assert_eq!(block_pos, u64::from(u32::MAX));
rng.set_block_pos(4294967295);
let mut output = [0u8; 64 * 4];
rng.fill_bytes(&mut output[..64 * 3]);
let block_before_overflow = hex!(
"ace4cd09e294d1912d4ad205d06f95d9c2f2bfcf453e8753f128765b62215f4d92c74f"
"2f626c6a640c0b1284d839ec81f1696281dafc3e684593937023b58b1d"
);
let first_block_after_overflow = hex!(
"3db41d3aa0d329285de6f225e6e24bd59c9a17006943d5c9b680e3873bdc683a581946"
"9899989690c281cd17c96159af0682b5b903468a61f50228cf09622b5a"
);
let second_block_after_overflow = hex!(
"46f0f6efee15c8f1b198cb49d92b990867905159440cc723916dc0012826981039ce17"
"66aa2542b05db3bd809ab142489d5dbfe1273e7399637b4b3213768aaa"
);
assert!(
output[..64].eq(&block_before_overflow),
"The first parblock was incorrect before overflow, indicating that ChaCha was not implemented correctly for this backend. Check the rounds() fn or the functions that it calls"
);
rng.set_block_pos(u64::from(u32::MAX) - 1);
let mut skipped_blocks = [0u8; 64 * 3];
rng.fill_bytes(&mut skipped_blocks);
rng.fill_bytes(&mut output[64 * 3..]);
output.chunks_exact(64).enumerate().skip(1).zip(&[first_block_after_overflow, second_block_after_overflow, second_block_after_overflow]).for_each(|((i, a), b)| {
let (index_of_first_incorrect_word, num_incorrect_bytes) = count_incorrect_bytes(a, b);
let msg = if num_incorrect_bytes == 0 {
"The block was correct and this will not be shown"
} else if num_incorrect_bytes > 32 {
"Most of the block was incorrect, indicating an issue with the counter using 32-bit addition towards the beginning of fn rounds()"
} else if num_incorrect_bytes <= 8 && matches!(index_of_first_incorrect_word, Some(12 | 13)) {
"When the state was added to the results/res buffer at the end of fn rounds, the counter was probably incremented in 32-bit fashion for this parblock"
} else {
// this is probably unreachable in the event of a failed assertion, but it depends on the seed
"Some of the block was incorrect"
};
assert!(a.eq(b), "PARBLOCK #{} uses incorrect counter addition\nDiagnostic = {}\nnum_incorrect_bytes = {}\nindex_of_first_incorrect_word = {:?}", i + 1, msg, num_incorrect_bytes, index_of_first_incorrect_word);
});
}
#[test]
fn test_wrapping_add() {
let mut rng = ChaCha20Rng::from_seed(KEY);
// test counter wrapping-add
rng.set_word_pos((1 << 68) - 65);
let mut output = [3u8; 1280];
rng.fill_bytes(&mut output);
assert_ne!(output, [0u8; 1280]);
assert!(rng.get_word_pos() < 2000);
assert!(rng.get_word_pos() != 0);
}
#[test]
fn test_chacha_clone_streams() {
let seed = [
0, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0, 5, 0, 0, 0, 6, 0, 0, 0, 7, 0,
0, 0,
];
let mut rng1 = ChaCha20Rng::from_seed(seed);
let mut rng2 = ChaCha20Rng::from_seed(seed);
for _ in 0..16 {
assert_eq!(rng1.next_u64(), rng2.next_u64());
}
rng1.set_stream(51);
assert_eq!(rng1.get_stream(), 51);
assert_eq!(rng2.get_stream(), 0);
let mut fill_1 = [0u8; 7];
rng1.fill_bytes(&mut fill_1);
let mut fill_2 = [0u8; 7];
rng2.fill_bytes(&mut fill_2);
assert_ne!(fill_1, fill_2);
for _ in 0..7 {
assert!(rng1.next_u64() != rng2.next_u64());
}
rng2.set_stream(51); // switch part way through block
for _ in 7..16 {
assert_ne!(rng1.next_u64(), rng2.next_u64());
}
rng1.set_stream(51);
rng2.set_stream(51);
for _ in 0..16 {
assert_eq!(rng1.next_u64(), rng2.next_u64());
}
}
#[test]
fn test_chacha_word_pos_wrap_exact() {
let mut rng = ChaCha20Rng::from_seed(Default::default());
// refilling the buffer in set_word_pos will wrap the block counter to 0
let last_block = (1 << 68) - u128::from(4 * BLOCK_WORDS);
rng.set_word_pos(last_block);
assert_eq!(rng.get_word_pos(), last_block);
}
#[test]
fn test_chacha_word_pos_wrap_excess() {
let mut rng = ChaCha20Rng::from_seed(Default::default());
// refilling the buffer in set_word_pos will wrap the block counter past 0
let last_block = (1 << 68) - u128::from(BLOCK_WORDS);
rng.set_word_pos(last_block);
assert_eq!(rng.get_word_pos(), last_block);
}
#[test]
fn test_chacha_word_pos_zero() {
let mut rng = ChaCha20Rng::from_seed(Default::default());
assert_eq!(rng.get_word_pos(), 0);
rng.set_word_pos(0);
assert_eq!(rng.get_word_pos(), 0);
}
#[test]
#[allow(trivial_casts)]
fn test_trait_objects() {
use rand_core::CryptoRng;
let seed = Default::default();
let mut rng1 = ChaCha20Rng::from_seed(seed);
let rng2 = &mut ChaCha20Rng::from_seed(seed) as &mut dyn CryptoRng;
for _ in 0..1000 {
assert_eq!(rng1.next_u64(), rng2.next_u64());
}
}
/// If this test fails, the backend may not be
/// performing 64-bit addition.
#[test]
fn counter_wrapping_64_bit_counter() {
let mut rng = ChaCha20Rng::from_seed([0u8; 32]);
// get first four blocks and word pos
let mut first_blocks = [0u8; 64 * 4];
rng.fill_bytes(&mut first_blocks);
let first_blocks_end_word_pos = rng.get_word_pos();
let first_blocks_end_block_counter = rng.get_block_pos();
// get first four blocks after wrapping
rng.set_block_pos(u64::MAX);
let mut result = [0u8; 64 * 5];
rng.fill_bytes(&mut result);
assert_eq!(first_blocks_end_word_pos, rng.get_word_pos());
assert_eq!(first_blocks_end_block_counter, rng.get_block_pos());
if first_blocks[0..64 * 4].ne(&result[64..]) {
for (i, (a, b)) in first_blocks.iter().zip(result.iter().skip(64)).enumerate() {
assert!(!a.ne(b), "i = {}\na = {}\nb = {}", i, a, b);
}
}
assert_eq!(&first_blocks[0..64 * 4], &result[64..]);
}
/// If this test fails, the backend may be doing 32-bit addition.
#[test]
fn counter_not_wrapping_at_32_bits() {
let mut rng = ChaCha20Rng::from_seed([0u8; 32]);
// get first four blocks and word pos
let mut first_blocks = [0u8; 64 * 4];
rng.fill_bytes(&mut first_blocks);
let first_blocks_end_word_pos = rng.get_word_pos();
// get first four blocks after the supposed overflow
rng.set_block_pos(u64::from(u32::MAX));
let mut result = [0u8; 64 * 5];
rng.fill_bytes(&mut result);
assert_ne!(first_blocks_end_word_pos, rng.get_word_pos());
assert_eq!(
rng.get_word_pos(),
first_blocks_end_word_pos + (1 << 32) * u128::from(BLOCK_WORDS)
);
assert_ne!(&first_blocks[0..64 * 4], &result[64..]);
}
#[test]
fn test_chacha8rng_serde_roundtrip() {
for skip_words in 0..100 {
let mut rng = ChaCha8Rng::from_seed(KEY);
rng.set_stream(STREAM);
for _ in 0..skip_words {
let _ = rng.next_u32();
}
let state = rng.serialize_state();
let mut rng2 = ChaCha8Rng::deserialize_state(&state);
for _ in 0..100 {
assert_eq!(rng.next_u32(), rng2.next_u32());
}
}
}
#[test]
fn test_chacha12rng_serde_roundtrip() {
for skip_words in 0..100 {
let mut rng = ChaCha12Rng::from_seed(KEY);
rng.set_stream(STREAM);
for _ in 0..skip_words {
let _ = rng.next_u32();
}
let state = rng.serialize_state();
let mut rng2 = ChaCha12Rng::deserialize_state(&state);
for _ in 0..100 {
assert_eq!(rng.next_u32(), rng2.next_u32());
}
}
}
#[test]
fn test_chacha20rng_serde_roundtrip() {
for skip_words in 0..100 {
let mut rng = ChaCha20Rng::from_seed(KEY);
rng.set_stream(STREAM);
for _ in 0..skip_words {
let _ = rng.next_u32();
}
let state = rng.serialize_state();
let mut rng2 = ChaCha20Rng::deserialize_state(&state);
for _ in 0..100 {
assert_eq!(rng.next_u32(), rng2.next_u32());
}
}
}
#[test]
fn test_rng_serialized_state_stability() {
const EXPECTED: SerializedRngState = hex!(
"0102030405060708090A0B0C0D0E0F10"
"1112131415161718191A1B1C1D1E1F20"
"F7F6F5F4F3F2F1F06400000000000000"
"00"
);
let mut rng = ChaCha8Rng::from_seed(KEY);
rng.set_stream(STREAM);
for _ in 0..100 {
let _ = rng.next_u32();
}
let state = rng.serialize_state();
assert_eq!(state, EXPECTED);
let mut rng = ChaCha12Rng::from_seed(KEY);
rng.set_stream(STREAM);
for _ in 0..100 {
let _ = rng.next_u32();
}
let state = rng.serialize_state();
assert_eq!(state, EXPECTED);
let mut rng = ChaCha20Rng::from_seed(KEY);
rng.set_stream(STREAM);
for _ in 0..100 {
let _ = rng.next_u32();
}
let state = rng.serialize_state();
assert_eq!(state, EXPECTED);
}