1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
//! Generic implementations of CTR mode for block ciphers.
//!
//! Mode functionality is accessed using traits from re-exported
//! [`cipher`](https://docs.rs/cipher) crate.
//!
//! # ⚠️ Security Warning: [Hazmat!]
//!
//! This crate does not ensure ciphertexts are authentic! Thus ciphertext integrity
//! is not verified, which can lead to serious vulnerabilities!
//!
//! # `Ctr128` Usage Example
//!
//! ```
//! use ctr::cipher::{NewCipher, StreamCipher, StreamCipherSeek};
//!
//! // `aes` crate provides AES block cipher implementation
//! type Aes128Ctr = ctr::Ctr128BE<aes::Aes128>;
//!
//! let mut data = [1, 2, 3, 4, 5, 6, 7];
//!
//! let key = b"very secret key.";
//! let nonce = b"and secret nonce";
//!
//! // create cipher instance
//! let mut cipher = Aes128Ctr::new(key.into(), nonce.into());
//!
//! // apply keystream (encrypt)
//! cipher.apply_keystream(&mut data);
//! assert_eq!(data, [6, 245, 126, 124, 180, 146, 37]);
//!
//! // seek to the keystream beginning and apply it again to the `data` (decrypt)
//! cipher.seek(0);
//! cipher.apply_keystream(&mut data);
//! assert_eq!(data, [1, 2, 3, 4, 5, 6, 7]);
//! ```
//!
//! [Hazmat!]: https://github.com/RustCrypto/meta/blob/master/HAZMAT.md

#![no_std]
#![doc(
    html_logo_url = "https://raw.githubusercontent.com/RustCrypto/meta/master/logo.svg",
    html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/meta/master/logo.svg"
)]
#![warn(missing_docs, rust_2018_idioms)]

pub use cipher;
use cipher::{
    errors::{LoopError, OverflowError},
    generic_array::{
        typenum::{Unsigned, U16},
        ArrayLength, GenericArray,
    },
    Block, BlockCipher, BlockEncrypt, FromBlockCipher, ParBlocks, SeekNum, StreamCipher,
    StreamCipherSeek,
};
use core::fmt;
use core::ops::Div;

pub mod flavors;
use flavors::CtrFlavor;

/// CTR mode with 128-bit big endian counter.
pub type Ctr128BE<B> = Ctr<B, flavors::Ctr128BE>;
/// CTR mode with 128-bit little endian counter.
pub type Ctr128LE<B> = Ctr<B, flavors::Ctr128LE>;
/// CTR mode with 64-bit big endian counter.
pub type Ctr64BE<B> = Ctr<B, flavors::Ctr64BE>;
/// CTR mode with 64-bit little endian counter.
pub type Ctr64LE<B> = Ctr<B, flavors::Ctr64LE>;
/// CTR mode with 32-bit big endian counter.
pub type Ctr32BE<B> = Ctr<B, flavors::Ctr32BE>;
/// CTR mode with 32-bit little endian counter.
pub type Ctr32LE<B> = Ctr<B, flavors::Ctr32LE>;

/// Generic CTR block mode isntance.
#[derive(Clone)]
pub struct Ctr<B, F>
where
    B: BlockEncrypt + BlockCipher<BlockSize = U16>,
    B::ParBlocks: ArrayLength<GenericArray<u8, U16>>,
    B::BlockSize: Div<F::Size>,
    F: CtrFlavor,
{
    cipher: B,
    nonce: GenericArray<F, F::Size>,
    counter: F,
    buffer: Block<B>,
    buf_pos: u8,
}

impl<B, F> Ctr<B, F>
where
    B: BlockEncrypt + BlockCipher<BlockSize = U16>,
    B::ParBlocks: ArrayLength<GenericArray<u8, U16>>,
    B::BlockSize: Div<F::Size>,
    F: CtrFlavor,
{
    fn check_data_len(&self, data: &[u8]) -> Result<(), LoopError> {
        let bs = B::BlockSize::USIZE;
        let leftover_bytes = bs - self.buf_pos as usize;
        if data.len() < leftover_bytes {
            return Ok(());
        }
        let blocks = 1 + (data.len() - leftover_bytes) / bs;
        self.counter
            .checked_add(blocks)
            .ok_or(LoopError)
            .map(|_| ())
    }

    /// Seek to the given block
    // TODO: replace with a trait-based method
    pub fn seek_block(&mut self, block: F::Backend) {
        self.counter = F::from_backend(block);
    }

    /// Return number of the current block
    // TODO: replace with a trait-based method
    pub fn current_block(&self) -> F::Backend {
        self.counter.to_backend()
    }
}

