| 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 neqo_crypto::{ |
| 8 |
constants::{ |
| 9 |
Cipher, TLS_AES_128_GCM_SHA256, TLS_AES_256_GCM_SHA384, TLS_CHACHA20_POLY1305_SHA256, |
| 10 |
TLS_VERSION_1_3, |
| 11 |
}, |
| 12 |
hkdf, |
| 13 |
hp::HpKey, |
| 14 |
}; |
| 15 |
use test_fixture::fixture_init; |
| 16 |
|
| 17 |
fn make_hp(cipher: Cipher) -> HpKey { |
| 18 |
fixture_init(); |
| 19 |
let ikm = hkdf::import_key(TLS_VERSION_1_3, &[0; 16]).expect("import IKM"); |
| 20 |
let prk = hkdf::extract(TLS_VERSION_1_3, cipher, None, &ikm).expect("extract works"); |
| 21 |
HpKey::extract(TLS_VERSION_1_3, cipher, &prk, "hp").expect("extract label works") |
| 22 |
} |
| 23 |
|
| 24 |
fn hp_test(cipher: Cipher, expected: &[u8]) { |
| 25 |
let hp = make_hp(cipher); |
| 26 |
let mask = hp.mask(&[0; 16]).expect("should produce a mask"); |
| 27 |
assert_eq!(mask, expected, "first invocation should be correct"); |
| 28 |
|
| 29 |
#[allow( |
| 30 |
clippy::allow_attributes, |
| 31 |
clippy::redundant_clone, |
| 32 |
reason = "False positive; remove once MSRV >= 1.88." |
| 33 |
)] |
| 34 |
let hp2 = hp.clone(); |
| 35 |
let mask = hp2.mask(&[0; 16]).expect("clone produces mask"); |
| 36 |
assert_eq!(mask, expected, "clone should produce the same mask"); |
| 37 |
|
| 38 |
let mask = hp.mask(&[0; 16]).expect("should produce a mask again"); |
| 39 |
assert_eq!(mask, expected, "second invocation should be the same"); |
| 40 |
} |
| 41 |
|
| 42 |
#[test] |
| 43 |
fn aes128() { |
| 44 |
const EXPECTED: &[u8] = &[ |
| 45 |
0x04, 0x7b, 0xda, 0x65, 0xc3, 0x41, 0xcf, 0xbc, 0x5d, 0xe1, 0x75, 0x2b, 0x9d, 0x7d, 0xc3, |
| 46 |
0x14, |
| 47 |
]; |
| 48 |
|
| 49 |
hp_test(TLS_AES_128_GCM_SHA256, EXPECTED); |
| 50 |
} |
| 51 |
|
| 52 |
#[test] |
| 53 |
fn aes256() { |
| 54 |
const EXPECTED: &[u8] = &[ |
| 55 |
0xb5, 0xea, 0xa2, 0x1c, 0x25, 0x77, 0x48, 0x18, 0xbf, 0x25, 0xea, 0xfa, 0xbd, 0x8d, 0x80, |
| 56 |
0x2b, |
| 57 |
]; |
| 58 |
|
| 59 |
hp_test(TLS_AES_256_GCM_SHA384, EXPECTED); |
| 60 |
} |
| 61 |
|
| 62 |
#[test] |
| 63 |
fn chacha20_ctr() { |
| 64 |
const EXPECTED: &[u8] = &[ |
| 65 |
0x34, 0x11, 0xb3, 0x53, 0x02, 0x0b, 0x16, 0xda, 0x0a, 0x85, 0x5a, 0x52, 0x0d, 0x06, 0x07, |
| 66 |
0x1f, |
| 67 |
]; |
| 68 |
|
| 69 |
hp_test(TLS_CHACHA20_POLY1305_SHA256, EXPECTED); |
| 70 |
} |
| 71 |
|
| 72 |
#[test] |
| 73 |
#[should_panic(expected = "out of range")] |
| 74 |
fn aes_short() { |
| 75 |
let hp = make_hp(TLS_AES_128_GCM_SHA256); |
| 76 |
drop(hp.mask(&[0; 15])); |
| 77 |
} |
| 78 |
|
| 79 |
#[test] |
| 80 |
#[should_panic(expected = "out of range")] |
| 81 |
fn chacha20_short() { |
| 82 |
let hp = make_hp(TLS_CHACHA20_POLY1305_SHA256); |
| 83 |
drop(hp.mask(&[0; 15])); |
| 84 |
} |