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
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note fips.om: FIPS 140-3 style self-tests and health tests for om/tls (FIPS 140-3 section 7.10, |
| 3 | note SP 800-140C/D, IG 10.3.A, SP 800-90B section 4.4). Tested against the FIPS 140-3 / CAVP test |
| 4 | note methods, not NIST-validated; nothing here is a certificate. Needs sha2, hkdf, drbg, x25519, |
| 5 | note chacha, aesgcm, ec, ecdh, rsa and x509 composed before it and tls.om after it (om/tls/build.sh). |
| 6 | note |
| 7 | note One process-wide mutable global, FPST, holds the module state (the only global in om/tls): |
| 8 | note FPST[0] 0 self-tests not run, 1 passed, 2 error state (latched; only a new process clears it) |
| 9 | note FPST[1] the first failure: 1..15 a pre-operational self-test (fips_selftest), 14 the DRBG |
| 10 | note self-test, 16 the software integrity test (fips_integ, after the known-answer tests), |
| 11 | note 20 the DRBG continuous test, 21 a pairwise consistency test (X25519 here, or |
| 12 | note the approved-mode P-256/P-384 key generation in tls.om: failed PCT or 16 rejected draws), |
| 13 | note 22 the repetition count test, 23 the adaptive proportion test, 24 getrandom failed, |
| 14 | note 25 a DRBG call failed, 30.. reserved for callers (fips_fail) |
| 15 | note FPST[2] 1 when the DRBG self-test (approved mode only) has passed |
| 16 | note FPST[3..8) health-test state: previous sample, RCT run length, APT reference, APT count, |
| 17 | note APT window position; FPST[8] health failures (bit 0 RCT, bit 1 APT); |
| 18 | note FPST[9] 1 after the SP 800-90B start-up test (FHSTART samples) has run |
| 19 | note |
| 20 | note Entropy input: the `random` word (Linux getrandom, flags 0). The samples are the kernel's |
| 21 | note already-conditioned CRNG output, not an Omega noise source; the RCT/APT below guard against a |
| 22 | note stuck or broken source (SP 800-90B 4.4), they do not make it an entropy source. Cut-offs for a |
| 23 | note claimed H = 6 bits per byte (below every SP 800-90B estimate measured on this source; the lowest |
| 24 | note was 6.638, a restart test) and alpha = 2^-40: RCT C = 1 + ceil(40 / 6) = 8; APT W = 512, |
| 25 | note C = 1 + CRITBINOM(512, 2^-6, 1 - 2^-40) = 35. |
| 26 | note The tests run on every sample with the same instructions for every value: the only branches |
| 27 | note are on the public window position and on the accumulated failure flag. |
| 28 | note |
| 29 | note Self-test vectors (hex texts below; a changed digit makes the module refuse, test/fips/run.sh): |
| 30 | note SHA-256/384/512 "abc" (FIPS 180-4 examples); HMAC-SHA-256/384 RFC 4231 test case 2; |
| 31 | note HKDF-SHA-256 RFC 5869 A.1; TLS 1.3 KDF RFC 8448 "derived" secret (HKDF-Expand-Label); |
| 32 | note AES-128-GCM CAVP gcmEncryptExtIV128 [IVlen 96, PTlen 256, AADlen 128, Taglen 128] Count 0 |
| 33 | note (encrypt and decrypt); ECDSA P-256 SHA-256 verify CAVP SigVer P-256,SHA-256 (test/ec |
| 34 | note c-p256-sha256-4r); RSA-2048 PKCS#1 v1.5 and PSS (SHA-256, MGF1-SHA-256, salt 32) verify, |
| 35 | note Wycheproof rsa_pkcs1_2048_sig_gen #81 and rsa_pss_2048_sha256_mgf1_32 #1; HMAC_DRBG |
| 36 | note CAVP pr_false [SHA-256] (instantiate with personalization, reseed, two generates). |
| 37 | note Non-approved algorithms, tested all the same: ChaCha20-Poly1305 RFC 8439 2.8.2 and X25519 |
| 38 | note RFC 7748 5.2. |
| 39 | |
| 40 | global FPST ulong 16 |
| 41 | note Health-test cut-offs (SP 800-90B 4.4.1/4.4.2) for H = 6 bits per sample, alpha = 2^-40, MODELED |
| 42 | note with Wolfram: h=6; a=2^-40; 1+Ceiling[-Log2[a]/h] -> 8 (RCT cut-off: 7 equal samples pass, |
| 43 | note 8 trip); 1+InverseCDF[BinomialDistribution[512,2^-h],1-a] -> 35 (APT: 34 in a window pass). |
| 44 | const FHRCT1 ulong 7 |
| 45 | const FHAPT1 ulong 34 |
| 46 | const FHAPTW ulong 512 |
| 47 | const FHSTART ulong 1024 |
| 48 | |
| 49 | text FKABC "abc" |
| 50 | text FKJEFE "Jefe" |
| 51 | text FKWHAT "what do ya want for nothing?" |
| 52 | text FKL13D "tls13 derived" |
| 53 | text FKPERS "omega-tls approved mode" |
| 54 | text FKS256 "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad" |
| 55 | text FKS384 "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7" |
| 56 | text FKS512 "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f" |
| 57 | text FKH256 "5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843" |
| 58 | text FKH384 "af45d2e376484031617f78d2b58a6b1b9c7ef464f5a01b47e42ec3736322445e8e2240ca5e69e2c78b3239ecfab21649" |
| 59 | text FKHS "000102030405060708090a0b0c" |
| 60 | text FKHI "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b" |
| 61 | text FKHN "f0f1f2f3f4f5f6f7f8f9" |
| 62 | text FKHO "3cb25f25faacd57a90434f64d0362f2a2d2d0a90cf1a5a4c5db02d56ecc4c5bf34007208d5b887185865" |
| 63 | text FKTD "6f2615a108c702c5678f54fc9dbab69716c076189c48250cebeac3576c3611ba" |
| 64 | text FKGK "298efa1ccf29cf62ae6824bfc19557fc" |
| 65 | text FKGI "6f58a93fe1d207fae4ed2f6d" |
| 66 | text FKGA "021fafd238463973ffe80256e5b1c6b1" |
| 67 | text FKGP "cc38bccd6bc536ad919b1395f5d63801f99f8068d65ca5ac63872daf16b93901" |
| 68 | text FKGC "dfce4e9cd291103d7fe4e63351d9e79d3dfd391e3267104658212da96521b7db542465ef599316f73a7a560509a2d9f2" |
| 69 | text FKEQ "04e424dc61d4bb3cb7ef4344a7f8957a0c5134e16f7a67c074f82e6e12f49abf3c970eed7aa2bc48651545949de1dddaf0127e5965ac85d1243d6f60e7dfaee927" |
| 70 | text FKED "d1b8ef21eb4182ee270638061063a3f3c16c114e33937f69fb232cc833965a94" |
| 71 | text FKES "bf96b99aa49c705c910be33142017c642ff540c76349b9dab72f981fd9347f4f17c55095819089c2e03b9cd415abdf12444e323075d98f31920b9e0f57ec871c" |
| 72 | text FKR1K "3082010a0282010100a2b451a07d0aa5f96e455671513550514a8a5b462ebef717094fa1fee82224e637f9746d3f7cafd31878d80325b6ef5a1700f65903b469429e89d6eac8845097b5ab393189db92512ed8a7711a1253facd20f79c15e8247f3d3e42e46e48c98e254a2fe9765313a03eff8f17e1a029397a1fa26a8dce26f490ed81299615d9814c22da610428e09c7d9658594266f5c021d0fceca08d945a12be82de4d1ece6b4c03145b5d3495d4ed5411eb878daf05fd7afc3e09ada0f1126422f590975a1969816f48698bcbba1b4d9cae79d460d8f9f85e7975005d9bc22c4e5ac0f7c1a45d12569a62807d3b9a02e5a530e773066f453d1f5b4c2e9cf7820283f742b9d50203010001" |
| 73 | text FKR1D "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" |
| 74 | text FKR1S "840f5dac53106dd1f9c57219224cf51289290c42f20466875ba8e830ac5690e541536fcc8ab03b731f82bf66d83f194e7e180b3963ec7a2f3f7904a7ce49aed47da4d4b79421eaf937d301b3e696169297b797c32c076a12be4de0b58e003c5123051a84a10c62f8dac2f42a8640008eb3c7cccd6760ff5b51b689763922582845f048fb8150e5a7a6ca2eccc7bdc85349ad5b26c52137a79fa3fe5c29ab5cd7615013219c1941b6708e9c3c23feff5febaf0c8ebca5750b54e3e6e99a3e876b396f27860b7f3ec4e9191703c6332d944f6f69751167680c79c4f6b57f1cc8755d24b6ec158ccdbacdb23107a33cb6b332516c13274d1f9dccc21dced869e486" |
| 75 | text FKR2K "3082010a0282010100a2b451a07d0aa5f96e455671513550514a8a5b462ebef717094fa1fee82224e637f9746d3f7cafd31878d80325b6ef5a1700f65903b469429e89d6eac8845097b5ab393189db92512ed8a7711a1253facd20f79c15e8247f3d3e42e46e48c98e254a2fe9765313a03eff8f17e1a029397a1fa26a8dce26f490ed81299615d9814c22da610428e09c7d9658594266f5c021d0fceca08d945a12be82de4d1ece6b4c03145b5d3495d4ed5411eb878daf05fd7afc3e09ada0f1126422f590975a1969816f48698bcbba1b4d9cae79d460d8f9f85e7975005d9bc22c4e5ac0f7c1a45d12569a62807d3b9a02e5a530e773066f453d1f5b4c2e9cf7820283f742b9d50203010001" |
| 76 | text FKR2D "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" |
| 77 | text FKR2S "4f01e0c12b08625ecac89a69231906edf826380f37c959a96690d046316d68ffce9d5c471694fcebfc6b45534864689256e4fc81c78e583f675d0c94b449647451e81beff01a11a516d5e5ce3f1a910437cb8a3a5096b19fb15f4524a35b23d89cdba12cf5b71aac1047b28c562df7c5542c34ce23a182cf7e0e231934b17294799d44877a1d68ef1b8f073619b7618e6b7c22db20030d98cf591ffc3d4da5f58613ecd5ecfc3b40a1d02f40891ca43695cd4c088b05a8054c89c595a47e274816f35384226f74459ee63e25a1bfc03c360490552ec38343f8ace502f065303b00bc0ec320711b211fde92e57feb9013c3609342495ec0d7cabdec21e54acc38" |
| 78 | text FKCK "808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9f070000004041424344454647" |
| 79 | text FKCA "50515253c0c1c2c3c4c5c6c7" |
| 80 | text FKCP "4c616469657320616e642047656e746c656d656e206f662074686520636c617373206f66202739393a204966204920636f756c64206f6666657220796f75206f6e6c79206f6e652074697020666f7220746865206675747572652c2073756e73637265656e20776f756c642062652069742e" |
| 81 | text FKCC "d31a8d34648e60db7b86afbc53ef7ec2a4aded51296e08fea9e2b5a736ee62d63dbea45e8ca9671282fafb69da92728b1a71de0a9e060b2905d6a5b67ecd3b3692ddbd7f2d778b8c9803aee328091b58fab324e4fad675945585808b4831d7bc3ff4def08e4b7a9de576d26586cec64b61161ae10b594f09e26a7e902ecbd0600691" |
| 82 | text FKXK "a546e36bf0527c9d3b16154b82465edd62144c0ac1fc5a18506a2244ba449ac4" |
| 83 | text FKXU "e6db6867583030db3594c1a424b15f7c726624ec26b3353b10a903a6d0ab1c4c" |
| 84 | text FKXO "c3da55379de9c6908e94ea4df28d084f32eccf03491c71f754b4075577a28552" |
| 85 | text FKDE "cdb0d9117cc6dbc9ef9dcb06a97579841d72dc18b2d46a1cb61e314012bdf416" |
| 86 | text FKDN "d0c0d01d156016d0eb6b7e9c7c3c8da8" |
| 87 | text FKDP "6f0fb9eab3f9ea7ab0a719bfa879bf0aaed683307fda0c6d73ce018b6e34faaa" |
| 88 | text FKDER "8ec6f7d5a8e2e88f43986f70b86e050d07c84b931bcf18e601c5a3eee3064c82" |
| 89 | text FKDAR "1ab4ca9014fa98a55938316de8ba5a68c629b0741bdd058c4d70c91cda5099b3" |
| 90 | text FKDA1 "16e2d0721b58d839a122852abd3bf2c942a31c84d82fca74211871880d7162ff" |
| 91 | text FKDA2 "53686f042a7b087d5d2eca0d2a96de131f275ed7151189f7ca52deaa78b79fb2" |
| 92 | text FKDR "dda04a2ca7b8147af1548f5d086591ca4fd951a345ce52b3cd49d47e84aa31a183e31fbc42a1ff1d95afec7143c8008c97bc2a9c091df0a763848391f68cb4a366ad89857ac725a53b303ddea767be8dc5f605b1b95f6d24c9f06be65a973a089320b3cc42569dcfd4b92b62a993785b0301b3fc452445656fce22664827b88f" |
| 93 | |
| 94 | note ---- module state ---- |
| 95 | note enter the error state with failure code (the first code is kept); returns code |
| 96 | fn fips_fail code ulong to ulong |
| 97 | if ne get FPST 0 2 |
| 98 | put FPST 1 code |
| 99 | end |
| 100 | put FPST 0 2 |
| 101 | ret code |
| 102 | end |
| 103 | |
| 104 | note 1 in the error state, 0 otherwise |
| 105 | fn fips_bad to ulong |
| 106 | if eq get FPST 0 2 |
| 107 | ret 1 |
| 108 | end |
| 109 | ret 0 |
| 110 | end |
| 111 | |
| 112 | note the first failure code (0 when none) |
| 113 | fn fips_detail to ulong |
| 114 | ret get FPST 1 |
| 115 | end |
| 116 | |
| 117 | note ---- hex texts ---- |
| 118 | fn fips_hx ch ulong to ulong |
| 119 | if le ch 57 |
| 120 | ret sub ch 48 |
| 121 | end |
| 122 | ret sub or ch 32 87 |
| 123 | end |
| 124 | |
| 125 | fn fips_hb t view uchar * i ulong to ulong |
| 126 | j = mul i 2 |
| 127 | ret or shl fips_hx ulong get t j 4 fips_hx ulong get t add j 1 |
| 128 | end |
| 129 | |
| 130 | note o[0..len t / 2) = the bytes of hex text t; returns that count |
| 131 | fn fips_unhex t view uchar * o edit uchar * to ulong |
| 132 | n = shr len t 1 |
| 133 | for i ulong n |
| 134 | put o i uchar fips_hb t i |
| 135 | end |
| 136 | ret n |
| 137 | end |
| 138 | |
| 139 | note 0 when a equals the bytes of hex text t (same length), 1 otherwise; no early exit |
| 140 | fn fips_cmp a view uchar * t view uchar * to ulong |
| 141 | n = shr len t 1 |
| 142 | if ne len a n |
| 143 | ret 1 |
| 144 | end |
| 145 | d = ulong 0 |
| 146 | for i ulong n |
| 147 | d = or d xor ulong get a i fips_hb t i |
| 148 | end |
| 149 | if ne d 0 |
| 150 | ret 1 |
| 151 | end |
| 152 | ret 0 |
| 153 | end |
| 154 | |
| 155 | note ---- software integrity test (FIPS 140-3 style, ISO/IEC 19790 7.10.2.2) and version service ---- |
| 156 | note The composed program carries one text slot FKIH: a 35-byte marker followed by 64 hex digits. |
| 157 | note No other text starts with that marker, so the marker occurs once in the file. om/tls/build.sh |
| 158 | note compiles the program, then fips_itool writes HMAC-SHA-256(FKIK, file) into the slot, the 64 |
| 159 | note slot bytes taken as '0' while hashing. At first use fips_check, after the known-answer tests |
| 160 | note (SHA-256 and HMAC-SHA-256 among them), recomputes the HMAC over /proc/self/exe the same way and |
| 161 | note compares it with the slot as loaded (code 16). An unpatched program keeps 64 '0' digits and |
| 162 | note fails closed. FKIK is public (OpenSSL FIPS provider style): this detects corruption and |
| 163 | note accidental modification, not a deliberate attacker who can rewrite the file. The file on disk |
| 164 | note is hashed, not the mapped image. |
| 165 | text FKIH "omega-tls-integrity-v1-hmac-sha256:0000000000000000000000000000000000000000000000000000000000000000" |
| 166 | text FKIK "omega-tls module integrity key v1" |
| 167 | text FKEXE "/proc/self/exe" |
| 168 | text FKNAME "Omega TLS module (om/tls)" |
| 169 | text FKVER "0.1.0" |
| 170 | text FKV1 " version " |
| 171 | text FKV2 " integrity hmac-sha256:" |
| 172 | text FKV3 " state " |
| 173 | const FIMK ulong 35 |
| 174 | const FICH ulong 65536 |
| 175 | const FISTEP ulong 65408 |
| 176 | const FIMAX ulong 4096 |
| 177 | |
| 178 | note 1 when b[i..i+FIMK) is the marker (the first FIMK bytes of FKIH), 0 otherwise |
| 179 | fn fips_imark b view uchar * i ulong to ulong |
| 180 | d = ulong 0 |
| 181 | for j ulong FIMK |
| 182 | d = or d xor ulong get b add i j ulong get FKIH j |
| 183 | end |
| 184 | if eq d 0 |
| 185 | ret 1 |
| 186 | end |
| 187 | ret 0 |
| 188 | end |
| 189 | |
| 190 | note markers in b[0..n) read at file offset pos: s updated to the slot offset of a marker found, |
| 191 | note -3 for a second one. A marker starting at FISTEP or later is left to the next chunk unless |
| 192 | note this is the last chunk (last 1). |
| 193 | fn fips_iscan b view uchar * n ulong last ulong pos long s long to long |
| 194 | if lt n FIMK |
| 195 | ret s |
| 196 | end |
| 197 | for i ulong add sub n FIMK 1 |
| 198 | if or lt i FISTEP eq last 1 |
| 199 | if eq fips_imark b i 1 |
| 200 | if ge s 0 |
| 201 | ret neg long 3 |
| 202 | end |
| 203 | s = add pos long add i FIMK |
| 204 | end |
| 205 | end |
| 206 | end |
| 207 | ret s |
| 208 | end |
| 209 | |
| 210 | note the slot offset of the one marker in file fd (b: a FICH-byte buffer), or -1 read error, |
| 211 | note -2 no marker, -3 more than one marker, -4 larger than FIMAX chunks. Chunks overlap by |
| 212 | note FICH - FISTEP bytes so a marker across a chunk edge is still seen, and counted once. |
| 213 | fn fips_ifind fd int b edit uchar * to long |
| 214 | s = neg long 2 |
| 215 | pos = long 0 |
| 216 | more = ulong 1 |
| 217 | for k ulong FIMAX |
| 218 | if eq more 1 |
| 219 | r = pread fd b 0 FICH pos |
| 220 | if lt r 0 |
| 221 | ret neg long 1 |
| 222 | end |
| 223 | n = ulong r |
| 224 | if lt n FICH |
| 225 | more = 0 |
| 226 | end |
| 227 | s = fips_iscan b n xor more 1 pos s |
| 228 | if eq s neg long 3 |
| 229 | ret s |
| 230 | end |
| 231 | pos = add pos long FISTEP |
| 232 | end |
| 233 | end |
| 234 | if eq more 1 |
| 235 | ret neg long 4 |
| 236 | end |
| 237 | ret s |
| 238 | end |
| 239 | |
| 240 | note b[0..n) read at file offset pos: the bytes of the slot [s, s + 64) set to '0'; returns how many |
| 241 | fn fips_imask b edit uchar * n ulong pos long s long to ulong |
| 242 | t = ulong 0 |
| 243 | for i ulong n |
| 244 | a = add pos long i |
| 245 | if and ge a s lt a add s 64 |
| 246 | put b i 48 |
| 247 | t = add t 1 |
| 248 | end |
| 249 | end |
| 250 | ret t |
| 251 | end |
| 252 | |
| 253 | note mac[0..32) = HMAC-SHA-256(FKIK, file fd) with the 64 slot bytes at offset s taken as '0'; |
| 254 | note 0, -1 read error, -3 slot past the end of the file |
| 255 | fn fips_imac fd int b edit uchar * s long mac edit uchar * to long |
| 256 | c = new ulong 96 0 |
| 257 | hmac_init 256 c FKIK |
| 258 | pos = long 0 |
| 259 | more = ulong 1 |
| 260 | t = ulong 0 |
| 261 | for k ulong FIMAX |
| 262 | if eq more 1 |
| 263 | r = pread fd b 0 FICH pos |
| 264 | if lt r 0 |
| 265 | drbg_zx c |
| 266 | ret neg long 1 |
| 267 | end |
| 268 | n = ulong r |
| 269 | if lt n FICH |
| 270 | more = 0 |
| 271 | end |
| 272 | t = add t fips_imask b n pos s |
| 273 | hmac_update c part b 0 n |
| 274 | pos = add pos long n |
| 275 | end |
| 276 | end |
| 277 | if ne t 64 |
| 278 | drbg_zx c |
| 279 | ret neg long 3 |
| 280 | end |
| 281 | n = hmac_final c mac |
| 282 | ret 0 |
| 283 | end |
| 284 | |
| 285 | note mac[0..32) = HMAC-SHA-256(FKIK, file fd) with the 64 slot bytes after the one marker taken as |
| 286 | note '0'. Returns the slot's file offset, or the negative fips_ifind / fips_imac failure. |
| 287 | fn fips_isum fd int mac edit uchar * to long |
| 288 | b = new uchar FICH 0 |
| 289 | s = fips_ifind fd b |
| 290 | if lt s 0 |
| 291 | ret s |
| 292 | end |
| 293 | r = fips_imac fd b s mac |
| 294 | if lt r 0 |
| 295 | ret r |
| 296 | end |
| 297 | ret s |
| 298 | end |
| 299 | |
| 300 | note the integrity test: 0 when the HMAC over /proc/self/exe matches the slot as loaded, 1 otherwise |
| 301 | fn fips_integ to ulong |
| 302 | m = new uchar 32 0 |
| 303 | fd = fopen FKEXE 0 len FKEXE 0 0 |
| 304 | if lt fd 0 |
| 305 | ret 1 |
| 306 | end |
| 307 | s = fips_isum int fd m |
| 308 | r = fclose int fd |
| 309 | if lt s 0 |
| 310 | ret 1 |
| 311 | end |
| 312 | ret fips_cmp m part FKIH FIMK 64 |
| 313 | end |
| 314 | |
| 315 | note o[k..k + len t) = t; returns k + len t |
| 316 | fn fips_put o edit uchar * k ulong t view uchar * to ulong |
| 317 | for i ulong len t |
| 318 | put o add k i get t i |
| 319 | end |
| 320 | ret add k len t |
| 321 | end |
| 322 | |
| 323 | note o[0..2 len m) = lower-case hex of m |
| 324 | fn fips_hex m view uchar * o edit uchar * to void |
| 325 | for i ulong len m |
| 326 | x = ulong get m i |
| 327 | for j ulong 2 |
| 328 | v = and shr x sub 4 mul j 4 15 |
| 329 | g = mul 39 shr sub 9 v 63 |
| 330 | q = add mul i 2 j |
| 331 | put o q uchar add add v 48 g |
| 332 | end |
| 333 | end |
| 334 | end |
| 335 | |
| 336 | note the integrity tool behind om/tls/build.sh: argument 1 names a composed program; writes the |
| 337 | note HMAC hex into its slot. 0 done, 2 usage or path too long, 3 open/read/write error, 4 no |
| 338 | note marker, two markers or file too large. |
| 339 | fn fips_itool to int |
| 340 | p = new uchar 4096 0 |
| 341 | m = new uchar 32 0 |
| 342 | h = new uchar 64 0 |
| 343 | if ne argcnt 2 |
| 344 | ret 2 |
| 345 | end |
| 346 | n = argread 1 p 0 4095 |
| 347 | if or le n 0 ge n 4095 |
| 348 | ret 2 |
| 349 | end |
| 350 | fd = fopen p 0 ulong n 2 0 |
| 351 | if lt fd 0 |
| 352 | ret 3 |
| 353 | end |
| 354 | s = fips_isum int fd m |
| 355 | if lt s 0 |
| 356 | r = fclose int fd |
| 357 | if eq s neg long 1 |
| 358 | ret 3 |
| 359 | end |
| 360 | ret 4 |
| 361 | end |
| 362 | fips_hex m h |
| 363 | w = pwrite int fd h 0 64 s |
| 364 | r = fclose int fd |
| 365 | if ne w 64 |
| 366 | ret 3 |
| 367 | end |
| 368 | ret 0 |
| 369 | end |
| 370 | |
| 371 | note the show-version service: writes "NAME version VER integrity hmac-sha256:HEX state S\n" into |
| 372 | note o (S: 0 self-tests not yet run, 1 passed, 2 error state) and returns its length; 0 when o is |
| 373 | note shorter than that line. HEX is the value this program verifies itself against. |
| 374 | fn fips_version o edit uchar * to ulong |
| 375 | n = add add add add add len FKNAME len FKV1 len FKVER len FKV2 len FKV3 66 |
| 376 | if lt len o n |
| 377 | ret 0 |
| 378 | end |
| 379 | k = fips_put o 0 FKNAME |
| 380 | k = fips_put o k FKV1 |
| 381 | k = fips_put o k FKVER |
| 382 | k = fips_put o k FKV2 |
| 383 | k = fips_put o k part FKIH FIMK 64 |
| 384 | k = fips_put o k FKV3 |
| 385 | put o k uchar add 48 get FPST 0 |
| 386 | put o add k 1 10 |
| 387 | ret add k 2 |
| 388 | end |
| 389 | |
| 390 | note ---- pre-operational self-tests (known-answer tests); each returns 0 or 1 ---- |
| 391 | fn fips_ksha alg ulong m view uchar * want view uchar * to ulong |
| 392 | o = new uchar 64 0 |
| 393 | n = sha2_digest alg m o |
| 394 | ret fips_cmp part o 0 n want |
| 395 | end |
| 396 | |
| 397 | fn fips_khmac alg ulong want view uchar * to ulong |
| 398 | c = new ulong 96 0 |
| 399 | o = new uchar 64 0 |
| 400 | hmac_init alg c FKJEFE |
| 401 | hmac_update c FKWHAT |
| 402 | n = hmac_final c o |
| 403 | drbg_zx c |
| 404 | ret fips_cmp part o 0 n want |
| 405 | end |
| 406 | |
| 407 | note HKDF-SHA-256 extract then expand (RFC 5869 A.1) |
| 408 | fn fips_khkdf to ulong |
| 409 | si = new uchar 64 0 |
| 410 | p = new uchar 64 0 |
| 411 | o = new uchar 42 0 |
| 412 | a = fips_unhex FKHS si |
| 413 | b = fips_unhex FKHI part si a sub 64 a |
| 414 | n = hkdf_extract 256 part si 0 add a b a part p 0 32 |
| 415 | if ne n 32 |
| 416 | ret 1 |
| 417 | end |
| 418 | m = fips_unhex FKHN part p 32 32 |
| 419 | if ne hkdf_expand 256 part p 0 add 32 m 32 o 0 |
| 420 | ret 1 |
| 421 | end |
| 422 | ret fips_cmp o FKHO |
| 423 | end |
| 424 | |
| 425 | note TLS 1.3 key schedule (RFC 8446 7.1): early secret = HKDF-Extract(0, 0^32), then |
| 426 | note Derive-Secret(., "derived", "") = HKDF-Expand-Label(., "derived", SHA-256(""), 32), RFC 8448 |
| 427 | fn fips_ktls to ulong |
| 428 | z = new uchar 32 0 |
| 429 | p = new uchar 81 0 |
| 430 | o = new uchar 32 0 |
| 431 | n = hkdf_extract 256 z 0 part p 0 32 |
| 432 | if ne n 32 |
| 433 | ret 1 |
| 434 | end |
| 435 | put p 33 32 |
| 436 | put p 34 13 |
| 437 | for i ulong 13 |
| 438 | put p add 35 i get FKL13D i |
| 439 | end |
| 440 | put p 48 32 |
| 441 | n = sha2_digest 256 part z 0 0 part p 49 32 |
| 442 | if ne hkdf_expand 256 p 32 o 0 |
| 443 | ret 1 |
| 444 | end |
| 445 | ret fips_cmp o FKTD |
| 446 | end |
| 447 | |
| 448 | note AES-128-GCM encrypt (CT || tag) then decrypt; 0, 1 (encrypt) or 2 (decrypt) |
| 449 | fn fips_kgcm to ulong |
| 450 | kn = new uchar 28 0 |
| 451 | ad = new uchar 16 0 |
| 452 | b = new uchar 48 0 |
| 453 | n = fips_unhex FKGK kn |
| 454 | n = fips_unhex FKGI part kn 16 12 |
| 455 | n = fips_unhex FKGA ad |
| 456 | n = fips_unhex FKGP b |
| 457 | if ne aead_aes128gcm_seal kn ad b 0 |
| 458 | ret 1 |
| 459 | end |
| 460 | if ne fips_cmp b FKGC 0 |
| 461 | ret 1 |
| 462 | end |
| 463 | if ne aead_aes128gcm_open kn ad b 0 |
| 464 | ret 2 |
| 465 | end |
| 466 | if ne fips_cmp part b 0 32 FKGP 0 |
| 467 | ret 2 |
| 468 | end |
| 469 | ret 0 |
| 470 | end |
| 471 | |
| 472 | note ChaCha20-Poly1305 seal then open (RFC 8439 2.8.2); non-approved |
| 473 | fn fips_kchacha to ulong |
| 474 | kn = new uchar 44 0 |
| 475 | ad = new uchar 12 0 |
| 476 | b = new uchar 130 0 |
| 477 | n = fips_unhex FKCK kn |
| 478 | n = fips_unhex FKCA ad |
| 479 | n = fips_unhex FKCP b |
| 480 | if ne aead_chacha_seal kn ad b 0 |
| 481 | ret 1 |
| 482 | end |
| 483 | if ne fips_cmp b FKCC 0 |
| 484 | ret 1 |
| 485 | end |
| 486 | if ne aead_chacha_open kn ad b 0 |
| 487 | ret 1 |
| 488 | end |
| 489 | ret fips_cmp part b 0 114 FKCP |
| 490 | end |
| 491 | |
| 492 | note ECDSA P-256 verify: the CAVP signature must verify and fail with the digest changed |
| 493 | fn fips_kecdsa to ulong |
| 494 | q = new uchar 65 0 |
| 495 | hs = new uchar 96 0 |
| 496 | n = fips_unhex FKEQ q |
| 497 | n = fips_unhex FKED hs |
| 498 | n = fips_unhex FKES part hs 32 64 |
| 499 | if ne ecdsa_verify_raw 256 q hs 32 0 |
| 500 | ret 1 |
| 501 | end |
| 502 | put hs 0 uchar xor ulong get hs 0 1 |
| 503 | if eq ecdsa_verify_raw 256 q hs 32 0 |
| 504 | ret 1 |
| 505 | end |
| 506 | ret 0 |
| 507 | end |
| 508 | |
| 509 | note RSA-2048 verify; hs = SHA-256 digest || signature (rsa.om); ps 0 PKCS#1 v1.5, 1 PSS |
| 510 | fn fips_rsav ps ulong k view uchar * hs edit uchar * to ulong |
| 511 | r = ulong 0 |
| 512 | if eq ps 0 |
| 513 | r = rsa_verify_pkcs1 256 k hs 32 |
| 514 | else |
| 515 | r = rsa_verify_pss 137455730944 k hs 32 |
| 516 | end |
| 517 | if ne r 0 |
| 518 | ret 1 |
| 519 | end |
| 520 | put hs 0 uchar xor ulong get hs 0 1 |
| 521 | if eq ps 0 |
| 522 | r = rsa_verify_pkcs1 256 k hs 32 |
| 523 | else |
| 524 | r = rsa_verify_pss 137455730944 k hs 32 |
| 525 | end |
| 526 | if eq r 0 |
| 527 | ret 1 |
| 528 | end |
| 529 | ret 0 |
| 530 | end |
| 531 | |
| 532 | fn fips_kpkcs to ulong |
| 533 | k = new uchar 270 0 |
| 534 | hs = new uchar 288 0 |
| 535 | n = fips_unhex FKR1K k |
| 536 | n = fips_unhex FKR1D hs |
| 537 | n = fips_unhex FKR1S part hs 32 256 |
| 538 | ret fips_rsav 0 k hs |
| 539 | end |
| 540 | |
| 541 | note PSS control word: SHA-256 | MGF1-SHA-256 << 16 | salt 32 << 32 = 137455730944 |
| 542 | fn fips_kpss to ulong |
| 543 | k = new uchar 270 0 |
| 544 | hs = new uchar 288 0 |
| 545 | n = fips_unhex FKR2K k |
| 546 | n = fips_unhex FKR2D hs |
| 547 | n = fips_unhex FKR2S part hs 32 256 |
| 548 | ret fips_rsav 1 k hs |
| 549 | end |
| 550 | |
| 551 | note X25519 (RFC 7748 5.2 first vector); non-approved |
| 552 | fn fips_kx to ulong |
| 553 | k = new uchar 32 0 |
| 554 | u = new uchar 32 0 |
| 555 | o = new uchar 32 0 |
| 556 | n = fips_unhex FKXK k |
| 557 | n = fips_unhex FKXU u |
| 558 | if ne x25519_scalarmult o k u 0 |
| 559 | ret 1 |
| 560 | end |
| 561 | ret fips_cmp o FKXO |
| 562 | end |
| 563 | |
| 564 | note HMAC_DRBG instantiate (with personalization), reseed (with additional input) and two |
| 565 | note generates with additional input; the second output must equal the CAVP ReturnedBits |
| 566 | fn fips_kdrbg to ulong |
| 567 | st = new uchar 128 0 |
| 568 | sm = new uchar 96 0 |
| 569 | o = new uchar 128 0 |
| 570 | a = new uchar 32 0 |
| 571 | n = fips_unhex FKDE sm |
| 572 | n = add n fips_unhex FKDN part sm n sub 96 n |
| 573 | n = add n fips_unhex FKDP part sm n sub 96 n |
| 574 | r = drbg_inst st part sm 0 n |
| 575 | n = fips_unhex FKDER sm |
| 576 | n = add n fips_unhex FKDAR part sm n sub 96 n |
| 577 | r = or r drbg_reseed st part sm 0 n |
| 578 | n = fips_unhex FKDA1 a |
| 579 | r = or r drbg_gen st o a |
| 580 | n = fips_unhex FKDA2 a |
| 581 | r = or r drbg_gen st o a |
| 582 | drbg_wipe st |
| 583 | if ne r 0 |
| 584 | ret 1 |
| 585 | end |
| 586 | ret fips_cmp o FKDR |
| 587 | end |
| 588 | |
| 589 | note all pre-operational self-tests except the DRBG: 0 or the number of the first that failed |
| 590 | fn fips_selftest to ulong |
| 591 | if ne fips_ksha 256 FKABC FKS256 0 |
| 592 | ret 1 |
| 593 | end |
| 594 | if ne fips_ksha 384 FKABC FKS384 0 |
| 595 | ret 2 |
| 596 | end |
| 597 | if ne fips_ksha 512 FKABC FKS512 0 |
| 598 | ret 3 |
| 599 | end |
| 600 | if ne fips_khmac 256 FKH256 0 |
| 601 | ret 4 |
| 602 | end |
| 603 | if ne fips_khmac 384 FKH384 0 |
| 604 | ret 5 |
| 605 | end |
| 606 | if ne fips_khkdf 0 |
| 607 | ret 6 |
| 608 | end |
| 609 | r = fips_kgcm |
| 610 | if ne r 0 |
| 611 | ret add 6 r |
| 612 | end |
| 613 | if ne fips_kecdsa 0 |
| 614 | ret 9 |
| 615 | end |
| 616 | if ne fips_kpkcs 0 |
| 617 | ret 10 |
| 618 | end |
| 619 | if ne fips_kpss 0 |
| 620 | ret 11 |
| 621 | end |
| 622 | if ne fips_kchacha 0 |
| 623 | ret 12 |
| 624 | end |
| 625 | if ne fips_kx 0 |
| 626 | ret 13 |
| 627 | end |
| 628 | if ne fips_ktls 0 |
| 629 | ret 15 |
| 630 | end |
| 631 | ret 0 |
| 632 | end |
| 633 | |
| 634 | note run the self-tests once per process (first use); ap 1 also runs the DRBG self-test. |
| 635 | note 0 when the module may operate, otherwise the latched failure code |
| 636 | fn fips_check ap ulong to ulong |
| 637 | if eq get FPST 0 0 |
| 638 | d = fips_selftest |
| 639 | if ne d 0 |
| 640 | ret fips_fail d |
| 641 | end |
| 642 | if ne fips_integ 0 |
| 643 | ret fips_fail 16 |
| 644 | end |
| 645 | put FPST 0 1 |
| 646 | end |
| 647 | if eq get FPST 0 2 |
| 648 | ret get FPST 1 |
| 649 | end |
| 650 | if and ne ap 0 eq get FPST 2 0 |
| 651 | if ne fips_kdrbg 0 |
| 652 | ret fips_fail 14 |
| 653 | end |
| 654 | put FPST 2 1 |
| 655 | end |
| 656 | ret 0 |
| 657 | end |
| 658 | |
| 659 | note ---- SP 800-90B 4.4 health tests on getrandom samples ---- |
| 660 | note one 8-bit sample x through the repetition count and adaptive proportion tests |
| 661 | fn fips_ht x ulong to void |
| 662 | z = shr sub xor x get FPST 3 1 63 |
| 663 | b = add mul z get FPST 4 1 |
| 664 | put FPST 4 b |
| 665 | f = shr sub FHRCT1 b 63 |
| 666 | p = get FPST 7 |
| 667 | if eq p 0 |
| 668 | put FPST 5 x |
| 669 | put FPST 6 0 |
| 670 | end |
| 671 | c = add get FPST 6 shr sub xor x get FPST 5 1 63 |
| 672 | put FPST 6 c |
| 673 | g = shr sub FHAPT1 c 63 |
| 674 | put FPST 8 or get FPST 8 or f shl g 1 |
| 675 | put FPST 7 rem add p 1 FHAPTW |
| 676 | put FPST 3 x |
| 677 | end |
| 678 | |
| 679 | note b gets len b bytes from getrandom, every one through the health tests; 0, or 1 with b |
| 680 | note zeroed and the error state latched (22 RCT, 23 APT, 24 getrandom) |
| 681 | fn fips_draw b edit uchar * to ulong |
| 682 | n = len b |
| 683 | r = random b 0 n 0 |
| 684 | if ne ulong r n |
| 685 | drbg_zb b |
| 686 | ret fips_fail 24 |
| 687 | end |
| 688 | for i ulong n |
| 689 | fips_ht ulong get b i |
| 690 | end |
| 691 | f = get FPST 8 |
| 692 | if ne f 0 |
| 693 | drbg_zb b |
| 694 | ret fips_fail add 22 xor and f 1 1 |
| 695 | end |
| 696 | ret 0 |
| 697 | end |
| 698 | |
| 699 | note entropy input for the DRBG: the start-up test (FHSTART samples, discarded) runs first |
| 700 | fn fips_entropy b edit uchar * to ulong |
| 701 | s = new uchar FHSTART 0 |
| 702 | if eq fips_bad 1 |
| 703 | ret 1 |
| 704 | end |
| 705 | if eq get FPST 9 0 |
| 706 | r = fips_draw s |
| 707 | drbg_zb s |
| 708 | if ne r 0 |
| 709 | ret 1 |
| 710 | end |
| 711 | put FPST 9 1 |
| 712 | end |
| 713 | if ne fips_draw b 0 |
| 714 | ret 1 |
| 715 | end |
| 716 | ret 0 |
| 717 | end |
| 718 | |
| 719 | note ---- approved-mode DRBG (tls.om, flag 4) ---- |
| 720 | note instantiate st: 48 bytes entropy input, 32 bytes nonce (also from the source, SP 800-90A |
| 721 | note 8.6.7) and the personalization string; 0, or 1 with the error state latched |
| 722 | fn fips_drbgnew st edit uchar * to ulong |
| 723 | sm = new uchar 112 0 |
| 724 | r = fips_entropy part sm 0 80 |
| 725 | if eq r 0 |
| 726 | for i ulong len FKPERS |
| 727 | put sm add 80 i get FKPERS i |
| 728 | end |
| 729 | r = drbg_inst st part sm 0 add 80 len FKPERS |
| 730 | if ne r 0 |
| 731 | r = fips_fail 25 |
| 732 | end |
| 733 | end |
| 734 | drbg_zb sm |
| 735 | if ne r 0 |
| 736 | ret 1 |
| 737 | end |
| 738 | ret 0 |
| 739 | end |
| 740 | |
| 741 | note b gets len b DRBG bytes with prediction resistance: reseed with 48 fresh health-tested |
| 742 | note bytes before every generate (SP 800-90A 9.3.1); 0, or 1 with b zeroed and the error latched |
| 743 | fn fips_drbgget st edit uchar * b edit uchar * to ulong |
| 744 | e = new uchar 48 0 |
| 745 | if ne fips_entropy e 0 |
| 746 | drbg_zb b |
| 747 | ret 1 |
| 748 | end |
| 749 | r = drbg_reseed st e |
| 750 | drbg_zb e |
| 751 | if eq r 0 |
| 752 | r = drbg_gen st b part e 0 0 |
| 753 | end |
| 754 | if eq r 0 |
| 755 | ret 0 |
| 756 | end |
| 757 | drbg_zb b |
| 758 | if eq r 4 |
| 759 | r = fips_fail 20 |
| 760 | ret 1 |
| 761 | end |
| 762 | r = fips_fail 25 |
| 763 | ret 1 |
| 764 | end |
| 765 | |
| 766 | note ---- conditional self-test: X25519 pairwise consistency (SP 800-56A 5.6.2.1.4 style) ---- |
| 767 | note recompute the public key from k and compare with pub (constant-time); 0, or 1 latched (21) |
| 768 | fn fips_pctx k view uchar * pub view uchar * to ulong |
| 769 | o = new uchar 32 0 |
| 770 | r = x25519_base o k |
| 771 | d = ulong 0 |
| 772 | for i ulong 32 |
| 773 | d = or d xor ulong get o i ulong get pub i |
| 774 | end |
| 775 | drbg_zb o |
| 776 | if or ne r 0 ne d 0 |
| 777 | r = fips_fail 21 |
| 778 | ret 1 |
| 779 | end |
| 780 | ret 0 |
| 781 | end |