]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost_md2012.c
Make it possible to re-define OPENSSL_ENGINES_INSTALL_DIR
[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 "compat.h"
13 #include <openssl/evp.h>
14 #include "gosthash2012.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 static EVP_MD *_hidden_GostR3411_2012_256_md = NULL;
32 static EVP_MD *_hidden_GostR3411_2012_512_md = NULL;
33
34 EVP_MD *digest_gost2012_256(void)
35 {
36     if (_hidden_GostR3411_2012_256_md == NULL) {
37         EVP_MD *md;
38
39         if ((md =
40              EVP_MD_meth_new(NID_id_GostR3411_2012_256, NID_undef)) == NULL
41 #if (OPENSSL_VERSION_NUMBER <= 0x10002100L)
42             || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_PKEY_METHOD_SIGNATURE)
43 #endif
44             || !EVP_MD_meth_set_result_size(md, 32)
45             || !EVP_MD_meth_set_input_blocksize(md, 64)
46             || !EVP_MD_meth_set_app_datasize(md, sizeof(gost2012_hash_ctx))
47             || !EVP_MD_meth_set_init(md, gost_digest_init256)
48             || !EVP_MD_meth_set_update(md, gost_digest_update)
49             || !EVP_MD_meth_set_final(md, gost_digest_final)
50             || !EVP_MD_meth_set_copy(md, gost_digest_copy)
51             || !EVP_MD_meth_set_ctrl(md, gost_digest_ctrl_256)
52             || !EVP_MD_meth_set_cleanup(md, gost_digest_cleanup)) {
53             EVP_MD_meth_free(md);
54             md = NULL;
55         }
56         _hidden_GostR3411_2012_256_md = md;
57     }
58     return _hidden_GostR3411_2012_256_md;
59 }
60
61 void digest_gost2012_256_destroy(void)
62 {
63     EVP_MD_meth_free(_hidden_GostR3411_2012_256_md);
64     _hidden_GostR3411_2012_256_md = NULL;
65 }
66
67 EVP_MD *digest_gost2012_512(void)
68 {
69     if (_hidden_GostR3411_2012_512_md == NULL) {
70         EVP_MD *md;
71
72         if ((md =
73              EVP_MD_meth_new(NID_id_GostR3411_2012_512, NID_undef)) == NULL
74 #if (OPENSSL_VERSION_NUMBER <= 0x10002100L)
75             || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_PKEY_METHOD_SIGNATURE)
76 #endif
77             || !EVP_MD_meth_set_result_size(md, 64)
78             || !EVP_MD_meth_set_input_blocksize(md, 64)
79             || !EVP_MD_meth_set_app_datasize(md, sizeof(gost2012_hash_ctx))
80             || !EVP_MD_meth_set_init(md, gost_digest_init512)
81             || !EVP_MD_meth_set_update(md, gost_digest_update)
82             || !EVP_MD_meth_set_final(md, gost_digest_final)
83             || !EVP_MD_meth_set_copy(md, gost_digest_copy)
84             || !EVP_MD_meth_set_ctrl(md, gost_digest_ctrl_512)
85             || !EVP_MD_meth_set_cleanup(md, gost_digest_cleanup)) {
86             EVP_MD_meth_free(md);
87             md = NULL;
88         }
89         _hidden_GostR3411_2012_512_md = md;
90     }
91     return _hidden_GostR3411_2012_512_md;
92 }
93
94 void digest_gost2012_512_destroy(void)
95 {
96     EVP_MD_meth_free(_hidden_GostR3411_2012_512_md);
97     _hidden_GostR3411_2012_512_md = NULL;
98 }
99
100 static int gost_digest_init512(EVP_MD_CTX *ctx)
101 {
102     init_gost2012_hash_ctx((gost2012_hash_ctx *) EVP_MD_CTX_md_data(ctx),
103                            512);
104     return 1;
105 }
106
107 static int gost_digest_init256(EVP_MD_CTX *ctx)
108 {
109     init_gost2012_hash_ctx((gost2012_hash_ctx *) EVP_MD_CTX_md_data(ctx),
110                            256);
111     return 1;
112 }
113
114 static int gost_digest_update(EVP_MD_CTX *ctx, const void *data, size_t count)
115 {
116     gost2012_hash_block((gost2012_hash_ctx *) EVP_MD_CTX_md_data(ctx), data,
117                         count);
118     return 1;
119 }
120
121 static int gost_digest_final(EVP_MD_CTX *ctx, unsigned char *md)
122 {
123     gost2012_finish_hash((gost2012_hash_ctx *) EVP_MD_CTX_md_data(ctx), md);
124     return 1;
125 }
126
127 static int gost_digest_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
128 {
129     if (EVP_MD_CTX_md_data(to) && EVP_MD_CTX_md_data(from))
130         memcpy(EVP_MD_CTX_md_data(to), EVP_MD_CTX_md_data(from),
131                sizeof(gost2012_hash_ctx));
132
133     return 1;
134 }
135
136 static int gost_digest_cleanup(EVP_MD_CTX *ctx)
137 {
138     if (EVP_MD_CTX_md_data(ctx))
139         memset(EVP_MD_CTX_md_data(ctx), 0x00, sizeof(gost2012_hash_ctx));
140
141     return 1;
142 }
143
144 static int gost_digest_ctrl_256(EVP_MD_CTX *ctx, int type, int arg, void *ptr)
145 {
146     switch (type) {
147     case EVP_MD_CTRL_MICALG:
148         {
149             *((char **)ptr) = OPENSSL_malloc(strlen(micalg_256) + 1);
150             if (*((char **)ptr) != NULL) {
151                 strcpy(*((char **)ptr), micalg_256);
152                 return 1;
153             }
154             return 0;
155         }
156     default:
157         return 0;
158     }
159 }
160
161 static int gost_digest_ctrl_512(EVP_MD_CTX *ctx, int type, int arg, void *ptr)
162 {
163     switch (type) {
164     case EVP_MD_CTRL_MICALG:
165         {
166             *((char **)ptr) = OPENSSL_malloc(strlen(micalg_512) + 1);
167             if (*((char **)ptr) != NULL) {
168                 strcpy(*((char **)ptr), micalg_512);
169                 return 1;
170             }
171         }
172     default:
173         return 0;
174     }
175 }