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