mirror of
https://gitlab.winehq.org/wine/wine-staging.git
synced 2025-01-28 22:04:43 -08:00
bcrypt-Improvements: Added patches to fix handling of padding.
This commit is contained in:
parent
533032bd79
commit
b95d6468de
@ -0,0 +1,110 @@
|
||||
From 8554c6eea279baa21e10d3b742e1c62a732bbe69 Mon Sep 17 00:00:00 2001
|
||||
From: =?UTF-8?q?Michael=20M=C3=BCller?= <michael@fds-team.de>
|
||||
Date: Wed, 21 Dec 2016 04:09:03 +0100
|
||||
Subject: bcrypt: Fix handling of padding when input size equals block size for
|
||||
AES.
|
||||
|
||||
---
|
||||
dlls/bcrypt/bcrypt_main.c | 13 +++++++------
|
||||
dlls/bcrypt/tests/bcrypt.c | 33 ++++++++++++++++++++++++++++++++-
|
||||
2 files changed, 39 insertions(+), 7 deletions(-)
|
||||
|
||||
diff --git a/dlls/bcrypt/bcrypt_main.c b/dlls/bcrypt/bcrypt_main.c
|
||||
index 944a9ea..f97638f 100644
|
||||
--- a/dlls/bcrypt/bcrypt_main.c
|
||||
+++ b/dlls/bcrypt/bcrypt_main.c
|
||||
@@ -997,11 +997,12 @@ NTSTATUS WINAPI BCryptEncrypt( BCRYPT_KEY_HANDLE handle, UCHAR *input, ULONG inp
|
||||
if ((status = key_set_params( key, iv, iv_len ))) return status;
|
||||
|
||||
*ret_len = input_len;
|
||||
- if (input_len & (key->block_size - 1))
|
||||
- {
|
||||
- if (!(flags & BCRYPT_BLOCK_PADDING)) return STATUS_INVALID_BUFFER_SIZE;
|
||||
- *ret_len = (input_len + key->block_size - 1) & ~(key->block_size - 1);
|
||||
- }
|
||||
+
|
||||
+ if (flags & BCRYPT_BLOCK_PADDING)
|
||||
+ *ret_len = (input_len + key->block_size) & ~(key->block_size - 1);
|
||||
+ else if (input_len & (key->block_size - 1))
|
||||
+ return STATUS_INVALID_BUFFER_SIZE;
|
||||
+
|
||||
if (!output) return STATUS_SUCCESS;
|
||||
if (output_len < *ret_len) return STATUS_BUFFER_TOO_SMALL;
|
||||
|
||||
@@ -1014,7 +1015,7 @@ NTSTATUS WINAPI BCryptEncrypt( BCRYPT_KEY_HANDLE handle, UCHAR *input, ULONG inp
|
||||
src += key->block_size;
|
||||
dst += key->block_size;
|
||||
}
|
||||
- if (bytes_left)
|
||||
+ if (bytes_left || (flags & BCRYPT_BLOCK_PADDING))
|
||||
{
|
||||
if (!(buf = HeapAlloc( GetProcessHeap(), 0, key->block_size ))) return STATUS_NO_MEMORY;
|
||||
memcpy( buf, src, bytes_left );
|
||||
diff --git a/dlls/bcrypt/tests/bcrypt.c b/dlls/bcrypt/tests/bcrypt.c
|
||||
index 2668153..64a4625 100644
|
||||
--- a/dlls/bcrypt/tests/bcrypt.c
|
||||
+++ b/dlls/bcrypt/tests/bcrypt.c
|
||||
@@ -918,14 +918,21 @@ static void test_BCryptEncrypt(void)
|
||||
{0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f};
|
||||
static UCHAR data[] =
|
||||
{0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10};
|
||||
+ static UCHAR data2[] =
|
||||
+ {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,
|
||||
+ 0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10};
|
||||
static UCHAR expected[] =
|
||||
{0xc6,0xa1,0x3b,0x37,0x87,0x8f,0x5b,0x82,0x6f,0x4f,0x81,0x62,0xa1,0xc8,0xd8,0x79};
|
||||
static UCHAR expected2[] =
|
||||
{0xc6,0xa1,0x3b,0x37,0x87,0x8f,0x5b,0x82,0x6f,0x4f,0x81,0x62,0xa1,0xc8,0xd8,0x79,
|
||||
0x28,0x73,0x3d,0xef,0x84,0x8f,0xb0,0xa6,0x5d,0x1a,0x51,0xb7,0xec,0x8f,0xea,0xe9};
|
||||
+ static UCHAR expected3[] =
|
||||
+ {0xc6,0xa1,0x3b,0x37,0x87,0x8f,0x5b,0x82,0x6f,0x4f,0x81,0x62,0xa1,0xc8,0xd8,0x79,
|
||||
+ 0xb1,0xa2,0x92,0x73,0xbe,0x2c,0x42,0x07,0xa5,0xac,0xe3,0x93,0x39,0x8c,0xb6,0xfb,
|
||||
+ 0x87,0x5d,0xea,0xa3,0x7e,0x0f,0xde,0xfa,0xd9,0xec,0x6c,0x4e,0x3c,0x76,0x86,0xe4};
|
||||
BCRYPT_ALG_HANDLE aes;
|
||||
BCRYPT_KEY_HANDLE key;
|
||||
- UCHAR *buf, ciphertext[32], ivbuf[16];
|
||||
+ UCHAR *buf, ciphertext[48], ivbuf[16];
|
||||
ULONG size, len, i;
|
||||
NTSTATUS ret;
|
||||
|
||||
@@ -982,6 +989,23 @@ static void test_BCryptEncrypt(void)
|
||||
for (i = 0; i < 32; i++)
|
||||
ok(ciphertext[i] == expected2[i], "%u: %02x != %02x\n", i, ciphertext[i], expected2[i]);
|
||||
|
||||
+ /* input size is a multiple of block size, block padding set */
|
||||
+ size = 0;
|
||||
+ memcpy(ivbuf, iv, sizeof(iv));
|
||||
+ ret = pBCryptEncrypt(key, data2, 32, NULL, ivbuf, 16, NULL, 0, &size, BCRYPT_BLOCK_PADDING);
|
||||
+ ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
|
||||
+ ok(size == 48, "got %u\n", size);
|
||||
+
|
||||
+ size = 0;
|
||||
+ memcpy(ivbuf, iv, sizeof(iv));
|
||||
+ memset(ciphertext, 0, sizeof(ciphertext));
|
||||
+ ret = pBCryptEncrypt(key, data2, 32, NULL, ivbuf, 16, ciphertext, 48, &size, BCRYPT_BLOCK_PADDING);
|
||||
+ ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
|
||||
+ ok(size == 48, "got %u\n", size);
|
||||
+ ok(!memcmp(ciphertext, expected3, sizeof(expected3)), "wrong data\n");
|
||||
+ for (i = 0; i < 48; i++)
|
||||
+ ok(ciphertext[i] == expected3[i], "%u: %02x != %02x\n", i, ciphertext[i], expected3[i]);
|
||||
+
|
||||
/* output size too small */
|
||||
size = 0;
|
||||
memcpy(ivbuf, iv, sizeof(iv));
|
||||
@@ -990,6 +1014,13 @@ static void test_BCryptEncrypt(void)
|
||||
ok(ret == STATUS_BUFFER_TOO_SMALL, "got %08x\n", ret);
|
||||
ok(size == 32, "got %u\n", size);
|
||||
|
||||
+ size = 0;
|
||||
+ memcpy(ivbuf, iv, sizeof(iv));
|
||||
+ memset(ciphertext, 0, sizeof(ciphertext));
|
||||
+ ret = pBCryptEncrypt(key, data2, 32, NULL, ivbuf, 16, ciphertext, 32, &size, BCRYPT_BLOCK_PADDING);
|
||||
+ ok(ret == STATUS_BUFFER_TOO_SMALL, "got %08x\n", ret);
|
||||
+ ok(size == 48, "got %u\n", size);
|
||||
+
|
||||
ret = pBCryptDestroyKey(key);
|
||||
ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
|
||||
HeapFree(GetProcessHeap(), 0, buf);
|
||||
--
|
||||
2.9.0
|
||||
|
@ -0,0 +1,165 @@
|
||||
From 6a57c0b62866b751a5140d35ae7b131bd895745f Mon Sep 17 00:00:00 2001
|
||||
From: =?UTF-8?q?Michael=20M=C3=BCller?= <michael@fds-team.de>
|
||||
Date: Wed, 21 Dec 2016 05:12:06 +0100
|
||||
Subject: bcrypt: Properly handle padding in AES decryption.
|
||||
|
||||
---
|
||||
dlls/bcrypt/bcrypt_main.c | 39 +++++++++++++++++++++++++++--
|
||||
dlls/bcrypt/tests/bcrypt.c | 62 +++++++++++++++++++++++++++++++++++++++++++++-
|
||||
2 files changed, 98 insertions(+), 3 deletions(-)
|
||||
|
||||
diff --git a/dlls/bcrypt/bcrypt_main.c b/dlls/bcrypt/bcrypt_main.c
|
||||
index f97638f..653301b 100644
|
||||
--- a/dlls/bcrypt/bcrypt_main.c
|
||||
+++ b/dlls/bcrypt/bcrypt_main.c
|
||||
@@ -1032,6 +1032,8 @@ NTSTATUS WINAPI BCryptDecrypt( BCRYPT_KEY_HANDLE handle, UCHAR *input, ULONG inp
|
||||
ULONG output_len, ULONG *ret_len, ULONG flags )
|
||||
{
|
||||
struct key *key = handle;
|
||||
+ ULONG bytes_left = input_len;
|
||||
+ UCHAR *buf, *src, *dst;
|
||||
NTSTATUS status;
|
||||
|
||||
FIXME( "%p, %p, %u, %p, %p, %u, %p, %u, %p, %08x\n", handle, input, input_len,
|
||||
@@ -1052,11 +1054,44 @@ NTSTATUS WINAPI BCryptDecrypt( BCRYPT_KEY_HANDLE handle, UCHAR *input, ULONG inp
|
||||
if ((status = key_set_params( key, iv, iv_len ))) return status;
|
||||
|
||||
*ret_len = input_len;
|
||||
+
|
||||
if (input_len & (key->block_size - 1)) return STATUS_INVALID_BUFFER_SIZE;
|
||||
if (!output) return STATUS_SUCCESS;
|
||||
- if (output_len < *ret_len) return STATUS_BUFFER_TOO_SMALL;
|
||||
+ if (flags & BCRYPT_BLOCK_PADDING)
|
||||
+ {
|
||||
+ if (output_len + key->block_size < *ret_len) return STATUS_BUFFER_TOO_SMALL;
|
||||
+ if (input_len < key->block_size) return STATUS_BUFFER_TOO_SMALL;
|
||||
+ bytes_left -= key->block_size;
|
||||
+ }
|
||||
+ else if (output_len < *ret_len)
|
||||
+ return STATUS_BUFFER_TOO_SMALL;
|
||||
|
||||
- return key_decrypt( key, input, input_len, output, output_len );
|
||||
+ src = input;
|
||||
+ dst = output;
|
||||
+ while (bytes_left >= key->block_size)
|
||||
+ {
|
||||
+ if ((status = key_decrypt( key, src, key->block_size, dst, key->block_size ))) return status;
|
||||
+ bytes_left -= key->block_size;
|
||||
+ src += key->block_size;
|
||||
+ dst += key->block_size;
|
||||
+ }
|
||||
+
|
||||
+ if (flags & BCRYPT_BLOCK_PADDING)
|
||||
+ {
|
||||
+ if (!(buf = HeapAlloc( GetProcessHeap(), 0, key->block_size ))) return STATUS_NO_MEMORY;
|
||||
+ status = key_decrypt( key, src, key->block_size, buf, key->block_size );
|
||||
+ if (!status && buf[ key->block_size - 1 ] <= key->block_size)
|
||||
+ {
|
||||
+ *ret_len -= buf[ key->block_size - 1 ];
|
||||
+ if (output_len < *ret_len) status = STATUS_BUFFER_TOO_SMALL;
|
||||
+ else memcpy( dst, buf, key->block_size - buf[ key->block_size - 1 ] );
|
||||
+ }
|
||||
+ else
|
||||
+ status = STATUS_UNSUCCESSFUL; /* FIXME: invalid padding */
|
||||
+ HeapFree( GetProcessHeap(), 0, buf );
|
||||
+ }
|
||||
+
|
||||
+ return status;
|
||||
}
|
||||
|
||||
BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
|
||||
diff --git a/dlls/bcrypt/tests/bcrypt.c b/dlls/bcrypt/tests/bcrypt.c
|
||||
index 64a4625..997b298 100644
|
||||
--- a/dlls/bcrypt/tests/bcrypt.c
|
||||
+++ b/dlls/bcrypt/tests/bcrypt.c
|
||||
@@ -1037,12 +1037,24 @@ static void test_BCryptDecrypt(void)
|
||||
{0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f};
|
||||
static UCHAR expected[] =
|
||||
{0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f};
|
||||
+ static UCHAR expected2[] =
|
||||
+ {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10};
|
||||
+ static UCHAR expected3[] =
|
||||
+ {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,
|
||||
+ 0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10};
|
||||
static UCHAR ciphertext[32] =
|
||||
{0xc6,0xa1,0x3b,0x37,0x87,0x8f,0x5b,0x82,0x6f,0x4f,0x81,0x62,0xa1,0xc8,0xd8,0x79,
|
||||
0x28,0x73,0x3d,0xef,0x84,0x8f,0xb0,0xa6,0x5d,0x1a,0x51,0xb7,0xec,0x8f,0xea,0xe9};
|
||||
+ static UCHAR ciphertext2[] =
|
||||
+ {0xc6,0xa1,0x3b,0x37,0x87,0x8f,0x5b,0x82,0x6f,0x4f,0x81,0x62,0xa1,0xc8,0xd8,0x79,
|
||||
+ 0x28,0x73,0x3d,0xef,0x84,0x8f,0xb0,0xa6,0x5d,0x1a,0x51,0xb7,0xec,0x8f,0xea,0xe9};
|
||||
+ static UCHAR ciphertext3[] =
|
||||
+ {0xc6,0xa1,0x3b,0x37,0x87,0x8f,0x5b,0x82,0x6f,0x4f,0x81,0x62,0xa1,0xc8,0xd8,0x79,
|
||||
+ 0xb1,0xa2,0x92,0x73,0xbe,0x2c,0x42,0x07,0xa5,0xac,0xe3,0x93,0x39,0x8c,0xb6,0xfb,
|
||||
+ 0x87,0x5d,0xea,0xa3,0x7e,0x0f,0xde,0xfa,0xd9,0xec,0x6c,0x4e,0x3c,0x76,0x86,0xe4};
|
||||
BCRYPT_ALG_HANDLE aes;
|
||||
BCRYPT_KEY_HANDLE key;
|
||||
- UCHAR *buf, plaintext[32], ivbuf[16];
|
||||
+ UCHAR *buf, plaintext[48], ivbuf[16];
|
||||
ULONG size, len;
|
||||
NTSTATUS ret;
|
||||
|
||||
@@ -1073,6 +1085,36 @@ static void test_BCryptDecrypt(void)
|
||||
ok(size == 32, "got %u\n", size);
|
||||
ok(!memcmp(plaintext, expected, sizeof(expected)), "wrong data\n");
|
||||
|
||||
+ /* test with padding smaller than block size */
|
||||
+ size = 0;
|
||||
+ memcpy(ivbuf, iv, sizeof(iv));
|
||||
+ ret = pBCryptDecrypt(key, ciphertext2, 32, NULL, ivbuf, 16, NULL, 0, &size, 0);
|
||||
+ ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
|
||||
+ ok(size == 32, "got %u\n", size);
|
||||
+
|
||||
+ size = 0;
|
||||
+ memcpy(ivbuf, iv, sizeof(iv));
|
||||
+ memset(plaintext, 0, sizeof(plaintext));
|
||||
+ ret = pBCryptDecrypt(key, ciphertext2, 32, NULL, ivbuf, 16, plaintext, 17, &size, BCRYPT_BLOCK_PADDING);
|
||||
+ ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
|
||||
+ ok(size == 17, "got %u\n", size);
|
||||
+ ok(!memcmp(plaintext, expected2, sizeof(expected2)), "wrong data\n");
|
||||
+
|
||||
+ /* test with padding of block size */
|
||||
+ size = 0;
|
||||
+ memcpy(ivbuf, iv, sizeof(iv));
|
||||
+ ret = pBCryptDecrypt(key, ciphertext3, 48, NULL, ivbuf, 16, NULL, 0, &size, 0);
|
||||
+ ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
|
||||
+ ok(size == 48, "got %u\n", size);
|
||||
+
|
||||
+ size = 0;
|
||||
+ memcpy(ivbuf, iv, sizeof(iv));
|
||||
+ memset(plaintext, 0, sizeof(plaintext));
|
||||
+ ret = pBCryptDecrypt(key, ciphertext3, 48, NULL, ivbuf, 16, plaintext, 32, &size, BCRYPT_BLOCK_PADDING);
|
||||
+ ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
|
||||
+ ok(size == 32, "got %u\n", size);
|
||||
+ ok(!memcmp(plaintext, expected3, sizeof(expected3)), "wrong data\n");
|
||||
+
|
||||
/* output size too small */
|
||||
size = 0;
|
||||
memcpy(ivbuf, iv, sizeof(iv));
|
||||
@@ -1080,6 +1122,24 @@ static void test_BCryptDecrypt(void)
|
||||
ok(ret == STATUS_BUFFER_TOO_SMALL, "got %08x\n", ret);
|
||||
ok(size == 32, "got %u\n", size);
|
||||
|
||||
+ size = 0;
|
||||
+ memcpy(ivbuf, iv, sizeof(iv));
|
||||
+ ret = pBCryptDecrypt(key, ciphertext2, 32, NULL, ivbuf, 16, plaintext, 15, &size, BCRYPT_BLOCK_PADDING);
|
||||
+ ok(ret == STATUS_BUFFER_TOO_SMALL, "got %08x\n", ret);
|
||||
+ ok(size == 32, "got %u\n", size);
|
||||
+
|
||||
+ size = 0;
|
||||
+ memcpy(ivbuf, iv, sizeof(iv));
|
||||
+ ret = pBCryptDecrypt(key, ciphertext2, 32, NULL, ivbuf, 16, plaintext, 16, &size, BCRYPT_BLOCK_PADDING);
|
||||
+ ok(ret == STATUS_BUFFER_TOO_SMALL, "got %08x\n", ret);
|
||||
+ ok(size == 17, "got %u\n", size);
|
||||
+
|
||||
+ size = 0;
|
||||
+ memcpy(ivbuf, iv, sizeof(iv));
|
||||
+ ret = pBCryptDecrypt(key, ciphertext3, 48, NULL, ivbuf, 16, plaintext, 31, &size, BCRYPT_BLOCK_PADDING);
|
||||
+ ok(ret == STATUS_BUFFER_TOO_SMALL, "got %08x\n", ret);
|
||||
+ ok(size == 48, "got %u\n", size);
|
||||
+
|
||||
/* input size is not a multiple of block size */
|
||||
size = 0;
|
||||
memcpy(ivbuf, iv, sizeof(iv));
|
||||
--
|
||||
2.9.0
|
||||
|
@ -2874,12 +2874,16 @@ if test "$enable_bcrypt_Improvements" -eq 1; then
|
||||
patch_apply bcrypt-Improvements/0003-bcrypt-Add-internal-fallback-implementation-for-hash.patch
|
||||
patch_apply bcrypt-Improvements/0004-bcrypt-Use-hash-fallback-implementation-as-default-a.patch
|
||||
patch_apply bcrypt-Improvements/0005-bcrypt-Implement-BCryptDuplicateHash.patch
|
||||
patch_apply bcrypt-Improvements/0006-bcrypt-Fix-handling-of-padding-when-input-size-equal.patch
|
||||
patch_apply bcrypt-Improvements/0007-bcrypt-Properly-handle-padding-in-AES-decryption.patch
|
||||
(
|
||||
echo '+ { "Hans Leidekker", "bcrypt: Add AES provider.", 1 },';
|
||||
echo '+ { "Michael Müller", "bcrypt: Directly implement hmac computation.", 1 },';
|
||||
echo '+ { "Michael Müller", "bcrypt: Add internal fallback implementation for hash calculations.", 1 },';
|
||||
echo '+ { "Michael Müller", "bcrypt: Use hash fallback implementation as default and remove gnutls / commoncrypto hash implemetation.", 1 },';
|
||||
echo '+ { "Michael Müller", "bcrypt: Implement BCryptDuplicateHash.", 1 },';
|
||||
echo '+ { "Michael Müller", "bcrypt: Fix handling of padding when input size equals block size for AES.", 1 },';
|
||||
echo '+ { "Michael Müller", "bcrypt: Properly handle padding in AES decryption.", 1 },';
|
||||
) >> "$patchlist"
|
||||
fi
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user