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
omega-mv / om / tls / test / fips / driver.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note driver.om: test driver for om/tls/fips.om (program mode, built with om/tls/build.sh after the |
| 3 | note whole library). usage: driver [drbgerr] |
| 4 | note 1. pre-operational self-tests: fips_check 0 (KATs), fips_check 1 (DRBG KAT); state words |
| 5 | note 2. entropy input: 4096 health-tested getrandom bytes; the start-up test ran; every drawn |
| 6 | note sample entered the APT window (1024 + 4096 + 32 samples leave its position at 32) |
| 7 | note 3. health-test controls on fips_ht: a run of 7 equal samples passes and 8 trip the RCT |
| 8 | note (cut-off 8); a window with 34 copies of its first sample passes and 35 trip the APT |
| 9 | note (W 512, cut-off 35) |
| 10 | note 4. approved-mode DRBG: instantiate, two prediction-resistant requests (64 and 32 bytes) whose |
| 11 | note first 32 bytes differ; the reseed counter shows a reseed before each request |
| 12 | note 5. X25519 pairwise consistency test on a good key pair; tls_rand in both modes; tls_wipe |
| 13 | note zeroes the approved-mode DRBG state in c |
| 14 | note 6. the latch: a wrong public key fails the PCT (detail 21) or, with drbgerr, a DRBG in its |
| 15 | note error state fails the request (detail 20); afterwards entropy, DRBG, fips_check, |
| 16 | note tls_connect (TLS_ERR_SELFTEST, before any I/O), tls_seal and tls_open all refuse |
| 17 | note When a self-test fails in step 1 the driver prints "REFUSED detail D", runs the refusal |
| 18 | note checks of step 6 and exits 3 when they hold. Otherwise prints "FAIL check N got G want W" per |
| 19 | note failed check and "fips driver: C checks, F failed"; exit 0 only when F is 0. |
| 20 | |
| 21 | global fdob uchar 32 |
| 22 | global fdst ulong 4 |
| 23 | text fdtf "FAIL check " |
| 24 | text fdtg " got " |
| 25 | text fdtw " want " |
| 26 | text fdtr "REFUSED detail " |
| 27 | text fdts "fips driver: " |
| 28 | text fdtc " checks, " |
| 29 | text fdtx " failed\n" |
| 30 | text fdtnl "\n" |
| 31 | text fdhost "fips.example" |
| 32 | note "TLS_ERR_SELFTEST" and "drbgerr" as hex (fips_cmp compares against hex texts) |
| 33 | text fdhexn "544c535f4552525f53454c4654455354" |
| 34 | text fdhexm "64726267657272" |
| 35 | text fdhexe "656367656e657272" |
| 36 | |
| 37 | fn fdprn v ulong to void |
| 38 | x = v |
| 39 | k = ulong 0 |
| 40 | while true bound 21 |
| 41 | put fdob sub 31 k uchar add 48 rem x 10 |
| 42 | k = add k 1 |
| 43 | x = div x 10 |
| 44 | if eq x 0 |
| 45 | break |
| 46 | end |
| 47 | end |
| 48 | r = write 1 fdob sub 32 k k |
| 49 | end |
| 50 | |
| 51 | note one check: id, got, want |
| 52 | fn fdt id ulong got ulong want ulong to void |
| 53 | put fdst 0 add get fdst 0 1 |
| 54 | if ne got want |
| 55 | put fdst 1 add get fdst 1 1 |
| 56 | r = write 1 fdtf 0 len fdtf |
| 57 | fdprn id |
| 58 | r = write 1 fdtg 0 len fdtg |
| 59 | fdprn got |
| 60 | r = write 1 fdtw 0 len fdtw |
| 61 | fdprn want |
| 62 | r = write 1 fdtnl 0 1 |
| 63 | end |
| 64 | end |
| 65 | |
| 66 | note 1 when a holds a nonzero byte |
| 67 | fn fdnz a view uchar * to ulong |
| 68 | d = ulong 0 |
| 69 | for i ulong len a |
| 70 | d = or d ulong get a i |
| 71 | end |
| 72 | if eq d 0 |
| 73 | ret 0 |
| 74 | end |
| 75 | ret 1 |
| 76 | end |
| 77 | |
| 78 | note 1 when a and b (same length) differ |
| 79 | fn fddiff a view uchar * b view uchar * to ulong |
| 80 | d = ulong 0 |
| 81 | for i ulong len a |
| 82 | d = or d xor ulong get a i ulong get b i |
| 83 | end |
| 84 | if eq d 0 |
| 85 | ret 0 |
| 86 | end |
| 87 | ret 1 |
| 88 | end |
| 89 | |
| 90 | note reset the health-test state (not the start-up flag) |
| 91 | fn fdhr to void |
| 92 | for i ulong 6 |
| 93 | put FPST add 3 i 0 |
| 94 | end |
| 95 | end |
| 96 | |
| 97 | note feed n copies of sample x, then one sample y |
| 98 | fn fdrun x ulong n ulong y ulong to ulong |
| 99 | fdhr |
| 100 | for i ulong n |
| 101 | fips_ht x |
| 102 | end |
| 103 | fips_ht y |
| 104 | ret get FPST 8 |
| 105 | end |
| 106 | |
| 107 | note one APT window: sample 0x41 at the multiples of 15 below 15n (n copies spread over the |
| 108 | note whole 512-sample window; n = 35 puts the last at 510), 0x80 + (i % 64) elsewhere (no runs) |
| 109 | fn fdapt n ulong to ulong |
| 110 | fdhr |
| 111 | for i ulong 512 |
| 112 | if eq and lt i mul 15 n eq rem i 15 0 1 |
| 113 | fips_ht 65 |
| 114 | else |
| 115 | fips_ht add 128 and i 63 |
| 116 | end |
| 117 | end |
| 118 | ret get FPST 8 |
| 119 | end |
| 120 | |
| 121 | note refusal checks once the module is in its error state (id base 60) |
| 122 | fn fdref c edit uchar * to void |
| 123 | z = new uchar 16 0 |
| 124 | n = new uchar 64 0 |
| 125 | fdt 60 fips_bad 1 |
| 126 | fdt 61 tls_init c fdhost 0 0 |
| 127 | fdt 62 tls_connect c z 0 TLESELF |
| 128 | fdt 63 ulong get c TLPH 3 |
| 129 | k = tls_errname TLESELF n |
| 130 | fdt 64 fips_cmp part n 0 k fdhexn 0 |
| 131 | fdt 65 tls_seal c 0 TLESELF |
| 132 | fdt 66 tls_open c 0 TLESELF |
| 133 | end |
| 134 | |
| 135 | note approved-mode ECDH key generation draws from the DRBG at c[TLDRBG]: its state must change |
| 136 | fn fdecg c edit uchar * b edit uchar * to void |
| 137 | for i ulong DRSZ |
| 138 | put b i get c add TLDRBG i |
| 139 | end |
| 140 | fdt 44 tls_ecgen c 32 0 |
| 141 | fdt 45 fddiff part c TLDRBG DRSZ part b 0 DRSZ 1 |
| 142 | fdt 46 tls_ecgen c 48 0 |
| 143 | fdt 47 fdnz part c TLECQ3 97 1 |
| 144 | end |
| 145 | |
| 146 | note ecgenerr: a key generation that cannot succeed (nb 40) latches the error state with detail 21 |
| 147 | fn fdecx c edit uchar * to void |
| 148 | fdt 76 tls_init c fdhost 4 0 |
| 149 | fdt 77 fips_drbgnew part c TLDRBG DRSZ 0 |
| 150 | fdt 70 tls_ecgen c 40 TLESELF |
| 151 | fdt 71 fips_detail 21 |
| 152 | fdt 72 fdnz part c TLECD2 145 0 |
| 153 | end |
| 154 | |
| 155 | note steps 2-5 (id base 10) |
| 156 | fn fdpos c edit uchar TLCSZ st edit uchar 128 b edit uchar 4096 k edit uchar 32 q edit uchar 32 to void |
| 157 | fdt 10 fips_entropy b 0 |
| 158 | fdt 11 get FPST 9 1 |
| 159 | fdt 12 get FPST 8 0 |
| 160 | fdt 13 fdnz b 1 |
| 161 | fdt 14 fips_entropy k 0 |
| 162 | fdt 15 get FPST 7 32 |
| 163 | fdt 20 fdrun 66 7 67 0 |
| 164 | fdt 21 fdrun 66 8 67 1 |
| 165 | fdt 23 fdapt 34 0 |
| 166 | fdt 24 fdapt 35 2 |
| 167 | fdhr |
| 168 | fdt 25 fips_bad 0 |
| 169 | fdt 30 fips_drbgnew st 0 |
| 170 | fdt 31 ulong get st DRFL 1 |
| 171 | fdt 32 fips_drbgget st part b 0 64 0 |
| 172 | fdt 33 fips_drbgget st q 0 |
| 173 | fdt 34 fddiff part b 0 32 q 1 |
| 174 | fdt 35 fdnz part b 0 64 1 |
| 175 | fdt 36 drbg_rd st DRCTR 2 |
| 176 | fdt 40 fips_entropy k 0 |
| 177 | fdt 41 x25519_base q k 0 |
| 178 | fdt 42 fips_pctx k q 0 |
| 179 | fdt 43 fips_bad 0 |
| 180 | fdt 50 tls_init c fdhost 4 0 |
| 181 | fdt 51 fips_drbgnew part c TLDRBG DRSZ 0 |
| 182 | fdt 52 tls_rand c k 0 |
| 183 | fdt 53 fdnz k 1 |
| 184 | fdecg c b |
| 185 | fdt 57 drbg_rd part c TLDRBG DRSZ DRCTR 2 |
| 186 | tls_wipe c |
| 187 | fdt 58 fdnz part c TLDRBG DRSZ 0 |
| 188 | fdt 54 tls_init c fdhost 0 0 |
| 189 | fdt 55 tls_rand c k 0 |
| 190 | fdt 56 fdnz k 1 |
| 191 | end |
| 192 | |
| 193 | note the driver mode from argv[1]: 0 none, 1 drbgerr, 2 ecgenerr |
| 194 | fn fdmode to ulong |
| 195 | a = new uchar 16 0 |
| 196 | if lt argcnt 2 |
| 197 | ret 0 |
| 198 | end |
| 199 | l = argread 1 a 0 16 |
| 200 | if eq l 7 |
| 201 | if eq fips_cmp part a 0 7 fdhexm 0 |
| 202 | ret 1 |
| 203 | end |
| 204 | end |
| 205 | if eq l 8 |
| 206 | if eq fips_cmp part a 0 8 fdhexe 0 |
| 207 | ret 2 |
| 208 | end |
| 209 | end |
| 210 | ret 0 |
| 211 | end |
| 212 | |
| 213 | fn main to int |
| 214 | c = new uchar TLCSZ 0 |
| 215 | st = new uchar 128 0 |
| 216 | b = new uchar 4096 0 |
| 217 | k = new uchar 32 0 |
| 218 | q = new uchar 32 0 |
| 219 | mode = fdmode |
| 220 | d = fips_check 0 |
| 221 | if eq d 0 |
| 222 | d = fips_check 1 |
| 223 | end |
| 224 | if ne d 0 |
| 225 | r = write 1 fdtr 0 len fdtr |
| 226 | fdprn d |
| 227 | r = write 1 fdtnl 0 1 |
| 228 | fdref c |
| 229 | if eq get fdst 1 0 |
| 230 | ret 3 |
| 231 | end |
| 232 | ret 1 |
| 233 | end |
| 234 | fdt 1 get FPST 0 1 |
| 235 | fdt 2 get FPST 2 1 |
| 236 | fdpos c st b k q |
| 237 | if eq mode 0 |
| 238 | put q 0 uchar xor ulong get q 0 1 |
| 239 | fdt 70 fips_pctx k q 1 |
| 240 | fdt 71 fips_detail 21 |
| 241 | end |
| 242 | if eq mode 2 |
| 243 | fdecx c |
| 244 | end |
| 245 | if eq mode 1 |
| 246 | put st DRERR 1 |
| 247 | fdt 70 fips_drbgget st part b 0 32 1 |
| 248 | fdt 71 fips_detail 20 |
| 249 | fdt 72 fdnz part b 0 32 0 |
| 250 | end |
| 251 | fdt 73 fips_entropy part b 0 32 1 |
| 252 | fdt 74 fips_drbgget st part b 0 32 1 |
| 253 | fdt 75 fips_check 0 fips_detail |
| 254 | fdref c |
| 255 | r = write 1 fdts 0 len fdts |
| 256 | fdprn get fdst 0 |
| 257 | r = write 1 fdtc 0 len fdtc |
| 258 | fdprn get fdst 1 |
| 259 | r = write 1 fdtx 0 len fdtx |
| 260 | if and gt get fdst 0 0 eq get fdst 1 0 |
| 261 | ret 0 |
| 262 | end |
| 263 | ret 1 |
| 264 | end |