| 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, SymKey, |
| 13 |
}; |
| 14 |
use test_fixture::fixture_init; |
| 15 |
|
| 16 |
const SALT: &[u8] = &[ |
| 17 |
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
| 18 |
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, |
| 19 |
0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, |
| 20 |
]; |
| 21 |
|
| 22 |
const IKM: &[u8] = &[ |
| 23 |
0x01, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
| 24 |
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, |
| 25 |
0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, |
| 26 |
]; |
| 27 |
|
| 28 |
const SESSION_HASH: &[u8] = &[ |
| 29 |
0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, |
| 30 |
0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, |
| 31 |
0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, |
| 32 |
0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, |
| 33 |
]; |
| 34 |
|
| 35 |
fn cipher_hash_len(cipher: Cipher) -> usize { |
| 36 |
match cipher { |
| 37 |
TLS_AES_128_GCM_SHA256 | TLS_CHACHA20_POLY1305_SHA256 => 32, |
| 38 |
TLS_AES_256_GCM_SHA384 => 48, |
| 39 |
_ => unreachable!(), |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
fn import_keys(cipher: Cipher) -> (SymKey, SymKey) { |
| 44 |
let l = cipher_hash_len(cipher); |
| 45 |
( |
| 46 |
hkdf::import_key(TLS_VERSION_1_3, &SALT[0..l]).expect("import salt"), |
| 47 |
hkdf::import_key(TLS_VERSION_1_3, &IKM[0..l]).expect("import IKM"), |
| 48 |
) |
| 49 |
} |
| 50 |
|
| 51 |
fn extract(cipher: Cipher, expected: &[u8]) { |
| 52 |
fixture_init(); |
| 53 |
let (salt, ikm) = import_keys(cipher); |
| 54 |
let prk = hkdf::extract(TLS_VERSION_1_3, cipher, Some(&salt), &ikm) |
| 55 |
.expect("HKDF Extract should work"); |
| 56 |
let raw_prk = prk.as_bytes().expect("key should have bytes"); |
| 57 |
assert_eq!(raw_prk, expected); |
| 58 |
} |
| 59 |
|
| 60 |
#[test] |
| 61 |
fn extract_sha256() { |
| 62 |
const EXPECTED: &[u8] = &[ |
| 63 |
0xa5, 0x68, 0x02, 0x5a, 0x95, 0xc9, 0x7f, 0x55, 0x38, 0xbc, 0xf7, 0x97, 0xcc, 0x0f, 0xd5, |
| 64 |
0xf6, 0xa8, 0x8d, 0x15, 0xbc, 0x0e, 0x85, 0x74, 0x70, 0x3c, 0xa3, 0x65, 0xbd, 0x76, 0xcf, |
| 65 |
0x9f, 0xd3, |
| 66 |
]; |
| 67 |
extract(TLS_AES_128_GCM_SHA256, EXPECTED); |
| 68 |
extract(TLS_CHACHA20_POLY1305_SHA256, EXPECTED); |
| 69 |
} |
| 70 |
|
| 71 |
#[test] |
| 72 |
fn extract_sha384() { |
| 73 |
extract( |
| 74 |
TLS_AES_256_GCM_SHA384, |
| 75 |
&[ |
| 76 |
0x01, 0x93, 0xc0, 0x07, 0x3f, 0x6a, 0x83, 0x0e, 0x2e, 0x4f, 0xb2, 0x58, 0xe4, 0x00, |
| 77 |
0x08, 0x5c, 0x68, 0x9c, 0x37, 0x32, 0x00, 0x37, 0xff, 0xc3, 0x1c, 0x5b, 0x98, 0x0b, |
| 78 |
0x02, 0x92, 0x3f, 0xfd, 0x73, 0x5a, 0x6f, 0x2a, 0x95, 0xa3, 0xee, 0xf6, 0xd6, 0x8e, |
| 79 |
0x6f, 0x86, 0xea, 0x63, 0xf8, 0x33, |
| 80 |
], |
| 81 |
); |
| 82 |
} |
| 83 |
|
| 84 |
fn derive_secret(cipher: Cipher, expected: &[u8]) { |
| 85 |
fixture_init(); |
| 86 |
|
| 87 |
// Here we only use the salt as the PRK. |
| 88 |
let (prk, _) = import_keys(cipher); |
| 89 |
let secret = hkdf::expand_label(TLS_VERSION_1_3, cipher, &prk, &[], "master secret") |
| 90 |
.expect("HKDF-Expand-Label should work"); |
| 91 |
let raw_secret = secret.as_bytes().expect("key should have bytes"); |
| 92 |
assert_eq!(raw_secret, expected); |
| 93 |
} |
| 94 |
|
| 95 |
#[test] |
| 96 |
fn derive_secret_sha256() { |
| 97 |
const EXPECTED: &[u8] = &[ |
| 98 |
0xb7, 0x08, 0x00, 0xe3, 0x8e, 0x48, 0x68, 0x91, 0xb1, 0x0f, 0x5e, 0x6f, 0x22, 0x53, 0x6b, |
| 99 |
0x84, 0x69, 0x75, 0xaa, 0xa3, 0x2a, 0xe7, 0xde, 0xaa, 0xc3, 0xd1, 0xb4, 0x05, 0x22, 0x5c, |
| 100 |
0x68, 0xf5, |
| 101 |
]; |
| 102 |
derive_secret(TLS_AES_128_GCM_SHA256, EXPECTED); |
| 103 |
derive_secret(TLS_CHACHA20_POLY1305_SHA256, EXPECTED); |
| 104 |
} |
| 105 |
|
| 106 |
#[test] |
| 107 |
fn derive_secret_sha384() { |
| 108 |
derive_secret( |
| 109 |
TLS_AES_256_GCM_SHA384, |
| 110 |
&[ |
| 111 |
0x13, 0xd3, 0x36, 0x9f, 0x3c, 0x78, 0xa0, 0x32, 0x40, 0xee, 0x16, 0xe9, 0x11, 0x12, |
| 112 |
0x66, 0xc7, 0x51, 0xad, 0xd8, 0x3c, 0xa1, 0xa3, 0x97, 0x74, 0xd7, 0x45, 0xff, 0xa7, |
| 113 |
0x88, 0x9e, 0x52, 0x17, 0x2e, 0xaa, 0x3a, 0xd2, 0x35, 0xd8, 0xd5, 0x35, 0xfd, 0x65, |
| 114 |
0x70, 0x9f, 0xa9, 0xf9, 0xfa, 0x23, |
| 115 |
], |
| 116 |
); |
| 117 |
} |
| 118 |
|
| 119 |
fn expand_label(cipher: Cipher, expected: &[u8]) { |
| 120 |
fixture_init(); |
| 121 |
|
| 122 |
let l = cipher_hash_len(cipher); |
| 123 |
let (prk, _) = import_keys(cipher); |
| 124 |
let secret = hkdf::expand_label( |
| 125 |
TLS_VERSION_1_3, |
| 126 |
cipher, |
| 127 |
&prk, |
| 128 |
&SESSION_HASH[0..l], |
| 129 |
"master secret", |
| 130 |
) |
| 131 |
.expect("HKDF-Expand-Label should work"); |
| 132 |
let raw_secret = secret.as_bytes().expect("key should have bytes"); |
| 133 |
assert_eq!(raw_secret, expected); |
| 134 |
} |
| 135 |
|
| 136 |
#[test] |
| 137 |
fn expand_label_sha256() { |
| 138 |
const EXPECTED: &[u8] = &[ |
| 139 |
0x3e, 0x4e, 0x6e, 0xd0, 0xbc, 0xc4, 0xf4, 0xff, 0xf0, 0xf5, 0x69, 0xd0, 0x6c, 0x1e, 0x0e, |
| 140 |
0x10, 0x32, 0xaa, 0xd7, 0xa3, 0xef, 0xf6, 0xa8, 0x65, 0x8e, 0xbe, 0xee, 0xc7, 0x1f, 0x01, |
| 141 |
0x6d, 0x3c, |
| 142 |
]; |
| 143 |
expand_label(TLS_AES_128_GCM_SHA256, EXPECTED); |
| 144 |
expand_label(TLS_CHACHA20_POLY1305_SHA256, EXPECTED); |
| 145 |
} |
| 146 |
|
| 147 |
#[test] |
| 148 |
fn expand_label_sha384() { |
| 149 |
expand_label( |
| 150 |
TLS_AES_256_GCM_SHA384, |
| 151 |
&[ |
| 152 |
0x41, 0xea, 0x77, 0x09, 0x8c, 0x90, 0x04, 0x10, 0xec, 0xbc, 0x37, 0xd8, 0x5b, 0x54, |
| 153 |
0xcd, 0x7b, 0x08, 0x15, 0x13, 0x20, 0xed, 0x1e, 0x3f, 0x54, 0x74, 0xf7, 0x8b, 0x06, |
| 154 |
0x38, 0x28, 0x06, 0x37, 0x75, 0x23, 0xa2, 0xb7, 0x34, 0xb1, 0x72, 0x2e, 0x59, 0x6d, |
| 155 |
0x5a, 0x31, 0xf5, 0x53, 0xab, 0x99, |
| 156 |
], |
| 157 |
); |
| 158 |
} |