1 /**********************************************************************
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 *
7 * GOST R 34.11-2012 interface to OpenSSL engine. *
9 * Author: Alexey Degtyarev <alexey@renatasystems.org> *
11 **********************************************************************/
13 #include <openssl/evp.h>
14 #include "gosthash2012.h"
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,
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,
26 static int gost_digest_ctrl_512(EVP_MD_CTX *ctx, int type, int arg,
29 const char micalg_256[] = "gostr3411-2012-256";
30 const char micalg_512[] = "gostr3411-2012-512";
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,
41 GOST_digest GostR3411_2012_256_digest = {
42 .nid = NID_id_GostR3411_2012_256,
43 .alias = "streebog256",
44 .template = &GostR3411_2012_template_digest,
46 .init = gost_digest_init256,
47 .ctrl = gost_digest_ctrl_256,
50 EVP_MD *digest_gost2012_256(void)
52 return GOST_init_digest(&GostR3411_2012_256_digest);
55 void digest_gost2012_256_destroy(void)
57 GOST_deinit_digest(&GostR3411_2012_256_digest);
60 GOST_digest GostR3411_2012_512_digest = {
61 .nid = NID_id_GostR3411_2012_512,
62 .alias = "streebog512",
63 .template = &GostR3411_2012_template_digest,
65 .init = gost_digest_init512,
66 .ctrl = gost_digest_ctrl_512,
69 EVP_MD *digest_gost2012_512(void)
71 return GOST_init_digest(&GostR3411_2012_512_digest);
74 void digest_gost2012_512_destroy(void)
76 GOST_deinit_digest(&GostR3411_2012_512_digest);
79 static int gost_digest_init512(EVP_MD_CTX *ctx)
81 init_gost2012_hash_ctx((gost2012_hash_ctx *) EVP_MD_CTX_md_data(ctx),
86 static int gost_digest_init256(EVP_MD_CTX *ctx)
88 init_gost2012_hash_ctx((gost2012_hash_ctx *) EVP_MD_CTX_md_data(ctx),
93 static int gost_digest_update(EVP_MD_CTX *ctx, const void *data, size_t count)
95 gost2012_hash_block((gost2012_hash_ctx *) EVP_MD_CTX_md_data(ctx), data,
100 static int gost_digest_final(EVP_MD_CTX *ctx, unsigned char *md)
102 gost2012_finish_hash((gost2012_hash_ctx *) EVP_MD_CTX_md_data(ctx), md);
106 static int gost_digest_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
108 if (EVP_MD_CTX_md_data(to) && EVP_MD_CTX_md_data(from))
109 memcpy(EVP_MD_CTX_md_data(to), EVP_MD_CTX_md_data(from),
110 sizeof(gost2012_hash_ctx));
115 static int gost_digest_cleanup(EVP_MD_CTX *ctx)
117 if (EVP_MD_CTX_md_data(ctx))
118 memset(EVP_MD_CTX_md_data(ctx), 0x00, sizeof(gost2012_hash_ctx));
123 static int gost_digest_ctrl_256(EVP_MD_CTX *ctx, int type, int arg, void *ptr)
126 case EVP_MD_CTRL_MICALG:
128 *((char **)ptr) = OPENSSL_malloc(strlen(micalg_256) + 1);
129 if (*((char **)ptr) != NULL) {
130 strcpy(*((char **)ptr), micalg_256);
140 static int gost_digest_ctrl_512(EVP_MD_CTX *ctx, int type, int arg, void *ptr)
143 case EVP_MD_CTRL_MICALG:
145 *((char **)ptr) = OPENSSL_malloc(strlen(micalg_512) + 1);
146 if (*((char **)ptr) != NULL) {
147 strcpy(*((char **)ptr), micalg_512);