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"
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,
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,
24 static int gost_digest_ctrl_512(EVP_MD_CTX *ctx, int type, int arg,
27 const char micalg_256[] = "gostr3411-2012-256";
28 const char micalg_512[] = "gostr3411-2012-512";
30 EVP_MD digest_gost2012_512 = {
31 NID_id_GostR3411_2012_512,
34 EVP_MD_FLAG_PKEY_METHOD_SIGNATURE,
42 {NID_undef, NID_undef, 0, 0, 0},
44 sizeof(gost2012_hash_ctx),
48 EVP_MD digest_gost2012_256 = {
49 NID_id_GostR3411_2012_256,
52 EVP_MD_FLAG_PKEY_METHOD_SIGNATURE,
60 {NID_undef, NID_undef, 0, 0, 0},
62 sizeof(gost2012_hash_ctx),
66 static int gost_digest_init512(EVP_MD_CTX *ctx)
68 init_gost2012_hash_ctx((gost2012_hash_ctx *) ctx->md_data, 512);
72 static int gost_digest_init256(EVP_MD_CTX *ctx)
74 init_gost2012_hash_ctx((gost2012_hash_ctx *) ctx->md_data, 256);
78 static int gost_digest_update(EVP_MD_CTX *ctx, const void *data, size_t count)
80 gost2012_hash_block((gost2012_hash_ctx *) ctx->md_data, data, count);
84 static int gost_digest_final(EVP_MD_CTX *ctx, unsigned char *md)
86 gost2012_finish_hash((gost2012_hash_ctx *) ctx->md_data, md);
90 static int gost_digest_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
92 if (to->md_data && from->md_data)
93 memcpy(to->md_data, from->md_data, sizeof(gost2012_hash_ctx));
98 static int gost_digest_cleanup(EVP_MD_CTX *ctx)
101 memset(ctx->md_data, 0x00, sizeof(gost2012_hash_ctx));
106 static int gost_digest_ctrl_256(EVP_MD_CTX *ctx, int type, int arg, void *ptr)
109 case EVP_MD_CTRL_MICALG:
111 *((char **)ptr) = OPENSSL_malloc(strlen(micalg_256) + 1);
112 if (*((char **)ptr) != NULL) {
113 strcpy(*((char **)ptr), micalg_256);
123 static int gost_digest_ctrl_512(EVP_MD_CTX *ctx, int type, int arg, void *ptr)
126 case EVP_MD_CTRL_MICALG:
128 *((char **)ptr) = OPENSSL_malloc(strlen(micalg_512) + 1);
129 if (*((char **)ptr) != NULL) {
130 strcpy(*((char **)ptr), micalg_512);