]> www.wagner.pp.ru Git - openssl-gost/engine.git/commitdiff
GOST key export, separate function
authorDmitry Belyavskiy <beldmit@gmail.com>
Tue, 4 Sep 2018 11:41:01 +0000 (14:41 +0300)
committerDmitry Belyavskiy <beldmit@gmail.com>
Tue, 4 Sep 2018 11:41:01 +0000 (14:41 +0300)
e_gost_err.c
e_gost_err.h
gost_ec_keyx.c
gost_lcl.h

index da9ab55aec560b90ba22c2743a3b5a53a5ce14b9..d3008d265bfb2dae1b5241901b9c5ee22e6d3ab3 100644 (file)
@@ -50,6 +50,9 @@ static ERR_STRING_DATA GOST_str_functs[] = {
     {ERR_PACK(0, GOST_F_PARAM_COPY_GOST_EC, 0), "param_copy_gost_ec"},
     {ERR_PACK(0, GOST_F_PKEY_GOST2001_PARAMGEN, 0), "pkey_gost2001_paramgen"},
     {ERR_PACK(0, GOST_F_PKEY_GOST2012_PARAMGEN, 0), "pkey_gost2012_paramgen"},
+    {ERR_PACK(0, GOST_F_PKEY_GOST2018_ECCP_ENCRYPT, 0),
+     "pkey_gost2018_encrypt"},
+    {ERR_PACK(0, GOST_F_PKEY_GOST2018_ENCRYPT, 0), "pkey_gost2018_encrypt"},
     {ERR_PACK(0, GOST_F_PKEY_GOST_CTRL, 0), "pkey_gost_ctrl"},
     {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"},
@@ -90,6 +93,8 @@ static ERR_STRING_DATA GOST_str_reasons[] = {
     "cannot pack ephemeral key"},
     {ERR_PACK(0, 0, GOST_R_CIPHER_NOT_FOUND), "cipher not found"},
     {ERR_PACK(0, 0, GOST_R_CTRL_CALL_FAILED), "ctrl call failed"},
+    {ERR_PACK(0, 0, GOST_R_ERROR_COMPUTING_EXPORT_KEYS),
+    "error computing export keys"},
     {ERR_PACK(0, 0, GOST_R_ERROR_COMPUTING_SHARED_KEY),
     "error computing shared key"},
     {ERR_PACK(0, 0, GOST_R_ERROR_PARSING_KEY_TRANSPORT_INFO),
index ce53f82aa6c2c0fffa6cd641c44842324fa0b4f4..2b1d8bab4d79610cb927a7e8f056872367cd5d75 100644 (file)
@@ -57,6 +57,8 @@ void ERR_GOST_error(int function, int reason, char *file, int line);
 # define GOST_F_PARAM_COPY_GOST_EC                        119
 # define GOST_F_PKEY_GOST2001_PARAMGEN                    120
 # define GOST_F_PKEY_GOST2012_PARAMGEN                    121
+# define GOST_F_PKEY_GOST2018_ECCP_ENCRYPT                150
+# define GOST_F_PKEY_GOST2018_ENCRYPT                     151
 # define GOST_F_PKEY_GOST_CTRL                            122
 # define GOST_F_PKEY_GOST_ECCP_DECRYPT                    123
 # define GOST_F_PKEY_GOST_ECCP_ENCRYPT                    124
@@ -88,6 +90,7 @@ void ERR_GOST_error(int function, int reason, char *file, int line);
 # define GOST_R_CANNOT_PACK_EPHEMERAL_KEY                 102
 # define GOST_R_CIPHER_NOT_FOUND                          103
 # define GOST_R_CTRL_CALL_FAILED                          104
+# define GOST_R_ERROR_COMPUTING_EXPORT_KEYS               135
 # define GOST_R_ERROR_COMPUTING_SHARED_KEY                105
 # define GOST_R_ERROR_PARSING_KEY_TRANSPORT_INFO          106
 # define GOST_R_ERROR_POINT_MUL                           107
index cc6aacb494bed348d09ed59263b0406fca578e92..878698f90ff18555be32e51d4dcf956474429c51 100644 (file)
@@ -342,9 +342,100 @@ int pkey_GOST_ECcp_encrypt(EVP_PKEY_CTX *pctx, unsigned char *out,
     return -1;
 }
 
+int pkey_gost2018_encrypt(EVP_PKEY_CTX *pctx, unsigned char *out,
+                          size_t *out_len, const unsigned char *key,
+                          size_t key_len)
+{
+    PSKeyTransport_gost *pst = NULL;
+    EVP_PKEY *pubk = EVP_PKEY_CTX_get0_pkey(pctx);
+    struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(pctx);
+    int pkey_nid = EVP_PKEY_base_id(pubk);
+    unsigned char expkeys[64];
+    EVP_PKEY *sec_key = NULL;
+    int ret = 0;
+    int mac_nid = NID_undef;
+    size_t mac_len = 0;
+    int exp_len = 0;
+    unsigned char *exp_buf = NULL;
+
+    switch (data->cipher_nid) {
+    case NID_magma_ctr:
+        mac_nid = NID_magma_mac;
+        mac_len = 8;
+        break;
+    case NID_grasshopper_ctr:
+        mac_nid = NID_grasshopper_mac;
+        mac_len = 16;
+        break;
+    default:
+        GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, GOST_R_INVALID_CIPHER);
+        return -1;
+        break;
+    }
+    exp_len = key_len + mac_len;
+    exp_buf = OPENSSL_malloc(exp_len);
+    if (!exp_buf) {
+        GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, ERR_R_MALLOC_FAILURE);
+        return -1;
+    }
+
+    if (out) {
+        sec_key = EVP_PKEY_new();
+        if (!EVP_PKEY_assign(sec_key, EVP_PKEY_base_id(pubk), EC_KEY_new())
+            || !EVP_PKEY_copy_parameters(sec_key, pubk)
+            || !gost_ec_keygen(EVP_PKEY_get0(sec_key))) {
+            GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT,
+                    GOST_R_ERROR_COMPUTING_SHARED_KEY);
+            goto err;
+        }
+    }
+    ret =
+        gost_keg(data->shared_ukm, pkey_nid,
+                 EC_KEY_get0_public_key(EVP_PKEY_get0(pubk)),
+                 EVP_PKEY_get0(sec_key), expkeys);
+    if (ret <= 0) {
+        GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT,
+                GOST_R_ERROR_COMPUTING_EXPORT_KEYS);
+        goto err;
+    }
+
+    ret = gost_kexp15(key, key_len, data->cipher_nid, expkeys + 32,
+                      mac_nid, expkeys + 0, data->shared_ukm + 24, 4, exp_buf,
+                      &exp_len);
+    if (ret <= 0) {
+        GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, GOST_R_CANNOT_PACK_EPHEMERAL_KEY);
+        goto err;
+    }
+
+    pst = PSKeyTransport_gost_new();
+    if (!pst) {
+        GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, ERR_R_MALLOC_FAILURE);
+        goto err;
+    }
+
+    if (!ASN1_OCTET_STRING_set(pst->psexp, exp_buf, exp_len)) {
+        GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, ERR_R_MALLOC_FAILURE);
+        goto err;
+    }
+
+    if (!X509_PUBKEY_set(&pst->ephem_key, out ? sec_key : pubk)) {
+        GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, GOST_R_CANNOT_PACK_EPHEMERAL_KEY);
+        goto err;
+    }
+
+    EVP_PKEY_free(sec_key);
+
+    if ((*out_len = i2d_PSKeyTransport_gost(pst, out ? &out : NULL)) > 0)
+        ret = 1;
+ err:
+    PSKeyTransport_gost_free(pst);
+    OPENSSL_free(exp_buf);
+    return ret;
+}
+
 /*
  * EVP_PKEY_METHOD callback decrypt
- * Implementation of GOST2001 key transport, cryptopo variation
+ * Implementation of GOST2001 key transport, cryptopro variation
  */
 int pkey_GOST_ECcp_decrypt(EVP_PKEY_CTX *pctx, unsigned char *key,
                            size_t *key_len, const unsigned char *in,
index 08fe5ce66a39fcbd136bcf415ec2b1b3daf01218..a6b6e1224845a3bb14ac29735d412657817ee487 100644 (file)
@@ -272,6 +272,17 @@ int gost_kdftree2012_256(unsigned char *keyout, size_t keyout_len,
                          const unsigned char *label, size_t label_len,
                          const unsigned char *seed, size_t seed_len,
                          const size_t representation);
+/* KExp/KImp */
+int gost_kexp15(const unsigned char *shared_key, const int shared_len,
+                int cipher_nid, const unsigned char *cipher_key,
+                int mac_nid, unsigned char *mac_key,
+                const unsigned char *iv, const size_t ivlen,
+                unsigned char *out, int *out_len);
+int gost_kimp15(const unsigned char *expkey, const size_t expkeylen,
+                int cipher_nid, const unsigned char *cipher_key,
+                int mac_nid, unsigned char *mac_key,
+                const unsigned char *iv, const size_t ivlen,
+                unsigned char *shared_key, size_t shared_len);
 /*============== miscellaneous functions============================= */
 /* from gost_sign.c */
 /* Convert GOST R 34.11 hash sum to bignum according to standard */