try multiple cryptosystems

This commit is contained in:
John Smith
2023-03-13 16:14:31 -04:00
parent d680f1b785
commit 3c7c49684c
9 changed files with 281 additions and 270 deletions

View File

@@ -1,70 +0,0 @@
use digest::generic_array::typenum::U64;
use digest::{Digest, Output};
use generic_array::GenericArray;
pub struct Blake3Digest512 {
dig: blake3::Hasher,
}
impl Digest for Blake3Digest512 {
type OutputSize = U64;
fn new() -> Self {
Self {
dig: blake3::Hasher::new(),
}
}
fn update(&mut self, data: impl AsRef<[u8]>) {
self.dig.update(data.as_ref());
}
fn chain(mut self, data: impl AsRef<[u8]>) -> Self
where
Self: Sized,
{
self.update(data);
self
}
fn finalize(self) -> Output<Self> {
let mut b = [0u8; 64];
self.dig.finalize_xof().fill(&mut b);
let mut out = GenericArray::<u8, U64>::default();
for n in 0..64 {
out[n] = b[n];
}
out
}
fn finalize_reset(&mut self) -> Output<Self> {
let mut b = [0u8; 64];
self.dig.finalize_xof().fill(&mut b);
let mut out = GenericArray::<u8, U64>::default();
for n in 0..64 {
out[n] = b[n];
}
self.reset();
out
}
fn reset(&mut self) {
self.dig.reset();
}
fn output_size() -> usize {
64
}
fn digest(data: &[u8]) -> Output<Self> {
let mut dig = blake3::Hasher::new();
dig.update(data);
let mut b = [0u8; 64];
dig.finalize_xof().fill(&mut b);
let mut out = GenericArray::<u8, U64>::default();
for n in 0..64 {
out[n] = b[n];
}
out
}
}

View File

@@ -1,6 +1,3 @@
pub mod blake3digest512;
pub use blake3digest512::*;
use super::*;
use chacha20::cipher::{KeyIvInit, StreamCipher};
@@ -75,12 +72,12 @@ impl CryptoSystem for CryptoSystemVLD0 {
// Generation
fn random_nonce(&self) -> Nonce {
let mut nonce = [0u8; 24];
let mut nonce = [0u8; NONCE_LENGTH];
random_bytes(&mut nonce).unwrap();
Nonce::new(nonce)
}
fn random_shared_secret(&self) -> SharedSecret {
let mut s = [0u8; 32];
let mut s = [0u8; SHARED_SECRET_LENGTH];
random_bytes(&mut s).unwrap();
SharedSecret::new(s)
}
@@ -165,12 +162,15 @@ impl CryptoSystem for CryptoSystemVLD0 {
let mut dig = Blake3Digest512::new();
dig.update(data);
let sig = keypair
let sig_bytes = keypair
.sign_prehashed(dig, None)
.map_err(VeilidAPIError::internal)?;
let dht_sig = Signature::new(sig.to_bytes());
Ok(dht_sig)
let sig = Signature::new(sig_bytes.to_bytes());
self.verify(dht_key, &data, &sig)?;
Ok(sig)
}
fn verify(
&self,