From 3aabdda31855e224a9e299c623ee7845df065296 Mon Sep 17 00:00:00 2001 From: Alexandr Kitaev Date: Wed, 22 Oct 2025 13:52:14 +0300 Subject: [PATCH 01/16] Implement STB 34.101.77-2020 --- .github/workflows/belt-bash.yml | 92 +++++++++ Cargo.lock | 4 + Cargo.toml | 1 + belt-bash/CHANGELOG.md | 8 + belt-bash/Cargo.toml | 16 ++ belt-bash/LICENSE-APACHE | 201 +++++++++++++++++++ belt-bash/LICENSE-MIT | 25 +++ belt-bash/README.md | 50 +++++ belt-bash/src/lib.rs | 337 ++++++++++++++++++++++++++++++++ 9 files changed, 734 insertions(+) create mode 100644 .github/workflows/belt-bash.yml create mode 100644 belt-bash/CHANGELOG.md create mode 100644 belt-bash/Cargo.toml create mode 100644 belt-bash/LICENSE-APACHE create mode 100644 belt-bash/LICENSE-MIT create mode 100644 belt-bash/README.md create mode 100644 belt-bash/src/lib.rs diff --git a/.github/workflows/belt-bash.yml b/.github/workflows/belt-bash.yml new file mode 100644 index 0000000..2ad851f --- /dev/null +++ b/.github/workflows/belt-bash.yml @@ -0,0 +1,92 @@ +name: belt-bash + +on: + pull_request: + paths: + - ".github/workflows/belt-bash.yml" + - "belt-bash/**" + - "Cargo.*" + push: + branches: master + +defaults: + run: + working-directory: belt-bash + +env: + RUSTFLAGS: "-Dwarnings" + CARGO_INCREMENTAL: 0 + +jobs: + set-msrv: + uses: RustCrypto/actions/.github/workflows/set-msrv.yml@master + with: + msrv: 1.85.0 + + build: + needs: set-msrv + runs-on: ubuntu-latest + strategy: + matrix: + rust: + - ${{needs.set-msrv.outputs.msrv}} + - stable + target: + - thumbv7em-none-eabi + - wasm32-unknown-unknown + steps: + - uses: actions/checkout@v4 + - uses: RustCrypto/actions/cargo-cache@master + - uses: dtolnay/rust-toolchain@master + with: + toolchain: ${{ matrix.rust }} + targets: ${{ matrix.target }} + - run: cargo build --no-default-features --target ${{ matrix.target }} + + minimal-versions: + uses: RustCrypto/actions/.github/workflows/minimal-versions.yml@master + with: + working-directory: ${{ github.workflow }} + + test: + needs: set-msrv + runs-on: ubuntu-latest + strategy: + matrix: + rust: + - ${{needs.set-msrv.outputs.msrv}} + - stable + steps: + - uses: actions/checkout@v4 + - uses: RustCrypto/actions/cargo-cache@master + - uses: dtolnay/rust-toolchain@master + with: + toolchain: ${{ matrix.rust }} + - run: cargo test --no-default-features + - run: cargo test + - run: cargo test --all-features + + miri: + runs-on: ubuntu-latest + env: + MIRIFLAGS: "-Zmiri-symbolic-alignment-check -Zmiri-strict-provenance" + strategy: + matrix: + target: + - x86_64-unknown-linux-gnu + - s390x-unknown-linux-gnu + steps: + - uses: actions/checkout@v4 + - uses: RustCrypto/actions/cargo-cache@master + - uses: dtolnay/rust-toolchain@master + with: + toolchain: nightly + - name: Install Miri + run: | + rustup component add miri + cargo miri setup + - name: Test with Miri + run: | + cargo miri test --target ${{ matrix.target }} --no-default-features + cargo miri test --target ${{ matrix.target }} + cargo miri test --target ${{ matrix.target }} --all-features diff --git a/Cargo.lock b/Cargo.lock index 3d67e6f..b49f700 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -9,6 +9,10 @@ dependencies = [ "zeroize", ] +[[package]] +name = "bash" +version = "0.1.0-rc.0" + [[package]] name = "cpufeatures" version = "0.2.17" diff --git a/Cargo.toml b/Cargo.toml index cf8ac35..66c9f59 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,5 +2,6 @@ resolver = "2" members = [ "ascon", + "belt-bash", "keccak", ] diff --git a/belt-bash/CHANGELOG.md b/belt-bash/CHANGELOG.md new file mode 100644 index 0000000..1bc6963 --- /dev/null +++ b/belt-bash/CHANGELOG.md @@ -0,0 +1,8 @@ +# 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.1.0 (UNRELEASED) diff --git a/belt-bash/Cargo.toml b/belt-bash/Cargo.toml new file mode 100644 index 0000000..388c4a4 --- /dev/null +++ b/belt-bash/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "bash" +version = "0.1.0-rc.0" +description = "Pure Rust implementation of the BASH algorithms defined in STB 34.101.77-2020" +license = "Apache-2.0 OR MIT" +authors = ["RustCrypto Developers"] +documentation = "https://docs.rs/bash" +homepage = "https://github.com/RustCrypto/sponges/tree/master/bash" +repository = "https://github.com/RustCrypto/sponges" +keywords = ["Bash", "belt", "permutation"] +categories = ["cryptography", "no-std"] +readme = "README.md" +edition = "2024" +rust-version = "1.85" + +[dependencies] diff --git a/belt-bash/LICENSE-APACHE b/belt-bash/LICENSE-APACHE new file mode 100644 index 0000000..59aa0f4 --- /dev/null +++ b/belt-bash/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 2025 The RustCrypto Project Developers + +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. \ No newline at end of file diff --git a/belt-bash/LICENSE-MIT b/belt-bash/LICENSE-MIT new file mode 100644 index 0000000..3b2f02c --- /dev/null +++ b/belt-bash/LICENSE-MIT @@ -0,0 +1,25 @@ +Copyright (c) 2025 The RustCrypto Project Developers + +Permission is hereby granted, free of charge, to any +person obtaining a copy of this software and associated +documentation files (the "Software"), to deal in the +Software without restriction, including without +limitation the rights to use, copy, modify, merge, +publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software +is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice +shall be included in all copies or substantial portions +of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF +ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED +TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A +PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT +SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR +IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/belt-bash/README.md b/belt-bash/README.md new file mode 100644 index 0000000..f101e01 --- /dev/null +++ b/belt-bash/README.md @@ -0,0 +1,50 @@ +# RustCrypto: BeltBash + +[![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 `bash` sponge algorithms +specified in the republic of Belarus standard [STB 34.101.77-2020]. + +## Security Notes + +No security audits of this crate have ever been performed, and it has not been thoroughly assessed to ensure its operation is constant-time on common CPU architectures. + +USE AT YOUR OWN RISK! + +## License + +Licensed under either of: + +* [Apache License, Version 2.0](https://www.apache.org/licenses/LICENSE-2.0) +* [MIT license](https://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://buildstats.info/crate/belt-bash +[crate-link]: https://crates.io/crates/belt-bash +[docs-image]: https://docs.rs/belt-bash/badge.svg +[docs-link]: https://docs.rs/belt-bash/ +[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/260038-AEADs +[downloads-image]: https://img.shields.io/crates/d/chacha20poly1305.svg +[build-image]: https://github.com/RustCrypto/AEADs/workflows/belt-bash/badge.svg?branch=master&event=push +[build-link]: https://github.com/RustCrypto/AEADs/actions + +[//]: # (general links) + +[STB 34.101.77-2020]: https://apmi.bsu.by/assets/files/std/bash-spec241.pdf \ No newline at end of file diff --git a/belt-bash/src/lib.rs b/belt-bash/src/lib.rs new file mode 100644 index 0000000..52b1a51 --- /dev/null +++ b/belt-bash/src/lib.rs @@ -0,0 +1,337 @@ +//! # BelHash - STB 34.101.77-2020 Sponge-Based Cryptographic Algorithms +//! +//! This module implements the cryptographic algorithms defined in the Belarusian +//! state standard STB 34.101.77-2020 "Sponge-based cryptographic algorithms". +//! +//! ## Overview +//! +//! The standard defines a family of cryptographic algorithms built on a sponge +//! construction with the `bash-f` sponge function at its core. The sponge function +//! operates on 1536-bit (192-byte) states. +//! +//! ## Components +//! +//! ### Core Primitives +//! +//! - **bash-s**: S-box transformation operating on three 64-bit words +//! - **bash-f**: Sponge permutation function (24 rounds) +//! +//! ### High-Level Algorithms +//! +//! - Hash functions (128-256 bit security levels) +//! - Authenticated encryption +//! - AEAD (Authenticated Encryption with Associated Data) +//! +//! ## Security Levels +//! +//! The standard supports three security levels: +//! - ℓ = 128 bits +//! - ℓ = 192 bits +//! - ℓ = 256 bits +//! +//! ## References +//! +//! - STB 34.101.77-2020 specification +//! - [Official standard](http://apmi.bsu.by/assets/files/std/bash-spec324.pdf) +//! +//! ## Note on Byte Order +//! +//! The specification uses big-endian representation for test vectors, while +//! internal computation uses little-endian. +//! The public API handles byte swapping automatically. + +#![no_std] +#![cfg_attr(docsrs, feature(doc_cfg))] +#![doc( + html_logo_url = "https://raw.githubusercontent.com/RustCrypto/meta/master/logo.svg", + html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/meta/master/logo.svg" +)] +#![allow(non_upper_case_globals)] +#![warn( + clippy::mod_module_files, + clippy::unwrap_used, + missing_docs, + rust_2018_idioms, + unused_lifetimes, + unused_qualifications +)] + +/// Number of 64-bit words in the state +const STATE_WORDS: usize = 24; + +/// Internal bash-s transformation. +/// +/// Implements the S-box transformation defined in Section 6.1 of STB 34.101.77-2020. +/// This is the core non-linear transformation used in the bash-f sponge function. +fn bash_s_internal( + mut w0: u64, + mut w1: u64, + mut w2: u64, + m1: u32, + n1: u32, + m2: u32, + n2: u32, +) -> (u64, u64, u64) { + // 1. T0 ← RotHi^m1(W0) + let t0 = w0.rotate_left(m1); + + // 2. W0 ← W0 ⊕ W1 ⊕ W2 + w0 ^= w1 ^ w2; + + // 3. T1 ← W1 ⊕ RotHi^n1(W0) + let t1 = w1 ^ w0.rotate_left(n1); + + // 4. W1 ← T0 ⊕ T1 + w1 = t0 ^ t1; + + // 5. W2 ← W2 ⊕ RotHi^m2(W2) ⊕ RotHi^n2(T1) + w2 ^= w2.rotate_left(m2) ^ t1.rotate_left(n2); + + // 6. T0 ← ¬W2 + let t0 = !w2; + + // 7. T1 ← W0 ∨ W2 + let t1 = w0 | w2; + + // 8. T2 ← W0 ∧ W1 + let t2 = w0 & w1; + + // 9. T0 ← T0 ∨ W1 + let t0 = t0 | w1; + + // 10. W1 ← W1 ⊕ T1 + w1 ^= t1; + + // 11. W2 ← W2 ⊕ T2 + w2 ^= t2; + + // 12. W0 ← W0 ⊕ T0 + w0 ^= t0; + + // 13. Return (W0, W1, W2) + (w0, w1, w2) +} + +/// bash-s transformation with standard-compliant byte order. +/// +/// This is the public interface to the bash-s algorithm as defined in +/// Section 6.1 of STB 34.101.77-2020. It handles conversion between +/// the standard's big-endian representation and the internal little-endian +/// computation. +/// ```rust +/// use bash::bash_s; +/// let w0 = 0xB194BAC80A08F53B; +/// let w1 = 0xE12BDC1AE28257EC; +/// let w2 = 0xE9DEE72C8F0C0FA6; +/// let (r0, r1, r2) = bash_s(w0, w1, w2, 8, 53, 14, 1); +/// assert_eq!(r0, 0x479E76129979DC5F); +/// assert_eq!(r1, 0x0F2B2C93ED128EDD); +/// assert_eq!(r2, 0x41009B1B112DFEF3); +/// ``` +pub fn bash_s(w0: u64, w1: u64, w2: u64, m1: u32, n1: u32, m2: u32, n2: u32) -> (u64, u64, u64) { + // Convert from big-endian (standard) to little-endian (internal) + let (w0_out, w1_out, w2_out) = bash_s_internal( + w0.swap_bytes(), + w1.swap_bytes(), + w2.swap_bytes(), + m1, + n1, + m2, + n2, + ); + + // Convert back to big-endian for output + ( + w0_out.swap_bytes(), + w1_out.swap_bytes(), + w2_out.swap_bytes(), + ) +} + +/// Internal bash-f sponge permutation. +/// +/// Implements the core sponge function defined in Section 6.2 of STB 34.101.77-2020. +/// This is a cryptographic permutation that operates on 1536-bit states. +/// +/// # Parameters +/// +/// - `state`: Mutable reference to 24 × 64-bit words (1536 bits total) in little-endian internal representation +fn bash_f_internal(state: &mut [u64; STATE_WORDS]) { + // 1. Split S into words (S0, S1, ..., S23) + + // 2. C ← B194BAC80A08F53B (initialize round constant, swapped to little-endian) + let mut c: u64 = 0xB194BAC80A08F53Bu64.swap_bytes(); + + // 3. For i = 1, 2, ..., 24 perform 24 rounds + for _ in 0..STATE_WORDS { + // 3.1. Apply S-box layer with varying rotation parameters + // (m1, n1, m2, n2) ← (8, 53, 14, 1) + let mut m1 = 8u32; + let mut n1 = 53u32; + let mut m2 = 14u32; + let mut n2 = 1u32; + + // 3.2. For j = 0, 1, ..., 7 apply bash-s to each of 8 columns + for j in 0..8 { + // 3.2.a. (Sj, S8+j, S16+j) ← bash-s(Sj, S8+j, S16+j, m1, n1, m2, n2) + let (s0, s1, s2) = + bash_s_internal(state[j], state[8 + j], state[16 + j], m1, n1, m2, n2); + state[j] = s0; + state[8 + j] = s1; + state[16 + j] = s2; + + // 3.2.b. (m1, n1, m2, n2) ← (7·m1 mod 64, 7·n1 mod 64, 7·m2 mod 64, 7·n2 mod 64) + m1 = (7 * m1) % 64; + n1 = (7 * n1) % 64; + m2 = (7 * m2) % 64; + n2 = (7 * n2) % 64; + } + + // 3.3. Apply word permutation π + // S ← S15 ‖ S10 ‖ S9 ‖ S12 ‖ S11 ‖ S14 ‖ S13 ‖ S8 ‖ + // S17 ‖ S16 ‖ S19 ‖ S18 ‖ S21 ‖ S20 ‖ S23 ‖ S22 ‖ + // S6 ‖ S3 ‖ S0 ‖ S5 ‖ S2 ‖ S7 ‖ S4 ‖ S1 + let temp = [ + state[15], state[10], state[9], state[12], state[11], state[14], state[13], state[8], + state[17], state[16], state[19], state[18], state[21], state[20], state[23], state[22], + state[6], state[3], state[0], state[5], state[2], state[7], state[4], state[1], + ]; + state.copy_from_slice(&temp); + + // 3.4. S23 ← S23 ⊕ C (add round constant) + state[23] ^= c; + + // 3.5. Update LFSR (Galois configuration) + // if ⌊C⌉ is even, then C ← ShLo(C) + // else C ← ShLo(C) ⊕ AED8E07F99E12BDC + if c & 1 == 0 { + c >>= 1; + } else { + c = (c >> 1) ^ 0xAED8E07F99E12BDCu64.swap_bytes(); + } + } + + // 4. Return S - state is modified in place +} + +/// bash-f sponge function with standard-compliant interface. +/// +/// This is the public interface as specified in Section 6.2 of STB 34.101.77-2020. +/// It accepts and returns states in big-endian byte order as per the standard. +/// +/// # Parameters +/// +/// - `state`: Mutable array of 24 × 64-bit words (192 bytes total) in **big-endian** byte order +/// +/// # Side Effects +/// +/// Transforms the state in-place through 24 rounds of the sponge permutation. +/// +/// # Example from Test Vector (Table A.2) +/// +/// ```rust +/// use bash::bash_f; +/// let mut state: [u64; 24] = [ +/// 0xB194BAC80A08F53B, 0x366D008E584A5DE4, 0x8504FA9D1BB6C7AC, 0x252E72C202FDCE0D, +/// 0x5BE3D61217B96181, 0xFE6786AD716B890B, 0x5CB0C0FF33C356B8, 0x35C405AED8E07F99, +/// 0xE12BDC1AE28257EC, 0x703FCCF095EE8DF1, 0xC1AB76389FE678CA, 0xF7C6F860D5BB9C4F, +/// 0xF33C657B637C306A, 0xDD4EA7799EB23D31, 0x3E98B56E27D3BCCF, 0x591E181F4C5AB793, +/// 0xE9DEE72C8F0C0FA6, 0x2DDB49F46F739647, 0x06075316ED247A37, 0x39CBA38303A98BF6, +/// 0x92BD9B1CE5D14101, 0x5445FBC95E4D0EF2, 0x682080AA227D642F, 0x2687F93490405511, +/// ]; +/// bash_f(&mut state); +/// assert_eq!(state[0], 0x8FE727775EA7F140); +/// // ... verify remaining words +/// ``` +pub fn bash_f(state: &mut [u64; 24]) { + state.iter_mut().for_each(|s| *s = s.swap_bytes()); + bash_f_internal(state); + state.iter_mut().for_each(|s| *s = s.swap_bytes()); +} + +#[cfg(test)] +mod tests { + use super::*; + + /// Test vector from Table A.1 of STB 34.101.77-2020. + #[test] + fn test_bash_s_table_a1() { + let w0 = 0xB194BAC80A08F53B; + let w1 = 0xE12BDC1AE28257EC; + let w2 = 0xE9DEE72C8F0C0FA6; + + let (w0_out, w1_out, w2_out) = bash_s(w0, w1, w2, 8, 53, 14, 1); + + assert_eq!(w0_out, 0x479E76129979DC5F); + assert_eq!(w1_out, 0x0F2B2C93ED128EDD); + assert_eq!(w2_out, 0x41009B1B112DFEF3); + } + + /// Test vector from Table A.2 of STB 34.101.77-2020. + #[test] + fn test_bash_f_table_a2() { + let mut state: [u64; 24] = [ + 0xB194BAC80A08F53B, + 0x366D008E584A5DE4, + 0x8504FA9D1BB6C7AC, + 0x252E72C202FDCE0D, + 0x5BE3D61217B96181, + 0xFE6786AD716B890B, + 0x5CB0C0FF33C356B8, + 0x35C405AED8E07F99, + 0xE12BDC1AE28257EC, + 0x703FCCF095EE8DF1, + 0xC1AB76389FE678CA, + 0xF7C6F860D5BB9C4F, + 0xF33C657B637C306A, + 0xDD4EA7799EB23D31, + 0x3E98B56E27D3BCCF, + 0x591E181F4C5AB793, + 0xE9DEE72C8F0C0FA6, + 0x2DDB49F46F739647, + 0x06075316ED247A37, + 0x39CBA38303A98BF6, + 0x92BD9B1CE5D14101, + 0x5445FBC95E4D0EF2, + 0x682080AA227D642F, + 0x2687F93490405511, + ]; + + bash_f(&mut state); + + let expected: [u64; 24] = [ + 0x8FE727775EA7F140, + 0xB95BB6A200CBB28C, + 0x7F0809C0C0BC68B7, + 0xDC5AEDC841BD94E4, + 0x03630C301FC255DF, + 0x5B67DB53EF65E376, + 0xE8A4D797A6172F22, + 0x71BA48093173D329, + 0xC3502AC946767326, + 0xA2891971392D3F70, + 0x89959F5D61621238, + 0x655975E00E2132A0, + 0xD5018CEEDB17731C, + 0xCD88FC50151D37C0, + 0xD4A3359506AEDC2E, + 0x6109511E7703AFBB, + 0x014642348D8568AA, + 0x1A5D9868C4C7E6DF, + 0xA756B1690C7C2608, + 0xA2DC136F5997AB8F, + 0xBB3F4D9F033C87CA, + 0x6070E117F099C409, + 0x4972ACD9D976214B, + 0x7CED8E3F8B6E058E, + ]; + + for i in 0..24 { + assert_eq!( + state[i], expected[i], + "Mismatch at S[{}]: got {:016X}, expected {:016X}", + i, state[i], expected[i] + ); + } + } +} From 237cce8613a7d4ad30965fb3b2058d6cb1054962 Mon Sep 17 00:00:00 2001 From: Alexandr Kitaev Date: Wed, 22 Oct 2025 16:22:05 +0300 Subject: [PATCH 02/16] `belt-bash`: fixes according to the review --- Cargo.lock | 2 +- belt-bash/CHANGELOG.md | 3 ++ belt-bash/Cargo.toml | 12 ++++++-- belt-bash/README.md | 18 ++++++++++-- belt-bash/src/lib.rs | 63 +++++------------------------------------- 5 files changed, 37 insertions(+), 61 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b49f700..8bcaac3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10,7 +10,7 @@ dependencies = [ ] [[package]] -name = "bash" +name = "belt-bash" version = "0.1.0-rc.0" [[package]] diff --git a/belt-bash/CHANGELOG.md b/belt-bash/CHANGELOG.md index 1bc6963..7c2ac0d 100644 --- a/belt-bash/CHANGELOG.md +++ b/belt-bash/CHANGELOG.md @@ -6,3 +6,6 @@ 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.1.0 (UNRELEASED) +- Initial release ([#92]) + +[#92]: (https://github.com/RustCrypto/sponges/pull/92) \ No newline at end of file diff --git a/belt-bash/Cargo.toml b/belt-bash/Cargo.toml index 388c4a4..08f30ec 100644 --- a/belt-bash/Cargo.toml +++ b/belt-bash/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "bash" +name = "belt-bash" version = "0.1.0-rc.0" description = "Pure Rust implementation of the BASH algorithms defined in STB 34.101.77-2020" license = "Apache-2.0 OR MIT" @@ -13,4 +13,12 @@ readme = "README.md" edition = "2024" rust-version = "1.85" -[dependencies] +[lints.rust] +missing_docs = "warn" +rust_2018_idioms = "warn" +unused_lifetimes = "warn" +unused_qualifications = "warn" + +[lints.clippy] +mod_module_files = "warn" +unwrap_used = "warn" diff --git a/belt-bash/README.md b/belt-bash/README.md index f101e01..fd2e873 100644 --- a/belt-bash/README.md +++ b/belt-bash/README.md @@ -7,8 +7,22 @@ [![Project Chat][chat-image]][chat-link] [![Build Status][build-image]][build-link] -Pure Rust implementation of the `bash` sponge algorithms -specified in the republic of Belarus standard [STB 34.101.77-2020]. +# BelHash - [STB 34.101.77-2020] Sponge-Based Cryptographic Algorithms + +This module implements the cryptographic algorithms defined in the Belarusian +state standard STB 34.101.77-2020 "Sponge-based cryptographic algorithms". + +## Overview + +The standard defines a family of cryptographic algorithms built on a sponge +construction with the `bash-f` sponge function at its core. The sponge function +operates on 1536-bit (192-byte) states. + +## Note on Byte Order + +The specification uses big-endian representation for test vectors, while +internal computation uses little-endian. +The public API handles byte swapping automatically. ## Security Notes diff --git a/belt-bash/src/lib.rs b/belt-bash/src/lib.rs index 52b1a51..a20b4f4 100644 --- a/belt-bash/src/lib.rs +++ b/belt-bash/src/lib.rs @@ -1,68 +1,19 @@ -//! # BelHash - STB 34.101.77-2020 Sponge-Based Cryptographic Algorithms -//! -//! This module implements the cryptographic algorithms defined in the Belarusian -//! state standard STB 34.101.77-2020 "Sponge-based cryptographic algorithms". -//! -//! ## Overview -//! -//! The standard defines a family of cryptographic algorithms built on a sponge -//! construction with the `bash-f` sponge function at its core. The sponge function -//! operates on 1536-bit (192-byte) states. -//! -//! ## Components -//! -//! ### Core Primitives -//! -//! - **bash-s**: S-box transformation operating on three 64-bit words -//! - **bash-f**: Sponge permutation function (24 rounds) -//! -//! ### High-Level Algorithms -//! -//! - Hash functions (128-256 bit security levels) -//! - Authenticated encryption -//! - AEAD (Authenticated Encryption with Associated Data) -//! -//! ## Security Levels -//! -//! The standard supports three security levels: -//! - ℓ = 128 bits -//! - ℓ = 192 bits -//! - ℓ = 256 bits -//! -//! ## References -//! -//! - STB 34.101.77-2020 specification -//! - [Official standard](http://apmi.bsu.by/assets/files/std/bash-spec324.pdf) -//! -//! ## Note on Byte Order -//! -//! The specification uses big-endian representation for test vectors, while -//! internal computation uses little-endian. -//! The public API handles byte swapping automatically. - #![no_std] #![cfg_attr(docsrs, feature(doc_cfg))] +#![doc = include_str!("../README.md")] #![doc( html_logo_url = "https://raw.githubusercontent.com/RustCrypto/meta/master/logo.svg", html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/meta/master/logo.svg" )] #![allow(non_upper_case_globals)] -#![warn( - clippy::mod_module_files, - clippy::unwrap_used, - missing_docs, - rust_2018_idioms, - unused_lifetimes, - unused_qualifications -)] /// Number of 64-bit words in the state const STATE_WORDS: usize = 24; -/// Internal bash-s transformation. +/// Internal `bash-s` transformation. /// /// Implements the S-box transformation defined in Section 6.1 of STB 34.101.77-2020. -/// This is the core non-linear transformation used in the bash-f sponge function. +/// This is the core non-linear transformation used in the `bash-f` sponge function. fn bash_s_internal( mut w0: u64, mut w1: u64, @@ -112,9 +63,9 @@ fn bash_s_internal( (w0, w1, w2) } -/// bash-s transformation with standard-compliant byte order. +/// `bash-s` transformation with standard-compliant byte order. /// -/// This is the public interface to the bash-s algorithm as defined in +/// This is the public interface to the `bash-s` algorithm as defined in /// Section 6.1 of STB 34.101.77-2020. It handles conversion between /// the standard's big-endian representation and the internal little-endian /// computation. @@ -148,7 +99,7 @@ pub fn bash_s(w0: u64, w1: u64, w2: u64, m1: u32, n1: u32, m2: u32, n2: u32) -> ) } -/// Internal bash-f sponge permutation. +/// Internal `bash-f` sponge permutation. /// /// Implements the core sponge function defined in Section 6.2 of STB 34.101.77-2020. /// This is a cryptographic permutation that operates on 1536-bit states. @@ -214,7 +165,7 @@ fn bash_f_internal(state: &mut [u64; STATE_WORDS]) { // 4. Return S - state is modified in place } -/// bash-f sponge function with standard-compliant interface. +/// `bash-f` sponge function with standard-compliant interface. /// /// This is the public interface as specified in Section 6.2 of STB 34.101.77-2020. /// It accepts and returns states in big-endian byte order as per the standard. From cd4be798c339f05b2b964c6e5dd7a9b36f58b251 Mon Sep 17 00:00:00 2001 From: Alexandr Kitaev Date: Wed, 22 Oct 2025 16:24:06 +0300 Subject: [PATCH 03/16] `belt-bash`: rename crate in doc test --- belt-bash/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/belt-bash/src/lib.rs b/belt-bash/src/lib.rs index a20b4f4..80d4998 100644 --- a/belt-bash/src/lib.rs +++ b/belt-bash/src/lib.rs @@ -70,7 +70,7 @@ fn bash_s_internal( /// the standard's big-endian representation and the internal little-endian /// computation. /// ```rust -/// use bash::bash_s; +/// use belt_bash::bash_s; /// let w0 = 0xB194BAC80A08F53B; /// let w1 = 0xE12BDC1AE28257EC; /// let w2 = 0xE9DEE72C8F0C0FA6; @@ -181,7 +181,7 @@ fn bash_f_internal(state: &mut [u64; STATE_WORDS]) { /// # Example from Test Vector (Table A.2) /// /// ```rust -/// use bash::bash_f; +/// use belt_bash::bash_f; /// let mut state: [u64; 24] = [ /// 0xB194BAC80A08F53B, 0x366D008E584A5DE4, 0x8504FA9D1BB6C7AC, 0x252E72C202FDCE0D, /// 0x5BE3D61217B96181, 0xFE6786AD716B890B, 0x5CB0C0FF33C356B8, 0x35C405AED8E07F99, From 8d258ff2c516b50298236a030327e51f7d4b0457 Mon Sep 17 00:00:00 2001 From: Alexandr Kitaev Date: Wed, 22 Oct 2025 16:52:23 +0300 Subject: [PATCH 04/16] `belt-bash`: mv `bash_s_internal` -> `bash_s`, make it private --- belt-bash/src/lib.rs | 60 +++++++++----------------------------------- 1 file changed, 12 insertions(+), 48 deletions(-) diff --git a/belt-bash/src/lib.rs b/belt-bash/src/lib.rs index 80d4998..2be6ba6 100644 --- a/belt-bash/src/lib.rs +++ b/belt-bash/src/lib.rs @@ -10,11 +10,11 @@ /// Number of 64-bit words in the state const STATE_WORDS: usize = 24; -/// Internal `bash-s` transformation. +/// `bash-s` transformation. /// /// Implements the S-box transformation defined in Section 6.1 of STB 34.101.77-2020. /// This is the core non-linear transformation used in the `bash-f` sponge function. -fn bash_s_internal( +fn bash_s( mut w0: u64, mut w1: u64, mut w2: u64, @@ -63,42 +63,6 @@ fn bash_s_internal( (w0, w1, w2) } -/// `bash-s` transformation with standard-compliant byte order. -/// -/// This is the public interface to the `bash-s` algorithm as defined in -/// Section 6.1 of STB 34.101.77-2020. It handles conversion between -/// the standard's big-endian representation and the internal little-endian -/// computation. -/// ```rust -/// use belt_bash::bash_s; -/// let w0 = 0xB194BAC80A08F53B; -/// let w1 = 0xE12BDC1AE28257EC; -/// let w2 = 0xE9DEE72C8F0C0FA6; -/// let (r0, r1, r2) = bash_s(w0, w1, w2, 8, 53, 14, 1); -/// assert_eq!(r0, 0x479E76129979DC5F); -/// assert_eq!(r1, 0x0F2B2C93ED128EDD); -/// assert_eq!(r2, 0x41009B1B112DFEF3); -/// ``` -pub fn bash_s(w0: u64, w1: u64, w2: u64, m1: u32, n1: u32, m2: u32, n2: u32) -> (u64, u64, u64) { - // Convert from big-endian (standard) to little-endian (internal) - let (w0_out, w1_out, w2_out) = bash_s_internal( - w0.swap_bytes(), - w1.swap_bytes(), - w2.swap_bytes(), - m1, - n1, - m2, - n2, - ); - - // Convert back to big-endian for output - ( - w0_out.swap_bytes(), - w1_out.swap_bytes(), - w2_out.swap_bytes(), - ) -} - /// Internal `bash-f` sponge permutation. /// /// Implements the core sponge function defined in Section 6.2 of STB 34.101.77-2020. @@ -111,7 +75,7 @@ fn bash_f_internal(state: &mut [u64; STATE_WORDS]) { // 1. Split S into words (S0, S1, ..., S23) // 2. C ← B194BAC80A08F53B (initialize round constant, swapped to little-endian) - let mut c: u64 = 0xB194BAC80A08F53Bu64.swap_bytes(); + let mut c: u64 = 0x3BF5080AC8BA94B1; // 3. For i = 1, 2, ..., 24 perform 24 rounds for _ in 0..STATE_WORDS { @@ -126,7 +90,7 @@ fn bash_f_internal(state: &mut [u64; STATE_WORDS]) { for j in 0..8 { // 3.2.a. (Sj, S8+j, S16+j) ← bash-s(Sj, S8+j, S16+j, m1, n1, m2, n2) let (s0, s1, s2) = - bash_s_internal(state[j], state[8 + j], state[16 + j], m1, n1, m2, n2); + bash_s(state[j], state[8 + j], state[16 + j], m1, n1, m2, n2); state[j] = s0; state[8 + j] = s1; state[16 + j] = s2; @@ -138,7 +102,7 @@ fn bash_f_internal(state: &mut [u64; STATE_WORDS]) { n2 = (7 * n2) % 64; } - // 3.3. Apply word permutation π + // 3.3. Apply word permutation // S ← S15 ‖ S10 ‖ S9 ‖ S12 ‖ S11 ‖ S14 ‖ S13 ‖ S8 ‖ // S17 ‖ S16 ‖ S19 ‖ S18 ‖ S21 ‖ S20 ‖ S23 ‖ S22 ‖ // S6 ‖ S3 ‖ S0 ‖ S5 ‖ S2 ‖ S7 ‖ S4 ‖ S1 @@ -158,7 +122,7 @@ fn bash_f_internal(state: &mut [u64; STATE_WORDS]) { if c & 1 == 0 { c >>= 1; } else { - c = (c >> 1) ^ 0xAED8E07F99E12BDCu64.swap_bytes(); + c = (c >> 1) ^ 0xDC2BE1997FE0D8AE; } } @@ -207,15 +171,15 @@ mod tests { /// Test vector from Table A.1 of STB 34.101.77-2020. #[test] fn test_bash_s_table_a1() { - let w0 = 0xB194BAC80A08F53B; - let w1 = 0xE12BDC1AE28257EC; - let w2 = 0xE9DEE72C8F0C0FA6; + let w0 = 0xB194BAC80A08F53Bu64.swap_bytes(); + let w1 = 0xE12BDC1AE28257ECu64.swap_bytes(); + let w2 = 0xE9DEE72C8F0C0FA6u64.swap_bytes(); let (w0_out, w1_out, w2_out) = bash_s(w0, w1, w2, 8, 53, 14, 1); - assert_eq!(w0_out, 0x479E76129979DC5F); - assert_eq!(w1_out, 0x0F2B2C93ED128EDD); - assert_eq!(w2_out, 0x41009B1B112DFEF3); + assert_eq!(w0_out, 0x479E76129979DC5Fu64.swap_bytes()); + assert_eq!(w1_out, 0x0F2B2C93ED128EDDu64.swap_bytes()); + assert_eq!(w2_out, 0x41009B1B112DFEF3u64.swap_bytes()); } /// Test vector from Table A.2 of STB 34.101.77-2020. From 6dc8c10265baa9d67972d24c13bfbaa7e2b9c1be Mon Sep 17 00:00:00 2001 From: Alexandr Kitaev Date: Wed, 22 Oct 2025 16:54:08 +0300 Subject: [PATCH 05/16] `belt-bash`: format --- belt-bash/src/lib.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/belt-bash/src/lib.rs b/belt-bash/src/lib.rs index 2be6ba6..d795e58 100644 --- a/belt-bash/src/lib.rs +++ b/belt-bash/src/lib.rs @@ -89,8 +89,7 @@ fn bash_f_internal(state: &mut [u64; STATE_WORDS]) { // 3.2. For j = 0, 1, ..., 7 apply bash-s to each of 8 columns for j in 0..8 { // 3.2.a. (Sj, S8+j, S16+j) ← bash-s(Sj, S8+j, S16+j, m1, n1, m2, n2) - let (s0, s1, s2) = - bash_s(state[j], state[8 + j], state[16 + j], m1, n1, m2, n2); + let (s0, s1, s2) = bash_s(state[j], state[8 + j], state[16 + j], m1, n1, m2, n2); state[j] = s0; state[8 + j] = s1; state[16 + j] = s2; From 0caf7b4cec1015b841d2d961f0358d26ce3f6955 Mon Sep 17 00:00:00 2001 From: Alexandr Kitaev Date: Wed, 22 Oct 2025 17:18:06 +0300 Subject: [PATCH 06/16] `belt-bash`: move `bash-f` to /test, remove unnecessary warns --- belt-bash/Cargo.toml | 9 ----- belt-bash/src/lib.rs | 89 +---------------------------------------- belt-bash/tests/bash.rs | 69 ++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 96 deletions(-) create mode 100644 belt-bash/tests/bash.rs diff --git a/belt-bash/Cargo.toml b/belt-bash/Cargo.toml index 08f30ec..3ca31e1 100644 --- a/belt-bash/Cargo.toml +++ b/belt-bash/Cargo.toml @@ -13,12 +13,3 @@ readme = "README.md" edition = "2024" rust-version = "1.85" -[lints.rust] -missing_docs = "warn" -rust_2018_idioms = "warn" -unused_lifetimes = "warn" -unused_qualifications = "warn" - -[lints.clippy] -mod_module_files = "warn" -unwrap_used = "warn" diff --git a/belt-bash/src/lib.rs b/belt-bash/src/lib.rs index d795e58..de422cd 100644 --- a/belt-bash/src/lib.rs +++ b/belt-bash/src/lib.rs @@ -1,11 +1,11 @@ #![no_std] -#![cfg_attr(docsrs, feature(doc_cfg))] #![doc = include_str!("../README.md")] #![doc( html_logo_url = "https://raw.githubusercontent.com/RustCrypto/meta/master/logo.svg", html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/meta/master/logo.svg" )] -#![allow(non_upper_case_globals)] +#![cfg_attr(docsrs, feature(doc_cfg))] +#![warn(missing_docs)] /// Number of 64-bit words in the state const STATE_WORDS: usize = 24; @@ -140,23 +140,6 @@ fn bash_f_internal(state: &mut [u64; STATE_WORDS]) { /// # Side Effects /// /// Transforms the state in-place through 24 rounds of the sponge permutation. -/// -/// # Example from Test Vector (Table A.2) -/// -/// ```rust -/// use belt_bash::bash_f; -/// let mut state: [u64; 24] = [ -/// 0xB194BAC80A08F53B, 0x366D008E584A5DE4, 0x8504FA9D1BB6C7AC, 0x252E72C202FDCE0D, -/// 0x5BE3D61217B96181, 0xFE6786AD716B890B, 0x5CB0C0FF33C356B8, 0x35C405AED8E07F99, -/// 0xE12BDC1AE28257EC, 0x703FCCF095EE8DF1, 0xC1AB76389FE678CA, 0xF7C6F860D5BB9C4F, -/// 0xF33C657B637C306A, 0xDD4EA7799EB23D31, 0x3E98B56E27D3BCCF, 0x591E181F4C5AB793, -/// 0xE9DEE72C8F0C0FA6, 0x2DDB49F46F739647, 0x06075316ED247A37, 0x39CBA38303A98BF6, -/// 0x92BD9B1CE5D14101, 0x5445FBC95E4D0EF2, 0x682080AA227D642F, 0x2687F93490405511, -/// ]; -/// bash_f(&mut state); -/// assert_eq!(state[0], 0x8FE727775EA7F140); -/// // ... verify remaining words -/// ``` pub fn bash_f(state: &mut [u64; 24]) { state.iter_mut().for_each(|s| *s = s.swap_bytes()); bash_f_internal(state); @@ -180,72 +163,4 @@ mod tests { assert_eq!(w1_out, 0x0F2B2C93ED128EDDu64.swap_bytes()); assert_eq!(w2_out, 0x41009B1B112DFEF3u64.swap_bytes()); } - - /// Test vector from Table A.2 of STB 34.101.77-2020. - #[test] - fn test_bash_f_table_a2() { - let mut state: [u64; 24] = [ - 0xB194BAC80A08F53B, - 0x366D008E584A5DE4, - 0x8504FA9D1BB6C7AC, - 0x252E72C202FDCE0D, - 0x5BE3D61217B96181, - 0xFE6786AD716B890B, - 0x5CB0C0FF33C356B8, - 0x35C405AED8E07F99, - 0xE12BDC1AE28257EC, - 0x703FCCF095EE8DF1, - 0xC1AB76389FE678CA, - 0xF7C6F860D5BB9C4F, - 0xF33C657B637C306A, - 0xDD4EA7799EB23D31, - 0x3E98B56E27D3BCCF, - 0x591E181F4C5AB793, - 0xE9DEE72C8F0C0FA6, - 0x2DDB49F46F739647, - 0x06075316ED247A37, - 0x39CBA38303A98BF6, - 0x92BD9B1CE5D14101, - 0x5445FBC95E4D0EF2, - 0x682080AA227D642F, - 0x2687F93490405511, - ]; - - bash_f(&mut state); - - let expected: [u64; 24] = [ - 0x8FE727775EA7F140, - 0xB95BB6A200CBB28C, - 0x7F0809C0C0BC68B7, - 0xDC5AEDC841BD94E4, - 0x03630C301FC255DF, - 0x5B67DB53EF65E376, - 0xE8A4D797A6172F22, - 0x71BA48093173D329, - 0xC3502AC946767326, - 0xA2891971392D3F70, - 0x89959F5D61621238, - 0x655975E00E2132A0, - 0xD5018CEEDB17731C, - 0xCD88FC50151D37C0, - 0xD4A3359506AEDC2E, - 0x6109511E7703AFBB, - 0x014642348D8568AA, - 0x1A5D9868C4C7E6DF, - 0xA756B1690C7C2608, - 0xA2DC136F5997AB8F, - 0xBB3F4D9F033C87CA, - 0x6070E117F099C409, - 0x4972ACD9D976214B, - 0x7CED8E3F8B6E058E, - ]; - - for i in 0..24 { - assert_eq!( - state[i], expected[i], - "Mismatch at S[{}]: got {:016X}, expected {:016X}", - i, state[i], expected[i] - ); - } - } } diff --git a/belt-bash/tests/bash.rs b/belt-bash/tests/bash.rs new file mode 100644 index 0000000..72971bd --- /dev/null +++ b/belt-bash/tests/bash.rs @@ -0,0 +1,69 @@ +use belt_bash::bash_f; + +/// Test vector from Table A.2 of STB 34.101.77-2020. +#[test] +fn test_bash_f_table_a2() { + let mut state: [u64; 24] = [ + 0xB194BAC80A08F53B, + 0x366D008E584A5DE4, + 0x8504FA9D1BB6C7AC, + 0x252E72C202FDCE0D, + 0x5BE3D61217B96181, + 0xFE6786AD716B890B, + 0x5CB0C0FF33C356B8, + 0x35C405AED8E07F99, + 0xE12BDC1AE28257EC, + 0x703FCCF095EE8DF1, + 0xC1AB76389FE678CA, + 0xF7C6F860D5BB9C4F, + 0xF33C657B637C306A, + 0xDD4EA7799EB23D31, + 0x3E98B56E27D3BCCF, + 0x591E181F4C5AB793, + 0xE9DEE72C8F0C0FA6, + 0x2DDB49F46F739647, + 0x06075316ED247A37, + 0x39CBA38303A98BF6, + 0x92BD9B1CE5D14101, + 0x5445FBC95E4D0EF2, + 0x682080AA227D642F, + 0x2687F93490405511, + ]; + + bash_f(&mut state); + + let expected: [u64; 24] = [ + 0x8FE727775EA7F140, + 0xB95BB6A200CBB28C, + 0x7F0809C0C0BC68B7, + 0xDC5AEDC841BD94E4, + 0x03630C301FC255DF, + 0x5B67DB53EF65E376, + 0xE8A4D797A6172F22, + 0x71BA48093173D329, + 0xC3502AC946767326, + 0xA2891971392D3F70, + 0x89959F5D61621238, + 0x655975E00E2132A0, + 0xD5018CEEDB17731C, + 0xCD88FC50151D37C0, + 0xD4A3359506AEDC2E, + 0x6109511E7703AFBB, + 0x014642348D8568AA, + 0x1A5D9868C4C7E6DF, + 0xA756B1690C7C2608, + 0xA2DC136F5997AB8F, + 0xBB3F4D9F033C87CA, + 0x6070E117F099C409, + 0x4972ACD9D976214B, + 0x7CED8E3F8B6E058E, + ]; + + for i in 0..24 { + assert_eq!( + state[i], expected[i], + "Mismatch at S[{}]: got {:016X}, expected {:016X}", + i, state[i], expected[i] + ); + } +} From c041bf75d1635a812ba90dc5835e806a0606cb58 Mon Sep 17 00:00:00 2001 From: Alexandr Kitaev Date: Wed, 22 Oct 2025 17:29:05 +0300 Subject: [PATCH 07/16] `belt-bash`: simplify test, description fix --- belt-bash/Cargo.toml | 2 +- belt-bash/tests/bash.rs | 8 +------- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/belt-bash/Cargo.toml b/belt-bash/Cargo.toml index 3ca31e1..884f5fc 100644 --- a/belt-bash/Cargo.toml +++ b/belt-bash/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "belt-bash" version = "0.1.0-rc.0" -description = "Pure Rust implementation of the BASH algorithms defined in STB 34.101.77-2020" +description = "Pure Rust implementation of the bash-f sponge function defined in STB 34.101.77-2020" license = "Apache-2.0 OR MIT" authors = ["RustCrypto Developers"] documentation = "https://docs.rs/bash" diff --git a/belt-bash/tests/bash.rs b/belt-bash/tests/bash.rs index 72971bd..4d46d38 100644 --- a/belt-bash/tests/bash.rs +++ b/belt-bash/tests/bash.rs @@ -59,11 +59,5 @@ fn test_bash_f_table_a2() { 0x7CED8E3F8B6E058E, ]; - for i in 0..24 { - assert_eq!( - state[i], expected[i], - "Mismatch at S[{}]: got {:016X}, expected {:016X}", - i, state[i], expected[i] - ); - } + assert_eq!(state, expected); } From e0f862af925c47e2c99832381920f994b10fb3ad Mon Sep 17 00:00:00 2001 From: Alexandr Kitaev Date: Wed, 22 Oct 2025 19:59:36 +0300 Subject: [PATCH 08/16] `belt-bash`: clarify implementation, remove `bash_f` internal --- belt-bash/src/lib.rs | 30 +++++------------------------- belt-bash/tests/bash.rs | 15 +++++++++++---- 2 files changed, 16 insertions(+), 29 deletions(-) diff --git a/belt-bash/src/lib.rs b/belt-bash/src/lib.rs index de422cd..84939e6 100644 --- a/belt-bash/src/lib.rs +++ b/belt-bash/src/lib.rs @@ -63,7 +63,7 @@ fn bash_s( (w0, w1, w2) } -/// Internal `bash-f` sponge permutation. +/// `bash-f` sponge permutation. /// /// Implements the core sponge function defined in Section 6.2 of STB 34.101.77-2020. /// This is a cryptographic permutation that operates on 1536-bit states. @@ -71,7 +71,7 @@ fn bash_s( /// # Parameters /// /// - `state`: Mutable reference to 24 × 64-bit words (1536 bits total) in little-endian internal representation -fn bash_f_internal(state: &mut [u64; STATE_WORDS]) { +pub fn bash_f(state: &mut [u64; STATE_WORDS]) { // 1. Split S into words (S0, S1, ..., S23) // 2. C ← B194BAC80A08F53B (initialize round constant, swapped to little-endian) @@ -105,12 +105,10 @@ fn bash_f_internal(state: &mut [u64; STATE_WORDS]) { // S ← S15 ‖ S10 ‖ S9 ‖ S12 ‖ S11 ‖ S14 ‖ S13 ‖ S8 ‖ // S17 ‖ S16 ‖ S19 ‖ S18 ‖ S21 ‖ S20 ‖ S23 ‖ S22 ‖ // S6 ‖ S3 ‖ S0 ‖ S5 ‖ S2 ‖ S7 ‖ S4 ‖ S1 - let temp = [ - state[15], state[10], state[9], state[12], state[11], state[14], state[13], state[8], - state[17], state[16], state[19], state[18], state[21], state[20], state[23], state[22], - state[6], state[3], state[0], state[5], state[2], state[7], state[4], state[1], + const INDEXES: [usize; STATE_WORDS] = [ + 15, 10, 9, 12, 11, 14, 13, 8, 17, 16, 19, 18, 21, 20, 23, 22, 6, 3, 0, 5, 2, 7, 4, 1, ]; - state.copy_from_slice(&temp); + *state = INDEXES.map(|i| state[i]); // 3.4. S23 ← S23 ⊕ C (add round constant) state[23] ^= c; @@ -128,24 +126,6 @@ fn bash_f_internal(state: &mut [u64; STATE_WORDS]) { // 4. Return S - state is modified in place } -/// `bash-f` sponge function with standard-compliant interface. -/// -/// This is the public interface as specified in Section 6.2 of STB 34.101.77-2020. -/// It accepts and returns states in big-endian byte order as per the standard. -/// -/// # Parameters -/// -/// - `state`: Mutable array of 24 × 64-bit words (192 bytes total) in **big-endian** byte order -/// -/// # Side Effects -/// -/// Transforms the state in-place through 24 rounds of the sponge permutation. -pub fn bash_f(state: &mut [u64; 24]) { - state.iter_mut().for_each(|s| *s = s.swap_bytes()); - bash_f_internal(state); - state.iter_mut().for_each(|s| *s = s.swap_bytes()); -} - #[cfg(test)] mod tests { use super::*; diff --git a/belt-bash/tests/bash.rs b/belt-bash/tests/bash.rs index 4d46d38..213911b 100644 --- a/belt-bash/tests/bash.rs +++ b/belt-bash/tests/bash.rs @@ -3,7 +3,7 @@ use belt_bash::bash_f; /// Test vector from Table A.2 of STB 34.101.77-2020. #[test] fn test_bash_f_table_a2() { - let mut state: [u64; 24] = [ + let input: [u64; 24] = [ 0xB194BAC80A08F53B, 0x366D008E584A5DE4, 0x8504FA9D1BB6C7AC, @@ -30,8 +30,6 @@ fn test_bash_f_table_a2() { 0x2687F93490405511, ]; - bash_f(&mut state); - let expected: [u64; 24] = [ 0x8FE727775EA7F140, 0xB95BB6A200CBB28C, @@ -59,5 +57,14 @@ fn test_bash_f_table_a2() { 0x7CED8E3F8B6E058E, ]; - assert_eq!(state, expected); + // Constants in the spec are given using LE order + // For example, in spec when they write B194BAC80A08F53B, they do not mean 0xB194BAC80A08F53B, but 0x3BF5080AC8BA94B1. + // https://github.com/RustCrypto/sponges/pull/92#issuecomment-3433315011 + let mut state = input.map(|x| x.swap_bytes()); + + bash_f(&mut state); + + let output = state.map(|x| x.swap_bytes()); + + assert_eq!(output, expected); } From 6e4c8e53c9e835237209bb8f89bd66da6a9b811c Mon Sep 17 00:00:00 2001 From: Alexandr Kitaev Date: Wed, 22 Oct 2025 20:03:46 +0300 Subject: [PATCH 09/16] `belt-bash`: add same comment from `bash_f` test to the `bash_s` test. --- belt-bash/src/lib.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/belt-bash/src/lib.rs b/belt-bash/src/lib.rs index 84939e6..2f2c3ff 100644 --- a/belt-bash/src/lib.rs +++ b/belt-bash/src/lib.rs @@ -133,6 +133,9 @@ mod tests { /// Test vector from Table A.1 of STB 34.101.77-2020. #[test] fn test_bash_s_table_a1() { + // Constants in the spec are given using LE order + // For example, in spec when they write B194BAC80A08F53B, they do not mean 0xB194BAC80A08F53B, but 0x3BF5080AC8BA94B1. + // https://github.com/RustCrypto/sponges/pull/92#issuecomment-3433315011 let w0 = 0xB194BAC80A08F53Bu64.swap_bytes(); let w1 = 0xE12BDC1AE28257ECu64.swap_bytes(); let w2 = 0xE9DEE72C8F0C0FA6u64.swap_bytes(); From ba0a61acaf5ff25d11560293842616a6efa3e7c9 Mon Sep 17 00:00:00 2001 From: Alexandr Kitaev Date: Wed, 22 Oct 2025 20:11:42 +0300 Subject: [PATCH 10/16] `belt-bash`: precalculate m1, n1, m2, n2 --- belt-bash/src/lib.rs | 44 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 10 deletions(-) diff --git a/belt-bash/src/lib.rs b/belt-bash/src/lib.rs index 2f2c3ff..838fefc 100644 --- a/belt-bash/src/lib.rs +++ b/belt-bash/src/lib.rs @@ -10,6 +10,38 @@ /// Number of 64-bit words in the state const STATE_WORDS: usize = 24; +/// Precalculated rotation params +/// ```ignore +/// const ROTATION_PARAMS: [(u32, u32, u32, u32); 8] = { +/// let mut params = [(0u32, 0u32, 0u32, 0u32); 8]; +/// let mut m1 = 8u32; +/// let mut n1 = 53u32; +/// let mut m2 = 14u32; +/// let mut n2 = 1u32; +/// +/// let mut j = 0; +/// while j < 8 { +/// params[j] = (m1, n1, m2, n2); +/// m1 = (7 * m1) % 64; +/// n1 = (7 * n1) % 64; +/// m2 = (7 * m2) % 64; +/// n2 = (7 * n2) % 64; +/// j += 1; +/// } +/// params +/// }; +/// ``` +const ROTATION_PARAMS: [(u32, u32, u32, u32); 8] = [ + (8, 53, 14, 1), // j=0 + (56, 51, 34, 7), // j=1: (7*8%64, 7*53%64, 7*14%64, 7*1%64) + (8, 37, 46, 49), // j=2: (7*56%64, 7*51%64, 7*34%64, 7*7%64) + (56, 3, 2, 23), // j=3 + (8, 21, 14, 33), // j=4 + (56, 19, 34, 39), // j=5 + (8, 5, 46, 17), // j=6 + (56, 35, 2, 55), // j=7 +]; + /// `bash-s` transformation. /// /// Implements the S-box transformation defined in Section 6.1 of STB 34.101.77-2020. @@ -81,24 +113,16 @@ pub fn bash_f(state: &mut [u64; STATE_WORDS]) { for _ in 0..STATE_WORDS { // 3.1. Apply S-box layer with varying rotation parameters // (m1, n1, m2, n2) ← (8, 53, 14, 1) - let mut m1 = 8u32; - let mut n1 = 53u32; - let mut m2 = 14u32; - let mut n2 = 1u32; // 3.2. For j = 0, 1, ..., 7 apply bash-s to each of 8 columns for j in 0..8 { // 3.2.a. (Sj, S8+j, S16+j) ← bash-s(Sj, S8+j, S16+j, m1, n1, m2, n2) + // 3.2.b. (m1, n1, m2, n2) ← (7·m1 mod 64, 7·n1 mod 64, 7·m2 mod 64, 7·n2 mod 64) + let (m1, n1, m2, n2) = ROTATION_PARAMS[j]; let (s0, s1, s2) = bash_s(state[j], state[8 + j], state[16 + j], m1, n1, m2, n2); state[j] = s0; state[8 + j] = s1; state[16 + j] = s2; - - // 3.2.b. (m1, n1, m2, n2) ← (7·m1 mod 64, 7·n1 mod 64, 7·m2 mod 64, 7·n2 mod 64) - m1 = (7 * m1) % 64; - n1 = (7 * n1) % 64; - m2 = (7 * m2) % 64; - n2 = (7 * n2) % 64; } // 3.3. Apply word permutation From 2a2a9ee7210676261ae8c46a08a71204bb7a687a Mon Sep 17 00:00:00 2001 From: Alexandr Kitaev Date: Wed, 22 Oct 2025 20:20:55 +0300 Subject: [PATCH 11/16] `belt-bash`: add benches and no_unroll --- belt-bash/Cargo.toml | 2 + belt-bash/benches/mod.rs | 38 +++++++++++++++++++ belt-bash/src/lib.rs | 81 ++++++++++++++++++++++++++-------------- 3 files changed, 93 insertions(+), 28 deletions(-) create mode 100644 belt-bash/benches/mod.rs diff --git a/belt-bash/Cargo.toml b/belt-bash/Cargo.toml index 884f5fc..59ce279 100644 --- a/belt-bash/Cargo.toml +++ b/belt-bash/Cargo.toml @@ -13,3 +13,5 @@ readme = "README.md" edition = "2024" rust-version = "1.85" +[features] +no_unroll = [] # Do not unroll loops for binary size reduction \ No newline at end of file diff --git a/belt-bash/benches/mod.rs b/belt-bash/benches/mod.rs new file mode 100644 index 0000000..b2fc7e2 --- /dev/null +++ b/belt-bash/benches/mod.rs @@ -0,0 +1,38 @@ +#![feature(test)] +extern crate test; + +use belt_bash::bash_f; +use test::Bencher; + +const STATE_WORDS: usize = 24; + +#[bench] +fn bench_bash_f(b: &mut Bencher) { + let mut state = [0u64; STATE_WORDS]; + b.iter(|| { + bash_f(test::black_box(&mut state)); + test::black_box(&state); + }); +} + +#[bench] +fn bench_bash_f_10_rounds(b: &mut Bencher) { + let mut state = [0u64; STATE_WORDS]; + b.iter(|| { + for _ in 0..10 { + bash_f(test::black_box(&mut state)); + } + test::black_box(&state); + }); +} + +#[bench] +fn bench_bash_f_100_rounds(b: &mut Bencher) { + let mut state = [0u64; STATE_WORDS]; + b.iter(|| { + for _ in 0..100 { + bash_f(test::black_box(&mut state)); + } + test::black_box(&state); + }); +} diff --git a/belt-bash/src/lib.rs b/belt-bash/src/lib.rs index 838fefc..46df841 100644 --- a/belt-bash/src/lib.rs +++ b/belt-bash/src/lib.rs @@ -11,35 +11,15 @@ const STATE_WORDS: usize = 24; /// Precalculated rotation params -/// ```ignore -/// const ROTATION_PARAMS: [(u32, u32, u32, u32); 8] = { -/// let mut params = [(0u32, 0u32, 0u32, 0u32); 8]; -/// let mut m1 = 8u32; -/// let mut n1 = 53u32; -/// let mut m2 = 14u32; -/// let mut n2 = 1u32; -/// -/// let mut j = 0; -/// while j < 8 { -/// params[j] = (m1, n1, m2, n2); -/// m1 = (7 * m1) % 64; -/// n1 = (7 * n1) % 64; -/// m2 = (7 * m2) % 64; -/// n2 = (7 * n2) % 64; -/// j += 1; -/// } -/// params -/// }; -/// ``` const ROTATION_PARAMS: [(u32, u32, u32, u32); 8] = [ - (8, 53, 14, 1), // j=0 - (56, 51, 34, 7), // j=1: (7*8%64, 7*53%64, 7*14%64, 7*1%64) - (8, 37, 46, 49), // j=2: (7*56%64, 7*51%64, 7*34%64, 7*7%64) - (56, 3, 2, 23), // j=3 - (8, 21, 14, 33), // j=4 - (56, 19, 34, 39), // j=5 - (8, 5, 46, 17), // j=6 - (56, 35, 2, 55), // j=7 + (8, 53, 14, 1), // j=0 + (56, 51, 34, 7), // j=1: (7*8%64, 7*53%64, 7*14%64, 7*1%64) + (8, 37, 46, 49), // j=2: (7*56%64, 7*51%64, 7*34%64, 7*7%64) + (56, 3, 2, 23), // j=3 + (8, 21, 14, 33), // j=4 + (56, 19, 34, 39), // j=5 + (8, 5, 46, 17), // j=6 + (56, 35, 2, 55), // j=7 ]; /// `bash-s` transformation. @@ -115,6 +95,9 @@ pub fn bash_f(state: &mut [u64; STATE_WORDS]) { // (m1, n1, m2, n2) ← (8, 53, 14, 1) // 3.2. For j = 0, 1, ..., 7 apply bash-s to each of 8 columns + // 3.2.a. (Sj, S8+j, S16+j) ← bash-s(Sj, S8+j, S16+j, m1, n1, m2, n2) + // 3.2.b. (m1, n1, m2, n2) ← (7·m1 mod 64, 7·n1 mod 64, 7·m2 mod 64, 7·n2 mod 64) + #[cfg(feature = "no_unroll")] for j in 0..8 { // 3.2.a. (Sj, S8+j, S16+j) ← bash-s(Sj, S8+j, S16+j, m1, n1, m2, n2) // 3.2.b. (m1, n1, m2, n2) ← (7·m1 mod 64, 7·n1 mod 64, 7·m2 mod 64, 7·n2 mod 64) @@ -125,6 +108,29 @@ pub fn bash_f(state: &mut [u64; STATE_WORDS]) { state[16 + j] = s2; } + #[cfg(not(feature = "no_unroll"))] + { + macro_rules! apply_s_box { + ($j:expr) => {{ + let (m1, n1, m2, n2) = ROTATION_PARAMS[$j]; + let (s0, s1, s2) = + bash_s(state[$j], state[8 + $j], state[16 + $j], m1, n1, m2, n2); + state[$j] = s0; + state[8 + $j] = s1; + state[16 + $j] = s2; + }}; + } + + apply_s_box!(0); + apply_s_box!(1); + apply_s_box!(2); + apply_s_box!(3); + apply_s_box!(4); + apply_s_box!(5); + apply_s_box!(6); + apply_s_box!(7); + } + // 3.3. Apply word permutation // S ← S15 ‖ S10 ‖ S9 ‖ S12 ‖ S11 ‖ S14 ‖ S13 ‖ S8 ‖ // S17 ‖ S16 ‖ S19 ‖ S18 ‖ S21 ‖ S20 ‖ S23 ‖ S22 ‖ @@ -170,4 +176,23 @@ mod tests { assert_eq!(w1_out, 0x0F2B2C93ED128EDDu64.swap_bytes()); assert_eq!(w2_out, 0x41009B1B112DFEF3u64.swap_bytes()); } + + #[test] + /// Verify precalculated params + fn verify_rotation_params() { + let mut m1 = 8u32; + let mut n1 = 53u32; + let mut m2 = 14u32; + let mut n2 = 1u32; + + for j in 0..8 { + let expected = ROTATION_PARAMS[j]; + assert_eq!((m1, n1, m2, n2), expected); + + m1 = (7 * m1) % 64; + n1 = (7 * n1) % 64; + m2 = (7 * m2) % 64; + n2 = (7 * n2) % 64; + } + } } From 4e0e1485c1827f666e88ae45a6a8125e86ca9c16 Mon Sep 17 00:00:00 2001 From: Alexandr Kitaev Date: Wed, 22 Oct 2025 20:25:11 +0300 Subject: [PATCH 12/16] `belt-bash`: Simplify unroll and no_unroll --- belt-bash/src/lib.rs | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/belt-bash/src/lib.rs b/belt-bash/src/lib.rs index 46df841..ae52cc3 100644 --- a/belt-bash/src/lib.rs +++ b/belt-bash/src/lib.rs @@ -97,30 +97,25 @@ pub fn bash_f(state: &mut [u64; STATE_WORDS]) { // 3.2. For j = 0, 1, ..., 7 apply bash-s to each of 8 columns // 3.2.a. (Sj, S8+j, S16+j) ← bash-s(Sj, S8+j, S16+j, m1, n1, m2, n2) // 3.2.b. (m1, n1, m2, n2) ← (7·m1 mod 64, 7·n1 mod 64, 7·m2 mod 64, 7·n2 mod 64) + macro_rules! apply_s_box { + ($j:expr) => {{ + let (m1, n1, m2, n2) = ROTATION_PARAMS[$j]; + let (s0, s1, s2) = bash_s(state[$j], state[8 + $j], state[16 + $j], m1, n1, m2, n2); + state[$j] = s0; + state[8 + $j] = s1; + state[16 + $j] = s2; + }}; + } + #[cfg(feature = "no_unroll")] for j in 0..8 { // 3.2.a. (Sj, S8+j, S16+j) ← bash-s(Sj, S8+j, S16+j, m1, n1, m2, n2) // 3.2.b. (m1, n1, m2, n2) ← (7·m1 mod 64, 7·n1 mod 64, 7·m2 mod 64, 7·n2 mod 64) - let (m1, n1, m2, n2) = ROTATION_PARAMS[j]; - let (s0, s1, s2) = bash_s(state[j], state[8 + j], state[16 + j], m1, n1, m2, n2); - state[j] = s0; - state[8 + j] = s1; - state[16 + j] = s2; + apply_s_box!(j); } #[cfg(not(feature = "no_unroll"))] { - macro_rules! apply_s_box { - ($j:expr) => {{ - let (m1, n1, m2, n2) = ROTATION_PARAMS[$j]; - let (s0, s1, s2) = - bash_s(state[$j], state[8 + $j], state[16 + $j], m1, n1, m2, n2); - state[$j] = s0; - state[8 + $j] = s1; - state[16 + $j] = s2; - }}; - } - apply_s_box!(0); apply_s_box!(1); apply_s_box!(2); From e8fb827d5e8a56b7c131338dccdeac4a1d6ebbb4 Mon Sep 17 00:00:00 2001 From: Alexandr Kitaev Date: Wed, 22 Oct 2025 20:31:18 +0300 Subject: [PATCH 13/16] `bash-f`: rename crate, simplify ROTATION_PARAMS --- .../workflows/{belt-bash.yml => bash-f.yml} | 8 +-- Cargo.lock | 2 +- Cargo.toml | 2 +- {belt-bash => bash-f}/CHANGELOG.md | 0 {belt-bash => bash-f}/Cargo.toml | 4 +- {belt-bash => bash-f}/LICENSE-APACHE | 0 {belt-bash => bash-f}/LICENSE-MIT | 0 {belt-bash => bash-f}/README.md | 10 ++-- {belt-bash => bash-f}/benches/mod.rs | 2 +- {belt-bash => bash-f}/src/lib.rs | 49 +++++++------------ {belt-bash => bash-f}/tests/bash.rs | 2 +- 11 files changed, 33 insertions(+), 46 deletions(-) rename .github/workflows/{belt-bash.yml => bash-f.yml} (95%) rename {belt-bash => bash-f}/CHANGELOG.md (100%) rename {belt-bash => bash-f}/Cargo.toml (86%) rename {belt-bash => bash-f}/LICENSE-APACHE (100%) rename {belt-bash => bash-f}/LICENSE-MIT (100%) rename {belt-bash => bash-f}/README.md (87%) rename {belt-bash => bash-f}/benches/mod.rs (97%) rename {belt-bash => bash-f}/src/lib.rs (83%) rename {belt-bash => bash-f}/tests/bash.rs (98%) diff --git a/.github/workflows/belt-bash.yml b/.github/workflows/bash-f.yml similarity index 95% rename from .github/workflows/belt-bash.yml rename to .github/workflows/bash-f.yml index 2ad851f..9f08be2 100644 --- a/.github/workflows/belt-bash.yml +++ b/.github/workflows/bash-f.yml @@ -1,17 +1,17 @@ -name: belt-bash +name: bash-f on: pull_request: paths: - - ".github/workflows/belt-bash.yml" - - "belt-bash/**" + - "bash-f.yml" + - "../../bash-f/**" - "Cargo.*" push: branches: master defaults: run: - working-directory: belt-bash + working-directory: bash-f env: RUSTFLAGS: "-Dwarnings" diff --git a/Cargo.lock b/Cargo.lock index 8bcaac3..1b499e2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10,7 +10,7 @@ dependencies = [ ] [[package]] -name = "belt-bash" +name = "bash-f" version = "0.1.0-rc.0" [[package]] diff --git a/Cargo.toml b/Cargo.toml index 66c9f59..ee29985 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,6 +2,6 @@ resolver = "2" members = [ "ascon", - "belt-bash", + "bash-f", "keccak", ] diff --git a/belt-bash/CHANGELOG.md b/bash-f/CHANGELOG.md similarity index 100% rename from belt-bash/CHANGELOG.md rename to bash-f/CHANGELOG.md diff --git a/belt-bash/Cargo.toml b/bash-f/Cargo.toml similarity index 86% rename from belt-bash/Cargo.toml rename to bash-f/Cargo.toml index 59ce279..5427b43 100644 --- a/belt-bash/Cargo.toml +++ b/bash-f/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "belt-bash" +name = "bash-f" version = "0.1.0-rc.0" description = "Pure Rust implementation of the bash-f sponge function defined in STB 34.101.77-2020" license = "Apache-2.0 OR MIT" @@ -14,4 +14,4 @@ edition = "2024" rust-version = "1.85" [features] -no_unroll = [] # Do not unroll loops for binary size reduction \ No newline at end of file +no_unroll = [] # Do not unroll loops for binary size reduction diff --git a/belt-bash/LICENSE-APACHE b/bash-f/LICENSE-APACHE similarity index 100% rename from belt-bash/LICENSE-APACHE rename to bash-f/LICENSE-APACHE diff --git a/belt-bash/LICENSE-MIT b/bash-f/LICENSE-MIT similarity index 100% rename from belt-bash/LICENSE-MIT rename to bash-f/LICENSE-MIT diff --git a/belt-bash/README.md b/bash-f/README.md similarity index 87% rename from belt-bash/README.md rename to bash-f/README.md index fd2e873..7caf4dd 100644 --- a/belt-bash/README.md +++ b/bash-f/README.md @@ -47,16 +47,16 @@ dual licensed as above, without any additional terms or conditions. [//]: # (badges) -[crate-image]: https://buildstats.info/crate/belt-bash -[crate-link]: https://crates.io/crates/belt-bash -[docs-image]: https://docs.rs/belt-bash/badge.svg -[docs-link]: https://docs.rs/belt-bash/ +[crate-image]: https://buildstats.info/crate/bash-f +[crate-link]: https://crates.io/crates/bash-f +[docs-image]: https://docs.rs/bash-f/badge.svg +[docs-link]: https://docs.rs/bash-f/ [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/260038-AEADs [downloads-image]: https://img.shields.io/crates/d/chacha20poly1305.svg -[build-image]: https://github.com/RustCrypto/AEADs/workflows/belt-bash/badge.svg?branch=master&event=push +[build-image]: https://github.com/RustCrypto/AEADs/workflows/bash-f/badge.svg?branch=master&event=push [build-link]: https://github.com/RustCrypto/AEADs/actions [//]: # (general links) diff --git a/belt-bash/benches/mod.rs b/bash-f/benches/mod.rs similarity index 97% rename from belt-bash/benches/mod.rs rename to bash-f/benches/mod.rs index b2fc7e2..3c5f2dc 100644 --- a/belt-bash/benches/mod.rs +++ b/bash-f/benches/mod.rs @@ -1,7 +1,7 @@ #![feature(test)] extern crate test; -use belt_bash::bash_f; +use bash_f::bash_f; use test::Bencher; const STATE_WORDS: usize = 24; diff --git a/belt-bash/src/lib.rs b/bash-f/src/lib.rs similarity index 83% rename from belt-bash/src/lib.rs rename to bash-f/src/lib.rs index ae52cc3..cd8db0a 100644 --- a/belt-bash/src/lib.rs +++ b/bash-f/src/lib.rs @@ -11,16 +11,24 @@ const STATE_WORDS: usize = 24; /// Precalculated rotation params -const ROTATION_PARAMS: [(u32, u32, u32, u32); 8] = [ - (8, 53, 14, 1), // j=0 - (56, 51, 34, 7), // j=1: (7*8%64, 7*53%64, 7*14%64, 7*1%64) - (8, 37, 46, 49), // j=2: (7*56%64, 7*51%64, 7*34%64, 7*7%64) - (56, 3, 2, 23), // j=3 - (8, 21, 14, 33), // j=4 - (56, 19, 34, 39), // j=5 - (8, 5, 46, 17), // j=6 - (56, 35, 2, 55), // j=7 -]; +const ROTATION_PARAMS: [(u32, u32, u32, u32); 8] = { + let mut params = [(0u32, 0u32, 0u32, 0u32); 8]; + let mut m1 = 8u32; + let mut n1 = 53u32; + let mut m2 = 14u32; + let mut n2 = 1u32; + + let mut j = 0; + while j < 8 { + params[j] = (m1, n1, m2, n2); + m1 = (7 * m1) % 64; + n1 = (7 * n1) % 64; + m2 = (7 * m2) % 64; + n2 = (7 * n2) % 64; + j += 1; + } + params +}; /// `bash-s` transformation. /// @@ -109,8 +117,6 @@ pub fn bash_f(state: &mut [u64; STATE_WORDS]) { #[cfg(feature = "no_unroll")] for j in 0..8 { - // 3.2.a. (Sj, S8+j, S16+j) ← bash-s(Sj, S8+j, S16+j, m1, n1, m2, n2) - // 3.2.b. (m1, n1, m2, n2) ← (7·m1 mod 64, 7·n1 mod 64, 7·m2 mod 64, 7·n2 mod 64) apply_s_box!(j); } @@ -171,23 +177,4 @@ mod tests { assert_eq!(w1_out, 0x0F2B2C93ED128EDDu64.swap_bytes()); assert_eq!(w2_out, 0x41009B1B112DFEF3u64.swap_bytes()); } - - #[test] - /// Verify precalculated params - fn verify_rotation_params() { - let mut m1 = 8u32; - let mut n1 = 53u32; - let mut m2 = 14u32; - let mut n2 = 1u32; - - for j in 0..8 { - let expected = ROTATION_PARAMS[j]; - assert_eq!((m1, n1, m2, n2), expected); - - m1 = (7 * m1) % 64; - n1 = (7 * n1) % 64; - m2 = (7 * m2) % 64; - n2 = (7 * n2) % 64; - } - } } diff --git a/belt-bash/tests/bash.rs b/bash-f/tests/bash.rs similarity index 98% rename from belt-bash/tests/bash.rs rename to bash-f/tests/bash.rs index 213911b..95ad5df 100644 --- a/belt-bash/tests/bash.rs +++ b/bash-f/tests/bash.rs @@ -1,4 +1,4 @@ -use belt_bash::bash_f; +use bash_f::bash_f; /// Test vector from Table A.2 of STB 34.101.77-2020. #[test] From 927eb4195f4c33b89acd3afb97b575f72bc5feb5 Mon Sep 17 00:00:00 2001 From: Alexandr Kitaev Date: Wed, 22 Oct 2025 20:46:35 +0300 Subject: [PATCH 14/16] `bash-f`: fixes, remove no_unroll --- .github/workflows/bash-f.yml | 2 +- bash-f/Cargo.toml | 5 +---- bash-f/README.md | 2 +- bash-f/src/lib.rs | 34 +++++++--------------------------- 4 files changed, 10 insertions(+), 33 deletions(-) diff --git a/.github/workflows/bash-f.yml b/.github/workflows/bash-f.yml index 9f08be2..7614514 100644 --- a/.github/workflows/bash-f.yml +++ b/.github/workflows/bash-f.yml @@ -4,7 +4,7 @@ on: pull_request: paths: - "bash-f.yml" - - "../../bash-f/**" + - "bash-f/**" - "Cargo.*" push: branches: master diff --git a/bash-f/Cargo.toml b/bash-f/Cargo.toml index 5427b43..1d105aa 100644 --- a/bash-f/Cargo.toml +++ b/bash-f/Cargo.toml @@ -7,11 +7,8 @@ authors = ["RustCrypto Developers"] documentation = "https://docs.rs/bash" homepage = "https://github.com/RustCrypto/sponges/tree/master/bash" repository = "https://github.com/RustCrypto/sponges" -keywords = ["Bash", "belt", "permutation"] +keywords = ["bash", "belt", "permutation"] categories = ["cryptography", "no-std"] readme = "README.md" edition = "2024" rust-version = "1.85" - -[features] -no_unroll = [] # Do not unroll loops for binary size reduction diff --git a/bash-f/README.md b/bash-f/README.md index 7caf4dd..84ea808 100644 --- a/bash-f/README.md +++ b/bash-f/README.md @@ -1,4 +1,4 @@ -# RustCrypto: BeltBash +# RustCrypto: bash-f [![crate][crate-image]][crate-link] [![Docs][docs-image]][docs-link] diff --git a/bash-f/src/lib.rs b/bash-f/src/lib.rs index cd8db0a..4ad3499 100644 --- a/bash-f/src/lib.rs +++ b/bash-f/src/lib.rs @@ -101,35 +101,15 @@ pub fn bash_f(state: &mut [u64; STATE_WORDS]) { for _ in 0..STATE_WORDS { // 3.1. Apply S-box layer with varying rotation parameters // (m1, n1, m2, n2) ← (8, 53, 14, 1) - // 3.2. For j = 0, 1, ..., 7 apply bash-s to each of 8 columns - // 3.2.a. (Sj, S8+j, S16+j) ← bash-s(Sj, S8+j, S16+j, m1, n1, m2, n2) - // 3.2.b. (m1, n1, m2, n2) ← (7·m1 mod 64, 7·n1 mod 64, 7·m2 mod 64, 7·n2 mod 64) - macro_rules! apply_s_box { - ($j:expr) => {{ - let (m1, n1, m2, n2) = ROTATION_PARAMS[$j]; - let (s0, s1, s2) = bash_s(state[$j], state[8 + $j], state[16 + $j], m1, n1, m2, n2); - state[$j] = s0; - state[8 + $j] = s1; - state[16 + $j] = s2; - }}; - } - - #[cfg(feature = "no_unroll")] for j in 0..8 { - apply_s_box!(j); - } - - #[cfg(not(feature = "no_unroll"))] - { - apply_s_box!(0); - apply_s_box!(1); - apply_s_box!(2); - apply_s_box!(3); - apply_s_box!(4); - apply_s_box!(5); - apply_s_box!(6); - apply_s_box!(7); + // 3.2.a. (Sj, S8+j, S16+j) ← bash-s(Sj, S8+j, S16+j, m1, n1, m2, n2) + // 3.2.b. (m1, n1, m2, n2) ← (7·m1 mod 64, 7·n1 mod 64, 7·m2 mod 64, 7·n2 mod 64) + let (m1, n1, m2, n2) = ROTATION_PARAMS[j]; + let (s0, s1, s2) = bash_s(state[j], state[8 + j], state[16 + j], m1, n1, m2, n2); + state[j] = s0; + state[8 + j] = s1; + state[16 + j] = s2; } // 3.3. Apply word permutation From 02b6a154d3fddcf88114b6fe628a9e4617885084 Mon Sep 17 00:00:00 2001 From: Alexandr Kitaev Date: Wed, 22 Oct 2025 20:48:11 +0300 Subject: [PATCH 15/16] `bash-f`: STATE_WORDS is now pub --- bash-f/benches/mod.rs | 4 +--- bash-f/src/lib.rs | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/bash-f/benches/mod.rs b/bash-f/benches/mod.rs index 3c5f2dc..1fd2a38 100644 --- a/bash-f/benches/mod.rs +++ b/bash-f/benches/mod.rs @@ -1,11 +1,9 @@ #![feature(test)] extern crate test; -use bash_f::bash_f; +use bash_f::{STATE_WORDS, bash_f}; use test::Bencher; -const STATE_WORDS: usize = 24; - #[bench] fn bench_bash_f(b: &mut Bencher) { let mut state = [0u64; STATE_WORDS]; diff --git a/bash-f/src/lib.rs b/bash-f/src/lib.rs index 4ad3499..7ccf440 100644 --- a/bash-f/src/lib.rs +++ b/bash-f/src/lib.rs @@ -8,7 +8,7 @@ #![warn(missing_docs)] /// Number of 64-bit words in the state -const STATE_WORDS: usize = 24; +pub const STATE_WORDS: usize = 24; /// Precalculated rotation params const ROTATION_PARAMS: [(u32, u32, u32, u32); 8] = { From f10afda76f4426e16fa4528292890f488c6b18a0 Mon Sep 17 00:00:00 2001 From: Alexandr Kitaev Date: Wed, 22 Oct 2025 21:00:10 +0300 Subject: [PATCH 16/16] `bash-f`: remove precalculation, fix README.md, fix doc --- bash-f/README.md | 6 ------ bash-f/src/lib.rs | 29 ++++++----------------------- 2 files changed, 6 insertions(+), 29 deletions(-) diff --git a/bash-f/README.md b/bash-f/README.md index 84ea808..8c8b510 100644 --- a/bash-f/README.md +++ b/bash-f/README.md @@ -18,12 +18,6 @@ The standard defines a family of cryptographic algorithms built on a sponge construction with the `bash-f` sponge function at its core. The sponge function operates on 1536-bit (192-byte) states. -## Note on Byte Order - -The specification uses big-endian representation for test vectors, while -internal computation uses little-endian. -The public API handles byte swapping automatically. - ## Security Notes No security audits of this crate have ever been performed, and it has not been thoroughly assessed to ensure its operation is constant-time on common CPU architectures. diff --git a/bash-f/src/lib.rs b/bash-f/src/lib.rs index 7ccf440..cb578db 100644 --- a/bash-f/src/lib.rs +++ b/bash-f/src/lib.rs @@ -10,26 +10,6 @@ /// Number of 64-bit words in the state pub const STATE_WORDS: usize = 24; -/// Precalculated rotation params -const ROTATION_PARAMS: [(u32, u32, u32, u32); 8] = { - let mut params = [(0u32, 0u32, 0u32, 0u32); 8]; - let mut m1 = 8u32; - let mut n1 = 53u32; - let mut m2 = 14u32; - let mut n2 = 1u32; - - let mut j = 0; - while j < 8 { - params[j] = (m1, n1, m2, n2); - m1 = (7 * m1) % 64; - n1 = (7 * n1) % 64; - m2 = (7 * m2) % 64; - n2 = (7 * n2) % 64; - j += 1; - } - params -}; - /// `bash-s` transformation. /// /// Implements the S-box transformation defined in Section 6.1 of STB 34.101.77-2020. @@ -90,7 +70,7 @@ fn bash_s( /// /// # Parameters /// -/// - `state`: Mutable reference to 24 × 64-bit words (1536 bits total) in little-endian internal representation +/// - `state`: Mutable reference to 24 × 64-bit words (1536 bits total) pub fn bash_f(state: &mut [u64; STATE_WORDS]) { // 1. Split S into words (S0, S1, ..., S23) @@ -101,15 +81,18 @@ pub fn bash_f(state: &mut [u64; STATE_WORDS]) { for _ in 0..STATE_WORDS { // 3.1. Apply S-box layer with varying rotation parameters // (m1, n1, m2, n2) ← (8, 53, 14, 1) + let (mut m1, mut n1, mut m2, mut n2) = (8, 53, 14, 1); + // 3.2. For j = 0, 1, ..., 7 apply bash-s to each of 8 columns for j in 0..8 { // 3.2.a. (Sj, S8+j, S16+j) ← bash-s(Sj, S8+j, S16+j, m1, n1, m2, n2) - // 3.2.b. (m1, n1, m2, n2) ← (7·m1 mod 64, 7·n1 mod 64, 7·m2 mod 64, 7·n2 mod 64) - let (m1, n1, m2, n2) = ROTATION_PARAMS[j]; let (s0, s1, s2) = bash_s(state[j], state[8 + j], state[16 + j], m1, n1, m2, n2); state[j] = s0; state[8 + j] = s1; state[16 + j] = s2; + + // 3.2.b. (m1, n1, m2, n2) ← (7·m1 mod 64, 7·n1 mod 64, 7·m2 mod 64, 7·n2 mod 64) + (m1, n1, m2, n2) = ((7 * m1) % 64, (7 * n1) % 64, (7 * m2) % 64, (7 * n2) % 64); } // 3.3. Apply word permutation