-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Arthur Gautier <[email protected]>
- Loading branch information
Showing
3 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright 2023 Contributors to the Parsec project. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use signature::rand_core::{CryptoRng, Error as RndError, RngCore}; | ||
use thiserror::Error; | ||
|
||
use crate::SessionLike; | ||
|
||
#[derive(Debug, Error)] | ||
pub enum Error {} | ||
|
||
/// [`Rng`] is a PKCS#11-backed CSPRNG. | ||
pub struct Rng<S: SessionLike>(S); | ||
|
||
// TODO(baloo): check for CKF_RNG bit flag (CK_TOKEN_INFO struct -> flags) | ||
impl<S: SessionLike> Rng<S> { | ||
pub fn new(session: S) -> Result<Self, Error> { | ||
Ok(Self(session)) | ||
} | ||
} | ||
|
||
macro_rules! impl_next_uint { | ||
($self:ident, $u:ty) => {{ | ||
let mut buf = <$u>::MIN.to_be_bytes(); | ||
$self.fill_bytes(&mut buf[..]); | ||
|
||
<$u>::from_be_bytes(buf) | ||
}}; | ||
} | ||
|
||
impl<S: SessionLike> RngCore for Rng<S> { | ||
fn next_u32(&mut self) -> u32 { | ||
impl_next_uint!(self, u32) | ||
} | ||
|
||
fn next_u64(&mut self) -> u64 { | ||
impl_next_uint!(self, u64) | ||
} | ||
|
||
fn fill_bytes(&mut self, dest: &mut [u8]) { | ||
self.try_fill_bytes(dest) | ||
.expect("Cryptoki provider failed to generate random"); | ||
} | ||
|
||
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), RndError> { | ||
self.0.generate_random_slice(dest).map_err(RndError::new) | ||
} | ||
} | ||
|
||
impl<S: SessionLike> CryptoRng for Rng<S> {} |
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,57 @@ | ||
// Copyright 2023 Contributors to the Parsec project. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
mod common; | ||
|
||
use crate::common::USER_PIN; | ||
use common::init_pins; | ||
use cryptoki::{session::UserType, types::AuthPin}; | ||
use cryptoki_rustcrypto::rng::Rng; | ||
use serial_test::serial; | ||
use signature::rand_core::{CryptoRngCore, RngCore}; | ||
use testresult::TestResult; | ||
|
||
// This test is meant to ensure we provide [`rand_core::CryptoRngCore`]. | ||
// This is the trait consumers will use throughout the RustCrypto ecosystem | ||
// to express interest in a CSPRNG. | ||
#[test] | ||
#[serial] | ||
fn ensure_crypto_rng_core() -> TestResult { | ||
fn just_making_sure<R: CryptoRngCore>(_r: &mut R) { | ||
// Hi! just making sure you provide a CSPRNG. | ||
} | ||
let (pkcs11, slot) = init_pins(); | ||
|
||
// open a session | ||
let session = pkcs11.open_rw_session(slot)?; | ||
|
||
// log in the session | ||
session.login(UserType::User, Some(&AuthPin::new(USER_PIN.into())))?; | ||
|
||
let mut rng = Rng::new(session).unwrap(); | ||
just_making_sure(&mut rng); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[test] | ||
#[serial] | ||
fn generate_random() -> TestResult { | ||
let (pkcs11, slot) = init_pins(); | ||
|
||
// open a session | ||
let session = pkcs11.open_rw_session(slot)?; | ||
|
||
// log in the session | ||
session.login(UserType::User, Some(&AuthPin::new(USER_PIN.into())))?; | ||
|
||
let mut rng = Rng::new(session).unwrap(); | ||
rng.next_u32(); | ||
rng.next_u64(); | ||
|
||
let mut buf = vec![0; 123]; | ||
rng.fill_bytes(&mut buf); | ||
rng.try_fill_bytes(&mut buf).unwrap(); | ||
|
||
Ok(()) | ||
} |