1*0957b409SSimon J. Gerraty /*
2*0957b409SSimon J. Gerraty * Copyright (c) 2016 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 U (2 + ((BR_MAX_RSA_FACTOR + 30) / 31))
28*0957b409SSimon J. Gerraty #define TLEN (8 * U)
29*0957b409SSimon J. Gerraty
30*0957b409SSimon J. Gerraty /* see bearssl_rsa.h */
31*0957b409SSimon J. Gerraty uint32_t
br_rsa_i31_private(unsigned char * x,const br_rsa_private_key * sk)32*0957b409SSimon J. Gerraty br_rsa_i31_private(unsigned char *x, const br_rsa_private_key *sk)
33*0957b409SSimon J. Gerraty {
34*0957b409SSimon J. Gerraty const unsigned char *p, *q;
35*0957b409SSimon J. Gerraty size_t plen, qlen;
36*0957b409SSimon J. Gerraty size_t fwlen;
37*0957b409SSimon J. Gerraty uint32_t p0i, q0i;
38*0957b409SSimon J. Gerraty size_t xlen, u;
39*0957b409SSimon J. Gerraty uint32_t tmp[1 + TLEN];
40*0957b409SSimon J. Gerraty long z;
41*0957b409SSimon J. Gerraty uint32_t *mp, *mq, *s1, *s2, *t1, *t2, *t3;
42*0957b409SSimon J. Gerraty uint32_t r;
43*0957b409SSimon J. Gerraty
44*0957b409SSimon J. Gerraty /*
45*0957b409SSimon J. Gerraty * Compute the actual lengths of p and q, in bytes.
46*0957b409SSimon J. Gerraty * These lengths are not considered secret (we cannot really hide
47*0957b409SSimon J. Gerraty * them anyway in constant-time code).
48*0957b409SSimon J. Gerraty */
49*0957b409SSimon J. Gerraty p = sk->p;
50*0957b409SSimon J. Gerraty plen = sk->plen;
51*0957b409SSimon J. Gerraty while (plen > 0 && *p == 0) {
52*0957b409SSimon J. Gerraty p ++;
53*0957b409SSimon J. Gerraty plen --;
54*0957b409SSimon J. Gerraty }
55*0957b409SSimon J. Gerraty q = sk->q;
56*0957b409SSimon J. Gerraty qlen = sk->qlen;
57*0957b409SSimon J. Gerraty while (qlen > 0 && *q == 0) {
58*0957b409SSimon J. Gerraty q ++;
59*0957b409SSimon J. Gerraty qlen --;
60*0957b409SSimon J. Gerraty }
61*0957b409SSimon J. Gerraty
62*0957b409SSimon J. Gerraty /*
63*0957b409SSimon J. Gerraty * Compute the maximum factor length, in words.
64*0957b409SSimon J. Gerraty */
65*0957b409SSimon J. Gerraty z = (long)(plen > qlen ? plen : qlen) << 3;
66*0957b409SSimon J. Gerraty fwlen = 1;
67*0957b409SSimon J. Gerraty while (z > 0) {
68*0957b409SSimon J. Gerraty z -= 31;
69*0957b409SSimon J. Gerraty fwlen ++;
70*0957b409SSimon J. Gerraty }
71*0957b409SSimon J. Gerraty
72*0957b409SSimon J. Gerraty /*
73*0957b409SSimon J. Gerraty * Round up the word length to an even number.
74*0957b409SSimon J. Gerraty */
75*0957b409SSimon J. Gerraty fwlen += (fwlen & 1);
76*0957b409SSimon J. Gerraty
77*0957b409SSimon J. Gerraty /*
78*0957b409SSimon J. Gerraty * We need to fit at least 6 values in the stack buffer.
79*0957b409SSimon J. Gerraty */
80*0957b409SSimon J. Gerraty if (6 * fwlen > TLEN) {
81*0957b409SSimon J. Gerraty return 0;
82*0957b409SSimon J. Gerraty }
83*0957b409SSimon J. Gerraty
84*0957b409SSimon J. Gerraty /*
85*0957b409SSimon J. Gerraty * Compute modulus length (in bytes).
86*0957b409SSimon J. Gerraty */
87*0957b409SSimon J. Gerraty xlen = (sk->n_bitlen + 7) >> 3;
88*0957b409SSimon J. Gerraty
89*0957b409SSimon J. Gerraty /*
90*0957b409SSimon J. Gerraty * Decode q.
91*0957b409SSimon J. Gerraty */
92*0957b409SSimon J. Gerraty mq = tmp;
93*0957b409SSimon J. Gerraty br_i31_decode(mq, q, qlen);
94*0957b409SSimon J. Gerraty
95*0957b409SSimon J. Gerraty /*
96*0957b409SSimon J. Gerraty * Decode p.
97*0957b409SSimon J. Gerraty */
98*0957b409SSimon J. Gerraty t1 = mq + fwlen;
99*0957b409SSimon J. Gerraty br_i31_decode(t1, p, plen);
100*0957b409SSimon J. Gerraty
101*0957b409SSimon J. Gerraty /*
102*0957b409SSimon J. Gerraty * Compute the modulus (product of the two factors), to compare
103*0957b409SSimon J. Gerraty * it with the source value. We use br_i31_mulacc(), since it's
104*0957b409SSimon J. Gerraty * already used later on.
105*0957b409SSimon J. Gerraty */
106*0957b409SSimon J. Gerraty t2 = mq + 2 * fwlen;
107*0957b409SSimon J. Gerraty br_i31_zero(t2, mq[0]);
108*0957b409SSimon J. Gerraty br_i31_mulacc(t2, mq, t1);
109*0957b409SSimon J. Gerraty
110*0957b409SSimon J. Gerraty /*
111*0957b409SSimon J. Gerraty * We encode the modulus into bytes, to perform the comparison
112*0957b409SSimon J. Gerraty * with bytes. We know that the product length, in bytes, is
113*0957b409SSimon J. Gerraty * exactly xlen.
114*0957b409SSimon J. Gerraty * The comparison actually computes the carry when subtracting
115*0957b409SSimon J. Gerraty * the modulus from the source value; that carry must be 1 for
116*0957b409SSimon J. Gerraty * a value in the correct range. We keep it in r, which is our
117*0957b409SSimon J. Gerraty * accumulator for the error code.
118*0957b409SSimon J. Gerraty */
119*0957b409SSimon J. Gerraty t3 = mq + 4 * fwlen;
120*0957b409SSimon J. Gerraty br_i31_encode(t3, xlen, t2);
121*0957b409SSimon J. Gerraty u = xlen;
122*0957b409SSimon J. Gerraty r = 0;
123*0957b409SSimon J. Gerraty while (u > 0) {
124*0957b409SSimon J. Gerraty uint32_t wn, wx;
125*0957b409SSimon J. Gerraty
126*0957b409SSimon J. Gerraty u --;
127*0957b409SSimon J. Gerraty wn = ((unsigned char *)t3)[u];
128*0957b409SSimon J. Gerraty wx = x[u];
129*0957b409SSimon J. Gerraty r = ((wx - (wn + r)) >> 8) & 1;
130*0957b409SSimon J. Gerraty }
131*0957b409SSimon J. Gerraty
132*0957b409SSimon J. Gerraty /*
133*0957b409SSimon J. Gerraty * Move the decoded p to another temporary buffer.
134*0957b409SSimon J. Gerraty */
135*0957b409SSimon J. Gerraty mp = mq + 2 * fwlen;
136*0957b409SSimon J. Gerraty memmove(mp, t1, fwlen * sizeof *t1);
137*0957b409SSimon J. Gerraty
138*0957b409SSimon J. Gerraty /*
139*0957b409SSimon J. Gerraty * Compute s2 = x^dq mod q.
140*0957b409SSimon J. Gerraty */
141*0957b409SSimon J. Gerraty q0i = br_i31_ninv31(mq[1]);
142*0957b409SSimon J. Gerraty s2 = mq + fwlen;
143*0957b409SSimon J. Gerraty br_i31_decode_reduce(s2, x, xlen, mq);
144*0957b409SSimon J. Gerraty r &= br_i31_modpow_opt(s2, sk->dq, sk->dqlen, mq, q0i,
145*0957b409SSimon J. Gerraty mq + 3 * fwlen, TLEN - 3 * fwlen);
146*0957b409SSimon J. Gerraty
147*0957b409SSimon J. Gerraty /*
148*0957b409SSimon J. Gerraty * Compute s1 = x^dp mod p.
149*0957b409SSimon J. Gerraty */
150*0957b409SSimon J. Gerraty p0i = br_i31_ninv31(mp[1]);
151*0957b409SSimon J. Gerraty s1 = mq + 3 * fwlen;
152*0957b409SSimon J. Gerraty br_i31_decode_reduce(s1, x, xlen, mp);
153*0957b409SSimon J. Gerraty r &= br_i31_modpow_opt(s1, sk->dp, sk->dplen, mp, p0i,
154*0957b409SSimon J. Gerraty mq + 4 * fwlen, TLEN - 4 * fwlen);
155*0957b409SSimon J. Gerraty
156*0957b409SSimon J. Gerraty /*
157*0957b409SSimon J. Gerraty * Compute:
158*0957b409SSimon J. Gerraty * h = (s1 - s2)*(1/q) mod p
159*0957b409SSimon J. Gerraty * s1 is an integer modulo p, but s2 is modulo q. PKCS#1 is
160*0957b409SSimon J. Gerraty * unclear about whether p may be lower than q (some existing,
161*0957b409SSimon J. Gerraty * widely deployed implementations of RSA don't tolerate p < q),
162*0957b409SSimon J. Gerraty * but we want to support that occurrence, so we need to use the
163*0957b409SSimon J. Gerraty * reduction function.
164*0957b409SSimon J. Gerraty *
165*0957b409SSimon J. Gerraty * Since we use br_i31_decode_reduce() for iq (purportedly, the
166*0957b409SSimon J. Gerraty * inverse of q modulo p), we also tolerate improperly large
167*0957b409SSimon J. Gerraty * values for this parameter.
168*0957b409SSimon J. Gerraty */
169*0957b409SSimon J. Gerraty t1 = mq + 4 * fwlen;
170*0957b409SSimon J. Gerraty t2 = mq + 5 * fwlen;
171*0957b409SSimon J. Gerraty br_i31_reduce(t2, s2, mp);
172*0957b409SSimon J. Gerraty br_i31_add(s1, mp, br_i31_sub(s1, t2, 1));
173*0957b409SSimon J. Gerraty br_i31_to_monty(s1, mp);
174*0957b409SSimon J. Gerraty br_i31_decode_reduce(t1, sk->iq, sk->iqlen, mp);
175*0957b409SSimon J. Gerraty br_i31_montymul(t2, s1, t1, mp, p0i);
176*0957b409SSimon J. Gerraty
177*0957b409SSimon J. Gerraty /*
178*0957b409SSimon J. Gerraty * h is now in t2. We compute the final result:
179*0957b409SSimon J. Gerraty * s = s2 + q*h
180*0957b409SSimon J. Gerraty * All these operations are non-modular.
181*0957b409SSimon J. Gerraty *
182*0957b409SSimon J. Gerraty * We need mq, s2 and t2. We use the t3 buffer as destination.
183*0957b409SSimon J. Gerraty * The buffers mp, s1 and t1 are no longer needed, so we can
184*0957b409SSimon J. Gerraty * reuse them for t3. Moreover, the first step of the computation
185*0957b409SSimon J. Gerraty * is to copy s2 into t3, after which s2 is not needed. Right
186*0957b409SSimon J. Gerraty * now, mq is in slot 0, s2 is in slot 1, and t2 is in slot 5.
187*0957b409SSimon J. Gerraty * Therefore, we have ample room for t3 by simply using s2.
188*0957b409SSimon J. Gerraty */
189*0957b409SSimon J. Gerraty t3 = s2;
190*0957b409SSimon J. Gerraty br_i31_mulacc(t3, mq, t2);
191*0957b409SSimon J. Gerraty
192*0957b409SSimon J. Gerraty /*
193*0957b409SSimon J. Gerraty * Encode the result. Since we already checked the value of xlen,
194*0957b409SSimon J. Gerraty * we can just use it right away.
195*0957b409SSimon J. Gerraty */
196*0957b409SSimon J. Gerraty br_i31_encode(x, xlen, t3);
197*0957b409SSimon J. Gerraty
198*0957b409SSimon J. Gerraty /*
199*0957b409SSimon J. Gerraty * The only error conditions remaining at that point are invalid
200*0957b409SSimon J. Gerraty * values for p and q (even integers).
201*0957b409SSimon J. Gerraty */
202*0957b409SSimon J. Gerraty return p0i & q0i & r;
203*0957b409SSimon J. Gerraty }
204