1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
use std::num::NonZeroU32;
use hmac::{Hmac, Mac};
use sha1::{Sha1, Digest as Digest1};
use pbkdf2::pbkdf2;
use crate::{ScramServerError, ScramHashing, ScramResult, scram_error_map, ScramErrorCode};
pub struct ScramSha1RustNative;
impl ScramHashing for ScramSha1RustNative
{
fn hash(data: &[u8]) -> Vec<u8>
{
let hash = Sha1::digest(data);
return Vec::from(hash.as_slice());
}
fn hmac(data: &[u8], key: &[u8]) -> ScramResult<Vec<u8>>
{
let mut mac =
Hmac::<Sha1>::new_from_slice(key)
.map_err(|e|
scram_error_map!(ScramErrorCode::ExternalError, ScramServerError::OtherError,
"hmac() HmacSha1 err, {}", e)
)?;
mac.update(data);
let result = mac.finalize();
return Ok( Vec::from(result.into_bytes().as_slice()) );
}
fn derive(password: &[u8], salt: &[u8], iterations: NonZeroU32) -> ScramResult<Vec<u8>>
{
let mut result = vec![0; Sha1::output_size()]; pbkdf2::<Hmac<Sha1>>(password, salt, iterations.get(), &mut result)
.map_err(|e|
scram_error_map!(ScramErrorCode::ExternalError, ScramServerError::OtherError,
"pbkdf2 Hmac::<Sha1> err, {}", e)
)?;
return Ok(result);
}
}
#[cfg(feature = "use_ring")]
pub mod sha1_ring_based
{
use std::num::NonZeroU32;
use ring::{digest as ring_digest, hmac as ring_hmac, pbkdf2 as ring_pbkdf2};
use crate::{ScramHashing, ScramResult};
pub struct ScramSha1Ring;
impl ScramHashing for ScramSha1Ring
{
fn hash(data: &[u8]) -> Vec<u8>
{
let hash = ring_digest::digest(&ring_digest::SHA1_FOR_LEGACY_USE_ONLY, data);
return Vec::from(hash.as_ref());
}
fn hmac(data: &[u8], key: &[u8]) -> ScramResult<Vec<u8>>
{
let s_key = ring_hmac::Key::new(ring_hmac::HMAC_SHA1_FOR_LEGACY_USE_ONLY, key);
let mut mac = ring_hmac::Context::with_key(&s_key);
mac.update(data);
let ret: Vec<u8> = mac.sign().as_ref().into();
return Ok(ret);
}
fn derive(password: &[u8], salt: &[u8], iterations: NonZeroU32) -> ScramResult<Vec<u8>>
{
let mut salted = vec![0; ring_digest::SHA1_OUTPUT_LEN];
ring_pbkdf2::derive(ring_pbkdf2::PBKDF2_HMAC_SHA1, iterations.into(), salt, password, &mut salted);
return Ok(salted);
}
}
}
#[cfg(feature = "use_ring")]
pub use self::sha1_ring_based::*;