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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
use aes_gcm::Aes256Gcm;
use aead::{Aead, NewAead, generic_array::GenericArray};
use std::{fmt, error};
use std::convert::{TryInto, TryFrom};

#[derive(Debug, Clone)]
pub enum InvalidKeyError {
    InvalidKeySizeError,
    InvalidKeyBase64Error
}

impl fmt::Display for InvalidKeyError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            InvalidKeyError::InvalidKeySizeError => write!(f, "{}", "Please provide a 32-byte, base64-encoded, key"),
            InvalidKeyError::InvalidKeyBase64Error => write!(f, "{}", "Please provide a valid base64"),
        }
    }
}

impl error::Error for InvalidKeyError {
    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        // Generic error, underlying cause isn't tracked.
        None
    }
}

pub struct Key {
    pub u8_array: [u8; 32]
}

impl TryFrom<&str> for Key {
    type Error = InvalidKeyError;
    fn try_from(base64_key: &str) -> Result<Self, InvalidKeyError> {
        let key = match base64::decode(base64_key) {
            Ok(data) => data,
            Err(_) => return Err(InvalidKeyError::InvalidKeyBase64Error)
        };

        let u8_array: Result<[u8; 32], _> = key.as_slice().try_into();
        match u8_array {
            Ok(value) => Ok(Self {
                u8_array: value
            }),
            Err(_) => Err(InvalidKeyError::InvalidKeySizeError)
        }
    }
}

impl TryFrom<String> for Key {
    type Error = InvalidKeyError;
    fn try_from(base64_key: String) -> Result<Self, InvalidKeyError> {
        Self::try_from(&base64_key[..])
    }
}

#[derive(Debug, Clone)]
pub enum InvalidIvError {
    InvalidIvSizeError,
    InvalidIvBase64Error
}


impl fmt::Display for InvalidIvError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            InvalidIvError::InvalidIvSizeError => write!(f, "{}", "Please provide a 12-byte, base64-encoded, iv"),
            InvalidIvError::InvalidIvBase64Error => write!(f, "{}", "Please provide a valid base64"),
        }
    }
}

impl error::Error for InvalidIvError {
    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        // Generic error, underlying cause isn't tracked.
        None
    }
}

pub struct Iv {
    pub u8_array: [u8; 12]
}

impl TryFrom<&str> for Iv {
    type Error = InvalidIvError;
    fn try_from(base64_iv: &str) -> Result<Iv, InvalidIvError> {
        let iv = match base64::decode(base64_iv) {
            Ok(data) => data,
            Err(_) => return Err(InvalidIvError::InvalidIvBase64Error)
        };

        let u8_array: Result<[u8; 12], _> = iv.as_slice().try_into();
        match u8_array {
            Ok(value) => Ok(Iv {
                u8_array: value
            }),
            Err(_) => Err(InvalidIvError::InvalidIvSizeError)
        }
    }
}
impl Iv {
    pub fn generate() -> Iv {
        Iv {
            u8_array: rand::random::<[u8; 12]>()
        }
    }
}

impl From<&Iv> for String {
    fn from(iv: &Iv) -> String {
        base64::encode(&iv.u8_array)
    }
}

impl fmt::Display for Iv {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", String::from(self))
    }
}


pub struct Encrypted {
    pub u8_vec: Vec<u8>
}

impl TryFrom<&str> for Encrypted {
    type Error = base64::DecodeError;
    fn try_from(base64_encrypted: &str) -> Result<Encrypted, base64::DecodeError> {
        Ok(Encrypted {
            u8_vec: base64::decode(base64_encrypted)?
        })
    }
}

impl From<&Encrypted> for String {
    fn from(encrypted: &Encrypted) -> String {
        base64::encode(&encrypted.u8_vec)
    }
}

impl fmt::Display for Encrypted {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", String::from(self))
    }
}

pub struct Decrypted<'a> {
    value: &'a str
}
impl<'a> From<&'a str> for Decrypted<'a> {
    fn from(value: &'a str) -> Self {
        Self { value: value }
    }
}

impl From<&Decrypted<'_>> for String {
    fn from(decrypted: &Decrypted<'_>) -> String {
        String::from(decrypted.value)
    }
}

impl<'a> fmt::Display for Decrypted<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", String::from(self))
    }
}


pub struct EncryptedAndIv {
    pub encrypted: Encrypted,
    pub iv: Iv
}


#[derive(Debug, Clone)]
pub enum EncryptionError {
    GenericEncryptionError
}
impl fmt::Display for EncryptionError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            EncryptionError::GenericEncryptionError => write!(f, "{}", "Encryption error"),
        }
    }
}

impl error::Error for EncryptionError {
    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        // Generic error, underlying cause isn't tracked.
        None
    }
}

