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);
+/*
+ * Single level template accessor.
+ * Note: that you cannot template 0 value.
+ */
+#define TPL(st,field) ( \
+ ((st)->field) ?: TPL_VAL(st,field) \
+)
+
+#define TPL_VAL(st,field) ( \
+ ((st)->template ? (st)->template->field : 0) \
+)
+
EVP_CIPHER *GOST_init_cipher(GOST_cipher *c)
{
if (c->cipher)
return c->cipher;
+ /* Some sanity checking. */
+ int flags = c->flags | TPL_VAL(c, flags);
+ int block_size = TPL(c, block_size);
+ switch (flags & EVP_CIPH_MODE) {
+ case EVP_CIPH_CTR_MODE:
+ case EVP_CIPH_CFB_MODE:
+ case EVP_CIPH_OFB_MODE:
+ OPENSSL_assert(block_size == 1);
+ OPENSSL_assert(flags & EVP_CIPH_NO_PADDING);
+ break;
+ default:
+ OPENSSL_assert(block_size != 1);
+ OPENSSL_assert(!(flags & EVP_CIPH_NO_PADDING));
+ }
+
+ if (TPL(c, iv_len))
+ OPENSSL_assert(flags & EVP_CIPH_CUSTOM_IV);
+ else
+ OPENSSL_assert(!(flags & EVP_CIPH_CUSTOM_IV));
+
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)) {
+ if (!(cipher = EVP_CIPHER_meth_new(c->nid, block_size, TPL(c, key_len)))
+ || !EVP_CIPHER_meth_set_iv_length(cipher, TPL(c, iv_len))
+ || !EVP_CIPHER_meth_set_flags(cipher, flags)
+ || !EVP_CIPHER_meth_set_init(cipher, TPL(c, init))
+ || !EVP_CIPHER_meth_set_do_cipher(cipher, TPL(c, do_cipher))
+ || !EVP_CIPHER_meth_set_cleanup(cipher, TPL(c, cleanup))
+ || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, TPL(c, ctx_size))
+ || !EVP_CIPHER_meth_set_set_asn1_params(cipher, TPL(c, set_asn1_parameters))
+ || !EVP_CIPHER_meth_set_get_asn1_params(cipher, TPL(c, get_asn1_parameters))
+ || !EVP_CIPHER_meth_set_ctrl(cipher, TPL(c, ctrl))) {
EVP_CIPHER_meth_free(cipher);
cipher = NULL;
}
}
}
-GOST_cipher Gost28147_89_cipher = {
- .nid = NID_id_Gost28147_89,
- .block_size = 1,
+static GOST_cipher gost_template_cipher = {
+ .block_size = 8,
.key_len = 32,
.iv_len = 8,
- .flags = EVP_CIPH_CFB_MODE |
- EVP_CIPH_NO_PADDING |
- EVP_CIPH_CUSTOM_IV |
+ .flags = 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,
.ctrl = gost_cipher_ctl,
};
+GOST_cipher Gost28147_89_cipher = {
+ .nid = NID_id_Gost28147_89,
+ .template = &gost_template_cipher,
+ .block_size = 1,
+ .flags = EVP_CIPH_CFB_MODE |
+ EVP_CIPH_NO_PADDING,
+ .init = gost_cipher_init,
+ .do_cipher = gost_cipher_do_cfb,
+};
+
GOST_cipher Gost28147_89_cbc_cipher = {
.nid = NID_gost89_cbc,
- .block_size = 8,
- .key_len = 32,
- .iv_len = 8,
- .flags = EVP_CIPH_CBC_MODE |
- EVP_CIPH_CUSTOM_IV |
- EVP_CIPH_RAND_KEY |
- EVP_CIPH_ALWAYS_CALL_INIT,
+ .template = &gost_template_cipher,
+ .flags = EVP_CIPH_CBC_MODE,
.init = gost_cipher_init_cbc,
.do_cipher = gost_cipher_do_cbc,
- .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,
};
GOST_cipher Gost28147_89_cnt_cipher = {
.nid = NID_gost89_cnt,
+ .template = &gost_template_cipher,
.block_size = 1,
- .key_len = 32,
- .iv_len = 8,
.flags = EVP_CIPH_OFB_MODE |
- EVP_CIPH_NO_PADDING |
- EVP_CIPH_CUSTOM_IV |
- EVP_CIPH_RAND_KEY |
- EVP_CIPH_ALWAYS_CALL_INIT,
+ EVP_CIPH_NO_PADDING,
.init = gost_cipher_init_cpa,
.do_cipher = gost_cipher_do_cnt,
- .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,
};
GOST_cipher Gost28147_89_cnt_12_cipher = {
.nid = NID_gost89_cnt_12,
+ .template = &gost_template_cipher,
.block_size = 1,
+ .flags = EVP_CIPH_OFB_MODE |
+ EVP_CIPH_NO_PADDING,
+ .init = gost_cipher_init_cp_12,
+ .do_cipher = gost_cipher_do_cnt,
+};
+
+static GOST_cipher magma_template_cipher = {
+ .block_size = 8,
.key_len = 32,
.iv_len = 8,
- .flags = EVP_CIPH_OFB_MODE |
- EVP_CIPH_NO_PADDING |
- EVP_CIPH_CUSTOM_IV |
+ .flags = EVP_CIPH_CUSTOM_IV |
EVP_CIPH_RAND_KEY |
EVP_CIPH_ALWAYS_CALL_INIT,
- .init = gost_cipher_init_cp_12,
- .do_cipher = gost_cipher_do_cnt,
.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,
+ .set_asn1_parameters = magma_set_asn1_parameters,
+ .get_asn1_parameters = magma_get_asn1_parameters,
+ .do_cipher = magma_cipher_do_ctr,
+ .ctrl = magma_cipher_ctl,
};
GOST_cipher magma_ctr_cipher = {
.nid = NID_magma_ctr,
+ .template = &magma_template_cipher,
.block_size = 1,
- .key_len = 32,
.iv_len = 4,
.flags = EVP_CIPH_CTR_MODE |
- EVP_CIPH_NO_PADDING |
- EVP_CIPH_CUSTOM_IV |
- EVP_CIPH_RAND_KEY |
- EVP_CIPH_ALWAYS_CALL_INIT,
+ EVP_CIPH_NO_PADDING,
.init = magma_cipher_init,
- .do_cipher = magma_cipher_do_ctr,
- .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 = magma_cipher_ctl,
};
GOST_cipher magma_ctr_acpkm_cipher = {
.nid = NID_magma_ctr_acpkm,
+ .template = &magma_template_cipher,
.block_size = 1,
- .key_len = 32,
.iv_len = 4,
.flags = EVP_CIPH_CTR_MODE |
- EVP_CIPH_NO_PADDING |
- EVP_CIPH_CUSTOM_IV |
- EVP_CIPH_RAND_KEY |
- EVP_CIPH_ALWAYS_CALL_INIT,
+ EVP_CIPH_NO_PADDING,
.init = magma_cipher_init,
- .do_cipher = magma_cipher_do_ctr,
- .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 = magma_cipher_ctl,
};
GOST_cipher magma_ctr_acpkm_omac_cipher = {
.nid = NID_magma_ctr_acpkm_omac,
+ .template = &magma_template_cipher,
.block_size = 1,
- .key_len = 32,
.iv_len = 4,
.flags = EVP_CIPH_CTR_MODE |
EVP_CIPH_NO_PADDING |
- EVP_CIPH_CUSTOM_IV |
- EVP_CIPH_RAND_KEY |
- EVP_CIPH_ALWAYS_CALL_INIT |
EVP_CIPH_CUSTOM_COPY |
EVP_CIPH_FLAG_CUSTOM_CIPHER |
EVP_CIPH_FLAG_CIPHER_WITH_MAC,
.init = magma_cipher_init_ctr_acpkm_omac,
.do_cipher = magma_cipher_do_ctr_acpkm_omac,
- .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 = magma_cipher_ctl_acpkm_omac,
};
-static EVP_CIPHER *_hidden_magma_cbc = NULL;
-const EVP_CIPHER *cipher_magma_cbc(void)
-{
- if (_hidden_magma_cbc == NULL
- && ((_hidden_magma_cbc =
- EVP_CIPHER_meth_new(NID_magma_cbc, 8 /* block_size */ ,
- 32 /* key_size */ )) == NULL
- || !EVP_CIPHER_meth_set_iv_length(_hidden_magma_cbc, 8)
- || !EVP_CIPHER_meth_set_flags(_hidden_magma_cbc,
- EVP_CIPH_CBC_MODE |
- EVP_CIPH_CUSTOM_IV |
- EVP_CIPH_RAND_KEY |
- EVP_CIPH_ALWAYS_CALL_INIT)
- || !EVP_CIPHER_meth_set_init(_hidden_magma_cbc, magma_cipher_init)
- || !EVP_CIPHER_meth_set_do_cipher(_hidden_magma_cbc,
- magma_cipher_do_cbc)
- || !EVP_CIPHER_meth_set_cleanup(_hidden_magma_cbc,
- gost_cipher_cleanup)
- || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_magma_cbc,
- sizeof(struct
- ossl_gost_cipher_ctx))
- || !EVP_CIPHER_meth_set_ctrl(_hidden_magma_cbc, magma_cipher_ctl))) {
- EVP_CIPHER_meth_free(_hidden_magma_cbc);
- _hidden_magma_cbc = NULL;
- }
- return _hidden_magma_cbc;
-}
+GOST_cipher magma_cbc_cipher = {
+ .nid = NID_magma_cbc,
+ .template = &gost_template_cipher,
+ .flags = EVP_CIPH_CBC_MODE,
+ .init = magma_cipher_init,
+ .do_cipher = magma_cipher_do_cbc,
+};
/* Implementation of GOST 28147-89 in MAC (imitovstavka) mode */
/* Init functions which set specific parameters */
/* Control function, knows how to set MAC key.*/
static int gost_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr);
-static EVP_MD *_hidden_Gost28147_89_MAC_md = NULL;
-static EVP_MD *_hidden_Gost28147_89_12_MAC_md = NULL;
-
-EVP_MD *imit_gost_cpa(void)
-{
- if (_hidden_Gost28147_89_MAC_md == NULL) {
- EVP_MD *md;
-
- if ((md = EVP_MD_meth_new(NID_id_Gost28147_89_MAC, NID_undef)) == NULL
- || !EVP_MD_meth_set_result_size(md, 4)
- || !EVP_MD_meth_set_input_blocksize(md, 8)
- || !EVP_MD_meth_set_app_datasize(md,
- sizeof(struct ossl_gost_imit_ctx))
- || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_XOF)
- || !EVP_MD_meth_set_init(md, gost_imit_init_cpa)
- || !EVP_MD_meth_set_update(md, gost_imit_update)
- || !EVP_MD_meth_set_final(md, gost_imit_final)
- || !EVP_MD_meth_set_copy(md, gost_imit_copy)
- || !EVP_MD_meth_set_cleanup(md, gost_imit_cleanup)
- || !EVP_MD_meth_set_ctrl(md, gost_imit_ctrl)) {
- EVP_MD_meth_free(md);
- md = NULL;
- }
- _hidden_Gost28147_89_MAC_md = md;
- }
- return _hidden_Gost28147_89_MAC_md;
-}
-
-void imit_gost_cpa_destroy(void)
-{
- EVP_MD_meth_free(_hidden_Gost28147_89_MAC_md);
- _hidden_Gost28147_89_MAC_md = NULL;
-}
-
-EVP_MD *imit_gost_cp_12(void)
-{
- if (_hidden_Gost28147_89_12_MAC_md == NULL) {
- EVP_MD *md;
-
- if ((md = EVP_MD_meth_new(NID_gost_mac_12, NID_undef)) == NULL
- || !EVP_MD_meth_set_result_size(md, 4)
- || !EVP_MD_meth_set_input_blocksize(md, 8)
- || !EVP_MD_meth_set_app_datasize(md,
- sizeof(struct ossl_gost_imit_ctx))
- || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_XOF)
- || !EVP_MD_meth_set_init(md, gost_imit_init_cp_12)
- || !EVP_MD_meth_set_update(md, gost_imit_update)
- || !EVP_MD_meth_set_final(md, gost_imit_final)
- || !EVP_MD_meth_set_copy(md, gost_imit_copy)
- || !EVP_MD_meth_set_cleanup(md, gost_imit_cleanup)
- || !EVP_MD_meth_set_ctrl(md, gost_imit_ctrl)) {
- EVP_MD_meth_free(md);
- md = NULL;
- }
- _hidden_Gost28147_89_12_MAC_md = md;
- }
- return _hidden_Gost28147_89_12_MAC_md;
-}
+GOST_digest Gost28147_89_MAC_digest = {
+ .nid = NID_id_Gost28147_89_MAC,
+ .result_size = 4,
+ .input_blocksize = 8,
+ .app_datasize = sizeof(struct ossl_gost_imit_ctx),
+ .flags = EVP_MD_FLAG_XOF,
+ .init = gost_imit_init_cpa,
+ .update = gost_imit_update,
+ .final = gost_imit_final,
+ .copy = gost_imit_copy,
+ .cleanup = gost_imit_cleanup,
+ .ctrl = gost_imit_ctrl,
+};
-void imit_gost_cp_12_destroy(void)
-{
- EVP_MD_meth_free(_hidden_Gost28147_89_12_MAC_md);
- _hidden_Gost28147_89_12_MAC_md = NULL;
-}
+GOST_digest Gost28147_89_mac_12_digest = {
+ .nid = NID_gost_mac_12,
+ .result_size = 4,
+ .input_blocksize = 8,
+ .app_datasize = sizeof(struct ossl_gost_imit_ctx),
+ .flags = EVP_MD_FLAG_XOF,
+ .init = gost_imit_init_cp_12,
+ .update = gost_imit_update,
+ .final = gost_imit_final,
+ .copy = gost_imit_copy,
+ .cleanup = gost_imit_cleanup,
+ .ctrl = gost_imit_ctrl,
+};
/*
* Correspondence between gost parameter OIDs and substitution blocks
* upon engine initialization
*/
-struct gost_cipher_info gost_cipher_list[] = {
+static struct gost_cipher_info gost_cipher_list[] = {
/*- NID *//*
* Subst block
*//*
}
/* Initializes EVP_CIPHER_CTX with default values */
-int gost_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
+static int gost_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
const unsigned char *iv, int enc)
{
return gost_cipher_init_param(ctx, key, iv, enc, NID_undef,
}
/* Initializes EVP_CIPHER_CTX with default values */
-int gost_cipher_init_cbc(EVP_CIPHER_CTX *ctx, const unsigned char *key,
+static int gost_cipher_init_cbc(EVP_CIPHER_CTX *ctx, const unsigned char *key,
const unsigned char *iv, int enc)
{
return gost_cipher_init_param(ctx, key, iv, enc, NID_undef,
}
/* Initializes EVP_CIPHER_CTX with default values */
-int magma_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
+static int magma_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
const unsigned char *iv, int enc)
{
struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
}
/* Initializes EVP_CIPHER_CTX with default values */
-int magma_cipher_init_ctr_acpkm_omac(EVP_CIPHER_CTX *ctx, const unsigned char *key,
+static int magma_cipher_init_ctr_acpkm_omac(EVP_CIPHER_CTX *ctx, const unsigned char *key,
const unsigned char *iv, int enc)
{
if (key) {
}
/* GOST encryption in CBC mode */
-int gost_cipher_do_cbc(EVP_CIPHER_CTX *ctx, unsigned char *out,
+static int gost_cipher_do_cbc(EVP_CIPHER_CTX *ctx, unsigned char *out,
const unsigned char *in, size_t inl)
{
unsigned char b[8];
}
/* MAGMA encryption in CBC mode */
-int magma_cipher_do_cbc(EVP_CIPHER_CTX *ctx, unsigned char *out,
+static int magma_cipher_do_cbc(EVP_CIPHER_CTX *ctx, unsigned char *out,
const unsigned char *in, size_t inl)
{
unsigned char b[8];
return inl;
}
/* GOST encryption in CFB mode */
-int gost_cipher_do_cfb(EVP_CIPHER_CTX *ctx, unsigned char *out,
+static int gost_cipher_do_cfb(EVP_CIPHER_CTX *ctx, unsigned char *out,
const unsigned char *in, size_t inl)
{
const unsigned char *in_ptr = in;
}
/* Cleaning up of EVP_CIPHER_CTX */
-int gost_cipher_cleanup(EVP_CIPHER_CTX *ctx)
+static int gost_cipher_cleanup(EVP_CIPHER_CTX *ctx)
{
struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
EVP_MD_CTX_free(c->omac_ctx);
}
/* Control function for gost cipher */
-int gost_cipher_ctl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
+static int gost_cipher_ctl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
{
switch (type) {
case EVP_CTRL_RAND_KEY:
}
/* Control function for gost cipher */
-int magma_cipher_ctl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
+static int magma_cipher_ctl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
{
switch (type) {
case EVP_CTRL_RAND_KEY:
}
/* Set cipher parameters from ASN1 structure */
-int gost89_set_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
+static int gost89_set_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
{
int len = 0;
unsigned char *buf = NULL;
}
/* Store parameters into ASN1 structure */
-int gost89_get_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
+static int gost89_get_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
{
int len;
GOST_CIPHER_PARAMS *gcp = NULL;
c->count = c->count % 1024 + 8;
}
-int gost_imit_update(EVP_MD_CTX *ctx, const void *data, size_t count)
+static int gost_imit_update(EVP_MD_CTX *ctx, const void *data, size_t count)
{
struct ossl_gost_imit_ctx *c = EVP_MD_CTX_md_data(ctx);
const unsigned char *p = data;
return 1;
}
-int gost_imit_final(EVP_MD_CTX *ctx, unsigned char *md)
+static int gost_imit_final(EVP_MD_CTX *ctx, unsigned char *md)
{
struct ossl_gost_imit_ctx *c = EVP_MD_CTX_md_data(ctx);
if (!c->key_set) {
return 1;
}
-int gost_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr)
+static int gost_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr)
{
switch (type) {
case EVP_MD_CTRL_KEY_LEN:
}
}
-int gost_imit_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
+static int gost_imit_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
{
if (EVP_MD_CTX_md_data(to) && EVP_MD_CTX_md_data(from)) {
memcpy(EVP_MD_CTX_md_data(to), EVP_MD_CTX_md_data(from),
}
/* Clean up imit ctx */
-int gost_imit_cleanup(EVP_MD_CTX *ctx)
+static int gost_imit_cleanup(EVP_MD_CTX *ctx)
{
memset(EVP_MD_CTX_md_data(ctx), 0, sizeof(struct ossl_gost_imit_ctx));
return 1;