cipher/
tweak.rs

1//! Traits used to define functionality of [tweakable block ciphers][1].
2//!
3//! [1]: https://people.eecs.berkeley.edu/~daw/papers/tweak-crypto02.pdf
4use common::{
5    Block, BlockSizeUser,
6    array::{Array, ArraySize},
7};
8use inout::InOut;
9
10mod ctx;
11mod zero;
12
13pub use zero::ZeroTweak;
14
15/// Tweak used by a [`TweakSizeUser`] implementor.
16pub type Tweak<C> = Array<u8, <C as TweakSizeUser>::TweakSize>;
17
18/// Trait which contains tweak size used by the tweak cipher traits.
19pub trait TweakSizeUser {
20    /// Size of the tweak in bytes.
21    type TweakSize: ArraySize;
22}
23
24/// Encrypt-only functionality for tweakable block ciphers.
25pub trait TweakBlockCipherEncrypt: BlockSizeUser + TweakSizeUser + Sized {
26    /// Encrypt data using backend provided to the rank-2 closure.
27    fn encrypt_with_backend(
28        &self,
29        f: impl TweakBlockCipherEncClosure<BlockSize = Self::BlockSize, TweakSize = Self::TweakSize>,
30    );
31
32    /// Encrypt single `inout` block.
33    #[inline]
34    fn encrypt_block_inout(&self, tweak: &Tweak<Self>, block: InOut<'_, '_, Block<Self>>) {
35        self.encrypt_with_backend(ctx::BlockCtx { tweak, block });
36    }
37
38    /// Encrypt single block in-place.
39    #[inline]
40    fn encrypt_block(&self, tweak: &Tweak<Self>, block: &mut Block<Self>) {
41        self.encrypt_block_inout(tweak, block.into());
42    }
43
44    /// Encrypt `in_block` and write result to `out_block`.
45    #[inline]
46    fn encrypt_block_b2b(
47        &self,
48        tweak: &Tweak<Self>,
49        in_block: &Block<Self>,
50        out_block: &mut Block<Self>,
51    ) {
52        self.encrypt_block_inout(tweak, (in_block, out_block).into());
53    }
54}
55
56/// Decrypt-only functionality for tweakable block ciphers.
57pub trait TweakBlockCipherDecrypt: BlockSizeUser + TweakSizeUser + Sized {
58    /// Decrypt data using backend provided to the rank-2 closure.
59    fn decrypt_with_backend(
60        &self,
61        f: impl TweakBlockCipherDecClosure<BlockSize = Self::BlockSize, TweakSize = Self::TweakSize>,
62    );
63
64    /// Decrypt single `inout` block.
65    #[inline]
66    fn decrypt_block_inout(&self, tweak: &Tweak<Self>, block: InOut<'_, '_, Block<Self>>) {
67        self.decrypt_with_backend(ctx::BlockCtx { tweak, block });
68    }
69
70    /// Decrypt single block in-place.
71    #[inline]
72    fn decrypt_block(&self, tweak: &Tweak<Self>, block: &mut Block<Self>) {
73        self.decrypt_block_inout(tweak, block.into());
74    }
75
76    /// Decrypt `in_block` and write result to `out_block`.
77    #[inline]
78    fn decrypt_block_b2b(
79        &self,
80        tweak: &Tweak<Self>,
81        in_block: &Block<Self>,
82        out_block: &mut Block<Self>,
83    ) {
84        self.decrypt_block_inout(tweak, (in_block, out_block).into());
85    }
86}
87
88/// Trait for [`TweakBlockCipherEncBackend`] users.
89///
90/// This trait is used to define rank-2 closures.
91pub trait TweakBlockCipherEncClosure: BlockSizeUser + TweakSizeUser {
92    /// Execute closure with the provided block cipher backend.
93    fn call<B>(self, backend: &B)
94    where
95        B: TweakBlockCipherEncBackend<BlockSize = Self::BlockSize, TweakSize = Self::TweakSize>;
96}
97
98/// Trait for [`TweakBlockCipherDecBackend`] users.
99///
100/// This trait is used to define rank-2 closures.
101pub trait TweakBlockCipherDecClosure: BlockSizeUser + TweakSizeUser {
102    /// Execute closure with the provided block cipher backend.
103    fn call<B>(self, backend: &B)
104    where
105        B: TweakBlockCipherDecBackend<BlockSize = Self::BlockSize, TweakSize = Self::TweakSize>;
106}
107
108/// Trait implemented by block cipher mode encryption backends.
109pub trait TweakBlockCipherEncBackend: BlockSizeUser + TweakSizeUser {
110    /// Encrypt single inout block.
111    fn encrypt_block_inout(&self, tweak: &Tweak<Self>, block: InOut<'_, '_, Block<Self>>);
112
113    /// Encrypt single block in-place.
114    #[inline]
115    fn encrypt_block(&self, tweak: &Tweak<Self>, block: &mut Block<Self>) {
116        self.encrypt_block_inout(tweak, block.into());
117    }
118
119    /// Encrypt `in_block` and write result to `out_block`.
120    #[inline]
121    fn encrypt_block_b2b(
122        &self,
123        tweak: &Tweak<Self>,
124        in_block: &Block<Self>,
125        out_block: &mut Block<Self>,
126    ) {
127        self.encrypt_block_inout(tweak, (in_block, out_block).into());
128    }
129}
130
131/// Trait implemented by block cipher mode decryption backends.
132pub trait TweakBlockCipherDecBackend: BlockSizeUser + TweakSizeUser {
133    /// Decrypt single inout block.
134    fn decrypt_block_inout(&self, tweak: &Tweak<Self>, block: InOut<'_, '_, Block<Self>>);
135
136    /// Decrypt single block in-place.
137    #[inline]
138    fn decrypt_block(&self, tweak: &Tweak<Self>, block: &mut Block<Self>) {
139        self.decrypt_block_inout(tweak, block.into());
140    }
141
142    /// Decrypt `in_block` and write result to `out_block`.
143    #[inline]
144    fn decrypt_block_b2b(
145        &self,
146        tweak: &Tweak<Self>,
147        in_block: &Block<Self>,
148        out_block: &mut Block<Self>,
149    ) {
150        self.decrypt_block_inout(tweak, (in_block, out_block).into());
151    }
152}