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