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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
// TODO: Note that Apple's implementation will malloc/copy keys to an aligned
// buffer if necessary.

// TODO: enum of all algorithms so that we can force iv, padding, etc. to be
// provided if required.

use std::{ffi::c_void, fmt::Display};

#[repr(C)]
#[derive(Copy, Clone)]
enum Operation {
    Encrypt = 0,
    Decrypt = 1,
}

#[repr(u32)]
#[non_exhaustive]
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Mode {
    ECB = 1,
    CBC = 2,
    CFB = 3,
    CTR = 4,
    // F8 = 5,
    // LRW = 6,
    OFB = 7,
    XTS = 8,
    /// Must be specified for RC4, and must not be specified for others.
    // RC4 = 9,
    CFB8 = 10,
}

#[repr(u32)]
#[derive(Copy, Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum Padding {
    None = 0,
    PKCS7 = 1,
}

type CCCryptorRef = *mut c_void;

extern "C" {
    fn CCCryptorCreateWithMode(
        operation: Operation,
        mode: u32,
        config: u32,
        padding: Padding,
        iv: *const c_void,
        key: *const c_void,
        key_length: usize,
        tweak: *const c_void,
        tweak_length: usize,
        rounds: usize,
        options: u32,
        handle: *mut CCCryptorRef,
    ) -> Status;

    fn CCCryptorRelease(handle: CCCryptorRef) -> Status;

    fn CCCryptorUpdate(
        handle: CCCryptorRef,
        input: *const c_void,
        input_len: usize,
        output: *mut c_void,
        output_len: usize,
        written: *mut usize,
    ) -> Status;

    fn CCCryptorFinal(
        handle: CCCryptorRef,
        output: *mut c_void,
        output_len: usize,
        written: *mut usize,
    ) -> Status;

    fn CCCryptorGetOutputLength(handle: CCCryptorRef, input_len: usize, finishing: bool) -> usize;
}

#[derive(Debug, PartialEq)]
#[non_exhaustive]
pub enum CryptorError {
    Param,
    Memory,
    Alignment,
    Decode,
    Unimplemented,
    RNGFailure,
    Unspecified,
    CallSequence,
    KeySize,
    Key,
    InitializationVectorPresent,
    Unexpected(i32),
}

impl std::error::Error for CryptorError {}

impl Display for CryptorError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let s = match self {
            Self::Param => "illegal parameter value",
            Self::Memory => "memory allocation failed",
            Self::Alignment => "input size was nit aligned properly",
            Self::Decode => "input data did not encode or decrypt properly",
            Self::Unimplemented => "function not implemented for the current algorithm",
            Self::RNGFailure => "random number generated failed",
            Self::Unspecified => "an unspecified failure occurred",
            Self::CallSequence => "call sequence failure",
            Self::KeySize => "key size is invalid",
            Self::Key => "key is invalid",
            Self::InitializationVectorPresent => "ECB mode does not support initialization vectors",
            Self::Unexpected(code) => {
                let s = format!("unexpected error {}", code);
                return f.write_str(&s);
            }
        };

        f.write_str(s)
    }
}

#[allow(dead_code)]
#[derive(Copy, Clone, Debug, PartialEq)]
#[repr(i32)]
enum Status {
    Success = 0,
    ParamError = -4300,
    BufferTooSmall = -4301,
    MemoryFailure = -4302,
    AlignmentError = -4303,
    DecodeError = -4304,
    Unimplemented = -4305,
    Overflow = -4306,
    RNGFailure = -4307,
    UnspecifiedError = -4308,
    CallSequenceError = -4309,
    KeySizeError = -4310,
    InvalidKey = -4311,
}

impl Into<CryptorError> for Status {
    fn into(self) -> CryptorError {
        match self {
            Status::Success => unreachable!(),
            Status::ParamError => CryptorError::Param,
            Status::MemoryFailure => CryptorError::Memory,
            Status::AlignmentError => CryptorError::Alignment,
            Status::DecodeError => CryptorError::Decode,
            Status::Unimplemented => CryptorError::Unimplemented,
            Status::RNGFailure => CryptorError::RNGFailure,
            Status::UnspecifiedError => CryptorError::Unspecified,
            Status::CallSequenceError => CryptorError::CallSequence,
            Status::KeySizeError => CryptorError::KeySize,
            Status::InvalidKey => CryptorError::Key,
            _ => CryptorError::Unexpected(self as i32),
        }
    }
}

