2009-03-30 11:24:55 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2009 Henri Verbeet for CodeWeavers
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
2016-01-13 10:43:35 +01:00
|
|
|
#include "wine/port.h"
|
2009-03-30 11:24:55 +02:00
|
|
|
|
2014-02-03 19:24:21 +01:00
|
|
|
#include <stdarg.h>
|
2016-01-06 14:19:16 +01:00
|
|
|
#ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
|
|
|
|
#include <CommonCrypto/CommonDigest.h>
|
2016-03-11 04:33:40 +01:00
|
|
|
#include <CommonCrypto/CommonHMAC.h>
|
2016-01-13 10:43:35 +01:00
|
|
|
#elif defined(SONAME_LIBGNUTLS)
|
|
|
|
#include <gnutls/gnutls.h>
|
|
|
|
#include <gnutls/crypto.h>
|
2016-01-06 14:19:16 +01:00
|
|
|
#endif
|
2014-02-03 19:24:21 +01:00
|
|
|
|
|
|
|
#include "ntstatus.h"
|
|
|
|
#define WIN32_NO_STATUS
|
|
|
|
#include "windef.h"
|
2009-03-30 11:24:55 +02:00
|
|
|
#include "winbase.h"
|
2014-02-10 23:12:15 +01:00
|
|
|
#include "ntsecapi.h"
|
2011-05-30 12:45:30 +02:00
|
|
|
#include "bcrypt.h"
|
2016-01-06 14:19:15 +01:00
|
|
|
|
2017-03-07 23:14:36 +01:00
|
|
|
#include "bcrypt_internal.h"
|
|
|
|
|
2014-02-03 19:24:21 +01:00
|
|
|
#include "wine/debug.h"
|
2016-01-13 10:43:35 +01:00
|
|
|
#include "wine/library.h"
|
2016-01-06 14:19:15 +01:00
|
|
|
#include "wine/unicode.h"
|
2009-03-30 11:24:55 +02:00
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(bcrypt);
|
|
|
|
|
2016-01-13 10:43:35 +01:00
|
|
|
static HINSTANCE instance;
|
|
|
|
|
2016-01-18 13:52:40 +01:00
|
|
|
#if defined(HAVE_GNUTLS_HASH) && !defined(HAVE_COMMONCRYPTO_COMMONDIGEST_H)
|
2016-01-13 10:43:35 +01:00
|
|
|
WINE_DECLARE_DEBUG_CHANNEL(winediag);
|
|
|
|
|
|
|
|
static void *libgnutls_handle;
|
|
|
|
#define MAKE_FUNCPTR(f) static typeof(f) * p##f
|
|
|
|
MAKE_FUNCPTR(gnutls_global_deinit);
|
|
|
|
MAKE_FUNCPTR(gnutls_global_init);
|
|
|
|
MAKE_FUNCPTR(gnutls_global_set_log_function);
|
|
|
|
MAKE_FUNCPTR(gnutls_global_set_log_level);
|
|
|
|
MAKE_FUNCPTR(gnutls_perror);
|
|
|
|
#undef MAKE_FUNCPTR
|
|
|
|
|
|
|
|
static void gnutls_log( int level, const char *msg )
|
|
|
|
{
|
|
|
|
TRACE( "<%d> %s", level, msg );
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL gnutls_initialize(void)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!(libgnutls_handle = wine_dlopen( SONAME_LIBGNUTLS, RTLD_NOW, NULL, 0 )))
|
|
|
|
{
|
|
|
|
ERR_(winediag)( "failed to load libgnutls, no support for crypto hashes\n" );
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define LOAD_FUNCPTR(f) \
|
|
|
|
if (!(p##f = wine_dlsym( libgnutls_handle, #f, NULL, 0 ))) \
|
|
|
|
{ \
|
|
|
|
ERR( "failed to load %s\n", #f ); \
|
|
|
|
goto fail; \
|
|
|
|
}
|
|
|
|
|
|
|
|
LOAD_FUNCPTR(gnutls_global_deinit)
|
|
|
|
LOAD_FUNCPTR(gnutls_global_init)
|
|
|
|
LOAD_FUNCPTR(gnutls_global_set_log_function)
|
|
|
|
LOAD_FUNCPTR(gnutls_global_set_log_level)
|
|
|
|
LOAD_FUNCPTR(gnutls_perror)
|
|
|
|
#undef LOAD_FUNCPTR
|
|
|
|
|
|
|
|
if ((ret = pgnutls_global_init()) != GNUTLS_E_SUCCESS)
|
|
|
|
{
|
|
|
|
pgnutls_perror( ret );
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (TRACE_ON( bcrypt ))
|
|
|
|
{
|
|
|
|
pgnutls_global_set_log_level( 4 );
|
|
|
|
pgnutls_global_set_log_function( gnutls_log );
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
wine_dlclose( libgnutls_handle, NULL, 0 );
|
|
|
|
libgnutls_handle = NULL;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void gnutls_uninitialize(void)
|
|
|
|
{
|
|
|
|
pgnutls_global_deinit();
|
|
|
|
wine_dlclose( libgnutls_handle, NULL, 0 );
|
|
|
|
libgnutls_handle = NULL;
|
|
|
|
}
|
2016-01-18 13:52:40 +01:00
|
|
|
#endif /* HAVE_GNUTLS_HASH && !HAVE_COMMONCRYPTO_COMMONDIGEST_H */
|
2016-01-13 10:43:35 +01:00
|
|
|
|
2011-05-30 12:45:30 +02:00
|
|
|
NTSTATUS WINAPI BCryptEnumAlgorithms(ULONG dwAlgOperations, ULONG *pAlgCount,
|
|
|
|
BCRYPT_ALGORITHM_IDENTIFIER **ppAlgList, ULONG dwFlags)
|
|
|
|
{
|
|
|
|
FIXME("%08x, %p, %p, %08x - stub\n", dwAlgOperations, pAlgCount, ppAlgList, dwFlags);
|
|
|
|
|
|
|
|
*ppAlgList=NULL;
|
|
|
|
*pAlgCount=0;
|
|
|
|
|
2014-02-03 19:24:21 +01:00
|
|
|
return STATUS_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2016-01-06 14:19:15 +01:00
|
|
|
#define MAGIC_ALG (('A' << 24) | ('L' << 16) | ('G' << 8) | '0')
|
2016-01-06 14:19:16 +01:00
|
|
|
#define MAGIC_HASH (('H' << 24) | ('A' << 16) | ('S' << 8) | 'H')
|
2016-01-06 14:19:15 +01:00
|
|
|
struct object
|
2014-04-30 04:24:58 +02:00
|
|
|
{
|
2016-01-06 14:19:15 +01:00
|
|
|
ULONG magic;
|
|
|
|
};
|
2014-04-30 04:24:58 +02:00
|
|
|
|
2016-01-06 14:19:15 +01:00
|
|
|
enum alg_id
|
|
|
|
{
|
2016-03-08 04:45:27 +01:00
|
|
|
ALG_ID_MD5,
|
2016-04-04 11:07:05 +02:00
|
|
|
ALG_ID_RNG,
|
2016-01-29 14:25:23 +01:00
|
|
|
ALG_ID_SHA1,
|
|
|
|
ALG_ID_SHA256,
|
|
|
|
ALG_ID_SHA384,
|
|
|
|
ALG_ID_SHA512
|
|
|
|
};
|
|
|
|
|
2017-03-07 23:13:51 +01:00
|
|
|
#define MAX_HASH_OUTPUT_BYTES 64
|
|
|
|
#define MAX_HASH_BLOCK_BITS 1024
|
|
|
|
|
2016-01-29 14:25:23 +01:00
|
|
|
static const struct {
|
2017-03-10 15:33:42 +01:00
|
|
|
ULONG object_length;
|
2016-01-29 14:25:23 +01:00
|
|
|
ULONG hash_length;
|
2017-03-07 23:13:51 +01:00
|
|
|
ULONG block_bits;
|
2016-01-29 14:25:30 +01:00
|
|
|
const WCHAR *alg_name;
|
2016-01-29 14:25:23 +01:00
|
|
|
} alg_props[] = {
|
2017-03-10 15:33:42 +01:00
|
|
|
/* ALG_ID_MD5 */ { 274, 16, 512, BCRYPT_MD5_ALGORITHM },
|
|
|
|
/* ALG_ID_RNG */ { 0, 0, 0, BCRYPT_RNG_ALGORITHM },
|
|
|
|
/* ALG_ID_SHA1 */ { 278, 20, 512, BCRYPT_SHA1_ALGORITHM },
|
|
|
|
/* ALG_ID_SHA256 */ { 286, 32, 512, BCRYPT_SHA256_ALGORITHM },
|
|
|
|
/* ALG_ID_SHA384 */ { 382, 48, 1024, BCRYPT_SHA384_ALGORITHM },
|
|
|
|
/* ALG_ID_SHA512 */ { 382, 64, 1024, BCRYPT_SHA512_ALGORITHM }
|
2016-01-06 14:19:15 +01:00
|
|
|
};
|
2014-04-30 04:24:58 +02:00
|
|
|
|
2016-01-06 14:19:15 +01:00
|
|
|
struct algorithm
|
|
|
|
{
|
|
|
|
struct object hdr;
|
|
|
|
enum alg_id id;
|
2016-03-11 04:33:40 +01:00
|
|
|
BOOL hmac;
|
2016-01-06 14:19:15 +01:00
|
|
|
};
|
2014-04-30 04:24:58 +02:00
|
|
|
|
2016-04-04 11:07:05 +02:00
|
|
|
NTSTATUS WINAPI BCryptGenRandom(BCRYPT_ALG_HANDLE handle, UCHAR *buffer, ULONG count, ULONG flags)
|
|
|
|
{
|
|
|
|
const DWORD supported_flags = BCRYPT_USE_SYSTEM_PREFERRED_RNG;
|
|
|
|
struct algorithm *algorithm = handle;
|
|
|
|
|
|
|
|
TRACE("%p, %p, %u, %08x - semi-stub\n", handle, buffer, count, flags);
|
|
|
|
|
|
|
|
if (!algorithm)
|
|
|
|
{
|
|
|
|
/* It's valid to call without an algorithm if BCRYPT_USE_SYSTEM_PREFERRED_RNG
|
|
|
|
* is set. In this case the preferred system RNG is used.
|
|
|
|
*/
|
|
|
|
if (!(flags & BCRYPT_USE_SYSTEM_PREFERRED_RNG))
|
|
|
|
return STATUS_INVALID_HANDLE;
|
|
|
|
}
|
|
|
|
else if (algorithm->hdr.magic != MAGIC_ALG || algorithm->id != ALG_ID_RNG)
|
|
|
|
return STATUS_INVALID_HANDLE;
|
|
|
|
|
|
|
|
if (!buffer)
|
|
|
|
return STATUS_INVALID_PARAMETER;
|
|
|
|
|
|
|
|
if (flags & ~supported_flags)
|
|
|
|
FIXME("unsupported flags %08x\n", flags & ~supported_flags);
|
|
|
|
|
|
|
|
if (algorithm)
|
|
|
|
FIXME("ignoring selected algorithm\n");
|
|
|
|
|
|
|
|
/* When zero bytes are requested the function returns success too. */
|
|
|
|
if (!count)
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
|
|
|
|
if (algorithm || (flags & BCRYPT_USE_SYSTEM_PREFERRED_RNG))
|
|
|
|
{
|
|
|
|
if (RtlGenRandom(buffer, count))
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
FIXME("called with unsupported parameters, returning error\n");
|
|
|
|
return STATUS_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2016-01-06 14:19:15 +01:00
|
|
|
NTSTATUS WINAPI BCryptOpenAlgorithmProvider( BCRYPT_ALG_HANDLE *handle, LPCWSTR id, LPCWSTR implementation, DWORD flags )
|
|
|
|
{
|
|
|
|
struct algorithm *alg;
|
|
|
|
enum alg_id alg_id;
|
|
|
|
|
2016-03-11 04:33:40 +01:00
|
|
|
const DWORD supported_flags = BCRYPT_ALG_HANDLE_HMAC_FLAG;
|
|
|
|
|
2016-01-06 14:19:15 +01:00
|
|
|
TRACE( "%p, %s, %s, %08x\n", handle, wine_dbgstr_w(id), wine_dbgstr_w(implementation), flags );
|
|
|
|
|
|
|
|
if (!handle || !id) return STATUS_INVALID_PARAMETER;
|
2016-03-11 04:33:40 +01:00
|
|
|
if (flags & ~supported_flags)
|
2016-01-06 14:19:15 +01:00
|
|
|
{
|
2016-03-11 04:33:40 +01:00
|
|
|
FIXME( "unsupported flags %08x\n", flags & ~supported_flags);
|
2016-01-06 14:19:15 +01:00
|
|
|
return STATUS_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcmpW( id, BCRYPT_SHA1_ALGORITHM )) alg_id = ALG_ID_SHA1;
|
2016-03-08 04:45:27 +01:00
|
|
|
else if (!strcmpW( id, BCRYPT_MD5_ALGORITHM )) alg_id = ALG_ID_MD5;
|
2016-04-04 11:07:05 +02:00
|
|
|
else if (!strcmpW( id, BCRYPT_RNG_ALGORITHM )) alg_id = ALG_ID_RNG;
|
2016-01-06 14:19:15 +01:00
|
|
|
else if (!strcmpW( id, BCRYPT_SHA256_ALGORITHM )) alg_id = ALG_ID_SHA256;
|
|
|
|
else if (!strcmpW( id, BCRYPT_SHA384_ALGORITHM )) alg_id = ALG_ID_SHA384;
|
|
|
|
else if (!strcmpW( id, BCRYPT_SHA512_ALGORITHM )) alg_id = ALG_ID_SHA512;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FIXME( "algorithm %s not supported\n", debugstr_w(id) );
|
|
|
|
return STATUS_NOT_IMPLEMENTED;
|
|
|
|
}
|
2016-01-20 14:51:43 +01:00
|
|
|
if (implementation && strcmpW( implementation, MS_PRIMITIVE_PROVIDER ))
|
2016-01-06 14:19:15 +01:00
|
|
|
{
|
|
|
|
FIXME( "implementation %s not supported\n", debugstr_w(implementation) );
|
|
|
|
return STATUS_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(alg = HeapAlloc( GetProcessHeap(), 0, sizeof(*alg) ))) return STATUS_NO_MEMORY;
|
|
|
|
alg->hdr.magic = MAGIC_ALG;
|
|
|
|
alg->id = alg_id;
|
2016-03-11 04:33:40 +01:00
|
|
|
alg->hmac = flags & BCRYPT_ALG_HANDLE_HMAC_FLAG;
|
2016-01-06 14:19:15 +01:00
|
|
|
|
|
|
|
*handle = alg;
|
|
|
|
return STATUS_SUCCESS;
|
2014-04-30 04:24:58 +02:00
|
|
|
}
|
2014-06-25 04:53:42 +02:00
|
|
|
|
2016-01-06 14:19:15 +01:00
|
|
|
NTSTATUS WINAPI BCryptCloseAlgorithmProvider( BCRYPT_ALG_HANDLE handle, DWORD flags )
|
2014-06-25 04:53:42 +02:00
|
|
|
{
|
2016-01-06 14:19:15 +01:00
|
|
|
struct algorithm *alg = handle;
|
2014-06-25 04:53:42 +02:00
|
|
|
|
2016-01-06 14:19:15 +01:00
|
|
|
TRACE( "%p, %08x\n", handle, flags );
|
|
|
|
|
|
|
|
if (!alg || alg->hdr.magic != MAGIC_ALG) return STATUS_INVALID_HANDLE;
|
|
|
|
HeapFree( GetProcessHeap(), 0, alg );
|
|
|
|
return STATUS_SUCCESS;
|
2014-06-25 04:53:42 +02:00
|
|
|
}
|
2014-08-16 00:18:06 +02:00
|
|
|
|
|
|
|
NTSTATUS WINAPI BCryptGetFipsAlgorithmMode(BOOLEAN *enabled)
|
|
|
|
{
|
|
|
|
FIXME("%p - semi-stub\n", enabled);
|
|
|
|
|
|
|
|
if (!enabled)
|
|
|
|
return STATUS_INVALID_PARAMETER;
|
|
|
|
|
|
|
|
*enabled = FALSE;
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
2014-10-21 22:53:49 +02:00
|
|
|
|
2017-03-07 23:13:51 +01:00
|
|
|
struct hash_impl
|
2016-01-06 14:19:16 +01:00
|
|
|
{
|
2017-03-07 23:14:36 +01:00
|
|
|
union
|
|
|
|
{
|
|
|
|
MD5_CTX md5;
|
|
|
|
SHA_CTX sha1;
|
2017-03-07 23:15:08 +01:00
|
|
|
SHA256_CTX sha256;
|
2017-03-08 13:09:04 +01:00
|
|
|
SHA512_CTX sha512;
|
2017-03-07 23:14:36 +01:00
|
|
|
} u;
|
2017-03-07 23:13:51 +01:00
|
|
|
};
|
2016-03-11 04:33:40 +01:00
|
|
|
|
2017-03-07 23:13:51 +01:00
|
|
|
static NTSTATUS hash_init( struct hash_impl *hash, enum alg_id alg_id )
|
2016-01-06 14:19:18 +01:00
|
|
|
{
|
2017-03-07 23:14:36 +01:00
|
|
|
switch (alg_id)
|
|
|
|
{
|
|
|
|
case ALG_ID_MD5:
|
|
|
|
MD5Init( &hash->u.md5 );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ALG_ID_SHA1:
|
|
|
|
A_SHAInit( &hash->u.sha1 );
|
|
|
|
break;
|
|
|
|
|
2017-03-07 23:15:08 +01:00
|
|
|
case ALG_ID_SHA256:
|
|
|
|
sha256_init( &hash->u.sha256 );
|
|
|
|
break;
|
|
|
|
|
2017-03-08 13:09:04 +01:00
|
|
|
case ALG_ID_SHA384:
|
|
|
|
sha384_init( &hash->u.sha512 );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ALG_ID_SHA512:
|
|
|
|
sha512_init( &hash->u.sha512 );
|
|
|
|
break;
|
|
|
|
|
2017-03-07 23:14:36 +01:00
|
|
|
default:
|
|
|
|
ERR( "unhandled id %u\n", alg_id );
|
|
|
|
return STATUS_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
return STATUS_SUCCESS;
|
2016-01-06 14:19:18 +01:00
|
|
|
}
|
2016-01-06 14:19:19 +01:00
|
|
|
|
2017-03-07 23:13:51 +01:00
|
|
|
static NTSTATUS hash_update( struct hash_impl *hash, enum alg_id alg_id,
|
|
|
|
UCHAR *input, ULONG size )
|
2016-03-11 04:33:40 +01:00
|
|
|
{
|
2017-03-07 23:14:36 +01:00
|
|
|
switch (alg_id)
|
|
|
|
{
|
|
|
|
case ALG_ID_MD5:
|
|
|
|
MD5Update( &hash->u.md5, input, size );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ALG_ID_SHA1:
|
|
|
|
A_SHAUpdate( &hash->u.sha1, input, size );
|
|
|
|
break;
|
|
|
|
|
2017-03-07 23:15:08 +01:00
|
|
|
case ALG_ID_SHA256:
|
|
|
|
sha256_update( &hash->u.sha256, input, size );
|
|
|
|
break;
|
|
|
|
|
2017-03-08 13:09:04 +01:00
|
|
|
case ALG_ID_SHA384:
|
|
|
|
sha384_update( &hash->u.sha512, input, size );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ALG_ID_SHA512:
|
|
|
|
sha512_update( &hash->u.sha512, input, size );
|
|
|
|
break;
|
|
|
|
|
2017-03-07 23:14:36 +01:00
|
|
|
default:
|
|
|
|
ERR( "unhandled id %u\n", alg_id );
|
|
|
|
return STATUS_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
return STATUS_SUCCESS;
|
2016-03-11 04:33:40 +01:00
|
|
|
}
|
|
|
|
|
2017-03-07 23:13:51 +01:00
|
|
|
static NTSTATUS hash_finish( struct hash_impl *hash, enum alg_id alg_id,
|
|
|
|
UCHAR *output, ULONG size )
|
2016-01-06 14:19:19 +01:00
|
|
|
{
|
2017-03-07 23:14:36 +01:00
|
|
|
switch (alg_id)
|
|
|
|
{
|
|
|
|
case ALG_ID_MD5:
|
|
|
|
MD5Final( &hash->u.md5 );
|
|
|
|
memcpy( output, hash->u.md5.digest, 16 );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ALG_ID_SHA1:
|
|
|
|
A_SHAFinal( &hash->u.sha1, (ULONG *)output );
|
|
|
|
break;
|
|
|
|
|
2017-03-07 23:15:08 +01:00
|
|
|
case ALG_ID_SHA256:
|
|
|
|
sha256_finalize( &hash->u.sha256, output );
|
|
|
|
break;
|
|
|
|
|
2017-03-08 13:09:04 +01:00
|
|
|
case ALG_ID_SHA384:
|
|
|
|
sha384_finalize( &hash->u.sha512, output );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ALG_ID_SHA512:
|
|
|
|
sha512_finalize( &hash->u.sha512, output );
|
|
|
|
break;
|
|
|
|
|
2017-03-07 23:14:36 +01:00
|
|
|
default:
|
|
|
|
ERR( "unhandled id %u\n", alg_id );
|
|
|
|
return STATUS_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
return STATUS_SUCCESS;
|
2016-01-06 14:19:19 +01:00
|
|
|
}
|
2016-03-11 04:33:40 +01:00
|
|
|
|
2017-03-07 23:13:51 +01:00
|
|
|
struct hash
|
2016-03-11 04:33:40 +01:00
|
|
|
{
|
2017-03-07 23:13:51 +01:00
|
|
|
struct object hdr;
|
|
|
|
enum alg_id alg_id;
|
|
|
|
BOOL hmac;
|
|
|
|
struct hash_impl outer;
|
|
|
|
struct hash_impl inner;
|
|
|
|
};
|
2016-01-06 14:19:16 +01:00
|
|
|
|
2016-01-29 14:25:23 +01:00
|
|
|
static NTSTATUS generic_alg_property( enum alg_id id, const WCHAR *prop, UCHAR *buf, ULONG size, ULONG *ret_size )
|
|
|
|
{
|
2017-03-10 15:33:42 +01:00
|
|
|
if (!strcmpW( prop, BCRYPT_OBJECT_LENGTH ))
|
|
|
|
{
|
|
|
|
if (!alg_props[id].object_length)
|
|
|
|
return STATUS_NOT_SUPPORTED;
|
|
|
|
*ret_size = sizeof(ULONG);
|
|
|
|
if (size < sizeof(ULONG))
|
|
|
|
return STATUS_BUFFER_TOO_SMALL;
|
|
|
|
if (buf)
|
|
|
|
*(ULONG *)buf = alg_props[id].object_length;
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2016-01-29 14:25:23 +01:00
|
|
|
if (!strcmpW( prop, BCRYPT_HASH_LENGTH ))
|
|
|
|
{
|
2017-03-10 15:34:08 +01:00
|
|
|
if (!alg_props[id].hash_length)
|
|
|
|
return STATUS_NOT_SUPPORTED;
|
2016-01-29 14:25:23 +01:00
|
|
|
*ret_size = sizeof(ULONG);
|
|
|
|
if (size < sizeof(ULONG))
|
|
|
|
return STATUS_BUFFER_TOO_SMALL;
|
|
|
|
if(buf)
|
|
|
|
*(ULONG*)buf = alg_props[id].hash_length;
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2016-01-29 14:25:30 +01:00
|
|
|
if (!strcmpW( prop, BCRYPT_ALGORITHM_NAME ))
|
|
|
|
{
|
|
|
|
*ret_size = (strlenW(alg_props[id].alg_name)+1)*sizeof(WCHAR);
|
|
|
|
if (size < *ret_size)
|
|
|
|
return STATUS_BUFFER_TOO_SMALL;
|
|
|
|
if(buf)
|
|
|
|
memcpy(buf, alg_props[id].alg_name, *ret_size);
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2016-01-29 14:25:23 +01:00
|
|
|
return STATUS_NOT_IMPLEMENTED;
|
|
|
|
}
|
2016-01-06 14:19:17 +01:00
|
|
|
|
|
|
|
static NTSTATUS get_alg_property( enum alg_id id, const WCHAR *prop, UCHAR *buf, ULONG size, ULONG *ret_size )
|
2014-10-21 22:53:49 +02:00
|
|
|
{
|
2016-01-29 14:25:23 +01:00
|
|
|
NTSTATUS status;
|
2014-10-21 22:53:49 +02:00
|
|
|
|
2016-01-29 14:25:23 +01:00
|
|
|
status = generic_alg_property( id, prop, buf, size, ret_size );
|
2017-03-10 15:33:42 +01:00
|
|
|
if (status == STATUS_NOT_IMPLEMENTED)
|
|
|
|
FIXME( "unsupported property %s\n", debugstr_w(prop) );
|
|
|
|
return status;
|
2016-01-06 14:19:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static NTSTATUS get_hash_property( enum alg_id id, const WCHAR *prop, UCHAR *buf, ULONG size, ULONG *ret_size )
|
|
|
|
{
|
2016-01-29 14:25:23 +01:00
|
|
|
NTSTATUS status;
|
2016-01-06 14:19:17 +01:00
|
|
|
|
2016-01-29 14:25:23 +01:00
|
|
|
status = generic_alg_property( id, prop, buf, size, ret_size );
|
|
|
|
if (status == STATUS_NOT_IMPLEMENTED)
|
|
|
|
FIXME( "unsupported property %s\n", debugstr_w(prop) );
|
|
|
|
return status;
|
2016-01-06 14:19:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
NTSTATUS WINAPI BCryptGetProperty( BCRYPT_HANDLE handle, LPCWSTR prop, UCHAR *buffer, ULONG count, ULONG *res, ULONG flags )
|
|
|
|
{
|
|
|
|
struct object *object = handle;
|
|
|
|
|
|
|
|
TRACE( "%p, %s, %p, %u, %p, %08x\n", handle, wine_dbgstr_w(prop), buffer, count, res, flags );
|
|
|
|
|
|
|
|
if (!object) return STATUS_INVALID_HANDLE;
|
|
|
|
if (!prop || !res) return STATUS_INVALID_PARAMETER;
|
|
|
|
|
|
|
|
switch (object->magic)
|
|
|
|
{
|
|
|
|
case MAGIC_ALG:
|
|
|
|
{
|
|
|
|
const struct algorithm *alg = (const struct algorithm *)object;
|
|
|
|
return get_alg_property( alg->id, prop, buffer, count, res );
|
|
|
|
}
|
|
|
|
case MAGIC_HASH:
|
|
|
|
{
|
|
|
|
const struct hash *hash = (const struct hash *)object;
|
|
|
|
return get_hash_property( hash->alg_id, prop, buffer, count, res );
|
|
|
|
}
|
|
|
|
default:
|
2016-01-24 19:52:00 +01:00
|
|
|
WARN( "unknown magic %08x\n", object->magic );
|
2016-01-06 14:19:17 +01:00
|
|
|
return STATUS_INVALID_HANDLE;
|
|
|
|
}
|
2014-10-21 22:53:49 +02:00
|
|
|
}
|
2015-09-25 11:23:40 +02:00
|
|
|
|
2016-01-06 14:19:16 +01:00
|
|
|
NTSTATUS WINAPI BCryptCreateHash( BCRYPT_ALG_HANDLE algorithm, BCRYPT_HASH_HANDLE *handle, UCHAR *object, ULONG objectlen,
|
|
|
|
UCHAR *secret, ULONG secretlen, ULONG flags )
|
2015-09-25 11:23:40 +02:00
|
|
|
{
|
2016-01-06 14:19:16 +01:00
|
|
|
struct algorithm *alg = algorithm;
|
2017-03-07 23:13:51 +01:00
|
|
|
UCHAR buffer[MAX_HASH_BLOCK_BITS / 8] = {0};
|
2016-01-06 14:19:16 +01:00
|
|
|
struct hash *hash;
|
2017-03-07 23:13:51 +01:00
|
|
|
int block_bytes;
|
2016-01-06 14:19:16 +01:00
|
|
|
NTSTATUS status;
|
2017-03-07 23:13:51 +01:00
|
|
|
int i;
|
2015-09-25 11:23:40 +02:00
|
|
|
|
2016-01-06 14:19:16 +01:00
|
|
|
TRACE( "%p, %p, %p, %u, %p, %u, %08x - stub\n", algorithm, handle, object, objectlen,
|
|
|
|
secret, secretlen, flags );
|
|
|
|
if (flags)
|
|
|
|
{
|
|
|
|
FIXME( "unimplemented flags %08x\n", flags );
|
|
|
|
return STATUS_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!alg || alg->hdr.magic != MAGIC_ALG) return STATUS_INVALID_HANDLE;
|
|
|
|
if (object) FIXME( "ignoring object buffer\n" );
|
|
|
|
|
|
|
|
if (!(hash = HeapAlloc( GetProcessHeap(), 0, sizeof(*hash) ))) return STATUS_NO_MEMORY;
|
|
|
|
hash->hdr.magic = MAGIC_HASH;
|
|
|
|
hash->alg_id = alg->id;
|
2016-03-11 04:33:40 +01:00
|
|
|
hash->hmac = alg->hmac;
|
|
|
|
|
2017-03-07 23:13:51 +01:00
|
|
|
/* initialize hash */
|
|
|
|
if ((status = hash_init( &hash->inner, hash->alg_id ))) goto end;
|
|
|
|
if (!hash->hmac) goto end;
|
|
|
|
|
|
|
|
/* initialize hmac */
|
|
|
|
if ((status = hash_init( &hash->outer, hash->alg_id ))) goto end;
|
|
|
|
block_bytes = alg_props[hash->alg_id].block_bits / 8;
|
|
|
|
if (secretlen > block_bytes)
|
2016-03-11 04:33:40 +01:00
|
|
|
{
|
2017-03-07 23:13:51 +01:00
|
|
|
struct hash_impl temp;
|
|
|
|
if ((status = hash_init( &temp, hash->alg_id ))) goto end;
|
|
|
|
if ((status = hash_update( &temp, hash->alg_id, secret, secretlen ))) goto end;
|
|
|
|
if ((status = hash_finish( &temp, hash->alg_id, buffer,
|
|
|
|
alg_props[hash->alg_id].hash_length ))) goto end;
|
2016-03-11 04:33:40 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-03-07 23:13:51 +01:00
|
|
|
memcpy( buffer, secret, secretlen );
|
2016-03-11 04:33:40 +01:00
|
|
|
}
|
2017-03-07 23:13:51 +01:00
|
|
|
for (i = 0; i < block_bytes; i++) buffer[i] ^= 0x5c;
|
|
|
|
if ((status = hash_update( &hash->outer, hash->alg_id, buffer, block_bytes ))) goto end;
|
|
|
|
for (i = 0; i < block_bytes; i++) buffer[i] ^= (0x5c ^ 0x36);
|
|
|
|
status = hash_update( &hash->inner, hash->alg_id, buffer, block_bytes );
|
2016-03-11 04:33:40 +01:00
|
|
|
|
2017-03-07 23:13:51 +01:00
|
|
|
end:
|
2016-03-11 04:33:40 +01:00
|
|
|
if (status != STATUS_SUCCESS)
|
2016-01-06 14:19:16 +01:00
|
|
|
{
|
|
|
|
HeapFree( GetProcessHeap(), 0, hash );
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
*handle = hash;
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2017-03-10 15:35:14 +01:00
|
|
|
NTSTATUS WINAPI BCryptDuplicateHash( BCRYPT_HASH_HANDLE handle, BCRYPT_HASH_HANDLE *handle_copy,
|
|
|
|
UCHAR *object, ULONG objectlen, ULONG flags )
|
|
|
|
{
|
|
|
|
struct hash *hash_orig = handle;
|
|
|
|
struct hash *hash_copy;
|
|
|
|
|
|
|
|
TRACE( "%p, %p, %p, %u, %u\n", handle, handle_copy, object, objectlen, flags );
|
|
|
|
|
|
|
|
if (!hash_orig || hash_orig->hdr.magic != MAGIC_HASH) return STATUS_INVALID_HANDLE;
|
|
|
|
if (!handle_copy) return STATUS_INVALID_PARAMETER;
|
|
|
|
if (object) FIXME( "ignoring object buffer\n" );
|
|
|
|
|
|
|
|
if (!(hash_copy = HeapAlloc( GetProcessHeap(), 0, sizeof(*hash_copy) )))
|
|
|
|
return STATUS_NO_MEMORY;
|
|
|
|
|
|
|
|
memcpy( hash_copy, hash_orig, sizeof(*hash_orig) );
|
|
|
|
|
|
|
|
*handle_copy = hash_copy;
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2016-01-06 14:19:16 +01:00
|
|
|
NTSTATUS WINAPI BCryptDestroyHash( BCRYPT_HASH_HANDLE handle )
|
|
|
|
{
|
|
|
|
struct hash *hash = handle;
|
|
|
|
|
|
|
|
TRACE( "%p\n", handle );
|
|
|
|
|
|
|
|
if (!hash || hash->hdr.magic != MAGIC_HASH) return STATUS_INVALID_HANDLE;
|
|
|
|
HeapFree( GetProcessHeap(), 0, hash );
|
|
|
|
return STATUS_SUCCESS;
|
2015-09-25 11:23:40 +02:00
|
|
|
}
|
2016-01-06 14:19:18 +01:00
|
|
|
|
|
|
|
NTSTATUS WINAPI BCryptHashData( BCRYPT_HASH_HANDLE handle, UCHAR *input, ULONG size, ULONG flags )
|
|
|
|
{
|
|
|
|
struct hash *hash = handle;
|
|
|
|
|
|
|
|
TRACE( "%p, %p, %u, %08x\n", handle, input, size, flags );
|
|
|
|
|
|
|
|
if (!hash || hash->hdr.magic != MAGIC_HASH) return STATUS_INVALID_HANDLE;
|
2016-03-09 05:42:45 +01:00
|
|
|
if (!input) return STATUS_SUCCESS;
|
2016-01-06 14:19:18 +01:00
|
|
|
|
2017-03-07 23:13:51 +01:00
|
|
|
return hash_update( &hash->inner, hash->alg_id, input, size );
|
2016-01-06 14:19:18 +01:00
|
|
|
}
|
2016-01-06 14:19:19 +01:00
|
|
|
|
|
|
|
NTSTATUS WINAPI BCryptFinishHash( BCRYPT_HASH_HANDLE handle, UCHAR *output, ULONG size, ULONG flags )
|
|
|
|
{
|
2017-03-07 23:13:51 +01:00
|
|
|
UCHAR buffer[MAX_HASH_OUTPUT_BYTES];
|
2016-01-06 14:19:19 +01:00
|
|
|
struct hash *hash = handle;
|
2017-03-07 23:13:51 +01:00
|
|
|
NTSTATUS status;
|
|
|
|
int hash_length;
|
2016-01-06 14:19:19 +01:00
|
|
|
|
|
|
|
TRACE( "%p, %p, %u, %08x\n", handle, output, size, flags );
|
|
|
|
|
|
|
|
if (!hash || hash->hdr.magic != MAGIC_HASH) return STATUS_INVALID_HANDLE;
|
|
|
|
if (!output) return STATUS_INVALID_PARAMETER;
|
|
|
|
|
2017-03-07 23:13:51 +01:00
|
|
|
if (!hash->hmac)
|
|
|
|
return hash_finish( &hash->inner, hash->alg_id, output, size );
|
|
|
|
|
|
|
|
hash_length = alg_props[hash->alg_id].hash_length;
|
|
|
|
if ((status = hash_finish( &hash->inner, hash->alg_id, buffer, hash_length ))) return status;
|
|
|
|
if ((status = hash_update( &hash->outer, hash->alg_id, buffer, hash_length ))) return status;
|
|
|
|
return hash_finish( &hash->outer, hash->alg_id, output, size );
|
2016-01-06 14:19:19 +01:00
|
|
|
}
|
2016-01-13 10:43:35 +01:00
|
|
|
|
2016-03-24 06:22:22 +01:00
|
|
|
NTSTATUS WINAPI BCryptHash( BCRYPT_ALG_HANDLE algorithm, UCHAR *secret, ULONG secretlen,
|
|
|
|
UCHAR *input, ULONG inputlen, UCHAR *output, ULONG outputlen )
|
|
|
|
{
|
|
|
|
NTSTATUS status;
|
|
|
|
BCRYPT_HASH_HANDLE handle;
|
|
|
|
|
|
|
|
TRACE( "%p, %p, %u, %p, %u, %p, %u\n", algorithm, secret, secretlen,
|
|
|
|
input, inputlen, output, outputlen );
|
|
|
|
|
|
|
|
status = BCryptCreateHash( algorithm, &handle, NULL, 0, secret, secretlen, 0);
|
|
|
|
if (status != STATUS_SUCCESS)
|
|
|
|
{
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = BCryptHashData( handle, input, inputlen, 0 );
|
|
|
|
if (status != STATUS_SUCCESS)
|
|
|
|
{
|
|
|
|
BCryptDestroyHash( handle );
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = BCryptFinishHash( handle, output, outputlen, 0 );
|
|
|
|
if (status != STATUS_SUCCESS)
|
|
|
|
{
|
|
|
|
BCryptDestroyHash( handle );
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
return BCryptDestroyHash( handle );
|
|
|
|
}
|
|
|
|
|
2016-01-13 10:43:35 +01:00
|
|
|
BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
|
|
|
|
{
|
|
|
|
switch (reason)
|
|
|
|
{
|
|
|
|
case DLL_PROCESS_ATTACH:
|
|
|
|
instance = hinst;
|
|
|
|
DisableThreadLibraryCalls( hinst );
|
2016-01-18 13:52:40 +01:00
|
|
|
#if defined(HAVE_GNUTLS_HASH) && !defined(HAVE_COMMONCRYPTO_COMMONDIGEST_H)
|
2016-01-13 10:43:35 +01:00
|
|
|
gnutls_initialize();
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DLL_PROCESS_DETACH:
|
|
|
|
if (reserved) break;
|
2016-01-18 13:52:40 +01:00
|
|
|
#if defined(HAVE_GNUTLS_HASH) && !defined(HAVE_COMMONCRYPTO_COMMONDIGEST_H)
|
2016-01-13 10:43:35 +01:00
|
|
|
gnutls_uninitialize();
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|