Skip to content

Commit

Permalink
Merge pull request #8 from abetterinternet/yuriks/error-impl
Browse files Browse the repository at this point in the history
Implement std::error::Error for EncryptError
  • Loading branch information
yuriks authored Oct 1, 2020
2 parents c662884 + 5d97f20 commit 61efbcc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "prio"
version = "0.1.1"
version = "0.2.0"
authors = ["Josh Aas <[email protected]>", "Karl Tarbe <[email protected]>"]
edition = "2018"
description = "Implementation of the Prio aggregation system core: https://crypto.stanford.edu/prio/"
Expand All @@ -10,8 +10,9 @@ repository = "https://github.com/abetterinternet/libprio-rs"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
rand = "0.7"
aes-ctr = "0.4.0"
aes-gcm = "0.6.0"
base64 = "0.12.3"
rand = "0.7"
ring = "0.16.15"
aes-gcm = "0.6.0"
thiserror = "1.0"
13 changes: 9 additions & 4 deletions src/encrypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,22 @@ pub const TAG_LENGTH: usize = 16;
const KEY_LENGTH: usize = 16;

/// Possible errors from encryption / decryption.
#[derive(Debug)]
#[derive(Debug, thiserror::Error)]
pub enum EncryptError {
/// Base64 decoding error
DecodeBase64Error(base64::DecodeError),
#[error("base64 decoding error")]
DecodeBase64Error(#[from] base64::DecodeError),
/// Error in ECDH
#[error("error in ECDH")]
KeyAgreementError,
/// Buffer for ciphertext was not large enough
#[error("buffer for ciphertext was not large enough")]
EncryptionError,
/// Authentication tags did not match.
#[error("authentication tags did not match")]
DecryptionError,
/// Input ciphertext was too small
#[error("input ciphertext was too small")]
DecryptionLengthError,
}

Expand All @@ -44,7 +49,7 @@ pub struct PrivateKey(Vec<u8>);
impl PublicKey {
/// Load public key from a base64 encoded X9.62 uncompressed representation.
pub fn from_base64(key: &str) -> Result<Self, EncryptError> {
let keydata = base64::decode(key).map_err(EncryptError::DecodeBase64Error)?;
let keydata = base64::decode(key)?;
Ok(PublicKey(keydata))
}
}
Expand All @@ -59,7 +64,7 @@ impl std::convert::From<&PrivateKey> for PublicKey {
impl PrivateKey {
/// Load private key from a base64 encoded string.
pub fn from_base64(key: &str) -> Result<Self, EncryptError> {
let keydata = base64::decode(key).map_err(EncryptError::DecodeBase64Error)?;
let keydata = base64::decode(key)?;
Ok(PrivateKey(keydata))
}
}
Expand Down

0 comments on commit 61efbcc

Please sign in to comment.