pub fn encrypt<'a>(key: &Key, decrypted: &Decrypted) -> Result<EncryptedAndIv, EncryptionError> {
    let iv = Iv::generate();
    let nonce = GenericArray::from_slice(&iv.u8_array);
    let client = Aes256Gcm::new(GenericArray::clone_from_slice(&key.u8_array));
    match client.encrypt(nonce, decrypted.value.as_bytes()) {
        Ok(ciphertext) => Ok(EncryptedAndIv {
            iv: iv,
            encrypted: Encrypted {
                u8_vec: ciphertext
            }
        }),
        Err(_) => Err(EncryptionError::GenericEncryptionError)
    }
}

#[derive(Debug, Clone)]
pub enum DecryptionError {
    InvalidUTF8DecryptionError,
    GenericDecryptionError
}
impl fmt::Display for DecryptionError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            DecryptionError::InvalidUTF8DecryptionError => write!(f, "{}", "Decryption error: invalid UTF-8"),
            DecryptionError::GenericDecryptionError => write!(f, "{}", "Decryption error"),
        }
    }
}

impl error::Error for DecryptionError {
    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        // Generic error, underlying cause isn't tracked.
        None
    }
}

pub fn decrypt(key: &Key, encrypted_and_iv: EncryptedAndIv) -> Result<String, DecryptionError> {
    let nonce = GenericArray::from_slice(&encrypted_and_iv.iv.u8_array);
    let client = Aes256Gcm::new(GenericArray::clone_from_slice(&key.u8_array));

    match client.decrypt(nonce, encrypted_and_iv.encrypted.u8_vec.as_ref()) {
        Ok(decrypted_u8_vec) => match String::from_utf8(decrypted_u8_vec) {
            Ok(decrypted_string) => Ok(decrypted_string),
            Err(_) => Err(DecryptionError::InvalidUTF8DecryptionError)
        },
        Err(_) => Err(DecryptionError::GenericDecryptionError)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn key_try_from_invalid_base64_fails() {
        match Key::try_from("012") {
            Ok(_) => assert!(false),
            Err(e) => match e {
                InvalidKeyError::InvalidKeySizeError => assert!(false, "Should err an InvalidKeyError::InvalidKeyBase64Error"),
                InvalidKeyError::InvalidKeyBase64Error => assert!(true)
            }
        }
    }

    #[test]
    fn key_try_from_valid_3byte_fails() {
        match Key::try_from("MDEy") {
            Ok(_) => assert!(false),
            Err(e) => match e {
                InvalidKeyError::InvalidKeyBase64Error => assert!(false, "Should err an InvalidKeyError::InvalidKeySizeError"),
                InvalidKeyError::InvalidKeySizeError => assert!(true)
            }
        }
    }

    #[test]
    fn key_try_from_valid_33byte_fails() {
        match Key::try_from("MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEy") {
            Ok(_) => assert!(false),
            Err(e) => match e {
                InvalidKeyError::InvalidKeyBase64Error => assert!(false, "Should err an InvalidKeyError::InvalidKeySizeError"),
                InvalidKeyError::InvalidKeySizeError => assert!(true)
            }
        }
    }

    #[test]
    fn key_try_from_valid_32_bytes_succeeds() {
        match Key::try_from("MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDE=") {
            Err(_) => assert!(false, "Should succeed"),
            Ok(key) => assert_eq!(key.u8_array, [48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 49])
        }
    }

    #[test]
    fn iv_try_from_invalid_base64_fails() {
        match Iv::try_from("012") {
            Ok(_) => assert!(false),
            Err(e) => match e {
                InvalidIvError::InvalidIvSizeError => assert!(false, "Should err an InvalidIvError::InvalidIvBase64Error"),
                InvalidIvError::InvalidIvBase64Error => assert!(true)
            }
        }
    }

    #[test]
    fn iv_try_from_valid_3byte_fails() {
        match Iv::try_from("YWJj") {
            Ok(_) => assert!(false),
            Err(e) => match e {
                InvalidIvError::InvalidIvBase64Error => assert!(false, "Should err an InvalidIvError::InvalidIvSizeError"),
                InvalidIvError::InvalidIvSizeError => assert!(true)
            }
        }
    }

    #[test]
    fn iv_try_from_valid_13byte_fails() {
        match Iv::try_from("MDEyMzQ1Njc4OTAxMg==") {
            Ok(_) => assert!(false),
            Err(e) => match e {
                InvalidIvError::InvalidIvBase64Error => assert!(false, "Should err an InvalidIvError::InvalidIvSizeError"),
                InvalidIvError::InvalidIvSizeError => assert!(true)
            }
        }
    }

    #[test]
    fn iv_try_from_valid_12byte_succeeds() {
        match Iv::try_from("MDEyMzQ1Njc4OTAx") {
            Err(_) => assert!(false, "Should succeeds"),
            Ok(iv) => assert_eq!(iv.u8_array, [48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 49])
        }
    }

    #[test]
    fn iv_generate() {
        assert!(
            Iv::generate().u8_array != Iv::generate().u8_array,
            "Should generate almost unique values"
        )
    }

    #[test]
    fn iv_format() {
        assert_eq!(
            format!("{}", Iv::try_from("MDEyMzQ1Njc4OTAx").unwrap()),
            "MDEyMzQ1Njc4OTAx"
        )
    }

    #[test]
    fn encrypted_from64_invalid_base64() {
        match Encrypted::try_from("aaaaaaa") {
            Ok(_) => assert!(false, "Should err"),
            Err(_) => assert!(true)
        }
    }

    #[test]
    fn encrypted_from64_valid_base64() {
        match Encrypted::try_from("YWFhYWFhYQ==") {
            Err(_) => assert!(false, "Should ok"),
            Ok(encryped) => assert_eq!(encryped.u8_vec, vec![97, 97, 97, 97, 97, 97, 97])
        }
    }

    #[test]
    fn encrypted_format() {
        assert_eq!(
            format!("{}", Encrypted::try_from("YWFhYWFhYQ==").unwrap()),
            "YWFhYWFhYQ=="
        )
    }

    // #[test]
    // Not able to find any example that would make this err...
    // fn encrypt_err_when_encryption_error() {
    //     let key = Key::try_from("12345678901234567890123456789012").unwrap();
    //     let decrypted = Decrypted::from("???")
    //     match encrypt(&key, &decrypted) {
    //         Ok(_) => assert!(false, "Should err"),
    //         Err(e) => assert!(true)
    //     }
    // }

    #[test]
    fn encrypted_values_are_different_for_same_inputs() {
        let encrypted_1 = encrypt(
            &Key::try_from("MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDE=").unwrap(),
            &Decrypted::from("This is a text.")
        ).unwrap();
        let encrypted_2 = encrypt(
            &Key::try_from("MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDE=").unwrap(),
            &Decrypted::from("This is a text.")
        ).unwrap();
        assert!(encrypted_1.encrypted.u8_vec != encrypted_2.encrypted.u8_vec)
    }

    #[test]
    fn encrypted_values_are_different_for_different_inputs() {
        let encrypted_1 = encrypt(
            &Key::try_from("MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDE=").unwrap(),
            &Decrypted::from("This is a text.")
        ).unwrap();
        let encrypted_2 = encrypt(
            &Key::try_from("MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDE=").unwrap(),
            &Decrypted::from("This is another text.")
        ).unwrap();
        assert!(encrypted_1.encrypted.u8_vec != encrypted_2.encrypted.u8_vec)
    }

    #[test]
    fn encrypt_decrypt_is_iso() {
        let key = Key::try_from("MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDE=").unwrap();
        let encrypted = encrypt(
            &key,
            &Decrypted::from("This is a text.")
        ).unwrap();

        assert_eq!(decrypt(&key, encrypted).unwrap(), String::from("This is a text."))
    }

    #[test]
    fn encrypt_decrypt_is_iso_with_string_key() {
        let key = Key::try_from(String::from("MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDE=")).unwrap();
        let encrypted = encrypt(
            &key,
            &Decrypted::from("This is a text.")
        ).unwrap();

        assert_eq!(decrypt(&key, encrypted).unwrap(), String::from("This is a text."))
    }

    #[test]
    fn decrypt_fails_when_non_utf8() {
        let key = Key::try_from("MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDE=").unwrap();

        let iv = Iv::generate();
        let nonce = GenericArray::from_slice(&iv.u8_array);
        let client = Aes256Gcm::new(GenericArray::clone_from_slice(&key.u8_array));
        let invalid_utf8_bytes: &[u8] = &[133u8, 133u8];
        let ciphertext = client.encrypt(nonce, invalid_utf8_bytes).unwrap();

        let encrypted_and_iv = EncryptedAndIv {
            iv: iv,
            encrypted: Encrypted {
                u8_vec: ciphertext
            }
        };

        match decrypt(&key, encrypted_and_iv) {
            Ok(_) => assert!(false, "Should err InvalidUTF8DecryptionError"),
            Err(e) => match e {
                DecryptionError::GenericDecryptionError => assert!(false, "Should err InvalidUTF8DecryptionError"),
                DecryptionError::InvalidUTF8DecryptionError => assert!(true)
            }
        }
    }

    // #[test]
    // Not able to find any example that would make this err...
    // fn decrypt_err_when_decryption_error() {
    //     let key = Key::try_from("12345678901234567890123456789012").unwrap();
    //     let encrypted_and_iv = EncryptedAndIv {
    //         encrypted: encrypted,
    //         iv: iv
    //     }
    //     match decrypt(&key, encrypted_and_iv) {
    //         Ok(_) => assert!(false, "Should err GenericDecryptionError"),
    //         Err(e) => match e {
    //             DecryptionError::InvalidUTF8DecryptionError => assert!(false, GenericDecryptionError),
    //             DecryptionError::GenericDecryptionError => assert!(true)
    //         }
    //     }
    // }
}