Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/native/sun/security/ec/impl/mpmontg.c
38918 views
/*1* Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.2* Use is subject to license terms.3*4* This library is free software; you can redistribute it and/or5* modify it under the terms of the GNU Lesser General Public6* License as published by the Free Software Foundation; either7* version 2.1 of the License, or (at your option) any later version.8*9* This library is distributed in the hope that it will be useful,10* but WITHOUT ANY WARRANTY; without even the implied warranty of11* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU12* Lesser General Public License for more details.13*14* You should have received a copy of the GNU Lesser General Public License15* along with this library; if not, write to the Free Software Foundation,16* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* *********************************************************************24*25* The Original Code is the Netscape security libraries.26*27* The Initial Developer of the Original Code is28* Netscape Communications Corporation.29* Portions created by the Initial Developer are Copyright (C) 200030* the Initial Developer. All Rights Reserved.31*32* Contributor(s):33* Sheueling Chang Shantz <[email protected]>,34* Stephen Fung <[email protected]>, and35* Douglas Stebila <[email protected]> of Sun Laboratories.36*37*********************************************************************** */3839/* This file implements moduluar exponentiation using Montgomery's40* method for modular reduction. This file implements the method41* described as "Improvement 1" in the paper "A Cryptogrpahic Library for42* the Motorola DSP56000" by Stephen R. Dusse' and Burton S. Kaliski Jr.43* published in "Advances in Cryptology: Proceedings of EUROCRYPT '90"44* "Lecture Notes in Computer Science" volume 473, 1991, pg 230-244,45* published by Springer Verlag.46*/4748#define MP_USING_CACHE_SAFE_MOD_EXP 149#ifndef _KERNEL50#include <string.h>51#include <stddef.h> /* ptrdiff_t */52#endif53#include "mpi-priv.h"54#include "mplogic.h"55#include "mpprime.h"56#ifdef MP_USING_MONT_MULF57#include "montmulf.h"58#endif5960/* if MP_CHAR_STORE_SLOW is defined, we */61/* need to know endianness of this platform. */62#ifdef MP_CHAR_STORE_SLOW63#if !defined(MP_IS_BIG_ENDIAN) && !defined(MP_IS_LITTLE_ENDIAN)64#error "You must define MP_IS_BIG_ENDIAN or MP_IS_LITTLE_ENDIAN\n" \65" if you define MP_CHAR_STORE_SLOW."66#endif67#endif6869#ifndef STATIC70#define STATIC71#endif7273#define MAX_ODD_INTS 32 /* 2 ** (WINDOW_BITS - 1) */7475#ifndef _KERNEL76#if defined(_WIN32_WCE)77#define ABORT res = MP_UNDEF; goto CLEANUP78#else79#define ABORT abort()80#endif81#else82#define ABORT res = MP_UNDEF; goto CLEANUP83#endif /* _KERNEL */8485/* computes T = REDC(T), 2^b == R */86mp_err s_mp_redc(mp_int *T, mp_mont_modulus *mmm)87{88mp_err res;89mp_size i;9091i = MP_USED(T) + MP_USED(&mmm->N) + 2;92MP_CHECKOK( s_mp_pad(T, i) );93for (i = 0; i < MP_USED(&mmm->N); ++i ) {94mp_digit m_i = MP_DIGIT(T, i) * mmm->n0prime;95/* T += N * m_i * (MP_RADIX ** i); */96MP_CHECKOK( s_mp_mul_d_add_offset(&mmm->N, m_i, T, i) );97}98s_mp_clamp(T);99100/* T /= R */101s_mp_div_2d(T, mmm->b);102103if ((res = s_mp_cmp(T, &mmm->N)) >= 0) {104/* T = T - N */105MP_CHECKOK( s_mp_sub(T, &mmm->N) );106#ifdef DEBUG107if ((res = mp_cmp(T, &mmm->N)) >= 0) {108res = MP_UNDEF;109goto CLEANUP;110}111#endif112}113res = MP_OKAY;114CLEANUP:115return res;116}117118#if !defined(MP_ASSEMBLY_MUL_MONT) && !defined(MP_MONT_USE_MP_MUL)119mp_err s_mp_mul_mont(const mp_int *a, const mp_int *b, mp_int *c,120mp_mont_modulus *mmm)121{122mp_digit *pb;123mp_digit m_i;124mp_err res;125mp_size ib;126mp_size useda, usedb;127128ARGCHK(a != NULL && b != NULL && c != NULL, MP_BADARG);129130if (MP_USED(a) < MP_USED(b)) {131const mp_int *xch = b; /* switch a and b, to do fewer outer loops */132b = a;133a = xch;134}135136MP_USED(c) = 1; MP_DIGIT(c, 0) = 0;137ib = MP_USED(a) + MP_MAX(MP_USED(b), MP_USED(&mmm->N)) + 2;138if((res = s_mp_pad(c, ib)) != MP_OKAY)139goto CLEANUP;140141useda = MP_USED(a);142pb = MP_DIGITS(b);143s_mpv_mul_d(MP_DIGITS(a), useda, *pb++, MP_DIGITS(c));144s_mp_setz(MP_DIGITS(c) + useda + 1, ib - (useda + 1));145m_i = MP_DIGIT(c, 0) * mmm->n0prime;146s_mp_mul_d_add_offset(&mmm->N, m_i, c, 0);147148/* Outer loop: Digits of b */149usedb = MP_USED(b);150for (ib = 1; ib < usedb; ib++) {151mp_digit b_i = *pb++;152153/* Inner product: Digits of a */154if (b_i)155s_mpv_mul_d_add_prop(MP_DIGITS(a), useda, b_i, MP_DIGITS(c) + ib);156m_i = MP_DIGIT(c, ib) * mmm->n0prime;157s_mp_mul_d_add_offset(&mmm->N, m_i, c, ib);158}159if (usedb < MP_USED(&mmm->N)) {160for (usedb = MP_USED(&mmm->N); ib < usedb; ++ib ) {161m_i = MP_DIGIT(c, ib) * mmm->n0prime;162s_mp_mul_d_add_offset(&mmm->N, m_i, c, ib);163}164}165s_mp_clamp(c);166s_mp_div_2d(c, mmm->b);167if (s_mp_cmp(c, &mmm->N) >= 0) {168MP_CHECKOK( s_mp_sub(c, &mmm->N) );169}170res = MP_OKAY;171172CLEANUP:173return res;174}175#endif176177178