interplane.Get Omega
Read in your language

spark-connector

aien-dev/spark-connector

Brought by drakestapleton

branch main Branches Tags History

spark-connector / runner / 10key.om

162 lines · 3477 bytes Raw Blame History
1note runner/10key.om -- the machine key: NIST P-256 ECDSA with SHA-256 (om/tls ec.om, ecdh.om).
2note The private scalar is made here from getrandom, kept in <state>/key (0600, 32 bytes) and never
3note sent. The public key is 04 || X || Y; the key id is the hex SHA-256 of those 65 bytes.
4note Signing (r_ksign) uses a fresh random k per signature, computes R = k*G with ecdh_pub (the
5note constant-time fixed-window P-256 multiplication of ec.om), and s = k^-1 (e + r d) mod n. Every
6note mod-n product and sum uses ecdh.om's constant-time ecd_mul / ecd_add (no branch on the data);
7note the one variable-time step, the inversion ec_inv, only sees k b for a second random b, so
8note s = (e + r d) b (k b)^-1. Every signature is checked with ecdsa_verify_raw before it is used.
9
10text rtkey "key"
11
12note r_kset: set rpub, rpubh and rkid from the public point q
13fn r_kset q view uchar * to void
14h = new uchar 32 0
15x = new uchar 130 0
16k = r_hex q x 0
17for i ulong 130
18put rpubh i get x i
19end
20for i ulong 65
21put rpub i get q i
22end
23r_sha q h
24k = r_hex h x 0
25for i ulong 64
26put rkid i get x i
27end
28end
29
30note r_kload: read <state>/key and derive the public key
31fn r_kload to bool
32d = new uchar 32 0
33q = new uchar 65 0
34n = r_path rtkey
35r = r_rd d
36if ne r 32
37r_wipe d
38ret false
39end
40e = ecdh_pub d q
41if ne e 0
42r_wipe d
43ret false
44end
45for i ulong 32
46put rsk i get d i
47end
48r_wipe d
49r_kset q
50ret true
51end
52
53note r_kgen: a new key, written to <state>/key with O_EXCL and mode 0600
54fn r_kgen to bool
55d = new uchar 32 0
56q = new uchar 65 0
57for t ulong 16
58r = random d 0 32 0
59if ne r 32
60ret false
61end
62e = ecdh_pub d q
63if eq e 0
64n = r_path rtkey
65ok = r_wr d rfonew 384
66for i ulong 32
67put rsk i get d i
68end
69r_wipe d
70if not ok
71ret false
72end
73r_kset q
74ret true
75end
76end
77ret false
78end
79
80note r_kin: true when x (slot o of w) is in [1, n-1]
81fn r_kin w edit ulong ECWN o ulong to bool
82if eq ec_cmp w o ECZ 0
83ret false
84end
85ret eq ec_cmp w o ECCN 1
86end
87
88note r_ksign: the 128-hex signature r || s of m into sg
89note in: [0,32) e = SHA-256(m), [32,64) d, [64,96) k, [96,128) the blind b
90fn r_ksign m view uchar * sg edit uchar * to bool
91w = new ulong ECWN 0
92h = new uchar 32 0
93in = new uchar 128 0
94q = new uchar 65 0
95hs = new uchar 96 0
96r_sha m h
97for i ulong 32
98put in i get h i
99put in add 32 i get rsk i
100end
101ok = false
102for t ulong 16
103x = random in 64 64 0
104if eq x 64
105if eq ecdh_pub part in 64 32 q 0
106ok = r_ksig2 w in q hs
107end
108end
109if ok
110break
111end
112end
113r_wipe in
114if not ok
115ret false
116end
117for i ulong 65
118put q i get rpub i
119end
120if ne ecdsa_verify_raw 256 q hs 32 0
121ret false
122end
123k = r_hex part hs 32 64 sg 0
124ret true
125end
126
127note r_ksig2: from R (q) and in (e, d, k, b), the signature into hs[32..96); hs[0..32) = e
128fn r_ksig2 w edit ulong ECWN in view uchar 128 q view uchar 65 hs edit uchar 96 to bool
129x = ec_setup w 256
130if ne x 0
131ret false
132end
133ec_load w ECA0 part q 1 32
134ecd_add w ECCN ECA0 ECA0 ECZ
135ec_load w ECA4 part in 96 32
136if not and r_kin w ECA0 r_kin w ECA4
137ret false
138end
139ec_load w ECA1 part in 0 32
140ecd_add w ECCN ECA1 ECA1 ECZ
141ec_load w ECA2 part in 32 32
142ec_load w ECA3 part in 64 32
143ecd_mul w ECCN ECA5 ECA4 add ECCN 28
144ecd_mul w ECCN ECA6 ECA3 ECA5
145ecd_mul w ECCN ECA6 ECA6 add ECCN 28
146ec_inv w ECCN ECA6 ECA6
147ecd_mul w ECCN ECA7 ECA2 add ECCN 28
148ecd_mul w ECCN ECA8 ECA0 ECA7
149ecd_add w ECCN ECA8 ECA8 ECA1
150ecd_mul w ECCN ECA8 ECA8 ECA5
151ecd_mul w ECCN ECA9 ECA8 ECA6
152ok = r_kin w ECA9
153for i ulong 32
154put hs i get in i
155end
156ecd_out w ECA0 hs 32
157ecd_out w ECA9 hs 64
158for i ulong ECWN
159put w i 0
160end
161ret ok
162end