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