@@ -10,7 +10,6 @@ mod encrypting_key;
1010pub use self :: { decrypting_key:: DecryptingKey , encrypting_key:: EncryptingKey } ;
1111
1212use alloc:: boxed:: Box ;
13- use alloc:: string:: { String , ToString } ;
1413use alloc:: vec:: Vec ;
1514use core:: fmt;
1615use crypto_bigint:: BoxedUint ;
@@ -44,7 +43,7 @@ pub struct Oaep {
4443 pub mgf_digest : Box < dyn DynDigest + Send + Sync > ,
4544
4645 /// Optional label.
47- pub label : Option < String > ,
46+ pub label : Option < Box < [ u8 ] > > ,
4847}
4948
5049impl Oaep {
@@ -77,13 +76,13 @@ impl Oaep {
7776 }
7877
7978 /// Create a new OAEP `PaddingScheme` with an associated `label`, using `T` as the hash function for both the label and for MGF1.
80- pub fn new_with_label < T : ' static + Digest + DynDigest + Send + Sync , S : AsRef < str > > (
79+ pub fn new_with_label < T : ' static + Digest + DynDigest + Send + Sync , S : Into < Box < [ u8 ] > > > (
8180 label : S ,
8281 ) -> Self {
8382 Self {
8483 digest : Box :: new ( T :: new ( ) ) ,
8584 mgf_digest : Box :: new ( T :: new ( ) ) ,
86- label : Some ( label. as_ref ( ) . to_string ( ) ) ,
85+ label : Some ( label. into ( ) ) ,
8786 }
8887 }
8988
@@ -123,14 +122,14 @@ impl Oaep {
123122 pub fn new_with_mgf_hash_and_label <
124123 T : ' static + Digest + DynDigest + Send + Sync ,
125124 U : ' static + Digest + DynDigest + Send + Sync ,
126- S : AsRef < str > ,
125+ S : Into < Box < [ u8 ] > > ,
127126 > (
128127 label : S ,
129128 ) -> Self {
130129 Self {
131130 digest : Box :: new ( T :: new ( ) ) ,
132131 mgf_digest : Box :: new ( U :: new ( ) ) ,
133- label : Some ( label. as_ref ( ) . to_string ( ) ) ,
132+ label : Some ( label. into ( ) ) ,
134133 }
135134 }
136135}
@@ -193,7 +192,7 @@ fn encrypt<R: CryptoRngCore + ?Sized>(
193192 msg : & [ u8 ] ,
194193 digest : & mut dyn DynDigest ,
195194 mgf_digest : & mut dyn DynDigest ,
196- label : Option < String > ,
195+ label : Option < Box < [ u8 ] > > ,
197196) -> Result < Vec < u8 > > {
198197 key:: check_public ( pub_key) ?;
199198
@@ -214,7 +213,7 @@ fn encrypt_digest<R: CryptoRngCore + ?Sized, D: Digest, MGD: Digest + FixedOutpu
214213 rng : & mut R ,
215214 pub_key : & RsaPublicKey ,
216215 msg : & [ u8 ] ,
217- label : Option < String > ,
216+ label : Option < Box < [ u8 ] > > ,
218217) -> Result < Vec < u8 > > {
219218 key:: check_public ( pub_key) ?;
220219
@@ -243,7 +242,7 @@ fn decrypt<R: CryptoRngCore + ?Sized>(
243242 ciphertext : & [ u8 ] ,
244243 digest : & mut dyn DynDigest ,
245244 mgf_digest : & mut dyn DynDigest ,
246- label : Option < String > ,
245+ label : Option < Box < [ u8 ] > > ,
247246) -> Result < Vec < u8 > > {
248247 if ciphertext. len ( ) != priv_key. size ( ) {
249248 return Err ( Error :: Decryption ) ;
@@ -274,7 +273,7 @@ fn decrypt_digest<R: CryptoRngCore + ?Sized, D: Digest, MGD: Digest + FixedOutpu
274273 rng : Option < & mut R > ,
275274 priv_key : & RsaPrivateKey ,
276275 ciphertext : & [ u8 ] ,
277- label : Option < String > ,
276+ label : Option < Box < [ u8 ] > > ,
278277) -> Result < Vec < u8 > > {
279278 key:: check_public ( priv_key) ?;
280279
@@ -296,7 +295,6 @@ mod tests {
296295 use crate :: traits:: PublicKeyParts ;
297296 use crate :: traits:: { Decryptor , RandomizedDecryptor , RandomizedEncryptor } ;
298297
299- use alloc:: string:: String ;
300298 use crypto_bigint:: { BoxedUint , Odd } ;
301299 use digest:: { Digest , DynDigest , FixedOutputReset } ;
302300 use rand_chacha:: {
@@ -369,18 +367,12 @@ mod tests {
369367 do_test_oaep_with_different_hashes :: < Sha3_512 , Sha1 > ( & priv_key) ;
370368 }
371369
372- fn get_label ( rng : & mut ChaCha8Rng ) -> Option < String > {
373- const GEN_ASCII_STR_CHARSET : & [ u8 ; 64 ] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ\
374- abcdefghijklmnopqrstuvwxyz\
375- 0123456789=+";
376-
370+ fn get_label ( rng : & mut ChaCha8Rng ) -> Option < Box < [ u8 ] > > {
377371 let mut buf = [ 0u8 ; 32 ] ;
378372 rng. fill_bytes ( & mut buf) ;
379- if buf[ 0 ] < ( 1 << 7 ) {
380- for v in buf. iter_mut ( ) {
381- * v = GEN_ASCII_STR_CHARSET [ ( * v >> 2 ) as usize ] ;
382- }
383- Some ( core:: str:: from_utf8 ( & buf) . unwrap ( ) . to_string ( ) )
373+
374+ if rng. next_u32 ( ) % 2 == 0 {
375+ Some ( buf. into ( ) )
384376 } else {
385377 None
386378 }
@@ -405,7 +397,7 @@ mod tests {
405397 let pub_key: RsaPublicKey = prk. into ( ) ;
406398
407399 let ciphertext = if let Some ( ref label) = label {
408- let padding = Oaep :: new_with_label :: < D , _ > ( label) ;
400+ let padding = Oaep :: new_with_label :: < D , _ > ( label. clone ( ) ) ;
409401 pub_key. encrypt ( & mut rng, padding, & input) . unwrap ( )
410402 } else {
411403 let padding = Oaep :: new :: < D > ( ) ;
@@ -415,8 +407,8 @@ mod tests {
415407 assert_ne ! ( input, ciphertext) ;
416408 let blind: bool = rng. next_u32 ( ) < ( 1 << 31 ) ;
417409
418- let padding = if let Some ( ref label) = label {
419- Oaep :: new_with_label :: < D , _ > ( label)
410+ let padding = if let Some ( label) = label {
411+ Oaep :: new_with_label :: < D , Box < [ u8 ] > > ( label)
420412 } else {
421413 Oaep :: new :: < D > ( )
422414 } ;
@@ -453,7 +445,7 @@ mod tests {
453445 let pub_key: RsaPublicKey = prk. into ( ) ;
454446
455447 let ciphertext = if let Some ( ref label) = label {
456- let padding = Oaep :: new_with_mgf_hash_and_label :: < D , U , _ > ( label) ;
448+ let padding = Oaep :: new_with_mgf_hash_and_label :: < D , U , _ > ( label. clone ( ) ) ;
457449 pub_key. encrypt ( & mut rng, padding, & input) . unwrap ( )
458450 } else {
459451 let padding = Oaep :: new_with_mgf_hash :: < D , U > ( ) ;
@@ -463,7 +455,7 @@ mod tests {
463455 assert_ne ! ( input, ciphertext) ;
464456 let blind: bool = rng. next_u32 ( ) < ( 1 << 31 ) ;
465457
466- let padding = if let Some ( ref label) = label {
458+ let padding = if let Some ( label) = label {
467459 Oaep :: new_with_mgf_hash_and_label :: < D , U , _ > ( label)
468460 } else {
469461 Oaep :: new_with_mgf_hash :: < D , U > ( )
@@ -491,7 +483,7 @@ mod tests {
491483 priv_key
492484 . decrypt_blinded(
493485 & mut rng,
494- Oaep :: new_with_label:: <Sha1 , _>( "label" ) ,
486+ Oaep :: new_with_label:: <Sha1 , _>( "label" . as_bytes ( ) ) ,
495487 & ciphertext,
496488 )
497489 . is_err( ) ,
@@ -579,7 +571,7 @@ mod tests {
579571 let priv_key = get_private_key ( ) ;
580572 let pub_key: RsaPublicKey = ( & priv_key) . into ( ) ;
581573 let encrypting_key = EncryptingKey :: < Sha1 > :: new ( pub_key) ;
582- let decrypting_key = DecryptingKey :: < Sha1 > :: new_with_label ( priv_key, "label" ) ;
574+ let decrypting_key = DecryptingKey :: < Sha1 > :: new_with_label ( priv_key, "label" . as_bytes ( ) ) ;
583575 let ciphertext = encrypting_key
584576 . encrypt_with_rng ( & mut rng, "a_plain_text" . as_bytes ( ) )
585577 . unwrap ( ) ;
0 commit comments