99 **/
1010
1111#include "internal_crypt_lib.h"
12- #include <openssl/hmac.h>
12+ #include <openssl/evp.h>
13+ #include <openssl/core_names.h>
14+ #include <string.h>
1315
1416/**
1517 * Allocates and initializes one HMAC_CTX context for subsequent HMAC-MD use.
2022 **/
2123void * hmac_md_new (void )
2224{
23-
24- /* Allocates & Initializes HMAC_CTX context by OpenSSL HMAC_CTX_new()*/
25-
26- return (void * )HMAC_CTX_new ();
25+ /* Create EVP_MAC context for HMAC using new API */
26+ EVP_MAC * mac = EVP_MAC_fetch (NULL , "HMAC" , NULL );
27+ if (mac == NULL ) {
28+ return NULL ;
29+ }
30+
31+ EVP_MAC_CTX * ctx = EVP_MAC_CTX_new (mac );
32+ EVP_MAC_free (mac );
33+
34+ return (void * )ctx ;
2735}
2836
2937/**
3038 * Release the specified HMAC_CTX context.
31- *
39+ *
3240 * @param[in] hmac_md_ctx Pointer to the HMAC_CTX context to be released.
3341 *
3442 **/
3543void hmac_md_free (void * hmac_md_ctx )
3644{
37-
38- /* Free OpenSSL HMAC_CTX context*/
39-
40- HMAC_CTX_free (( HMAC_CTX * ) hmac_md_ctx );
45+ /* Free EVP_MAC_CTX context */
46+ if ( hmac_md_ctx != NULL ) {
47+ EVP_MAC_CTX_free (( EVP_MAC_CTX * ) hmac_md_ctx );
48+ }
4149}
4250
4351/**
@@ -58,15 +66,29 @@ void hmac_md_free(void *hmac_md_ctx)
5866bool hmac_md_set_key (const EVP_MD * md , void * hmac_md_ctx ,
5967 const uint8_t * key , size_t key_size )
6068{
69+ /* Check input parameters */
70+ if (hmac_md_ctx == NULL || key == NULL || key_size == 0 ) {
71+ return false;
72+ }
6173
62- /* Check input parameters.*/
63-
64- if (hmac_md_ctx == NULL || key_size > INT_MAX ) {
74+ EVP_MAC_CTX * ctx = (EVP_MAC_CTX * )hmac_md_ctx ;
75+
76+ /* Get digest name from EVP_MD */
77+ const char * digest_name = EVP_MD_get0_name (md );
78+ if (digest_name == NULL ) {
6579 return false;
6680 }
6781
68- if (HMAC_Init_ex ((HMAC_CTX * )hmac_md_ctx , key , (uint32_t )key_size , md ,
69- NULL ) != 1 ) {
82+ /* Setup parameters for HMAC */
83+ OSSL_PARAM params [3 ];
84+ params [0 ] = OSSL_PARAM_construct_utf8_string (OSSL_MAC_PARAM_DIGEST ,
85+ (char * )digest_name , 0 );
86+ params [1 ] = OSSL_PARAM_construct_octet_string (OSSL_MAC_PARAM_KEY ,
87+ (void * )key , key_size );
88+ params [2 ] = OSSL_PARAM_construct_end ();
89+
90+ /* Initialize MAC operation with key and parameters */
91+ if (EVP_MAC_init (ctx , key , key_size , params ) != 1 ) {
7092 return false;
7193 }
7294
@@ -88,19 +110,51 @@ bool hmac_md_set_key(const EVP_MD *md, void *hmac_md_ctx,
88110 **/
89111bool hmac_md_duplicate (const void * hmac_md_ctx , void * new_hmac_md_ctx )
90112{
113+ if (hmac_md_ctx == NULL || new_hmac_md_ctx == NULL ) {
114+ return false;
115+ }
91116
92- /* Check input parameters.*/
117+ EVP_MAC_CTX * src_ctx = (EVP_MAC_CTX * )hmac_md_ctx ;
118+ EVP_MAC_CTX * dst_ctx = (EVP_MAC_CTX * )new_hmac_md_ctx ;
93119
94- if (hmac_md_ctx == NULL || new_hmac_md_ctx == NULL ) {
120+ /* Get parameters from source context */
121+ OSSL_PARAM params [2 ];
122+ unsigned char * key = NULL ;
123+ size_t key_len = 0 ;
124+ char digest_name [64 ] = {0 }; // Preallocate buffer instead of using pointer to pointer
125+
126+ /* Get key - use fixed buffer size */
127+ params [0 ] = OSSL_PARAM_construct_octet_string ("key" , & key , 0 );
128+ params [1 ] = OSSL_PARAM_construct_end ();
129+
130+ if (EVP_MAC_CTX_get_params (src_ctx , params ) != 1 ) {
95131 return false;
96132 }
97133
98- if (HMAC_CTX_copy ((HMAC_CTX * )new_hmac_md_ctx ,
99- (HMAC_CTX * )hmac_md_ctx ) != 1 ) {
134+ /* Get digest algorithm name - use preallocated buffer */
135+ params [0 ] = OSSL_PARAM_construct_utf8_string ("digest" , digest_name , sizeof (digest_name ));
136+ params [1 ] = OSSL_PARAM_construct_end ();
137+
138+ if (EVP_MAC_CTX_get_params (src_ctx , params ) != 1 ) {
139+ OPENSSL_free (key );
100140 return false;
101141 }
102142
103- return true;
143+ /* Set parameters for destination context */
144+ OSSL_PARAM set_params [3 ];
145+ set_params [0 ] = OSSL_PARAM_construct_octet_string ("key" , key , key_len );
146+ set_params [1 ] = OSSL_PARAM_construct_utf8_string ("digest" , digest_name , 0 );
147+ set_params [2 ] = OSSL_PARAM_construct_end ();
148+
149+ bool result = false;
150+ if (EVP_MAC_init (dst_ctx , key , key_len , set_params ) == 1 ) {
151+ result = true;
152+ }
153+
154+ /* Clean up temporarily allocated memory */
155+ OPENSSL_free (key );
156+
157+ return result ;
104158}
105159
106160/**
@@ -124,24 +178,23 @@ bool hmac_md_duplicate(const void *hmac_md_ctx, void *new_hmac_md_ctx)
124178bool hmac_md_update (void * hmac_md_ctx , const void * data ,
125179 size_t data_size )
126180{
127-
128- /* Check input parameters.*/
129-
181+ /* Check input parameters */
130182 if (hmac_md_ctx == NULL ) {
131183 return false;
132184 }
133185
134-
135- /* Check invalid parameters, in case that only DataLength was checked in OpenSSL*/
136-
186+ /* Check invalid parameters */
137187 if (data == NULL && data_size != 0 ) {
138188 return false;
139189 }
140190
191+ /* If data_size is 0 and data is NULL, it's a valid case - do nothing */
192+ if (data_size == 0 ) {
193+ return true;
194+ }
141195
142- /* OpenSSL HMAC-MD digest update*/
143-
144- if (HMAC_Update ((HMAC_CTX * )hmac_md_ctx , data , data_size ) != 1 ) {
196+ /* Update MAC computation with new data */
197+ if (EVP_MAC_update ((EVP_MAC_CTX * )hmac_md_ctx , data , data_size ) != 1 ) {
145198 return false;
146199 }
147200
@@ -170,22 +223,16 @@ bool hmac_md_update(void *hmac_md_ctx, const void *data,
170223 **/
171224bool hmac_md_final (void * hmac_md_ctx , uint8_t * hmac_value )
172225{
173- uint32_t length ;
174-
175-
176- /* Check input parameters.*/
177-
226+ size_t out_len = 0 ;
227+
228+ /* Check input parameters */
178229 if (hmac_md_ctx == NULL || hmac_value == NULL ) {
179230 return false;
180231 }
181232
182-
183- /* OpenSSL HMAC-MD digest finalization*/
184-
185- if (HMAC_Final ((HMAC_CTX * )hmac_md_ctx , hmac_value , & length ) != 1 ) {
186- return false;
187- }
188- if (HMAC_CTX_reset ((HMAC_CTX * )hmac_md_ctx ) != 1 ) {
233+ /* Finalize MAC computation and get the result */
234+ if (EVP_MAC_final ((EVP_MAC_CTX * )hmac_md_ctx , hmac_value , & out_len ,
235+ EVP_MAC_CTX_get_mac_size ((EVP_MAC_CTX * )hmac_md_ctx )) != 1 ) {
189236 return false;
190237 }
191238
@@ -217,35 +264,60 @@ bool hmac_md_all(const EVP_MD *md, const void *data,
217264 size_t data_size , const uint8_t * key , size_t key_size ,
218265 uint8_t * hmac_value )
219266{
220- uint32_t length ;
221- HMAC_CTX * ctx ;
222- bool ret_val ;
267+ EVP_MAC * mac = NULL ;
268+ EVP_MAC_CTX * ctx = NULL ;
269+ size_t out_len = 0 ;
270+ bool ret_val = false;
271+
272+ /* Check input parameters */
273+ if (md == NULL || data == NULL || key == NULL || hmac_value == NULL ) {
274+ return false;
275+ }
223276
224- ctx = HMAC_CTX_new ();
277+ /* Create MAC object and context */
278+ mac = EVP_MAC_fetch (NULL , "HMAC" , NULL );
279+ if (mac == NULL ) {
280+ goto done ;
281+ }
282+
283+ ctx = EVP_MAC_CTX_new (mac );
225284 if (ctx == NULL ) {
226- return false ;
285+ goto done ;
227286 }
228287
229- ret_val = (bool )HMAC_CTX_reset (ctx );
230- if (!ret_val ) {
288+ /* Get digest name */
289+ const char * digest_name = EVP_MD_get0_name (md );
290+ if (digest_name == NULL ) {
231291 goto done ;
232292 }
233- ret_val = (bool )HMAC_Init_ex (ctx , key , (uint32_t )key_size , md , NULL );
234- if (!ret_val ) {
293+
294+ /* Setup parameters */
295+ OSSL_PARAM params [2 ];
296+ params [0 ] = OSSL_PARAM_construct_utf8_string (OSSL_MAC_PARAM_DIGEST ,
297+ (char * )digest_name , 0 );
298+ params [1 ] = OSSL_PARAM_construct_end ();
299+
300+ /* Initialize with key */
301+ if (EVP_MAC_init (ctx , key , key_size , params ) != 1 ) {
235302 goto done ;
236303 }
237- ret_val = (bool )HMAC_Update (ctx , data , data_size );
238- if (!ret_val ) {
304+
305+ /* Update with data */
306+ if (data_size > 0 && EVP_MAC_update (ctx , data , data_size ) != 1 ) {
239307 goto done ;
240308 }
241- ret_val = (bool )HMAC_Final (ctx , hmac_value , & length );
242- if (!ret_val ) {
309+
310+ /* Finalize and get result */
311+ size_t mac_size = EVP_MAC_CTX_get_mac_size (ctx );
312+ if (EVP_MAC_final (ctx , hmac_value , & out_len , mac_size ) != 1 ) {
243313 goto done ;
244314 }
245315
246- done :
247- HMAC_CTX_free (ctx );
316+ ret_val = true;
248317
318+ done :
319+ EVP_MAC_CTX_free (ctx );
320+ EVP_MAC_free (mac );
249321 return ret_val ;
250322}
251323
0 commit comments