Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/aienux/crates/aienos-crypto/crypto_test.om # omega-mv Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance. Brought by drakestapleton Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / aienux / crates / aienos-crypto / crypto_test.om` 285 lines ยท 6177 bytes Raw Blame History 1 note SPDX-License-Identifier: Apache-2.0 2 note aienux/crates/aienos-crypto/crypto_test.om: Behavioral self-test for aienos-crypto. 3 note Verifies FIPS 197 S-box & AES-256 C.2, RFC 8452 POLYVAL & AES-256-GCM-SIV Appendix C, 4 note FIPS 180-4 SHA-256, and RFC 4231 HMAC-SHA256 Case 1. 5 note Compose with: si/directives/om2.om om/tls/sha2.om om/tls/aesgcm.om om/tls/gcmsiv.om aienux/crates/aienos-crypto/crypto.om aienux/crates/aienos-crypto/crypto_test.om 6 7 text crt_abc "abc" 8 text crt_hi "Hi " 9 text crt_there "There" 10 11 fn crt_hex_nib c uchar to uchar 12 if and ge c 48 le c 57 13 ret sub c 48 14 end 15 if and ge c 97 le c 102 16 ret add sub c 97 10 17 end 18 if and ge c 65 le c 70 19 ret add sub c 65 10 20 end 21 ret 0 22 end 23 24 fn crt_from_hex src view uchar * dst edit uchar * to void 25 n = div len src 2 26 for i ulong n bound 256 27 hi = crt_hex_nib get src mul i 2 28 lo = crt_hex_nib get src add mul i 2 1 29 put dst i or shl hi 4 lo 30 end 31 end 32 33 text crt_aes_exp "`8ea2b7ca516745bfeafc49904b496089`" 34 text crt_pv_a "`66e94bd4ef8a2c3b884cfa59ca342b2e`" 35 text crt_pv_b "`ff000000000000000000000000000000`" 36 text crt_pv_exp "`ebe563401e7e91ea3ad6426b8140c394`" 37 text crt_pv_h "`25629347589242761d31f826ba4b757b`" 38 text crt_pv_x1 "`4f4f95668c83dfb6401762bb2d01a262`" 39 text crt_pv_x2 "`d1a24ddd2721d006bbe45f20d3c9f362`" 40 text crt_pv_wexp "`f7a3b47b846119fae5b7866cf5e5b77e`" 41 text crt_siv_k "`0100000000000000000000000000000000000000000000000000000000000000`" 42 text crt_siv_n "`030000000000000000000000`" 43 text crt_siv_pt1 "`01000000000000000000000000000000`" 44 text crt_siv_exp1 "`85a01b63025ba19b7fd3ddfc033b3e76c9eac6fa700942702e90862383c6c366`" 45 text crt_siv_pt2 "`0200000000000000`" 46 text crt_siv_aad2 "01" 47 text crt_siv_exp2 "`1de22967237a813291213f267e3b452f02d01ae33e4ec854`" 48 text crt_sha_empty "`e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855`" 49 text crt_sha_abc "`ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad`" 50 text crt_hmac_exp "`b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7`" 51 52 fn crt_test_sbox_and_aes to int 53 note Spot-check key FIPS 197 S-box entries and bijection across all 256 inputs 54 if ne cr_sbox_ct 0x00 0x63 55 ret 1 56 end 57 if ne cr_sbox_ct 0x01 0x7c 58 ret 2 59 end 60 if ne cr_sbox_ct 0x53 0xed 61 ret 3 62 end 63 if ne cr_sbox_ct 0x52 0x00 64 ret 4 65 end 66 if ne cr_sbox_ct 0xff 0x16 67 ret 5 68 end 69 seen = new uchar 256 0 70 for b ulong 256 71 s = ulong cr_sbox_ct uchar b 72 if ne get seen s 0 73 ret 6 74 end 75 put seen s 1 76 end 77 78 note NIST FIPS 197 Appendix C.2 (AES-256) 79 key = new uchar 32 0 80 for i ulong 32 81 put key i uchar i 82 end 83 blk = new uchar 16 0 84 for i ulong 16 85 put blk i uchar mul i 0x11 86 end 87 exp = new uchar 16 0 88 crt_from_hex crt_aes_exp exp 89 if ne cr_aes256_encrypt_block view key blk CR_OK 90 ret 7 91 end 92 if not cr_ct_eq view blk view exp 93 ret 8 94 end 95 ret 0 96 end 97 98 fn crt_test_polyval to int 99 note RFC 8452 Section 7 dot test vector 100 a = new uchar 16 0 101 b = new uchar 16 0 102 exp = new uchar 16 0 103 got = new uchar 16 0 104 crt_from_hex crt_pv_a a 105 crt_from_hex crt_pv_b b 106 crt_from_hex crt_pv_exp exp 107 if ne cr_polyval_dot view a view b got CR_OK 108 ret 10 109 end 110 if not cr_ct_eq view got view exp 111 ret 11 112 end 113 114 note RFC 8452 Appendix A worked example (2 blocks) 115 h = new uchar 16 0 116 x1 = new uchar 16 0 117 x2 = new uchar 16 0 118 wexp = new uchar 16 0 119 acc = new uchar 16 0 120 crt_from_hex crt_pv_h h 121 crt_from_hex crt_pv_x1 x1 122 crt_from_hex crt_pv_x2 x2 123 crt_from_hex crt_pv_wexp wexp 124 if ne cr_polyval_update_block view h acc view x1 CR_OK 125 ret 12 126 end 127 if ne cr_polyval_update_block view h acc view x2 CR_OK 128 ret 13 129 end 130 if not cr_ct_eq view acc view wexp 131 ret 14 132 end 133 ret 0 134 end 135 136 fn crt_test_gcmsiv_vec2 kn view uchar 44 to int 137 note RFC 8452 Appendix C: AES-256-GCM-SIV 8-byte PT, 1-byte AAD 138 pt2 = new uchar 8 0 139 aad2 = new uchar 1 0 140 exp2 = new uchar 24 0 141 ct2 = new uchar 24 0 142 crt_from_hex crt_siv_pt2 pt2 143 crt_from_hex crt_siv_aad2 aad2 144 crt_from_hex crt_siv_exp2 exp2 145 for i ulong 8 146 put ct2 i get pt2 i 147 end 148 if ne cr_gcmsiv_seal_kn kn view aad2 ct2 CR_OK 149 ret 28 150 end 151 if not cr_ct_eq view ct2 view exp2 152 ret 29 153 end 154 if ne cr_gcmsiv_open_kn kn view aad2 ct2 CR_OK 155 ret 30 156 end 157 if not cr_ct_eq view part ct2 0 8 view pt2 158 ret 31 159 end 160 161 note Tamper detection + mandatory zeroing of output buffer on failure 162 for i ulong 24 163 put ct2 i get exp2 i 164 end 165 put ct2 0 xor get ct2 0 1 166 if ne cr_gcmsiv_open_kn kn view aad2 ct2 CR_ERR_AUTH 167 ret 32 168 end 169 for i ulong 8 170 if ne get ct2 i 0 171 ret 33 172 end 173 end 174 ret 0 175 end 176 177 fn crt_test_gcmsiv to int 178 key = new uchar 32 0 179 nonce = new uchar 12 0 180 crt_from_hex crt_siv_k key 181 crt_from_hex crt_siv_n nonce 182 183 note Key derivation determinism check 184 ak1 = new uchar 16 0 185 ek1 = new uchar 32 0 186 ak2 = new uchar 16 0 187 ek2 = new uchar 32 0 188 if ne cr_gcmsiv_derive_keys view key view nonce ak1 ek1 CR_OK 189 ret 20 190 end 191 if ne cr_gcmsiv_derive_keys view key view nonce ak2 ek2 CR_OK 192 ret 21 193 end 194 if not cr_ct_eq view ak1 view ak2 195 ret 22 196 end 197 if not cr_ct_eq view ek1 view ek2 198 ret 23 199 end 200 201 note RFC 8452 Appendix C: AES-256-GCM-SIV 16-byte PT, 0-byte AAD 202 kn = new uchar 44 0 203 cr_gcmsiv_pack_kn view key view nonce kn 204 pt1 = new uchar 16 0 205 exp1 = new uchar 32 0 206 ct1 = new uchar 32 0 207 empty = new uchar 1 0 208 crt_from_hex crt_siv_pt1 pt1 209 crt_from_hex crt_siv_exp1 exp1 210 for i ulong 16 211 put ct1 i get pt1 i 212 end 213 if ne cr_gcmsiv_seal_kn view kn view part empty 0 0 ct1 CR_OK 214 ret 24 215 end 216 if not cr_ct_eq view ct1 view exp1 217 ret 25 218 end 219 if ne cr_gcmsiv_open_kn view kn view part empty 0 0 ct1 CR_OK 220 ret 26 221 end 222 if not cr_ct_eq view part ct1 0 16 view pt1 223 ret 27 224 end 225 226 ret crt_test_gcmsiv_vec2 view kn 227 end 228 229 fn crt_test_sha_hmac to int 230 empty = new uchar 1 0 231 dig = new uchar 32 0 232 exp = new uchar 32 0 233 234 note SHA-256("") 235 crt_from_hex crt_sha_empty exp 236 if ne cr_sha256 view part empty 0 0 dig CR_OK 237 ret 40 238 end 239 if not cr_ct_eq view dig view exp 240 ret 41 241 end 242 243 note SHA-256("abc") 244 crt_from_hex crt_sha_abc exp 245 if ne cr_sha256 crt_abc dig CR_OK 246 ret 42 247 end 248 if not cr_ct_eq view dig view exp 249 ret 43 250 end 251 252 note RFC 4231 Case 1 HMAC-SHA256 253 hkey = new uchar 32 0 254 for i ulong 20 255 put hkey i 0x0b 256 end 257 crt_from_hex crt_hmac_exp exp 258 if ne cr_hmac_sha256_2 view hkey crt_hi crt_there dig CR_OK 259 ret 44 260 end 261 if not cr_ct_eq view dig view exp 262 ret 45 263 end 264 ret 0 265 end 266 267 fn main to int 268 rc = crt_test_sbox_and_aes 269 if ne rc 0 270 ret rc 271 end 272 rc = crt_test_polyval 273 if ne rc 0 274 ret rc 275 end 276 rc = crt_test_gcmsiv 277 if ne rc 0 278 ret rc 279 end 280 rc = crt_test_sha_hmac 281 if ne rc 0 282 ret rc 283 end 284 ret 0 285 end