impl<B, F> FromBlockCipher for Ctr<B, F>
where
    B: BlockEncrypt + BlockCipher<BlockSize = U16>,
    B::ParBlocks: ArrayLength<GenericArray<u8, U16>>,
    B::BlockSize: Div<F::Size>,
    F: CtrFlavor,
{
    type BlockCipher = B;
    type NonceSize = B::BlockSize;

    #[inline]
    fn from_block_cipher(cipher: B, nonce: &Block<B>) -> Self {
        let nonce = F::load(nonce);
        Self {
            cipher,
            buffer: Default::default(),
            nonce,
            counter: Default::default(),
            buf_pos: 0,
        }
    }
}

impl<B, F> StreamCipher for Ctr<B, F>
where
    B: BlockEncrypt + BlockCipher<BlockSize = U16>,
    B::ParBlocks: ArrayLength<GenericArray<u8, U16>>,
    B::BlockSize: Div<F::Size>,
    F: CtrFlavor,
{
    fn try_apply_keystream(&mut self, mut data: &mut [u8]) -> Result<(), LoopError> {
        self.check_data_len(data)?;
        let bs = B::BlockSize::USIZE;
        let pos = self.buf_pos as usize;
        debug_assert!(bs > pos);

        let mut counter = self.counter.clone();
        if pos != 0 {
            if data.len() < bs - pos {
                let n = pos + data.len();
                xor(data, &self.buffer[pos..n]);
                self.buf_pos = n as u8;
                return Ok(());
            } else {
                let (l, r) = data.split_at_mut(bs - pos);
                data = r;
                xor(l, &self.buffer[pos..]);
                counter.increment();
            }
        }

        // Process blocks in parallel if cipher supports it
        let pb = B::ParBlocks::USIZE;
        if pb != 1 {
            let mut chunks = data.chunks_exact_mut(bs * pb);
            let mut blocks: ParBlocks<B> = Default::default();
            for chunk in &mut chunks {
                for b in blocks.iter_mut() {
                    *b = counter.generate_block(&self.nonce);
                    counter.increment();
                }

                self.cipher.encrypt_par_blocks(&mut blocks);
                xor(chunk, to_slice::<B>(&blocks));
            }
            data = chunks.into_remainder();
        }

        let mut chunks = data.chunks_exact_mut(bs);
        for chunk in &mut chunks {
            let mut block = counter.generate_block(&self.nonce);
            counter.increment();
            self.cipher.encrypt_block(&mut block);
            xor(chunk, &block);
        }

        let rem = chunks.into_remainder();
        if !rem.is_empty() {
            self.buffer = counter.generate_block(&self.nonce);
            self.cipher.encrypt_block(&mut self.buffer);
            xor(rem, &self.buffer[..rem.len()]);
        }
        self.buf_pos = rem.len() as u8;
        self.counter = counter;
        Ok(())
    }
}

impl<B, F> StreamCipherSeek for Ctr<B, F>
where
    B: BlockEncrypt + BlockCipher<BlockSize = U16>,
    B::ParBlocks: ArrayLength<GenericArray<u8, U16>>,
    B::BlockSize: Div<F::Size>,
    F: CtrFlavor,
{
    fn try_current_pos<T: SeekNum>(&self) -> Result<T, OverflowError> {
        T::from_block_byte(self.counter.to_backend(), self.buf_pos, B::BlockSize::U8)
    }

    fn try_seek<S: SeekNum>(&mut self, pos: S) -> Result<(), LoopError> {
        let res: (F::Backend, u8) = pos.to_block_byte(B::BlockSize::U8)?;
        self.counter = F::from_backend(res.0);
        self.buf_pos = res.1;
        if self.buf_pos != 0 {
            let mut block = self.counter.generate_block(&self.nonce);
            self.cipher.encrypt_block(&mut block);
            self.buffer = block;
        }
        Ok(())
    }
}

impl<B, F> fmt::Debug for Ctr<B, F>
where
    B: BlockEncrypt + BlockCipher<BlockSize = U16> + fmt::Debug,
    B::ParBlocks: ArrayLength<GenericArray<u8, U16>>,
    B::BlockSize: Div<F::Size>,
    F: CtrFlavor + fmt::Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
        write!(f, "Ctr-{:?}-{:?}", self.counter, self.cipher)
    }
}

#[inline(always)]
fn xor(buf: &mut [u8], key: &[u8]) {
    debug_assert_eq!(buf.len(), key.len());
    for (a, b) in buf.iter_mut().zip(key) {
        *a ^= *b;
    }
}

#[inline(always)]
fn to_slice<C: BlockEncrypt>(blocks: &ParBlocks<C>) -> &[u8] {
    let blocks_len = C::BlockSize::to_usize() * C::ParBlocks::to_usize();
    unsafe { core::slice::from_raw_parts(blocks.as_ptr() as *const u8, blocks_len) }
}