diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
commit | 26a029d407be480d791972afb5975cf62c9360a6 (patch) | |
tree | f435a8308119effd964b339f76abb83a57c29483 /third_party/rust/sha3 | |
parent | Initial commit. (diff) | |
download | firefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz firefox-26a029d407be480d791972afb5975cf62c9360a6.zip |
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/sha3')
29 files changed, 1607 insertions, 0 deletions
diff --git a/third_party/rust/sha3/.cargo-checksum.json b/third_party/rust/sha3/.cargo-checksum.json new file mode 100644 index 0000000000..2ec4a7d641 --- /dev/null +++ b/third_party/rust/sha3/.cargo-checksum.json @@ -0,0 +1 @@ +{"files":{"CHANGELOG.md":"a4c9f43db614cd2cd21016252916419331f98f2a16325cf0146d799371cf5529","Cargo.toml":"52e60224e4ce23ad82e4efe367b260847f3761326e04cfb86adc9be77492c974","LICENSE-APACHE":"a9040321c3712d8fd0b09cf52b17445de04a23a10165049ae187cd39e5c86be5","LICENSE-MIT":"f18f6229547ab07f0b7b3e1f83acad8bb436f5f4c95a8a98b44f876caa00f04e","README.md":"fcf7e481b90c8792222a69388c17c60710004eb1e21d8dfdf2a7fd66ac391e54","benches/mod.rs":"09ea0a3bf6630c8828ee190b7ee31abcc685cf4c1c11fd19d9d50620d82ed6b1","src/lib.rs":"2344ba106bc83a88d509f97d41a36a95f33caa0a6abe467857b3dd4951b396e5","src/macros.rs":"f2eee02312aaeb4bede40666459d6d46a4c685c53bc3bfa69a9fb1ebfb934643","src/state.rs":"7c3f074c7f707312d0e5952fae65006ad7cd029b42237f974ad80a2656b862cf","tests/aliases.rs":"1267b02be0d6a0c8cd10159ff5930c168f50f82debc61bb8b38269ea67a40cfa","tests/cshake.rs":"4bef5076b88398fa2e370e21fb6710c1925665cb0872fa8be9779c8c0db3a2af","tests/data/cshake128.blb":"8e351e2e27c4a9650818ff7137f1aee0370d59c603cb83e23c8edceb78907709","tests/data/cshake256.blb":"9ff795a294dfe075e4d1b9d04897ab9dc7d20f625eab2dd10f5c2930d756dbf4","tests/data/keccak_224.blb":"d5c811df0e6b8a186c848c1e772abd61fb1fccca10f0c0aea8d870796b235316","tests/data/keccak_256.blb":"a0113d2d8097f2d6270483bd77a2e065d6eb3a738f18dbbdc84842bf9bc5600a","tests/data/keccak_256_full.blb":"39358fd8ae92d36154d71b31ca0027a3b6601ff49c05fd4d89839f1b99e1103d","tests/data/keccak_384.blb":"25a3844fae90137a7877a0eadafe400300071f26d7bdb7b2fc01967e3f2760fa","tests/data/keccak_512.blb":"ab9e7777c154f6045937ed8b4a372f56622883d6e9973288656a61a636a2c4c2","tests/data/sha3_224.blb":"9c6676da06e149cf2f71be4b4554d042f7c5fa6d5f43696a30ca8d6747c85a23","tests/data/sha3_256.blb":"00e7834e0abc16614b772a0c6245a29c16807e79c54aa153b008f11cd26268d6","tests/data/sha3_384.blb":"cd4d9c607c5518a0274415b89512ea4cb9be3fd25edf8269aef566d904b797e9","tests/data/sha3_512.blb":"f551f332df7fc50b313544aadad361ce3ce5fd91f21259b93c64b35157904be3","tests/data/shake128.blb":"5900de7f0e09bfd290bee04b183f69fef8407a022491f5f6018cad737de53e4a","tests/data/shake256.blb":"4b65535c6e28e34f840df71b6dd0d99f51bac13d191e3769861e8560bf9d2373","tests/data/turboshake128.blb":"ca66fd86b6d21bfc49da37a05e2a3c4cb4a08c2ef2cd8a66703099f8a8fc7235","tests/data/turboshake256.blb":"6c06855c1900bd2d281eed7b59d07ba25e40af395b4be5ca53776562c9b77865","tests/mod.rs":"891eb4032ff6e3fb516b9ad7b655d5ef048d432335b9da75b4af07c902a8f839","tests/turboshake.rs":"bb90605141d7d5bd84f8c1ed35c9e9e9a037438f315cca08ba1ce24e19962a7a"},"package":"75872d278a8f37ef87fa0ddbda7802605cb18344497949862c0d4dcb291eba60"}
\ No newline at end of file diff --git a/third_party/rust/sha3/CHANGELOG.md b/third_party/rust/sha3/CHANGELOG.md new file mode 100644 index 0000000000..9992ac31e7 --- /dev/null +++ b/third_party/rust/sha3/CHANGELOG.md @@ -0,0 +1,118 @@ +# 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.8 (2023-04-08) +### Fixed +- Performance regression: now uses `p1600` fn ([#472]) + +[#472]: https://github.com/RustCrypto/hashes/pull/472 + +## 0.10.7 (2023-04-11) +### Added +- `asm` feature ([#437]) +- TurboSHAKE ([#458]) + +[#437]: https://github.com/RustCrypto/hashes/pull/437 +[#458]: https://github.com/RustCrypto/hashes/pull/458 + +## 0.10.6 (2022-10-19) +### Fixed +- XOF reader type aliases ([#427]) + +[#427]: https://github.com/RustCrypto/hashes/pull/427 + +## 0.10.5 (2022-09-16) +### Added +- Feature-gated OID support ([#405]) + +[#405]: https://github.com/RustCrypto/hashes/pull/405 + +## 0.10.4 (2022-09-02) +### Fixed +- MSRV issue which was not resolved by v0.10.3 ([#401]) + +[#401]: https://github.com/RustCrypto/hashes/pull/401 + +## 0.10.3 (2022-09-02) +### Fixed +- MSRV issue caused by publishing v0.10.2 using a buggy Nightly toolchain ([#399]) + +[#399]: https://github.com/RustCrypto/hashes/pull/399 + +## 0.10.2 (2022-07-30) +### Added +- cSHAKE128 and cSHAKE256 implementations ([#355]) + +[#355]: https://github.com/RustCrypto/hashes/pull/355 + +## 0.10.1 (2022-02-17) +### Fixed +- Minimal versions build ([#363]) + +[#363]: https://github.com/RustCrypto/hashes/pull/363 + +## 0.10.0 (2021-12-07) +### Changed +- Update to `digest` v0.10 ([#217]) + +[#217]: https://github.com/RustCrypto/hashes/pull/217 + +## 0.9.1 (2020-06-28) +### Changed +- Update to `block-buffer` v0.9 ([#164]) +- Update to `opaque-debug` v0.3 ([#168]) + +[#164]: https://github.com/RustCrypto/hashes/pull/164 +[#168]: https://github.com/RustCrypto/hashes/pull/168 + +## 0.9.0 (2020-06-10) +### Changed +- Update to `digest` v0.9 release; MSRV 1.41+ ([#155]) +- Use new `*Dirty` traits from the `digest` crate ([#153]) +- Bump `block-buffer` to v0.8 release ([#151]) +- Rename `*result*` to `finalize` ([#148]) +- Upgrade to Rust 2018 edition ([#134]) + +[#155]: https://github.com/RustCrypto/hashes/pull/155 +[#153]: https://github.com/RustCrypto/hashes/pull/153 +[#151]: https://github.com/RustCrypto/hashes/pull/151 +[#148]: https://github.com/RustCrypto/hashes/pull/148 +[#134]: https://github.com/RustCrypto/hashes/pull/133 + +## 0.8.2 (2019-04-24) + +## 0.8.1 (2018-11-14) + +## 0.8.0 (2018-10-02) + +## 0.7.3 (2018-03-27) + +## 0.7.2 (2017-11-18) + +## 0.7.1 (2017-11-17) + +## 0.7.0 (2017-11-15) + +## 0.6.0 (2017-06-12) + +## 0.5.3 (2017-05-31) + +## 0.5.2 (2017-05-30) + +## 0.5.1 (2017-05-02) + +## 0.5.0 (2017-04-06) + +## 0.4.1 (2017-01-20) + +## 0.4.0 (2016-12-25) + +## 0.3.0 (2016-11-17) + +## 0.2.0 (2016-10-14) + +## 0.1.0 (2016-10-13) diff --git a/third_party/rust/sha3/Cargo.toml b/third_party/rust/sha3/Cargo.toml new file mode 100644 index 0000000000..a2a9e0b6fc --- /dev/null +++ b/third_party/rust/sha3/Cargo.toml @@ -0,0 +1,56 @@ +# 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 = "2018" +name = "sha3" +version = "0.10.8" +authors = ["RustCrypto Developers"] +description = """ +Pure Rust implementation of SHA-3, a family of Keccak-based hash functions +including the SHAKE family of eXtendable-Output Functions (XOFs), as well as +the accelerated variant TurboSHAKE +""" +documentation = "https://docs.rs/sha3" +readme = "README.md" +keywords = [ + "crypto", + "sha3", + "keccak", + "hash", + "digest", +] +categories = [ + "cryptography", + "no-std", +] +license = "MIT OR Apache-2.0" +repository = "https://github.com/RustCrypto/hashes" + +[dependencies.digest] +version = "0.10.4" + +[dependencies.keccak] +version = "0.1.4" + +[dev-dependencies.digest] +version = "0.10.4" +features = ["dev"] + +[dev-dependencies.hex-literal] +version = "0.2.2" + +[features] +asm = ["keccak/asm"] +default = ["std"] +oid = ["digest/oid"] +reset = [] +std = ["digest/std"] diff --git a/third_party/rust/sha3/LICENSE-APACHE b/third_party/rust/sha3/LICENSE-APACHE new file mode 100644 index 0000000000..78173fa2e7 --- /dev/null +++ b/third_party/rust/sha3/LICENSE-APACHE @@ -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. diff --git a/third_party/rust/sha3/LICENSE-MIT b/third_party/rust/sha3/LICENSE-MIT new file mode 100644 index 0000000000..c7ee28b0fe --- /dev/null +++ b/third_party/rust/sha3/LICENSE-MIT @@ -0,0 +1,28 @@ +Copyright (c) 2006-2009 Graydon Hoare +Copyright (c) 2009-2013 Mozilla Foundation +Copyright (c) 2014 Sébastien Martini +Copyright (c) 2016-2023 Artyom Pavlov, Marek Kotewicz + +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. diff --git a/third_party/rust/sha3/README.md b/third_party/rust/sha3/README.md new file mode 100644 index 0000000000..caef5d594e --- /dev/null +++ b/third_party/rust/sha3/README.md @@ -0,0 +1,56 @@ +# RustCrypto: SHA-3 + +[![crate][crate-image]][crate-link] +[![Docs][docs-image]][docs-link] +![Apache2/MIT licensed][license-image] +![Rust Version][rustc-image] +[![Project Chat][chat-image]][chat-link] +[![Build Status][build-image]][build-link] + +Pure Rust implementation of the [SHA-3 (Keccak) hash function][1]. + +[Documentation][docs-link] + +## Minimum Supported Rust Version + +Rust **1.41** or higher. + +Minimum supported Rust version can be changed in the future, but it will be +done with a minor version bump. + +## SemVer Policy + +- All on-by-default features of this library are covered by SemVer +- MSRV is considered exempt from SemVer as noted above + +## 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/sha3.svg +[crate-link]: https://crates.io/crates/sha3 +[docs-image]: https://docs.rs/sha3/badge.svg +[docs-link]: https://docs.rs/sha3/ +[license-image]: https://img.shields.io/badge/license-Apache2.0/MIT-blue.svg +[rustc-image]: https://img.shields.io/badge/rustc-1.41+-blue.svg +[chat-image]: https://img.shields.io/badge/zulip-join_chat-blue.svg +[chat-link]: https://rustcrypto.zulipchat.com/#narrow/stream/260041-hashes +[build-image]: https://github.com/RustCrypto/hashes/workflows/sha3/badge.svg?branch=master +[build-link]: https://github.com/RustCrypto/hashes/actions?query=workflow%3Asha3 + +[//]: # (general links) + +[1]: https://en.wikipedia.org/wiki/SHA-3 diff --git a/third_party/rust/sha3/benches/mod.rs b/third_party/rust/sha3/benches/mod.rs new file mode 100644 index 0000000000..db29405169 --- /dev/null +++ b/third_party/rust/sha3/benches/mod.rs @@ -0,0 +1,54 @@ +#![feature(test)] +extern crate test; + +use digest::bench_update; +use sha3::{Sha3_224, Sha3_256, Sha3_384, Sha3_512, Shake128, Shake256}; +use test::Bencher; + +bench_update!( + Sha3_224::default(); + sha3_224_10 10; + sha3_224_100 100; + sha3_224_1000 1000; + sha3_224_10000 10000; +); + +bench_update!( + Sha3_256::default(); + sha3_256_10 10; + sha3_265_100 100; + sha3_256_1000 1000; + sha3_256_10000 10000; +); + +bench_update!( + Sha3_384::default(); + sha3_384_10 10; + sha3_384_100 100; + sha3_384_1000 1000; + sha3_384_10000 10000; +); + +bench_update!( + Sha3_512::default(); + sha3_512_10 10; + sha3_512_100 100; + sha3_512_1000 1000; + sha3_512_10000 10000; +); + +bench_update!( + Shake128::default(); + shake128_10 10; + shake128_100 100; + shake128_1000 1000; + shake128_10000 10000; +); + +bench_update!( + Shake256::default(); + shake256_10 10; + shake256_100 100; + shake256_1000 1000; + shake256_10000 10000; +); diff --git a/third_party/rust/sha3/src/lib.rs b/third_party/rust/sha3/src/lib.rs new file mode 100644 index 0000000000..0f87ee580b --- /dev/null +++ b/third_party/rust/sha3/src/lib.rs @@ -0,0 +1,217 @@ +//! An implementation of the [SHA-3][1] cryptographic hash algorithms. +//! +//! There are 6 standard algorithms specified in the SHA-3 standard: +//! +//! * `SHA3-224` +//! * `SHA3-256` +//! * `SHA3-384` +//! * `SHA3-512` +//! * `SHAKE128`, an extendable output function (XOF) +//! * `SHAKE256`, an extendable output function (XOF) +//! * `Keccak224`, `Keccak256`, `Keccak384`, `Keccak512` (NIST submission +//! without padding changes) +//! +//! Additionally supports `TurboSHAKE`. +//! +//! # Examples +//! +//! Output size of SHA3-256 is fixed, so its functionality is usually +//! accessed via the `Digest` trait: +//! +//! ``` +//! use hex_literal::hex; +//! use sha3::{Digest, Sha3_256}; +//! +//! // create a SHA3-256 object +//! let mut hasher = Sha3_256::new(); +//! +//! // write input message +//! hasher.update(b"abc"); +//! +//! // read hash digest +//! let result = hasher.finalize(); +//! +//! assert_eq!(result[..], hex!(" +//! 3a985da74fe225b2045c172d6bd390bd855f086e3e9d525b46bfe24511431532 +//! ")[..]); +//! ``` +//! +//! SHAKE functions have an extendable output, so finalization method returns +//! XOF reader from which results of arbitrary length can be read. Note that +//! these functions do not implement `Digest`, so lower-level traits have to +//! be imported: +//! +//! ``` +//! use sha3::{Shake128, digest::{Update, ExtendableOutput, XofReader}}; +//! use hex_literal::hex; +//! +//! let mut hasher = Shake128::default(); +//! hasher.update(b"abc"); +//! let mut reader = hasher.finalize_xof(); +//! let mut res1 = [0u8; 10]; +//! reader.read(&mut res1); +//! assert_eq!(res1, hex!("5881092dd818bf5cf8a3")); +//! ``` +//! +//! Also see [RustCrypto/hashes][2] readme. +//! +//! [1]: https://en.wikipedia.org/wiki/SHA-3 +//! [2]: https://github.com/RustCrypto/hashes + +#![no_std] +#![doc( + html_logo_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg", + html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg" +)] +#![forbid(unsafe_code)] +#![warn(missing_docs, rust_2018_idioms)] + +pub use digest::{self, Digest}; + +use core::fmt; +#[cfg(feature = "oid")] +use digest::const_oid::{AssociatedOid, ObjectIdentifier}; +use digest::{ + block_buffer::Eager, + consts::{U104, U136, U144, U168, U200, U28, U32, U48, U64, U72}, + core_api::{ + AlgorithmName, Block, BlockSizeUser, Buffer, BufferKindUser, CoreWrapper, + ExtendableOutputCore, FixedOutputCore, OutputSizeUser, Reset, UpdateCore, XofReaderCore, + XofReaderCoreWrapper, + }, + generic_array::typenum::Unsigned, + HashMarker, Output, +}; + +#[macro_use] +mod macros; +mod state; + +use crate::state::Sha3State; + +// Paddings +const KECCAK: u8 = 0x01; +const SHA3: u8 = 0x06; +const SHAKE: u8 = 0x1f; +const CSHAKE: u8 = 0x4; + +// Round counts +const TURBO_SHAKE_ROUND_COUNT: usize = 12; + +impl_sha3!(Keccak224Core, Keccak224, U28, U144, KECCAK, "Keccak-224"); +impl_sha3!(Keccak256Core, Keccak256, U32, U136, KECCAK, "Keccak-256"); +impl_sha3!(Keccak384Core, Keccak384, U48, U104, KECCAK, "Keccak-384"); +impl_sha3!(Keccak512Core, Keccak512, U64, U72, KECCAK, "Keccak-512"); + +impl_sha3!( + Keccak256FullCore, + Keccak256Full, + U200, + U136, + KECCAK, + "SHA-3 CryptoNight variant", +); + +impl_sha3!( + Sha3_224Core, + Sha3_224, + U28, + U144, + SHA3, + "SHA-3-224", + "2.16.840.1.101.3.4.2.7", +); +impl_sha3!( + Sha3_256Core, + Sha3_256, + U32, + U136, + SHA3, + "SHA-3-256", + "2.16.840.1.101.3.4.2.8", +); +impl_sha3!( + Sha3_384Core, + Sha3_384, + U48, + U104, + SHA3, + "SHA-3-384", + "2.16.840.1.101.3.4.2.9", +); +impl_sha3!( + Sha3_512Core, + Sha3_512, + U64, + U72, + SHA3, + "SHA-3-512", + "2.16.840.1.101.3.4.2.10", +); + +impl_shake!( + Shake128Core, + Shake128, + Shake128ReaderCore, + Shake128Reader, + U168, + SHAKE, + "SHAKE128", + "2.16.840.1.101.3.4.2.11", +); +impl_shake!( + Shake256Core, + Shake256, + Shake256ReaderCore, + Shake256Reader, + U136, + SHAKE, + "SHAKE256", + "2.16.840.1.101.3.4.2.11", +); + +impl_turbo_shake!( + TurboShake128Core, + TurboShake128, + TurboShake128ReaderCore, + TurboShake128Reader, + U168, + "TurboSHAKE128", +); +impl_turbo_shake!( + TurboShake256Core, + TurboShake256, + TurboShake256ReaderCore, + TurboShake256Reader, + U136, + "TurboSHAKE256", +); + +impl_cshake!( + CShake128Core, + CShake128, + CShake128ReaderCore, + CShake128Reader, + U168, + SHAKE, + CSHAKE, + "CSHAKE128", +); +impl_cshake!( + CShake256Core, + CShake256, + CShake256ReaderCore, + CShake256Reader, + U136, + SHAKE, + CSHAKE, + "CSHAKE256", +); + +#[inline(always)] +pub(crate) fn left_encode(val: u64, b: &mut [u8; 9]) -> &[u8] { + b[1..].copy_from_slice(&val.to_be_bytes()); + let i = b[1..8].iter().take_while(|&&a| a == 0).count(); + b[i] = (8 - i) as u8; + &b[i..] +} diff --git a/third_party/rust/sha3/src/macros.rs b/third_party/rust/sha3/src/macros.rs new file mode 100644 index 0000000000..f262913059 --- /dev/null +++ b/third_party/rust/sha3/src/macros.rs @@ -0,0 +1,529 @@ +macro_rules! impl_sha3 { + ( + $name:ident, $full_name:ident, $output_size:ident, + $rate:ident, $pad:expr, $alg_name:expr $(,)? + ) => { + #[doc = "Core "] + #[doc = $alg_name] + #[doc = " hasher state."] + #[derive(Clone)] + #[allow(non_camel_case_types)] + pub struct $name { + state: Sha3State, + } + + impl HashMarker for $name {} + + impl BlockSizeUser for $name { + type BlockSize = $rate; + } + + impl BufferKindUser for $name { + type BufferKind = Eager; + } + + impl OutputSizeUser for $name { + type OutputSize = $output_size; + } + + impl UpdateCore for $name { + #[inline] + fn update_blocks(&mut self, blocks: &[Block<Self>]) { + for block in blocks { + self.state.absorb_block(block) + } + } + } + + impl FixedOutputCore for $name { + #[inline] + fn finalize_fixed_core(&mut self, buffer: &mut Buffer<Self>, out: &mut Output<Self>) { + let pos = buffer.get_pos(); + let block = buffer.pad_with_zeros(); + block[pos] = $pad; + let n = block.len(); + block[n - 1] |= 0x80; + + self.state.absorb_block(block); + + self.state.as_bytes(out); + } + } + + impl Default for $name { + #[inline] + fn default() -> Self { + Self { + state: Default::default(), + } + } + } + + impl Reset for $name { + #[inline] + fn reset(&mut self) { + *self = Default::default(); + } + } + + impl AlgorithmName for $name { + fn write_alg_name(f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(stringify!($full_name)) + } + } + + impl fmt::Debug for $name { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(concat!(stringify!($name), " { ... }")) + } + } + + #[doc = $alg_name] + #[doc = " hasher state."] + pub type $full_name = CoreWrapper<$name>; + }; + ( + $name:ident, $full_name:ident, $output_size:ident, + $rate:ident, $pad:expr, $alg_name:expr, $oid:literal $(,)? + ) => { + impl_sha3!($name, $full_name, $output_size, $rate, $pad, $alg_name); + + #[cfg(feature = "oid")] + #[cfg_attr(docsrs, doc(cfg(feature = "oid")))] + impl AssociatedOid for $name { + const OID: ObjectIdentifier = ObjectIdentifier::new_unwrap($oid); + } + }; +} + +macro_rules! impl_shake { + ( + $name:ident, $full_name:ident, $reader:ident, $reader_full:ident, + $rate:ident, $pad:expr, $alg_name:expr $(,)? + ) => { + #[doc = "Core "] + #[doc = $alg_name] + #[doc = " hasher state."] + #[derive(Clone)] + #[allow(non_camel_case_types)] + pub struct $name { + state: Sha3State, + } + + impl HashMarker for $name {} + + impl BlockSizeUser for $name { + type BlockSize = $rate; + } + + impl BufferKindUser for $name { + type BufferKind = Eager; + } + + impl UpdateCore for $name { + #[inline] + fn update_blocks(&mut self, blocks: &[Block<Self>]) { + for block in blocks { + self.state.absorb_block(block) + } + } + } + + impl ExtendableOutputCore for $name { + type ReaderCore = $reader; + + #[inline] + fn finalize_xof_core(&mut self, buffer: &mut Buffer<Self>) -> Self::ReaderCore { + let pos = buffer.get_pos(); + let block = buffer.pad_with_zeros(); + block[pos] = $pad; + let n = block.len(); + block[n - 1] |= 0x80; + + self.state.absorb_block(block); + $reader { + state: self.state.clone(), + } + } + } + + impl Default for $name { + #[inline] + fn default() -> Self { + Self { + state: Default::default(), + } + } + } + + impl Reset for $name { + #[inline] + fn reset(&mut self) { + *self = Default::default(); + } + } + + impl AlgorithmName for $name { + fn write_alg_name(f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(stringify!($full_name)) + } + } + + impl fmt::Debug for $name { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(concat!(stringify!($name), " { ... }")) + } + } + + #[doc = "Core "] + #[doc = $alg_name] + #[doc = " reader state."] + #[derive(Clone)] + #[allow(non_camel_case_types)] + pub struct $reader { + state: Sha3State, + } + + impl BlockSizeUser for $reader { + type BlockSize = $rate; + } + + impl XofReaderCore for $reader { + #[inline] + fn read_block(&mut self) -> Block<Self> { + let mut block = Block::<Self>::default(); + self.state.as_bytes(&mut block); + self.state.permute(); + block + } + } + + #[doc = $alg_name] + #[doc = " hasher state."] + pub type $full_name = CoreWrapper<$name>; + + #[doc = $alg_name] + #[doc = " reader state."] + pub type $reader_full = XofReaderCoreWrapper<$reader>; + }; + ( + $name:ident, $full_name:ident, $reader:ident, $reader_full:ident, + $rate:ident, $pad:expr, $alg_name:expr, $oid:literal $(,)? + ) => { + impl_shake!( + $name, + $full_name, + $reader, + $reader_full, + $rate, + $pad, + $alg_name + ); + + #[cfg(feature = "oid")] + #[cfg_attr(docsrs, doc(cfg(feature = "oid")))] + impl AssociatedOid for $name { + const OID: ObjectIdentifier = ObjectIdentifier::new_unwrap($oid); + } + }; +} + +macro_rules! impl_turbo_shake { + ( + $name:ident, $full_name:ident, $reader:ident, $reader_full:ident, + $rate:ident, $alg_name:expr $(,)? + ) => { + #[doc = "Core "] + #[doc = $alg_name] + #[doc = " hasher state."] + #[derive(Clone)] + #[allow(non_camel_case_types)] + pub struct $name { + domain_separation: u8, + state: Sha3State, + } + + impl $name { + /// Creates a new TurboSHAKE instance with the given domain separation. + /// Note that the domain separation needs to be a byte with a value in + /// the range [0x01, . . . , 0x7F] + pub fn new(domain_separation: u8) -> Self { + assert!((0x01..=0x7F).contains(&domain_separation)); + Self { + domain_separation, + state: Sha3State::new(TURBO_SHAKE_ROUND_COUNT), + } + } + } + + impl HashMarker for $name {} + + impl BlockSizeUser for $name { + type BlockSize = $rate; + } + + impl BufferKindUser for $name { + type BufferKind = Eager; + } + + impl UpdateCore for $name { + #[inline] + fn update_blocks(&mut self, blocks: &[Block<Self>]) { + for block in blocks { + self.state.absorb_block(block) + } + } + } + + impl ExtendableOutputCore for $name { + type ReaderCore = $reader; + + #[inline] + fn finalize_xof_core(&mut self, buffer: &mut Buffer<Self>) -> Self::ReaderCore { + let pos = buffer.get_pos(); + let block = buffer.pad_with_zeros(); + block[pos] = self.domain_separation; + let n = block.len(); + block[n - 1] |= 0x80; + + self.state.absorb_block(block); + $reader { + state: self.state.clone(), + } + } + } + + impl Reset for $name { + #[inline] + fn reset(&mut self) { + *self = Self::new(self.domain_separation); + } + } + + impl AlgorithmName for $name { + fn write_alg_name(f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(stringify!($full_name)) + } + } + + impl fmt::Debug for $name { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(concat!(stringify!($name), " { ... }")) + } + } + + #[doc = "Core "] + #[doc = $alg_name] + #[doc = " reader state."] + #[derive(Clone)] + #[allow(non_camel_case_types)] + pub struct $reader { + state: Sha3State, + } + + impl BlockSizeUser for $reader { + type BlockSize = $rate; + } + + impl XofReaderCore for $reader { + #[inline] + fn read_block(&mut self) -> Block<Self> { + let mut block = Block::<Self>::default(); + self.state.as_bytes(&mut block); + self.state.permute(); + block + } + } + + #[doc = $alg_name] + #[doc = " hasher state."] + pub type $full_name = CoreWrapper<$name>; + + #[doc = $alg_name] + #[doc = " reader state."] + pub type $reader_full = XofReaderCoreWrapper<$reader>; + }; + ( + $name:ident, $full_name:ident, $reader:ident, $reader_full:ident, + $rate:ident, $alg_name:expr, $oid:literal $(,)? + ) => { + impl_turbo_shake!($name, $full_name, $reader, $reader_full, $rate, $alg_name); + + #[cfg(feature = "oid")] + #[cfg_attr(docsrs, doc(cfg(feature = "oid")))] + impl AssociatedOid for $name { + const OID: ObjectIdentifier = ObjectIdentifier::new_unwrap($oid); + } + }; +} + +macro_rules! impl_cshake { + ( + $name:ident, $full_name:ident, $reader:ident, $reader_full:ident, + $rate:ident, $shake_pad:expr, $cshake_pad:expr, $alg_name:expr, + ) => { + #[doc = "Core "] + #[doc = $alg_name] + #[doc = " hasher state."] + #[derive(Clone)] + #[allow(non_camel_case_types)] + pub struct $name { + padding: u8, + state: Sha3State, + #[cfg(feature = "reset")] + initial_state: Sha3State, + } + + impl $name { + /// Creates a new CSHAKE instance with the given customization. + pub fn new(customization: &[u8]) -> Self { + Self::new_with_function_name(&[], customization) + } + + /// Creates a new CSHAKE instance with the given function name and customization. + /// Note that the function name is intended for use by NIST and should only be set to + /// values defined by NIST. You probably don't need to use this function. + pub fn new_with_function_name(function_name: &[u8], customization: &[u8]) -> Self { + let mut state = Sha3State::default(); + if function_name.is_empty() && customization.is_empty() { + return Self { + padding: $shake_pad, + state: state.clone(), + #[cfg(feature = "reset")] + initial_state: state, + }; + } + + let mut buffer = Buffer::<Self>::default(); + let mut b = [0u8; 9]; + buffer.digest_blocks(left_encode($rate::to_u64(), &mut b), |blocks| { + for block in blocks { + state.absorb_block(block); + } + }); + buffer.digest_blocks( + left_encode((function_name.len() * 8) as u64, &mut b), + |blocks| { + for block in blocks { + state.absorb_block(block); + } + }, + ); + buffer.digest_blocks(function_name, |blocks| { + for block in blocks { + state.absorb_block(block); + } + }); + buffer.digest_blocks( + left_encode((customization.len() * 8) as u64, &mut b), + |blocks| { + for block in blocks { + state.absorb_block(block); + } + }, + ); + buffer.digest_blocks(customization, |blocks| { + for block in blocks { + state.absorb_block(block); + } + }); + state.absorb_block(buffer.pad_with_zeros()); + + Self { + padding: $cshake_pad, + state: state.clone(), + #[cfg(feature = "reset")] + initial_state: state, + } + } + } + + impl HashMarker for $name {} + + impl BlockSizeUser for $name { + type BlockSize = $rate; + } + + impl BufferKindUser for $name { + type BufferKind = Eager; + } + + impl UpdateCore for $name { + #[inline] + fn update_blocks(&mut self, blocks: &[Block<Self>]) { + for block in blocks { + self.state.absorb_block(block) + } + } + } + + impl ExtendableOutputCore for $name { + type ReaderCore = $reader; + + #[inline] + fn finalize_xof_core(&mut self, buffer: &mut Buffer<Self>) -> Self::ReaderCore { + let pos = buffer.get_pos(); + let block = buffer.pad_with_zeros(); + block[pos] = self.padding; + let n = block.len(); + block[n - 1] |= 0x80; + + self.state.absorb_block(block); + $reader { + state: self.state.clone(), + } + } + } + + #[cfg(feature = "reset")] + impl Reset for $name { + #[inline] + fn reset(&mut self) { + self.state = self.initial_state.clone(); + } + } + + impl AlgorithmName for $name { + fn write_alg_name(f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(stringify!($full_name)) + } + } + + impl fmt::Debug for $name { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(concat!(stringify!($name), " { ... }")) + } + } + + #[doc = "Core "] + #[doc = $alg_name] + #[doc = " reader state."] + #[derive(Clone)] + #[allow(non_camel_case_types)] + pub struct $reader { + state: Sha3State, + } + + impl BlockSizeUser for $reader { + type BlockSize = $rate; + } + + impl XofReaderCore for $reader { + #[inline] + fn read_block(&mut self) -> Block<Self> { + let mut block = Block::<Self>::default(); + self.state.as_bytes(&mut block); + self.state.permute(); + block + } + } + + #[doc = $alg_name] + #[doc = " hasher state."] + pub type $full_name = CoreWrapper<$name>; + + #[doc = $alg_name] + #[doc = " reader state."] + pub type $reader_full = XofReaderCoreWrapper<$reader>; + }; +} diff --git a/third_party/rust/sha3/src/state.rs b/third_party/rust/sha3/src/state.rs new file mode 100644 index 0000000000..1ba9f11bd1 --- /dev/null +++ b/third_party/rust/sha3/src/state.rs @@ -0,0 +1,51 @@ +use core::convert::TryInto; + +const PLEN: usize = 25; +const DEFAULT_ROUND_COUNT: usize = 24; + +#[derive(Clone)] +pub(crate) struct Sha3State { + pub state: [u64; PLEN], + round_count: usize, +} + +impl Default for Sha3State { + fn default() -> Self { + Self { + state: [0u64; PLEN], + round_count: DEFAULT_ROUND_COUNT, + } + } +} + +impl Sha3State { + pub(crate) fn new(round_count: usize) -> Self { + Self { + state: [0u64; PLEN], + round_count, + } + } + + #[inline(always)] + pub(crate) fn absorb_block(&mut self, block: &[u8]) { + debug_assert_eq!(block.len() % 8, 0); + + for (b, s) in block.chunks_exact(8).zip(self.state.iter_mut()) { + *s ^= u64::from_le_bytes(b.try_into().unwrap()); + } + + keccak::p1600(&mut self.state, self.round_count); + } + + #[inline(always)] + pub(crate) fn as_bytes(&self, out: &mut [u8]) { + for (o, s) in out.chunks_mut(8).zip(self.state.iter()) { + o.copy_from_slice(&s.to_le_bytes()[..o.len()]); + } + } + + #[inline(always)] + pub(crate) fn permute(&mut self) { + keccak::p1600(&mut self.state, self.round_count); + } +} diff --git a/third_party/rust/sha3/tests/aliases.rs b/third_party/rust/sha3/tests/aliases.rs new file mode 100644 index 0000000000..9670bb54a8 --- /dev/null +++ b/third_party/rust/sha3/tests/aliases.rs @@ -0,0 +1,19 @@ +//! Checks that we defined reader type aliases correctly +#![allow(dead_code)] +use sha3::digest::ExtendableOutput; + +fn shake128(v: sha3::Shake128) -> sha3::Shake128Reader { + v.finalize_xof() +} + +fn shake256(v: sha3::Shake256) -> sha3::Shake256Reader { + v.finalize_xof() +} + +fn cshake128(v: sha3::CShake128) -> sha3::CShake128Reader { + v.finalize_xof() +} + +fn cshake256(v: sha3::CShake256) -> sha3::CShake256Reader { + v.finalize_xof() +} diff --git a/third_party/rust/sha3/tests/cshake.rs b/third_party/rust/sha3/tests/cshake.rs new file mode 100644 index 0000000000..0ba62e2c96 --- /dev/null +++ b/third_party/rust/sha3/tests/cshake.rs @@ -0,0 +1,145 @@ +use core::fmt::Debug; +use digest::ExtendableOutput; +#[cfg(feature = "reset")] +use digest::ExtendableOutputReset; + +#[cfg(feature = "reset")] +pub(crate) fn cshake_reset_test<D, F>(input: &[u8], output: &[u8], new: F) -> Option<&'static str> +where + D: ExtendableOutputReset + Debug + Clone, + F: Fn() -> D, +{ + let mut hasher = new(); + let mut buf = [0u8; 1024]; + let buf = &mut buf[..output.len()]; + // Test that it works when accepting the message all at once + hasher.update(input); + let mut hasher2 = hasher.clone(); + hasher.finalize_xof_into(buf); + if buf != output { + return Some("whole message"); + } + buf.iter_mut().for_each(|b| *b = 0); + + // Test if reset works correctly + hasher2.reset(); + hasher2.update(input); + hasher2.finalize_xof_reset_into(buf); + if buf != output { + return Some("whole message after reset"); + } + buf.iter_mut().for_each(|b| *b = 0); + + // Test that it works when accepting the message in chunks + for n in 1..core::cmp::min(17, input.len()) { + let mut hasher = new(); + for chunk in input.chunks(n) { + hasher.update(chunk); + hasher2.update(chunk); + } + hasher.finalize_xof_into(buf); + if buf != output { + return Some("message in chunks"); + } + buf.iter_mut().for_each(|b| *b = 0); + + hasher2.finalize_xof_reset_into(buf); + if buf != output { + return Some("message in chunks"); + } + buf.iter_mut().for_each(|b| *b = 0); + } + + None +} + +pub(crate) fn cshake_test<D, F>(input: &[u8], output: &[u8], new: F) -> Option<&'static str> +where + D: ExtendableOutput + Debug + Clone, + F: Fn() -> D, +{ + let mut hasher = new(); + let mut buf = [0u8; 1024]; + let buf = &mut buf[..output.len()]; + // Test that it works when accepting the message all at once + hasher.update(input); + let mut hasher2 = hasher.clone(); + hasher.finalize_xof_into(buf); + if buf != output { + return Some("whole message"); + } + buf.iter_mut().for_each(|b| *b = 0); + + // Test that it works when accepting the message in chunks + for n in 1..core::cmp::min(17, input.len()) { + let mut hasher = new(); + for chunk in input.chunks(n) { + hasher.update(chunk); + hasher2.update(chunk); + } + hasher.finalize_xof_into(buf); + if buf != output { + return Some("message in chunks"); + } + buf.iter_mut().for_each(|b| *b = 0); + } + + None +} + +macro_rules! new_cshake_test { + ($name:ident, $test_name:expr, $hasher:ty, $hasher_core:ty, $test_func:ident $(,)?) => { + #[test] + fn $name() { + use digest::dev::blobby::Blob3Iterator; + let data = include_bytes!(concat!("data/", $test_name, ".blb")); + + for (i, row) in Blob3Iterator::new(data).unwrap().enumerate() { + let [customization, input, output] = row.unwrap(); + if let Some(desc) = $test_func(input, output, || { + <$hasher>::from_core(<$hasher_core>::new(customization)) + }) { + panic!( + "\n\ + Failed test №{}: {}\n\ + input:\t{:?}\n\ + output:\t{:?}\n", + i, desc, input, output, + ); + } + } + } + }; +} + +#[cfg(feature = "reset")] +new_cshake_test!( + cshake128_reset, + "cshake128", + sha3::CShake128, + sha3::CShake128Core, + cshake_reset_test +); +#[cfg(feature = "reset")] +new_cshake_test!( + cshake256_reset, + "cshake256", + sha3::CShake256, + sha3::CShake256Core, + cshake_reset_test +); + +new_cshake_test!( + cshake128, + "cshake128", + sha3::CShake128, + sha3::CShake128Core, + cshake_test +); +new_cshake_test!( + cshake256, + "cshake256", + sha3::CShake256, + sha3::CShake256Core, + cshake_test +); diff --git a/third_party/rust/sha3/tests/data/cshake128.blb b/third_party/rust/sha3/tests/data/cshake128.blb Binary files differnew file mode 100644 index 0000000000..e8ae154f75 --- /dev/null +++ b/third_party/rust/sha3/tests/data/cshake128.blb diff --git a/third_party/rust/sha3/tests/data/cshake256.blb b/third_party/rust/sha3/tests/data/cshake256.blb Binary files differnew file mode 100644 index 0000000000..9e15af1cc2 --- /dev/null +++ b/third_party/rust/sha3/tests/data/cshake256.blb diff --git a/third_party/rust/sha3/tests/data/keccak_224.blb b/third_party/rust/sha3/tests/data/keccak_224.blb Binary files differnew file mode 100644 index 0000000000..393d0acebd --- /dev/null +++ b/third_party/rust/sha3/tests/data/keccak_224.blb diff --git a/third_party/rust/sha3/tests/data/keccak_256.blb b/third_party/rust/sha3/tests/data/keccak_256.blb Binary files differnew file mode 100644 index 0000000000..d964e20947 --- /dev/null +++ b/third_party/rust/sha3/tests/data/keccak_256.blb diff --git a/third_party/rust/sha3/tests/data/keccak_256_full.blb b/third_party/rust/sha3/tests/data/keccak_256_full.blb Binary files differnew file mode 100644 index 0000000000..71af3f2347 --- /dev/null +++ b/third_party/rust/sha3/tests/data/keccak_256_full.blb diff --git a/third_party/rust/sha3/tests/data/keccak_384.blb b/third_party/rust/sha3/tests/data/keccak_384.blb Binary files differnew file mode 100644 index 0000000000..2509429442 --- /dev/null +++ b/third_party/rust/sha3/tests/data/keccak_384.blb diff --git a/third_party/rust/sha3/tests/data/keccak_512.blb b/third_party/rust/sha3/tests/data/keccak_512.blb Binary files differnew file mode 100644 index 0000000000..a9e47e0770 --- /dev/null +++ b/third_party/rust/sha3/tests/data/keccak_512.blb diff --git a/third_party/rust/sha3/tests/data/sha3_224.blb b/third_party/rust/sha3/tests/data/sha3_224.blb Binary files differnew file mode 100644 index 0000000000..510e160c4c --- /dev/null +++ b/third_party/rust/sha3/tests/data/sha3_224.blb diff --git a/third_party/rust/sha3/tests/data/sha3_256.blb b/third_party/rust/sha3/tests/data/sha3_256.blb Binary files differnew file mode 100644 index 0000000000..00fe6fcd91 --- /dev/null +++ b/third_party/rust/sha3/tests/data/sha3_256.blb diff --git a/third_party/rust/sha3/tests/data/sha3_384.blb b/third_party/rust/sha3/tests/data/sha3_384.blb Binary files differnew file mode 100644 index 0000000000..802759adf6 --- /dev/null +++ b/third_party/rust/sha3/tests/data/sha3_384.blb diff --git a/third_party/rust/sha3/tests/data/sha3_512.blb b/third_party/rust/sha3/tests/data/sha3_512.blb Binary files differnew file mode 100644 index 0000000000..b02800c0fa --- /dev/null +++ b/third_party/rust/sha3/tests/data/sha3_512.blb diff --git a/third_party/rust/sha3/tests/data/shake128.blb b/third_party/rust/sha3/tests/data/shake128.blb Binary files differnew file mode 100644 index 0000000000..58fb208148 --- /dev/null +++ b/third_party/rust/sha3/tests/data/shake128.blb diff --git a/third_party/rust/sha3/tests/data/shake256.blb b/third_party/rust/sha3/tests/data/shake256.blb Binary files differnew file mode 100644 index 0000000000..c5340e26a8 --- /dev/null +++ b/third_party/rust/sha3/tests/data/shake256.blb diff --git a/third_party/rust/sha3/tests/data/turboshake128.blb b/third_party/rust/sha3/tests/data/turboshake128.blb Binary files differnew file mode 100644 index 0000000000..5f1d64d11e --- /dev/null +++ b/third_party/rust/sha3/tests/data/turboshake128.blb diff --git a/third_party/rust/sha3/tests/data/turboshake256.blb b/third_party/rust/sha3/tests/data/turboshake256.blb Binary files differnew file mode 100644 index 0000000000..6c6635314d --- /dev/null +++ b/third_party/rust/sha3/tests/data/turboshake256.blb diff --git a/third_party/rust/sha3/tests/mod.rs b/third_party/rust/sha3/tests/mod.rs new file mode 100644 index 0000000000..120ff41e23 --- /dev/null +++ b/third_party/rust/sha3/tests/mod.rs @@ -0,0 +1,24 @@ +#![no_std] + +use digest::dev::{fixed_reset_test, xof_reset_test}; +use digest::new_test; + +new_test!(keccak_224, "keccak_224", sha3::Keccak224, fixed_reset_test); +new_test!(keccak_256, "keccak_256", sha3::Keccak256, fixed_reset_test); +new_test!(keccak_384, "keccak_384", sha3::Keccak384, fixed_reset_test); +new_test!(keccak_512, "keccak_512", sha3::Keccak512, fixed_reset_test); +// tests are from https://github.com/kazcw/yellowsun/blob/test-keccak/src/lib.rs#L171 +new_test!( + keccak_256_full, + "keccak_256_full", + sha3::Keccak256Full, + fixed_reset_test +); + +new_test!(sha3_224, "sha3_224", sha3::Sha3_224, fixed_reset_test); +new_test!(sha3_256, "sha3_256", sha3::Sha3_256, fixed_reset_test); +new_test!(sha3_384, "sha3_384", sha3::Sha3_384, fixed_reset_test); +new_test!(sha3_512, "sha3_512", sha3::Sha3_512, fixed_reset_test); + +new_test!(shake128, "shake128", sha3::Shake128, xof_reset_test); +new_test!(shake256, "shake256", sha3::Shake256, xof_reset_test); diff --git a/third_party/rust/sha3/tests/turboshake.rs b/third_party/rust/sha3/tests/turboshake.rs new file mode 100644 index 0000000000..3d08df1961 --- /dev/null +++ b/third_party/rust/sha3/tests/turboshake.rs @@ -0,0 +1,108 @@ +use core::{convert::TryInto, fmt::Debug}; +use digest::ExtendableOutput; + +pub(crate) fn turbo_shake_test<D, F>( + input: &[u8], + output: &[u8], + truncate_output: usize, + new: F, +) -> Option<&'static str> +where + D: ExtendableOutput + Debug + Clone, + F: Fn() -> D, +{ + let mut hasher = new(); + let mut buf = [0u8; 16 * 1024]; + let buf = &mut buf[..truncate_output + output.len()]; + // Test that it works when accepting the message all at once + hasher.update(input); + let mut hasher2 = hasher.clone(); + hasher.finalize_xof_into(buf); + if &buf[truncate_output..] != output { + return Some("whole message"); + } + buf.iter_mut().for_each(|b| *b = 0); + + // Test that it works when accepting the message in chunks + for n in 1..core::cmp::min(17, input.len()) { + let mut hasher = new(); + for chunk in input.chunks(n) { + hasher.update(chunk); + hasher2.update(chunk); + } + hasher.finalize_xof_into(buf); + if &buf[truncate_output..] != output { + return Some("message in chunks"); + } + buf.iter_mut().for_each(|b| *b = 0); + } + + None +} + +macro_rules! new_turbo_shake_test { + ($name:ident, $test_name:expr, $hasher:ty, $hasher_core:ty, $test_func:ident $(,)?) => { + #[test] + fn $name() { + use digest::dev::blobby::Blob5Iterator; + let data = include_bytes!(concat!("data/", $test_name, ".blb")); + + for (i, row) in Blob5Iterator::new(data).unwrap().enumerate() { + let [domain_separation, input, input_pattern_length, output, truncate_output] = + row.unwrap(); + + let input = if (input_pattern_length.len() == 0) { + input.to_vec() + } else if (input.len() == 0) { + let pattern_length = + u64::from_be_bytes(input_pattern_length.try_into().unwrap()); + let mut input = Vec::<u8>::new(); + for value in 0..pattern_length { + input.push((value % 0xFB).try_into().unwrap()); + } + input + } else { + panic!( + "\ + failed to read tests data\n\ + input:\t{:02X?}\n\ + input_pattern_length:\t{:02X?}\n", + input, input_pattern_length, + ); + }; + + if let Some(desc) = $test_func( + &input, + output, + u64::from_be_bytes(truncate_output.try_into().unwrap()) + .try_into() + .unwrap(), + || <$hasher>::from_core(<$hasher_core>::new(domain_separation[0])), + ) { + panic!( + "\n\ + Failed test №{}: {}\n\ + input:\t{:02X?}\n\ + output:\t{:02X?}\n", + i, desc, &input, output, + ); + } + } + } + }; +} + +new_turbo_shake_test!( + turboshake128, + "turboshake128", + sha3::TurboShake128, + sha3::TurboShake128Core, + turbo_shake_test, +); +new_turbo_shake_test!( + turboshake256, + "turboshake256", + sha3::TurboShake256, + sha3::TurboShake256Core, + turbo_shake_test, +); |