SPB Git

Repository Public Apache-2.0

acvp_hash.rs 5.9 KB 186 lines rust

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : SPB Quantum Crypto7// File             : crates/spb-pqc-core/tests/acvp_hash.rs8// Module           : Tests9// Repository       : https://git.spboucher.ai/spb-quantum-crypto10// Project Website  : https://pqce.spboucher.ai11// Created          : 2026-12-0312// Last Modified    : 2026-12-0313//14// Author           : Simon-Pierre Boucher15// Position         : Professor of Finance16// Institution      : Université du Québec en Outaouais (UQO)17// Organization     : SP Boucher18// Website          : https://spboucher.ai19// Git              : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development      : dev@spboucher.ai23// Support          : support@spboucher.ai24// Security/Admin   : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License          : Apache-2.033//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041//! NIST ACVP known answers for SHA-2, SHA-3 and SHAKE.42//!43//! Scope actually replayed (reported by each test):44//! * AFT / VOT cases whose message and output lengths are whole bytes — the45//!   public API is byte-oriented, bit-length cases are counted as "not applicable";46//! * MCT (Monte Carlo) chains for SHA-2 and SHA-3;47//! * LDT (multi-gigabyte messages) is not replayed.4849mod common;50use common::*;51use spb_pqc_core::hash::{self, HashAlg};5253fn aft(name: &str, alg: HashAlg) -> (usize, usize) {54    let v = load(name);55    let (mut run, mut skipped) = (0, 0);56    for g in groups(&v) {57        let tt = s(g, "testType");58        if tt != "AFT" && tt != "VOT" {59            continue;60        }61        for t in tests(g) {62            let bits = t["len"].as_u64().unwrap();63            let out_bits = t.get("outLen").and_then(|x| x.as_u64());64            if bits % 8 != 0 || out_bits.is_some_and(|o| o % 8 != 0) {65                skipped += 1;66                continue;67            }68            let msg = if bits == 0 {69                Vec::new()70            } else {71                bytes(t, "msg")72            };73            assert_eq!(msg.len() as u64 * 8, bits);74            let got = match out_bits {75                Some(o) => hash::xof(alg, &msg, (o / 8) as usize).unwrap(),76                None => hash::digest(alg, &msg),77            };78            assert_eq!(hex(&got), s(t, "md"), "{name} tcId {}", t["tcId"]);79            run += 1;80        }81    }82    println!("{name}: {run} byte-aligned AFT/VOT vectors passed, {skipped} bit-length vectors not applicable");83    assert!(run > 0);84    (run, skipped)85}8687#[test]88fn sha2_256_aft() {89    aft("SHA2-256-1.0", HashAlg::Sha256);90}91#[test]92fn sha2_512_aft() {93    aft("SHA2-512-1.0", HashAlg::Sha512);94}95#[test]96fn sha3_256_aft() {97    aft("SHA3-256-2.0", HashAlg::Sha3_256);98}99#[test]100fn sha3_512_aft() {101    aft("SHA3-512-2.0", HashAlg::Sha3_512);102}103#[test]104fn shake_128_aft_vot() {105    aft("SHAKE-128-1.0", HashAlg::Shake128);106}107#[test]108fn shake_256_aft_vot() {109    aft("SHAKE-256-1.0", HashAlg::Shake256);110}111112/// SHA-2 Monte Carlo test, "standard" and "alternate" message construction113/// (ACVP SHA specification, §6.2).114fn sha2_mct(name: &str, alg: HashAlg) {115    let v = load(name);116    let mut chains = 0;117    for g in groups(&v) {118        if s(g, "testType") != "MCT" {119            continue;120        }121        let alternate = s(g, "mctVersion") == "alternate";122        for t in tests(g) {123            let mut seed = bytes(t, "msg");124            let initial_len = seed.len();125            for r in t["resultsArray"].as_array().unwrap() {126                let (mut a, mut b, mut c) = (seed.clone(), seed.clone(), seed.clone());127                for _ in 0..1000 {128                    let mut m = [a.as_slice(), b.as_slice(), c.as_slice()].concat();129                    if alternate {130                        m.resize(initial_len, 0);131                    }132                    let md = hash::digest(alg, &m);133                    a = b;134                    b = c;135                    c = md;136                }137                assert_eq!(hex(&c), s(r, "md"), "{name} MCT tcId {}", t["tcId"]);138                seed = c;139            }140            chains += 1;141        }142    }143    println!("{name}: {chains} MCT chain(s) of 100 x 1000 digests passed");144    assert!(chains > 0);145}146147/// SHA-3 Monte Carlo test (ACVP SHA3 specification): MD[i] = SHA3(MD[i-1]).148fn sha3_mct(name: &str, alg: HashAlg) {149    let v = load(name);150    let mut chains = 0;151    for g in groups(&v) {152        if s(g, "testType") != "MCT" || s(g, "mctVersion") != "standard" {153            continue;154        }155        for t in tests(g) {156            let mut md = bytes(t, "msg");157            for r in t["resultsArray"].as_array().unwrap() {158                for _ in 0..1000 {159                    md = hash::digest(alg, &md);160                }161                assert_eq!(hex(&md), s(r, "md"), "{name} MCT tcId {}", t["tcId"]);162            }163            chains += 1;164        }165    }166    println!("{name}: {chains} MCT chain(s) of 100 x 1000 digests passed");167    assert!(chains > 0);168}169170#[test]171fn sha2_256_mct() {172    sha2_mct("SHA2-256-1.0", HashAlg::Sha256);173}174#[test]175fn sha2_512_mct() {176    sha2_mct("SHA2-512-1.0", HashAlg::Sha512);177}178#[test]179fn sha3_256_mct() {180    sha3_mct("SHA3-256-2.0", HashAlg::Sha3_256);181}182#[test]183fn sha3_512_mct() {184    sha3_mct("SHA3-512-2.0", HashAlg::Sha3_512);185}186
Files main
Menu

Theme