19 #include <winpr/config.h>
21 #include <winpr/crt.h>
22 #include <winpr/assert.h>
23 #include <winpr/crypto.h>
26 #include <openssl/md4.h>
27 #include <openssl/md5.h>
28 #include <openssl/sha.h>
29 #include <openssl/evp.h>
30 #include <openssl/hmac.h>
31 #if OPENSSL_VERSION_NUMBER >= 0x30000000L
32 #include <openssl/core_names.h>
38 #include <mbedtls/md5.h>
40 #include <mbedtls/sha1.h>
41 #include <mbedtls/md.h>
42 #if MBEDTLS_VERSION_MAJOR < 3
43 #define mbedtls_md_info_from_ctx(_ctx) (_ctx->md_info)
47 #if defined(WITH_INTERNAL_MD4)
51 #if defined(WITH_INTERNAL_MD5)
57 #define TAG WINPR_TAG("crypto.hash")
64 extern const EVP_MD* winpr_openssl_get_evp_md(WINPR_MD_TYPE md);
68 const EVP_MD* winpr_openssl_get_evp_md(WINPR_MD_TYPE md)
70 const char* name = winpr_md_type_to_string(md);
73 return EVP_get_digestbyname(name);
78 mbedtls_md_type_t winpr_mbedtls_get_md_type(
int md)
80 mbedtls_md_type_t type = MBEDTLS_MD_NONE;
85 type = MBEDTLS_MD_MD5;
89 type = MBEDTLS_MD_SHA1;
93 type = MBEDTLS_MD_SHA224;
97 type = MBEDTLS_MD_SHA256;
100 case WINPR_MD_SHA384:
101 type = MBEDTLS_MD_SHA384;
104 case WINPR_MD_SHA512:
105 type = MBEDTLS_MD_SHA512;
118 static const struct hash_map hashes[] = { {
"md2", WINPR_MD_MD2 },
119 {
"md4", WINPR_MD_MD4 },
120 {
"md5", WINPR_MD_MD5 },
121 {
"sha1", WINPR_MD_SHA1 },
122 {
"sha224", WINPR_MD_SHA224 },
123 {
"sha256", WINPR_MD_SHA256 },
124 {
"sha384", WINPR_MD_SHA384 },
125 {
"sha512", WINPR_MD_SHA512 },
126 {
"sha3_224", WINPR_MD_SHA3_224 },
127 {
"sha3_256", WINPR_MD_SHA3_256 },
128 {
"sha3_384", WINPR_MD_SHA3_384 },
129 {
"sha3_512", WINPR_MD_SHA3_512 },
130 {
"shake128", WINPR_MD_SHAKE128 },
131 {
"shake256", WINPR_MD_SHAKE256 },
132 { NULL, WINPR_MD_NONE } };
134 WINPR_MD_TYPE winpr_md_type_from_string(
const char* name)
136 const struct hash_map* cur = hashes;
139 if (_stricmp(cur->name, name) == 0)
143 return WINPR_MD_NONE;
146 const char* winpr_md_type_to_string(WINPR_MD_TYPE md)
148 const struct hash_map* cur = hashes;
158 struct winpr_hmac_ctx_private_st
162 #if defined(WITH_INTERNAL_MD5)
165 #if defined(WITH_OPENSSL)
166 #if OPENSSL_VERSION_NUMBER >= 0x30000000L
172 #if defined(WITH_MBEDTLS)
173 mbedtls_md_context_t hmac;
177 WINPR_HMAC_CTX* winpr_HMAC_New(
void)
179 WINPR_HMAC_CTX* ctx = (WINPR_HMAC_CTX*)calloc(1,
sizeof(WINPR_HMAC_CTX));
182 #if defined(WITH_OPENSSL)
183 #if (OPENSSL_VERSION_NUMBER < 0x10100000L) || \
184 (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x2070000fL)
186 if (!(ctx->hmac = (HMAC_CTX*)calloc(1,
sizeof(HMAC_CTX))))
189 HMAC_CTX_init(ctx->hmac);
190 #elif OPENSSL_VERSION_NUMBER < 0x30000000L
191 if (!(ctx->hmac = HMAC_CTX_new()))
194 EVP_MAC* emac = EVP_MAC_fetch(NULL,
"HMAC", NULL);
197 ctx->xhmac = EVP_MAC_CTX_new(emac);
202 #elif defined(WITH_MBEDTLS)
203 mbedtls_md_init(&ctx->hmac);
208 WINPR_PRAGMA_DIAG_PUSH
209 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
210 winpr_HMAC_Free(ctx);
211 WINPR_PRAGMA_DIAG_POP
215 BOOL winpr_HMAC_Init(WINPR_HMAC_CTX* ctx, WINPR_MD_TYPE md,
const void* key,
size_t keylen)
222 #if defined(WITH_INTERNAL_MD5)
224 hmac_md5_init(&ctx->hmac_md5, key, keylen);
231 #if defined(WITH_OPENSSL)
232 #if OPENSSL_VERSION_NUMBER >= 0x30000000L
233 char* hash = WINPR_CAST_CONST_PTR_AWAY(winpr_md_type_to_string(md),
char*);
238 const char* param_name = OSSL_MAC_PARAM_DIGEST;
239 const OSSL_PARAM param[] = { OSSL_PARAM_construct_utf8_string(param_name, hash, 0),
240 OSSL_PARAM_construct_end() };
242 if (EVP_MAC_init(ctx->xhmac, key, keylen, param) == 1)
245 HMAC_CTX* hmac = ctx->hmac;
246 const EVP_MD* evp = winpr_openssl_get_evp_md(md);
251 if (keylen > INT_MAX)
253 #if (OPENSSL_VERSION_NUMBER < 0x10000000L) || \
254 (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x2070000fL)
255 HMAC_Init_ex(hmac, key, (
int)keylen, evp, NULL);
259 if (HMAC_Init_ex(hmac, key, (
int)keylen, evp, NULL) == 1)
264 #elif defined(WITH_MBEDTLS)
265 mbedtls_md_context_t* hmac = &ctx->hmac;
266 mbedtls_md_type_t md_type = winpr_mbedtls_get_md_type(md);
267 const mbedtls_md_info_t* md_info = mbedtls_md_info_from_type(md_type);
269 if (!md_info || !hmac)
272 if (mbedtls_md_info_from_ctx(hmac) != md_info)
274 mbedtls_md_free(hmac);
276 if (mbedtls_md_setup(hmac, md_info, 1) != 0)
280 if (mbedtls_md_hmac_starts(hmac, key, keylen) == 0)
287 BOOL winpr_HMAC_Update(WINPR_HMAC_CTX* ctx,
const void* input,
size_t ilen)
293 #if defined(WITH_INTERNAL_MD5)
295 hmac_md5_update(&ctx->hmac_md5, input, ilen);
302 #if defined(WITH_OPENSSL)
303 #if OPENSSL_VERSION_NUMBER >= 0x30000000L
304 if (EVP_MAC_update(ctx->xhmac, input, ilen) == 1)
307 HMAC_CTX* hmac = ctx->hmac;
308 #if (OPENSSL_VERSION_NUMBER < 0x10000000L) || \
309 (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x2070000fL)
310 HMAC_Update(hmac, input, ilen);
314 if (HMAC_Update(hmac, input, ilen) == 1)
318 #elif defined(WITH_MBEDTLS)
319 mbedtls_md_context_t* mdctx = &ctx->hmac;
321 if (mbedtls_md_hmac_update(mdctx, input, ilen) == 0)
328 BOOL winpr_HMAC_Final(WINPR_HMAC_CTX* ctx,
void* output,
size_t olen)
334 #if defined(WITH_INTERNAL_MD5)
336 if (olen < WINPR_MD5_DIGEST_LENGTH)
338 hmac_md5_finalize(&ctx->hmac_md5, output);
345 #if defined(WITH_OPENSSL)
346 #if OPENSSL_VERSION_NUMBER >= 0x30000000L
347 const int rc = EVP_MAC_final(ctx->xhmac, output, NULL, olen);
351 HMAC_CTX* hmac = ctx->hmac;
352 #if (OPENSSL_VERSION_NUMBER < 0x10000000L) || \
353 (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x2070000fL)
354 HMAC_Final(hmac, output, NULL);
358 if (HMAC_Final(hmac, output, NULL) == 1)
363 #elif defined(WITH_MBEDTLS)
364 mbedtls_md_context_t* mdctx = &ctx->hmac;
366 if (mbedtls_md_hmac_finish(mdctx, output) == 0)
373 void winpr_HMAC_Free(WINPR_HMAC_CTX* ctx)
378 #if defined(WITH_OPENSSL)
379 #if OPENSSL_VERSION_NUMBER >= 0x30000000L
380 EVP_MAC_CTX_free(ctx->xhmac);
382 HMAC_CTX* hmac = ctx->hmac;
386 #if (OPENSSL_VERSION_NUMBER < 0x10100000L) || \
387 (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x2070000fL)
388 HMAC_CTX_cleanup(hmac);
395 #elif defined(WITH_MBEDTLS)
396 mbedtls_md_context_t* hmac = &ctx->hmac;
399 mbedtls_md_free(hmac);
406 BOOL winpr_HMAC(WINPR_MD_TYPE md,
const void* key,
size_t keylen,
const void* input,
size_t ilen,
407 void* output,
size_t olen)
410 WINPR_HMAC_CTX* ctx = winpr_HMAC_New();
415 if (!winpr_HMAC_Init(ctx, md, key, keylen))
418 if (!winpr_HMAC_Update(ctx, input, ilen))
421 if (!winpr_HMAC_Final(ctx, output, olen))
426 winpr_HMAC_Free(ctx);
434 struct winpr_digest_ctx_private_st
438 #if defined(WITH_INTERNAL_MD4)
441 #if defined(WITH_INTERNAL_MD5)
444 #if defined(WITH_OPENSSL)
447 #if defined(WITH_MBEDTLS)
448 mbedtls_md_context_t* mdctx;
452 WINPR_DIGEST_CTX* winpr_Digest_New(
void)
454 WINPR_DIGEST_CTX* ctx = calloc(1,
sizeof(WINPR_DIGEST_CTX));
458 #if defined(WITH_OPENSSL)
459 #if (OPENSSL_VERSION_NUMBER < 0x10100000L) || \
460 (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x2070000fL)
461 ctx->mdctx = EVP_MD_CTX_create();
463 ctx->mdctx = EVP_MD_CTX_new();
468 #elif defined(WITH_MBEDTLS)
469 ctx->mdctx = (mbedtls_md_context_t*)calloc(1,
sizeof(mbedtls_md_context_t));
474 mbedtls_md_init(ctx->mdctx);
479 WINPR_PRAGMA_DIAG_PUSH
480 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
481 winpr_Digest_Free(ctx);
482 WINPR_PRAGMA_DIAG_POP
486 #if defined(WITH_OPENSSL)
487 static BOOL winpr_Digest_Init_Internal(WINPR_DIGEST_CTX* ctx,
const EVP_MD* evp)
490 EVP_MD_CTX* mdctx = ctx->mdctx;
495 if (EVP_DigestInit_ex(mdctx, evp, NULL) != 1)
497 WLog_ERR(TAG,
"Failed to initialize digest %s", winpr_md_type_to_string(ctx->md));
504 #elif defined(WITH_MBEDTLS)
505 static BOOL winpr_Digest_Init_Internal(WINPR_DIGEST_CTX* ctx, WINPR_MD_TYPE md)
508 mbedtls_md_context_t* mdctx = ctx->mdctx;
509 mbedtls_md_type_t md_type = winpr_mbedtls_get_md_type(md);
510 const mbedtls_md_info_t* md_info = mbedtls_md_info_from_type(md_type);
515 if (mbedtls_md_info_from_ctx(mdctx) != md_info)
517 mbedtls_md_free(mdctx);
519 if (mbedtls_md_setup(mdctx, md_info, 0) != 0)
523 if (mbedtls_md_starts(mdctx) != 0)
530 BOOL winpr_Digest_Init_Allow_FIPS(WINPR_DIGEST_CTX* ctx, WINPR_MD_TYPE md)
538 #if defined(WITH_INTERNAL_MD5)
539 winpr_MD5_Init(&ctx->md5);
545 WLog_ERR(TAG,
"Invalid FIPS digest %s requested", winpr_md_type_to_string(md));
549 #if defined(WITH_OPENSSL)
550 #if OPENSSL_VERSION_NUMBER >= 0x30000000L
551 if (md == WINPR_MD_MD5)
553 EVP_MD* md5 = EVP_MD_fetch(NULL,
"MD5",
"fips=no");
554 BOOL rc = winpr_Digest_Init_Internal(ctx, md5);
559 const EVP_MD* evp = winpr_openssl_get_evp_md(md);
560 EVP_MD_CTX_set_flags(ctx->mdctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
561 return winpr_Digest_Init_Internal(ctx, evp);
562 #elif defined(WITH_MBEDTLS)
563 return winpr_Digest_Init_Internal(ctx, md);
567 BOOL winpr_Digest_Init(WINPR_DIGEST_CTX* ctx, WINPR_MD_TYPE md)
574 #if defined(WITH_INTERNAL_MD4)
576 winpr_MD4_Init(&ctx->md4);
579 #if defined(WITH_INTERNAL_MD5)
581 winpr_MD5_Init(&ctx->md5);
588 #if defined(WITH_OPENSSL)
589 const EVP_MD* evp = winpr_openssl_get_evp_md(md);
590 return winpr_Digest_Init_Internal(ctx, evp);
592 return winpr_Digest_Init_Internal(ctx, md);
596 BOOL winpr_Digest_Update(WINPR_DIGEST_CTX* ctx,
const void* input,
size_t ilen)
602 #if defined(WITH_INTERNAL_MD4)
604 winpr_MD4_Update(&ctx->md4, input, ilen);
607 #if defined(WITH_INTERNAL_MD5)
609 winpr_MD5_Update(&ctx->md5, input, ilen);
616 #if defined(WITH_OPENSSL)
617 EVP_MD_CTX* mdctx = ctx->mdctx;
619 if (EVP_DigestUpdate(mdctx, input, ilen) != 1)
622 #elif defined(WITH_MBEDTLS)
623 mbedtls_md_context_t* mdctx = ctx->mdctx;
625 if (mbedtls_md_update(mdctx, input, ilen) != 0)
632 BOOL winpr_Digest_Final(WINPR_DIGEST_CTX* ctx,
void* output,
size_t olen)
638 #if defined(WITH_INTERNAL_MD4)
640 if (olen < WINPR_MD4_DIGEST_LENGTH)
642 winpr_MD4_Final(output, &ctx->md4);
645 #if defined(WITH_INTERNAL_MD5)
647 if (olen < WINPR_MD5_DIGEST_LENGTH)
649 winpr_MD5_Final(output, &ctx->md5);
657 #if defined(WITH_OPENSSL)
658 EVP_MD_CTX* mdctx = ctx->mdctx;
660 if (EVP_DigestFinal_ex(mdctx, output, NULL) == 1)
663 #elif defined(WITH_MBEDTLS)
664 mbedtls_md_context_t* mdctx = ctx->mdctx;
666 if (mbedtls_md_finish(mdctx, output) == 0)
673 BOOL winpr_DigestSign_Init(WINPR_DIGEST_CTX* ctx, WINPR_MD_TYPE digest,
void* key)
677 #if defined(WITH_OPENSSL)
678 const EVP_MD* evp = winpr_openssl_get_evp_md(digest);
682 const int rdsi = EVP_DigestSignInit(ctx->mdctx, NULL, evp, NULL, key);
691 BOOL winpr_DigestSign_Update(WINPR_DIGEST_CTX* ctx,
const void* input,
size_t ilen)
695 #if defined(WITH_OPENSSL)
696 EVP_MD_CTX* mdctx = ctx->mdctx;
698 if (EVP_DigestSignUpdate(mdctx, input, ilen) != 1)
706 BOOL winpr_DigestSign_Final(WINPR_DIGEST_CTX* ctx,
void* output,
size_t* piolen)
710 #if defined(WITH_OPENSSL)
711 EVP_MD_CTX* mdctx = ctx->mdctx;
713 return EVP_DigestSignFinal(mdctx, output, piolen) == 1;
719 void winpr_Digest_Free(WINPR_DIGEST_CTX* ctx)
723 #if defined(WITH_OPENSSL)
726 #if (OPENSSL_VERSION_NUMBER < 0x10100000L) || \
727 (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x2070000fL)
728 EVP_MD_CTX_destroy(ctx->mdctx);
730 EVP_MD_CTX_free(ctx->mdctx);
734 #elif defined(WITH_MBEDTLS)
737 mbedtls_md_free(ctx->mdctx);
745 BOOL winpr_Digest_Allow_FIPS(WINPR_MD_TYPE md,
const void* input,
size_t ilen,
void* output,
749 WINPR_DIGEST_CTX* ctx = winpr_Digest_New();
754 if (!winpr_Digest_Init_Allow_FIPS(ctx, md))
757 if (!winpr_Digest_Update(ctx, input, ilen))
760 if (!winpr_Digest_Final(ctx, output, olen))
765 winpr_Digest_Free(ctx);
769 BOOL winpr_Digest(WINPR_MD_TYPE md,
const void* input,
size_t ilen,
void* output,
size_t olen)
772 WINPR_DIGEST_CTX* ctx = winpr_Digest_New();
777 if (!winpr_Digest_Init(ctx, md))
780 if (!winpr_Digest_Update(ctx, input, ilen))
783 if (!winpr_Digest_Final(ctx, output, olen))
788 winpr_Digest_Free(ctx);