use super::*;
#[inline]
pub fn auth_now_secs() -> u64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|dur: Duration| dur.as_secs())
.unwrap_or(0)
}
#[inline(always)]
pub(crate) fn request_header_back<'a>(req: &'a Request, lower_key: &str) -> Option<&'a str> {
req.get_headers()
.get(lower_key)
.and_then(|values: &RequestHeadersValue| values.back())
.map(String::as_str)
}
#[inline]
pub(crate) fn sha256_finish_from(mut state: [u32; 8], prefix_bytes: u64, data: &[u8]) -> [u8; 32] {
let total_bytes: u64 = prefix_bytes.wrapping_add(data.len() as u64);
let bit_len: u64 = total_bytes.wrapping_mul(8);
let (blocks, rem_slice): (&[[u8; 64]], &[u8]) = data.as_chunks::<64>();
for block in blocks {
sha256_compress(&mut state, block);
}
let rem: usize = rem_slice.len();
let mut tail0: [u8; 64] = [0u8; 64];
tail0[..rem].copy_from_slice(rem_slice);
tail0[rem] = 0x80;
if rem < 56 {
tail0[56..64].copy_from_slice(&bit_len.to_be_bytes());
sha256_compress(&mut state, &tail0);
} else {
sha256_compress(&mut state, &tail0);
let mut tail1: [u8; 64] = [0u8; 64];
tail1[56..64].copy_from_slice(&bit_len.to_be_bytes());
sha256_compress(&mut state, &tail1);
}
let mut out: [u8; 32] = [0u8; 32];
let b0: [u8; 4] = state[0].to_be_bytes();
let b1: [u8; 4] = state[1].to_be_bytes();
let b2: [u8; 4] = state[2].to_be_bytes();
let b3: [u8; 4] = state[3].to_be_bytes();
let b4: [u8; 4] = state[4].to_be_bytes();
let b5: [u8; 4] = state[5].to_be_bytes();
let b6: [u8; 4] = state[6].to_be_bytes();
let b7: [u8; 4] = state[7].to_be_bytes();
out[0..4].copy_from_slice(&b0);
out[4..8].copy_from_slice(&b1);
out[8..12].copy_from_slice(&b2);
out[12..16].copy_from_slice(&b3);
out[16..20].copy_from_slice(&b4);
out[20..24].copy_from_slice(&b5);
out[24..28].copy_from_slice(&b6);
out[28..32].copy_from_slice(&b7);
out
}
#[inline]
pub fn sha256(data: &[u8]) -> [u8; 32] {
sha256_finish_from(SHA256_INIT, 0, data)
}
#[inline(always)]
pub(crate) fn sha256_step(
abcd: (u32, u32, u32, &mut u32),
efgh: (u32, u32, u32, &mut u32),
w: u32,
k: u32,
) {
let (a, b, c, d): (u32, u32, u32, &mut u32) = abcd;
let (e, f, g, h): (u32, u32, u32, &mut u32) = efgh;
let s1: u32 = e.rotate_right(6) ^ e.rotate_right(11) ^ e.rotate_right(25);
let ch: u32 = (e & f) ^ ((!e) & g);
let t1: u32 = (*h)
.wrapping_add(s1)
.wrapping_add(ch)
.wrapping_add(k)
.wrapping_add(w);
let s0: u32 = a.rotate_right(2) ^ a.rotate_right(13) ^ a.rotate_right(22);
let maj: u32 = (a & b) ^ (a & c) ^ (b & c);
*d = (*d).wrapping_add(t1);
*h = t1.wrapping_add(s0.wrapping_add(maj));
}
#[inline(always)]
pub(crate) fn sha256_w_next(w0: u32, w1: u32, w9: u32, w14: u32) -> u32 {
let s0: u32 = w1.rotate_right(7) ^ w1.rotate_right(18) ^ (w1 >> 3);
let s1: u32 = w14.rotate_right(17) ^ w14.rotate_right(19) ^ (w14 >> 10);
w0.wrapping_add(s0).wrapping_add(w9).wrapping_add(s1)
}
#[inline(always)]
pub(crate) fn sha256_expand_16(w: &mut [u32; 16]) {
w[0] = sha256_w_next(w[0], w[1], w[9], w[14]);
w[1] = sha256_w_next(w[1], w[2], w[10], w[15]);
w[2] = sha256_w_next(w[2], w[3], w[11], w[0]);
w[3] = sha256_w_next(w[3], w[4], w[12], w[1]);
w[4] = sha256_w_next(w[4], w[5], w[13], w[2]);
w[5] = sha256_w_next(w[5], w[6], w[14], w[3]);
w[6] = sha256_w_next(w[6], w[7], w[15], w[4]);
w[7] = sha256_w_next(w[7], w[8], w[0], w[5]);
w[8] = sha256_w_next(w[8], w[9], w[1], w[6]);
w[9] = sha256_w_next(w[9], w[10], w[2], w[7]);
w[10] = sha256_w_next(w[10], w[11], w[3], w[8]);
w[11] = sha256_w_next(w[11], w[12], w[4], w[9]);
w[12] = sha256_w_next(w[12], w[13], w[5], w[10]);
w[13] = sha256_w_next(w[13], w[14], w[6], w[11]);
w[14] = sha256_w_next(w[14], w[15], w[7], w[12]);
w[15] = sha256_w_next(w[15], w[0], w[8], w[13]);
}
#[inline(always)]
pub(crate) fn sha256_rounds_16(v: &mut [u32; 8], w: &[u32; 16], k_base: usize) {
let [mut a, mut b, mut c, mut d, mut e, mut f, mut g, mut h]: [u32; 8] = *v;
sha256_step((a, b, c, &mut d), (e, f, g, &mut h), w[0], SHA256_K[k_base]);
sha256_step(
(h, a, b, &mut c),
(d, e, f, &mut g),
w[1],
SHA256_K[k_base + 1],
);
sha256_step(
(g, h, a, &mut b),
(c, d, e, &mut f),
w[2],
SHA256_K[k_base + 2],
);
sha256_step(
(f, g, h, &mut a),
(b, c, d, &mut e),
w[3],
SHA256_K[k_base + 3],
);
sha256_step(
(e, f, g, &mut h),
(a, b, c, &mut d),
w[4],
SHA256_K[k_base + 4],
);
sha256_step(
(d, e, f, &mut g),
(h, a, b, &mut c),
w[5],
SHA256_K[k_base + 5],
);
sha256_step(
(c, d, e, &mut f),
(g, h, a, &mut b),
w[6],
SHA256_K[k_base + 6],
);
sha256_step(
(b, c, d, &mut e),
(f, g, h, &mut a),
w[7],
SHA256_K[k_base + 7],
);
sha256_step(
(a, b, c, &mut d),
(e, f, g, &mut h),
w[8],
SHA256_K[k_base + 8],
);
sha256_step(
(h, a, b, &mut c),
(d, e, f, &mut g),
w[9],
SHA256_K[k_base + 9],
);
sha256_step(
(g, h, a, &mut b),
(c, d, e, &mut f),
w[10],
SHA256_K[k_base + 10],
);
sha256_step(
(f, g, h, &mut a),
(b, c, d, &mut e),
w[11],
SHA256_K[k_base + 11],
);
sha256_step(
(e, f, g, &mut h),
(a, b, c, &mut d),
w[12],
SHA256_K[k_base + 12],
);
sha256_step(
(d, e, f, &mut g),
(h, a, b, &mut c),
w[13],
SHA256_K[k_base + 13],
);
sha256_step(
(c, d, e, &mut f),
(g, h, a, &mut b),
w[14],
SHA256_K[k_base + 14],
);
sha256_step(
(b, c, d, &mut e),
(f, g, h, &mut a),
w[15],
SHA256_K[k_base + 15],
);
*v = [a, b, c, d, e, f, g, h];
}
pub(crate) fn sha256_compress(state: &mut [u32; 8], block: &[u8; 64]) {
let mut w: [u32; 16] = [
u32::from_be_bytes([block[0], block[1], block[2], block[3]]),
u32::from_be_bytes([block[4], block[5], block[6], block[7]]),
u32::from_be_bytes([block[8], block[9], block[10], block[11]]),
u32::from_be_bytes([block[12], block[13], block[14], block[15]]),
u32::from_be_bytes([block[16], block[17], block[18], block[19]]),
u32::from_be_bytes([block[20], block[21], block[22], block[23]]),
u32::from_be_bytes([block[24], block[25], block[26], block[27]]),
u32::from_be_bytes([block[28], block[29], block[30], block[31]]),
u32::from_be_bytes([block[32], block[33], block[34], block[35]]),
u32::from_be_bytes([block[36], block[37], block[38], block[39]]),
u32::from_be_bytes([block[40], block[41], block[42], block[43]]),
u32::from_be_bytes([block[44], block[45], block[46], block[47]]),
u32::from_be_bytes([block[48], block[49], block[50], block[51]]),
u32::from_be_bytes([block[52], block[53], block[54], block[55]]),
u32::from_be_bytes([block[56], block[57], block[58], block[59]]),
u32::from_be_bytes([block[60], block[61], block[62], block[63]]),
];
let mut v: [u32; 8] = *state;
sha256_rounds_16(&mut v, &w, 0);
sha256_expand_16(&mut w);
sha256_rounds_16(&mut v, &w, 16);
sha256_expand_16(&mut w);
sha256_rounds_16(&mut v, &w, 32);
sha256_expand_16(&mut w);
sha256_rounds_16(&mut v, &w, 48);
state[0] = state[0].wrapping_add(v[0]);
state[1] = state[1].wrapping_add(v[1]);
state[2] = state[2].wrapping_add(v[2]);
state[3] = state[3].wrapping_add(v[3]);
state[4] = state[4].wrapping_add(v[4]);
state[5] = state[5].wrapping_add(v[5]);
state[6] = state[6].wrapping_add(v[6]);
state[7] = state[7].wrapping_add(v[7]);
}
#[inline]
pub fn hmac_sha256(key: &[u8], data: &[u8]) -> [u8; 32] {
HmacSha256Key::new(key).mac(data)
}
#[inline]
pub fn constant_time_eq(a: &[u8], b: &[u8]) -> bool {
if a.len() != b.len() {
return false;
}
let mut diff: u8 = 0;
for i in 0..a.len() {
diff |= a[i] ^ b[i];
}
diff == 0
}
#[inline]
pub(crate) fn base64url_encode_into(input: &[u8], out: &mut Vec<u8>) {
let encoded_len: usize = (input.len() * 4).div_ceil(3);
out.reserve(encoded_len);
let (triples, rem): (&[[u8; 3]], &[u8]) = input.as_chunks::<3>();
for chunk in triples {
let triple: u32 = ((chunk[0] as u32) << 16) | ((chunk[1] as u32) << 8) | (chunk[2] as u32);
out.extend_from_slice(&[
BASE64URL_ALPHABET[((triple >> 18) & 0x3F) as usize],
BASE64URL_ALPHABET[((triple >> 12) & 0x3F) as usize],
BASE64URL_ALPHABET[((triple >> 6) & 0x3F) as usize],
BASE64URL_ALPHABET[(triple & 0x3F) as usize],
]);
}
if rem.len() == 1 {
let val: u32 = (rem[0] as u32) << 16;
out.extend_from_slice(&[
BASE64URL_ALPHABET[((val >> 18) & 0x3F) as usize],
BASE64URL_ALPHABET[((val >> 12) & 0x3F) as usize],
]);
} else if rem.len() == 2 {
let val: u32 = ((rem[0] as u32) << 16) | ((rem[1] as u32) << 8);
out.extend_from_slice(&[
BASE64URL_ALPHABET[((val >> 18) & 0x3F) as usize],
BASE64URL_ALPHABET[((val >> 12) & 0x3F) as usize],
BASE64URL_ALPHABET[((val >> 6) & 0x3F) as usize],
]);
}
}
#[inline]
pub fn base64url_encode(input: &[u8]) -> String {
let mut buf: Vec<u8> = Vec::with_capacity((input.len() * 4).div_ceil(3));
base64url_encode_into(input, &mut buf);
String::from_utf8(buf).unwrap_or_default()
}
#[inline]
pub(crate) fn decode_base64_into(bytes: &[u8], table: &[u8; 256], out: &mut [u8]) -> Option<usize> {
if bytes.len() % 4 == 1 {
return None;
}
let decoded_len: usize = (bytes.len() * 3) / 4;
if out.len() < decoded_len {
return None;
}
let mut pos: usize = 0;
let (quads, rem): (&[[u8; 4]], &[u8]) = bytes.as_chunks::<4>();
for chunk in quads {
let a: u8 = table[chunk[0] as usize];
let b: u8 = table[chunk[1] as usize];
let c: u8 = table[chunk[2] as usize];
let d: u8 = table[chunk[3] as usize];
let ab: u8 = a | b;
let cd: u8 = c | d;
if ((ab | cd) & 0x80) != 0 {
return None;
}
let quad: u32 = ((a as u32) << 18) | ((b as u32) << 12) | ((c as u32) << 6) | (d as u32);
out[pos] = ((quad >> 16) & 0xFF) as u8;
out[pos + 1] = ((quad >> 8) & 0xFF) as u8;
out[pos + 2] = (quad & 0xFF) as u8;
pos += 3;
}
if rem.len() == 2 {
let a: u8 = table[rem[0] as usize];
let b: u8 = table[rem[1] as usize];
if ((a | b) & 0x80) != 0 {
return None;
}
let val: u32 = ((a as u32) << 18) | ((b as u32) << 12);
out[pos] = ((val >> 16) & 0xFF) as u8;
pos += 1;
} else if rem.len() == 3 {
let a: u8 = table[rem[0] as usize];
let b: u8 = table[rem[1] as usize];
let c: u8 = table[rem[2] as usize];
let ab: u8 = a | b;
if ((ab | c) & 0x80) != 0 {
return None;
}
let val: u32 = ((a as u32) << 18) | ((b as u32) << 12) | ((c as u32) << 6);
out[pos] = ((val >> 16) & 0xFF) as u8;
out[pos + 1] = ((val >> 8) & 0xFF) as u8;
pos += 2;
}
Some(pos)
}
#[inline]
pub(crate) fn decode_base64url_32(input: &str) -> Option<[u8; 32]> {
let trimmed: &[u8] = input.trim_end_matches('=').as_bytes();
if trimmed.len() != HMAC_SIG_B64_LEN {
return None;
}
let mut out: [u8; 32] = [0u8; 32];
let written: usize = decode_base64_into(trimmed, &BASE64URL_DECODE_TABLE, &mut out)?;
if written == 32 { Some(out) } else { None }
}
#[inline]
pub(crate) fn decode_base64_inner(input: &str, url_safe: bool) -> Option<Vec<u8>> {
let bytes: &[u8] = input.trim_end_matches('=').as_bytes();
if bytes.len() % 4 == 1 {
return None;
}
let table: &[u8; 256] = if url_safe {
&BASE64URL_DECODE_TABLE
} else {
&BASE64_STD_DECODE_TABLE
};
let decoded_len: usize = (bytes.len() * 3) / 4;
let mut out: Vec<u8> = vec![0u8; decoded_len];
let written: usize = decode_base64_into(bytes, table, &mut out)?;
out.truncate(written);
Some(out)
}
#[inline]
pub fn base64url_decode(input: &str) -> Option<Vec<u8>> {
decode_base64_inner(input, true)
}
#[inline]
pub fn base64_decode(input: &str) -> Option<Vec<u8>> {
decode_base64_inner(input, false)
}
#[inline]
pub(crate) fn strip_auth_scheme<'a>(header: &'a str, lowercase_prefix: &str) -> Option<&'a str> {
let trimmed: &'a str = header.trim();
if trimmed.len() < lowercase_prefix.len() {
return None;
}
let (head, tail): (&'a str, &'a str) = trimmed.split_at(lowercase_prefix.len());
if head.eq_ignore_ascii_case(lowercase_prefix) {
let token: &'a str = tail.trim();
if token.is_empty() { None } else { Some(token) }
} else {
None
}
}
pub(crate) fn encode_hs256_header_b64(default_kid: Option<&str>) -> String {
let Some(kid) = default_kid else {
return DEFAULT_HS256_HEADER_B64.to_owned();
};
let header: JwtHeader = JwtHeader {
alg: JWT_ALG_HS256.to_owned(),
typ: Some(JWT_TYP_JWT.to_owned()),
kid: Some(kid.to_owned()),
};
let json_bytes: Vec<u8> = to_vec(&header).unwrap_or_default();
base64url_encode(&json_bytes)
}