@@ -41,57 +41,81 @@ impl <Ids: KeyIds, Key: KeyId, T: CompositeEncryptable<Ids, Key, Output>, Output
4141 }
4242}
4343
44- /// An encryption operation that takes the input value - a primitive such as `Vec<u8>`, `String` - and encrypts it into the output value.
45- pub trait Encryptable < Ids : KeyIds , Key : KeyId , Output > {
44+ /// An encryption operation that takes the input value - a primitive such as `String` and encrypts it into the output value.
45+ /// The implementation decides the content format.
46+ pub trait TypedEncryptable < Ids : KeyIds , Key : KeyId , Output > {
4647 fn encrypt (
4748 & self ,
4849 ctx : & mut KeyStoreContext < Ids > ,
4950 key : Key ,
50- content_format : ContentFormat ,
5151 ) -> Result < Output , CryptoError > ;
5252}
5353
54- impl < Ids : KeyIds > Encryptable < Ids , Ids :: Symmetric , EncString > for & [ u8 ] {
54+ impl < Ids : KeyIds , Key : KeyId , T : TypedEncryptable < Ids , Key , Output > , Output >
55+ TypedEncryptable < Ids , Key , Option < Output > > for Option < T >
56+ {
57+ fn encrypt (
58+ & self ,
59+ ctx : & mut KeyStoreContext < Ids > ,
60+ key : Key ,
61+ ) -> Result < Option < Output > , CryptoError > {
62+ self . as_ref ( )
63+ . map ( |value| value. encrypt ( ctx, key) )
64+ . transpose ( )
65+ }
66+ }
67+
68+ impl < Ids : KeyIds > TypedEncryptable < Ids , Ids :: Symmetric , EncString > for & str
69+ {
5570 fn encrypt (
5671 & self ,
5772 ctx : & mut KeyStoreContext < Ids > ,
5873 key : Ids :: Symmetric ,
59- content_format : ContentFormat ,
6074 ) -> Result < EncString , CryptoError > {
61- ctx . encrypt_data_with_symmetric_key ( key , self , content_format )
75+ self . as_bytes ( ) . encrypt ( ctx , key , ContentFormat :: Utf8 )
6276 }
6377}
6478
65- impl < Ids : KeyIds > Encryptable < Ids , Ids :: Symmetric , EncString > for Vec < u8 > {
79+ impl < Ids : KeyIds > TypedEncryptable < Ids , Ids :: Symmetric , EncString > for String
80+ {
6681 fn encrypt (
6782 & self ,
6883 ctx : & mut KeyStoreContext < Ids > ,
6984 key : Ids :: Symmetric ,
70- content_format : ContentFormat ,
7185 ) -> Result < EncString , CryptoError > {
72- ctx . encrypt_data_with_symmetric_key ( key , self , content_format )
86+ self . as_bytes ( ) . encrypt ( ctx , key , ContentFormat :: Utf8 )
7387 }
7488}
7589
76- impl < Ids : KeyIds > Encryptable < Ids , Ids :: Symmetric , EncString > for & str {
90+ /// An encryption operation that takes the input value - a primitive such as `Vec<u8>` - and encrypts it into the output value.
91+ pub trait Encryptable < Ids : KeyIds , Key : KeyId , Output > {
92+ fn encrypt (
93+ & self ,
94+ ctx : & mut KeyStoreContext < Ids > ,
95+ key : Key ,
96+ content_format : ContentFormat ,
97+ ) -> Result < Output , CryptoError > ;
98+ }
99+
100+ impl < Ids : KeyIds > Encryptable < Ids , Ids :: Symmetric , EncString > for & [ u8 ] {
77101 fn encrypt (
78102 & self ,
79103 ctx : & mut KeyStoreContext < Ids > ,
80104 key : Ids :: Symmetric ,
81105 content_format : ContentFormat ,
82106 ) -> Result < EncString , CryptoError > {
83- self . as_bytes ( ) . encrypt ( ctx , key , content_format)
107+ ctx . encrypt_data_with_symmetric_key ( key , self , content_format)
84108 }
85109}
86110
87- impl < Ids : KeyIds > Encryptable < Ids , Ids :: Symmetric , EncString > for String {
111+ impl < Ids : KeyIds > Encryptable < Ids , Ids :: Symmetric , EncString > for Vec < u8 > {
88112 fn encrypt (
89113 & self ,
90114 ctx : & mut KeyStoreContext < Ids > ,
91115 key : Ids :: Symmetric ,
92- content_format : crate :: cose :: ContentFormat ,
116+ content_format : ContentFormat ,
93117 ) -> Result < EncString , CryptoError > {
94- self . as_bytes ( ) . encrypt ( ctx , key , content_format)
118+ ctx . encrypt_data_with_symmetric_key ( key , self , content_format)
95119 }
96120}
97121
@@ -128,7 +152,7 @@ impl<Ids: KeyIds, Key: KeyId, T: Encryptable<Ids, Key, Output>, Output>
128152#[ cfg( test) ]
129153mod tests {
130154 use crate :: {
131- cose:: ContentFormat , traits:: tests:: * , AsymmetricCryptoKey , Decryptable , Encryptable , KeyStore , SymmetricCryptoKey
155+ cose:: ContentFormat , traits:: tests:: * , AsymmetricCryptoKey , Decryptable , Encryptable , KeyStore , SymmetricCryptoKey , TypedEncryptable
132156 } ;
133157
134158 fn test_store ( ) -> KeyStore < TestIds > {
@@ -184,10 +208,10 @@ mod tests {
184208 let str_data: & str = string_data. as_str ( ) ;
185209
186210 let string_encrypted = string_data
187- . encrypt ( & mut ctx, key, ContentFormat :: OctetStream )
211+ . encrypt ( & mut ctx, key)
188212 . unwrap ( ) ;
189213 let str_encrypted = str_data
190- . encrypt ( & mut ctx, key, ContentFormat :: OctetStream )
214+ . encrypt ( & mut ctx, key)
191215 . unwrap ( ) ;
192216
193217 let string_decrypted: String = string_encrypted. decrypt ( & mut ctx, key) . unwrap ( ) ;
@@ -206,7 +230,7 @@ mod tests {
206230 let string_data = Some ( "Hello, World!" . to_string ( ) ) ;
207231
208232 let string_encrypted = string_data
209- . encrypt ( & mut ctx, key, ContentFormat :: OctetStream )
233+ . encrypt ( & mut ctx, key)
210234 . unwrap ( ) ;
211235
212236 let string_decrypted: Option < String > = string_encrypted. decrypt ( & mut ctx, key) . unwrap ( ) ;
@@ -222,15 +246,15 @@ mod tests {
222246 let key = TestSymmKey :: A ( 0 ) ;
223247 let none_data: Option < String > = None ;
224248 let string_encrypted = none_data
225- . encrypt ( & mut ctx, key, ContentFormat :: OctetStream )
249+ . encrypt ( & mut ctx, key)
226250 . unwrap ( ) ;
227251 assert_eq ! ( string_encrypted, None ) ;
228252
229253 // The None implementation will not do any decrypt operations, so it won't fail even if the
230254 // key doesn't exist
231255 let bad_key = TestSymmKey :: B ( ( 0 , 1 ) ) ;
232256 let string_encrypted_bad = none_data
233- . encrypt ( & mut ctx, bad_key, ContentFormat :: OctetStream )
257+ . encrypt ( & mut ctx, bad_key)
234258 . unwrap ( ) ;
235259 assert_eq ! ( string_encrypted_bad, None ) ;
236260 }
0 commit comments