scram_rs/
scram_hashing_sha2.rs1#[cfg(feature = "std")]
19use std::num::NonZeroU32;
20#[cfg(not(feature = "std"))]
21use core::num::NonZeroU32;
22
23#[cfg(not(feature = "std"))]
24use alloc::vec::Vec;
25#[cfg(not(feature = "std"))]
26use alloc::vec;
27
28use sha2::Sha256;
29use hmac::{Hmac, Mac};
30use pbkdf2::pbkdf2;
31use sha1::Digest as Digest1;
32
33use crate::{ScramHashing, ScramOps};
34
35use super::scram_error::{ScramResult, ScramErrorCode};
36use super::scram_error_map;
37
38pub struct ScramSha256RustNative;
41
42impl ScramOps for ScramSha256RustNative {}
43
44impl ScramHashing for ScramSha256RustNative
45{
46 fn hash(data: &[u8]) -> Vec<u8>
47 {
48 let hash = Sha256::digest(data);
49
50 return Vec::from(hash.as_slice());
51 }
52
53 fn hmac(data: &[u8], key: &[u8]) -> ScramResult<Vec<u8>>
54 {
55 use crate::ScramServerError;
56
57 let mut mac =
58 Hmac::<Sha256>::new_from_slice(key)
59 .map_err(|e|
60 scram_error_map!(ScramErrorCode::ExternalError, ScramServerError::OtherError,
61 "hmac() Hmac::<Sha256> err, {}", e)
62 )?;
63
64 mac.update(data);
65
66 let result = mac.finalize();
67 let ret = Vec::from(result.into_bytes().as_slice());
68
69 return Ok(ret);
70 }
71
72 fn derive(password: &[u8], salt: &[u8], iterations: NonZeroU32) -> ScramResult<Vec<u8>>
73 {
74 use crate::ScramServerError;
75
76 let mut salted = vec![0; Sha256::output_size()]; pbkdf2::<Hmac<Sha256>>(password, salt, iterations.get(), &mut salted)
78 .map_err(|e|
79 scram_error_map!(ScramErrorCode::ExternalError, ScramServerError::OtherError,
80 "pbkdf2 Hmac::<Sha1> err, {}", e)
81 )?;
82
83 return Ok(salted);
84 }
85}
86
87
88#[cfg(feature = "use_ring")]
89pub mod sha256_ring_based
90{
91 #[cfg(feature = "std")]
92 use std::num::NonZeroU32;
93 #[cfg(not(feature = "std"))]
94 use core::num::NonZeroU32;
95
96 #[cfg(not(feature = "std"))]
97 use alloc::vec::Vec;
98 #[cfg(not(feature = "std"))]
99 use alloc::vec;
100
101 use ring::{digest as ring_digest, hmac as ring_hmac, pbkdf2 as ring_pbkdf2};
102
103 use crate::{ScramHashing, ScramOps, ScramResult};
104
105 pub struct ScramSha256Ring;
108
109 impl ScramOps for ScramSha256Ring {}
110
111 impl ScramHashing for ScramSha256Ring
112 {
113 fn hash(data: &[u8]) -> Vec<u8>
114 {
115 let hash = ring_digest::digest(&ring_digest::SHA256, data);
116
117 return Vec::from(hash.as_ref());
118 }
119
120 fn hmac(data: &[u8], key: &[u8]) -> ScramResult<Vec<u8>>
121 {
122 let s_key = ring_hmac::Key::new(ring_hmac::HMAC_SHA256, key);
123 let mut mac = ring_hmac::Context::with_key(&s_key);
124
125 mac.update(data);
126
127 let ret: Vec<u8> = mac.sign().as_ref().into();
128
129 return Ok(ret);
130 }
131
132 fn derive(password: &[u8], salt: &[u8], iterations: NonZeroU32) -> ScramResult<Vec<u8>>
133 {
134 let mut salted = vec![0; ring_digest::SHA256_OUTPUT_LEN];
135
136 ring_pbkdf2::derive(ring_pbkdf2::PBKDF2_HMAC_SHA256, iterations.into(), salt, password, &mut salted);
137
138 return Ok(salted);
139 }
140
141 }
142}
143
144#[cfg(feature = "use_ring")]
145pub use self::sha256_ring_based::*;