{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"},
"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),
# 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
# 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
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,
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 */