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
//! Chacha20 encryption functions
//!
//! [Official documentation](https://monocypher.org/manual/advanced/chacha20)

use ffi;
use std::mem;

pub struct Context(ffi::crypto_chacha_ctx);

/// These functions provide an incremental interface for the Chacha20 encryption primitive.
///
/// # Example
///
/// ```
/// use monocypher::chacha20::Context;
/// use monocypher::utils::wipe;
///
///    let mut key: [u8; 32] = [
///        171, 107, 219, 186, 0, 173, 209, 50, 252, 77, 93, 85, 99, 106, 222, 162, 122, 140, 150,
///        228, 61, 93, 186, 251, 45, 23, 222, 14, 121, 172, 147, 241,
///    ];
///    let nonce: [u8; 8] = [0, 0, 0, 0, 0, 0, 0, 1];
///
///    let mut ctx = Context::new(&key, nonce);
///    let mut ctx2 = Context::new(&key, nonce);
///    let ciphertext = ctx.encrypt("test".as_bytes());
///    let plaintext = ctx2.decrypt(&ciphertext);
///
///    wipe(&mut key);
///
///    assert_eq!(&plaintext, &"test".as_bytes())
/// ```
impl Context {

    /// Initialises a new context with the given key and nonce.
    /// Uses an 8-byte nonce, which is too small to be selected at random.
    /// Use a counter.
    #[inline]
    pub fn new(key: &[u8], nonce: [u8; 8]) -> Context {
        unsafe {
            let mut ctx = mem::uninitialized();
            ffi::crypto_chacha20_init(&mut ctx, key.as_ptr(), nonce.as_ptr());
            Context(ctx)
        }
    }

    /// Initialises a new context with the given key and nonce.
    /// Uses a 24-byte nonce, which is big enough to be selected at random.
    /// Use your operating system to generate cryptographic secure random numbers.
    /// Read the about random number generators in the [documentation](https://monocypher.org/manual/)
    #[inline]
    pub fn new_x(key: &[u8], nonce: [u8; 24]) -> Context {
        unsafe {
            let mut ctx = mem::uninitialized();
            ffi::crypto_chacha20_x_init(&mut ctx, key.as_ptr(), nonce.as_ptr());
            Context(ctx)
        }
    }

    /// Encrypts the given plaintext.
    #[inline]
    pub fn encrypt(&mut self, plaintext: &[u8]) -> Vec<u8> {
        let mut cipher_text = vec![0u8; plaintext.len()];
        unsafe {
            ffi::crypto_chacha20_encrypt(
                &mut self.0,
                cipher_text.as_mut_ptr(),
                plaintext.as_ptr(),
                plaintext.len(),
            );
            cipher_text
        }
    }

    /// Decrypts the given ciphertext.
    #[inline]
    pub fn decrypt(&mut self, ciphertext: &[u8]) -> Vec<u8> {
        self.encrypt(ciphertext)
    }

    /// Same as encrypt but with plaintext beeing NULL.
    /// Usefull as a non cryptographic user space random number generator.
    #[inline]
    pub fn stream(&mut self, stream: &mut [u8]) {
        unsafe {
            ffi::crypto_chacha20_stream(&mut self.0, stream.as_mut_ptr(), stream.len());
        }
    }

    /// Resets the internal counter of the context to the given number.
    /// Resuming the encryption will use the stream at the block number.
    /// May be used to en/decrypt part of a long message.
    /// Can also be used to implement AEAD constructions like the ones
    /// explained in [RFC 7539](https://tools.ietf.org/html/rfc7539).
    #[inline]
    pub fn chacha20_set_ctr(&mut self, ctr: u64) {
        unsafe {
            ffi::crypto_chacha20_set_ctr(&mut self.0, ctr);
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn new() {
        let key: [u8; 32] = [
            171, 107, 219, 186, 0, 173, 209, 50, 252, 77, 93, 85, 99, 106, 222, 162, 122, 140, 150,
            228, 61, 93, 186, 251, 45, 23, 222, 14, 121, 172, 147, 241,
        ];

        let nonce: [u8; 8] = [0, 0, 0, 0, 0, 0, 0, 1];

        let mut ctx = Context::new(&key, nonce);
        let mut ctx2 = Context::new(&key, nonce);
        let ciphertext = ctx.encrypt("test".as_bytes());
        let plaintext = ctx2.decrypt(&ciphertext);

        assert_eq!(&plaintext, &"test".as_bytes())
    }

    #[test]
    fn new_wrong_nonce() {
        let key: [u8; 32] = [
            171, 107, 219, 186, 0, 173, 209, 50, 252, 77, 93, 85, 99, 106, 222, 162, 122, 140, 150,
            228, 61, 93, 186, 251, 45, 23, 222, 14, 121, 172, 147, 241,
        ];

        let nonce: [u8; 8] = [0, 0, 0, 0, 0, 0, 0, 1];
        let nonce2: [u8; 8] = [0, 0, 0, 0, 0, 0, 0, 8];

        let mut ctx = Context::new(&key, nonce);
        let mut ctx2 = Context::new(&key, nonce2);
        let ciphertext = ctx.encrypt("test".as_bytes());
        let plaintext = ctx2.decrypt(&ciphertext);

        assert_ne!(&plaintext, &"test".as_bytes())
    }

    #[test]
    fn new_x() {
        let key: [u8; 32] = [
            171, 107, 219, 186, 0, 173, 209, 50, 252, 77, 93, 85, 99, 106, 222, 162, 122, 140, 150,
            228, 61, 93, 186, 251, 45, 23, 222, 14, 121, 172, 147, 241,
        ];

        let nonce = [1u8; 24];

        let mut ctx = Context::new_x(&key, nonce);
        let mut ctx2 = Context::new_x(&key, nonce);
        let ciphertext = ctx.encrypt("test".as_bytes());
        let plaintext = ctx2.decrypt(&ciphertext);

        assert_eq!(&plaintext, &"test".as_bytes())
    }

    #[test]
    fn stream() {
        let key: [u8; 32] = [
            171, 107, 219, 186, 0, 173, 209, 50, 252, 77, 93, 85, 99, 106, 222, 162, 122, 140, 150,
            228, 61, 93, 186, 251, 45, 23, 222, 14, 121, 172, 147, 241,
        ];
        let nonce: [u8; 8] = [0, 0, 0, 0, 0, 0, 0, 1];

        let mut ctx = Context::new(&key, nonce);
        let mut v: Vec<u8> = vec![0, 0, 0, 0];
        ctx.stream(& mut v);
        assert_ne!(v, vec![0, 0, 0, 0])
    }

    #[test]
    fn ctx() {
        let key: [u8; 32] = [
            171, 107, 219, 186, 0, 173, 209, 50, 252, 77, 93, 85, 99, 106, 222, 162, 122, 140, 150,
            228, 61, 93, 186, 251, 45, 23, 222, 14, 121, 172, 147, 241,
        ];
        let nonce: [u8; 24] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1];
        let nonce2: [u8; 24] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2];

        let mut ctx = Context::new_x(&key, nonce);
        let mut ctx2 = Context::new_x(&key, nonce2);
        let ciphertext = ctx.encrypt("test".as_bytes());
        ctx2.chacha20_set_ctr(1);
        let plaintext = ctx2.decrypt(&ciphertext);

        assert_ne!(&plaintext, &"test".as_bytes())
    }
}