Path: blob/main/contrib/bearssl/src/rsa/rsa_i32_priv.c
39488 views
/*1* Copyright (c) 2016 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#define U (1 + (BR_MAX_RSA_FACTOR >> 5))2728/* see bearssl_rsa.h */29uint32_t30br_rsa_i32_private(unsigned char *x, const br_rsa_private_key *sk)31{32const unsigned char *p, *q;33size_t plen, qlen;34uint32_t tmp[6 * U];35uint32_t *mp, *mq, *s1, *s2, *t1, *t2, *t3;36uint32_t p0i, q0i;37size_t xlen, u;38uint32_t r;3940/*41* All our temporary buffers are from the tmp[] array.42*43* The mp, mq, s1, s2, t1 and t2 buffers are large enough to44* contain a RSA factor. The t3 buffer can contain a complete45* RSA modulus. t3 shares its storage space with s2, s1 and t1,46* in that order (this is important, see below).47*/48mq = tmp;49mp = tmp + U;50t2 = tmp + 2 * U;51s2 = tmp + 3 * U;52s1 = tmp + 4 * U;53t1 = tmp + 5 * U;54t3 = s2;5556/*57* Compute the actual lengths (in bytes) of p and q, and check58* that they fit within our stack buffers.59*/60p = sk->p;61plen = sk->plen;62while (plen > 0 && *p == 0) {63p ++;64plen --;65}66q = sk->q;67qlen = sk->qlen;68while (qlen > 0 && *q == 0) {69q ++;70qlen --;71}72if (plen > (BR_MAX_RSA_FACTOR >> 3)73|| qlen > (BR_MAX_RSA_FACTOR >> 3))74{75return 0;76}7778/*79* Decode p and q.80*/81br_i32_decode(mp, p, plen);82br_i32_decode(mq, q, qlen);8384/*85* Recompute modulus, to compare with the source value.86*/87br_i32_zero(t2, mp[0]);88br_i32_mulacc(t2, mp, mq);89xlen = (sk->n_bitlen + 7) >> 3;90br_i32_encode(t2 + 2 * U, xlen, t2);91u = xlen;92r = 0;93while (u > 0) {94uint32_t wn, wx;9596u --;97wn = ((unsigned char *)(t2 + 2 * U))[u];98wx = x[u];99r = ((wx - (wn + r)) >> 8) & 1;100}101102/*103* Compute s1 = x^dp mod p.104*/105p0i = br_i32_ninv32(mp[1]);106br_i32_decode_reduce(s1, x, xlen, mp);107br_i32_modpow(s1, sk->dp, sk->dplen, mp, p0i, t1, t2);108109/*110* Compute s2 = x^dq mod q.111*/112q0i = br_i32_ninv32(mq[1]);113br_i32_decode_reduce(s2, x, xlen, mq);114br_i32_modpow(s2, sk->dq, sk->dqlen, mq, q0i, t1, t2);115116/*117* Compute:118* h = (s1 - s2)*(1/q) mod p119* s1 is an integer modulo p, but s2 is modulo q. PKCS#1 is120* unclear about whether p may be lower than q (some existing,121* widely deployed implementations of RSA don't tolerate p < q),122* but we want to support that occurrence, so we need to use the123* reduction function.124*125* Since we use br_i32_decode_reduce() for iq (purportedly, the126* inverse of q modulo p), we also tolerate improperly large127* values for this parameter.128*/129br_i32_reduce(t2, s2, mp);130br_i32_add(s1, mp, br_i32_sub(s1, t2, 1));131br_i32_to_monty(s1, mp);132br_i32_decode_reduce(t1, sk->iq, sk->iqlen, mp);133br_i32_montymul(t2, s1, t1, mp, p0i);134135/*136* h is now in t2. We compute the final result:137* s = s2 + q*h138* All these operations are non-modular.139*140* We need mq, s2 and t2. We use the t3 buffer as destination.141* The buffers mp, s1 and t1 are no longer needed. Moreover,142* the first step is to copy s2 into the destination buffer t3.143* We thus arranged for t3 to actually share space with s2, and144* to be followed by the space formerly used by s1 and t1.145*/146br_i32_mulacc(t3, mq, t2);147148/*149* Encode the result. Since we already checked the value of xlen,150* we can just use it right away.151*/152br_i32_encode(x, xlen, t3);153154/*155* The only error conditions remaining at that point are invalid156* values for p and q (even integers).157*/158return p0i & q0i & r;159}160161162