Files
alkane-toolkit/vendor/md5crypt/src/lib.rs
T

152 lines
4.6 KiB
Rust

//! Pure Rust implementation of the md5crypt password hashing function.
//!
//! ## Security warning!
//! md5crypt is considered cryptographically broken and unsafe for use as a password scrambler.
//! This crate is provided for interoperability with legacy systems only.
//!
//! See the following links for more information:
//! - <https://web.archive.org/web/20180317164935/http://phk.freebsd.dk/sagas/md5crypt_eol.html>
//! - <https://datatracker.ietf.org/doc/html/rfc6151>
//! - <https://dl.acm.org/doi/10.1109/CIS.2009.214>
//! - <https://www.kb.cert.org/vuls/id/836068>
//!
//! # Usage
//!
//! ```
//! use md5crypt::md5crypt;
//!
//! let password = b"hunter2";
//! let salt = b"1234abcd";
//! assert_eq!(md5crypt(password, salt), b"$1$1234abcd$k941IFPqhCBpKvhOnZqRd/");
//! ```
use md5::Context;
const B64CHARS: &[u8; 64] = b"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
fn to64(input: usize, len: usize) -> Vec<u8> {
let mut out = Vec::<u8>::new();
let mut u = input;
for _ in 0..len {
out.push(B64CHARS[u % 64]);
u /= 64;
}
out
}
// Copyright (c) 2003 Poul-Henning Kamp
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
// OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
// SUCH DAMAGE.
/// md5crypt based on musl crypt_md5.c
///
/// No key or salt limit is defined. musl's md5crypt suggests limiting the key
/// to 30000 bytes and the salt to 8 bytes.
///
/// # Arguments
/// - `key` - The password to process as a byte vector.
/// - `salt` - The salt value to use as a byte vector.
///
/// # Returns
/// Byte vector of the form: `$1$salt$hash`.
pub fn md5crypt(key: &[u8], salt: &[u8]) -> Vec<u8> {
let mut ctx = Context::new();
// md5(key salt key)
ctx.consume(key);
ctx.consume(salt);
ctx.consume(key);
let mut md = ctx.finalize();
// md5(key $1$ salt repeated-md weird-key[0]-0)
let mut ctx = Context::new();
ctx.consume(key);
ctx.consume("$1$");
ctx.consume(salt);
let mut i = key.len();
while i > md.len() {
ctx.consume(*md);
i -= md.len();
}
ctx.consume(&md[0..i]);
md[0] = 0;
let mut i = key.len();
while i != 0 {
if (i & 1) != 0 {
ctx.consume([md[0]]);
} else {
ctx.consume([key[0]]);
}
i >>= 1;
}
let mut md = ctx.finalize();
// md = f(md, key, salt) iteration
for i in 0..1000 {
let mut ctx = Context::new();
if i % 2 != 0 {
ctx.consume(key);
} else {
ctx.consume(*md);
}
if (i % 3) != 0 {
ctx.consume(salt);
}
if (i % 7) != 0 {
ctx.consume(key);
}
if (i % 2) != 0 {
ctx.consume(*md);
} else {
ctx.consume(key);
}
md = ctx.finalize();
}
let perm = [(0, 6, 12), (1, 7, 13), (2, 8, 14), (3, 9, 15), (4, 10, 5)];
let mut crypted = Vec::<u8>::new();
for i in 0..5 {
crypted.append(&mut to64(
((md[perm[i].0] as usize) << 16)
| ((md[perm[i].1] as usize) << 8)
| (md[perm[i].2] as usize),
4,
));
}
crypted.append(&mut to64(md[11].into(), 2));
[b"$1$", salt, b"$", &crypted].concat()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_works() {
assert_eq!(
md5crypt(b"Xy01@#\x01\x02\x80\x7f\xff\r\n\x81\t !", b"abcd0123"),
b"$1$abcd0123$9Qcg8DyviekV3tDGMZynJ1"
);
}
}