]> www.wagner.pp.ru Git - openssl-gost/engine.git/commitdiff
Add TLS 1.3 EC point encoding for key_share ext.
authorigrkir <i.kirillov@kryptonite.ru>
Mon, 27 Sep 2021 06:58:07 +0000 (09:58 +0300)
committerDmitry Belyavskiy <beldmit@users.noreply.github.com>
Wed, 29 Dec 2021 12:16:50 +0000 (15:16 +0300)
reverse byte order

e_gost_err.c
e_gost_err.h
gost_ameth.c
gost_ec_sign.c
gost_lcl.h

index 4396e002f97607ab0b3625a1ddc2ff9e4c35c35c..2a8320705f8048c5204cf41afdf75c16e6509ca1 100644 (file)
@@ -36,7 +36,10 @@ static ERR_STRING_DATA GOST_str_functs[] = {
     {ERR_PACK(0, GOST_F_GOST_CMS_SET_SHARED_INFO, 0),
      "gost_cms_set_shared_info"},
     {ERR_PACK(0, GOST_F_GOST_EC_COMPUTE_PUBLIC, 0), "gost_ec_compute_public"},
+    {ERR_PACK(0, GOST_F_GOST_EC_KEY2BUF, 0), "gost_ec_key2buf"},
     {ERR_PACK(0, GOST_F_GOST_EC_KEYGEN, 0), "gost_ec_keygen"},
+    {ERR_PACK(0, GOST_F_GOST_EC_OCT2POINT, 0), "gost_ec_oct2point"},
+    {ERR_PACK(0, GOST_F_GOST_EC_POINT2OCT, 0), "gost_ec_point2oct"},
     {ERR_PACK(0, GOST_F_GOST_EC_SIGN, 0), "gost_ec_sign"},
     {ERR_PACK(0, GOST_F_GOST_EC_VERIFY, 0), "gost_ec_verify"},
     {ERR_PACK(0, GOST_F_GOST_ENCODE_CMS_PARAMS, 0), "gost_encode_cms_params"},
@@ -88,6 +91,10 @@ static ERR_STRING_DATA GOST_str_functs[] = {
     {ERR_PACK(0, GOST_F_PKEY_GOST_DECRYPT, 0), "pkey_gost_decrypt"},
     {ERR_PACK(0, GOST_F_PKEY_GOST_ECCP_DECRYPT, 0), "pkey_GOST_ECcp_decrypt"},
     {ERR_PACK(0, GOST_F_PKEY_GOST_ECCP_ENCRYPT, 0), "pkey_GOST_ECcp_encrypt"},
+    {ERR_PACK(0, GOST_F_PKEY_GOST_EC_2020_DERIVE, 0),
+     "pkey_gost_ec_2020_derive"},
+    {ERR_PACK(0, GOST_F_PKEY_GOST_EC_COMPUTE_KEY, 0),
+     "pkey_gost_ec_compute_key"},
     {ERR_PACK(0, GOST_F_PKEY_GOST_EC_CTRL_STR_256, 0),
      "pkey_gost_ec_ctrl_str_256"},
     {ERR_PACK(0, GOST_F_PKEY_GOST_EC_CTRL_STR_512, 0),
index 018e4a42de4889dc879442adf97b61b60bf0ed41..19f856cfc335b44983a6c1f3d411d5ea85cacd5c 100644 (file)
@@ -42,7 +42,10 @@ void ERR_GOST_error(int function, int reason, char *file, int line);
 # define GOST_F_GOST_CMS_SET_KTRI_SHARED_INFO             157
 # define GOST_F_GOST_CMS_SET_SHARED_INFO                  155
 # define GOST_F_GOST_EC_COMPUTE_PUBLIC                    107
+# define GOST_F_GOST_EC_KEY2BUF                           174
 # define GOST_F_GOST_EC_KEYGEN                            108
+# define GOST_F_GOST_EC_OCT2POINT                         175
+# define GOST_F_GOST_EC_POINT2OCT                         176
 # define GOST_F_GOST_EC_SIGN                              109
 # define GOST_F_GOST_EC_VERIFY                            110
 # define GOST_F_GOST_ENCODE_CMS_PARAMS                    161
@@ -84,6 +87,8 @@ void ERR_GOST_error(int function, int reason, char *file, int line);
 # define GOST_F_PKEY_GOST_DECRYPT                         153
 # define GOST_F_PKEY_GOST_ECCP_DECRYPT                    123
 # define GOST_F_PKEY_GOST_ECCP_ENCRYPT                    124
+# define GOST_F_PKEY_GOST_EC_2020_DERIVE                  177
+# define GOST_F_PKEY_GOST_EC_COMPUTE_KEY                  178
 # define GOST_F_PKEY_GOST_EC_CTRL_STR_256                 125
 # define GOST_F_PKEY_GOST_EC_CTRL_STR_512                 126
 # define GOST_F_PKEY_GOST_EC_CTRL_STR_COMMON              154
index c60ce0acb9e6a2a38db2089e8e55fd439bd59f13..b8998417446ecda8c9d64514841e3d00b8454cff 100644 (file)
@@ -531,6 +531,12 @@ static int pkey_ctrl_gost(EVP_PKEY *pkey, int op, long arg1, void *arg2)
     case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
         *(int *)arg2 = md_nid;
         return 2;
+
+    case ASN1_PKEY_CTRL_SET1_TLS_ENCPT:
+        return gost_ec_oct2key((EC_KEY *)EVP_PKEY_get0(pkey), arg2, arg1);
+
+    case ASN1_PKEY_CTRL_GET1_TLS_ENCPT:
+        return gost_ec_key2buf((EC_KEY *)EVP_PKEY_get0(pkey), arg2);
     }
 
     return -2;
index e4800a461d1452d77991619010567fbef6ad3875..f7b8966d01801b4b057b7e80f1d31e5f85a77171 100644 (file)
@@ -613,3 +613,174 @@ int gost_ec_keygen(EC_KEY *ec)
 
     return (ok) ? gost_ec_compute_public(ec) : 0;
 }
+
+int gost_ec_oct2point(const EC_GROUP *group, EC_POINT *point,
+                      const unsigned char *buf, size_t len)
+{
+    BN_CTX *ctx = NULL;
+    BIGNUM *x, *y, *p;
+    size_t field_len, enc_len;
+    int ret = 0;
+    
+    if (len == 0) {
+        GOSTerr(GOST_F_GOST_EC_OCT2POINT, EC_R_BUFFER_TOO_SMALL);
+        return 0;
+    }
+
+    field_len = (EC_GROUP_get_degree(group) + 7) / 8;
+    enc_len = 2 * field_len;
+
+    if (len != enc_len) {
+        GOSTerr(GOST_F_GOST_EC_OCT2POINT, EC_R_INVALID_ENCODING);
+        return 0;
+    }
+
+    ctx = BN_CTX_new();
+    if (ctx == NULL)
+        return 0;
+
+    BN_CTX_start(ctx);
+    x = BN_CTX_get(ctx);
+    y = BN_CTX_get(ctx);
+    p = BN_CTX_get(ctx);
+    if (y == NULL)
+        goto err;
+
+    if (!EC_GROUP_get_curve(group, p, NULL, NULL, NULL))
+        goto err;
+
+    if (!BN_lebin2bn(buf, field_len, x))
+        goto err;
+    if (BN_ucmp(x, p) >= 0) {
+        GOSTerr(GOST_F_GOST_EC_OCT2POINT, EC_R_INVALID_ENCODING);
+        goto err;
+    }
+
+    if (!BN_lebin2bn(buf + field_len, field_len, y))
+        goto err;
+    if (BN_ucmp(y, p) >= 0) {
+        GOSTerr(GOST_F_GOST_EC_OCT2POINT, EC_R_INVALID_ENCODING);
+        goto err;
+    }
+
+    /*
+    * EC_POINT_set_affine_coordinates is responsible for checking that
+    * the point is on the curve.
+    */
+    if (!EC_POINT_set_affine_coordinates(group, point, x, y, ctx))
+        goto err;
+
+    ret = 1;
+
+ err:
+    BN_CTX_end(ctx);
+    BN_CTX_free(ctx);
+    return ret;
+}
+
+
+size_t gost_ec_point2oct(const EC_GROUP *group, const EC_POINT *point,
+                         unsigned char *buf, size_t len)
+{
+    size_t ret;
+    BN_CTX *ctx = NULL;
+    int used_ctx = 0;
+    BIGNUM *x, *y;
+    size_t field_len;
+
+    field_len = (EC_GROUP_get_degree(group) + 7) / 8;
+    ret = 2 * field_len;
+
+    if (buf != NULL) {
+        if (len < ret) {
+            GOSTerr(GOST_F_GOST_EC_POINT2OCT, EC_R_BUFFER_TOO_SMALL);
+            goto err;
+        }
+
+        ctx = BN_CTX_new();
+        if (ctx == NULL)
+            return 0;
+
+        BN_CTX_start(ctx);
+        used_ctx = 1;
+        x = BN_CTX_get(ctx);
+        y = BN_CTX_get(ctx);
+        if (y == NULL)
+            goto err;
+
+        if (!EC_POINT_get_affine_coordinates(group, point, x, y, ctx))
+            goto err;
+
+        if (BN_bn2lebinpad(x, buf, field_len) != field_len
+            || BN_bn2lebinpad(y, buf + field_len, field_len) != field_len)
+        goto err;
+    }
+
+    if (used_ctx)
+        BN_CTX_end(ctx);
+    BN_CTX_free(ctx);
+    return ret;
+    
+  err:
+    if (used_ctx)
+        BN_CTX_end(ctx);
+    BN_CTX_free(ctx);
+    return 0;
+}
+
+size_t gost_ec_key2buf(const EC_KEY *key, unsigned char **pbuf)
+{
+    size_t len;
+    unsigned char *buf;
+    const EC_GROUP *grp = NULL;
+    const EC_POINT *pkey = NULL;
+    
+    pkey = EC_KEY_get0_public_key(key);
+    grp = EC_KEY_get0_group(key);
+
+    if(pkey == NULL || grp == NULL)
+        return 0;
+
+    len = gost_ec_point2oct(grp, pkey, NULL, 0);
+    if (len == 0)
+        return 0;
+    if ((buf = OPENSSL_malloc(len)) == NULL) {
+        GOSTerr(GOST_F_GOST_EC_KEY2BUF, ERR_R_MALLOC_FAILURE); 
+        return 0;
+    }
+    len = gost_ec_point2oct(grp, pkey, buf, len);
+    if (len == 0) {
+        OPENSSL_free(buf);
+        return 0;
+    }
+    *pbuf = buf;
+    return len;
+}
+
+int gost_ec_oct2key(EC_KEY *key, const unsigned char *buf, size_t len)
+{
+    const EC_GROUP *group = NULL;
+    const EC_POINT *pub_key = NULL;
+    EC_POINT *point = NULL;
+    int ok = 0;
+    
+    if (key == NULL || (group = EC_KEY_get0_group(key)) == NULL)
+        return 0;
+
+    if ((pub_key = EC_KEY_get0_public_key(key)) == NULL) {
+        if((point = EC_POINT_new(group)) == NULL ||
+           !EC_KEY_set_public_key(key, point))
+           goto err;
+        pub_key = EC_KEY_get0_public_key(key);
+    }
+        
+    if (gost_ec_oct2point(group, (EC_POINT *)pub_key, buf, len) == 0)
+        goto err;
+
+    ok = 1;
+
+ err:
+    if (point)
+        EC_POINT_free(point);
+    return ok;
+}
index afa19ebb7bc708a5a21d6d8efb21307d1dc02103..6da6a66257ddd3bf1df21b6b0ad7548cd6f8b4c6 100644 (file)
@@ -252,6 +252,8 @@ int gost_ec_verify(const unsigned char *dgst, int dgst_len,
 int gost_ec_compute_public(EC_KEY *ec);
 int gost_ec_point_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *n,
                       const EC_POINT *q, const BIGNUM *m, BN_CTX *ctx);
+size_t gost_ec_key2buf(const EC_KEY *key, unsigned char **pbuf);
+int gost_ec_oct2key(EC_KEY *key, const unsigned char *buf, size_t len);
 
 #define CURVEDEF(a) \
 int point_mul_##a(const EC_GROUP *group, EC_POINT *r, const EC_POINT *q, const BIGNUM *m, BN_CTX *ctx);\