]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost_md2012.c
gost_md: Rework alias support in digest registration
[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     .alias = "streebog256",
43     .template = &GostR3411_2012_template_digest,
44     .result_size = 32,
45     .init = gost_digest_init256,
46     .ctrl = gost_digest_ctrl_256,
47 };
48
49 EVP_MD *digest_gost2012_256(void)
50 {
51     return GOST_init_digest(&GostR3411_2012_256_digest);
52 }
53
54 void digest_gost2012_256_destroy(void)
55 {
56     GOST_deinit_digest(&GostR3411_2012_256_digest);
57 }
58
59 GOST_digest GostR3411_2012_512_digest = {
60     .nid = NID_id_GostR3411_2012_512,
61     .alias = "streebog512",
62     .template = &GostR3411_2012_template_digest,
63     .result_size = 64,
64     .init = gost_digest_init512,
65     .ctrl = gost_digest_ctrl_512,
66 };
67
68 EVP_MD *digest_gost2012_512(void)
69 {
70     return GOST_init_digest(&GostR3411_2012_512_digest);
71 }
72
73 void digest_gost2012_512_destroy(void)
74 {
75     GOST_deinit_digest(&GostR3411_2012_512_digest);
76 }
77
78 static int gost_digest_init512(EVP_MD_CTX *ctx)
79 {
80     init_gost2012_hash_ctx((gost2012_hash_ctx *) EVP_MD_CTX_md_data(ctx),
81                            512);
82     return 1;
83 }
84
85 static int gost_digest_init256(EVP_MD_CTX *ctx)
86 {
87     init_gost2012_hash_ctx((gost2012_hash_ctx *) EVP_MD_CTX_md_data(ctx),
88                            256);
89     return 1;
90 }
91
92 static int gost_digest_update(EVP_MD_CTX *ctx, const void *data, size_t count)
93 {
94     gost2012_hash_block((gost2012_hash_ctx *) EVP_MD_CTX_md_data(ctx), data,
95                         count);
96     return 1;
97 }
98
99 static int gost_digest_final(EVP_MD_CTX *ctx, unsigned char *md)
100 {
101     gost2012_finish_hash((gost2012_hash_ctx *) EVP_MD_CTX_md_data(ctx), md);
102     return 1;
103 }
104
105 static int gost_digest_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
106 {
107     if (EVP_MD_CTX_md_data(to) && EVP_MD_CTX_md_data(from))
108         memcpy(EVP_MD_CTX_md_data(to), EVP_MD_CTX_md_data(from),
109                sizeof(gost2012_hash_ctx));
110
111     return 1;
112 }
113
114 static int gost_digest_cleanup(EVP_MD_CTX *ctx)
115 {
116     if (EVP_MD_CTX_md_data(ctx))
117         memset(EVP_MD_CTX_md_data(ctx), 0x00, sizeof(gost2012_hash_ctx));
118
119     return 1;
120 }
121
122 static int gost_digest_ctrl_256(EVP_MD_CTX *ctx, int type, int arg, void *ptr)
123 {
124     switch (type) {
125     case EVP_MD_CTRL_MICALG:
126         {
127             *((char **)ptr) = OPENSSL_malloc(strlen(micalg_256) + 1);
128             if (*((char **)ptr) != NULL) {
129                 strcpy(*((char **)ptr), micalg_256);
130                 return 1;
131             }
132             return 0;
133         }
134     default:
135         return 0;
136     }
137 }
138
139 static int gost_digest_ctrl_512(EVP_MD_CTX *ctx, int type, int arg, void *ptr)
140 {
141     switch (type) {
142     case EVP_MD_CTRL_MICALG:
143         {
144             *((char **)ptr) = OPENSSL_malloc(strlen(micalg_512) + 1);
145             if (*((char **)ptr) != NULL) {
146                 strcpy(*((char **)ptr), micalg_512);
147                 return 1;
148             }
149         }
150     default:
151         return 0;
152     }
153 }