-
-
Notifications
You must be signed in to change notification settings - Fork 439
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Testing functions requiring CryptoRng #1319
Comments
You should use CSPRNG like use rand_chacha::{ChaCha8Rng, rand_core::SeedableRng};
let mut rng = ChaCha8Rng::seed_from_u64(42); |
My test involves comparing encrypted output from other language (python) which doesn't implement ChaCha in their standard library. I want to compare the output of these 2 functions: import base64
from Crypto.Cipher import PKCS1_v1_5
from Crypto.PublicKey import RSA
rsa_public_key = "MIGfMA0GC...vwIDAQAB"
rsa_public_key = RSA.import_key(base64.b64decode(rsa_public_key))
def grb(n: int) -> bytes:
return b"\x01"
cipher = PKCS1_v1_5.new(rsa_public_key, randfunc=grb)
x = cipher.encrypt("x".encode())
print(base64.b64encode(x)) This will return: Also the rust implementation: use rand::{prelude::*, rngs::mock::StepRng};
use rsa::{
pkcs8::DecodePublicKey, PaddingScheme, PublicKey, RsaPublicKey,
};
struct CryptoStepRng {
inner: StepRng,
}
impl CryptoStepRng {
pub fn new(initial: u64, increment: u64) -> Self {
CryptoStepRng {
inner: StepRng::new(initial, increment),
}
}
}
impl RngCore for CryptoStepRng {
fn next_u32(&mut self) -> u32 {
self.inner.next_u32()
}
fn next_u64(&mut self) -> u64 {
self.inner.next_u64()
}
fn fill_bytes(&mut self, dest: &mut [u8]) {
self.inner.fill_bytes(dest)
}
fn try_fill_bytes(
&mut self,
dest: &mut [u8],
) -> Result<(), rand::Error> {
self.inner.try_fill_bytes(dest)
}
}
impl CryptoRng for CryptoStepRng {}
fn main() {
let mut mrng = CryptoStepRng::new(1, 0);
let rsa_public_key_string = "MIGfMA0GC...vwIDAQAB";
let rsa_public_key_bytes =
base64::decode(rsa_public_key_string).unwrap();
let rsa_public_key = RsaPublicKey::from_public_key_der(
&rsa_public_key_bytes.as_slice(),
)
.unwrap();
let aes_key_encrypted = rsa_public_key
.encrypt(&mut mrng, PaddingScheme::new_pkcs1v15_encrypt(), b"x")
.unwrap();
let aes_key_encrypted = base64::encode(aes_key_encrypted);
println!("{}", &aes_key_encrypted);
} As expected, also returns: This is one of the example where it is useful for StepRng to have CryptoRng implemented. Trying to figure out how to generate ChaCha rng in python would be a lot more complicated. |
If you really want to, you can always implement you own RNG type by wrapping |
That's exactly what I'm doing in the provided example, but I think StepRng should support that outside of the box, and I don't see any reason why it doesn't. |
I am inclined to close this issue as "won't fix". |
Hi! I know this is a closed issue but I would like to bring attention to this just for a short while. I propose adding some examples in the documentation or the book to test functions that use rand crate, in my particular example I believe this is quite a common case where functions that use random generators need to be unit tested and documenting a process would be very helpful. |
@Fethbita I think that mocking |
Thanks for the tip @newpavlov. As recommended, I went with a PR: rust-random/book#64. |
I want to test encrypting some value with rsa public key using the RSA crate., but it only accepts CryptoRng for the rng: RSA/keys.rs#L141
StepRng doesn't implement CryptoRng. I could go around this by creating my own mocking rng, but it doesn't fix the core problem.
I think StepRng should support CryptoRng. It's obviously not a cryptographically secure rng, but it being in the
mock
module should be enough of a warning to not use it in actual code.If this issue gets accepted, I can make a PR for it.
What are your thoughts?
The text was updated successfully, but these errors were encountered: