add a comment
[openssl.git] / crypto / ec / ecp_smpl.c
index 8e062dc95126e9c5e610855d4fb004f898172dc8..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>
 
 #include "ec_lcl.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_generator,
-               ec_GFp_simple_group_get0_generator,
-               ec_GFp_simple_group_get_order,
-               ec_GFp_simple_group_get_cofactor,
-               ec_GFp_simple_group_check,
+               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,
                ec_GFp_simple_point_clear_finish,
@@ -81,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,
@@ -96,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 */ };
@@ -104,15 +112,26 @@ 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);
        BN_init(&group->a);
        BN_init(&group->b);
        group->a_is_minus3 = 0;
-       group->generator = NULL;
-       BN_init(&group->order);
-       BN_init(&group->cofactor);
        return 1;
        }
 
@@ -122,10 +141,6 @@ void ec_GFp_simple_group_finish(EC_GROUP *group)
        BN_free(&group->field);
        BN_free(&group->a);
        BN_free(&group->b);
-       if (group->generator != NULL)
-               EC_POINT_free(group->generator);
-       BN_free(&group->order);
-       BN_free(&group->cofactor);
        }
 
 
@@ -134,13 +149,6 @@ void ec_GFp_simple_group_clear_finish(EC_GROUP *group)
        BN_clear_free(&group->field);
        BN_clear_free(&group->a);
        BN_clear_free(&group->b);
-       if (group->generator != NULL)
-               {
-               EC_POINT_clear_free(group->generator);
-               group->generator = NULL;
-               }
-       BN_clear_free(&group->order);
-       BN_clear_free(&group->cofactor);
        }
 
 
@@ -152,33 +160,11 @@ int ec_GFp_simple_group_copy(EC_GROUP *dest, const EC_GROUP *src)
 
        dest->a_is_minus3 = src->a_is_minus3;
 
-       if (src->generator != NULL)
-               {
-               if (dest->generator == NULL)
-                       {
-                       dest->generator = EC_POINT_new(dest);
-                       if (dest->generator == NULL) return 0;
-                       }
-               if (!EC_POINT_copy(dest->generator, src->generator)) return 0;
-               }
-       else
-               {
-               /* src->generator == NULL */
-               if (dest->generator != NULL)
-                       {
-                       EC_POINT_clear_free(dest->generator);
-                       dest->generator = NULL;
-                       }
-               }
-
-       if (!BN_copy(&dest->order, &src->order)) return 0;
-       if (!BN_copy(&dest->cofactor, &src->cofactor)) return 0;
-
        return 1;
        }
 
 
-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;
@@ -188,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;
                }
 
@@ -205,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;
@@ -233,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;
@@ -284,75 +270,25 @@ int ec_GFp_simple_group_get_curve_GFp(const EC_GROUP *group, BIGNUM *p, BIGNUM *
        }
 
 
-
-int ec_GFp_simple_group_set_generator(EC_GROUP *group, const EC_POINT *generator,
-       const BIGNUM *order, const BIGNUM *cofactor)
-       {
-       if (generator == NULL)
-               {
-               ECerr(EC_F_EC_GFP_SIMPLE_GROUP_SET_GENERATOR, ERR_R_PASSED_NULL_PARAMETER);
-               return 0   ;
-               }
-
-       if (group->generator == NULL)
-               {
-               group->generator = EC_POINT_new(group);
-               if (group->generator == NULL) return 0;
-               }
-       if (!EC_POINT_copy(group->generator, generator)) return 0;
-
-       if (order != NULL)
-               { if (!BN_copy(&group->order, order)) return 0; }       
-       else
-               { if (!BN_zero(&group->order)) return 0; }      
-
-       if (cofactor != NULL)
-               { if (!BN_copy(&group->cofactor, cofactor)) return 0; } 
-       else
-               { if (!BN_zero(&group->cofactor)) return 0; }   
-
-       return 1;
-       }
-
-
-EC_POINT *ec_GFp_simple_group_get0_generator(const EC_GROUP *group)
-       {
-       return group->generator;
-       }
-
-
-int ec_GFp_simple_group_get_order(const EC_GROUP *group, BIGNUM *order, BN_CTX *ctx)
-       {
-       if (!BN_copy(order, &group->order))
-               return 0;
-
-       return !BN_is_zero(&group->order);
-       }
-
-
-int ec_GFp_simple_group_get_cofactor(const EC_GROUP *group, BIGNUM *cofactor, BN_CTX *ctx)
+int ec_GFp_simple_group_get_degree(const EC_GROUP *group)
        {
-       if (!BN_copy(cofactor, &group->cofactor))
-               return 0;
-
-       return !BN_is_zero(&group->cofactor);
+       return BN_num_bits(&group->field);
        }
 
 
