1use crate::{Decrypt, Encrypt, Error, cbc_dec, cbc_enc, xor};
2use cipher::{
3 Block, BlockCipherDecBackend, BlockCipherDecClosure, BlockCipherDecrypt, BlockCipherEncBackend,
4 BlockCipherEncClosure, BlockCipherEncrypt, BlockSizeUser, InnerIvInit, IvSizeUser,
5 array::{Array, ArraySize},
6 common::InnerUser,
7 inout::InOutBuf,
8 typenum::Unsigned,
9};
10
11#[derive(Debug)]
13pub struct CbcCs1<C: BlockSizeUser> {
14 cipher: C,
15 iv: Block<C>,
16}
17
18impl<C: BlockSizeUser> InnerUser for CbcCs1<C> {
19 type Inner = C;
20}
21
22impl<C: BlockSizeUser> IvSizeUser for CbcCs1<C> {
23 type IvSize = C::BlockSize;
24}
25
26impl<C: BlockSizeUser> InnerIvInit for CbcCs1<C> {
27 fn inner_iv_init(cipher: Self::Inner, iv: &cipher::Iv<Self>) -> Self {
28 Self {
29 cipher,
30 iv: iv.clone(),
31 }
32 }
33}
34
35impl<C: BlockCipherEncrypt> Encrypt for CbcCs1<C> {
36 fn encrypt_inout(self, buf: InOutBuf<'_, '_, u8>) -> Result<(), Error> {
37 if buf.len() < C::BlockSize::USIZE {
38 return Err(Error);
39 }
40 let Self { cipher, iv } = self;
41 cipher.encrypt_with_backend(Closure { iv, buf });
42 Ok(())
43 }
44}
45
46impl<C: BlockCipherDecrypt> Decrypt for CbcCs1<C> {
47 fn decrypt_inout(self, buf: InOutBuf<'_, '_, u8>) -> Result<(), Error> {
48 if buf.len() < C::BlockSize::USIZE {
49 return Err(Error);
50 }
51 let Self { cipher, iv } = self;
52 cipher.decrypt_with_backend(Closure { iv, buf });
53 Ok(())
54 }
55}
56
57struct Closure<'a, BS: ArraySize> {
58 iv: Array<u8, BS>,
59 buf: InOutBuf<'a, 'a, u8>,
60}
61
62impl<BS: ArraySize> BlockSizeUser for Closure<'_, BS> {
63 type BlockSize = BS;
64}
65
66impl<BS: ArraySize> BlockCipherEncClosure for Closure<'_, BS> {
67 fn call<B: BlockCipherEncBackend<BlockSize = BS>>(self, cipher: &B) {
68 let Self { mut iv, mut buf } = self;
69 let (blocks, tail) = buf.reborrow().into_chunks();
70
71 cbc_enc(cipher, &mut iv, blocks);
72
73 if tail.is_empty() {
74 return;
75 }
76
77 let mut block = Block::<B>::default();
78 block[..tail.len()].copy_from_slice(tail.get_in());
79 xor(&mut block, &iv);
80 cipher.encrypt_block_inplace(&mut block);
81
82 let pos = buf.len() - B::BlockSize::USIZE;
83 buf.get_out()[pos..].copy_from_slice(&block);
84 }
85}
86
87impl<BS: ArraySize> BlockCipherDecClosure for Closure<'_, BS> {
88 fn call<B: BlockCipherDecBackend<BlockSize = BS>>(self, cipher: &B) {
89 let Self { mut iv, mut buf } = self;
90
91 let (mut blocks, tail) = buf.reborrow().into_chunks();
92
93 if !tail.is_empty() {
94 let mid = blocks.len() - 1;
95 blocks = blocks.split_at(mid).0;
96 };
97
98 cbc_dec(cipher, &mut iv, blocks);
99
100 if tail.is_empty() {
101 return;
102 }
103
104 let tail_len = tail.len();
105 let bs = B::BlockSize::USIZE;
106 let mid = buf.len() - (bs + tail_len);
107 let mut rem = buf.split_at(mid).1;
108
109 let n = rem.len() - bs;
110 let mut block1: Block<B> = rem.get_in()[..bs].try_into().unwrap();
111 let mut block2: Block<B> = rem.get_in()[n..].try_into().unwrap();
112
113 cipher.decrypt_block_inplace(&mut block2);
114 block1[n..].copy_from_slice(&block2[n..]);
115 xor(&mut block2, &block1);
116
117 cipher.decrypt_block_inplace(&mut block1);
118 xor(&mut block1, &iv);
119
120 rem.get_out()[..bs].copy_from_slice(&block1);
121 rem.get_out()[bs..].copy_from_slice(&block2[..n]);
122 }
123}