bcrypt: Implement BCryptDecrypt.
Partially based on a patch by Michael Müller. Signed-off-by: Hans Leidekker <hans@codeweavers.com> Signed-off-by: Alexandre Julliard <julliard@winehq.org>
This commit is contained in:
parent
7f8dd873c8
commit
024a8a0be2
|
@ -51,9 +51,10 @@ WINE_DECLARE_DEBUG_CHANNEL(winediag);
|
|||
|
||||
static void *libgnutls_handle;
|
||||
#define MAKE_FUNCPTR(f) static typeof(f) * p##f
|
||||
MAKE_FUNCPTR(gnutls_cipher_init);
|
||||
MAKE_FUNCPTR(gnutls_cipher_decrypt2);
|
||||
MAKE_FUNCPTR(gnutls_cipher_deinit);
|
||||
MAKE_FUNCPTR(gnutls_cipher_encrypt2);
|
||||
MAKE_FUNCPTR(gnutls_cipher_init);
|
||||
MAKE_FUNCPTR(gnutls_global_deinit);
|
||||
MAKE_FUNCPTR(gnutls_global_init);
|
||||
MAKE_FUNCPTR(gnutls_global_set_log_function);
|
||||
|
@ -83,9 +84,10 @@ static BOOL gnutls_initialize(void)
|
|||
goto fail; \
|
||||
}
|
||||
|
||||
LOAD_FUNCPTR(gnutls_cipher_init)
|
||||
LOAD_FUNCPTR(gnutls_cipher_decrypt2)
|
||||
LOAD_FUNCPTR(gnutls_cipher_deinit)
|
||||
LOAD_FUNCPTR(gnutls_cipher_encrypt2)
|
||||
LOAD_FUNCPTR(gnutls_cipher_init)
|
||||
LOAD_FUNCPTR(gnutls_global_deinit)
|
||||
LOAD_FUNCPTR(gnutls_global_init)
|
||||
LOAD_FUNCPTR(gnutls_global_set_log_function)
|
||||
|
@ -789,6 +791,20 @@ static NTSTATUS key_encrypt( struct key *key, const UCHAR *input, ULONG input_le
|
|||
return STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
static NTSTATUS key_decrypt( struct key *key, const UCHAR *input, ULONG input_len, UCHAR *output,
|
||||
ULONG output_len )
|
||||
{
|
||||
int ret;
|
||||
|
||||
if ((ret = pgnutls_cipher_decrypt2( key->handle, input, input_len, output, output_len )))
|
||||
{
|
||||
pgnutls_perror( ret );
|
||||
return STATUS_INTERNAL_ERROR;
|
||||
}
|
||||
|
||||
return STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
static NTSTATUS key_destroy( struct key *key )
|
||||
{
|
||||
if (key->handle) pgnutls_cipher_deinit( key->handle );
|
||||
|
@ -822,6 +838,13 @@ static NTSTATUS key_encrypt( struct key *key, const UCHAR *input, ULONG input_le
|
|||
return STATUS_NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
static NTSTATUS key_decrypt( struct key *key, const UCHAR *input, ULONG input_len, UCHAR *output,
|
||||
ULONG output_len )
|
||||
{
|
||||
ERR( "support for keys not available at build time\n" );
|
||||
return STATUS_NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
static NTSTATUS key_destroy( struct key *key )
|
||||
{
|
||||
ERR( "support for keys not available at build time\n" );
|
||||
|
@ -927,9 +950,67 @@ NTSTATUS WINAPI BCryptDecrypt( BCRYPT_KEY_HANDLE handle, UCHAR *input, ULONG inp
|
|||
void *padding, UCHAR *iv, ULONG iv_len, UCHAR *output,
|
||||
ULONG output_len, ULONG *ret_len, ULONG flags )
|
||||
{
|
||||
FIXME( "%p, %p, %u, %p, %p, %u, %p, %u, %p, %08x\n", handle, input, input_len,
|
||||
struct key *key = handle;
|
||||
ULONG bytes_left = input_len;
|
||||
UCHAR *buf, *src, *dst;
|
||||
NTSTATUS status;
|
||||
|
||||
TRACE( "%p, %p, %u, %p, %p, %u, %p, %u, %p, %08x\n", handle, input, input_len,
|
||||
padding, iv, iv_len, output, output_len, ret_len, flags );
|
||||
return STATUS_NOT_IMPLEMENTED;
|
||||
|
||||
if (!key || key->hdr.magic != MAGIC_KEY) return STATUS_INVALID_HANDLE;
|
||||
if (padding)
|
||||
{
|
||||
FIXME( "padding info not implemented\n" );
|
||||
return STATUS_NOT_IMPLEMENTED;
|
||||
}
|
||||
if (flags & ~BCRYPT_BLOCK_PADDING)
|
||||
{
|
||||
FIXME( "flags %08x not supported\n", flags );
|
||||
return STATUS_NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
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 (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;
|
||||
|
||||
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 )
|
||||
|
|
|
@ -795,11 +795,6 @@ static void test_BCryptGenerateSymmetricKey(void)
|
|||
|
||||
size = 0xdeadbeef;
|
||||
ret = pBCryptDecrypt(key, NULL, 0, NULL, NULL, 0, NULL, 0, &size, 0);
|
||||
if (ret == STATUS_NOT_IMPLEMENTED) /* remove whole IF when Wine is fixed */
|
||||
{
|
||||
todo_wine ok(0, "BCryptDecrypt not implemented\n");
|
||||
return;
|
||||
}
|
||||
ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
|
||||
ok(!size, "got %u\n", size);
|
||||
|
||||
|
@ -952,12 +947,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;
|
||||
|
||||
|
@ -977,11 +984,6 @@ static void test_BCryptDecrypt(void)
|
|||
size = 0;
|
||||
memcpy(ivbuf, iv, sizeof(iv));
|
||||
ret = pBCryptDecrypt(key, ciphertext, 32, NULL, ivbuf, 16, NULL, 0, &size, 0);
|
||||
if (ret == STATUS_NOT_IMPLEMENTED) /* remove whole IF when Wine is fixed */
|
||||
{
|
||||
todo_wine ok(0, "BCryptDecrypt not implemented\n");
|
||||
return;
|
||||
}
|
||||
ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
|
||||
ok(size == 32, "got %u\n", size);
|
||||
|
||||
|
@ -993,6 +995,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));
|
||||
|
@ -1000,6 +1032,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));
|
||||
|
|
Loading…
Reference in New Issue