xref: /freebsd/contrib/bearssl/src/rsa/rsa_i15_modulus.c (revision 0957b409a90fd597c1e9124cbaf3edd2b488f4ac)
1*0957b409SSimon J. Gerraty /*
2*0957b409SSimon J. Gerraty  * Copyright (c) 2018 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 /* see bearssl_rsa.h */
28*0957b409SSimon J. Gerraty size_t
29*0957b409SSimon J. Gerraty br_rsa_i15_compute_modulus(void *n, const br_rsa_private_key *sk)
30*0957b409SSimon J. Gerraty {
31*0957b409SSimon J. Gerraty 	uint16_t tmp[2 * ((BR_MAX_RSA_SIZE + 14) / 15) + 5];
32*0957b409SSimon J. Gerraty 	uint16_t *t, *p, *q;
33*0957b409SSimon J. Gerraty 	const unsigned char *pbuf, *qbuf;
34*0957b409SSimon J. Gerraty 	size_t nlen, plen, qlen, tlen;
35*0957b409SSimon J. Gerraty 
36*0957b409SSimon J. Gerraty 	/*
37*0957b409SSimon J. Gerraty 	 * Compute actual byte and lengths for p and q.
38*0957b409SSimon J. Gerraty 	 */
39*0957b409SSimon J. Gerraty 	pbuf = sk->p;
40*0957b409SSimon J. Gerraty 	plen = sk->plen;
41*0957b409SSimon J. Gerraty 	while (plen > 0 && *pbuf == 0) {
42*0957b409SSimon J. Gerraty 		pbuf ++;
43*0957b409SSimon J. Gerraty 		plen --;
44*0957b409SSimon J. Gerraty 	}
45*0957b409SSimon J. Gerraty 	qbuf = sk->q;
46*0957b409SSimon J. Gerraty 	qlen = sk->qlen;
47*0957b409SSimon J. Gerraty 	while (qlen > 0 && *qbuf == 0) {
48*0957b409SSimon J. Gerraty 		qbuf ++;
49*0957b409SSimon J. Gerraty 		qlen --;
50*0957b409SSimon J. Gerraty 	}
51*0957b409SSimon J. Gerraty 
52*0957b409SSimon J. Gerraty 	t = tmp;
53*0957b409SSimon J. Gerraty 	tlen = (sizeof tmp) / (sizeof tmp[0]);
54*0957b409SSimon J. Gerraty 
55*0957b409SSimon J. Gerraty 	/*
56*0957b409SSimon J. Gerraty 	 * Decode p.
57*0957b409SSimon J. Gerraty 	 */
58*0957b409SSimon J. Gerraty 	if ((15 * tlen) < (plen << 3) + 15) {
59*0957b409SSimon J. Gerraty 		return 0;
60*0957b409SSimon J. Gerraty 	}
61*0957b409SSimon J. Gerraty 	br_i15_decode(t, pbuf, plen);
62*0957b409SSimon J. Gerraty 	p = t;
63*0957b409SSimon J. Gerraty 	plen = (p[0] + 31) >> 4;
64*0957b409SSimon J. Gerraty 	t += plen;
65*0957b409SSimon J. Gerraty 	tlen -= plen;
66*0957b409SSimon J. Gerraty 
67*0957b409SSimon J. Gerraty 	/*
68*0957b409SSimon J. Gerraty 	 * Decode q.
69*0957b409SSimon J. Gerraty 	 */
70*0957b409SSimon J. Gerraty 	if ((15 * tlen) < (qlen << 3) + 15) {
71*0957b409SSimon J. Gerraty 		return 0;
72*0957b409SSimon J. Gerraty 	}
73*0957b409SSimon J. Gerraty 	br_i15_decode(t, qbuf, qlen);
74*0957b409SSimon J. Gerraty 	q = t;
75*0957b409SSimon J. Gerraty 	qlen = (q[0] + 31) >> 4;
76*0957b409SSimon J. Gerraty 	t += qlen;
77*0957b409SSimon J. Gerraty 	tlen -= qlen;
78*0957b409SSimon J. Gerraty 
79*0957b409SSimon J. Gerraty 	/*
80*0957b409SSimon J. Gerraty 	 * Computation can proceed only if we have enough room for the
81*0957b409SSimon J. Gerraty 	 * modulus.
82*0957b409SSimon J. Gerraty 	 */
83*0957b409SSimon J. Gerraty 	if (tlen < (plen + qlen + 1)) {
84*0957b409SSimon J. Gerraty 		return 0;
85*0957b409SSimon J. Gerraty 	}
86*0957b409SSimon J. Gerraty 
87*0957b409SSimon J. Gerraty 	/*
88*0957b409SSimon J. Gerraty 	 * Private key already contains the modulus bit length, from which
89*0957b409SSimon J. Gerraty 	 * we can infer the output length. Even if n is NULL, we still had
90*0957b409SSimon J. Gerraty 	 * to decode p and q to make sure that the product can be computed.
91*0957b409SSimon J. Gerraty 	 */
92*0957b409SSimon J. Gerraty 	nlen = (sk->n_bitlen + 7) >> 3;
93*0957b409SSimon J. Gerraty 	if (n != NULL) {
94*0957b409SSimon J. Gerraty 		br_i15_zero(t, p[0]);
95*0957b409SSimon J. Gerraty 		br_i15_mulacc(t, p, q);
96*0957b409SSimon J. Gerraty 		br_i15_encode(n, nlen, t);
97*0957b409SSimon J. Gerraty 	}
98*0957b409SSimon J. Gerraty 	return nlen;
99*0957b409SSimon J. Gerraty }
100