1818use crate :: Result ;
1919
2020/// Algorithm for computing digests
21- #[ derive( Debug , Ord , PartialOrd , Eq , PartialEq ) ]
21+ #[ derive( Debug , Ord , PartialOrd , Eq , PartialEq , Clone , Copy ) ]
2222#[ non_exhaustive]
2323pub enum DigestAlgorithm {
2424 /// SHA-256
2525 Sha256 ,
2626}
2727
2828/// Algorithm for signing payloads
29- #[ derive( Debug , Ord , PartialOrd , Eq , PartialEq ) ]
29+ #[ derive( Debug , Ord , PartialOrd , Eq , PartialEq , Clone , Copy ) ]
3030#[ non_exhaustive]
3131pub enum SigningAlgorithm {
3232 /// RSASSA-PKCS1-v1_5 using SHA-256
@@ -35,42 +35,16 @@ pub enum SigningAlgorithm {
3535
3636/// Provides cryptographic primitives
3737pub trait CryptoProvider : std:: fmt:: Debug + Send + Sync {
38- /// Compute a digest
39- fn digest ( & self , algorithm : DigestAlgorithm ) -> Result < Box < dyn DigestContext > > ;
38+ /// Compute the digest of `data`
39+ fn digest ( & self , algorithm : DigestAlgorithm , data : & [ & [ u8 ] ] ) -> Result < Vec < u8 > > ;
4040
41- /// Compute an HMAC with the provided `secret`
42- fn hmac ( & self , algorithm : DigestAlgorithm , secret : & [ u8 ] ) -> Result < Box < dyn HmacContext > > ;
41+ /// Compute the HMAC of `data` with the provided `secret`
42+ fn hmac ( & self , algorithm : DigestAlgorithm , secret : & [ u8 ] , data : & [ u8 ] ) -> Result < Vec < u8 > > ;
4343
4444 /// Sign a payload with the provided PEM-encoded secret
4545 fn sign ( & self , algorithm : SigningAlgorithm , pem : & [ u8 ] ) -> Result < Box < dyn Signer > > ;
4646}
4747
48- /// Incrementally compute a digest, see [`CryptoProvider::digest`]
49- pub trait DigestContext : Send {
50- /// Updates the digest with all the data in data.
51- ///
52- /// It is implementation-defined behaviour to call this after calling [`Self::finish`]
53- fn update ( & mut self , data : & [ u8 ] ) ;
54-
55- /// Finalizes the digest calculation and returns the digest value.
56- ///
57- /// It is implementation-defined behaviour to call this after calling [`Self::finish`]
58- fn finish ( & mut self ) -> Result < & [ u8 ] > ;
59- }
60-
61- /// Incrementally compute a HMAC, see [`CryptoProvider::hmac`]
62- pub trait HmacContext : Send {
63- /// Updates the HMAC with all the data in data.
64- ///
65- /// It is implementation-defined behaviour to call this after calling [`Self::finish`]
66- fn update ( & mut self , data : & [ u8 ] ) ;
67-
68- /// Finalizes the HMAC calculation and returns the HMAC value.
69- ///
70- /// It is implementation-defined behaviour to call this after calling [`Self::finish`]
71- fn finish ( & mut self ) -> Result < & [ u8 ] > ;
72- }
73-
7448/// Sign a payload, see [`CryptoProvider::sign`]
7549pub trait Signer : Send + Sync {
7650 /// Sign the provided payload
@@ -150,26 +124,24 @@ pub(crate) mod ring {
150124 }
151125
152126 impl CryptoProvider for RingCryptoProvider {
153- fn digest ( & self , algorithm : DigestAlgorithm ) -> Result < Box < dyn DigestContext > > {
127+ fn digest ( & self , algorithm : DigestAlgorithm , data : & [ & [ u8 ] ] ) -> Result < Vec < u8 > > {
154128 let algorithm = match algorithm {
155129 DigestAlgorithm :: Sha256 => & digest:: SHA256 ,
156130 } ;
157- let ctx = digest:: Context :: new ( algorithm) ;
158- Ok ( Box :: new ( RingDigestContext {
159- ctx : Some ( ctx ) ,
160- out : None ,
161- } ) )
131+ let mut ctx = digest:: Context :: new ( algorithm) ;
132+ for chunk in data {
133+ ctx. update ( chunk ) ;
134+ }
135+ Ok ( ctx . finish ( ) . as_ref ( ) . to_vec ( ) )
162136 }
163137
164- fn hmac ( & self , algorithm : DigestAlgorithm , secret : & [ u8 ] ) -> Result < Box < dyn HmacContext > > {
138+ fn hmac ( & self , algorithm : DigestAlgorithm , secret : & [ u8 ] , data : & [ u8 ] ) -> Result < Vec < u8 > > {
165139 let algorithm = match algorithm {
166140 DigestAlgorithm :: Sha256 => hmac:: HMAC_SHA256 ,
167141 } ;
168142 let ctx = hmac:: Context :: with_key ( & hmac:: Key :: new ( algorithm, secret) ) ;
169- Ok ( Box :: new ( RingHmacContext {
170- ctx : Some ( ctx) ,
171- out : None ,
172- } ) )
143+ ctx. update ( data) ;
144+ Ok ( ctx. sign ( ) . as_ref ( ) . to_vec ( ) )
173145 }
174146
175147 fn sign ( & self , algorithm : SigningAlgorithm , pem : & [ u8 ] ) -> Result < Box < dyn Signer > > {
@@ -179,38 +151,6 @@ pub(crate) mod ring {
179151 }
180152 }
181153
182- struct RingDigestContext {
183- ctx : Option < digest:: Context > ,
184- out : Option < digest:: Digest > ,
185- }
186-
187- impl DigestContext for RingDigestContext {
188- fn update ( & mut self , data : & [ u8 ] ) {
189- self . ctx . as_mut ( ) . unwrap ( ) . update ( data) ;
190- }
191-
192- fn finish ( & mut self ) -> Result < & [ u8 ] > {
193- let digest = self . ctx . take ( ) . unwrap ( ) . finish ( ) ;
194- Ok ( digest:: Digest :: as_ref ( self . out . insert ( digest) ) )
195- }
196- }
197-
198- struct RingHmacContext {
199- ctx : Option < hmac:: Context > ,
200- out : Option < hmac:: Tag > ,
201- }
202-
203- impl HmacContext for RingHmacContext {
204- fn update ( & mut self , data : & [ u8 ] ) {
205- self . ctx . as_mut ( ) . unwrap ( ) . update ( data) ;
206- }
207-
208- fn finish ( & mut self ) -> Result < & [ u8 ] > {
209- let tag = self . ctx . take ( ) . unwrap ( ) . sign ( ) ;
210- Ok ( hmac:: Tag :: as_ref ( self . out . insert ( tag) ) )
211- }
212- }
213-
214154 /// A private RSA key for a service account
215155 #[ derive( Debug ) ]
216156 pub ( crate ) struct RsaKeyPair ( signature:: RsaKeyPair ) ;
@@ -302,26 +242,24 @@ pub(crate) mod aws_lc_rs {
302242 }
303243
304244 impl CryptoProvider for AwsLcCryptoProvider {
305- fn digest ( & self , algorithm : DigestAlgorithm ) -> Result < Box < dyn DigestContext > > {
245+ fn digest ( & self , algorithm : DigestAlgorithm , data : & [ & [ u8 ] ] ) -> Result < Vec < u8 > > {
306246 let algorithm = match algorithm {
307247 DigestAlgorithm :: Sha256 => & digest:: SHA256 ,
308248 } ;
309- let ctx = digest:: Context :: new ( algorithm) ;
310- Ok ( Box :: new ( AwsLcDigestContext {
311- ctx : Some ( ctx ) ,
312- out : None ,
313- } ) )
249+ let mut ctx = digest:: Context :: new ( algorithm) ;
250+ for chunk in data {
251+ ctx. update ( chunk ) ;
252+ }
253+ Ok ( ctx . finish ( ) . as_ref ( ) . to_vec ( ) )
314254 }
315255
316- fn hmac ( & self , algorithm : DigestAlgorithm , secret : & [ u8 ] ) -> Result < Box < dyn HmacContext > > {
256+ fn hmac ( & self , algorithm : DigestAlgorithm , secret : & [ u8 ] , data : & [ u8 ] ) -> Result < Vec < u8 > > {
317257 let algorithm = match algorithm {
318258 DigestAlgorithm :: Sha256 => hmac:: HMAC_SHA256 ,
319259 } ;
320- let ctx = hmac:: Context :: with_key ( & hmac:: Key :: new ( algorithm, secret) ) ;
321- Ok ( Box :: new ( AwsLcHmacContext {
322- ctx : Some ( ctx) ,
323- out : None ,
324- } ) )
260+ let mut ctx = hmac:: Context :: with_key ( & hmac:: Key :: new ( algorithm, secret) ) ;
261+ ctx. update ( data) ;
262+ Ok ( ctx. sign ( ) . as_ref ( ) . to_vec ( ) )
325263 }
326264
327265 fn sign ( & self , algorithm : SigningAlgorithm , pem : & [ u8 ] ) -> Result < Box < dyn Signer > > {
@@ -331,38 +269,6 @@ pub(crate) mod aws_lc_rs {
331269 }
332270 }
333271
334- struct AwsLcDigestContext {
335- ctx : Option < digest:: Context > ,
336- out : Option < digest:: Digest > ,
337- }
338-
339- impl DigestContext for AwsLcDigestContext {
340- fn update ( & mut self , data : & [ u8 ] ) {
341- self . ctx . as_mut ( ) . unwrap ( ) . update ( data) ;
342- }
343-
344- fn finish ( & mut self ) -> Result < & [ u8 ] > {
345- let digest = self . ctx . take ( ) . unwrap ( ) . finish ( ) ;
346- Ok ( digest:: Digest :: as_ref ( self . out . insert ( digest) ) )
347- }
348- }
349-
350- struct AwsLcHmacContext {
351- ctx : Option < hmac:: Context > ,
352- out : Option < hmac:: Tag > ,
353- }
354-
355- impl HmacContext for AwsLcHmacContext {
356- fn update ( & mut self , data : & [ u8 ] ) {
357- self . ctx . as_mut ( ) . unwrap ( ) . update ( data) ;
358- }
359-
360- fn finish ( & mut self ) -> Result < & [ u8 ] > {
361- let tag = self . ctx . take ( ) . unwrap ( ) . sign ( ) ;
362- Ok ( hmac:: Tag :: as_ref ( self . out . insert ( tag) ) )
363- }
364- }
365-
366272 /// A private RSA key for a service account
367273 #[ derive( Debug ) ]
368274 pub ( crate ) struct RsaKeyPair ( signature:: RsaKeyPair ) ;
0 commit comments