xref: /freebsd/contrib/bearssl/src/ec/ecdsa_i15_sign_raw.c (revision 2aaf9152a852aba9eb2036b95f4948ee77988826)
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 #define ORDER_LEN   ((BR_MAX_EC_SIZE + 7) >> 3)
30*0957b409SSimon J. Gerraty 
31*0957b409SSimon J. Gerraty /* see bearssl_ec.h */
32*0957b409SSimon J. Gerraty size_t
br_ecdsa_i15_sign_raw(const br_ec_impl * impl,const br_hash_class * hf,const void * hash_value,const br_ec_private_key * sk,void * sig)33*0957b409SSimon J. Gerraty br_ecdsa_i15_sign_raw(const br_ec_impl *impl,
34*0957b409SSimon J. Gerraty 	const br_hash_class *hf, const void *hash_value,
35*0957b409SSimon J. Gerraty 	const br_ec_private_key *sk, void *sig)
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 	 * We also rely on the last byte of the curve order to be distinct
42*0957b409SSimon J. Gerraty 	 * from 0 and 1.
43*0957b409SSimon J. Gerraty 	 */
44*0957b409SSimon J. Gerraty 	const br_ec_curve_def *cd;
45*0957b409SSimon J. Gerraty 	uint16_t n[I15_LEN], r[I15_LEN], s[I15_LEN], x[I15_LEN];
46*0957b409SSimon J. Gerraty 	uint16_t m[I15_LEN], k[I15_LEN], t1[I15_LEN], t2[I15_LEN];
47*0957b409SSimon J. Gerraty 	unsigned char tt[ORDER_LEN << 1];
48*0957b409SSimon J. Gerraty 	unsigned char eU[POINT_LEN];
49*0957b409SSimon J. Gerraty 	size_t hash_len, nlen, ulen;
50*0957b409SSimon J. Gerraty 	uint16_t n0i;
51*0957b409SSimon J. Gerraty 	uint32_t ctl;
52*0957b409SSimon J. Gerraty 	br_hmac_drbg_context drbg;
53*0957b409SSimon J. Gerraty 
54*0957b409SSimon J. Gerraty 	/*
55*0957b409SSimon J. Gerraty 	 * If the curve is not supported, then exit with an error.
56*0957b409SSimon J. Gerraty 	 */
57*0957b409SSimon J. Gerraty 	if (((impl->supported_curves >> sk->curve) & 1) == 0) {
58*0957b409SSimon J. Gerraty 		return 0;
59*0957b409SSimon J. Gerraty 	}
60*0957b409SSimon J. Gerraty 
61*0957b409SSimon J. Gerraty 	/*
62*0957b409SSimon J. Gerraty 	 * Get the curve parameters (generator and order).
63*0957b409SSimon J. Gerraty 	 */
64*0957b409SSimon J. Gerraty 	switch (sk->curve) {
65*0957b409SSimon J. Gerraty 	case BR_EC_secp256r1:
66*0957b409SSimon J. Gerraty 		cd = &br_secp256r1;
67*0957b409SSimon J. Gerraty 		break;
68*0957b409SSimon J. Gerraty 	case BR_EC_secp384r1:
69*0957b409SSimon J. Gerraty 		cd = &br_secp384r1;
70*0957b409SSimon J. Gerraty 		break;
71*0957b409SSimon J. Gerraty 	case BR_EC_secp521r1:
72*0957b409SSimon J. Gerraty 		cd = &br_secp521r1;
73*0957b409SSimon J. Gerraty 		break;
74*0957b409SSimon J. Gerraty 	default:
75*0957b409SSimon J. Gerraty 		return 0;
76*0957b409SSimon J. Gerraty 	}
77*0957b409SSimon J. Gerraty 
78*0957b409SSimon J. Gerraty 	/*
79*0957b409SSimon J. Gerraty 	 * Get modulus.
80*0957b409SSimon J. Gerraty 	 */
81*0957b409SSimon J. Gerraty 	nlen = cd->order_len;
82*0957b409SSimon J. Gerraty 	br_i15_decode(n, cd->order, nlen);
83*0957b409SSimon J. Gerraty 	n0i = br_i15_ninv15(n[1]);
84*0957b409SSimon J. Gerraty 
85*0957b409SSimon J. Gerraty 	/*
86*0957b409SSimon J. Gerraty 	 * Get private key as an i15 integer. This also checks that the
87*0957b409SSimon J. Gerraty 	 * private key is well-defined (not zero, and less than the
88*0957b409SSimon J. Gerraty 	 * curve order).
89*0957b409SSimon J. Gerraty 	 */
90*0957b409SSimon J. Gerraty 	if (!br_i15_decode_mod(x, sk->x, sk->xlen, n)) {
91*0957b409SSimon J. Gerraty 		return 0;
92*0957b409SSimon J. Gerraty 	}
93*0957b409SSimon J. Gerraty 	if (br_i15_iszero(x)) {
94*0957b409SSimon J. Gerraty 		return 0;
95*0957b409SSimon J. Gerraty 	}
96*0957b409SSimon J. Gerraty 
97*0957b409SSimon J. Gerraty 	/*
98*0957b409SSimon J. Gerraty 	 * Get hash length.
99*0957b409SSimon J. Gerraty 	 */
100*0957b409SSimon J. Gerraty 	hash_len = (hf->desc >> BR_HASHDESC_OUT_OFF) & BR_HASHDESC_OUT_MASK;
101*0957b409SSimon J. Gerraty 
102*0957b409SSimon J. Gerraty 	/*
103*0957b409SSimon J. Gerraty 	 * Truncate and reduce the hash value modulo the curve order.
104*0957b409SSimon J. Gerraty 	 */
105*0957b409SSimon J. Gerraty 	br_ecdsa_i15_bits2int(m, hash_value, hash_len, n[0]);
106*0957b409SSimon J. Gerraty 	br_i15_sub(m, n, br_i15_sub(m, n, 0) ^ 1);
107*0957b409SSimon J. Gerraty 
108*0957b409SSimon J. Gerraty 	/*
109*0957b409SSimon J. Gerraty 	 * RFC 6979 generation of the "k" value.
110*0957b409SSimon J. Gerraty 	 *
111*0957b409SSimon J. Gerraty 	 * The process uses HMAC_DRBG (with the hash function used to
112*0957b409SSimon J. Gerraty 	 * process the message that is to be signed). The seed is the
113*0957b409SSimon J. Gerraty 	 * concatenation of the encodings of the private key and
114*0957b409SSimon J. Gerraty 	 * the hash value (after truncation and modular reduction).
115*0957b409SSimon J. Gerraty 	 */
116*0957b409SSimon J. Gerraty 	br_i15_encode(tt, nlen, x);
117*0957b409SSimon J. Gerraty 	br_i15_encode(tt + nlen, nlen, m);
118*0957b409SSimon J. Gerraty 	br_hmac_drbg_init(&drbg, hf, tt, nlen << 1);
119*0957b409SSimon J. Gerraty 	for (;;) {
120*0957b409SSimon J. Gerraty 		br_hmac_drbg_generate(&drbg, tt, nlen);
121*0957b409SSimon J. Gerraty 		br_ecdsa_i15_bits2int(k, tt, nlen, n[0]);
122*0957b409SSimon J. Gerraty 		if (br_i15_iszero(k)) {
123*0957b409SSimon J. Gerraty 			continue;
124*0957b409SSimon J. Gerraty 		}
125*0957b409SSimon J. Gerraty 		if (br_i15_sub(k, n, 0)) {
126*0957b409SSimon J. Gerraty 			break;
127*0957b409SSimon J. Gerraty 		}
128*0957b409SSimon J. Gerraty 	}
129*0957b409SSimon J. Gerraty 
130*0957b409SSimon J. Gerraty 	/*
131*0957b409SSimon J. Gerraty 	 * Compute k*G and extract the X coordinate, then reduce it
132*0957b409SSimon J. Gerraty 	 * modulo the curve order. Since we support only curves with
133*0957b409SSimon J. Gerraty 	 * prime order, that reduction is only a matter of computing
134*0957b409SSimon J. Gerraty 	 * a subtraction.
135*0957b409SSimon J. Gerraty 	 */
136*0957b409SSimon J. Gerraty 	br_i15_encode(tt, nlen, k);
137*0957b409SSimon J. Gerraty 	ulen = impl->mulgen(eU, tt, nlen, sk->curve);
138*0957b409SSimon J. Gerraty 	br_i15_zero(r, n[0]);
139*0957b409SSimon J. Gerraty 	br_i15_decode(r, &eU[1], ulen >> 1);
140*0957b409SSimon J. Gerraty 	r[0] = n[0];
141*0957b409SSimon J. Gerraty 	br_i15_sub(r, n, br_i15_sub(r, n, 0) ^ 1);
142*0957b409SSimon J. Gerraty 
143*0957b409SSimon J. Gerraty 	/*
144*0957b409SSimon J. Gerraty 	 * Compute 1/k in double-Montgomery representation. We do so by
145*0957b409SSimon J. Gerraty 	 * first converting _from_ Montgomery representation (twice),
146*0957b409SSimon J. Gerraty 	 * then using a modular exponentiation.
147*0957b409SSimon J. Gerraty 	 */
148*0957b409SSimon J. Gerraty 	br_i15_from_monty(k, n, n0i);
149*0957b409SSimon J. Gerraty 	br_i15_from_monty(k, n, n0i);
150*0957b409SSimon J. Gerraty 	memcpy(tt, cd->order, nlen);
151*0957b409SSimon J. Gerraty 	tt[nlen - 1] -= 2;
152*0957b409SSimon J. Gerraty 	br_i15_modpow(k, tt, nlen, n, n0i, t1, t2);
153*0957b409SSimon J. Gerraty 
154*0957b409SSimon J. Gerraty 	/*
155*0957b409SSimon J. Gerraty 	 * Compute s = (m+xr)/k (mod n).
156*0957b409SSimon J. Gerraty 	 * The k[] array contains R^2/k (double-Montgomery representation);
157*0957b409SSimon J. Gerraty 	 * we thus can use direct Montgomery multiplications and conversions
158*0957b409SSimon J. Gerraty 	 * from Montgomery, avoiding any call to br_i15_to_monty() (which
159*0957b409SSimon J. Gerraty 	 * is slower).
160*0957b409SSimon J. Gerraty 	 */
161*0957b409SSimon J. Gerraty 	br_i15_from_monty(m, n, n0i);
162*0957b409SSimon J. Gerraty 	br_i15_montymul(t1, x, r, n, n0i);
163*0957b409SSimon J. Gerraty 	ctl = br_i15_add(t1, m, 1);
164*0957b409SSimon J. Gerraty 	ctl |= br_i15_sub(t1, n, 0) ^ 1;
165*0957b409SSimon J. Gerraty 	br_i15_sub(t1, n, ctl);
166*0957b409SSimon J. Gerraty 	br_i15_montymul(s, t1, k, n, n0i);
167*0957b409SSimon J. Gerraty 
168*0957b409SSimon J. Gerraty 	/*
169*0957b409SSimon J. Gerraty 	 * Encode r and s in the signature.
170*0957b409SSimon J. Gerraty 	 */
171*0957b409SSimon J. Gerraty 	br_i15_encode(sig, nlen, r);
172*0957b409SSimon J. Gerraty 	br_i15_encode((unsigned char *)sig + nlen, nlen, s);
173*0957b409SSimon J. Gerraty 	return nlen << 1;
174*0957b409SSimon J. Gerraty }
175