#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq)]
/// The configuration for a [`Cryptor`].
///
/// ```
/// # use common_crypto::cryptor::{Config, Mode};
/// let config = Config::AES256 {
///     mode: Mode::CTR,
///     iv: Some(b"use random iv :)"),
///     key: b"aes128 key must be 32 bytes long",
/// };
/// ```
// TODO: padding, rounds
pub enum Config<'a> {
    AES128 {
        mode: Mode,
        iv: Option<&'a [u8; 16]>,
        key: &'a [u8; 16],
    },
    AES192 {
        mode: Mode,
        iv: Option<&'a [u8; 16]>,
        key: &'a [u8; 24],
    },
    AES256 {
        mode: Mode,
        iv: Option<&'a [u8; 16]>,
        key: &'a [u8; 32],
    },
    DES {
        mode: Mode,
        iv: Option<&'a [u8; 8]>,
        key: &'a [u8; 8],
    },
    TDES {
        mode: Mode,
        iv: Option<&'a [u8; 8]>,
        key: &'a [u8; 24],
    },
    CAST {
        mode: Mode,
        iv: Option<&'a [u8; 8]>,
        /// Valid key sizes are between 5 and 24.
        key: &'a [u8],
        padding: Padding,
    },
    RC4 {
        /// Valid key sizes are between 1 and 512.
        key: &'a [u8],
    },
    RC2 {
        mode: Mode,
        iv: Option<&'a [u8; 8]>,
        /// Valid key sizes are between 1 and 128.
        key: &'a [u8],
    },
    Blowfish {
        mode: Mode,
        iv: Option<&'a [u8; 8]>,
        /// Valid key sizes are between 8 and 56.
        key: &'a [u8],
    },
}

impl<'a> From<&Config<'a>> for u32 {
    fn from(config: &Config) -> Self {
        match config {
            Config::AES128 { .. } => 0,
            Config::AES192 { .. } => 0,
            Config::AES256 { .. } => 0,
            Config::DES { .. } => 1,
            Config::TDES { .. } => 2,
            Config::CAST { .. } => 3,
            Config::RC4 { .. } => 4,
            Config::RC2 { .. } => 5,
            Config::Blowfish { .. } => 6,
        }
    }
}

impl<'a> Config<'a> {
    fn padding(&self) -> Padding {
        match self {
            Config::CAST { padding, .. } => *padding,
            _ => Padding::None,
        }
    }

    fn rounds(&self) -> usize {
        0
    }

    const fn mode(&self) -> u32 {
        match self {
            Config::AES128 { mode, .. } => *mode as u32,
            Config::AES192 { mode, .. } => *mode as u32,
            Config::AES256 { mode, .. } => *mode as u32,
            Config::DES { mode, .. } => *mode as u32,
            Config::TDES { mode, .. } => *mode as u32,
            Config::CAST { mode, .. } => *mode as u32,
            Config::RC4 { .. } => 9,
            Config::RC2 { mode, .. } => *mode as u32,
            Config::Blowfish { mode, .. } => *mode as u32,
        }
    }

    fn iv_ptr(&self) -> Result<*const u8, CryptorError> {
        let (mode, ptr) = match self {
            Config::AES128 { mode, iv, .. } if iv.is_some() => (mode, iv.unwrap().as_ptr()),
            Config::AES192 { mode, iv, .. } if iv.is_some() => (mode, iv.unwrap().as_ptr()),
            Config::AES256 { mode, iv, .. } if iv.is_some() => (mode, iv.unwrap().as_ptr()),
            Config::DES { mode, iv, .. } if iv.is_some() => (mode, iv.unwrap().as_ptr()),
            Config::CAST { mode, iv, .. } if iv.is_some() => (mode, iv.unwrap().as_ptr()),
            Config::RC2 { mode, iv, .. } if iv.is_some() => (mode, iv.unwrap().as_ptr()),
            Config::Blowfish { mode, iv, .. } if iv.is_some() => (mode, iv.unwrap().as_ptr()),
            _ => return Ok(std::ptr::null()),
        };

        if mode == &Mode::ECB {
            Err(CryptorError::InitializationVectorPresent)
        } else {
            Ok(ptr)
        }
    }

