ECPublicKey_set_octet_string and ECPublicKey_get_octet_string
[openssl.git] / crypto / ec / ec_asn1.c
index 26f3916d014d84c6317ba366b94b15f997660664..927a3716cff251e439e5154d96acfc3ccf9fb5bd 100644 (file)
@@ -3,7 +3,7 @@
  * Written by Nils Larsch for the OpenSSL project.
  */
 /* ====================================================================
- * Copyright (c) 2000-2002 The OpenSSL Project.  All rights reserved.
+ * Copyright (c) 2000-2003 The OpenSSL Project.  All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
 #include <openssl/asn1t.h>
 #include <openssl/objects.h>
 
+
+int EC_GROUP_get_basis_type(const EC_GROUP *group)
+       {
+       int i=0;
+
+       if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) !=
+               NID_X9_62_characteristic_two_field)
+               /* everything else is currently not supported */
+               return 0;
+
+       while (group->poly[i] != 0)
+               i++;
+
+       if (i == 4)
+               return NID_X9_62_ppBasis;
+       else if (i == 2)
+               return NID_X9_62_tpBasis;
+       else
+               /* everything else is currently not supported */
+               return 0;
+       }
+
+int EC_GROUP_get_trinomial_basis(const EC_GROUP *group, unsigned int *k)
+       {
+       if (group == NULL)
+               return 0;
+
+       if (EC_GROUP_method_of(group)->group_set_curve != ec_GF2m_simple_group_set_curve
+           || !((group->poly[0] != 0) && (group->poly[1] != 0) && (group->poly[2] == 0)))
+               {
+               ECerr(EC_F_EC_GROUP_GET_TRINOMIAL_BASIS, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
+               return 0;
+               }
+
+       if (k)
+               *k = group->poly[1];
+
+       return 1;
+       }
+
+int EC_GROUP_get_pentanomial_basis(const EC_GROUP *group, unsigned int *k1,
+       unsigned int *k2, unsigned int *k3)
+       {
+       if (group == NULL)
+               return 0;
+
+       if (EC_GROUP_method_of(group)->group_set_curve != ec_GF2m_simple_group_set_curve
+           || !((group->poly[0] != 0) && (group->poly[1] != 0) && (group->poly[2] != 0) && (group->poly[3] != 0) && (group->poly[4] == 0)))
+               {
+               ECerr(EC_F_EC_GROUP_GET_PENTANOMIAL_BASIS, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
+               return 0;
+               }
+
+       if (k1)
+               *k1 = group->poly[3];
+       if (k2)
+               *k2 = group->poly[2];
+       if (k3)
+               *k3 = group->poly[1];
+
+       return 1;
+       }
+
+
+
 /* some structures needed for the asn1 encoding */
 typedef struct x9_62_fieldid_st {
         ASN1_OBJECT *fieldType;
@@ -159,7 +224,7 @@ ASN1_SEQUENCE(ECPARAMETERS) = {
        ASN1_SIMPLE(ECPARAMETERS, curve, X9_62_CURVE),
        ASN1_SIMPLE(ECPARAMETERS, base, ASN1_OCTET_STRING),
        ASN1_SIMPLE(ECPARAMETERS, order, ASN1_INTEGER),
-       ASN1_SIMPLE(ECPARAMETERS, cofactor, ASN1_INTEGER)
+       ASN1_OPT(ECPARAMETERS, cofactor, ASN1_INTEGER)
 } ASN1_SEQUENCE_END(ECPARAMETERS)
 
 DECLARE_ASN1_FUNCTIONS_const(ECPARAMETERS)
@@ -281,7 +346,6 @@ static X9_62_FIELDID *ec_asn1_group2field(const EC_GROUP *group,
        else    /* nid == NID_X9_62_characteristic_two_field */
                {
                int             field_type;
-               unsigned int    k1, k2, k3;
 
                char_two = X9_62_CHARACTERISTIC_TWO_new();
                if (char_two == NULL)
@@ -292,7 +356,7 @@ static X9_62_FIELDID *ec_asn1_group2field(const EC_GROUP *group,
        
                char_two->m = (long)EC_GROUP_get_degree(group);
 
-               field_type = EC_GROUP_get_basis_type(group, &k1, &k2, &k3);
+               field_type = EC_GROUP_get_basis_type(group);
 
                if (field_type == 0)
                        {
@@ -308,6 +372,11 @@ static X9_62_FIELDID *ec_asn1_group2field(const EC_GROUP *group,
 
                if (field_type == NID_X9_62_tpBasis)
                        {
+                       unsigned int k;
+
+                       if (!EC_GROUP_get_trinomial_basis(group, &k))
+                               goto err;
+
                        char_two->parameters->type = V_ASN1_INTEGER;
                        char_two->parameters->value.integer = 
                                ASN1_INTEGER_new();
@@ -317,7 +386,7 @@ static X9_62_FIELDID *ec_asn1_group2field(const EC_GROUP *group,
                                        ERR_R_ASN1_LIB);
                                goto err;
                                }
-                       if (!ASN1_INTEGER_set(char_two->parameters->value.integer, (long)k1))
+                       if (!ASN1_INTEGER_set(char_two->parameters->value.integer, (long)k))
                                {
                                ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS,
                                        ERR_R_ASN1_LIB);
@@ -326,6 +395,11 @@ static X9_62_FIELDID *ec_asn1_group2field(const EC_GROUP *group,
                        }
                else if (field_type == NID_X9_62_ppBasis)
                        {
+                       unsigned int k1, k2, k3;
+
+                       if (!EC_GROUP_get_pentanomial_basis(group, &k1, &k2, &k3))
+                               goto err;
+
                        penta = X9_62_PENTANOMIAL_new();
                        /* set k? values */
                        penta->k1 = (long)k1;
@@ -641,17 +715,15 @@ static ECPARAMETERS *ec_asn1_group2parameters(const EC_GROUP *group,
                goto err;
                }
 
-       /* set the cofactor */
-       if (!EC_GROUP_get_cofactor(group, tmp, NULL))
-               {
-               ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
-               goto err;
-               }
-       ret->cofactor = BN_to_ASN1_INTEGER(tmp, ret->cofactor);
-       if (ret->cofactor == NULL)
+       /* set the cofactor (optional) */
+       if (EC_GROUP_get_cofactor(group, tmp, NULL))
                {
-               ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_ASN1_LIB);
-               goto err;
+               ret->cofactor = BN_to_ASN1_INTEGER(tmp, ret->cofactor);
+               if (ret->cofactor == NULL)
+                       {
+                       ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_ASN1_LIB);
+                       goto err;
+                       }
                }
 
        ok = 1;
@@ -904,9 +976,7 @@ static EC_GROUP *ec_asn1_parameters2group(const ECPARAMETERS *params)
                ret->seed_len = params->curve->seed->length;
                }
 
-       /* extract the order, cofactor and generator */
-       if (!params->order || !params->cofactor || !params->base ||
-           !params->base->data)
+       if (!params->order || !params->base || !params->base->data)
                {
                ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
                goto err;
@@ -914,14 +984,11 @@ static EC_GROUP *ec_asn1_parameters2group(const ECPARAMETERS *params)
 
        if ((point = EC_POINT_new(ret)) == NULL) goto err;
 
-       a = ASN1_INTEGER_to_BN(params->order, a);
-       b = ASN1_INTEGER_to_BN(params->cofactor, b);
-       if (!a || !b)
-               {
-               ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_ASN1_LIB);
-               goto err;
-               }
+       /* set the point conversion form */
+       EC_GROUP_set_point_conversion_form(ret, (point_conversion_form_t)
+                               (params->base->data[0] & ~0x01));
 
+       /* extract the ec point */
        if (!EC_POINT_oct2point(ret, point, params->base->data, 
                                params->base->length, NULL))
                {
@@ -929,10 +996,29 @@ static EC_GROUP *ec_asn1_parameters2group(const ECPARAMETERS *params)
                goto err;
                }
 
-       /* set the point conversion form */
-       EC_GROUP_set_point_conversion_form(ret, (point_conversion_form_t)
-                               (params->base->data[0] & ~0x01));
-
+       /* extract the order */
+       if ((a = ASN1_INTEGER_to_BN(params->order, a)) == NULL)
+               {
+               ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_ASN1_LIB);
+               goto err;
+               }
+       
+       /* extract the cofactor (optional) */
+       if (params->cofactor == NULL)
+               {
+               if (b)
+                       {
+                       BN_free(b);
+                       b = NULL;
+                       }
+               }
+       else
+               if ((b = ASN1_INTEGER_to_BN(params->cofactor, b)) == NULL)
+                       {
+                       ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_ASN1_LIB);
+                       goto err;
+                       }
+       /* set the generator, order and cofactor (if present) */
        if (!EC_GROUP_set_generator(ret, point, a, b))
                {
                ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB);
@@ -1320,8 +1406,7 @@ EC_KEY *d2i_ECParameters(EC_KEY **a, const unsigned char **in, long len)
        return ret;
        }
 
-EC_KEY *ECPublicKey_set_octet_string(EC_KEY **a, const unsigned char **in, 
-                                       long len)
+EC_KEY *o2i_ECPublicKey(EC_KEY **a, const unsigned char **in, long len)
        {
        EC_KEY *ret=NULL;
 
@@ -1329,33 +1414,34 @@ EC_KEY *ECPublicKey_set_octet_string(EC_KEY **a, const unsigned char **in,
                {
                /* sorry, but a EC_GROUP-structur is necessary
                  * to set the public key */
-               ECerr(EC_F_ECPUBLICKEY_SET_OCTET, ERR_R_PASSED_NULL_PARAMETER);
+               ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_PASSED_NULL_PARAMETER);
                return 0;
                }
        ret = *a;
        if (ret->pub_key == NULL && 
                (ret->pub_key = EC_POINT_new(ret->group)) == NULL)
                {
-               ECerr(EC_F_ECPUBLICKEY_SET_OCTET, ERR_R_MALLOC_FAILURE);
+               ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_MALLOC_FAILURE);
                return 0;
                }
        if (!EC_POINT_oct2point(ret->group, ret->pub_key, *in, len, NULL))
                {
-               ECerr(EC_F_ECPUBLICKEY_SET_OCTET, ERR_R_EC_LIB);
+               ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_EC_LIB);
                return 0;
                }
        /* save the point conversion form */
        ret->conv_form = (point_conversion_form_t)(*in[0] & ~0x01);
+       *in += len;
        return ret;
        }
 
-int ECPublicKey_get_octet_string(EC_KEY *a, unsigned char **out)
+int i2o_ECPublicKey(EC_KEY *a, unsigned char **out)
        {
         size_t  buf_len=0;
 
         if (a == NULL) 
                {
-               ECerr(EC_F_ECPUBLICKEY_GET_OCTET, ERR_R_PASSED_NULL_PARAMETER);
+               ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_PASSED_NULL_PARAMETER);
                return 0;
                }
 
@@ -1369,17 +1455,17 @@ int ECPublicKey_get_octet_string(EC_KEY *a, unsigned char **out)
        if (*out == NULL)
                if ((*out = OPENSSL_malloc(buf_len)) == NULL)
                        {
-                       ECerr(EC_F_ECPUBLICKEY_GET_OCTET, 
-                               ERR_R_MALLOC_FAILURE);
+                       ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_MALLOC_FAILURE);
                        return 0;
                        }
         if (!EC_POINT_point2oct(a->group, a->pub_key, a->conv_form,
                                *out, buf_len, NULL))
                {
-               ECerr(EC_F_ECPUBLICKEY_GET_OCTET, ERR_R_EC_LIB);
+               ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_EC_LIB);
                OPENSSL_free(*out);
                *out = NULL;
                return 0;
                }
+       *out += buf_len;
        return buf_len;
        }