Skip to content

Commit 90ee76c

Browse files
committed
fix processing armored data
1 parent d021439 commit 90ee76c

File tree

4 files changed

+23
-17
lines changed

4 files changed

+23
-17
lines changed

Cargo.lock

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ required-features = ["cli"]
2323
[dependencies]
2424
amplify = "4.6.0"
2525
strict_encoding = "2.7.0-beta.4"
26-
ascii-armor = "0.7.0"
26+
ascii-armor = "0.7.1"
2727
baid64 = "0.2.2"
2828
base64 = "0.22.1"
2929
secp256k1 = { version = "0.29.0", features = ["rand", "global-context", "rand-std"] }

src/encrypt.rs

+15-13
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,21 @@ use crate::{Algo, InvalidPubkey, SsiPair, SsiPub, LIB_NAME_SSI};
3535

3636
#[derive(Copy, Clone, Debug, Display, Error)]
3737
pub enum EncryptionError {
38-
#[display("the number of receivers exceeds 2^16")]
38+
#[display("the number of receivers exceeds 2^16.")]
3939
TooManyReceivers,
40-
#[display("invalid public key {0}")]
40+
#[display("invalid public key {0}.")]
4141
InvalidPubkey(SsiPub),
4242
}
4343

44-
#[derive(Copy, Clone, Debug, Display, Error)]
44+
#[derive(Copy, Clone, Debug, Display, Error, From)]
4545
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}.")]
4747
KeyMismatch(SsiPub),
48-
#[display("invalid public key {0}")]
48+
#[display("invalid public key {0}.")]
4949
InvalidPubkey(SsiPub),
50+
#[from(aes_gcm::Error)]
51+
#[display("unable to decrypt data.")]
52+
Decrypt,
5053
}
5154

5255
#[derive(Clone, Debug, From)]
@@ -142,7 +145,7 @@ impl Encrypted {
142145
let key = pair
143146
.decrypt_key(key)
144147
.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)?)
146149
}
147150
}
148151

@@ -195,13 +198,12 @@ pub fn encrypt(source: Vec<u8>, key: impl AsRef<[u8]>) -> (Nonce<Aes256Gcm>, Vec
195198
(nonce, ciphered_data)
196199
}
197200

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> {
199206
let key = Sha256::digest(key.as_ref());
200207
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)
207209
}

src/secret.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ pub enum RevealError {
5050

5151
/// unsupported algorithm #{0}.
5252
Unsupported(u8),
53+
54+
/// unable to decrypt data.
55+
#[from(aes_gcm::Error)]
56+
Decrypt,
5357
}
5458

5559
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
@@ -62,7 +66,7 @@ pub struct EncryptedSecret {
6266

6367
impl EncryptedSecret {
6468
pub fn reveal(&self, passwd: impl AsRef<str>) -> Result<SsiSecret, RevealError> {
65-
let sk = decrypt(&self.key, self.nonce, passwd.as_ref());
69+
let sk = decrypt(&self.key, self.nonce, passwd.as_ref())?;
6670
match self.algo {
6771
Algo::Ed25519 => Ok(ec25519::SecretKey::from_slice(&sk)?.into()),
6872
Algo::Bip340 => Ok(secp256k1::SecretKey::from_slice(&sk)?.into()),

0 commit comments

Comments
 (0)