4272N/A/*
4272N/A * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
4272N/A * Use is subject to license terms.
4272N/A *
4272N/A * This library is free software; you can redistribute it and/or
4272N/A * modify it under the terms of the GNU Lesser General Public
4272N/A * License as published by the Free Software Foundation; either
4272N/A * version 2.1 of the License, or (at your option) any later version.
1674N/A *
4272N/A * This library is distributed in the hope that it will be useful,
4272N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
4272N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
4272N/A * Lesser General Public License for more details.
1674N/A *
4272N/A * You should have received a copy of the GNU Lesser General Public License
4272N/A * along with this library; if not, write to the Free Software Foundation,
4272N/A * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1674N/A *
4272N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4272N/A * or visit www.oracle.com if you need additional information or have any
4272N/A * questions.
4272N/A */
4272N/A
4272N/A/* *********************************************************************
1674N/A *
1674N/A * The Original Code is the Netscape security libraries.
1674N/A *
1674N/A * The Initial Developer of the Original Code is
1674N/A * Netscape Communications Corporation.
1674N/A * Portions created by the Initial Developer are Copyright (C) 2000
1674N/A * the Initial Developer. All Rights Reserved.
1674N/A *
1674N/A * Contributor(s):
1674N/A * Sheueling Chang Shantz <sheueling.chang@sun.com>,
1674N/A * Stephen Fung <stephen.fung@sun.com>, and
1674N/A * Douglas Stebila <douglas@stebila.ca> of Sun Laboratories.
1674N/A *
1674N/A *********************************************************************** */
1674N/A
1674N/A/* This file implements moduluar exponentiation using Montgomery's
1674N/A * method for modular reduction. This file implements the method
1674N/A * described as "Improvement 1" in the paper "A Cryptogrpahic Library for
1674N/A * the Motorola DSP56000" by Stephen R. Dusse' and Burton S. Kaliski Jr.
1674N/A * published in "Advances in Cryptology: Proceedings of EUROCRYPT '90"
1674N/A * "Lecture Notes in Computer Science" volume 473, 1991, pg 230-244,
1674N/A * published by Springer Verlag.
1674N/A */
1674N/A
1674N/A#define MP_USING_CACHE_SAFE_MOD_EXP 1
1674N/A#ifndef _KERNEL
1674N/A#include <string.h>
1674N/A#include <stddef.h> /* ptrdiff_t */
1674N/A#endif
1674N/A#include "mpi-priv.h"
1674N/A#include "mplogic.h"
1674N/A#include "mpprime.h"
1674N/A#ifdef MP_USING_MONT_MULF
1674N/A#include "montmulf.h"
1674N/A#endif
1674N/A
1674N/A/* if MP_CHAR_STORE_SLOW is defined, we */
1674N/A/* need to know endianness of this platform. */
1674N/A#ifdef MP_CHAR_STORE_SLOW
1674N/A#if !defined(MP_IS_BIG_ENDIAN) && !defined(MP_IS_LITTLE_ENDIAN)
1674N/A#error "You must define MP_IS_BIG_ENDIAN or MP_IS_LITTLE_ENDIAN\n" \
1674N/A " if you define MP_CHAR_STORE_SLOW."
1674N/A#endif
1674N/A#endif
1674N/A
1674N/A#ifndef STATIC
1674N/A#define STATIC
1674N/A#endif
1674N/A
1674N/A#define MAX_ODD_INTS 32 /* 2 ** (WINDOW_BITS - 1) */
1674N/A
1674N/A#ifndef _KERNEL
1674N/A#if defined(_WIN32_WCE)
1674N/A#define ABORT res = MP_UNDEF; goto CLEANUP
1674N/A#else
1674N/A#define ABORT abort()
1674N/A#endif
1674N/A#else
1674N/A#define ABORT res = MP_UNDEF; goto CLEANUP
1674N/A#endif /* _KERNEL */
1674N/A
1674N/A/* computes T = REDC(T), 2^b == R */
1674N/Amp_err s_mp_redc(mp_int *T, mp_mont_modulus *mmm)
1674N/A{
1674N/A mp_err res;
1674N/A mp_size i;
1674N/A
1674N/A i = MP_USED(T) + MP_USED(&mmm->N) + 2;
1674N/A MP_CHECKOK( s_mp_pad(T, i) );
1674N/A for (i = 0; i < MP_USED(&mmm->N); ++i ) {
1674N/A mp_digit m_i = MP_DIGIT(T, i) * mmm->n0prime;
1674N/A /* T += N * m_i * (MP_RADIX ** i); */
1674N/A MP_CHECKOK( s_mp_mul_d_add_offset(&mmm->N, m_i, T, i) );
1674N/A }
1674N/A s_mp_clamp(T);
1674N/A
1674N/A /* T /= R */
1674N/A s_mp_div_2d(T, mmm->b);
1674N/A
1674N/A if ((res = s_mp_cmp(T, &mmm->N)) >= 0) {
1674N/A /* T = T - N */
1674N/A MP_CHECKOK( s_mp_sub(T, &mmm->N) );
1674N/A#ifdef DEBUG
1674N/A if ((res = mp_cmp(T, &mmm->N)) >= 0) {
1674N/A res = MP_UNDEF;
1674N/A goto CLEANUP;
1674N/A }
1674N/A#endif
1674N/A }
1674N/A res = MP_OKAY;
1674N/ACLEANUP:
1674N/A return res;
1674N/A}
1674N/A
1674N/A#if !defined(MP_ASSEMBLY_MUL_MONT) && !defined(MP_MONT_USE_MP_MUL)
1674N/Amp_err s_mp_mul_mont(const mp_int *a, const mp_int *b, mp_int *c,
1674N/A mp_mont_modulus *mmm)
1674N/A{
1674N/A mp_digit *pb;
1674N/A mp_digit m_i;
1674N/A mp_err res;
1674N/A mp_size ib;
1674N/A mp_size useda, usedb;
1674N/A
1674N/A ARGCHK(a != NULL && b != NULL && c != NULL, MP_BADARG);
1674N/A
1674N/A if (MP_USED(a) < MP_USED(b)) {
1674N/A const mp_int *xch = b; /* switch a and b, to do fewer outer loops */
1674N/A b = a;
1674N/A a = xch;
1674N/A }
1674N/A
1674N/A MP_USED(c) = 1; MP_DIGIT(c, 0) = 0;
1674N/A ib = MP_USED(a) + MP_MAX(MP_USED(b), MP_USED(&mmm->N)) + 2;
1674N/A if((res = s_mp_pad(c, ib)) != MP_OKAY)
1674N/A goto CLEANUP;
1674N/A
1674N/A useda = MP_USED(a);
1674N/A pb = MP_DIGITS(b);
1674N/A s_mpv_mul_d(MP_DIGITS(a), useda, *pb++, MP_DIGITS(c));
1674N/A s_mp_setz(MP_DIGITS(c) + useda + 1, ib - (useda + 1));
1674N/A m_i = MP_DIGIT(c, 0) * mmm->n0prime;
1674N/A s_mp_mul_d_add_offset(&mmm->N, m_i, c, 0);
1674N/A
1674N/A /* Outer loop: Digits of b */
1674N/A usedb = MP_USED(b);
1674N/A for (ib = 1; ib < usedb; ib++) {
1674N/A mp_digit b_i = *pb++;
1674N/A
1674N/A /* Inner product: Digits of a */
1674N/A if (b_i)
1674N/A s_mpv_mul_d_add_prop(MP_DIGITS(a), useda, b_i, MP_DIGITS(c) + ib);
1674N/A m_i = MP_DIGIT(c, ib) * mmm->n0prime;
1674N/A s_mp_mul_d_add_offset(&mmm->N, m_i, c, ib);
1674N/A }
1674N/A if (usedb < MP_USED(&mmm->N)) {
1674N/A for (usedb = MP_USED(&mmm->N); ib < usedb; ++ib ) {
1674N/A m_i = MP_DIGIT(c, ib) * mmm->n0prime;
1674N/A s_mp_mul_d_add_offset(&mmm->N, m_i, c, ib);
1674N/A }
1674N/A }
1674N/A s_mp_clamp(c);
1674N/A s_mp_div_2d(c, mmm->b);
1674N/A if (s_mp_cmp(c, &mmm->N) >= 0) {
1674N/A MP_CHECKOK( s_mp_sub(c, &mmm->N) );
1674N/A }
1674N/A res = MP_OKAY;
1674N/A
1674N/ACLEANUP:
1674N/A return res;
1674N/A}
1674N/A#endif