19#include <winpr/config.h>
22#include <winpr/assert.h>
23#include <winpr/crypto.h>
26#define TAG WINPR_TAG("crypto.cipher")
28#if defined(WITH_INTERNAL_RC4)
33#include <openssl/aes.h>
34#include <openssl/rc4.h>
35#include <openssl/des.h>
36#include <openssl/evp.h>
40#include <mbedtls/md.h>
41#include <mbedtls/aes.h>
42#include <mbedtls/des.h>
43#include <mbedtls/cipher.h>
44#if MBEDTLS_VERSION_MAJOR < 3
45#define mbedtls_cipher_info_get_iv_size(_info) (_info->iv_size)
46#define mbedtls_cipher_info_get_key_bitlen(_info) (_info->key_bitlen)
50struct winpr_cipher_ctx_private_st
52 WINPR_CIPHER_TYPE cipher;
53 WINPR_CRYPTO_OPERATION op;
59 mbedtls_cipher_context_t* mctx;
67struct winpr_rc4_ctx_private_st
69#if defined(WITH_INTERNAL_RC4)
70 winpr_int_RC4_CTX* ictx;
72#if defined(WITH_OPENSSL)
78static WINPR_RC4_CTX* winpr_RC4_New_Internal(
const BYTE* key,
size_t keylen, BOOL override_fips)
80 if (!key || (keylen == 0))
83 WINPR_RC4_CTX* ctx = (WINPR_RC4_CTX*)calloc(1,
sizeof(WINPR_RC4_CTX));
87#if defined(WITH_INTERNAL_RC4)
88 WINPR_UNUSED(override_fips);
89 ctx->ictx = winpr_int_rc4_new(key, keylen);
92#elif defined(WITH_OPENSSL)
93 const EVP_CIPHER* evp =
nullptr;
98 ctx->ctx = EVP_CIPHER_CTX_new();
107 EVP_CIPHER_CTX_reset(ctx->ctx);
108 if (EVP_EncryptInit_ex(ctx->ctx, evp,
nullptr,
nullptr,
nullptr) != 1)
112#if !(OPENSSL_VERSION_NUMBER < 0x10001000L)
114 if (override_fips == TRUE)
115 EVP_CIPHER_CTX_set_flags(ctx->ctx, EVP_CIPH_FLAG_NON_FIPS_ALLOW);
118 EVP_CIPHER_CTX_set_key_length(ctx->ctx, (
int)keylen);
119 if (EVP_EncryptInit_ex(ctx->ctx,
nullptr,
nullptr, key,
nullptr) != 1)
125 WINPR_PRAGMA_DIAG_PUSH
126 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
129 WINPR_PRAGMA_DIAG_POP
133WINPR_RC4_CTX* winpr_RC4_New_Allow_FIPS(
const void* key,
size_t keylen)
135 return winpr_RC4_New_Internal(key, keylen, TRUE);
138WINPR_RC4_CTX* winpr_RC4_New(
const void* key,
size_t keylen)
140 return winpr_RC4_New_Internal(key, keylen, FALSE);
143BOOL winpr_RC4_Update(WINPR_RC4_CTX* ctx,
size_t length,
const void* input,
void* output)
147#if defined(WITH_INTERNAL_RC4)
148 return winpr_int_rc4_update(ctx->ictx, length, input, output);
149#elif defined(WITH_OPENSSL)
150 WINPR_ASSERT(ctx->ctx);
151 int outputLength = 0;
152 if (length > INT_MAX)
156 return EVP_CipherUpdate(ctx->ctx, output, &outputLength, input, (
int)length) == 1;
161void winpr_RC4_Free(WINPR_RC4_CTX* ctx)
166#if defined(WITH_INTERNAL_RC4)
167 winpr_int_rc4_free(ctx->ictx);
168#elif defined(WITH_OPENSSL)
169 EVP_CIPHER_CTX_free(ctx->ctx);
179extern const EVP_MD* winpr_openssl_get_evp_md(WINPR_MD_TYPE md);
183extern mbedtls_md_type_t winpr_mbedtls_get_md_type(
int md);
188 WINPR_CIPHER_TYPE md;
191static const struct cipher_map s_cipher_map[] = {
192 { WINPR_CIPHER_NONE,
"none" },
193 { WINPR_CIPHER_NULL,
"null" },
194 { WINPR_CIPHER_AES_128_ECB,
"aes-128-ecb" },
195 { WINPR_CIPHER_AES_192_ECB,
"aes-192-ecb" },
196 { WINPR_CIPHER_AES_256_ECB,
"aes-256-ecb" },
197 { WINPR_CIPHER_AES_128_CBC,
"aes-128-cbc" },
198 { WINPR_CIPHER_AES_192_CBC,
"aes-192-cbc" },
199 { WINPR_CIPHER_AES_256_CBC,
"aes-256-cbc" },
200 { WINPR_CIPHER_AES_128_CFB128,
"aes-128-cfb128" },
201 { WINPR_CIPHER_AES_192_CFB128,
"aes-192-cfb128" },
202 { WINPR_CIPHER_AES_256_CFB128,
"aes-256-cfb128" },
203 { WINPR_CIPHER_AES_128_CTR,
"aes-128-ctr" },
204 { WINPR_CIPHER_AES_192_CTR,
"aes-192-ctr" },
205 { WINPR_CIPHER_AES_256_CTR,
"aes-256-ctr" },
206 { WINPR_CIPHER_AES_128_GCM,
"aes-128-gcm" },
207 { WINPR_CIPHER_AES_192_GCM,
"aes-192-gcm" },
208 { WINPR_CIPHER_AES_256_GCM,
"aes-256-gcm" },
209 { WINPR_CIPHER_CAMELLIA_128_ECB,
"camellia-128-ecb" },
210 { WINPR_CIPHER_CAMELLIA_192_ECB,
"camellia-192-ecb" },
211 { WINPR_CIPHER_CAMELLIA_256_ECB,
"camellia-256-ecb" },
212 { WINPR_CIPHER_CAMELLIA_128_CBC,
"camellia-128-cbc" },
213 { WINPR_CIPHER_CAMELLIA_192_CBC,
"camellia-192-cbc" },
214 { WINPR_CIPHER_CAMELLIA_256_CBC,
"camellia-256-cbc" },
215 { WINPR_CIPHER_CAMELLIA_128_CFB128,
"camellia-128-cfb128" },
216 { WINPR_CIPHER_CAMELLIA_192_CFB128,
"camellia-192-cfb128" },
217 { WINPR_CIPHER_CAMELLIA_256_CFB128,
"camellia-256-cfb128" },
218 { WINPR_CIPHER_CAMELLIA_128_CTR,
"camellia-128-ctr" },
219 { WINPR_CIPHER_CAMELLIA_192_CTR,
"camellia-192-ctr" },
220 { WINPR_CIPHER_CAMELLIA_256_CTR,
"camellia-256-ctr" },
221 { WINPR_CIPHER_CAMELLIA_128_GCM,
"camellia-128-gcm" },
222 { WINPR_CIPHER_CAMELLIA_192_GCM,
"camellia-192-gcm" },
223 { WINPR_CIPHER_CAMELLIA_256_GCM,
"camellia-256-gcm" },
224 { WINPR_CIPHER_DES_ECB,
"des-ecb" },
225 { WINPR_CIPHER_DES_CBC,
"des-cbc" },
226 { WINPR_CIPHER_DES_EDE_ECB,
"des-ede-ecb" },
227 { WINPR_CIPHER_DES_EDE_CBC,
"des-ede-cbc" },
228 { WINPR_CIPHER_DES_EDE3_ECB,
"des-ede3-ecb" },
229 { WINPR_CIPHER_DES_EDE3_CBC,
"des-ede3-cbc" },
230 { WINPR_CIPHER_BLOWFISH_ECB,
"blowfish-ecb" },
231 { WINPR_CIPHER_BLOWFISH_CBC,
"blowfish-cbc" },
232 { WINPR_CIPHER_BLOWFISH_CFB64,
"blowfish-cfb64" },
233 { WINPR_CIPHER_BLOWFISH_CTR,
"blowfish-ctr" },
234 { WINPR_CIPHER_ARC4_128,
"rc4" },
235 { WINPR_CIPHER_AES_128_CCM,
"aes-128-ccm" },
236 { WINPR_CIPHER_AES_192_CCM,
"aes-192-ccm" },
237 { WINPR_CIPHER_AES_256_CCM,
"aes-256-ccm" },
238 { WINPR_CIPHER_CAMELLIA_128_CCM,
"camellia-128-ccm" },
239 { WINPR_CIPHER_CAMELLIA_192_CCM,
"camellia-192-ccm" },
240 { WINPR_CIPHER_CAMELLIA_256_CCM,
"camellia-256-ccm" },
243static int cipher_compare(
const void* a,
const void* b)
245 const WINPR_CIPHER_TYPE* cipher = a;
246 const struct cipher_map* map = b;
247 if (*cipher == map->md)
249 return *cipher > map->md ? 1 : -1;
252const char* winpr_cipher_type_to_string(WINPR_CIPHER_TYPE md)
254 WINPR_CIPHER_TYPE lc = md;
255 const struct cipher_map* ret = bsearch(&lc, s_cipher_map, ARRAYSIZE(s_cipher_map),
256 sizeof(
struct cipher_map), cipher_compare);
262static int cipher_string_compare(
const void* a,
const void* b)
264 const char* cipher = a;
265 const struct cipher_map* map = b;
266 return strcmp(cipher, map->name);
269WINPR_CIPHER_TYPE winpr_cipher_type_from_string(
const char* name)
271 const struct cipher_map* ret = bsearch(name, s_cipher_map, ARRAYSIZE(s_cipher_map),
272 sizeof(
struct cipher_map), cipher_string_compare);
274 return WINPR_CIPHER_NONE;
278#if defined(WITH_OPENSSL)
279static const EVP_CIPHER* winpr_openssl_get_evp_cipher(WINPR_CIPHER_TYPE cipher)
281 const EVP_CIPHER* evp =
nullptr;
285 case WINPR_CIPHER_NULL:
286 evp = EVP_enc_null();
289 case WINPR_CIPHER_AES_128_ECB:
290 evp = EVP_get_cipherbyname(
"aes-128-ecb");
293 case WINPR_CIPHER_AES_192_ECB:
294 evp = EVP_get_cipherbyname(
"aes-192-ecb");
297 case WINPR_CIPHER_AES_256_ECB:
298 evp = EVP_get_cipherbyname(
"aes-256-ecb");
301 case WINPR_CIPHER_AES_128_CBC:
302 evp = EVP_get_cipherbyname(
"aes-128-cbc");
305 case WINPR_CIPHER_AES_192_CBC:
306 evp = EVP_get_cipherbyname(
"aes-192-cbc");
309 case WINPR_CIPHER_AES_256_CBC:
310 evp = EVP_get_cipherbyname(
"aes-256-cbc");
313 case WINPR_CIPHER_AES_128_CFB128:
314 evp = EVP_get_cipherbyname(
"aes-128-cfb128");
317 case WINPR_CIPHER_AES_192_CFB128:
318 evp = EVP_get_cipherbyname(
"aes-192-cfb128");
321 case WINPR_CIPHER_AES_256_CFB128:
322 evp = EVP_get_cipherbyname(
"aes-256-cfb128");
325 case WINPR_CIPHER_AES_128_CTR:
326 evp = EVP_get_cipherbyname(
"aes-128-ctr");
329 case WINPR_CIPHER_AES_192_CTR:
330 evp = EVP_get_cipherbyname(
"aes-192-ctr");
333 case WINPR_CIPHER_AES_256_CTR:
334 evp = EVP_get_cipherbyname(
"aes-256-ctr");
337 case WINPR_CIPHER_AES_128_GCM:
338 evp = EVP_get_cipherbyname(
"aes-128-gcm");
341 case WINPR_CIPHER_AES_192_GCM:
342 evp = EVP_get_cipherbyname(
"aes-192-gcm");
345 case WINPR_CIPHER_AES_256_GCM:
346 evp = EVP_get_cipherbyname(
"aes-256-gcm");
349 case WINPR_CIPHER_AES_128_CCM:
350 evp = EVP_get_cipherbyname(
"aes-128-ccm");
353 case WINPR_CIPHER_AES_192_CCM:
354 evp = EVP_get_cipherbyname(
"aes-192-ccm");
357 case WINPR_CIPHER_AES_256_CCM:
358 evp = EVP_get_cipherbyname(
"aes-256-ccm");
361 case WINPR_CIPHER_CAMELLIA_128_ECB:
362 evp = EVP_get_cipherbyname(
"camellia-128-ecb");
365 case WINPR_CIPHER_CAMELLIA_192_ECB:
366 evp = EVP_get_cipherbyname(
"camellia-192-ecb");
369 case WINPR_CIPHER_CAMELLIA_256_ECB:
370 evp = EVP_get_cipherbyname(
"camellia-256-ecb");
373 case WINPR_CIPHER_CAMELLIA_128_CBC:
374 evp = EVP_get_cipherbyname(
"camellia-128-cbc");
377 case WINPR_CIPHER_CAMELLIA_192_CBC:
378 evp = EVP_get_cipherbyname(
"camellia-192-cbc");
381 case WINPR_CIPHER_CAMELLIA_256_CBC:
382 evp = EVP_get_cipherbyname(
"camellia-256-cbc");
385 case WINPR_CIPHER_CAMELLIA_128_CFB128:
386 evp = EVP_get_cipherbyname(
"camellia-128-cfb128");
389 case WINPR_CIPHER_CAMELLIA_192_CFB128:
390 evp = EVP_get_cipherbyname(
"camellia-192-cfb128");
393 case WINPR_CIPHER_CAMELLIA_256_CFB128:
394 evp = EVP_get_cipherbyname(
"camellia-256-cfb128");
397 case WINPR_CIPHER_CAMELLIA_128_CTR:
398 evp = EVP_get_cipherbyname(
"camellia-128-ctr");
401 case WINPR_CIPHER_CAMELLIA_192_CTR:
402 evp = EVP_get_cipherbyname(
"camellia-192-ctr");
405 case WINPR_CIPHER_CAMELLIA_256_CTR:
406 evp = EVP_get_cipherbyname(
"camellia-256-ctr");
409 case WINPR_CIPHER_CAMELLIA_128_GCM:
410 evp = EVP_get_cipherbyname(
"camellia-128-gcm");
413 case WINPR_CIPHER_CAMELLIA_192_GCM:
414 evp = EVP_get_cipherbyname(
"camellia-192-gcm");
417 case WINPR_CIPHER_CAMELLIA_256_GCM:
418 evp = EVP_get_cipherbyname(
"camellia-256-gcm");
421 case WINPR_CIPHER_CAMELLIA_128_CCM:
422 evp = EVP_get_cipherbyname(
"camellia-128-ccm");
425 case WINPR_CIPHER_CAMELLIA_192_CCM:
426 evp = EVP_get_cipherbyname(
"camellia-192-ccm");
429 case WINPR_CIPHER_CAMELLIA_256_CCM:
430 evp = EVP_get_cipherbyname(
"camellia-256-ccm");
433 case WINPR_CIPHER_DES_ECB:
434 evp = EVP_get_cipherbyname(
"des-ecb");
437 case WINPR_CIPHER_DES_CBC:
438 evp = EVP_get_cipherbyname(
"des-cbc");
441 case WINPR_CIPHER_DES_EDE_ECB:
442 evp = EVP_get_cipherbyname(
"des-ede-ecb");
445 case WINPR_CIPHER_DES_EDE_CBC:
446 evp = EVP_get_cipherbyname(
"des-ede-cbc");
449 case WINPR_CIPHER_DES_EDE3_ECB:
450 evp = EVP_get_cipherbyname(
"des-ede3-ecb");
453 case WINPR_CIPHER_DES_EDE3_CBC:
454 evp = EVP_get_cipherbyname(
"des-ede3-cbc");
457 case WINPR_CIPHER_ARC4_128:
458 evp = EVP_get_cipherbyname(
"rc4");
461 case WINPR_CIPHER_BLOWFISH_ECB:
462 evp = EVP_get_cipherbyname(
"blowfish-ecb");
465 case WINPR_CIPHER_BLOWFISH_CBC:
466 evp = EVP_get_cipherbyname(
"blowfish-cbc");
469 case WINPR_CIPHER_BLOWFISH_CFB64:
470 evp = EVP_get_cipherbyname(
"blowfish-cfb64");
473 case WINPR_CIPHER_BLOWFISH_CTR:
474 evp = EVP_get_cipherbyname(
"blowfish-ctr");
483#elif defined(WITH_MBEDTLS)
484mbedtls_cipher_type_t winpr_mbedtls_get_cipher_type(
int cipher)
486 mbedtls_cipher_type_t type = MBEDTLS_CIPHER_NONE;
490 case WINPR_CIPHER_NONE:
491 type = MBEDTLS_CIPHER_NONE;
494 case WINPR_CIPHER_NULL:
495 type = MBEDTLS_CIPHER_NULL;
498 case WINPR_CIPHER_AES_128_ECB:
499 type = MBEDTLS_CIPHER_AES_128_ECB;
502 case WINPR_CIPHER_AES_192_ECB:
503 type = MBEDTLS_CIPHER_AES_192_ECB;
506 case WINPR_CIPHER_AES_256_ECB:
507 type = MBEDTLS_CIPHER_AES_256_ECB;
510 case WINPR_CIPHER_AES_128_CBC:
511 type = MBEDTLS_CIPHER_AES_128_CBC;
514 case WINPR_CIPHER_AES_192_CBC:
515 type = MBEDTLS_CIPHER_AES_192_CBC;
518 case WINPR_CIPHER_AES_256_CBC:
519 type = MBEDTLS_CIPHER_AES_256_CBC;
522 case WINPR_CIPHER_AES_128_CFB128:
523 type = MBEDTLS_CIPHER_AES_128_CFB128;
526 case WINPR_CIPHER_AES_192_CFB128:
527 type = MBEDTLS_CIPHER_AES_192_CFB128;
530 case WINPR_CIPHER_AES_256_CFB128:
531 type = MBEDTLS_CIPHER_AES_256_CFB128;
534 case WINPR_CIPHER_AES_128_CTR:
535 type = MBEDTLS_CIPHER_AES_128_CTR;
538 case WINPR_CIPHER_AES_192_CTR:
539 type = MBEDTLS_CIPHER_AES_192_CTR;
542 case WINPR_CIPHER_AES_256_CTR:
543 type = MBEDTLS_CIPHER_AES_256_CTR;
546 case WINPR_CIPHER_AES_128_GCM:
547 type = MBEDTLS_CIPHER_AES_128_GCM;
550 case WINPR_CIPHER_AES_192_GCM:
551 type = MBEDTLS_CIPHER_AES_192_GCM;
554 case WINPR_CIPHER_AES_256_GCM:
555 type = MBEDTLS_CIPHER_AES_256_GCM;
558 case WINPR_CIPHER_AES_128_CCM:
559 type = MBEDTLS_CIPHER_AES_128_CCM;
562 case WINPR_CIPHER_AES_192_CCM:
563 type = MBEDTLS_CIPHER_AES_192_CCM;
566 case WINPR_CIPHER_AES_256_CCM:
567 type = MBEDTLS_CIPHER_AES_256_CCM;
575#if !defined(WITHOUT_FREERDP_3x_DEPRECATED)
576WINPR_CIPHER_CTX* winpr_Cipher_New(WINPR_CIPHER_TYPE cipher, WINPR_CRYPTO_OPERATION op,
577 const void* key,
const void* iv)
579 return winpr_Cipher_NewEx(cipher, op, key, 0, iv, 0);
583WINPR_API WINPR_CIPHER_CTX* winpr_Cipher_NewEx(WINPR_CIPHER_TYPE cipher, WINPR_CRYPTO_OPERATION op,
584 const void* key, WINPR_ATTR_UNUSED
size_t keylen,
585 const void* iv, WINPR_ATTR_UNUSED
size_t ivlen)
587 if (cipher == WINPR_CIPHER_ARC4_128)
590 "WINPR_CIPHER_ARC4_128 (RC4) cipher not supported, use winpr_RC4_new instead");
594 WINPR_CIPHER_CTX* ctx = calloc(1,
sizeof(WINPR_CIPHER_CTX));
598 ctx->cipher = cipher;
601#if defined(WITH_OPENSSL)
602 const EVP_CIPHER* evp = winpr_openssl_get_evp_cipher(cipher);
606 ctx->ectx = EVP_CIPHER_CTX_new();
611 const int operation = (op == WINPR_ENCRYPT) ? 1 : 0;
612 if (EVP_CipherInit_ex(ctx->ectx, evp,
nullptr, key, iv, operation) != 1)
616 EVP_CIPHER_CTX_set_padding(ctx->ectx, 0);
618#elif defined(WITH_MBEDTLS)
619 mbedtls_cipher_type_t cipher_type = winpr_mbedtls_get_cipher_type(cipher);
620 const mbedtls_cipher_info_t* cipher_info = mbedtls_cipher_info_from_type(cipher_type);
625 ctx->mctx = calloc(1,
sizeof(mbedtls_cipher_context_t));
629 const mbedtls_operation_t operation = (op == WINPR_ENCRYPT) ? MBEDTLS_ENCRYPT : MBEDTLS_DECRYPT;
630 mbedtls_cipher_init(ctx->mctx);
632 if (mbedtls_cipher_setup(ctx->mctx, cipher_info) != 0)
635 const int key_bitlen = mbedtls_cipher_get_key_bitlen(ctx->mctx);
637 if (mbedtls_cipher_setkey(ctx->mctx, key, key_bitlen, operation) != 0)
640 if (mbedtls_cipher_set_padding_mode(ctx->mctx, MBEDTLS_PADDING_NONE) != 0)
647 winpr_Cipher_Free(ctx);
651BOOL winpr_Cipher_SetPadding(WINPR_CIPHER_CTX* ctx, BOOL enabled)
655#if defined(WITH_OPENSSL)
658 EVP_CIPHER_CTX_set_padding(ctx->ectx, enabled);
659#elif defined(WITH_MBEDTLS)
660 mbedtls_cipher_padding_t option = enabled ? MBEDTLS_PADDING_PKCS7 : MBEDTLS_PADDING_NONE;
661 if (mbedtls_cipher_set_padding_mode((mbedtls_cipher_context_t*)ctx, option) != 0)
669BOOL winpr_Cipher_Update(WINPR_CIPHER_CTX* ctx,
const void* input,
size_t ilen,
void* output,
675#if defined(WITH_OPENSSL)
676 int outl = (int)*olen;
680 WLog_ERR(TAG,
"input length %" PRIuz
" > %d, abort", ilen, INT_MAX);
684 WINPR_ASSERT(ctx->ectx);
685 if (EVP_CipherUpdate(ctx->ectx, output, &outl, input, (
int)ilen) == 1)
687 *olen = (size_t)outl;
691#elif defined(WITH_MBEDTLS)
692 WINPR_ASSERT(ctx->mctx);
693 if (mbedtls_cipher_update(ctx->mctx, input, ilen, output, olen) == 0)
698 WLog_ERR(TAG,
"Failed to update the data");
702BOOL winpr_Cipher_Final(WINPR_CIPHER_CTX* ctx,
void* output,
size_t* olen)
706#if defined(WITH_OPENSSL)
707 int outl = (int)*olen;
709 WINPR_ASSERT(ctx->ectx);
710 if (EVP_CipherFinal_ex(ctx->ectx, output, &outl) == 1)
712 *olen = (size_t)outl;
716#elif defined(WITH_MBEDTLS)
718 WINPR_ASSERT(ctx->mctx);
719 if (mbedtls_cipher_finish(ctx->mctx, output, olen) == 0)
727void winpr_Cipher_Free(WINPR_CIPHER_CTX* ctx)
732#if defined(WITH_OPENSSL)
734 EVP_CIPHER_CTX_free(ctx->ectx);
735#elif defined(WITH_MBEDTLS)
738 mbedtls_cipher_free(ctx->mctx);
750int winpr_Cipher_BytesToKey(
int cipher, WINPR_MD_TYPE md,
const void* salt,
const void* data,
751 size_t datal,
size_t count,
void* key,
void* iv)
757#if defined(WITH_OPENSSL)
758 const EVP_MD* evp_md =
nullptr;
759 const EVP_CIPHER* evp_cipher =
nullptr;
760 evp_md = winpr_openssl_get_evp_md(md);
761 evp_cipher = winpr_openssl_get_evp_cipher(WINPR_ASSERTING_INT_CAST(WINPR_CIPHER_TYPE, cipher));
762 WINPR_ASSERT(datal <= INT_MAX);
763 WINPR_ASSERT(count <= INT_MAX);
764 return EVP_BytesToKey(evp_cipher, evp_md, salt, data, (
int)datal, (
int)count, key, iv);
765#elif defined(WITH_MBEDTLS)
768 int niv, nkey, addmd = 0;
769 unsigned int mds = 0;
770 mbedtls_md_context_t ctx;
771 const mbedtls_md_info_t* md_info;
772 mbedtls_cipher_type_t cipher_type;
773 const mbedtls_cipher_info_t* cipher_info;
774 mbedtls_md_type_t md_type = winpr_mbedtls_get_md_type(md);
775 md_info = mbedtls_md_info_from_type(md_type);
776 cipher_type = winpr_mbedtls_get_cipher_type(cipher);
777 cipher_info = mbedtls_cipher_info_from_type(cipher_type);
778 nkey = mbedtls_cipher_info_get_key_bitlen(cipher_info) / 8;
779 niv = mbedtls_cipher_info_get_iv_size(cipher_info);
781 if ((nkey > 64) || (niv > 64))
787 mbedtls_md_init(&ctx);
789 if (mbedtls_md_setup(&ctx, md_info, 0) != 0)
794 if (mbedtls_md_starts(&ctx) != 0)
799 if (mbedtls_md_update(&ctx, md_buf, mds) != 0)
803 if (mbedtls_md_update(&ctx, data, datal) != 0)
808 if (mbedtls_md_update(&ctx, salt, 8) != 0)
812 if (mbedtls_md_finish(&ctx, md_buf) != 0)
815 mds = mbedtls_md_get_size(md_info);
817 for (
unsigned int i = 1; i < (
unsigned int)count; i++)
819 if (mbedtls_md_starts(&ctx) != 0)
822 if (mbedtls_md_update(&ctx, md_buf, mds) != 0)
825 if (mbedtls_md_finish(&ctx, md_buf) != 0)
842 *(BYTE*)(key++) = md_buf[i];
849 if (niv && (i != mds))
860 *(BYTE*)(iv++) = md_buf[i];
867 if ((nkey == 0) && (niv == 0))
871 rv = mbedtls_cipher_info_get_key_bitlen(cipher_info) / 8;
873 mbedtls_md_free(&ctx);
874 SecureZeroMemory(md_buf, 64);