2 * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 #include "internal/cryptlib.h"
16 int BN_mod_exp2_mont(BIGNUM *rr, const BIGNUM *a1, const BIGNUM *p1,
17 const BIGNUM *a2, const BIGNUM *p2, const BIGNUM *m,
18 BN_CTX *ctx, BN_MONT_CTX *in_mont)
20 int i, j, bits, b, bits1, bits2, ret =
21 0, wpos1, wpos2, window1, window2, wvalue1, wvalue2;
24 const BIGNUM *a_mod_m;
25 /* Tables of variables obtained from 'ctx' */
26 BIGNUM *val1[TABLE_SIZE], *val2[TABLE_SIZE];
27 BN_MONT_CTX *mont = NULL;
36 BNerr(BN_F_BN_MOD_EXP2_MONT, BN_R_CALLED_WITH_EVEN_MODULUS);
39 bits1 = BN_num_bits(p1);
40 bits2 = BN_num_bits(p2);
41 if ((bits1 == 0) && (bits2 == 0)) {
46 bits = (bits1 > bits2) ? bits1 : bits2;
51 val1[0] = BN_CTX_get(ctx);
52 val2[0] = BN_CTX_get(ctx);
59 if ((mont = BN_MONT_CTX_new()) == NULL)
61 if (!BN_MONT_CTX_set(mont, m, ctx))
65 window1 = BN_window_bits_for_exponent_size(bits1);
66 window2 = BN_window_bits_for_exponent_size(bits2);
69 * Build table for a1: val1[i] := a1^(2*i + 1) mod m for i = 0 .. 2^(window1-1)
71 if (a1->neg || BN_ucmp(a1, m) >= 0) {
72 if (!BN_mod(val1[0], a1, m, ctx))
77 if (BN_is_zero(a_mod_m)) {
83 if (!BN_to_montgomery(val1[0], a_mod_m, mont, ctx))
86 if (!BN_mod_mul_montgomery(d, val1[0], val1[0], mont, ctx))
89 j = 1 << (window1 - 1);
90 for (i = 1; i < j; i++) {
91 if (((val1[i] = BN_CTX_get(ctx)) == NULL) ||
92 !BN_mod_mul_montgomery(val1[i], val1[i - 1], d, mont, ctx))
98 * Build table for a2: val2[i] := a2^(2*i + 1) mod m for i = 0 .. 2^(window2-1)
100 if (a2->neg || BN_ucmp(a2, m) >= 0) {
101 if (!BN_mod(val2[0], a2, m, ctx))
106 if (BN_is_zero(a_mod_m)) {
111 if (!BN_to_montgomery(val2[0], a_mod_m, mont, ctx))
114 if (!BN_mod_mul_montgomery(d, val2[0], val2[0], mont, ctx))
117 j = 1 << (window2 - 1);
118 for (i = 1; i < j; i++) {
119 if (((val2[i] = BN_CTX_get(ctx)) == NULL) ||
120 !BN_mod_mul_montgomery(val2[i], val2[i - 1], d, mont, ctx))
125 /* Now compute the power product, using independent windows. */
127 wvalue1 = 0; /* The 'value' of the first window */
128 wvalue2 = 0; /* The 'value' of the second window */
129 wpos1 = 0; /* If wvalue1 > 0, the bottom bit of the
131 wpos2 = 0; /* If wvalue2 > 0, the bottom bit of the
134 if (!BN_to_montgomery(r, BN_value_one(), mont, ctx))
136 for (b = bits - 1; b >= 0; b--) {
138 if (!BN_mod_mul_montgomery(r, r, r, mont, ctx))
143 if (BN_is_bit_set(p1, b)) {
145 * consider bits b-window1+1 .. b for this window
148 while (!BN_is_bit_set(p1, i)) /* works for i<0 */
152 for (i = b - 1; i >= wpos1; i--) {
154 if (BN_is_bit_set(p1, i))
160 if (BN_is_bit_set(p2, b)) {
162 * consider bits b-window2+1 .. b for this window
165 while (!BN_is_bit_set(p2, i))
169 for (i = b - 1; i >= wpos2; i--) {
171 if (BN_is_bit_set(p2, i))
176 if (wvalue1 && b == wpos1) {
177 /* wvalue1 is odd and < 2^window1 */
178 if (!BN_mod_mul_montgomery(r, r, val1[wvalue1 >> 1], mont, ctx))
184 if (wvalue2 && b == wpos2) {
185 /* wvalue2 is odd and < 2^window2 */
186 if (!BN_mod_mul_montgomery(r, r, val2[wvalue2 >> 1], mont, ctx))
192 if (!BN_from_montgomery(rr, r, mont, ctx))
197 BN_MONT_CTX_free(mont);