2015-10-08 20:58:55 +03:00
|
|
|
/**
|
|
|
|
* WinPR: Windows Portable Runtime
|
|
|
|
*
|
|
|
|
* Copyright 2015 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <winpr/crt.h>
|
|
|
|
|
|
|
|
#include <winpr/crypto.h>
|
|
|
|
|
|
|
|
#ifdef WITH_OPENSSL
|
|
|
|
#include <openssl/md4.h>
|
|
|
|
#include <openssl/md5.h>
|
|
|
|
#include <openssl/sha.h>
|
|
|
|
#include <openssl/evp.h>
|
|
|
|
#include <openssl/hmac.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef WITH_MBEDTLS
|
|
|
|
#include <mbedtls/md4.h>
|
|
|
|
#include <mbedtls/md5.h>
|
|
|
|
#include <mbedtls/sha1.h>
|
|
|
|
#include <mbedtls/md.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
|
|
|
* MD5
|
|
|
|
*/
|
|
|
|
|
2016-02-24 22:32:20 +03:00
|
|
|
BOOL winpr_MD5_Init(WINPR_MD5_CTX* ctx)
|
2015-10-08 20:58:55 +03:00
|
|
|
{
|
|
|
|
#if defined(WITH_OPENSSL)
|
2016-02-24 22:32:20 +03:00
|
|
|
if (MD5_Init((MD5_CTX*) ctx) != 1)
|
2016-02-26 11:28:54 +03:00
|
|
|
return FALSE;
|
2015-10-08 20:58:55 +03:00
|
|
|
#elif defined(WITH_MBEDTLS) && defined(MBEDTLS_MD5_C)
|
|
|
|
mbedtls_md5_init((mbedtls_md5_context*) ctx);
|
|
|
|
mbedtls_md5_starts((mbedtls_md5_context*) ctx);
|
|
|
|
#endif
|
2016-02-24 22:32:20 +03:00
|
|
|
|
2016-02-26 11:28:54 +03:00
|
|
|
return TRUE;
|
2015-10-08 20:58:55 +03:00
|
|
|
}
|
|
|
|
|
2016-02-24 22:32:20 +03:00
|
|
|
BOOL winpr_MD5_Update(WINPR_MD5_CTX* ctx, const BYTE* input, size_t ilen)
|
2015-10-08 20:58:55 +03:00
|
|
|
{
|
|
|
|
#if defined(WITH_OPENSSL)
|
2016-02-24 22:32:20 +03:00
|
|
|
if (MD5_Update((MD5_CTX*) ctx, input, ilen) != 1)
|
2016-02-26 11:28:54 +03:00
|
|
|
return FALSE;
|
2015-10-08 20:58:55 +03:00
|
|
|
#elif defined(WITH_MBEDTLS) && defined(MBEDTLS_MD5_C)
|
|
|
|
mbedtls_md5_update((mbedtls_md5_context*) ctx, input, ilen);
|
|
|
|
#endif
|
2016-02-24 22:32:20 +03:00
|
|
|
|
2016-02-26 11:28:54 +03:00
|
|
|
return TRUE;
|
2015-10-08 20:58:55 +03:00
|
|
|
}
|
|
|
|
|
2016-02-24 23:45:09 +03:00
|
|
|
BOOL winpr_MD5_Final(WINPR_MD5_CTX* ctx, BYTE* output, size_t ilen)
|
2015-10-08 20:58:55 +03:00
|
|
|
{
|
2016-02-26 11:28:54 +03:00
|
|
|
if (ilen < WINPR_MD5_DIGEST_LENGTH)
|
|
|
|
return FALSE;
|
2016-02-24 23:45:09 +03:00
|
|
|
|
2015-10-08 20:58:55 +03:00
|
|
|
#if defined(WITH_OPENSSL)
|
2016-02-24 22:32:20 +03:00
|
|
|
if (MD5_Final(output, (MD5_CTX*) ctx) != 1)
|
2016-02-26 11:28:54 +03:00
|
|
|
return FALSE;
|
2015-10-08 20:58:55 +03:00
|
|
|
#elif defined(WITH_MBEDTLS) && defined(MBEDTLS_MD5_C)
|
|
|
|
mbedtls_md5_finish((mbedtls_md5_context*) ctx, output);
|
|
|
|
mbedtls_md5_free((mbedtls_md5_context*) ctx);
|
|
|
|
#endif
|
2016-02-24 22:32:20 +03:00
|
|
|
|
2016-02-26 11:28:54 +03:00
|
|
|
return TRUE;
|
2015-10-08 20:58:55 +03:00
|
|
|
}
|
|
|
|
|
2016-02-24 23:45:09 +03:00
|
|
|
BOOL winpr_MD5(const BYTE* input, size_t ilen, BYTE* output, size_t olen)
|
2015-10-08 20:58:55 +03:00
|
|
|
{
|
|
|
|
WINPR_MD5_CTX ctx;
|
2016-02-24 22:32:20 +03:00
|
|
|
|
|
|
|
if (!winpr_MD5_Init(&ctx))
|
2016-02-26 11:28:54 +03:00
|
|
|
return FALSE;
|
2016-02-24 22:32:20 +03:00
|
|
|
if (!winpr_MD5_Update(&ctx, input, ilen))
|
2016-02-26 11:28:54 +03:00
|
|
|
return FALSE;
|
2016-02-24 23:45:09 +03:00
|
|
|
return winpr_MD5_Final(&ctx, output, olen);
|
2015-10-08 20:58:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* MD4
|
|
|
|
*/
|
|
|
|
|
2016-02-24 22:32:20 +03:00
|
|
|
BOOL winpr_MD4_Init(WINPR_MD4_CTX* ctx)
|
2015-10-08 20:58:55 +03:00
|
|
|
{
|
|
|
|
#if defined(WITH_OPENSSL)
|
2016-02-24 22:32:20 +03:00
|
|
|
if (MD4_Init((MD4_CTX*) ctx) != 1)
|
2016-02-26 11:28:54 +03:00
|
|
|
return FALSE;
|
2015-10-08 20:58:55 +03:00
|
|
|
#elif defined(WITH_MBEDTLS) && defined(MBEDTLS_MD4_C)
|
|
|
|
mbedtls_md4_init((mbedtls_md4_context*) ctx);
|
|
|
|
mbedtls_md4_starts((mbedtls_md4_context*) ctx);
|
|
|
|
#endif
|
2016-02-26 11:28:54 +03:00
|
|
|
return TRUE;
|
2015-10-08 20:58:55 +03:00
|
|
|
}
|
|
|
|
|
2016-02-24 22:32:20 +03:00
|
|
|
BOOL winpr_MD4_Update(WINPR_MD4_CTX* ctx, const BYTE* input, size_t ilen)
|
2015-10-08 20:58:55 +03:00
|
|
|
{
|
|
|
|
#if defined(WITH_OPENSSL)
|
2016-02-24 22:32:20 +03:00
|
|
|
if (MD4_Update((MD4_CTX*) ctx, input, ilen) != 1)
|
2016-02-26 11:28:54 +03:00
|
|
|
return FALSE;
|
2015-10-08 20:58:55 +03:00
|
|
|
#elif defined(WITH_MBEDTLS) && defined(MBEDTLS_MD4_C)
|
|
|
|
mbedtls_md4_update((mbedtls_md4_context*) ctx, input, ilen);
|
|
|
|
#endif
|
2016-02-24 22:32:20 +03:00
|
|
|
|
2016-02-26 11:28:54 +03:00
|
|
|
return TRUE;
|
2015-10-08 20:58:55 +03:00
|
|
|
}
|
|
|
|
|
2016-02-24 23:45:09 +03:00
|
|
|
BOOL winpr_MD4_Final(WINPR_MD4_CTX* ctx, BYTE* output, size_t olen)
|
2015-10-08 20:58:55 +03:00
|
|
|
{
|
2016-02-26 11:28:54 +03:00
|
|
|
if (olen < WINPR_MD4_DIGEST_LENGTH)
|
|
|
|
return FALSE;
|
2016-02-24 23:45:09 +03:00
|
|
|
|
2015-10-08 20:58:55 +03:00
|
|
|
#if defined(WITH_OPENSSL)
|
2016-02-24 22:32:20 +03:00
|
|
|
if (MD4_Final(output, (MD4_CTX*) ctx) != 1)
|
2016-02-26 11:28:54 +03:00
|
|
|
return FALSE;
|
2015-10-08 20:58:55 +03:00
|
|
|
#elif defined(WITH_MBEDTLS) && defined(MBEDTLS_MD4_C)
|
|
|
|
mbedtls_md4_finish((mbedtls_md4_context*) ctx, output);
|
|
|
|
mbedtls_md4_free((mbedtls_md4_context*) ctx);
|
|
|
|
#endif
|
2016-02-24 22:32:20 +03:00
|
|
|
|
2016-02-26 11:28:54 +03:00
|
|
|
return TRUE;
|
2015-10-08 20:58:55 +03:00
|
|
|
}
|
|
|
|
|
2016-02-24 23:45:09 +03:00
|
|
|
BOOL winpr_MD4(const BYTE* input, size_t ilen, BYTE* output, size_t olen)
|
2015-10-08 20:58:55 +03:00
|
|
|
{
|
|
|
|
WINPR_MD4_CTX ctx;
|
2016-02-24 22:32:20 +03:00
|
|
|
|
|
|
|
if (!winpr_MD4_Init(&ctx))
|
2016-02-26 11:28:54 +03:00
|
|
|
return FALSE;
|
2016-02-24 22:32:20 +03:00
|
|
|
if (!winpr_MD4_Update(&ctx, input, ilen))
|
2016-02-26 11:28:54 +03:00
|
|
|
return FALSE;
|
2016-02-24 23:45:09 +03:00
|
|
|
return winpr_MD4_Final(&ctx, output, olen);
|
2015-10-08 20:58:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SHA1
|
|
|
|
*/
|
|
|
|
|
2016-02-24 22:32:20 +03:00
|
|
|
BOOL winpr_SHA1_Init(WINPR_SHA1_CTX* ctx)
|
2015-10-08 20:58:55 +03:00
|
|
|
{
|
|
|
|
#if defined(WITH_OPENSSL)
|
2016-02-24 22:32:20 +03:00
|
|
|
if (SHA1_Init((SHA_CTX*) ctx) != 1)
|
2016-02-26 11:28:54 +03:00
|
|
|
return FALSE;
|
2015-10-08 20:58:55 +03:00
|
|
|
#elif defined(WITH_MBEDTLS) && defined(MBEDTLS_SHA1_C)
|
|
|
|
mbedtls_sha1_init((mbedtls_sha1_context*) ctx);
|
|
|
|
mbedtls_sha1_starts((mbedtls_sha1_context*) ctx);
|
|
|
|
#endif
|
2016-02-24 22:32:20 +03:00
|
|
|
|
2016-02-26 11:28:54 +03:00
|
|
|
return TRUE;
|
2015-10-08 20:58:55 +03:00
|
|
|
}
|
|
|
|
|
2016-02-24 22:32:20 +03:00
|
|
|
BOOL winpr_SHA1_Update(WINPR_SHA1_CTX* ctx, const BYTE* input, size_t ilen)
|
2015-10-08 20:58:55 +03:00
|
|
|
{
|
|
|
|
#if defined(WITH_OPENSSL)
|
2016-02-24 22:32:20 +03:00
|
|
|
if (SHA1_Update((SHA_CTX*) ctx, input, ilen) != 1)
|
2016-02-26 11:28:54 +03:00
|
|
|
return FALSE;
|
2015-10-08 20:58:55 +03:00
|
|
|
#elif defined(WITH_MBEDTLS) && defined(MBEDTLS_SHA1_C)
|
|
|
|
mbedtls_sha1_update((mbedtls_sha1_context*) ctx, input, ilen);
|
|
|
|
#endif
|
2016-02-24 22:32:20 +03:00
|
|
|
|
2016-02-26 11:28:54 +03:00
|
|
|
return TRUE;
|
2015-10-08 20:58:55 +03:00
|
|
|
}
|
|
|
|
|
2016-02-24 23:45:09 +03:00
|
|
|
BOOL winpr_SHA1_Final(WINPR_SHA1_CTX* ctx, BYTE* output, size_t olen)
|
2015-10-08 20:58:55 +03:00
|
|
|
{
|
2016-02-26 11:28:54 +03:00
|
|
|
if (olen < WINPR_SHA1_DIGEST_LENGTH)
|
|
|
|
return FALSE;
|
2016-02-24 23:45:09 +03:00
|
|
|
|
2015-10-08 20:58:55 +03:00
|
|
|
#if defined(WITH_OPENSSL)
|
2016-02-24 22:32:20 +03:00
|
|
|
if (SHA1_Final(output, (SHA_CTX*) ctx) != 1)
|
2016-02-26 11:28:54 +03:00
|
|
|
return FALSE;
|
2015-10-08 20:58:55 +03:00
|
|
|
#elif defined(WITH_MBEDTLS) && defined(MBEDTLS_SHA1_C)
|
|
|
|
mbedtls_sha1_finish((mbedtls_sha1_context*) ctx, output);
|
|
|
|
mbedtls_sha1_free((mbedtls_sha1_context*) ctx);
|
|
|
|
#endif
|
2016-02-24 22:32:20 +03:00
|
|
|
|
2016-02-26 11:28:54 +03:00
|
|
|
return TRUE;
|
2015-10-08 20:58:55 +03:00
|
|
|
}
|
|
|
|
|
2016-02-24 23:45:09 +03:00
|
|
|
BOOL winpr_SHA1(const BYTE* input, size_t ilen, BYTE* output, size_t olen)
|
2015-10-08 20:58:55 +03:00
|
|
|
{
|
|
|
|
WINPR_SHA1_CTX ctx;
|
2016-02-24 22:32:20 +03:00
|
|
|
|
|
|
|
if (!winpr_SHA1_Init(&ctx))
|
2016-02-26 11:28:54 +03:00
|
|
|
return FALSE;
|
2016-02-24 22:32:20 +03:00
|
|
|
if (!winpr_SHA1_Update(&ctx, input, ilen))
|
2016-02-26 11:28:54 +03:00
|
|
|
return FALSE;
|
2016-02-24 23:45:09 +03:00
|
|
|
return winpr_SHA1_Final(&ctx, output, olen);
|
2015-10-08 20:58:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* HMAC
|
|
|
|
*/
|
|
|
|
|
2015-10-09 22:23:15 +03:00
|
|
|
#ifdef WITH_OPENSSL
|
2015-10-08 20:58:55 +03:00
|
|
|
const EVP_MD* winpr_openssl_get_evp_md(int md)
|
|
|
|
{
|
|
|
|
const EVP_MD* evp = NULL;
|
|
|
|
|
2015-10-13 16:13:52 +03:00
|
|
|
OpenSSL_add_all_digests();
|
|
|
|
|
2015-10-08 20:58:55 +03:00
|
|
|
switch (md)
|
|
|
|
{
|
|
|
|
case WINPR_MD_MD2:
|
2015-10-13 16:43:26 +03:00
|
|
|
evp = EVP_get_digestbyname("md2");
|
2015-10-08 20:58:55 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_MD_MD4:
|
2015-10-13 16:43:26 +03:00
|
|
|
evp = EVP_get_digestbyname("md4");
|
2015-10-08 20:58:55 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_MD_MD5:
|
2015-10-13 16:43:26 +03:00
|
|
|
evp = EVP_get_digestbyname("md5");
|
2015-10-08 20:58:55 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_MD_SHA1:
|
2015-10-13 16:43:26 +03:00
|
|
|
evp = EVP_get_digestbyname("sha1");
|
2015-10-08 20:58:55 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_MD_SHA224:
|
2015-10-13 16:43:26 +03:00
|
|
|
evp = EVP_get_digestbyname("sha224");
|
2015-10-08 20:58:55 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_MD_SHA256:
|
2015-10-13 16:43:26 +03:00
|
|
|
evp = EVP_get_digestbyname("sha256");
|
2015-10-08 20:58:55 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_MD_SHA384:
|
2015-10-13 16:43:26 +03:00
|
|
|
evp = EVP_get_digestbyname("sha384");
|
2015-10-08 20:58:55 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_MD_SHA512:
|
2015-10-13 16:43:26 +03:00
|
|
|
evp = EVP_get_digestbyname("sha512");
|
2015-10-08 20:58:55 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_MD_RIPEMD160:
|
2015-10-13 16:13:52 +03:00
|
|
|
evp = EVP_get_digestbyname("ripemd160");
|
2015-10-08 20:58:55 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return evp;
|
|
|
|
}
|
2015-10-09 22:23:15 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef WITH_MBEDTLS
|
2015-10-08 20:58:55 +03:00
|
|
|
mbedtls_md_type_t winpr_mbedtls_get_md_type(int md)
|
|
|
|
{
|
|
|
|
mbedtls_md_type_t type = MBEDTLS_MD_NONE;
|
|
|
|
|
|
|
|
switch (md)
|
|
|
|
{
|
|
|
|
case WINPR_MD_MD2:
|
|
|
|
type = MBEDTLS_MD_MD2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_MD_MD4:
|
|
|
|
type = MBEDTLS_MD_MD4;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_MD_MD5:
|
|
|
|
type = MBEDTLS_MD_MD5;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_MD_SHA1:
|
|
|
|
type = MBEDTLS_MD_SHA1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_MD_SHA224:
|
|
|
|
type = MBEDTLS_MD_SHA224;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_MD_SHA256:
|
|
|
|
type = MBEDTLS_MD_SHA256;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_MD_SHA384:
|
|
|
|
type = MBEDTLS_MD_SHA384;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_MD_SHA512:
|
|
|
|
type = MBEDTLS_MD_SHA512;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WINPR_MD_RIPEMD160:
|
|
|
|
type = MBEDTLS_MD_RIPEMD160;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-02-24 23:45:09 +03:00
|
|
|
BOOL winpr_HMAC_Init(WINPR_HMAC_CTX* ctx, WINPR_MD_TYPE md, const BYTE* key, size_t keylen)
|
2015-10-08 20:58:55 +03:00
|
|
|
{
|
|
|
|
#if defined(WITH_OPENSSL)
|
|
|
|
const EVP_MD* evp = winpr_openssl_get_evp_md(md);
|
2015-10-13 16:43:26 +03:00
|
|
|
|
|
|
|
if (!evp)
|
2016-02-24 23:45:09 +03:00
|
|
|
return FALSE;
|
2015-10-13 16:43:26 +03:00
|
|
|
|
2015-10-08 20:58:55 +03:00
|
|
|
HMAC_CTX_init((HMAC_CTX*) ctx);
|
2015-10-13 16:43:26 +03:00
|
|
|
|
2015-10-13 16:54:59 +03:00
|
|
|
#if (OPENSSL_VERSION_NUMBER < 0x10000000L)
|
|
|
|
HMAC_Init_ex((HMAC_CTX*) ctx, key, keylen, evp, NULL);
|
|
|
|
#else
|
2015-10-13 16:43:26 +03:00
|
|
|
if (HMAC_Init_ex((HMAC_CTX*) ctx, key, keylen, evp, NULL) != 1)
|
2016-02-24 23:45:09 +03:00
|
|
|
return FALSE;
|
2015-10-13 16:54:59 +03:00
|
|
|
#endif
|
|
|
|
|
2015-10-08 20:58:55 +03:00
|
|
|
#elif defined(WITH_MBEDTLS)
|
|
|
|
const mbedtls_md_info_t* md_info;
|
|
|
|
mbedtls_md_type_t md_type = winpr_mbedtls_get_md_type(md);
|
|
|
|
md_info = mbedtls_md_info_from_type(md_type);
|
2015-10-13 16:43:26 +03:00
|
|
|
|
|
|
|
if (!md_info)
|
2016-02-24 23:45:09 +03:00
|
|
|
return FALSE;
|
2015-10-13 16:43:26 +03:00
|
|
|
|
2015-10-08 20:58:55 +03:00
|
|
|
mbedtls_md_init((mbedtls_md_context_t*) ctx);
|
2015-10-13 16:43:26 +03:00
|
|
|
|
|
|
|
if (mbedtls_md_setup((mbedtls_md_context_t*) ctx, md_info, 1) != 0)
|
2016-02-24 23:45:09 +03:00
|
|
|
return FALSE;
|
2015-10-13 16:43:26 +03:00
|
|
|
|
|
|
|
if (mbedtls_md_hmac_starts((mbedtls_md_context_t*) ctx, key, keylen) != 0)
|
2016-02-24 23:45:09 +03:00
|
|
|
return FALSE;
|
2015-10-08 20:58:55 +03:00
|
|
|
#endif
|
2016-02-24 23:45:09 +03:00
|
|
|
return TRUE;
|
2015-10-08 20:58:55 +03:00
|
|
|
}
|
|
|
|
|
2016-02-24 23:45:09 +03:00
|
|
|
BOOL winpr_HMAC_Update(WINPR_HMAC_CTX* ctx, const BYTE* input, size_t ilen)
|
2015-10-08 20:58:55 +03:00
|
|
|
{
|
|
|
|
#if defined(WITH_OPENSSL)
|
2015-10-13 16:54:59 +03:00
|
|
|
#if (OPENSSL_VERSION_NUMBER < 0x10000000L)
|
|
|
|
HMAC_Update((HMAC_CTX*) ctx, input, ilen);
|
|
|
|
#else
|
2015-10-13 16:43:26 +03:00
|
|
|
if (HMAC_Update((HMAC_CTX*) ctx, input, ilen) != 1)
|
2016-02-24 23:45:09 +03:00
|
|
|
return FALSE;
|
2015-10-13 16:54:59 +03:00
|
|
|
#endif
|
2015-10-08 20:58:55 +03:00
|
|
|
#elif defined(WITH_MBEDTLS)
|
2015-10-13 16:43:26 +03:00
|
|
|
if (mbedtls_md_hmac_update((mbedtls_md_context_t*) ctx, input, ilen) != 0)
|
2016-02-24 23:45:09 +03:00
|
|
|
return FALSE;
|
2015-10-08 20:58:55 +03:00
|
|
|
#endif
|
2016-02-24 23:45:09 +03:00
|
|
|
return TRUE;
|
2015-10-08 20:58:55 +03:00
|
|
|
}
|
|
|
|
|
2016-02-24 23:45:09 +03:00
|
|
|
BOOL winpr_HMAC_Final(WINPR_HMAC_CTX* ctx, BYTE* output, size_t olen)
|
2015-10-08 20:58:55 +03:00
|
|
|
{
|
2016-02-26 11:28:54 +03:00
|
|
|
/* TODO
|
|
|
|
if (olen < ctx->digestLength)
|
|
|
|
return FALSE;
|
|
|
|
*/
|
2016-02-24 23:45:09 +03:00
|
|
|
|
2015-10-08 20:58:55 +03:00
|
|
|
#if defined(WITH_OPENSSL)
|
2015-10-13 16:54:59 +03:00
|
|
|
#if (OPENSSL_VERSION_NUMBER < 0x10000000L)
|
|
|
|
HMAC_Final((HMAC_CTX*) ctx, output, NULL);
|
|
|
|
#else
|
2015-10-13 16:43:26 +03:00
|
|
|
if (HMAC_Final((HMAC_CTX*) ctx, output, NULL) != 1)
|
2016-02-24 23:45:09 +03:00
|
|
|
return FALSE;
|
2015-10-13 16:54:59 +03:00
|
|
|
#endif
|
2015-10-08 20:58:55 +03:00
|
|
|
HMAC_CTX_cleanup((HMAC_CTX*) ctx);
|
|
|
|
#elif defined(WITH_MBEDTLS)
|
2015-10-13 16:43:26 +03:00
|
|
|
if (mbedtls_md_hmac_finish((mbedtls_md_context_t*) ctx, output) != 0)
|
2016-02-24 23:45:09 +03:00
|
|
|
return FALSE;
|
2015-10-13 16:43:26 +03:00
|
|
|
|
2015-10-08 20:58:55 +03:00
|
|
|
mbedtls_md_free((mbedtls_md_context_t*) ctx);
|
|
|
|
#endif
|
2016-02-24 23:45:09 +03:00
|
|
|
return TRUE;
|
2015-10-08 20:58:55 +03:00
|
|
|
}
|
|
|
|
|
2016-02-24 23:45:09 +03:00
|
|
|
BOOL winpr_HMAC(WINPR_MD_TYPE md, const BYTE* key, size_t keylen,
|
2016-02-26 11:28:54 +03:00
|
|
|
const BYTE* input, size_t ilen, BYTE* output, size_t olen)
|
2015-10-08 20:58:55 +03:00
|
|
|
{
|
|
|
|
WINPR_HMAC_CTX ctx;
|
2015-10-13 16:43:26 +03:00
|
|
|
|
2016-02-24 23:45:09 +03:00
|
|
|
if (!winpr_HMAC_Init(&ctx, md, key, keylen))
|
|
|
|
return FALSE;
|
2015-10-13 16:43:26 +03:00
|
|
|
|
2016-02-24 23:45:09 +03:00
|
|
|
if (!winpr_HMAC_Update(&ctx, input, ilen))
|
|
|
|
return FALSE;
|
2015-10-13 16:43:26 +03:00
|
|
|
|
2016-02-24 23:45:09 +03:00
|
|
|
if (!winpr_HMAC_Final(&ctx, output, olen))
|
|
|
|
return FALSE;
|
2015-10-13 16:43:26 +03:00
|
|
|
|
2016-02-24 23:45:09 +03:00
|
|
|
return TRUE;
|
2015-10-08 20:58:55 +03:00
|
|
|
}
|
|
|
|
|
2015-10-09 22:23:15 +03:00
|
|
|
/**
|
|
|
|
* Generic Digest API
|
|
|
|
*/
|
|
|
|
|
2016-02-24 23:45:09 +03:00
|
|
|
BOOL winpr_Digest_Init(WINPR_DIGEST_CTX* ctx, WINPR_MD_TYPE md)
|
2015-10-09 22:23:15 +03:00
|
|
|
{
|
|
|
|
#if defined(WITH_OPENSSL)
|
|
|
|
const EVP_MD* evp = winpr_openssl_get_evp_md(md);
|
2015-10-13 16:43:26 +03:00
|
|
|
|
|
|
|
if (!evp)
|
2016-02-24 23:45:09 +03:00
|
|
|
return FALSE;
|
2015-10-13 16:43:26 +03:00
|
|
|
|
2015-10-09 22:23:15 +03:00
|
|
|
EVP_MD_CTX_init((EVP_MD_CTX*) ctx);
|
2015-10-13 16:43:26 +03:00
|
|
|
|
|
|
|
if (EVP_DigestInit_ex((EVP_MD_CTX*) ctx, evp, NULL) != 1)
|
2016-02-24 23:45:09 +03:00
|
|
|
return FALSE;
|
2015-10-09 22:23:15 +03:00
|
|
|
#elif defined(WITH_MBEDTLS)
|
|
|
|
const mbedtls_md_info_t* md_info;
|
|
|
|
mbedtls_md_type_t md_type = winpr_mbedtls_get_md_type(md);
|
|
|
|
md_info = mbedtls_md_info_from_type(md_type);
|
2015-10-13 16:43:26 +03:00
|
|
|
|
|
|
|
if (!md_info)
|
2016-02-24 23:45:09 +03:00
|
|
|
return FALSE;
|
2015-10-13 16:43:26 +03:00
|
|
|
|
2015-10-09 22:23:15 +03:00
|
|
|
mbedtls_md_init((mbedtls_md_context_t*) ctx);
|
2015-10-13 16:43:26 +03:00
|
|
|
|
|
|
|
if (mbedtls_md_setup((mbedtls_md_context_t*) ctx, md_info, 0) != 0)
|
2016-02-24 23:45:09 +03:00
|
|
|
return FALSE;
|
2015-10-13 16:43:26 +03:00
|
|
|
|
|
|
|
if (mbedtls_md_starts((mbedtls_md_context_t*) ctx) != 0)
|
2016-02-24 23:45:09 +03:00
|
|
|
return FALSE;
|
2015-10-09 22:23:15 +03:00
|
|
|
#endif
|
2016-02-24 23:45:09 +03:00
|
|
|
return TRUE;
|
2015-10-09 22:23:15 +03:00
|
|
|
}
|
|
|
|
|
2016-02-24 23:45:09 +03:00
|
|
|
BOOL winpr_Digest_Update(WINPR_DIGEST_CTX* ctx, const BYTE* input, size_t ilen)
|
2015-10-09 22:23:15 +03:00
|
|
|
{
|
|
|
|
#if defined(WITH_OPENSSL)
|
2015-10-13 16:43:26 +03:00
|
|
|
if (EVP_DigestUpdate((EVP_MD_CTX*) ctx, input, ilen) != 1)
|
2016-02-24 23:45:09 +03:00
|
|
|
return FALSE;
|
2015-10-09 22:23:15 +03:00
|
|
|
#elif defined(WITH_MBEDTLS)
|
2015-10-13 16:43:26 +03:00
|
|
|
if (mbedtls_md_update((mbedtls_md_context_t*) ctx, input, ilen) != 0)
|
2016-02-24 23:45:09 +03:00
|
|
|
return FALSE;
|
2015-10-09 22:23:15 +03:00
|
|
|
#endif
|
2016-02-24 23:45:09 +03:00
|
|
|
return TRUE;
|
2015-10-09 22:23:15 +03:00
|
|
|
}
|
|
|
|
|
2016-02-24 23:45:09 +03:00
|
|
|
BOOL winpr_Digest_Final(WINPR_DIGEST_CTX* ctx, BYTE* output, size_t olen)
|
2015-10-09 22:23:15 +03:00
|
|
|
{
|
2016-02-26 11:28:54 +03:00
|
|
|
// TODO: output length check
|
2015-10-09 22:23:15 +03:00
|
|
|
#if defined(WITH_OPENSSL)
|
2015-10-13 16:43:26 +03:00
|
|
|
if (EVP_DigestFinal_ex((EVP_MD_CTX*) ctx, output, NULL) != 1)
|
2016-02-24 23:45:09 +03:00
|
|
|
return FALSE;
|
2015-10-09 22:23:15 +03:00
|
|
|
#elif defined(WITH_MBEDTLS)
|
2015-10-13 16:43:26 +03:00
|
|
|
if (mbedtls_md_finish((mbedtls_md_context_t*) ctx, output) != 0)
|
2016-02-24 23:45:09 +03:00
|
|
|
return FALSE;
|
2015-10-13 16:43:26 +03:00
|
|
|
|
2015-10-09 22:23:15 +03:00
|
|
|
mbedtls_md_free((mbedtls_md_context_t*) ctx);
|
|
|
|
#endif
|
2016-02-24 23:45:09 +03:00
|
|
|
return TRUE;
|
2015-10-09 22:23:15 +03:00
|
|
|
}
|
|
|
|
|
2016-02-24 23:45:09 +03:00
|
|
|
BOOL winpr_Digest(int md, const BYTE* input, size_t ilen, BYTE* output, size_t olen)
|
2015-10-09 22:23:15 +03:00
|
|
|
{
|
|
|
|
WINPR_DIGEST_CTX ctx;
|
2015-10-13 16:43:26 +03:00
|
|
|
|
2016-02-24 23:45:09 +03:00
|
|
|
if (!winpr_Digest_Init(&ctx, md))
|
|
|
|
return FALSE;
|
2015-10-13 16:43:26 +03:00
|
|
|
|
2016-02-24 23:45:09 +03:00
|
|
|
if (!winpr_Digest_Update(&ctx, input, ilen))
|
|
|
|
return FALSE;
|
2015-10-13 16:43:26 +03:00
|
|
|
|
2016-02-24 23:45:09 +03:00
|
|
|
if (!winpr_Digest_Final(&ctx, output, olen))
|
|
|
|
return FALSE;
|
2015-10-13 16:43:26 +03:00
|
|
|
|
2016-02-24 23:45:09 +03:00
|
|
|
return TRUE;
|
2015-10-09 22:23:15 +03:00
|
|
|
}
|