* Parses GOST algorithm parameters from X509_ALGOR and modifies pkey setting
* NID and parameters
*/
-static int decode_gost_algor_params(EVP_PKEY *pkey, const X509_ALGOR *palg)
+static int decode_gost_algor_params(EVP_PKEY *pkey, X509_ALGOR *palg)
{
- const ASN1_OBJECT *palg_obj = NULL;
+ ASN1_OBJECT *palg_obj = NULL;
int ptype = V_ASN1_UNDEF;
int pkey_nid = NID_undef, param_nid = NID_undef;
ASN1_STRING *pval = NULL;
if (!pkey || !palg)
return 0;
- X509_ALGOR_get0(&palg_obj, &ptype, (const void **)&pval, palg);
+ X509_ALGOR_get0(&palg_obj, &ptype, (void **)&pval, palg);
if (ptype != V_ASN1_SEQUENCE) {
GOSTerr(GOST_F_DECODE_GOST_ALGOR_PARAMS,
GOST_R_BAD_KEY_PARAMETERS_FORMAT);
if (!EC_KEY_set_private_key(ec, priv))
return 0;
if (!EVP_PKEY_missing_parameters(pkey))
- gost_ec_compute_public(ec);
+ return gost_ec_compute_public(ec);
break;
}
default:
return pknum_masked;
}
-static int priv_decode_gost(EVP_PKEY *pk, const PKCS8_PRIV_KEY_INFO *p8inf)
+static int priv_decode_gost(EVP_PKEY *pk, PKCS8_PRIV_KEY_INFO *p8inf)
{
const unsigned char *pkey_buf = NULL, *p = NULL;
int priv_len = 0;
BIGNUM *pk_num = NULL;
int ret = 0;
- const X509_ALGOR *palg = NULL;
- const ASN1_OBJECT *palg_obj = NULL;
+ X509_ALGOR *palg = NULL;
+ ASN1_OBJECT *palg_obj = NULL;
ASN1_INTEGER *priv_key = NULL;
int expected_key_len = 32;
return -2;
}
+ static int mac_ctrl_magma(EVP_PKEY *pkey, int op, long arg1, void *arg2)
+ {
+ switch (op) {
+ case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
+ if (arg2) {
+ *(int *)arg2 = NID_magma_mac;
+ return 2;
+ }
+ }
+ return -2;
+ }
+
+ static int mac_ctrl_grasshopper(EVP_PKEY *pkey, int op, long arg1, void *arg2)
+ {
+ switch (op) {
+ case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
+ if (arg2) {
+ *(int *)arg2 = NID_grasshopper_mac;
+ return 2;
+ }
+ }
+ return -2;
+ }
+
static int gost2001_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
{
int nid =
EVP_PKEY_asn1_set_free(*ameth, mackey_free_gost);
EVP_PKEY_asn1_set_ctrl(*ameth, mac_ctrl_gost_12);
break;
+ case NID_magma_mac:
+ EVP_PKEY_asn1_set_free(*ameth, mackey_free_gost);
+ EVP_PKEY_asn1_set_ctrl(*ameth, mac_ctrl_magma);
+ break;
+ case NID_grasshopper_mac:
+ EVP_PKEY_asn1_set_free(*ameth, mackey_free_gost);
+ EVP_PKEY_asn1_set_ctrl(*ameth, mac_ctrl_grasshopper);
+ break;
}
return 1;
}
NID_grasshopper_cfb,
NID_grasshopper_ofb,
NID_grasshopper_ctr,
+ NID_magma_cbc,
+ NID_magma_ctr,
0
};
static int gost_digest_nids(const int** nids) {
- static int digest_nids[6] = {0, 0, 0, 0, 0, 0};
+ static int digest_nids[8] = {0, 0, 0, 0, 0, 0, 0, 0};
static int pos = 0;
static int init = 0;
digest_nids[pos++] = EVP_MD_type(md);
if ((md = imit_gost_cp_12()) != NULL)
digest_nids[pos++] = EVP_MD_type(md);
+ if ((md = magma_omac()) != NULL)
+ digest_nids[pos++] = EVP_MD_type(md);
+ if ((md = grasshopper_omac()) != NULL)
+ digest_nids[pos++] = EVP_MD_type(md);
+
+
digest_nids[pos] = 0;
init = 1;
}
NID_id_GostR3410_2012_256,
NID_id_GostR3410_2012_512,
NID_gost_mac_12,
+ NID_magma_mac,
+ NID_grasshopper_mac,
0
};
static EVP_PKEY_METHOD* pmeth_GostR3410_2001 = NULL,
* pmeth_GostR3410_2012_256 = NULL,
* pmeth_GostR3410_2012_512 = NULL,
- * pmeth_Gost28147_MAC = NULL, * pmeth_Gost28147_MAC_12 = NULL;
+ * pmeth_Gost28147_MAC = NULL, * pmeth_Gost28147_MAC_12 = NULL,
+ * pmeth_magma_mac = NULL, * pmeth_grasshopper_mac = NULL;
static EVP_PKEY_ASN1_METHOD* ameth_GostR3410_2001 = NULL,
* ameth_GostR3410_2012_256 = NULL,
* ameth_GostR3410_2012_512 = NULL,
- * ameth_Gost28147_MAC = NULL, * ameth_Gost28147_MAC_12 = NULL;
+ * ameth_Gost28147_MAC = NULL, * ameth_Gost28147_MAC_12 = NULL,
+ * ameth_magma_mac = NULL, * ameth_grasshopper_mac = NULL;
static int gost_engine_init(ENGINE* e) {
return 1;
}
static int gost_engine_destroy(ENGINE* e) {
- digest_gost_destroy();
- digest_gost2012_256_destroy();
- digest_gost2012_512_destroy();
-
- imit_gost_cpa_destroy();
- imit_gost_cp_12_destroy();
- magma_omac_destroy();
- grasshopper_omac_destroy();
-
- cipher_gost_destroy();
-
- gost_param_free();
-
- pmeth_GostR3410_2001 = NULL;
- pmeth_Gost28147_MAC = NULL;
- pmeth_GostR3410_2012_256 = NULL;
- pmeth_GostR3410_2012_512 = NULL;
- pmeth_Gost28147_MAC_12 = NULL;
- pmeth_magma_mac = NULL;
- pmeth_grasshopper_mac = NULL;
-
- ameth_GostR3410_2001 = NULL;
- ameth_Gost28147_MAC = NULL;
- ameth_GostR3410_2012_256 = NULL;
- ameth_GostR3410_2012_512 = NULL;
- ameth_Gost28147_MAC_12 = NULL;
- ameth_magma_mac = NULL;
- ameth_grasshopper_mac = NULL;
-
- ERR_unload_GOST_strings();
-
- return 1;
+ digest_gost_destroy();
+ digest_gost2012_256_destroy();
+ digest_gost2012_512_destroy();
+
+ imit_gost_cpa_destroy();
+ imit_gost_cp_12_destroy();
++ magma_omac_destroy();
++ grasshopper_omac_destroy();
+
+ cipher_gost_destroy();
+ cipher_gost_grasshopper_destroy();
+
+ gost_param_free();
+
+ pmeth_GostR3410_2001 = NULL;
+ pmeth_Gost28147_MAC = NULL;
+ pmeth_GostR3410_2012_256 = NULL;
+ pmeth_GostR3410_2012_512 = NULL;
+ pmeth_Gost28147_MAC_12 = NULL;
++ pmeth_magma_mac = NULL;
++ pmeth_grasshopper_mac = NULL;
+
+ ameth_GostR3410_2001 = NULL;
+ ameth_Gost28147_MAC = NULL;
+ ameth_GostR3410_2012_256 = NULL;
+ ameth_GostR3410_2012_512 = NULL;
+ ameth_Gost28147_MAC_12 = NULL;
++ ameth_magma_mac = NULL;
++ ameth_grasshopper_mac = NULL;
+
+ ERR_unload_GOST_strings();
+
+ return 1;
}
static int bind_gost(ENGINE* e, const char* id) {
"GOST-MAC-12",
"GOST 28147-89 MAC with 2012 params"))
goto end;
+ if (!register_ameth_gost(NID_magma_mac, &ameth_magma_mac,
+ "MAGMA-MAC", "GOST R 34.13-2015 Magma MAC"))
+ goto end;
+ if (!register_ameth_gost(NID_grasshopper_mac, &ameth_grasshopper_mac,
+ "GRASSHOPPER-MAC", "GOST R 34.13-2015 Grasshopper MAC"))
+ goto end;
if (!register_pmeth_gost(NID_id_GostR3410_2001, &pmeth_GostR3410_2001, 0))
goto end;
goto end;
if (!register_pmeth_gost(NID_gost_mac_12, &pmeth_Gost28147_MAC_12, 0))
goto end;
+ if (!register_pmeth_gost(NID_magma_mac, &pmeth_magma_mac, 0))
+ goto end;
+ if (!register_pmeth_gost(NID_grasshopper_mac, &pmeth_grasshopper_mac, 0))
+ goto end;
if (!ENGINE_register_ciphers(e)
|| !ENGINE_register_digests(e)
|| !ENGINE_register_pkey_meths(e)
|| !EVP_add_cipher(cipher_gost_grasshopper_cfb())
|| !EVP_add_cipher(cipher_gost_grasshopper_ofb())
|| !EVP_add_cipher(cipher_gost_grasshopper_ctr())
+ || !EVP_add_cipher(cipher_magma_cbc())
+ || !EVP_add_cipher(cipher_magma_ctr())
|| !EVP_add_digest(digest_gost())
|| !EVP_add_digest(digest_gost2012_512())
|| !EVP_add_digest(digest_gost2012_256())
|| !EVP_add_digest(imit_gost_cpa())
|| !EVP_add_digest(imit_gost_cp_12())
+ || !EVP_add_digest(magma_omac())
+ || !EVP_add_digest(grasshopper_omac())
) {
goto end;
}
*digest = digest_gost2012_512();
} else if (nid == NID_gost_mac_12) {
*digest = imit_gost_cp_12();
+ } else if (nid == NID_magma_mac) {
+ *digest = magma_omac();
+ } else if (nid == NID_grasshopper_mac) {
+ *digest = grasshopper_omac();
} else {
ok = 0;
*digest = NULL;
*cipher = cipher_gost_grasshopper_ofb();
} else if (nid == NID_grasshopper_ctr) {
*cipher = cipher_gost_grasshopper_ctr();
+ } else if (nid == NID_magma_cbc) {
+ *cipher = cipher_magma_cbc();
+ } else if (nid == NID_magma_ctr) {
+ *cipher = cipher_magma_ctr();
} else {
ok = 0;
*cipher = NULL;
case NID_gost_mac_12:
*pmeth = pmeth_Gost28147_MAC_12;
return 1;
+ case NID_magma_mac:
+ *pmeth = pmeth_magma_mac;
+ return 1;
+ case NID_grasshopper_mac:
+ *pmeth = pmeth_grasshopper_mac;
+ return 1;
default:;
}
case NID_gost_mac_12:
*ameth = ameth_Gost28147_MAC_12;
return 1;
+ case NID_magma_mac:
+ *ameth = ameth_magma_mac;
+ return 1;
+ case NID_grasshopper_mac:
+ *ameth = ameth_grasshopper_mac;
+ return 1;
default:;
}
#include <openssl/err.h>
#include <string.h>
+ #include "gost_lcl.h"
#include "e_gost_err.h"
enum GRASSHOPPER_CIPHER_TYPE {
return 1;
}
- /* increment counter (128-bit int) by 1 */
- static void ctr128_inc(unsigned char *counter)
+ void inc_counter(unsigned char* counter, size_t counter_bytes)
{
- unsigned int n = 16;
unsigned char c;
+ unsigned int n = counter_bytes;
do {
--n;
} while (n);
}
+ /* increment counter (128-bit int) by 1 */
+ static void ctr128_inc(unsigned char *counter)
+ {
+ inc_counter(counter, 16);
+ }
+
int gost_grasshopper_cipher_do_ctr(EVP_CIPHER_CTX* ctx, unsigned char* out,
const unsigned char* in, size_t inl) {
gost_grasshopper_cipher_ctx_ctr* c = (gost_grasshopper_cipher_ctx_ctr*) EVP_CIPHER_CTX_get_cipher_data(ctx);
if (!os || !ASN1_OCTET_STRING_set(os, buf, len)) {
OPENSSL_free(buf);
- GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
+ GOSTerr(GOST_F_GOST_GRASSHOPPER_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
return 0;
}
OPENSSL_free(buf);
switch (type) {
case EVP_CTRL_RAND_KEY: {
if (RAND_bytes((unsigned char*) ptr, EVP_CIPHER_CTX_key_length(ctx)) <= 0) {
- GOSTerr(GOST_F_GOST_CIPHER_CTL, GOST_R_RNG_ERROR);
+ GOSTerr(GOST_F_GOST_GRASSHOPPER_CIPHER_CTL, GOST_R_RNG_ERROR);
return -1;
}
break;
}
default:
- GOSTerr(GOST_F_GOST_CIPHER_CTL, GOST_R_UNSUPPORTED_CIPHER_CTL_COMMAND);
+ GOSTerr(GOST_F_GOST_GRASSHOPPER_CIPHER_CTL, GOST_R_UNSUPPORTED_CIPHER_CTL_COMMAND);
return -1;
}
return 1;
return cipher_gost_grasshopper(EVP_CIPH_CTR_MODE, GRASSHOPPER_CIPHER_CTR);
}
+void cipher_gost_grasshopper_destroy(void)
+{
+ EVP_CIPHER_meth_free(gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_ECB]);
+ gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_ECB] = NULL;
+ EVP_CIPHER_meth_free(gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CBC]);
+ gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CBC] = NULL;
+ EVP_CIPHER_meth_free(gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_OFB]);
+ gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_OFB] = NULL;
+ EVP_CIPHER_meth_free(gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CFB]);
+ gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CFB] = NULL;
+ EVP_CIPHER_meth_free(gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CTR]);
+ gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CTR] = NULL;
+}
+
#if defined(__cplusplus)
}
#endif