+void gost_magma_encrypt_wrap(unsigned char *in, unsigned char *out,
+ struct ossl_gost_cipher_ctx *c) {
+ int i;
+ unsigned char b[8];
+ unsigned char d[8];
+ for (i = 0; i < 8; i++) {
+ b[7 - i] = in[i];
+ }
+ gostcrypt(&(c->cctx), b, d);
+ for (i = 0; i < 8; i++) {
+ out[7 - i] = d[i];
+ }
+}
+
+/* ----------------------------------------------------------------------------------------------- */
+/*! Функция реализует операцию умножения двух элементов конечного поля \f$ \mathbb F_{2^{64}}\f$,
+ порожденного неприводимым многочленом
+ \f$ f(x) = x^{64} + x^4 + x^3 + x + 1 \in \mathbb F_2[x]\f$. Для умножения используется
+ простейшая реализация, основанная на приведении по модулю после каждого шага алгоритма. */
+/* ----------------------------------------------------------------------------------------------- */
+static void gf64_mul (uint64_t *result, uint64_t *arg1, uint64_t *arg2)
+{
+ int i = 0;
+ register uint64_t t, X0;
+ uint64_t Z0 = 0;
+
+#ifdef L_ENDIAN
+ X0 = BSWAP64(*arg1);
+#else
+ X0 = *arg1;
+#endif
+
+#ifdef L_ENDIAN
+ t = BSWAP64(*(arg2));
+#else
+ t = *(arg2);
+#endif
+
+ for (i = 0; i < 63; i++) {
+ if (t & 0x1) {
+ Z0 ^= X0;
+ }
+ t >>= 1;
+ if (X0 & 0x8000000000000000) {
+ X0 <<= 1;
+ X0 ^= 0x1b;
+ }
+ else {
+ X0 <<= 1;
+ }
+ }
+
+ if (t & 0x1) {
+ Z0 ^= X0;
+ }
+
+#ifdef L_ENDIAN
+ *(result) = BSWAP64(Z0);
+#else
+ *(result) = Z0;
+#endif
+}
+
+static int gost_magma_cipher_init_mgm(EVP_CIPHER_CTX *ctx, const unsigned char *key,
+ const unsigned char *iv, int enc)
+{
+ gost_mgm_ctx *mctx =
+ (gost_mgm_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
+ int bl;
+
+ if (!iv && !key)
+ return 1;
+ if (key) {
+ bl = EVP_CIPHER_CTX_iv_length(ctx);
+ if (!gost_cipher_set_param(&mctx->ks.g_ks, NID_id_tc26_gost_28147_param_Z))
+ return 0;
+ magma_key(&(mctx->ks.g_ks.cctx), key);
+ gost_mgm128_init(&mctx->mgm, &mctx->ks,
+ (block128_f) gost_magma_encrypt_wrap, gf64_mul, bl);
+
+ /*
+ * If we have an iv can set it directly, otherwise use saved IV.
+ */
+ if (iv == NULL && mctx->iv_set)
+ iv = mctx->iv;
+ if (iv) {
+ if (gost_mgm128_setiv(&mctx->mgm, iv, mctx->ivlen) != 1)
+ return 0;
+ mctx->iv_set = 1;
+ }
+ mctx->key_set = 1;
+ } else {
+ /* If key set use IV, otherwise copy */
+ if (mctx->key_set) {
+ if (gost_mgm128_setiv(&mctx->mgm, iv, mctx->ivlen) != 1)
+ return 0;
+ }
+ else
+ memcpy(mctx->iv, iv, mctx->ivlen);
+ mctx->iv_set = 1;
+ }
+ return 1;
+}
+