@@ -35,18 +35,21 @@ use crate::{Algo, InvalidPubkey, SsiPair, SsiPub, LIB_NAME_SSI};
35
35
36
36
#[ derive( Copy , Clone , Debug , Display , Error ) ]
37
37
pub enum EncryptionError {
38
- #[ display( "the number of receivers exceeds 2^16" ) ]
38
+ #[ display( "the number of receivers exceeds 2^16. " ) ]
39
39
TooManyReceivers ,
40
- #[ display( "invalid public key {0}" ) ]
40
+ #[ display( "invalid public key {0}. " ) ]
41
41
InvalidPubkey ( SsiPub ) ,
42
42
}
43
43
44
- #[ derive( Copy , Clone , Debug , Display , Error ) ]
44
+ #[ derive( Copy , Clone , Debug , Display , Error , From ) ]
45
45
pub enum DecryptionError {
46
- #[ display( "the message can't be decrypted using key {0}" ) ]
46
+ #[ display( "the message can't be decrypted using key {0}. " ) ]
47
47
KeyMismatch ( SsiPub ) ,
48
- #[ display( "invalid public key {0}" ) ]
48
+ #[ display( "invalid public key {0}. " ) ]
49
49
InvalidPubkey ( SsiPub ) ,
50
+ #[ from( aes_gcm:: Error ) ]
51
+ #[ display( "unable to decrypt data." ) ]
52
+ Decrypt ,
50
53
}
51
54
52
55
#[ derive( Clone , Debug , From ) ]
@@ -142,7 +145,7 @@ impl Encrypted {
142
145
let key = pair
143
146
. decrypt_key ( key)
144
147
. map_err ( |_| DecryptionError :: InvalidPubkey ( pair. pk ) ) ?;
145
- Ok ( decrypt ( self . data . as_slice ( ) , self . nonce . into ( ) , key) )
148
+ Ok ( decrypt ( self . data . as_slice ( ) , self . nonce . into ( ) , key) ? )
146
149
}
147
150
}
148
151
@@ -195,13 +198,12 @@ pub fn encrypt(source: Vec<u8>, key: impl AsRef<[u8]>) -> (Nonce<Aes256Gcm>, Vec
195
198
( nonce, ciphered_data)
196
199
}
197
200
198
- pub fn decrypt ( encrypted : & [ u8 ] , nonce : Nonce < Aes256Gcm > , key : impl AsRef < [ u8 ] > ) -> Vec < u8 > {
201
+ pub fn decrypt (
202
+ encrypted : & [ u8 ] ,
203
+ nonce : Nonce < Aes256Gcm > ,
204
+ key : impl AsRef < [ u8 ] > ,
205
+ ) -> Result < Vec < u8 > , aes_gcm:: Error > {
199
206
let key = Sha256 :: digest ( key. as_ref ( ) ) ;
200
207
let key = aes_gcm:: Key :: < Aes256Gcm > :: from_slice ( key. as_slice ( ) ) ;
201
-
202
- let cipher = Aes256Gcm :: new ( key) ;
203
-
204
- cipher
205
- . decrypt ( & nonce, encrypted)
206
- . expect ( "failed to decrypt data" )
208
+ Aes256Gcm :: new ( key) . decrypt ( & nonce, encrypted)
207
209
}
0 commit comments