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, void *ptr);
23 static int gost_digest_ctrl_512(EVP_MD_CTX *ctx, int type, int arg, void *ptr);
25 const char micalg_256[] = "gostr3411-2012-256";
26 const char micalg_512[] = "gostr3411-2012-512";
28 EVP_MD digest_gost2012_512 = {
29 NID_id_GostR3411_2012_512,
32 EVP_MD_FLAG_PKEY_METHOD_SIGNATURE,
40 {NID_undef, NID_undef, 0, 0, 0},
42 sizeof(gost2012_hash_ctx),
46 EVP_MD digest_gost2012_256 = {
47 NID_id_GostR3411_2012_256,
50 EVP_MD_FLAG_PKEY_METHOD_SIGNATURE,
58 {NID_undef, NID_undef, 0, 0, 0},
60 sizeof(gost2012_hash_ctx),
64 static int gost_digest_init512(EVP_MD_CTX *ctx)
66 init_gost2012_hash_ctx((gost2012_hash_ctx *) ctx->md_data, 512);
70 static int gost_digest_init256(EVP_MD_CTX *ctx)
72 init_gost2012_hash_ctx((gost2012_hash_ctx *) ctx->md_data, 256);
76 static int gost_digest_update(EVP_MD_CTX *ctx, const void *data, size_t count)
78 gost2012_hash_block((gost2012_hash_ctx *) ctx->md_data, data, count);
82 static int gost_digest_final(EVP_MD_CTX *ctx, unsigned char *md)
84 gost2012_finish_hash((gost2012_hash_ctx *) ctx->md_data, md);
88 static int gost_digest_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
90 if (to->md_data && from->md_data)
91 memcpy(to->md_data, from->md_data, sizeof(gost2012_hash_ctx));
96 static int gost_digest_cleanup(EVP_MD_CTX *ctx)
99 memset(ctx->md_data, 0x00, sizeof(gost2012_hash_ctx));
104 static int gost_digest_ctrl_256(EVP_MD_CTX *ctx, int type, int arg, void *ptr)
108 case EVP_MD_CTRL_MICALG:
110 *((char **)ptr) = OPENSSL_malloc(strlen(micalg_256)+1);
111 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)
127 case EVP_MD_CTRL_MICALG:
129 *((char **)ptr) = OPENSSL_malloc(strlen(micalg_512)+1);
130 if (*((char **)ptr) != NULL)
132 strcpy(*((char **)ptr), micalg_512);