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/* Fast modular reduction for p384 = 2^384 - 2^128 - 2^96 + 2^32 - 1. a can be r.
1N/A * Uses algorithm 2.30 from Hankerson, Menezes, Vanstone. Guide to
1N/A * Elliptic Curve Cryptography. */
1N/Amp_err
1N/Aec_GFp_nistp384_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 m[10];
1N/A
1N/A#ifdef ECL_THIRTY_TWO_BIT
1N/A mp_digit s[10][12];
1N/A for (i = 0; i < 10; i++) {
1N/A MP_SIGN(&m[i]) = MP_ZPOS;
1N/A MP_ALLOC(&m[i]) = 12;
1N/A MP_USED(&m[i]) = 12;
1N/A MP_DIGITS(&m[i]) = s[i];
1N/A MP_FLAG(&m[i]) = MP_FLAG(a);
1N/A }
1N/A#else
1N/A mp_digit s[10][6];
1N/A for (i = 0; i < 10; i++) {
1N/A MP_SIGN(&m[i]) = MP_ZPOS;
1N/A MP_ALLOC(&m[i]) = 6;
1N/A MP_USED(&m[i]) = 6;
1N/A MP_DIGITS(&m[i]) = s[i];
1N/A MP_FLAG(&m[i]) = MP_FLAG(a);
1N/A }
1N/A#endif
1N/A
1N/A#ifdef ECL_THIRTY_TWO_BIT
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 > 768) || (a_bits <= 736)) {
1N/A MP_CHECKOK(mp_mod(a, &meth->irr, r));
1N/A } else {
1N/A for (i = 0; i < 12; i++) {
1N/A s[0][i] = MP_DIGIT(a, i);
1N/A }
1N/A s[1][0] = 0;
1N/A s[1][1] = 0;
1N/A s[1][2] = 0;
1N/A s[1][3] = 0;
1N/A s[1][4] = MP_DIGIT(a, 21);
1N/A s[1][5] = MP_DIGIT(a, 22);
1N/A s[1][6] = MP_DIGIT(a, 23);
1N/A s[1][7] = 0;
1N/A s[1][8] = 0;
1N/A s[1][9] = 0;
1N/A s[1][10] = 0;
1N/A s[1][11] = 0;
1N/A for (i = 0; i < 12; i++) {
1N/A s[2][i] = MP_DIGIT(a, i+12);
1N/A }
1N/A s[3][0] = MP_DIGIT(a, 21);
1N/A s[3][1] = MP_DIGIT(a, 22);
1N/A s[3][2] = MP_DIGIT(a, 23);
1N/A for (i = 3; i < 12; i++) {
1N/A s[3][i] = MP_DIGIT(a, i+9);
1N/A }
1N/A s[4][0] = 0;
1N/A s[4][1] = MP_DIGIT(a, 23);
1N/A s[4][2] = 0;
1N/A s[4][3] = MP_DIGIT(a, 20);
1N/A for (i = 4; i < 12; i++) {
1N/A s[4][i] = MP_DIGIT(a, i+8);
1N/A }
1N/A s[5][0] = 0;
1N/A s[5][1] = 0;
1N/A s[5][2] = 0;
1N/A s[5][3] = 0;
1N/A s[5][4] = MP_DIGIT(a, 20);
1N/A s[5][5] = MP_DIGIT(a, 21);
1N/A s[5][6] = MP_DIGIT(a, 22);
1N/A s[5][7] = MP_DIGIT(a, 23);
1N/A s[5][8] = 0;
1N/A s[5][9] = 0;
1N/A s[5][10] = 0;
1N/A s[5][11] = 0;
1N/A s[6][0] = MP_DIGIT(a, 20);
1N/A s[6][1] = 0;
1N/A s[6][2] = 0;
1N/A s[6][3] = MP_DIGIT(a, 21);
1N/A s[6][4] = MP_DIGIT(a, 22);
1N/A s[6][5] = MP_DIGIT(a, 23);
1N/A s[6][6] = 0;
1N/A s[6][7] = 0;
1N/A s[6][8] = 0;
1N/A s[6][9] = 0;
1N/A s[6][10] = 0;
1N/A s[6][11] = 0;
1N/A s[7][0] = MP_DIGIT(a, 23);
1N/A for (i = 1; i < 12; i++) {
1N/A s[7][i] = MP_DIGIT(a, i+11);
1N/A }
1N/A s[8][0] = 0;
1N/A s[8][1] = MP_DIGIT(a, 20);
1N/A s[8][2] = MP_DIGIT(a, 21);
1N/A s[8][3] = MP_DIGIT(a, 22);
1N/A s[8][4] = MP_DIGIT(a, 23);
1N/A s[8][5] = 0;
1N/A s[8][6] = 0;
1N/A s[8][7] = 0;
1N/A s[8][8] = 0;
1N/A s[8][9] = 0;
1N/A s[8][10] = 0;
1N/A s[8][11] = 0;
1N/A s[9][0] = 0;
1N/A s[9][1] = 0;
1N/A s[9][2] = 0;
1N/A s[9][3] = MP_DIGIT(a, 23);
1N/A s[9][4] = MP_DIGIT(a, 23);
1N/A s[9][5] = 0;
1N/A s[9][6] = 0;
1N/A s[9][7] = 0;
1N/A s[9][8] = 0;
1N/A s[9][9] = 0;
1N/A s[9][10] = 0;
1N/A s[9][11] = 0;
1N/A
1N/A MP_CHECKOK(mp_add(&m[0], &m[1], r));
1N/A MP_CHECKOK(mp_add(r, &m[1], r));
1N/A MP_CHECKOK(mp_add(r, &m[2], r));
1N/A MP_CHECKOK(mp_add(r, &m[3], r));
1N/A MP_CHECKOK(mp_add(r, &m[4], r));
1N/A MP_CHECKOK(mp_add(r, &m[5], r));
1N/A MP_CHECKOK(mp_add(r, &m[6], r));
1N/A MP_CHECKOK(mp_sub(r, &m[7], r));
1N/A MP_CHECKOK(mp_sub(r, &m[8], r));
1N/A MP_CHECKOK(mp_submod(r, &m[9], &meth->irr, r));
1N/A s_mp_clamp(r);
1N/A }
1N/A#else
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 > 768) || (a_bits <= 736)) {
1N/A MP_CHECKOK(mp_mod(a, &meth->irr, r));
1N/A } else {
1N/A for (i = 0; i < 6; i++) {
1N/A s[0][i] = MP_DIGIT(a, i);
1N/A }
1N/A s[1][0] = 0;
1N/A s[1][1] = 0;
1N/A s[1][2] = (MP_DIGIT(a, 10) >> 32) | (MP_DIGIT(a, 11) << 32);
1N/A s[1][3] = MP_DIGIT(a, 11) >> 32;
1N/A s[1][4] = 0;
1N/A s[1][5] = 0;
1N/A for (i = 0; i < 6; i++) {
1N/A s[2][i] = MP_DIGIT(a, i+6);
1N/A }
1N/A s[3][0] = (MP_DIGIT(a, 10) >> 32) | (MP_DIGIT(a, 11) << 32);
1N/A s[3][1] = (MP_DIGIT(a, 11) >> 32) | (MP_DIGIT(a, 6) << 32);
1N/A for (i = 2; i < 6; i++) {
1N/A s[3][i] = (MP_DIGIT(a, i+4) >> 32) | (MP_DIGIT(a, i+5) << 32);
1N/A }
1N/A s[4][0] = (MP_DIGIT(a, 11) >> 32) << 32;
1N/A s[4][1] = MP_DIGIT(a, 10) << 32;
1N/A for (i = 2; i < 6; i++) {
1N/A s[4][i] = MP_DIGIT(a, i+4);
1N/A }
1N/A s[5][0] = 0;
1N/A s[5][1] = 0;
1N/A s[5][2] = MP_DIGIT(a, 10);
1N/A s[5][3] = MP_DIGIT(a, 11);
1N/A s[5][4] = 0;
1N/A s[5][5] = 0;
1N/A s[6][0] = (MP_DIGIT(a, 10) << 32) >> 32;
1N/A s[6][1] = (MP_DIGIT(a, 10) >> 32) << 32;
1N/A s[6][2] = MP_DIGIT(a, 11);
1N/A s[6][3] = 0;
1N/A s[6][4] = 0;
1N/A s[6][5] = 0;
1N/A s[7][0] = (MP_DIGIT(a, 11) >> 32) | (MP_DIGIT(a, 6) << 32);
1N/A for (i = 1; i < 6; i++) {
1N/A s[7][i] = (MP_DIGIT(a, i+5) >> 32) | (MP_DIGIT(a, i+6) << 32);
1N/A }
1N/A s[8][0] = MP_DIGIT(a, 10) << 32;
1N/A s[8][1] = (MP_DIGIT(a, 10) >> 32) | (MP_DIGIT(a, 11) << 32);
1N/A s[8][2] = MP_DIGIT(a, 11) >> 32;
1N/A s[8][3] = 0;
1N/A s[8][4] = 0;
1N/A s[8][5] = 0;
1N/A s[9][0] = 0;
1N/A s[9][1] = (MP_DIGIT(a, 11) >> 32) << 32;
1N/A s[9][2] = MP_DIGIT(a, 11) >> 32;
1N/A s[9][3] = 0;
1N/A s[9][4] = 0;
1N/A s[9][5] = 0;
1N/A
1N/A MP_CHECKOK(mp_add(&m[0], &m[1], r));
1N/A MP_CHECKOK(mp_add(r, &m[1], r));
1N/A MP_CHECKOK(mp_add(r, &m[2], r));
1N/A MP_CHECKOK(mp_add(r, &m[3], r));
1N/A MP_CHECKOK(mp_add(r, &m[4], r));
1N/A MP_CHECKOK(mp_add(r, &m[5], r));
1N/A MP_CHECKOK(mp_add(r, &m[6], r));
1N/A MP_CHECKOK(mp_sub(r, &m[7], r));
1N/A MP_CHECKOK(mp_sub(r, &m[8], r));
1N/A MP_CHECKOK(mp_submod(r, &m[9], &meth->irr, r));
1N/A s_mp_clamp(r);
1N/A }
1N/A#endif
1N/A
1N/A CLEANUP:
1N/A return res;
1N/A}
1N/A
1N/A/* Compute the square of polynomial a, reduce modulo p384. Store the
1N/A * result in r. r could be a. Uses optimized modular reduction for p384.
1N/A */
1N/Amp_err
1N/Aec_GFp_nistp384_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_nistp384_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 p384.
1N/A * Store the result in r. r could be a or b; a could be b. Uses
1N/A * optimized modular reduction for p384. */
1N/Amp_err
1N/Aec_GFp_nistp384_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_nistp384_mod(r, r, meth));
1N/A CLEANUP:
1N/A return res;
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_gfp384(ECGroup *group, ECCurveName name)
1N/A{
1N/A if (name == ECCurve_NIST_P384) {
1N/A group->meth->field_mod = &ec_GFp_nistp384_mod;
1N/A group->meth->field_mul = &ec_GFp_nistp384_mul;
1N/A group->meth->field_sqr = &ec_GFp_nistp384_sqr;
1N/A }
1N/A return MP_OKAY;
1N/A}
1N/A/* END CSTYLED */