| 1 |
#![allow(dead_code)] // TODO: remove |
| 2 |
|
| 3 |
use super::SymKey; |
| 4 |
use crate::{err::Res, hpke::Aead as AeadId}; |
| 5 |
use aead::{AeadMut, Key, NewAead, Nonce, Payload}; |
| 6 |
use aes_gcm::{Aes128Gcm, Aes256Gcm}; |
| 7 |
use chacha20poly1305::ChaCha20Poly1305; |
| 8 |
use std::convert::TryFrom; |
| 9 |
|
| 10 |
/// All the nonces are the same length. Exploit that. |
| 11 |
pub const NONCE_LEN: usize = 12; |
| 12 |
const COUNTER_LEN: usize = 8; |
| 13 |
const TAG_LEN: usize = 16; |
| 14 |
|
| 15 |
type SequenceNumber = u64; |
| 16 |
|
| 17 |
#[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 18 |
pub enum Mode { |
| 19 |
Encrypt, |
| 20 |
Decrypt, |
| 21 |
} |
| 22 |
|
| 23 |
enum AeadEngine { |
| 24 |
Aes128Gcm(Box<Aes128Gcm>), |
| 25 |
Aes256Gcm(Box<Aes256Gcm>), |
| 26 |
ChaCha20Poly1305(Box<ChaCha20Poly1305>), |
| 27 |
} |
| 28 |
|
| 29 |
// Dispatch functions; this just shows how janky that this sort of abstraction can be. |
| 30 |
// If this grows too much, this is fairly clearly responsive to using a macro. |
| 31 |
impl AeadEngine { |
| 32 |
fn encrypt(&mut self, nonce: &[u8], pt: Payload) -> Res<Vec<u8>> { |
| 33 |
let tag = match self { |
| 34 |
Self::Aes128Gcm(e) => e.encrypt(Nonce::<Aes128Gcm>::from_slice(nonce), pt)?, |
| 35 |
Self::Aes256Gcm(e) => e.encrypt(Nonce::<Aes256Gcm>::from_slice(nonce), pt)?, |
| 36 |
Self::ChaCha20Poly1305(e) => { |
| 37 |
e.encrypt(Nonce::<ChaCha20Poly1305>::from_slice(nonce), pt)? |
| 38 |
} |
| 39 |
}; |
| 40 |
Ok(tag) |
| 41 |
} |
| 42 |
fn decrypt(&mut self, nonce: &[u8], pt: Payload) -> Res<Vec<u8>> { |
| 43 |
let tag = match self { |
| 44 |
Self::Aes128Gcm(e) => e.decrypt(Nonce::<Aes128Gcm>::from_slice(nonce), pt)?, |
| 45 |
Self::Aes256Gcm(e) => e.decrypt(Nonce::<Aes256Gcm>::from_slice(nonce), pt)?, |
| 46 |
Self::ChaCha20Poly1305(e) => { |
| 47 |
e.decrypt(Nonce::<ChaCha20Poly1305>::from_slice(nonce), pt)? |
| 48 |
} |
| 49 |
}; |
| 50 |
Ok(tag) |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
/// A switch-hitting AEAD that uses a selected primitive. |
| 55 |
pub struct Aead { |
| 56 |
mode: Mode, |
| 57 |
engine: AeadEngine, |
| 58 |
nonce_base: [u8; NONCE_LEN], |
| 59 |
seq: SequenceNumber, |
| 60 |
} |
| 61 |
|
| 62 |
impl Aead { |
| 63 |
#[allow(clippy::unnecessary_wraps)] |
| 64 |
pub fn new( |
| 65 |
mode: Mode, |
| 66 |
algorithm: AeadId, |
| 67 |
key: &SymKey, |
| 68 |
nonce_base: [u8; NONCE_LEN], |
| 69 |
) -> Res<Self> { |
| 70 |
let aead = match algorithm { |
| 71 |
AeadId::Aes128Gcm => AeadEngine::Aes128Gcm(Box::new(Aes128Gcm::new( |
| 72 |
Key::<Aes128Gcm>::from_slice(key.as_ref()), |
| 73 |
))), |
| 74 |
AeadId::Aes256Gcm => AeadEngine::Aes256Gcm(Box::new(Aes256Gcm::new( |
| 75 |
Key::<Aes256Gcm>::from_slice(key.as_ref()), |
| 76 |
))), |
| 77 |
AeadId::ChaCha20Poly1305 => AeadEngine::ChaCha20Poly1305(Box::new( |
| 78 |
ChaCha20Poly1305::new(Key::<ChaCha20Poly1305>::from_slice(key.as_ref())), |
| 79 |
)), |
| 80 |
}; |
| 81 |
Ok(Self { |
| 82 |
mode, |
| 83 |
engine: aead, |
| 84 |
nonce_base, |
| 85 |
seq: 0, |
| 86 |
}) |
| 87 |
} |
| 88 |
|
| 89 |
#[cfg(test)] |
| 90 |
#[allow(clippy::unnecessary_wraps)] |
| 91 |
fn import_key(_alg: AeadId, k: &[u8]) -> Res<SymKey> { |
| 92 |
Ok(SymKey::from(k)) |
| 93 |
} |
| 94 |
|
| 95 |
fn nonce(&self, seq: SequenceNumber) -> Vec<u8> { |
| 96 |
let mut nonce = Vec::from(self.nonce_base); |
| 97 |
for (i, n) in nonce.iter_mut().rev().take(COUNTER_LEN).enumerate() { |
| 98 |
*n ^= u8::try_from((seq >> (8 * i)) & 0xff).unwrap(); |
| 99 |
} |
| 100 |
nonce |
| 101 |
} |
| 102 |
|
| 103 |
pub fn seal(&mut self, aad: &[u8], pt: &[u8]) -> Res<Vec<u8>> { |
| 104 |
assert_eq!(self.mode, Mode::Encrypt); |
| 105 |
// A copy for the nonce generator to write into. But we don't use the value. |
| 106 |
let nonce = self.nonce(self.seq); |
| 107 |
self.seq += 1; |
| 108 |
let ct = self.engine.encrypt(&nonce, Payload { msg: pt, aad })?; |
| 109 |
Ok(ct) |
| 110 |
} |
| 111 |
|
| 112 |
pub fn open(&mut self, aad: &[u8], seq: SequenceNumber, ct: &[u8]) -> Res<Vec<u8>> { |
| 113 |
assert_eq!(self.mode, Mode::Decrypt); |
| 114 |
let nonce = self.nonce(seq); |
| 115 |
let pt = self.engine.decrypt(&nonce, Payload { msg: ct, aad })?; |
| 116 |
Ok(pt) |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
#[cfg(test)] |
| 121 |
mod test { |
| 122 |
use super::{ |
| 123 |
super::super::{hpke::Aead as AeadId, init}, |
| 124 |
Aead, Mode, SequenceNumber, NONCE_LEN, |
| 125 |
}; |
| 126 |
|
| 127 |
/// Check that the first invocation of encryption matches expected values. |
| 128 |
/// Also check decryption of the same. |
| 129 |
fn check0( |
| 130 |
algorithm: AeadId, |
| 131 |
key: &[u8], |
| 132 |
nonce: &[u8; NONCE_LEN], |
| 133 |
aad: &[u8], |
| 134 |
pt: &[u8], |
| 135 |
ct: &[u8], |
| 136 |
) { |
| 137 |
init(); |
| 138 |
let k = Aead::import_key(algorithm, key).unwrap(); |
| 139 |
|
| 140 |
let mut enc = Aead::new(Mode::Encrypt, algorithm, &k, *nonce).unwrap(); |
| 141 |
let ciphertext = enc.seal(aad, pt).unwrap(); |
| 142 |
assert_eq!(&ciphertext[..], ct); |
| 143 |
|
| 144 |
let mut dec = Aead::new(Mode::Decrypt, algorithm, &k, *nonce).unwrap(); |
| 145 |
let plaintext = dec.open(aad, 0, ct).unwrap(); |
| 146 |
assert_eq!(&plaintext[..], pt); |
| 147 |
} |
| 148 |
|
| 149 |
fn decrypt( |
| 150 |
algorithm: AeadId, |
| 151 |
key: &[u8], |
| 152 |
nonce: &[u8; NONCE_LEN], |
| 153 |
seq: SequenceNumber, |
| 154 |
aad: &[u8], |
| 155 |
pt: &[u8], |
| 156 |
ct: &[u8], |
| 157 |
) { |
| 158 |
let k = Aead::import_key(algorithm, key).unwrap(); |
| 159 |
let mut dec = Aead::new(Mode::Decrypt, algorithm, &k, *nonce).unwrap(); |
| 160 |
let plaintext = dec.open(aad, seq, ct).unwrap(); |
| 161 |
assert_eq!(&plaintext[..], pt); |
| 162 |
} |
| 163 |
|
| 164 |
/// This tests the AEAD in QUIC in combination with the HKDF code. |
| 165 |
/// This is an AEAD-only example. |
| 166 |
#[test] |
| 167 |
fn quic_retry() { |
| 168 |
const KEY: &[u8] = &[ |
| 169 |
0xbe, 0x0c, 0x69, 0x0b, 0x9f, 0x66, 0x57, 0x5a, 0x1d, 0x76, 0x6b, 0x54, 0xe3, 0x68, |
| 170 |
0xc8, 0x4e, |
| 171 |
]; |
| 172 |
const NONCE: &[u8; NONCE_LEN] = &[ |
| 173 |
0x46, 0x15, 0x99, 0xd3, 0x5d, 0x63, 0x2b, 0xf2, 0x23, 0x98, 0x25, 0xbb, |
| 174 |
]; |
| 175 |
const AAD: &[u8] = &[ |
| 176 |
0x08, 0x83, 0x94, 0xc8, 0xf0, 0x3e, 0x51, 0x57, 0x08, 0xff, 0x00, 0x00, 0x00, 0x01, |
| 177 |
0x00, 0x08, 0xf0, 0x67, 0xa5, 0x50, 0x2a, 0x42, 0x62, 0xb5, 0x74, 0x6f, 0x6b, 0x65, |
| 178 |
0x6e, |
| 179 |
]; |
| 180 |
const CT: &[u8] = &[ |
| 181 |
0x04, 0xa2, 0x65, 0xba, 0x2e, 0xff, 0x4d, 0x82, 0x90, 0x58, 0xfb, 0x3f, 0x0f, 0x24, |
| 182 |
0x96, 0xba, |
| 183 |
]; |
| 184 |
check0(AeadId::Aes128Gcm, KEY, NONCE, AAD, &[], CT); |
| 185 |
} |
| 186 |
|
| 187 |
#[test] |
| 188 |
fn quic_server_initial() { |
| 189 |
const ALG: AeadId = AeadId::Aes128Gcm; |
| 190 |
const KEY: &[u8] = &[ |
| 191 |
0xcf, 0x3a, 0x53, 0x31, 0x65, 0x3c, 0x36, 0x4c, 0x88, 0xf0, 0xf3, 0x79, 0xb6, 0x06, |
| 192 |
0x7e, 0x37, |
| 193 |
]; |
| 194 |
const NONCE_BASE: &[u8; NONCE_LEN] = &[ |
| 195 |
0x0a, 0xc1, 0x49, 0x3c, 0xa1, 0x90, 0x58, 0x53, 0xb0, 0xbb, 0xa0, 0x3e, |
| 196 |
]; |
| 197 |
// Note that this integrates the sequence number of 1 from the example, |
| 198 |
// otherwise we can't use a sequence number of 0 to encrypt. |
| 199 |
const NONCE: &[u8; NONCE_LEN] = &[ |
| 200 |
0x0a, 0xc1, 0x49, 0x3c, 0xa1, 0x90, 0x58, 0x53, 0xb0, 0xbb, 0xa0, 0x3f, |
| 201 |
]; |
| 202 |
const AAD: &[u8] = &[ |
| 203 |
0xc1, 0x00, 0x00, 0x00, 0x01, 0x00, 0x08, 0xf0, 0x67, 0xa5, 0x50, 0x2a, 0x42, 0x62, |
| 204 |
0xb5, 0x00, 0x40, 0x75, 0x00, 0x01, |
| 205 |
]; |
| 206 |
const PT: &[u8] = &[ |
| 207 |
0x02, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x40, 0x5a, 0x02, 0x00, 0x00, 0x56, 0x03, |
| 208 |
0x03, 0xee, 0xfc, 0xe7, 0xf7, 0xb3, 0x7b, 0xa1, 0xd1, 0x63, 0x2e, 0x96, 0x67, 0x78, |
| 209 |
0x25, 0xdd, 0xf7, 0x39, 0x88, 0xcf, 0xc7, 0x98, 0x25, 0xdf, 0x56, 0x6d, 0xc5, 0x43, |
| 210 |
0x0b, 0x9a, 0x04, 0x5a, 0x12, 0x00, 0x13, 0x01, 0x00, 0x00, 0x2e, 0x00, 0x33, 0x00, |
| 211 |
0x24, 0x00, 0x1d, 0x00, 0x20, 0x9d, 0x3c, 0x94, 0x0d, 0x89, 0x69, 0x0b, 0x84, 0xd0, |
| 212 |
0x8a, 0x60, 0x99, 0x3c, 0x14, 0x4e, 0xca, 0x68, 0x4d, 0x10, 0x81, 0x28, 0x7c, 0x83, |
| 213 |
0x4d, 0x53, 0x11, 0xbc, 0xf3, 0x2b, 0xb9, 0xda, 0x1a, 0x00, 0x2b, 0x00, 0x02, 0x03, |
| 214 |
0x04, |
| 215 |
]; |
| 216 |
const CT: &[u8] = &[ |
| 217 |
0x5a, 0x48, 0x2c, 0xd0, 0x99, 0x1c, 0xd2, 0x5b, 0x0a, 0xac, 0x40, 0x6a, 0x58, 0x16, |
| 218 |
0xb6, 0x39, 0x41, 0x00, 0xf3, 0x7a, 0x1c, 0x69, 0x79, 0x75, 0x54, 0x78, 0x0b, 0xb3, |
| 219 |
0x8c, 0xc5, 0xa9, 0x9f, 0x5e, 0xde, 0x4c, 0xf7, 0x3c, 0x3e, 0xc2, 0x49, 0x3a, 0x18, |
| 220 |
0x39, 0xb3, 0xdb, 0xcb, 0xa3, 0xf6, 0xea, 0x46, 0xc5, 0xb7, 0x68, 0x4d, 0xf3, 0x54, |
| 221 |
0x8e, 0x7d, 0xde, 0xb9, 0xc3, 0xbf, 0x9c, 0x73, 0xcc, 0x3f, 0x3b, 0xde, 0xd7, 0x4b, |
| 222 |
0x56, 0x2b, 0xfb, 0x19, 0xfb, 0x84, 0x02, 0x2f, 0x8e, 0xf4, 0xcd, 0xd9, 0x37, 0x95, |
| 223 |
0xd7, 0x7d, 0x06, 0xed, 0xbb, 0x7a, 0xaf, 0x2f, 0x58, 0x89, 0x18, 0x50, 0xab, 0xbd, |
| 224 |
0xca, 0x3d, 0x20, 0x39, 0x8c, 0x27, 0x64, 0x56, 0xcb, 0xc4, 0x21, 0x58, 0x40, 0x7d, |
| 225 |
0xd0, 0x74, 0xee, |
| 226 |
]; |
| 227 |
check0(ALG, KEY, NONCE, AAD, PT, CT); |
| 228 |
decrypt(ALG, KEY, NONCE_BASE, 1, AAD, PT, CT); |
| 229 |
} |
| 230 |
|
| 231 |
#[test] |
| 232 |
fn quic_chacha() { |
| 233 |
const ALG: AeadId = AeadId::ChaCha20Poly1305; |
| 234 |
const KEY: &[u8] = &[ |
| 235 |
0xc6, 0xd9, 0x8f, 0xf3, 0x44, 0x1c, 0x3f, 0xe1, 0xb2, 0x18, 0x20, 0x94, 0xf6, 0x9c, |
| 236 |
0xaa, 0x2e, 0xd4, 0xb7, 0x16, 0xb6, 0x54, 0x88, 0x96, 0x0a, 0x7a, 0x98, 0x49, 0x79, |
| 237 |
0xfb, 0x23, 0xe1, 0xc8, |
| 238 |
]; |
| 239 |
const NONCE_BASE: &[u8; NONCE_LEN] = &[ |
| 240 |
0xe0, 0x45, 0x9b, 0x34, 0x74, 0xbd, 0xd0, 0xe4, 0x4a, 0x41, 0xc1, 0x44, |
| 241 |
]; |
| 242 |
// Note that this integrates the sequence number of 654360564 from the example, |
| 243 |
// otherwise we can't use a sequence number of 0 to encrypt. |
| 244 |
const NONCE: &[u8; NONCE_LEN] = &[ |
| 245 |
0xe0, 0x45, 0x9b, 0x34, 0x74, 0xbd, 0xd0, 0xe4, 0x6d, 0x41, 0x7e, 0xb0, |
| 246 |
]; |
| 247 |
const AAD: &[u8] = &[0x42, 0x00, 0xbf, 0xf4]; |
| 248 |
const PT: &[u8] = &[0x01]; |
| 249 |
const CT: &[u8] = &[ |
| 250 |
0x65, 0x5e, 0x5c, 0xd5, 0x5c, 0x41, 0xf6, 0x90, 0x80, 0x57, 0x5d, 0x79, 0x99, 0xc2, |
| 251 |
0x5a, 0x5b, 0xfb, |
| 252 |
]; |
| 253 |
check0(ALG, KEY, NONCE, AAD, PT, CT); |
| 254 |
// Now use the real nonce and sequence number from the example. |
| 255 |
decrypt(ALG, KEY, NONCE_BASE, 654_360_564, AAD, PT, CT); |
| 256 |
} |
| 257 |
} |