Debian Codegraph @

third_party/rust/neqo-crypto/src/hkdf.rs

raw
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 std::{
8 os::raw::{c_char, c_uint},
9 ptr::null_mut,
10 };
11
12 use crate::{
13 constants::{
14 Cipher, Version, TLS_AES_128_GCM_SHA256, TLS_AES_256_GCM_SHA384,
15 TLS_CHACHA20_POLY1305_SHA256, TLS_VERSION_1_3,
16 },
17 err::{Error, Res},
18 p11::{
19 Item, PK11Origin, PK11SymKey, PK11_ImportDataKey, Slot, SymKey, CKA_DERIVE,
20 CKM_HKDF_DERIVE, CK_ATTRIBUTE_TYPE, CK_MECHANISM_TYPE,
21 },
22 random,
23 };
24
25 experimental_api!(SSL_HkdfExtract(
26 version: Version,
27 cipher: Cipher,
28 salt: *mut PK11SymKey,
29 ikm: *mut PK11SymKey,
30 prk: *mut *mut PK11SymKey,
31 ));
32 experimental_api!(SSL_HkdfExpandLabel(
33 version: Version,
34 cipher: Cipher,
35 prk: *mut PK11SymKey,
36 handshake_hash: *const u8,
37 handshake_hash_len: c_uint,
38 label: *const c_char,
39 label_len: c_uint,
40 secret: *mut *mut PK11SymKey,
41 ));
42
43 const MAX_KEY_SIZE: usize = 48;
44 const fn key_size(version: Version, cipher: Cipher) -> Res<usize> {
45 if version != TLS_VERSION_1_3 {
46 return Err(Error::UnsupportedVersion);
47 }
48 let size = match cipher {
49 TLS_AES_128_GCM_SHA256 | TLS_CHACHA20_POLY1305_SHA256 => 32,
50 TLS_AES_256_GCM_SHA384 => 48,
51 _ => return Err(Error::UnsupportedCipher),
52 };
53 debug_assert!(size <= MAX_KEY_SIZE);
54 Ok(size)
55 }
56
57 /// Generate a random key of the right size for the given suite.
58 ///
59 /// # Errors
60 ///
61 /// If the ciphersuite or protocol version is not supported.
62 pub fn generate_key(version: Version, cipher: Cipher) -> Res<SymKey> {
63 // With generic_const_expr, this becomes:
64 // import_key(version, &random::<{ key_size(version, cipher) }>())
65 import_key(
66 version,
67 &random::<MAX_KEY_SIZE>()[0..key_size(version, cipher)?],
68 )
69 }
70
71 /// Import a symmetric key for use with HKDF.
72 ///
73 /// # Errors
74 ///
75 /// Errors returned if the key buffer is an incompatible size or the NSS functions fail.
76 pub fn import_key(version: Version, buf: &[u8]) -> Res<SymKey> {
77 if version != TLS_VERSION_1_3 {
78 return Err(Error::UnsupportedVersion);
79 }
80 let slot = Slot::internal()?;
81 let key_ptr = unsafe {
82 PK11_ImportDataKey(
83 *slot,
84 CK_MECHANISM_TYPE::from(CKM_HKDF_DERIVE),
85 PK11Origin::PK11_OriginUnwrap,
86 CK_ATTRIBUTE_TYPE::from(CKA_DERIVE),
87 &mut Item::wrap(buf)?,
88 null_mut(),
89 )
90 };
91 SymKey::from_ptr(key_ptr)
92 }
93
94 /// Extract a PRK from the given salt and IKM using the algorithm defined in RFC 5869.
95 ///
96 /// # Errors
97 ///
98 /// Errors returned if inputs are too large or the NSS functions fail.
99 pub fn extract(
100 version: Version,
101 cipher: Cipher,
102 salt: Option<&SymKey>,
103 ikm: &SymKey,
104 ) -> Res<SymKey> {
105 let mut prk: *mut PK11SymKey = null_mut();
106 let salt_ptr: *mut PK11SymKey = salt.map_or(null_mut(), |s| **s);
107 unsafe { SSL_HkdfExtract(version, cipher, salt_ptr, **ikm, &mut prk) }?;
108 SymKey::from_ptr(prk)
109 }
110
111 /// Expand a PRK using the HKDF-Expand-Label function defined in RFC 8446.
112 ///
113 /// # Errors
114 ///
115 /// Errors returned if inputs are too large or the NSS functions fail.
116 pub fn expand_label(
117 version: Version,
118 cipher: Cipher,
119 prk: &SymKey,
120 handshake_hash: &[u8],
121 label: &str,
122 ) -> Res<SymKey> {
123 let l = label.as_bytes();
124 let mut secret: *mut PK11SymKey = null_mut();
125
126 // Note that this doesn't allow for passing null() for the handshake hash.
127 // A zero-length slice produces an identical result.
128 unsafe {
129 SSL_HkdfExpandLabel(
130 version,
131 cipher,
132 **prk,
133 handshake_hash.as_ptr(),
134 c_uint::try_from(handshake_hash.len())?,
135 l.as_ptr().cast(),
136 c_uint::try_from(l.len())?,
137 &mut secret,
138 )
139 }?;
140 SymKey::from_ptr(secret)
141 }