Path: blob/main/contrib/bearssl/src/rsa/rsa_i15_pubexp.c
39483 views
/*1* Copyright (c) 2018 Thomas Pornin <[email protected]>2*3* Permission is hereby granted, free of charge, to any person obtaining4* a copy of this software and associated documentation files (the5* "Software"), to deal in the Software without restriction, including6* without limitation the rights to use, copy, modify, merge, publish,7* distribute, sublicense, and/or sell copies of the Software, and to8* permit persons to whom the Software is furnished to do so, subject to9* the following conditions:10*11* The above copyright notice and this permission notice shall be12* included in all copies or substantial portions of the Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,15* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF16* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND17* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS18* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN19* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN20* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE21* SOFTWARE.22*/2324#include "inner.h"2526/*27* Recompute public exponent, based on factor p and reduced private28* exponent dp.29*/30static uint32_t31get_pubexp(const unsigned char *pbuf, size_t plen,32const unsigned char *dpbuf, size_t dplen)33{34/*35* dp is the inverse of e modulo p-1. If p = 3 mod 4, then36* p-1 = 2*((p-1)/2). Taken modulo 2, e is odd and has inverse 1;37* thus, dp must be odd.38*39* We compute the inverse of dp modulo (p-1)/2. This requires40* first reducing dp modulo (p-1)/2 (this can be done with a41* conditional subtract, no need to use the generic modular42* reduction function); then, we use moddiv.43*/4445uint16_t tmp[6 * ((BR_MAX_RSA_FACTOR + 29) / 15)];46uint16_t *p, *dp, *x;47size_t len;48uint32_t e;4950/*51* Compute actual factor length (in bytes) and check that it fits52* under our size constraints.53*/54while (plen > 0 && *pbuf == 0) {55pbuf ++;56plen --;57}58if (plen == 0 || plen < 5 || plen > (BR_MAX_RSA_FACTOR / 8)) {59return 0;60}6162/*63* Compute actual reduced exponent length (in bytes) and check that64* it is not longer than p.65*/66while (dplen > 0 && *dpbuf == 0) {67dpbuf ++;68dplen --;69}70if (dplen > plen || dplen == 071|| (dplen == plen && dpbuf[0] > pbuf[0]))72{73return 0;74}7576/*77* Verify that p = 3 mod 4 and that dp is odd.78*/79if ((pbuf[plen - 1] & 3) != 3 || (dpbuf[dplen - 1] & 1) != 1) {80return 0;81}8283/*84* Decode p and compute (p-1)/2.85*/86p = tmp;87br_i15_decode(p, pbuf, plen);88len = (p[0] + 31) >> 4;89br_i15_rshift(p, 1);9091/*92* Decode dp and make sure its announced bit length matches that of93* p (we already know that the size of dp, in bits, does not exceed94* the size of p, so we just have to copy the header word).95*/96dp = p + len;97memset(dp, 0, len * sizeof *dp);98br_i15_decode(dp, dpbuf, dplen);99dp[0] = p[0];100101/*102* Subtract (p-1)/2 from dp if necessary.103*/104br_i15_sub(dp, p, NOT(br_i15_sub(dp, p, 0)));105106/*107* If another subtraction is needed, then this means that the108* value was invalid. We don't care to leak information about109* invalid keys.110*/111if (br_i15_sub(dp, p, 0) == 0) {112return 0;113}114115/*116* Invert dp modulo (p-1)/2. If the inversion fails, then the117* key value was invalid.118*/119x = dp + len;120br_i15_zero(x, p[0]);121x[1] = 1;122if (br_i15_moddiv(x, dp, p, br_i15_ninv15(p[1]), x + len) == 0) {123return 0;124}125126/*127* We now have an inverse. We must set it to zero (error) if its128* length is greater than 32 bits and/or if it is an even integer.129* Take care that the bit_length function returns an encoded130* bit length.131*/132e = (uint32_t)x[1] | ((uint32_t)x[2] << 15) | ((uint32_t)x[3] << 30);133e &= -LT(br_i15_bit_length(x + 1, len - 1), 35);134e &= -(e & 1);135return e;136}137138/* see bearssl_rsa.h */139uint32_t140br_rsa_i15_compute_pubexp(const br_rsa_private_key *sk)141{142/*143* Get the public exponent from both p and q. This is the right144* exponent if we get twice the same value.145*/146uint32_t ep, eq;147148ep = get_pubexp(sk->p, sk->plen, sk->dp, sk->dplen);149eq = get_pubexp(sk->q, sk->qlen, sk->dq, sk->dqlen);150return ep & -EQ(ep, eq);151}152153154