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
extern crate byteorder;
extern crate noise_protocol as noise;
extern crate ring;

use self::byteorder::{ByteOrder, BigEndian, LittleEndian};
use noise::{Cipher, Hash, U8Array};
use ring::aead;
use ring::digest;

pub enum Aes256Gcm {}

pub enum ChaCha20Poly1305 {}

pub struct Sha256 {
    context: digest::Context,
}

pub struct Sha512 {
    context: digest::Context,
}

impl Cipher for Aes256Gcm {
    type Key = [u8; 32];

    fn name() -> &'static str {
        "AESGCM"
    }

    fn encrypt(k: &Self::Key, nonce: u64, authtext: &[u8], plaintext: &[u8], out: &mut [u8]) {
        assert_eq!(plaintext.len() + 16, out.len());

        let mut nonce_bytes = [0u8; 12];
        BigEndian::write_u64(&mut nonce_bytes[4..], nonce);

        out[..plaintext.len()].copy_from_slice(plaintext);

        let key = aead::SealingKey::new(&aead::AES_256_GCM, k.as_slice()).unwrap();
        aead::seal_in_place(&key, &nonce_bytes, authtext, out, 16).unwrap();
    }

    fn decrypt(k: &Self::Key,
               nonce: u64,
               authtext: &[u8],
               ciphertext: &[u8],
               out: &mut [u8])
               -> Result<(), ()> {
        assert_eq!(ciphertext.len() - 16, out.len());

        let mut nonce_bytes = [0u8; 12];
        BigEndian::write_u64(&mut nonce_bytes[4..], nonce);

        // Eh, ring API is ... weird.
        let mut in_out = ciphertext.to_vec();

        let k = aead::OpeningKey::new(&aead::AES_256_GCM, k.as_slice()).unwrap();
        let out0 = aead::open_in_place(&k, &nonce_bytes, authtext, 0, &mut in_out).map_err(|_| ())?;
        assert_eq!(out0.len(), out.len());

        out.copy_from_slice(out0);
        Ok(())
    }
}

impl Cipher for ChaCha20Poly1305 {
    type Key = [u8; 32];

    fn name() -> &'static str {
        "ChaChaPoly"
    }

    fn encrypt(k: &Self::Key, nonce: u64, authtext: &[u8], plaintext: &[u8], out: &mut [u8]) {
        assert_eq!(plaintext.len() + 16, out.len());

        let mut nonce_bytes = [0u8; 12];
        LittleEndian::write_u64(&mut nonce_bytes[4..], nonce);

        out[..plaintext.len()].copy_from_slice(plaintext);

        let k = aead::SealingKey::new(&aead::CHACHA20_POLY1305, k.as_slice()).unwrap();
        aead::seal_in_place(&k, &nonce_bytes, authtext, out, 16).unwrap();
    }

    fn decrypt(k: &Self::Key,
               nonce: u64,
               authtext: &[u8],
               ciphertext: &[u8],
               out: &mut [u8])
               -> Result<(), ()> {
        assert_eq!(ciphertext.len() - 16, out.len());

        let mut nonce_bytes = [0u8; 12];
        LittleEndian::write_u64(&mut nonce_bytes[4..], nonce);

        let mut in_out = ciphertext.to_vec();

        let k = aead::OpeningKey::new(&aead::CHACHA20_POLY1305, k.as_slice()).unwrap();
        let out0 = aead::open_in_place(&k, &nonce_bytes, authtext, 0, &mut in_out).map_err(|_| ())?;

        out.copy_from_slice(out0);
        Ok(())
    }
}

impl Default for Sha256 {
    fn default() -> Sha256 {
        Sha256 { context: digest::Context::new(&digest::SHA256) }
    }
}

impl Hash for Sha256 {
    type Block = [u8; 64];
    type Output = [u8; 32];

    fn name() -> &'static str {
        "SHA256"
    }

    fn input(&mut self, data: &[u8]) {
        self.context.update(data);
    }

    fn result(&mut self) -> Self::Output {
        let mut out = [0u8; 32];
        // XXX have to clone becuase finish() moves Context.
        out.copy_from_slice(self.context.clone().finish().as_ref());
        out
    }
}

impl Default for Sha512 {
    fn default() -> Sha512 {
        Sha512 { context: digest::Context::new(&digest::SHA512) }
    }
}

impl Hash for Sha512 {
    type Block = [u8; 128];
    type Output = [u8; 64];

    fn name() -> &'static str {
        "SHA512"
    }

    fn input(&mut self, data: &[u8]) {
        self.context.update(data);
    }

    fn result(&mut self) -> Self::Output {
        let mut out = [0u8; 64];
        out.copy_from_slice(self.context.clone().finish().as_ref());
        out
    }
}