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
#![no_std]
#[cfg(test)]
#[macro_use]
extern crate std;
use block_cipher_trait::generic_array::functional::FunctionalSequence;
use block_cipher_trait::generic_array::typenum::U16;
use block_cipher_trait::generic_array::{ArrayLength, GenericArray};
use block_cipher_trait::BlockCipher;
use cmac::crypto_mac::MacResult;
use cmac::{Cmac, Mac};
use ctr::stream_cipher::{NewStreamCipher, SyncStreamCipher};
use subtle::ConstantTimeEq;
pub struct Eax<C: BlockCipher<BlockSize = U16> + Clone>
where C::ParBlocks: ArrayLength<GenericArray<u8, U16>>
{
phantom: core::marker::PhantomData<C>,
}
impl<C: BlockCipher<BlockSize = U16> + Clone> Eax<C>
where C::ParBlocks: ArrayLength<GenericArray<u8, U16>>
{
pub fn encrypt(
key: &GenericArray<u8, C::KeySize>,
nonce: &GenericArray<u8, C::KeySize>,
header: &[u8],
data: &mut [u8],
) -> GenericArray<u8, <Cmac<C> as Mac>::OutputSize>
{
let n = Self::cmac_with_iv(key, 0, nonce).code();
let h = Self::cmac_with_iv(key, 1, header).code();
let mut cipher = ctr::Ctr128::<C>::new(key, &n);
cipher.apply_keystream(data);
let c = Self::cmac_with_iv(key, 2, data).code();
n.zip(h, |a, b| a ^ b).zip(c, |a, b| a ^ b)
}
pub fn decrypt(
key: &GenericArray<u8, C::KeySize>,
nonce: &GenericArray<u8, C::KeySize>,
header: &[u8],
data: &mut [u8],
mac: &[u8],
) -> Result<(), cmac::crypto_mac::MacError>
{
let n = Self::cmac_with_iv(key, 0, nonce).code();
let h = Self::cmac_with_iv(key, 1, header).code();
let c = Self::cmac_with_iv(key, 2, data).code();
let mac2 = n.zip(h, |a, b| a ^ b).zip(c, |a, b| a ^ b);
let mac2 = &mac2[..mac.len()];
if mac.ct_eq(mac2).unwrap_u8() != 1 {
return Err(cmac::crypto_mac::MacError);
}
let mut cipher = ctr::Ctr128::<C>::new(key, &n);
cipher.apply_keystream(data);
Ok(())
}
fn cmac_with_iv(
key: &GenericArray<u8, C::KeySize>,
iv: u8,
data: &[u8],
) -> MacResult<<Cmac<C> as Mac>::OutputSize>
{
let mut mac = Cmac::<C>::new(key);
mac.input(&[0; 15]);
mac.input(&[iv]);
mac.input(data);
mac.result()
}
}
#[cfg(test)]
mod test {
use super::*;
use std::prelude::v1::*;
use quickcheck::quickcheck;
#[test]
fn known_data() {
let mut data = [1, 2, 3, 4, 5];
let mac = Eax::<aes::Aes128>::encrypt(&[0; 16].into(), &[0; 16].into(), &[1, 2, 3, 4], &mut data);
assert_eq!(mac.as_slice(), &[232, 88, 147, 206, 130, 126, 14, 121, 62, 127, 33, 233, 239, 81, 51, 177]);
assert_eq!(&data, &[182, 81, 68, 170, 62]);
}
quickcheck! {
fn roundtrip(data: Vec<u8>) -> bool {
let mut enc = data.clone();
let mac = Eax::<aes::Aes128>::encrypt(&[0; 16].into(), &[0; 16].into(), &[1, 2, 3, 4], &mut enc);
Eax::<aes::Aes128>::decrypt(&[0; 16].into(), &[0; 16].into(), &[1, 2, 3, 4], &mut enc, &mac).unwrap();
data == enc
}
}
}