add a comment
[openssl.git] / crypto / ec / ecp_smpl.c
index be7f15f782bd8834320496130ebfde2b85360a38..267134af4b5e5c61593e49fb8ee883065ab60c29 100644 (file)
@@ -1,6 +1,8 @@
 /* crypto/ec/ecp_smpl.c */
 /* Includes code written by Lenka Fibikova <fibikova@exp-math.uni-essen.de>
- * for the OpenSSL project. */
+ * for the OpenSSL project. 
+ * Includes code written by Bodo Moeller for the OpenSSL project.
+*/
 /* ====================================================================
  * Copyright (c) 1998-2002 The OpenSSL Project.  All rights reserved.
  *
  * Hudson (tjh@cryptsoft.com).
  *
  */
+/* ====================================================================
+ * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
+ * Portions of this software developed by SUN MICROSYSTEMS, INC.,
+ * and contributed to the OpenSSL project.
+ */
 
 #include <openssl/err.h>
 #include <openssl/symhacks.h>
 const EC_METHOD *EC_GFp_simple_method(void)
        {
        static const EC_METHOD ret = {
+               NID_X9_62_prime_field,
                ec_GFp_simple_group_init,
                ec_GFp_simple_group_finish,
                ec_GFp_simple_group_clear_finish,
                ec_GFp_simple_group_copy,
-               ec_GFp_simple_group_set_curve_GFp,
-               ec_GFp_simple_group_get_curve_GFp,
+               ec_GFp_simple_group_set_curve,
+               ec_GFp_simple_group_get_curve,
+               ec_GFp_simple_group_get_degree,
                ec_GFp_simple_group_check_discriminant,
                ec_GFp_simple_point_init,
                ec_GFp_simple_point_finish,
@@ -77,14 +86,16 @@ const EC_METHOD *EC_GFp_simple_method(void)
                ec_GFp_simple_point_set_to_infinity,
                ec_GFp_simple_set_Jprojective_coordinates_GFp,
                ec_GFp_simple_get_Jprojective_coordinates_GFp,
-               ec_GFp_simple_point_set_affine_coordinates_GFp,
-               ec_GFp_simple_point_get_affine_coordinates_GFp,
-               ec_GFp_simple_set_compressed_coordinates_GFp,
+               ec_GFp_simple_point_set_affine_coordinates,
+               ec_GFp_simple_point_get_affine_coordinates,
+               ec_GFp_simple_set_compressed_coordinates,
                ec_GFp_simple_point2oct,
                ec_GFp_simple_oct2point,
                ec_GFp_simple_add,
                ec_GFp_simple_dbl,
                ec_GFp_simple_invert,
+               0 /* mul */,
+               0 /* precompute_mult */,
                ec_GFp_simple_is_at_infinity,
                ec_GFp_simple_is_on_curve,
                ec_GFp_simple_cmp,
@@ -92,6 +103,7 @@ const EC_METHOD *EC_GFp_simple_method(void)
                ec_GFp_simple_points_make_affine,
                ec_GFp_simple_field_mul,
                ec_GFp_simple_field_sqr,
+               0 /* field_div */,
                0 /* field_encode */,
                0 /* field_decode */,
                0 /* field_set_to_one */ };
@@ -100,6 +112,20 @@ const EC_METHOD *EC_GFp_simple_method(void)
        }
 
 
+/* Most method functions in this file are designed to work with
+ * non-trivial representations of field elements if necessary
+ * (see ecp_mont.c): while standard modular addition and subtraction
+ * are used, the field_mul and field_sqr methods will be used for
+ * multiplication, and field_encode and field_decode (if defined)
+ * will be used for converting between representations.
+
+ * Functions ec_GFp_simple_points_make_affine() and
+ * ec_GFp_simple_point_get_affine_coordinates() specifically assume
+ * that if a non-trivial representation is used, it is a Montgomery
+ * representation (i.e. 'encoding' means multiplying by some factor R).
+ */
+
+
 int ec_GFp_simple_group_init(EC_GROUP *group)
        {
        BN_init(&group->field);
@@ -138,7 +164,7 @@ int ec_GFp_simple_group_copy(EC_GROUP *dest, const EC_GROUP *src)
        }
 
 
