1N/A/* BEGIN CSTYLED */
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 for prime field curves.
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>
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 (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
1N/A *
1N/A * Sun elects to use this software under the MPL license.
1N/A */
1N/A
1N/A#include "ecp.h"
1N/A#include "mpi.h"
1N/A#include "mplogic.h"
1N/A#include "mpi-priv.h"
1N/A#ifndef _KERNEL
1N/A#include <stdlib.h>
1N/A#endif
1N/A
1N/A#define ECP521_DIGITS ECL_CURVE_DIGITS(521)
1N/A
1N/A/* Fast modular reduction for p521 = 2^521 - 1. a can be r. Uses
1N/A * algorithm 2.31 from Hankerson, Menezes, Vanstone. Guide to
1N/A * Elliptic Curve Cryptography. */
1N/Amp_err
1N/Aec_GFp_nistp521_mod(const mp_int *a, mp_int *r, const GFMethod *meth)
1N/A{
1N/A mp_err res = MP_OKAY;
1N/A int a_bits = mpl_significant_bits(a);
1N/A int i;
1N/A
1N/A /* m1, m2 are statically-allocated mp_int of exactly the size we need */
1N/A mp_int m1;
1N/A
1N/A mp_digit s1[ECP521_DIGITS] = { 0 };
1N/A
1N/A MP_SIGN(&m1) = MP_ZPOS;
1N/A MP_ALLOC(&m1) = ECP521_DIGITS;
1N/A MP_USED(&m1) = ECP521_DIGITS;
1N/A MP_DIGITS(&m1) = s1;
1N/A MP_FLAG(&m1) = MP_FLAG(a);
1N/A
1N/A if (a_bits < 521) {
1N/A if (a==r) return MP_OKAY;
1N/A return mp_copy(a, r);
1N/A }
1N/A /* for polynomials larger than twice the field size or polynomials
1N/A * not using all words, use regular reduction */
1N/A if (a_bits > (521*2)) {
1N/A MP_CHECKOK(mp_mod(a, &meth->irr, r));
1N/A } else {
1N/A#define FIRST_DIGIT (ECP521_DIGITS-1)
1N/A for (i = FIRST_DIGIT; i < MP_USED(a)-1; i++) {
1N/A s1[i-FIRST_DIGIT] = (MP_DIGIT(a, i) >> 9)
1N/A | (MP_DIGIT(a, 1+i) << (MP_DIGIT_BIT-9));
1N/A }
1N/A s1[i-FIRST_DIGIT] = MP_DIGIT(a, i) >> 9;
1N/A
1N/A if ( a != r ) {
1N/A MP_CHECKOK(s_mp_pad(r,ECP521_DIGITS));
1N/A for (i = 0; i < ECP521_DIGITS; i++) {
1N/A MP_DIGIT(r,i) = MP_DIGIT(a, i);
1N/A }
1N/A }
1N/A MP_USED(r) = ECP521_DIGITS;
1N/A MP_DIGIT(r,FIRST_DIGIT) &= 0x1FF;
1N/A
1N/A MP_CHECKOK(s_mp_add(r, &m1));
1N/A if (MP_DIGIT(r, FIRST_DIGIT) & 0x200) {
1N/A MP_CHECKOK(s_mp_add_d(r,1));
1N/A MP_DIGIT(r,FIRST_DIGIT) &= 0x1FF;
1N/A }
1N/A s_mp_clamp(r);
1N/A }
1N/A
1N/A CLEANUP:
1N/A return res;
1N/A}
1N/A
1N/A/* Compute the square of polynomial a, reduce modulo p521. Store the
1N/A * result in r. r could be a. Uses optimized modular reduction for p521.
1N/A */
1N/Amp_err
1N/Aec_GFp_nistp521_sqr(const mp_int *a, mp_int *r, const GFMethod *meth)
1N/A{
1N/A mp_err res = MP_OKAY;
1N/A
1N/A MP_CHECKOK(mp_sqr(a, r));
1N/A MP_CHECKOK(ec_GFp_nistp521_mod(r, r, meth));
1N/A CLEANUP:
1N/A return res;
1N/A}
1N/A
1N/A/* Compute the product of two polynomials a and b, reduce modulo p521.
1N/A * Store the result in r. r could be a or b; a could be b. Uses
1N/A * optimized modular reduction for p521. */
1N/Amp_err
1N/Aec_GFp_nistp521_mul(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 MP_CHECKOK(mp_mul(a, b, r));
1N/A MP_CHECKOK(ec_GFp_nistp521_mod(r, r, meth));
1N/A CLEANUP:
1N/A return res;
1N/A}
1N/A
1N/A/* Divides two field elements. If a is NULL, then returns the inverse of
1N/A * b. */
1N/Amp_err
1N/Aec_GFp_nistp521_div(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 mp_int t;
1N/A
1N/A /* If a is NULL, then return the inverse of b, otherwise return a/b. */
1N/A if (a == NULL) {
1N/A return mp_invmod(b, &meth->irr, r);
1N/A } else {
1N/A /* MPI doesn't support divmod, so we implement it using invmod and
1N/A * mulmod. */
1N/A MP_CHECKOK(mp_init(&t, FLAG(b)));
1N/A MP_CHECKOK(mp_invmod(b, &meth->irr, &t));
1N/A MP_CHECKOK(mp_mul(a, &t, r));
1N/A MP_CHECKOK(ec_GFp_nistp521_mod(r, r, meth));
1N/A CLEANUP:
1N/A mp_clear(&t);
1N/A return res;
1N/A }
1N/A}
1N/A
1N/A/* Wire in fast field arithmetic and precomputation of base point for
1N/A * named curves. */
1N/Amp_err
1N/Aec_group_set_gfp521(ECGroup *group, ECCurveName name)
1N/A{
1N/A if (name == ECCurve_NIST_P521) {
1N/A group->meth->field_mod = &ec_GFp_nistp521_mod;
1N/A group->meth->field_mul = &ec_GFp_nistp521_mul;
1N/A group->meth->field_sqr = &ec_GFp_nistp521_sqr;
1N/A group->meth->field_div = &ec_GFp_nistp521_div;
1N/A }
1N/A return MP_OKAY;
1N/A}
1N/A/* END CSTYLED */