Skip to content

Commit d9915d4

Browse files
authored
Merge pull request gost-engine#520 from Mironenko/digest-mac
Introduce intermediate digest/MAC API and use it in the engine and provider
2 parents e31e59e + a13b0e4 commit d9915d4

30 files changed

Lines changed: 853 additions & 666 deletions

CMakeLists.txt

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ set(GOST_ERR_SOURCE_FILES
162162
e_gost_err.h
163163
)
164164

165-
set(GOST_LEGACY_CORE_SOURCE_FILES
165+
set(GOST_CORE_SOURCE_FILES
166166
gost_ameth.c
167167
gost_pmeth.c
168168
gost_ctl.c
@@ -178,14 +178,9 @@ set(GOST_LEGACY_CORE_SOURCE_FILES
178178
gost_lcl.h
179179
gost_params.c
180180
gost_keyexpimp.c
181-
)
182-
183-
set(GOST_NEW_CORE_DIGEST_SOURCE_FILES
184-
gost_digest_3411_2012.c
185-
gost_digest_3411_94.c
186-
gost_digest_base.c
187181
gost_digest.c
188-
)
182+
gost_digest_ctx.c
183+
)
189184

190185
set(GOST_EC_SOURCE_FILES
191186
gost_ec_keyx.c
@@ -206,14 +201,16 @@ set (GOST_OMAC_SOURCE_FILES
206201
)
207202

208203
set(GOST_LIB_SOURCE_FILES
209-
${GOST_LEGACY_CORE_SOURCE_FILES}
204+
${GOST_CORE_SOURCE_FILES}
210205
${GOST_GRASSHOPPER_SOURCE_FILES}
211206
${GOST_EC_SOURCE_FILES}
212207
${GOST_OMAC_SOURCE_FILES}
213208
)
214209

215210
set(GOST_ENGINE_SOURCE_FILES
216211
gost_eng.c
212+
gost_eng_digest.c
213+
gost_eng_digest_define.c
217214
)
218215

219216
set(GOST_PROV_SOURCE_FILES
@@ -418,10 +415,6 @@ add_library(gost_err STATIC ${GOST_ERR_SOURCE_FILES})
418415
set_target_properties(gost_err PROPERTIES POSITION_INDEPENDENT_CODE ON)
419416
target_link_libraries(gost_err PRIVATE OpenSSL::Crypto)
420417

421-
add_library(gost_new_core_digest STATIC ${GOST_NEW_CORE_DIGEST_SOURCE_FILES})
422-
set_target_properties(gost_new_core_digest PROPERTIES POSITION_INDEPENDENT_CODE ON)
423-
target_link_libraries(gost_new_core_digest PRIVATE OpenSSL::Crypto gosthash gosthash2012)
424-
425418
# The GOST engine in module form
426419
add_library(gost_engine MODULE ${GOST_ENGINE_SOURCE_FILES})
427420
# Set the suffix explicitly to adapt to OpenSSL's idea of what a
@@ -451,7 +444,7 @@ set_target_properties(gost_prov PROPERTIES
451444
PREFIX "" OUTPUT_NAME "gostprov" SUFFIX ${CMAKE_SHARED_LIBRARY_SUFFIX}
452445
COMPILE_DEFINITIONS "BUILDING_GOST_PROVIDER;OPENSSL_NO_DYNAMIC_ENGINE"
453446
)
454-
target_link_libraries(gost_prov PRIVATE gost_core gost_new_core_digest libprov)
447+
target_link_libraries(gost_prov PRIVATE gost_core libprov)
455448

456449
if (NOT MSVC)
457450
# The GOST provider in library form
@@ -462,7 +455,7 @@ set_target_properties(lib_gost_prov PROPERTIES
462455
OUTPUT_NAME "gostprov"
463456
COMPILE_DEFINITIONS "BUILDING_GOST_PROVIDER;BUILDING_PROVIDER_AS_LIBRARY;OPENSSL_NO_DYNAMIC_ENGINE"
464457
)
465-
target_link_libraries(lib_gost_prov PRIVATE gost_core gost_new_core_digest libprov)
458+
target_link_libraries(lib_gost_prov PRIVATE gost_core libprov)
466459
endif()
467460

468461
set(GOST_SUM_SOURCE_FILES

gost_crypt.c

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "gost_lcl.h"
1717
#include "gost_gost2015.h"
1818
#include "gost_tls12_additional.h"
19+
#include "gost_digest_details.h"
1920

2021
#if !defined(CCGOST_DEBUG) && !defined(DEBUG)
2122
# ifndef NDEBUG
@@ -290,23 +291,20 @@ GOST_cipher magma_cbc_cipher = {
290291

291292
/* Implementation of GOST 28147-89 in MAC (imitovstavka) mode */
292293
/* Init functions which set specific parameters */
293-
static int gost_imit_init_cpa(EVP_MD_CTX *ctx);
294-
static int gost_imit_init_cp_12(EVP_MD_CTX *ctx);
295-
/* process block of data */
296-
static int gost_imit_update(EVP_MD_CTX *ctx, const void *data, size_t count);
297-
/* Return computed value */
298-
static int gost_imit_final(EVP_MD_CTX *ctx, unsigned char *md);
299-
/* Copies context */
300-
static int gost_imit_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from);
301-
static int gost_imit_cleanup(EVP_MD_CTX *ctx);
302-
/* Control function, knows how to set MAC key.*/
303-
static int gost_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr);
304-
305-
GOST_digest Gost28147_89_MAC_digest = {
294+
static int gost_imit_init_cpa(GOST_digest_ctx *ctx);
295+
static int gost_imit_init_cp_12(GOST_digest_ctx *ctx);
296+
static int gost_imit_update(GOST_digest_ctx *ctx, const void *data,
297+
size_t count);
298+
static int gost_imit_final(GOST_digest_ctx *ctx, unsigned char *md);
299+
static int gost_imit_copy(GOST_digest_ctx *to, const GOST_digest_ctx *from);
300+
static int gost_imit_cleanup(GOST_digest_ctx *ctx);
301+
static int gost_imit_ctrl(GOST_digest_ctx *ctx, int cmd, int p1, void *p2);
302+
303+
GOST_digest Gost28147_89_mac = {
306304
.nid = NID_id_Gost28147_89_MAC,
307305
.result_size = 4,
308306
.input_blocksize = 8,
309-
.app_datasize = sizeof(struct ossl_gost_imit_ctx),
307+
.algctx_size = sizeof(struct ossl_gost_imit_ctx),
310308
.flags = EVP_MD_FLAG_XOF,
311309
.init = gost_imit_init_cpa,
312310
.update = gost_imit_update,
@@ -316,11 +314,11 @@ GOST_digest Gost28147_89_MAC_digest = {
316314
.ctrl = gost_imit_ctrl,
317315
};
318316

319-
GOST_digest Gost28147_89_mac_12_digest = {
317+
GOST_digest Gost28147_89_mac_12 = {
320318
.nid = NID_gost_mac_12,
321319
.result_size = 4,
322320
.input_blocksize = 8,
323-
.app_datasize = sizeof(struct ossl_gost_imit_ctx),
321+
.algctx_size = sizeof(struct ossl_gost_imit_ctx),
324322
.flags = EVP_MD_FLAG_XOF,
325323
.init = gost_imit_init_cp_12,
326324
.update = gost_imit_update,
@@ -1512,9 +1510,9 @@ static int magma_get_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
15121510
return 1;
15131511
}
15141512

1515-
static int gost_imit_init(EVP_MD_CTX *ctx, gost_subst_block * block)
1513+
static int gost_imit_init(GOST_digest_ctx *ctx, gost_subst_block * block)
15161514
{
1517-
struct ossl_gost_imit_ctx *c = EVP_MD_CTX_md_data(ctx);
1515+
struct ossl_gost_imit_ctx *c = GOST_digest_ctx_data(ctx);
15181516
memset(c->buffer, 0, sizeof(c->buffer));
15191517
memset(c->partial_block, 0, sizeof(c->partial_block));
15201518
c->count = 0;
@@ -1525,12 +1523,12 @@ static int gost_imit_init(EVP_MD_CTX *ctx, gost_subst_block * block)
15251523
return 1;
15261524
}
15271525

1528-
static int gost_imit_init_cpa(EVP_MD_CTX *ctx)
1526+
static int gost_imit_init_cpa(GOST_digest_ctx *ctx)
15291527
{
15301528
return gost_imit_init(ctx, &Gost28147_CryptoProParamSetA);
15311529
}
15321530

1533-
static int gost_imit_init_cp_12(EVP_MD_CTX *ctx)
1531+
static int gost_imit_init_cp_12(GOST_digest_ctx *ctx)
15341532
{
15351533
return gost_imit_init(ctx, &Gost28147_TC26ParamSetZ);
15361534
}
@@ -1551,9 +1549,9 @@ static void mac_block_mesh(struct ossl_gost_imit_ctx *c,
15511549
c->count = c->count % 1024 + 8;
15521550
}
15531551

1554-
static int gost_imit_update(EVP_MD_CTX *ctx, const void *data, size_t count)
1552+
static int gost_imit_update(GOST_digest_ctx *ctx, const void *data, size_t count)
15551553
{
1556-
struct ossl_gost_imit_ctx *c = EVP_MD_CTX_md_data(ctx);
1554+
struct ossl_gost_imit_ctx *c = GOST_digest_ctx_data(ctx);
15571555
const unsigned char *p = data;
15581556
size_t bytes = count;
15591557
if (!(c->key_set)) {
@@ -1584,9 +1582,9 @@ static int gost_imit_update(EVP_MD_CTX *ctx, const void *data, size_t count)
15841582
return 1;
15851583
}
15861584

1587-
static int gost_imit_final(EVP_MD_CTX *ctx, unsigned char *md)
1585+
static int gost_imit_final(GOST_digest_ctx *ctx, unsigned char *md)
15881586
{
1589-
struct ossl_gost_imit_ctx *c = EVP_MD_CTX_md_data(ctx);
1587+
struct ossl_gost_imit_ctx *c = GOST_digest_ctx_data(ctx);
15901588
if (!c->key_set) {
15911589
GOSTerr(GOST_F_GOST_IMIT_FINAL, GOST_R_MAC_KEY_NOT_SET);
15921590
return 0;
@@ -1607,21 +1605,21 @@ static int gost_imit_final(EVP_MD_CTX *ctx, unsigned char *md)
16071605
return 1;
16081606
}
16091607

1610-
static int gost_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr)
1608+
static int gost_imit_ctrl(GOST_digest_ctx *ctx, int type, int arg, void *ptr)
16111609
{
16121610
switch (type) {
16131611
case EVP_MD_CTRL_KEY_LEN:
16141612
*((unsigned int *)(ptr)) = 32;
16151613
return 1;
16161614
case EVP_MD_CTRL_SET_KEY:
16171615
{
1618-
struct ossl_gost_imit_ctx *gost_imit_ctx = EVP_MD_CTX_md_data(ctx);
1616+
struct ossl_gost_imit_ctx *gost_imit_ctx = GOST_digest_ctx_data(ctx);
16191617

1620-
if (EVP_MD_meth_get_init(EVP_MD_CTX_md(ctx)) (ctx) <= 0) {
1618+
if (GOST_digest_meth_get_init(GOST_digest_ctx_digest(ctx))(ctx) <= 0) {
16211619
GOSTerr(GOST_F_GOST_IMIT_CTRL, GOST_R_MAC_KEY_NOT_SET);
16221620
return 0;
16231621
}
1624-
EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_NO_INIT);
1622+
GOST_digest_ctx_set_flags(ctx, EVP_MD_CTX_FLAG_NO_INIT);
16251623

16261624
if (arg == 0) {
16271625
struct gost_mac_key *key = (struct gost_mac_key *)ptr;
@@ -1649,7 +1647,7 @@ static int gost_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr)
16491647
}
16501648
case EVP_MD_CTRL_XOF_LEN:
16511649
{
1652-
struct ossl_gost_imit_ctx *c = EVP_MD_CTX_md_data(ctx);
1650+
struct ossl_gost_imit_ctx *c = GOST_digest_ctx_data(ctx);
16531651
if (arg < 1 || arg > 8) {
16541652
GOSTerr(GOST_F_GOST_IMIT_CTRL, GOST_R_INVALID_MAC_SIZE);
16551653
return 0;
@@ -1663,19 +1661,19 @@ static int gost_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr)
16631661
}
16641662
}
16651663

1666-
static int gost_imit_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
1664+
static int gost_imit_copy(GOST_digest_ctx *to, const GOST_digest_ctx *from)
16671665
{
1668-
if (EVP_MD_CTX_md_data(to) && EVP_MD_CTX_md_data(from)) {
1669-
memcpy(EVP_MD_CTX_md_data(to), EVP_MD_CTX_md_data(from),
1666+
if (GOST_digest_ctx_data(to) && GOST_digest_ctx_data(from)) {
1667+
memcpy(GOST_digest_ctx_data(to), GOST_digest_ctx_data(from),
16701668
sizeof(struct ossl_gost_imit_ctx));
16711669
}
16721670
return 1;
16731671
}
16741672

16751673
/* Clean up imit ctx */
1676-
static int gost_imit_cleanup(EVP_MD_CTX *ctx)
1674+
static int gost_imit_cleanup(GOST_digest_ctx *ctx)
16771675
{
1678-
memset(EVP_MD_CTX_md_data(ctx), 0, sizeof(struct ossl_gost_imit_ctx));
1676+
OPENSSL_cleanse(GOST_digest_ctx_data(ctx), sizeof(struct ossl_gost_imit_ctx));
16791677
return 1;
16801678
}
16811679
/* vim: set expandtab cinoptions=\:0,l1,t0,g0,(0 sw=4 : */

gost_digest.c

Lines changed: 103 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,106 @@
11
#include "gost_digest.h"
2+
#include "gost_digest_details.h"
23

3-
void* GOST_digest_ctx_data(const GOST_digest_ctx* ctx) {
4-
return ctx->algctx;
4+
#include <openssl/evp.h>
5+
6+
static int default_static_init(const GOST_digest_ctx *ctx) {
7+
return 1;
8+
}
9+
10+
static int default_static_deinit(const GOST_digest_ctx *ctx) {
11+
return 1;
12+
}
13+
14+
static int default_init(GOST_digest_ctx *ctx) {
15+
return 1;
16+
}
17+
18+
static int default_update(GOST_digest_ctx *ctx, const void *data, size_t count) {
19+
return 1;
20+
}
21+
22+
static int default_final(GOST_digest_ctx *ctx, unsigned char *md) {
23+
return 1;
24+
}
25+
26+
static int default_copy(GOST_digest_ctx *to, const GOST_digest_ctx *from) {
27+
return 1;
28+
}
29+
30+
static int default_cleanup(GOST_digest_ctx *ctx){
31+
return 1;
32+
}
33+
34+
static int default_ctrl(GOST_digest_ctx *ctx, int cmd, int p1, void *p2) {
35+
return -2;
36+
}
37+
38+
#define THIS_OR_BASE(st, field) \
39+
THIS_OR_BASE_OR_DEFAULT(st, field, 0)
40+
41+
#define THIS_OR_BASE_OR_DEFAULT(st, field, dflt) ( \
42+
((st)->field) ? ((st)->field) : BASE_VAL(st, field, dflt) \
43+
)
44+
45+
#define BASE_VAL(st, field, dflt) ( \
46+
(((st)->base && (st)->base->field) ? (st)->base->field : dflt) \
47+
)
48+
49+
const GOST_digest* GOST_digest_init(GOST_digest* d) {
50+
if (d->this) {
51+
return d->this;
52+
}
53+
54+
d->nid = THIS_OR_BASE(d, nid);
55+
d->result_size = THIS_OR_BASE(d, result_size);
56+
d->input_blocksize = THIS_OR_BASE(d, input_blocksize);
57+
d->flags = THIS_OR_BASE(d, flags);
58+
d->alias = THIS_OR_BASE(d, alias);
59+
60+
d->algctx_size = THIS_OR_BASE(d, algctx_size);
61+
62+
d->init = THIS_OR_BASE_OR_DEFAULT(d, init, default_init);
63+
d->update = THIS_OR_BASE_OR_DEFAULT(d, update, default_update);
64+
d->final = THIS_OR_BASE_OR_DEFAULT(d, final, default_final);
65+
d->copy = THIS_OR_BASE_OR_DEFAULT(d, copy, default_copy);
66+
d->cleanup = THIS_OR_BASE_OR_DEFAULT(d, cleanup, default_cleanup);
67+
d->ctrl = THIS_OR_BASE_OR_DEFAULT(d, ctrl, default_ctrl);
68+
69+
if (d->alias)
70+
EVP_add_digest_alias(OBJ_nid2sn(d->nid), d->alias);
71+
72+
d->this = d;
73+
74+
return d;
75+
}
76+
77+
void GOST_digest_deinit(GOST_digest* d) {
78+
if (!d->this) {
79+
return;
80+
}
81+
82+
if (d->alias)
83+
EVP_delete_digest_alias(d->alias);
84+
85+
d->this = NULL;
86+
}
87+
88+
unsigned long GOST_digest_flags(const GOST_digest* d) {
89+
return d->flags;
90+
}
91+
92+
int GOST_digest_type(const GOST_digest* d) {
93+
return d->nid;
94+
}
95+
96+
int GOST_digest_block_size(const GOST_digest* d) {
97+
return d->input_blocksize;
98+
}
99+
100+
int GOST_digest_size(const GOST_digest* d) {
101+
return d->result_size;
102+
}
103+
104+
int (*GOST_digest_meth_get_init(const GOST_digest *d))(GOST_digest_ctx *) {
105+
return d->init;
5106
}

0 commit comments

Comments
 (0)