sha1-0.11.0/.cargo_vcs_info.json0000644000000001421046102023000117660ustar { "git": { "sha1": "2f00175af936de46b3ddefe65c4de93cb4e876e4" }, "path_in_vcs": "sha1" }sha1-0.11.0/CHANGELOG.md000064400000000000000000000042151046102023000123520ustar 00000000000000# 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.11.0 (2026-03-27) ### Added - `alloc` crate feature ([#678]) - `sha1_backend` configuration flag ([#808]) ### Changed - Edition changed to 2024 and MSRV bumped to 1.85 ([#652]) - Relax MSRV policy and allow MSRV bumps in patch releases - Update to `digest` v0.11 - Replace type aliases with newtypes ([#678]) - Implementation of the `SerializableState` trait ([#716]) ### Removed - `asm`, `loongarch64_asm`, `force-soft`, `std`, and `compress` crate features ([#542], [#678], [#808]) [#542]: https://github.com/RustCrypto/hashes/pull/542 [#652]: https://github.com/RustCrypto/hashes/pull/652 [#678]: https://github.com/RustCrypto/hashes/pull/678 [#716]: https://github.com/RustCrypto/hashes/pull/716 [#808]: https://github.com/RustCrypto/hashes/pull/808 ## 0.10.6 (2023-09-21) ### Added - `asm!`-based backend for LoongArch64 targets gated behind `loongarch64_asm` feature [#504] [#504]: https://github.com/RustCrypto/hashes/pull/504 ## 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-08-30) ### Changed - Ignore `asm` feature on unsupported targets ([#388]) [#388]: https://github.com/RustCrypto/hashes/pull/388 ## 0.10.1 (2022-02-17) ### Fixed - Minimal versions build ([#363]) [#363]: https://github.com/RustCrypto/hashes/pull/363 ## 0.10.0 (2022-01-17) ### Changed - The crate is transferred to the RustCrypto organization. New implementation is identical to the `sha-1 v0.10.0` crate and expressed in terms of traits from the `digest` crate. ([#350]) [#350]: https://github.com/RustCrypto/hashes/pull/350 sha1-0.11.0/Cargo.lock0000644000000053261046102023000077520ustar # 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.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cdd35008169921d80bc60d3d0ab416eecb028c4cd653352907921d95084790be" dependencies = [ "hybrid-array", "zeroize", ] [[package]] name = "cfg-if" version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" [[package]] name = "const-oid" version = "0.10.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a6ef517f0926dd24a1582492c791b6a4818a4d94e789a334894aa15b0d12f55c" [[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.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "77727bb15fa921304124b128af125e7e3b968275d1b108b379190264f4423710" dependencies = [ "hybrid-array", ] [[package]] name = "digest" version = "0.11.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4850db49bf08e663084f7fb5c87d202ef91a3907271aff24a94eb97ff039153c" dependencies = [ "blobby", "block-buffer", "const-oid", "crypto-common", "zeroize", ] [[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.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8655f91cd07f2b9d0c24137bd650fe69617773435ee5ec83022377777ce65ef1" dependencies = [ "typenum", ] [[package]] name = "libc" version = "0.2.183" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b5b646652bf6661599e1da8901b3b9522896f01e736bad5f723fe7a3a27f899d" [[package]] name = "sha1" version = "0.11.0" dependencies = [ "cfg-if", "cpufeatures", "digest", "hex-literal", ] [[package]] name = "typenum" version = "1.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "562d481066bde0658276a35467c4af00bdc6ee726305698a55b86e61d7ad82bb" [[package]] name = "zeroize" version = "1.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b97154e67e32c85465826e8bcc1c59429aaaf107c1e4a9e53c8d8ccd5eff88d0" sha1-0.11.0/Cargo.toml0000644000000033071046102023000077720ustar # 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 = "sha1" version = "0.11.0" authors = ["RustCrypto Developers"] build = false autolib = false autobins = false autoexamples = false autotests = false autobenches = false description = "SHA-1 hash function" documentation = "https://docs.rs/sha1" readme = "README.md" keywords = [ "sha1", "hash", "digest", ] categories = [ "cryptography", "no-std", ] license = "MIT OR Apache-2.0" repository = "https://github.com/RustCrypto/hashes" [package.metadata.docs.rs] all-features = true [features] alloc = ["digest/alloc"] default = [ "alloc", "oid", ] oid = ["digest/oid"] zeroize = ["digest/zeroize"] [lib] name = "sha1" path = "src/lib.rs" [[test]] name = "mod" path = "tests/mod.rs" [[bench]] name = "mod" path = "benches/mod.rs" [dependencies.cfg-if] version = "1.0" [dependencies.digest] version = "0.11" [dev-dependencies.digest] version = "0.11" features = ["dev"] [dev-dependencies.hex-literal] version = "1" [target.'cfg(any(target_arch = "aarch64", target_arch = "x86", target_arch = "x86_64"))'.dependencies.cpufeatures] version = "0.3" [lints.rust.unexpected_cfgs] level = "warn" priority = 0 check-cfg = ['cfg(sha1_backend, values("aarch64-sha2", "x86-sha", "soft"))'] sha1-0.11.0/Cargo.toml.orig000064400000000000000000000016611046102023000134320ustar 00000000000000[package] name = "sha1" version = "0.11.0" authors = ["RustCrypto Developers"] edition = "2024" rust-version = "1.85" documentation = "https://docs.rs/sha1" readme = "README.md" repository = "https://github.com/RustCrypto/hashes" license = "MIT OR Apache-2.0" keywords = ["sha1", "hash", "digest"] categories = ["cryptography", "no-std"] description = "SHA-1 hash function" [dependencies] digest = "0.11" cfg-if = "1.0" [target.'cfg(any(target_arch = "aarch64", target_arch = "x86", target_arch = "x86_64"))'.dependencies] cpufeatures = "0.3" [dev-dependencies] digest = { version = "0.11", features = ["dev"] } hex-literal = "1" [features] default = ["alloc", "oid"] alloc = ["digest/alloc"] oid = ["digest/oid"] # Enable OID support zeroize = ["digest/zeroize"] [lints.rust] unexpected_cfgs = { level = "warn", check-cfg = ['cfg(sha1_backend, values("aarch64-sha2", "x86-sha", "soft"))'] } [package.metadata.docs.rs] all-features = true sha1-0.11.0/LICENSE-APACHE000064400000000000000000000251411046102023000124660ustar 00000000000000 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. sha1-0.11.0/LICENSE-MIT000064400000000000000000000022541046102023000121760ustar 00000000000000Copyright (c) 2016-2026 The RustCrypto Project Developers Copyright (c) 2016 Artyom Pavlov Copyright (c) 2009-2013 Mozilla Foundation Copyright (c) 2006-2009 Graydon Hoare 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. sha1-0.11.0/README.md000064400000000000000000000050601046102023000120170ustar 00000000000000# RustCrypto: SHA-1 [![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-1] cryptographic hash algorithm. ## Warning: Cryptographically Broken! The SHA-1 hash function should be considered cryptographically broken and unsuitable for further use in any security critical capacity, as it is [practically vulnerable to chosen-prefix collisions][1]. We provide this crate for legacy interoperability purposes only. If possible use the [sha1-checked] crate, while slower it provides the ability to detect potential collisions, as well as generate alternative safe hashes. ## Examples ### One-shot API ```rust use hex_literal::hex; use sha1::{Sha1, Digest}; let result = Sha1::digest(b"hello world"); assert_eq!(result, hex!("2aae6c35c94fcfb415dbe95f408b9ce91ee846ed")); ``` ### Incremental API ```rust use hex_literal::hex; use sha1::{Sha1, Digest}; let mut hasher = Sha1::new(); hasher.update(b"hello world"); let hash = hasher.finalize(); assert_eq!(hash, hex!("2aae6c35c94fcfb415dbe95f408b9ce91ee846ed")); ``` See the [`digest`] crate docs for additional examples. ## License The crate is 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/sha1.svg [crate-link]: https://crates.io/crates/sha1 [docs-image]: https://docs.rs/sha1/badge.svg [docs-link]: https://docs.rs/sha1/ [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/260041-hashes [build-image]: https://github.com/RustCrypto/hashes/actions/workflows/sha1.yml/badge.svg?branch=master [build-link]: https://github.com/RustCrypto/hashes/actions/workflows/sha1.yml?query=branch:master [//]: # (general links) [SHA-1]: https://en.wikipedia.org/wiki/SHA-1 [1]: https://sha-mbles.github.io/ [`digest`]: https://docs.rs/digest [sha1-checked]: https://crates.io/crates/sha1-checked sha1-0.11.0/benches/mod.rs000064400000000000000000000003271046102023000132750ustar 00000000000000#![feature(test)] extern crate test; use digest::bench_update; use sha1::Sha1; use test::Bencher; bench_update!( Sha1::default(); sha1_10 10; sha1_100 100; sha1_1000 1000; sha1_10000 10000; ); sha1-0.11.0/src/block_api.rs000064400000000000000000000065451046102023000136310ustar 00000000000000use core::fmt; use digest::{ HashMarker, Output, array::Array, block_api::{ AlgorithmName, Block, BlockSizeUser, Buffer, BufferKindUser, Eager, FixedOutputCore, OutputSizeUser, Reset, UpdateCore, }, common::hazmat::{DeserializeStateError, SerializableState, SerializedState}, typenum::{U20, U28, U64, Unsigned}, }; #[cfg(feature = "zeroize")] use digest::zeroize::{Zeroize, ZeroizeOnDrop}; use crate::consts::{H0, State}; /// SHA-1 compression function pub fn compress(state: &mut State, blocks: &[[u8; 64]]) { crate::compress::compress(state, blocks); } /// Core SHA-1 hasher state. #[derive(Clone)] pub struct Sha1Core { h: State, block_len: u64, } impl HashMarker for Sha1Core {} impl BlockSizeUser for Sha1Core { type BlockSize = U64; } impl BufferKindUser for Sha1Core { type BufferKind = Eager; } impl OutputSizeUser for Sha1Core { type OutputSize = U20; } impl UpdateCore for Sha1Core { #[inline] fn update_blocks(&mut self, blocks: &[Block]) { self.block_len += blocks.len() as u64; let blocks = Array::cast_slice_to_core(blocks); compress(&mut self.h, blocks); } } impl FixedOutputCore for Sha1Core { #[inline] fn finalize_fixed_core(&mut self, buffer: &mut Buffer, out: &mut Output) { let bs = Self::BlockSize::U64; let bit_len = 8 * (buffer.get_pos() as u64 + bs * self.block_len); let mut h = self.h; buffer.len64_padding_be(bit_len, |b| compress(&mut h, &[b.0])); for (chunk, v) in out.chunks_exact_mut(4).zip(h.iter()) { chunk.copy_from_slice(&v.to_be_bytes()); } } } impl Default for Sha1Core { #[inline] fn default() -> Self { Self { h: H0, block_len: 0, } } } impl Reset for Sha1Core { #[inline] fn reset(&mut self) { *self = Default::default(); } } impl AlgorithmName for Sha1Core { fn write_alg_name(f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str("Sha1") } } impl fmt::Debug for Sha1Core { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str("Sha1Core { ... }") } } impl Drop for Sha1Core { fn drop(&mut self) { #[cfg(feature = "zeroize")] { self.h.zeroize(); self.block_len.zeroize(); } } } #[cfg(feature = "zeroize")] impl ZeroizeOnDrop for Sha1Core {} impl SerializableState for Sha1Core { type SerializedStateSize = U28; fn serialize(&self) -> SerializedState { let mut serialized_h = SerializedState::::default(); for (val, chunk) in self.h.iter().zip(serialized_h.chunks_exact_mut(4)) { chunk.copy_from_slice(&val.to_le_bytes()); } serialized_h[20..].copy_from_slice(&self.block_len.to_le_bytes()); serialized_h } fn deserialize( serialized_state: &SerializedState, ) -> Result { let (serialized_h, serialized_block_len) = serialized_state.split::(); let mut h = State::default(); for (val, chunk) in h.iter_mut().zip(serialized_h.chunks_exact(4)) { *val = u32::from_le_bytes(chunk.try_into().unwrap()); } let block_len = u64::from_le_bytes(*serialized_block_len.as_ref()); Ok(Self { h, block_len }) } } sha1-0.11.0/src/compress/aarch64_sha2.rs000064400000000000000000000127421046102023000157020ustar 00000000000000//! SHA-1 `aarch64` backend. use crate::consts::K; #[cfg(not(target_arch = "aarch64"))] compile_error!("aarch64-sha2 backend can be used only aarch64 target arches"); // note that `sha2` implicitly enables `neon` #[target_feature(enable = "sha2")] #[allow(unsafe_op_in_unsafe_fn)] pub(crate) unsafe fn compress(state: &mut [u32; 5], blocks: &[[u8; 64]]) { use core::arch::aarch64::*; let mut abcd = vld1q_u32(state.as_ptr()); let mut e0 = state[4]; let [k0, k1, k2, k3] = K.map(|k| vdupq_n_u32(k)); let (mut e1, mut tmp0, mut tmp1); for block in blocks { let abcd_cpy = abcd; let e0_cpy = e0; // Load and reverse byte order let [mut msg0, mut msg1, mut msg2, mut msg3] = [0, 1, 2, 3].map(|i| { let p = block.as_ptr().add(16 * i); vreinterpretq_u32_u8(vrev32q_u8(vld1q_u8(p))) }); tmp0 = vaddq_u32(msg0, k0); tmp1 = vaddq_u32(msg1, k0); // Rounds 0-3 e1 = vsha1h_u32(vgetq_lane_u32(abcd, 0)); abcd = vsha1cq_u32(abcd, e0, tmp0); tmp0 = vaddq_u32(msg2, k0); msg0 = vsha1su0q_u32(msg0, msg1, msg2); // Rounds 4-7 e0 = vsha1h_u32(vgetq_lane_u32(abcd, 0)); abcd = vsha1cq_u32(abcd, e1, tmp1); tmp1 = vaddq_u32(msg3, k0); msg0 = vsha1su1q_u32(msg0, msg3); msg1 = vsha1su0q_u32(msg1, msg2, msg3); // Rounds 8-11 e1 = vsha1h_u32(vgetq_lane_u32(abcd, 0)); abcd = vsha1cq_u32(abcd, e0, tmp0); tmp0 = vaddq_u32(msg0, k0); msg1 = vsha1su1q_u32(msg1, msg0); msg2 = vsha1su0q_u32(msg2, msg3, msg0); // Rounds 12-15 e0 = vsha1h_u32(vgetq_lane_u32(abcd, 0)); abcd = vsha1cq_u32(abcd, e1, tmp1); tmp1 = vaddq_u32(msg1, k1); msg2 = vsha1su1q_u32(msg2, msg1); msg3 = vsha1su0q_u32(msg3, msg0, msg1); // Rounds 16-19 e1 = vsha1h_u32(vgetq_lane_u32(abcd, 0)); abcd = vsha1cq_u32(abcd, e0, tmp0); tmp0 = vaddq_u32(msg2, k1); msg3 = vsha1su1q_u32(msg3, msg2); msg0 = vsha1su0q_u32(msg0, msg1, msg2); // Rounds 20-23 e0 = vsha1h_u32(vgetq_lane_u32(abcd, 0)); abcd = vsha1pq_u32(abcd, e1, tmp1); tmp1 = vaddq_u32(msg3, k1); msg0 = vsha1su1q_u32(msg0, msg3); msg1 = vsha1su0q_u32(msg1, msg2, msg3); // Rounds 24-27 e1 = vsha1h_u32(vgetq_lane_u32(abcd, 0)); abcd = vsha1pq_u32(abcd, e0, tmp0); tmp0 = vaddq_u32(msg0, k1); msg1 = vsha1su1q_u32(msg1, msg0); msg2 = vsha1su0q_u32(msg2, msg3, msg0); // Rounds 28-31 e0 = vsha1h_u32(vgetq_lane_u32(abcd, 0)); abcd = vsha1pq_u32(abcd, e1, tmp1); tmp1 = vaddq_u32(msg1, k1); msg2 = vsha1su1q_u32(msg2, msg1); msg3 = vsha1su0q_u32(msg3, msg0, msg1); // Rounds 32-35 e1 = vsha1h_u32(vgetq_lane_u32(abcd, 0)); abcd = vsha1pq_u32(abcd, e0, tmp0); tmp0 = vaddq_u32(msg2, k2); msg3 = vsha1su1q_u32(msg3, msg2); msg0 = vsha1su0q_u32(msg0, msg1, msg2); // Rounds 36-39 e0 = vsha1h_u32(vgetq_lane_u32(abcd, 0)); abcd = vsha1pq_u32(abcd, e1, tmp1); tmp1 = vaddq_u32(msg3, k2); msg0 = vsha1su1q_u32(msg0, msg3); msg1 = vsha1su0q_u32(msg1, msg2, msg3); // Rounds 40-43 e1 = vsha1h_u32(vgetq_lane_u32(abcd, 0)); abcd = vsha1mq_u32(abcd, e0, tmp0); tmp0 = vaddq_u32(msg0, k2); msg1 = vsha1su1q_u32(msg1, msg0); msg2 = vsha1su0q_u32(msg2, msg3, msg0); // Rounds 44-47 e0 = vsha1h_u32(vgetq_lane_u32(abcd, 0)); abcd = vsha1mq_u32(abcd, e1, tmp1); tmp1 = vaddq_u32(msg1, k2); msg2 = vsha1su1q_u32(msg2, msg1); msg3 = vsha1su0q_u32(msg3, msg0, msg1); // Rounds 48-51 e1 = vsha1h_u32(vgetq_lane_u32(abcd, 0)); abcd = vsha1mq_u32(abcd, e0, tmp0); tmp0 = vaddq_u32(msg2, k2); msg3 = vsha1su1q_u32(msg3, msg2); msg0 = vsha1su0q_u32(msg0, msg1, msg2); // Rounds 52-55 e0 = vsha1h_u32(vgetq_lane_u32(abcd, 0)); abcd = vsha1mq_u32(abcd, e1, tmp1); tmp1 = vaddq_u32(msg3, k3); msg0 = vsha1su1q_u32(msg0, msg3); msg1 = vsha1su0q_u32(msg1, msg2, msg3); // Rounds 56-59 e1 = vsha1h_u32(vgetq_lane_u32(abcd, 0)); abcd = vsha1mq_u32(abcd, e0, tmp0); tmp0 = vaddq_u32(msg0, k3); msg1 = vsha1su1q_u32(msg1, msg0); msg2 = vsha1su0q_u32(msg2, msg3, msg0); // Rounds 60-63 e0 = vsha1h_u32(vgetq_lane_u32(abcd, 0)); abcd = vsha1pq_u32(abcd, e1, tmp1); tmp1 = vaddq_u32(msg1, k3); msg2 = vsha1su1q_u32(msg2, msg1); msg3 = vsha1su0q_u32(msg3, msg0, msg1); // Rounds 64-67 e1 = vsha1h_u32(vgetq_lane_u32(abcd, 0)); abcd = vsha1pq_u32(abcd, e0, tmp0); tmp0 = vaddq_u32(msg2, k3); msg3 = vsha1su1q_u32(msg3, msg2); // Rounds 68-71 e0 = vsha1h_u32(vgetq_lane_u32(abcd, 0)); abcd = vsha1pq_u32(abcd, e1, tmp1); tmp1 = vaddq_u32(msg3, k3); // Rounds 72-75 e1 = vsha1h_u32(vgetq_lane_u32(abcd, 0)); abcd = vsha1pq_u32(abcd, e0, tmp0); // Rounds 76-79 e0 = vsha1h_u32(vgetq_lane_u32(abcd, 0)); abcd = vsha1pq_u32(abcd, e1, tmp1); // Update state abcd = vaddq_u32(abcd_cpy, abcd); e0 = e0.wrapping_add(e0_cpy); } // Save state vst1q_u32(state.as_mut_ptr(), abcd); state[4] = e0; } sha1-0.11.0/src/compress/loongarch64_asm.rs000064400000000000000000000226741046102023000165300ustar 00000000000000//! LoongArch64 assembly backend use crate::consts::K; use core::arch::asm; #[cfg(not(target_arch = "loongarch64"))] compile_error!("loongarch-asm backend can be used only on loongarch64 target arches"); macro_rules! c { ($($l:expr)*) => { concat!($($l ,)*) }; } macro_rules! round0a { ($a:literal, $b:literal, $c:literal, $d:literal, $e:literal, $i:literal) => { c!( "ld.w $t5, $a1, (" $i " * 4);" "revb.2h $t5, $t5;" "rotri.w $t5, $t5, 16;" "add.w " $e ", " $e ", $t5;" "st.w $t5, $sp, (" $i " * 4);" "xor $t5, " $c "," $d ";" "and $t5, $t5, " $b ";" "xor $t5, $t5, " $d ";" roundtail!($a, $b, $e, $i, "$a4") ) }; } macro_rules! scheldule { ($i:literal, $e:literal) => { c!( "ld.w $t5, $sp, (((" $i " - 3) & 0xF) * 4);" "ld.w $t6, $sp, (((" $i " - 8) & 0xF) * 4);" "ld.w $t7, $sp, (((" $i " - 14) & 0xF) * 4);" "ld.w $t8, $sp, (((" $i " - 16) & 0xF) * 4);" "xor $t5, $t5, $t6;" "xor $t5, $t5, $t7;" "xor $t5, $t5, $t8;" "rotri.w $t5, $t5, 31;" "add.w " $e "," $e ", $t5;" "st.w $t5, $sp, ((" $i " & 0xF) * 4);" ) }; } macro_rules! round0b { ($a:literal, $b:literal, $c:literal, $d:literal, $e:literal, $i:literal) => { c!( scheldule!($i, $e) "xor $t5," $c "," $d ";" "and $t5, $t5," $b ";" "xor $t5, $t5," $d ";" roundtail!($a, $b, $e, $i, "$a4") ) }; } macro_rules! round1 { ($a:literal, $b:literal, $c:literal, $d:literal, $e:literal, $i:literal) => { c!( scheldule!($i, $e) "xor $t5," $b "," $c ";" "xor $t5, $t5," $d ";" roundtail!($a, $b, $e, $i, "$a5") ) }; } macro_rules! round2 { ($a:literal, $b:literal, $c:literal, $d:literal, $e:literal, $i:literal) => { c!( scheldule!($i, $e) "or $t5," $c "," $d ";" "and $t5, $t5, " $b ";" "and $t7," $c "," $d ";" "or $t5, $t5, $t7;" roundtail!($a, $b, $e, $i, "$a6") ) }; } macro_rules! round3 { ($a:literal, $b:literal, $c:literal, $d:literal, $e:literal, $i:literal) => { c!( scheldule!($i, $e) "xor $t5," $b "," $c ";" "xor $t5, $t5," $d ";" roundtail!($a, $b, $e, $i, "$a7") ) }; } macro_rules! roundtail { ($a:literal, $b:literal, $e:literal, $i:literal, $k:literal) => { c!( "rotri.w " $b "," $b ", 2;" "add.w " $e "," $e ", $t5;" "add.w " $e "," $e "," $k ";" "rotri.w $t5," $a ", 27;" "add.w " $e "," $e ", $t5;" ) }; } pub(crate) fn compress(state: &mut [u32; 5], blocks: &[[u8; 64]]) { if blocks.is_empty() { return; } unsafe { asm!( // Allocate scratch stack space "addi.d $sp, $sp, -64;", // Load state "ld.w $t0, $a0, 0", "ld.w $t1, $a0, 4", "ld.w $t2, $a0, 8", "ld.w $t3, $a0, 12", "ld.w $t4, $a0, 16", "42:", round0a!("$t0", "$t1", "$t2", "$t3", "$t4", 0), round0a!("$t4", "$t0", "$t1", "$t2", "$t3", 1), round0a!("$t3", "$t4", "$t0", "$t1", "$t2", 2), round0a!("$t2", "$t3", "$t4", "$t0", "$t1", 3), round0a!("$t1", "$t2", "$t3", "$t4", "$t0", 4), round0a!("$t0", "$t1", "$t2", "$t3", "$t4", 5), round0a!("$t4", "$t0", "$t1", "$t2", "$t3", 6), round0a!("$t3", "$t4", "$t0", "$t1", "$t2", 7), round0a!("$t2", "$t3", "$t4", "$t0", "$t1", 8), round0a!("$t1", "$t2", "$t3", "$t4", "$t0", 9), round0a!("$t0", "$t1", "$t2", "$t3", "$t4", 10), round0a!("$t4", "$t0", "$t1", "$t2", "$t3", 11), round0a!("$t3", "$t4", "$t0", "$t1", "$t2", 12), round0a!("$t2", "$t3", "$t4", "$t0", "$t1", 13), round0a!("$t1", "$t2", "$t3", "$t4", "$t0", 14), round0a!("$t0", "$t1", "$t2", "$t3", "$t4", 15), round0b!("$t4", "$t0", "$t1", "$t2", "$t3", 16), round0b!("$t3", "$t4", "$t0", "$t1", "$t2", 17), round0b!("$t2", "$t3", "$t4", "$t0", "$t1", 18), round0b!("$t1", "$t2", "$t3", "$t4", "$t0", 19), round1!("$t0", "$t1", "$t2", "$t3", "$t4", 20), round1!("$t4", "$t0", "$t1", "$t2", "$t3", 21), round1!("$t3", "$t4", "$t0", "$t1", "$t2", 22), round1!("$t2", "$t3", "$t4", "$t0", "$t1", 23), round1!("$t1", "$t2", "$t3", "$t4", "$t0", 24), round1!("$t0", "$t1", "$t2", "$t3", "$t4", 25), round1!("$t4", "$t0", "$t1", "$t2", "$t3", 26), round1!("$t3", "$t4", "$t0", "$t1", "$t2", 27), round1!("$t2", "$t3", "$t4", "$t0", "$t1", 28), round1!("$t1", "$t2", "$t3", "$t4", "$t0", 29), round1!("$t0", "$t1", "$t2", "$t3", "$t4", 30), round1!("$t4", "$t0", "$t1", "$t2", "$t3", 31), round1!("$t3", "$t4", "$t0", "$t1", "$t2", 32), round1!("$t2", "$t3", "$t4", "$t0", "$t1", 33), round1!("$t1", "$t2", "$t3", "$t4", "$t0", 34), round1!("$t0", "$t1", "$t2", "$t3", "$t4", 35), round1!("$t4", "$t0", "$t1", "$t2", "$t3", 36), round1!("$t3", "$t4", "$t0", "$t1", "$t2", 37), round1!("$t2", "$t3", "$t4", "$t0", "$t1", 38), round1!("$t1", "$t2", "$t3", "$t4", "$t0", 39), round2!("$t0", "$t1", "$t2", "$t3", "$t4", 40), round2!("$t4", "$t0", "$t1", "$t2", "$t3", 41), round2!("$t3", "$t4", "$t0", "$t1", "$t2", 42), round2!("$t2", "$t3", "$t4", "$t0", "$t1", 43), round2!("$t1", "$t2", "$t3", "$t4", "$t0", 44), round2!("$t0", "$t1", "$t2", "$t3", "$t4", 45), round2!("$t4", "$t0", "$t1", "$t2", "$t3", 46), round2!("$t3", "$t4", "$t0", "$t1", "$t2", 47), round2!("$t2", "$t3", "$t4", "$t0", "$t1", 48), round2!("$t1", "$t2", "$t3", "$t4", "$t0", 49), round2!("$t0", "$t1", "$t2", "$t3", "$t4", 50), round2!("$t4", "$t0", "$t1", "$t2", "$t3", 51), round2!("$t3", "$t4", "$t0", "$t1", "$t2", 52), round2!("$t2", "$t3", "$t4", "$t0", "$t1", 53), round2!("$t1", "$t2", "$t3", "$t4", "$t0", 54), round2!("$t0", "$t1", "$t2", "$t3", "$t4", 55), round2!("$t4", "$t0", "$t1", "$t2", "$t3", 56), round2!("$t3", "$t4", "$t0", "$t1", "$t2", 57), round2!("$t2", "$t3", "$t4", "$t0", "$t1", 58), round2!("$t1", "$t2", "$t3", "$t4", "$t0", 59), round3!("$t0", "$t1", "$t2", "$t3", "$t4", 60), round3!("$t4", "$t0", "$t1", "$t2", "$t3", 61), round3!("$t3", "$t4", "$t0", "$t1", "$t2", 62), round3!("$t2", "$t3", "$t4", "$t0", "$t1", 63), round3!("$t1", "$t2", "$t3", "$t4", "$t0", 64), round3!("$t0", "$t1", "$t2", "$t3", "$t4", 65), round3!("$t4", "$t0", "$t1", "$t2", "$t3", 66), round3!("$t3", "$t4", "$t0", "$t1", "$t2", 67), round3!("$t2", "$t3", "$t4", "$t0", "$t1", 68), round3!("$t1", "$t2", "$t3", "$t4", "$t0", 69), round3!("$t0", "$t1", "$t2", "$t3", "$t4", 70), round3!("$t4", "$t0", "$t1", "$t2", "$t3", 71), round3!("$t3", "$t4", "$t0", "$t1", "$t2", 72), round3!("$t2", "$t3", "$t4", "$t0", "$t1", 73), round3!("$t1", "$t2", "$t3", "$t4", "$t0", 74), round3!("$t0", "$t1", "$t2", "$t3", "$t4", 75), round3!("$t4", "$t0", "$t1", "$t2", "$t3", 76), round3!("$t3", "$t4", "$t0", "$t1", "$t2", 77), round3!("$t2", "$t3", "$t4", "$t0", "$t1", 78), round3!("$t1", "$t2", "$t3", "$t4", "$t0", 79), // Update state registers "ld.w $t5, $a0, 0", // a "ld.w $t6, $a0, 4", // b "ld.w $t7, $a0, 8", // c "ld.w $t8, $a0, 12", // d "add.w $t0, $t0, $t5", "ld.w $t5, $a0, 16", // e "add.w $t1, $t1, $t6", "add.w $t2, $t2, $t7", "add.w $t3, $t3, $t8", "add.w $t4, $t4, $t5", // Save updated state "st.w $t0, $a0, 0", "st.w $t1, $a0, 4", "st.w $t2, $a0, 8", "st.w $t3, $a0, 12", "st.w $t4, $a0, 16", // Looping over blocks "addi.d $a1, $a1, 64", "addi.d $a2, $a2, -1", "bnez $a2, 42b", // Restore stack register "addi.d $sp, $sp, 64", in("$a0") state, inout("$a1") blocks.as_ptr() => _, inout("$a2") blocks.len() => _, in("$a4") K[0], in("$a5") K[1], in("$a6") K[2], in("$a7") K[3], // Clobbers out("$t0") _, out("$t1") _, out("$t2") _, out("$t3") _, out("$t4") _, out("$t5") _, out("$t6") _, out("$t7") _, out("$t8") _, options(preserves_flags), ); } } sha1-0.11.0/src/compress/soft.rs000064400000000000000000000155211046102023000145060ustar 00000000000000#![allow(clippy::many_single_char_names)] use crate::consts::K; #[inline(always)] fn add(a: [u32; 4], b: [u32; 4]) -> [u32; 4] { [ a[0].wrapping_add(b[0]), a[1].wrapping_add(b[1]), a[2].wrapping_add(b[2]), a[3].wrapping_add(b[3]), ] } #[inline(always)] fn xor(a: [u32; 4], b: [u32; 4]) -> [u32; 4] { [a[0] ^ b[0], a[1] ^ b[1], a[2] ^ b[2], a[3] ^ b[3]] } #[inline] fn sha1_first_add(e: u32, w0: [u32; 4]) -> [u32; 4] { let [a, b, c, d] = w0; [e.wrapping_add(a), b, c, d] } fn sha1msg1(a: [u32; 4], b: [u32; 4]) -> [u32; 4] { let [_, _, w2, w3] = a; let [w4, w5, _, _] = b; [a[0] ^ w2, a[1] ^ w3, a[2] ^ w4, a[3] ^ w5] } fn sha1msg2(a: [u32; 4], b: [u32; 4]) -> [u32; 4] { let [x0, x1, x2, x3] = a; let [_, w13, w14, w15] = b; let w16 = (x0 ^ w13).rotate_left(1); let w17 = (x1 ^ w14).rotate_left(1); let w18 = (x2 ^ w15).rotate_left(1); let w19 = (x3 ^ w16).rotate_left(1); [w16, w17, w18, w19] } #[inline] fn sha1_first_half(abcd: [u32; 4], msg: [u32; 4]) -> [u32; 4] { sha1_first_add(abcd[0].rotate_left(30), msg) } fn sha1_digest_round_x4(abcd: [u32; 4], work: [u32; 4], i: i8) -> [u32; 4] { match i { 0 => sha1rnds4c(abcd, add(work, [K[0]; 4])), 1 => sha1rnds4p(abcd, add(work, [K[1]; 4])), 2 => sha1rnds4m(abcd, add(work, [K[2]; 4])), 3 => sha1rnds4p(abcd, add(work, [K[3]; 4])), _ => unreachable!("unknown icosaround index"), } } fn sha1rnds4c(abcd: [u32; 4], msg: [u32; 4]) -> [u32; 4] { let [mut a, mut b, mut c, mut d] = abcd; let [t, u, v, w] = msg; let mut e = 0u32; macro_rules! bool3ary_202 { ($a:expr, $b:expr, $c:expr) => { $c ^ ($a & ($b ^ $c)) }; } // Choose, MD5F, SHA1C e = e .wrapping_add(a.rotate_left(5)) .wrapping_add(bool3ary_202!(b, c, d)) .wrapping_add(t); b = b.rotate_left(30); d = d .wrapping_add(e.rotate_left(5)) .wrapping_add(bool3ary_202!(a, b, c)) .wrapping_add(u); a = a.rotate_left(30); c = c .wrapping_add(d.rotate_left(5)) .wrapping_add(bool3ary_202!(e, a, b)) .wrapping_add(v); e = e.rotate_left(30); b = b .wrapping_add(c.rotate_left(5)) .wrapping_add(bool3ary_202!(d, e, a)) .wrapping_add(w); d = d.rotate_left(30); [b, c, d, e] } fn sha1rnds4p(abcd: [u32; 4], msg: [u32; 4]) -> [u32; 4] { let [mut a, mut b, mut c, mut d] = abcd; let [t, u, v, w] = msg; let mut e = 0u32; macro_rules! bool3ary_150 { ($a:expr, $b:expr, $c:expr) => { $a ^ $b ^ $c }; } // Parity, XOR, MD5H, SHA1P e = e .wrapping_add(a.rotate_left(5)) .wrapping_add(bool3ary_150!(b, c, d)) .wrapping_add(t); b = b.rotate_left(30); d = d .wrapping_add(e.rotate_left(5)) .wrapping_add(bool3ary_150!(a, b, c)) .wrapping_add(u); a = a.rotate_left(30); c = c .wrapping_add(d.rotate_left(5)) .wrapping_add(bool3ary_150!(e, a, b)) .wrapping_add(v); e = e.rotate_left(30); b = b .wrapping_add(c.rotate_left(5)) .wrapping_add(bool3ary_150!(d, e, a)) .wrapping_add(w); d = d.rotate_left(30); [b, c, d, e] } fn sha1rnds4m(abcd: [u32; 4], msg: [u32; 4]) -> [u32; 4] { let [mut a, mut b, mut c, mut d] = abcd; let [t, u, v, w] = msg; let mut e = 0u32; macro_rules! bool3ary_232 { ($a:expr, $b:expr, $c:expr) => { ($a & $b) ^ ($a & $c) ^ ($b & $c) }; } // Majority, SHA1M e = e .wrapping_add(a.rotate_left(5)) .wrapping_add(bool3ary_232!(b, c, d)) .wrapping_add(t); b = b.rotate_left(30); d = d .wrapping_add(e.rotate_left(5)) .wrapping_add(bool3ary_232!(a, b, c)) .wrapping_add(u); a = a.rotate_left(30); c = c .wrapping_add(d.rotate_left(5)) .wrapping_add(bool3ary_232!(e, a, b)) .wrapping_add(v); e = e.rotate_left(30); b = b .wrapping_add(c.rotate_left(5)) .wrapping_add(bool3ary_232!(d, e, a)) .wrapping_add(w); d = d.rotate_left(30); [b, c, d, e] } macro_rules! rounds4 { ($h0:ident, $h1:ident, $wk:expr, $i:expr) => { sha1_digest_round_x4($h0, sha1_first_half($h1, $wk), $i) }; } macro_rules! schedule { ($v0:expr, $v1:expr, $v2:expr, $v3:expr) => { sha1msg2(xor(sha1msg1($v0, $v1), $v2), $v3) }; } macro_rules! schedule_rounds4 { ( $h0:ident, $h1:ident, $w0:expr, $w1:expr, $w2:expr, $w3:expr, $w4:expr, $i:expr ) => { $w4 = schedule!($w0, $w1, $w2, $w3); $h1 = rounds4!($h0, $h1, $w4, $i); }; } #[inline(always)] fn digest_block(state: &mut [u32; 5], block: [u32; 16]) { let mut w0 = [block[0], block[1], block[2], block[3]]; let mut w1 = [block[4], block[5], block[6], block[7]]; let mut w2 = [block[8], block[9], block[10], block[11]]; let mut w3 = [block[12], block[13], block[14], block[15]]; #[allow(clippy::needless_late_init)] let mut w4; let mut h0 = [state[0], state[1], state[2], state[3]]; let mut h1 = sha1_first_add(state[4], w0); // Rounds 0..20 h1 = sha1_digest_round_x4(h0, h1, 0); h0 = rounds4!(h1, h0, w1, 0); h1 = rounds4!(h0, h1, w2, 0); h0 = rounds4!(h1, h0, w3, 0); schedule_rounds4!(h0, h1, w0, w1, w2, w3, w4, 0); // Rounds 20..40 schedule_rounds4!(h1, h0, w1, w2, w3, w4, w0, 1); schedule_rounds4!(h0, h1, w2, w3, w4, w0, w1, 1); schedule_rounds4!(h1, h0, w3, w4, w0, w1, w2, 1); schedule_rounds4!(h0, h1, w4, w0, w1, w2, w3, 1); schedule_rounds4!(h1, h0, w0, w1, w2, w3, w4, 1); // Rounds 40..60 schedule_rounds4!(h0, h1, w1, w2, w3, w4, w0, 2); schedule_rounds4!(h1, h0, w2, w3, w4, w0, w1, 2); schedule_rounds4!(h0, h1, w3, w4, w0, w1, w2, 2); schedule_rounds4!(h1, h0, w4, w0, w1, w2, w3, 2); schedule_rounds4!(h0, h1, w0, w1, w2, w3, w4, 2); // Rounds 60..80 schedule_rounds4!(h1, h0, w1, w2, w3, w4, w0, 3); schedule_rounds4!(h0, h1, w2, w3, w4, w0, w1, 3); schedule_rounds4!(h1, h0, w3, w4, w0, w1, w2, 3); schedule_rounds4!(h0, h1, w4, w0, w1, w2, w3, 3); schedule_rounds4!(h1, h0, w0, w1, w2, w3, w4, 3); let e = h1[0].rotate_left(30); let [a, b, c, d] = h0; state[0] = state[0].wrapping_add(a); state[1] = state[1].wrapping_add(b); state[2] = state[2].wrapping_add(c); state[3] = state[3].wrapping_add(d); state[4] = state[4].wrapping_add(e); } fn read_block(block: &[u8; 64]) -> [u32; 16] { core::array::from_fn(|i| { let chunk = &block[4 * i..][..4]; u32::from_be_bytes(chunk.try_into().unwrap()) }) } pub(crate) fn compress(state: &mut [u32; 5], blocks: &[[u8; 64]]) { for block in blocks.iter().map(read_block) { digest_block(state, block); } } sha1-0.11.0/src/compress/x86_sha.rs000064400000000000000000000064561046102023000150220ustar 00000000000000//! SHA-1 `x86`/`x86_64` backend #[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))] compile_error!("x86-sha backend can be used only on x86 and x86_64 target arches"); #[cfg(target_arch = "x86")] use core::arch::x86::*; #[cfg(target_arch = "x86_64")] use core::arch::x86_64::*; macro_rules! rounds4 { ($h0:ident, $h1:ident, $wk:expr, $i:expr) => { _mm_sha1rnds4_epu32($h0, _mm_sha1nexte_epu32($h1, $wk), $i) }; } macro_rules! schedule { ($v0:expr, $v1:expr, $v2:expr, $v3:expr) => { _mm_sha1msg2_epu32(_mm_xor_si128(_mm_sha1msg1_epu32($v0, $v1), $v2), $v3) }; } macro_rules! schedule_rounds4 { ( $h0:ident, $h1:ident, $w0:expr, $w1:expr, $w2:expr, $w3:expr, $w4:expr, $i:expr ) => { $w4 = schedule!($w0, $w1, $w2, $w3); $h1 = rounds4!($h0, $h1, $w4, $i); }; } #[target_feature(enable = "sha,sse2,ssse3,sse4.1")] #[allow(unsafe_op_in_unsafe_fn)] pub(crate) unsafe fn compress(state: &mut [u32; 5], blocks: &[[u8; 64]]) { #[allow(non_snake_case)] let MASK: __m128i = _mm_set_epi64x(0x0001_0203_0405_0607, 0x0809_0A0B_0C0D_0E0F); let mut state_abcd = _mm_loadu_si128(state.as_ptr().cast()); state_abcd = _mm_shuffle_epi32(state_abcd, 0b00011011); let mut state_e = _mm_set_epi32(state[4] as i32, 0, 0, 0); for block in blocks { let block_ptr: *const __m128i = block.as_ptr().cast(); let mut w0 = _mm_shuffle_epi8(_mm_loadu_si128(block_ptr.offset(0)), MASK); let mut w1 = _mm_shuffle_epi8(_mm_loadu_si128(block_ptr.offset(1)), MASK); let mut w2 = _mm_shuffle_epi8(_mm_loadu_si128(block_ptr.offset(2)), MASK); let mut w3 = _mm_shuffle_epi8(_mm_loadu_si128(block_ptr.offset(3)), MASK); #[allow(clippy::needless_late_init)] let mut w4; let mut h0 = state_abcd; let mut h1 = _mm_add_epi32(state_e, w0); // Rounds 0..20 h1 = _mm_sha1rnds4_epu32(h0, h1, 0); h0 = rounds4!(h1, h0, w1, 0); h1 = rounds4!(h0, h1, w2, 0); h0 = rounds4!(h1, h0, w3, 0); schedule_rounds4!(h0, h1, w0, w1, w2, w3, w4, 0); // Rounds 20..40 schedule_rounds4!(h1, h0, w1, w2, w3, w4, w0, 1); schedule_rounds4!(h0, h1, w2, w3, w4, w0, w1, 1); schedule_rounds4!(h1, h0, w3, w4, w0, w1, w2, 1); schedule_rounds4!(h0, h1, w4, w0, w1, w2, w3, 1); schedule_rounds4!(h1, h0, w0, w1, w2, w3, w4, 1); // Rounds 40..60 schedule_rounds4!(h0, h1, w1, w2, w3, w4, w0, 2); schedule_rounds4!(h1, h0, w2, w3, w4, w0, w1, 2); schedule_rounds4!(h0, h1, w3, w4, w0, w1, w2, 2); schedule_rounds4!(h1, h0, w4, w0, w1, w2, w3, 2); schedule_rounds4!(h0, h1, w0, w1, w2, w3, w4, 2); // Rounds 60..80 schedule_rounds4!(h1, h0, w1, w2, w3, w4, w0, 3); schedule_rounds4!(h0, h1, w2, w3, w4, w0, w1, 3); schedule_rounds4!(h1, h0, w3, w4, w0, w1, w2, 3); schedule_rounds4!(h0, h1, w4, w0, w1, w2, w3, 3); schedule_rounds4!(h1, h0, w0, w1, w2, w3, w4, 3); state_abcd = _mm_add_epi32(state_abcd, h0); state_e = _mm_sha1nexte_epu32(h1, state_e); } state_abcd = _mm_shuffle_epi32(state_abcd, 0b00011011); _mm_storeu_si128(state.as_mut_ptr().cast(), state_abcd); state[4] = _mm_extract_epi32(state_e, 3) as u32; } sha1-0.11.0/src/compress.rs000064400000000000000000000046541046102023000135400ustar 00000000000000cfg_if::cfg_if! { if #[cfg(sha1_backend = "soft")] { mod soft; pub(crate) use soft::compress; } else if #[cfg(sha1_backend = "aarch64-sha2")] { mod aarch64_sha2; #[cfg(not(target_feature = "sha2"))] compile_error!("aarch64-sha2 backend requires sha2 target feature"); pub(crate) fn compress(state: &mut [u32; 5], blocks: &[[u8; 64]]) { // SAFETY: we checked above that the required target features are enabled unsafe { aarch64_sha2::compress(state, blocks) } } } else if #[cfg(sha1_backend = "x86-sha")] { mod x86_sha; #[cfg(not(all( target_feature = "sha", target_feature = "sse2", target_feature = "ssse3", target_feature = "sse4.1", )))] compile_error!("x86-sha backend requires sha, sse2, ssse3, sse4.1 target features"); pub(crate) fn compress(state: &mut [u32; 5], blocks: &[[u8; 64]]) { // SAFETY: we checked above that the required target features are enabled unsafe { x86_sha::compress(state, blocks) } } } else if #[cfg(target_arch = "loongarch64")] { mod loongarch64_asm; pub(crate) use loongarch64_asm::compress; } else { mod soft; cfg_if::cfg_if! { if #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] { mod x86_sha; cpufeatures::new!(shani_cpuid, "sha", "sse2", "ssse3", "sse4.1"); } else if #[cfg(target_arch = "aarch64")] { mod aarch64_sha2; cpufeatures::new!(sha2_hwcap, "sha2"); } } pub(crate) fn compress(state: &mut [u32; 5], blocks: &[[u8; 64]]) { cfg_if::cfg_if! { if #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] { if shani_cpuid::get() { // SAFETY: we checked that required target features are available return unsafe { x86_sha::compress(state, blocks) }; } } else if #[cfg(target_arch = "aarch64")] { if sha2_hwcap::get() { // SAFETY: we checked that `sha2` target feature is available return unsafe { aarch64_sha2::compress(state, blocks) }; } } } soft::compress(state, blocks); } } } sha1-0.11.0/src/consts.rs000064400000000000000000000004001046102023000131770ustar 00000000000000pub(crate) const STATE_LEN: usize = 5; pub(crate) type State = [u32; STATE_LEN]; pub(crate) const H0: State = [0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0]; pub(crate) const K: [u32; 4] = [0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xCA62C1D6]; sha1-0.11.0/src/lib.rs000064400000000000000000000010731046102023000124430ustar 00000000000000#![no_std] #![doc = include_str!("../README.md")] #![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" )] #![cfg_attr(docsrs, feature(doc_cfg))] #![warn(missing_docs, unreachable_pub)] pub use digest::{self, Digest}; /// Block-level types pub mod block_api; mod compress; mod consts; digest::buffer_fixed!( /// SHA-1 hasher. pub struct Sha1(block_api::Sha1Core); oid: "1.3.14.3.2.26"; impl: FixedHashTraits; ); sha1-0.11.0/tests/data/sha1_kat.blb000064400000000000000000000003531046102023000147670ustar 00000000000000abc(>6Gj>%qxPl؝pabcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq(>D;nJQ)FpVThe quick brown fox jumps over the lazy dog(/z-(턞v9VThe quick brown fox jumps over the lazy cog(ޟ,^:Z } sha1-0.11.0/tests/data/sha1_serialization.bin000064400000000000000000000001341046102023000170730ustar 00000000000000Ъp? & Psha1-0.11.0/tests/mod.rs000064400000000000000000000006251046102023000130310ustar 00000000000000use digest::dev::{feed_rand_16mib, fixed_reset_test}; use hex_literal::hex; use sha1::{Digest, Sha1}; digest::new_test!(sha1_kat, Sha1, fixed_reset_test); digest::hash_serialization_test!(sha1_serialization, Sha1); #[test] fn sha1_rand() { let mut h = Sha1::new(); feed_rand_16mib(&mut h); assert_eq!( h.finalize(), hex!("7e565a25a8b123e9881addbcedcd927b23377a78"), ); }