code wiki / _hdl_build / nx_md5_kat.nx

nx_md5_kat.nx source

↩ module page · 35 lines · 2518 B

1// nx_md5_kat.nx -- SOVEREIGN known-answer test for nx_md5_canonical, using the RFC 1321 Appendix A.5 suite 2// (no oracle: the RFC vectors ARE the known answers). Explicit byte-lengths (so the empty case is a true n=0, 3// not a strlen of the "" literal), and 62- and 80-byte inputs so the MULTI-BLOCK path is exercised (the old 4// compile-only smoke only ever hashed <=3-byte single-block inputs). GREEN iff every vector matches. 5// license_tier: ORIGINAL 6import "nx_syscalls.nx" 7import "nx_md5_canonical.nx" 8import "nx_hex_codec.nx" 9 10func k_puts(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 11func k_eq(a: *u8, b: *u8) -> i64 { var i: i64=0; while b[i]!=(0 as u8){ if a[i]!=b[i]{return 0} i=i+1 } return 1 } 12func k_check(label: *u8, data: *u8, n: i64, exp: *u8) -> i64 { 13 let out: *u8=sys_mmap(16); md5(data, n, out) 14 let hx: *u8=sys_mmap(40); hex_encode(out, 16, hx); hx[32]=0 as u8 15 k_puts(label); k_puts(" -> " as *u8); k_puts(hx) 16 let ok: i64=k_eq(hx, exp) 17 if ok==1 { k_puts(" PASS\n" as *u8) } else { k_puts(" FAIL want " as *u8); k_puts(exp); k_puts("\n" as *u8) } 18 return ok 19} 20 21func main() -> i64 { 22 k_puts("=== MD5 KAT (RFC 1321 A.5; includes multi-block 62B/80B) ===\n" as *u8) 23 var pass: i64=0; let total: i64=7 24 pass=pass+k_check("[empty] " as *u8, "" as *u8, 0, "d41d8cd98f00b204e9800998ecf8427e" as *u8) 25 pass=pass+k_check("'a' " as *u8, "a" as *u8, 1, "0cc175b9c0f1b6a831c399e269772661" as *u8) 26 pass=pass+k_check("'abc' " as *u8, "abc" as *u8, 3, "900150983cd24fb0d6963f7d28e17f72" as *u8) 27 pass=pass+k_check("'message digest' " as *u8, "message digest" as *u8, 14, "f96b697d7cb7938d525a2f31aaf161d0" as *u8) 28 pass=pass+k_check("a..z " as *u8, "abcdefghijklmnopqrstuvwxyz" as *u8, 26, "c3fcd3d76192e4007dfb496cca67e13b" as *u8) 29 pass=pass+k_check("A..Za..z0..9 (62B) " as *u8, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" as *u8, 62, "d174ab98d277d9f5a5611c2c9f419d9f" as *u8) 30 pass=pass+k_check("8x1234567890 (80B) " as *u8, "12345678901234567890123456789012345678901234567890123456789012345678901234567890" as *u8, 80, "57edf4a22be3c955ac49da2e2107b67a" as *u8) 31 k_puts("----\nMD5-KAT pass=" as *u8) 32 let bb: *u8=sys_mmap(8); bb[0]=(48+pass) as u8; sys_write(1,bb,1); k_puts("/7\n" as *u8) 33 if pass==total { k_puts("MD5-KAT GREEN\n" as *u8); sys_exit(0); return 0 } 34 k_puts("MD5-KAT RED\n" as *u8); sys_exit(1); return 1 35}