|  | 
|  | 1 | +// Copyright 2019 Contributors to the Parsec project. | 
|  | 2 | +// SPDX-License-Identifier: Apache-2.0 | 
|  | 3 | + | 
|  | 4 | +use aes::cipher::AsyncStreamCipher; | 
|  | 5 | +use cfb_mode::cipher::BlockCipherEncrypt; | 
|  | 6 | +use core::{ | 
|  | 7 | +    marker::PhantomData, | 
|  | 8 | +    ops::{Add, Mul}, | 
|  | 9 | +}; | 
|  | 10 | +use digest::{ | 
|  | 11 | +    array::ArraySize, | 
|  | 12 | +    consts::{B1, U8}, | 
|  | 13 | +    crypto_common::{Iv, KeyIvInit, KeySizeUser}, | 
|  | 14 | +    typenum::{ | 
|  | 15 | +        operator_aliases::{Add1, Sum}, | 
|  | 16 | +        Unsigned, | 
|  | 17 | +    }, | 
|  | 18 | +    Digest, FixedOutputReset, KeyInit, Mac, OutputSizeUser, | 
|  | 19 | +}; | 
|  | 20 | +use ecdsa::elliptic_curve::{ | 
|  | 21 | +    ecdh::{EphemeralSecret, SharedSecret}, | 
|  | 22 | +    sec1::{FromEncodedPoint, ModulusSize, ToEncodedPoint}, | 
|  | 23 | +    AffinePoint, Curve, CurveArithmetic, FieldBytesSize, PublicKey, | 
|  | 24 | +}; | 
|  | 25 | +use hmac::{EagerHash, Hmac}; | 
|  | 26 | +use rand::thread_rng; | 
|  | 27 | + | 
|  | 28 | +use crate::{ | 
|  | 29 | +    structures::{EncryptedSecret, IdObject, Name}, | 
|  | 30 | +    utils::kdf::{self}, | 
|  | 31 | +}; | 
|  | 32 | + | 
|  | 33 | +// TpmHmacKey intends to code for the key expected for hmac | 
|  | 34 | +// in the KDFa derivations. There are no standard sizes for hmac keys really, | 
|  | 35 | +// upstream RustCrypto considers it to be BlockSize, but TPM specification | 
|  | 36 | +// has a different opinion on the matter, and expect the key to the output | 
|  | 37 | +// bit size of the hash algorithm used. | 
|  | 38 | +// | 
|  | 39 | +// See https://trustedcomputinggroup.org/wp-content/uploads/TPM-2.0-1.83-Part-1-Architecture.pdf#page=202 | 
|  | 40 | +// section 24.5 HMAC: | 
|  | 41 | +//   bits the number of bits in the digest produced by ekNameAlg | 
|  | 42 | +struct TpmHmac<H>(PhantomData<H>); | 
|  | 43 | + | 
|  | 44 | +impl<H> KeySizeUser for TpmHmac<H> | 
|  | 45 | +where | 
|  | 46 | +    H: OutputSizeUser, | 
|  | 47 | +{ | 
|  | 48 | +    type KeySize = H::OutputSize; | 
|  | 49 | +} | 
|  | 50 | + | 
|  | 51 | +pub fn make_credential_ecc<C, EkHash, EkCipher>( | 
|  | 52 | +    ek_public: PublicKey<C>, | 
|  | 53 | +    secret: &[u8], | 
|  | 54 | +    key_name: Name, | 
|  | 55 | +) -> (IdObject, EncryptedSecret) | 
|  | 56 | +where | 
|  | 57 | +    C: Curve + CurveArithmetic, | 
|  | 58 | + | 
|  | 59 | +    AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>, | 
|  | 60 | +    FieldBytesSize<C>: ModulusSize, | 
|  | 61 | + | 
|  | 62 | +    <FieldBytesSize<C> as Add>::Output: Add<FieldBytesSize<C>>, | 
|  | 63 | +    Sum<FieldBytesSize<C>, FieldBytesSize<C>>: ArraySize, | 
|  | 64 | +    Sum<FieldBytesSize<C>, FieldBytesSize<C>>: Add<U8>, | 
|  | 65 | +    Sum<Sum<FieldBytesSize<C>, FieldBytesSize<C>>, U8>: Add<B1>, | 
|  | 66 | +    Add1<Sum<Sum<FieldBytesSize<C>, FieldBytesSize<C>>, U8>>: ArraySize, | 
|  | 67 | + | 
|  | 68 | +    EkHash: Digest + EagerHash + FixedOutputReset, | 
|  | 69 | +    <EkHash as OutputSizeUser>::OutputSize: Mul<U8>, | 
|  | 70 | +    <<EkHash as OutputSizeUser>::OutputSize as Mul<U8>>::Output: Unsigned, | 
|  | 71 | +    <<EkHash as EagerHash>::Core as OutputSizeUser>::OutputSize: ArraySize + Mul<U8>, | 
|  | 72 | +    <<<EkHash as EagerHash>::Core as OutputSizeUser>::OutputSize as Mul<U8>>::Output: Unsigned, | 
|  | 73 | + | 
|  | 74 | +    EkCipher: KeySizeUser + BlockCipherEncrypt + KeyInit, | 
|  | 75 | +    <EkCipher as KeySizeUser>::KeySize: Mul<U8>, | 
|  | 76 | +    <<EkCipher as KeySizeUser>::KeySize as Mul<U8>>::Output: ArraySize, | 
|  | 77 | +{ | 
|  | 78 | +    let mut rng = thread_rng(); | 
|  | 79 | + | 
|  | 80 | +    // See Table 22 - Key Generation for the various labels used here after: | 
|  | 81 | +    // https://trustedcomputinggroup.org/wp-content/uploads/TPM-2.0-1.83-Part-1-Architecture.pdf#page=183 | 
|  | 82 | + | 
|  | 83 | +    // C.6.4. ECC Secret Sharing for Credentials | 
|  | 84 | +    // https://trustedcomputinggroup.org/wp-content/uploads/TPM-2.0-1.83-Part-1-Architecture.pdf#page=311 | 
|  | 85 | +    let local = EphemeralSecret::<C>::random(&mut rng); | 
|  | 86 | + | 
|  | 87 | +    let ecdh_secret: SharedSecret<C> = local.diffie_hellman(&ek_public); | 
|  | 88 | +    let local_public = local.public_key(); | 
|  | 89 | +    drop(local); | 
|  | 90 | + | 
|  | 91 | +    let seed = kdf::kdfe::<kdf::Identity, EkHash, C, TpmHmac<EkHash>>( | 
|  | 92 | +        &ecdh_secret, | 
|  | 93 | +        &local_public, | 
|  | 94 | +        &ek_public, | 
|  | 95 | +    ); | 
|  | 96 | +    drop(ecdh_secret); | 
|  | 97 | + | 
|  | 98 | +    // The local ECDH pair is used as "encrypted seed" | 
|  | 99 | +    let encrypted_seed = { | 
|  | 100 | +        let mut out = vec![]; | 
|  | 101 | +        out.extend_from_slice(&FieldBytesSize::<C>::U16.to_be_bytes()[..]); | 
|  | 102 | +        out.extend_from_slice(&local_public.to_encoded_point(false).x().unwrap()); | 
|  | 103 | +        out.extend_from_slice(&FieldBytesSize::<C>::U16.to_be_bytes()[..]); | 
|  | 104 | +        out.extend_from_slice(&local_public.to_encoded_point(false).y().unwrap()); | 
|  | 105 | +        out | 
|  | 106 | +    }; | 
|  | 107 | + | 
|  | 108 | +    // Prepare the sensitive data | 
|  | 109 | +    // this will be then encrypted using AES-CFB (size of the symmetric key depends on the EK). | 
|  | 110 | +    let mut sensitive_data = { | 
|  | 111 | +        let mut out = vec![]; | 
|  | 112 | +        out.extend_from_slice(&u16::try_from(secret.len()).unwrap().to_be_bytes()[..]); | 
|  | 113 | +        out.extend_from_slice(secret); | 
|  | 114 | +        out | 
|  | 115 | +    }; | 
|  | 116 | + | 
|  | 117 | +    // We'll now encrypt the sensitive data, and hmac the result of the encryption | 
|  | 118 | +    // https://trustedcomputinggroup.org/wp-content/uploads/TPM-2.0-1.83-Part-1-Architecture.pdf#page=201 | 
|  | 119 | +    // See 24.4 Symmetric Encryption | 
|  | 120 | +    let sym_key = kdf::kdfa::<EkHash, kdf::Storage, EkCipher>(&seed, key_name.value(), &[]); | 
|  | 121 | + | 
|  | 122 | +    // TODO(baloo): once the weak key detection merges | 
|  | 123 | +    // https://github.com/RustCrypto/traits/pull/1739 | 
|  | 124 | +    // https://github.com/RustCrypto/block-ciphers/pull/465 | 
|  | 125 | +    // | 
|  | 126 | +    // We should check for weak keys, and re-run all the steps above until we get a non-weak key | 
|  | 127 | +    // this is to be in compliance with TPM spec section 11.4.10.4: | 
|  | 128 | +    // https://trustedcomputinggroup.org/wp-content/uploads/TPM-2.0-1.83-Part-1-Architecture.pdf#page=82 | 
|  | 129 | + | 
|  | 130 | +    let iv: Iv<cfb_mode::Encryptor<EkCipher>> = Default::default(); | 
|  | 131 | + | 
|  | 132 | +    cfb_mode::Encryptor::<EkCipher>::new(&sym_key.into(), &iv.into()).encrypt(&mut sensitive_data); | 
|  | 133 | + | 
|  | 134 | +    // See 24.5 HMAC | 
|  | 135 | +    let hmac_key = kdf::kdfa::<EkHash, kdf::Integrity, TpmHmac<EkHash>>(&seed, &[], &[]); | 
|  | 136 | +    let mut hmac = Hmac::<EkHash>::new_from_slice(&hmac_key).unwrap(); | 
|  | 137 | +    hmac.update(&sensitive_data); | 
|  | 138 | +    hmac.update(key_name.value()); | 
|  | 139 | +    let hmac = hmac.finalize(); | 
|  | 140 | + | 
|  | 141 | +    // We'll now serialize the object and get everything through the door. | 
|  | 142 | +    let mut out = vec![]; | 
|  | 143 | +    out.extend_from_slice( | 
|  | 144 | +        &u16::try_from(hmac.into_bytes().len()) | 
|  | 145 | +            .unwrap() | 
|  | 146 | +            .to_be_bytes()[..], | 
|  | 147 | +    ); | 
|  | 148 | +    out.extend_from_slice(&hmac.into_bytes()); | 
|  | 149 | +    out.extend_from_slice(&sensitive_data); | 
|  | 150 | + | 
|  | 151 | +    ( | 
|  | 152 | +        IdObject::from_bytes(&out).unwrap(), | 
|  | 153 | +        EncryptedSecret::from_bytes(&encrypted_seed).unwrap(), | 
|  | 154 | +    ) | 
|  | 155 | +} | 
0 commit comments