code wiki / _hdl_build / nx_opq_probe1.nx

nx_opq_probe1.nx source

↩ module page · 65 lines · 2453 B

1// nx_opq_probe1.nx -- stage probe for nx_opaque_core: derive the RFC 9807 C.1.5 oprf_key. 2// seed = Expand(oprf_seed, cred_id || "OprfKey", Nok); (oprf_key,_) = DeriveKeyPair(seed, "OPAQUE-DeriveKeyPair") 3// expect oprf_key = 2dfb5cb9aa1476093be74ca0d43e5b02862a05f5d6972614d7433acdc66f7f31 4// license_tier: ORIGINAL 5import "hub/nx_opaque_core.nx" 6import "nx_syscalls.nx" 7 8func p1_w(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 9func p1_nib(c: i64) -> i64 { 10 if c >= 48 { if c <= 57 { return c - 48 } } 11 if c >= 97 { if c <= 102 { return c - 87 } } 12 return 0 13} 14func p1_hex(hexs: *u8, out: *u8) -> i64 { 15 var i: i64 = 0 16 while hexs[i*2] != (0 as u8) { 17 out[i] = ((p1_nib(hexs[i*2] as i64) << 4) | p1_nib(hexs[i*2+1] as i64)) as u8 18 i = i + 1 19 } 20 return i 21} 22func p1_dump(label: *u8, b: *u8, n: i64) -> i64 { 23 p1_w(label) 24 let hx: *u8 = "0123456789abcdef" as *u8 25 let line: *u8 = sys_mmap(2*n + 2) 26 var i: i64 = 0 27 while i < n { 28 line[i*2] = hx[((b[i] as i64) >> 4) & 15] 29 line[i*2+1] = hx[(b[i] as i64) & 15] 30 i = i + 1 31 } 32 line[2*n] = 10 as u8 33 sys_write(1, line, 2*n + 1) 34 return 0 35} 36 37func main() -> i64 { 38 let oprf_seed: *u8 = sys_mmap(32) 39 p1_hex("62f60b286d20ce4fd1d64809b0021dad6ed5d52a2c8cf27ae6582543a0a8dce2" as *u8, oprf_seed) 40 let want: *u8 = sys_mmap(32) 41 p1_hex("2dfb5cb9aa1476093be74ca0d43e5b02862a05f5d6972614d7433acdc66f7f31" as *u8, want) 42 43 // info = credential_identifier "1234" || "OprfKey" (11 bytes) 44 let info: *u8 = sys_mmap(16) 45 let cid: *u8 = "1234" as *u8 46 var i: i64 = 0 47 while i < 4 { info[i] = cid[i]; i = i + 1 } 48 let ok7: *u8 = "OprfKey" as *u8 49 var j: i64 = 0 50 while j < 7 { info[4 + j] = ok7[j]; j = j + 1 } 51 52 let seed: *u8 = sys_mmap(32) 53 if hkdf_expand(oprf_seed, info, 11, 32, seed) != 0 { p1_w("FAIL expand\n" as *u8); sys_exit(1) } 54 55 let oprf_key: *u8 = sys_mmap(32) 56 let dkp_info: *u8 = "OPAQUE-DeriveKeyPair" as *u8 57 let rc: i64 = nx_opq_derive_keypair(seed, dkp_info, 20, oprf_key, 0 as *u8) 58 if rc != NX_OPQ_OK { p1_w("FAIL derive_keypair rc\n" as *u8); sys_exit(1) } 59 60 p1_dump("oprf_key=" as *u8, oprf_key, 32) 61 if nx_opq_ct_eq(oprf_key, want, 32) != 1 { p1_w("FAIL oprf_key != RFC C.1.5\n" as *u8); sys_exit(1) } 62 p1_w("PROBE1 GREEN (Expand + HashToScalar + DeriveKeyPair == RFC)\n" as *u8) 63 sys_exit(0) 64 return 0 65}