Repository: google/OpenSK
File: src/ctap/mod.rs (Rust)

Code before:
        let attestation_statement = PackedAttestationStatement {
            alg: SignatureAlgorithm::ES256 as i64,
            sig: signature.to_asn1_der(),
            x5c,
            ecdaa_key_id: None,
        };
        Ok(ResponseData::AuthenticatorMakeCredential(
            AuthenticatorMakeCredentialResponse {
                fmt: String::from("packed"),
                auth_data,
                att_stmt: attestation_statement,
            },
        ))
    }

    // Generates a different per-credential secret for each UV mode.
    // The computation is deterministic, and private_key expected to be unique.
    fn generate_cred_random(
        &mut self,
        private_key: &crypto::ecdsa::SecKey,
        has_uv: bool,
    ) -> Result<[u8; 32], Ctap2StatusCode> {
        let mut private_key_bytes = [0u8; 32];
        private_key.to_bytes(&mut private_key_bytes);
        let key = self.persistent_store.cred_random_secret(has_uv)?;
        Ok(hmac_256::<Sha256>(&key, &private_key_bytes[..]))
    }

    // Processes the input of a get_assertion operation for a given credential
    // and returns the correct Get(Next)Assertion response.
    fn assertion_response(
        &mut self,
        credential: PublicKeyCredentialSource,
        assertion_input: AssertionInput,
        number_of_credentials: Option<usize>,
    ) -> Result<ResponseData, Ctap2StatusCode> {
        let AssertionInput {
            client_data_hash,
            mut auth_data,
            hmac_secret_input,
            has_uv,
        } = assertion_input;

        // Process extensions.
        if let Some(hmac_secret_input) = hmac_secret_input {
            let cred_random = self.generate_cred_random(&credential.private_key, has_uv)?;
            let encrypted_output = self
                .pin_protocol_v1
                .process_hmac_secret(hmac_secret_input, &cred_random)?;
            let extensions_output = cbor_map! {
                "hmac-secret" => encrypted_output,

Review comment (suggestion):
- This should be fine given the assumptions (i.e. `private_key_bytes` was chosen uniformly at random).
- Some nit on formatting: the `[..]` part is not really needed here. The only difference I see is that it creates a `&[u8]` rather than a `&[u8; 32]`, but the conversion to `&[u8]` is implicit IIRC.

Code after:
            sig: signature.to_asn1_der(),
            x5c,
            ecdaa_key_id: None,
        };
        Ok(ResponseData::AuthenticatorMakeCredential(
            AuthenticatorMakeCredentialResponse {
                fmt: String::from("packed"),
                auth_data,
                att_stmt: attestation_statement,
            },
        ))
    }

    // Generates a different per-credential secret for each UV mode.
    // The computation is deterministic, and private_key expected to be unique.
    fn generate_cred_random(
        &mut self,
        private_key: &crypto::ecdsa::SecKey,
        has_uv: bool,
    ) -> Result<[u8; 32], Ctap2StatusCode> {
        let mut private_key_bytes = [0u8; 32];
        private_key.to_bytes(&mut private_key_bytes);
        let key = self.persistent_store.cred_random_secret(has_uv)?;
        Ok(hmac_256::<Sha256>(&key, &private_key_bytes))
    }

    // Processes the input of a get_assertion operation for a given credential
    // and returns the correct Get(Next)Assertion response.
    fn assertion_response(
        &mut self,
        credential: PublicKeyCredentialSource,
        assertion_input: AssertionInput,
        number_of_credentials: Option<usize>,
    ) -> Result<ResponseData, Ctap2StatusCode> {
        let AssertionInput {
            client_data_hash,
            mut auth_data,
            hmac_secret_input,
            has_uv,
        } = assertion_input;

        // Process extensions.
        if let Some(hmac_secret_input) = hmac_secret_input {
            let cred_random = self.generate_cred_random(&credential.private_key, has_uv)?;
            let encrypted_output = self
                .pin_protocol_v1
                .process_hmac_secret(hmac_secret_input, &cred_random)?;
            let extensions_output = cbor_map! {
                "hmac-secret" => encrypted_output,
            };
            if !cbor::write(extensions_output, &mut auth_data) {