-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
48c4dae
commit 6187cef
Showing
9 changed files
with
482 additions
and
14 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
mod signature_verification; | ||
|
||
mod error; | ||
pub use error::*; | ||
|
||
|
32 changes: 32 additions & 0 deletions
32
packages/ic-certificate-verification/src/signature_verification/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
use self::signature_cache::{SignatureCache, SignatureCacheEntry}; | ||
use crate::CertificateVerificationError; | ||
use miracl_core_bls12381::bls12381::bls::{core_verify, BLS_OK}; | ||
|
||
mod signature_cache; | ||
|
||
#[cfg(test)] | ||
mod reproducible_rng; | ||
|
||
#[cfg(test)] | ||
mod tests; | ||
|
||
pub fn verify_signature( | ||
pk: &[u8], | ||
sig: &[u8], | ||
msg: &[u8], | ||
) -> Result<(), CertificateVerificationError> { | ||
let entry = SignatureCacheEntry::new(pk, sig, msg); | ||
|
||
if SignatureCache::global().contains(&entry) { | ||
return Ok(()); | ||
} | ||
|
||
let result = core_verify(sig, msg, pk); | ||
|
||
if !matches!(result, BLS_OK) { | ||
return Err(CertificateVerificationError::SignatureVerificationFailed); | ||
} | ||
|
||
SignatureCache::global().insert(&entry); | ||
Ok(()) | ||
} |
74 changes: 74 additions & 0 deletions
74
packages/ic-certificate-verification/src/signature_verification/reproducible_rng.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
use rand::{CryptoRng, Error, Rng, RngCore, SeedableRng}; | ||
use rand_chacha::ChaCha20Rng; | ||
|
||
/// Byte length of the seed type used in [`ReproducibleRng`]. | ||
const SEED_LEN: usize = 32; | ||
|
||
/// Provides a seeded RNG, where the randomly chosen seed is printed on standard output. | ||
pub fn reproducible_rng() -> ReproducibleRng { | ||
ReproducibleRng::new() | ||
} | ||
|
||
/// Wraps the logic of [`reproducible_rng`] into a separate struct. | ||
/// | ||
/// This is needed when [`reproducible_rng`] cannot be used because its | ||
/// return type `impl Rng + CryptoRng` can only be used as function parameter | ||
/// or as return type | ||
/// (See [impl trait type](https://doc.rust-lang.org/reference/types/impl-trait.html)). | ||
pub struct ReproducibleRng { | ||
rng: ChaCha20Rng, | ||
seed: [u8; SEED_LEN], | ||
} | ||
|
||
impl ReproducibleRng { | ||
/// Randomly generates a seed and prints it to `stdout`. | ||
pub fn new() -> Self { | ||
let mut seed = [0u8; SEED_LEN]; | ||
rand::thread_rng().fill(&mut seed); | ||
let rng = Self::from_seed_internal(seed); | ||
println!("{rng:?}"); | ||
rng | ||
} | ||
|
||
fn from_seed_internal(seed: [u8; SEED_LEN]) -> Self { | ||
let rng = ChaCha20Rng::from_seed(seed); | ||
Self { rng, seed } | ||
} | ||
} | ||
|
||
impl std::fmt::Debug for ReproducibleRng { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
write!( | ||
f, | ||
"Copy the seed below to reproduce the failed test.\n | ||
let seed: [u8; 32] = {:?};", | ||
self.seed | ||
) | ||
} | ||
} | ||
|
||
impl Default for ReproducibleRng { | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} | ||
|
||
impl RngCore for ReproducibleRng { | ||
fn next_u32(&mut self) -> u32 { | ||
self.rng.next_u32() | ||
} | ||
|
||
fn next_u64(&mut self) -> u64 { | ||
self.rng.next_u64() | ||
} | ||
|
||
fn fill_bytes(&mut self, dest: &mut [u8]) { | ||
self.rng.fill(dest) | ||
} | ||
|
||
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> { | ||
self.rng.try_fill_bytes(dest) | ||
} | ||
} | ||
|
||
impl CryptoRng for ReproducibleRng {} |
Oops, something went wrong.