1 /**********************************************************************
3 * Copyright (c) 2013 Cryptocom LTD. *
4 * This file is distributed under the same license as OpenSSL *
6 * GOST R 34.11-2012 interface to OpenSSL engine. *
8 * Author: Alexey Degtyarev <alexey@renatasystems.org> *
10 **********************************************************************/
12 #include <openssl/evp.h>
13 #include "gosthash2012.h"
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,
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,
25 static int gost_digest_ctrl_512(EVP_MD_CTX *ctx, int type, int arg,
28 const char micalg_256[] = "gostr3411-2012-256";
29 const char micalg_512[] = "gostr3411-2012-512";
31 GOST_digest GostR3411_2012_template_digest = {
32 .input_blocksize = 64,
33 .app_datasize = sizeof(gost2012_hash_ctx),
34 .update = gost_digest_update,
35 .final = gost_digest_final,
36 .copy = gost_digest_copy,
37 .cleanup = gost_digest_cleanup,
40 GOST_digest GostR3411_2012_256_digest = {
41 .nid = NID_id_GostR3411_2012_256,
42 .alias = "streebog256",
43 .template = &GostR3411_2012_template_digest,
45 .init = gost_digest_init256,
46 .ctrl = gost_digest_ctrl_256,
49 EVP_MD *digest_gost2012_256(void)
51 return GOST_init_digest(&GostR3411_2012_256_digest);
54 void digest_gost2012_256_destroy(void)
56 GOST_deinit_digest(&GostR3411_2012_256_digest);
59 GOST_digest GostR3411_2012_512_digest = {
60 .nid = NID_id_GostR3411_2012_512,
61 .alias = "streebog512",
62 .template = &GostR3411_2012_template_digest,
64 .init = gost_digest_init512,
65 .ctrl = gost_digest_ctrl_512,
68 EVP_MD *digest_gost2012_512(void)
70 return GOST_init_digest(&GostR3411_2012_512_digest);
73 void digest_gost2012_512_destroy(void)
75 GOST_deinit_digest(&GostR3411_2012_512_digest);
78 static int gost_digest_init512(EVP_MD_CTX *ctx)
80 init_gost2012_hash_ctx((gost2012_hash_ctx *) EVP_MD_CTX_md_data(ctx),
85 static int gost_digest_init256(EVP_MD_CTX *ctx)
87 init_gost2012_hash_ctx((gost2012_hash_ctx *) EVP_MD_CTX_md_data(ctx),
92 static int gost_digest_update(EVP_MD_CTX *ctx, const void *data, size_t count)
94 gost2012_hash_block((gost2012_hash_ctx *) EVP_MD_CTX_md_data(ctx), data,
99 static int gost_digest_final(EVP_MD_CTX *ctx, unsigned char *md)
101 gost2012_finish_hash((gost2012_hash_ctx *) EVP_MD_CTX_md_data(ctx), md);
105 static int gost_digest_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
107 if (EVP_MD_CTX_md_data(to) && EVP_MD_CTX_md_data(from))
108 memcpy(EVP_MD_CTX_md_data(to), EVP_MD_CTX_md_data(from),
109 sizeof(gost2012_hash_ctx));
114 static int gost_digest_cleanup(EVP_MD_CTX *ctx)
116 if (EVP_MD_CTX_md_data(ctx))
117 memset(EVP_MD_CTX_md_data(ctx), 0x00, sizeof(gost2012_hash_ctx));
122 static int gost_digest_ctrl_256(EVP_MD_CTX *ctx, int type, int arg, void *ptr)
125 case EVP_MD_CTRL_MICALG:
127 *((char **)ptr) = OPENSSL_malloc(strlen(micalg_256) + 1);
128 if (*((char **)ptr) != NULL) {
129 strcpy(*((char **)ptr), micalg_256);
139 static int gost_digest_ctrl_512(EVP_MD_CTX *ctx, int type, int arg, void *ptr)
142 case EVP_MD_CTRL_MICALG:
144 *((char **)ptr) = OPENSSL_malloc(strlen(micalg_512) + 1);
145 if (*((char **)ptr) != NULL) {
146 strcpy(*((char **)ptr), micalg_512);