1238384Sjkim/**********************************************************************
2238384Sjkim *                          gost_crypt.c                              *
3238384Sjkim *             Copyright (c) 2005-2006 Cryptocom LTD                  *
4238384Sjkim *         This file is distributed under the same license as OpenSSL *
5238384Sjkim *                                                                    *
6238384Sjkim *       OpenSSL interface to GOST 28147-89 cipher functions          *
7238384Sjkim *          Requires OpenSSL 0.9.9 for compilation                    *
8238384Sjkim **********************************************************************/
9238384Sjkim#include <string.h>
10238384Sjkim#include "gost89.h"
11238384Sjkim#include <openssl/rand.h>
12238384Sjkim#include "e_gost_err.h"
13238384Sjkim#include "gost_lcl.h"
14246772Sjkim
15246772Sjkim#if !defined(CCGOST_DEBUG) && !defined(DEBUG)
16246772Sjkim# ifndef NDEBUG
17246772Sjkim#  define NDEBUG
18246772Sjkim# endif
19246772Sjkim#endif
20246772Sjkim#include <assert.h>
21246772Sjkim
22296341Sdelphijstatic int gost_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
23296341Sdelphij                            const unsigned char *iv, int enc);
24296341Sdelphijstatic int gost_cipher_init_cpa(EVP_CIPHER_CTX *ctx, const unsigned char *key,
25296341Sdelphij                                const unsigned char *iv, int enc);
26296341Sdelphij/* Handles block of data in CFB mode */
27296341Sdelphijstatic int gost_cipher_do_cfb(EVP_CIPHER_CTX *ctx, unsigned char *out,
28296341Sdelphij                              const unsigned char *in, size_t inl);
29296341Sdelphij/* Handles block of data in CNT mode */
30296341Sdelphijstatic int gost_cipher_do_cnt(EVP_CIPHER_CTX *ctx, unsigned char *out,
31296341Sdelphij                              const unsigned char *in, size_t inl);
32296341Sdelphij/* Cleanup function */
33238384Sjkimstatic int gost_cipher_cleanup(EVP_CIPHER_CTX *);
34238384Sjkim/* set/get cipher parameters */
35296341Sdelphijstatic int gost89_set_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params);
36296341Sdelphijstatic int gost89_get_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params);
37238384Sjkim/* Control function */
38296341Sdelphijstatic int gost_cipher_ctl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr);
39238384Sjkim
40296341SdelphijEVP_CIPHER cipher_gost = {
41296341Sdelphij    NID_id_Gost28147_89,
42296341Sdelphij    1,                          /* block_size */
43296341Sdelphij    32,                         /* key_size */
44296341Sdelphij    8,                          /* iv_len */
45296341Sdelphij    EVP_CIPH_CFB_MODE | EVP_CIPH_NO_PADDING |
46296341Sdelphij        EVP_CIPH_CUSTOM_IV | EVP_CIPH_RAND_KEY | EVP_CIPH_ALWAYS_CALL_INIT,
47296341Sdelphij    gost_cipher_init,
48296341Sdelphij    gost_cipher_do_cfb,
49296341Sdelphij    gost_cipher_cleanup,
50296341Sdelphij    sizeof(struct ossl_gost_cipher_ctx), /* ctx_size */
51296341Sdelphij    gost89_set_asn1_parameters,
52296341Sdelphij    gost89_get_asn1_parameters,
53296341Sdelphij    gost_cipher_ctl,
54296341Sdelphij    NULL,
55296341Sdelphij};
56238384Sjkim
57296341SdelphijEVP_CIPHER cipher_gost_cpacnt = {
58296341Sdelphij    NID_gost89_cnt,
59296341Sdelphij    1,                          /* block_size */
60296341Sdelphij    32,                         /* key_size */
61296341Sdelphij    8,                          /* iv_len */
62296341Sdelphij    EVP_CIPH_OFB_MODE | EVP_CIPH_NO_PADDING |
63296341Sdelphij        EVP_CIPH_CUSTOM_IV | EVP_CIPH_RAND_KEY | EVP_CIPH_ALWAYS_CALL_INIT,
64296341Sdelphij    gost_cipher_init_cpa,
65296341Sdelphij    gost_cipher_do_cnt,
66296341Sdelphij    gost_cipher_cleanup,
67296341Sdelphij    sizeof(struct ossl_gost_cipher_ctx), /* ctx_size */
68296341Sdelphij    gost89_set_asn1_parameters,
69296341Sdelphij    gost89_get_asn1_parameters,
70296341Sdelphij    gost_cipher_ctl,
71296341Sdelphij    NULL,
72296341Sdelphij};
73238384Sjkim
74238384Sjkim/* Implementation of GOST 28147-89 in MAC (imitovstavka) mode */
75238384Sjkim/* Init functions which set specific parameters */
76238384Sjkimstatic int gost_imit_init_cpa(EVP_MD_CTX *ctx);
77238384Sjkim/* process block of data */
78238384Sjkimstatic int gost_imit_update(EVP_MD_CTX *ctx, const void *data, size_t count);
79238384Sjkim/* Return computed value */
80296341Sdelphijstatic int gost_imit_final(EVP_MD_CTX *ctx, unsigned char *md);
81238384Sjkim/* Copies context */
82296341Sdelphijstatic int gost_imit_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from);
83238384Sjkimstatic int gost_imit_cleanup(EVP_MD_CTX *ctx);
84238384Sjkim/* Control function, knows how to set MAC key.*/
85296341Sdelphijstatic int gost_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr);
86238384Sjkim
87296341SdelphijEVP_MD imit_gost_cpa = {
88296341Sdelphij    NID_id_Gost28147_89_MAC,
89296341Sdelphij    NID_undef,
90296341Sdelphij    4,
91296341Sdelphij    0,
92296341Sdelphij    gost_imit_init_cpa,
93296341Sdelphij    gost_imit_update,
94296341Sdelphij    gost_imit_final,
95296341Sdelphij    gost_imit_copy,
96296341Sdelphij    gost_imit_cleanup,
97296341Sdelphij    NULL,
98296341Sdelphij    NULL,
99296341Sdelphij    {0, 0, 0, 0, 0},
100296341Sdelphij    8,
101296341Sdelphij    sizeof(struct ossl_gost_imit_ctx),
102296341Sdelphij    gost_imit_ctrl
103296341Sdelphij};
104238384Sjkim
105296341Sdelphij/*
106238384Sjkim * Correspondence between gost parameter OIDs and substitution blocks
107238384Sjkim * NID field is filed by register_gost_NID function in engine.c
108238384Sjkim * upon engine initialization
109238384Sjkim */
110238384Sjkim
111296341Sdelphijstruct gost_cipher_info gost_cipher_list[] = {
112296341Sdelphij    /*- NID *//*
113296341Sdelphij     * Subst block
114296341Sdelphij     *//*
115296341Sdelphij     * Key meshing
116296341Sdelphij     */
117296341Sdelphij    /*
118296341Sdelphij     * {NID_id_GostR3411_94_CryptoProParamSet,&GostR3411_94_CryptoProParamSet,0},
119296341Sdelphij     */
120296341Sdelphij    {NID_id_Gost28147_89_cc, &GostR3411_94_CryptoProParamSet, 0},
121296341Sdelphij    {NID_id_Gost28147_89_CryptoPro_A_ParamSet, &Gost28147_CryptoProParamSetA,
122296341Sdelphij     1},
123296341Sdelphij    {NID_id_Gost28147_89_CryptoPro_B_ParamSet, &Gost28147_CryptoProParamSetB,
124296341Sdelphij     1},
125296341Sdelphij    {NID_id_Gost28147_89_CryptoPro_C_ParamSet, &Gost28147_CryptoProParamSetC,
126296341Sdelphij     1},
127296341Sdelphij    {NID_id_Gost28147_89_CryptoPro_D_ParamSet, &Gost28147_CryptoProParamSetD,
128296341Sdelphij     1},
129296341Sdelphij    {NID_id_Gost28147_89_TestParamSet, &Gost28147_TestParamSet, 1},
130296341Sdelphij    {NID_undef, NULL, 0}
131296341Sdelphij};
132238384Sjkim
133296341Sdelphij/*
134296341Sdelphij * get encryption parameters from crypto network settings FIXME For now we
135296341Sdelphij * use environment var CRYPT_PARAMS as place to store these settings.
136296341Sdelphij * Actually, it is better to use engine control command, read from
137296341Sdelphij * configuration file to set them
138296341Sdelphij */
139238384Sjkimconst struct gost_cipher_info *get_encryption_params(ASN1_OBJECT *obj)
140296341Sdelphij{
141296341Sdelphij    int nid;
142296341Sdelphij    struct gost_cipher_info *param;
143296341Sdelphij    if (!obj) {
144296341Sdelphij        const char *params = get_gost_engine_param(GOST_PARAM_CRYPT_PARAMS);
145296341Sdelphij        if (!params || !strlen(params))
146296341Sdelphij            return &gost_cipher_list[1];
147238384Sjkim
148296341Sdelphij        nid = OBJ_txt2nid(params);
149296341Sdelphij        if (nid == NID_undef) {
150296341Sdelphij            GOSTerr(GOST_F_GET_ENCRYPTION_PARAMS,
151296341Sdelphij                    GOST_R_INVALID_CIPHER_PARAM_OID);
152296341Sdelphij            return NULL;
153296341Sdelphij        }
154296341Sdelphij    } else {
155296341Sdelphij        nid = OBJ_obj2nid(obj);
156296341Sdelphij    }
157296341Sdelphij    for (param = gost_cipher_list; param->sblock != NULL && param->nid != nid;
158296341Sdelphij         param++) ;
159296341Sdelphij    if (!param->sblock) {
160296341Sdelphij        GOSTerr(GOST_F_GET_ENCRYPTION_PARAMS, GOST_R_INVALID_CIPHER_PARAMS);
161296341Sdelphij        return NULL;
162296341Sdelphij    }
163296341Sdelphij    return param;
164296341Sdelphij}
165238384Sjkim
166238384Sjkim/* Sets cipher param from paramset NID. */
167296341Sdelphijstatic int gost_cipher_set_param(struct ossl_gost_cipher_ctx *c, int nid)
168296341Sdelphij{
169296341Sdelphij    const struct gost_cipher_info *param;
170296341Sdelphij    param =
171296341Sdelphij        get_encryption_params((nid == NID_undef ? NULL : OBJ_nid2obj(nid)));
172296341Sdelphij    if (!param)
173296341Sdelphij        return 0;
174238384Sjkim
175296341Sdelphij    c->paramNID = param->nid;
176296341Sdelphij    c->key_meshing = param->key_meshing;
177296341Sdelphij    c->count = 0;
178296341Sdelphij    gost_init(&(c->cctx), param->sblock);
179296341Sdelphij    return 1;
180296341Sdelphij}
181296341Sdelphij
182238384Sjkim/* Initializes EVP_CIPHER_CTX by paramset NID */
183296341Sdelphijstatic int gost_cipher_init_param(EVP_CIPHER_CTX *ctx,
184296341Sdelphij                                  const unsigned char *key,
185296341Sdelphij                                  const unsigned char *iv, int enc,
186296341Sdelphij                                  int paramNID, int mode)
187296341Sdelphij{
188296341Sdelphij    struct ossl_gost_cipher_ctx *c = ctx->cipher_data;
189296341Sdelphij    if (ctx->app_data == NULL) {
190296341Sdelphij        if (!gost_cipher_set_param(c, paramNID))
191296341Sdelphij            return 0;
192296341Sdelphij        ctx->app_data = ctx->cipher_data;
193296341Sdelphij    }
194296341Sdelphij    if (key)
195296341Sdelphij        gost_key(&(c->cctx), key);
196296341Sdelphij    if (iv)
197296341Sdelphij        memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
198296341Sdelphij    memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx));
199296341Sdelphij    return 1;
200296341Sdelphij}
201238384Sjkim
202238384Sjkimstatic int gost_cipher_init_cpa(EVP_CIPHER_CTX *ctx, const unsigned char *key,
203296341Sdelphij                                const unsigned char *iv, int enc)
204296341Sdelphij{
205296341Sdelphij    struct ossl_gost_cipher_ctx *c = ctx->cipher_data;
206296341Sdelphij    gost_init(&(c->cctx), &Gost28147_CryptoProParamSetA);
207296341Sdelphij    c->key_meshing = 1;
208296341Sdelphij    c->count = 0;
209296341Sdelphij    if (key)
210296341Sdelphij        gost_key(&(c->cctx), key);
211296341Sdelphij    if (iv)
212296341Sdelphij        memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
213296341Sdelphij    memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx));
214296341Sdelphij    return 1;
215296341Sdelphij}
216238384Sjkim
217238384Sjkim/* Initializes EVP_CIPHER_CTX with default values */
218238384Sjkimint gost_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
219296341Sdelphij                     const unsigned char *iv, int enc)
220296341Sdelphij{
221296341Sdelphij    return gost_cipher_init_param(ctx, key, iv, enc, NID_undef,
222296341Sdelphij                                  EVP_CIPH_CFB_MODE);
223296341Sdelphij}
224296341Sdelphij
225296341Sdelphij/*
226296341Sdelphij * Wrapper around gostcrypt function from gost89.c which perform key meshing
227296341Sdelphij * when nesseccary
228238384Sjkim */
229296341Sdelphijstatic void gost_crypt_mesh(void *ctx, unsigned char *iv, unsigned char *buf)
230296341Sdelphij{
231296341Sdelphij    struct ossl_gost_cipher_ctx *c = ctx;
232296341Sdelphij    assert(c->count % 8 == 0 && c->count <= 1024);
233296341Sdelphij    if (c->key_meshing && c->count == 1024) {
234296341Sdelphij        cryptopro_key_meshing(&(c->cctx), iv);
235296341Sdelphij    }
236296341Sdelphij    gostcrypt(&(c->cctx), iv, buf);
237296341Sdelphij    c->count = c->count % 1024 + 8;
238296341Sdelphij}
239238384Sjkim
240296341Sdelphijstatic void gost_cnt_next(void *ctx, unsigned char *iv, unsigned char *buf)
241296341Sdelphij{
242296341Sdelphij    struct ossl_gost_cipher_ctx *c = ctx;
243296341Sdelphij    word32 g, go;
244296341Sdelphij    unsigned char buf1[8];
245296341Sdelphij    assert(c->count % 8 == 0 && c->count <= 1024);
246296341Sdelphij    if (c->key_meshing && c->count == 1024) {
247296341Sdelphij        cryptopro_key_meshing(&(c->cctx), iv);
248296341Sdelphij    }
249296341Sdelphij    if (c->count == 0) {
250296341Sdelphij        gostcrypt(&(c->cctx), iv, buf1);
251296341Sdelphij    } else {
252296341Sdelphij        memcpy(buf1, iv, 8);
253296341Sdelphij    }
254296341Sdelphij    g = buf1[0] | (buf1[1] << 8) | (buf1[2] << 16) | (buf1[3] << 24);
255296341Sdelphij    g += 0x01010101;
256296341Sdelphij    buf1[0] = (unsigned char)(g & 0xff);
257296341Sdelphij    buf1[1] = (unsigned char)((g >> 8) & 0xff);
258296341Sdelphij    buf1[2] = (unsigned char)((g >> 16) & 0xff);
259296341Sdelphij    buf1[3] = (unsigned char)((g >> 24) & 0xff);
260296341Sdelphij    g = buf1[4] | (buf1[5] << 8) | (buf1[6] << 16) | (buf1[7] << 24);
261296341Sdelphij    go = g;
262296341Sdelphij    g += 0x01010104;
263296341Sdelphij    if (go > g)                 /* overflow */
264296341Sdelphij        g++;
265296341Sdelphij    buf1[4] = (unsigned char)(g & 0xff);
266296341Sdelphij    buf1[5] = (unsigned char)((g >> 8) & 0xff);
267296341Sdelphij    buf1[6] = (unsigned char)((g >> 16) & 0xff);
268296341Sdelphij    buf1[7] = (unsigned char)((g >> 24) & 0xff);
269296341Sdelphij    memcpy(iv, buf1, 8);
270296341Sdelphij    gostcrypt(&(c->cctx), buf1, buf);
271296341Sdelphij    c->count = c->count % 1024 + 8;
272296341Sdelphij}
273238384Sjkim
274238384Sjkim/* GOST encryption in CFB mode */
275296341Sdelphijint gost_cipher_do_cfb(EVP_CIPHER_CTX *ctx, unsigned char *out,
276296341Sdelphij                       const unsigned char *in, size_t inl)
277296341Sdelphij{
278296341Sdelphij    const unsigned char *in_ptr = in;
279296341Sdelphij    unsigned char *out_ptr = out;
280296341Sdelphij    size_t i = 0;
281296341Sdelphij    size_t j = 0;
282238384Sjkim/* process partial block if any */
283296341Sdelphij    if (ctx->num) {
284296341Sdelphij        for (j = ctx->num, i = 0; j < 8 && i < inl;
285296341Sdelphij             j++, i++, in_ptr++, out_ptr++) {
286296341Sdelphij            if (!ctx->encrypt)
287296341Sdelphij                ctx->buf[j + 8] = *in_ptr;
288296341Sdelphij            *out_ptr = ctx->buf[j] ^ (*in_ptr);
289296341Sdelphij            if (ctx->encrypt)
290296341Sdelphij                ctx->buf[j + 8] = *out_ptr;
291296341Sdelphij        }
292296341Sdelphij        if (j == 8) {
293296341Sdelphij            memcpy(ctx->iv, ctx->buf + 8, 8);
294296341Sdelphij            ctx->num = 0;
295296341Sdelphij        } else {
296296341Sdelphij            ctx->num = j;
297296341Sdelphij            return 1;
298296341Sdelphij        }
299296341Sdelphij    }
300238384Sjkim
301296341Sdelphij    for (; i + 8 < inl; i += 8, in_ptr += 8, out_ptr += 8) {
302296341Sdelphij        /*
303296341Sdelphij         * block cipher current iv
304296341Sdelphij         */
305296341Sdelphij        gost_crypt_mesh(ctx->cipher_data, ctx->iv, ctx->buf);
306296341Sdelphij        /*
307296341Sdelphij         * xor next block of input text with it and output it
308296341Sdelphij         */
309296341Sdelphij        /*
310296341Sdelphij         * output this block
311296341Sdelphij         */
312296341Sdelphij        if (!ctx->encrypt)
313296341Sdelphij            memcpy(ctx->iv, in_ptr, 8);
314296341Sdelphij        for (j = 0; j < 8; j++) {
315296341Sdelphij            out_ptr[j] = ctx->buf[j] ^ in_ptr[j];
316296341Sdelphij        }
317296341Sdelphij        /* Encrypt */
318296341Sdelphij        /* Next iv is next block of cipher text */
319296341Sdelphij        if (ctx->encrypt)
320296341Sdelphij            memcpy(ctx->iv, out_ptr, 8);
321296341Sdelphij    }
322238384Sjkim/* Process rest of buffer */
323296341Sdelphij    if (i < inl) {
324296341Sdelphij        gost_crypt_mesh(ctx->cipher_data, ctx->iv, ctx->buf);
325296341Sdelphij        if (!ctx->encrypt)
326296341Sdelphij            memcpy(ctx->buf + 8, in_ptr, inl - i);
327296341Sdelphij        for (j = 0; i < inl; j++, i++) {
328296341Sdelphij            out_ptr[j] = ctx->buf[j] ^ in_ptr[j];
329296341Sdelphij        }
330296341Sdelphij        ctx->num = j;
331296341Sdelphij        if (ctx->encrypt)
332296341Sdelphij            memcpy(ctx->buf + 8, out_ptr, j);
333296341Sdelphij    } else {
334296341Sdelphij        ctx->num = 0;
335296341Sdelphij    }
336296341Sdelphij    return 1;
337296341Sdelphij}
338238384Sjkim
339238384Sjkimstatic int gost_cipher_do_cnt(EVP_CIPHER_CTX *ctx, unsigned char *out,
340296341Sdelphij                              const unsigned char *in, size_t inl)
341296341Sdelphij{
342296341Sdelphij    const unsigned char *in_ptr = in;
343296341Sdelphij    unsigned char *out_ptr = out;
344296341Sdelphij    size_t i = 0;
345296341Sdelphij    size_t j;
346238384Sjkim/* process partial block if any */
347296341Sdelphij    if (ctx->num) {
348296341Sdelphij        for (j = ctx->num, i = 0; j < 8 && i < inl;
349296341Sdelphij             j++, i++, in_ptr++, out_ptr++) {
350296341Sdelphij            *out_ptr = ctx->buf[j] ^ (*in_ptr);
351296341Sdelphij        }
352296341Sdelphij        if (j == 8) {
353296341Sdelphij            ctx->num = 0;
354296341Sdelphij        } else {
355296341Sdelphij            ctx->num = j;
356296341Sdelphij            return 1;
357296341Sdelphij        }
358296341Sdelphij    }
359238384Sjkim
360296341Sdelphij    for (; i + 8 < inl; i += 8, in_ptr += 8, out_ptr += 8) {
361296341Sdelphij        /*
362296341Sdelphij         * block cipher current iv
363296341Sdelphij         */
364296341Sdelphij        /* Encrypt */
365296341Sdelphij        gost_cnt_next(ctx->cipher_data, ctx->iv, ctx->buf);
366296341Sdelphij        /*
367296341Sdelphij         * xor next block of input text with it and output it
368296341Sdelphij         */
369296341Sdelphij        /*
370296341Sdelphij         * output this block
371296341Sdelphij         */
372296341Sdelphij        for (j = 0; j < 8; j++) {
373296341Sdelphij            out_ptr[j] = ctx->buf[j] ^ in_ptr[j];
374296341Sdelphij        }
375296341Sdelphij    }
376238384Sjkim/* Process rest of buffer */
377296341Sdelphij    if (i < inl) {
378296341Sdelphij        gost_cnt_next(ctx->cipher_data, ctx->iv, ctx->buf);
379296341Sdelphij        for (j = 0; i < inl; j++, i++) {
380296341Sdelphij            out_ptr[j] = ctx->buf[j] ^ in_ptr[j];
381296341Sdelphij        }
382296341Sdelphij        ctx->num = j;
383296341Sdelphij    } else {
384296341Sdelphij        ctx->num = 0;
385296341Sdelphij    }
386296341Sdelphij    return 1;
387296341Sdelphij}
388238384Sjkim
389238384Sjkim/* Cleaning up of EVP_CIPHER_CTX */
390296341Sdelphijint gost_cipher_cleanup(EVP_CIPHER_CTX *ctx)
391296341Sdelphij{
392296341Sdelphij    gost_destroy(&((struct ossl_gost_cipher_ctx *)ctx->cipher_data)->cctx);
393296341Sdelphij    ctx->app_data = NULL;
394296341Sdelphij    return 1;
395296341Sdelphij}
396238384Sjkim
397238384Sjkim/* Control function for gost cipher */
398296341Sdelphijint gost_cipher_ctl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
399296341Sdelphij{
400296341Sdelphij    switch (type) {
401296341Sdelphij    case EVP_CTRL_RAND_KEY:
402296341Sdelphij        {
403296341Sdelphij            if (RAND_bytes((unsigned char *)ptr, ctx->key_len) <= 0) {
404296341Sdelphij                GOSTerr(GOST_F_GOST_CIPHER_CTL,
405296341Sdelphij                        GOST_R_RANDOM_GENERATOR_ERROR);
406296341Sdelphij                return -1;
407296341Sdelphij            }
408296341Sdelphij            break;
409296341Sdelphij        }
410296341Sdelphij    case EVP_CTRL_PBE_PRF_NID:
411296341Sdelphij        if (ptr) {
412296341Sdelphij            *((int *)ptr) = NID_id_HMACGostR3411_94;
413296341Sdelphij            return 1;
414296341Sdelphij        } else {
415296341Sdelphij            return 0;
416296341Sdelphij        }
417238384Sjkim
418296341Sdelphij    default:
419296341Sdelphij        GOSTerr(GOST_F_GOST_CIPHER_CTL,
420296341Sdelphij                GOST_R_UNSUPPORTED_CIPHER_CTL_COMMAND);
421296341Sdelphij        return -1;
422296341Sdelphij    }
423296341Sdelphij    return 1;
424296341Sdelphij}
425296341Sdelphij
426238384Sjkim/* Set cipher parameters from ASN1 structure */
427296341Sdelphijint gost89_set_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
428296341Sdelphij{
429296341Sdelphij    int len = 0;
430296341Sdelphij    unsigned char *buf = NULL;
431296341Sdelphij    unsigned char *p = NULL;
432296341Sdelphij    struct ossl_gost_cipher_ctx *c = ctx->cipher_data;
433296341Sdelphij    GOST_CIPHER_PARAMS *gcp = GOST_CIPHER_PARAMS_new();
434296341Sdelphij    ASN1_OCTET_STRING *os = NULL;
435296341Sdelphij    if (!gcp) {
436296341Sdelphij        GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, GOST_R_NO_MEMORY);
437296341Sdelphij        return 0;
438296341Sdelphij    }
439296341Sdelphij    if (!ASN1_OCTET_STRING_set(gcp->iv, ctx->iv, ctx->cipher->iv_len)) {
440296341Sdelphij        GOST_CIPHER_PARAMS_free(gcp);
441296341Sdelphij        GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, GOST_R_NO_MEMORY);
442296341Sdelphij        return 0;
443296341Sdelphij    }
444296341Sdelphij    ASN1_OBJECT_free(gcp->enc_param_set);
445296341Sdelphij    gcp->enc_param_set = OBJ_nid2obj(c->paramNID);
446238384Sjkim
447296341Sdelphij    len = i2d_GOST_CIPHER_PARAMS(gcp, NULL);
448296341Sdelphij    p = buf = (unsigned char *)OPENSSL_malloc(len);
449296341Sdelphij    if (!buf) {
450296341Sdelphij        GOST_CIPHER_PARAMS_free(gcp);
451296341Sdelphij        GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, GOST_R_NO_MEMORY);
452296341Sdelphij        return 0;
453296341Sdelphij    }
454296341Sdelphij    i2d_GOST_CIPHER_PARAMS(gcp, &p);
455296341Sdelphij    GOST_CIPHER_PARAMS_free(gcp);
456238384Sjkim
457296341Sdelphij    os = ASN1_OCTET_STRING_new();
458238384Sjkim
459296341Sdelphij    if (!os || !ASN1_OCTET_STRING_set(os, buf, len)) {
460296341Sdelphij        OPENSSL_free(buf);
461296341Sdelphij        GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, GOST_R_NO_MEMORY);
462296341Sdelphij        return 0;
463296341Sdelphij    }
464296341Sdelphij    OPENSSL_free(buf);
465238384Sjkim
466296341Sdelphij    ASN1_TYPE_set(params, V_ASN1_SEQUENCE, os);
467296341Sdelphij    return 1;
468296341Sdelphij}
469238384Sjkim
470238384Sjkim/* Store parameters into ASN1 structure */
471296341Sdelphijint gost89_get_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
472296341Sdelphij{
473296341Sdelphij    int ret = -1;
474296341Sdelphij    int len;
475296341Sdelphij    GOST_CIPHER_PARAMS *gcp = NULL;
476296341Sdelphij    unsigned char *p;
477296341Sdelphij    struct ossl_gost_cipher_ctx *c = ctx->cipher_data;
478296341Sdelphij    if (ASN1_TYPE_get(params) != V_ASN1_SEQUENCE) {
479296341Sdelphij        return ret;
480296341Sdelphij    }
481238384Sjkim
482296341Sdelphij    p = params->value.sequence->data;
483238384Sjkim
484296341Sdelphij    gcp = d2i_GOST_CIPHER_PARAMS(NULL, (const unsigned char **)&p,
485296341Sdelphij                                 params->value.sequence->length);
486238384Sjkim
487296341Sdelphij    len = gcp->iv->length;
488296341Sdelphij    if (len != ctx->cipher->iv_len) {
489296341Sdelphij        GOST_CIPHER_PARAMS_free(gcp);
490296341Sdelphij        GOSTerr(GOST_F_GOST89_GET_ASN1_PARAMETERS, GOST_R_INVALID_IV_LENGTH);
491296341Sdelphij        return -1;
492296341Sdelphij    }
493296341Sdelphij    if (!gost_cipher_set_param(c, OBJ_obj2nid(gcp->enc_param_set))) {
494296341Sdelphij        GOST_CIPHER_PARAMS_free(gcp);
495296341Sdelphij        return -1;
496296341Sdelphij    }
497296341Sdelphij    memcpy(ctx->oiv, gcp->iv->data, len);
498238384Sjkim
499296341Sdelphij    GOST_CIPHER_PARAMS_free(gcp);
500238384Sjkim
501296341Sdelphij    return 1;
502296341Sdelphij}
503238384Sjkim
504238384Sjkimint gost_imit_init_cpa(EVP_MD_CTX *ctx)
505296341Sdelphij{
506296341Sdelphij    struct ossl_gost_imit_ctx *c = ctx->md_data;
507296341Sdelphij    memset(c->buffer, 0, sizeof(c->buffer));
508296341Sdelphij    memset(c->partial_block, 0, sizeof(c->partial_block));
509296341Sdelphij    c->count = 0;
510296341Sdelphij    c->bytes_left = 0;
511296341Sdelphij    c->key_meshing = 1;
512296341Sdelphij    gost_init(&(c->cctx), &Gost28147_CryptoProParamSetA);
513296341Sdelphij    return 1;
514296341Sdelphij}
515238384Sjkim
516296341Sdelphijstatic void mac_block_mesh(struct ossl_gost_imit_ctx *c,
517296341Sdelphij                           const unsigned char *data)
518296341Sdelphij{
519296341Sdelphij    unsigned char buffer[8];
520296341Sdelphij    /*
521296341Sdelphij     * We are using local buffer for iv because CryptoPro doesn't interpret
522296341Sdelphij     * internal state of MAC algorithm as iv during keymeshing (but does
523296341Sdelphij     * initialize internal state from iv in key transport
524296341Sdelphij     */
525296341Sdelphij    assert(c->count % 8 == 0 && c->count <= 1024);
526296341Sdelphij    if (c->key_meshing && c->count == 1024) {
527296341Sdelphij        cryptopro_key_meshing(&(c->cctx), buffer);
528296341Sdelphij    }
529296341Sdelphij    mac_block(&(c->cctx), c->buffer, data);
530296341Sdelphij    c->count = c->count % 1024 + 8;
531296341Sdelphij}
532238384Sjkim
533238384Sjkimint gost_imit_update(EVP_MD_CTX *ctx, const void *data, size_t count)
534296341Sdelphij{
535296341Sdelphij    struct ossl_gost_imit_ctx *c = ctx->md_data;
536296341Sdelphij    const unsigned char *p = data;
537296341Sdelphij    size_t bytes = count, i;
538296341Sdelphij    if (!(c->key_set)) {
539296341Sdelphij        GOSTerr(GOST_F_GOST_IMIT_UPDATE, GOST_R_MAC_KEY_NOT_SET);
540296341Sdelphij        return 0;
541296341Sdelphij    }
542296341Sdelphij    if (c->bytes_left) {
543296341Sdelphij        for (i = c->bytes_left; i < 8 && bytes > 0; bytes--, i++, p++) {
544296341Sdelphij            c->partial_block[i] = *p;
545296341Sdelphij        }
546296341Sdelphij        if (i == 8) {
547296341Sdelphij            mac_block_mesh(c, c->partial_block);
548296341Sdelphij        } else {
549296341Sdelphij            c->bytes_left = i;
550296341Sdelphij            return 1;
551296341Sdelphij        }
552296341Sdelphij    }
553296341Sdelphij    while (bytes > 8) {
554296341Sdelphij        mac_block_mesh(c, p);
555296341Sdelphij        p += 8;
556296341Sdelphij        bytes -= 8;
557296341Sdelphij    }
558296341Sdelphij    if (bytes > 0) {
559296341Sdelphij        memcpy(c->partial_block, p, bytes);
560296341Sdelphij    }
561296341Sdelphij    c->bytes_left = bytes;
562296341Sdelphij    return 1;
563296341Sdelphij}
564238384Sjkim
565296341Sdelphijint gost_imit_final(EVP_MD_CTX *ctx, unsigned char *md)
566296341Sdelphij{
567296341Sdelphij    struct ossl_gost_imit_ctx *c = ctx->md_data;
568296341Sdelphij    if (!c->key_set) {
569296341Sdelphij        GOSTerr(GOST_F_GOST_IMIT_FINAL, GOST_R_MAC_KEY_NOT_SET);
570296341Sdelphij        return 0;
571296341Sdelphij    }
572296341Sdelphij    if (c->count == 0 && c->bytes_left) {
573296341Sdelphij        unsigned char buffer[8];
574296341Sdelphij        memset(buffer, 0, 8);
575296341Sdelphij        gost_imit_update(ctx, buffer, 8);
576296341Sdelphij    }
577296341Sdelphij    if (c->bytes_left) {
578296341Sdelphij        int i;
579296341Sdelphij        for (i = c->bytes_left; i < 8; i++) {
580296341Sdelphij            c->partial_block[i] = 0;
581296341Sdelphij        }
582296341Sdelphij        mac_block_mesh(c, c->partial_block);
583296341Sdelphij    }
584296341Sdelphij    get_mac(c->buffer, 32, md);
585296341Sdelphij    return 1;
586296341Sdelphij}
587238384Sjkim
588296341Sdelphijint gost_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr)
589296341Sdelphij{
590296341Sdelphij    switch (type) {
591296341Sdelphij    case EVP_MD_CTRL_KEY_LEN:
592296341Sdelphij        *((unsigned int *)(ptr)) = 32;
593296341Sdelphij        return 1;
594296341Sdelphij    case EVP_MD_CTRL_SET_KEY:
595296341Sdelphij        {
596296341Sdelphij            if (arg != 32) {
597296341Sdelphij                GOSTerr(GOST_F_GOST_IMIT_CTRL, GOST_R_INVALID_MAC_KEY_LENGTH);
598296341Sdelphij                return 0;
599296341Sdelphij            }
600238384Sjkim
601296341Sdelphij            gost_key(&(((struct ossl_gost_imit_ctx *)(ctx->md_data))->cctx),
602296341Sdelphij                     ptr);
603296341Sdelphij            ((struct ossl_gost_imit_ctx *)(ctx->md_data))->key_set = 1;
604296341Sdelphij            return 1;
605238384Sjkim
606296341Sdelphij        }
607296341Sdelphij    default:
608296341Sdelphij        return 0;
609296341Sdelphij    }
610296341Sdelphij}
611238384Sjkim
612296341Sdelphijint gost_imit_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
613296341Sdelphij{
614296341Sdelphij    memcpy(to->md_data, from->md_data, sizeof(struct ossl_gost_imit_ctx));
615296341Sdelphij    return 1;
616296341Sdelphij}
617238384Sjkim
618238384Sjkim/* Clean up imit ctx */
619238384Sjkimint gost_imit_cleanup(EVP_MD_CTX *ctx)
620296341Sdelphij{
621296341Sdelphij    memset(ctx->md_data, 0, sizeof(struct ossl_gost_imit_ctx));
622296341Sdelphij    return 1;
623296341Sdelphij}
624