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 .template = &GostR3411_2012_template_digest,
44 .init = gost_digest_init256,
45 .ctrl = gost_digest_ctrl_256,
48 EVP_MD *digest_gost2012_256(void)
50 return GOST_init_digest(&GostR3411_2012_256_digest);
53 void digest_gost2012_256_destroy(void)
55 GOST_deinit_digest(&GostR3411_2012_256_digest);
58 GOST_digest GostR3411_2012_512_digest = {
59 .nid = NID_id_GostR3411_2012_512,
60 .template = &GostR3411_2012_template_digest,
62 .init = gost_digest_init512,
63 .ctrl = gost_digest_ctrl_512,
66 EVP_MD *digest_gost2012_512(void)
68 return GOST_init_digest(&GostR3411_2012_512_digest);
71 void digest_gost2012_512_destroy(void)
73 GOST_deinit_digest(&GostR3411_2012_512_digest);
76 static int gost_digest_init512(EVP_MD_CTX *ctx)
78 init_gost2012_hash_ctx((gost2012_hash_ctx *) EVP_MD_CTX_md_data(ctx),
83 static int gost_digest_init256(EVP_MD_CTX *ctx)
85 init_gost2012_hash_ctx((gost2012_hash_ctx *) EVP_MD_CTX_md_data(ctx),
90 static int gost_digest_update(EVP_MD_CTX *ctx, const void *data, size_t count)
92 gost2012_hash_block((gost2012_hash_ctx *) EVP_MD_CTX_md_data(ctx), data,
97 static int gost_digest_final(EVP_MD_CTX *ctx, unsigned char *md)
99 gost2012_finish_hash((gost2012_hash_ctx *) EVP_MD_CTX_md_data(ctx), md);
103 static int gost_digest_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
105 if (EVP_MD_CTX_md_data(to) && EVP_MD_CTX_md_data(from))
106 memcpy(EVP_MD_CTX_md_data(to), EVP_MD_CTX_md_data(from),
107 sizeof(gost2012_hash_ctx));
112 static int gost_digest_cleanup(EVP_MD_CTX *ctx)
114 if (EVP_MD_CTX_md_data(ctx))
115 memset(EVP_MD_CTX_md_data(ctx), 0x00, sizeof(gost2012_hash_ctx));
120 static int gost_digest_ctrl_256(EVP_MD_CTX *ctx, int type, int arg, void *ptr)
123 case EVP_MD_CTRL_MICALG:
125 *((char **)ptr) = OPENSSL_malloc(strlen(micalg_256) + 1);
126 if (*((char **)ptr) != NULL) {
127 strcpy(*((char **)ptr), micalg_256);
137 static int gost_digest_ctrl_512(EVP_MD_CTX *ctx, int type, int arg, void *ptr)
140 case EVP_MD_CTRL_MICALG:
142 *((char **)ptr) = OPENSSL_malloc(strlen(micalg_512) + 1);
143 if (*((char **)ptr) != NULL) {
144 strcpy(*((char **)ptr), micalg_512);