code wiki / (root) / nx_tls13_hrr_test.nx

nx_tls13_hrr_test.nx source

↩ module page · 75 lines · 2579 B

1// nx_tls13_hrr_test.nx -- KAT for HelloRetryRequest detection. 2// 3// expect_exit: 0 4// license_tier: ORIGINAL 5 6import "nx_syscalls.nx" 7import "nx_tls13.nx" 8import "nx_tls13_hello.nx" 9import "nx_tls13_hrr.nx" 10 11func main() -> i64 { 12 // ---- Test A: magic random byte-exact ---- 13 let magic: *u8 = sys_mmap(64) 14 tls13_hrr_magic_random(magic) 15 if (magic[0] & 0xff) != 0xcf { return 1 } 16 if (magic[1] & 0xff) != 0x21 { return 2 } 17 if (magic[7] & 0xff) != 0x11 { return 3 } 18 if (magic[15] & 0xff) != 0x91 { return 4 } 19 if (magic[23] & 0xff) != 0x5e { return 5 } 20 if (magic[31] & 0xff) != 0x9c { return 6 } 21 22 // ---- Test B: build a NORMAL ServerHello -> is_hrr returns 0 ---- 23 let sh: *u8 = sys_mmap(256) 24 sh[0] = HT_SERVER_HELLO & 0xff 25 sh[1] = 0; sh[2] = 0; sh[3] = 40 // body_len = 40 26 sh[4] = 0x03; sh[5] = 0x03 // legacy_version 27 // Random (not magic): 32 bytes starting with 0x10 28 var i: i64 = 0 29 while i < 32 { 30 sh[6 + i] = 0x10 + i 31 i = i + 1 32 } 33 sh[38] = 0 // session_id_len = 0 34 sh[39] = 0x13; sh[40] = 0x03 // cipher = ChaCha20-Poly1305 35 sh[41] = 0 // compression 36 sh[42] = 0; sh[43] = 0 // ext_list_len = 0 37 let is_normal: i64 = tls13_hello_is_hrr(sh, 44) 38 if is_normal != 0 { return 10 } 39 40 // ---- Test C: build an HRR (ServerHello with magic random) -> 1 ---- 41 let hrr: *u8 = sys_mmap(256) 42 hrr[0] = HT_SERVER_HELLO & 0xff 43 hrr[1] = 0; hrr[2] = 0; hrr[3] = 40 44 hrr[4] = 0x03; hrr[5] = 0x03 45 // Random = HRR magic 46 var j: i64 = 0 47 while j < 32 { 48 hrr[6 + j] = magic[j] 49 j = j + 1 50 } 51 hrr[38] = 0 52 hrr[39] = 0x13; hrr[40] = 0x03 53 hrr[41] = 0 54 hrr[42] = 0; hrr[43] = 0 55 let is_hrr: i64 = tls13_hello_is_hrr(hrr, 44) 56 if is_hrr != 1 { return 20 } 57 58 // ---- Test D: nearly-magic random (1 bit flipped) -> 0 ---- 59 hrr[6] = hrr[6] ^ 0x01 60 let near_miss: i64 = tls13_hello_is_hrr(hrr, 44) 61 if near_miss != 0 { return 30 } 62 // restore 63 hrr[6] = hrr[6] ^ 0x01 64 65 // ---- Test E: malformed (wrong msg_type) -> 0 ---- 66 sh[0] = HT_CLIENT_HELLO & 0xff 67 let wrong_type: i64 = tls13_hello_is_hrr(sh, 44) 68 if wrong_type != 0 { return 40 } 69 70 // ---- Test F: truncated message -> 0 ---- 71 let short_buf: i64 = tls13_hello_is_hrr(hrr, 10) 72 if short_buf != 0 { return 50 } 73 74 return 0 75}