Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / third_party / betocore / np_hkdf / src / lib.rs
lib.rs262 lines · 9.1 KiBRaw
| 1 | // Copyright 2022 Google LLC |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | //! Wrappers around NP's usage of HKDF. |
| 16 | //! |
| 17 | //! All HKDF calls should happen in this module and expose the correct result type for |
| 18 | //! each derived key use case. |
| 19 | |
| 20 | #![no_std] |
| 21 | |
| 22 | #[cfg(feature = "std")] |
| 23 | extern crate std; |
| 24 | |
| 25 | use crypto_provider::aead::Aead; |
| 26 | use crypto_provider::{aes, aes::Aes128Key, hkdf::Hkdf, hmac::Hmac, CryptoProvider}; |
| 27 | |
| 28 | pub mod v1_salt; |
| 29 | |
| 30 | /// A wrapper around the common NP usage of HMAC-SHA256. |
| 31 | /// |
| 32 | /// These are generally derived via HKDF, but could be used for any HMAC-SHA256 key. |
| 33 | #[derive(Clone, Debug)] |
| 34 | pub struct NpHmacSha256Key { |
| 35 | /// Nearby Presence uses 32-byte HMAC keys. |
| 36 | /// |
| 37 | /// Inside the HMAC algorithm they will be padded to 64 bytes. |
| 38 | key: [u8; 32], |
| 39 | } |
| 40 | |
| 41 | impl NpHmacSha256Key { |
| 42 | /// Build a fresh HMAC instance. |
| 43 | /// |
| 44 | /// Since each HMAC is modified as data is fed to it, HMACs should not be reused. |
| 45 | /// |
| 46 | /// See also [Self::calculate_hmac] for simple use cases. |
| 47 | pub fn build_hmac<C: CryptoProvider>(&self) -> C::HmacSha256 { |
| 48 | C::HmacSha256::new_from_key(self.key) |
| 49 | } |
| 50 | |
| 51 | /// Returns a reference to the underlying key bytes. |
| 52 | pub fn as_bytes(&self) -> &[u8; 32] { |
| 53 | &self.key |
| 54 | } |
| 55 | |
| 56 | /// Build an HMAC, update it with the provided `data`, and finalize it, returning the resulting |
| 57 | /// MAC. This is convenient for one-and-done HMAC usage rather than incrementally accumulating |
| 58 | /// the final MAC. |
| 59 | pub fn calculate_hmac<C: CryptoProvider>(&self, data: &[u8]) -> [u8; 32] { |
| 60 | let mut hmac = self.build_hmac::<C>(); |
| 61 | hmac.update(data); |
| 62 | hmac.finalize() |
| 63 | } |
| 64 | |
| 65 | /// Build an HMAC, update it with the provided `data`, and verify it. |
| 66 | /// |
| 67 | /// This is convenient for one-and-done HMAC usage rather than incrementally accumulating |
| 68 | /// the final MAC. |
| 69 | pub fn verify_hmac<C: CryptoProvider>( |
| 70 | &self, |
| 71 | data: &[u8], |
| 72 | expected_mac: [u8; 32], |
| 73 | ) -> Result<(), crypto_provider::hmac::MacError> { |
| 74 | let mut hmac = self.build_hmac::<C>(); |
| 75 | hmac.update(data); |
| 76 | hmac.verify(expected_mac) |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | impl From<[u8; 32]> for NpHmacSha256Key { |
| 81 | fn from(key: [u8; 32]) -> Self { |
| 82 | Self { key } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /// Salt use for all NP HKDFs |
| 87 | const NP_HKDF_SALT: &[u8] = b"Google Nearby"; |
| 88 | |
| 89 | /// A wrapper around an NP key seed for deriving HKDF-SHA256 sub keys. |
| 90 | pub struct NpKeySeedHkdf<C: CryptoProvider> { |
| 91 | hkdf: NpHkdf<C>, |
| 92 | } |
| 93 | |
| 94 | impl<C: CryptoProvider> NpKeySeedHkdf<C> { |
| 95 | /// Build an HKDF from a NP credential key seed |
| 96 | pub fn new(key_seed: &[u8; 32]) -> Self { |
| 97 | Self { hkdf: NpHkdf::new(key_seed) } |
| 98 | } |
| 99 | |
| 100 | /// LDT key used to decrypt a legacy advertisement |
| 101 | #[allow(clippy::expect_used)] |
| 102 | pub fn v0_ldt_key(&self) -> ldt::LdtKey<xts_aes::XtsAes128Key> { |
| 103 | ldt::LdtKey::from_concatenated( |
| 104 | &self.hkdf.derive_array(b"V0 LDT key").expect("LDT key is a valid length"), |
| 105 | ) |
| 106 | } |
| 107 | |
| 108 | /// HMAC key used when verifying the raw identity token extracted from an advertisement |
| 109 | pub fn v0_identity_token_hmac_key(&self) -> NpHmacSha256Key { |
| 110 | self.hkdf.derive_hmac_sha256_key(b"V0 Identity token verification HMAC key") |
| 111 | } |
| 112 | |
| 113 | /// AES-GCM nonce used when decrypting metadata |
| 114 | #[allow(clippy::expect_used)] |
| 115 | pub fn v0_metadata_nonce(&self) -> <C::Aes128Gcm as Aead>::Nonce { |
| 116 | self.hkdf.derive_array(b"V0 Metadata nonce").expect("Nonce is a valid length") |
| 117 | } |
| 118 | |
| 119 | /// AES-GCM nonce used when decrypting metadata. |
| 120 | #[allow(clippy::expect_used)] |
| 121 | pub fn v1_metadata_nonce(&self) -> <C::Aes128Gcm as Aead>::Nonce { |
| 122 | self.hkdf.derive_array(b"V1 Metadata nonce").expect("Nonce is a valid length") |
| 123 | } |
| 124 | |
| 125 | /// Derived keys for MIC short salt sections |
| 126 | pub fn v1_mic_short_salt_keys(&self) -> MicShortSaltSectionKeys<'_, C> { |
| 127 | MicShortSaltSectionKeys { hkdf: &self.hkdf } |
| 128 | } |
| 129 | |
| 130 | /// Derived keys for MIC extended salt sections |
| 131 | pub fn v1_mic_extended_salt_keys(&self) -> MicExtendedSaltSectionKeys<'_, C> { |
| 132 | MicExtendedSaltSectionKeys { hkdf: &self.hkdf } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | /// Derived keys for MIC short salt sections |
| 137 | pub struct MicShortSaltSectionKeys<'a, C: CryptoProvider> { |
| 138 | hkdf: &'a NpHkdf<C>, |
| 139 | } |
| 140 | |
| 141 | impl<C: CryptoProvider> MicShortSaltSectionKeys<'_, C> { |
| 142 | /// HMAC-SHA256 key used when verifying a section's ciphertext |
| 143 | pub fn mic_hmac_key(&self) -> NpHmacSha256Key { |
| 144 | self.hkdf.derive_hmac_sha256_key(b"MIC Section short salt HMAC key") |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | impl<C: CryptoProvider> DerivedSectionKeys<C> for MicShortSaltSectionKeys<'_, C> { |
| 149 | fn aes_key(&self) -> Aes128Key { |
| 150 | self.hkdf.derive_aes128_key(b"MIC Section short salt AES key") |
| 151 | } |
| 152 | |
| 153 | fn identity_token_hmac_key(&self) -> NpHmacSha256Key { |
| 154 | self.hkdf.derive_hmac_sha256_key(b"MIC Section short salt identity token HMAC key") |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | /// Derived keys for MIC extended salt sections |
| 159 | pub struct MicExtendedSaltSectionKeys<'a, C: CryptoProvider> { |
| 160 | hkdf: &'a NpHkdf<C>, |
| 161 | } |
| 162 | |
| 163 | impl<C: CryptoProvider> MicExtendedSaltSectionKeys<'_, C> { |
| 164 | /// HMAC-SHA256 key used when verifying a section's ciphertext |
| 165 | pub fn mic_hmac_key(&self) -> NpHmacSha256Key { |
| 166 | self.hkdf.derive_hmac_sha256_key(b"MIC Section extended salt HMAC key") |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | impl<C: CryptoProvider> DerivedSectionKeys<C> for MicExtendedSaltSectionKeys<'_, C> { |
| 171 | fn aes_key(&self) -> Aes128Key { |
| 172 | self.hkdf.derive_aes128_key(b"MIC Section extended salt AES key") |
| 173 | } |
| 174 | |
| 175 | fn identity_token_hmac_key(&self) -> NpHmacSha256Key { |
| 176 | self.hkdf.derive_hmac_sha256_key(b"MIC Section extended salt identity token HMAC key") |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | /// Derived keys for encrypted V1 sections |
| 181 | pub trait DerivedSectionKeys<C: CryptoProvider> { |
| 182 | /// AES128 key used when encrypting a section's ciphertext |
| 183 | fn aes_key(&self) -> Aes128Key; |
| 184 | |
| 185 | /// HMAC-SHA256 key used when verifying a section's plaintext identity token |
| 186 | fn identity_token_hmac_key(&self) -> NpHmacSha256Key; |
| 187 | } |
| 188 | |
| 189 | /// Expand a legacy salt into the expanded salt used with XOR padding in LDT. |
| 190 | #[allow(clippy::expect_used)] |
| 191 | pub fn v0_ldt_expanded_salt<C: CryptoProvider>(salt: &[u8; 2]) -> [u8; aes::BLOCK_SIZE] { |
| 192 | simple_np_hkdf_expand::<{ aes::BLOCK_SIZE }, C>(salt, b"V0 LDT salt pad") |
| 193 | // XTS tweak size is the block cipher's block size |
| 194 | .expect("Tweak size is a valid HKDF size") |
| 195 | } |
| 196 | |
| 197 | /// Expand a v0 identity token into an AES128 metadata key. |
| 198 | #[allow(clippy::expect_used)] |
| 199 | pub fn v0_metadata_expanded_key<C: CryptoProvider>(identity_token: &[u8; 14]) -> [u8; 16] { |
| 200 | simple_np_hkdf_expand::<16, C>(identity_token, b"V0 Metadata key expansion") |
| 201 | .expect("AES128 key is a valid HKDF size") |
| 202 | } |
| 203 | |
| 204 | /// Expand an extended MIC section's short salt into an AES-CTR nonce. |
| 205 | #[allow(clippy::expect_used)] |
| 206 | pub fn extended_mic_section_short_salt_nonce<C: CryptoProvider>( |
| 207 | salt: [u8; 2], |
| 208 | ) -> aes::ctr::AesCtrNonce { |
| 209 | simple_np_hkdf_expand::<{ aes::ctr::AES_CTR_NONCE_LEN }, C>( |
| 210 | salt.as_slice(), |
| 211 | b"MIC Section short salt nonce", |
| 212 | ) |
| 213 | .expect("AES-CTR nonce is a valid HKDF size") |
| 214 | } |
| 215 | |
| 216 | /// Build an HKDF using the NP HKDF salt, calculate output, and discard the HKDF. |
| 217 | /// If using the NP key seed as IKM, see [NpKeySeedHkdf] instead. |
| 218 | /// |
| 219 | /// Returns None if the requested size is > 255 * 32 bytes. |
| 220 | fn simple_np_hkdf_expand<const N: usize, C: CryptoProvider>( |
| 221 | ikm: &[u8], |
| 222 | info: &[u8], |
| 223 | ) -> Option<[u8; N]> { |
| 224 | let mut buf = [0; N]; |
| 225 | let hkdf = np_salt_hkdf::<C>(ikm); |
| 226 | hkdf.expand(info, &mut buf[..]).map(|_| buf).ok() |
| 227 | } |
| 228 | |
| 229 | /// Construct an HKDF with the Nearby Presence salt and provided `ikm` |
| 230 | fn np_salt_hkdf<C: CryptoProvider>(ikm: &[u8]) -> C::HkdfSha256 { |
| 231 | C::HkdfSha256::new(Some(NP_HKDF_SALT), ikm) |
| 232 | } |
| 233 | |
| 234 | /// NP-flavored HKDF operations for common derived output types |
| 235 | struct NpHkdf<C: CryptoProvider> { |
| 236 | hkdf: C::HkdfSha256, |
| 237 | } |
| 238 | |
| 239 | impl<C: CryptoProvider> NpHkdf<C> { |
| 240 | /// Build an HKDF using the NP HKDF salt and supplied `ikm` |
| 241 | pub fn new(ikm: &[u8]) -> Self { |
| 242 | Self { hkdf: np_salt_hkdf::<C>(ikm) } |
| 243 | } |
| 244 | |
| 245 | /// Derive a length `N` array using the provided `info` |
| 246 | /// Returns `None` if N > 255 * 32. |
| 247 | pub fn derive_array<const N: usize>(&self, info: &[u8]) -> Option<[u8; N]> { |
| 248 | let mut arr = [0_u8; N]; |
| 249 | self.hkdf.expand(info, &mut arr).map(|_| arr).ok() |
| 250 | } |
| 251 | |
| 252 | /// Derive an HMAC-SHA256 key using the provided `info` |
| 253 | #[allow(clippy::expect_used)] |
| 254 | pub fn derive_hmac_sha256_key(&self, info: &[u8]) -> NpHmacSha256Key { |
| 255 | self.derive_array(info).expect("HMAC-SHA256 keys are a valid length").into() |
| 256 | } |
| 257 | /// Derive an AES-128 key using the provided `info` |
| 258 | #[allow(clippy::expect_used)] |
| 259 | pub fn derive_aes128_key(&self, info: &[u8]) -> Aes128Key { |
| 260 | self.derive_array(info).expect("AES128 keys are a valid length").into() |
| 261 | } |
| 262 | } |