This commit is contained in:
John Smith
2022-07-12 12:45:54 -04:00
parent c9d3f38fa0
commit 886d5bbd7c
17 changed files with 171 additions and 181 deletions

View File

@@ -232,13 +232,13 @@ impl Crypto {
nonce: &Nonce,
shared_secret: &SharedSecret,
associated_data: Option<&[u8]>,
) -> Result<(), String> {
) -> Result<(), VeilidAPIError> {
let key = ch::Key::from(*shared_secret);
let xnonce = ch::XNonce::from(*nonce);
let aead = ch::XChaCha20Poly1305::new(&key);
aead.decrypt_in_place(&xnonce, associated_data.unwrap_or(b""), body)
.map_err(map_to_string)
.map_err(logthru_crypto!())
.map_err(VeilidAPIError::generic)
}
pub fn decrypt_aead(
@@ -246,11 +246,11 @@ impl Crypto {
nonce: &Nonce,
shared_secret: &SharedSecret,
associated_data: Option<&[u8]>,
) -> Result<Vec<u8>, String> {
) -> Result<Vec<u8>, VeilidAPIError> {
let mut out = body.to_vec();
Self::decrypt_in_place_aead(&mut out, nonce, shared_secret, associated_data)
.map_err(map_to_string)
.map_err(logthru_crypto!())?;
.map_err(VeilidAPIError::generic)?;
Ok(out)
}
@@ -259,14 +259,14 @@ impl Crypto {
nonce: &Nonce,
shared_secret: &SharedSecret,
associated_data: Option<&[u8]>,
) -> Result<(), String> {
) -> Result<(), VeilidAPIError> {
let key = ch::Key::from(*shared_secret);
let xnonce = ch::XNonce::from(*nonce);
let aead = ch::XChaCha20Poly1305::new(&key);
aead.encrypt_in_place(&xnonce, associated_data.unwrap_or(b""), body)
.map_err(map_to_string)
.map_err(logthru_crypto!())
.map_err(VeilidAPIError::generic)
}
pub fn encrypt_aead(
@@ -274,11 +274,11 @@ impl Crypto {
nonce: &Nonce,
shared_secret: &SharedSecret,
associated_data: Option<&[u8]>,
) -> Result<Vec<u8>, String> {
) -> Result<Vec<u8>, VeilidAPIError> {
let mut out = body.to_vec();
Self::encrypt_in_place_aead(&mut out, nonce, shared_secret, associated_data)
.map_err(map_to_string)
.map_err(logthru_crypto!())?;
.map_err(VeilidAPIError::generic)?;
Ok(out)
}

View File

@@ -158,7 +158,7 @@ macro_rules! byte_array_type {
let res = BASE64URL_NOPAD.decode_mut(input.as_bytes(), &mut bytes);
match res {
Ok(_) => Ok(Self::new(bytes)),
Err(_) => apierr_generic!("Failed to decode"),
Err(_) => apibail_generic!("Failed to decode"),
}
}
}
@@ -277,7 +277,7 @@ macro_rules! byte_array_type {
out.valid = true;
Ok(out)
}
Err(err) => apierr_generic!(err),
Err(err) => Err(VeilidAPIError::generic(err)),
}
}
}
@@ -382,7 +382,8 @@ pub fn sign(
kpb[..DHT_KEY_SECRET_LENGTH].copy_from_slice(&dht_key_secret.bytes);
kpb[DHT_KEY_SECRET_LENGTH..].copy_from_slice(&dht_key.bytes);
let keypair = Keypair::from_bytes(&kpb).map_err(mapapierr_parse!("Keypair is invalid"))?;
let keypair = Keypair::from_bytes(&kpb)
.map_err(|e| VeilidAPIError::parse_error("Keypair is invalid", e))?;
let mut dig = Blake3Digest512::new();
dig.update(data);
@@ -402,16 +403,16 @@ pub fn verify(
) -> Result<(), VeilidAPIError> {
assert!(dht_key.valid);
assert!(signature.valid);
let pk =
PublicKey::from_bytes(&dht_key.bytes).map_err(mapapierr_parse!("Public key is invalid"))?;
let pk = PublicKey::from_bytes(&dht_key.bytes)
.map_err(|e| VeilidAPIError::parse_error("Public key is invalid", e))?;
let sig = Signature::from_bytes(&signature.bytes)
.map_err(mapapierr_parse!("Signature is invalid"))?;
.map_err(|e| VeilidAPIError::parse_error("Signature is invalid", e))?;
let mut dig = Blake3Digest512::new();
dig.update(data);
pk.verify_prehashed(dig, None, &sig)
.map_err(mapapierr_parse!("Verification failed"))?;
.map_err(|e| VeilidAPIError::parse_error("Verification failed", e))?;
Ok(())
}