SPB Git

Repository Public Apache-2.0

sha2.rs 20.8 KB 706 lines rust

1//! SHA-2 family (FIPS 180-4): SHA-224, SHA-256, SHA-384, SHA-512, SHA-512/224,2//! SHA-512/256, plus HMAC (FIPS 198-1) and MGF1 (RFC 8017 §B.2.1) as required by3//! SLH-DSA (FIPS 205 §11.2) and the pre-hash variants of FIPS 204 / 205.4//!5//! | FIPS 180-4                     | Here                              |6//! |--------------------------------|-----------------------------------|7//! | §4.1.2 / §4.1.3 functions      | `compress256` / `compress512`     |8//! | §4.2.2 / §4.2.3 constants      | `K256` / `K512` (cube roots, regenerated by `scripts/gen-sha2-constants.py`) |9//! | §5.1 padding                   | `finalize_into`                   |10//! | §5.3 initial hash values       | `IV_*`                            |11//! | §5.3.6 SHA-512/t IV generation | `sha512t_iv`                      |12//! | §6.2 / §6.4 hash computation   | [`Sha256`] / [`Sha512`]           |13//!14//! All arithmetic is on public-width words with no data-dependent branch.1516use crate::ct::wipe;1718const K256: [u32; 64] = [19    0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,20    0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,21    0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,22    0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,23    0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,24    0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,25    0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,26    0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,27];2829const K512: [u64; 80] = [30    0x428a2f98d728ae22,31    0x7137449123ef65cd,32    0xb5c0fbcfec4d3b2f,33    0xe9b5dba58189dbbc,34    0x3956c25bf348b538,35    0x59f111f1b605d019,36    0x923f82a4af194f9b,37    0xab1c5ed5da6d8118,38    0xd807aa98a3030242,39    0x12835b0145706fbe,40    0x243185be4ee4b28c,41    0x550c7dc3d5ffb4e2,42    0x72be5d74f27b896f,43    0x80deb1fe3b1696b1,44    0x9bdc06a725c71235,45    0xc19bf174cf692694,46    0xe49b69c19ef14ad2,47    0xefbe4786384f25e3,48    0x0fc19dc68b8cd5b5,49    0x240ca1cc77ac9c65,50    0x2de92c6f592b0275,51    0x4a7484aa6ea6e483,52    0x5cb0a9dcbd41fbd4,53    0x76f988da831153b5,54    0x983e5152ee66dfab,55    0xa831c66d2db43210,56    0xb00327c898fb213f,57    0xbf597fc7beef0ee4,58    0xc6e00bf33da88fc2,59    0xd5a79147930aa725,60    0x06ca6351e003826f,61    0x142929670a0e6e70,62    0x27b70a8546d22ffc,63    0x2e1b21385c26c926,64    0x4d2c6dfc5ac42aed,65    0x53380d139d95b3df,66    0x650a73548baf63de,67    0x766a0abb3c77b2a8,68    0x81c2c92e47edaee6,69    0x92722c851482353b,70    0xa2bfe8a14cf10364,71    0xa81a664bbc423001,72    0xc24b8b70d0f89791,73    0xc76c51a30654be30,74    0xd192e819d6ef5218,75    0xd69906245565a910,76    0xf40e35855771202a,77    0x106aa07032bbd1b8,78    0x19a4c116b8d2d0c8,79    0x1e376c085141ab53,80    0x2748774cdf8eeb99,81    0x34b0bcb5e19b48a8,82    0x391c0cb3c5c95a63,83    0x4ed8aa4ae3418acb,84    0x5b9cca4f7763e373,85    0x682e6ff3d6b2b8a3,86    0x748f82ee5defb2fc,87    0x78a5636f43172f60,88    0x84c87814a1f0ab72,89    0x8cc702081a6439ec,90    0x90befffa23631e28,91    0xa4506cebde82bde9,92    0xbef9a3f7b2c67915,93    0xc67178f2e372532b,94    0xca273eceea26619c,95    0xd186b8c721c0c207,96    0xeada7dd6cde0eb1e,97    0xf57d4f7fee6ed178,98    0x06f067aa72176fba,99    0x0a637dc5a2c898a6,100    0x113f9804bef90dae,101    0x1b710b35131c471b,102    0x28db77f523047d84,103    0x32caab7b40c72493,104    0x3c9ebe0a15c9bebc,105    0x431d67c49c100d4c,106    0x4cc5d4becb3e42b6,107    0x597f299cfc657e2a,108    0x5fcb6fab3ad6faec,109    0x6c44198c4a475817,110];111112const IV_256: [u32; 8] = [113    0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19,114];115const IV_224: [u32; 8] = [116    0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939, 0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4,117];118const IV_512: [u64; 8] = [119    0x6a09e667f3bcc908,120    0xbb67ae8584caa73b,121    0x3c6ef372fe94f82b,122    0xa54ff53a5f1d36f1,123    0x510e527fade682d1,124    0x9b05688c2b3e6c1f,125    0x1f83d9abfb41bd6b,126    0x5be0cd19137e2179,127];128const IV_384: [u64; 8] = [129    0xcbbb9d5dc1059ed8,130    0x629a292a367cd507,131    0x9159015a3070dd17,132    0x152fecd8f70e5939,133    0x67332667ffc00b31,134    0x8eb44a8768581511,135    0xdb0c2e0d64f98fa7,136    0x47b5481dbefa4fa4,137];138139fn compress256(h: &mut [u32; 8], block: &[u8]) {140    debug_assert_eq!(block.len(), 64);141    let mut w = [0u32; 64];142    for (t, c) in block.chunks_exact(4).enumerate() {143        w[t] = u32::from_be_bytes([c[0], c[1], c[2], c[3]]);144    }145    for t in 16..64 {146        let s0 = w[t - 15].rotate_right(7) ^ w[t - 15].rotate_right(18) ^ (w[t - 15] >> 3);147        let s1 = w[t - 2].rotate_right(17) ^ w[t - 2].rotate_right(19) ^ (w[t - 2] >> 10);148        w[t] = s1149            .wrapping_add(w[t - 7])150            .wrapping_add(s0)151            .wrapping_add(w[t - 16]);152    }153    let [mut a, mut b, mut c, mut d, mut e, mut f, mut g, mut hh] = *h;154    for t in 0..64 {155        let big1 = e.rotate_right(6) ^ e.rotate_right(11) ^ e.rotate_right(25);156        let ch = (e & f) ^ (!e & g);157        let t1 = hh158            .wrapping_add(big1)159            .wrapping_add(ch)160            .wrapping_add(K256[t])161            .wrapping_add(w[t]);162        let big0 = a.rotate_right(2) ^ a.rotate_right(13) ^ a.rotate_right(22);163        let maj = (a & b) ^ (a & c) ^ (b & c);164        let t2 = big0.wrapping_add(maj);165        hh = g;166        g = f;167        f = e;168        e = d.wrapping_add(t1);169        d = c;170        c = b;171        b = a;172        a = t1.wrapping_add(t2);173    }174    for (s, v) in h.iter_mut().zip([a, b, c, d, e, f, g, hh]) {175        *s = s.wrapping_add(v);176    }177    wipe(&mut w);178}179180fn compress512(h: &mut [u64; 8], block: &[u8]) {181    debug_assert_eq!(block.len(), 128);182    let mut w = [0u64; 80];183    for (t, c) in block.chunks_exact(8).enumerate() {184        w[t] = u64::from_be_bytes([c[0], c[1], c[2], c[3], c[4], c[5], c[6], c[7]]);185    }186    for t in 16..80 {187        let s0 = w[t - 15].rotate_right(1) ^ w[t - 15].rotate_right(8) ^ (w[t - 15] >> 7);188        let s1 = w[t - 2].rotate_right(19) ^ w[t - 2].rotate_right(61) ^ (w[t - 2] >> 6);189        w[t] = s1190            .wrapping_add(w[t - 7])191            .wrapping_add(s0)192            .wrapping_add(w[t - 16]);193    }194    let [mut a, mut b, mut c, mut d, mut e, mut f, mut g, mut hh] = *h;195    for t in 0..80 {196        let big1 = e.rotate_right(14) ^ e.rotate_right(18) ^ e.rotate_right(41);197        let ch = (e & f) ^ (!e & g);198        let t1 = hh199            .wrapping_add(big1)200            .wrapping_add(ch)201            .wrapping_add(K512[t])202            .wrapping_add(w[t]);203        let big0 = a.rotate_right(28) ^ a.rotate_right(34) ^ a.rotate_right(39);204        let maj = (a & b) ^ (a & c) ^ (b & c);205        let t2 = big0.wrapping_add(maj);206        hh = g;207        g = f;208        f = e;209        e = d.wrapping_add(t1);210        d = c;211        c = b;212        b = a;213        a = t1.wrapping_add(t2);214    }215    for (s, v) in h.iter_mut().zip([a, b, c, d, e, f, g, hh]) {216        *s = s.wrapping_add(v);217    }218    wipe(&mut w);219}220221/// SHA-256 / SHA-224 streaming state. `Clone` allows mid-state reuse (SLH-DSA222/// absorbs the padded `PK.seed` block once and clones the state per call).223#[derive(Clone)]224pub struct Sha256 {225    h: [u32; 8],226    buf: [u8; 64],227    buf_len: usize,228    total: u64,229    out_len: usize,230}231232impl Sha256 {233    /// SHA-256.234    pub const fn new() -> Self {235        Sha256 {236            h: IV_256,237            buf: [0; 64],238            buf_len: 0,239            total: 0,240            out_len: 32,241        }242    }243    /// SHA-224.244    pub const fn new_224() -> Self {245        Sha256 {246            h: IV_224,247            buf: [0; 64],248            buf_len: 0,249            total: 0,250            out_len: 28,251        }252    }253    /// Digest length in bytes.254    pub const fn output_len(&self) -> usize {255        self.out_len256    }257    /// Absorbs more input.258    pub fn update(&mut self, mut data: &[u8]) {259        self.total = self.total.wrapping_add(data.len() as u64);260        if self.buf_len > 0 {261            let take = core::cmp::min(64 - self.buf_len, data.len());262            self.buf[self.buf_len..self.buf_len + take].copy_from_slice(&data[..take]);263            self.buf_len += take;264            data = &data[take..];265            if self.buf_len == 64 {266                compress256(&mut self.h, &self.buf);267                self.buf_len = 0;268            }269        }270        while data.len() >= 64 {271            compress256(&mut self.h, &data[..64]);272            data = &data[64..];273        }274        if !data.is_empty() {275            self.buf[..data.len()].copy_from_slice(data);276            self.buf_len = data.len();277        }278    }279    /// Pads (FIPS 180-4 §5.1.1) and writes the first `out.len()` digest bytes;280    /// `out.len()` must not exceed `output_len()`.281    pub fn finalize_into(mut self, out: &mut [u8]) {282        assert!(out.len() <= self.out_len);283        let bit_len = self.total.wrapping_mul(8);284        let mut pad = [0u8; 72];285        pad[0] = 0x80;286        let pad_len = if self.buf_len < 56 {287            56 - self.buf_len288        } else {289            120 - self.buf_len290        };291        pad[pad_len..pad_len + 8].copy_from_slice(&bit_len.to_be_bytes());292        self.update_no_count(&pad[..pad_len + 8]);293        debug_assert_eq!(self.buf_len, 0);294        let mut full = [0u8; 32];295        for (c, w) in full.chunks_exact_mut(4).zip(self.h.iter()) {296            c.copy_from_slice(&w.to_be_bytes());297        }298        out.copy_from_slice(&full[..out.len()]);299        wipe(&mut full);300    }301    fn update_no_count(&mut self, data: &[u8]) {302        let saved = self.total;303        self.update(data);304        self.total = saved;305    }306}307308impl Default for Sha256 {309    fn default() -> Self {310        Self::new()311    }312}313314impl Drop for Sha256 {315    fn drop(&mut self) {316        wipe(&mut self.h);317        wipe(&mut self.buf);318    }319}320321/// SHA-512 / SHA-384 / SHA-512/224 / SHA-512/256 streaming state.322#[derive(Clone)]323pub struct Sha512 {324    h: [u64; 8],325    buf: [u8; 128],326    buf_len: usize,327    total: u128,328    out_len: usize,329}330331/// SHA-512/t IV generation function (FIPS 180-4 §5.3.6).332fn sha512t_iv(t: u32) -> [u64; 8] {333    let mut h = IV_512;334    for w in h.iter_mut() {335        *w ^= 0xa5a5_a5a5_a5a5_a5a5;336    }337    let mut st = Sha512 {338        h,339        buf: [0; 128],340        buf_len: 0,341        total: 0,342        out_len: 64,343    };344    st.update(format!("SHA-512/{t}").as_bytes());345    let mut out = [0u8; 64];346    st.finalize_into(&mut out);347    let mut iv = [0u64; 8];348    for (w, c) in iv.iter_mut().zip(out.chunks_exact(8)) {349        *w = u64::from_be_bytes([c[0], c[1], c[2], c[3], c[4], c[5], c[6], c[7]]);350    }351    iv352}353354impl Sha512 {355    /// SHA-512.356    pub const fn new() -> Self {357        Sha512 {358            h: IV_512,359            buf: [0; 128],360            buf_len: 0,361            total: 0,362            out_len: 64,363        }364    }365    /// SHA-384.366    pub const fn new_384() -> Self {367        Sha512 {368            h: IV_384,369            buf: [0; 128],370            buf_len: 0,371            total: 0,372            out_len: 48,373        }374    }375    /// SHA-512/224.376    pub fn new_512_224() -> Self {377        Sha512 {378            h: sha512t_iv(224),379            buf: [0; 128],380            buf_len: 0,381            total: 0,382            out_len: 28,383        }384    }385    /// SHA-512/256.386    pub fn new_512_256() -> Self {387        Sha512 {388            h: sha512t_iv(256),389            buf: [0; 128],390            buf_len: 0,391            total: 0,392            out_len: 32,393        }394    }395    /// Digest length in bytes.396    pub const fn output_len(&self) -> usize {397        self.out_len398    }399    /// Absorbs more input.400    pub fn update(&mut self, mut data: &[u8]) {401        self.total = self.total.wrapping_add(data.len() as u128);402        if self.buf_len > 0 {403            let take = core::cmp::min(128 - self.buf_len, data.len());404            self.buf[self.buf_len..self.buf_len + take].copy_from_slice(&data[..take]);405            self.buf_len += take;406            data = &data[take..];407            if self.buf_len == 128 {408                compress512(&mut self.h, &self.buf);409                self.buf_len = 0;410            }411        }412        while data.len() >= 128 {413            compress512(&mut self.h, &data[..128]);414            data = &data[128..];415        }416        if !data.is_empty() {417            self.buf[..data.len()].copy_from_slice(data);418            self.buf_len = data.len();419        }420    }421    /// Pads (FIPS 180-4 §5.1.2) and writes the first `out.len()` digest bytes.422    pub fn finalize_into(mut self, out: &mut [u8]) {423        assert!(out.len() <= self.out_len);424        let bit_len = self.total.wrapping_mul(8);425        let mut pad = [0u8; 144];426        pad[0] = 0x80;427        let pad_len = if self.buf_len < 112 {428            112 - self.buf_len429        } else {430            240 - self.buf_len431        };432        pad[pad_len..pad_len + 16].copy_from_slice(&bit_len.to_be_bytes());433        let saved = self.total;434        self.update(&pad[..pad_len + 16]);435        self.total = saved;436        debug_assert_eq!(self.buf_len, 0);437        let mut full = [0u8; 64];438        for (c, w) in full.chunks_exact_mut(8).zip(self.h.iter()) {439            c.copy_from_slice(&w.to_be_bytes());440        }441        out.copy_from_slice(&full[..out.len()]);442        wipe(&mut full);443    }444}445446impl Default for Sha512 {447    fn default() -> Self {448        Self::new()449    }450}451452impl Drop for Sha512 {453    fn drop(&mut self) {454        wipe(&mut self.h);455        wipe(&mut self.buf);456    }457}458459/// One-shot SHA-256 over the concatenation of `parts`.460pub fn sha256(parts: &[&[u8]]) -> [u8; 32] {461    let mut h = Sha256::new();462    for p in parts {463        h.update(p);464    }465    let mut out = [0u8; 32];466    h.finalize_into(&mut out);467    out468}469470/// One-shot SHA-512 over the concatenation of `parts`.471pub fn sha512(parts: &[&[u8]]) -> [u8; 64] {472    let mut h = Sha512::new();473    for p in parts {474        h.update(p);475    }476    let mut out = [0u8; 64];477    h.finalize_into(&mut out);478    out479}480481/// HMAC-SHA-256 (FIPS 198-1) over the concatenation of `parts`.482pub fn hmac_sha256(key: &[u8], parts: &[&[u8]]) -> [u8; 32] {483    let mut k0 = [0u8; 64];484    if key.len() > 64 {485        k0[..32].copy_from_slice(&sha256(&[key]));486    } else {487        k0[..key.len()].copy_from_slice(key);488    }489    let mut ipad = [0x36u8; 64];490    let mut opad = [0x5cu8; 64];491    for i in 0..64 {492        ipad[i] ^= k0[i];493        opad[i] ^= k0[i];494    }495    let mut inner = Sha256::new();496    inner.update(&ipad);497    for p in parts {498        inner.update(p);499    }500    let mut ih = [0u8; 32];501    inner.finalize_into(&mut ih);502    let out = sha256(&[&opad, &ih]);503    wipe(&mut k0);504    wipe(&mut ipad);505    wipe(&mut opad);506    wipe(&mut ih);507    out508}509510/// HMAC-SHA-512 (FIPS 198-1) over the concatenation of `parts`.511pub fn hmac_sha512(key: &[u8], parts: &[&[u8]]) -> [u8; 64] {512    let mut k0 = [0u8; 128];513    if key.len() > 128 {514        k0[..64].copy_from_slice(&sha512(&[key]));515    } else {516        k0[..key.len()].copy_from_slice(key);517    }518    let mut ipad = [0x36u8; 128];519    let mut opad = [0x5cu8; 128];520    for i in 0..128 {521        ipad[i] ^= k0[i];522        opad[i] ^= k0[i];523    }524    let mut inner = Sha512::new();525    inner.update(&ipad);526    for p in parts {527        inner.update(p);528    }529    let mut ih = [0u8; 64];530    inner.finalize_into(&mut ih);531    let out = sha512(&[&opad, &ih]);532    wipe(&mut k0);533    wipe(&mut ipad);534    wipe(&mut opad);535    wipe(&mut ih);536    out537}538539/// MGF1 with SHA-256 (RFC 8017 §B.2.1): `out = T[0..out.len()]`,540/// `T = Hash(seed || C(0)) || Hash(seed || C(1)) || …` with 4-byte big-endian counters.541pub fn mgf1_sha256(seed_parts: &[&[u8]], out: &mut [u8]) {542    for (counter, chunk) in out.chunks_mut(32).enumerate() {543        let mut h = Sha256::new();544        for p in seed_parts {545            h.update(p);546        }547        h.update(&(counter as u32).to_be_bytes());548        let mut block = [0u8; 32];549        h.finalize_into(&mut block);550        chunk.copy_from_slice(&block[..chunk.len()]);551    }552}553554/// MGF1 with SHA-512 (RFC 8017 §B.2.1).555pub fn mgf1_sha512(seed_parts: &[&[u8]], out: &mut [u8]) {556    for (counter, chunk) in out.chunks_mut(64).enumerate() {557        let mut h = Sha512::new();558        for p in seed_parts {559            h.update(p);560        }561        h.update(&(counter as u32).to_be_bytes());562        let mut block = [0u8; 64];563        h.finalize_into(&mut block);564        chunk.copy_from_slice(&block[..chunk.len()]);565    }566}567568#[cfg(test)]569mod tests {570    use super::*;571572    fn hex(b: &[u8]) -> String {573        b.iter().map(|x| format!("{x:02x}")).collect()574    }575576    #[test]577    fn sha256_fips180_examples() {578        assert_eq!(579            hex(&sha256(&[b"abc"])),580            "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"581        );582        assert_eq!(583            hex(&sha256(&[b""])),584            "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"585        );586        assert_eq!(587            hex(&sha256(&[588                b"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"589            ])),590            "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1"591        );592    }593594    #[test]595    fn sha512_fips180_examples() {596        assert_eq!(597            hex(&sha512(&[b"abc"])),598            "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a\599             2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f"600        );601    }602603    #[test]604    fn truncated_variants() {605        let mut o = [0u8; 28];606        let mut h = Sha256::new_224();607        h.update(b"abc");608        h.finalize_into(&mut o);609        assert_eq!(610            hex(&o),611            "23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7"612        );613614        let mut o = [0u8; 48];615        let mut h = Sha512::new_384();616        h.update(b"abc");617        h.finalize_into(&mut o);618        assert_eq!(619            hex(&o),620            "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed\621             8086072ba1e7cc2358baeca134c825a7"622        );623624        // FIPS 180-4 §5.3.6.1 / §5.3.6.2 list these IVs explicitly.625        assert_eq!(sha512t_iv(224)[0], 0x8C3D37C819544DA2);626        assert_eq!(sha512t_iv(224)[7], 0x1112E6AD91D692A1);627        assert_eq!(sha512t_iv(256)[0], 0x22312194FC2BF72C);628        assert_eq!(sha512t_iv(256)[7], 0x0EB72DDC81C52CA2);629630        let mut o = [0u8; 32];631        let mut h = Sha512::new_512_256();632        h.update(b"abc");633        h.finalize_into(&mut o);634        assert_eq!(635            hex(&o),636            "53048e2681941ef99b2e29b76b4c7dabe4c2d0c634fc6d46e0e2f13107e7af23"637        );638639        let mut o = [0u8; 28];640        let mut h = Sha512::new_512_224();641        h.update(b"abc");642        h.finalize_into(&mut o);643        assert_eq!(644            hex(&o),645            "4634270f707b6a54daae7530460842e20e37ed265ceee9a43e8924aa"646        );647    }648649    #[test]650    fn padding_boundaries_incremental() {651        // Lengths around the 55/56/64 and 111/112/128 byte padding edges.652        let data: Vec<u8> = (0..300u32).map(|i| (i * 31 + 5) as u8).collect();653        for len in [654            0usize, 1, 54, 55, 56, 57, 63, 64, 65, 111, 112, 113, 127, 128, 129, 255, 300,655        ] {656            let one = sha256(&[&data[..len]]);657            let mut h = Sha256::new();658            for b in &data[..len] {659                h.update(core::slice::from_ref(b));660            }661            let mut inc = [0u8; 32];662            h.finalize_into(&mut inc);663            assert_eq!(one, inc, "sha256 len {len}");664665            let one = sha512(&[&data[..len]]);666            let mut h = Sha512::new();667            for b in &data[..len] {668                h.update(core::slice::from_ref(b));669            }670            let mut inc = [0u8; 64];671            h.finalize_into(&mut inc);672            assert_eq!(one, inc, "sha512 len {len}");673        }674    }675676    #[test]677    fn hmac_rfc4231_case_1_and_2() {678        let key = [0x0bu8; 20];679        assert_eq!(680            hex(&hmac_sha256(&key, &[b"Hi There"])),681            "b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7"682        );683        assert_eq!(684            hex(&hmac_sha512(&key, &[b"Hi There"])),685            "87aa7cdea5ef619d4ff0b4241a1d6cb02379f4e2ce4ec2787ad0b30545e17cde\686             daa833b7d6b8a702038b274eaea3f4e4be9d914eeb61f1702e696c203a126854"687        );688        assert_eq!(689            hex(&hmac_sha256(690                b"Jefe",691                &[b"what do ya want ", b"for nothing?"]692            )),693            "5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843"694        );695        // RFC 4231 case 6: key longer than the block size.696        let key = [0xaau8; 131];697        assert_eq!(698            hex(&hmac_sha256(699                &key,700                &[b"Test Using Larger Than Block-Size Key - Hash Key First"]701            )),702            "60e431591ee0b67f0d8a26aacbf5b77f8e0bc6213728c5140546040f0ee37f54"703        );704    }705}706
Files main
Menu

Theme