7b780772b215ffe5b84abab0bd93e003e9768785
[openssl.git] / engines / ccgost / 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 EVP_MD digest_gost = {
23     NID_id_GostR3411_94,
24     NID_undef,
25     32,
26     0,
27     gost_digest_init,
28     gost_digest_update,
29     gost_digest_final,
30     gost_digest_copy,
31     gost_digest_cleanup,
32     32,
33     sizeof(struct ossl_gost_digest_ctx),
34     NULL
35 };
36
37 int gost_digest_init(EVP_MD_CTX *ctx)
38 {
39     struct ossl_gost_digest_ctx *c = EVP_MD_CTX_md_data(ctx);
40     memset(&(c->dctx), 0, sizeof(gost_hash_ctx));
41     gost_init(&(c->cctx), &GostR3411_94_CryptoProParamSet);
42     c->dctx.cipher_ctx = &(c->cctx);
43     return 1;
44 }
45
46 int gost_digest_update(EVP_MD_CTX *ctx, const void *data, size_t count)
47 {
48     return hash_block((gost_hash_ctx *) EVP_MD_CTX_md_data(ctx), data, count);
49 }
50
51 int gost_digest_final(EVP_MD_CTX *ctx, unsigned char *md)
52 {
53     return finish_hash((gost_hash_ctx *) EVP_MD_CTX_md_data(ctx), md);
54
55 }
56
57 int gost_digest_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
58 {
59     struct ossl_gost_digest_ctx *md_ctx = EVP_MD_CTX_md_data(to);
60     if (EVP_MD_CTX_md_data(to) && EVP_MD_CTX_md_data(from)) {
61         memcpy(EVP_MD_CTX_md_data(to), EVP_MD_CTX_md_data(from),
62                sizeof(struct ossl_gost_digest_ctx));
63         md_ctx->dctx.cipher_ctx = &(md_ctx->cctx);
64     }
65     return 1;
66 }
67
68 int gost_digest_cleanup(EVP_MD_CTX *ctx)
69 {
70     if (EVP_MD_CTX_md_data(ctx))
71         memset(EVP_MD_CTX_md_data(ctx), 0, sizeof(struct ossl_gost_digest_ctx));
72     return 1;
73 }