//! The pure cryptographic steps of pairing (`docs/PROTOCOL.md` 1.5), public so QA can test them against //! independent vectors (for example Python `hashlib` + `cryptography` with fixed test keys). use aes::Aes128; use aes::cipher::{BlockCipherDecrypt, BlockCipherEncrypt, KeyInit}; use sha2::{Digest, Sha256}; use super::{CHALLENGE_LEN, HASH_LEN, MAX_SIGNATURE_BYTES, Pin, SALT_LEN, SECRET_LEN}; use crate::Result; use crate::util::{cap, malformed}; /// AES-128 key used for every encrypted pairing value. pub type PairingKey = [u8; 16]; /// `K = SHA-256(salt ‖ PIN digits)[0..16]`. /// /// # Errors /// None today; the `Result` keeps the signature uniform with the other steps. pub fn derive_key(salt: &[u8; SALT_LEN], pin: &Pin) -> Result { let mut h = Sha256::new(); h.update(salt); h.update(pin.as_bytes()); let digest = h.finalize(); let mut key = [0u8; 16]; key.copy_from_slice(digest.get(..16).ok_or(malformed("sha-256 output"))?); Ok(key) } fn ecb(key: &PairingKey, data: &[u8], encrypt: bool) -> Result> { if data.is_empty() || !data.len().is_multiple_of(16) { return Err(malformed("aes-ecb input is not whole blocks")); } let cipher = Aes128::new(key.into()); let mut out = data.to_vec(); for chunk in out.as_chunks_mut::<16>().0 { let block: &mut aes::Block = chunk.into(); if encrypt { cipher.encrypt_block(block); } else { cipher.decrypt_block(block); } } Ok(out) } /// AES-128-ECB encryption without padding. /// /// # Errors /// [`crate::Error::Malformed`] when `data` is not a whole, non-zero number of 16-byte blocks. pub fn ecb_encrypt(key: &PairingKey, data: &[u8]) -> Result> { ecb(key, data, true) } /// AES-128-ECB decryption without padding. /// /// # Errors /// As [`ecb_encrypt`]. pub fn ecb_decrypt(key: &PairingKey, data: &[u8]) -> Result> { ecb(key, data, false) } /// `SHA-256(challenge ‖ cert_signature ‖ secret)`: the host's proof `H_s` (with the client challenge, the host /// certificate's signature and the host secret) and the client's proof `H_c` (with the host challenge, the client /// certificate's signature and the client secret). /// /// # Errors /// [`crate::Error::TooLarge`] for a signature over [`MAX_SIGNATURE_BYTES`]. pub fn proof_hash( challenge: &[u8; CHALLENGE_LEN], cert_signature: &[u8], secret: &[u8; SECRET_LEN], ) -> Result<[u8; HASH_LEN]> { cap("certificate signature", MAX_SIGNATURE_BYTES, cert_signature.len())?; let mut h = Sha256::new(); h.update(challenge); h.update(cert_signature); h.update(secret); let mut out = [0u8; HASH_LEN]; out.copy_from_slice(&h.finalize()); Ok(out) } /// Splits a decrypted `challengeresponse` (48 bytes) into the host proof `H_s` and the host challenge `S_c`. /// /// # Errors /// [`crate::Error::Malformed`] unless exactly 48 bytes. pub fn split_challenge_response(decrypted: &[u8]) -> Result<([u8; HASH_LEN], [u8; CHALLENGE_LEN])> { if decrypted.len() != HASH_LEN + CHALLENGE_LEN { return Err(malformed("challengeresponse is not 48 bytes")); } let hash = crate::util::array::("challengeresponse", decrypted, 0)?; let challenge = crate::util::array::("challengeresponse", decrypted, HASH_LEN)?; Ok((hash, challenge)) } /// Splits a `pairingsecret` into the host secret (16 bytes) and its signature (the rest). /// /// # Errors /// [`crate::Error::Malformed`] when shorter than 17 bytes, [`crate::Error::TooLarge`] when the signature is over /// [`MAX_SIGNATURE_BYTES`]. pub fn split_pairing_secret(pairing_secret: &[u8]) -> Result<([u8; SECRET_LEN], &[u8])> { if pairing_secret.len() <= SECRET_LEN { return Err(malformed("pairingsecret too short")); } let signature = pairing_secret.get(SECRET_LEN..).unwrap_or(&[]); cap("pairingsecret signature", MAX_SIGNATURE_BYTES, signature.len())?; let secret = crate::util::array::("pairingsecret", pairing_secret, 0)?; Ok((secret, signature)) }