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 / ecdh.om

418 lines · 12123 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note om/tls/ecdh.om: constant-time ECDH (ECC CDH primitive, NIST SP 800-56A Rev. 3 section 5.7.1.2)
3note on NIST P-256 and P-384, for the TLS 1.3 key_share groups secp256r1 (23) and secp384r1 (24).
4note Library unit (no om2 line, no main), composed after ec.om. Module code ECD: functions ecdh_*
5note and ecd_*, consts ED*. Pure: no I/O and no randomness; the caller draws the private scalar.
6note
7note API (nb = 32 for P-256, 48 for P-384; points are SEC1 uncompressed 0x04 || X || Y, 1 + 2 nb bytes):
8note ecdh_pub d q q = d*G for the private scalar d (nb big-endian bytes; nb selects the curve)
9note ecdh_shared d p z z = x(d*P), nb bytes, after partial public-key validation of P
10note ecdh_pct d q pair-wise consistency: q passes full public-key validation and equals d*G
11note Return codes: 0 ok, 1 shared point is the point at infinity (z zeroed), 2 bad buffer length,
12note 3 peer point invalid (encoding other than uncompressed, coordinate >= p, not on the curve),
13note 4 private scalar not in [1, n-1] (q or z zeroed), 5 len d not 32 or 48, 6 pair-wise check failed.
14note Private-key generation: draw nb random bytes (from the approved DRBG in approved mode), call
15note ecdh_pub, and draw again while it returns 4 (FIPS 186-5 Appendix A.2.2, rejection sampling).
16note Peer validation is SP 800-56A Rev. 3 section 5.6.2.3.4 (ECC partial public-key validation):
17note not the point at infinity (an uncompressed encoding cannot express it), both coordinates in
18note [0, p-1], and Y^2 = X^3 - 3X + b. Both curves have cofactor 1, so for them this equals the
19note full validation of section 5.6.2.3.3 (step 4, n*Q = O, holds for every point on the curve).
20note
21note Arithmetic: the 32-bit-limb Montgomery field of ec.om (same workspace layout and constants).
22note Only ec.om helpers without data-dependent branches touch secrets: ec_row, ec_step, ec_sum,
23note ec_copy, ec_pcopy, ec_load, ec_bit (index from the public bit position). ec_csub, ec_mul,
24note ec_add, ec_sub, ec_inv, ec_dbl and ec_padd branch and are used only on public data (setup and
25note peer-point validation, ec_pub). The field operations below replace them on secrets:
26note ecd_csub selects with a mask from the full borrow chain, never a branch.
27note Scalar multiplication: Montgomery ladder over all 32L bits of the scalar (fixed count, leading
28note zero bits included), the swap is an xor with the mask 0 - bit, and each step is R1 = R0 + R1,
29note R0 = R0 + R0 with the complete projective addition formula for a = -3 of Renes, Costello and
30note Batina, "Complete addition formulas for prime order elliptic curves" (EUROCRYPT 2016),
31note Algorithm 4: no exceptional case (infinity, doubling, inverse points), so no branch. R0 starts at
32note the point at infinity (0 : 1 : 0). Affine conversion: Fermat inversion Z^(p-2) by CT
33note multiplication (the exponent is public, so its branch pattern is fixed per curve).
34note Timing caveats: omc emits plain AArch64 (MUL, ADD, SUB, EOR, AND, ORR, LSL/LSR); the DIT bit is
35note not set by this unit. The workspace (scalar, ladder points, temporaries) is overwritten with 0
36note before every return.
37note
38note Workspace w (ECWN cells, layout of ec.om). This unit adds names for slots ec.om uses only during
39note signature verification: EDR0 (ECGQ) and EDR1 (ECR) ladder points, EDK (ECSR) scalar, EDZI (ECSS)
40note inverse, EDAC (ECAC) inversion accumulator, EDT0..EDT4, EDX3, EDY3, EDZ3 formula temporaries
41note (ECA3..ECA9, ECU1). The peer point is ECQ (ec_pub); ECA0..ECA2 stay free for ec_pub.
42
43const EDR0 ulong 204
44const EDR1 ulong 240
45const EDK ulong 276
46const EDZI ulong 288
47const EDAC ulong 468
48const EDT0 ulong 384
49const EDT1 ulong 396
50const EDT2 ulong 408
51const EDT3 ulong 420
52const EDT4 ulong 432
53const EDX3 ulong 444
54const EDY3 ulong 456
55const EDZ3 ulong 312
56
57note o = T - m if T >= m else T (T has L+1 limbs, T < 2m); modulus m at offset c. The borrow of
58note the full chain gives the mask m = 0 (keep T) or all ones (take T - m); no branch on the data.
59fn ecd_csub w edit ulong ECWN c ulong o ulong to void
60n = get w 0
61br = ulong 0
62x = ulong 0
63for j ulong n
64x = sub sub get w add 2 j get w add c j br
65br = shr x 63
66end
67x = sub get w add 2 n br
68mk = sub shr x 63 1
69br = 0
70for j ulong n
71t = get w add 2 j
72x = sub sub t get w add c j br
73br = shr x 63
74put w add o j xor t and mk xor t and x 0xffffffff
75end
76end
77
78note o = a * b * R^-1 mod m (CIOS as ec_mul, constant-time final subtraction). o may alias a or b.
79fn ecd_mul w edit ulong ECWN c ulong o ulong a ulong b ulong to void
80n = get w 0
81for j ulong add n 2
82put w add 2 j 0
83end
84for i ulong n
85ec_step w c a get w add b i
86end
87ecd_csub w c o
88end
89
90note o = a + b mod m (a, b < m)
91fn ecd_add w edit ulong ECWN c ulong o ulong a ulong b ulong to void
92ec_sum w a b
93ecd_csub w c o
94end
95
96note o = a - b mod m (a, b < m): T = a + m - b in (0, 2m), then the constant-time subtraction
97fn ecd_sub w edit ulong ECWN c ulong o ulong a ulong b ulong to void
98ec_sum w a c
99n = get w 0
100br = ulong 0
101x = ulong 0
102for j ulong n
103x = sub sub get w add 2 j get w add b j br
104put w add 2 j and x 0xffffffff
105br = shr x 63
106end
107put w add 2 n sub get w add 2 n br
108ecd_csub w c o
109end
110
111note o = a + b, complete projective addition for a = -3 (RCB 2016 Algorithm 4, 12M + 2 mb + 29a).
112note Points are (X, Y, Z) in three slots; a == b gives the doubling; o may alias a or b (the
113note result is formed in EDX3, EDY3, EDZ3 after the last read of the inputs).
114fn ecd_padd w edit ulong ECWN o ulong a ulong b ulong to void
115ecd_mul w ECCP EDT0 a b
116ecd_mul w ECCP EDT1 add a 12 add b 12
117ecd_mul w ECCP EDT2 add a 24 add b 24
118ecd_add w ECCP EDT3 a add a 12
119ecd_add w ECCP EDT4 b add b 12
120ecd_mul w ECCP EDT3 EDT3 EDT4
121ecd_add w ECCP EDT4 EDT0 EDT1
122ecd_sub w ECCP EDT3 EDT3 EDT4
123ecd_add w ECCP EDT4 add a 12 add a 24
124ecd_add w ECCP EDX3 add b 12 add b 24
125ecd_mul w ECCP EDT4 EDT4 EDX3
126ecd_add w ECCP EDX3 EDT1 EDT2
127ecd_sub w ECCP EDT4 EDT4 EDX3
128ecd_add w ECCP EDX3 a add a 24
129ecd_add w ECCP EDY3 b add b 24
130ecd_mul w ECCP EDX3 EDX3 EDY3
131ecd_add w ECCP EDY3 EDT0 EDT2
132ecd_sub w ECCP EDY3 EDX3 EDY3
133ecd_mul w ECCP EDZ3 ECB EDT2
134ecd_sub w ECCP EDX3 EDY3 EDZ3
135ecd_add w ECCP EDZ3 EDX3 EDX3
136ecd_add w ECCP EDX3 EDX3 EDZ3
137ecd_sub w ECCP EDZ3 EDT1 EDX3
138ecd_add w ECCP EDX3 EDT1 EDX3
139ecd_mul w ECCP EDY3 ECB EDY3
140ecd_add w ECCP EDT1 EDT2 EDT2
141ecd_add w ECCP EDT2 EDT1 EDT2
142ecd_sub w ECCP EDY3 EDY3 EDT2
143ecd_sub w ECCP EDY3 EDY3 EDT0
144ecd_add w ECCP EDT1 EDY3 EDY3
145ecd_add w ECCP EDY3 EDT1 EDY3
146ecd_add w ECCP EDT1 EDT0 EDT0
147ecd_add w ECCP EDT0 EDT1 EDT0
148ecd_sub w ECCP EDT0 EDT0 EDT2
149ecd_mul w ECCP EDT1 EDT4 EDY3
150ecd_mul w ECCP EDT2 EDT0 EDY3
151ecd_mul w ECCP EDY3 EDX3 EDZ3
152ecd_add w ECCP EDY3 EDY3 EDT2
153ecd_mul w ECCP EDX3 EDT3 EDX3
154ecd_sub w ECCP EDX3 EDX3 EDT1
155ecd_mul w ECCP EDZ3 EDT4 EDZ3
156ecd_mul w ECCP EDT1 EDT3 EDT0
157ecd_add w ECCP EDZ3 EDZ3 EDT1
158ec_copy w o EDX3
159ec_copy w add o 12 EDY3
160ec_copy w add o 24 EDZ3
161end
162
163note swap the points EDR0 and EDR1 when m is all ones, keep them when m is 0 (m = 0 - bit)
164fn ecd_cswap w edit ulong ECWN m ulong to void
165for j ulong 36
166x = get w add EDR0 j
167y = get w add EDR1 j
168d = and m xor x y
169put w add EDR0 j xor x d
170put w add EDR1 j xor y d
171end
172end
173
174note EDR0 = k*P for the scalar k at EDK and the point at slot p (Montgomery, Z = R mod p).
175note Fixed count 32L; the swap before each step uses bit xor previous bit, as x25519.om does.
176fn ecd_ladder w edit ulong ECWN p ulong to void
177for j ulong 36
178put w add EDR0 j 0
179end
180ec_copy w add EDR0 12 add ECCP 16
181ec_pcopy w EDR1 p
182nb = mul get w 0 32
183sw = ulong 0
184for k ulong nb
185i = sub sub nb 1 k
186b = ec_bit w EDK i
187sw = xor sw b
188ecd_cswap w sub 0 sw
189sw = b
190ecd_padd w EDR1 EDR0 EDR1
191ecd_padd w EDR0 EDR0 EDR0
192end
193ecd_cswap w sub 0 sw
194end
195
196note o = a^(p-2) mod p with constant-time multiplication; the exponent p - 2 is public
197fn ecd_inv w edit ulong ECWN o ulong a ulong to void
198ec_copy w EDAC add ECCP 16
199nb = mul get w 0 32
200for k ulong nb
201i = sub sub nb 1 k
202ecd_mul w ECCP EDAC EDAC EDAC
203v = get w add ECCP shr i 5
204if eq shr i 5 0
205v = sub v 2
206end
207if eq and shr v and i 31 1 1
208ecd_mul w ECCP EDAC EDAC a
209end
210end
211ec_copy w o EDAC
212end
213
214note 1 when 1 <= k < n for the scalar k at EDK, else 0: full borrow chain of k - n and an OR of
215note the limbs, no branch on k
216fn ecd_range w edit ulong ECWN to ulong
217n = get w 0
218br = ulong 0
219z = ulong 0
220x = ulong 0
221for j ulong n
222v = get w add EDK j
223x = sub sub v get w add ECCN j br
224br = shr x 63
225z = or z v
226end
227ret and br shr or z sub 0 z 63
228end
229
230note EDR0 to affine (X, Y plain, not Montgomery); returns 1 when Z != 0 (finite point), else 0
231fn ecd_fin w edit ulong ECWN to ulong
232n = get w 0
233z = ulong 0
234for j ulong n
235z = or z get w add add EDR0 24 j
236end
237ecd_inv w EDZI add EDR0 24
238ecd_mul w ECCP EDR0 EDR0 EDZI
239ecd_mul w ECCP EDR0 EDR0 ECON
240ecd_mul w ECCP add EDR0 12 add EDR0 12 EDZI
241ecd_mul w ECCP add EDR0 12 add EDR0 12 ECON
242ret shr or z sub 0 z 63
243end
244
245note write the L-limb number at a as 4L big-endian bytes to o[off..]
246fn ecd_out w edit ulong ECWN a ulong o edit uchar * off ulong to void
247nb = mul get w 0 4
248for i ulong nb
249v = shr get w add a shr i 2 mul and i 3 8
250put o add off sub sub nb 1 i uchar and v 255
251end
252end
253
254fn ecd_wipe w edit ulong ECWN to void
255for j ulong ECWN
256put w j 0
257end
258end
259
260fn ecd_zero o edit uchar * to void
261for j ulong len o
262put o j 0
263end
264end
265
266note curve from the private scalar length: 32 bytes P-256, 48 bytes P-384, else 0
267fn ecd_crv d view uchar * to ulong
268if eq len d 32
269ret 256
270end
271if eq len d 48
272ret 384
273end
274ret 0
275end
276
277note EDR0 (X, Y plain affine) = k*P for the scalar k at EDK and the point at slot p (Montgomery,
278note Z = R mod p); returns 1 when the result is finite, else 0. P-256 (L = 8, public) goes through the
279note constant-time fixed-window p2_smul of ec.om in its own workspace, which is wiped; P-384 through
280note ecd_ladder and ecd_fin. The curve test branches on public data only.
281fn ecd_mult w edit ulong ECWN p ulong to ulong
282if ne get w 0 8
283ecd_ladder w p
284ret ecd_fin w
285end
286v = new ulong P2WN 0
287p2_init v
288ecd_mul w ECCP EDT0 p ECON
289ecd_mul w ECCP EDT1 add p 12 ECON
290for j ulong 8 unroll
291put v add P2K j get w add EDK j
292put v add P2P j get w add EDT0 j
293put v add add P2P 8 j get w add EDT1 j
294end
295f = p2_smul v
296for j ulong 8 unroll
297put w add EDR0 j get v add P2O j
298put w add add EDR0 12 j get v add add P2O 8 j
299end
300p2_wipe v
301ret f
302end
303
304fn ecdh_pub d view uchar * q edit uchar * to ulong
305prev = dit 1
306r = ecdh_pub_core d q
307z = scrub 16384
308z = dit int prev
309ret r
310end
311
312fn ecdh_pub_core d view uchar * q edit uchar * to ulong
313w = new ulong ECWN 0
314crv = ecd_crv d
315if eq crv 0
316ret 5
317end
318nb = len d
319if ne len q add 1 mul nb 2
320ret 2
321end
322rc = ec_setup w crv
323if ne rc 0
324ret rc
325end
326ec_load w EDK d
327ok = ecd_range w
328fin = ecd_mult w ECG
329z = scrub 16384
330put q 0 4
331ecd_out w EDR0 q 1
332ecd_out w add EDR0 12 q add 1 nb
333ecd_wipe w
334if eq and ok fin 0
335ecd_zero q
336ret 4
337end
338ret 0
339end
340
341fn ecdh_shared d view uchar * p view uchar * z edit uchar * to ulong
342prev = dit 1
343r = ecdh_shared_core d p z
344s = scrub 16384
345s = dit int prev
346ret r
347end
348
349fn ecdh_shared_core d view uchar * p view uchar * z edit uchar * to ulong
350w = new ulong ECWN 0
351crv = ecd_crv d
352if eq crv 0
353ret 5
354end
355if ne len z len d
356ret 2
357end
358rc = ec_setup w crv
359if ne rc 0
360ret rc
361end
362ecd_zero z
363rc = ec_pub w p
364if ne rc 0
365ecd_wipe w
366ret 3
367end
368ec_load w EDK d
369ok = ecd_range w
370fin = ecd_mult w ECQ
371s = scrub 16384
372ecd_out w EDR0 z 0
373ecd_wipe w
374if eq ok 0
375ecd_zero z
376ret 4
377end
378if eq fin 0
379ecd_zero z
380ret 1
381end
382ret 0
383end
384
385note pair-wise consistency (SP 800-56A Rev. 3 section 5.6.2.1.4): q passes full public-key
386note validation (section 5.6.2.3.3; cofactor 1, so ec_pub's checks are the full set) and equals
387note d*G recomputed here, compared without an early exit. 0 ok, 6 failed, 2 or 5 as above.
388fn ecdh_pct d view uchar * q view uchar * to ulong
389w = new ulong ECWN 0
390t = new uchar 97 0
391crv = ecd_crv d
392if eq crv 0
393ret 5
394end
395ql = add 1 mul len d 2
396if ne len q ql
397ret 2
398end
399rc = ec_setup w crv
400if ne rc 0
401ret rc
402end
403rc = ec_pub w q
404ecd_wipe w
405if ne rc 0
406ret 6
407end
408rc = ecdh_pub d part t 0 ql
409acc = ulong 0
410for j ulong ql
411acc = or acc ulong xor get t j get q j
412end
413ecd_zero t
414if or ne rc 0 ne acc 0
415ret 6
416end
417ret 0
418end