]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost_md.c
gost_md: Rework digest registration, add GostR3411_94_digest
[openssl-gost/engine.git] / gost_md.c
1 /**********************************************************************
2  *                          md_gost.c                                 *
3  *             Copyright (c) 2005-2006 Cryptocom LTD                  *
4  *         This file is distributed under the same license as OpenSSL *
5  *                                                                    *
6  *       OpenSSL interface to GOST R 34.11-94 hash functions          *
7  *          Requires OpenSSL 0.9.9 for compilation                    *
8  **********************************************************************/
9 #include <string.h>
10 #include "gost_lcl.h"
11 #include "gosthash.h"
12 #include "e_gost_err.h"
13
14 /* implementation of GOST 34.11 hash function See gost_md.c*/
15 static int gost_digest_init(EVP_MD_CTX *ctx);
16 static int gost_digest_update(EVP_MD_CTX *ctx, const void *data,
17                               size_t count);
18 static int gost_digest_final(EVP_MD_CTX *ctx, unsigned char *md);
19 static int gost_digest_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from);
20 static int gost_digest_cleanup(EVP_MD_CTX *ctx);
21
22 GOST_digest GostR3411_94_digest = {
23     .nid = NID_id_GostR3411_94,
24     .result_size = 32,
25     .input_blocksize = 32,
26     .app_datasize = sizeof(struct ossl_gost_digest_ctx),
27     .init = gost_digest_init,
28     .update = gost_digest_update,
29     .final = gost_digest_final,
30     .copy = gost_digest_copy,
31     .cleanup = gost_digest_cleanup,
32 };
33
34 /*
35  * Single level template accessor.
36  * Note: that you cannot template 0 value.
37  */
38 #define TPL(st,field) ( \
39     ((st)->field) ?: TPL_VAL(st,field) \
40 )
41
42 #define TPL_VAL(st,field) ( \
43     ((st)->template ? (st)->template->field : 0) \
44 )
45
46 EVP_MD *GOST_init_digest(GOST_digest *d)
47 {
48     if (d->digest)
49         return d->digest;
50
51     EVP_MD *md;
52     if (!(md = EVP_MD_meth_new(d->nid, NID_undef))
53         || !EVP_MD_meth_set_result_size(md, TPL(d, result_size))
54         || !EVP_MD_meth_set_input_blocksize(md, TPL(d, input_blocksize))
55         || !EVP_MD_meth_set_app_datasize(md, TPL(d, app_datasize))
56         || !EVP_MD_meth_set_flags(md, d->flags | TPL_VAL(d, flags))
57         || !EVP_MD_meth_set_init(md, TPL(d, init))
58         || !EVP_MD_meth_set_update(md, TPL(d, update))
59         || !EVP_MD_meth_set_final(md, TPL(d, final))
60         || !EVP_MD_meth_set_copy(md, TPL(d, copy))
61         || !EVP_MD_meth_set_cleanup(md, TPL(d, cleanup))
62         || !EVP_MD_meth_set_ctrl(md, TPL(d, ctrl))) {
63         EVP_MD_meth_free(md);
64         md = NULL;
65     }
66     d->digest = md;
67     return md;
68 }
69
70 void GOST_deinit_digest(GOST_digest *d)
71 {
72     EVP_MD_meth_free(d->digest);
73     d->digest = NULL;
74 }
75
76 EVP_MD *digest_gost(void)
77 {
78     return GOST_init_digest(&GostR3411_94_digest);
79 }
80
81 void digest_gost_destroy(void)
82 {
83     GOST_deinit_digest(&GostR3411_94_digest);
84 }
85
86 int gost_digest_init(EVP_MD_CTX *ctx)
87 {
88     struct ossl_gost_digest_ctx *c = EVP_MD_CTX_md_data(ctx);
89     memset(&(c->dctx), 0, sizeof(gost_hash_ctx));
90     gost_init(&(c->cctx), &GostR3411_94_CryptoProParamSet);
91     c->dctx.cipher_ctx = &(c->cctx);
92     return 1;
93 }
94
95 int gost_digest_update(EVP_MD_CTX *ctx, const void *data, size_t count)
96 {
97     return hash_block((gost_hash_ctx *) EVP_MD_CTX_md_data(ctx), data, count);
98 }
99
100 int gost_digest_final(EVP_MD_CTX *ctx, unsigned char *md)
101 {
102     return finish_hash((gost_hash_ctx *) EVP_MD_CTX_md_data(ctx), md);
103
104 }
105
106 int gost_digest_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
107 {
108     struct ossl_gost_digest_ctx *md_ctx = EVP_MD_CTX_md_data(to);
109     if (EVP_MD_CTX_md_data(to) && EVP_MD_CTX_md_data(from)) {
110         memcpy(EVP_MD_CTX_md_data(to), EVP_MD_CTX_md_data(from),
111                sizeof(struct ossl_gost_digest_ctx));
112         md_ctx->dctx.cipher_ctx = &(md_ctx->cctx);
113     }
114     return 1;
115 }
116
117 int gost_digest_cleanup(EVP_MD_CTX *ctx)
118 {
119     if (EVP_MD_CTX_md_data(ctx))
120         memset(EVP_MD_CTX_md_data(ctx), 0,
121                sizeof(struct ossl_gost_digest_ctx));
122     return 1;
123 }
124 /* vim: set expandtab cinoptions=\:0,l1,t0,g0,(0 sw=4 : */