|
| 1 | +#include <openssl/evp.h> |
| 2 | +#include <openssl/objects.h> |
| 3 | +#include "gosthash2012.h" |
| 4 | +#include "gost_digest_3411_2012.h" |
| 5 | +#include "gost_digest_base.h" |
| 6 | + |
| 7 | +static int gost_digest_init(GOST_digest_ctx *ctx); |
| 8 | +static int gost_digest_update(GOST_digest_ctx *ctx, const void *data, |
| 9 | + size_t count); |
| 10 | +static int gost_digest_final(GOST_digest_ctx *ctx, unsigned char *md); |
| 11 | +static int gost_digest_copy(GOST_digest_ctx *to, const GOST_digest_ctx *from); |
| 12 | +static int gost_digest_cleanup(GOST_digest_ctx *ctx); |
| 13 | + |
| 14 | +#define INIT_COMMON_MEMBERS() \ |
| 15 | + INIT_MEMBER(base, &GostR3411_digest_base), \ |
| 16 | + \ |
| 17 | + INIT_MEMBER(input_blocksize, 64), \ |
| 18 | + INIT_MEMBER(algctx_size, sizeof(gost2012_hash_ctx)), \ |
| 19 | + \ |
| 20 | + INIT_MEMBER(init, gost_digest_init), \ |
| 21 | + INIT_MEMBER(update, gost_digest_update), \ |
| 22 | + INIT_MEMBER(final, gost_digest_final), \ |
| 23 | + INIT_MEMBER(copy, gost_digest_copy), \ |
| 24 | + INIT_MEMBER(cleanup, gost_digest_cleanup) |
| 25 | + |
| 26 | +const GOST_digest GostR3411_2012_256_digest = { |
| 27 | + INIT_MEMBER(nid, NID_id_GostR3411_2012_256), |
| 28 | + INIT_MEMBER(alias, "streebog256"), |
| 29 | + INIT_MEMBER(micalg, "gostr3411-2012-256"), |
| 30 | + INIT_MEMBER(result_size, 32), |
| 31 | + |
| 32 | + INIT_COMMON_MEMBERS(), |
| 33 | +}; |
| 34 | + |
| 35 | +const GOST_digest GostR3411_2012_512_digest = { |
| 36 | + INIT_MEMBER(nid, NID_id_GostR3411_2012_512), |
| 37 | + INIT_MEMBER(alias, "streebog512"), |
| 38 | + INIT_MEMBER(micalg, "gostr3411-2012-512"), |
| 39 | + INIT_MEMBER(result_size, 64), |
| 40 | + |
| 41 | + INIT_COMMON_MEMBERS(), |
| 42 | +}; |
| 43 | + |
| 44 | +static inline gost2012_hash_ctx* impl_digest_ctx_data(const GOST_digest_ctx *ctx) { |
| 45 | + return (gost2012_hash_ctx*)GOST_digest_ctx_data(ctx); |
| 46 | +} |
| 47 | + |
| 48 | +static int gost_digest_init(GOST_digest_ctx *ctx) |
| 49 | +{ |
| 50 | + init_gost2012_hash_ctx(impl_digest_ctx_data(ctx), 8 * GET_MEMBER(ctx->cls, result_size)); |
| 51 | + return 1; |
| 52 | +} |
| 53 | + |
| 54 | +static int gost_digest_update(GOST_digest_ctx *ctx, const void *data, size_t count) |
| 55 | +{ |
| 56 | + gost2012_hash_block(impl_digest_ctx_data(ctx), data, count); |
| 57 | + return 1; |
| 58 | +} |
| 59 | + |
| 60 | +static int gost_digest_final(GOST_digest_ctx *ctx, unsigned char *md) |
| 61 | +{ |
| 62 | + gost2012_finish_hash(impl_digest_ctx_data(ctx), md); |
| 63 | + return 1; |
| 64 | +} |
| 65 | + |
| 66 | +static int gost_digest_copy(GOST_digest_ctx *to, const GOST_digest_ctx *from) |
| 67 | +{ |
| 68 | + memcpy(impl_digest_ctx_data(to), impl_digest_ctx_data(from), sizeof(gost2012_hash_ctx)); |
| 69 | + |
| 70 | + return 1; |
| 71 | +} |
| 72 | + |
| 73 | +static int gost_digest_cleanup(GOST_digest_ctx *ctx) |
| 74 | +{ |
| 75 | + OPENSSL_cleanse(impl_digest_ctx_data(ctx), sizeof(gost2012_hash_ctx)); |
| 76 | + |
| 77 | + return 1; |
| 78 | +} |
0 commit comments