    fn key(&self) -> &[u8] {
        match self {
            Config::AES128 { key, .. } => *key,
            Config::AES192 { key, .. } => *key,
            Config::AES256 { key, .. } => *key,
            Config::DES { key, .. } => *key,
            Config::TDES { key, .. } => *key,
            Config::CAST { key, .. } => *key,
            Config::RC4 { key, .. } => *key,
            Config::RC2 { key, .. } => *key,
            Config::Blowfish { key, .. } => *key,
        }
    }
}

/// A cryptor supporting all of the block and stream ciphers provided by the
/// common crypto library.
///
/// ```
/// # use common_crypto::cryptor::{Config, Cryptor};
/// let config = Config::RC4 { key: b"Key" };
/// assert_eq!(
///     Cryptor::encrypt(&config, b"Plaintext").unwrap(),
///     &[0xbb, 0xf3, 0x16, 0xe8, 0xd9, 0x40, 0xaf, 0x0a, 0xd3]
/// );
/// ```

#[derive(Debug)]
pub struct Cryptor {
    handle: CCCryptorRef,
}

impl<'a> Drop for Cryptor {
    fn drop(&mut self) {
        unsafe {
            CCCryptorRelease(self.handle);
        }
    }
}

impl Cryptor {
    fn new<'a>(config: &Config<'a>, operation: Operation) -> Result<Cryptor, CryptorError> {
        let mut handle: CCCryptorRef = std::ptr::null_mut();

        let status = unsafe {
            CCCryptorCreateWithMode(
                operation,
                config.mode(),
                config.into(),
                config.padding(),
                config.iv_ptr()? as *const c_void,
                config.key().as_ptr() as *const c_void,
                config.key().len(),
                // Tweak is unsued
                std::ptr::null(),
                0,
                config.rounds(),
                0,
                &mut handle as *mut *mut c_void,
            )
        };

        if status != Status::Success {
            return Err(status.into());
        }

        Ok(Cryptor { handle })
    }

    pub fn new_encryptor<'a>(config: &Config<'a>) -> Result<Self, CryptorError> {
        Self::new(config, Operation::Encrypt)
    }

    pub fn new_decryptor<'a>(config: &Config<'a>) -> Result<Self, CryptorError> {
        Self::new(config, Operation::Decrypt)
    }

    /// Encrypts the data and writes to the provided buffer. The buffer will
    /// be resized as required, and will be cleared on error.
    pub fn update(
        &self,
        input: impl AsRef<[u8]>,
        output: &mut Vec<u8>,
    ) -> Result<(), CryptorError> {
        let input = input.as_ref();
        let mut written = 0usize;

        output.resize(
            unsafe { CCCryptorGetOutputLength(self.handle, input.len(), false) },
            0,
        );

        let status = unsafe {
            CCCryptorUpdate(
                self.handle,
                input.as_ptr() as *const c_void,
                input.len(),
                output.as_mut_ptr() as *mut c_void,
                output.capacity(),
                &mut written as *mut usize,
            )
        };

        if status != Status::Success {
            output.clear();
            return Err(status.into());
        }

        output.resize(written, 0);

        Ok(())
    }

    /// Finalises the encryption, returning any remaining data where
    /// appropriate. The cryptor cannot be used again.
    pub fn finish(self, output: &mut Vec<u8>) -> Result<(), CryptorError> {
        let mut written = 0usize;

        let status = unsafe {
            CCCryptorFinal(
                self.handle,
                output.as_mut_ptr() as *mut c_void,
                output.capacity(),
                &mut written as *mut usize,
            )
        };

        if status != Status::Success {
            output.clear();
            return Err(status.into());
        }

        output.resize(written, 0);

        Ok(())
    }
}

impl Cryptor {
    pub fn encrypt<'a>(
        config: &Config<'a>,
        input: impl AsRef<[u8]>,
    ) -> Result<Vec<u8>, CryptorError> {
        let mut output = Vec::new();
        Cryptor::new_encryptor(config)?.update(input, &mut output)?;
        Ok(output)
    }

    pub fn decrypt<'a>(
        config: &Config<'a>,
        input: impl AsRef<[u8]>,
    ) -> Result<Vec<u8>, CryptorError> {
        let mut output = Vec::new();
        Cryptor::new_decryptor(config)?.update(input, &mut output)?;
        Ok(output)
    }
}