1*0957b409SSimon J. Gerraty /*
2*0957b409SSimon J. Gerraty * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
3*0957b409SSimon J. Gerraty *
4*0957b409SSimon J. Gerraty * Permission is hereby granted, free of charge, to any person obtaining
5*0957b409SSimon J. Gerraty * a copy of this software and associated documentation files (the
6*0957b409SSimon J. Gerraty * "Software"), to deal in the Software without restriction, including
7*0957b409SSimon J. Gerraty * without limitation the rights to use, copy, modify, merge, publish,
8*0957b409SSimon J. Gerraty * distribute, sublicense, and/or sell copies of the Software, and to
9*0957b409SSimon J. Gerraty * permit persons to whom the Software is furnished to do so, subject to
10*0957b409SSimon J. Gerraty * the following conditions:
11*0957b409SSimon J. Gerraty *
12*0957b409SSimon J. Gerraty * The above copyright notice and this permission notice shall be
13*0957b409SSimon J. Gerraty * included in all copies or substantial portions of the Software.
14*0957b409SSimon J. Gerraty *
15*0957b409SSimon J. Gerraty * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16*0957b409SSimon J. Gerraty * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17*0957b409SSimon J. Gerraty * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18*0957b409SSimon J. Gerraty * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19*0957b409SSimon J. Gerraty * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20*0957b409SSimon J. Gerraty * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21*0957b409SSimon J. Gerraty * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*0957b409SSimon J. Gerraty * SOFTWARE.
23*0957b409SSimon J. Gerraty */
24*0957b409SSimon J. Gerraty
25*0957b409SSimon J. Gerraty #include "inner.h"
26*0957b409SSimon J. Gerraty
27*0957b409SSimon J. Gerraty #define I15_LEN ((BR_MAX_EC_SIZE + 29) / 15)
28*0957b409SSimon J. Gerraty #define POINT_LEN (1 + (((BR_MAX_EC_SIZE + 7) >> 3) << 1))
29*0957b409SSimon J. Gerraty
30*0957b409SSimon J. Gerraty /* see bearssl_ec.h */
31*0957b409SSimon J. Gerraty uint32_t
br_ecdsa_i15_vrfy_raw(const br_ec_impl * impl,const void * hash,size_t hash_len,const br_ec_public_key * pk,const void * sig,size_t sig_len)32*0957b409SSimon J. Gerraty br_ecdsa_i15_vrfy_raw(const br_ec_impl *impl,
33*0957b409SSimon J. Gerraty const void *hash, size_t hash_len,
34*0957b409SSimon J. Gerraty const br_ec_public_key *pk,
35*0957b409SSimon J. Gerraty const void *sig, size_t sig_len)
36*0957b409SSimon J. Gerraty {
37*0957b409SSimon J. Gerraty /*
38*0957b409SSimon J. Gerraty * IMPORTANT: this code is fit only for curves with a prime
39*0957b409SSimon J. Gerraty * order. This is needed so that modular reduction of the X
40*0957b409SSimon J. Gerraty * coordinate of a point can be done with a simple subtraction.
41*0957b409SSimon J. Gerraty */
42*0957b409SSimon J. Gerraty const br_ec_curve_def *cd;
43*0957b409SSimon J. Gerraty uint16_t n[I15_LEN], r[I15_LEN], s[I15_LEN], t1[I15_LEN], t2[I15_LEN];
44*0957b409SSimon J. Gerraty unsigned char tx[(BR_MAX_EC_SIZE + 7) >> 3];
45*0957b409SSimon J. Gerraty unsigned char ty[(BR_MAX_EC_SIZE + 7) >> 3];
46*0957b409SSimon J. Gerraty unsigned char eU[POINT_LEN];
47*0957b409SSimon J. Gerraty size_t nlen, rlen, ulen;
48*0957b409SSimon J. Gerraty uint16_t n0i;
49*0957b409SSimon J. Gerraty uint32_t res;
50*0957b409SSimon J. Gerraty
51*0957b409SSimon J. Gerraty /*
52*0957b409SSimon J. Gerraty * If the curve is not supported, then report an error.
53*0957b409SSimon J. Gerraty */
54*0957b409SSimon J. Gerraty if (((impl->supported_curves >> pk->curve) & 1) == 0) {
55*0957b409SSimon J. Gerraty return 0;
56*0957b409SSimon J. Gerraty }
57*0957b409SSimon J. Gerraty
58*0957b409SSimon J. Gerraty /*
59*0957b409SSimon J. Gerraty * Get the curve parameters (generator and order).
60*0957b409SSimon J. Gerraty */
61*0957b409SSimon J. Gerraty switch (pk->curve) {
62*0957b409SSimon J. Gerraty case BR_EC_secp256r1:
63*0957b409SSimon J. Gerraty cd = &br_secp256r1;
64*0957b409SSimon J. Gerraty break;
65*0957b409SSimon J. Gerraty case BR_EC_secp384r1:
66*0957b409SSimon J. Gerraty cd = &br_secp384r1;
67*0957b409SSimon J. Gerraty break;
68*0957b409SSimon J. Gerraty case BR_EC_secp521r1:
69*0957b409SSimon J. Gerraty cd = &br_secp521r1;
70*0957b409SSimon J. Gerraty break;
71*0957b409SSimon J. Gerraty default:
72*0957b409SSimon J. Gerraty return 0;
73*0957b409SSimon J. Gerraty }
74*0957b409SSimon J. Gerraty
75*0957b409SSimon J. Gerraty /*
76*0957b409SSimon J. Gerraty * Signature length must be even.
77*0957b409SSimon J. Gerraty */
78*0957b409SSimon J. Gerraty if (sig_len & 1) {
79*0957b409SSimon J. Gerraty return 0;
80*0957b409SSimon J. Gerraty }
81*0957b409SSimon J. Gerraty rlen = sig_len >> 1;
82*0957b409SSimon J. Gerraty
83*0957b409SSimon J. Gerraty /*
84*0957b409SSimon J. Gerraty * Public key point must have the proper size for this curve.
85*0957b409SSimon J. Gerraty */
86*0957b409SSimon J. Gerraty if (pk->qlen != cd->generator_len) {
87*0957b409SSimon J. Gerraty return 0;
88*0957b409SSimon J. Gerraty }
89*0957b409SSimon J. Gerraty
90*0957b409SSimon J. Gerraty /*
91*0957b409SSimon J. Gerraty * Get modulus; then decode the r and s values. They must be
92*0957b409SSimon J. Gerraty * lower than the modulus, and s must not be null.
93*0957b409SSimon J. Gerraty */
94*0957b409SSimon J. Gerraty nlen = cd->order_len;
95*0957b409SSimon J. Gerraty br_i15_decode(n, cd->order, nlen);
96*0957b409SSimon J. Gerraty n0i = br_i15_ninv15(n[1]);
97*0957b409SSimon J. Gerraty if (!br_i15_decode_mod(r, sig, rlen, n)) {
98*0957b409SSimon J. Gerraty return 0;
99*0957b409SSimon J. Gerraty }
100*0957b409SSimon J. Gerraty if (!br_i15_decode_mod(s, (const unsigned char *)sig + rlen, rlen, n)) {
101*0957b409SSimon J. Gerraty return 0;
102*0957b409SSimon J. Gerraty }
103*0957b409SSimon J. Gerraty if (br_i15_iszero(s)) {
104*0957b409SSimon J. Gerraty return 0;
105*0957b409SSimon J. Gerraty }
106*0957b409SSimon J. Gerraty
107*0957b409SSimon J. Gerraty /*
108*0957b409SSimon J. Gerraty * Invert s. We do that with a modular exponentiation; we use
109*0957b409SSimon J. Gerraty * the fact that for all the curves we support, the least
110*0957b409SSimon J. Gerraty * significant byte is not 0 or 1, so we can subtract 2 without
111*0957b409SSimon J. Gerraty * any carry to process.
112*0957b409SSimon J. Gerraty * We also want 1/s in Montgomery representation, which can be
113*0957b409SSimon J. Gerraty * done by converting _from_ Montgomery representation before
114*0957b409SSimon J. Gerraty * the inversion (because (1/s)*R = 1/(s/R)).
115*0957b409SSimon J. Gerraty */
116*0957b409SSimon J. Gerraty br_i15_from_monty(s, n, n0i);
117*0957b409SSimon J. Gerraty memcpy(tx, cd->order, nlen);
118*0957b409SSimon J. Gerraty tx[nlen - 1] -= 2;
119*0957b409SSimon J. Gerraty br_i15_modpow(s, tx, nlen, n, n0i, t1, t2);
120*0957b409SSimon J. Gerraty
121*0957b409SSimon J. Gerraty /*
122*0957b409SSimon J. Gerraty * Truncate the hash to the modulus length (in bits) and reduce
123*0957b409SSimon J. Gerraty * it modulo the curve order. The modular reduction can be done
124*0957b409SSimon J. Gerraty * with a subtraction since the truncation already reduced the
125*0957b409SSimon J. Gerraty * value to the modulus bit length.
126*0957b409SSimon J. Gerraty */
127*0957b409SSimon J. Gerraty br_ecdsa_i15_bits2int(t1, hash, hash_len, n[0]);
128*0957b409SSimon J. Gerraty br_i15_sub(t1, n, br_i15_sub(t1, n, 0) ^ 1);
129*0957b409SSimon J. Gerraty
130*0957b409SSimon J. Gerraty /*
131*0957b409SSimon J. Gerraty * Multiply the (truncated, reduced) hash value with 1/s, result in
132*0957b409SSimon J. Gerraty * t2, encoded in ty.
133*0957b409SSimon J. Gerraty */
134*0957b409SSimon J. Gerraty br_i15_montymul(t2, t1, s, n, n0i);
135*0957b409SSimon J. Gerraty br_i15_encode(ty, nlen, t2);
136*0957b409SSimon J. Gerraty
137*0957b409SSimon J. Gerraty /*
138*0957b409SSimon J. Gerraty * Multiply r with 1/s, result in t1, encoded in tx.
139*0957b409SSimon J. Gerraty */
140*0957b409SSimon J. Gerraty br_i15_montymul(t1, r, s, n, n0i);
141*0957b409SSimon J. Gerraty br_i15_encode(tx, nlen, t1);
142*0957b409SSimon J. Gerraty
143*0957b409SSimon J. Gerraty /*
144*0957b409SSimon J. Gerraty * Compute the point x*Q + y*G.
145*0957b409SSimon J. Gerraty */
146*0957b409SSimon J. Gerraty ulen = cd->generator_len;
147*0957b409SSimon J. Gerraty memcpy(eU, pk->q, ulen);
148*0957b409SSimon J. Gerraty res = impl->muladd(eU, NULL, ulen,
149*0957b409SSimon J. Gerraty tx, nlen, ty, nlen, cd->curve);
150*0957b409SSimon J. Gerraty
151*0957b409SSimon J. Gerraty /*
152*0957b409SSimon J. Gerraty * Get the X coordinate, reduce modulo the curve order, and
153*0957b409SSimon J. Gerraty * compare with the 'r' value.
154*0957b409SSimon J. Gerraty *
155*0957b409SSimon J. Gerraty * The modular reduction can be done with subtractions because
156*0957b409SSimon J. Gerraty * we work with curves of prime order, so the curve order is
157*0957b409SSimon J. Gerraty * close to the field order (Hasse's theorem).
158*0957b409SSimon J. Gerraty */
159*0957b409SSimon J. Gerraty br_i15_zero(t1, n[0]);
160*0957b409SSimon J. Gerraty br_i15_decode(t1, &eU[1], ulen >> 1);
161*0957b409SSimon J. Gerraty t1[0] = n[0];
162*0957b409SSimon J. Gerraty br_i15_sub(t1, n, br_i15_sub(t1, n, 0) ^ 1);
163*0957b409SSimon J. Gerraty res &= ~br_i15_sub(t1, r, 1);
164*0957b409SSimon J. Gerraty res &= br_i15_iszero(t1);
165*0957b409SSimon J. Gerraty return res;
166*0957b409SSimon J. Gerraty }
167