Skip to content

Commit 94181b0

Browse files
authored
Merge pull request gost-engine#522 from Mironenko/refactor-hmac-ctx
refactor: use EVP_MAC in gost_tls12_additional_kdftree
2 parents d9915d4 + 7fda7e7 commit 94181b0

1 file changed

Lines changed: 32 additions & 15 deletions

File tree

gost_tls12_additional_kdftree.c

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#include <string.h>
22

33
#include <openssl/buffer.h>
4+
#include <openssl/core_names.h>
45
#include <openssl/err.h>
56
#include <openssl/evp.h>
6-
#include <openssl/hmac.h>
7+
#include <openssl/params.h>
78

89
#include "gost_tls12_additional.h"
910
#include "e_gost_err.h"
@@ -29,19 +30,34 @@ int gost_kdftree2012_256(unsigned char *keyout, size_t keyout_len,
2930
int iters, i = 0;
3031
unsigned char zero = 0;
3132
unsigned char *ptr = keyout;
32-
HMAC_CTX *ctx;
33+
EVP_MAC *mac = NULL;
34+
EVP_MAC_CTX *ctx = NULL;
3335
unsigned char *len_ptr = NULL;
3436
uint32_t len_repr = be32(keyout_len * 8);
3537
size_t len_repr_len = 4;
38+
OSSL_PARAM params[] = {
39+
OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_DIGEST,
40+
(char *)SN_id_GostR3411_2012_256, 0),
41+
OSSL_PARAM_END
42+
};
3643

37-
ctx = HMAC_CTX_new();
44+
mac = EVP_MAC_fetch(NULL, OSSL_MAC_NAME_HMAC, NULL);
45+
if (mac == NULL) {
46+
GOSTerr(GOST_F_GOST_KDFTREE2012_256, ERR_R_INTERNAL_ERROR);
47+
return 0;
48+
}
49+
50+
ctx = EVP_MAC_CTX_new(mac);
3851
if (ctx == NULL) {
3952
GOSTerr(GOST_F_GOST_KDFTREE2012_256, ERR_R_MALLOC_FAILURE);
53+
EVP_MAC_free(mac);
4054
return 0;
4155
}
4256

4357
if ((keyout_len == 0) || (keyout_len % 32 != 0)) {
4458
GOSTerr(GOST_F_GOST_KDFTREE2012_256, ERR_R_INTERNAL_ERROR);
59+
EVP_MAC_CTX_free(ctx);
60+
EVP_MAC_free(mac);
4561
return 0;
4662
}
4763
iters = keyout_len / 32;
@@ -56,26 +72,27 @@ int gost_kdftree2012_256(unsigned char *keyout, size_t keyout_len,
5672
uint32_t iter_net = be32(i);
5773
unsigned char *rep_ptr =
5874
((unsigned char *)&iter_net) + (4 - representation);
75+
size_t out_len = 0;
5976

60-
if (HMAC_Init_ex(ctx, key, keylen,
61-
EVP_get_digestbynid(NID_id_GostR3411_2012_256),
62-
NULL) <= 0
63-
|| HMAC_Update(ctx, rep_ptr, representation) <= 0
64-
|| HMAC_Update(ctx, label, label_len) <= 0
65-
|| HMAC_Update(ctx, &zero, 1) <= 0
66-
|| HMAC_Update(ctx, seed, seed_len) <= 0
67-
|| HMAC_Update(ctx, len_ptr, len_repr_len) <= 0
68-
|| HMAC_Final(ctx, ptr, NULL) <= 0) {
77+
if (EVP_MAC_init(ctx, key, keylen, params) <= 0
78+
|| EVP_MAC_update(ctx, rep_ptr, representation) <= 0
79+
|| EVP_MAC_update(ctx, label, label_len) <= 0
80+
|| EVP_MAC_update(ctx, &zero, 1) <= 0
81+
|| EVP_MAC_update(ctx, seed, seed_len) <= 0
82+
|| EVP_MAC_update(ctx, len_ptr, len_repr_len) <= 0
83+
|| EVP_MAC_final(ctx, ptr, &out_len, 32) <= 0
84+
|| out_len != 32) {
6985
GOSTerr(GOST_F_GOST_KDFTREE2012_256, ERR_R_INTERNAL_ERROR);
70-
HMAC_CTX_free(ctx);
86+
EVP_MAC_CTX_free(ctx);
87+
EVP_MAC_free(mac);
7188
return 0;
7289
}
7390

74-
HMAC_CTX_reset(ctx);
7591
ptr += 32;
7692
}
7793

78-
HMAC_CTX_free(ctx);
94+
EVP_MAC_CTX_free(ctx);
95+
EVP_MAC_free(mac);
7996

8097
return 1;
8198
}

0 commit comments

Comments
 (0)