From 03630a42f4db194ca70192769fbf3ea3c3dccca5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20M=C3=BCller?= Date: Tue, 7 Mar 2017 23:14:36 +0100 Subject: [PATCH] bcrypt: Use builtin MD5/SHA1 implementation. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Michael Müller Signed-off-by: Sebastian Lackner Signed-off-by: Alexandre Julliard --- dlls/bcrypt/bcrypt_internal.h | 53 +++++++++++++++++++++++++++++++ dlls/bcrypt/bcrypt_main.c | 60 +++++++++++++++++++++++++++++++---- 2 files changed, 106 insertions(+), 7 deletions(-) create mode 100644 dlls/bcrypt/bcrypt_internal.h diff --git a/dlls/bcrypt/bcrypt_internal.h b/dlls/bcrypt/bcrypt_internal.h new file mode 100644 index 00000000000..de6a37e9b5e --- /dev/null +++ b/dlls/bcrypt/bcrypt_internal.h @@ -0,0 +1,53 @@ +/* + * Copyright 2016 Michael Müller + * + * 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 + * + */ + +#ifndef __BCRYPT_INTERNAL_H +#define __BCRYPT_INTERNAL_H + +#include + +#include "windef.h" +#include "winbase.h" + +/* Definitions from advapi32 */ +typedef struct +{ + unsigned int i[2]; + unsigned int buf[4]; + unsigned char in[64]; + unsigned char digest[16]; +} MD5_CTX; + +VOID WINAPI MD5Init(MD5_CTX *ctx); +VOID WINAPI MD5Update(MD5_CTX *ctx, const unsigned char *buf, unsigned int len); +VOID WINAPI MD5Final(MD5_CTX *ctx); + +typedef struct +{ + ULONG Unknown[6]; + ULONG State[5]; + ULONG Count[2]; + UCHAR Buffer[64]; +} SHA_CTX; + +VOID WINAPI A_SHAInit(SHA_CTX *ctx); +VOID WINAPI A_SHAUpdate(SHA_CTX *ctx, const UCHAR *buffer, UINT size); +VOID WINAPI A_SHAFinal(SHA_CTX *ctx, PULONG result); + +#endif /* __BCRYPT_INTERNAL_H */ diff --git a/dlls/bcrypt/bcrypt_main.c b/dlls/bcrypt/bcrypt_main.c index 849dc3e5c35..cef2b1bdd16 100644 --- a/dlls/bcrypt/bcrypt_main.c +++ b/dlls/bcrypt/bcrypt_main.c @@ -36,6 +36,8 @@ #include "ntsecapi.h" #include "bcrypt.h" +#include "bcrypt_internal.h" + #include "wine/debug.h" #include "wine/library.h" #include "wine/unicode.h" @@ -442,27 +444,71 @@ static NTSTATUS hash_finish( struct hash_impl *hash, enum alg_id alg_id, #else struct hash_impl { - + union + { + MD5_CTX md5; + SHA_CTX sha1; + } u; }; static NTSTATUS hash_init( struct hash_impl *hash, enum alg_id alg_id ) { - ERR( "support for hashes not available at build time\n" ); - return STATUS_NOT_IMPLEMENTED; + switch (alg_id) + { + case ALG_ID_MD5: + MD5Init( &hash->u.md5 ); + break; + + case ALG_ID_SHA1: + A_SHAInit( &hash->u.sha1 ); + break; + + default: + ERR( "unhandled id %u\n", alg_id ); + return STATUS_NOT_IMPLEMENTED; + } + return STATUS_SUCCESS; } static NTSTATUS hash_update( struct hash_impl *hash, enum alg_id alg_id, UCHAR *input, ULONG size ) { - ERR( "support for hashes not available at build time\n" ); - return STATUS_NOT_IMPLEMENTED; + 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; + + default: + ERR( "unhandled id %u\n", alg_id ); + return STATUS_NOT_IMPLEMENTED; + } + return STATUS_SUCCESS; } static NTSTATUS hash_finish( struct hash_impl *hash, enum alg_id alg_id, UCHAR *output, ULONG size ) { - ERR( "support for hashes not available at build time\n" ); - return STATUS_NOT_IMPLEMENTED; + 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; + + default: + ERR( "unhandled id %u\n", alg_id ); + return STATUS_NOT_IMPLEMENTED; + } + return STATUS_SUCCESS; } #endif