+GRASSHOPPER_INLINE int gost_grasshopper_cipher_init_cbc(EVP_CIPHER_CTX *ctx, const unsigned char
+ *key, const unsigned char
+ *iv, int enc)
+{
+ gost_grasshopper_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
+ c->type = GRASSHOPPER_CIPHER_CBC;
+ return gost_grasshopper_cipher_init(ctx, key, iv, enc);
+}
+
+GRASSHOPPER_INLINE int gost_grasshopper_cipher_init_ofb(EVP_CIPHER_CTX *ctx, const unsigned char
+ *key, const unsigned char
+ *iv, int enc)
+{
+ gost_grasshopper_cipher_ctx_ofb *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
+
+ c->c.type = GRASSHOPPER_CIPHER_OFB;
+
+ grasshopper_zero128(&c->buffer1);
+
+ return gost_grasshopper_cipher_init(ctx, key, iv, enc);
+}
+
+GRASSHOPPER_INLINE int gost_grasshopper_cipher_init_cfb(EVP_CIPHER_CTX *ctx, const unsigned char
+ *key, const unsigned char
+ *iv, int enc)
+{
+ gost_grasshopper_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
+ c->type = GRASSHOPPER_CIPHER_CFB;
+ return gost_grasshopper_cipher_init(ctx, key, iv, enc);
+}
+
+GRASSHOPPER_INLINE int gost_grasshopper_cipher_init_ctr(EVP_CIPHER_CTX *ctx, const unsigned char
+ *key, const unsigned char
+ *iv, int enc)
+{
+ gost_grasshopper_cipher_ctx_ctr *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
+
+ c->c.type = GRASSHOPPER_CIPHER_CTR;
+ EVP_CIPHER_CTX_set_num(ctx, 0);
+
+ grasshopper_zero128(&c->partial_buffer);
+
+ return gost_grasshopper_cipher_init(ctx, key, iv, enc);
+}
+
+GRASSHOPPER_INLINE int gost_grasshopper_cipher_init_ctracpkm(EVP_CIPHER_CTX
+ *ctx, const unsigned
+ char *key, const unsigned
+ char *iv, int enc)
+{
+ gost_grasshopper_cipher_ctx_ctr *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
+
+ /* NB: setting type makes EVP do_cipher callback useless */
+ c->c.type = GRASSHOPPER_CIPHER_CTRACPKM;
+ EVP_CIPHER_CTX_set_num(ctx, 0);
+ c->section_size = 4096;
+
+ return gost_grasshopper_cipher_init(ctx, key, iv, enc);
+}
+
+GRASSHOPPER_INLINE int gost_grasshopper_cipher_init_mgm(EVP_CIPHER_CTX *ctx, const unsigned char
+ *key, const unsigned char
+ *iv, int enc)
+{
+ gost_grasshopper_cipher_ctx_mgm *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
+
+ c->c.type = GRASSHOPPER_CIPHER_MGM;
+ c->taglen = 16;
+ EVP_CIPHER_CTX_set_num(ctx, 0);
+
+ grasshopper_zero128(&c->partial_buffer);
+
+ return gost_grasshopper_cipher_init(ctx, key, iv, enc);
+}
+
+GRASSHOPPER_INLINE int gost_grasshopper_cipher_do(EVP_CIPHER_CTX *ctx,
+ unsigned char *out,
+ const unsigned char *in,
+ size_t inl)
+{
+ gost_grasshopper_cipher_ctx *c =
+ (gost_grasshopper_cipher_ctx *) EVP_CIPHER_CTX_get_cipher_data(ctx);
+ struct GRASSHOPPER_CIPHER_PARAMS *params = &gost_cipher_params[c->type];
+
+ return params->do_cipher(ctx, out, in, inl);
+}
+
+int gost_grasshopper_cipher_do_ecb(EVP_CIPHER_CTX *ctx, unsigned char *out,
+ const unsigned char *in, size_t inl)
+{
+ gost_grasshopper_cipher_ctx *c =
+ (gost_grasshopper_cipher_ctx *) EVP_CIPHER_CTX_get_cipher_data(ctx);
+ bool encrypting = (bool) EVP_CIPHER_CTX_encrypting(ctx);
+ const unsigned char *current_in = in;
+ unsigned char *current_out = out;
+ size_t blocks = inl / GRASSHOPPER_BLOCK_SIZE;
+ size_t i;
+
+ for (i = 0; i < blocks;
+ i++, current_in += GRASSHOPPER_BLOCK_SIZE, current_out +=
+ GRASSHOPPER_BLOCK_SIZE) {
+ if (encrypting) {
+ grasshopper_encrypt_block(&c->encrypt_round_keys,
+ (grasshopper_w128_t *) current_in,
+ (grasshopper_w128_t *) current_out,
+ &c->buffer);
+ } else {
+ grasshopper_decrypt_block(&c->decrypt_round_keys,
+ (grasshopper_w128_t *) current_in,
+ (grasshopper_w128_t *) current_out,
+ &c->buffer);
+ }