code wiki / (root) / nx_aesni_kat.nx

nx_aesni_kat.nx source

↩ module page · 42 lines · 1690 B

1// nx_aesni_kat.nx -- end-to-end proof of the AES-NI pipeline (assembler mnemonics + 2// compiler __aes128_enc_block intrinsic). FIPS-197 Appendix C.1 AES-128 vector: 3// key = 00 01 02 ... 0f 4// in = 00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff 5// out = 69 c4 e0 d8 6a 7b 04 30 d8 cd b7 80 70 b4 c5 5a 6// Key expansion stays software (one-time); the block cipher runs on AES-NI. 7// exit 0 = hardware AES-NI produced the exact FIPS ciphertext. 8// 9// expect_exit: 0 10 11import "nx_syscalls.nx" 12import "nx_aes128_gcm.nx" 13 14func main() -> i64 { 15 let key: *u8 = sys_mmap(16) 16 var i: i64 = 0 17 while i < 16 { key[i] = i as u8; i = i + 1 } // 00 01 .. 0f 18 let sched: *u8 = sys_mmap(176) 19 aes128_expand_key(key, sched) 20 21 let blk: *u8 = sys_mmap(16) 22 blk[0]=0x00 as u8; blk[1]=0x11 as u8; blk[2]=0x22 as u8; blk[3]=0x33 as u8 23 blk[4]=0x44 as u8; blk[5]=0x55 as u8; blk[6]=0x66 as u8; blk[7]=0x77 as u8 24 blk[8]=0x88 as u8; blk[9]=0x99 as u8; blk[10]=0xaa as u8; blk[11]=0xbb as u8 25 blk[12]=0xcc as u8; blk[13]=0xdd as u8; blk[14]=0xee as u8; blk[15]=0xff as u8 26 27 let r: i64 = __aes128_enc_block(blk, sched) // <-- hardware AES-NI 28 29 let ct: *u8 = sys_mmap(16) 30 ct[0]=0x69 as u8; ct[1]=0xc4 as u8; ct[2]=0xe0 as u8; ct[3]=0xd8 as u8 31 ct[4]=0x6a as u8; ct[5]=0x7b as u8; ct[6]=0x04 as u8; ct[7]=0x30 as u8 32 ct[8]=0xd8 as u8; ct[9]=0xcd as u8; ct[10]=0xb7 as u8; ct[11]=0x80 as u8 33 ct[12]=0x70 as u8; ct[13]=0xb4 as u8; ct[14]=0xc5 as u8; ct[15]=0x5a as u8 34 35 i = 0 36 while i < 16 { 37 if (blk[i]&0xff) != (ct[i]&0xff) { sys_exit(1 + i); return 1 } 38 i = i + 1 39 } 40 sys_exit(0) 41 return 0 42}