-int ec_GFp_simple_group_check(const EC_GROUP *group, BN_CTX *ctx)
+int ec_GFp_simple_group_check_discriminant(const EC_GROUP *group, BN_CTX *ctx)
        {
        int ret = 0;
        BIGNUM *a,*b,*order,*tmp_1,*tmp_2;
        const BIGNUM *p = &group->field;
        BN_CTX *new_ctx = NULL;
-       EC_POINT *point = NULL;
 
        if (ctx == NULL)
                {
                ctx = new_ctx = BN_CTX_new();
                if (ctx == NULL)
                        {
-                       ECerr(EC_F_EC_GFP_SIMPLE_GROUP_CHECK, ERR_R_MALLOC_FAILURE);
+                       ECerr(EC_F_EC_GFP_SIMPLE_GROUP_CHECK_DISCRIMINANT, ERR_R_MALLOC_FAILURE);
                        goto err;
                        }
                }
@@ -380,11 +316,7 @@ int ec_GFp_simple_group_check(const EC_GROUP *group, BN_CTX *ctx)
          * 0 =< a, b < p */
        if (BN_is_zero(a))
                {
-               if (BN_is_zero(b))
-                       {
-                       ECerr(EC_F_EC_GFP_SIMPLE_GROUP_CHECK, EC_R_DISCRIMINANT_IS_ZERO);
-                       goto err;
-                       }
+               if (BN_is_zero(b)) goto err;
                }
        else if (!BN_is_zero(b))
                {
@@ -398,49 +330,14 @@ int ec_GFp_simple_group_check(const EC_GROUP *group, BN_CTX *ctx)
                /* tmp_2 = 27*b^2 */
 
                if (!BN_mod_add(a, tmp_1, tmp_2, p, ctx)) goto err;
-               if (BN_is_zero(a))
-                       {
-                       ECerr(EC_F_EC_GFP_SIMPLE_GROUP_CHECK, EC_R_DISCRIMINANT_IS_ZERO);
-                       goto err;
-                       }
+               if (BN_is_zero(a)) goto err;
                }
-       
-       /* check the generator */
-       if (group->generator == NULL)
-               {
-               ECerr(EC_F_EC_GFP_SIMPLE_GROUP_CHECK, EC_R_UNDEFINED_GENERATOR);
-               goto err;
-               }
-       if (!ec_GFp_simple_is_on_curve(group, group->generator, ctx))
-               {
-               ECerr(EC_F_EC_GFP_SIMPLE_GROUP_CHECK, EC_R_POINT_IS_NOT_ON_CURVE);
-               goto err;
-               }
-
-       /* check the order of the generator */
-       if ((point = EC_POINT_new(group)) == NULL) goto err;
-       if (!EC_GROUP_get_order(group, order, ctx)) goto err; 
-       if (BN_is_zero(order))
-               {
-               ECerr(EC_F_EC_GFP_SIMPLE_GROUP_CHECK, EC_R_UNDEFINED_ORDER);
-               goto err;
-               }
-       
-       if (!EC_POINT_mul(group, point, order, NULL, NULL, ctx)) goto err;
-       if (!EC_POINT_is_at_infinity(group, point))
-               {
-               ECerr(EC_F_EC_GFP_SIMPLE_GROUP_CHECK, EC_R_INVALID_GROUP_ORDER);
-               goto err;
-               }
-
        ret = 1;
 
 err:
        BN_CTX_end(ctx);
        if (new_ctx != NULL)
                BN_CTX_free(new_ctx);
-       if (point)
-               EC_POINT_free(point);
        return ret;
        }
 
@@ -604,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;
                }
 
@@ -618,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;
                }
 
@@ -640,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);
@@ -652,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;
                        }
                
@@ -695,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)
@@ -712,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;
                        }
                }
 
@@ -733,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;
@@ -816,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))
                {
@@ -835,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;
                }