]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost_md2012.c
Remove duplicates
[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
15 static int gost_digest_init512(EVP_MD_CTX *ctx);
16 static int gost_digest_init256(EVP_MD_CTX *ctx);
17 static int gost_digest_update(EVP_MD_CTX *ctx, const void *data,
18                               size_t count);
19 static int gost_digest_final(EVP_MD_CTX *ctx, unsigned char *md);
20 static int gost_digest_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from);
21 static int gost_digest_cleanup(EVP_MD_CTX *ctx);
22 static int gost_digest_ctrl_256(EVP_MD_CTX *ctx, int type, int arg,
23                                 void *ptr);
24 static int gost_digest_ctrl_512(EVP_MD_CTX *ctx, int type, int arg,
25                                 void *ptr);
26
27 const char micalg_256[] = "gostr3411-2012-256";
28 const char micalg_512[] = "gostr3411-2012-512";
29
30 EVP_MD digest_gost2012_512 = {
31     NID_id_GostR3411_2012_512,
32     NID_undef,
33     64,                         /* digest size */
34     EVP_MD_FLAG_PKEY_METHOD_SIGNATURE,
35     gost_digest_init512,
36     gost_digest_update,
37     gost_digest_final,
38     gost_digest_copy,
39     gost_digest_cleanup,
40     NULL,
41     NULL,
42     {NID_undef, NID_undef, 0, 0, 0},
43     64,                         /* block size */
44     sizeof(gost2012_hash_ctx),
45     gost_digest_ctrl_512,
46 };
47
48 EVP_MD digest_gost2012_256 = {
49     NID_id_GostR3411_2012_256,
50     NID_undef,
51     32,                         /* digest size */
52     EVP_MD_FLAG_PKEY_METHOD_SIGNATURE,
53     gost_digest_init256,
54     gost_digest_update,
55     gost_digest_final,
56     gost_digest_copy,
57     gost_digest_cleanup,
58     NULL,
59     NULL,
60     {NID_undef, NID_undef, 0, 0, 0},
61     64,                         /* block size */
62     sizeof(gost2012_hash_ctx),
63     gost_digest_ctrl_256
64 };
65
66 static int gost_digest_init512(EVP_MD_CTX *ctx)
67 {
68     init_gost2012_hash_ctx((gost2012_hash_ctx *) ctx->md_data, 512);
69     return 1;
70 }
71
72 static int gost_digest_init256(EVP_MD_CTX *ctx)
73 {
74     init_gost2012_hash_ctx((gost2012_hash_ctx *) ctx->md_data, 256);
75     return 1;
76 }
77
78 static int gost_digest_update(EVP_MD_CTX *ctx, const void *data, size_t count)
79 {
80     gost2012_hash_block((gost2012_hash_ctx *) ctx->md_data, data, count);
81     return 1;
82 }
83
84 static int gost_digest_final(EVP_MD_CTX *ctx, unsigned char *md)
85 {
86     gost2012_finish_hash((gost2012_hash_ctx *) ctx->md_data, md);
87     return 1;
88 }
89
90 static int gost_digest_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
91 {
92     if (to->md_data && from->md_data)
93         memcpy(to->md_data, from->md_data, sizeof(gost2012_hash_ctx));
94
95     return 1;
96 }
97
98 static int gost_digest_cleanup(EVP_MD_CTX *ctx)
99 {
100     if (ctx->md_data)
101         memset(ctx->md_data, 0x00, sizeof(gost2012_hash_ctx));
102
103     return 1;
104 }
105
106 static int gost_digest_ctrl_256(EVP_MD_CTX *ctx, int type, int arg, void *ptr)
107 {
108     switch (type) {
109     case EVP_MD_CTRL_MICALG:
110         {
111             *((char **)ptr) = OPENSSL_malloc(strlen(micalg_256) + 1);
112             if (*((char **)ptr) != NULL) {
113                 strcpy(*((char **)ptr), micalg_256);
114                 return 1;
115             }
116             return 0;
117         }
118     default:
119         return 0;
120     }
121 }
122
123 static int gost_digest_ctrl_512(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_512) + 1);
129             if (*((char **)ptr) != NULL) {
130                 strcpy(*((char **)ptr), micalg_512);
131                 return 1;
132             }
133             return 0;
134         }
135         return 1;
136     default:
137         return 0;
138     }
139 }