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
#[macro_use]
extern crate lazy_static;

extern crate rand;

mod block;
mod keys;
mod sbox;
mod pbox;
mod modes;
mod errors;

pub use self::block::Block;
pub use self::keys::{Key, Key80Bit, Key128Bit};
pub use self::modes::OpMode;
pub use self::errors::DecryptError;

/// Encrypt a string.
///
/// Encrypt a string with a specific key and operation mode.
/// Because the ciphertext is not a valid UTF-8 string, it is
/// instead returned as a byte vector. In addition, if the
/// specified operation mode needs an initialization vector,
/// one will be randomly generated and returned with the
/// ciphertext.
///
/// # Arguments
///
/// * `text` - String slice containing the plaintext to encrypt.
/// * `key` - The key to be used for encryption.
/// * `mode` - Block cipher mode of operation that will be used.
///
/// # Examples
///
/// ```
/// use present::{encrypt_str, Key80Bit, OpMode};
/// let key = Key80Bit::new([0xFF; 10]);
/// let (ciphertext, iv) = encrypt_str("Hello, world!", &key, &OpMode::CBC);
/// assert!(iv.is_some());
/// ```
pub fn encrypt_str<K: Key>(text: &str, key: &K, mode: &OpMode) -> (Vec<u8>, Option<Block>) {
    // Check how much padding needs to be appended to the string
    let pad_len = match text.len() % 8 {
        0 => 8,
        x => 8 - x,
    };
    let mut ciphertext: Vec<Block> = Vec::with_capacity((text.len() + pad_len) / 8);

    match *mode {
        OpMode::ECB => {
            let mut current_bytes = [0u8; 8];
            for (i, byte) in text.bytes().enumerate() {
                current_bytes[i % 8] = byte;

                if i % 8 == 7 {
                    let mut block = Block::from_bytes(&current_bytes);
                    block.encrypt(key);
                    ciphertext.push(block);
                }
            }

            add_padding(&mut current_bytes, pad_len);

            let mut block = Block::from_bytes(&current_bytes);
            block.encrypt(key);
            ciphertext.push(block);

            (blocks_to_bytes(ciphertext), None)
        },
        OpMode::CBC => {
            let iv = modes::random_iv();

            let mut current_bytes = [0u8; 8];
            for (i, byte) in text.bytes().enumerate() {
                // Fill current block with bytes from the input string
                current_bytes[i % 8] = byte;

                // When a block is full, process it
                if i % 8 == 7 {
                    // Encrypt current block
                    let mut block = Block::from_bytes(&current_bytes);

                    // XOR with previous block (IV for the first block)
                    match ciphertext.iter().rev().next() {
                        Some(pb) => block ^= pb,
                        None => block ^= &iv,
                    };

                    // Perform actual encryption
                    block.encrypt(key);

                    // Add encrypted block to ciphertext vector
                    ciphertext.push(block);
                }
            }

            // Add padding
            add_padding(&mut current_bytes, pad_len);

            // Encrypt final block
            let mut block = Block::from_bytes(&current_bytes);
            match ciphertext.iter().rev().next() {
                Some(pb) => block ^= pb,
                None => block ^= &iv,
            };
            block.encrypt(key);

            // Add final block to ciphertext vector
            ciphertext.push(block);

            // Return ciphertext in bytes + IV
            (blocks_to_bytes(ciphertext), Some(iv))
        },
    }
}

/// Decrypt a string.
///
/// Decrypt a string (given as a byte slice) using a specific key
/// and operation mode. If decryption was successful, the decrypted
/// string is returned, otherwise an error.
///
/// # Arguments
///
/// * `ciphertext` - Encrypted string as a byte slice. Usually the result of a call to
///   `encrypt_str`.
/// * `key` - The key to be used for decryption (must be the same that was used for encryption).
/// * `mode` - Block cipher mode of operation that will be used (must be the same that was used
///   for encryption).
/// * `init_vec` - If the operation mode uses an initialization vector, pass `Some` with the IV
///   that was returned by `encrypt_str`. Otherwise, pass `None`.
///
/// # Errors
///
/// Returns `Err` with a `DecryptError` if an error occurred during decryption. See the
/// [documentation of `DecryptError`](enum.DecryptError.html) for details.
///
/// # Examples
///
/// ```
/// use present::{encrypt_str, decrypt_str, Key80Bit, OpMode};
/// let key = Key80Bit::new([0xFF; 10]);
/// let op_mode = OpMode::CBC;
/// let (ciphertext, iv) = encrypt_str("Hello, world!", &key, &op_mode);
///
/// let decrypt_result = decrypt_str(&ciphertext, &key, &op_mode, iv);
/// assert_eq!(decrypt_result.unwrap(), "Hello, world!");
/// ```
pub fn decrypt_str<K: Key>(ciphertext: &[u8], key: &K, mode: &OpMode, init_vec: Option<Block>) -> Result<String, DecryptError> {
    // Check that ciphertext is at least one block
    if ciphertext.len() < 8 {
        return Err(DecryptError::CiphertextTooShort(ciphertext.len()));
    }

    // Check that ciphertext length aligns with block size
    if ciphertext.len() % 8 != 0 {
        return Err(DecryptError::CiphertextNotAligned(ciphertext.len()));
    }

    let mut plain_bytes: Vec<u8> = Vec::with_capacity(ciphertext.len());

    match *mode {
        OpMode::ECB => {
            let mut current_bytes = [0u8; 8];
            for (i, byte) in ciphertext.iter().enumerate() {
                current_bytes[i % 8] = *byte;

                if i % 8 == 7 {
                    let mut block = Block::from_bytes(&current_bytes);
                    block.decrypt(key);
                    plain_bytes.extend(block.to_bytes().iter());
                }
            }

            let len = plain_bytes.len();
            let to_remove = check_padding(&plain_bytes[(len - 8)..])?;
            plain_bytes.truncate(len - to_remove);

            String::from_utf8(plain_bytes).map_err(|e| DecryptError::from(e))
        },
        OpMode::CBC => {
            let mut last_block = match init_vec {
                Some(x) => x,
                None => return Err(DecryptError::InitVecMissing),
            };

            let mut current_bytes = [0u8; 8];
            for(i, byte) in ciphertext.iter().enumerate() {
                current_bytes[i % 8] = *byte;

                if i % 8 == 7 {
                    let mut block = Block::from_bytes(&current_bytes);
                    block.decrypt(key);
                    block ^= &last_block;
                    plain_bytes.extend(block.to_bytes().iter());

                    last_block = Block::from_bytes(&current_bytes);
                }
            }

            let len = plain_bytes.len();
            let to_remove = check_padding(&plain_bytes[(len - 8)..])?;
            plain_bytes.truncate(len - to_remove);

            String::from_utf8(plain_bytes).map_err(|e| DecryptError::from(e))
        },
    }
}

