| 1 |
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 2 |
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 3 |
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 4 |
// option. This file may not be copied, modified, or distributed |
| 5 |
// except according to those terms. |
| 6 |
|
| 7 |
use crate::p11::Context; |
| 8 |
use crate::p11::{ |
| 9 |
self, PK11_AEADOp, PK11_CreateContextBySymKey, CKA_DECRYPT, CKA_ENCRYPT, CKA_NSS_MESSAGE, |
| 10 |
CKG_GENERATE_COUNTER_XOR, CKG_NO_GENERATE, CKM_AES_GCM, CKM_CHACHA20_POLY1305, |
| 11 |
CK_ATTRIBUTE_TYPE, CK_GENERATOR_FUNCTION, CK_MECHANISM_TYPE, |
| 12 |
}; |
| 13 |
use crate::secstatus_to_res; |
| 14 |
use crate::SECItemBorrowed; |
| 15 |
use crate::{Error, SymKey}; |
| 16 |
use std::convert::TryFrom; |
| 17 |
use std::convert::TryInto; |
| 18 |
use std::mem; |
| 19 |
use std::os::raw::c_int; |
| 20 |
|
| 21 |
/// All the nonces are the same length. Exploit that. |
| 22 |
pub const NONCE_LEN: usize = 12; |
| 23 |
|
| 24 |
/// The portion of the nonce that is a counter. |
| 25 |
const COUNTER_LEN: usize = mem::size_of::<SequenceNumber>(); |
| 26 |
|
| 27 |
/// The NSS API insists on us identifying the tag separately, which is awful. |
| 28 |
/// All of the AEAD functions here have a tag of this length, so use a fixed offset. |
| 29 |
const TAG_LEN: usize = 16; |
| 30 |
|
| 31 |
pub type SequenceNumber = u64; |
| 32 |
|
| 33 |
/// All the lengths used by `PK11_AEADOp` are signed. This converts to that. |
| 34 |
fn c_int_len<T>(l: T) -> c_int |
| 35 |
where |
| 36 |
T: TryInto<c_int>, |
| 37 |
T::Error: std::error::Error, |
| 38 |
{ |
| 39 |
l.try_into().unwrap() |
| 40 |
} |
| 41 |
|
| 42 |
#[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 43 |
pub enum Mode { |
| 44 |
Encrypt, |
| 45 |
Decrypt, |
| 46 |
} |
| 47 |
|
| 48 |
impl Mode { |
| 49 |
fn p11mode(self) -> CK_ATTRIBUTE_TYPE { |
| 50 |
CK_ATTRIBUTE_TYPE::from( |
| 51 |
CKA_NSS_MESSAGE |
| 52 |
| match self { |
| 53 |
Self::Encrypt => CKA_ENCRYPT, |
| 54 |
Self::Decrypt => CKA_DECRYPT, |
| 55 |
}, |
| 56 |
) |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
#[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 61 |
pub enum AeadAlgorithms { |
| 62 |
Aes128Gcm, |
| 63 |
Aes256Gcm, |
| 64 |
ChaCha20Poly1305, |
| 65 |
} |
| 66 |
|
| 67 |
pub struct Aead { |
| 68 |
mode: Mode, |
| 69 |
ctx: Context, |
| 70 |
nonce_base: [u8; NONCE_LEN], |
| 71 |
} |
| 72 |
|
| 73 |
impl Aead { |
| 74 |
fn mech(algorithm: AeadAlgorithms) -> p11::CK_MECHANISM_TYPE { |
| 75 |
CK_MECHANISM_TYPE::from(match algorithm { |
| 76 |
crate::aead::AeadAlgorithms::Aes128Gcm | crate::aead::AeadAlgorithms::Aes256Gcm => { |
| 77 |
CKM_AES_GCM |
| 78 |
} |
| 79 |
crate::aead::AeadAlgorithms::ChaCha20Poly1305 => CKM_CHACHA20_POLY1305, |
| 80 |
}) |
| 81 |
} |
| 82 |
|
| 83 |
pub fn import_key(algorithm: AeadAlgorithms, key: &[u8]) -> Result<SymKey, Error> { |
| 84 |
let slot = p11::Slot::internal().map_err(|_| crate::Error::InternalError)?; |
| 85 |
|
| 86 |
let key_item = SECItemBorrowed::wrap(key); |
| 87 |
let key_item_ptr = key_item.as_ref() as *const _ as *mut _; |
| 88 |
|
| 89 |
let ptr = unsafe { |
| 90 |
p11::PK11_ImportSymKey( |
| 91 |
*slot, |
| 92 |
Self::mech(algorithm), |
| 93 |
p11::PK11Origin::PK11_OriginUnwrap, |
| 94 |
p11::CK_ATTRIBUTE_TYPE::from(p11::CKA_ENCRYPT | p11::CKA_DECRYPT), |
| 95 |
key_item_ptr, |
| 96 |
std::ptr::null_mut(), |
| 97 |
) |
| 98 |
}; |
| 99 |
unsafe { SymKey::from_ptr(ptr) } |
| 100 |
} |
| 101 |
|
| 102 |
pub fn new( |
| 103 |
mode: Mode, |
| 104 |
algorithm: AeadAlgorithms, |
| 105 |
key: &SymKey, |
| 106 |
nonce_base: [u8; NONCE_LEN], |
| 107 |
) -> Result<Self, crate::Error> { |
| 108 |
crate::init(); |
| 109 |
|
| 110 |
let ptr = unsafe { |
| 111 |
PK11_CreateContextBySymKey( |
| 112 |
Self::mech(algorithm), |
| 113 |
mode.p11mode(), |
| 114 |
**key, |
| 115 |
SECItemBorrowed::wrap(&nonce_base[..]).as_ref(), |
| 116 |
) |
| 117 |
}; |
| 118 |
Ok(Self { |
| 119 |
mode, |
| 120 |
ctx: unsafe { Context::from_ptr(ptr)? }, |
| 121 |
nonce_base, |
| 122 |
}) |
| 123 |
} |
| 124 |
|
| 125 |
pub fn seal(&mut self, aad: &[u8], pt: &[u8]) -> Result<Vec<u8>, crate::Error> { |
| 126 |
crate::init(); |
| 127 |
|
| 128 |
assert_eq!(self.mode, Mode::Encrypt); |
| 129 |
// A copy for the nonce generator to write into. But we don't use the value. |
| 130 |
let mut nonce = self.nonce_base; |
| 131 |
// Ciphertext with enough space for the tag. |
| 132 |
// Even though we give the operation a separate buffer for the tag, |
| 133 |
// reserve the capacity on allocation. |
| 134 |
let mut ct = vec![0; pt.len() + TAG_LEN]; |
| 135 |
let mut ct_len: c_int = 0; |
| 136 |
let mut tag = vec![0; TAG_LEN]; |
| 137 |
secstatus_to_res(unsafe { |
| 138 |
PK11_AEADOp( |
| 139 |
*self.ctx, |
| 140 |
CK_GENERATOR_FUNCTION::from(CKG_GENERATE_COUNTER_XOR), |
| 141 |
c_int_len(NONCE_LEN - COUNTER_LEN), // Fixed portion of the nonce. |
| 142 |
nonce.as_mut_ptr(), |
| 143 |
c_int_len(nonce.len()), |
| 144 |
aad.as_ptr(), |
| 145 |
c_int_len(aad.len()), |
| 146 |
ct.as_mut_ptr(), |
| 147 |
&mut ct_len, |
| 148 |
c_int_len(ct.len()), // signed :( |
| 149 |
tag.as_mut_ptr(), |
| 150 |
c_int_len(tag.len()), |
| 151 |
pt.as_ptr(), |
| 152 |
c_int_len(pt.len()), |
| 153 |
) |
| 154 |
})?; |
| 155 |
ct.truncate(usize::try_from(ct_len).unwrap()); |
| 156 |
debug_assert_eq!(ct.len(), pt.len()); |
| 157 |
ct.append(&mut tag); |
| 158 |
Ok(ct) |
| 159 |
} |
| 160 |
|
| 161 |
pub fn open( |
| 162 |
&mut self, |
| 163 |
aad: &[u8], |
| 164 |
seq: SequenceNumber, |
| 165 |
ct: &[u8], |
| 166 |
) -> Result<Vec<u8>, crate::Error> { |
| 167 |
crate::init(); |
| 168 |
|
| 169 |
assert_eq!(self.mode, Mode::Decrypt); |
| 170 |
let mut nonce = self.nonce_base; |
| 171 |
for (i, n) in nonce.iter_mut().rev().take(COUNTER_LEN).enumerate() { |
| 172 |
*n ^= u8::try_from((seq >> (8 * i)) & 0xff).unwrap(); |
| 173 |
} |
| 174 |
let mut pt = vec![0; ct.len()]; // NSS needs more space than it uses for plaintext. |
| 175 |
let mut pt_len: c_int = 0; |
| 176 |
let pt_expected = ct.len().checked_sub(TAG_LEN).ok_or(Error::AeadTruncated)?; |
| 177 |
secstatus_to_res(unsafe { |
| 178 |
PK11_AEADOp( |
| 179 |
*self.ctx, |
| 180 |
CK_GENERATOR_FUNCTION::from(CKG_NO_GENERATE), |
| 181 |
c_int_len(NONCE_LEN - COUNTER_LEN), // Fixed portion of the nonce. |
| 182 |
nonce.as_mut_ptr(), |
| 183 |
c_int_len(nonce.len()), |
| 184 |
aad.as_ptr(), |
| 185 |
c_int_len(aad.len()), |
| 186 |
pt.as_mut_ptr(), |
| 187 |
&mut pt_len, |
| 188 |
c_int_len(pt.len()), |
| 189 |
ct.as_ptr().add(pt_expected) as *mut _, |
| 190 |
c_int_len(TAG_LEN), |
| 191 |
ct.as_ptr(), |
| 192 |
c_int_len(pt_expected), |
| 193 |
) |
| 194 |
})?; |
| 195 |
let len = usize::try_from(pt_len).unwrap(); |
| 196 |
debug_assert_eq!(len, pt_expected); |
| 197 |
pt.truncate(len); |
| 198 |
Ok(pt) |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
#[cfg(test)] |
| 203 |
mod test { |
| 204 |
use super::{super::init, Aead, AeadAlgorithms, Mode, SequenceNumber, NONCE_LEN}; |
| 205 |
|
| 206 |
/// Check that the first invocation of encryption matches expected values. |
| 207 |
/// Also check decryption of the same. |
| 208 |
fn check0( |
| 209 |
algorithm: AeadAlgorithms, |
| 210 |
key: &[u8], |
| 211 |
nonce: &[u8; NONCE_LEN], |
| 212 |
aad: &[u8], |
| 213 |
pt: &[u8], |
| 214 |
ct: &[u8], |
| 215 |
) { |
| 216 |
init(); |
| 217 |
let k = Aead::import_key(algorithm, key).unwrap(); |
| 218 |
|
| 219 |
let mut enc = Aead::new(Mode::Encrypt, algorithm, &k, *nonce).unwrap(); |
| 220 |
let ciphertext = enc.seal(aad, pt).unwrap(); |
| 221 |
assert_eq!(&ciphertext[..], ct); |
| 222 |
|
| 223 |
let mut dec = Aead::new(Mode::Decrypt, algorithm, &k, *nonce).unwrap(); |
| 224 |
let plaintext = dec.open(aad, 0, ct).unwrap(); |
| 225 |
assert_eq!(&plaintext[..], pt); |
| 226 |
} |
| 227 |
|
| 228 |
fn decrypt( |
| 229 |
algorithm: AeadAlgorithms, |
| 230 |
key: &[u8], |
| 231 |
nonce: &[u8; NONCE_LEN], |
| 232 |
seq: SequenceNumber, |
| 233 |
aad: &[u8], |
| 234 |
pt: &[u8], |
| 235 |
ct: &[u8], |
| 236 |
) { |
| 237 |
let k = Aead::import_key(algorithm, key).unwrap(); |
| 238 |
let mut dec = Aead::new(Mode::Decrypt, algorithm, &k, *nonce).unwrap(); |
| 239 |
let plaintext = dec.open(aad, seq, ct).unwrap(); |
| 240 |
assert_eq!(&plaintext[..], pt); |
| 241 |
} |
| 242 |
|
| 243 |
/// This tests the AEAD in QUIC in combination with the HKDF code. |
| 244 |
/// This is an AEAD-only example. |
| 245 |
#[test] |
| 246 |
fn quic_retry() { |
| 247 |
const KEY: &[u8] = &[ |
| 248 |
0xbe, 0x0c, 0x69, 0x0b, 0x9f, 0x66, 0x57, 0x5a, 0x1d, 0x76, 0x6b, 0x54, 0xe3, 0x68, |
| 249 |
0xc8, 0x4e, |
| 250 |
]; |
| 251 |
const NONCE: &[u8; NONCE_LEN] = &[ |
| 252 |
0x46, 0x15, 0x99, 0xd3, 0x5d, 0x63, 0x2b, 0xf2, 0x23, 0x98, 0x25, 0xbb, |
| 253 |
]; |
| 254 |
const AAD: &[u8] = &[ |
| 255 |
0x08, 0x83, 0x94, 0xc8, 0xf0, 0x3e, 0x51, 0x57, 0x08, 0xff, 0x00, 0x00, 0x00, 0x01, |
| 256 |
0x00, 0x08, 0xf0, 0x67, 0xa5, 0x50, 0x2a, 0x42, 0x62, 0xb5, 0x74, 0x6f, 0x6b, 0x65, |
| 257 |
0x6e, |
| 258 |
]; |
| 259 |
const CT: &[u8] = &[ |
| 260 |
0x04, 0xa2, 0x65, 0xba, 0x2e, 0xff, 0x4d, 0x82, 0x90, 0x58, 0xfb, 0x3f, 0x0f, 0x24, |
| 261 |
0x96, 0xba, |
| 262 |
]; |
| 263 |
check0(AeadAlgorithms::Aes128Gcm, KEY, NONCE, AAD, &[], CT); |
| 264 |
} |
| 265 |
|
| 266 |
#[test] |
| 267 |
fn quic_server_initial() { |
| 268 |
const ALG: AeadAlgorithms = AeadAlgorithms::Aes128Gcm; |
| 269 |
const KEY: &[u8] = &[ |
| 270 |
0xcf, 0x3a, 0x53, 0x31, 0x65, 0x3c, 0x36, 0x4c, 0x88, 0xf0, 0xf3, 0x79, 0xb6, 0x06, |
| 271 |
0x7e, 0x37, |
| 272 |
]; |
| 273 |
const NONCE_BASE: &[u8; NONCE_LEN] = &[ |
| 274 |
0x0a, 0xc1, 0x49, 0x3c, 0xa1, 0x90, 0x58, 0x53, 0xb0, 0xbb, 0xa0, 0x3e, |
| 275 |
]; |
| 276 |
// Note that this integrates the sequence number of 1 from the example, |
| 277 |
// otherwise we can't use a sequence number of 0 to encrypt. |
| 278 |
const NONCE: &[u8; NONCE_LEN] = &[ |
| 279 |
0x0a, 0xc1, 0x49, 0x3c, 0xa1, 0x90, 0x58, 0x53, 0xb0, 0xbb, 0xa0, 0x3f, |
| 280 |
]; |
| 281 |
const AAD: &[u8] = &[ |
| 282 |
0xc1, 0x00, 0x00, 0x00, 0x01, 0x00, 0x08, 0xf0, 0x67, 0xa5, 0x50, 0x2a, 0x42, 0x62, |
| 283 |
0xb5, 0x00, 0x40, 0x75, 0x00, 0x01, |
| 284 |
]; |
| 285 |
const PT: &[u8] = &[ |
| 286 |
0x02, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x40, 0x5a, 0x02, 0x00, 0x00, 0x56, 0x03, |
| 287 |
0x03, 0xee, 0xfc, 0xe7, 0xf7, 0xb3, 0x7b, 0xa1, 0xd1, 0x63, 0x2e, 0x96, 0x67, 0x78, |
| 288 |
0x25, 0xdd, 0xf7, 0x39, 0x88, 0xcf, 0xc7, 0x98, 0x25, 0xdf, 0x56, 0x6d, 0xc5, 0x43, |
| 289 |
0x0b, 0x9a, 0x04, 0x5a, 0x12, 0x00, 0x13, 0x01, 0x00, 0x00, 0x2e, 0x00, 0x33, 0x00, |
| 290 |
0x24, 0x00, 0x1d, 0x00, 0x20, 0x9d, 0x3c, 0x94, 0x0d, 0x89, 0x69, 0x0b, 0x84, 0xd0, |
| 291 |
0x8a, 0x60, 0x99, 0x3c, 0x14, 0x4e, 0xca, 0x68, 0x4d, 0x10, 0x81, 0x28, 0x7c, 0x83, |
| 292 |
0x4d, 0x53, 0x11, 0xbc, 0xf3, 0x2b, 0xb9, 0xda, 0x1a, 0x00, 0x2b, 0x00, 0x02, 0x03, |
| 293 |
0x04, |
| 294 |
]; |
| 295 |
const CT: &[u8] = &[ |
| 296 |
0x5a, 0x48, 0x2c, 0xd0, 0x99, 0x1c, 0xd2, 0x5b, 0x0a, 0xac, 0x40, 0x6a, 0x58, 0x16, |
| 297 |
0xb6, 0x39, 0x41, 0x00, 0xf3, 0x7a, 0x1c, 0x69, 0x79, 0x75, 0x54, 0x78, 0x0b, 0xb3, |
| 298 |
0x8c, 0xc5, 0xa9, 0x9f, 0x5e, 0xde, 0x4c, 0xf7, 0x3c, 0x3e, 0xc2, 0x49, 0x3a, 0x18, |
| 299 |
0x39, 0xb3, 0xdb, 0xcb, 0xa3, 0xf6, 0xea, 0x46, 0xc5, 0xb7, 0x68, 0x4d, 0xf3, 0x54, |
| 300 |
0x8e, 0x7d, 0xde, 0xb9, 0xc3, 0xbf, 0x9c, 0x73, 0xcc, 0x3f, 0x3b, 0xde, 0xd7, 0x4b, |
| 301 |
0x56, 0x2b, 0xfb, 0x19, 0xfb, 0x84, 0x02, 0x2f, 0x8e, 0xf4, 0xcd, 0xd9, 0x37, 0x95, |
| 302 |
0xd7, 0x7d, 0x06, 0xed, 0xbb, 0x7a, 0xaf, 0x2f, 0x58, 0x89, 0x18, 0x50, 0xab, 0xbd, |
| 303 |
0xca, 0x3d, 0x20, 0x39, 0x8c, 0x27, 0x64, 0x56, 0xcb, 0xc4, 0x21, 0x58, 0x40, 0x7d, |
| 304 |
0xd0, 0x74, 0xee, |
| 305 |
]; |
| 306 |
check0(ALG, KEY, NONCE, AAD, PT, CT); |
| 307 |
decrypt(ALG, KEY, NONCE_BASE, 1, AAD, PT, CT); |
| 308 |
} |
| 309 |
|
| 310 |
#[test] |
| 311 |
fn quic_chacha() { |
| 312 |
const ALG: AeadAlgorithms = AeadAlgorithms::ChaCha20Poly1305; |
| 313 |
const KEY: &[u8] = &[ |
| 314 |
0xc6, 0xd9, 0x8f, 0xf3, 0x44, 0x1c, 0x3f, 0xe1, 0xb2, 0x18, 0x20, 0x94, 0xf6, 0x9c, |
| 315 |
0xaa, 0x2e, 0xd4, 0xb7, 0x16, 0xb6, 0x54, 0x88, 0x96, 0x0a, 0x7a, 0x98, 0x49, 0x79, |
| 316 |
0xfb, 0x23, 0xe1, 0xc8, |
| 317 |
]; |
| 318 |
const NONCE_BASE: &[u8; NONCE_LEN] = &[ |
| 319 |
0xe0, 0x45, 0x9b, 0x34, 0x74, 0xbd, 0xd0, 0xe4, 0x4a, 0x41, 0xc1, 0x44, |
| 320 |
]; |
| 321 |
// Note that this integrates the sequence number of 654360564 from the example, |
| 322 |
// otherwise we can't use a sequence number of 0 to encrypt. |
| 323 |
const NONCE: &[u8; NONCE_LEN] = &[ |
| 324 |
0xe0, 0x45, 0x9b, 0x34, 0x74, 0xbd, 0xd0, 0xe4, 0x6d, 0x41, 0x7e, 0xb0, |
| 325 |
]; |
| 326 |
const AAD: &[u8] = &[0x42, 0x00, 0xbf, 0xf4]; |
| 327 |
const PT: &[u8] = &[0x01]; |
| 328 |
const CT: &[u8] = &[ |
| 329 |
0x65, 0x5e, 0x5c, 0xd5, 0x5c, 0x41, 0xf6, 0x90, 0x80, 0x57, 0x5d, 0x79, 0x99, 0xc2, |
| 330 |
0x5a, 0x5b, 0xfb, |
| 331 |
]; |
| 332 |
check0(ALG, KEY, NONCE, AAD, PT, CT); |
| 333 |
// Now use the real nonce and sequence number from the example. |
| 334 |
decrypt(ALG, KEY, NONCE_BASE, 654_360_564, AAD, PT, CT); |
| 335 |
} |
| 336 |
} |