]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost_md2012.c
Initial commit providing GOST 2012 algorithms.
[openssl-gost/engine.git] / gost_md2012.c
1 /**********************************************************************
2  *                          gost_md2012.c                             *
3  *             Copyright (c) 2013 Cryptocom LTD.                      *
4  *         This file is distributed under the same license as OpenSSL *
5  *                                                                    *
6  *          GOST R 34.11-2012 interface to OpenSSL engine.            *
7  *                                                                    *
8  * Author: Alexey Degtyarev <alexey@renatasystems.org>                *
9  *                                                                    *
10  **********************************************************************/
11
12 #include <openssl/evp.h>
13 #include "gosthash2012.h"
14
15 static int gost_digest_init512(EVP_MD_CTX *ctx);
16 static int gost_digest_init256(EVP_MD_CTX *ctx);
17 static int gost_digest_update(EVP_MD_CTX *ctx, const void *data,
18                               size_t count);
19 static int gost_digest_final(EVP_MD_CTX *ctx, unsigned char *md);
20 static int gost_digest_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from);
21 static int gost_digest_cleanup(EVP_MD_CTX *ctx);
22
23 EVP_MD digest_gost2012_512 = {
24     NID_id_GostR3411_2012_512,
25     NID_undef,
26     64,                         /* digest size */
27     EVP_MD_FLAG_PKEY_METHOD_SIGNATURE,
28     gost_digest_init512,
29     gost_digest_update,
30     gost_digest_final,
31     gost_digest_copy,
32     gost_digest_cleanup,
33     NULL,
34     NULL,
35     {NID_undef, NID_undef, 0, 0, 0},
36     64,                         /* block size */
37     sizeof(gost2012_hash_ctx),
38     NULL
39 };
40
41 EVP_MD digest_gost2012_256 = {
42     NID_id_GostR3411_2012_256,
43     NID_undef,
44     32,                         /* digest size */
45     EVP_MD_FLAG_PKEY_METHOD_SIGNATURE,
46     gost_digest_init256,
47     gost_digest_update,
48     gost_digest_final,
49     gost_digest_copy,
50     gost_digest_cleanup,
51     NULL,
52     NULL,
53     {NID_undef, NID_undef, 0, 0, 0},
54     64,                         /* block size */
55     sizeof(gost2012_hash_ctx),
56     NULL
57 };
58
59 static int gost_digest_init512(EVP_MD_CTX *ctx)
60 {
61     init_gost2012_hash_ctx((gost2012_hash_ctx *) ctx->md_data, 512);
62     return 1;
63 }
64
65 static int gost_digest_init256(EVP_MD_CTX *ctx)
66 {
67     init_gost2012_hash_ctx((gost2012_hash_ctx *) ctx->md_data, 256);
68     return 1;
69 }
70
71 static int gost_digest_update(EVP_MD_CTX *ctx, const void *data, size_t count)
72 {
73     gost2012_hash_block((gost2012_hash_ctx *) ctx->md_data, data, count);
74     return 1;
75 }
76
77 static int gost_digest_final(EVP_MD_CTX *ctx, unsigned char *md)
78 {
79     gost2012_finish_hash((gost2012_hash_ctx *) ctx->md_data, md);
80     return 1;
81 }
82
83 static int gost_digest_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
84 {
85     if (to->md_data && from->md_data)
86         memcpy(to->md_data, from->md_data, sizeof(from->md_data));
87
88     return 1;
89 }
90
91 static int gost_digest_cleanup(EVP_MD_CTX *ctx)
92 {
93     if (ctx->md_data)
94         memset(ctx->md_data, 0x00, sizeof(gost2012_hash_ctx));
95
96     return 1;
97 }