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 #![forbid(unsafe_code)] 16 #![deny( 17 missing_docs, 18 clippy::indexing_slicing, 19 clippy::unwrap_used, 20 clippy::panic, 21 clippy::expect_used 22 )] 23 24 //! Defining traits for an LDT specific Tweakable Block Cipher 25 26 use crypto_provider::{CryptoProvider, CryptoRng}; 27 28 /// The higher level trait defining the single block at a time Tweakable Block Cipher types. 29 /// Holds associates types for both the [TweakableBlockCipherEncrypter] and corresponding 30 /// [TweakableBlockCipherDecrypter] 31 pub trait TweakableBlockCipher<const B: usize> { 32 /// The tweakable block cipher encryption cipher 33 type EncryptionCipher: TweakableBlockCipherEncrypter<B, Key = Self::Key, Tweak = Self::Tweak>; 34 35 /// The tweakable block cipher decryption cipher 36 type DecryptionCipher: TweakableBlockCipherDecrypter<B, Key = Self::Key, Tweak = Self::Tweak>; 37 38 /// The tweak type used with encryption/decryption. 39 type Tweak: From<[u8; B]>; 40 41 /// the tweakable block cipher key type for the tbc 42 type Key: TweakableBlockCipherKey; 43 } 44 45 /// Trait defining a Tweakable Block Cipher, single block at a time, decrypt operation 46 /// `B` is the block size in bytes. 47 pub trait TweakableBlockCipherEncrypter<const B: usize> { 48 /// The tweakable block cipher key type for the tbc 49 type Key: TweakableBlockCipherKey; 50 /// The tweak type used when encrypting 51 type Tweak: From<[u8; B]>; 52 /// Build a [TweakableBlockCipherEncrypter] with the provided and the provided key. new(key: &Self::Key) -> Self53 fn new(key: &Self::Key) -> Self; 54 /// Encrypt `block` in place using the specified `tweak`. encrypt(&self, tweak: Self::Tweak, block: &mut [u8; B])55 fn encrypt(&self, tweak: Self::Tweak, block: &mut [u8; B]); 56 } 57 58 /// Trait defining a Tweakable Block Cipher, single block at a time, encrypt operation 59 /// `B` is the block size in bytes. 60 pub trait TweakableBlockCipherDecrypter<const B: usize> { 61 /// The tweakable block cipher key type for the tbc 62 type Key: TweakableBlockCipherKey; 63 /// The tweak type used when decrypting 64 type Tweak: From<[u8; B]>; 65 /// Build a [TweakableBlockCipherDecrypter] with the provided and the provided key. new(key: &Self::Key) -> Self66 fn new(key: &Self::Key) -> Self; 67 /// Decrypt `block` in place using the specified `tweak`. decrypt(&self, tweak: Self::Tweak, block: &mut [u8; B])68 fn decrypt(&self, tweak: Self::Tweak, block: &mut [u8; B]); 69 } 70 71 /// A tweakable block cipher key as used by LDT 72 pub trait TweakableBlockCipherKey: Sized { 73 /// Two tweakable block cipher keys concatenated, as used by LDT 74 type ConcatenatedKeyArray: ConcatenatedKeyArray; 75 76 /// Split a concatenated array of two keys' bytes into individual keys. split_from_concatenated(key: &Self::ConcatenatedKeyArray) -> (Self, Self)77 fn split_from_concatenated(key: &Self::ConcatenatedKeyArray) -> (Self, Self); 78 79 /// Concatenate with another key to form an array of both key's bytes. concatenate_with(&self, other: &Self) -> Self::ConcatenatedKeyArray80 fn concatenate_with(&self, other: &Self) -> Self::ConcatenatedKeyArray; 81 } 82 83 /// The array form of two concatenated tweakable block cipher keys. 84 pub trait ConcatenatedKeyArray: Sized { 85 /// Build a concatenated key from a secure RNG. from_random<C: CryptoProvider>(rng: &mut C::CryptoRng) -> Self86 fn from_random<C: CryptoProvider>(rng: &mut C::CryptoRng) -> Self; 87 } 88 89 impl ConcatenatedKeyArray for [u8; 64] { from_random<C: CryptoProvider>(rng: &mut C::CryptoRng) -> Self90 fn from_random<C: CryptoProvider>(rng: &mut C::CryptoRng) -> Self { 91 let mut arr = [0; 64]; 92 rng.fill(&mut arr); 93 arr 94 } 95 } 96 97 impl ConcatenatedKeyArray for [u8; 128] { from_random<C: CryptoProvider>(rng: &mut C::CryptoRng) -> Self98 fn from_random<C: CryptoProvider>(rng: &mut C::CryptoRng) -> Self { 99 let mut arr = [0; 128]; 100 rng.fill(&mut arr); 101 arr 102 } 103 } 104