scram_rs/
scram_hashing_sha5.rs

1/*-
2 * Scram-rs - a SCRAM authentification authorization library
3 * 
4 * Copyright (C) 2021  Aleksandr Morozov
5 * Copyright (C) 2025 Aleksandr Morozov
6 * 
7 * The syslog-rs crate can be redistributed and/or modified
8 * under the terms of either of the following licenses:
9 *
10 *   1. the Mozilla Public License Version 2.0 (the “MPL”) OR
11 *
12 *   2. The MIT License (MIT)
13 *                     
14 *   3. EUROPEAN UNION PUBLIC LICENCE v. 1.2 EUPL © the European Union 2007, 2016
15 */
16
17
18#[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::Sha512;
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
38/// A `ScramProvider` which provides SCRAM-SHA-512 and SCRAM-SHA-512-PLUS 
39/// based on the PBKDF2, Sha, Hmac
40pub struct ScramSha512RustNative;
41
42impl ScramOps for ScramSha512RustNative {}
43
44impl ScramHashing for ScramSha512RustNative 
45{
46    fn hash(data: &[u8]) -> Vec<u8> 
47    {
48        let hash = Sha512::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::<Sha512>::new_from_slice(key)
59                .map_err(|e| 
60                    scram_error_map!(ScramErrorCode::ExternalError, ScramServerError::OtherError, 
61                        "hmac() Hmac::<Sha512> 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; Sha512::output_size()]; //64
77        pbkdf2::<Hmac<Sha512>>(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#[cfg(feature = "use_ring")]
88pub mod sha512_ring_based
89{
90    #[cfg(feature = "std")]
91    use std::num::NonZeroU32;
92    #[cfg(not(feature = "std"))]
93    use core::num::NonZeroU32;
94
95    #[cfg(not(feature = "std"))]
96    use alloc::vec::Vec;
97    #[cfg(not(feature = "std"))]
98    use alloc::vec;
99
100    use ring::{digest as ring_digest, hmac as ring_hmac, pbkdf2 as ring_pbkdf2};
101
102    use crate::{ScramHashing, ScramOps, ScramResult};
103
104    /// A `ScramProvider` which provides SCRAM-SHA-256 and SCRAM-SHA-256-PLUS
105    /// based on the Ring. 
106    pub struct ScramSha512Ring;
107
108    impl ScramOps for ScramSha512Ring {}
109
110    impl ScramHashing for ScramSha512Ring 
111    {
112        fn hash(data: &[u8]) -> Vec<u8> 
113        {
114            let hash = ring_digest::digest(&ring_digest::SHA512, data);
115
116            return Vec::from(hash.as_ref());
117        }
118
119        fn hmac(data: &[u8], key: &[u8]) -> ScramResult<Vec<u8>> 
120        {
121            let s_key = ring_hmac::Key::new(ring_hmac::HMAC_SHA512, key);
122            let mut mac = ring_hmac::Context::with_key(&s_key);
123
124            mac.update(data);
125
126            let ret: Vec<u8> = mac.sign().as_ref().into();
127
128            return Ok(ret);
129        }
130
131        fn derive(password: &[u8], salt: &[u8], iterations: NonZeroU32) -> ScramResult<Vec<u8>> 
132        {
133            let mut salted = vec![0; ring_digest::SHA512_OUTPUT_LEN];
134
135            ring_pbkdf2::derive(ring_pbkdf2::PBKDF2_HMAC_SHA512, iterations.into(), salt, password, &mut salted);
136
137            return Ok(salted);
138        }
139
140    }
141}
142
143#[cfg(feature = "use_ring")]
144pub use self::sha512_ring_based::*;