code wiki / (root) / nx_sha256_sha384_coexist_kat_test.nx

nx_sha256_sha384_coexist_kat_test.nx source

↩ module page · 63 lines · 2150 B

1// nx_sha256_sha384_coexist_kat_test.nx -- regression KAT for the 2// top-level `func blk_set_byte(...)` collision that silently 3// corrupted SHA-256 whenever nx_sha384/nx_sha512 was also imported 4// (2026-05-20, root cause of cert chain verify failures). 5// 6// Imports BOTH nx_sha256.nx and nx_sha512.nx (where sha384 lives), 7// computes a SHA-256 over a known input, and asserts the digest 8// matches the FIPS 180-4 KAT for SHA-256("abc"). 9// 10// expect_exit: 0 11// license_tier: ORIGINAL 12 13import "nx_syscalls.nx" 14import "nx_sha256.nx" 15import "nx_sha512.nx" 16 17func main() -> i64 { 18 let abc: *u8 = sys_mmap(8) 19 abc[0]=0x61; abc[1]=0x62; abc[2]=0x63 // "abc" 20 21 let out: *u8 = sys_mmap(32) 22 sha256_digest(abc, 3, out) 23 24 // FIPS 180-4 KAT for SHA-256("abc"): 25 // ba 78 16 bf 8f 01 cf ea 41 41 40 de 5d ae 22 23 26 // b0 03 61 a3 96 17 7a 9c b4 10 ff 61 f2 00 15 ad 27 let exp: *u8 = sys_mmap(32) 28 exp[0]=0xba; exp[1]=0x78; exp[2]=0x16; exp[3]=0xbf 29 exp[4]=0x8f; exp[5]=0x01; exp[6]=0xcf; exp[7]=0xea 30 exp[8]=0x41; exp[9]=0x41; exp[10]=0x40; exp[11]=0xde 31 exp[12]=0x5d; exp[13]=0xae; exp[14]=0x22; exp[15]=0x23 32 exp[16]=0xb0; exp[17]=0x03; exp[18]=0x61; exp[19]=0xa3 33 exp[20]=0x96; exp[21]=0x17; exp[22]=0x7a; exp[23]=0x9c 34 exp[24]=0xb4; exp[25]=0x10; exp[26]=0xff; exp[27]=0x61 35 exp[28]=0xf2; exp[29]=0x00; exp[30]=0x15; exp[31]=0xad 36 37 var i: i64 = 0 38 while i < 32 { 39 if (out[i] & 0xff) != (exp[i] & 0xff) { return 10 + i } 40 i = i + 1 41 } 42 43 // Also exercise sha384 to confirm coexistence is real. 44 let h384: *u8 = sys_mmap(48) 45 sha384_digest(abc, 3, h384) 46 // SHA-384("abc") first byte should be 0xCB: 47 if (h384[0] & 0xff) != 0xCB { return 100 } 48 49 // Re-run SHA-256 AFTER SHA-384 to confirm no cross-pollution. 50 let out2: *u8 = sys_mmap(32) 51 sha256_digest(abc, 3, out2) 52 i = 0 53 while i < 32 { 54 if (out2[i] & 0xff) != (exp[i] & 0xff) { return 200 + i } 55 i = i + 1 56 } 57 58 let msg: *u8 = sys_mmap(64) 59 msg[0]=0x50; msg[1]=0x41; msg[2]=0x53; msg[3]=0x53 // "PASS" 60 msg[4]=0x0A 61 sys_write(1, msg, 5) 62 return 0 63}