Skip to main content

bitwarden_random/
snow_resolver.rs

1use snow::{
2    Error,
3    params::{CipherChoice, DHChoice, HashChoice},
4    resolvers::{CryptoResolver, DefaultResolver},
5    types::{Cipher, Dh, Hash, Random},
6};
7
8use crate::SdkRngImpl;
9
10impl Random for SdkRngImpl {
11    fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
12        rand::Rng::fill_bytes(self, dest);
13        Ok(())
14    }
15}
16
17/// A snow [`CryptoResolver`] that supplies the SDK's [`SdkRngImpl`]
18#[derive(Default)]
19pub struct SdkCryptoResolver;
20
21impl CryptoResolver for SdkCryptoResolver {
22    fn resolve_rng(&self) -> Option<Box<dyn Random>> {
23        Some(Box::new(SdkRngImpl::default()))
24    }
25
26    fn resolve_dh(&self, choice: &DHChoice) -> Option<Box<dyn Dh>> {
27        DefaultResolver.resolve_dh(choice)
28    }
29
30    fn resolve_hash(&self, choice: &HashChoice) -> Option<Box<dyn Hash>> {
31        DefaultResolver.resolve_hash(choice)
32    }
33
34    fn resolve_cipher(&self, choice: &CipherChoice) -> Option<Box<dyn Cipher>> {
35        DefaultResolver.resolve_cipher(choice)
36    }
37    // `resolve_kem` is behind snow's `hfs` feature (not enabled); its default returns None.
38}