add a comment
[openssl.git] / crypto / ec / ecp_smpl.c
index 70fed912a23e1a018a55425584408879cca724a0..267134af4b5e5c61593e49fb8ee883065ab60c29 100644 (file)
@@ -112,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);
@@ -177,7 +191,7 @@ int ec_GFp_simple_group_set_curve(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;
@@ -505,8 +519,8 @@ int ec_GFp_simple_point_get_affine_coordinates(const EC_GROUP *group, const EC_P
        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))
@@ -523,8 +537,6 @@ int ec_GFp_simple_point_get_affine_coordinates(const EC_GROUP *group, const EC_P
                }
 
        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);
@@ -535,27 +547,37 @@ int ec_GFp_simple_point_get_affine_coordinates(const EC_GROUP *group, const EC_P
        
        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
@@ -578,15 +600,8 @@ int ec_GFp_simple_point_get_affine_coordinates(const EC_GROUP *group, const EC_P
        
                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)
@@ -595,14 +610,14 @@ int ec_GFp_simple_point_get_affine_coordinates(const EC_GROUP *group, const EC_P
                                {
                                /* 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;
                        }
                }
 
@@ -705,8 +720,6 @@ int ec_GFp_simple_set_compressed_coordinates(const EC_GROUP *group, EC_POINT *po
                        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))
                {
@@ -720,6 +733,7 @@ int ec_GFp_simple_set_compressed_coordinates(const EC_GROUP *group, EC_POINT *po
                        if (kron == 1)
                                ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES, EC_R_INVALID_COMPRESSION_BIT);
                        else
+                               /* 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;
                        }