Skip to content

Commit afbb4f2

Browse files
author
Vladislav Sapegin
committed
refactor: use EVP_MAC in gost_tls12_additional_kdftree
1 parent d9915d4 commit afbb4f2

1 file changed

Lines changed: 27 additions & 16 deletions

File tree

gost_tls12_additional_kdftree.c

Lines changed: 27 additions & 16 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,28 @@ 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();
38-
if (ctx == NULL) {
44+
mac = EVP_MAC_fetch(NULL, OSSL_MAC_NAME_HMAC, NULL);
45+
if (mac == NULL || (ctx = EVP_MAC_CTX_new(mac)) == NULL) {
3946
GOSTerr(GOST_F_GOST_KDFTREE2012_256, ERR_R_MALLOC_FAILURE);
47+
EVP_MAC_free(mac);
4048
return 0;
4149
}
4250

4351
if ((keyout_len == 0) || (keyout_len % 32 != 0)) {
4452
GOSTerr(GOST_F_GOST_KDFTREE2012_256, ERR_R_INTERNAL_ERROR);
53+
EVP_MAC_CTX_free(ctx);
54+
EVP_MAC_free(mac);
4555
return 0;
4656
}
4757
iters = keyout_len / 32;
@@ -56,26 +66,27 @@ int gost_kdftree2012_256(unsigned char *keyout, size_t keyout_len,
5666
uint32_t iter_net = be32(i);
5767
unsigned char *rep_ptr =
5868
((unsigned char *)&iter_net) + (4 - representation);
69+
size_t out_len = 0;
5970

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) {
71+
if (EVP_MAC_init(ctx, key, keylen, params) <= 0
72+
|| EVP_MAC_update(ctx, rep_ptr, representation) <= 0
73+
|| EVP_MAC_update(ctx, label, label_len) <= 0
74+
|| EVP_MAC_update(ctx, &zero, 1) <= 0
75+
|| EVP_MAC_update(ctx, seed, seed_len) <= 0
76+
|| EVP_MAC_update(ctx, len_ptr, len_repr_len) <= 0
77+
|| EVP_MAC_final(ctx, ptr, &out_len, 32) <= 0
78+
|| out_len != 32) {
6979
GOSTerr(GOST_F_GOST_KDFTREE2012_256, ERR_R_INTERNAL_ERROR);
70-
HMAC_CTX_free(ctx);
80+
EVP_MAC_CTX_free(ctx);
81+
EVP_MAC_free(mac);
7182
return 0;
7283
}
7384

74-
HMAC_CTX_reset(ctx);
7585
ptr += 32;
7686
}
7787

78-
HMAC_CTX_free(ctx);
88+
EVP_MAC_CTX_free(ctx);
89+
EVP_MAC_free(mac);
7990

8091
return 1;
8192
}

0 commit comments

Comments
 (0)