1N/A/*
1N/A * ***** BEGIN LICENSE BLOCK *****
1N/A * Version: MPL 1.1/GPL 2.0/LGPL 2.1
1N/A *
1N/A * The contents of this file are subject to the Mozilla Public License Version
1N/A * 1.1 (the "License"); you may not use this file except in compliance with
1N/A * the License. You may obtain a copy of the License at
1N/A * http://www.mozilla.org/MPL/
1N/A *
1N/A * Software distributed under the License is distributed on an "AS IS" basis,
1N/A * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
1N/A * for the specific language governing rights and limitations under the
1N/A * License.
1N/A *
1N/A * The Original Code is the elliptic curve math library.
1N/A *
1N/A * The Initial Developer of the Original Code is
1N/A * Sun Microsystems, Inc.
1N/A * Portions created by the Initial Developer are Copyright (C) 2003
1N/A * the Initial Developer. All Rights Reserved.
1N/A *
1N/A * Contributor(s):
1N/A * Douglas Stebila <douglas@stebila.ca>, Sun Microsystems Laboratories
1N/A *
1N/A * Alternatively, the contents of this file may be used under the terms of
1N/A * either the GNU General Public License Version 2 or later (the "GPL"), or
1N/A * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
1N/A * in which case the provisions of the GPL or the LGPL are applicable instead
1N/A * of those above. If you wish to allow use of your version of this file only
1N/A * under the terms of either the GPL or the LGPL, and not to allow others to
1N/A * use your version of this file under the terms of the MPL, indicate your
1N/A * decision by deleting the provisions above and replace them with the notice
1N/A * and other provisions required by the GPL or the LGPL. If you do not delete
1N/A * the provisions above, a recipient may use your version of this file under
1N/A * the terms of any one of the MPL, the GPL or the LGPL.
1N/A *
1N/A * ***** END LICENSE BLOCK ***** */
1N/A/*
1N/A * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
1N/A * Use is subject to license terms.
1N/A *
1N/A * Sun elects to use this software under the MPL license.
1N/A */
1N/A
1N/A#pragma ident "%Z%%M% %I% %E% SMI"
1N/A
1N/A/* Uses Montgomery reduction for field arithmetic. See mpi/mpmontg.c for
1N/A * code implementation. */
1N/A
1N/A#include "mpi.h"
1N/A#include "mplogic.h"
1N/A#include "mpi-priv.h"
1N/A#include "ecl-priv.h"
1N/A#include "ecp.h"
1N/A#ifndef _KERNEL
1N/A#include <stdlib.h>
1N/A#include <stdio.h>
1N/A#endif
1N/A
1N/A/* Construct a generic GFMethod for arithmetic over prime fields with
1N/A * irreducible irr. */
1N/AGFMethod *
1N/AGFMethod_consGFp_mont(const mp_int *irr)
1N/A{
1N/A mp_err res = MP_OKAY;
1N/A int i;
1N/A GFMethod *meth = NULL;
1N/A mp_mont_modulus *mmm;
1N/A
1N/A meth = GFMethod_consGFp(irr);
1N/A if (meth == NULL)
1N/A return NULL;
1N/A
1N/A#ifdef _KERNEL
1N/A mmm = (mp_mont_modulus *) kmem_alloc(sizeof(mp_mont_modulus),
1N/A FLAG(irr));
1N/A#else
1N/A mmm = (mp_mont_modulus *) malloc(sizeof(mp_mont_modulus));
1N/A#endif
1N/A if (mmm == NULL) {
1N/A res = MP_MEM;
1N/A goto CLEANUP;
1N/A }
1N/A
1N/A meth->field_mul = &ec_GFp_mul_mont;
1N/A meth->field_sqr = &ec_GFp_sqr_mont;
1N/A meth->field_div = &ec_GFp_div_mont;
1N/A meth->field_enc = &ec_GFp_enc_mont;
1N/A meth->field_dec = &ec_GFp_dec_mont;
1N/A meth->extra1 = mmm;
1N/A meth->extra2 = NULL;
1N/A meth->extra_free = &ec_GFp_extra_free_mont;
1N/A
1N/A mmm->N = meth->irr;
1N/A i = mpl_significant_bits(&meth->irr);
1N/A i += MP_DIGIT_BIT - 1;
1N/A mmm->b = i - i % MP_DIGIT_BIT;
1N/A mmm->n0prime = 0 - s_mp_invmod_radix(MP_DIGIT(&meth->irr, 0));
1N/A
1N/A CLEANUP:
1N/A if (res != MP_OKAY) {
1N/A GFMethod_free(meth);
1N/A return NULL;
1N/A }
1N/A return meth;
1N/A}
1N/A
1N/A/* Wrapper functions for generic prime field arithmetic. */
1N/A
1N/A/* Field multiplication using Montgomery reduction. */
1N/Amp_err
1N/Aec_GFp_mul_mont(const mp_int *a, const mp_int *b, mp_int *r,
1N/A const GFMethod *meth)
1N/A{
1N/A mp_err res = MP_OKAY;
1N/A
1N/A#ifdef MP_MONT_USE_MP_MUL
1N/A /* if MP_MONT_USE_MP_MUL is defined, then the function s_mp_mul_mont
1N/A * is not implemented and we have to use mp_mul and s_mp_redc directly
1N/A */
1N/A MP_CHECKOK(mp_mul(a, b, r));
1N/A MP_CHECKOK(s_mp_redc(r, (mp_mont_modulus *) meth->extra1));
1N/A#else
1N/A mp_int s;
1N/A
1N/A MP_DIGITS(&s) = 0;
1N/A /* s_mp_mul_mont doesn't allow source and destination to be the same */
1N/A if ((a == r) || (b == r)) {
1N/A MP_CHECKOK(mp_init(&s, FLAG(a)));
1N/A MP_CHECKOK(s_mp_mul_mont
1N/A (a, b, &s, (mp_mont_modulus *) meth->extra1));
1N/A MP_CHECKOK(mp_copy(&s, r));
1N/A mp_clear(&s);
1N/A } else {
1N/A return s_mp_mul_mont(a, b, r, (mp_mont_modulus *) meth->extra1);
1N/A }
1N/A#endif
1N/A CLEANUP:
1N/A return res;
1N/A}
1N/A
1N/A/* Field squaring using Montgomery reduction. */
1N/Amp_err
1N/Aec_GFp_sqr_mont(const mp_int *a, mp_int *r, const GFMethod *meth)
1N/A{
1N/A return ec_GFp_mul_mont(a, a, r, meth);
1N/A}
1N/A
1N/A/* Field division using Montgomery reduction. */
1N/Amp_err
1N/Aec_GFp_div_mont(const mp_int *a, const mp_int *b, mp_int *r,
1N/A const GFMethod *meth)
1N/A{
1N/A mp_err res = MP_OKAY;
1N/A
1N/A /* if A=aZ represents a encoded in montgomery coordinates with Z and #
1N/A * and \ respectively represent multiplication and division in
1N/A * montgomery coordinates, then A\B = (a/b)Z = (A/B)Z and Binv =
1N/A * (1/b)Z = (1/B)(Z^2) where B # Binv = Z */
1N/A MP_CHECKOK(ec_GFp_div(a, b, r, meth));
1N/A MP_CHECKOK(ec_GFp_enc_mont(r, r, meth));
1N/A if (a == NULL) {
1N/A MP_CHECKOK(ec_GFp_enc_mont(r, r, meth));
1N/A }
1N/A CLEANUP:
1N/A return res;
1N/A}
1N/A
1N/A/* Encode a field element in Montgomery form. See s_mp_to_mont in
1N/A * mpi/mpmontg.c */
1N/Amp_err
1N/Aec_GFp_enc_mont(const mp_int *a, mp_int *r, const GFMethod *meth)
1N/A{
1N/A mp_mont_modulus *mmm;
1N/A mp_err res = MP_OKAY;
1N/A
1N/A mmm = (mp_mont_modulus *) meth->extra1;
1N/A MP_CHECKOK(mpl_lsh(a, r, mmm->b));
1N/A MP_CHECKOK(mp_mod(r, &mmm->N, r));
1N/A CLEANUP:
1N/A return res;
1N/A}
1N/A
1N/A/* Decode a field element from Montgomery form. */
1N/Amp_err
1N/Aec_GFp_dec_mont(const mp_int *a, mp_int *r, const GFMethod *meth)
1N/A{
1N/A mp_err res = MP_OKAY;
1N/A
1N/A if (a != r) {
1N/A MP_CHECKOK(mp_copy(a, r));
1N/A }
1N/A MP_CHECKOK(s_mp_redc(r, (mp_mont_modulus *) meth->extra1));
1N/A CLEANUP:
1N/A return res;
1N/A}
1N/A
1N/A/* Free the memory allocated to the extra fields of Montgomery GFMethod
1N/A * object. */
1N/Avoid
1N/Aec_GFp_extra_free_mont(GFMethod *meth)
1N/A{
1N/A if (meth->extra1 != NULL) {
1N/A#ifdef _KERNEL
1N/A kmem_free(meth->extra1, sizeof(mp_mont_modulus));
1N/A#else
1N/A free(meth->extra1);
1N/A#endif
1N/A meth->extra1 = NULL;
1N/A }
1N/A}