]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost_md.c
Merge branch 'magma_impl' into openssl_1_0_2_alt
[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 static EVP_MD *_hidden_GostR3411_94_md = NULL;
23
24 EVP_MD *digest_gost(void)
25 {
26     if (_hidden_GostR3411_94_md == NULL) {
27         EVP_MD *md;
28
29         if ((md = EVP_MD_meth_new(NID_id_GostR3411_94, NID_undef)) == NULL
30             || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_PKEY_METHOD_SIGNATURE)
31             || !EVP_MD_meth_set_result_size(md, 32)
32             || !EVP_MD_meth_set_input_blocksize(md, 32)
33             || !EVP_MD_meth_set_app_datasize(md,
34                                              sizeof(struct
35                                                     ossl_gost_digest_ctx))
36             || !EVP_MD_meth_set_init(md, gost_digest_init)
37             || !EVP_MD_meth_set_update(md, gost_digest_update)
38             || !EVP_MD_meth_set_final(md, gost_digest_final)
39             || !EVP_MD_meth_set_copy(md, gost_digest_copy)
40             || !EVP_MD_meth_set_cleanup(md, gost_digest_cleanup)) {
41             EVP_MD_meth_free(md);
42             md = NULL;
43         }
44         _hidden_GostR3411_94_md = md;
45     }
46     return _hidden_GostR3411_94_md;
47 }
48
49 void digest_gost_destroy(void)
50 {
51     EVP_MD_meth_free(_hidden_GostR3411_94_md);
52     _hidden_GostR3411_94_md = NULL;
53 }
54
55 int gost_digest_init(EVP_MD_CTX *ctx)
56 {
57     struct ossl_gost_digest_ctx *c = EVP_MD_CTX_md_data(ctx);
58     memset(&(c->dctx), 0, sizeof(gost_hash_ctx));
59     gost_init(&(c->cctx), &GostR3411_94_CryptoProParamSet);
60     c->dctx.cipher_ctx = &(c->cctx);
61     return 1;
62 }
63
64 int gost_digest_update(EVP_MD_CTX *ctx, const void *data, size_t count)
65 {
66     return hash_block((gost_hash_ctx *) EVP_MD_CTX_md_data(ctx), data, count);
67 }
68
69 int gost_digest_final(EVP_MD_CTX *ctx, unsigned char *md)
70 {
71     return finish_hash((gost_hash_ctx *) EVP_MD_CTX_md_data(ctx), md);
72
73 }
74
75 int gost_digest_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
76 {
77     struct ossl_gost_digest_ctx *md_ctx = EVP_MD_CTX_md_data(to);
78     if (EVP_MD_CTX_md_data(to) && EVP_MD_CTX_md_data(from)) {
79         memcpy(EVP_MD_CTX_md_data(to), EVP_MD_CTX_md_data(from),
80                sizeof(struct ossl_gost_digest_ctx));
81         md_ctx->dctx.cipher_ctx = &(md_ctx->cctx);
82     }
83     return 1;
84 }
85
86 int gost_digest_cleanup(EVP_MD_CTX *ctx)
87 {
88     if (EVP_MD_CTX_md_data(ctx))
89         memset(EVP_MD_CTX_md_data(ctx), 0,
90                sizeof(struct ossl_gost_digest_ctx));
91     return 1;
92 }