1 #![no_std] 2 // Copyright 2023 Google LLC 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 //! Defining traits for an LDT specific Tweakable Block Cipher 17 18 #[cfg(feature = "std")] 19 extern crate std; 20 21 use crypto_provider::{CryptoProvider, CryptoRng}; 22 23 /// The higher level trait defining the single block at a time Tweakable Block Cipher types. 24 /// Holds associates types for both the [TweakableBlockCipherEncrypter] and corresponding 25 /// [TweakableBlockCipherDecrypter] 26 pub trait TweakableBlockCipher<const B: usize> { 27 /// The tweakable block cipher encryption cipher 28 type EncryptionCipher: TweakableBlockCipherEncrypter<B, Key = Self::Key, Tweak = Self::Tweak>; 29 30 /// The tweakable block cipher decryption cipher 31 type DecryptionCipher: TweakableBlockCipherDecrypter<B, Key = Self::Key, Tweak = Self::Tweak>; 32 33 /// The tweak type used with encryption/decryption. 34 type Tweak: From<[u8; B]>; 35 36 /// the tweakable block cipher key type for the tbc 37 type Key: TweakableBlockCipherKey; 38 } 39 40 /// Trait defining a Tweakable Block Cipher, single block at a time, decrypt operation 41 /// `B` is the block size in bytes. 42 pub trait TweakableBlockCipherEncrypter<const B: usize> { 43 /// The tweakable block cipher key type for the tbc 44 type Key: TweakableBlockCipherKey; 45 /// The tweak type used when encrypting 46 type Tweak: From<[u8; B]>; 47 /// Build a [TweakableBlockCipherEncrypter] with the provided and the provided key. new(key: &Self::Key) -> Self48 fn new(key: &Self::Key) -> Self; 49 /// Encrypt `block` in place using the specified `tweak`. encrypt(&self, tweak: Self::Tweak, block: &mut [u8; B])50 fn encrypt(&self, tweak: Self::Tweak, block: &mut [u8; B]); 51 } 52 53 /// Trait defining a Tweakable Block Cipher, single block at a time, encrypt operation 54 /// `B` is the block size in bytes. 55 pub trait TweakableBlockCipherDecrypter<const B: usize> { 56 /// The tweakable block cipher key type for the tbc 57 type Key: TweakableBlockCipherKey; 58 /// The tweak type used when decrypting 59 type Tweak: From<[u8; B]>; 60 /// Build a [TweakableBlockCipherDecrypter] with the provided and the provided key. new(key: &Self::Key) -> Self61 fn new(key: &Self::Key) -> Self; 62 /// Decrypt `block` in place using the specified `tweak`. decrypt(&self, tweak: Self::Tweak, block: &mut [u8; B])63 fn decrypt(&self, tweak: Self::Tweak, block: &mut [u8; B]); 64 } 65 66 /// A tweakable block cipher key as used by LDT 67 pub trait TweakableBlockCipherKey: Sized { 68 /// Two tweakable block cipher keys concatenated, as used by LDT 69 type ConcatenatedKeyArray: ConcatenatedKeyArray; 70 71 /// Split a concatenated array of two keys' bytes into individual keys. split_from_concatenated(key: &Self::ConcatenatedKeyArray) -> (Self, Self)72 fn split_from_concatenated(key: &Self::ConcatenatedKeyArray) -> (Self, Self); 73 74 /// Concatenate with another key to form an array of both key's bytes. concatenate_with(&self, other: &Self) -> Self::ConcatenatedKeyArray75 fn concatenate_with(&self, other: &Self) -> Self::ConcatenatedKeyArray; 76 } 77 78 /// The array form of two concatenated tweakable block cipher keys. 79 pub trait ConcatenatedKeyArray: Sized { 80 /// Build a concatenated key from a secure RNG. from_random<C: CryptoProvider>(rng: &mut C::CryptoRng) -> Self81 fn from_random<C: CryptoProvider>(rng: &mut C::CryptoRng) -> Self; 82 } 83 84 impl ConcatenatedKeyArray for [u8; 64] { from_random<C: CryptoProvider>(rng: &mut C::CryptoRng) -> Self85 fn from_random<C: CryptoProvider>(rng: &mut C::CryptoRng) -> Self { 86 let mut arr = [0; 64]; 87 rng.fill(&mut arr); 88 arr 89 } 90 } 91 92 impl ConcatenatedKeyArray for [u8; 128] { from_random<C: CryptoProvider>(rng: &mut C::CryptoRng) -> Self93 fn from_random<C: CryptoProvider>(rng: &mut C::CryptoRng) -> Self { 94 let mut arr = [0; 128]; 95 rng.fill(&mut arr); 96 arr 97 } 98 } 99