Added generic HKDF implementation.
[BearSSL] / src / rsa / rsa_i31_priv.c
index cb2858b..b1e1244 100644 (file)
@@ -35,7 +35,7 @@ br_rsa_i31_private(unsigned char *x, const br_rsa_private_key *sk)
        size_t plen, qlen;
        size_t fwlen;
        uint32_t p0i, q0i;
        size_t plen, qlen;
        size_t fwlen;
        uint32_t p0i, q0i;
-       size_t xlen;
+       size_t xlen, u;
        uint32_t tmp[1 + TLEN];
        long z;
        uint32_t *mp, *mq, *s1, *s2, *t1, *t2, *t3;
        uint32_t tmp[1 + TLEN];
        long z;
        uint32_t *mp, *mq, *s1, *s2, *t1, *t2, *t3;
@@ -82,7 +82,7 @@ br_rsa_i31_private(unsigned char *x, const br_rsa_private_key *sk)
        }
 
        /*
        }
 
        /*
-        * Compute signature length (in bytes).
+        * Compute modulus length (in bytes).
         */
        xlen = (sk->n_bitlen + 7) >> 3;
 
         */
        xlen = (sk->n_bitlen + 7) >> 3;
 
@@ -93,19 +93,56 @@ br_rsa_i31_private(unsigned char *x, const br_rsa_private_key *sk)
        br_i31_decode(mq, q, qlen);
 
        /*
        br_i31_decode(mq, q, qlen);
 
        /*
-        * Compute s2 = x^dq mod q.
+        * Decode p.
         */
         */
-       q0i = br_i31_ninv31(mq[1]);
-       s2 = mq + fwlen;
-       br_i31_decode_reduce(s2, x, xlen, mq);
-       r = br_i31_modpow_opt(s2, sk->dq, sk->dqlen, mq, q0i,
-               mq + 2 * fwlen, TLEN - 2 * fwlen);
+       t1 = mq + fwlen;
+       br_i31_decode(t1, p, plen);
 
        /*
 
        /*
-        * Decode p.
+        * Compute the modulus (product of the two factors), to compare
+        * it with the source value. We use br_i31_mulacc(), since it's
+        * already used later on.
+        */
+       t2 = mq + 2 * fwlen;
+       br_i31_zero(t2, mq[0]);
+       br_i31_mulacc(t2, mq, t1);
+
+       /*
+        * We encode the modulus into bytes, to perform the comparison
+        * with bytes. We know that the product length, in bytes, is
+        * exactly xlen.
+        * The comparison actually computes the carry when subtracting
+        * the modulus from the source value; that carry must be 1 for
+        * a value in the correct range. We keep it in r, which is our
+        * accumulator for the error code.
+        */
+       t3 = mq + 4 * fwlen;
+       br_i31_encode(t3, xlen, t2);
+       u = xlen;
+       r = 0;
+       while (u > 0) {
+               uint32_t wn, wx;
+
+               u --;
+               wn = ((unsigned char *)t3)[u];
+               wx = x[u];
+               r = ((wx - (wn + r)) >> 8) & 1;
+       }
+
+       /*
+        * Move the decoded p to another temporary buffer.
         */
        mp = mq + 2 * fwlen;
         */
        mp = mq + 2 * fwlen;
-       br_i31_decode(mp, p, plen);
+       memmove(mp, t1, fwlen * sizeof *t1);
+
+       /*
+        * Compute s2 = x^dq mod q.
+        */
+       q0i = br_i31_ninv31(mq[1]);
+       s2 = mq + fwlen;
+       br_i31_decode_reduce(s2, x, xlen, mq);
+       r &= br_i31_modpow_opt(s2, sk->dq, sk->dqlen, mq, q0i,
+               mq + 3 * fwlen, TLEN - 3 * fwlen);
 
        /*
         * Compute s1 = x^dp mod p.
 
        /*
         * Compute s1 = x^dp mod p.