1 /* ********************************************************************* 2 * 3 * Sun elects to have this file available under and governed by the 4 * Mozilla Public License Version 1.1 ("MPL") (see 5 * http://www.mozilla.org/MPL/ for full license text). For the avoidance 6 * of doubt and subject to the following, Sun also elects to allow 7 * licensees to use this file under the MPL, the GNU General Public 8 * License version 2 only or the Lesser General Public License version 9 * 2.1 only. Any references to the "GNU General Public License version 2 10 * or later" or "GPL" in the following shall be construed to mean the 11 * GNU General Public License version 2 only. Any references to the "GNU 12 * Lesser General Public License version 2.1 or later" or "LGPL" in the 13 * following shall be construed to mean the GNU Lesser General Public 14 * License version 2.1 only. However, the following notice accompanied 15 * the original version of this file: 16 * 17 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 18 * 19 * The contents of this file are subject to the Mozilla Public License Version 20 * 1.1 (the "License"); you may not use this file except in compliance with 21 * the License. You may obtain a copy of the License at 22 * http://www.mozilla.org/MPL/ 23 * 24 * Software distributed under the License is distributed on an "AS IS" basis, 25 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 26 * for the specific language governing rights and limitations under the 27 * License. 28 * 29 * The Original Code is the Netscape security libraries. 30 * 31 * The Initial Developer of the Original Code is 32 * Netscape Communications Corporation. 33 * Portions created by the Initial Developer are Copyright (C) 2000 34 * the Initial Developer. All Rights Reserved. 35 * 36 * Contributor(s): 37 * Sheueling Chang Shantz <sheueling.chang@sun.com>, 38 * Stephen Fung <stephen.fung@sun.com>, and 39 * Douglas Stebila <douglas@stebila.ca> of Sun Laboratories. 40 * 41 * Alternatively, the contents of this file may be used under the terms of 42 * either the GNU General Public License Version 2 or later (the "GPL"), or 43 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), 44 * in which case the provisions of the GPL or the LGPL are applicable instead 45 * of those above. If you wish to allow use of your version of this file only 46 * under the terms of either the GPL or the LGPL, and not to allow others to 47 * use your version of this file under the terms of the MPL, indicate your 48 * decision by deleting the provisions above and replace them with the notice 49 * and other provisions required by the GPL or the LGPL. If you do not delete 50 * the provisions above, a recipient may use your version of this file under 51 * the terms of any one of the MPL, the GPL or the LGPL. 52 * 53 *********************************************************************** */ 54 /* 55 * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved. 56 * Use is subject to license terms. 57 */ 58 59 /* $Id: mpmontg.c,v 1.20 2006/08/29 02:41:38 nelson%bolyard.com Exp $ */ 60 61 /* This file implements moduluar exponentiation using Montgomery's 62 * method for modular reduction. This file implements the method 63 * described as "Improvement 1" in the paper "A Cryptogrpahic Library for 64 * the Motorola DSP56000" by Stephen R. Dusse' and Burton S. Kaliski Jr. 65 * published in "Advances in Cryptology: Proceedings of EUROCRYPT '90" 66 * "Lecture Notes in Computer Science" volume 473, 1991, pg 230-244, 67 * published by Springer Verlag. 68 */ 69 70 #define MP_USING_CACHE_SAFE_MOD_EXP 1 71 #ifndef _KERNEL 72 #include <string.h> 73 #include <stddef.h> /* ptrdiff_t */ 74 #endif 75 #include "mpi-priv.h" 76 #include "mplogic.h" 77 #include "mpprime.h" 78 #ifdef MP_USING_MONT_MULF 79 #include "montmulf.h" 80 #endif 81 82 /* if MP_CHAR_STORE_SLOW is defined, we */ 83 /* need to know endianness of this platform. */ 84 #ifdef MP_CHAR_STORE_SLOW 85 #if !defined(MP_IS_BIG_ENDIAN) && !defined(MP_IS_LITTLE_ENDIAN) 86 #error "You must define MP_IS_BIG_ENDIAN or MP_IS_LITTLE_ENDIAN\n" \ 87 " if you define MP_CHAR_STORE_SLOW." 88 #endif 89 #endif 90 91 #ifndef STATIC 92 #define STATIC 93 #endif 94 95 #define MAX_ODD_INTS 32 /* 2 ** (WINDOW_BITS - 1) */ 96 97 #ifndef _KERNEL 98 #if defined(_WIN32_WCE) 99 #define ABORT res = MP_UNDEF; goto CLEANUP 100 #else 101 #define ABORT abort() 102 #endif 103 #else 104 #define ABORT res = MP_UNDEF; goto CLEANUP 105 #endif /* _KERNEL */ 106 107 /* computes T = REDC(T), 2^b == R */ 108 mp_err s_mp_redc(mp_int *T, mp_mont_modulus *mmm) 109 { 110 mp_err res; 111 mp_size i; 112 113 i = MP_USED(T) + MP_USED(&mmm->N) + 2; 114 MP_CHECKOK( s_mp_pad(T, i) ); 115 for (i = 0; i < MP_USED(&mmm->N); ++i ) { 116 mp_digit m_i = MP_DIGIT(T, i) * mmm->n0prime; 117 /* T += N * m_i * (MP_RADIX ** i); */ 118 MP_CHECKOK( s_mp_mul_d_add_offset(&mmm->N, m_i, T, i) ); 119 } 120 s_mp_clamp(T); 121 122 /* T /= R */ 123 s_mp_div_2d(T, mmm->b); 124 125 if ((res = s_mp_cmp(T, &mmm->N)) >= 0) { 126 /* T = T - N */ 127 MP_CHECKOK( s_mp_sub(T, &mmm->N) ); 128 #ifdef DEBUG 129 if ((res = mp_cmp(T, &mmm->N)) >= 0) { 130 res = MP_UNDEF; 131 goto CLEANUP; 132 } 133 #endif 134 } 135 res = MP_OKAY; 136 CLEANUP: 137 return res; 138 } 139 140 #if !defined(MP_ASSEMBLY_MUL_MONT) && !defined(MP_MONT_USE_MP_MUL) 141 mp_err s_mp_mul_mont(const mp_int *a, const mp_int *b, mp_int *c, 142 mp_mont_modulus *mmm) 143 { 144 mp_digit *pb; 145 mp_digit m_i; 146 mp_err res; 147 mp_size ib; 148 mp_size useda, usedb; 149 150 ARGCHK(a != NULL && b != NULL && c != NULL, MP_BADARG); 151 152 if (MP_USED(a) < MP_USED(b)) { 153 const mp_int *xch = b; /* switch a and b, to do fewer outer loops */ 154 b = a; 155 a = xch; 156 } 157 158 MP_USED(c) = 1; MP_DIGIT(c, 0) = 0; 159 ib = MP_USED(a) + MP_MAX(MP_USED(b), MP_USED(&mmm->N)) + 2; 160 if((res = s_mp_pad(c, ib)) != MP_OKAY) 161 goto CLEANUP; 162 163 useda = MP_USED(a); 164 pb = MP_DIGITS(b); 165 s_mpv_mul_d(MP_DIGITS(a), useda, *pb++, MP_DIGITS(c)); 166 s_mp_setz(MP_DIGITS(c) + useda + 1, ib - (useda + 1)); 167 m_i = MP_DIGIT(c, 0) * mmm->n0prime; 168 s_mp_mul_d_add_offset(&mmm->N, m_i, c, 0); 169 170 /* Outer loop: Digits of b */ 171 usedb = MP_USED(b); 172 for (ib = 1; ib < usedb; ib++) { 173 mp_digit b_i = *pb++; 174 175 /* Inner product: Digits of a */ 176 if (b_i) 177 s_mpv_mul_d_add_prop(MP_DIGITS(a), useda, b_i, MP_DIGITS(c) + ib); 178 m_i = MP_DIGIT(c, ib) * mmm->n0prime; 179 s_mp_mul_d_add_offset(&mmm->N, m_i, c, ib); 180 } 181 if (usedb < MP_USED(&mmm->N)) { 182 for (usedb = MP_USED(&mmm->N); ib < usedb; ++ib ) { 183 m_i = MP_DIGIT(c, ib) * mmm->n0prime; 184 s_mp_mul_d_add_offset(&mmm->N, m_i, c, ib); 185 } 186 } 187 s_mp_clamp(c); 188 s_mp_div_2d(c, mmm->b); 189 if (s_mp_cmp(c, &mmm->N) >= 0) { 190 MP_CHECKOK( s_mp_sub(c, &mmm->N) ); 191 } 192 res = MP_OKAY; 193 194 CLEANUP: 195 return res; 196 } 197 #endif