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
use byteorder::{ ByteOrder, LittleEndian };
use gimli_permutation::{ S, gimli };


#[derive(Clone)]
pub struct GimliAead([u32; S]);

pub struct Encrypt;
pub struct Decrypt;

impl GimliAead {
    pub fn new(key: &[u8; 32], nonce: &[u8; 16]) -> GimliAead {
        let mut state = [0; S];
        with(&mut state, |state| {
            state[..16].copy_from_slice(nonce);
            state[16..].copy_from_slice(key);
        });
        gimli(&mut state);
        GimliAead(state)
    }

    #[inline]
    fn process_aad<M>(self, aad: &[u8], mode: M) -> Process<M> {
        let GimliAead(mut state) = self;

        let mut iter = aad.chunks_exact(16);

        while let Some(chunk) = iter.next() {
            with(&mut state, |state| {
                for i in 0..16 {
                    state[i] ^= chunk[i];
                }
            });
            gimli(&mut state);
        }

        with(&mut state, |state| {
            let chunk = iter.remainder();
            for i in 0..chunk.len() {
                state[i] ^= chunk[i];
            }

            state[chunk.len()] ^= 1;
            state[47] ^= 1;
        });
        gimli(&mut state);

        Process { state, _mode: mode }
    }

    pub fn encrypt(self, aad: &[u8]) -> Process<Encrypt> {
        self.process_aad(aad, Encrypt)
    }

    pub fn decrypt(self, aad: &[u8]) -> Process<Decrypt> {
        self.process_aad(aad, Decrypt)
    }
}

pub struct Process<M> {
    state: [u32; S],
    _mode: M
}

impl Process<Encrypt> {
    pub fn process(self, buf: &mut [u8], tag: &mut [u8; 16]) {
        let Process { mut state, .. } = self;

        let mut iter = buf.chunks_exact_mut(16);

        while let Some(chunk) = iter.next() {
            with(&mut state, |state| {
                for i in 0..16 {
                    state[i] ^= chunk[i];
                    chunk[i] = state[i];
                }
            });
            gimli(&mut state);
        }

        with(&mut state, |state| {
            let chunk = iter.into_remainder();
            for i in 0..chunk.len() {
                state[i] ^= chunk[i];
                chunk[i] = state[i];
            }

            state[chunk.len()] ^= 1;
            state[47] ^= 1;
        });
        gimli(&mut state);

        with(&mut state, |state| tag.copy_from_slice(&state[..16]));
    }
}

impl Process<Decrypt> {
    pub fn process(self, buf: &mut [u8], tag: &[u8; 16]) -> bool {
        let Process { mut state, .. } = self;

        let mut iter = buf.rchunks_exact_mut(16);

        while let Some(chunk) = iter.next() {
            with(&mut state, |state| {
                for i in 0..16 {
                    let c = chunk[i];
                    chunk[i] = state[i] ^ c;
                    state[i] = c;
                }
            });
            gimli(&mut state);
        }

        with(&mut state, |state| {
            let chunk = iter.into_remainder();
            for i in 0..chunk.len() {
                let c = chunk[i];
                chunk[i] = state[i] ^ c;
                state[i] = c;
            }

            state[chunk.len()] ^= 1;
            state[47] ^= 1;
        });
        gimli(&mut state);

        let mut result = 0;
        with(&mut state, |state| {
            for i in 0..16 {
                result |= state[i] ^ tag[i];
            }
        });

        result == 0
    }
}


fn with<F>(state: &mut [u32; S], f: F)
    where F: FnOnce(&mut [u8; S * 4])
{
    #[inline]
    fn transmute(arr: &mut [u32; S]) -> &mut [u8; S * 4] {
        unsafe { &mut *(arr as *mut [u32; S] as *mut [u8; S * 4]) }
    }

    LittleEndian::from_slice_u32(state);
    f(transmute(state));
    LittleEndian::from_slice_u32(state);
}