+static int magma_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
+ const unsigned char *iv, int enc);
+static int magma_cipher_init_ctr_acpkm_omac(EVP_CIPHER_CTX *ctx, const unsigned char *key,
+ const unsigned char *iv, int enc);
+/* Handles block of data in CBC mode */
+static int magma_cipher_do_cbc(EVP_CIPHER_CTX *ctx, unsigned char *out,
+ const unsigned char *in, size_t inl);
+static int magma_cipher_do_ctr(EVP_CIPHER_CTX *ctx, unsigned char *out,
+ const unsigned char *in, size_t inl);
+
+static int magma_cipher_do_ctr_acpkm_omac(EVP_CIPHER_CTX *ctx, unsigned char *out,
+ const unsigned char *in, size_t inl);
+
+/* set/get cipher parameters */
+static int magma_set_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params);
+static int magma_get_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params);
+/* Control function */
+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, 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;
+ }
+ 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;
+ }
+}
+
+static GOST_cipher gost_template_cipher = {
+ .block_size = 8,
+ .key_len = 32,
+ .iv_len = 8,
+ .flags = EVP_CIPH_CUSTOM_IV |
+ EVP_CIPH_RAND_KEY |
+ EVP_CIPH_ALWAYS_CALL_INIT,
+ .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,