2020// protocol.
2121
2222use crate :: message:: EncryptedData ;
23+ use alloc:: vec:: Vec ;
2324use anyhow:: { anyhow, Context } ;
25+ use core:: convert:: TryInto ;
2426use ring:: {
2527 aead:: { self , BoundKey } ,
2628 agreement,
29+ digest:: { digest, SHA256 } ,
2730 hkdf:: { Salt , HKDF_SHA256 } ,
2831 rand:: { SecureRandom , SystemRandom } ,
2932 signature:: { EcdsaKeyPair , EcdsaSigningAlgorithm , EcdsaVerificationAlgorithm , KeyPair } ,
3033} ;
31- use sha2:: { digest:: Digest , Sha256 } ;
32- use std:: convert:: TryInto ;
3334
3435/// Length of the encryption nonce.
3536/// `ring::aead` uses 96-bit (12-byte) nonces.
@@ -193,7 +194,7 @@ impl KeyNegotiator {
193194 . map_err ( |error| anyhow ! ( "Couldn't get public key: {:?}" , error) ) ?
194195 . as_ref ( )
195196 . to_vec ( ) ;
196- public_key. as_slice ( ) . try_into ( ) . context ( format ! (
197+ public_key. as_slice ( ) . try_into ( ) . context ( alloc :: format!(
197198 "Incorrect public key length, expected {}, found {}" ,
198199 KEY_AGREEMENT_ALGORITHM_KEY_LENGTH ,
199200 public_key. len( )
@@ -234,7 +235,7 @@ impl KeyNegotiator {
234235 & agreement:: UnparsedPublicKey :: new ( KEY_AGREEMENT_ALGORITHM , peer_public_key) ,
235236 anyhow ! ( "Couldn't derive session keys" ) ,
236237 |key_material| {
237- let key_material = key_material. try_into ( ) . context ( format ! (
238+ let key_material = key_material. try_into ( ) . context ( alloc :: format!(
238239 "Incorrect key material length, expected {}, found {}" ,
239240 KEY_AGREEMENT_ALGORITHM_KEY_LENGTH ,
240241 key_material. len( )
@@ -298,7 +299,7 @@ impl KeyNegotiator {
298299 client_public_key : & [ u8 ; KEY_AGREEMENT_ALGORITHM_KEY_LENGTH ] ,
299300 ) -> anyhow:: Result < [ u8 ; AEAD_ALGORITHM_KEY_LENGTH ] > {
300301 // Session key is derived from a purpose string and two public keys.
301- let info = vec ! [ key_purpose. as_bytes( ) , server_public_key, client_public_key] ;
302+ let info = alloc :: vec![ key_purpose. as_bytes( ) , server_public_key, client_public_key] ;
302303
303304 // Initialize key derivation function.
304305 let salt = Salt :: new ( HKDF_SHA256 , KEY_DERIVATION_SALT . as_bytes ( ) ) ;
@@ -339,6 +340,7 @@ pub struct Signer {
339340
340341impl Signer {
341342 pub fn create ( ) -> anyhow:: Result < Self > {
343+ // TODO(#2557): Ensure SystemRandom work when building for x86_64 UEFI targets.
342344 let rng = ring:: rand:: SystemRandom :: new ( ) ;
343345 let key_pair_pkcs8 = EcdsaKeyPair :: generate_pkcs8 ( SIGNING_ALGORITHM , & rng)
344346 . map_err ( |error| anyhow ! ( "Couldn't generate PKCS#8 key pair: {:?}" , error) ) ?;
@@ -350,7 +352,7 @@ impl Signer {
350352
351353 pub fn public_key ( & self ) -> anyhow:: Result < [ u8 ; SIGNING_ALGORITHM_KEY_LENGTH ] > {
352354 let public_key = self . key_pair . public_key ( ) . as_ref ( ) . to_vec ( ) ;
353- public_key. as_slice ( ) . try_into ( ) . context ( format ! (
355+ public_key. as_slice ( ) . try_into ( ) . context ( alloc :: format!(
354356 "Incorrect public key length, expected {}, found {}" ,
355357 SIGNING_ALGORITHM_KEY_LENGTH ,
356358 public_key. len( )
@@ -365,7 +367,7 @@ impl Signer {
365367 . map_err ( |error| anyhow ! ( "Couldn't sign input: {:?}" , error) ) ?
366368 . as_ref ( )
367369 . to_vec ( ) ;
368- signature. as_slice ( ) . try_into ( ) . context ( format ! (
370+ signature. as_slice ( ) . try_into ( ) . context ( alloc :: format!(
369371 "Incorrect signature length, expected {}, found {}" ,
370372 SIGNATURE_LENGTH ,
371373 signature. len( )
@@ -397,11 +399,8 @@ impl SignatureVerifier {
397399
398400/// Computes a SHA-256 digest of `input` and returns it in a form of raw bytes.
399401pub fn get_sha256 ( input : & [ u8 ] ) -> [ u8 ; SHA256_HASH_LENGTH ] {
400- let mut hasher = Sha256 :: new ( ) ;
401- hasher. update ( & input) ;
402- hasher
403- . finalize ( )
404- . as_slice ( )
402+ digest ( & SHA256 , input)
403+ . as_ref ( )
405404 . try_into ( )
406405 . expect ( "Incorrect SHA-256 hash length" )
407406}
0 commit comments