usesuper::CryptoError; use aes::cipher::{BlockDecryptMut, BlockEncryptMut, KeyIvInit}; use hmac::Mac; use p256::elliptic_curve::sec1::FromEncodedPoint; use rand_core::RngCore; use sha2::Digest; use std::convert::TryInto;
/// Ephemeral ECDH over P256. Generates an ephemeral P256 key pair. Returns /// 1) the x coordinate of the shared point, and /// 2) the uncompressed SEC 1 encoding of the ephemeral public key. pubfn ecdhe_p256_raw(peer: &super::COSEEC2Key) -> Result<(Vec<u8>, Vec<u8>)> { let peer_public = cose_key_to_public(peer)?;
let internal_private = p256::ecdh::EphemeralSecret::random(&mut rand_core::OsRng); let internal_public = internal_private.public_key().to_sec1_bytes().into_vec();
let shared_point = internal_private.diffie_hellman(&peer_public);
let iv = iv.unwrap_or(&[0u8; AES_BLOCK_SIZE]).into();
// Validate that the data is an exact multiple of the block size since we have no // padding available. let blocks = data.chunks_exact(AES_BLOCK_SIZE); if !blocks.remainder().is_empty() { return Err(CryptoError::LibraryFailure);
}
// Since we now know that `data` is a multiple of `AES_BLOCK_SIZE`, so this will always have the // same number of blocks as it. letmut ciphertext = vec![0u8; data.len()]; // XXX: `slice::as_chunks` would be better but it requires an MSRV of 1.88. for (input_block, output_block) in blocks
.into_iter()
.zip(ciphertext.chunks_exact_mut(AES_BLOCK_SIZE))
{ let input: &[u8; AES_BLOCK_SIZE] = input_block.try_into().unwrap(); let output: &mut [u8; AES_BLOCK_SIZE] = output_block.try_into().unwrap();
let iv = iv.unwrap_or(&[0u8; AES_BLOCK_SIZE]).into();
// See comments in `encrypt_aes_256_cbc_no_pad` for rationale. let blocks = data.chunks_exact(AES_BLOCK_SIZE); if !blocks.remainder().is_empty() { return Err(CryptoError::LibraryFailure);
}
letmut decryptor = Aes256CbcDec::new(&key.into(), iv); letmut plaintext = vec![0u8; data.len()]; for (input_block, output_block) in blocks
.into_iter()
.zip(plaintext.chunks_exact_mut(AES_BLOCK_SIZE))
{ let input: &[u8; AES_BLOCK_SIZE] = input_block.try_into().unwrap(); let output: &mut [u8; AES_BLOCK_SIZE] = output_block.try_into().unwrap();
let client_private = p256::SecretKey::from_slice(client_private).unwrap(); let shared_point =
p256::ecdh::diffie_hellman(client_private.to_nonzero_scalar(), peer_public.as_affine());
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.