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