interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / om / tls / fips.om

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