]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost_md2012.c
gost_md2012: Add registration using GostR3411_2012_{256,512}_digest
[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 #include "gost_lcl.h"
15
16 static int gost_digest_init512(EVP_MD_CTX *ctx);
17 static int gost_digest_init256(EVP_MD_CTX *ctx);
18 static int gost_digest_update(EVP_MD_CTX *ctx, const void *data,
19                               size_t count);
20 static int gost_digest_final(EVP_MD_CTX *ctx, unsigned char *md);
21 static int gost_digest_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from);
22 static int gost_digest_cleanup(EVP_MD_CTX *ctx);
23 static int gost_digest_ctrl_256(EVP_MD_CTX *ctx, int type, int arg,
24                                 void *ptr);
25 static int gost_digest_ctrl_512(EVP_MD_CTX *ctx, int type, int arg,
26                                 void *ptr);
27
28 const char micalg_256[] = "gostr3411-2012-256";
29 const char micalg_512[] = "gostr3411-2012-512";
30
31 GOST_digest GostR3411_2012_template_digest = {
32     .input_blocksize = 64,
33     .app_datasize = sizeof(gost2012_hash_ctx),
34     .update = gost_digest_update,
35     .final = gost_digest_final,
36     .copy = gost_digest_copy,
37     .cleanup = gost_digest_cleanup,
38 };
39
40 GOST_digest GostR3411_2012_256_digest = {
41     .nid = NID_id_GostR3411_2012_256,
42     .template = &GostR3411_2012_template_digest,
43     .result_size = 32,
44     .init = gost_digest_init256,
45     .ctrl = gost_digest_ctrl_256,
46 };
47
48 EVP_MD *digest_gost2012_256(void)
49 {
50     return GOST_init_digest(&GostR3411_2012_256_digest);
51 }
52
53 void digest_gost2012_256_destroy(void)
54 {
55     GOST_deinit_digest(&GostR3411_2012_256_digest);
56 }
57
58 GOST_digest GostR3411_2012_512_digest = {
59     .nid = NID_id_GostR3411_2012_512,
60     .template = &GostR3411_2012_template_digest,
61     .result_size = 64,
62     .init = gost_digest_init512,
63     .ctrl = gost_digest_ctrl_512,
64 };
65
66 EVP_MD *digest_gost2012_512(void)
67 {
68     return GOST_init_digest(&GostR3411_2012_512_digest);
69 }
70
71 void digest_gost2012_512_destroy(void)
72 {
73     GOST_deinit_digest(&GostR3411_2012_512_digest);
74 }
75
76 static int gost_digest_init512(EVP_MD_CTX *ctx)
77 {
78     init_gost2012_hash_ctx((gost2012_hash_ctx *) EVP_MD_CTX_md_data(ctx),
79                            512);
80     return 1;
81 }
82
83 static int gost_digest_init256(EVP_MD_CTX *ctx)
84 {
85     init_gost2012_hash_ctx((gost2012_hash_ctx *) EVP_MD_CTX_md_data(ctx),
86                            256);
87     return 1;
88 }
89
90 static int gost_digest_update(EVP_MD_CTX *ctx, const void *data, size_t count)
91 {
92     gost2012_hash_block((gost2012_hash_ctx *) EVP_MD_CTX_md_data(ctx), data,
93                         count);
94     return 1;
95 }
96
97 static int gost_digest_final(EVP_MD_CTX *ctx, unsigned char *md)
98 {
99     gost2012_finish_hash((gost2012_hash_ctx *) EVP_MD_CTX_md_data(ctx), md);
100     return 1;
101 }
102
103 static int gost_digest_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
104 {
105     if (EVP_MD_CTX_md_data(to) && EVP_MD_CTX_md_data(from))
106         memcpy(EVP_MD_CTX_md_data(to), EVP_MD_CTX_md_data(from),
107                sizeof(gost2012_hash_ctx));
108
109     return 1;
110 }
111
112 static int gost_digest_cleanup(EVP_MD_CTX *ctx)
113 {
114     if (EVP_MD_CTX_md_data(ctx))
115         memset(EVP_MD_CTX_md_data(ctx), 0x00, sizeof(gost2012_hash_ctx));
116
117     return 1;
118 }
119
120 static int gost_digest_ctrl_256(EVP_MD_CTX *ctx, int type, int arg, void *ptr)
121 {
122     switch (type) {
123     case EVP_MD_CTRL_MICALG:
124         {
125             *((char **)ptr) = OPENSSL_malloc(strlen(micalg_256) + 1);
126             if (*((char **)ptr) != NULL) {
127                 strcpy(*((char **)ptr), micalg_256);
128                 return 1;
129             }
130             return 0;
131         }
132     default:
133         return 0;
134     }
135 }
136
137 static int gost_digest_ctrl_512(EVP_MD_CTX *ctx, int type, int arg, void *ptr)
138 {
139     switch (type) {
140     case EVP_MD_CTRL_MICALG:
141         {
142             *((char **)ptr) = OPENSSL_malloc(strlen(micalg_512) + 1);
143             if (*((char **)ptr) != NULL) {
144                 strcpy(*((char **)ptr), micalg_512);
145                 return 1;
146             }
147         }
148     default:
149         return 0;
150     }
151 }