2 * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 /* see bearssl_rsa.h */
29 br_rsa_i15_compute_modulus(void *n, const br_rsa_private_key *sk)
31 uint16_t tmp[4 * (((BR_MAX_RSA_SIZE / 2) + 14) / 15) + 5];
33 const unsigned char *pbuf, *qbuf;
34 size_t nlen, plen, qlen, tlen;
37 * Compute actual byte and lengths for p and q.
41 while (plen > 0 && *pbuf == 0) {
47 while (qlen > 0 && *qbuf == 0) {
53 tlen = (sizeof tmp) / (sizeof tmp[0]);
58 if ((15 * tlen) < (plen << 3) + 15) {
61 br_i15_decode(t, pbuf, plen);
63 plen = (p[0] + 31) >> 4;
70 if ((15 * tlen) < (qlen << 3) + 15) {
73 br_i15_decode(t, qbuf, qlen);
75 qlen = (q[0] + 31) >> 4;
80 * Computation can proceed only if we have enough room for the
83 if (tlen < (plen + qlen + 1)) {
88 * Private key already contains the modulus bit length, from which
89 * we can infer the output length. Even if n is NULL, we still had
90 * to decode p and q to make sure that the product can be computed.
92 nlen = (sk->n_bitlen + 7) >> 3;
95 br_i15_mulacc(t, p, q);
96 br_i15_encode(n, nlen, t);