Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
main ▾

Modern-Apps.git / third_party / betocore / np_adv / src / credential / v1.rs

v1.rs461 lines · 18.1 KiBRaw
1// Copyright 2023 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//! Cryptographic materials for v1 advertisement-format credentials.
16
17use crate::credential::{protocol_version_seal, DiscoveryMetadataCryptoMaterial, ProtocolVersion};
18use crate::extended::V1IdentityToken;
19use crypto_provider::{aead::Aead, aes, CryptoProvider};
20use np_hkdf::{DerivedSectionKeys, NpKeySeedHkdf};
21
22/// Cryptographic information about a particular V1 discovery credential
23/// necessary to match and decrypt encrypted V1 sections.
24#[derive(Clone, Debug, PartialEq, Eq)]
25pub struct V1DiscoveryCredential {
26 /// The 32-byte key-seed used for generating other key material.
27 pub key_seed: [u8; 32],
28
29 /// The MIC-short-salt variant of the HMAC of the identity token.
30 pub expected_mic_short_salt_identity_token_hmac: [u8; 32],
31
32 /// The MIC-extended-salt variant of the HMAC of the identity token.
33 pub expected_mic_extended_salt_identity_token_hmac: [u8; 32],
34}
35
36impl V1DiscoveryCredential {
37 /// Construct a V1 discovery credential from the provided identity data.
38 pub fn new(
39 key_seed: [u8; 32],
40 expected_mic_short_salt_identity_token_hmac: [u8; 32],
41 expected_mic_extended_salt_identity_token_hmac: [u8; 32],
42 ) -> Self {
43 Self {
44 key_seed,
45 expected_mic_short_salt_identity_token_hmac,
46 expected_mic_extended_salt_identity_token_hmac,
47 }
48 }
49
50 /// Constructs pre-calculated crypto material from this discovery credential.
51 pub(crate) fn to_precalculated<C: CryptoProvider>(
52 &self,
53 ) -> PrecalculatedV1DiscoveryCryptoMaterial {
54 PrecalculatedV1DiscoveryCryptoMaterial {
55 mic_short_salt_identity_resolution_material: self
56 .mic_short_salt_identity_resolution_material::<C>(),
57 mic_extended_salt_identity_resolution_material: self
58 .mic_extended_salt_identity_resolution_material::<C>(),
59 mic_short_salt_verification_material: self.mic_short_salt_verification_material::<C>(),
60 mic_extended_salt_verification_material: self
61 .mic_extended_salt_verification_material::<C>(),
62 metadata_nonce: self.metadata_nonce::<C>(),
63 }
64 }
65}
66
67impl DiscoveryMetadataCryptoMaterial<V1> for V1DiscoveryCredential {
68 fn metadata_nonce<C: CryptoProvider>(&self) -> [u8; 12] {
69 np_hkdf::NpKeySeedHkdf::<C>::new(&self.key_seed).v1_metadata_nonce()
70 }
71}
72
73impl V1DiscoveryCryptoMaterial for V1DiscoveryCredential {
74 fn mic_short_salt_identity_resolution_material<C: CryptoProvider>(
75 &self,
76 ) -> MicShortSaltSectionIdentityResolutionMaterial {
77 let hkdf = np_hkdf::NpKeySeedHkdf::<C>::new(&self.key_seed);
78 MicShortSaltSectionIdentityResolutionMaterial::from_hkdf_and_expected_identity_token_hmac(
79 &hkdf,
80 self.expected_mic_short_salt_identity_token_hmac,
81 )
82 }
83
84 fn mic_extended_salt_identity_resolution_material<C: CryptoProvider>(
85 &self,
86 ) -> MicExtendedSaltSectionIdentityResolutionMaterial {
87 let hkdf = np_hkdf::NpKeySeedHkdf::<C>::new(&self.key_seed);
88 MicExtendedSaltSectionIdentityResolutionMaterial::from_hkdf_and_expected_identity_token_hmac(
89 &hkdf,
90 self.expected_mic_extended_salt_identity_token_hmac,
91 )
92 }
93
94 fn mic_short_salt_verification_material<C: CryptoProvider>(
95 &self,
96 ) -> MicShortSaltSectionVerificationMaterial {
97 let hkdf = np_hkdf::NpKeySeedHkdf::<C>::new(&self.key_seed);
98 let mic_hmac_key = *hkdf.v1_mic_short_salt_keys().mic_hmac_key().as_bytes();
99 MicShortSaltSectionVerificationMaterial { mic_hmac_key }
100 }
101
102 fn mic_extended_salt_verification_material<C: CryptoProvider>(
103 &self,
104 ) -> MicExtendedSaltSectionVerificationMaterial {
105 let hkdf = np_hkdf::NpKeySeedHkdf::<C>::new(&self.key_seed);
106 let mic_hmac_key = *hkdf.v1_mic_extended_salt_keys().mic_hmac_key().as_bytes();
107 MicExtendedSaltSectionVerificationMaterial { mic_hmac_key }
108 }
109}
110
111/// Type-level identifier for the V1 protocol version.
112#[derive(Debug, Clone)]
113pub enum V1 {}
114
115impl protocol_version_seal::ProtocolVersionSeal for V1 {}
116
117impl ProtocolVersion for V1 {
118 type DiscoveryCredential = V1DiscoveryCredential;
119 type IdentityToken = V1IdentityToken;
120
121 fn metadata_nonce_from_key_seed<C: CryptoProvider>(
122 hkdf: &NpKeySeedHkdf<C>,
123 ) -> <C::Aes128Gcm as Aead>::Nonce {
124 hkdf.v1_metadata_nonce()
125 }
126
127 fn extract_metadata_key<C: CryptoProvider>(
128 identity_token: Self::IdentityToken,
129 ) -> aes::Aes128Key {
130 identity_token.into_bytes().into()
131 }
132}
133
134/// Trait which exists purely to be able to restrict the protocol
135/// version of certain type-bounds to V1.
136pub trait V1ProtocolVersion: ProtocolVersion {}
137
138impl V1ProtocolVersion for V1 {}
139
140/// Cryptographic materials necessary for determining whether or not
141/// a given V1 advertisement section matches an identity.
142/// Per the construction of the V1 specification, this is also
143/// the information necessary to decrypt the raw byte contents
144/// of an encrypted V1 section.
145#[derive(Clone)]
146pub(crate) struct SectionIdentityResolutionMaterial {
147 /// The AES key for decrypting section ciphertext
148 pub(crate) aes_key: aes::Aes128Key,
149 /// The identity token HMAC key for deriving and verifying the identity metadata
150 /// key HMAC against the expected value.
151 pub(crate) identity_token_hmac_key: [u8; 32],
152 /// The expected identity token HMAC to check against for an identity match.
153 pub(crate) expected_identity_token_hmac: [u8; 32],
154}
155
156/// Cryptographic materials necessary for determining whether or not
157/// a given V1 MIC extended salt advertisement section matches an identity.
158#[derive(Clone)]
159pub struct MicExtendedSaltSectionIdentityResolutionMaterial(SectionIdentityResolutionMaterial);
160
161impl MicExtendedSaltSectionIdentityResolutionMaterial {
162 /// Extracts the underlying section-identity resolution material carried around
163 /// within this wrapper for resolution of MIC extended salt sections.
164 pub(crate) fn into_raw_resolution_material(self) -> SectionIdentityResolutionMaterial {
165 self.0
166 }
167 /// Gets the underlying section-identity resolution material carried around
168 /// within this wrapper for resolution of MIC extended salt sections.
169 #[cfg(any(test, feature = "devtools"))]
170 pub(crate) fn as_raw_resolution_material(&self) -> &SectionIdentityResolutionMaterial {
171 &self.0
172 }
173 #[cfg(test)]
174 pub(crate) fn as_mut_raw_resolution_material(
175 &mut self,
176 ) -> &mut SectionIdentityResolutionMaterial {
177 &mut self.0
178 }
179 /// Constructs identity-resolution material for an MIC extended salt section whose
180 /// discovery credential leverages the provided HKDF and has the given
181 /// expected metadata-key HMAC.
182 pub(crate) fn from_hkdf_and_expected_identity_token_hmac<C: CryptoProvider>(
183 hkdf: &np_hkdf::NpKeySeedHkdf<C>,
184 expected_identity_token_hmac: [u8; 32],
185 ) -> Self {
186 Self(SectionIdentityResolutionMaterial {
187 aes_key: hkdf.v1_mic_extended_salt_keys().aes_key(),
188 identity_token_hmac_key: *hkdf
189 .v1_mic_extended_salt_keys()
190 .identity_token_hmac_key()
191 .as_bytes(),
192 expected_identity_token_hmac,
193 })
194 }
195}
196
197/// Cryptographic materials necessary for determining whether
198/// a given V1 MIC short salt advertisement section matches an identity.
199#[derive(Clone)]
200pub struct MicShortSaltSectionIdentityResolutionMaterial(SectionIdentityResolutionMaterial);
201
202impl MicShortSaltSectionIdentityResolutionMaterial {
203 /// Extracts the underlying section-identity resolution material carried around
204 /// within this wrapper for resolution of MIC short salt sections.
205 pub(crate) fn into_raw_resolution_material(self) -> SectionIdentityResolutionMaterial {
206 self.0
207 }
208 /// Gets the underlying section-identity resolution material carried around
209 /// within this wrapper for resolution of MIC short salt sections.
210 #[cfg(any(test, feature = "devtools"))]
211 pub(crate) fn as_raw_resolution_material(&self) -> &SectionIdentityResolutionMaterial {
212 &self.0
213 }
214 #[cfg(test)]
215 pub(crate) fn as_mut_raw_resolution_material(
216 &mut self,
217 ) -> &mut SectionIdentityResolutionMaterial {
218 &mut self.0
219 }
220
221 /// Constructs identity-resolution material for a MIC short salt section whose
222 /// discovery credential leverages the provided HKDF and has the given
223 /// expected metadata-key HMAC.
224 pub(crate) fn from_hkdf_and_expected_identity_token_hmac<C: CryptoProvider>(
225 hkdf: &np_hkdf::NpKeySeedHkdf<C>,
226 expected_identity_token_hmac: [u8; 32],
227 ) -> Self {
228 Self(SectionIdentityResolutionMaterial {
229 aes_key: hkdf.v1_mic_short_salt_keys().aes_key(),
230 identity_token_hmac_key: *hkdf
231 .v1_mic_short_salt_keys()
232 .identity_token_hmac_key()
233 .as_bytes(),
234 expected_identity_token_hmac,
235 })
236 }
237}
238
239/// Crypto materials for V1 MIC short salt sections which are not employed in identity resolution,
240/// but may be necessary to fully decrypt a MIC short salt section.
241#[derive(Clone)]
242pub struct MicShortSaltSectionVerificationMaterial {
243 /// The MIC HMAC key for verifying the integrity of MIC short salt sections.
244 pub(crate) mic_hmac_key: [u8; 32],
245}
246
247impl MicSectionVerificationMaterial for MicShortSaltSectionVerificationMaterial {
248 fn mic_hmac_key(&self) -> np_hkdf::NpHmacSha256Key {
249 self.mic_hmac_key.into()
250 }
251}
252
253/// Crypto materials for V1 MIC extended salt sections which are not employed in identity
254/// resolution, but may be necessary to fully decrypt a MIC extended salt section.
255#[derive(Clone)]
256pub struct MicExtendedSaltSectionVerificationMaterial {
257 /// The MIC HMAC key for verifying the integrity of MIC extended salt sections.
258 pub(crate) mic_hmac_key: [u8; 32],
259}
260
261impl MicSectionVerificationMaterial for MicExtendedSaltSectionVerificationMaterial {
262 fn mic_hmac_key(&self) -> np_hkdf::NpHmacSha256Key {
263 self.mic_hmac_key.into()
264 }
265}
266
267pub(crate) trait MicSectionVerificationMaterial {
268 /// Returns the HMAC key for calculating the MIC of the sections
269 fn mic_hmac_key(&self) -> np_hkdf::NpHmacSha256Key;
270}
271
272// Space-time tradeoffs:
273// - Calculating an HKDF from the key seed costs about 2us on a gLinux laptop, and occupies 80b.
274// - Calculating an AES (16b) or HMAC (32b) key from the HKDF costs about 700ns.
275// The right tradeoff may also vary by use case. For frequently used identities we should
276// probably pre-calculate everything. For occasionally used ones, or ones that are loaded from
277// disk, used once, and discarded, we might want to precalculate on a separate thread or the
278// like.
279// The AES key and metadata key HMAC key are the most frequently used ones, as the MIC HMAC key
280// is only used on the matching identity, not all identities.
281
282/// Cryptographic material for an individual NP credential used to decrypt and verify v1 sections.
283pub trait V1DiscoveryCryptoMaterial: DiscoveryMetadataCryptoMaterial<V1> {
284 /// Constructs or copies the identity resolution material for MIC short salt sections
285 fn mic_short_salt_identity_resolution_material<C: CryptoProvider>(
286 &self,
287 ) -> MicShortSaltSectionIdentityResolutionMaterial;
288
289 /// Constructs or copies the identity resolution material for MIC extended salt sections
290 fn mic_extended_salt_identity_resolution_material<C: CryptoProvider>(
291 &self,
292 ) -> MicExtendedSaltSectionIdentityResolutionMaterial;
293
294 /// Constructs or copies non-identity-resolution deserialization material for MIC short salt
295 /// sections.
296 fn mic_short_salt_verification_material<C: CryptoProvider>(
297 &self,
298 ) -> MicShortSaltSectionVerificationMaterial;
299
300 /// Constructs or copies non-identity-resolution deserialization material for MIC extended salt
301 /// sections.
302 fn mic_extended_salt_verification_material<C: CryptoProvider>(
303 &self,
304 ) -> MicExtendedSaltSectionVerificationMaterial;
305}
306
307/// [`V1DiscoveryCryptoMaterial`] that minimizes CPU time when providing key material at
308/// the expense of occupied memory
309pub struct PrecalculatedV1DiscoveryCryptoMaterial {
310 pub(crate) mic_short_salt_identity_resolution_material:
311 MicShortSaltSectionIdentityResolutionMaterial,
312 pub(crate) mic_extended_salt_identity_resolution_material:
313 MicExtendedSaltSectionIdentityResolutionMaterial,
314 pub(crate) mic_short_salt_verification_material: MicShortSaltSectionVerificationMaterial,
315 pub(crate) mic_extended_salt_verification_material: MicExtendedSaltSectionVerificationMaterial,
316 pub(crate) metadata_nonce: [u8; 12],
317}
318
319impl DiscoveryMetadataCryptoMaterial<V1> for PrecalculatedV1DiscoveryCryptoMaterial {
320 fn metadata_nonce<C: CryptoProvider>(&self) -> [u8; 12] {
321 self.metadata_nonce
322 }
323}
324
325impl V1DiscoveryCryptoMaterial for PrecalculatedV1DiscoveryCryptoMaterial {
326 fn mic_short_salt_identity_resolution_material<C: CryptoProvider>(
327 &self,
328 ) -> MicShortSaltSectionIdentityResolutionMaterial {
329 self.mic_short_salt_identity_resolution_material.clone()
330 }
331
332 fn mic_extended_salt_identity_resolution_material<C: CryptoProvider>(
333 &self,
334 ) -> MicExtendedSaltSectionIdentityResolutionMaterial {
335 self.mic_extended_salt_identity_resolution_material.clone()
336 }
337
338 fn mic_short_salt_verification_material<C: CryptoProvider>(
339 &self,
340 ) -> MicShortSaltSectionVerificationMaterial {
341 self.mic_short_salt_verification_material.clone()
342 }
343
344 fn mic_extended_salt_verification_material<C: CryptoProvider>(
345 &self,
346 ) -> MicExtendedSaltSectionVerificationMaterial {
347 self.mic_extended_salt_verification_material.clone()
348 }
349}
350
351// Implementations for reference types -- we don't provide a blanket impl for references
352// due to the potential to conflict with downstream crates' implementations.
353
354impl DiscoveryMetadataCryptoMaterial<V1> for &V1DiscoveryCredential {
355 fn metadata_nonce<C: CryptoProvider>(&self) -> [u8; 12] {
356 (*self).metadata_nonce::<C>()
357 }
358}
359
360impl V1DiscoveryCryptoMaterial for &V1DiscoveryCredential {
361 fn mic_short_salt_identity_resolution_material<C: CryptoProvider>(
362 &self,
363 ) -> MicShortSaltSectionIdentityResolutionMaterial {
364 (*self).mic_short_salt_identity_resolution_material::<C>()
365 }
366
367 fn mic_extended_salt_identity_resolution_material<C: CryptoProvider>(
368 &self,
369 ) -> MicExtendedSaltSectionIdentityResolutionMaterial {
370 (*self).mic_extended_salt_identity_resolution_material::<C>()
371 }
372
373 fn mic_short_salt_verification_material<C: CryptoProvider>(
374 &self,
375 ) -> MicShortSaltSectionVerificationMaterial {
376 (*self).mic_short_salt_verification_material::<C>()
377 }
378
379 fn mic_extended_salt_verification_material<C: CryptoProvider>(
380 &self,
381 ) -> MicExtendedSaltSectionVerificationMaterial {
382 (*self).mic_extended_salt_verification_material::<C>()
383 }
384}
385
386impl DiscoveryMetadataCryptoMaterial<V1> for &PrecalculatedV1DiscoveryCryptoMaterial {
387 fn metadata_nonce<C: CryptoProvider>(&self) -> [u8; 12] {
388 (*self).metadata_nonce::<C>()
389 }
390}
391
392impl V1DiscoveryCryptoMaterial for &PrecalculatedV1DiscoveryCryptoMaterial {
393 fn mic_short_salt_identity_resolution_material<C: CryptoProvider>(
394 &self,
395 ) -> MicShortSaltSectionIdentityResolutionMaterial {
396 (*self).mic_short_salt_identity_resolution_material::<C>()
397 }
398
399 fn mic_extended_salt_identity_resolution_material<C: CryptoProvider>(
400 &self,
401 ) -> MicExtendedSaltSectionIdentityResolutionMaterial {
402 (*self).mic_extended_salt_identity_resolution_material::<C>()
403 }
404
405 fn mic_short_salt_verification_material<C: CryptoProvider>(
406 &self,
407 ) -> MicShortSaltSectionVerificationMaterial {
408 (*self).mic_short_salt_verification_material::<C>()
409 }
410
411 fn mic_extended_salt_verification_material<C: CryptoProvider>(
412 &self,
413 ) -> MicExtendedSaltSectionVerificationMaterial {
414 (*self).mic_extended_salt_verification_material::<C>()
415 }
416}
417
418/// Crypto material for creating V1 sections.
419#[derive(Clone)]
420pub struct V1BroadcastCredential {
421 /// The 32-byte key-seed used for generating other key material.
422 pub key_seed: [u8; 32],
423
424 /// The 16-byte identity-token which identifies the sender.
425 pub identity_token: V1IdentityToken,
426}
427
428impl V1BroadcastCredential {
429 /// Builds some simple V1 encrypted broadcast crypto-materials out of
430 /// the provided key-seed and identity token.
431 pub fn new(key_seed: [u8; 32], identity_token: V1IdentityToken) -> Self {
432 Self { key_seed, identity_token }
433 }
434
435 /// Key seed from which other keys are derived.
436 pub(crate) fn key_seed(&self) -> [u8; 32] {
437 self.key_seed
438 }
439
440 /// Identity token that will be incorporated into encrypted advertisements.
441 pub(crate) fn identity_token(&self) -> V1IdentityToken {
442 self.identity_token
443 }
444
445 /// Derive a discovery credential with the data necessary to discover advertisements produced
446 /// by this broadcast credential.
447 pub fn derive_discovery_credential<C: CryptoProvider>(&self) -> V1DiscoveryCredential {
448 let key_seed = self.key_seed();
449 let hkdf = np_hkdf::NpKeySeedHkdf::<C>::new(&key_seed);
450
451 V1DiscoveryCredential::new(
452 key_seed,
453 hkdf.v1_mic_short_salt_keys()
454 .identity_token_hmac_key()
455 .calculate_hmac::<C>(self.identity_token.as_slice()),
456 hkdf.v1_mic_extended_salt_keys()
457 .identity_token_hmac_key()
458 .calculate_hmac::<C>(self.identity_token.as_slice()),
459 )
460 }
461}