Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ set(GOST_ERR_SOURCE_FILES
e_gost_err.h
)

set(GOST_CORE_SOURCE_FILES
set(GOST_LEGACY_CORE_SOURCE_FILES
gost_ameth.c
gost_pmeth.c
gost_ctl.c
Expand All @@ -174,6 +174,13 @@ set(GOST_CORE_SOURCE_FILES
gost_keyexpimp.c
)

set(GOST_NEW_CORE_DIGEST_SOURCE_FILES
gost_digest_3411_2012.c
gost_digest_3411_94.c
gost_digest_base.c
gost_digest.c
)

set(GOST_EC_SOURCE_FILES
gost_ec_keyx.c
gost_ec_sign.c
Expand All @@ -193,7 +200,7 @@ set (GOST_OMAC_SOURCE_FILES
)

set(GOST_LIB_SOURCE_FILES
${GOST_CORE_SOURCE_FILES}
${GOST_LEGACY_CORE_SOURCE_FILES}
${GOST_GRASSHOPPER_SOURCE_FILES}
${GOST_EC_SOURCE_FILES}
${GOST_OMAC_SOURCE_FILES}
Expand Down Expand Up @@ -384,7 +391,7 @@ target_link_libraries(gost89 PRIVATE OpenSSL::Crypto)

add_library(gosthash STATIC ${GOST_HASH_SOURCE_FILES})
set_target_properties(gosthash PROPERTIES POSITION_INDEPENDENT_CODE ON)
target_link_libraries(gosthash PRIVATE OpenSSL::Crypto)
target_link_libraries(gosthash PRIVATE OpenSSL::Crypto gost89)

add_library(gosthash2012 STATIC ${GOST_HASH_2012_SOURCE_FILES})
set_target_properties(gosthash2012 PROPERTIES POSITION_INDEPENDENT_CODE ON)
Expand All @@ -398,6 +405,10 @@ add_library(gost_err STATIC ${GOST_ERR_SOURCE_FILES})
set_target_properties(gost_err PROPERTIES POSITION_INDEPENDENT_CODE ON)
target_link_libraries(gost_err PRIVATE OpenSSL::Crypto)

add_library(gost_new_core_digest STATIC ${GOST_NEW_CORE_DIGEST_SOURCE_FILES})
set_target_properties(gost_new_core_digest PROPERTIES POSITION_INDEPENDENT_CODE ON)
target_link_libraries(gost_new_core_digest PRIVATE OpenSSL::Crypto gosthash gosthash2012)

# The GOST engine in module form
add_library(gost_engine MODULE ${GOST_ENGINE_SOURCE_FILES})
# Set the suffix explicitly to adapt to OpenSSL's idea of what a
Expand Down Expand Up @@ -427,7 +438,7 @@ set_target_properties(gost_prov PROPERTIES
PREFIX "" OUTPUT_NAME "gostprov" SUFFIX ${CMAKE_SHARED_LIBRARY_SUFFIX}
COMPILE_DEFINITIONS "BUILDING_GOST_PROVIDER;OPENSSL_NO_DYNAMIC_ENGINE"
)
target_link_libraries(gost_prov PRIVATE gost_core libprov)
target_link_libraries(gost_prov PRIVATE gost_core gost_new_core_digest libprov)

if (NOT MSVC)
# The GOST provider in library form
Expand All @@ -438,7 +449,7 @@ set_target_properties(lib_gost_prov PROPERTIES
OUTPUT_NAME "gostprov"
COMPILE_DEFINITIONS "BUILDING_GOST_PROVIDER;BUILDING_PROVIDER_AS_LIBRARY;OPENSSL_NO_DYNAMIC_ENGINE"
)
target_link_libraries(lib_gost_prov PRIVATE gost_core libprov)
target_link_libraries(lib_gost_prov PRIVATE gost_core gost_new_core_digest libprov)
endif()

set(GOST_SUM_SOURCE_FILES
Expand Down
5 changes: 5 additions & 0 deletions gost_digest.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "gost_digest.h"

void* GOST_digest_ctx_data(const GOST_digest_ctx* ctx) {
return ctx->algctx;
}
56 changes: 56 additions & 0 deletions gost_digest.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#pragma once

#include <stddef.h>
#include <stdint.h>

#include "utils_one_level_inheritance.h"

struct gost_digest_st;
typedef struct gost_digest_st GOST_digest;

struct gost_digest_ctx_st;
typedef struct gost_digest_ctx_st GOST_digest_ctx;

typedef GOST_digest_ctx* (gost_digest_st_new_fn)(const GOST_digest *);
typedef void (gost_digest_st_free_fn)(GOST_digest_ctx *);

typedef int (gost_digest_st_init_fn)(GOST_digest_ctx *ctx);
typedef int (gost_digest_st_update_fn)(GOST_digest_ctx *ctx, const void *data, size_t count);
typedef int (gost_digest_st_final_fn)(GOST_digest_ctx *ctx, unsigned char *md);
typedef int (gost_digest_st_copy_fn)(GOST_digest_ctx *to, const GOST_digest_ctx *from);
typedef int (gost_digest_st_cleanup_fn)(GOST_digest_ctx *ctx);
typedef int (gost_digest_st_ctrl_fn)(GOST_digest_ctx *ctx, int cmd, int p1, void *p2);

typedef void (gost_digest_st_static_init_fn)(const GOST_digest *);
typedef void (gost_digest_st_static_deinit_fn)(const GOST_digest *);

struct gost_digest_st {
DECL_BASE(const struct gost_digest_st);

DECL_MEMBER(int, nid);
DECL_MEMBER(const char *, alias);
DECL_MEMBER(int, result_size);
DECL_MEMBER(int, input_blocksize);
DECL_MEMBER(int, flags);
DECL_MEMBER(const char *, micalg);
DECL_MEMBER(size_t, algctx_size);

DECL_MEMBER(gost_digest_st_new_fn *, new);
DECL_MEMBER(gost_digest_st_free_fn *, free);
DECL_MEMBER(gost_digest_st_init_fn *, init);
DECL_MEMBER(gost_digest_st_update_fn *, update);
DECL_MEMBER(gost_digest_st_final_fn *, final);
DECL_MEMBER(gost_digest_st_copy_fn *, copy);
DECL_MEMBER(gost_digest_st_cleanup_fn *, cleanup);
DECL_MEMBER(gost_digest_st_ctrl_fn *, ctrl);

DECL_MEMBER(gost_digest_st_static_init_fn *, static_init);
DECL_MEMBER(gost_digest_st_static_deinit_fn *, static_deinit);
};

struct gost_digest_ctx_st {
const GOST_digest* cls;
void* algctx;
};

void* GOST_digest_ctx_data(const GOST_digest_ctx* ctx);
78 changes: 78 additions & 0 deletions gost_digest_3411_2012.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#include <openssl/evp.h>
#include <openssl/objects.h>
#include "gosthash2012.h"
#include "gost_digest_3411_2012.h"
#include "gost_digest_base.h"

static int gost_digest_init(GOST_digest_ctx *ctx);
static int gost_digest_update(GOST_digest_ctx *ctx, const void *data,
size_t count);
static int gost_digest_final(GOST_digest_ctx *ctx, unsigned char *md);
static int gost_digest_copy(GOST_digest_ctx *to, const GOST_digest_ctx *from);
static int gost_digest_cleanup(GOST_digest_ctx *ctx);

#define INIT_COMMON_MEMBERS() \
INIT_MEMBER(base, &GostR3411_digest_base), \
\
INIT_MEMBER(input_blocksize, 64), \
INIT_MEMBER(algctx_size, sizeof(gost2012_hash_ctx)), \
\
INIT_MEMBER(init, gost_digest_init), \
INIT_MEMBER(update, gost_digest_update), \
INIT_MEMBER(final, gost_digest_final), \
INIT_MEMBER(copy, gost_digest_copy), \
INIT_MEMBER(cleanup, gost_digest_cleanup)

const GOST_digest GostR3411_2012_256_digest = {
INIT_MEMBER(nid, NID_id_GostR3411_2012_256),
INIT_MEMBER(alias, "streebog256"),
INIT_MEMBER(micalg, "gostr3411-2012-256"),
INIT_MEMBER(result_size, 32),

INIT_COMMON_MEMBERS(),
};

const GOST_digest GostR3411_2012_512_digest = {
INIT_MEMBER(nid, NID_id_GostR3411_2012_512),
INIT_MEMBER(alias, "streebog512"),
INIT_MEMBER(micalg, "gostr3411-2012-512"),
INIT_MEMBER(result_size, 64),

INIT_COMMON_MEMBERS(),
};

static inline gost2012_hash_ctx* impl_digest_ctx_data(const GOST_digest_ctx *ctx) {
return (gost2012_hash_ctx*)GOST_digest_ctx_data(ctx);
}

static int gost_digest_init(GOST_digest_ctx *ctx)
{
init_gost2012_hash_ctx(impl_digest_ctx_data(ctx), 8 * GET_MEMBER(ctx->cls, result_size));
return 1;
}

static int gost_digest_update(GOST_digest_ctx *ctx, const void *data, size_t count)
{
gost2012_hash_block(impl_digest_ctx_data(ctx), data, count);
return 1;
}

static int gost_digest_final(GOST_digest_ctx *ctx, unsigned char *md)
{
gost2012_finish_hash(impl_digest_ctx_data(ctx), md);
return 1;
}

static int gost_digest_copy(GOST_digest_ctx *to, const GOST_digest_ctx *from)
{
memcpy(impl_digest_ctx_data(to), impl_digest_ctx_data(from), sizeof(gost2012_hash_ctx));

return 1;
}

static int gost_digest_cleanup(GOST_digest_ctx *ctx)
{
OPENSSL_cleanse(impl_digest_ctx_data(ctx), sizeof(gost2012_hash_ctx));

return 1;
}
6 changes: 6 additions & 0 deletions gost_digest_3411_2012.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#pragma once

#include "gost_digest.h"

extern const GOST_digest GostR3411_2012_256_digest;
extern const GOST_digest GostR3411_2012_512_digest;
76 changes: 76 additions & 0 deletions gost_digest_3411_94.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include <string.h>

#include <openssl/objects.h>

#include "gost_digest_3411_94.h"
#include "gost_digest_base.h"
#include "gosthash.h"
#include "gost89.h"

static int gost_digest_init(GOST_digest_ctx *ctx);
static int gost_digest_update(GOST_digest_ctx *ctx, const void *data,
size_t count);
static int gost_digest_final(GOST_digest_ctx *ctx, unsigned char *md);
static int gost_digest_copy(GOST_digest_ctx *to, const GOST_digest_ctx *from);
static int gost_digest_cleanup(GOST_digest_ctx *ctx);

struct ossl_gost_digest_ctx {
gost_hash_ctx dctx;
gost_ctx cctx;
};

static inline struct ossl_gost_digest_ctx* impl_digest_ctx_data(const GOST_digest_ctx *ctx) {
return (struct ossl_gost_digest_ctx*)GOST_digest_ctx_data(ctx);
}

const GOST_digest GostR3411_94_digest = {
INIT_MEMBER(nid, NID_id_GostR3411_94),
INIT_MEMBER(result_size, 32),
INIT_MEMBER(input_blocksize, 32),
INIT_MEMBER(algctx_size, sizeof(struct ossl_gost_digest_ctx)),

INIT_MEMBER(base, &GostR3411_digest_base),

INIT_MEMBER(init, gost_digest_init),
INIT_MEMBER(update, gost_digest_update),
INIT_MEMBER(final, gost_digest_final),
INIT_MEMBER(copy, gost_digest_copy),
INIT_MEMBER(cleanup, gost_digest_cleanup),
};

static int gost_digest_init(GOST_digest_ctx *ctx)
{
struct ossl_gost_digest_ctx *c = impl_digest_ctx_data(ctx);
memset(&(c->dctx), 0, sizeof(gost_hash_ctx));
gost_init(&(c->cctx), &GostR3411_94_CryptoProParamSet);
c->dctx.cipher_ctx = &(c->cctx);
return 1;
}

static int gost_digest_update(GOST_digest_ctx *ctx, const void *data, size_t count)
{
return hash_block(&(impl_digest_ctx_data(ctx)->dctx), data, count);
}

static int gost_digest_final(GOST_digest_ctx *ctx, unsigned char *md)
{
return finish_hash(&(impl_digest_ctx_data(ctx)->dctx), md);
}

static int gost_digest_copy(GOST_digest_ctx *to, const GOST_digest_ctx *from)
{
struct ossl_gost_digest_ctx *md_ctx = impl_digest_ctx_data(to);
if (impl_digest_ctx_data(to) && impl_digest_ctx_data(from)) {
memcpy(impl_digest_ctx_data(to), impl_digest_ctx_data(from),
sizeof(struct ossl_gost_digest_ctx));
md_ctx->dctx.cipher_ctx = &(md_ctx->cctx);
}
return 1;
}

static int gost_digest_cleanup(GOST_digest_ctx *ctx)
{
if (impl_digest_ctx_data(ctx))
OPENSSL_cleanse(impl_digest_ctx_data(ctx), sizeof(struct ossl_gost_digest_ctx));
return 1;
}
5 changes: 5 additions & 0 deletions gost_digest_3411_94.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

#include "gost_digest.h"

extern const GOST_digest GostR3411_94_digest;
51 changes: 51 additions & 0 deletions gost_digest_base.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <openssl/evp.h>

#include "gost_digest_base.h"

static void gost_digest_static_init(const GOST_digest* d);
static void gost_digest_static_deinit(const GOST_digest* d);

static GOST_digest_ctx* gost_digest_new(const GOST_digest* d);
static void gost_digest_free(GOST_digest_ctx* vctx);

const GOST_digest GostR3411_digest_base = {
INIT_MEMBER(static_init, gost_digest_static_init),
INIT_MEMBER(static_deinit, gost_digest_static_deinit),
INIT_MEMBER(new, gost_digest_new),
INIT_MEMBER(free, gost_digest_free),
};

static GOST_digest_ctx* gost_digest_new(const GOST_digest *d)
{
GOST_digest_ctx *ctx = (GOST_digest_ctx*)OPENSSL_zalloc(sizeof(GOST_digest_ctx));
if (!ctx)
return ctx;

ctx->cls = d;
ctx->algctx = OPENSSL_zalloc(GET_MEMBER(d, algctx_size));
if (!ctx->algctx) {
OPENSSL_free(ctx);
ctx = NULL;
}

return ctx;
}

void gost_digest_free(GOST_digest_ctx *ctx)
{
if (!ctx)
return;

OPENSSL_free(ctx->algctx);
OPENSSL_free(ctx);
}

static void gost_digest_static_init(const GOST_digest* d) {
if (GET_MEMBER(d, alias))
EVP_add_digest_alias(OBJ_nid2sn(GET_MEMBER(d, nid)), GET_MEMBER(d, alias));
}

static void gost_digest_static_deinit(const GOST_digest* d) {
if (GET_MEMBER(d, alias))
EVP_delete_digest_alias(GET_MEMBER(d, alias));
}
5 changes: 5 additions & 0 deletions gost_digest_base.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

#include "gost_digest.h"

extern const GOST_digest GostR3411_digest_base;
6 changes: 3 additions & 3 deletions gost_eng.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ static EVP_PKEY_ASN1_METHOD* ameth_GostR3410_2001 = NULL,
* ameth_magma_mac_acpkm = NULL, * ameth_grasshopper_mac_acpkm = NULL;

GOST_digest *gost_digest_array[] = {
&GostR3411_94_digest,
&GostR3411_94_digest_legacy,
&Gost28147_89_MAC_digest,
&GostR3411_2012_256_digest,
&GostR3411_2012_512_digest,
&GostR3411_2012_256_digest_legacy,
&GostR3411_2012_512_digest_legacy,
&Gost28147_89_mac_12_digest,
&magma_mac_digest,
&grasshopper_mac_digest,
Expand Down
6 changes: 3 additions & 3 deletions gost_lcl.h
Original file line number Diff line number Diff line change
Expand Up @@ -439,11 +439,11 @@ int internal_print_gost_ec_pub(BIO *out, const EC_KEY *ec, int indent, int pkey_
int internal_print_gost_ec_param(BIO *out, const EC_KEY *ec, int indent);

/* ENGINE implementation data */
extern GOST_digest GostR3411_94_digest;
extern GOST_digest GostR3411_94_digest_legacy;
extern GOST_digest Gost28147_89_MAC_digest;
extern GOST_digest Gost28147_89_mac_12_digest;
extern GOST_digest GostR3411_2012_256_digest;
extern GOST_digest GostR3411_2012_512_digest;
extern GOST_digest GostR3411_2012_256_digest_legacy;
extern GOST_digest GostR3411_2012_512_digest_legacy;
extern GOST_digest magma_mac_digest;
extern GOST_digest grasshopper_mac_digest;
extern GOST_digest kuznyechik_ctracpkm_omac_digest;
Expand Down
2 changes: 1 addition & 1 deletion gost_md.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ static int gost_digest_final(EVP_MD_CTX *ctx, unsigned char *md);
static int gost_digest_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from);
static int gost_digest_cleanup(EVP_MD_CTX *ctx);

GOST_digest GostR3411_94_digest = {
GOST_digest GostR3411_94_digest_legacy = {
.nid = NID_id_GostR3411_94,
.result_size = 32,
.input_blocksize = 32,
Expand Down
Loading
Loading