/**********************************************************************
- * gost_crypt.c *
+ * gost_crypt.c - Initialize all ciphers *
+ * *
* Copyright (c) 2005-2006 Cryptocom LTD *
+ * Copyright (c) 2020 Chikunov Vitaly <vt@altlinux.org> *
* This file is distributed under the same license as OpenSSL *
* *
* OpenSSL interface to GOST 28147-89 cipher functions *
static int magma_cipher_ctl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr);
static int magma_cipher_ctl_acpkm_omac(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr);
-static EVP_CIPHER *_hidden_Gost28147_89_cipher = NULL;
-const EVP_CIPHER *cipher_gost(void)
+EVP_CIPHER *GOST_init_cipher(GOST_cipher *c)
{
- if (_hidden_Gost28147_89_cipher == NULL
- && ((_hidden_Gost28147_89_cipher =
- EVP_CIPHER_meth_new(NID_id_Gost28147_89, 1 /* block_size */ ,
- 32 /* key_size */ )) == NULL
- || !EVP_CIPHER_meth_set_iv_length(_hidden_Gost28147_89_cipher, 8)
- || !EVP_CIPHER_meth_set_flags(_hidden_Gost28147_89_cipher,
- EVP_CIPH_CFB_MODE |
- EVP_CIPH_NO_PADDING |
- EVP_CIPH_CUSTOM_IV |
- EVP_CIPH_RAND_KEY |
- EVP_CIPH_ALWAYS_CALL_INIT)
- || !EVP_CIPHER_meth_set_init(_hidden_Gost28147_89_cipher,
- gost_cipher_init)
- || !EVP_CIPHER_meth_set_do_cipher(_hidden_Gost28147_89_cipher,
- gost_cipher_do_cfb)
- || !EVP_CIPHER_meth_set_cleanup(_hidden_Gost28147_89_cipher,
- gost_cipher_cleanup)
- || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_Gost28147_89_cipher,
- sizeof(struct
- ossl_gost_cipher_ctx))
- ||
- !EVP_CIPHER_meth_set_set_asn1_params(_hidden_Gost28147_89_cipher,
- gost89_set_asn1_parameters)
- ||
- !EVP_CIPHER_meth_set_get_asn1_params(_hidden_Gost28147_89_cipher,
- gost89_get_asn1_parameters)
- || !EVP_CIPHER_meth_set_ctrl(_hidden_Gost28147_89_cipher,
- gost_cipher_ctl))) {
- EVP_CIPHER_meth_free(_hidden_Gost28147_89_cipher);
- _hidden_Gost28147_89_cipher = NULL;
+ if (c->cipher)
+ return c->cipher;
+
+ EVP_CIPHER *cipher;
+ if (!(cipher = EVP_CIPHER_meth_new(c->nid, c->block_size, c->key_len))
+ || !EVP_CIPHER_meth_set_iv_length(cipher, c->iv_len)
+ || !EVP_CIPHER_meth_set_flags(cipher, c->flags)
+ || !EVP_CIPHER_meth_set_init(cipher, c->init)
+ || !EVP_CIPHER_meth_set_do_cipher(cipher, c->do_cipher)
+ || !EVP_CIPHER_meth_set_cleanup(cipher, c->cleanup)
+ || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, c->ctx_size)
+ || !EVP_CIPHER_meth_set_set_asn1_params(cipher, c->set_asn1_parameters)
+ || !EVP_CIPHER_meth_set_get_asn1_params(cipher, c->get_asn1_parameters)
+ || !EVP_CIPHER_meth_set_ctrl(cipher, c->ctrl)) {
+ EVP_CIPHER_meth_free(cipher);
+ cipher = NULL;
+ }
+ c->cipher = cipher;
+ return c->cipher;
+}
+
+void GOST_deinit_cipher(GOST_cipher *c)
+{
+ if (c->cipher) {
+ EVP_CIPHER_meth_free(c->cipher);
+ c->cipher = NULL;
}
- return _hidden_Gost28147_89_cipher;
}
+GOST_cipher Gost28147_89_cipher = {
+ .nid = NID_id_Gost28147_89,
+ .block_size = 1,
+ .key_len = 32,
+ .iv_len = 8,
+ .flags = EVP_CIPH_CFB_MODE |
+ EVP_CIPH_NO_PADDING |
+ EVP_CIPH_CUSTOM_IV |
+ EVP_CIPH_RAND_KEY |
+ EVP_CIPH_ALWAYS_CALL_INIT,
+ .init = gost_cipher_init,
+ .do_cipher = gost_cipher_do_cfb,
+ .cleanup = gost_cipher_cleanup,
+ .ctx_size = sizeof(struct ossl_gost_cipher_ctx),
+ .set_asn1_parameters = gost89_set_asn1_parameters,
+ .get_asn1_parameters = gost89_get_asn1_parameters,
+ .ctrl = gost_cipher_ctl,
+};
+
static EVP_CIPHER *_hidden_Gost28147_89_cbc = NULL;
const EVP_CIPHER *cipher_gost_cbc(void)
{
void cipher_gost_destroy(void)
{
- EVP_CIPHER_meth_free(_hidden_Gost28147_89_cipher);
- _hidden_Gost28147_89_cipher = NULL;
+ //EVP_CIPHER_meth_free(_hidden_Gost28147_89_cipher);
+ //_hidden_Gost28147_89_cipher = NULL;
EVP_CIPHER_meth_free(_hidden_gost89_cnt);
_hidden_gost89_cnt = NULL;
EVP_CIPHER_meth_free(_hidden_Gost28147_89_cbc);
if (!gost_cipher_set_param(c, NID_id_tc26_gost_28147_param_Z))
return 0;
EVP_CIPHER_CTX_set_app_data(ctx, EVP_CIPHER_CTX_get_cipher_data(ctx));
+
+ if (enc) {
+ if (init_zero_kdf_seed(c->kdf_seed) == 0)
+ return -1;
+ }
}
+
if (key)
magma_key(&(c->cctx), key);
if (iv) {
{
if (key) {
struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
- unsigned char keys[64];
- const EVP_MD *md = EVP_get_digestbynid(NID_magma_mac);
- EVP_PKEY *mac_key;
-
- if (md == NULL)
- return 0;
-
- if (enc) {
- if (RAND_bytes(c->kdf_seed, 8) != 1)
- return 0;
- }
-
- if (gost_kdftree2012_256(keys, 64, key, 32, (const unsigned char *)"kdf tree", 8, c->kdf_seed, 8, 1) <= 0)
- return 0;
-
+ unsigned char cipher_key[32];
c->omac_ctx = EVP_MD_CTX_new();
- mac_key = EVP_PKEY_new_mac_key(NID_magma_mac, NULL, keys+32, 32);
- if (mac_key == NULL || c->omac_ctx == NULL) {
- EVP_PKEY_free(mac_key);
- OPENSSL_cleanse(keys, sizeof(keys));
- return 0;
+ if (c->omac_ctx == NULL) {
+ GOSTerr(GOST_F_MAGMA_CIPHER_INIT_CTR_ACPKM_OMAC, ERR_R_MALLOC_FAILURE);
+ return 0;
}
- if (EVP_DigestInit_ex(c->omac_ctx, md, NULL) <= 0 ||
- EVP_DigestSignInit(c->omac_ctx, NULL, md, NULL, mac_key) <= 0) {
- EVP_PKEY_free(mac_key);
- OPENSSL_cleanse(keys, sizeof(keys));
- return 0;
+ if (gost2015_acpkm_omac_init(NID_magma_mac, enc, key,
+ c->omac_ctx, cipher_key, c->kdf_seed) != 1) {
+ EVP_MD_CTX_free(c->omac_ctx);
+ c->omac_ctx = NULL;
+ return 0;
}
- EVP_PKEY_free(mac_key);
- OPENSSL_cleanse(keys + 32, sizeof(keys) - 32);
- return magma_cipher_init(ctx, keys, iv, enc);
+ return magma_cipher_init(ctx, cipher_key, iv, enc);
}
+
return magma_cipher_init(ctx, key, iv, enc);
}
d[7 - i] = in_ptr[i];
}
gostdecrypt(&(c->cctx), d, b);
+ memcpy(d, in_ptr, 8);
for (i = 0; i < 8; i++) {
out_ptr[i] = iv[i] ^ b[7 - i];
}
- memcpy(iv, in_ptr, 8);
+ memcpy(iv, d, 8);
out_ptr += 8;
in_ptr += 8;
inl -= 8;
if (in == NULL && inl == 0) /* Final call */
return gost2015_final_call(ctx, c->omac_ctx, MAGMA_MAC_MAX_SIZE, c->tag, magma_cipher_do_ctr);
+ if (in == NULL)
+ return -1;
+
/* As in and out can be the same pointer, process unencrypted here */
if (EVP_CIPHER_CTX_encrypting(ctx))
EVP_DigestSignUpdate(c->omac_ctx, in, inl);
memset(EVP_MD_CTX_md_data(ctx), 0, sizeof(struct ossl_gost_imit_ctx));
return 1;
}
+/* vim: set expandtab cinoptions=\:0,l1,t0,g0,(0 sw=4 : */