From a164748dfe4c2cc2dd45fbc8c781e212e56e8b36 Mon Sep 17 00:00:00 2001 From: ashWhiteHat Date: Tue, 4 Jul 2023 09:49:47 +0900 Subject: [PATCH 01/15] rc6: init rc6 lib --- Cargo.lock | 4 +++ Cargo.toml | 1 + rc6/Cargo.toml | 13 +++++++++ rc6/LICENSE-APACHE | 13 +++++++++ rc6/LICENSE-MIT | 25 ++++++++++++++++ rc6/README.md | 72 ++++++++++++++++++++++++++++++++++++++++++++++ rc6/src/lib.rs | 14 +++++++++ 7 files changed, 142 insertions(+) create mode 100644 rc6/Cargo.toml create mode 100644 rc6/LICENSE-APACHE create mode 100644 rc6/LICENSE-MIT create mode 100644 rc6/README.md create mode 100644 rc6/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 57e8b41e..fbd326bc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -190,6 +190,10 @@ dependencies = [ "cipher", ] +[[package]] +name = "rc6" +version = "0.1.0" + [[package]] name = "serpent" version = "0.6.0-pre" diff --git a/Cargo.toml b/Cargo.toml index cbf5fa31..7585a409 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,7 @@ members = [ "magma", "rc2", "rc5", + "rc6", "serpent", "sm4", "speck", diff --git a/rc6/Cargo.toml b/rc6/Cargo.toml new file mode 100644 index 00000000..8f1e4a45 --- /dev/null +++ b/rc6/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "rc6" +version = "0.1.0" +description = "RC6 block cipher" +authors = ["RustCrypto Developers"] +edition = "2021" +license = "MIT OR Apache-2.0" +readme = "README.md" +repository = "https://github.com/RustCrypto/block-ciphers" +keywords = ["crypto", "rc6", "block-cipher"] +categories = ["cryptography"] + +[dependencies] diff --git a/rc6/LICENSE-APACHE b/rc6/LICENSE-APACHE new file mode 100644 index 00000000..652489d7 --- /dev/null +++ b/rc6/LICENSE-APACHE @@ -0,0 +1,13 @@ +Copyright 2017 Damian Czaja + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. diff --git a/rc6/LICENSE-MIT b/rc6/LICENSE-MIT new file mode 100644 index 00000000..069bb21e --- /dev/null +++ b/rc6/LICENSE-MIT @@ -0,0 +1,25 @@ +Copyright (c) 2017 Damian Czaja + +Permission is hereby granted, free of charge, to any +person obtaining a copy of this software and associated +documentation files (the "Software"), to deal in the +Software without restriction, including without +limitation the rights to use, copy, modify, merge, +publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software +is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice +shall be included in all copies or substantial portions +of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF +ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED +TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A +PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT +SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR +IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. diff --git a/rc6/README.md b/rc6/README.md new file mode 100644 index 00000000..7b54516e --- /dev/null +++ b/rc6/README.md @@ -0,0 +1,72 @@ +# [RustCrypto]: RC6 Cipher + +[![crate][crate-image]][crate-link] +[![Docs][docs-image]][docs-link] +[![Build Status][build-image]][build-link] +![Apache2/MIT licensed][license-image] +![Rust Version][rustc-image] +[![Project Chat][chat-image]][chat-link] +[![HAZMAT][hazmat-image]][hazmat-link] + +Pure Rust implementation of the [RC6] block cipher. + +[Documentation][docs-link] + +## ⚠️ Security Warning: [Hazmat!][hazmat-link] + +This crate does not ensure ciphertexts are authentic (i.e. by using a MAC to +verify ciphertext integrity), which can lead to serious vulnerabilities +if used incorrectly! + +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! + +## Minimum Supported Rust Version + +Rust **1.56** or higher. + +Minimum supported Rust version can be changed in the future, but it will be +done with a minor version bump. + +## SemVer Policy + +- All on-by-default features of this library are covered by SemVer +- MSRV is considered exempt from SemVer as noted above + +## License + +Licensed under either of: + + * [Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0) + * [MIT license](http://opensource.org/licenses/MIT) + +at your option. + +### Contribution + +Unless you explicitly state otherwise, any contribution intentionally submitted +for inclusion in the work by you, as defined in the Apache-2.0 license, shall be +dual licensed as above, without any additional terms or conditions. + +[//]: # (badges) + +[crate-image]: https://img.shields.io/crates/v/rc6.svg +[crate-link]: https://crates.io/crates/rc6 +[docs-image]: https://docs.rs/rc6/badge.svg +[docs-link]: https://docs.rs/rc6/ +[license-image]: https://img.shields.io/badge/license-Apache2.0/MIT-blue.svg +[rustc-image]: https://img.shields.io/badge/rustc-1.56+-blue.svg +[hazmat-image]: https://img.shields.io/badge/crypto-hazmat%E2%9A%A0-red.svg +[hazmat-link]: https://github.com/RustCrypto/meta/blob/master/HAZMAT.md +[chat-image]: https://img.shields.io/badge/zulip-join_chat-blue.svg +[chat-link]: https://rustcrypto.zulipchat.com/#narrow/stream/260039-block-ciphers +[build-image]: https://github.com/RustCrypto/block-ciphers/actions/workflows/rc6.yml/badge.svg +[build-link]: https://github.com/RustCrypto/block-ciphers/actions/workflows/rc6.yml + +[//]: # (general links) + +[RustCrypto]: https://github.com/RustCrypto/ +[RC6]: https://en.wikipedia.org/wiki/RC6 diff --git a/rc6/src/lib.rs b/rc6/src/lib.rs new file mode 100644 index 00000000..7d12d9af --- /dev/null +++ b/rc6/src/lib.rs @@ -0,0 +1,14 @@ +pub fn add(left: usize, right: usize) -> usize { + left + right +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn it_works() { + let result = add(2, 2); + assert_eq!(result, 4); + } +} From e74650c46df66d082ddff9948bc5ecf16dd4bc75 Mon Sep 17 00:00:00 2001 From: ashWhiteHat Date: Tue, 4 Jul 2023 10:33:49 +0900 Subject: [PATCH 02/15] rc6: rc6 word operations --- Cargo.lock | 3 +++ rc6/Cargo.toml | 7 +++++++ rc6/src/core/mod.rs | 6 ++++++ rc6/src/core/primitives.rs | 28 ++++++++++++++++++++++++++++ rc6/src/lib.rs | 15 ++------------- 5 files changed, 46 insertions(+), 13 deletions(-) create mode 100644 rc6/src/core/mod.rs create mode 100644 rc6/src/core/primitives.rs diff --git a/Cargo.lock b/Cargo.lock index fbd326bc..7fc01f51 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -193,6 +193,9 @@ dependencies = [ [[package]] name = "rc6" version = "0.1.0" +dependencies = [ + "cipher", +] [[package]] name = "serpent" diff --git a/rc6/Cargo.toml b/rc6/Cargo.toml index 8f1e4a45..8879ce33 100644 --- a/rc6/Cargo.toml +++ b/rc6/Cargo.toml @@ -11,3 +11,10 @@ keywords = ["crypto", "rc6", "block-cipher"] categories = ["cryptography"] [dependencies] +cipher = { version = "0.4.3", features = ["zeroize"] } + +[dev-dependencies] +cipher = { version = "0.4.3", features = ["dev"] } + +[features] +zeroize = [] diff --git a/rc6/src/core/mod.rs b/rc6/src/core/mod.rs new file mode 100644 index 00000000..2370a1e2 --- /dev/null +++ b/rc6/src/core/mod.rs @@ -0,0 +1,6 @@ +//! Implementation according to the [RC6 paper] +//! [RC6 paper]: https://www.grc.com/r&d/rc6.pdf + +mod primitives; + +pub use primitives::*; diff --git a/rc6/src/core/primitives.rs b/rc6/src/core/primitives.rs new file mode 100644 index 00000000..8401cb1c --- /dev/null +++ b/rc6/src/core/primitives.rs @@ -0,0 +1,28 @@ +use cipher::{ + generic_array::{ArrayLength, GenericArray}, + zeroize::DefaultIsZeroes, +}; +use core::ops::Add; + +pub trait Word: Default + Copy + From + Add + DefaultIsZeroes + Default { + type Bytes: ArrayLength; + + const ZERO: Self; + const THREE: Self; + const EIGHT: Self; + + const P: Self; + const Q: Self; + + fn wrapping_add(self, rhs: Self) -> Self; + fn wrapping_sub(self, rhs: Self) -> Self; + fn wrapping_mul(self, rhs: Self) -> Self; + + fn rotate_left(self, n: Self) -> Self; + fn rotate_right(self, n: Self) -> Self; + + fn from_le_bytes(bytes: &GenericArray) -> Self; + fn to_le_bytes(self) -> GenericArray; + + fn bitxor(self, other: Self) -> Self; +} diff --git a/rc6/src/lib.rs b/rc6/src/lib.rs index 7d12d9af..c949fea5 100644 --- a/rc6/src/lib.rs +++ b/rc6/src/lib.rs @@ -1,14 +1,3 @@ -pub fn add(left: usize, right: usize) -> usize { - left + right -} +#![no_std] -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn it_works() { - let result = add(2, 2); - assert_eq!(result, 4); - } -} +mod core; From 08dfd36530031cbd0a9eda93c64e115ed31e26bc Mon Sep 17 00:00:00 2001 From: ashWhiteHat Date: Tue, 4 Jul 2023 13:47:14 +0900 Subject: [PATCH 03/15] rc6: define test case --- rc6/src/block_cipher.rs | 7 ++++ rc6/src/lib.rs | 1 + rc6/tests/mod.rs | 82 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+) create mode 100644 rc6/src/block_cipher.rs create mode 100644 rc6/tests/mod.rs diff --git a/rc6/src/block_cipher.rs b/rc6/src/block_cipher.rs new file mode 100644 index 00000000..356d9ef3 --- /dev/null +++ b/rc6/src/block_cipher.rs @@ -0,0 +1,7 @@ +use crate::core::RC6; +use cipher::typenum::{U12, U16, U20, U24, U4, U8}; + +pub type RC6_8_12_4 = RC6; +pub type RC6_16_16_8 = RC6; +pub type RC6_32_20_16 = RC6; +pub type RC6_64_24_24 = RC6; diff --git a/rc6/src/lib.rs b/rc6/src/lib.rs index c949fea5..d6fe6b5d 100644 --- a/rc6/src/lib.rs +++ b/rc6/src/lib.rs @@ -1,3 +1,4 @@ #![no_std] +mod block_cipher; mod core; diff --git a/rc6/tests/mod.rs b/rc6/tests/mod.rs new file mode 100644 index 00000000..d348450c --- /dev/null +++ b/rc6/tests/mod.rs @@ -0,0 +1,82 @@ +/// generated using the code in: https://www.ietf.org/archive/id/draft-krovetz-rc6-rc5-vectors-00.txt +#[cfg(test)] +mod tests { + use cipher::{generic_array::GenericArray, BlockDecrypt, BlockEncrypt, KeyInit}; + use rc6::{RC6_16_16_8, RC6_32_20_16, RC6_64_24_24, RC6_8_12_4}; + + #[test] + fn enc_dec_8_12_4() { + let key = [0x00, 0x01, 0x02, 0x03]; + + let pt = [0x00, 0x01, 0x02, 0x03]; + let ct = [0xAE, 0xFC, 0x46, 0x12]; + + let rc5 = ::new_from_slice(&key).unwrap(); + let mut block = GenericArray::clone_from_slice(&pt); + rc5.encrypt_block(&mut block); + + assert_eq!(ct, block[..]); + } + + #[test] + fn enc_dec_16_16_8() { + let key = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07]; + + let pt = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07]; + let ct = [0x2F, 0xF0, 0xB6, 0x8E, 0xAE, 0xFF, 0xAD, 0x5B]; + + let rc5 = ::new_from_slice(&key).unwrap(); + let mut block = GenericArray::clone_from_slice(&pt); + rc5.encrypt_block(&mut block); + + assert_eq!(ct, block[..]); + } + + #[test] + fn enc_dec_32_20_16() { + let key = [ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, + 0x0E, 0x0F, + ]; + + let pt = [ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, + 0x0E, 0x0F, + ]; + let ct = [ + 0x3A, 0x96, 0xF9, 0xC7, 0xF6, 0x75, 0x5C, 0xFE, 0x46, 0xF0, 0x0E, 0x3D, 0xCD, 0x5D, + 0x2A, 0x3C, + ]; + + let rc5 = ::new_from_slice(&key).unwrap(); + let mut block = GenericArray::clone_from_slice(&pt); + rc5.encrypt_block(&mut block); + + assert_eq!(ct, block[..]); + } + + #[test] + fn enc_dec_64_24_24() { + let key = [ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, + 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, + ]; + + let pt = [ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, + 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, + 0x1C, 0x1D, 0x1E, 0x1F, + ]; + let ct = [ + 0xC0, 0x02, 0xDE, 0x05, 0x0B, 0xD5, 0x5E, 0x5D, 0x36, 0x86, 0x4A, 0xB9, 0x85, 0x33, + 0x38, 0xE6, 0xDC, 0x4A, 0x13, 0x26, 0xC6, 0xBD, 0xAA, 0xEB, 0x1B, 0xC9, 0xE4, 0xFD, + 0x67, 0x88, 0x66, 0x17, + ]; + + let rc5 = ::new_from_slice(&key).unwrap(); + let mut block = GenericArray::clone_from_slice(&pt); + rc5.encrypt_block(&mut block); + + assert_eq!(ct, block[..]); + } +} From 06e8acb732cf309ae3a69f3ecb7502fa55f4c541 Mon Sep 17 00:00:00 2001 From: ashWhiteHat Date: Tue, 4 Jul 2023 14:13:05 +0900 Subject: [PATCH 04/15] rc6: draft encrypt block --- rc6/src/block_cipher.rs | 348 ++++++++++++++++++++++++++++++++++++- rc6/src/core/backend.rs | 185 ++++++++++++++++++++ rc6/src/core/mod.rs | 2 + rc6/src/core/primitives.rs | 221 ++++++++++++++++++++++- rc6/src/lib.rs | 2 + rc6/tests/mod.rs | 2 +- 6 files changed, 756 insertions(+), 4 deletions(-) create mode 100644 rc6/src/core/backend.rs diff --git a/rc6/src/block_cipher.rs b/rc6/src/block_cipher.rs index 356d9ef3..71f76c1b 100644 --- a/rc6/src/block_cipher.rs +++ b/rc6/src/block_cipher.rs @@ -1,5 +1,349 @@ -use crate::core::RC6; -use cipher::typenum::{U12, U16, U20, U24, U4, U8}; +use core::ops::{Add, Div, Mul, Sub}; + +use cipher::{ + generic_array::ArrayLength, + inout::InOut, + typenum::{Diff, IsLess, Le, NonZero, Sum, Unsigned, U1, U12, U16, U2, U20, U24, U256, U4, U8}, + AlgorithmName, Block, BlockBackend, BlockCipher, BlockDecrypt, BlockEncrypt, BlockSizeUser, + KeyInit, KeySizeUser, ParBlocksSizeUser, +}; + +use crate::core::{BlockSize, ExpandedKeyTableSize, KeyAsWordsSize, Word, RC6}; + +impl KeyInit for RC6 +where + W: Word, + // Block size + W::Bytes: Mul, + BlockSize: ArrayLength, + // Rounds range + R: Unsigned, + R: IsLess, + Le: NonZero, + // ExpandedKeyTableSize + R: Add, + Sum: Mul, + ExpandedKeyTableSize: ArrayLength, + // Key range + B: ArrayLength, + B: IsLess, + Le: NonZero, + // KeyAsWordsSize + B: Add, + Sum: Sub, + Diff, U1>: Div, + KeyAsWordsSize: ArrayLength, +{ + fn new(key: &cipher::Key) -> Self { + Self::new(key) + } +} + +impl KeySizeUser for RC6 +where + W: Word, + // Block size + W::Bytes: Mul, + BlockSize: ArrayLength, + // Rounds range + R: Unsigned, + R: IsLess, + Le: NonZero, + // ExpandedKeyTableSize + R: Add, + Sum: Mul, + ExpandedKeyTableSize: ArrayLength, + B: ArrayLength, +{ + type KeySize = B; +} + +impl BlockCipher for RC6 +where + W: Word, + // Block size + W::Bytes: Mul, + BlockSize: ArrayLength, + // Rounds range + R: Unsigned, + R: IsLess, + Le: NonZero, + // ExpandedKeyTableSize + R: Add, + Sum: Mul, + ExpandedKeyTableSize: ArrayLength, +{ +} + +impl BlockSizeUser for RC6 +where + W: Word, + // Block size + W::Bytes: Mul, + BlockSize: ArrayLength, + // Rounds range + R: Unsigned, + R: IsLess, + Le: NonZero, + // ExpandedKeyTableSize + R: Add, + Sum: Mul, + ExpandedKeyTableSize: ArrayLength, +{ + type BlockSize = BlockSize; +} + +impl BlockEncrypt for RC6 +where + W: Word, + // Block size + W::Bytes: Mul, + BlockSize: ArrayLength, + // Rounds range + R: Unsigned, + R: IsLess, + Le: NonZero, + // ExpandedKeyTableSize + R: Add, + Sum: Mul, + ExpandedKeyTableSize: ArrayLength, + // Key range + B: ArrayLength, + B: IsLess, + Le: NonZero, + // KeyAsWordsSize + B: Add, + Sum: Sub, + Diff, U1>: Div, + KeyAsWordsSize: ArrayLength, +{ + fn encrypt_with_backend(&self, f: impl cipher::BlockClosure) { + f.call(&mut RC6EncryptBackend { enc_dec: self }) + } +} + +struct RC6EncryptBackend<'a, W, R, B> +where + W: Word, + // Block size + W::Bytes: Mul, + BlockSize: ArrayLength, + // Rounds range + R: Unsigned, + R: IsLess, + Le: NonZero, + // ExpandedKeyTableSize + R: Add, + Sum: Mul, + ExpandedKeyTableSize: ArrayLength, +{ + enc_dec: &'a RC6, +} +impl<'a, W, R, B> BlockSizeUser for RC6EncryptBackend<'a, W, R, B> +where + W: Word, + // Block size + W::Bytes: Mul, + BlockSize: ArrayLength, + // Rounds range + R: Unsigned, + R: IsLess, + Le: NonZero, + // ExpandedKeyTableSize + R: Add, + Sum: Mul, + ExpandedKeyTableSize: ArrayLength, +{ + type BlockSize = BlockSize; +} + +impl<'a, W, R, B> ParBlocksSizeUser for RC6EncryptBackend<'a, W, R, B> +where + W: Word, + // Block size + W::Bytes: Mul, + BlockSize: ArrayLength, + // Rounds range + R: Unsigned, + R: IsLess, + Le: NonZero, + // ExpandedKeyTableSize + R: Add, + Sum: Mul, + ExpandedKeyTableSize: ArrayLength, +{ + type ParBlocksSize = U1; +} + +impl<'a, W, R, B> BlockBackend for RC6EncryptBackend<'a, W, R, B> +where + W: Word, + // Block size + W::Bytes: Mul, + BlockSize: ArrayLength, + // Rounds range + R: Unsigned, + R: IsLess, + Le: NonZero, + // ExpandedKeyTableSize + R: Add, + Sum: Mul, + ExpandedKeyTableSize: ArrayLength, + // Key range + B: ArrayLength, + B: IsLess, + Le: NonZero, + // KeyAsWordsSize + B: Add, + Sum: Sub, + Diff, U1>: Div, + KeyAsWordsSize: ArrayLength, +{ + #[inline(always)] + fn proc_block(&mut self, block: InOut<'_, '_, Block>) { + let backend = self.enc_dec; + backend.encrypt(block) + } +} + +impl BlockDecrypt for RC6 +where + W: Word, + // Block size + W::Bytes: Mul, + BlockSize: ArrayLength, + // Rounds range + R: Unsigned, + R: IsLess, + Le: NonZero, + // ExpandedKeyTableSize + R: Add, + Sum: Mul, + ExpandedKeyTableSize: ArrayLength, + // Key range + B: ArrayLength, + B: IsLess, + Le: NonZero, + // KeyAsWordsSize + B: Add, + Sum: Sub, + Diff, U1>: Div, + KeyAsWordsSize: ArrayLength, +{ + fn decrypt_with_backend(&self, f: impl cipher::BlockClosure) { + f.call(&mut RC6DecryptBackend { enc_dec: self }) + } +} + +struct RC6DecryptBackend<'a, W, R, B> +where + W: Word, + // Block size + W::Bytes: Mul, + BlockSize: ArrayLength, + // Rounds range + R: Unsigned, + R: IsLess, + Le: NonZero, + // ExpandedKeyTableSize + R: Add, + Sum: Mul, + ExpandedKeyTableSize: ArrayLength, +{ + enc_dec: &'a RC6, +} +impl<'a, W, R, B> BlockSizeUser for RC6DecryptBackend<'a, W, R, B> +where + W: Word, + // Block size + W::Bytes: Mul, + BlockSize: ArrayLength, + // Rounds range + R: Unsigned, + R: IsLess, + Le: NonZero, + // ExpandedKeyTableSize + R: Add, + Sum: Mul, + ExpandedKeyTableSize: ArrayLength, +{ + type BlockSize = BlockSize; +} + +impl<'a, W, R, B> ParBlocksSizeUser for RC6DecryptBackend<'a, W, R, B> +where + W: Word, + // Block size + W::Bytes: Mul, + BlockSize: ArrayLength, + // Rounds range + R: Unsigned, + R: IsLess, + Le: NonZero, + // ExpandedKeyTableSize + R: Add, + Sum: Mul, + ExpandedKeyTableSize: ArrayLength, +{ + type ParBlocksSize = U1; +} + +impl<'a, W, R, B> BlockBackend for RC6DecryptBackend<'a, W, R, B> +where + W: Word, + // Block size + W::Bytes: Mul, + BlockSize: ArrayLength, + // Rounds range + R: Unsigned, + R: IsLess, + Le: NonZero, + // ExpandedKeyTableSize + R: Add, + Sum: Mul, + ExpandedKeyTableSize: ArrayLength, + // Key range + B: ArrayLength, + B: IsLess, + Le: NonZero, + // KeyAsWordsSize + B: Add, + Sum: Sub, + Diff, U1>: Div, + KeyAsWordsSize: ArrayLength, +{ + #[inline(always)] + fn proc_block(&mut self, block: InOut<'_, '_, Block>) { + let backend = self.enc_dec; + backend.decrypt(block) + } +} + +impl AlgorithmName for RC6 +where + W: Word, + // Block size + W::Bytes: Mul, + BlockSize: ArrayLength, + // Rounds range + R: Unsigned, + R: IsLess, + Le: NonZero, + // ExpandedKeyTableSize + R: Add, + Sum: Mul, + ExpandedKeyTableSize: ArrayLength, +{ + fn write_alg_name(f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + write!( + f, + "RC6 - {}/{}/{}", + core::any::type_name::(), + ::to_u8(), + ::to_u8(), + ) + } +} pub type RC6_8_12_4 = RC6; pub type RC6_16_16_8 = RC6; diff --git a/rc6/src/core/backend.rs b/rc6/src/core/backend.rs new file mode 100644 index 00000000..1a8f4ea6 --- /dev/null +++ b/rc6/src/core/backend.rs @@ -0,0 +1,185 @@ +use core::{ + cmp::max, + marker::PhantomData, + ops::{Add, Div, Mul, Sub}, +}; + +use cipher::{ + generic_array::{sequence::GenericSequence, ArrayLength, GenericArray}, + inout::InOut, + typenum::{Diff, IsLess, Le, NonZero, Sum, U1, U2}, + typenum::{Unsigned, U256}, +}; + +use super::{ + Block, BlockSize, ExpandedKeyTable, ExpandedKeyTableSize, Key, KeyAsWords, KeyAsWordsSize, Word, +}; + +pub struct RC6 +where + W: Word, + R: Unsigned, + R: IsLess, + // ExpandedKeyTableSize + R: Add, + Sum: Mul, + ExpandedKeyTableSize: ArrayLength, +{ + key_table: ExpandedKeyTable, + _key_size: PhantomData, +} + +impl RC6 +where + W: Word, + // Block size + W::Bytes: Mul, + BlockSize: ArrayLength, + // Rounds range + R: Unsigned, + R: IsLess, + Le: NonZero, + // ExpandedKeyTableSize + R: Add, + Sum: Mul, + ExpandedKeyTableSize: ArrayLength, + // Key range + B: ArrayLength, + B: IsLess, + Le: NonZero, + // KeyAsWordsSize + B: Add, + Sum: Sub, + Diff, U1>: Div, + KeyAsWordsSize: ArrayLength, +{ + pub fn new(key: &Key) -> RC6 { + Self { + key_table: Self::substitute_key(key), + _key_size: PhantomData, + } + } + + fn substitute_key(key: &Key) -> ExpandedKeyTable { + let key_as_words = Self::key_into_words(key); + let expanded_key_table = Self::initialize_expanded_key_table(); + + Self::mix_in(expanded_key_table, key_as_words) + } + + fn key_into_words(key: &Key) -> KeyAsWords { + // can be uninitialized + let mut key_as_words: GenericArray> = GenericArray::default(); + + for i in (0..B::USIZE).rev() { + key_as_words[i / W::Bytes::USIZE] = + key_as_words[i / W::Bytes::USIZE].rotate_left(W::EIGHT) + key[i].into(); + // no need for wrapping addition since we are adding a byte sized uint onto an uint with its lsb byte zeroed + } + + key_as_words + } + + fn initialize_expanded_key_table() -> ExpandedKeyTable { + // must be zero initialized + let mut expanded_key_table: GenericArray> = + GenericArray::generate(|_| W::ZERO); + + expanded_key_table[0] = W::P; + for i in 1..expanded_key_table.len() { + expanded_key_table[i] = expanded_key_table[i - 1].wrapping_add(W::Q); + } + + expanded_key_table + } + + fn mix_in( + mut key_table: ExpandedKeyTable, + mut key_as_words: KeyAsWords, + ) -> ExpandedKeyTable { + let (mut expanded_key_index, mut key_as_words_index) = (0, 0); + let (mut a, mut b) = (W::ZERO, W::ZERO); + + for _ in 0..3 * max(key_as_words.len(), key_table.len()) { + key_table[expanded_key_index] = key_table[expanded_key_index] + .wrapping_add(a) + .wrapping_add(b) + .rotate_left(W::THREE); + + a = key_table[expanded_key_index]; + + key_as_words[key_as_words_index] = key_as_words[key_as_words_index] + .wrapping_add(a) + .wrapping_add(b) + .rotate_left(a.wrapping_add(b)); + + b = key_as_words[key_as_words_index]; + + expanded_key_index = (expanded_key_index + 1) % key_table.len(); + key_as_words_index = (key_as_words_index + 1) % key_as_words.len(); + } + + key_table + } +} + +impl RC6 +where + W: Word, + // Block size + W::Bytes: Mul, + BlockSize: ArrayLength, + // Rounds range + R: Unsigned, + R: IsLess, + Le: NonZero, + // ExpandedKeyTableSize + R: Add, + Sum: Mul, + ExpandedKeyTableSize: ArrayLength, +{ + pub fn encrypt(&self, mut block: InOut<'_, '_, Block>) { + let (mut a, mut b) = Self::words_from_block(block.get_in()); + let key = &self.key_table; + + a = a.wrapping_add(key[0]); + b = b.wrapping_add(key[1]); + + for i in 1..=R::USIZE { + a = a.bitxor(b).rotate_left(b).wrapping_add(key[2 * i]); + b = b.bitxor(a).rotate_left(a).wrapping_add(key[2 * i + 1]); + } + + Self::block_from_words(a, b, block.get_out()) + } + + pub fn decrypt(&self, mut block: InOut<'_, '_, Block>) { + let (mut a, mut b) = Self::words_from_block(block.get_in()); + let key = &self.key_table; + + for i in (1..=R::USIZE).rev() { + b = b.wrapping_sub(key[2 * i + 1]).rotate_right(a).bitxor(a); + a = a.wrapping_sub(key[2 * i]).rotate_right(b).bitxor(b); + } + + b = b.wrapping_sub(key[1]); + a = a.wrapping_sub(key[0]); + + Self::block_from_words(a, b, block.get_out()) + } + + fn words_from_block(block: &Block) -> (W, W) { + // Block size is 2 * word::BYTES so the unwrap is safe + let a = W::from_le_bytes(block[..W::Bytes::USIZE].try_into().unwrap()); + let b = W::from_le_bytes(block[W::Bytes::USIZE..].try_into().unwrap()); + + (a, b) + } + + fn block_from_words(a: W, b: W, out_block: &mut Block) { + let (left, right) = out_block.split_at_mut(W::Bytes::USIZE); + + left.copy_from_slice(&a.to_le_bytes()); + right.copy_from_slice(&b.to_le_bytes()); + } +} diff --git a/rc6/src/core/mod.rs b/rc6/src/core/mod.rs index 2370a1e2..80ed305c 100644 --- a/rc6/src/core/mod.rs +++ b/rc6/src/core/mod.rs @@ -1,6 +1,8 @@ //! Implementation according to the [RC6 paper] //! [RC6 paper]: https://www.grc.com/r&d/rc6.pdf +mod backend; mod primitives; +pub use backend::RC6; pub use primitives::*; diff --git a/rc6/src/core/primitives.rs b/rc6/src/core/primitives.rs index 8401cb1c..07a5bde0 100644 --- a/rc6/src/core/primitives.rs +++ b/rc6/src/core/primitives.rs @@ -1,8 +1,21 @@ +use core::ops::{Add, BitXor}; + use cipher::{ generic_array::{ArrayLength, GenericArray}, + typenum::{Diff, Prod, Quot, Sum, U1, U2, U4, U8}, zeroize::DefaultIsZeroes, }; -use core::ops::Add; + +pub type BlockSize = Prod<::Bytes, U2>; +pub type Block = GenericArray>; + +pub type Key = GenericArray; + +pub type ExpandedKeyTable = GenericArray>; +pub type ExpandedKeyTableSize = Prod, U2>; + +pub type KeyAsWords = GenericArray>; +pub type KeyAsWordsSize = Quot::Bytes>, U1>, ::Bytes>; pub trait Word: Default + Copy + From + Add + DefaultIsZeroes + Default { type Bytes: ArrayLength; @@ -26,3 +39,209 @@ pub trait Word: Default + Copy + From + Add + DefaultIsZeroes fn bitxor(self, other: Self) -> Self; } + +impl Word for u8 { + type Bytes = U1; + + const ZERO: Self = 0; + const THREE: Self = 3; + const EIGHT: Self = 8; + + const P: Self = 0xb7; + const Q: Self = 0x9e; + + #[inline(always)] + fn wrapping_add(self, rhs: Self) -> Self { + u8::wrapping_add(self, rhs) + } + + #[inline(always)] + fn wrapping_sub(self, rhs: Self) -> Self { + u8::wrapping_sub(self, rhs) + } + + #[inline(always)] + fn wrapping_mul(self, rhs: Self) -> Self { + u8::wrapping_mul(self, rhs) + } + + #[inline(always)] + fn rotate_left(self, n: Self) -> Self { + u8::rotate_left(self, n as u32) + } + + #[inline(always)] + fn rotate_right(self, n: Self) -> Self { + u8::rotate_right(self, n as u32) + } + + #[inline(always)] + fn from_le_bytes(bytes: &GenericArray) -> Self { + u8::from_le_bytes(bytes.as_slice().try_into().unwrap()) + } + + #[inline(always)] + fn to_le_bytes(self) -> GenericArray { + u8::to_le_bytes(self).into() + } + + #[inline(always)] + fn bitxor(self, other: Self) -> Self { + ::bitxor(self, other) + } +} + +impl Word for u16 { + type Bytes = U2; + + const ZERO: Self = 0; + const THREE: Self = 3; + const EIGHT: Self = 8; + + const P: Self = 0xb7e1; + const Q: Self = 0x9e37; + + #[inline(always)] + fn wrapping_add(self, rhs: Self) -> Self { + u16::wrapping_add(self, rhs) + } + + #[inline(always)] + fn wrapping_sub(self, rhs: Self) -> Self { + u16::wrapping_sub(self, rhs) + } + + #[inline(always)] + fn wrapping_mul(self, rhs: Self) -> Self { + u16::wrapping_mul(self, rhs) + } + + #[inline(always)] + fn rotate_left(self, n: Self) -> Self { + u16::rotate_left(self, n as u32) + } + + #[inline(always)] + fn rotate_right(self, n: Self) -> Self { + u16::rotate_right(self, n as u32) + } + + #[inline(always)] + fn from_le_bytes(bytes: &GenericArray) -> Self { + u16::from_le_bytes(bytes.as_slice().try_into().unwrap()) + } + + #[inline(always)] + fn to_le_bytes(self) -> GenericArray { + u16::to_le_bytes(self).into() + } + + #[inline(always)] + fn bitxor(self, other: Self) -> Self { + ::bitxor(self, other) + } +} + +impl Word for u32 { + type Bytes = U4; + + const ZERO: Self = 0; + const THREE: Self = 3; + const EIGHT: Self = 8; + + const P: Self = 0xb7e15163; + const Q: Self = 0x9e3779b9; + + #[inline(always)] + fn wrapping_add(self, rhs: Self) -> Self { + u32::wrapping_add(self, rhs) + } + + #[inline(always)] + fn wrapping_sub(self, rhs: Self) -> Self { + u32::wrapping_sub(self, rhs) + } + + #[inline(always)] + fn wrapping_mul(self, rhs: Self) -> Self { + u32::wrapping_mul(self, rhs) + } + + #[inline(always)] + fn rotate_left(self, n: Self) -> Self { + u32::rotate_left(self, n) + } + + #[inline(always)] + fn rotate_right(self, n: Self) -> Self { + u32::rotate_right(self, n) + } + + #[inline(always)] + fn from_le_bytes(bytes: &GenericArray) -> Self { + u32::from_le_bytes(bytes.as_slice().try_into().unwrap()) + } + + #[inline(always)] + fn to_le_bytes(self) -> GenericArray { + u32::to_le_bytes(self).into() + } + + #[inline(always)] + fn bitxor(self, other: Self) -> Self { + ::bitxor(self, other) + } +} + +impl Word for u64 { + type Bytes = U8; + + const ZERO: Self = 0; + const THREE: Self = 3; + const EIGHT: Self = 8; + + const P: Self = 0xb7e151628aed2a6b; + const Q: Self = 0x9e3779b97f4a7c15; + + #[inline(always)] + fn wrapping_add(self, rhs: Self) -> Self { + u64::wrapping_add(self, rhs) + } + + #[inline(always)] + fn wrapping_sub(self, rhs: Self) -> Self { + u64::wrapping_sub(self, rhs) + } + + #[inline(always)] + fn wrapping_mul(self, rhs: Self) -> Self { + u64::wrapping_mul(self, rhs) + } + + #[inline(always)] + fn rotate_left(self, n: Self) -> Self { + let size = Self::BITS; + u64::rotate_left(self, (n % size as u64) as u32) + } + + #[inline(always)] + fn rotate_right(self, n: Self) -> Self { + let size = Self::BITS; + u64::rotate_right(self, (n % size as u64) as u32) + } + + #[inline(always)] + fn from_le_bytes(bytes: &GenericArray) -> Self { + u64::from_le_bytes(bytes.as_slice().try_into().unwrap()) + } + + #[inline(always)] + fn to_le_bytes(self) -> GenericArray { + u64::to_le_bytes(self).into() + } + + #[inline(always)] + fn bitxor(self, other: Self) -> Self { + ::bitxor(self, other) + } +} diff --git a/rc6/src/lib.rs b/rc6/src/lib.rs index d6fe6b5d..b2d584b0 100644 --- a/rc6/src/lib.rs +++ b/rc6/src/lib.rs @@ -2,3 +2,5 @@ mod block_cipher; mod core; + +pub use block_cipher::*; diff --git a/rc6/tests/mod.rs b/rc6/tests/mod.rs index d348450c..96d97639 100644 --- a/rc6/tests/mod.rs +++ b/rc6/tests/mod.rs @@ -1,7 +1,7 @@ /// generated using the code in: https://www.ietf.org/archive/id/draft-krovetz-rc6-rc5-vectors-00.txt #[cfg(test)] mod tests { - use cipher::{generic_array::GenericArray, BlockDecrypt, BlockEncrypt, KeyInit}; + use cipher::{generic_array::GenericArray, BlockEncrypt, KeyInit}; use rc6::{RC6_16_16_8, RC6_32_20_16, RC6_64_24_24, RC6_8_12_4}; #[test] From 318b7a6637511fe621a37c37ff683d65efa94b3a Mon Sep 17 00:00:00 2001 From: ashWhiteHat Date: Wed, 5 Jul 2023 10:04:57 +0900 Subject: [PATCH 05/15] rc6: modify block size --- rc6/src/block_cipher.rs | 62 +++++++++++++++++++------------------- rc6/src/core/backend.rs | 13 ++++---- rc6/src/core/primitives.rs | 4 +-- rc6/tests/mod.rs | 16 +++++----- 4 files changed, 47 insertions(+), 48 deletions(-) diff --git a/rc6/src/block_cipher.rs b/rc6/src/block_cipher.rs index 71f76c1b..2065e319 100644 --- a/rc6/src/block_cipher.rs +++ b/rc6/src/block_cipher.rs @@ -3,7 +3,7 @@ use core::ops::{Add, Div, Mul, Sub}; use cipher::{ generic_array::ArrayLength, inout::InOut, - typenum::{Diff, IsLess, Le, NonZero, Sum, Unsigned, U1, U12, U16, U2, U20, U24, U256, U4, U8}, + typenum::{Diff, IsLess, Le, NonZero, Sum, Unsigned, U1, U12, U16, U20, U24, U256, U4, U8}, AlgorithmName, Block, BlockBackend, BlockCipher, BlockDecrypt, BlockEncrypt, BlockSizeUser, KeyInit, KeySizeUser, ParBlocksSizeUser, }; @@ -14,7 +14,7 @@ impl KeyInit for RC6 where W: Word, // Block size - W::Bytes: Mul, + W::Bytes: Mul, BlockSize: ArrayLength, // Rounds range R: Unsigned, @@ -22,7 +22,7 @@ where Le: NonZero, // ExpandedKeyTableSize R: Add, - Sum: Mul, + Sum: Mul, ExpandedKeyTableSize: ArrayLength, // Key range B: ArrayLength, @@ -43,7 +43,7 @@ impl KeySizeUser for RC6 where W: Word, // Block size - W::Bytes: Mul, + W::Bytes: Mul, BlockSize: ArrayLength, // Rounds range R: Unsigned, @@ -51,7 +51,7 @@ where Le: NonZero, // ExpandedKeyTableSize R: Add, - Sum: Mul, + Sum: Mul, ExpandedKeyTableSize: ArrayLength, B: ArrayLength, { @@ -62,7 +62,7 @@ impl BlockCipher for RC6 where W: Word, // Block size - W::Bytes: Mul, + W::Bytes: Mul, BlockSize: ArrayLength, // Rounds range R: Unsigned, @@ -70,7 +70,7 @@ where Le: NonZero, // ExpandedKeyTableSize R: Add, - Sum: Mul, + Sum: Mul, ExpandedKeyTableSize: ArrayLength, { } @@ -79,7 +79,7 @@ impl BlockSizeUser for RC6 where W: Word, // Block size - W::Bytes: Mul, + W::Bytes: Mul, BlockSize: ArrayLength, // Rounds range R: Unsigned, @@ -87,7 +87,7 @@ where Le: NonZero, // ExpandedKeyTableSize R: Add, - Sum: Mul, + Sum: Mul, ExpandedKeyTableSize: ArrayLength, { type BlockSize = BlockSize; @@ -97,7 +97,7 @@ impl BlockEncrypt for RC6 where W: Word, // Block size - W::Bytes: Mul, + W::Bytes: Mul, BlockSize: ArrayLength, // Rounds range R: Unsigned, @@ -105,7 +105,7 @@ where Le: NonZero, // ExpandedKeyTableSize R: Add, - Sum: Mul, + Sum: Mul, ExpandedKeyTableSize: ArrayLength, // Key range B: ArrayLength, @@ -126,7 +126,7 @@ struct RC6EncryptBackend<'a, W, R, B> where W: Word, // Block size - W::Bytes: Mul, + W::Bytes: Mul, BlockSize: ArrayLength, // Rounds range R: Unsigned, @@ -134,7 +134,7 @@ where Le: NonZero, // ExpandedKeyTableSize R: Add, - Sum: Mul, + Sum: Mul, ExpandedKeyTableSize: ArrayLength, { enc_dec: &'a RC6, @@ -143,7 +143,7 @@ impl<'a, W, R, B> BlockSizeUser for RC6EncryptBackend<'a, W, R, B> where W: Word, // Block size - W::Bytes: Mul, + W::Bytes: Mul, BlockSize: ArrayLength, // Rounds range R: Unsigned, @@ -151,7 +151,7 @@ where Le: NonZero, // ExpandedKeyTableSize R: Add, - Sum: Mul, + Sum: Mul, ExpandedKeyTableSize: ArrayLength, { type BlockSize = BlockSize; @@ -161,7 +161,7 @@ impl<'a, W, R, B> ParBlocksSizeUser for RC6EncryptBackend<'a, W, R, B> where W: Word, // Block size - W::Bytes: Mul, + W::Bytes: Mul, BlockSize: ArrayLength, // Rounds range R: Unsigned, @@ -169,7 +169,7 @@ where Le: NonZero, // ExpandedKeyTableSize R: Add, - Sum: Mul, + Sum: Mul, ExpandedKeyTableSize: ArrayLength, { type ParBlocksSize = U1; @@ -179,7 +179,7 @@ impl<'a, W, R, B> BlockBackend for RC6EncryptBackend<'a, W, R, B> where W: Word, // Block size - W::Bytes: Mul, + W::Bytes: Mul, BlockSize: ArrayLength, // Rounds range R: Unsigned, @@ -187,7 +187,7 @@ where Le: NonZero, // ExpandedKeyTableSize R: Add, - Sum: Mul, + Sum: Mul, ExpandedKeyTableSize: ArrayLength, // Key range B: ArrayLength, @@ -210,7 +210,7 @@ impl BlockDecrypt for RC6 where W: Word, // Block size - W::Bytes: Mul, + W::Bytes: Mul, BlockSize: ArrayLength, // Rounds range R: Unsigned, @@ -218,7 +218,7 @@ where Le: NonZero, // ExpandedKeyTableSize R: Add, - Sum: Mul, + Sum: Mul, ExpandedKeyTableSize: ArrayLength, // Key range B: ArrayLength, @@ -239,7 +239,7 @@ struct RC6DecryptBackend<'a, W, R, B> where W: Word, // Block size - W::Bytes: Mul, + W::Bytes: Mul, BlockSize: ArrayLength, // Rounds range R: Unsigned, @@ -247,7 +247,7 @@ where Le: NonZero, // ExpandedKeyTableSize R: Add, - Sum: Mul, + Sum: Mul, ExpandedKeyTableSize: ArrayLength, { enc_dec: &'a RC6, @@ -256,7 +256,7 @@ impl<'a, W, R, B> BlockSizeUser for RC6DecryptBackend<'a, W, R, B> where W: Word, // Block size - W::Bytes: Mul, + W::Bytes: Mul, BlockSize: ArrayLength, // Rounds range R: Unsigned, @@ -264,7 +264,7 @@ where Le: NonZero, // ExpandedKeyTableSize R: Add, - Sum: Mul, + Sum: Mul, ExpandedKeyTableSize: ArrayLength, { type BlockSize = BlockSize; @@ -274,7 +274,7 @@ impl<'a, W, R, B> ParBlocksSizeUser for RC6DecryptBackend<'a, W, R, B> where W: Word, // Block size - W::Bytes: Mul, + W::Bytes: Mul, BlockSize: ArrayLength, // Rounds range R: Unsigned, @@ -282,7 +282,7 @@ where Le: NonZero, // ExpandedKeyTableSize R: Add, - Sum: Mul, + Sum: Mul, ExpandedKeyTableSize: ArrayLength, { type ParBlocksSize = U1; @@ -292,7 +292,7 @@ impl<'a, W, R, B> BlockBackend for RC6DecryptBackend<'a, W, R, B> where W: Word, // Block size - W::Bytes: Mul, + W::Bytes: Mul, BlockSize: ArrayLength, // Rounds range R: Unsigned, @@ -300,7 +300,7 @@ where Le: NonZero, // ExpandedKeyTableSize R: Add, - Sum: Mul, + Sum: Mul, ExpandedKeyTableSize: ArrayLength, // Key range B: ArrayLength, @@ -323,7 +323,7 @@ impl AlgorithmName for RC6 where W: Word, // Block size - W::Bytes: Mul, + W::Bytes: Mul, BlockSize: ArrayLength, // Rounds range R: Unsigned, @@ -331,7 +331,7 @@ where Le: NonZero, // ExpandedKeyTableSize R: Add, - Sum: Mul, + Sum: Mul, ExpandedKeyTableSize: ArrayLength, { fn write_alg_name(f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { diff --git a/rc6/src/core/backend.rs b/rc6/src/core/backend.rs index 1a8f4ea6..a5c7c1b6 100644 --- a/rc6/src/core/backend.rs +++ b/rc6/src/core/backend.rs @@ -7,8 +7,7 @@ use core::{ use cipher::{ generic_array::{sequence::GenericSequence, ArrayLength, GenericArray}, inout::InOut, - typenum::{Diff, IsLess, Le, NonZero, Sum, U1, U2}, - typenum::{Unsigned, U256}, + typenum::{Diff, IsLess, Le, NonZero, Sum, Unsigned, U1, U256, U4}, }; use super::{ @@ -22,7 +21,7 @@ where R: IsLess, // ExpandedKeyTableSize R: Add, - Sum: Mul, + Sum: Mul, ExpandedKeyTableSize: ArrayLength, { key_table: ExpandedKeyTable, @@ -33,7 +32,7 @@ impl RC6 where W: Word, // Block size - W::Bytes: Mul, + W::Bytes: Mul, BlockSize: ArrayLength, // Rounds range R: Unsigned, @@ -41,7 +40,7 @@ where Le: NonZero, // ExpandedKeyTableSize R: Add, - Sum: Mul, + Sum: Mul, ExpandedKeyTableSize: ArrayLength, // Key range B: ArrayLength, @@ -127,7 +126,7 @@ impl RC6 where W: Word, // Block size - W::Bytes: Mul, + W::Bytes: Mul, BlockSize: ArrayLength, // Rounds range R: Unsigned, @@ -135,7 +134,7 @@ where Le: NonZero, // ExpandedKeyTableSize R: Add, - Sum: Mul, + Sum: Mul, ExpandedKeyTableSize: ArrayLength, { pub fn encrypt(&self, mut block: InOut<'_, '_, Block>) { diff --git a/rc6/src/core/primitives.rs b/rc6/src/core/primitives.rs index 07a5bde0..f9556ed3 100644 --- a/rc6/src/core/primitives.rs +++ b/rc6/src/core/primitives.rs @@ -6,13 +6,13 @@ use cipher::{ zeroize::DefaultIsZeroes, }; -pub type BlockSize = Prod<::Bytes, U2>; +pub type BlockSize = Prod<::Bytes, U4>; pub type Block = GenericArray>; pub type Key = GenericArray; pub type ExpandedKeyTable = GenericArray>; -pub type ExpandedKeyTableSize = Prod, U2>; +pub type ExpandedKeyTableSize = Prod, U4>; pub type KeyAsWords = GenericArray>; pub type KeyAsWordsSize = Quot::Bytes>, U1>, ::Bytes>; diff --git a/rc6/tests/mod.rs b/rc6/tests/mod.rs index 96d97639..9d135591 100644 --- a/rc6/tests/mod.rs +++ b/rc6/tests/mod.rs @@ -11,9 +11,9 @@ mod tests { let pt = [0x00, 0x01, 0x02, 0x03]; let ct = [0xAE, 0xFC, 0x46, 0x12]; - let rc5 = ::new_from_slice(&key).unwrap(); + let rc6 = ::new_from_slice(&key).unwrap(); let mut block = GenericArray::clone_from_slice(&pt); - rc5.encrypt_block(&mut block); + rc6.encrypt_block(&mut block); assert_eq!(ct, block[..]); } @@ -25,9 +25,9 @@ mod tests { let pt = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07]; let ct = [0x2F, 0xF0, 0xB6, 0x8E, 0xAE, 0xFF, 0xAD, 0x5B]; - let rc5 = ::new_from_slice(&key).unwrap(); + let rc6 = ::new_from_slice(&key).unwrap(); let mut block = GenericArray::clone_from_slice(&pt); - rc5.encrypt_block(&mut block); + rc6.encrypt_block(&mut block); assert_eq!(ct, block[..]); } @@ -48,9 +48,9 @@ mod tests { 0x2A, 0x3C, ]; - let rc5 = ::new_from_slice(&key).unwrap(); + let rc6 = ::new_from_slice(&key).unwrap(); let mut block = GenericArray::clone_from_slice(&pt); - rc5.encrypt_block(&mut block); + rc6.encrypt_block(&mut block); assert_eq!(ct, block[..]); } @@ -73,9 +73,9 @@ mod tests { 0x67, 0x88, 0x66, 0x17, ]; - let rc5 = ::new_from_slice(&key).unwrap(); + let rc6 = ::new_from_slice(&key).unwrap(); let mut block = GenericArray::clone_from_slice(&pt); - rc5.encrypt_block(&mut block); + rc6.encrypt_block(&mut block); assert_eq!(ct, block[..]); } From 1e34613e5c34406465843e2ed08eed9fdda14bf3 Mon Sep 17 00:00:00 2001 From: ashWhiteHat Date: Wed, 31 Jul 2024 17:07:00 +0900 Subject: [PATCH 06/15] rc6: words block conversion test --- Cargo.lock | 93 ++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 70 insertions(+), 23 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7fc01f51..b78c8f58 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7,7 +7,7 @@ name = "aes" version = "0.9.0-pre.1" dependencies = [ "cfg-if", - "cipher", + "cipher 0.5.0-pre.6", "cpufeatures", "hex-literal", "zeroize", @@ -17,7 +17,7 @@ dependencies = [ name = "aria" version = "0.2.0-pre" dependencies = [ - "cipher", + "cipher 0.5.0-pre.6", "hex-literal", ] @@ -25,7 +25,7 @@ dependencies = [ name = "belt-block" version = "0.2.0-pre.1" dependencies = [ - "cipher", + "cipher 0.5.0-pre.6", "hex-literal", ] @@ -40,7 +40,7 @@ name = "blowfish" version = "0.10.0-pre.1" dependencies = [ "byteorder", - "cipher", + "cipher 0.5.0-pre.6", ] [[package]] @@ -54,14 +54,14 @@ name = "camellia" version = "0.2.0-pre" dependencies = [ "byteorder", - "cipher", + "cipher 0.5.0-pre.6", ] [[package]] name = "cast5" version = "0.12.0-pre" dependencies = [ - "cipher", + "cipher 0.5.0-pre.6", "hex-literal", ] @@ -69,7 +69,7 @@ dependencies = [ name = "cast6" version = "0.2.0-pre" dependencies = [ - "cipher", + "cipher 0.5.0-pre.6", "hex-literal", ] @@ -79,6 +79,18 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "cipher" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773f3b9af64447d2ce9850330c473515014aa235e6a783b02db81ff39e4a3dad" +dependencies = [ + "blobby", + "crypto-common 0.1.6", + "inout 0.1.3", + "zeroize", +] + [[package]] name = "cipher" version = "0.5.0-pre.6" @@ -86,8 +98,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c71c893d5a1e8257048dbb29954d2e1f85f091a150304f1defe4ca2806da5d3f" dependencies = [ "blobby", - "crypto-common", - "inout", + "crypto-common 0.2.0-rc.0", + "inout 0.2.0-rc.0", "zeroize", ] @@ -100,6 +112,16 @@ dependencies = [ "libc", ] +[[package]] +name = "crypto-common" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" +dependencies = [ + "generic-array", + "typenum", +] + [[package]] name = "crypto-common" version = "0.2.0-rc.0" @@ -113,14 +135,24 @@ dependencies = [ name = "des" version = "0.9.0-pre.1" dependencies = [ - "cipher", + "cipher 0.5.0-pre.6", +] + +[[package]] +name = "generic-array" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" +dependencies = [ + "typenum", + "version_check", ] [[package]] name = "gift-cipher" version = "0.0.0" dependencies = [ - "cipher", + "cipher 0.5.0-pre.6", ] [[package]] @@ -142,7 +174,16 @@ dependencies = [ name = "idea" version = "0.6.0-pre" dependencies = [ - "cipher", + "cipher 0.5.0-pre.6", +] + +[[package]] +name = "inout" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a0c10553d664a4d0bcff9f4215d0aac67a639cc68ef660840afe309b807bc9f5" +dependencies = [ + "generic-array", ] [[package]] @@ -158,7 +199,7 @@ dependencies = [ name = "kuznyechik" version = "0.9.0-pre.1" dependencies = [ - "cipher", + "cipher 0.5.0-pre.6", "hex-literal", ] @@ -172,7 +213,7 @@ checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" name = "magma" version = "0.10.0-pre.1" dependencies = [ - "cipher", + "cipher 0.5.0-pre.6", "hex-literal", ] @@ -180,21 +221,21 @@ dependencies = [ name = "rc2" version = "0.9.0-pre" dependencies = [ - "cipher", + "cipher 0.5.0-pre.6", ] [[package]] name = "rc5" version = "0.1.0-pre" dependencies = [ - "cipher", + "cipher 0.5.0-pre.6", ] [[package]] name = "rc6" version = "0.1.0" dependencies = [ - "cipher", + "cipher 0.4.4", ] [[package]] @@ -202,14 +243,14 @@ name = "serpent" version = "0.6.0-pre" dependencies = [ "byteorder", - "cipher", + "cipher 0.5.0-pre.6", ] [[package]] name = "sm4" version = "0.6.0-pre" dependencies = [ - "cipher", + "cipher 0.5.0-pre.6", "hex-literal", ] @@ -217,7 +258,7 @@ dependencies = [ name = "speck-cipher" version = "0.0.0" dependencies = [ - "cipher", + "cipher 0.5.0-pre.6", "hex-literal", ] @@ -225,7 +266,7 @@ dependencies = [ name = "threefish" version = "0.6.0-pre" dependencies = [ - "cipher", + "cipher 0.5.0-pre.6", "hex-literal", "zeroize", ] @@ -234,7 +275,7 @@ dependencies = [ name = "twofish" version = "0.8.0-pre" dependencies = [ - "cipher", + "cipher 0.5.0-pre.6", "hex-literal", ] @@ -244,11 +285,17 @@ version = "1.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" +[[package]] +name = "version_check" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" + [[package]] name = "xtea" version = "0.0.0" dependencies = [ - "cipher", + "cipher 0.5.0-pre.6", ] [[package]] From 933118b3bd328845c753a8c43f465de1cfc6e796 Mon Sep 17 00:00:00 2001 From: ashWhiteHat Date: Fri, 7 Jul 2023 10:51:31 +0900 Subject: [PATCH 07/15] rc6: draft encryption algorithm --- rc6/src/core/backend.rs | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/rc6/src/core/backend.rs b/rc6/src/core/backend.rs index a5c7c1b6..fcb90b06 100644 --- a/rc6/src/core/backend.rs +++ b/rc6/src/core/backend.rs @@ -140,16 +140,37 @@ where pub fn encrypt(&self, mut block: InOut<'_, '_, Block>) { let (mut a, mut b) = Self::words_from_block(block.get_in()); let key = &self.key_table; + let log_w = W::from((W::Bytes::USIZE as f64 * 8 as f64).log2() as u8); - a = a.wrapping_add(key[0]); - b = b.wrapping_add(key[1]); + // B = B + S[0] + b = b.wrapping_sub(key[0]); + // D = D + S[1] + d = d.wrapping_sub(key[1]); for i in 1..=R::USIZE { - a = a.bitxor(b).rotate_left(b).wrapping_add(key[2 * i]); - b = b.bitxor(a).rotate_left(a).wrapping_add(key[2 * i + 1]); + // 2B + 1 + let b2 = b.wrapping_mul(W::from(2)).wrapping_add(W::from(1)); + // B * 2B << log w + let t = b.wrapping_mul(b2).rotate_left(log_w); + // 2D + 1 + let d2 = d.wrapping_mul(W::from(2)).wrapping_add(W::from(1)); + // D * 2D << log w + let u = d.wrapping_mul(d2).rotate_left(log_w); + // (A xor t << u) + S[2i] + a = a.bitxor(t).rotate_left(u).wrapping_add(key[2 * i]); + // (C xor u << t) + S[2i + 1] + c = c.bitxor(u).rotate_left(t).wrapping_add(key[2 * i + 1]); + let (tmp_a, tmp_b, tmp_c, tmp_d) = (b, c, d, a); + a = tmp_a; + b = tmp_b; + c = tmp_c; + d = tmp_d; } - Self::block_from_words(a, b, block.get_out()) + a = a.wrapping_sub(key[2 * R::USIZE + 2]); + c = c.wrapping_sub(key[2 * R::USIZE + 3]); + + Self::block_from_words(a, b, c, d, block.get_out()) } pub fn decrypt(&self, mut block: InOut<'_, '_, Block>) { From 21bfb855fbba374cec25df3445a14bc8427a0032 Mon Sep 17 00:00:00 2001 From: ashWhiteHat Date: Sun, 9 Jul 2023 10:02:28 +0900 Subject: [PATCH 08/15] rc6: adjust table size --- rc6/src/block_cipher.rs | 64 +++++++++++++++++++------------------- rc6/src/core/backend.rs | 14 ++++----- rc6/src/core/primitives.rs | 7 ++--- 3 files changed, 42 insertions(+), 43 deletions(-) diff --git a/rc6/src/block_cipher.rs b/rc6/src/block_cipher.rs index 2065e319..57e4e816 100644 --- a/rc6/src/block_cipher.rs +++ b/rc6/src/block_cipher.rs @@ -3,7 +3,7 @@ use core::ops::{Add, Div, Mul, Sub}; use cipher::{ generic_array::ArrayLength, inout::InOut, - typenum::{Diff, IsLess, Le, NonZero, Sum, Unsigned, U1, U12, U16, U20, U24, U256, U4, U8}, + typenum::{Diff, IsLess, Le, NonZero, Sum, Unsigned, U1, U12, U16, U2, U20, U24, U256, U4, U8}, AlgorithmName, Block, BlockBackend, BlockCipher, BlockDecrypt, BlockEncrypt, BlockSizeUser, KeyInit, KeySizeUser, ParBlocksSizeUser, }; @@ -21,8 +21,8 @@ where R: IsLess, Le: NonZero, // ExpandedKeyTableSize - R: Add, - Sum: Mul, + R: Add, + Sum: Mul, ExpandedKeyTableSize: ArrayLength, // Key range B: ArrayLength, @@ -50,8 +50,8 @@ where R: IsLess, Le: NonZero, // ExpandedKeyTableSize - R: Add, - Sum: Mul, + R: Add, + Sum: Mul, ExpandedKeyTableSize: ArrayLength, B: ArrayLength, { @@ -69,8 +69,8 @@ where R: IsLess, Le: NonZero, // ExpandedKeyTableSize - R: Add, - Sum: Mul, + R: Add, + Sum: Mul, ExpandedKeyTableSize: ArrayLength, { } @@ -86,8 +86,8 @@ where R: IsLess, Le: NonZero, // ExpandedKeyTableSize - R: Add, - Sum: Mul, + R: Add, + Sum: Mul, ExpandedKeyTableSize: ArrayLength, { type BlockSize = BlockSize; @@ -104,8 +104,8 @@ where R: IsLess, Le: NonZero, // ExpandedKeyTableSize - R: Add, - Sum: Mul, + R: Add, + Sum: Mul, ExpandedKeyTableSize: ArrayLength, // Key range B: ArrayLength, @@ -133,8 +133,8 @@ where R: IsLess, Le: NonZero, // ExpandedKeyTableSize - R: Add, - Sum: Mul, + R: Add, + Sum: Mul, ExpandedKeyTableSize: ArrayLength, { enc_dec: &'a RC6, @@ -150,8 +150,8 @@ where R: IsLess, Le: NonZero, // ExpandedKeyTableSize - R: Add, - Sum: Mul, + R: Add, + Sum: Mul, ExpandedKeyTableSize: ArrayLength, { type BlockSize = BlockSize; @@ -168,8 +168,8 @@ where R: IsLess, Le: NonZero, // ExpandedKeyTableSize - R: Add, - Sum: Mul, + R: Add, + Sum: Mul, ExpandedKeyTableSize: ArrayLength, { type ParBlocksSize = U1; @@ -186,8 +186,8 @@ where R: IsLess, Le: NonZero, // ExpandedKeyTableSize - R: Add, - Sum: Mul, + R: Add, + Sum: Mul, ExpandedKeyTableSize: ArrayLength, // Key range B: ArrayLength, @@ -217,8 +217,8 @@ where R: IsLess, Le: NonZero, // ExpandedKeyTableSize - R: Add, - Sum: Mul, + R: Add, + Sum: Mul, ExpandedKeyTableSize: ArrayLength, // Key range B: ArrayLength, @@ -246,8 +246,8 @@ where R: IsLess, Le: NonZero, // ExpandedKeyTableSize - R: Add, - Sum: Mul, + R: Add, + Sum: Mul, ExpandedKeyTableSize: ArrayLength, { enc_dec: &'a RC6, @@ -263,8 +263,8 @@ where R: IsLess, Le: NonZero, // ExpandedKeyTableSize - R: Add, - Sum: Mul, + R: Add, + Sum: Mul, ExpandedKeyTableSize: ArrayLength, { type BlockSize = BlockSize; @@ -281,8 +281,8 @@ where R: IsLess, Le: NonZero, // ExpandedKeyTableSize - R: Add, - Sum: Mul, + R: Add, + Sum: Mul, ExpandedKeyTableSize: ArrayLength, { type ParBlocksSize = U1; @@ -299,8 +299,8 @@ where R: IsLess, Le: NonZero, // ExpandedKeyTableSize - R: Add, - Sum: Mul, + R: Add, + Sum: Mul, ExpandedKeyTableSize: ArrayLength, // Key range B: ArrayLength, @@ -330,8 +330,8 @@ where R: IsLess, Le: NonZero, // ExpandedKeyTableSize - R: Add, - Sum: Mul, + R: Add, + Sum: Mul, ExpandedKeyTableSize: ArrayLength, { fn write_alg_name(f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { @@ -347,5 +347,5 @@ where pub type RC6_8_12_4 = RC6; pub type RC6_16_16_8 = RC6; -pub type RC6_32_20_16 = RC6; +pub type RC6_32_20_16 = RC6; pub type RC6_64_24_24 = RC6; diff --git a/rc6/src/core/backend.rs b/rc6/src/core/backend.rs index fcb90b06..49de06f2 100644 --- a/rc6/src/core/backend.rs +++ b/rc6/src/core/backend.rs @@ -7,7 +7,7 @@ use core::{ use cipher::{ generic_array::{sequence::GenericSequence, ArrayLength, GenericArray}, inout::InOut, - typenum::{Diff, IsLess, Le, NonZero, Sum, Unsigned, U1, U256, U4}, + typenum::{Diff, IsLess, Le, NonZero, Sum, Unsigned, U1, U2, U256, U4}, }; use super::{ @@ -20,8 +20,8 @@ where R: Unsigned, R: IsLess, // ExpandedKeyTableSize - R: Add, - Sum: Mul, + R: Add, + Sum: Mul, ExpandedKeyTableSize: ArrayLength, { key_table: ExpandedKeyTable, @@ -39,8 +39,8 @@ where R: IsLess, Le: NonZero, // ExpandedKeyTableSize - R: Add, - Sum: Mul, + R: Add, + Sum: Mul, ExpandedKeyTableSize: ArrayLength, // Key range B: ArrayLength, @@ -133,8 +133,8 @@ where R: IsLess, Le: NonZero, // ExpandedKeyTableSize - R: Add, - Sum: Mul, + R: Add, + Sum: Mul, ExpandedKeyTableSize: ArrayLength, { pub fn encrypt(&self, mut block: InOut<'_, '_, Block>) { diff --git a/rc6/src/core/primitives.rs b/rc6/src/core/primitives.rs index f9556ed3..7cbf74a5 100644 --- a/rc6/src/core/primitives.rs +++ b/rc6/src/core/primitives.rs @@ -1,10 +1,9 @@ -use core::ops::{Add, BitXor}; - use cipher::{ generic_array::{ArrayLength, GenericArray}, typenum::{Diff, Prod, Quot, Sum, U1, U2, U4, U8}, zeroize::DefaultIsZeroes, }; +use core::ops::{Add, BitXor}; pub type BlockSize = Prod<::Bytes, U4>; pub type Block = GenericArray>; @@ -12,12 +11,12 @@ pub type Block = GenericArray>; pub type Key = GenericArray; pub type ExpandedKeyTable = GenericArray>; -pub type ExpandedKeyTableSize = Prod, U4>; +pub type ExpandedKeyTableSize = Prod, U2>; pub type KeyAsWords = GenericArray>; pub type KeyAsWordsSize = Quot::Bytes>, U1>, ::Bytes>; -pub trait Word: Default + Copy + From + Add + DefaultIsZeroes + Default { +pub trait Word: Default + Copy + From + Add + DefaultIsZeroes { type Bytes: ArrayLength; const ZERO: Self; From b62755848c3b3294f8d1f5a894e6bc2e23780d1b Mon Sep 17 00:00:00 2001 From: ashWhiteHat Date: Sun, 9 Jul 2023 10:25:50 +0900 Subject: [PATCH 09/15] rc6: implement encryption algorithm --- rc6/src/core/backend.rs | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/rc6/src/core/backend.rs b/rc6/src/core/backend.rs index 49de06f2..1d34a4e9 100644 --- a/rc6/src/core/backend.rs +++ b/rc6/src/core/backend.rs @@ -142,23 +142,17 @@ where let key = &self.key_table; let log_w = W::from((W::Bytes::USIZE as f64 * 8 as f64).log2() as u8); - // B = B + S[0] - b = b.wrapping_sub(key[0]); - // D = D + S[1] - d = d.wrapping_sub(key[1]); + b = b.wrapping_add(key[0]); + d = d.wrapping_add(key[1]); for i in 1..=R::USIZE { - // 2B + 1 - let b2 = b.wrapping_mul(W::from(2)).wrapping_add(W::from(1)); - // B * 2B << log w - let t = b.wrapping_mul(b2).rotate_left(log_w); - // 2D + 1 - let d2 = d.wrapping_mul(W::from(2)).wrapping_add(W::from(1)); - // D * 2D << log w - let u = d.wrapping_mul(d2).rotate_left(log_w); - // (A xor t << u) + S[2i] + let t = b + .wrapping_mul(b.wrapping_mul(W::from(2)).wrapping_add(W::from(1))) + .rotate_left(log_w); + let u = d + .wrapping_mul(d.wrapping_mul(W::from(2)).wrapping_add(W::from(1))) + .rotate_left(log_w); a = a.bitxor(t).rotate_left(u).wrapping_add(key[2 * i]); - // (C xor u << t) + S[2i + 1] c = c.bitxor(u).rotate_left(t).wrapping_add(key[2 * i + 1]); let (tmp_a, tmp_b, tmp_c, tmp_d) = (b, c, d, a); a = tmp_a; @@ -167,8 +161,8 @@ where d = tmp_d; } - a = a.wrapping_sub(key[2 * R::USIZE + 2]); - c = c.wrapping_sub(key[2 * R::USIZE + 3]); + a = a.wrapping_add(key[2 * R::USIZE + 2]); + c = c.wrapping_add(key[2 * R::USIZE + 3]); Self::block_from_words(a, b, c, d, block.get_out()) } From 115e04866c3f60f372ad95691ee6e6611774c24c Mon Sep 17 00:00:00 2001 From: ashWhiteHat Date: Sun, 9 Jul 2023 10:44:47 +0900 Subject: [PATCH 10/15] rc6: implement decryption algorithm --- rc6/src/core/backend.rs | 23 +++++++++++++++++++---- rc6/tests/mod.rs | 14 +++++++++++++- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/rc6/src/core/backend.rs b/rc6/src/core/backend.rs index 1d34a4e9..dd3785cb 100644 --- a/rc6/src/core/backend.rs +++ b/rc6/src/core/backend.rs @@ -170,14 +170,29 @@ where pub fn decrypt(&self, mut block: InOut<'_, '_, Block>) { let (mut a, mut b) = Self::words_from_block(block.get_in()); let key = &self.key_table; + let log_w = W::from((W::Bytes::USIZE as f64 * 8 as f64).log2() as u8); + + c = c.wrapping_sub(key[2 * R::USIZE + 3]); + a = a.wrapping_sub(key[2 * R::USIZE + 2]); for i in (1..=R::USIZE).rev() { - b = b.wrapping_sub(key[2 * i + 1]).rotate_right(a).bitxor(a); - a = a.wrapping_sub(key[2 * i]).rotate_right(b).bitxor(b); + let (tmp_a, tmp_b, tmp_c, tmp_d) = (d, a, b, c); + a = tmp_a; + b = tmp_b; + c = tmp_c; + d = tmp_d; + let u = d + .wrapping_mul(d.wrapping_mul(W::from(2)).wrapping_add(W::from(1))) + .rotate_left(log_w); + let t = b + .wrapping_mul(b.wrapping_mul(W::from(2)).wrapping_add(W::from(1))) + .rotate_left(log_w); + c = c.wrapping_sub(key[2 * i + 1]).rotate_right(t).bitxor(u); + a = a.wrapping_sub(key[2 * i]).rotate_right(u).bitxor(t); } - b = b.wrapping_sub(key[1]); - a = a.wrapping_sub(key[0]); + d = d.wrapping_sub(key[1]); + b = b.wrapping_sub(key[0]); Self::block_from_words(a, b, block.get_out()) } diff --git a/rc6/tests/mod.rs b/rc6/tests/mod.rs index 9d135591..526e1618 100644 --- a/rc6/tests/mod.rs +++ b/rc6/tests/mod.rs @@ -1,7 +1,7 @@ /// generated using the code in: https://www.ietf.org/archive/id/draft-krovetz-rc6-rc5-vectors-00.txt #[cfg(test)] mod tests { - use cipher::{generic_array::GenericArray, BlockEncrypt, KeyInit}; + use cipher::{generic_array::GenericArray, BlockDecrypt, BlockEncrypt, KeyInit}; use rc6::{RC6_16_16_8, RC6_32_20_16, RC6_64_24_24, RC6_8_12_4}; #[test] @@ -16,6 +16,9 @@ mod tests { rc6.encrypt_block(&mut block); assert_eq!(ct, block[..]); + + rc6.decrypt_block(&mut block); + assert_eq!(pt, block[..]); } #[test] @@ -30,6 +33,9 @@ mod tests { rc6.encrypt_block(&mut block); assert_eq!(ct, block[..]); + + rc6.decrypt_block(&mut block); + assert_eq!(pt, block[..]); } #[test] @@ -53,6 +59,9 @@ mod tests { rc6.encrypt_block(&mut block); assert_eq!(ct, block[..]); + + rc6.decrypt_block(&mut block); + assert_eq!(pt, block[..]); } #[test] @@ -78,5 +87,8 @@ mod tests { rc6.encrypt_block(&mut block); assert_eq!(ct, block[..]); + + rc6.decrypt_block(&mut block); + assert_eq!(pt, block[..]); } } From bc11b3f7ebef9236129e3e865b3c44920146860c Mon Sep 17 00:00:00 2001 From: ashWhiteHat Date: Sun, 9 Jul 2023 11:19:58 +0900 Subject: [PATCH 11/15] rc6: refactor and fix u8 golden rato --- rc6/src/core/backend.rs | 20 ++++++++++---------- rc6/src/core/primitives.rs | 2 +- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/rc6/src/core/backend.rs b/rc6/src/core/backend.rs index dd3785cb..24b105e8 100644 --- a/rc6/src/core/backend.rs +++ b/rc6/src/core/backend.rs @@ -154,11 +154,11 @@ where .rotate_left(log_w); a = a.bitxor(t).rotate_left(u).wrapping_add(key[2 * i]); c = c.bitxor(u).rotate_left(t).wrapping_add(key[2 * i + 1]); - let (tmp_a, tmp_b, tmp_c, tmp_d) = (b, c, d, a); - a = tmp_a; - b = tmp_b; - c = tmp_c; - d = tmp_d; + let tmp = a; + a = b; + b = c; + c = d; + d = tmp; } a = a.wrapping_add(key[2 * R::USIZE + 2]); @@ -176,11 +176,11 @@ where a = a.wrapping_sub(key[2 * R::USIZE + 2]); for i in (1..=R::USIZE).rev() { - let (tmp_a, tmp_b, tmp_c, tmp_d) = (d, a, b, c); - a = tmp_a; - b = tmp_b; - c = tmp_c; - d = tmp_d; + let tmp = d; + d = c; + c = b; + b = a; + a = tmp; let u = d .wrapping_mul(d.wrapping_mul(W::from(2)).wrapping_add(W::from(1))) .rotate_left(log_w); diff --git a/rc6/src/core/primitives.rs b/rc6/src/core/primitives.rs index 7cbf74a5..da8d2be1 100644 --- a/rc6/src/core/primitives.rs +++ b/rc6/src/core/primitives.rs @@ -47,7 +47,7 @@ impl Word for u8 { const EIGHT: Self = 8; const P: Self = 0xb7; - const Q: Self = 0x9e; + const Q: Self = 0x9f; #[inline(always)] fn wrapping_add(self, rhs: Self) -> Self { From e8e7360e64fd71211993c2e1ea04045607cb9659 Mon Sep 17 00:00:00 2001 From: ashWhiteHat Date: Sun, 9 Jul 2023 12:03:41 +0900 Subject: [PATCH 12/15] rc6: fix clippy and add github actions --- .github/workflows/rc6.yml | 59 +++++++++++++++++++++++++++++++++++++++ rc6/src/core/backend.rs | 4 +-- 2 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/rc6.yml diff --git a/.github/workflows/rc6.yml b/.github/workflows/rc6.yml new file mode 100644 index 00000000..9e664306 --- /dev/null +++ b/.github/workflows/rc6.yml @@ -0,0 +1,59 @@ +name: rc6 +on: + pull_request: + paths: + - "rc6/**" + - "Cargo.*" + push: + branches: master + +defaults: + run: + working-directory: rc6 + +env: + CARGO_INCREMENTAL: 0 + RUSTFLAGS: "-Dwarnings" + +jobs: + build: + runs-on: ubuntu-latest + strategy: + matrix: + rust: + - 1.56.0 # MSRV + - stable + target: + - thumbv7em-none-eabi + - wasm32-unknown-unknown + steps: + - uses: actions/checkout@v3 + - uses: RustCrypto/actions/cargo-cache@master + - uses: dtolnay/rust-toolchain@master + with: + toolchain: ${{ matrix.rust }} + targets: ${{ matrix.target }} + - run: cargo build --no-default-features --release --target ${{ matrix.target }} + + minimal-versions: + uses: RustCrypto/actions/.github/workflows/minimal-versions.yml@master + with: + working-directory: ${{ github.workflow }} + + test: + runs-on: ubuntu-latest + strategy: + matrix: + rust: + - 1.56.0 # MSRV + - stable + steps: + - uses: actions/checkout@v3 + - uses: RustCrypto/actions/cargo-cache@master + - uses: dtolnay/rust-toolchain@master + with: + toolchain: ${{ matrix.rust }} + - run: cargo check --all-features + - run: cargo test --no-default-features + - run: cargo test + - run: cargo test --all-features diff --git a/rc6/src/core/backend.rs b/rc6/src/core/backend.rs index 24b105e8..e1a345a2 100644 --- a/rc6/src/core/backend.rs +++ b/rc6/src/core/backend.rs @@ -140,7 +140,7 @@ where pub fn encrypt(&self, mut block: InOut<'_, '_, Block>) { let (mut a, mut b) = Self::words_from_block(block.get_in()); let key = &self.key_table; - let log_w = W::from((W::Bytes::USIZE as f64 * 8 as f64).log2() as u8); + let log_w = W::from((usize::BITS - 1 - (W::Bytes::USIZE * 8).leading_zeros()) as u8); b = b.wrapping_add(key[0]); d = d.wrapping_add(key[1]); @@ -170,7 +170,7 @@ where pub fn decrypt(&self, mut block: InOut<'_, '_, Block>) { let (mut a, mut b) = Self::words_from_block(block.get_in()); let key = &self.key_table; - let log_w = W::from((W::Bytes::USIZE as f64 * 8 as f64).log2() as u8); + let log_w = W::from((usize::BITS - 1 - (W::Bytes::USIZE * 8).leading_zeros()) as u8); c = c.wrapping_sub(key[2 * R::USIZE + 3]); a = a.wrapping_sub(key[2 * R::USIZE + 2]); From 342a69e27f977164e62858292972a7f855902f62 Mon Sep 17 00:00:00 2001 From: ashWhiteHat Date: Wed, 31 Jul 2024 17:17:33 +0900 Subject: [PATCH 13/15] rc6: fix tests --- .github/workflows/rc6.yml | 4 +- Cargo.lock | 114 ++++++++++++++++++++++++++++++++++++++ rc6/Cargo.toml | 1 + rc6/src/core/backend.rs | 68 +++++++++++++++++++---- 4 files changed, 174 insertions(+), 13 deletions(-) diff --git a/.github/workflows/rc6.yml b/.github/workflows/rc6.yml index 9e664306..fbdf9035 100644 --- a/.github/workflows/rc6.yml +++ b/.github/workflows/rc6.yml @@ -21,7 +21,7 @@ jobs: strategy: matrix: rust: - - 1.56.0 # MSRV + - 1.65.0 # MSRVs - stable target: - thumbv7em-none-eabi @@ -45,7 +45,7 @@ jobs: strategy: matrix: rust: - - 1.56.0 # MSRV + - 1.65.0 # MSRVs - stable steps: - uses: actions/checkout@v3 diff --git a/Cargo.lock b/Cargo.lock index b78c8f58..235a7f0d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -148,6 +148,17 @@ dependencies = [ "version_check", ] +[[package]] +name = "getrandom" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + [[package]] name = "gift-cipher" version = "0.0.0" @@ -217,6 +228,64 @@ dependencies = [ "hex-literal", ] +[[package]] +name = "ppv-lite86" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2288c0e17cc8d342c712bb43a257a80ebffce59cdb33d5000d8348f3ec02528b" +dependencies = [ + "zerocopy", + "zerocopy-derive", +] + +[[package]] +name = "proc-macro2" +version = "1.0.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + [[package]] name = "rc2" version = "0.9.0-pre" @@ -236,6 +305,7 @@ name = "rc6" version = "0.1.0" dependencies = [ "cipher 0.4.4", + "rand", ] [[package]] @@ -262,6 +332,17 @@ dependencies = [ "hex-literal", ] +[[package]] +name = "syn" +version = "2.0.72" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc4b9b9bf2add8093d3f2c0204471e951b2285580335de42f9d2534f3ae7a8af" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + [[package]] name = "threefish" version = "0.6.0-pre" @@ -285,12 +366,24 @@ version = "1.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" + [[package]] name = "version_check" version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + [[package]] name = "xtea" version = "0.0.0" @@ -298,6 +391,27 @@ dependencies = [ "cipher 0.5.0-pre.6", ] +[[package]] +name = "zerocopy" +version = "0.7.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0" +dependencies = [ + "byteorder", + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.7.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "zeroize" version = "1.8.1" diff --git a/rc6/Cargo.toml b/rc6/Cargo.toml index 8879ce33..17b91e8e 100644 --- a/rc6/Cargo.toml +++ b/rc6/Cargo.toml @@ -15,6 +15,7 @@ cipher = { version = "0.4.3", features = ["zeroize"] } [dev-dependencies] cipher = { version = "0.4.3", features = ["dev"] } +rand = { version = "0.8.5" } [features] zeroize = [] diff --git a/rc6/src/core/backend.rs b/rc6/src/core/backend.rs index e1a345a2..675d9d86 100644 --- a/rc6/src/core/backend.rs +++ b/rc6/src/core/backend.rs @@ -138,7 +138,7 @@ where ExpandedKeyTableSize: ArrayLength, { pub fn encrypt(&self, mut block: InOut<'_, '_, Block>) { - let (mut a, mut b) = Self::words_from_block(block.get_in()); + let (mut a, mut b, mut c, mut d) = Self::words_from_block(block.get_in()); let key = &self.key_table; let log_w = W::from((usize::BITS - 1 - (W::Bytes::USIZE * 8).leading_zeros()) as u8); @@ -168,7 +168,7 @@ where } pub fn decrypt(&self, mut block: InOut<'_, '_, Block>) { - let (mut a, mut b) = Self::words_from_block(block.get_in()); + let (mut a, mut b, mut c, mut d) = Self::words_from_block(block.get_in()); let key = &self.key_table; let log_w = W::from((usize::BITS - 1 - (W::Bytes::USIZE * 8).leading_zeros()) as u8); @@ -194,21 +194,67 @@ where d = d.wrapping_sub(key[1]); b = b.wrapping_sub(key[0]); - Self::block_from_words(a, b, block.get_out()) + Self::block_from_words(a, b, c, d, block.get_out()) } - fn words_from_block(block: &Block) -> (W, W) { - // Block size is 2 * word::BYTES so the unwrap is safe + fn words_from_block(block: &Block) -> (W, W, W, W) { + // Block size is 4 * word::BYTES so the unwrap is safe let a = W::from_le_bytes(block[..W::Bytes::USIZE].try_into().unwrap()); - let b = W::from_le_bytes(block[W::Bytes::USIZE..].try_into().unwrap()); + let b = W::from_le_bytes( + block[W::Bytes::USIZE..W::Bytes::USIZE * 2] + .try_into() + .unwrap(), + ); + let c = W::from_le_bytes( + block[W::Bytes::USIZE * 2..W::Bytes::USIZE * 3] + .try_into() + .unwrap(), + ); + let d = W::from_le_bytes( + block[W::Bytes::USIZE * 3..W::Bytes::USIZE * 4] + .try_into() + .unwrap(), + ); + + (a, b, c, d) + } + + fn block_from_words(a: W, b: W, c: W, d: W, out_block: &mut Block) { + let (left, right) = out_block.split_at_mut(W::Bytes::USIZE * 2); + let (l_l, l_h) = left.split_at_mut(W::Bytes::USIZE); + let (r_l, r_h) = right.split_at_mut(W::Bytes::USIZE); - (a, b) + l_l.copy_from_slice(&a.to_le_bytes()); + l_h.copy_from_slice(&b.to_le_bytes()); + r_l.copy_from_slice(&c.to_le_bytes()); + r_h.copy_from_slice(&d.to_le_bytes()); } +} - fn block_from_words(a: W, b: W, out_block: &mut Block) { - let (left, right) = out_block.split_at_mut(W::Bytes::USIZE); +#[cfg(test)] +mod tests { + use crate::block_cipher::{RC6_16_16_8, RC6_32_20_16, RC6_64_24_24, RC6_8_12_4}; + use crate::core::backend::GenericArray; + use rand::{thread_rng, Rng}; + + #[macro_export] + macro_rules! words_block_conv { + ($rc_tyoe:ident, $key_size:expr) => { + let mut pt = [0u8; $key_size]; + thread_rng().fill(&mut pt[..]); + let block = GenericArray::clone_from_slice(&pt); + let mut after_block = block.clone(); + let (a, b, c, d) = $rc_tyoe::words_from_block(&block); + $rc_tyoe::block_from_words(a, b, c, d, &mut after_block); + assert_eq!(block, after_block); + }; + } - left.copy_from_slice(&a.to_le_bytes()); - right.copy_from_slice(&b.to_le_bytes()); + #[test] + fn words_block_test() { + words_block_conv!(RC6_16_16_8, 8); + words_block_conv!(RC6_32_20_16, 16); + words_block_conv!(RC6_64_24_24, 32); + words_block_conv!(RC6_8_12_4, 4); } } From e0dfb5691e059fd4740040ef11fa58f0d0e1e5aa Mon Sep 17 00:00:00 2001 From: ashWhiteHat Date: Wed, 31 Jul 2024 17:19:49 +0900 Subject: [PATCH 14/15] rc6: dump cipher version --- Cargo.lock | 93 +++++++++++++------------------------------------- rc6/Cargo.toml | 4 +-- 2 files changed, 25 insertions(+), 72 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 235a7f0d..990445d6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7,7 +7,7 @@ name = "aes" version = "0.9.0-pre.1" dependencies = [ "cfg-if", - "cipher 0.5.0-pre.6", + "cipher", "cpufeatures", "hex-literal", "zeroize", @@ -17,7 +17,7 @@ dependencies = [ name = "aria" version = "0.2.0-pre" dependencies = [ - "cipher 0.5.0-pre.6", + "cipher", "hex-literal", ] @@ -25,7 +25,7 @@ dependencies = [ name = "belt-block" version = "0.2.0-pre.1" dependencies = [ - "cipher 0.5.0-pre.6", + "cipher", "hex-literal", ] @@ -40,7 +40,7 @@ name = "blowfish" version = "0.10.0-pre.1" dependencies = [ "byteorder", - "cipher 0.5.0-pre.6", + "cipher", ] [[package]] @@ -54,14 +54,14 @@ name = "camellia" version = "0.2.0-pre" dependencies = [ "byteorder", - "cipher 0.5.0-pre.6", + "cipher", ] [[package]] name = "cast5" version = "0.12.0-pre" dependencies = [ - "cipher 0.5.0-pre.6", + "cipher", "hex-literal", ] @@ -69,7 +69,7 @@ dependencies = [ name = "cast6" version = "0.2.0-pre" dependencies = [ - "cipher 0.5.0-pre.6", + "cipher", "hex-literal", ] @@ -79,18 +79,6 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" -[[package]] -name = "cipher" -version = "0.4.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "773f3b9af64447d2ce9850330c473515014aa235e6a783b02db81ff39e4a3dad" -dependencies = [ - "blobby", - "crypto-common 0.1.6", - "inout 0.1.3", - "zeroize", -] - [[package]] name = "cipher" version = "0.5.0-pre.6" @@ -98,8 +86,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c71c893d5a1e8257048dbb29954d2e1f85f091a150304f1defe4ca2806da5d3f" dependencies = [ "blobby", - "crypto-common 0.2.0-rc.0", - "inout 0.2.0-rc.0", + "crypto-common", + "inout", "zeroize", ] @@ -112,16 +100,6 @@ dependencies = [ "libc", ] -[[package]] -name = "crypto-common" -version = "0.1.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" -dependencies = [ - "generic-array", - "typenum", -] - [[package]] name = "crypto-common" version = "0.2.0-rc.0" @@ -135,17 +113,7 @@ dependencies = [ name = "des" version = "0.9.0-pre.1" dependencies = [ - "cipher 0.5.0-pre.6", -] - -[[package]] -name = "generic-array" -version = "0.14.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" -dependencies = [ - "typenum", - "version_check", + "cipher", ] [[package]] @@ -163,7 +131,7 @@ dependencies = [ name = "gift-cipher" version = "0.0.0" dependencies = [ - "cipher 0.5.0-pre.6", + "cipher", ] [[package]] @@ -185,16 +153,7 @@ dependencies = [ name = "idea" version = "0.6.0-pre" dependencies = [ - "cipher 0.5.0-pre.6", -] - -[[package]] -name = "inout" -version = "0.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a0c10553d664a4d0bcff9f4215d0aac67a639cc68ef660840afe309b807bc9f5" -dependencies = [ - "generic-array", + "cipher", ] [[package]] @@ -210,7 +169,7 @@ dependencies = [ name = "kuznyechik" version = "0.9.0-pre.1" dependencies = [ - "cipher 0.5.0-pre.6", + "cipher", "hex-literal", ] @@ -224,7 +183,7 @@ checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" name = "magma" version = "0.10.0-pre.1" dependencies = [ - "cipher 0.5.0-pre.6", + "cipher", "hex-literal", ] @@ -290,21 +249,21 @@ dependencies = [ name = "rc2" version = "0.9.0-pre" dependencies = [ - "cipher 0.5.0-pre.6", + "cipher", ] [[package]] name = "rc5" version = "0.1.0-pre" dependencies = [ - "cipher 0.5.0-pre.6", + "cipher", ] [[package]] name = "rc6" version = "0.1.0" dependencies = [ - "cipher 0.4.4", + "cipher", "rand", ] @@ -313,14 +272,14 @@ name = "serpent" version = "0.6.0-pre" dependencies = [ "byteorder", - "cipher 0.5.0-pre.6", + "cipher", ] [[package]] name = "sm4" version = "0.6.0-pre" dependencies = [ - "cipher 0.5.0-pre.6", + "cipher", "hex-literal", ] @@ -328,7 +287,7 @@ dependencies = [ name = "speck-cipher" version = "0.0.0" dependencies = [ - "cipher 0.5.0-pre.6", + "cipher", "hex-literal", ] @@ -347,7 +306,7 @@ dependencies = [ name = "threefish" version = "0.6.0-pre" dependencies = [ - "cipher 0.5.0-pre.6", + "cipher", "hex-literal", "zeroize", ] @@ -356,7 +315,7 @@ dependencies = [ name = "twofish" version = "0.8.0-pre" dependencies = [ - "cipher 0.5.0-pre.6", + "cipher", "hex-literal", ] @@ -372,12 +331,6 @@ version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" -[[package]] -name = "version_check" -version = "0.9.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" - [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" @@ -388,7 +341,7 @@ checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" name = "xtea" version = "0.0.0" dependencies = [ - "cipher 0.5.0-pre.6", + "cipher", ] [[package]] diff --git a/rc6/Cargo.toml b/rc6/Cargo.toml index 17b91e8e..15678496 100644 --- a/rc6/Cargo.toml +++ b/rc6/Cargo.toml @@ -11,10 +11,10 @@ keywords = ["crypto", "rc6", "block-cipher"] categories = ["cryptography"] [dependencies] -cipher = { version = "0.4.3", features = ["zeroize"] } +cipher = { version = "0.5.0-pre.6", features = ["zeroize"] } [dev-dependencies] -cipher = { version = "0.4.3", features = ["dev"] } +cipher = { version = "0.5.0-pre.6", features = ["dev"] } rand = { version = "0.8.5" } [features] From 5572b524065bdd68009170a731a8fdc10781a5ed Mon Sep 17 00:00:00 2001 From: ashWhiteHat Date: Sun, 4 Aug 2024 11:34:40 +0900 Subject: [PATCH 15/15] rc6: fix test --- Cargo.lock | 114 ------------------------------------- rc6/Cargo.toml | 1 - rc6/src/block_cipher.rs | 93 +++++++++++++++--------------- rc6/src/core/backend.rs | 50 ++++------------ rc6/src/core/primitives.rs | 32 +++++------ rc6/src/lib.rs | 1 + rc6/tests/mod.rs | 23 ++++---- 7 files changed, 88 insertions(+), 226 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 990445d6..7fc01f51 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -116,17 +116,6 @@ dependencies = [ "cipher", ] -[[package]] -name = "getrandom" -version = "0.2.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" -dependencies = [ - "cfg-if", - "libc", - "wasi", -] - [[package]] name = "gift-cipher" version = "0.0.0" @@ -187,64 +176,6 @@ dependencies = [ "hex-literal", ] -[[package]] -name = "ppv-lite86" -version = "0.2.19" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2288c0e17cc8d342c712bb43a257a80ebffce59cdb33d5000d8348f3ec02528b" -dependencies = [ - "zerocopy", - "zerocopy-derive", -] - -[[package]] -name = "proc-macro2" -version = "1.0.86" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77" -dependencies = [ - "unicode-ident", -] - -[[package]] -name = "quote" -version = "1.0.36" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" -dependencies = [ - "proc-macro2", -] - -[[package]] -name = "rand" -version = "0.8.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" -dependencies = [ - "libc", - "rand_chacha", - "rand_core", -] - -[[package]] -name = "rand_chacha" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" -dependencies = [ - "ppv-lite86", - "rand_core", -] - -[[package]] -name = "rand_core" -version = "0.6.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" -dependencies = [ - "getrandom", -] - [[package]] name = "rc2" version = "0.9.0-pre" @@ -264,7 +195,6 @@ name = "rc6" version = "0.1.0" dependencies = [ "cipher", - "rand", ] [[package]] @@ -291,17 +221,6 @@ dependencies = [ "hex-literal", ] -[[package]] -name = "syn" -version = "2.0.72" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dc4b9b9bf2add8093d3f2c0204471e951b2285580335de42f9d2534f3ae7a8af" -dependencies = [ - "proc-macro2", - "quote", - "unicode-ident", -] - [[package]] name = "threefish" version = "0.6.0-pre" @@ -325,18 +244,6 @@ version = "1.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" -[[package]] -name = "unicode-ident" -version = "1.0.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" - -[[package]] -name = "wasi" -version = "0.11.0+wasi-snapshot-preview1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" - [[package]] name = "xtea" version = "0.0.0" @@ -344,27 +251,6 @@ dependencies = [ "cipher", ] -[[package]] -name = "zerocopy" -version = "0.7.35" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0" -dependencies = [ - "byteorder", - "zerocopy-derive", -] - -[[package]] -name = "zerocopy-derive" -version = "0.7.35" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - [[package]] name = "zeroize" version = "1.8.1" diff --git a/rc6/Cargo.toml b/rc6/Cargo.toml index 15678496..1e6a880d 100644 --- a/rc6/Cargo.toml +++ b/rc6/Cargo.toml @@ -15,7 +15,6 @@ cipher = { version = "0.5.0-pre.6", features = ["zeroize"] } [dev-dependencies] cipher = { version = "0.5.0-pre.6", features = ["dev"] } -rand = { version = "0.8.5" } [features] zeroize = [] diff --git a/rc6/src/block_cipher.rs b/rc6/src/block_cipher.rs index 57e4e816..e5ea04a3 100644 --- a/rc6/src/block_cipher.rs +++ b/rc6/src/block_cipher.rs @@ -1,11 +1,12 @@ use core::ops::{Add, Div, Mul, Sub}; use cipher::{ - generic_array::ArrayLength, + array::ArraySize, + crypto_common::BlockSizes, inout::InOut, typenum::{Diff, IsLess, Le, NonZero, Sum, Unsigned, U1, U12, U16, U2, U20, U24, U256, U4, U8}, - AlgorithmName, Block, BlockBackend, BlockCipher, BlockDecrypt, BlockEncrypt, BlockSizeUser, - KeyInit, KeySizeUser, ParBlocksSizeUser, + AlgorithmName, Block, BlockBackend, BlockCipher, BlockCipherDecrypt, BlockCipherEncrypt, + BlockSizeUser, KeyInit, KeySizeUser, ParBlocksSizeUser, }; use crate::core::{BlockSize, ExpandedKeyTableSize, KeyAsWordsSize, Word, RC6}; @@ -15,7 +16,7 @@ where W: Word, // Block size W::Bytes: Mul, - BlockSize: ArrayLength, + BlockSize: BlockSizes, // Rounds range R: Unsigned, R: IsLess, @@ -23,16 +24,16 @@ where // ExpandedKeyTableSize R: Add, Sum: Mul, - ExpandedKeyTableSize: ArrayLength, + ExpandedKeyTableSize: ArraySize, // Key range - B: ArrayLength, + B: ArraySize, B: IsLess, Le: NonZero, // KeyAsWordsSize B: Add, Sum: Sub, Diff, U1>: Div, - KeyAsWordsSize: ArrayLength, + KeyAsWordsSize: ArraySize, { fn new(key: &cipher::Key) -> Self { Self::new(key) @@ -44,7 +45,7 @@ where W: Word, // Block size W::Bytes: Mul, - BlockSize: ArrayLength, + BlockSize: BlockSizes, // Rounds range R: Unsigned, R: IsLess, @@ -52,8 +53,8 @@ where // ExpandedKeyTableSize R: Add, Sum: Mul, - ExpandedKeyTableSize: ArrayLength, - B: ArrayLength, + ExpandedKeyTableSize: ArraySize, + B: ArraySize, { type KeySize = B; } @@ -63,7 +64,7 @@ where W: Word, // Block size W::Bytes: Mul, - BlockSize: ArrayLength, + BlockSize: BlockSizes, // Rounds range R: Unsigned, R: IsLess, @@ -71,7 +72,7 @@ where // ExpandedKeyTableSize R: Add, Sum: Mul, - ExpandedKeyTableSize: ArrayLength, + ExpandedKeyTableSize: ArraySize, { } @@ -80,7 +81,7 @@ where W: Word, // Block size W::Bytes: Mul, - BlockSize: ArrayLength, + BlockSize: BlockSizes, // Rounds range R: Unsigned, R: IsLess, @@ -88,17 +89,17 @@ where // ExpandedKeyTableSize R: Add, Sum: Mul, - ExpandedKeyTableSize: ArrayLength, + ExpandedKeyTableSize: ArraySize, { type BlockSize = BlockSize; } -impl BlockEncrypt for RC6 +impl BlockCipherEncrypt for RC6 where W: Word, // Block size W::Bytes: Mul, - BlockSize: ArrayLength, + BlockSize: BlockSizes, // Rounds range R: Unsigned, R: IsLess, @@ -106,16 +107,16 @@ where // ExpandedKeyTableSize R: Add, Sum: Mul, - ExpandedKeyTableSize: ArrayLength, + ExpandedKeyTableSize: ArraySize, // Key range - B: ArrayLength, + B: BlockSizes, B: IsLess, Le: NonZero, // KeyAsWordsSize B: Add, Sum: Sub, Diff, U1>: Div, - KeyAsWordsSize: ArrayLength, + KeyAsWordsSize: ArraySize, { fn encrypt_with_backend(&self, f: impl cipher::BlockClosure) { f.call(&mut RC6EncryptBackend { enc_dec: self }) @@ -127,7 +128,7 @@ where W: Word, // Block size W::Bytes: Mul, - BlockSize: ArrayLength, + BlockSize: BlockSizes, // Rounds range R: Unsigned, R: IsLess, @@ -135,7 +136,7 @@ where // ExpandedKeyTableSize R: Add, Sum: Mul, - ExpandedKeyTableSize: ArrayLength, + ExpandedKeyTableSize: ArraySize, { enc_dec: &'a RC6, } @@ -144,7 +145,7 @@ where W: Word, // Block size W::Bytes: Mul, - BlockSize: ArrayLength, + BlockSize: BlockSizes, // Rounds range R: Unsigned, R: IsLess, @@ -152,7 +153,7 @@ where // ExpandedKeyTableSize R: Add, Sum: Mul, - ExpandedKeyTableSize: ArrayLength, + ExpandedKeyTableSize: ArraySize, { type BlockSize = BlockSize; } @@ -162,7 +163,7 @@ where W: Word, // Block size W::Bytes: Mul, - BlockSize: ArrayLength, + BlockSize: BlockSizes, // Rounds range R: Unsigned, R: IsLess, @@ -170,7 +171,7 @@ where // ExpandedKeyTableSize R: Add, Sum: Mul, - ExpandedKeyTableSize: ArrayLength, + ExpandedKeyTableSize: ArraySize, { type ParBlocksSize = U1; } @@ -180,7 +181,7 @@ where W: Word, // Block size W::Bytes: Mul, - BlockSize: ArrayLength, + BlockSize: BlockSizes, // Rounds range R: Unsigned, R: IsLess, @@ -188,16 +189,16 @@ where // ExpandedKeyTableSize R: Add, Sum: Mul, - ExpandedKeyTableSize: ArrayLength, + ExpandedKeyTableSize: ArraySize, // Key range - B: ArrayLength, + B: BlockSizes, B: IsLess, Le: NonZero, // KeyAsWordsSize B: Add, Sum: Sub, Diff, U1>: Div, - KeyAsWordsSize: ArrayLength, + KeyAsWordsSize: ArraySize, { #[inline(always)] fn proc_block(&mut self, block: InOut<'_, '_, Block>) { @@ -206,12 +207,12 @@ where } } -impl BlockDecrypt for RC6 +impl BlockCipherDecrypt for RC6 where W: Word, // Block size W::Bytes: Mul, - BlockSize: ArrayLength, + BlockSize: BlockSizes, // Rounds range R: Unsigned, R: IsLess, @@ -219,16 +220,16 @@ where // ExpandedKeyTableSize R: Add, Sum: Mul, - ExpandedKeyTableSize: ArrayLength, + ExpandedKeyTableSize: ArraySize, // Key range - B: ArrayLength, + B: BlockSizes, B: IsLess, Le: NonZero, // KeyAsWordsSize B: Add, Sum: Sub, Diff, U1>: Div, - KeyAsWordsSize: ArrayLength, + KeyAsWordsSize: ArraySize, { fn decrypt_with_backend(&self, f: impl cipher::BlockClosure) { f.call(&mut RC6DecryptBackend { enc_dec: self }) @@ -240,7 +241,7 @@ where W: Word, // Block size W::Bytes: Mul, - BlockSize: ArrayLength, + BlockSize: BlockSizes, // Rounds range R: Unsigned, R: IsLess, @@ -248,7 +249,7 @@ where // ExpandedKeyTableSize R: Add, Sum: Mul, - ExpandedKeyTableSize: ArrayLength, + ExpandedKeyTableSize: ArraySize, { enc_dec: &'a RC6, } @@ -257,7 +258,7 @@ where W: Word, // Block size W::Bytes: Mul, - BlockSize: ArrayLength, + BlockSize: BlockSizes, // Rounds range R: Unsigned, R: IsLess, @@ -265,7 +266,7 @@ where // ExpandedKeyTableSize R: Add, Sum: Mul, - ExpandedKeyTableSize: ArrayLength, + ExpandedKeyTableSize: ArraySize, { type BlockSize = BlockSize; } @@ -275,7 +276,7 @@ where W: Word, // Block size W::Bytes: Mul, - BlockSize: ArrayLength, + BlockSize: BlockSizes, // Rounds range R: Unsigned, R: IsLess, @@ -283,7 +284,7 @@ where // ExpandedKeyTableSize R: Add, Sum: Mul, - ExpandedKeyTableSize: ArrayLength, + ExpandedKeyTableSize: ArraySize, { type ParBlocksSize = U1; } @@ -293,7 +294,7 @@ where W: Word, // Block size W::Bytes: Mul, - BlockSize: ArrayLength, + BlockSize: BlockSizes, // Rounds range R: Unsigned, R: IsLess, @@ -301,16 +302,16 @@ where // ExpandedKeyTableSize R: Add, Sum: Mul, - ExpandedKeyTableSize: ArrayLength, + ExpandedKeyTableSize: ArraySize, // Key range - B: ArrayLength, + B: ArraySize, B: IsLess, Le: NonZero, // KeyAsWordsSize B: Add, Sum: Sub, Diff, U1>: Div, - KeyAsWordsSize: ArrayLength, + KeyAsWordsSize: ArraySize, { #[inline(always)] fn proc_block(&mut self, block: InOut<'_, '_, Block>) { @@ -324,7 +325,7 @@ where W: Word, // Block size W::Bytes: Mul, - BlockSize: ArrayLength, + BlockSize: BlockSizes, // Rounds range R: Unsigned, R: IsLess, @@ -332,7 +333,7 @@ where // ExpandedKeyTableSize R: Add, Sum: Mul, - ExpandedKeyTableSize: ArrayLength, + ExpandedKeyTableSize: ArraySize, { fn write_alg_name(f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { write!( diff --git a/rc6/src/core/backend.rs b/rc6/src/core/backend.rs index 675d9d86..7bfbc785 100644 --- a/rc6/src/core/backend.rs +++ b/rc6/src/core/backend.rs @@ -5,7 +5,8 @@ use core::{ }; use cipher::{ - generic_array::{sequence::GenericSequence, ArrayLength, GenericArray}, + array::{Array, ArraySize}, + crypto_common::BlockSizes, inout::InOut, typenum::{Diff, IsLess, Le, NonZero, Sum, Unsigned, U1, U2, U256, U4}, }; @@ -22,7 +23,7 @@ where // ExpandedKeyTableSize R: Add, Sum: Mul, - ExpandedKeyTableSize: ArrayLength, + ExpandedKeyTableSize: ArraySize, { key_table: ExpandedKeyTable, _key_size: PhantomData, @@ -33,7 +34,7 @@ where W: Word, // Block size W::Bytes: Mul, - BlockSize: ArrayLength, + BlockSize: BlockSizes, // Rounds range R: Unsigned, R: IsLess, @@ -41,16 +42,16 @@ where // ExpandedKeyTableSize R: Add, Sum: Mul, - ExpandedKeyTableSize: ArrayLength, + ExpandedKeyTableSize: ArraySize, // Key range - B: ArrayLength, + B: ArraySize, B: IsLess, Le: NonZero, // KeyAsWordsSize B: Add, Sum: Sub, Diff, U1>: Div, - KeyAsWordsSize: ArrayLength, + KeyAsWordsSize: ArraySize, { pub fn new(key: &Key) -> RC6 { Self { @@ -68,7 +69,7 @@ where fn key_into_words(key: &Key) -> KeyAsWords { // can be uninitialized - let mut key_as_words: GenericArray> = GenericArray::default(); + let mut key_as_words: Array> = Array::default(); for i in (0..B::USIZE).rev() { key_as_words[i / W::Bytes::USIZE] = @@ -81,8 +82,7 @@ where fn initialize_expanded_key_table() -> ExpandedKeyTable { // must be zero initialized - let mut expanded_key_table: GenericArray> = - GenericArray::generate(|_| W::ZERO); + let mut expanded_key_table: Array> = Array::from_fn(|_| W::ZERO); expanded_key_table[0] = W::P; for i in 1..expanded_key_table.len() { @@ -127,7 +127,7 @@ where W: Word, // Block size W::Bytes: Mul, - BlockSize: ArrayLength, + BlockSize: BlockSizes, // Rounds range R: Unsigned, R: IsLess, @@ -135,7 +135,7 @@ where // ExpandedKeyTableSize R: Add, Sum: Mul, - ExpandedKeyTableSize: ArrayLength, + ExpandedKeyTableSize: ArraySize, { pub fn encrypt(&self, mut block: InOut<'_, '_, Block>) { let (mut a, mut b, mut c, mut d) = Self::words_from_block(block.get_in()); @@ -230,31 +230,3 @@ where r_h.copy_from_slice(&d.to_le_bytes()); } } - -#[cfg(test)] -mod tests { - use crate::block_cipher::{RC6_16_16_8, RC6_32_20_16, RC6_64_24_24, RC6_8_12_4}; - use crate::core::backend::GenericArray; - use rand::{thread_rng, Rng}; - - #[macro_export] - macro_rules! words_block_conv { - ($rc_tyoe:ident, $key_size:expr) => { - let mut pt = [0u8; $key_size]; - thread_rng().fill(&mut pt[..]); - let block = GenericArray::clone_from_slice(&pt); - let mut after_block = block.clone(); - let (a, b, c, d) = $rc_tyoe::words_from_block(&block); - $rc_tyoe::block_from_words(a, b, c, d, &mut after_block); - assert_eq!(block, after_block); - }; - } - - #[test] - fn words_block_test() { - words_block_conv!(RC6_16_16_8, 8); - words_block_conv!(RC6_32_20_16, 16); - words_block_conv!(RC6_64_24_24, 32); - words_block_conv!(RC6_8_12_4, 4); - } -} diff --git a/rc6/src/core/primitives.rs b/rc6/src/core/primitives.rs index da8d2be1..99044949 100644 --- a/rc6/src/core/primitives.rs +++ b/rc6/src/core/primitives.rs @@ -1,23 +1,23 @@ use cipher::{ - generic_array::{ArrayLength, GenericArray}, + array::{Array, ArraySize}, typenum::{Diff, Prod, Quot, Sum, U1, U2, U4, U8}, zeroize::DefaultIsZeroes, }; use core::ops::{Add, BitXor}; pub type BlockSize = Prod<::Bytes, U4>; -pub type Block = GenericArray>; +pub type Block = Array>; -pub type Key = GenericArray; +pub type Key = Array; -pub type ExpandedKeyTable = GenericArray>; +pub type ExpandedKeyTable = Array>; pub type ExpandedKeyTableSize = Prod, U2>; -pub type KeyAsWords = GenericArray>; +pub type KeyAsWords = Array>; pub type KeyAsWordsSize = Quot::Bytes>, U1>, ::Bytes>; pub trait Word: Default + Copy + From + Add + DefaultIsZeroes { - type Bytes: ArrayLength; + type Bytes: ArraySize; const ZERO: Self; const THREE: Self; @@ -33,8 +33,8 @@ pub trait Word: Default + Copy + From + Add + DefaultIsZeroes fn rotate_left(self, n: Self) -> Self; fn rotate_right(self, n: Self) -> Self; - fn from_le_bytes(bytes: &GenericArray) -> Self; - fn to_le_bytes(self) -> GenericArray; + fn from_le_bytes(bytes: &Array) -> Self; + fn to_le_bytes(self) -> Array; fn bitxor(self, other: Self) -> Self; } @@ -75,12 +75,12 @@ impl Word for u8 { } #[inline(always)] - fn from_le_bytes(bytes: &GenericArray) -> Self { + fn from_le_bytes(bytes: &Array) -> Self { u8::from_le_bytes(bytes.as_slice().try_into().unwrap()) } #[inline(always)] - fn to_le_bytes(self) -> GenericArray { + fn to_le_bytes(self) -> Array { u8::to_le_bytes(self).into() } @@ -126,12 +126,12 @@ impl Word for u16 { } #[inline(always)] - fn from_le_bytes(bytes: &GenericArray) -> Self { + fn from_le_bytes(bytes: &Array) -> Self { u16::from_le_bytes(bytes.as_slice().try_into().unwrap()) } #[inline(always)] - fn to_le_bytes(self) -> GenericArray { + fn to_le_bytes(self) -> Array { u16::to_le_bytes(self).into() } @@ -177,12 +177,12 @@ impl Word for u32 { } #[inline(always)] - fn from_le_bytes(bytes: &GenericArray) -> Self { + fn from_le_bytes(bytes: &Array) -> Self { u32::from_le_bytes(bytes.as_slice().try_into().unwrap()) } #[inline(always)] - fn to_le_bytes(self) -> GenericArray { + fn to_le_bytes(self) -> Array { u32::to_le_bytes(self).into() } @@ -230,12 +230,12 @@ impl Word for u64 { } #[inline(always)] - fn from_le_bytes(bytes: &GenericArray) -> Self { + fn from_le_bytes(bytes: &Array) -> Self { u64::from_le_bytes(bytes.as_slice().try_into().unwrap()) } #[inline(always)] - fn to_le_bytes(self) -> GenericArray { + fn to_le_bytes(self) -> Array { u64::to_le_bytes(self).into() } diff --git a/rc6/src/lib.rs b/rc6/src/lib.rs index b2d584b0..14f2f735 100644 --- a/rc6/src/lib.rs +++ b/rc6/src/lib.rs @@ -3,4 +3,5 @@ mod block_cipher; mod core; +pub use crate::core::RC6; pub use block_cipher::*; diff --git a/rc6/tests/mod.rs b/rc6/tests/mod.rs index 526e1618..af61f44a 100644 --- a/rc6/tests/mod.rs +++ b/rc6/tests/mod.rs @@ -1,8 +1,10 @@ /// generated using the code in: https://www.ietf.org/archive/id/draft-krovetz-rc6-rc5-vectors-00.txt #[cfg(test)] +#[allow(deprecated)] // uses `clone_from_slice` mod tests { - use cipher::{generic_array::GenericArray, BlockDecrypt, BlockEncrypt, KeyInit}; - use rc6::{RC6_16_16_8, RC6_32_20_16, RC6_64_24_24, RC6_8_12_4}; + use cipher::consts::*; + use cipher::{array::Array, BlockCipherDecrypt, BlockCipherEncrypt, KeyInit}; + use rc6::RC6; #[test] fn enc_dec_8_12_4() { @@ -11,8 +13,9 @@ mod tests { let pt = [0x00, 0x01, 0x02, 0x03]; let ct = [0xAE, 0xFC, 0x46, 0x12]; - let rc6 = ::new_from_slice(&key).unwrap(); - let mut block = GenericArray::clone_from_slice(&pt); + let rc6 = as KeyInit>::new_from_slice(&key).unwrap(); + + let mut block = Array::clone_from_slice(&pt); rc6.encrypt_block(&mut block); assert_eq!(ct, block[..]); @@ -28,8 +31,8 @@ mod tests { let pt = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07]; let ct = [0x2F, 0xF0, 0xB6, 0x8E, 0xAE, 0xFF, 0xAD, 0x5B]; - let rc6 = ::new_from_slice(&key).unwrap(); - let mut block = GenericArray::clone_from_slice(&pt); + let rc6 = as KeyInit>::new_from_slice(&key).unwrap(); + let mut block = Array::clone_from_slice(&pt); rc6.encrypt_block(&mut block); assert_eq!(ct, block[..]); @@ -54,8 +57,8 @@ mod tests { 0x2A, 0x3C, ]; - let rc6 = ::new_from_slice(&key).unwrap(); - let mut block = GenericArray::clone_from_slice(&pt); + let rc6 = as KeyInit>::new_from_slice(&key).unwrap(); + let mut block = Array::clone_from_slice(&pt); rc6.encrypt_block(&mut block); assert_eq!(ct, block[..]); @@ -82,8 +85,8 @@ mod tests { 0x67, 0x88, 0x66, 0x17, ]; - let rc6 = ::new_from_slice(&key).unwrap(); - let mut block = GenericArray::clone_from_slice(&pt); + let rc6 = as KeyInit>::new_from_slice(&key).unwrap(); + let mut block = Array::clone_from_slice(&pt); rc6.encrypt_block(&mut block); assert_eq!(ct, block[..]);