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