-int ec_GFp_simple_group_set_curve_GFp(EC_GROUP *group,
+int ec_GFp_simple_group_set_curve(EC_GROUP *group,
        const BIGNUM *p, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
        {
        int ret = 0;
@@ -148,7 +174,7 @@ int ec_GFp_simple_group_set_curve_GFp(EC_GROUP *group,
        /* p must be a prime > 3 */
        if (BN_num_bits(p) <= 2 || !BN_is_odd(p))
                {
-               ECerr(EC_F_EC_GFP_SIMPLE_GROUP_SET_CURVE_GFP, EC_R_INVALID_FIELD);
+               ECerr(EC_F_EC_GFP_SIMPLE_GROUP_SET_CURVE, EC_R_INVALID_FIELD);
                return 0;
                }
 
@@ -165,7 +191,7 @@ int ec_GFp_simple_group_set_curve_GFp(EC_GROUP *group,
 
        /* group->field */
        if (!BN_copy(&group->field, p)) goto err;
-       group->field.neg = 0;
+       BN_set_sign(&group->field, 0);
 
        /* group->a */
        if (!BN_nnmod(tmp_a, a, p, ctx)) goto err;
@@ -193,7 +219,7 @@ int ec_GFp_simple_group_set_curve_GFp(EC_GROUP *group,
        }
 
 
-int ec_GFp_simple_group_get_curve_GFp(const EC_GROUP *group, BIGNUM *p, BIGNUM *a, BIGNUM *b, BN_CTX *ctx)
+int ec_GFp_simple_group_get_curve(const EC_GROUP *group, BIGNUM *p, BIGNUM *a, BIGNUM *b, BN_CTX *ctx)
        {
        int ret = 0;
        BN_CTX *new_ctx = NULL;
@@ -244,6 +270,12 @@ int ec_GFp_simple_group_get_curve_GFp(const EC_GROUP *group, BIGNUM *p, BIGNUM *
        }
 
 
+int ec_GFp_simple_group_get_degree(const EC_GROUP *group)
+       {
+       return BN_num_bits(&group->field);
+       }
+
+
 int ec_GFp_simple_group_check_discriminant(const EC_GROUP *group, BN_CTX *ctx)
        {
        int ret = 0;
@@ -469,13 +501,13 @@ int ec_GFp_simple_get_Jprojective_coordinates_GFp(const EC_GROUP *group, const E
        }
 
 
-int ec_GFp_simple_point_set_affine_coordinates_GFp(const EC_GROUP *group, EC_POINT *point,
+int ec_GFp_simple_point_set_affine_coordinates(const EC_GROUP *group, EC_POINT *point,
        const BIGNUM *x, const BIGNUM *y, BN_CTX *ctx)
        {
        if (x == NULL || y == NULL)
                {
                /* unlike for projective coordinates, we do not tolerate this */
-               ECerr(EC_F_EC_GFP_SIMPLE_POINT_SET_AFFINE_COORDINATES_GFP, ERR_R_PASSED_NULL_PARAMETER);
+               ECerr(EC_F_EC_GFP_SIMPLE_POINT_SET_AFFINE_COORDINATES, ERR_R_PASSED_NULL_PARAMETER);
                return 0;
                }
 
@@ -483,17 +515,17 @@ int ec_GFp_simple_point_set_affine_coordinates_GFp(const EC_GROUP *group, EC_POI
        }
 
 
-int ec_GFp_simple_point_get_affine_coordinates_GFp(const EC_GROUP *group, const EC_POINT *point,
+int ec_GFp_simple_point_get_affine_coordinates(const EC_GROUP *group, const EC_POINT *point,
        BIGNUM *x, BIGNUM *y, BN_CTX *ctx)
        {
        BN_CTX *new_ctx = NULL;
-       BIGNUM *X, *Y, *Z, *Z_1, *Z_2, *Z_3;
-       const BIGNUM *X_, *Y_, *Z_;
+       BIGNUM *Z, *Z_1, *Z_2, *Z_3;
+       const BIGNUM *Z_;
        int ret = 0;
 
        if (EC_POINT_is_at_infinity(group, point))
                {
-               ECerr(EC_F_EC_GFP_SIMPLE_POINT_GET_AFFINE_COORDINATES_GFP, EC_R_POINT_AT_INFINITY);
+               ECerr(EC_F_EC_GFP_SIMPLE_POINT_GET_AFFINE_COORDINATES, EC_R_POINT_AT_INFINITY);
                return 0;
                }
 
@@ -505,8 +537,6 @@ int ec_GFp_simple_point_get_affine_coordinates_GFp(const EC_GROUP *group, const
                }
 
        BN_CTX_start(ctx);
-       X = BN_CTX_get(ctx);
-       Y = BN_CTX_get(ctx);
        Z = BN_CTX_get(ctx);
        Z_1 = BN_CTX_get(ctx);
        Z_2 = BN_CTX_get(ctx);
@@ -517,34 +547,44 @@ int ec_GFp_simple_point_get_affine_coordinates_GFp(const EC_GROUP *group, const
        
        if (group->meth->field_decode)
                {
-               if (!group->meth->field_decode(group, X, &point->X, ctx)) goto err;
-               if (!group->meth->field_decode(group, Y, &point->Y, ctx)) goto err;
                if (!group->meth->field_decode(group, Z, &point->Z, ctx)) goto err;
-               X_ = X; Y_ = Y; Z_ = Z;
+               Z_ = Z;
                }
        else
                {
-               X_ = &point->X;
-               Y_ = &point->Y;
                Z_ = &point->Z;
                }
        
        if (BN_is_one(Z_))
                {
-               if (x != NULL)
+               if (group->meth->field_decode)
                        {
-                       if (!BN_copy(x, X_)) goto err;
+                       if (x != NULL)
+                               {
+                               if (!group->meth->field_decode(group, x, &point->X, ctx)) goto err;
+                               }
+                       if (y != NULL)
+                               {
+                               if (!group->meth->field_decode(group, y, &point->Y, ctx)) goto err;
+                               }
                        }
-               if (y != NULL)
+               else
                        {
-                       if (!BN_copy(y, Y_)) goto err;
+                       if (x != NULL)
+                               {
+                               if (!BN_copy(x, &point->X)) goto err;
+                               }
+                       if (y != NULL)
+                               {
+                               if (!BN_copy(y, &point->Y)) goto err;
+                               }
                        }
                }
        else
                {
                if (!BN_mod_inverse(Z_1, Z_, &group->field, ctx))
                        {
-                       ECerr(EC_F_EC_GFP_SIMPLE_POINT_GET_AFFINE_COORDINATES_GFP, ERR_R_BN_LIB);
+                       ECerr(EC_F_EC_GFP_SIMPLE_POINT_GET_AFFINE_COORDINATES, ERR_R_BN_LIB);
                        goto err;
                        }
                
@@ -560,15 +600,8 @@ int ec_GFp_simple_point_get_affine_coordinates_GFp(const EC_GROUP *group, const
        
                if (x != NULL)
                        {
-                       if (group->meth->field_encode == 0)
-                               {
-                               /* field_mul works on standard representation */
-                               if (!group->meth->field_mul(group, x, X_, Z_2, ctx)) goto err;
-                               }
-                       else
-                               {
-                               if (!BN_mod_mul(x, X_, Z_2, &group->field, ctx)) goto err;
-                               }
+                       /* in the Montgomery case, field_mul will cancel out Montgomery factor in X: */
+                       if (!group->meth->field_mul(group, x, &point->X, Z_2, ctx)) goto err;
                        }
 
                if (y != NULL)
@@ -577,14 +610,14 @@ int ec_GFp_simple_point_get_affine_coordinates_GFp(const EC_GROUP *group, const
                                {
                                /* field_mul works on standard representation */
                                if (!group->meth->field_mul(group, Z_3, Z_2, Z_1, ctx)) goto err;
-                               if (!group->meth->field_mul(group, y, Y_, Z_3, ctx)) goto err;
-                               
                                }
                        else
                                {
                                if (!BN_mod_mul(Z_3, Z_2, Z_1, &group->field, ctx)) goto err;
-                               if (!BN_mod_mul(y, Y_, Z_3, &group->field, ctx)) goto err;
                                }
+
+                       /* in the Montgomery case, field_mul will cancel out Montgomery factor in Y: */
+                       if (!group->meth->field_mul(group, y, &point->Y, Z_3, ctx)) goto err;
                        }
                }
 
@@ -598,7 +631,7 @@ int ec_GFp_simple_point_get_affine_coordinates_GFp(const EC_GROUP *group, const
        }
 
 
-int ec_GFp_simple_set_compressed_coordinates_GFp(const EC_GROUP *group, EC_POINT *point,
+int ec_GFp_simple_set_compressed_coordinates(const EC_GROUP *group, EC_POINT *point,
        const BIGNUM *x_, int y_bit, BN_CTX *ctx)
        {
        BN_CTX *new_ctx = NULL;
@@ -681,14 +714,12 @@ int ec_GFp_simple_set_compressed_coordinates_GFp(const EC_GROUP *group, EC_POINT
                if (ERR_GET_LIB(err) == ERR_LIB_BN && ERR_GET_REASON(err) == BN_R_NOT_A_SQUARE)
                        {
                        (void)ERR_get_error();
-                       ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES_GFP, EC_R_INVALID_COMPRESSED_POINT);
+                       ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES, EC_R_INVALID_COMPRESSED_POINT);
                        }
                else
-                       ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES_GFP, ERR_R_BN_LIB);
+                       ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES, ERR_R_BN_LIB);
                goto err;
                }
-       /* If tmp1 is not a square (i.e. there is no point on the curve with
-        * our x), then y now is a nonsense value too */
 
        if (y_bit != BN_is_odd(y))
                {
@@ -700,16 +731,17 @@ int ec_GFp_simple_set_compressed_coordinates_GFp(const EC_GROUP *group, EC_POINT
                        if (kron == -2) goto err;
 
                        if (kron == 1)
-                               ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES_GFP, EC_R_INVALID_COMPRESSION_BIT);
+                               ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES, EC_R_INVALID_COMPRESSION_BIT);
                        else
-                               ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES_GFP, EC_R_INVALID_COMPRESSED_POINT);
+                               /* BN_mod_sqrt() should have cought this error (not a square) */
+                               ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES, EC_R_INVALID_COMPRESSED_POINT);
                        goto err;
                        }
                if (!BN_usub(y, &group->field, y)) goto err;
                }
        if (y_bit != BN_is_odd(y))
                {
-               ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES_GFP, ERR_R_INTERNAL_ERROR);
+               ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES, ERR_R_INTERNAL_ERROR);
                goto err;
                }