]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost_md2012.c
tcl_tests: ca.try: Ignore openssl crl exit status for 'corrupted CRL' test
[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 GOST_digest GostR3411_2012_512_digest = {
51     .nid = NID_id_GostR3411_2012_512,
52     .alias = "streebog512",
53     .template = &GostR3411_2012_template_digest,
54     .result_size = 64,
55     .init = gost_digest_init512,
56     .ctrl = gost_digest_ctrl_512,
57 };
58
59 static int gost_digest_init512(EVP_MD_CTX *ctx)
60 {
61     init_gost2012_hash_ctx((gost2012_hash_ctx *) EVP_MD_CTX_md_data(ctx),
62                            512);
63     return 1;
64 }
65
66 static int gost_digest_init256(EVP_MD_CTX *ctx)
67 {
68     init_gost2012_hash_ctx((gost2012_hash_ctx *) EVP_MD_CTX_md_data(ctx),
69                            256);
70     return 1;
71 }
72
73 static int gost_digest_update(EVP_MD_CTX *ctx, const void *data, size_t count)
74 {
75     gost2012_hash_block((gost2012_hash_ctx *) EVP_MD_CTX_md_data(ctx), data,
76                         count);
77     return 1;
78 }
79
80 static int gost_digest_final(EVP_MD_CTX *ctx, unsigned char *md)
81 {
82     gost2012_finish_hash((gost2012_hash_ctx *) EVP_MD_CTX_md_data(ctx), md);
83     return 1;
84 }
85
86 static int gost_digest_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
87 {
88     if (EVP_MD_CTX_md_data(to) && EVP_MD_CTX_md_data(from))
89         memcpy(EVP_MD_CTX_md_data(to), EVP_MD_CTX_md_data(from),
90                sizeof(gost2012_hash_ctx));
91
92     return 1;
93 }
94
95 static int gost_digest_cleanup(EVP_MD_CTX *ctx)
96 {
97     if (EVP_MD_CTX_md_data(ctx))
98         memset(EVP_MD_CTX_md_data(ctx), 0x00, sizeof(gost2012_hash_ctx));
99
100     return 1;
101 }
102
103 static int gost_digest_ctrl_256(EVP_MD_CTX *ctx, int type, int arg, void *ptr)
104 {
105     switch (type) {
106     case EVP_MD_CTRL_MICALG:
107         {
108             *((char **)ptr) = OPENSSL_malloc(strlen(micalg_256) + 1);
109             if (*((char **)ptr) != NULL) {
110                 strcpy(*((char **)ptr), micalg_256);
111                 return 1;
112             }
113             return 0;
114         }
115     default:
116         return 0;
117     }
118 }
119
120 static int gost_digest_ctrl_512(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_512) + 1);
126             if (*((char **)ptr) != NULL) {
127                 strcpy(*((char **)ptr), micalg_512);
128                 return 1;
129             }
130         }
131     default:
132         return 0;
133     }
134 }