samedi 13 février 2021

Generating secure random bytes in rust

I'm trying to use ring::rand::Secure/SystemRandom to generate some secure random bytes:

let mut randoms: [u8; 10] = [0; 10];
let sr = ring::rand::SystemRandom::new();
sr.fill(&randoms); // Error: fill method not found in `SystemRandom`

Now i'm not sure why SystemRandom doesn't implement 'fill' - there's a fair bit of indirection in the ring library but I believe it should:

Firstly, SystemRandom implements sealed::SecureRandom:

impl sealed::SecureRandom for SystemRandom {
    #[inline(always)]
    fn fill_impl(&self, dest: &mut [u8]) -> Result<(), error::Unspecified> {
        fill_impl(dest)
    }
}

Next, sealed::SecureRandom implements SecureRandom (i'm unsure why the need for generics here?):

impl<T> SecureRandom for T
where
    T: sealed::SecureRandom,
{
    #[inline(always)]
    fn fill(&self, dest: &mut [u8]) -> Result<(), error::Unspecified> {
        self.fill_impl(dest)
    }
}

And thus SystemRandom implements SecureRandom via the internal sealed::SecureRandom.

However it doesn't seem to compile, am i missing something important here?

Thanks

  • edit -

Turns out i had use ring::rand::SystemRandom but needed to also have use ring::rand::SecureRandom to make that function visible.

Now my problem is:

sr.fill(&randoms);
|        ^^^^^^^^ types differ in mutability



Aucun commentaire:

Enregistrer un commentaire