From e55323c4c29e6b2645e7e7357453c34fb9a42440 Mon Sep 17 00:00:00 2001 From: Dmitry Belyavskiy Date: Tue, 4 Sep 2018 14:41:01 +0300 Subject: [PATCH 1/1] GOST key export, separate function --- e_gost_err.c | 5 +++ e_gost_err.h | 3 ++ gost_ec_keyx.c | 93 +++++++++++++++++++++++++++++++++++++++++++++++++- gost_lcl.h | 11 ++++++ 4 files changed, 111 insertions(+), 1 deletion(-) diff --git a/e_gost_err.c b/e_gost_err.c index da9ab55..d3008d2 100644 --- a/e_gost_err.c +++ b/e_gost_err.c @@ -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), diff --git a/e_gost_err.h b/e_gost_err.h index ce53f82..2b1d8ba 100644 --- a/e_gost_err.h +++ b/e_gost_err.h @@ -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 diff --git a/gost_ec_keyx.c b/gost_ec_keyx.c index cc6aacb..878698f 100644 --- a/gost_ec_keyx.c +++ b/gost_ec_keyx.c @@ -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, diff --git a/gost_lcl.h b/gost_lcl.h index 08fe5ce..a6b6e12 100644 --- a/gost_lcl.h +++ b/gost_lcl.h @@ -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 */ -- 2.39.2