-static EVP_CIPHER *_hidden_Gost28147_89_cipher = NULL;
-const EVP_CIPHER *cipher_gost(void)
-{
- 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;
- }
- return _hidden_Gost28147_89_cipher;
-}
-
-static EVP_CIPHER *_hidden_Gost28147_89_cbc = NULL;
-const EVP_CIPHER *cipher_gost_cbc(void)
-{
- if (_hidden_Gost28147_89_cbc == NULL
- && ((_hidden_Gost28147_89_cbc =
- EVP_CIPHER_meth_new(NID_gost89_cbc, 8 /* block_size */ ,
- 32 /* key_size */ )) == NULL
- || !EVP_CIPHER_meth_set_iv_length(_hidden_Gost28147_89_cbc, 8)
- || !EVP_CIPHER_meth_set_flags(_hidden_Gost28147_89_cbc,
- EVP_CIPH_CBC_MODE |
- EVP_CIPH_CUSTOM_IV |
- EVP_CIPH_RAND_KEY |
- EVP_CIPH_ALWAYS_CALL_INIT)
- || !EVP_CIPHER_meth_set_init(_hidden_Gost28147_89_cbc,
- gost_cipher_init_cbc)
- || !EVP_CIPHER_meth_set_do_cipher(_hidden_Gost28147_89_cbc,
- gost_cipher_do_cbc)
- || !EVP_CIPHER_meth_set_cleanup(_hidden_Gost28147_89_cbc,
- gost_cipher_cleanup)
- || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_Gost28147_89_cbc,
- sizeof(struct
- ossl_gost_cipher_ctx))
- || !EVP_CIPHER_meth_set_set_asn1_params(_hidden_Gost28147_89_cbc,
- gost89_set_asn1_parameters)
- || !EVP_CIPHER_meth_set_get_asn1_params(_hidden_Gost28147_89_cbc,
- gost89_get_asn1_parameters)
- || !EVP_CIPHER_meth_set_ctrl(_hidden_Gost28147_89_cbc,
- gost_cipher_ctl))) {
- EVP_CIPHER_meth_free(_hidden_Gost28147_89_cbc);
- _hidden_Gost28147_89_cbc = NULL;
- }
- return _hidden_Gost28147_89_cbc;
-}
-
-static EVP_CIPHER *_hidden_gost89_cnt = NULL;
-const EVP_CIPHER *cipher_gost_cpacnt(void)
-{
- if (_hidden_gost89_cnt == NULL
- && ((_hidden_gost89_cnt =
- EVP_CIPHER_meth_new(NID_gost89_cnt, 1 /* block_size */ ,
- 32 /* key_size */ )) == NULL
- || !EVP_CIPHER_meth_set_iv_length(_hidden_gost89_cnt, 8)
- || !EVP_CIPHER_meth_set_flags(_hidden_gost89_cnt,
- EVP_CIPH_OFB_MODE |
- EVP_CIPH_NO_PADDING |
- EVP_CIPH_CUSTOM_IV |
- EVP_CIPH_RAND_KEY |
- EVP_CIPH_ALWAYS_CALL_INIT)
- || !EVP_CIPHER_meth_set_init(_hidden_gost89_cnt,
- gost_cipher_init_cpa)
- || !EVP_CIPHER_meth_set_do_cipher(_hidden_gost89_cnt,
- gost_cipher_do_cnt)
- || !EVP_CIPHER_meth_set_cleanup(_hidden_gost89_cnt,
- gost_cipher_cleanup)
- || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_gost89_cnt,
- sizeof(struct
- ossl_gost_cipher_ctx))
- || !EVP_CIPHER_meth_set_set_asn1_params(_hidden_gost89_cnt,
- gost89_set_asn1_parameters)
- || !EVP_CIPHER_meth_set_get_asn1_params(_hidden_gost89_cnt,
- gost89_get_asn1_parameters)
- || !EVP_CIPHER_meth_set_ctrl(_hidden_gost89_cnt,
- gost_cipher_ctl))) {
- EVP_CIPHER_meth_free(_hidden_gost89_cnt);
- _hidden_gost89_cnt = NULL;
- }
- return _hidden_gost89_cnt;
-}
-
-static EVP_CIPHER *_hidden_gost89_cnt_12 = NULL;
-const EVP_CIPHER *cipher_gost_cpcnt_12(void)
-{
- if (_hidden_gost89_cnt_12 == NULL
- && ((_hidden_gost89_cnt_12 =
- EVP_CIPHER_meth_new(NID_gost89_cnt_12, 1 /* block_size */ ,
- 32 /* key_size */ )) == NULL
- || !EVP_CIPHER_meth_set_iv_length(_hidden_gost89_cnt_12, 8)
- || !EVP_CIPHER_meth_set_flags(_hidden_gost89_cnt_12,
- EVP_CIPH_OFB_MODE |
- EVP_CIPH_NO_PADDING |
- EVP_CIPH_CUSTOM_IV |
- EVP_CIPH_RAND_KEY |
- EVP_CIPH_ALWAYS_CALL_INIT)
- || !EVP_CIPHER_meth_set_init(_hidden_gost89_cnt_12,
- gost_cipher_init_cp_12)
- || !EVP_CIPHER_meth_set_do_cipher(_hidden_gost89_cnt_12,
- gost_cipher_do_cnt)
- || !EVP_CIPHER_meth_set_cleanup(_hidden_gost89_cnt_12,
- gost_cipher_cleanup)
- || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_gost89_cnt_12,
- sizeof(struct
- ossl_gost_cipher_ctx))
- || !EVP_CIPHER_meth_set_set_asn1_params(_hidden_gost89_cnt_12,
- gost89_set_asn1_parameters)
- || !EVP_CIPHER_meth_set_get_asn1_params(_hidden_gost89_cnt_12,
- gost89_get_asn1_parameters)
- || !EVP_CIPHER_meth_set_ctrl(_hidden_gost89_cnt_12,
- gost_cipher_ctl))) {
- EVP_CIPHER_meth_free(_hidden_gost89_cnt_12);
- _hidden_gost89_cnt_12 = NULL;
- }
- return _hidden_gost89_cnt_12;
-}
-
-void cipher_gost_destroy(void)
-{
- 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);
- _hidden_Gost28147_89_cbc = NULL;
- EVP_CIPHER_meth_free(_hidden_gost89_cnt_12);
- _hidden_gost89_cnt_12 = NULL;
+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) ? ((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;
+ }