forked from GaloisInc/cryptol-specs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecdsa.cry
347 lines (277 loc) · 13.5 KB
/
ecdsa.cry
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/* Reference ECDSA signature algorithm as formalized from [ANSI X9.62-2005, pg 8]
Reference ECDSA signature verification algorithm as formalized from [ANSI X9.62-2005, pg 9]
Copyright (c) 2018, Galois Inc.
www.cryptol.net
You can freely use this source code for educational purposes.
*/
module Primitive::Asymmetric::Signature::ecdsa where
import Common::bv
import Common::mod_arith
import Common::mul_java
import Common::Field /* Defines Field type and generic operations */
import Common::EC::ec_point_ops
import Common::EC::ref_ec_mul
import Common::EC::p384_field
import Common::EC::p384_ec_point_ops
import Common::EC::p384_ec_mul
/* Scalar multiply operations {{{1 */
test_params : ([384], AffinePoint [384], [384], AffinePoint [384])
test_params =
// (0x7b52dc21312701d79a2465bb9fdf6fd30c4f76cf74787e44348f8217e6ffd5e02ee8c55a7c5d91b452825b1026d26893, {x=0xdf1c86a62d76090308a0d4c371704b36f3bfa41f0dbf8185d297b8a2c811756f0f4be19871cde079c5d43cfd0fab7fe8; y=0x70521fdd9165b0d33e948be941c590f2fbe7e2754ba110ae24089d41773acfdd34701acaafa669a884d3eefbf557d98b}, 0x211bb4730ad83e31aedbd66a5a750e800cf4de096495323ea94a73f25e9bcb5068a9dcc93e98efecb136eb057e6f41a4, {x=0x6bdf86d8f4cdf5b63a6a1e93a75457c0816fa2f3f69bc706ab69a7b5a63d4956d83cc4ca551f6aaf7653b8dd0991a6af; y=0xa8ce019e78abd658350e5885c8fddb172117a24bf157dfdbe8a1831e03758e630784470287c8e25908d931f22581bf97});
// (0x6250471849e0c0eb47e9cf2c8e04c9ad0745e358d1cf11fb53e178a3c5b301b5f9e7f62e61c8f2113615cc4ff9927f9b, {x=0x005255b811499293ea986c66b4698e0748284d391130afd3f9febbc71ef3b39fe13a3deaa1489d817b885edf270b1bf6; y=0xc9a1dcdb74a4ae432edea47750d754d918b2f1fd8d9ee612b4f0cf2bebcb0eeef43ef5d0ce90cb75362bd2cfe69bffad}, 0xd9e8d87abffb84c0c06fe47396aa84871f08420561a7d8cc64fef7796fc0ca9a5406a37a054cc8cd57e26a3a7249fdb3, {x=0x8da95367d9e731f86adda9bfae6ff170cf090be5223f3b33c0d7c6c585bfc1330137d237fdc16bd432797a1da1dfa331; y=0x114d8b24d75d0149603aa6eab6ccec1cd83f5baaca7898dd6edd7c2ca91176893102b9fe129a09f9fdb208e18be46d16});
(0xe74872524f90425d832a4f264e1f7b9209ec7456e9b4c467d8eac6c68f973ea7fea24f4d3fa8498bb1e26f933eaa261b
, { x=0xbbab93c429707b2afe9ac285314d7b40c4ee31531b13291aa9058307cdd038b572fdf2f3a84dc912804fa426fe531a3c
, y=0x4c2cffaa333bfd82b575918bb979bf51480c836082c097a03fed5454877b37d2f44f0401d212c16b05052bd383047b31
}
, 0xd4356773f2b311955542c5835696b9dca9d0e59197d5746bbf7dc079a666d955f74114662e38d31196bf94b060cc0c80
, { x=0xd40c63c65b413d0741c56e884f0dca63b822a6f6879d36f9261d9b4a270d5d79a9be790fdfb871b5d6ae9204da692663
, y=0x4d6b2b937c85b3c0c3cd2df85e9307cc89e0093f336af3b4531aa126bbdbec466562d8aa0dc1cf360fc1f1d0951e3ad1
})
/* ECDSA Operations {{{1 */
/** Operations for ECDSA abstraction layer */
type Curve fv gv = {
point_ops : PointOps fv gv
, base : AffinePoint fv
, affinify : JacobianPoint fv -> AffinePoint fv
, mul : (gv, AffinePoint fv) -> JacobianPoint fv
, twin_mul : (gv, AffinePoint fv, gv, AffinePoint fv) -> JacobianPoint fv
}
/* Create a public key from a private key */
initialize_public_key : {fv} (Curve [fv] [fv], [fv]) -> AffinePoint [fv]
initialize_public_key(c, k) = c.affinify(c.mul(k, c.base))
/**
* Reference ECDSA signature algorithm as formalized from [ANSI X9.62-2005, pg 8]
* Parameters:
* Curve c
* Private key d of signatory.
* Integer e obtained after hashing message on step e)
* Ephemeral EC private key in range [1 .. c.curve)
*/
ecdsa_sign : {fv} (fin fv, fv >= 1) => (Curve [fv] [fv], [fv], [fv], [fv]) -> ([fv], [fv])
ecdsa_sign(c,d,e,k) = if is_zero(r) || is_zero(s) then (0,0) else (r, s)
where
g = c.point_ops.group_field
is_zero(x) = g.is_equal(x, g.field_zero)
/* Compute x coordinate of public key. */
j = (c.affinify(c.mul(k, c.base))).x
/* c) Set r = j mod n */
r = g.normalize(j)
s = g.div(g.add(g.normalize(e), g.mul(d, r)), k)
/**
* Reference ECDSA signature verification algorithm as formalized from [ANSI X9.62-2005, pg 9]
* Parameters
* Curve c
* Integer e' obtained after hashing message on step "c".
* Pair returned by ref_ecdsa_sign algorithm.
* Public key of key signatory.
* Result:
* Bit that is True if verification suceeds, and false otherwise.
*/
ecdsa_public_verify : {fv} (fin fv, fv >= 1)
=> (Curve [fv] [fv], [fv], ([fv], [fv]), AffinePoint [fv]) -> Bit
ecdsa_public_verify(c, e, (r, s), q)
/* h) Compare v and r' if v = r', output "valid"; otherwise output "invalid". */
= in_range(s) && (v == r)
where
f = c.point_ops.field
g = c.point_ops.group_field
mul(x,y) = g.mul(x, y)
div(x,y) = g.div(x, y)
is_zero(x) = g.is_equal(x, g.field_zero)
in_range(x) = ~is_zero(x) && g.is_val(x)
s_inv = div(1, s)
u1 = mul(e, s_inv)
u2 = mul(r, s_inv)
r2 = c.twin_mul(u1, c.base, u2, q)
/* f) Convert the field element x_r to an integer j as described in A.5 */
j = f.mul(r2.x, f.sq(f.div(f.field_unit, r2.z)))
/* g) v = j mod n */
v = g.normalize(j)
/* This version performs a cheaper sequence of operations, and matches
* the Java implementation. Correspondence between the reference and
* implementation versions of ecdsa_public_verify is probably best proved
* independent of the Java code.
*/
ecdsa_public_verify_imp :
(Curve [384] [384], [384], ([384], [384]), AffinePoint [384]) -> Bit
ecdsa_public_verify_imp(c, e, (r, s), q)
= in_range(r) && in_range(s) &&
~is_zero(r2.z) &&
(g.is_equal(j, r2.x) || ((carrybits == False) && (g.is_equal(k, r2.x))))
where
f = c.point_ops.field
g = c.point_ops.group_field
mul(x,y) = g.mul(x, y)
div(x,y) = g.div(x, y)
is_zero(x) = g.is_equal(x, g.field_zero)
in_range(x) = ~is_zero(x) && g.is_val(x)
s_inv = div(1, s)
u1 = mul(g.normalize(e), s_inv)
u2 = mul(r, s_inv)
r2 = c.twin_mul(u1, c.base, u2, q)
r2zsq = f.sq(r2.z)
j = f.mul(r, r2zsq)
summ = r + p384_group_size
carrybits = (summ < r) && (summ < p384_group_size)
k = f.mul(summ, r2zsq)
/* P384 reference definitions {{{1 */
/* Utility functions {{{2 */
type RsltWithCarry a b = { carrybits : a , rslt : b }
p384_incFieldPrime : [384] -> RsltWithCarry [32] [384]
p384_incFieldPrime x = { carrybits = zero # [(bs @ 0)]
, rslt = drop(bs) : [384]
}
where bs = safe_add(x, p384_prime)
p384_decFieldPrime : [384] -> RsltWithCarry [32] [384]
p384_decFieldPrime x = { carrybits = if (bs @ 0) then -1 else 0
, rslt = drop(bs) : [384]
}
where bs = safe_sub(x, p384_prime)
/* P384 modular arithmetic definitions. {{{2 */
/* P384 field definitions. {{{2 */
p384_field_cube(x) = p384_field_mul(x, p384_field_sq(x))
/* P384 group field operations {{{2 */
/** P384 curve operations {{{2 */
p384_base : AffinePoint [384]
p384_base = nzAffinePoint(
join [0xaa87ca22, 0xbe8b0537, 0x8eb1c71e, 0xf320ad74, 0x6e1d3b62, 0x8ba79b98
,0x59f741e0, 0x82542a38, 0x5502f25d, 0xbf55296c, 0x3a545e38, 0x72760ab7],
join [0x3617de4a, 0x96262c6f, 0x5d9e98bf, 0x9292dc29, 0xf8f41dbd, 0x289a147c
,0xe9da3113, 0xb5f0b8c0, 0x0a60b1ce, 0x1d7e819d, 0x7a431d7c, 0x90ea0e5f])
p384_group_add : ([384],[384]) -> [384]
p384_group_add = p384_group_field.add
p384_curve : Curve [384] [384]
p384_curve =
{ point_ops = p384_point_ops
, base = p384_base
, affinify = \x -> ec_affinify(p384_field, x)
, mul = p384_ec_mul
, twin_mul = p384_ec_twin_mul
}
p384_ecdsa_sign : ([384], [384], [384]) -> { r:[384], s:[384] }
p384_ecdsa_sign(d,e,k) = { r = rr, s = ss }
where (rr, ss) = ecdsa_sign(p384_curve,d,e,k)
p384_ecdsa_public_verify : ([384], [384], [384], AffinePoint [384]) -> Bit
p384_ecdsa_public_verify(e,pr,ps,q) =
ecdsa_public_verify_imp(p384_curve,e,(pr,ps),q)
/* P384 test code {{{2 */
/* Curve "a" parameter, defined in [FIPS-186-3, page 87] to be -3 */
p384_a : [384]
p384_a = p384_prime - 3
/* Curve "b" parameter, defined in [FIPS-186-3, page 89] */
p384_b : [384]
p384_b = 0xb3312fa7e23ee7e4988e056be3f82d19181d9c6efe8141120314088f5013875ac656398d8a2ed19d2a85c8edd3ec2aef
/* Convert a p384 Jacobian point to an affine point */
// p384_affinify : JacobianPoint [384] -> AffinePoint [384]
// p384_affinify p = ec_affinify(p384_field, p)
/* Convert a p384 affine point to a Jacobian point */
p384_jacobify : AffinePoint [384] -> JacobianPoint [384]
p384_jacobify p = { x = p.x, y = p.y, z = 1 }
p384_base4 : JacobianPoint [384]
p384_base4 = p384_ec_double(p384_ec_double(p384_jacobify(p384_base)))
p384_base3 : AffinePoint [384]
p384_base3 = p384_affinify(p384_ec_full_sub(p384_base4, p384_base))
p384_base5 : AffinePoint [384]
p384_base5 = p384_affinify(p384_ec_full_add(p384_base4, p384_base))
/* Does an affine point lie on the curve? */
p384_is_affine_point : AffinePoint [384] -> Bit
p384_is_affine_point p =
p384_field_sq(p.y) ==
p384_field_add(p384_field_cube(p.x),
p384_field_add(p384_field_mul(p384_a, p.x), p384_b))
/* P384 implementation curve definition {{{1 */
/* Chunked arithmetic routines {{{2 */
/* Tests for p384 */
/* Point "S" defined in [NSA 2008, Section 4.4.2] */
p384_s : AffinePoint [384]
p384_s = nzAffinePoint(
join [0xfba203b8, 0x1bbd23f2, 0xb3be971c, 0xc23997e1, 0xae4d89e6, 0x9cb6f923,
0x85dda827, 0x68ada415, 0xebab4167, 0x459da98e, 0x62b1332d, 0x1e73cb0e],
join [0x5ffedbae, 0xfdeba603, 0xe7923e06, 0xcdb5d0c6, 0x5b223014, 0x29293376,
0xd5c6944e, 0x3fa6259f, 0x162b4788, 0xde6987fd, 0x59aed5e4, 0xb5285e45])
/* Point "T" defined in [NSA 2008, Section 4.4.2] */
p384_t : AffinePoint [384]
p384_t = nzAffinePoint(
join [0xaacc0520, 0x2e7fda6f, 0xc73d82f0, 0xa6622052, 0x7da8117e, 0xe8f8330e,
0xad7d20ee, 0x6f255f58, 0x2d8bd38c, 0x5a7f2b40, 0xbcdb68ba, 0x13d81051],
join [0x84009a26, 0x3fefba7c, 0x2c57cffa, 0x5db3634d, 0x286131af, 0xc0fca8d2,
0x5afa22a7, 0xb5dce0d9, 0x470da892, 0x33cee178, 0x592f49b6, 0xfecb5092])
/* Multiple "D" defined in [NSA 2008, Section 4.4.2] */
p384_d =
join [0xa4ebcae5, 0xa6659834, 0x93ab3e62, 0x6085a24c, 0x104311a7, 0x61b5a8fd,
0xac052ed1, 0xf111a5c4, 0x4f76f456, 0x59d2d111, 0xa61b5fdd, 0x97583480]
/* Multiple "E" defined in [NSA 2008, Section 4.4.2] */
p384_e =
join [0xafcf8811, 0x9a3a76c8, 0x7acbd600, 0x8e1349b2, 0x9f4ba9aa, 0x0e12ce89,
0xbcfcae21, 0x80b38d81, 0xab8cf150, 0x95301a18, 0x2afbc689, 0x3e75385d]
p384_projectify : AffinePoint [384] -> JacobianPoint [384]
p384_projectify(p) = ec_projectify(f,p)
where f = p384_curve.point_ops.field
p384_affinify : JacobianPoint [384] -> AffinePoint [384]
p384_affinify(p) = ec_affinify(f,p)
where f = p384_curve.point_ops.field
test_p384_s_plus_t : AffinePoint [384]
test_p384_s_plus_t = p384_affinify(add(p384_projectify(s),t))
where s = p384_s
t = p384_t
add = p384_curve.point_ops.add
expected_p384_s_plus_t : AffinePoint [384]
expected_p384_s_plus_t =
{ x = join [0x12dc5ce7, 0xacdfc584, 0x4d939f40, 0xb4df012e, 0x68f865b8, 0x9c3213ba,
0x97090a24, 0x7a2fc009, 0x075cf471, 0xcd2e85c4, 0x89979b65, 0xee0b5eed]
, y = join [0x167312e5, 0x8fe0c0af, 0xa248f285, 0x4e3cddcb, 0x557f983b, 0x3189b67f,
0x21eee013, 0x41e7e9fe, 0x67f6ee81, 0xb36988ef, 0xa406945c, 0x8804a4b0]
}
test_p384_s_minus_t : AffinePoint [384]
test_p384_s_minus_t = p384_affinify(sub(p384_projectify(s),t))
where s = p384_s
t = p384_t
sub = p384_curve.point_ops.sub
expected_p384_s_minus_t : AffinePoint [384]
expected_p384_s_minus_t =
{ x = join [0x6afdaf8d, 0xa8b11c98, 0x4cf177e5, 0x51cee542, 0xcda4ac2f, 0x25cd522d,
0x0cd710f8, 0x8059c656, 0x5aef78f6, 0xb5ed6cc0, 0x5a6666de, 0xf2a2fb59]
, y = join [0x7bed0e15, 0x8ae8cc70, 0xe847a603, 0x47ca1548, 0xc348decc, 0x6309f48b,
0x59bd5afc, 0x9a9b804e, 0x7f787617, 0x8cb5a7eb, 0x4f6940a9, 0xc73e8e5e]
}
test_p384_double_s : AffinePoint [384]
test_p384_double_s = p384_affinify(dbl(p384_projectify(s)))
where s = p384_s
dbl = p384_curve.point_ops.double
expected_p384_double_s : AffinePoint [384]
expected_p384_double_s =
{ x = join [0x2a2111b1, 0xe0aa8b2f, 0xc5a19755, 0x16bc4d58, 0x017ff96b, 0x25e1bdff,
0x3c229d5f, 0xac3bacc3, 0x19dcbec2, 0x9f9478f4, 0x2dee597b, 0x4641504c]
, y = join [0xfa2e3d9d, 0xc84db895, 0x4ce8085e, 0xf28d7184, 0xfddfd134, 0x4b4d4797,
0x343af9b5, 0xf9d83752, 0x0b450f72, 0x6443e411, 0x4bd4e5bd, 0xb2f65ddd]
}
test_p384_ds : AffinePoint [384]
test_p384_ds = p384_affinify(mul(p384_d, p384_s))
where mul = p384_curve.mul
expected_p384_ds : AffinePoint [384]
expected_p384_ds =
{ x = join [0xe4f77e7f, 0xfeb7f095, 0x8910e3a6, 0x80d677a4, 0x77191df1, 0x66160ff7,
0xef6bb526, 0x1f791aa7, 0xb45e3e65, 0x3d151b95, 0xdad3d93c, 0xa0290ef2]
, y = join [0xac7dee41, 0xd8c5f4a7, 0xd5836960, 0xa773cfc1, 0x376289d3, 0x373f8cf7,
0x417b0c62, 0x07ac32e9, 0x13856612, 0xfc9ff2e3, 0x57eb2ee0, 0x5cf9667f]
}
test_p384_ds_et : AffinePoint [384]
test_p384_ds_et = p384_affinify(twin_mul(p384_d, p384_s, p384_e, p384_t))
where twin_mul = p384_curve.twin_mul
expected_p384_ds_et : AffinePoint [384]
expected_p384_ds_et =
{ x = join [0x917ea28b, 0xcd641741, 0xae5d18c2, 0xf1bd917b, 0xa68d34f0, 0xf0577387,
0xdc812604, 0x62aea60e, 0x2417b8bd, 0xc5d954fc, 0x729d211d, 0xb23a02dc]
, y = join [0x1a29f7ce, 0x6d074654, 0xd77b4088, 0x8c73e925, 0x46c8f16a, 0x5ff6bcbd,
0x307f758d, 0x4aee684b, 0xeff26f67, 0x42f597e2, 0x585c86da, 0x908f7186]
}
property test_plus =
test_p384_s_plus_t == expected_p384_s_plus_t
property test_minus =
test_p384_s_minus_t == expected_p384_s_minus_t
property test_double =
test_p384_double_s == expected_p384_double_s
property test_ds =
test_p384_ds == expected_p384_ds
property test_ds_et =
test_p384_ds_et == expected_p384_ds_et