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 GOST_digest GostR3411_2012_512_digest = {
51 .nid = NID_id_GostR3411_2012_512,
52 .alias = "streebog512",
53 .template = &GostR3411_2012_template_digest,
55 .init = gost_digest_init512,
56 .ctrl = gost_digest_ctrl_512,
59 static int gost_digest_init512(EVP_MD_CTX *ctx)
61 init_gost2012_hash_ctx((gost2012_hash_ctx *) EVP_MD_CTX_md_data(ctx),
66 static int gost_digest_init256(EVP_MD_CTX *ctx)
68 init_gost2012_hash_ctx((gost2012_hash_ctx *) EVP_MD_CTX_md_data(ctx),
73 static int gost_digest_update(EVP_MD_CTX *ctx, const void *data, size_t count)
75 gost2012_hash_block((gost2012_hash_ctx *) EVP_MD_CTX_md_data(ctx), data,
80 static int gost_digest_final(EVP_MD_CTX *ctx, unsigned char *md)
82 gost2012_finish_hash((gost2012_hash_ctx *) EVP_MD_CTX_md_data(ctx), md);
86 static int gost_digest_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
88 if (EVP_MD_CTX_md_data(to) && EVP_MD_CTX_md_data(from))
89 memcpy(EVP_MD_CTX_md_data(to), EVP_MD_CTX_md_data(from),
90 sizeof(gost2012_hash_ctx));
95 static int gost_digest_cleanup(EVP_MD_CTX *ctx)
97 if (EVP_MD_CTX_md_data(ctx))
98 memset(EVP_MD_CTX_md_data(ctx), 0x00, sizeof(gost2012_hash_ctx));
103 static int gost_digest_ctrl_256(EVP_MD_CTX *ctx, int type, int arg, void *ptr)
106 case EVP_MD_CTRL_MICALG:
108 *((char **)ptr) = OPENSSL_malloc(strlen(micalg_256) + 1);
109 if (*((char **)ptr) != NULL) {
110 strcpy(*((char **)ptr), micalg_256);
120 static int gost_digest_ctrl_512(EVP_MD_CTX *ctx, int type, int arg, void *ptr)
123 case EVP_MD_CTRL_MICALG:
125 *((char **)ptr) = OPENSSL_malloc(strlen(micalg_512) + 1);
126 if (*((char **)ptr) != NULL) {
127 strcpy(*((char **)ptr), micalg_512);