fn add_padding(current_bytes: &mut [u8; 8], pad_len: usize) {
    if pad_len > 8 {
        panic!("Logic error! Padding length cannot be >8, but is {}", pad_len);
    }

    for byte in current_bytes.iter_mut().rev().take(pad_len) {
        // PKCS5 padding (pad with bytes all of the same value
        // as the number of padding bytes)
        *byte = pad_len as u8;
    }
}

fn check_padding(final_block: &[u8]) -> Result<usize, DecryptError> {
    if final_block.len() != 8 {
        panic!("Logic error! Received {} element slice for padding check, expected 8 elements!", final_block.len());
    }

    let pad = final_block[7];
    if pad > 8 {
        return Err(DecryptError::InvalidPadding);
    }

    for byte in final_block.iter().rev().take(pad as usize) {
        if *byte != pad {
            return Err(DecryptError::InvalidPadding);
        }
    }

    Ok(pad as usize)
}

fn blocks_to_bytes(blocks: Vec<Block>) -> Vec<u8> {
    let mut ret = Vec::with_capacity(blocks.len() * 8);
    for block in &blocks {
        for byte in &(block.to_bytes()) {
            ret.push(*byte);
        }
    }
    ret
}

#[cfg(test)]
mod tests {
    use super::*;
    use super::{add_padding, blocks_to_bytes, check_padding};

    #[test]
    fn test_add_padding_to_block() {
        let mut bytes = [0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF];
        add_padding(&mut bytes, 4);
        assert_eq!(bytes, [0x01, 0x23, 0x45, 0x67, 0x04, 0x04, 0x04, 0x04]);

        add_padding(&mut bytes, 1);
        assert_eq!(bytes, [0x01, 0x23, 0x45, 0x67, 0x04, 0x04, 0x04, 0x01]);

        add_padding(&mut bytes, 8);
        assert_eq!(bytes, [0x08; 8]);
    }

    #[test]
    #[should_panic]
    fn test_add_padding_rejects_invalid_length() {
        let mut bytes = [0u8; 8];
        add_padding(&mut bytes, 9);
    }

    #[test]
    fn test_check_padding_returns_correct_values() {
        let bytes = [0x4E, 0xDD, 0xA0, 0x34, 0x04, 0x04, 0x04, 0x04];
        assert_eq!(check_padding(&bytes).unwrap(), 4);

        let bytes = [0x4E, 0xDD, 0xA0, 0x34, 0x04, 0x03, 0x03, 0x03];
        assert_eq!(check_padding(&bytes).unwrap(), 3);

        let bytes = [0x4E, 0xDD, 0xA0, 0x34, 0xBC, 0xE5, 0xA2, 0x01];
        assert_eq!(check_padding(&bytes).unwrap(), 1);

        let bytes = [0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08];
        assert_eq!(check_padding(&bytes).unwrap(), 8);
    }

    #[test]
    #[should_panic]
    fn test_check_padding_rejects_invalid_slices() {
        let bytes = [0x34, 0x14];
        check_padding(&bytes).unwrap();
    }

    #[test]
    #[should_panic]
    fn test_check_padding_rejects_invalid_padding() {
        let bytes = [0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09];
        check_padding(&bytes).unwrap();
    }

    #[test]
    #[should_panic]
    fn test_check_padding_rejects_wrong_padding() {
        let bytes = [0x35, 0xD2, 0x39, 0xE5, 0xAA, 0x04, 0x03, 0x03];
        check_padding(&bytes).unwrap();
    }

    #[test]
    fn test_blocks_to_bytes() {
        let blocks = vec![Block::new(0x0123456789ABCDEF_u64), Block::new(0xFEDCBA9876543210_u64)];
        let bytes = blocks_to_bytes(blocks);
        assert_eq!(bytes[0], 0x01);
        assert_eq!(bytes[5], 0xAB);
        assert_eq!(bytes[8], 0xFE);
        assert_eq!(bytes[15], 0x10);
    }
}