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 * Stephen Fung <fungstep@hotmail.com> and
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#ifndef _ECL_PRIV_H
1N/A#define _ECL_PRIV_H
1N/A
1N/A#pragma ident "%Z%%M% %I% %E% SMI"
1N/A
1N/A#include "ecl.h"
1N/A#include "mpi.h"
1N/A#include "mplogic.h"
1N/A
1N/A/* MAX_FIELD_SIZE_DIGITS is the maximum size of field element supported */
1N/A/* the following needs to go away... */
1N/A#if defined(MP_USE_LONG_LONG_DIGIT) || defined(MP_USE_LONG_DIGIT)
1N/A#define ECL_SIXTY_FOUR_BIT
1N/A#else
1N/A#define ECL_THIRTY_TWO_BIT
1N/A#endif
1N/A
1N/A#define ECL_CURVE_DIGITS(curve_size_in_bits) \
1N/A (((curve_size_in_bits)+(sizeof(mp_digit)*8-1))/(sizeof(mp_digit)*8))
1N/A#define ECL_BITS (sizeof(mp_digit)*8)
1N/A#define ECL_MAX_FIELD_SIZE_DIGITS (80/sizeof(mp_digit))
1N/A
1N/A/* Gets the i'th bit in the binary representation of a. If i >= length(a),
1N/A * then return 0. (The above behaviour differs from mpl_get_bit, which
1N/A * causes an error if i >= length(a).) */
1N/A#define MP_GET_BIT(a, i) \
1N/A ((i) >= mpl_significant_bits((a))) ? 0 : mpl_get_bit((a), (i))
1N/A
1N/A#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD)
1N/A#define MP_ADD_CARRY(a1, a2, s, cin, cout) \
1N/A { mp_word w; \
1N/A w = ((mp_word)(cin)) + (a1) + (a2); \
1N/A s = ACCUM(w); \
1N/A cout = CARRYOUT(w); }
1N/A
1N/A#define MP_SUB_BORROW(a1, a2, s, bin, bout) \
1N/A { mp_word w; \
1N/A w = ((mp_word)(a1)) - (a2) - (bin); \
1N/A s = ACCUM(w); \
1N/A bout = (w >> MP_DIGIT_BIT) & 1; }
1N/A
1N/A#else
1N/A/* NOTE,
1N/A * cin and cout could be the same variable.
1N/A * bin and bout could be the same variable.
1N/A * a1 or a2 and s could be the same variable.
1N/A * don't trash those outputs until their respective inputs have
1N/A * been read. */
1N/A#define MP_ADD_CARRY(a1, a2, s, cin, cout) \
1N/A { mp_digit tmp,sum; \
1N/A tmp = (a1); \
1N/A sum = tmp + (a2); \
1N/A tmp = (sum < tmp); /* detect overflow */ \
1N/A s = sum += (cin); \
1N/A cout = tmp + (sum < (cin)); }
1N/A
1N/A#define MP_SUB_BORROW(a1, a2, s, bin, bout) \
1N/A { mp_digit tmp; \
1N/A tmp = (a1); \
1N/A s = tmp - (a2); \
1N/A tmp = (s > tmp); /* detect borrow */ \
1N/A if ((bin) && !s--) tmp++; \
1N/A bout = tmp; }
1N/A#endif
1N/A
1N/A
1N/Astruct GFMethodStr;
1N/Atypedef struct GFMethodStr GFMethod;
1N/Astruct GFMethodStr {
1N/A /* Indicates whether the structure was constructed from dynamic memory
1N/A * or statically created. */
1N/A int constructed;
1N/A /* Irreducible that defines the field. For prime fields, this is the
1N/A * prime p. For binary polynomial fields, this is the bitstring
1N/A * representation of the irreducible polynomial. */
1N/A mp_int irr;
1N/A /* For prime fields, the value irr_arr[0] is the number of bits in the
1N/A * field. For binary polynomial fields, the irreducible polynomial
1N/A * f(t) is represented as an array of unsigned int[], where f(t) is
1N/A * of the form: f(t) = t^p[0] + t^p[1] + ... + t^p[4] where m = p[0]
1N/A * > p[1] > ... > p[4] = 0. */
1N/A unsigned int irr_arr[5];
1N/A /* Field arithmetic methods. All methods (except field_enc and
1N/A * field_dec) are assumed to take field-encoded parameters and return
1N/A * field-encoded values. All methods (except field_enc and field_dec)
1N/A * are required to be implemented. */
1N/A mp_err (*field_add) (const mp_int *a, const mp_int *b, mp_int *r,
1N/A const GFMethod *meth);
1N/A mp_err (*field_neg) (const mp_int *a, mp_int *r, const GFMethod *meth);
1N/A mp_err (*field_sub) (const mp_int *a, const mp_int *b, mp_int *r,
1N/A const GFMethod *meth);
1N/A mp_err (*field_mod) (const mp_int *a, mp_int *r, const GFMethod *meth);
1N/A mp_err (*field_mul) (const mp_int *a, const mp_int *b, mp_int *r,
1N/A const GFMethod *meth);
1N/A mp_err (*field_sqr) (const mp_int *a, mp_int *r, const GFMethod *meth);
1N/A mp_err (*field_div) (const mp_int *a, const mp_int *b, mp_int *r,
1N/A const GFMethod *meth);
1N/A mp_err (*field_enc) (const mp_int *a, mp_int *r, const GFMethod *meth);
1N/A mp_err (*field_dec) (const mp_int *a, mp_int *r, const GFMethod *meth);
1N/A /* Extra storage for implementation-specific data. Any memory
1N/A * allocated to these extra fields will be cleared by extra_free. */
1N/A void *extra1;
1N/A void *extra2;
1N/A void (*extra_free) (GFMethod *meth);
1N/A};
1N/A
1N/A/* Construct generic GFMethods. */
1N/AGFMethod *GFMethod_consGFp(const mp_int *irr);
1N/AGFMethod *GFMethod_consGFp_mont(const mp_int *irr);
1N/AGFMethod *GFMethod_consGF2m(const mp_int *irr,
1N/A const unsigned int irr_arr[5]);
1N/A/* Free the memory allocated (if any) to a GFMethod object. */
1N/Avoid GFMethod_free(GFMethod *meth);
1N/A
1N/Astruct ECGroupStr {
1N/A /* Indicates whether the structure was constructed from dynamic memory
1N/A * or statically created. */
1N/A int constructed;
1N/A /* Field definition and arithmetic. */
1N/A GFMethod *meth;
1N/A /* Textual representation of curve name, if any. */
1N/A char *text;
1N/A#ifdef _KERNEL
1N/A int text_len;
1N/A#endif
1N/A /* Curve parameters, field-encoded. */
1N/A mp_int curvea, curveb;
1N/A /* x and y coordinates of the base point, field-encoded. */
1N/A mp_int genx, geny;
1N/A /* Order and cofactor of the base point. */
1N/A mp_int order;
1N/A int cofactor;
1N/A /* Point arithmetic methods. All methods are assumed to take
1N/A * field-encoded parameters and return field-encoded values. All
1N/A * methods (except base_point_mul and points_mul) are required to be
1N/A * implemented. */
1N/A mp_err (*point_add) (const mp_int *px, const mp_int *py,
1N/A const mp_int *qx, const mp_int *qy, mp_int *rx,
1N/A mp_int *ry, const ECGroup *group);
1N/A mp_err (*point_sub) (const mp_int *px, const mp_int *py,
1N/A const mp_int *qx, const mp_int *qy, mp_int *rx,
1N/A mp_int *ry, const ECGroup *group);
1N/A mp_err (*point_dbl) (const mp_int *px, const mp_int *py, mp_int *rx,
1N/A mp_int *ry, const ECGroup *group);
1N/A mp_err (*point_mul) (const mp_int *n, const mp_int *px,
1N/A const mp_int *py, mp_int *rx, mp_int *ry,
1N/A const ECGroup *group);
1N/A mp_err (*base_point_mul) (const mp_int *n, mp_int *rx, mp_int *ry,
1N/A const ECGroup *group);
1N/A mp_err (*points_mul) (const mp_int *k1, const mp_int *k2,
1N/A const mp_int *px, const mp_int *py, mp_int *rx,
1N/A mp_int *ry, const ECGroup *group);
1N/A mp_err (*validate_point) (const mp_int *px, const mp_int *py, const ECGroup *group);
1N/A /* Extra storage for implementation-specific data. Any memory
1N/A * allocated to these extra fields will be cleared by extra_free. */
1N/A void *extra1;
1N/A void *extra2;
1N/A void (*extra_free) (ECGroup *group);
1N/A};
1N/A
1N/A/* Wrapper functions for generic prime field arithmetic. */
1N/Amp_err ec_GFp_add(const mp_int *a, const mp_int *b, mp_int *r,
1N/A const GFMethod *meth);
1N/Amp_err ec_GFp_neg(const mp_int *a, mp_int *r, const GFMethod *meth);
1N/Amp_err ec_GFp_sub(const mp_int *a, const mp_int *b, mp_int *r,
1N/A const GFMethod *meth);
1N/A
1N/A/* fixed length in-line adds. Count is in words */
1N/Amp_err ec_GFp_add_3(const mp_int *a, const mp_int *b, mp_int *r,
1N/A const GFMethod *meth);
1N/Amp_err ec_GFp_add_4(const mp_int *a, const mp_int *b, mp_int *r,
1N/A const GFMethod *meth);
1N/Amp_err ec_GFp_add_5(const mp_int *a, const mp_int *b, mp_int *r,
1N/A const GFMethod *meth);
1N/Amp_err ec_GFp_add_6(const mp_int *a, const mp_int *b, mp_int *r,
1N/A const GFMethod *meth);
1N/Amp_err ec_GFp_sub_3(const mp_int *a, const mp_int *b, mp_int *r,
1N/A const GFMethod *meth);
1N/Amp_err ec_GFp_sub_4(const mp_int *a, const mp_int *b, mp_int *r,
1N/A const GFMethod *meth);
1N/Amp_err ec_GFp_sub_5(const mp_int *a, const mp_int *b, mp_int *r,
1N/A const GFMethod *meth);
1N/Amp_err ec_GFp_sub_6(const mp_int *a, const mp_int *b, mp_int *r,
1N/A const GFMethod *meth);
1N/A
1N/Amp_err ec_GFp_mod(const mp_int *a, mp_int *r, const GFMethod *meth);
1N/Amp_err ec_GFp_mul(const mp_int *a, const mp_int *b, mp_int *r,
1N/A const GFMethod *meth);
1N/Amp_err ec_GFp_sqr(const mp_int *a, mp_int *r, const GFMethod *meth);
1N/Amp_err ec_GFp_div(const mp_int *a, const mp_int *b, mp_int *r,
1N/A const GFMethod *meth);
1N/A/* Wrapper functions for generic binary polynomial field arithmetic. */
1N/Amp_err ec_GF2m_add(const mp_int *a, const mp_int *b, mp_int *r,
1N/A const GFMethod *meth);
1N/Amp_err ec_GF2m_neg(const mp_int *a, mp_int *r, const GFMethod *meth);
1N/Amp_err ec_GF2m_mod(const mp_int *a, mp_int *r, const GFMethod *meth);
1N/Amp_err ec_GF2m_mul(const mp_int *a, const mp_int *b, mp_int *r,
1N/A const GFMethod *meth);
1N/Amp_err ec_GF2m_sqr(const mp_int *a, mp_int *r, const GFMethod *meth);
1N/Amp_err ec_GF2m_div(const mp_int *a, const mp_int *b, mp_int *r,
1N/A const GFMethod *meth);
1N/A
1N/A/* Montgomery prime field arithmetic. */
1N/Amp_err ec_GFp_mul_mont(const mp_int *a, const mp_int *b, mp_int *r,
1N/A const GFMethod *meth);
1N/Amp_err ec_GFp_sqr_mont(const mp_int *a, mp_int *r, const GFMethod *meth);
1N/Amp_err ec_GFp_div_mont(const mp_int *a, const mp_int *b, mp_int *r,
1N/A const GFMethod *meth);
1N/Amp_err ec_GFp_enc_mont(const mp_int *a, mp_int *r, const GFMethod *meth);
1N/Amp_err ec_GFp_dec_mont(const mp_int *a, mp_int *r, const GFMethod *meth);
1N/Avoid ec_GFp_extra_free_mont(GFMethod *meth);
1N/A
1N/A/* point multiplication */
1N/Amp_err ec_pts_mul_basic(const mp_int *k1, const mp_int *k2,
1N/A const mp_int *px, const mp_int *py, mp_int *rx,
1N/A mp_int *ry, const ECGroup *group);
1N/Amp_err ec_pts_mul_simul_w2(const mp_int *k1, const mp_int *k2,
1N/A const mp_int *px, const mp_int *py, mp_int *rx,
1N/A mp_int *ry, const ECGroup *group);
1N/A
1N/A/* Computes the windowed non-adjacent-form (NAF) of a scalar. Out should
1N/A * be an array of signed char's to output to, bitsize should be the number
1N/A * of bits of out, in is the original scalar, and w is the window size.
1N/A * NAF is discussed in the paper: D. Hankerson, J. Hernandez and A.
1N/A * Menezes, "Software implementation of elliptic curve cryptography over
1N/A * binary fields", Proc. CHES 2000. */
1N/Amp_err ec_compute_wNAF(signed char *out, int bitsize, const mp_int *in,
1N/A int w);
1N/A
1N/A/* Optimized field arithmetic */
1N/Amp_err ec_group_set_gfp192(ECGroup *group, ECCurveName);
1N/Amp_err ec_group_set_gfp224(ECGroup *group, ECCurveName);
1N/Amp_err ec_group_set_gfp256(ECGroup *group, ECCurveName);
1N/Amp_err ec_group_set_gfp384(ECGroup *group, ECCurveName);
1N/Amp_err ec_group_set_gfp521(ECGroup *group, ECCurveName);
1N/Amp_err ec_group_set_gf2m163(ECGroup *group, ECCurveName name);
1N/Amp_err ec_group_set_gf2m193(ECGroup *group, ECCurveName name);
1N/Amp_err ec_group_set_gf2m233(ECGroup *group, ECCurveName name);
1N/A
1N/A/* Optimized floating-point arithmetic */
1N/A#ifdef ECL_USE_FP
1N/Amp_err ec_group_set_secp160r1_fp(ECGroup *group);
1N/Amp_err ec_group_set_nistp192_fp(ECGroup *group);
1N/Amp_err ec_group_set_nistp224_fp(ECGroup *group);
1N/A#endif
1N/A
1N/A#endif /* _ECL_PRIV_H */