2014-02-03 19:24:48 +01:00
|
|
|
/*
|
|
|
|
* Unit test for bcrypt functions
|
|
|
|
*
|
|
|
|
* Copyright 2014 Bruno Jesus
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2016-01-13 10:43:37 +01:00
|
|
|
#include <stdio.h>
|
2014-02-03 19:24:48 +01:00
|
|
|
#include <ntstatus.h>
|
|
|
|
#define WIN32_NO_STATUS
|
|
|
|
#include <windows.h>
|
|
|
|
#include <bcrypt.h>
|
2019-06-17 11:52:48 +02:00
|
|
|
#include <ncrypt.h>
|
2014-02-03 19:24:48 +01:00
|
|
|
|
|
|
|
#include "wine/test.h"
|
|
|
|
|
2019-02-13 10:21:15 +01:00
|
|
|
static NTSTATUS (WINAPI *pBCryptHash)(BCRYPT_ALG_HANDLE, UCHAR *, ULONG, UCHAR *, ULONG, UCHAR *, ULONG);
|
2016-03-24 06:22:22 +01:00
|
|
|
|
2014-02-03 19:24:48 +01:00
|
|
|
static void test_BCryptGenRandom(void)
|
|
|
|
{
|
|
|
|
NTSTATUS ret;
|
|
|
|
UCHAR buffer[256];
|
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptGenRandom(NULL, NULL, 0, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_INVALID_HANDLE, "Expected STATUS_INVALID_HANDLE, got %#lx\n", ret);
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptGenRandom(NULL, buffer, 0, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_INVALID_HANDLE, "Expected STATUS_INVALID_HANDLE, got %#lx\n", ret);
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptGenRandom(NULL, buffer, sizeof(buffer), 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_INVALID_HANDLE, "Expected STATUS_INVALID_HANDLE, got %#lx\n", ret);
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptGenRandom(NULL, buffer, sizeof(buffer), BCRYPT_USE_SYSTEM_PREFERRED_RNG);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "Expected success, got %#lx\n", ret);
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptGenRandom(NULL, buffer, sizeof(buffer),
|
2014-02-10 23:12:15 +01:00
|
|
|
BCRYPT_USE_SYSTEM_PREFERRED_RNG|BCRYPT_RNG_USE_ENTROPY_IN_BUFFER);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "Expected success, got %#lx\n", ret);
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptGenRandom(NULL, NULL, sizeof(buffer), BCRYPT_USE_SYSTEM_PREFERRED_RNG);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_INVALID_PARAMETER, "Expected STATUS_INVALID_PARAMETER, got %#lx\n", ret);
|
2014-02-10 23:12:15 +01:00
|
|
|
|
|
|
|
/* Zero sized buffer should work too */
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptGenRandom(NULL, buffer, 0, BCRYPT_USE_SYSTEM_PREFERRED_RNG);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "Expected success, got %#lx\n", ret);
|
2014-02-10 23:12:15 +01:00
|
|
|
|
|
|
|
/* Test random number generation - It's impossible for a sane RNG to return 8 zeros */
|
|
|
|
memset(buffer, 0, 16);
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptGenRandom(NULL, buffer, 8, BCRYPT_USE_SYSTEM_PREFERRED_RNG);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "Expected success, got %#lx\n", ret);
|
2014-02-10 23:12:15 +01:00
|
|
|
ok(memcmp(buffer, buffer + 8, 8), "Expected a random number, got 0\n");
|
2014-02-03 19:24:48 +01:00
|
|
|
}
|
|
|
|
|
2014-08-16 00:18:06 +02:00
|
|
|
static void test_BCryptGetFipsAlgorithmMode(void)
|
|
|
|
{
|
2016-12-02 21:36:39 +01:00
|
|
|
HKEY hkey = NULL;
|
2016-09-09 00:24:19 +02:00
|
|
|
BOOLEAN expected;
|
2014-08-16 00:18:06 +02:00
|
|
|
BOOLEAN enabled;
|
2016-09-09 00:24:19 +02:00
|
|
|
DWORD value, count[2] = {sizeof(value), sizeof(value)};
|
|
|
|
NTSTATUS ret;
|
|
|
|
|
2020-10-07 23:34:28 +02:00
|
|
|
if (RegOpenKeyW(HKEY_LOCAL_MACHINE,
|
|
|
|
L"System\\CurrentControlSet\\Control\\Lsa\\FIPSAlgorithmPolicy", &hkey) == ERROR_SUCCESS &&
|
|
|
|
RegQueryValueExW(hkey, L"Enabled", NULL, NULL, (void *)&value, &count[0]) == ERROR_SUCCESS)
|
2016-09-09 00:24:19 +02:00
|
|
|
{
|
|
|
|
expected = !!value;
|
|
|
|
}
|
2020-10-07 23:34:28 +02:00
|
|
|
else if (RegOpenKeyW(HKEY_LOCAL_MACHINE, L"System\\CurrentControlSet\\Control\\Lsa", &hkey) == ERROR_SUCCESS &&
|
|
|
|
RegQueryValueExW(hkey, L"FIPSAlgorithmPolicy", NULL, NULL, (void *)&value, &count[0]) == ERROR_SUCCESS)
|
2016-09-09 00:24:19 +02:00
|
|
|
{
|
|
|
|
expected = !!value;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
expected = FALSE;
|
2022-02-21 08:07:51 +01:00
|
|
|
todo_wine
|
2016-09-09 00:24:19 +02:00
|
|
|
ok(0, "Neither XP or Vista key is present\n");
|
|
|
|
}
|
2016-12-02 21:36:39 +01:00
|
|
|
RegCloseKey(hkey);
|
2014-08-16 00:18:06 +02:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptGetFipsAlgorithmMode(&enabled);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %#lx\n", ret);
|
2016-09-09 00:24:19 +02:00
|
|
|
ok(enabled == expected, "expected result %d, got %d\n", expected, enabled);
|
2014-08-16 00:18:06 +02:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptGetFipsAlgorithmMode(NULL);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_INVALID_PARAMETER, "Expected STATUS_INVALID_PARAMETER, got %#lx\n", ret);
|
2014-08-16 00:18:06 +02:00
|
|
|
}
|
|
|
|
|
2016-01-26 10:30:02 +01:00
|
|
|
static void format_hash(const UCHAR *bytes, ULONG size, char *buf)
|
2016-01-13 10:43:37 +01:00
|
|
|
{
|
|
|
|
ULONG i;
|
2016-01-26 10:30:02 +01:00
|
|
|
buf[0] = '\0';
|
2016-01-13 10:43:37 +01:00
|
|
|
for (i = 0; i < size; i++)
|
|
|
|
{
|
2016-01-26 10:30:02 +01:00
|
|
|
sprintf(buf + i * 2, "%02x", bytes[i]);
|
2016-01-13 10:43:37 +01:00
|
|
|
}
|
2016-01-26 10:30:02 +01:00
|
|
|
return;
|
2016-01-13 10:43:37 +01:00
|
|
|
}
|
|
|
|
|
2017-03-10 15:34:39 +01:00
|
|
|
#define test_object_length(a) _test_object_length(__LINE__,a)
|
|
|
|
static void _test_object_length(unsigned line, void *handle)
|
|
|
|
{
|
|
|
|
NTSTATUS status;
|
|
|
|
ULONG len, size;
|
|
|
|
|
|
|
|
len = size = 0xdeadbeef;
|
2021-12-03 15:49:30 +01:00
|
|
|
status = BCryptGetProperty(NULL, BCRYPT_OBJECT_LENGTH, (UCHAR *)&len, sizeof(len), &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok_(__FILE__,line)(status == STATUS_INVALID_HANDLE, "BCryptGetProperty failed: %#lx\n", status);
|
2017-03-10 15:34:39 +01:00
|
|
|
|
|
|
|
len = size = 0xdeadbeef;
|
2021-12-03 15:49:30 +01:00
|
|
|
status = BCryptGetProperty(handle, NULL, (UCHAR *)&len, sizeof(len), &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok_(__FILE__,line)(status == STATUS_INVALID_PARAMETER, "BCryptGetProperty failed: %#lx\n", status);
|
2017-03-10 15:34:39 +01:00
|
|
|
|
|
|
|
len = size = 0xdeadbeef;
|
2021-12-03 15:49:30 +01:00
|
|
|
status = BCryptGetProperty(handle, BCRYPT_OBJECT_LENGTH, (UCHAR *)&len, sizeof(len), NULL, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok_(__FILE__,line)(status == STATUS_INVALID_PARAMETER, "BCryptGetProperty failed: %#lx\n", status);
|
2017-03-10 15:34:39 +01:00
|
|
|
|
|
|
|
len = size = 0xdeadbeef;
|
2021-12-03 15:49:30 +01:00
|
|
|
status = BCryptGetProperty(handle, BCRYPT_OBJECT_LENGTH, NULL, sizeof(len), &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok_(__FILE__,line)(status == STATUS_SUCCESS, "BCryptGetProperty failed: %#lx\n", status);
|
|
|
|
ok_(__FILE__,line)(size == sizeof(len), "got %lu\n", size);
|
2017-03-10 15:34:39 +01:00
|
|
|
|
|
|
|
len = size = 0xdeadbeef;
|
2021-12-03 15:49:30 +01:00
|
|
|
status = BCryptGetProperty(handle, BCRYPT_OBJECT_LENGTH, (UCHAR *)&len, 0, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok_(__FILE__,line)(status == STATUS_BUFFER_TOO_SMALL, "BCryptGetProperty failed: %#lx\n", status);
|
|
|
|
ok_(__FILE__,line)(len == 0xdeadbeef, "got %lu\n", len);
|
|
|
|
ok_(__FILE__,line)(size == sizeof(len), "got %lu\n", size);
|
2017-03-10 15:34:39 +01:00
|
|
|
|
|
|
|
len = size = 0xdeadbeef;
|
2021-12-03 15:49:30 +01:00
|
|
|
status = BCryptGetProperty(handle, BCRYPT_OBJECT_LENGTH, (UCHAR *)&len, sizeof(len), &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok_(__FILE__,line)(status == STATUS_SUCCESS, "BCryptGetProperty failed: %#lx\n", status);
|
2017-03-10 15:34:39 +01:00
|
|
|
ok_(__FILE__,line)(len != 0xdeadbeef, "len not set\n");
|
2022-02-09 10:57:53 +01:00
|
|
|
ok_(__FILE__,line)(size == sizeof(len), "got %lu\n", size);
|
2017-03-10 15:34:39 +01:00
|
|
|
}
|
|
|
|
|
2016-01-29 14:25:11 +01:00
|
|
|
#define test_hash_length(a,b) _test_hash_length(__LINE__,a,b)
|
|
|
|
static void _test_hash_length(unsigned line, void *handle, ULONG exlen)
|
|
|
|
{
|
|
|
|
ULONG len = 0xdeadbeef, size = 0xdeadbeef;
|
|
|
|
NTSTATUS status;
|
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
status = BCryptGetProperty(handle, BCRYPT_HASH_LENGTH, (UCHAR *)&len, sizeof(len), &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok_(__FILE__,line)(status == STATUS_SUCCESS, "BCryptGetProperty failed: %#lx\n", status);
|
|
|
|
ok_(__FILE__,line)(size == sizeof(len), "got %lu\n", size);
|
|
|
|
ok_(__FILE__,line)(len == exlen, "len = %lu, expected %lu\n", len, exlen);
|
2016-01-29 14:25:11 +01:00
|
|
|
}
|
|
|
|
|
2016-01-29 14:25:30 +01:00
|
|
|
#define test_alg_name(a,b) _test_alg_name(__LINE__,a,b)
|
2020-02-04 21:44:10 +01:00
|
|
|
static void _test_alg_name(unsigned line, void *handle, const WCHAR *exname)
|
2016-01-29 14:25:30 +01:00
|
|
|
{
|
|
|
|
ULONG size = 0xdeadbeef;
|
|
|
|
UCHAR buf[256];
|
|
|
|
const WCHAR *name = (const WCHAR*)buf;
|
|
|
|
NTSTATUS status;
|
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
status = BCryptGetProperty(handle, BCRYPT_ALGORITHM_NAME, buf, sizeof(buf), &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok_(__FILE__,line)(status == STATUS_SUCCESS, "BCryptGetProperty failed: %#lx\n", status);
|
|
|
|
ok_(__FILE__,line)(size == (lstrlenW(exname) + 1) * sizeof(WCHAR), "got %lu\n", size);
|
2020-02-04 21:44:10 +01:00
|
|
|
ok_(__FILE__,line)(!lstrcmpW(name, exname), "alg name = %s, expected %s\n", wine_dbgstr_w(name),
|
|
|
|
wine_dbgstr_w(exname));
|
2016-01-29 14:25:30 +01:00
|
|
|
}
|
|
|
|
|
2017-12-07 19:45:43 +01:00
|
|
|
struct hash_test
|
2016-01-13 10:43:37 +01:00
|
|
|
{
|
2020-02-04 21:44:10 +01:00
|
|
|
const WCHAR *alg;
|
2017-12-07 19:45:43 +01:00
|
|
|
unsigned hash_size;
|
|
|
|
const char *hash;
|
2019-03-06 10:14:28 +01:00
|
|
|
const char *hash2;
|
2017-12-07 19:45:43 +01:00
|
|
|
const char *hmac_hash;
|
2019-03-06 10:14:28 +01:00
|
|
|
const char *hmac_hash2;
|
2017-12-07 19:45:43 +01:00
|
|
|
};
|
2016-01-20 14:51:44 +01:00
|
|
|
|
2017-12-07 19:45:43 +01:00
|
|
|
static void test_hash(const struct hash_test *test)
|
2016-01-13 10:43:37 +01:00
|
|
|
{
|
|
|
|
BCRYPT_ALG_HANDLE alg;
|
|
|
|
BCRYPT_HASH_HANDLE hash;
|
2017-12-07 19:45:43 +01:00
|
|
|
UCHAR buf[512], buf_hmac[1024], hash_buf[128], hmac_hash[128];
|
|
|
|
char str[512];
|
2016-01-13 10:43:37 +01:00
|
|
|
NTSTATUS ret;
|
2017-03-10 15:34:39 +01:00
|
|
|
ULONG len;
|
2016-01-13 10:43:37 +01:00
|
|
|
|
|
|
|
alg = NULL;
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptOpenAlgorithmProvider(&alg, test->alg, MS_PRIMITIVE_PROVIDER, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2016-01-24 19:50:49 +01:00
|
|
|
ok(alg != NULL, "alg not set\n");
|
2016-01-13 10:43:37 +01:00
|
|
|
|
2017-03-10 15:34:39 +01:00
|
|
|
test_object_length(alg);
|
2017-12-07 19:45:43 +01:00
|
|
|
test_hash_length(alg, test->hash_size);
|
|
|
|
test_alg_name(alg, test->alg);
|
2016-01-20 14:51:44 +01:00
|
|
|
|
2016-01-13 10:43:37 +01:00
|
|
|
hash = NULL;
|
|
|
|
len = sizeof(buf);
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptCreateHash(alg, &hash, buf, len, NULL, 0, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2016-01-13 10:43:37 +01:00
|
|
|
ok(hash != NULL, "hash not set\n");
|
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptHashData(hash, NULL, 0, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2016-03-09 05:42:45 +01:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptHashData(hash, (UCHAR *)"test", sizeof("test"), 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2016-01-13 10:43:37 +01:00
|
|
|
|
2017-12-07 19:45:43 +01:00
|
|
|
test_hash_length(hash, test->hash_size);
|
|
|
|
test_alg_name(hash, test->alg);
|
2016-01-13 10:43:37 +01:00
|
|
|
|
2017-12-07 19:45:43 +01:00
|
|
|
memset(hash_buf, 0, sizeof(hash_buf));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptFinishHash(hash, hash_buf, test->hash_size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2017-12-07 19:45:43 +01:00
|
|
|
format_hash( hash_buf, test->hash_size, str );
|
|
|
|
ok(!strcmp(str, test->hash), "got %s\n", str);
|
2016-01-13 10:43:37 +01:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDestroyHash(hash);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2016-01-13 10:43:37 +01:00
|
|
|
|
2019-03-06 10:14:28 +01:00
|
|
|
hash = NULL;
|
|
|
|
len = sizeof(buf);
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptCreateHash(alg, &hash, buf, len, NULL, 0, BCRYPT_HASH_REUSABLE_FLAG);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS || broken(ret == STATUS_INVALID_PARAMETER) /* < win8 */, "got %#lx\n", ret);
|
2019-03-06 10:14:28 +01:00
|
|
|
if (ret == STATUS_SUCCESS)
|
|
|
|
{
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptHashData(hash, (UCHAR *)"test", sizeof("test"), 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2019-03-06 10:14:28 +01:00
|
|
|
|
|
|
|
memset(hash_buf, 0, sizeof(hash_buf));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptFinishHash(hash, hash_buf, test->hash_size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2019-03-06 10:14:28 +01:00
|
|
|
format_hash( hash_buf, test->hash_size, str );
|
|
|
|
ok(!strcmp(str, test->hash), "got %s\n", str);
|
|
|
|
|
|
|
|
/* reuse it */
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptHashData(hash, (UCHAR *)"tset", sizeof("tset"), 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2019-03-06 10:14:28 +01:00
|
|
|
|
|
|
|
memset(hash_buf, 0, sizeof(hash_buf));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptFinishHash(hash, hash_buf, test->hash_size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2019-03-06 10:14:28 +01:00
|
|
|
format_hash( hash_buf, test->hash_size, str );
|
|
|
|
ok(!strcmp(str, test->hash2), "got %s\n", str);
|
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDestroyHash(hash);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2019-03-06 10:14:28 +01:00
|
|
|
}
|
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptCloseAlgorithmProvider(alg, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2016-03-11 04:33:40 +01:00
|
|
|
|
|
|
|
alg = NULL;
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptOpenAlgorithmProvider(&alg, test->alg, MS_PRIMITIVE_PROVIDER, BCRYPT_ALG_HANDLE_HMAC_FLAG);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2016-03-11 04:33:40 +01:00
|
|
|
ok(alg != NULL, "alg not set\n");
|
|
|
|
|
|
|
|
hash = NULL;
|
|
|
|
len = sizeof(buf_hmac);
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptCreateHash(alg, &hash, buf_hmac, len, (UCHAR *)"key", sizeof("key"), 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2016-03-11 04:33:40 +01:00
|
|
|
ok(hash != NULL, "hash not set\n");
|
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptHashData(hash, (UCHAR *)"test", sizeof("test"), 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2016-03-11 04:33:40 +01:00
|
|
|
|
2017-12-07 19:45:43 +01:00
|
|
|
test_hash_length(hash, test->hash_size);
|
|
|
|
test_alg_name(hash, test->alg);
|
2016-03-11 04:33:40 +01:00
|
|
|
|
2017-12-07 19:45:43 +01:00
|
|
|
memset(hmac_hash, 0, sizeof(hmac_hash));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptFinishHash(hash, hmac_hash, test->hash_size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2017-12-07 19:45:43 +01:00
|
|
|
format_hash( hmac_hash, test->hash_size, str );
|
|
|
|
ok(!strcmp(str, test->hmac_hash), "got %s\n", str);
|
2016-03-11 04:33:40 +01:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDestroyHash(hash);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2016-03-11 04:33:40 +01:00
|
|
|
|
2019-03-06 10:14:28 +01:00
|
|
|
hash = NULL;
|
|
|
|
len = sizeof(buf_hmac);
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptCreateHash(alg, &hash, buf_hmac, len, (UCHAR *)"key", sizeof("key"), BCRYPT_HASH_REUSABLE_FLAG);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS || broken(ret == STATUS_INVALID_PARAMETER) /* < win8 */, "got %#lx\n", ret);
|
2019-03-06 10:14:28 +01:00
|
|
|
if (ret == STATUS_SUCCESS)
|
|
|
|
{
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptHashData(hash, (UCHAR *)"test", sizeof("test"), 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2019-03-06 10:14:28 +01:00
|
|
|
|
|
|
|
memset(hmac_hash, 0, sizeof(hmac_hash));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptFinishHash(hash, hmac_hash, test->hash_size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2019-03-06 10:14:28 +01:00
|
|
|
format_hash( hmac_hash, test->hash_size, str );
|
|
|
|
ok(!strcmp(str, test->hmac_hash), "got %s\n", str);
|
|
|
|
|
|
|
|
/* reuse it */
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptHashData(hash, (UCHAR *)"tset", sizeof("tset"), 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2019-03-06 10:14:28 +01:00
|
|
|
|
|
|
|
memset(hmac_hash, 0, sizeof(hmac_hash));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptFinishHash(hash, hmac_hash, test->hash_size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2019-03-06 10:14:28 +01:00
|
|
|
format_hash( hmac_hash, test->hash_size, str );
|
|
|
|
ok(!strcmp(str, test->hmac_hash2), "got %s\n", str);
|
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDestroyHash(hash);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2019-03-06 10:14:28 +01:00
|
|
|
}
|
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDestroyHash(hash);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_INVALID_PARAMETER, "got %#lx\n", ret);
|
2018-12-17 09:47:58 +01:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDestroyHash(NULL);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_INVALID_PARAMETER, "got %#lx\n", ret);
|
2018-12-17 09:47:58 +01:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptCloseAlgorithmProvider(alg, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2016-01-13 10:43:37 +01:00
|
|
|
}
|
|
|
|
|
2017-12-07 19:45:43 +01:00
|
|
|
static void test_hashes(void)
|
2016-01-13 10:43:37 +01:00
|
|
|
{
|
2019-03-06 10:14:28 +01:00
|
|
|
static const struct hash_test tests[] =
|
|
|
|
{
|
2020-02-04 21:44:10 +01:00
|
|
|
{ L"SHA1", 20,
|
2019-03-06 10:14:28 +01:00
|
|
|
"961fa64958818f767707072755d7018dcd278e94",
|
|
|
|
"9314f62ff64197143c91fc86de37e9ae776a3fb8",
|
|
|
|
"2472cf65d0e090618d769d3e46f0d9446cf212da",
|
|
|
|
"b2d2ba8cfd714d474cf0d9622cc5d15e1f53d53f",
|
2017-12-07 19:45:43 +01:00
|
|
|
},
|
2020-02-04 21:44:10 +01:00
|
|
|
{ L"SHA256", 32,
|
2019-03-06 10:14:28 +01:00
|
|
|
"ceb73749c899693706ede1e30c9929b3fd5dd926163831c2fb8bd41e6efb1126",
|
|
|
|
"ea0938c118a7b15954f41b85195f2b42aec3a9429c63f593cfa65c137ffaa986",
|
|
|
|
"34c1aa473a4468a91d06e7cdbc75bc4f93b830ccfc2a47ffd74e8e6ed29e4c72",
|
|
|
|
"55feb7052060bd99e33f36eb0982c7f4856eb6a84fbefe19a1afd9faafc3af6f",
|
2017-12-07 19:45:43 +01:00
|
|
|
},
|
2020-02-04 21:44:10 +01:00
|
|
|
{ L"SHA384", 48,
|
2019-03-06 10:14:28 +01:00
|
|
|
"62b21e90c9022b101671ba1f808f8631a8149f0f12904055839a35c1ca78ae53"
|
|
|
|
"63eed1e743a692d70e0504b0cfd12ef9",
|
|
|
|
"724db7c0bbc51ef1ac3fc793083fc54c0e5c423faec9b11378c01c236b19aaaf"
|
|
|
|
"a45177ad055feaf003968cc40ece44c7",
|
|
|
|
"4b3e6d6ff2da121790ab7e7b9247583e3a7eed2db5bd4dabc680303b1608f37d"
|
|
|
|
"fdc836d96a704c03283bc05b4f6c5eb8",
|
|
|
|
"03e1818e5c165a0e54619e513acb06c393e1a6cb0ddbb4036b5f29617b334642"
|
|
|
|
"e6e0be8b214d8508595b17a8c4b4e7db",
|
2017-12-07 19:45:43 +01:00
|
|
|
},
|
2020-02-04 21:44:10 +01:00
|
|
|
{ L"SHA512", 64,
|
2019-03-06 10:14:28 +01:00
|
|
|
"d55ced17163bf5386f2cd9ff21d6fd7fe576a915065c24744d09cfae4ec84ee1"
|
|
|
|
"ef6ef11bfbc5acce3639bab725b50a1fe2c204f8c820d6d7db0df0ecbc49c5ca",
|
|
|
|
"7752d707b54d2b00e7d1c09120d189475b0fd2e31ebb988cf0a01fc8492ddc0b"
|
|
|
|
"3ca9c9ca61d9d7d1fb65ca7665e87f043c1d5bc9f786f8345e951c2d91ac594f",
|
|
|
|
"415fb6b10018ca03b38a1b1399c42ac0be5e8aceddb9a73103f5e543bf2d888f"
|
|
|
|
"2eecf91373941f9315dd730a77937fa92444450fbece86f409d9cb5ec48c6513",
|
|
|
|
"1487bcecba46ae677622fa499e4cb2f0fdf92f6f3427cba76382d537a06e49c3"
|
|
|
|
"3e70a2fc1fc730092bf21128c3704cc6387f6dfbf7e2f9f315bbb894505a1205",
|
2017-12-07 19:45:43 +01:00
|
|
|
},
|
2020-02-04 21:44:10 +01:00
|
|
|
{ L"MD2", 16,
|
2019-03-06 10:14:28 +01:00
|
|
|
"1bb33606ba908912a84221109d29cd7e",
|
|
|
|
"b9a6ad9323b17e2d0cd389dddd6ef78a",
|
|
|
|
"7f05b0638d77f4a27f3a9c4d353cd648",
|
|
|
|
"05980873e6bfdd05dd7b30078de7e42a",
|
2017-12-07 19:46:20 +01:00
|
|
|
},
|
2020-02-04 21:44:10 +01:00
|
|
|
{ L"MD4", 16,
|
2019-03-06 10:14:28 +01:00
|
|
|
"74b5db93c0b41e36ca7074338fc0b637",
|
|
|
|
"a14a9ff2059a8c28f47b01e6bc48a1bf",
|
|
|
|
"bc2e8ac4d8248ed21b8d26227a30ea3a",
|
|
|
|
"b609db0eb4b8669db74f2c20099701e4",
|
2017-12-07 19:46:58 +01:00
|
|
|
},
|
2020-02-04 21:44:10 +01:00
|
|
|
{ L"MD5", 16,
|
2019-03-06 10:14:28 +01:00
|
|
|
"e2a3e68d23ce348b8f68b3079de3d4c9",
|
|
|
|
"bcdd7ca574342aa9db0e212348eacb16",
|
|
|
|
"7bda029b93fa8d817fcc9e13d6bdf092",
|
|
|
|
"dd636ab8e9592c5088e57c37d44c5bb3",
|
2017-12-07 19:45:43 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
unsigned i;
|
|
|
|
|
2018-05-29 01:05:21 +02:00
|
|
|
for(i = 0; i < ARRAY_SIZE(tests); i++)
|
2017-12-07 19:45:43 +01:00
|
|
|
test_hash(tests+i);
|
2016-03-08 04:45:27 +01:00
|
|
|
}
|
|
|
|
|
2016-03-24 06:22:22 +01:00
|
|
|
static void test_BcryptHash(void)
|
|
|
|
{
|
|
|
|
static const char expected[] =
|
|
|
|
"e2a3e68d23ce348b8f68b3079de3d4c9";
|
|
|
|
static const char expected_hmac[] =
|
|
|
|
"7bda029b93fa8d817fcc9e13d6bdf092";
|
|
|
|
BCRYPT_ALG_HANDLE alg;
|
|
|
|
UCHAR md5[16], md5_hmac[16];
|
|
|
|
char str[65];
|
|
|
|
NTSTATUS ret;
|
|
|
|
|
2019-02-13 10:21:18 +01:00
|
|
|
if (!pBCryptHash) /* < Win10 */
|
|
|
|
{
|
|
|
|
win_skip("BCryptHash is not available\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-24 06:22:22 +01:00
|
|
|
alg = NULL;
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptOpenAlgorithmProvider(&alg, BCRYPT_MD5_ALGORITHM, MS_PRIMITIVE_PROVIDER, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2016-03-24 06:22:22 +01:00
|
|
|
ok(alg != NULL, "alg not set\n");
|
|
|
|
|
|
|
|
test_hash_length(alg, 16);
|
2020-02-04 21:44:10 +01:00
|
|
|
test_alg_name(alg, L"MD5");
|
2016-03-24 06:22:22 +01:00
|
|
|
|
|
|
|
memset(md5, 0, sizeof(md5));
|
|
|
|
ret = pBCryptHash(alg, NULL, 0, (UCHAR *)"test", sizeof("test"), md5, sizeof(md5));
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2016-03-24 06:22:22 +01:00
|
|
|
format_hash( md5, sizeof(md5), str );
|
|
|
|
ok(!strcmp(str, expected), "got %s\n", str);
|
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptCloseAlgorithmProvider(alg, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2016-03-24 06:22:22 +01:00
|
|
|
|
|
|
|
alg = NULL;
|
|
|
|
memset(md5_hmac, 0, sizeof(md5_hmac));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptOpenAlgorithmProvider(&alg, BCRYPT_MD5_ALGORITHM, MS_PRIMITIVE_PROVIDER, BCRYPT_ALG_HANDLE_HMAC_FLAG);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2016-03-24 06:22:22 +01:00
|
|
|
ok(alg != NULL, "alg not set\n");
|
|
|
|
|
|
|
|
ret = pBCryptHash(alg, (UCHAR *)"key", sizeof("key"), (UCHAR *)"test", sizeof("test"), md5_hmac, sizeof(md5_hmac));
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2016-03-24 06:22:22 +01:00
|
|
|
format_hash( md5_hmac, sizeof(md5_hmac), str );
|
|
|
|
ok(!strcmp(str, expected_hmac), "got %s\n", str);
|
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptCloseAlgorithmProvider(alg, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2016-03-24 06:22:22 +01:00
|
|
|
}
|
|
|
|
|
2019-02-13 10:21:18 +01:00
|
|
|
/* test vectors from RFC 6070 */
|
|
|
|
static UCHAR password[] = "password";
|
|
|
|
static UCHAR salt[] = "salt";
|
|
|
|
static UCHAR long_password[] = "passwordPASSWORDpassword";
|
|
|
|
static UCHAR long_salt[] = "saltSALTsaltSALTsaltSALTsaltSALTsalt";
|
|
|
|
static UCHAR password_NUL[] = "pass\0word";
|
|
|
|
static UCHAR salt_NUL[] = "sa\0lt";
|
|
|
|
|
|
|
|
static UCHAR dk1[] = "0c60c80f961f0e71f3a9b524af6012062fe037a6";
|
|
|
|
static UCHAR dk2[] = "ea6c014dc72d6f8ccd1ed92ace1d41f0d8de8957";
|
|
|
|
static UCHAR dk3[] = "4b007901b765489abead49d926f721d065a429c1";
|
|
|
|
static UCHAR dk4[] = "364dd6bc200ec7d197f1b85f4a61769010717124";
|
|
|
|
static UCHAR dk5[] = "3d2eec4fe41c849b80c8d83662c0e44a8b291a964cf2f07038";
|
|
|
|
static UCHAR dk6[] = "56fa6aa75548099dcc37d7f03425e0c3";
|
|
|
|
|
|
|
|
static const struct
|
|
|
|
{
|
|
|
|
ULONG pwd_len;
|
|
|
|
ULONG salt_len;
|
|
|
|
ULONGLONG iterations;
|
|
|
|
ULONG dk_len;
|
|
|
|
UCHAR *pwd;
|
|
|
|
UCHAR *salt;
|
|
|
|
const UCHAR *dk;
|
|
|
|
} rfc6070[] =
|
|
|
|
{
|
|
|
|
{ 8, 4, 1, 20, password, salt, dk1 },
|
|
|
|
{ 8, 4, 2, 20, password, salt, dk2 },
|
|
|
|
{ 8, 4, 4096, 20, password, salt, dk3 },
|
|
|
|
{ 8, 4, 1000000, 20, password, salt, dk4 },
|
|
|
|
{ 24, 36, 4096, 25, long_password, long_salt, dk5 },
|
|
|
|
{ 9, 5, 4096, 16, password_NUL, salt_NUL, dk6 }
|
|
|
|
};
|
|
|
|
|
|
|
|
static void test_BcryptDeriveKeyPBKDF2(void)
|
|
|
|
{
|
|
|
|
BCRYPT_ALG_HANDLE alg;
|
|
|
|
UCHAR buf[25];
|
|
|
|
char str[51];
|
|
|
|
NTSTATUS ret;
|
|
|
|
ULONG i;
|
|
|
|
|
|
|
|
alg = NULL;
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptOpenAlgorithmProvider(&alg, BCRYPT_SHA1_ALGORITHM, MS_PRIMITIVE_PROVIDER,
|
2019-02-13 10:21:18 +01:00
|
|
|
BCRYPT_ALG_HANDLE_HMAC_FLAG);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2019-02-13 10:21:18 +01:00
|
|
|
ok(alg != NULL, "alg not set\n");
|
|
|
|
|
|
|
|
test_hash_length(alg, 20);
|
2020-02-04 21:44:10 +01:00
|
|
|
test_alg_name(alg, L"SHA1");
|
2019-02-13 10:21:18 +01:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDeriveKeyPBKDF2(alg, rfc6070[0].pwd, rfc6070[0].pwd_len, rfc6070[0].salt, rfc6070[0].salt_len,
|
2019-02-13 10:21:18 +01:00
|
|
|
0, buf, rfc6070[0].dk_len, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_INVALID_PARAMETER, "got %#lx\n", ret);
|
2019-02-13 10:21:18 +01:00
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(rfc6070); i++)
|
|
|
|
{
|
|
|
|
memset(buf, 0, sizeof(buf));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDeriveKeyPBKDF2(alg, rfc6070[i].pwd, rfc6070[i].pwd_len, rfc6070[i].salt, rfc6070[i].salt_len,
|
2019-02-13 10:21:18 +01:00
|
|
|
rfc6070[i].iterations, buf, rfc6070[i].dk_len, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2019-02-13 10:21:18 +01:00
|
|
|
format_hash(buf, rfc6070[i].dk_len, str);
|
|
|
|
ok(!memcmp(str, rfc6070[i].dk, rfc6070[i].dk_len), "got %s\n", str);
|
|
|
|
}
|
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptCloseAlgorithmProvider(alg, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2019-02-13 10:21:18 +01:00
|
|
|
}
|
|
|
|
|
2016-04-04 11:07:05 +02:00
|
|
|
static void test_rng(void)
|
|
|
|
{
|
|
|
|
BCRYPT_ALG_HANDLE alg;
|
|
|
|
ULONG size, len;
|
|
|
|
UCHAR buf[16];
|
|
|
|
NTSTATUS ret;
|
|
|
|
|
|
|
|
alg = NULL;
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptOpenAlgorithmProvider(&alg, BCRYPT_RNG_ALGORITHM, MS_PRIMITIVE_PROVIDER, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2016-04-04 11:07:05 +02:00
|
|
|
ok(alg != NULL, "alg not set\n");
|
|
|
|
|
|
|
|
len = size = 0xdeadbeef;
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptGetProperty(alg, BCRYPT_OBJECT_LENGTH, (UCHAR *)&len, sizeof(len), &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_NOT_SUPPORTED, "got %#lx\n", ret);
|
2016-04-04 11:07:05 +02:00
|
|
|
|
2017-03-10 15:34:08 +01:00
|
|
|
len = size = 0xdeadbeef;
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptGetProperty(alg, BCRYPT_HASH_LENGTH, (UCHAR *)&len, sizeof(len), &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_NOT_SUPPORTED, "got %#lx\n", ret);
|
2017-03-10 15:34:08 +01:00
|
|
|
|
2020-02-04 21:44:10 +01:00
|
|
|
test_alg_name(alg, L"RNG");
|
2016-04-04 11:07:05 +02:00
|
|
|
|
|
|
|
memset(buf, 0, 16);
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptGenRandom(alg, buf, 8, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2016-04-04 11:07:05 +02:00
|
|
|
ok(memcmp(buf, buf + 8, 8), "got zeroes\n");
|
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptCloseAlgorithmProvider(alg, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2016-04-04 11:07:05 +02:00
|
|
|
}
|
|
|
|
|
2016-12-02 21:36:41 +01:00
|
|
|
static void test_aes(void)
|
|
|
|
{
|
2017-08-17 13:36:39 +02:00
|
|
|
BCRYPT_KEY_LENGTHS_STRUCT key_lengths;
|
2016-12-02 21:36:41 +01:00
|
|
|
BCRYPT_ALG_HANDLE alg;
|
|
|
|
ULONG size, len;
|
|
|
|
UCHAR mode[64];
|
|
|
|
NTSTATUS ret;
|
2017-08-16 11:23:42 +02:00
|
|
|
|
2016-12-02 21:36:41 +01:00
|
|
|
alg = NULL;
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptOpenAlgorithmProvider(&alg, BCRYPT_AES_ALGORITHM, MS_PRIMITIVE_PROVIDER, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2016-12-02 21:36:41 +01:00
|
|
|
ok(alg != NULL, "alg not set\n");
|
|
|
|
|
|
|
|
len = size = 0;
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptGetProperty(alg, BCRYPT_OBJECT_LENGTH, (UCHAR *)&len, sizeof(len), &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2017-01-17 10:49:27 +01:00
|
|
|
ok(len, "expected non-zero len\n");
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(size == sizeof(len), "got %lu\n", size);
|
2016-12-02 21:36:41 +01:00
|
|
|
|
|
|
|
len = size = 0;
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptGetProperty(alg, BCRYPT_BLOCK_LENGTH, (UCHAR *)&len, sizeof(len), &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
|
|
|
ok(len == 16, "got %lu\n", len);
|
|
|
|
ok(size == sizeof(len), "got %lu\n", size);
|
2016-12-02 21:36:41 +01:00
|
|
|
|
|
|
|
size = 0;
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptGetProperty(alg, BCRYPT_CHAINING_MODE, mode, 0, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_BUFFER_TOO_SMALL, "got %#lx\n", ret);
|
|
|
|
ok(size == 64, "got %lu\n", size);
|
2016-12-02 21:36:41 +01:00
|
|
|
|
|
|
|
size = 0;
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptGetProperty(alg, BCRYPT_CHAINING_MODE, mode, sizeof(mode) - 1, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_BUFFER_TOO_SMALL, "got %#lx\n", ret);
|
|
|
|
ok(size == 64, "got %lu\n", size);
|
2018-02-28 05:15:55 +01:00
|
|
|
|
|
|
|
size = 0;
|
|
|
|
memset(mode, 0, sizeof(mode));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptGetProperty(alg, BCRYPT_CHAINING_MODE, mode, sizeof(mode), &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2018-03-19 12:16:11 +01:00
|
|
|
ok(!lstrcmpW((const WCHAR *)mode, BCRYPT_CHAIN_MODE_CBC), "got %s\n", wine_dbgstr_w((const WCHAR *)mode));
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(size == 64, "got %lu\n", size);
|
2016-12-02 21:36:41 +01:00
|
|
|
|
2017-08-17 13:36:39 +02:00
|
|
|
size = 0;
|
|
|
|
memset(&key_lengths, 0, sizeof(key_lengths));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptGetProperty(alg, BCRYPT_KEY_LENGTHS, (UCHAR*)&key_lengths, sizeof(key_lengths), &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
|
|
|
ok(size == sizeof(key_lengths), "got %lu\n", size);
|
|
|
|
ok(key_lengths.dwMinLength == 128, "Expected 128, got %lu\n", key_lengths.dwMinLength);
|
|
|
|
ok(key_lengths.dwMaxLength == 256, "Expected 256, got %lu\n", key_lengths.dwMaxLength);
|
|
|
|
ok(key_lengths.dwIncrement == 64, "Expected 64, got %lu\n", key_lengths.dwIncrement);
|
2017-08-17 13:36:39 +02:00
|
|
|
|
2018-03-19 12:16:11 +01:00
|
|
|
memcpy(mode, BCRYPT_CHAIN_MODE_GCM, sizeof(BCRYPT_CHAIN_MODE_GCM));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptSetProperty(alg, BCRYPT_CHAINING_MODE, mode, 0, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2018-03-19 12:16:11 +01:00
|
|
|
|
|
|
|
size = 0;
|
|
|
|
memset(mode, 0, sizeof(mode));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptGetProperty(alg, BCRYPT_CHAINING_MODE, mode, sizeof(mode), &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2018-03-19 12:16:12 +01:00
|
|
|
ok(!lstrcmpW((const WCHAR *)mode, BCRYPT_CHAIN_MODE_GCM), "got %s\n", wine_dbgstr_w((const WCHAR *)mode));
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(size == 64, "got %lu\n", size);
|
2018-03-19 12:16:11 +01:00
|
|
|
|
2020-02-04 21:44:10 +01:00
|
|
|
test_alg_name(alg, L"AES");
|
2016-12-02 21:36:41 +01:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptCloseAlgorithmProvider(alg, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2016-12-02 21:36:41 +01:00
|
|
|
}
|
|
|
|
|
2020-11-10 14:42:27 +01:00
|
|
|
static void test_3des(void)
|
|
|
|
{
|
|
|
|
BCRYPT_KEY_LENGTHS_STRUCT key_lengths;
|
|
|
|
BCRYPT_ALG_HANDLE alg;
|
|
|
|
ULONG size, len;
|
|
|
|
UCHAR mode[64];
|
|
|
|
NTSTATUS ret;
|
|
|
|
|
|
|
|
alg = NULL;
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptOpenAlgorithmProvider(&alg, BCRYPT_3DES_ALGORITHM, MS_PRIMITIVE_PROVIDER, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2020-11-10 14:42:27 +01:00
|
|
|
ok(alg != NULL, "alg not set\n");
|
|
|
|
|
|
|
|
len = size = 0;
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptGetProperty(alg, BCRYPT_OBJECT_LENGTH, (UCHAR *)&len, sizeof(len), &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2020-11-10 14:42:27 +01:00
|
|
|
ok(len, "expected non-zero len\n");
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(size == sizeof(len), "got %lu\n", size);
|
2020-11-10 14:42:27 +01:00
|
|
|
|
|
|
|
len = size = 0;
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptGetProperty(alg, BCRYPT_BLOCK_LENGTH, (UCHAR *)&len, sizeof(len), &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
|
|
|
ok(len == 8, "got %lu\n", len);
|
|
|
|
ok(size == sizeof(len), "got %lu\n", size);
|
2020-11-10 14:42:27 +01:00
|
|
|
|
|
|
|
size = 0;
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptGetProperty(alg, BCRYPT_CHAINING_MODE, mode, 0, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_BUFFER_TOO_SMALL, "got %#lx\n", ret);
|
|
|
|
ok(size == 64, "got %lu\n", size);
|
2020-11-10 14:42:27 +01:00
|
|
|
|
|
|
|
size = 0;
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptGetProperty(alg, BCRYPT_CHAINING_MODE, mode, sizeof(mode) - 1, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_BUFFER_TOO_SMALL, "got %#lx\n", ret);
|
|
|
|
ok(size == 64, "got %lu\n", size);
|
2020-11-10 14:42:27 +01:00
|
|
|
|
|
|
|
size = 0;
|
|
|
|
memset(mode, 0, sizeof(mode));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptGetProperty(alg, BCRYPT_CHAINING_MODE, mode, sizeof(mode), &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2020-11-10 14:42:27 +01:00
|
|
|
ok(!lstrcmpW((const WCHAR *)mode, BCRYPT_CHAIN_MODE_CBC), "got %s\n", wine_dbgstr_w((const WCHAR *)mode));
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(size == 64, "got %lu\n", size);
|
2020-11-10 14:42:27 +01:00
|
|
|
|
|
|
|
size = 0;
|
|
|
|
memset(&key_lengths, 0, sizeof(key_lengths));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptGetProperty(alg, BCRYPT_KEY_LENGTHS, (UCHAR*)&key_lengths, sizeof(key_lengths), &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
|
|
|
ok(size == sizeof(key_lengths), "got %lu\n", size);
|
|
|
|
ok(key_lengths.dwMinLength == 192, "Expected 192, got %lu\n", key_lengths.dwMinLength);
|
|
|
|
ok(key_lengths.dwMaxLength == 192, "Expected 192, got %lu\n", key_lengths.dwMaxLength);
|
|
|
|
ok(key_lengths.dwIncrement == 0, "Expected 0, got %lu\n", key_lengths.dwIncrement);
|
2020-11-10 14:42:27 +01:00
|
|
|
|
|
|
|
memcpy(mode, BCRYPT_CHAIN_MODE_GCM, sizeof(BCRYPT_CHAIN_MODE_GCM));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptSetProperty(alg, BCRYPT_CHAINING_MODE, mode, 0, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_NOT_SUPPORTED, "got %#lx\n", ret);
|
2020-11-10 14:42:27 +01:00
|
|
|
|
|
|
|
test_alg_name(alg, L"3DES");
|
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptCloseAlgorithmProvider(alg, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2020-11-10 14:42:27 +01:00
|
|
|
}
|
|
|
|
|
2016-12-02 21:36:42 +01:00
|
|
|
static void test_BCryptGenerateSymmetricKey(void)
|
|
|
|
{
|
|
|
|
static UCHAR secret[] =
|
|
|
|
{0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f};
|
|
|
|
static UCHAR iv[] =
|
|
|
|
{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};
|
|
|
|
static UCHAR expected[] =
|
|
|
|
{0xc6,0xa1,0x3b,0x37,0x87,0x8f,0x5b,0x82,0x6f,0x4f,0x81,0x62,0xa1,0xc8,0xd8,0x79};
|
|
|
|
BCRYPT_ALG_HANDLE aes;
|
2018-03-22 11:33:36 +01:00
|
|
|
BCRYPT_KEY_HANDLE key, key2;
|
2018-12-04 08:59:20 +01:00
|
|
|
UCHAR *buf, ciphertext[16], plaintext[16], ivbuf[16], mode[64];
|
|
|
|
BCRYPT_KEY_LENGTHS_STRUCT key_lengths;
|
2016-12-02 21:36:42 +01:00
|
|
|
ULONG size, len, i;
|
|
|
|
NTSTATUS ret;
|
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptOpenAlgorithmProvider(&aes, BCRYPT_AES_ALGORITHM, NULL, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2016-12-02 21:36:42 +01:00
|
|
|
|
|
|
|
len = size = 0xdeadbeef;
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptGetProperty(aes, BCRYPT_OBJECT_LENGTH, (UCHAR *)&len, sizeof(len), &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2016-12-02 21:36:42 +01:00
|
|
|
|
2018-03-22 11:33:39 +01:00
|
|
|
key = (void *)0xdeadbeef;
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptGenerateSymmetricKey(NULL, &key, NULL, 0, secret, sizeof(secret), 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_INVALID_HANDLE, "got %#lx\n", ret);
|
2018-03-22 11:33:39 +01:00
|
|
|
ok(key == (void *)0xdeadbeef, "got %p\n", key);
|
|
|
|
|
2016-12-02 21:36:42 +01:00
|
|
|
key = NULL;
|
|
|
|
buf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
|
2021-11-12 15:09:46 +01:00
|
|
|
|
|
|
|
key = (BCRYPT_KEY_HANDLE)0xdeadbeef;
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptGenerateSymmetricKey(aes, &key, buf, len, secret, 1, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_INVALID_PARAMETER, "got %#lx\n", ret);
|
2021-11-12 15:09:46 +01:00
|
|
|
ok(key == (HANDLE)0xdeadbeef, "got unexpected key %p.\n", key);
|
|
|
|
|
|
|
|
key = (BCRYPT_KEY_HANDLE)0xdeadbeef;
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptGenerateSymmetricKey(aes, &key, buf, len, secret, sizeof(secret) + 1, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_INVALID_PARAMETER, "got %#lx\n", ret);
|
2021-11-12 15:09:46 +01:00
|
|
|
ok(key == (HANDLE)0xdeadbeef, "got unexpected key %p.\n", key);
|
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptGenerateSymmetricKey(aes, &key, buf, len, secret, sizeof(secret), 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2016-12-02 21:36:42 +01:00
|
|
|
ok(key != NULL, "key not set\n");
|
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptSetProperty(aes, BCRYPT_CHAINING_MODE, (UCHAR *)BCRYPT_CHAIN_MODE_CBC,
|
2016-12-02 21:36:42 +01:00
|
|
|
sizeof(BCRYPT_CHAIN_MODE_CBC), 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2016-12-02 21:36:42 +01:00
|
|
|
|
2019-07-05 10:38:01 +02:00
|
|
|
size = 0;
|
|
|
|
memset(mode, 0, sizeof(mode));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptGetProperty(key, BCRYPT_CHAINING_MODE, mode, sizeof(mode), &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2019-07-05 10:38:01 +02:00
|
|
|
ok(!lstrcmpW((const WCHAR *)mode, BCRYPT_CHAIN_MODE_CBC), "got %s\n", wine_dbgstr_w((const WCHAR *)mode));
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(size == 64, "got %lu\n", size);
|
2019-07-05 10:38:01 +02:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptSetProperty(key, BCRYPT_CHAINING_MODE, (UCHAR *)BCRYPT_CHAIN_MODE_ECB, 0, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS || broken(ret == STATUS_NOT_SUPPORTED) /* < Win 8 */, "got %#lx\n", ret);
|
2019-07-05 10:38:01 +02:00
|
|
|
if (ret == STATUS_SUCCESS)
|
|
|
|
{
|
|
|
|
size = 0;
|
|
|
|
memset(mode, 0, sizeof(mode));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptGetProperty(key, BCRYPT_CHAINING_MODE, mode, sizeof(mode), &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2019-07-05 10:38:01 +02:00
|
|
|
ok(!lstrcmpW((const WCHAR *)mode, BCRYPT_CHAIN_MODE_ECB), "got %s\n", wine_dbgstr_w((const WCHAR *)mode));
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(size == 64, "got %lu\n", size);
|
2019-07-05 10:38:01 +02:00
|
|
|
}
|
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptSetProperty(key, BCRYPT_CHAINING_MODE, (UCHAR *)BCRYPT_CHAIN_MODE_CBC,
|
2018-03-20 21:27:13 +01:00
|
|
|
sizeof(BCRYPT_CHAIN_MODE_CBC), 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS || broken(ret == STATUS_NOT_SUPPORTED) /* < Win 8 */, "got %#lx\n", ret);
|
2018-03-20 21:27:13 +01:00
|
|
|
|
2016-12-02 21:36:42 +01:00
|
|
|
size = 0xdeadbeef;
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptEncrypt(key, NULL, 0, NULL, NULL, 0, NULL, 0, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
|
|
|
ok(!size, "got %lu\n", size);
|
2016-12-02 21:36:42 +01:00
|
|
|
|
|
|
|
size = 0;
|
|
|
|
memcpy(ivbuf, iv, sizeof(iv));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptEncrypt(key, data, 16, NULL, ivbuf, 16, NULL, 0, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
|
|
|
ok(size == 16, "got %lu\n", size);
|
2016-12-02 21:36:42 +01:00
|
|
|
|
|
|
|
size = 0;
|
|
|
|
memcpy(ivbuf, iv, sizeof(iv));
|
|
|
|
memset(ciphertext, 0, sizeof(ciphertext));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptEncrypt(key, data, 16, NULL, ivbuf, 16, ciphertext, 16, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
|
|
|
ok(size == 16, "got %lu\n", size);
|
2016-12-02 21:36:42 +01:00
|
|
|
ok(!memcmp(ciphertext, expected, sizeof(expected)), "wrong data\n");
|
|
|
|
for (i = 0; i < 16; i++)
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ciphertext[i] == expected[i], "%lu: %02x != %02x\n", i, ciphertext[i], expected[i]);
|
2016-12-02 21:36:42 +01:00
|
|
|
|
2018-03-22 11:33:39 +01:00
|
|
|
key2 = (void *)0xdeadbeef;
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDuplicateKey(NULL, &key2, NULL, 0, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_INVALID_HANDLE, "got %#lx\n", ret);
|
2018-03-22 11:33:39 +01:00
|
|
|
ok(key2 == (void *)0xdeadbeef, "got %p\n", key2);
|
2018-03-22 11:33:36 +01:00
|
|
|
|
|
|
|
if (0) /* crashes on some Windows versions */
|
|
|
|
{
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDuplicateKey(key, NULL, NULL, 0, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_INVALID_PARAMETER, "got %#lx\n", ret);
|
2018-03-22 11:33:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
key2 = (void *)0xdeadbeef;
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDuplicateKey(key, &key2, NULL, 0, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS || broken(ret == STATUS_INVALID_PARAMETER), "got %#lx\n", ret);
|
2018-03-22 11:33:36 +01:00
|
|
|
|
|
|
|
if (ret == STATUS_SUCCESS)
|
|
|
|
{
|
|
|
|
size = 0;
|
|
|
|
memcpy(ivbuf, iv, sizeof(iv));
|
|
|
|
memset(ciphertext, 0, sizeof(ciphertext));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptEncrypt(key2, data, 16, NULL, ivbuf, 16, ciphertext, 16, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
|
|
|
ok(size == 16, "got %lu\n", size);
|
2018-03-22 11:33:36 +01:00
|
|
|
ok(!memcmp(ciphertext, expected, sizeof(expected)), "wrong data\n");
|
|
|
|
for (i = 0; i < 16; i++)
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ciphertext[i] == expected[i], "%lu: %02x != %02x\n", i, ciphertext[i], expected[i]);
|
2018-03-22 11:33:36 +01:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDestroyKey(key2);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2018-03-22 11:33:36 +01:00
|
|
|
}
|
|
|
|
|
2016-12-02 21:36:42 +01:00
|
|
|
size = 0xdeadbeef;
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDecrypt(key, NULL, 0, NULL, NULL, 0, NULL, 0, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
|
|
|
ok(!size, "got %lu\n", size);
|
2016-12-02 21:36:42 +01:00
|
|
|
|
|
|
|
size = 0;
|
|
|
|
memcpy(ivbuf, iv, sizeof(iv));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDecrypt(key, ciphertext, 16, NULL, ivbuf, 16, NULL, 0, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
|
|
|
ok(size == 16, "got %lu\n", size);
|
2016-12-02 21:36:42 +01:00
|
|
|
|
|
|
|
size = 0;
|
|
|
|
memcpy(ivbuf, iv, sizeof(iv));
|
|
|
|
memset(plaintext, 0, sizeof(plaintext));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDecrypt(key, ciphertext, 16, NULL, ivbuf, 16, plaintext, 16, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
|
|
|
ok(size == 16, "got %lu\n", size);
|
2016-12-02 21:36:42 +01:00
|
|
|
ok(!memcmp(plaintext, data, sizeof(data)), "wrong data\n");
|
|
|
|
|
2018-12-04 08:59:20 +01:00
|
|
|
memset(mode, 0, sizeof(mode));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptGetProperty(key, BCRYPT_CHAINING_MODE, mode, sizeof(mode), &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2018-12-04 08:59:20 +01:00
|
|
|
ok(!lstrcmpW((const WCHAR *)mode, BCRYPT_CHAIN_MODE_CBC), "wrong mode\n");
|
|
|
|
|
|
|
|
len = 0;
|
|
|
|
size = 0;
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptGetProperty(key, BCRYPT_BLOCK_LENGTH, (UCHAR *)&len, sizeof(len), &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
|
|
|
ok(len == 16, "got %lu\n", len);
|
|
|
|
ok(size == sizeof(len), "got %lu\n", size);
|
2018-12-04 08:59:20 +01:00
|
|
|
|
|
|
|
size = 0;
|
|
|
|
memset(&key_lengths, 0, sizeof(key_lengths));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptGetProperty(aes, BCRYPT_KEY_LENGTHS, (UCHAR*)&key_lengths, sizeof(key_lengths), &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
|
|
|
ok(size == sizeof(key_lengths), "got %lu\n", size);
|
|
|
|
ok(key_lengths.dwMinLength == 128, "Expected 128, got %lu\n", key_lengths.dwMinLength);
|
|
|
|
ok(key_lengths.dwMaxLength == 256, "Expected 256, got %lu\n", key_lengths.dwMaxLength);
|
|
|
|
ok(key_lengths.dwIncrement == 64, "Expected 64, got %lu\n", key_lengths.dwIncrement);
|
2018-12-04 08:59:20 +01:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDestroyKey(key);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2016-12-02 21:36:42 +01:00
|
|
|
HeapFree(GetProcessHeap(), 0, buf);
|
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptCloseAlgorithmProvider(aes, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2016-12-02 21:36:42 +01:00
|
|
|
}
|
|
|
|
|
2021-04-20 16:53:34 +02:00
|
|
|
#define RACE_TEST_COUNT 200
|
|
|
|
static LONG encrypt_race_start_barrier;
|
|
|
|
|
|
|
|
static DWORD WINAPI encrypt_race_thread(void *parameter)
|
|
|
|
{
|
|
|
|
static UCHAR nonce[] =
|
|
|
|
{0x11,0x20,0x30,0x40,0x50,0x60,0x10,0x20,0x30,0x40,0x50,0x60};
|
|
|
|
static UCHAR auth_data[] =
|
|
|
|
{0x61,0x50,0x40,0x30,0x20,0x10,0x60,0x50,0x40,0x30,0x20,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 expected4[] =
|
|
|
|
{0xb2,0x27,0x19,0x09,0xc7,0x89,0xdc,0x52,0x24,0x83,0x3a,0x55,0x34,0x76,0x2c,0xbf,
|
|
|
|
0x15,0xa1,0xcb,0x40,0x78,0x11,0xba,0xbc,0xa4,0x76,0x69,0x7c,0x75,0x4f,0x11,0xba};
|
|
|
|
static UCHAR expected_tag3[] =
|
|
|
|
{0xef,0xee,0x75,0x99,0xb8,0x12,0xe9,0xf0,0xb4,0xcc,0x65,0x11,0x67,0x60,0x2d,0xe6};
|
|
|
|
|
|
|
|
BCRYPT_AUTHENTICATED_CIPHER_MODE_INFO auth_info;
|
|
|
|
BCRYPT_KEY_HANDLE key = parameter;
|
|
|
|
UCHAR ciphertext[48], tag[16];
|
|
|
|
unsigned int i, test;
|
|
|
|
NTSTATUS ret;
|
|
|
|
ULONG size;
|
|
|
|
|
|
|
|
memset(&auth_info, 0, sizeof(auth_info));
|
|
|
|
auth_info.cbSize = sizeof(auth_info);
|
|
|
|
auth_info.dwInfoVersion = 1;
|
|
|
|
auth_info.pbNonce = nonce;
|
|
|
|
auth_info.cbNonce = sizeof(nonce);
|
|
|
|
auth_info.pbTag = tag;
|
|
|
|
auth_info.cbTag = sizeof(tag);
|
|
|
|
auth_info.pbAuthData = auth_data;
|
|
|
|
auth_info.cbAuthData = sizeof(auth_data);
|
|
|
|
|
|
|
|
InterlockedIncrement(&encrypt_race_start_barrier);
|
|
|
|
while (InterlockedCompareExchange(&encrypt_race_start_barrier, 3, 2) != 2)
|
|
|
|
;
|
|
|
|
|
|
|
|
for (test = 0; test < RACE_TEST_COUNT; ++test)
|
|
|
|
{
|
|
|
|
size = 0;
|
|
|
|
memset(ciphertext, 0xff, sizeof(ciphertext));
|
|
|
|
memset(tag, 0xff, sizeof(tag));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptEncrypt(key, data2, 32, &auth_info, NULL, 0, ciphertext, 32, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
|
|
|
ok(size == 32, "got %lu\n", size);
|
2021-04-20 16:53:34 +02:00
|
|
|
ok(!memcmp(ciphertext, expected4, sizeof(expected4)), "wrong data\n");
|
|
|
|
ok(!memcmp(tag, expected_tag3, sizeof(expected_tag3)), "wrong tag\n");
|
|
|
|
for (i = 0; i < 32; i++)
|
|
|
|
ok(ciphertext[i] == expected4[i], "%u: %02x != %02x\n", i, ciphertext[i], expected4[i]);
|
|
|
|
for (i = 0; i < 16; i++)
|
|
|
|
ok(tag[i] == expected_tag3[i], "%u: %02x != %02x\n", i, tag[i], expected_tag3[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-12-02 21:36:43 +01:00
|
|
|
static void test_BCryptEncrypt(void)
|
|
|
|
{
|
2018-03-19 12:16:08 +01:00
|
|
|
static UCHAR nonce[] =
|
2018-03-22 11:33:38 +01:00
|
|
|
{0x10,0x20,0x30,0x40,0x50,0x60,0x10,0x20,0x30,0x40,0x50,0x60};
|
|
|
|
static UCHAR auth_data[] =
|
|
|
|
{0x60,0x50,0x40,0x30,0x20,0x10,0x60,0x50,0x40,0x30,0x20,0x10};
|
2016-12-02 21:36:43 +01:00
|
|
|
static UCHAR secret[] =
|
|
|
|
{0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f};
|
2018-03-23 14:06:10 +01:00
|
|
|
static UCHAR secret256[] =
|
|
|
|
{0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,
|
|
|
|
0x0f,0x0e,0x0d,0x0c,0x0b,0x0a,0x09,0x08,0x07,0x06,0x05,0x04,0x03,0x02,0x01,0x00};
|
2016-12-02 21:36:43 +01:00
|
|
|
static UCHAR iv[] =
|
|
|
|
{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};
|
2017-08-16 11:23:47 +02:00
|
|
|
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};
|
2016-12-02 21:36:43 +01:00
|
|
|
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};
|
2017-08-16 11:23:47 +02:00
|
|
|
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};
|
2018-03-19 12:16:08 +01:00
|
|
|
static UCHAR expected4[] =
|
|
|
|
{0xe1,0x82,0xc3,0xc0,0x24,0xfb,0x86,0x85,0xf3,0xf1,0x2b,0x7d,0x09,0xb4,0x73,0x67,
|
|
|
|
0x86,0x64,0xc3,0xfe,0xa3,0x07,0x61,0xf8,0x16,0xc9,0x78,0x7f,0xe7,0xb1,0xc4,0x94};
|
2018-03-23 14:06:09 +01:00
|
|
|
static UCHAR expected5[] =
|
|
|
|
{0x0a,0x94,0x0b,0xb5,0x41,0x6e,0xf0,0x45,0xf1,0xc3,0x94,0x58,0xc6,0x53,0xea,0x5a};
|
|
|
|
static UCHAR expected6[] =
|
|
|
|
{0x0a,0x94,0x0b,0xb5,0x41,0x6e,0xf0,0x45,0xf1,0xc3,0x94,0x58,0xc6,0x53,0xea,0x5a,
|
|
|
|
0x84,0x07,0x66,0xb7,0x49,0xc0,0x9b,0x49,0x74,0x28,0x8c,0x10,0xb9,0xc2,0x09,0x70};
|
|
|
|
static UCHAR expected7[] =
|
|
|
|
{0x0a,0x94,0x0b,0xb5,0x41,0x6e,0xf0,0x45,0xf1,0xc3,0x94,0x58,0xc6,0x53,0xea,0x5a,
|
|
|
|
0x95,0x4f,0x64,0xf2,0xe4,0xe8,0x6e,0x9e,0xee,0x82,0xd2,0x02,0x16,0x68,0x48,0x99,
|
|
|
|
0x95,0x4f,0x64,0xf2,0xe4,0xe8,0x6e,0x9e,0xee,0x82,0xd2,0x02,0x16,0x68,0x48,0x99};
|
|
|
|
static UCHAR expected8[] =
|
|
|
|
{0xb5,0x8a,0x10,0x64,0xd8,0xac,0xa9,0x9b,0xd9,0xb0,0x40,0x5b,0x85,0x45,0xf5,0xbb};
|
|
|
|
static UCHAR expected9[] =
|
|
|
|
{0x0a,0x94,0x0b,0xb5,0x41,0x6e,0xf0,0x45,0xf1,0xc3,0x94,0x58,0xc6,0x53,0xea,0x5a};
|
2018-03-23 14:06:10 +01:00
|
|
|
static UCHAR expected10[] =
|
|
|
|
{0x66,0xb8,0xbd,0xe5,0x90,0x6c,0xec,0xdf,0xfa,0x8a,0xb2,0xfd,0x92,0x84,0xeb,0xf0,
|
|
|
|
0x95,0xc4,0xdf,0xa7,0x7a,0x62,0xe4,0xab,0xd4,0x0e,0x94,0x4e,0xd7,0x6e,0xa1,0x47,
|
|
|
|
0x29,0x4b,0x37,0xfe,0x28,0x6d,0x5f,0x69,0x46,0x30,0x73,0xc0,0xaa,0x42,0xe4,0x46};
|
2018-03-19 12:16:08 +01:00
|
|
|
static UCHAR expected_tag[] =
|
|
|
|
{0x89,0xb3,0x92,0x00,0x39,0x20,0x09,0xb4,0x6a,0xd6,0xaf,0xca,0x4b,0x5b,0xfd,0xd0};
|
|
|
|
static UCHAR expected_tag2[] =
|
|
|
|
{0x9a,0x92,0x32,0x2c,0x61,0x2a,0xae,0xef,0x66,0x2a,0xfb,0x55,0xe9,0x48,0xdf,0xbd};
|
2018-03-22 11:33:38 +01:00
|
|
|
static UCHAR expected_tag3[] =
|
|
|
|
{0x17,0x9d,0xc0,0x7a,0xf0,0xcf,0xaa,0xd5,0x1c,0x11,0xc4,0x4b,0xd6,0xa3,0x3e,0x77};
|
2018-03-22 11:33:40 +01:00
|
|
|
static UCHAR expected_tag4[] =
|
|
|
|
{0x4c,0x42,0x83,0x9e,0x8d,0x40,0xf1,0x19,0xd6,0x2b,0x1c,0x66,0x03,0x2b,0x39,0x63};
|
|
|
|
|
2018-03-19 12:16:08 +01:00
|
|
|
BCRYPT_AUTHENTICATED_CIPHER_MODE_INFO auth_info;
|
|
|
|
UCHAR *buf, ciphertext[48], ivbuf[16], tag[16];
|
|
|
|
BCRYPT_AUTH_TAG_LENGTHS_STRUCT tag_length;
|
2021-04-20 16:53:34 +02:00
|
|
|
ULONG size, len, i, test;
|
2016-12-02 21:36:43 +01:00
|
|
|
BCRYPT_ALG_HANDLE aes;
|
|
|
|
BCRYPT_KEY_HANDLE key;
|
2021-04-20 16:53:34 +02:00
|
|
|
HANDLE hthread;
|
2016-12-02 21:36:43 +01:00
|
|
|
NTSTATUS ret;
|
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptOpenAlgorithmProvider(&aes, BCRYPT_AES_ALGORITHM, NULL, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2016-12-02 21:36:43 +01:00
|
|
|
|
2018-03-19 12:16:08 +01:00
|
|
|
/******************
|
|
|
|
* AES - CBC mode *
|
|
|
|
******************/
|
|
|
|
|
2016-12-02 21:36:43 +01:00
|
|
|
len = 0xdeadbeef;
|
|
|
|
size = sizeof(len);
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptGetProperty(aes, BCRYPT_OBJECT_LENGTH, (UCHAR *)&len, sizeof(len), &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2016-12-02 21:36:43 +01:00
|
|
|
|
2018-03-22 11:33:39 +01:00
|
|
|
key = NULL;
|
2016-12-02 21:36:43 +01:00
|
|
|
buf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptGenerateSymmetricKey(aes, &key, buf, len, secret, sizeof(secret), 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2018-03-22 11:33:39 +01:00
|
|
|
ok(key != NULL, "key not set\n");
|
2016-12-02 21:36:43 +01:00
|
|
|
|
|
|
|
/* input size is a multiple of block size */
|
|
|
|
size = 0;
|
|
|
|
memcpy(ivbuf, iv, sizeof(iv));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptEncrypt(key, data, 16, NULL, ivbuf, 16, NULL, 0, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
|
|
|
ok(size == 16, "got %lu\n", size);
|
2016-12-02 21:36:43 +01:00
|
|
|
|
|
|
|
size = 0;
|
|
|
|
memcpy(ivbuf, iv, sizeof(iv));
|
|
|
|
memset(ciphertext, 0, sizeof(ciphertext));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptEncrypt(key, data, 16, NULL, ivbuf, 16, ciphertext, 16, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
|
|
|
ok(size == 16, "got %lu\n", size);
|
2016-12-02 21:36:43 +01:00
|
|
|
ok(!memcmp(ciphertext, expected, sizeof(expected)), "wrong data\n");
|
|
|
|
for (i = 0; i < 16; i++)
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ciphertext[i] == expected[i], "%lu: %02x != %02x\n", i, ciphertext[i], expected[i]);
|
2016-12-02 21:36:43 +01:00
|
|
|
|
2018-03-23 14:06:09 +01:00
|
|
|
/* NULL initialization vector */
|
|
|
|
size = 0;
|
|
|
|
memset(ciphertext, 0, sizeof(ciphertext));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptEncrypt(key, data, 16, NULL, NULL, 0, ciphertext, 16, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
|
|
|
ok(size == 16, "got %lu\n", size);
|
2018-03-23 14:06:09 +01:00
|
|
|
todo_wine ok(!memcmp(ciphertext, expected8, sizeof(expected8)), "wrong data\n");
|
|
|
|
|
|
|
|
/* all zero initialization vector */
|
|
|
|
size = 0;
|
|
|
|
memset(ciphertext, 0, sizeof(ciphertext));
|
|
|
|
memset(ivbuf, 0, sizeof(ivbuf));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptEncrypt(key, data, 16, NULL, ivbuf, 16, ciphertext, 16, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
|
|
|
ok(size == 16, "got %lu\n", size);
|
2018-03-23 14:06:09 +01:00
|
|
|
ok(!memcmp(ciphertext, expected9, sizeof(expected9)), "wrong data\n");
|
|
|
|
for (i = 0; i < 16; i++)
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ciphertext[i] == expected9[i], "%lu: %02x != %02x\n", i, ciphertext[i], expected9[i]);
|
2018-03-23 14:06:09 +01:00
|
|
|
|
2016-12-02 21:36:43 +01:00
|
|
|
/* input size is not a multiple of block size */
|
|
|
|
size = 0;
|
|
|
|
memcpy(ivbuf, iv, sizeof(iv));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptEncrypt(key, data, 17, NULL, ivbuf, 16, NULL, 0, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_INVALID_BUFFER_SIZE, "got %#lx\n", ret);
|
|
|
|
ok(size == 17, "got %lu\n", size);
|
2016-12-02 21:36:43 +01:00
|
|
|
|
|
|
|
/* input size is not a multiple of block size, block padding set */
|
|
|
|
size = 0;
|
|
|
|
memcpy(ivbuf, iv, sizeof(iv));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptEncrypt(key, data, 17, NULL, ivbuf, 16, NULL, 0, &size, BCRYPT_BLOCK_PADDING);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
|
|
|
ok(size == 32, "got %lu\n", size);
|
2016-12-02 21:36:43 +01:00
|
|
|
|
|
|
|
size = 0;
|
|
|
|
memcpy(ivbuf, iv, sizeof(iv));
|
|
|
|
memset(ciphertext, 0, sizeof(ciphertext));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptEncrypt(key, data, 17, NULL, ivbuf, 16, ciphertext, 32, &size, BCRYPT_BLOCK_PADDING);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
|
|
|
ok(size == 32, "got %lu\n", size);
|
2016-12-02 21:36:43 +01:00
|
|
|
ok(!memcmp(ciphertext, expected2, sizeof(expected2)), "wrong data\n");
|
|
|
|
for (i = 0; i < 32; i++)
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ciphertext[i] == expected2[i], "%lu: %02x != %02x\n", i, ciphertext[i], expected2[i]);
|
2016-12-02 21:36:43 +01:00
|
|
|
|
2017-08-16 11:23:47 +02:00
|
|
|
/* input size is a multiple of block size, block padding set */
|
|
|
|
size = 0;
|
|
|
|
memcpy(ivbuf, iv, sizeof(iv));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptEncrypt(key, data2, 32, NULL, ivbuf, 16, NULL, 0, &size, BCRYPT_BLOCK_PADDING);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
|
|
|
ok(size == 48, "got %lu\n", size);
|
2017-08-16 11:23:47 +02:00
|
|
|
|
|
|
|
size = 0;
|
|
|
|
memcpy(ivbuf, iv, sizeof(iv));
|
|
|
|
memset(ciphertext, 0, sizeof(ciphertext));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptEncrypt(key, data2, 32, NULL, ivbuf, 16, ciphertext, 48, &size, BCRYPT_BLOCK_PADDING);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
|
|
|
ok(size == 48, "got %lu\n", size);
|
2017-08-16 11:23:47 +02:00
|
|
|
ok(!memcmp(ciphertext, expected3, sizeof(expected3)), "wrong data\n");
|
|
|
|
for (i = 0; i < 48; i++)
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ciphertext[i] == expected3[i], "%lu: %02x != %02x\n", i, ciphertext[i], expected3[i]);
|
2017-08-16 11:23:47 +02:00
|
|
|
|
2016-12-02 21:36:43 +01:00
|
|
|
/* output size too small */
|
|
|
|
size = 0;
|
|
|
|
memcpy(ivbuf, iv, sizeof(iv));
|
|
|
|
memset(ciphertext, 0, sizeof(ciphertext));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptEncrypt(key, data, 17, NULL, ivbuf, 16, ciphertext, 31, &size, BCRYPT_BLOCK_PADDING);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_BUFFER_TOO_SMALL, "got %#lx\n", ret);
|
|
|
|
ok(size == 32, "got %lu\n", size);
|
2016-12-02 21:36:43 +01:00
|
|
|
|
2017-08-16 11:23:47 +02:00
|
|
|
size = 0;
|
|
|
|
memcpy(ivbuf, iv, sizeof(iv));
|
|
|
|
memset(ciphertext, 0, sizeof(ciphertext));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptEncrypt(key, data2, 32, NULL, ivbuf, 16, ciphertext, 32, &size, BCRYPT_BLOCK_PADDING);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_BUFFER_TOO_SMALL, "got %#lx\n", ret);
|
|
|
|
ok(size == 48, "got %lu\n", size);
|
2017-08-16 11:23:47 +02:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDestroyKey(key);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2016-12-02 21:36:43 +01:00
|
|
|
HeapFree(GetProcessHeap(), 0, buf);
|
|
|
|
|
2018-03-23 14:06:10 +01:00
|
|
|
/* 256 bit key */
|
|
|
|
buf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
|
2021-11-12 15:09:46 +01:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptGenerateSymmetricKey(aes, &key, buf, len, secret256, sizeof(secret256), 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDestroyKey(key);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2021-11-12 15:09:46 +01:00
|
|
|
|
|
|
|
/* Key generations succeeds if the key size exceeds maximum and uses maximum key length
|
|
|
|
* from secret. */
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptGenerateSymmetricKey(aes, &key, buf, len, secret256, sizeof(secret256) + 1, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2018-03-23 14:06:10 +01:00
|
|
|
|
|
|
|
size = 0;
|
|
|
|
memcpy(ivbuf, iv, sizeof(iv));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptEncrypt(key, data2, 32, NULL, ivbuf, 16, NULL, 0, &size, BCRYPT_BLOCK_PADDING);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
|
|
|
ok(size == 48, "got %lu\n", size);
|
2018-03-23 14:06:10 +01:00
|
|
|
|
|
|
|
size = 0;
|
|
|
|
memcpy(ivbuf, iv, sizeof(iv));
|
|
|
|
memset(ciphertext, 0, sizeof(ciphertext));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptEncrypt(key, data2, 32, NULL, ivbuf, 16, ciphertext, 48, &size, BCRYPT_BLOCK_PADDING);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
|
|
|
ok(size == 48, "got %lu\n", size);
|
2018-03-23 14:06:10 +01:00
|
|
|
ok(!memcmp(ciphertext, expected10, sizeof(expected10)), "wrong data\n");
|
|
|
|
for (i = 0; i < 48; i++)
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ciphertext[i] == expected10[i], "%lu: %02x != %02x\n", i, ciphertext[i], expected10[i]);
|
2018-03-23 14:06:10 +01:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDestroyKey(key);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2018-03-23 14:06:10 +01:00
|
|
|
HeapFree(GetProcessHeap(), 0, buf);
|
|
|
|
|
2018-03-19 12:16:08 +01:00
|
|
|
/******************
|
|
|
|
* AES - GCM mode *
|
|
|
|
******************/
|
|
|
|
|
|
|
|
size = 0;
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptGetProperty(aes, BCRYPT_AUTH_TAG_LENGTH, NULL, 0, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_NOT_SUPPORTED, "got %#lx\n", ret);
|
2018-03-19 12:16:08 +01:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptSetProperty(aes, BCRYPT_CHAINING_MODE, (UCHAR*)BCRYPT_CHAIN_MODE_GCM, sizeof(BCRYPT_CHAIN_MODE_GCM), 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2018-03-19 12:16:08 +01:00
|
|
|
|
|
|
|
size = 0;
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptGetProperty(aes, BCRYPT_AUTH_TAG_LENGTH, NULL, 0, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
|
|
|
ok(size == sizeof(tag_length), "got %lu\n", size);
|
2018-03-19 12:16:08 +01:00
|
|
|
|
|
|
|
size = 0;
|
|
|
|
memset(&tag_length, 0, sizeof(tag_length));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptGetProperty(aes, BCRYPT_AUTH_TAG_LENGTH, (UCHAR*)&tag_length, sizeof(tag_length), &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
|
|
|
ok(size == sizeof(tag_length), "got %lu\n", size);
|
|
|
|
ok(tag_length.dwMinLength == 12, "Expected 12, got %lu\n", tag_length.dwMinLength);
|
|
|
|
ok(tag_length.dwMaxLength == 16, "Expected 16, got %lu\n", tag_length.dwMaxLength);
|
|
|
|
ok(tag_length.dwIncrement == 1, "Expected 1, got %lu\n", tag_length.dwIncrement);
|
2018-03-19 12:16:08 +01:00
|
|
|
|
|
|
|
len = 0xdeadbeef;
|
|
|
|
size = sizeof(len);
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptGetProperty(aes, BCRYPT_OBJECT_LENGTH, (UCHAR *)&len, sizeof(len), &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2018-03-19 12:16:08 +01:00
|
|
|
|
2018-03-22 11:33:39 +01:00
|
|
|
key = NULL;
|
2018-03-19 12:16:08 +01:00
|
|
|
buf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptGenerateSymmetricKey(aes, &key, buf, len, secret, sizeof(secret), 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2018-03-22 11:33:39 +01:00
|
|
|
ok(key != NULL, "key not set\n");
|
2018-03-19 12:16:08 +01:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptGetProperty(key, BCRYPT_AUTH_TAG_LENGTH, (UCHAR*)&tag_length, sizeof(tag_length), &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_NOT_SUPPORTED, "got %#lx\n", ret);
|
2018-12-04 08:59:20 +01:00
|
|
|
|
2018-03-19 12:16:08 +01:00
|
|
|
memset(&auth_info, 0, sizeof(auth_info));
|
|
|
|
auth_info.cbSize = sizeof(auth_info);
|
|
|
|
auth_info.dwInfoVersion = 1;
|
|
|
|
auth_info.pbNonce = nonce;
|
|
|
|
auth_info.cbNonce = sizeof(nonce);
|
|
|
|
auth_info.pbTag = tag;
|
|
|
|
auth_info.cbTag = sizeof(tag);
|
|
|
|
|
|
|
|
/* input size is a multiple of block size */
|
|
|
|
size = 0;
|
|
|
|
memcpy(ivbuf, iv, sizeof(iv));
|
|
|
|
memset(ciphertext, 0xff, sizeof(ciphertext));
|
|
|
|
memset(tag, 0xff, sizeof(tag));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptEncrypt(key, data2, 32, &auth_info, ivbuf, 16, ciphertext, 32, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
|
|
|
ok(size == 32, "got %lu\n", size);
|
2018-03-20 21:27:06 +01:00
|
|
|
ok(!memcmp(ciphertext, expected4, sizeof(expected4)), "wrong data\n");
|
2018-03-20 21:27:09 +01:00
|
|
|
ok(!memcmp(tag, expected_tag, sizeof(expected_tag)), "wrong tag\n");
|
2018-03-19 12:16:08 +01:00
|
|
|
for (i = 0; i < 32; i++)
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ciphertext[i] == expected4[i], "%lu: %02x != %02x\n", i, ciphertext[i], expected4[i]);
|
2018-03-19 12:16:08 +01:00
|
|
|
for (i = 0; i < 16; i++)
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(tag[i] == expected_tag[i], "%lu: %02x != %02x\n", i, tag[i], expected_tag[i]);
|
2018-03-19 12:16:08 +01:00
|
|
|
|
2018-03-23 14:06:09 +01:00
|
|
|
/* NULL initialization vector */
|
|
|
|
size = 0;
|
|
|
|
memset(ciphertext, 0xff, sizeof(ciphertext));
|
|
|
|
memset(tag, 0xff, sizeof(tag));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptEncrypt(key, data2, 32, &auth_info, NULL, 0, ciphertext, 32, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
|
|
|
ok(size == 32, "got %lu\n", size);
|
2018-03-23 14:06:09 +01:00
|
|
|
ok(!memcmp(ciphertext, expected4, sizeof(expected4)), "wrong data\n");
|
|
|
|
ok(!memcmp(tag, expected_tag, sizeof(expected_tag)), "wrong tag\n");
|
|
|
|
for (i = 0; i < 32; i++)
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ciphertext[i] == expected4[i], "%lu: %02x != %02x\n", i, ciphertext[i], expected4[i]);
|
2018-03-23 14:06:09 +01:00
|
|
|
for (i = 0; i < 16; i++)
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(tag[i] == expected_tag[i], "%lu: %02x != %02x\n", i, tag[i], expected_tag[i]);
|
2018-03-23 14:06:09 +01:00
|
|
|
|
|
|
|
/* all zero initialization vector */
|
|
|
|
size = 0;
|
|
|
|
memset(ciphertext, 0xff, sizeof(ciphertext));
|
|
|
|
memset(tag, 0xff, sizeof(tag));
|
|
|
|
memset(ivbuf, 0, sizeof(ivbuf));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptEncrypt(key, data2, 32, &auth_info, ivbuf, 16, ciphertext, 32, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
|
|
|
ok(size == 32, "got %lu\n", size);
|
2018-03-23 14:06:09 +01:00
|
|
|
ok(!memcmp(ciphertext, expected4, sizeof(expected4)), "wrong data\n");
|
|
|
|
ok(!memcmp(tag, expected_tag, sizeof(expected_tag)), "wrong tag\n");
|
|
|
|
for (i = 0; i < 32; i++)
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ciphertext[i] == expected4[i], "%lu: %02x != %02x\n", i, ciphertext[i], expected4[i]);
|
2018-03-23 14:06:09 +01:00
|
|
|
for (i = 0; i < 16; i++)
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(tag[i] == expected_tag[i], "%lu: %02x != %02x\n", i, tag[i], expected_tag[i]);
|
2018-03-23 14:06:09 +01:00
|
|
|
|
2018-03-19 12:16:08 +01:00
|
|
|
/* input size is not multiple of block size */
|
|
|
|
size = 0;
|
|
|
|
memcpy(ivbuf, iv, sizeof(iv));
|
|
|
|
memset(ciphertext, 0xff, sizeof(ciphertext));
|
|
|
|
memset(tag, 0xff, sizeof(tag));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptEncrypt(key, data2, 24, &auth_info, ivbuf, 16, ciphertext, 24, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
|
|
|
ok(size == 24, "got %lu\n", size);
|
2018-03-20 21:27:06 +01:00
|
|
|
ok(!memcmp(ciphertext, expected4, 24), "wrong data\n");
|
2018-03-20 21:27:09 +01:00
|
|
|
ok(!memcmp(tag, expected_tag2, sizeof(expected_tag2)), "wrong tag\n");
|
2018-03-19 12:16:08 +01:00
|
|
|
for (i = 0; i < 24; i++)
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ciphertext[i] == expected4[i], "%lu: %02x != %02x\n", i, ciphertext[i], expected4[i]);
|
2018-03-19 12:16:08 +01:00
|
|
|
for (i = 0; i < 16; i++)
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(tag[i] == expected_tag2[i], "%lu: %02x != %02x\n", i, tag[i], expected_tag2[i]);
|
2018-03-19 12:16:08 +01:00
|
|
|
|
2018-03-22 11:33:38 +01:00
|
|
|
/* test with auth data */
|
|
|
|
auth_info.pbAuthData = auth_data;
|
|
|
|
auth_info.cbAuthData = sizeof(auth_data);
|
|
|
|
|
|
|
|
size = 0;
|
|
|
|
memcpy(ivbuf, iv, sizeof(iv));
|
|
|
|
memset(ciphertext, 0xff, sizeof(ciphertext));
|
|
|
|
memset(tag, 0xff, sizeof(tag));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptEncrypt(key, data2, 32, &auth_info, ivbuf, 16, ciphertext, 32, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
|
|
|
ok(size == 32, "got %lu\n", size);
|
2018-03-22 11:33:38 +01:00
|
|
|
ok(!memcmp(ciphertext, expected4, sizeof(expected4)), "wrong data\n");
|
|
|
|
ok(!memcmp(tag, expected_tag3, sizeof(expected_tag3)), "wrong tag\n");
|
|
|
|
for (i = 0; i < 32; i++)
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ciphertext[i] == expected4[i], "%lu: %02x != %02x\n", i, ciphertext[i], expected4[i]);
|
2018-03-22 11:33:38 +01:00
|
|
|
for (i = 0; i < 16; i++)
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(tag[i] == expected_tag3[i], "%lu: %02x != %02x\n", i, tag[i], expected_tag3[i]);
|
2018-03-22 11:33:38 +01:00
|
|
|
|
2018-03-22 11:33:40 +01:00
|
|
|
memset(tag, 0xff, sizeof(tag));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptEncrypt(key, data2, 0, &auth_info, ivbuf, 16, NULL, 0, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
|
|
|
ok(!size, "got %lu\n", size);
|
2018-03-22 11:33:40 +01:00
|
|
|
for (i = 0; i < 16; i++)
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(tag[i] == 0xff, "%lu: %02x != %02x\n", i, tag[i], 0xff);
|
2018-03-22 11:33:40 +01:00
|
|
|
|
|
|
|
memset(tag, 0xff, sizeof(tag));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptEncrypt(key, NULL, 0, &auth_info, ivbuf, 16, NULL, 0, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
|
|
|
ok(!size, "got %lu\n", size);
|
2018-03-22 11:33:40 +01:00
|
|
|
ok(!memcmp(tag, expected_tag4, sizeof(expected_tag4)), "wrong tag\n");
|
|
|
|
for (i = 0; i < 16; i++)
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(tag[i] == expected_tag4[i], "%lu: %02x != %02x\n", i, tag[i], expected_tag4[i]);
|
2018-03-22 11:33:40 +01:00
|
|
|
|
2018-03-19 12:16:08 +01:00
|
|
|
/* test with padding */
|
|
|
|
memcpy(ivbuf, iv, sizeof(iv));
|
|
|
|
memset(ciphertext, 0, sizeof(ciphertext));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptEncrypt(key, data2, 32, &auth_info, ivbuf, 16, ciphertext, 32, &size, BCRYPT_BLOCK_PADDING);
|
2022-02-09 10:57:53 +01:00
|
|
|
todo_wine ok(ret == STATUS_BUFFER_TOO_SMALL, "got %#lx\n", ret);
|
2018-03-19 12:16:08 +01:00
|
|
|
|
|
|
|
memcpy(ivbuf, iv, sizeof(iv));
|
|
|
|
memset(ciphertext, 0, sizeof(ciphertext));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptEncrypt(key, data2, 32, &auth_info, ivbuf, 16, ciphertext, 48, &size, BCRYPT_BLOCK_PADDING);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_INVALID_PARAMETER, "got %#lx\n", ret);
|
2018-03-19 12:16:08 +01:00
|
|
|
|
2021-04-20 16:53:34 +02:00
|
|
|
/* race test */
|
|
|
|
|
|
|
|
encrypt_race_start_barrier = 0;
|
|
|
|
hthread = CreateThread(NULL, 0, encrypt_race_thread, key, 0, NULL);
|
|
|
|
|
|
|
|
while (InterlockedCompareExchange(&encrypt_race_start_barrier, 2, 1) != 1)
|
|
|
|
;
|
|
|
|
|
|
|
|
for (test = 0; test < RACE_TEST_COUNT; ++test)
|
|
|
|
{
|
|
|
|
size = 0;
|
|
|
|
memset(ciphertext, 0xff, sizeof(ciphertext));
|
|
|
|
memset(tag, 0xff, sizeof(tag));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptEncrypt(key, data2, 32, &auth_info, NULL, 0, ciphertext, 32, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
|
|
|
ok(size == 32, "got %lu\n", size);
|
2021-04-20 16:53:34 +02:00
|
|
|
ok(!memcmp(ciphertext, expected4, sizeof(expected4)), "wrong data\n");
|
|
|
|
ok(!memcmp(tag, expected_tag3, sizeof(expected_tag2)), "wrong tag\n");
|
|
|
|
for (i = 0; i < 32; i++)
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ciphertext[i] == expected4[i], "%lu: %02x != %02x\n", i, ciphertext[i], expected4[i]);
|
2021-04-20 16:53:34 +02:00
|
|
|
for (i = 0; i < 16; i++)
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(tag[i] == expected_tag3[i], "%lu: %02x != %02x\n", i, tag[i], expected_tag3[i]);
|
2021-04-20 16:53:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
WaitForSingleObject(hthread, INFINITE);
|
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDestroyKey(key);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2018-03-19 12:16:08 +01:00
|
|
|
HeapFree(GetProcessHeap(), 0, buf);
|
|
|
|
|
2018-03-23 14:06:09 +01:00
|
|
|
/******************
|
|
|
|
* AES - ECB mode *
|
|
|
|
******************/
|
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptSetProperty(aes, BCRYPT_CHAINING_MODE, (UCHAR*)BCRYPT_CHAIN_MODE_ECB, sizeof(BCRYPT_CHAIN_MODE_ECB), 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2018-03-23 14:06:09 +01:00
|
|
|
|
|
|
|
len = 0xdeadbeef;
|
|
|
|
size = sizeof(len);
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptGetProperty(aes, BCRYPT_OBJECT_LENGTH, (UCHAR *)&len, sizeof(len), &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2018-03-23 14:06:09 +01:00
|
|
|
|
|
|
|
buf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptGenerateSymmetricKey(aes, &key, buf, len, secret, sizeof(secret), 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2018-03-23 14:06:09 +01:00
|
|
|
|
|
|
|
/* initialization vector is not allowed */
|
|
|
|
size = 0;
|
|
|
|
memcpy(ivbuf, iv, sizeof(iv));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptEncrypt(key, data, 16, NULL, ivbuf, 16, ciphertext, 16, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_INVALID_PARAMETER, "got %#lx\n", ret);
|
|
|
|
ok(size == 16, "got %lu\n", size);
|
2018-03-23 14:06:09 +01:00
|
|
|
|
|
|
|
/* input size is a multiple of block size */
|
|
|
|
size = 0;
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptEncrypt(key, data, 16, NULL, NULL, 16, NULL, 0, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
|
|
|
ok(size == 16, "got %lu\n", size);
|
2018-03-23 14:06:09 +01:00
|
|
|
|
|
|
|
size = 0;
|
|
|
|
memset(ciphertext, 0, sizeof(ciphertext));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptEncrypt(key, data, 16, NULL, NULL, 16, ciphertext, 16, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
|
|
|
ok(size == 16, "got %lu\n", size);
|
2018-03-23 14:06:09 +01:00
|
|
|
ok(!memcmp(ciphertext, expected5, sizeof(expected5)), "wrong data\n");
|
|
|
|
for (i = 0; i < 16; i++)
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ciphertext[i] == expected5[i], "%lu: %02x != %02x\n", i, ciphertext[i], expected5[i]);
|
2018-03-23 14:06:09 +01:00
|
|
|
|
|
|
|
/* input size is not a multiple of block size */
|
|
|
|
size = 0;
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptEncrypt(key, data, 17, NULL, NULL, 16, NULL, 0, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_INVALID_BUFFER_SIZE, "got %#lx\n", ret);
|
|
|
|
ok(size == 17, "got %lu\n", size);
|
2018-03-23 14:06:09 +01:00
|
|
|
|
|
|
|
/* input size is not a multiple of block size, block padding set */
|
|
|
|
size = 0;
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptEncrypt(key, data, 17, NULL, NULL, 16, NULL, 0, &size, BCRYPT_BLOCK_PADDING);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
|
|
|
ok(size == 32, "got %lu\n", size);
|
2018-03-23 14:06:09 +01:00
|
|
|
|
|
|
|
size = 0;
|
|
|
|
memset(ciphertext, 0, sizeof(ciphertext));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptEncrypt(key, data, 17, NULL, NULL, 16, ciphertext, 32, &size, BCRYPT_BLOCK_PADDING);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
|
|
|
ok(size == 32, "got %lu\n", size);
|
2018-03-23 14:06:09 +01:00
|
|
|
ok(!memcmp(ciphertext, expected6, sizeof(expected6)), "wrong data\n");
|
|
|
|
for (i = 0; i < 32; i++)
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ciphertext[i] == expected6[i], "%lu: %02x != %02x\n", i, ciphertext[i], expected6[i]);
|
2018-03-23 14:06:09 +01:00
|
|
|
|
|
|
|
/* input size is a multiple of block size, block padding set */
|
|
|
|
size = 0;
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptEncrypt(key, data2, 32, NULL, NULL, 16, NULL, 0, &size, BCRYPT_BLOCK_PADDING);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
|
|
|
ok(size == 48, "got %lu\n", size);
|
2018-03-23 14:06:09 +01:00
|
|
|
|
|
|
|
size = 0;
|
|
|
|
memset(ciphertext, 0, sizeof(ciphertext));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptEncrypt(key, data2, 32, NULL, NULL, 16, ciphertext, 48, &size, BCRYPT_BLOCK_PADDING);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
|
|
|
ok(size == 48, "got %lu\n", size);
|
2018-03-23 14:06:09 +01:00
|
|
|
ok(!memcmp(ciphertext, expected7, sizeof(expected7)), "wrong data\n");
|
|
|
|
for (i = 0; i < 48; i++)
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ciphertext[i] == expected7[i], "%lu: %02x != %02x\n", i, ciphertext[i], expected7[i]);
|
2018-03-23 14:06:09 +01:00
|
|
|
|
|
|
|
/* output size too small */
|
|
|
|
size = 0;
|
|
|
|
memset(ciphertext, 0, sizeof(ciphertext));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptEncrypt(key, data, 17, NULL, NULL, 16, ciphertext, 31, &size, BCRYPT_BLOCK_PADDING);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_BUFFER_TOO_SMALL, "got %#lx\n", ret);
|
|
|
|
ok(size == 32, "got %lu\n", size);
|
2018-03-23 14:06:09 +01:00
|
|
|
|
|
|
|
size = 0;
|
|
|
|
memset(ciphertext, 0, sizeof(ciphertext));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptEncrypt(key, data2, 32, NULL, NULL, 16, ciphertext, 32, &size, BCRYPT_BLOCK_PADDING);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_BUFFER_TOO_SMALL, "got %#lx\n", ret);
|
|
|
|
ok(size == 48, "got %lu\n", size);
|
2018-03-23 14:06:09 +01:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDestroyKey(key);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2018-03-23 14:06:09 +01:00
|
|
|
HeapFree(GetProcessHeap(), 0, buf);
|
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptCloseAlgorithmProvider(aes, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2016-12-02 21:36:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_BCryptDecrypt(void)
|
|
|
|
{
|
2018-03-19 12:16:08 +01:00
|
|
|
static UCHAR nonce[] =
|
2018-03-22 11:33:38 +01:00
|
|
|
{0x10,0x20,0x30,0x40,0x50,0x60,0x10,0x20,0x30,0x40,0x50,0x60};
|
|
|
|
static UCHAR auth_data[] =
|
|
|
|
{0x60,0x50,0x40,0x30,0x20,0x10,0x60,0x50,0x40,0x30,0x20,0x10};
|
2016-12-02 21:36:43 +01:00
|
|
|
static UCHAR secret[] =
|
|
|
|
{0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f};
|
|
|
|
static UCHAR iv[] =
|
|
|
|
{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};
|
2017-08-16 11:23:48 +02:00
|
|
|
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};
|
2016-12-02 21:36:43 +01:00
|
|
|
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};
|
2017-08-16 11:23:48 +02:00
|
|
|
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};
|
2018-03-19 12:16:08 +01:00
|
|
|
static UCHAR ciphertext4[] =
|
|
|
|
{0xe1,0x82,0xc3,0xc0,0x24,0xfb,0x86,0x85,0xf3,0xf1,0x2b,0x7d,0x09,0xb4,0x73,0x67,
|
|
|
|
0x86,0x64,0xc3,0xfe,0xa3,0x07,0x61,0xf8,0x16,0xc9,0x78,0x7f,0xe7,0xb1,0xc4,0x94};
|
2018-03-23 14:06:09 +01:00
|
|
|
static UCHAR ciphertext5[] =
|
|
|
|
{0x0a,0x94,0x0b,0xb5,0x41,0x6e,0xf0,0x45,0xf1,0xc3,0x94,0x58,0xc6,0x53,0xea,0x5a,
|
|
|
|
0x84,0x07,0x66,0xb7,0x49,0xc0,0x9b,0x49,0x74,0x28,0x8c,0x10,0xb9,0xc2,0x09,0x70};
|
|
|
|
static UCHAR ciphertext6[] =
|
|
|
|
{0x0a,0x94,0x0b,0xb5,0x41,0x6e,0xf0,0x45,0xf1,0xc3,0x94,0x58,0xc6,0x53,0xea,0x5a,
|
|
|
|
0x95,0x4f,0x64,0xf2,0xe4,0xe8,0x6e,0x9e,0xee,0x82,0xd2,0x02,0x16,0x68,0x48,0x99,
|
|
|
|
0x95,0x4f,0x64,0xf2,0xe4,0xe8,0x6e,0x9e,0xee,0x82,0xd2,0x02,0x16,0x68,0x48,0x99};
|
2018-03-19 12:16:08 +01:00
|
|
|
static UCHAR tag[] =
|
|
|
|
{0x89,0xb3,0x92,0x00,0x39,0x20,0x09,0xb4,0x6a,0xd6,0xaf,0xca,0x4b,0x5b,0xfd,0xd0};
|
2018-03-22 11:33:38 +01:00
|
|
|
static UCHAR tag2[] =
|
|
|
|
{0x17,0x9d,0xc0,0x7a,0xf0,0xcf,0xaa,0xd5,0x1c,0x11,0xc4,0x4b,0xd6,0xa3,0x3e,0x77};
|
2018-03-19 12:16:08 +01:00
|
|
|
BCRYPT_AUTHENTICATED_CIPHER_MODE_INFO auth_info;
|
2017-08-17 13:36:39 +02:00
|
|
|
BCRYPT_KEY_LENGTHS_STRUCT key_lengths;
|
2018-12-04 08:59:20 +01:00
|
|
|
BCRYPT_AUTH_TAG_LENGTHS_STRUCT tag_lengths;
|
2016-12-02 21:36:43 +01:00
|
|
|
BCRYPT_ALG_HANDLE aes;
|
|
|
|
BCRYPT_KEY_HANDLE key;
|
2017-08-16 11:23:48 +02:00
|
|
|
UCHAR *buf, plaintext[48], ivbuf[16];
|
2016-12-02 21:36:43 +01:00
|
|
|
ULONG size, len;
|
|
|
|
NTSTATUS ret;
|
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptOpenAlgorithmProvider(&aes, BCRYPT_AES_ALGORITHM, NULL, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2016-12-02 21:36:43 +01:00
|
|
|
|
2017-08-17 13:36:39 +02:00
|
|
|
size = 0;
|
|
|
|
memset(&key_lengths, 0, sizeof(key_lengths));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptGetProperty(aes, BCRYPT_KEY_LENGTHS, (UCHAR*)&key_lengths, sizeof(key_lengths), &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
|
|
|
ok(size == sizeof(key_lengths), "got %lu\n", size);
|
|
|
|
ok(key_lengths.dwMinLength == 128, "Expected 128, got %lu\n", key_lengths.dwMinLength);
|
|
|
|
ok(key_lengths.dwMaxLength == 256, "Expected 256, got %lu\n", key_lengths.dwMaxLength);
|
|
|
|
ok(key_lengths.dwIncrement == 64, "Expected 64, got %lu\n", key_lengths.dwIncrement);
|
2017-08-17 13:36:39 +02:00
|
|
|
|
2018-03-19 12:16:08 +01:00
|
|
|
/******************
|
|
|
|
* AES - CBC mode *
|
|
|
|
******************/
|
|
|
|
|
2016-12-02 21:36:43 +01:00
|
|
|
len = 0xdeadbeef;
|
|
|
|
size = sizeof(len);
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptGetProperty(aes, BCRYPT_OBJECT_LENGTH, (UCHAR *)&len, sizeof(len), &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2016-12-02 21:36:43 +01:00
|
|
|
|
2018-03-22 11:33:39 +01:00
|
|
|
key = NULL;
|
2016-12-02 21:36:43 +01:00
|
|
|
buf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptGenerateSymmetricKey(aes, &key, buf, len, secret, sizeof(secret), 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2018-03-22 11:33:39 +01:00
|
|
|
ok(key != NULL, "key not set\n");
|
2016-12-02 21:36:43 +01:00
|
|
|
|
|
|
|
/* input size is a multiple of block size */
|
|
|
|
size = 0;
|
|
|
|
memcpy(ivbuf, iv, sizeof(iv));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDecrypt(key, ciphertext, 32, NULL, ivbuf, 16, NULL, 0, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
|
|
|
ok(size == 32, "got %lu\n", size);
|
2016-12-02 21:36:43 +01:00
|
|
|
|
|
|
|
size = 0;
|
|
|
|
memcpy(ivbuf, iv, sizeof(iv));
|
|
|
|
memset(plaintext, 0, sizeof(plaintext));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDecrypt(key, ciphertext, 32, NULL, ivbuf, 16, plaintext, 32, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
|
|
|
ok(size == 32, "got %lu\n", size);
|
2016-12-02 21:36:43 +01:00
|
|
|
ok(!memcmp(plaintext, expected, sizeof(expected)), "wrong data\n");
|
|
|
|
|
2017-08-16 11:23:48 +02:00
|
|
|
/* test with padding smaller than block size */
|
|
|
|
size = 0;
|
|
|
|
memcpy(ivbuf, iv, sizeof(iv));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDecrypt(key, ciphertext2, 32, NULL, ivbuf, 16, NULL, 0, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
|
|
|
ok(size == 32, "got %lu\n", size);
|
2017-08-16 11:23:48 +02:00
|
|
|
|
|
|
|
size = 0;
|
|
|
|
memcpy(ivbuf, iv, sizeof(iv));
|
|
|
|
memset(plaintext, 0, sizeof(plaintext));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDecrypt(key, ciphertext2, 32, NULL, ivbuf, 16, plaintext, 17, &size, BCRYPT_BLOCK_PADDING);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
|
|
|
ok(size == 17, "got %lu\n", size);
|
2017-08-16 11:23:48 +02:00
|
|
|
ok(!memcmp(plaintext, expected2, sizeof(expected2)), "wrong data\n");
|
|
|
|
|
|
|
|
/* test with padding of block size */
|
|
|
|
size = 0;
|
|
|
|
memcpy(ivbuf, iv, sizeof(iv));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDecrypt(key, ciphertext3, 48, NULL, ivbuf, 16, NULL, 0, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
|
|
|
ok(size == 48, "got %lu\n", size);
|
2017-08-16 11:23:48 +02:00
|
|
|
|
|
|
|
size = 0;
|
|
|
|
memcpy(ivbuf, iv, sizeof(iv));
|
|
|
|
memset(plaintext, 0, sizeof(plaintext));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDecrypt(key, ciphertext3, 48, NULL, ivbuf, 16, plaintext, 32, &size, BCRYPT_BLOCK_PADDING);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
|
|
|
ok(size == 32, "got %lu\n", size);
|
2017-08-16 11:23:48 +02:00
|
|
|
ok(!memcmp(plaintext, expected3, sizeof(expected3)), "wrong data\n");
|
|
|
|
|
2016-12-02 21:36:43 +01:00
|
|
|
/* output size too small */
|
|
|
|
size = 0;
|
|
|
|
memcpy(ivbuf, iv, sizeof(iv));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDecrypt(key, ciphertext, 32, NULL, ivbuf, 16, plaintext, 31, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_BUFFER_TOO_SMALL, "got %#lx\n", ret);
|
|
|
|
ok(size == 32, "got %lu\n", size);
|
2016-12-02 21:36:43 +01:00
|
|
|
|
2017-08-16 11:23:48 +02:00
|
|
|
size = 0;
|
|
|
|
memcpy(ivbuf, iv, sizeof(iv));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDecrypt(key, ciphertext2, 32, NULL, ivbuf, 16, plaintext, 15, &size, BCRYPT_BLOCK_PADDING);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_BUFFER_TOO_SMALL, "got %#lx\n", ret);
|
|
|
|
ok(size == 32, "got %lu\n", size);
|
2017-08-16 11:23:48 +02:00
|
|
|
|
|
|
|
size = 0;
|
|
|
|
memcpy(ivbuf, iv, sizeof(iv));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDecrypt(key, ciphertext2, 32, NULL, ivbuf, 16, plaintext, 16, &size, BCRYPT_BLOCK_PADDING);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_BUFFER_TOO_SMALL, "got %#lx\n", ret);
|
|
|
|
ok(size == 17, "got %lu\n", size);
|
2017-08-16 11:23:48 +02:00
|
|
|
|
|
|
|
size = 0;
|
|
|
|
memcpy(ivbuf, iv, sizeof(iv));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDecrypt(key, ciphertext3, 48, NULL, ivbuf, 16, plaintext, 31, &size, BCRYPT_BLOCK_PADDING);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_BUFFER_TOO_SMALL, "got %#lx\n", ret);
|
|
|
|
ok(size == 48, "got %lu\n", size);
|
2017-08-16 11:23:48 +02:00
|
|
|
|
2016-12-02 21:36:43 +01:00
|
|
|
/* input size is not a multiple of block size */
|
|
|
|
size = 0;
|
|
|
|
memcpy(ivbuf, iv, sizeof(iv));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDecrypt(key, ciphertext, 17, NULL, ivbuf, 16, NULL, 0, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_INVALID_BUFFER_SIZE, "got %#lx\n", ret);
|
|
|
|
ok(size == 17 || broken(size == 0 /* Win < 7 */), "got %lu\n", size);
|
2016-12-02 21:36:43 +01:00
|
|
|
|
|
|
|
/* input size is not a multiple of block size, block padding set */
|
|
|
|
size = 0;
|
|
|
|
memcpy(ivbuf, iv, sizeof(iv));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDecrypt(key, ciphertext, 17, NULL, ivbuf, 16, NULL, 0, &size, BCRYPT_BLOCK_PADDING);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_INVALID_BUFFER_SIZE, "got %#lx\n", ret);
|
|
|
|
ok(size == 17 || broken(size == 0 /* Win < 7 */), "got %lu\n", size);
|
2016-12-02 21:36:43 +01:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDestroyKey(key);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2016-12-02 21:36:43 +01:00
|
|
|
HeapFree(GetProcessHeap(), 0, buf);
|
|
|
|
|
2018-03-19 12:16:08 +01:00
|
|
|
/******************
|
|
|
|
* AES - GCM mode *
|
|
|
|
******************/
|
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptSetProperty(aes, BCRYPT_CHAINING_MODE, (UCHAR*)BCRYPT_CHAIN_MODE_GCM, sizeof(BCRYPT_CHAIN_MODE_GCM), 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2018-03-19 12:16:08 +01:00
|
|
|
|
2018-03-22 11:33:39 +01:00
|
|
|
key = NULL;
|
2018-03-19 12:16:08 +01:00
|
|
|
buf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptGenerateSymmetricKey(aes, &key, buf, len, secret, sizeof(secret), 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2018-03-22 11:33:39 +01:00
|
|
|
ok(key != NULL, "key not set\n");
|
2018-03-19 12:16:08 +01:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptGetProperty(key, BCRYPT_AUTH_TAG_LENGTH, (UCHAR*)&tag_lengths, sizeof(tag_lengths), &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_NOT_SUPPORTED, "got %#lx\n", ret);
|
2018-12-04 08:59:20 +01:00
|
|
|
|
2018-03-19 12:16:08 +01:00
|
|
|
memset(&auth_info, 0, sizeof(auth_info));
|
|
|
|
auth_info.cbSize = sizeof(auth_info);
|
|
|
|
auth_info.dwInfoVersion = 1;
|
|
|
|
auth_info.pbNonce = nonce;
|
|
|
|
auth_info.cbNonce = sizeof(nonce);
|
|
|
|
auth_info.pbTag = tag;
|
|
|
|
auth_info.cbTag = sizeof(tag);
|
|
|
|
|
|
|
|
/* input size is a multiple of block size */
|
|
|
|
size = 0;
|
|
|
|
memcpy(ivbuf, iv, sizeof(iv));
|
|
|
|
memset(plaintext, 0, sizeof(plaintext));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDecrypt(key, ciphertext4, 32, &auth_info, ivbuf, 16, plaintext, 32, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
|
|
|
ok(size == 32, "got %lu\n", size);
|
2018-03-20 21:27:08 +01:00
|
|
|
ok(!memcmp(plaintext, expected3, sizeof(expected3)), "wrong data\n");
|
2018-03-19 12:16:08 +01:00
|
|
|
|
2018-03-22 11:33:38 +01:00
|
|
|
/* test with auth data */
|
|
|
|
auth_info.pbAuthData = auth_data;
|
|
|
|
auth_info.cbAuthData = sizeof(auth_data);
|
|
|
|
auth_info.pbTag = tag2;
|
|
|
|
auth_info.cbTag = sizeof(tag2);
|
|
|
|
|
|
|
|
size = 0;
|
|
|
|
memcpy(ivbuf, iv, sizeof(iv));
|
|
|
|
memset(plaintext, 0, sizeof(plaintext));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDecrypt(key, ciphertext4, 32, &auth_info, ivbuf, 16, plaintext, 32, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
|
|
|
ok(size == 32, "got %lu\n", size);
|
2018-03-22 11:33:38 +01:00
|
|
|
ok(!memcmp(plaintext, expected3, sizeof(expected3)), "wrong data\n");
|
|
|
|
|
2018-03-19 12:16:08 +01:00
|
|
|
/* test with wrong tag */
|
|
|
|
memcpy(ivbuf, iv, sizeof(iv));
|
|
|
|
auth_info.pbTag = iv; /* wrong tag */
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDecrypt(key, ciphertext4, 32, &auth_info, ivbuf, 16, plaintext, 32, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_AUTH_TAG_MISMATCH, "got %#lx\n", ret);
|
|
|
|
ok(size == 32, "got %lu\n", size);
|
2018-03-19 12:16:08 +01:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDestroyKey(key);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2018-03-19 12:16:08 +01:00
|
|
|
HeapFree(GetProcessHeap(), 0, buf);
|
|
|
|
|
2018-03-23 14:06:09 +01:00
|
|
|
/******************
|
|
|
|
* AES - ECB mode *
|
|
|
|
******************/
|
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptSetProperty(aes, BCRYPT_CHAINING_MODE, (UCHAR*)BCRYPT_CHAIN_MODE_ECB, sizeof(BCRYPT_CHAIN_MODE_ECB), 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2018-03-23 14:06:09 +01:00
|
|
|
|
|
|
|
len = 0xdeadbeef;
|
|
|
|
size = sizeof(len);
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptGetProperty(aes, BCRYPT_OBJECT_LENGTH, (UCHAR *)&len, sizeof(len), &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2018-03-23 14:06:09 +01:00
|
|
|
|
|
|
|
buf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptGenerateSymmetricKey(aes, &key, buf, len, secret, sizeof(secret), 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2018-03-23 14:06:09 +01:00
|
|
|
|
|
|
|
/* initialization vector is not allowed */
|
|
|
|
size = 0;
|
|
|
|
memcpy(ivbuf, iv, sizeof(iv));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDecrypt(key, ciphertext5, 32, NULL, ivbuf, 16, plaintext, 32, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_INVALID_PARAMETER, "got %#lx\n", ret);
|
|
|
|
ok(size == 32, "got %lu\n", size);
|
2018-03-23 14:06:09 +01:00
|
|
|
|
|
|
|
/* input size is a multiple of block size */
|
|
|
|
size = 0;
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDecrypt(key, ciphertext5, 32, NULL, NULL, 16, NULL, 0, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
|
|
|
ok(size == 32, "got %lu\n", size);
|
2018-03-23 14:06:09 +01:00
|
|
|
|
|
|
|
size = 0;
|
|
|
|
memset(plaintext, 0, sizeof(plaintext));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDecrypt(key, ciphertext5, 32, NULL, NULL, 16, plaintext, 32, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
|
|
|
ok(size == 32, "got %lu\n", size);
|
2018-03-23 14:06:09 +01:00
|
|
|
ok(!memcmp(plaintext, expected, sizeof(expected)), "wrong data\n");
|
|
|
|
|
|
|
|
/* test with padding smaller than block size */
|
|
|
|
size = 0;
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDecrypt(key, ciphertext5, 32, NULL, NULL, 16, NULL, 0, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
|
|
|
ok(size == 32, "got %lu\n", size);
|
2018-03-23 14:06:09 +01:00
|
|
|
|
|
|
|
size = 0;
|
|
|
|
memset(plaintext, 0, sizeof(plaintext));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDecrypt(key, ciphertext5, 32, NULL, NULL, 16, plaintext, 17, &size, BCRYPT_BLOCK_PADDING);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
|
|
|
ok(size == 17, "got %lu\n", size);
|
2018-03-23 14:06:09 +01:00
|
|
|
ok(!memcmp(plaintext, expected2, sizeof(expected2)), "wrong data\n");
|
|
|
|
|
|
|
|
/* test with padding of block size */
|
|
|
|
size = 0;
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDecrypt(key, ciphertext6, 48, NULL, NULL, 16, NULL, 0, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
|
|
|
ok(size == 48, "got %lu\n", size);
|
2018-03-23 14:06:09 +01:00
|
|
|
|
|
|
|
size = 0;
|
|
|
|
memset(plaintext, 0, sizeof(plaintext));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDecrypt(key, ciphertext6, 48, NULL, NULL, 16, plaintext, 32, &size, BCRYPT_BLOCK_PADDING);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
|
|
|
ok(size == 32, "got %lu\n", size);
|
2018-03-23 14:06:09 +01:00
|
|
|
ok(!memcmp(plaintext, expected3, sizeof(expected3)), "wrong data\n");
|
|
|
|
|
|
|
|
/* output size too small */
|
|
|
|
size = 0;
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDecrypt(key, ciphertext4, 32, NULL, NULL, 16, plaintext, 31, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_BUFFER_TOO_SMALL, "got %#lx\n", ret);
|
|
|
|
ok(size == 32, "got %lu\n", size);
|
2018-03-23 14:06:09 +01:00
|
|
|
|
|
|
|
size = 0;
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDecrypt(key, ciphertext5, 32, NULL, NULL, 16, plaintext, 15, &size, BCRYPT_BLOCK_PADDING);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_BUFFER_TOO_SMALL, "got %#lx\n", ret);
|
|
|
|
ok(size == 32, "got %lu\n", size);
|
2018-03-23 14:06:09 +01:00
|
|
|
|
|
|
|
size = 0;
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDecrypt(key, ciphertext5, 32, NULL, NULL, 16, plaintext, 16, &size, BCRYPT_BLOCK_PADDING);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_BUFFER_TOO_SMALL, "got %#lx\n", ret);
|
|
|
|
ok(size == 17, "got %lu\n", size);
|
2018-03-23 14:06:09 +01:00
|
|
|
|
|
|
|
size = 0;
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDecrypt(key, ciphertext6, 48, NULL, NULL, 16, plaintext, 31, &size, BCRYPT_BLOCK_PADDING);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_BUFFER_TOO_SMALL, "got %#lx\n", ret);
|
|
|
|
ok(size == 48, "got %lu\n", size);
|
2018-03-23 14:06:09 +01:00
|
|
|
|
|
|
|
/* input size is not a multiple of block size */
|
|
|
|
size = 0;
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDecrypt(key, ciphertext4, 17, NULL, NULL, 16, NULL, 0, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_INVALID_BUFFER_SIZE, "got %#lx\n", ret);
|
|
|
|
ok(size == 17 || broken(size == 0 /* Win < 7 */), "got %lu\n", size);
|
2018-03-23 14:06:09 +01:00
|
|
|
|
|
|
|
/* input size is not a multiple of block size, block padding set */
|
|
|
|
size = 0;
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDecrypt(key, ciphertext4, 17, NULL, NULL, 16, NULL, 0, &size, BCRYPT_BLOCK_PADDING);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_INVALID_BUFFER_SIZE, "got %#lx\n", ret);
|
|
|
|
ok(size == 17 || broken(size == 0 /* Win < 7 */), "got %lu\n", size);
|
2018-03-23 14:06:09 +01:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDestroyKey(key);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2018-03-23 14:06:09 +01:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDestroyKey(key);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_INVALID_HANDLE, "got %#lx\n", ret);
|
2018-03-23 14:06:09 +01:00
|
|
|
HeapFree(GetProcessHeap(), 0, buf);
|
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDestroyKey(NULL);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_INVALID_HANDLE, "got %#lx\n", ret);
|
2018-12-17 09:47:58 +01:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptCloseAlgorithmProvider(aes, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2018-12-17 09:47:58 +01:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptCloseAlgorithmProvider(aes, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_INVALID_HANDLE, "got %#lx\n", ret);
|
2018-12-17 09:47:58 +01:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptCloseAlgorithmProvider(NULL, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_INVALID_HANDLE, "got %#lx\n", ret);
|
2016-12-02 21:36:43 +01:00
|
|
|
}
|
|
|
|
|
2018-02-28 13:33:06 +01:00
|
|
|
static void test_key_import_export(void)
|
|
|
|
{
|
|
|
|
UCHAR buffer1[sizeof(BCRYPT_KEY_DATA_BLOB_HEADER) + 16];
|
2018-03-29 12:26:16 +02:00
|
|
|
UCHAR buffer2[sizeof(BCRYPT_KEY_DATA_BLOB_HEADER) + 16], *buf;
|
2018-02-28 13:33:06 +01:00
|
|
|
BCRYPT_KEY_DATA_BLOB_HEADER *key_data1 = (void*)buffer1;
|
|
|
|
BCRYPT_ALG_HANDLE aes;
|
|
|
|
BCRYPT_KEY_HANDLE key;
|
|
|
|
NTSTATUS ret;
|
|
|
|
ULONG size;
|
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptOpenAlgorithmProvider(&aes, BCRYPT_AES_ALGORITHM, NULL, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2018-02-28 13:33:06 +01:00
|
|
|
|
|
|
|
key_data1->dwMagic = BCRYPT_KEY_DATA_BLOB_MAGIC;
|
|
|
|
key_data1->dwVersion = BCRYPT_KEY_DATA_BLOB_VERSION1;
|
|
|
|
key_data1->cbKeyData = 16;
|
|
|
|
memset(&key_data1[1], 0x11, 16);
|
|
|
|
|
2018-03-29 12:26:16 +02:00
|
|
|
key = NULL;
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptImportKey(aes, NULL, BCRYPT_KEY_DATA_BLOB, &key, NULL, 0, buffer1, sizeof(buffer1), 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS || broken(ret == STATUS_INVALID_PARAMETER) /* vista */, "got %#lx\n", ret);
|
2018-02-28 13:33:06 +01:00
|
|
|
if (ret == STATUS_INVALID_PARAMETER)
|
|
|
|
{
|
|
|
|
win_skip("broken BCryptImportKey\n");
|
|
|
|
return;
|
|
|
|
}
|
2018-03-29 12:26:16 +02:00
|
|
|
ok(key != NULL, "key not set\n");
|
2018-02-28 13:33:06 +01:00
|
|
|
|
|
|
|
size = 0;
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptExportKey(key, NULL, BCRYPT_KEY_DATA_BLOB, buffer2, 0, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_BUFFER_TOO_SMALL, "got %#lx\n", ret);
|
|
|
|
ok(size == sizeof(buffer2), "got %lu\n", size);
|
2018-02-28 13:33:06 +01:00
|
|
|
|
|
|
|
size = 0;
|
|
|
|
memset(buffer2, 0xff, sizeof(buffer2));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptExportKey(key, NULL, BCRYPT_KEY_DATA_BLOB, buffer2, sizeof(buffer2), &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
|
|
|
ok(size == sizeof(buffer2), "Got %lu\n", size);
|
2018-02-28 13:33:06 +01:00
|
|
|
ok(!memcmp(buffer1, buffer2, sizeof(buffer1)), "Expected exported key to match imported key\n");
|
|
|
|
|
2018-03-29 12:26:16 +02:00
|
|
|
/* opaque blob */
|
|
|
|
size = 0;
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptExportKey(key, NULL, BCRYPT_OPAQUE_KEY_BLOB, buffer2, 0, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_BUFFER_TOO_SMALL, "got %#lx\n", ret);
|
2018-03-29 12:26:16 +02:00
|
|
|
ok(size > 0, "got zero\n");
|
|
|
|
|
|
|
|
buf = HeapAlloc(GetProcessHeap(), 0, size);
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptExportKey(key, NULL, BCRYPT_OPAQUE_KEY_BLOB, buf, size, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2018-03-29 12:26:16 +02:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDestroyKey(key);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2018-03-29 12:26:16 +02:00
|
|
|
|
|
|
|
key = NULL;
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptImportKey(aes, NULL, BCRYPT_OPAQUE_KEY_BLOB, &key, NULL, 0, buf, size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2018-03-29 12:26:16 +02:00
|
|
|
ok(key != NULL, "key not set\n");
|
|
|
|
HeapFree(GetProcessHeap(), 0, buf);
|
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDestroyKey(key);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2018-02-28 13:33:06 +01:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptCloseAlgorithmProvider(aes, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2018-02-28 13:33:06 +01:00
|
|
|
}
|
|
|
|
|
2020-07-29 14:34:14 +02:00
|
|
|
static BYTE eccPrivkey[] =
|
|
|
|
{
|
|
|
|
/* X */
|
|
|
|
0x26, 0xff, 0x0e, 0xf9, 0x71, 0x93, 0xf8, 0xed, 0x59, 0xfa, 0x24, 0xec, 0x18, 0x13, 0xfe, 0xf5,
|
|
|
|
0x0b, 0x4a, 0xb1, 0x27, 0xb7, 0xab, 0x3e, 0x4f, 0xc5, 0x5a, 0x91, 0xa3, 0x6e, 0x21, 0x61, 0x65,
|
|
|
|
/* Y */
|
|
|
|
0x62, 0x7b, 0x8b, 0x30, 0x7a, 0x63, 0x4c, 0x1a, 0xf4, 0x54, 0x54, 0xbb, 0x75, 0x59, 0x68, 0x36,
|
|
|
|
0xfe, 0x49, 0x95, 0x75, 0x9e, 0x20, 0x3e, 0x69, 0x58, 0xb9, 0x7a, 0x84, 0x03, 0x45, 0x5c, 0x10,
|
|
|
|
/* d */
|
|
|
|
0xb9, 0xcd, 0xbe, 0xd4, 0x75, 0x5d, 0x05, 0xe5, 0x83, 0x0c, 0xd3, 0x37, 0x34, 0x15, 0xe3, 0x2c,
|
|
|
|
0xe5, 0x85, 0x15, 0xa9, 0xee, 0xba, 0x94, 0x03, 0x03, 0x0b, 0x86, 0xea, 0x85, 0x40, 0xbd, 0x35,
|
|
|
|
};
|
2018-03-23 14:06:11 +01:00
|
|
|
static BYTE eccPubkey[] =
|
|
|
|
{
|
|
|
|
/* X */
|
|
|
|
0x3b, 0x3c, 0x34, 0xc8, 0x3f, 0x15, 0xea, 0x02, 0x68, 0x46, 0x69, 0xdf, 0x0c, 0xa6, 0xee, 0x7a,
|
|
|
|
0xd9, 0x82, 0x08, 0x9b, 0x37, 0x53, 0x42, 0xf3, 0x13, 0x63, 0xda, 0x65, 0x79, 0xe8, 0x04, 0x9e,
|
|
|
|
/* Y */
|
|
|
|
0x8c, 0x77, 0xc4, 0x33, 0x77, 0xd9, 0x5a, 0x7f, 0x60, 0x7b, 0x98, 0xce, 0xf3, 0x96, 0x56, 0xd6,
|
|
|
|
0xb5, 0x8d, 0x87, 0x7a, 0x00, 0x2b, 0xf3, 0x70, 0xb3, 0x90, 0x73, 0xa0, 0x56, 0x06, 0x3b, 0x22,
|
|
|
|
};
|
|
|
|
static BYTE certHash[] =
|
|
|
|
{
|
|
|
|
0x28, 0x19, 0x0f, 0x15, 0x6d, 0x75, 0xcc, 0xcf, 0x62, 0xf1, 0x5e, 0xe6, 0x8a, 0xc3, 0xf0, 0x5d,
|
|
|
|
0x89, 0x28, 0x2d, 0x48, 0xd8, 0x73, 0x7c, 0x05, 0x05, 0x8e, 0xbc, 0xce, 0x28, 0xb7, 0xba, 0xc9,
|
|
|
|
};
|
|
|
|
static BYTE certSignature[] =
|
|
|
|
{
|
|
|
|
/* r */
|
|
|
|
0xd7, 0x29, 0xce, 0x5a, 0xef, 0x74, 0x85, 0xd1, 0x18, 0x5f, 0x6e, 0xf1, 0xba, 0x53, 0xd4, 0xcd,
|
|
|
|
0xdd, 0xe0, 0x5d, 0xf1, 0x5e, 0x48, 0x51, 0xea, 0x63, 0xc0, 0xe8, 0xe2, 0xf6, 0xfa, 0x4c, 0xaf,
|
|
|
|
/* s */
|
|
|
|
0xe3, 0x94, 0x15, 0x3b, 0x6c, 0x71, 0x6e, 0x44, 0x22, 0xcb, 0xa0, 0x88, 0xcd, 0x0a, 0x5a, 0x50,
|
|
|
|
0x29, 0x7c, 0x5c, 0xd6, 0x6c, 0xd2, 0xe0, 0x7f, 0xcd, 0x02, 0x92, 0x21, 0x4c, 0x2c, 0x92, 0xee,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void test_ECDSA(void)
|
|
|
|
{
|
2020-07-29 14:34:14 +02:00
|
|
|
BYTE buffer[sizeof(BCRYPT_ECCKEY_BLOB) + sizeof(eccPrivkey)];
|
2018-03-23 14:06:11 +01:00
|
|
|
BCRYPT_ECCKEY_BLOB *ecckey = (void *)buffer;
|
2020-07-29 14:34:14 +02:00
|
|
|
BCRYPT_ALG_HANDLE alg;
|
|
|
|
BCRYPT_KEY_HANDLE key;
|
2018-03-23 14:06:11 +01:00
|
|
|
NTSTATUS status;
|
2020-07-29 14:34:14 +02:00
|
|
|
ULONG size;
|
2018-03-23 14:06:11 +01:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
status = BCryptOpenAlgorithmProvider(&alg, BCRYPT_ECDSA_P256_ALGORITHM, NULL, 0);
|
2018-03-23 14:06:11 +01:00
|
|
|
if (status)
|
|
|
|
{
|
2022-02-09 10:57:53 +01:00
|
|
|
skip("Failed to open ECDSA provider: %#lx, skipping test\n", status);
|
2018-03-23 14:06:11 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ecckey->dwMagic = BCRYPT_ECDSA_PUBLIC_P256_MAGIC;
|
|
|
|
memcpy(ecckey + 1, eccPubkey, sizeof(eccPubkey));
|
|
|
|
|
|
|
|
ecckey->cbKey = 2;
|
2020-07-29 14:34:14 +02:00
|
|
|
size = sizeof(BCRYPT_ECCKEY_BLOB) + sizeof(eccPubkey);
|
2021-12-03 15:49:30 +01:00
|
|
|
status = BCryptImportKeyPair(alg, NULL, BCRYPT_ECCPUBLIC_BLOB, &key, buffer, size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(status == STATUS_INVALID_PARAMETER, "Expected STATUS_INVALID_PARAMETER, got %#lx\n", status);
|
2018-03-23 14:06:11 +01:00
|
|
|
|
2020-09-02 09:59:46 +02:00
|
|
|
ecckey->dwMagic = BCRYPT_ECDH_PUBLIC_P256_MAGIC;
|
|
|
|
ecckey->cbKey = 32;
|
2021-12-03 15:49:30 +01:00
|
|
|
status = BCryptImportKeyPair(alg, NULL, BCRYPT_ECCPUBLIC_BLOB, &key, buffer, size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(status == STATUS_INVALID_PARAMETER, "Expected STATUS_INVALID_PARAMETER, got %#lx\n", status);
|
2020-09-02 09:59:46 +02:00
|
|
|
|
|
|
|
ecckey->dwMagic = BCRYPT_ECDSA_PUBLIC_P256_MAGIC;
|
2020-07-29 14:34:14 +02:00
|
|
|
ecckey->cbKey = 32;
|
2021-12-03 15:49:30 +01:00
|
|
|
status = BCryptImportKeyPair(alg, NULL, BCRYPT_ECCPUBLIC_BLOB, &key, buffer, size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(!status, "BCryptImportKeyPair failed: %#lx\n", status);
|
2018-03-23 14:06:11 +01:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
status = BCryptVerifySignature(key, NULL, certHash, sizeof(certHash) - 1, certSignature, sizeof(certSignature), 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(status == STATUS_INVALID_SIGNATURE, "Expected STATUS_INVALID_SIGNATURE, got %#lx\n", status);
|
2018-03-23 14:06:11 +01:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
status = BCryptVerifySignature(key, NULL, certHash, sizeof(certHash), certSignature, sizeof(certSignature), 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(!status, "BCryptVerifySignature failed: %#lx\n", status);
|
2021-12-03 15:49:30 +01:00
|
|
|
BCryptDestroyKey(key);
|
2020-07-29 14:34:14 +02:00
|
|
|
|
|
|
|
ecckey->dwMagic = BCRYPT_ECDSA_PRIVATE_P256_MAGIC;
|
|
|
|
memcpy(ecckey + 1, eccPrivkey, sizeof(eccPrivkey));
|
|
|
|
|
|
|
|
ecckey->cbKey = 2;
|
|
|
|
size = sizeof(BCRYPT_ECCKEY_BLOB) + sizeof(eccPrivkey);
|
2021-12-03 15:49:30 +01:00
|
|
|
status = BCryptImportKeyPair(alg, NULL, BCRYPT_ECCPRIVATE_BLOB, &key, buffer, size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(status == STATUS_INVALID_PARAMETER, "Expected STATUS_INVALID_PARAMETER, got %#lx\n", status);
|
2020-07-29 14:34:14 +02:00
|
|
|
|
2020-09-02 09:59:46 +02:00
|
|
|
ecckey->dwMagic = BCRYPT_ECDH_PRIVATE_P256_MAGIC;
|
|
|
|
ecckey->cbKey = 32;
|
2021-12-03 15:49:30 +01:00
|
|
|
status = BCryptImportKeyPair(alg, NULL, BCRYPT_ECCPRIVATE_BLOB, &key, buffer, size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(status == STATUS_INVALID_PARAMETER, "Expected STATUS_INVALID_PARAMETER, got %#lx\n", status);
|
2020-09-02 09:59:46 +02:00
|
|
|
|
|
|
|
ecckey->dwMagic = BCRYPT_ECDSA_PRIVATE_P256_MAGIC;
|
2020-07-29 14:34:14 +02:00
|
|
|
ecckey->cbKey = 32;
|
2021-12-03 15:49:30 +01:00
|
|
|
status = BCryptImportKeyPair(alg, NULL, BCRYPT_ECCPRIVATE_BLOB, &key, buffer, size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(!status, "BCryptImportKeyPair failed: %#lx\n", status);
|
2018-03-23 14:06:11 +01:00
|
|
|
|
2020-09-02 09:59:47 +02:00
|
|
|
memset( buffer, 0, sizeof(buffer) );
|
2021-12-03 15:49:30 +01:00
|
|
|
status = BCryptExportKey(key, NULL, BCRYPT_ECCPRIVATE_BLOB, buffer, size, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(status == STATUS_SUCCESS, "got %#lx\n", status);
|
2020-09-02 09:59:47 +02:00
|
|
|
ecckey = (BCRYPT_ECCKEY_BLOB *)buffer;
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ecckey->dwMagic == BCRYPT_ECDSA_PRIVATE_P256_MAGIC, "got %#lx\n", ecckey->dwMagic);
|
|
|
|
ok(ecckey->cbKey == 32, "got %lu\n", ecckey->cbKey);
|
|
|
|
ok(size == sizeof(*ecckey) + ecckey->cbKey * 3, "got %lu\n", size);
|
2020-09-02 09:59:47 +02:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
BCryptDestroyKey(key);
|
|
|
|
BCryptCloseAlgorithmProvider(alg, 0);
|
2018-03-23 14:06:11 +01:00
|
|
|
}
|
|
|
|
|
2018-03-26 15:04:36 +02:00
|
|
|
static UCHAR rsaPublicBlob[] =
|
|
|
|
{
|
|
|
|
0x52, 0x53, 0x41, 0x31, 0x00, 0x08, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0xad, 0x41, 0x09, 0xa2, 0x56,
|
|
|
|
0x3a, 0x7b, 0x75, 0x4b, 0x72, 0x9b, 0x28, 0x72, 0x3b, 0xae, 0x9f, 0xd8, 0xa8, 0x25, 0x4a, 0x4c,
|
|
|
|
0x19, 0xf5, 0xa6, 0xd0, 0x05, 0x1c, 0x59, 0x8f, 0xe3, 0xf3, 0x2d, 0x29, 0x47, 0xf8, 0x80, 0x25,
|
|
|
|
0x25, 0x21, 0x58, 0xc2, 0xac, 0xa1, 0x9e, 0x93, 0x8e, 0x82, 0x6d, 0xd7, 0xf3, 0xe7, 0x8f, 0x0b,
|
|
|
|
0xc0, 0x41, 0x85, 0x29, 0x3c, 0xf1, 0x0b, 0x2c, 0x5d, 0x49, 0xed, 0xb4, 0x30, 0x6e, 0x02, 0x15,
|
|
|
|
0x4b, 0x9a, 0x08, 0x0d, 0xe1, 0x6f, 0xa8, 0xd3, 0x12, 0xab, 0x66, 0x48, 0x4d, 0xd9, 0x28, 0x03,
|
|
|
|
0x6c, 0x9d, 0x44, 0x7a, 0xed, 0xc9, 0x43, 0x4f, 0x9d, 0x4e, 0x3c, 0x7d, 0x0e, 0xff, 0x07, 0x87,
|
|
|
|
0xeb, 0xca, 0xca, 0x65, 0x6d, 0xbe, 0xc5, 0x31, 0x8b, 0xcc, 0x7e, 0x0a, 0x71, 0x4a, 0x4d, 0x9d,
|
|
|
|
0x3d, 0xfd, 0x7a, 0x56, 0x32, 0x8a, 0x6c, 0x6d, 0x9d, 0x2a, 0xd9, 0x8e, 0x68, 0x89, 0x63, 0xc6,
|
|
|
|
0x4f, 0x24, 0xd1, 0x2a, 0x72, 0x69, 0x08, 0x77, 0xa0, 0x7f, 0xfe, 0xc6, 0x33, 0x8d, 0xb4, 0x7d,
|
|
|
|
0x73, 0x91, 0x13, 0x9c, 0x47, 0x53, 0x6a, 0x13, 0xdf, 0x19, 0xc7, 0xed, 0x48, 0x81, 0xed, 0xd8,
|
|
|
|
0x1f, 0x11, 0x11, 0xbb, 0x41, 0x15, 0x5b, 0xa4, 0xf5, 0xc9, 0x2b, 0x48, 0x5e, 0xd8, 0x4b, 0x52,
|
|
|
|
0x1f, 0xf7, 0x87, 0xf2, 0x68, 0x25, 0x28, 0x79, 0xee, 0x39, 0x41, 0xc9, 0x0e, 0xc8, 0xf9, 0xf2,
|
|
|
|
0xd8, 0x24, 0x09, 0xb4, 0xd4, 0xb7, 0x90, 0xba, 0x26, 0xe8, 0x1d, 0xb4, 0xd7, 0x09, 0x00, 0xc4,
|
|
|
|
0xa0, 0xb6, 0x14, 0xe8, 0x4c, 0x29, 0x60, 0x54, 0x2e, 0x01, 0xde, 0x54, 0x66, 0x40, 0x22, 0x50,
|
|
|
|
0x27, 0xf1, 0xe7, 0x62, 0xa9, 0x00, 0x5a, 0x61, 0x2e, 0xfa, 0xfe, 0x16, 0xd8, 0xe0, 0xe7, 0x66,
|
|
|
|
0x17, 0xda, 0xb8, 0x0c, 0xa6, 0x04, 0x8d, 0xf8, 0x21, 0x68, 0x39
|
|
|
|
};
|
|
|
|
|
|
|
|
static UCHAR rsaHash[] =
|
|
|
|
{
|
|
|
|
0x96, 0x1f, 0xa6, 0x49, 0x58, 0x81, 0x8f, 0x76, 0x77, 0x07, 0x07, 0x27, 0x55, 0xd7, 0x01, 0x8d,
|
|
|
|
0xcd, 0x27, 0x8e, 0x94
|
|
|
|
};
|
|
|
|
|
|
|
|
static UCHAR rsaSignature[] =
|
|
|
|
{
|
|
|
|
0xa8, 0x3a, 0x9d, 0xaf, 0x92, 0x94, 0xa4, 0x4d, 0x34, 0xba, 0x41, 0x0c, 0xc1, 0x23, 0x91, 0xc7,
|
|
|
|
0x91, 0xa8, 0xf8, 0xfc, 0x94, 0x87, 0x4d, 0x05, 0x85, 0x63, 0xe8, 0x7d, 0xea, 0x7f, 0x6b, 0x8d,
|
|
|
|
0xbb, 0x9a, 0xd4, 0x46, 0xa6, 0xc0, 0xd6, 0xdc, 0x91, 0xba, 0xd3, 0x1a, 0xbf, 0xf4, 0x52, 0xa0,
|
|
|
|
0xc7, 0x15, 0x87, 0xe9, 0x1e, 0x60, 0x49, 0x9c, 0xee, 0x5a, 0x9c, 0x6c, 0xbd, 0x7a, 0x3e, 0xc3,
|
|
|
|
0x48, 0xb3, 0xee, 0xca, 0x68, 0x40, 0x9b, 0xa1, 0x4c, 0x6e, 0x20, 0xd6, 0xca, 0x6c, 0x72, 0xaf,
|
|
|
|
0x2b, 0x6b, 0x62, 0x7c, 0x78, 0x06, 0x94, 0x4c, 0x02, 0xf3, 0x8d, 0x49, 0xe0, 0x11, 0xc4, 0x9b,
|
|
|
|
0x62, 0x5b, 0xc2, 0xfd, 0x68, 0xf4, 0x07, 0x15, 0x71, 0x11, 0x4c, 0x35, 0x97, 0x5d, 0xc0, 0xe6,
|
|
|
|
0x22, 0xc9, 0x8a, 0x7b, 0x96, 0xc9, 0xc3, 0xe4, 0x2b, 0x1e, 0x88, 0x17, 0x4f, 0x98, 0x9b, 0xf3,
|
|
|
|
0x42, 0x23, 0x0c, 0xa0, 0xfa, 0x19, 0x03, 0x2a, 0xf7, 0x13, 0x2d, 0x27, 0xac, 0x9f, 0xaf, 0x2d,
|
|
|
|
0xa3, 0xce, 0xf7, 0x63, 0xbb, 0x39, 0x9f, 0x72, 0x80, 0xdd, 0x6c, 0x73, 0x00, 0x85, 0x70, 0xf2,
|
|
|
|
0xed, 0x50, 0xed, 0xa0, 0x74, 0x42, 0xd7, 0x22, 0x46, 0x24, 0xee, 0x67, 0xdf, 0xb5, 0x45, 0xe8,
|
|
|
|
0x49, 0xf4, 0x9c, 0xe4, 0x00, 0x83, 0xf2, 0x27, 0x8e, 0xa2, 0xb1, 0xc3, 0xc2, 0x01, 0xd7, 0x59,
|
|
|
|
0x2e, 0x4d, 0xac, 0x49, 0xa2, 0xc1, 0x8d, 0x88, 0x4b, 0xfe, 0x28, 0xe5, 0xac, 0xa6, 0x85, 0xc4,
|
|
|
|
0x1f, 0xf8, 0xc5, 0xc5, 0x14, 0x4e, 0xa3, 0xcb, 0x17, 0xb7, 0x64, 0xb3, 0xc2, 0x12, 0xf8, 0xf8,
|
|
|
|
0x36, 0x99, 0x1c, 0x91, 0x9b, 0xbd, 0xed, 0x55, 0x0f, 0xfd, 0x49, 0x85, 0xbb, 0x32, 0xad, 0x78,
|
|
|
|
0xc1, 0x74, 0xe6, 0x7c, 0x18, 0x0f, 0x2b, 0x3b, 0xaa, 0xd1, 0x9d, 0x40, 0x71, 0x1d, 0x19, 0x53
|
|
|
|
};
|
|
|
|
|
2021-12-03 15:49:29 +01:00
|
|
|
static UCHAR rsaPrivateBlob[] =
|
|
|
|
{
|
|
|
|
0x52, 0x53, 0x41, 0x32, 0x00, 0x02, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
|
|
|
|
0x20, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0xa6, 0x8b, 0x46, 0x26, 0xb5,
|
|
|
|
0xa9, 0x69, 0x83, 0x94, 0x66, 0xa7, 0xf3, 0x33, 0x95, 0x74, 0xe9, 0xeb, 0xc8, 0xcd, 0xd7, 0x81,
|
|
|
|
0x9e, 0x45, 0x66, 0xb2, 0x48, 0x8b, 0x1f, 0xfe, 0xb3, 0x62, 0xc4, 0x0d, 0xa2, 0xf9, 0xf3, 0xe2,
|
|
|
|
0xa6, 0x86, 0xd1, 0x1e, 0x8a, 0xbb, 0x1d, 0xa5, 0xc5, 0xe8, 0xa7, 0x50, 0x37, 0xfd, 0x69, 0x1f,
|
|
|
|
0x6f, 0x99, 0x99, 0xca, 0x39, 0x13, 0xea, 0x5b, 0x6b, 0xe3, 0x91, 0xc0, 0xd2, 0x2c, 0x0b, 0x21,
|
|
|
|
0xb1, 0xac, 0xa9, 0xe8, 0xa0, 0x6d, 0xa4, 0x1f, 0x1b, 0x34, 0xcb, 0x88, 0x7f, 0x2e, 0xeb, 0x7d,
|
|
|
|
0x91, 0x38, 0x48, 0xce, 0x05, 0x73, 0x05, 0xdd, 0x22, 0x94, 0xc3, 0xdd, 0x1c, 0xfd, 0xc5, 0x41,
|
|
|
|
0x2e, 0x94, 0xf9, 0xed, 0xe5, 0x92, 0x5f, 0x3f, 0x06, 0xf8, 0x49, 0x60, 0xb8, 0x92, 0x52, 0x6a,
|
|
|
|
0x56, 0x6e, 0xd7, 0x04, 0x1a, 0xb5, 0xb5, 0x1c, 0x31, 0xd1, 0x1b,
|
|
|
|
};
|
|
|
|
|
|
|
|
static UCHAR rsaFullPrivateBlob[] =
|
|
|
|
{
|
|
|
|
0x52, 0x53, 0x41, 0x33, 0x00, 0x02, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
|
|
|
|
0x20, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0xa6, 0x8b, 0x46, 0x26, 0xb5,
|
|
|
|
0xa9, 0x69, 0x83, 0x94, 0x66, 0xa7, 0xf3, 0x33, 0x95, 0x74, 0xe9, 0xeb, 0xc8, 0xcd, 0xd7, 0x81,
|
|
|
|
0x9e, 0x45, 0x66, 0xb2, 0x48, 0x8b, 0x1f, 0xfe, 0xb3, 0x62, 0xc4, 0x0d, 0xa2, 0xf9, 0xf3, 0xe2,
|
|
|
|
0xa6, 0x86, 0xd1, 0x1e, 0x8a, 0xbb, 0x1d, 0xa5, 0xc5, 0xe8, 0xa7, 0x50, 0x37, 0xfd, 0x69, 0x1f,
|
|
|
|
0x6f, 0x99, 0x99, 0xca, 0x39, 0x13, 0xea, 0x5b, 0x6b, 0xe3, 0x91, 0xc0, 0xd2, 0x2c, 0x0b, 0x21,
|
|
|
|
0xb1, 0xac, 0xa9, 0xe8, 0xa0, 0x6d, 0xa4, 0x1f, 0x1b, 0x34, 0xcb, 0x88, 0x7f, 0x2e, 0xeb, 0x7d,
|
|
|
|
0x91, 0x38, 0x48, 0xce, 0x05, 0x73, 0x05, 0xdd, 0x22, 0x94, 0xc3, 0xdd, 0x1c, 0xfd, 0xc5, 0x41,
|
|
|
|
0x2e, 0x94, 0xf9, 0xed, 0xe5, 0x92, 0x5f, 0x3f, 0x06, 0xf8, 0x49, 0x60, 0xb8, 0x92, 0x52, 0x6a,
|
|
|
|
0x56, 0x6e, 0xd7, 0x04, 0x1a, 0xb5, 0xb5, 0x1c, 0x31, 0xd1, 0x1b, 0xa3, 0xf3, 0xd1, 0x69, 0x61,
|
|
|
|
0xab, 0xfe, 0xc1, 0xb6, 0x40, 0x7b, 0x19, 0xbb, 0x2d, 0x59, 0xf5, 0xda, 0x49, 0x32, 0x6f, 0x20,
|
|
|
|
0x24, 0xd3, 0xb3, 0xec, 0x21, 0xec, 0x0c, 0xc7, 0x5b, 0xf9, 0x1b, 0xba, 0x6e, 0xe9, 0x61, 0xda,
|
|
|
|
0x55, 0xc6, 0x72, 0xfd, 0x2d, 0x66, 0x3f, 0x3c, 0xcb, 0x49, 0xa9, 0xc5, 0x0d, 0x9b, 0x02, 0x36,
|
|
|
|
0x7a, 0xee, 0x36, 0x09, 0x55, 0xe4, 0x03, 0xf2, 0xe3, 0xe6, 0x25, 0x14, 0x89, 0x7f, 0x2b, 0xfb,
|
|
|
|
0x27, 0x0e, 0x8d, 0x37, 0x84, 0xfd, 0xad, 0x10, 0x79, 0x43, 0x4e, 0x38, 0x4a, 0xd4, 0x5e, 0xfa,
|
|
|
|
0xda, 0x9f, 0x88, 0x21, 0x7c, 0xb4, 0x98, 0xb6, 0x6e, 0x1c, 0x24, 0x09, 0xe5, 0xe7, 0x22, 0x6f,
|
|
|
|
0xd3, 0x84, 0xc0, 0xdc, 0x36, 0x09, 0xaf, 0x4b, 0x96, 0x8b, 0x5f, 0x47, 0xb3, 0x24, 0x80, 0xb5,
|
|
|
|
0x64, 0x69, 0xad, 0x83, 0xd5, 0x09, 0xe7, 0xb9, 0xe4, 0x81, 0x6f, 0x1a, 0xe2, 0x6d, 0xf1, 0x5e,
|
|
|
|
0x2b, 0xb3, 0x7a, 0xd0, 0x77, 0xef, 0x82, 0xcd, 0x55, 0x2e, 0xd5, 0xb1, 0xa7, 0x72, 0xec, 0x02,
|
|
|
|
0x9d, 0xe2, 0xcc, 0x5a, 0xf1, 0x68, 0x30, 0xe5, 0xbc, 0x8d, 0xad,
|
|
|
|
};
|
|
|
|
|
2018-03-26 15:04:36 +02:00
|
|
|
static void test_RSA(void)
|
|
|
|
{
|
2019-09-23 15:48:52 +02:00
|
|
|
static UCHAR hash[] =
|
|
|
|
{0x7e,0xe3,0x74,0xe7,0xc5,0x0b,0x6b,0x70,0xdb,0xab,0x32,0x6d,0x1d,0x51,0xd6,0x74,0x79,0x8e,0x5b,0x4b};
|
2018-03-26 15:04:36 +02:00
|
|
|
BCRYPT_PKCS1_PADDING_INFO pad;
|
2019-09-23 15:48:52 +02:00
|
|
|
BCRYPT_ALG_HANDLE alg;
|
|
|
|
BCRYPT_KEY_HANDLE key;
|
|
|
|
BCRYPT_RSAKEY_BLOB *rsablob;
|
|
|
|
UCHAR sig[64];
|
2021-12-03 15:49:29 +01:00
|
|
|
ULONG len, size, size2, schemes;
|
2018-03-26 15:04:36 +02:00
|
|
|
NTSTATUS ret;
|
2019-09-23 15:48:52 +02:00
|
|
|
BYTE *buf;
|
2018-03-26 15:04:36 +02:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptOpenAlgorithmProvider(&alg, BCRYPT_RSA_ALGORITHM, NULL, 0);
|
2018-03-26 15:04:36 +02:00
|
|
|
if (ret)
|
|
|
|
{
|
2022-02-09 10:57:53 +01:00
|
|
|
win_skip("Failed to open RSA provider: %#lx, skipping test\n", ret);
|
2018-03-26 15:04:36 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-17 13:35:42 +01:00
|
|
|
schemes = size = 0;
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptGetProperty(alg, L"PaddingSchemes", (UCHAR *)&schemes, sizeof(schemes), &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(!ret, "got %#lx\n", ret);
|
2020-02-17 13:35:42 +01:00
|
|
|
ok(schemes, "schemes not set\n");
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(size == sizeof(schemes), "got %lu\n", size);
|
2020-02-17 13:35:42 +01:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptImportKeyPair(alg, NULL, BCRYPT_RSAPUBLIC_BLOB, &key, rsaPublicBlob, sizeof(rsaPublicBlob), 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(!ret, "BCryptImportKeyPair failed: %#lx\n", ret);
|
2018-03-26 15:04:36 +02:00
|
|
|
|
|
|
|
pad.pszAlgId = BCRYPT_SHA1_ALGORITHM;
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptVerifySignature(key, &pad, rsaHash, sizeof(rsaHash), rsaSignature, sizeof(rsaSignature), BCRYPT_PAD_PKCS1);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(!ret, "BCryptVerifySignature failed: %#lx\n", ret);
|
2018-03-26 15:04:36 +02:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptVerifySignature(key, NULL, rsaHash, sizeof(rsaHash), rsaSignature, sizeof(rsaSignature), BCRYPT_PAD_PKCS1);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_INVALID_PARAMETER, "Expected STATUS_INVALID_PARAMETER, got %#lx\n", ret);
|
2018-03-26 15:04:36 +02:00
|
|
|
|
|
|
|
pad.pszAlgId = BCRYPT_SHA1_ALGORITHM;
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptVerifySignature(key, &pad, rsaHash, sizeof(rsaHash), rsaSignature, sizeof(rsaSignature), 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_INVALID_PARAMETER, "Expected STATUS_INVALID_PARAMETER, got %#lx\n", ret);
|
2018-03-26 15:04:36 +02:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptVerifySignature(key, NULL, rsaHash, sizeof(rsaHash), rsaSignature, sizeof(rsaSignature), 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_INVALID_PARAMETER, "Expected STATUS_INVALID_PARAMETER, got %#lx\n", ret);
|
2018-03-26 15:04:36 +02:00
|
|
|
|
|
|
|
pad.pszAlgId = BCRYPT_AES_ALGORITHM;
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptVerifySignature(key, &pad, rsaHash, sizeof(rsaHash), rsaSignature, sizeof(rsaSignature), BCRYPT_PAD_PKCS1);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_NOT_SUPPORTED, "Expected STATUS_NOT_SUPPORTED, got %#lx\n", ret);
|
2018-03-26 15:04:36 +02:00
|
|
|
|
|
|
|
pad.pszAlgId = NULL;
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptVerifySignature(key, &pad, rsaHash, sizeof(rsaHash), rsaSignature, sizeof(rsaSignature), BCRYPT_PAD_PKCS1);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_INVALID_SIGNATURE, "Expected STATUS_INVALID_SIGNATURE, got %#lx\n", ret);
|
2018-03-26 15:04:36 +02:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDestroyKey(key);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(!ret, "BCryptDestroyKey failed: %#lx\n", ret);
|
2018-03-26 15:04:36 +02:00
|
|
|
|
2019-09-23 15:48:52 +02:00
|
|
|
/* sign/verify with export/import round-trip */
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptGenerateKeyPair(alg, &key, 512, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2019-09-23 15:48:52 +02:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptFinalizeKeyPair(key, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2019-09-23 15:48:52 +02:00
|
|
|
|
|
|
|
pad.pszAlgId = BCRYPT_SHA1_ALGORITHM;
|
|
|
|
memset(sig, 0, sizeof(sig));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptSignHash(key, &pad, hash, sizeof(hash), sig, sizeof(sig), &len, BCRYPT_PAD_PKCS1);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(!ret, "got %#lx\n", ret);
|
2019-09-23 15:48:52 +02:00
|
|
|
|
2021-12-03 15:49:29 +01:00
|
|
|
/* export private key */
|
|
|
|
size = 0;
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptExportKey(key, NULL, BCRYPT_RSAPRIVATE_BLOB, NULL, 0, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2021-12-03 15:49:29 +01:00
|
|
|
ok(size, "size not set\n");
|
|
|
|
|
|
|
|
buf = HeapAlloc(GetProcessHeap(), 0, size);
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptExportKey(key, NULL, BCRYPT_RSAPRIVATE_BLOB, buf, size, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2021-12-03 15:49:29 +01:00
|
|
|
rsablob = (BCRYPT_RSAKEY_BLOB *)buf;
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(rsablob->Magic == BCRYPT_RSAPRIVATE_MAGIC, "got %#lx\n", rsablob->Magic);
|
|
|
|
ok(rsablob->BitLength == 512, "got %lu\n", rsablob->BitLength);
|
|
|
|
ok(rsablob->cbPublicExp == 3, "got %lu\n", rsablob->cbPublicExp);
|
|
|
|
ok(rsablob->cbModulus == 64, "got %lu\n", rsablob->cbModulus);
|
|
|
|
ok(rsablob->cbPrime1 == 32, "got %lu\n", rsablob->cbPrime1);
|
|
|
|
ok(rsablob->cbPrime2 == 32, "got %lu\n", rsablob->cbPrime2);
|
2021-12-03 15:49:29 +01:00
|
|
|
size2 = sizeof(*rsablob) + rsablob->cbPublicExp + rsablob->cbModulus + rsablob->cbPrime1 + rsablob->cbPrime2;
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(size == size2, "got %lu expected %lu\n", size2, size);
|
2021-12-03 15:49:29 +01:00
|
|
|
HeapFree(GetProcessHeap(), 0, buf);
|
|
|
|
|
|
|
|
size = 0;
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptExportKey(key, NULL, BCRYPT_RSAFULLPRIVATE_BLOB, NULL, 0, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2021-12-03 15:49:29 +01:00
|
|
|
ok(size, "size not set\n");
|
|
|
|
|
|
|
|
buf = HeapAlloc(GetProcessHeap(), 0, size);
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptExportKey(key, NULL, BCRYPT_RSAFULLPRIVATE_BLOB, buf, size, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2021-12-03 15:49:29 +01:00
|
|
|
rsablob = (BCRYPT_RSAKEY_BLOB *)buf;
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(rsablob->Magic == BCRYPT_RSAFULLPRIVATE_MAGIC, "got %#lx\n", rsablob->Magic);
|
|
|
|
ok(rsablob->BitLength == 512, "got %lu\n", rsablob->BitLength);
|
|
|
|
ok(rsablob->cbPublicExp == 3, "got %lu\n", rsablob->cbPublicExp);
|
|
|
|
ok(rsablob->cbModulus == 64, "got %lu\n", rsablob->cbModulus);
|
|
|
|
ok(rsablob->cbPrime1 == 32, "got %lu\n", rsablob->cbPrime1);
|
|
|
|
ok(rsablob->cbPrime2 == 32, "got %lu\n", rsablob->cbPrime2);
|
2021-12-03 15:49:29 +01:00
|
|
|
size2 = sizeof(*rsablob) + rsablob->cbPublicExp + rsablob->cbModulus * 2 + rsablob->cbPrime1 * 3 + rsablob->cbPrime2 * 2;
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(size == size2, "got %lu expected %lu\n", size2, size);
|
2021-12-03 15:49:29 +01:00
|
|
|
HeapFree(GetProcessHeap(), 0, buf);
|
|
|
|
|
|
|
|
/* export public key */
|
2019-09-23 15:48:52 +02:00
|
|
|
size = 0;
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptExportKey(key, NULL, BCRYPT_RSAPUBLIC_BLOB, NULL, 0, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2019-09-23 15:48:52 +02:00
|
|
|
ok(size, "size not set\n");
|
|
|
|
|
|
|
|
buf = HeapAlloc(GetProcessHeap(), 0, size);
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptExportKey(key, NULL, BCRYPT_RSAPUBLIC_BLOB, buf, size, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2019-09-23 15:48:52 +02:00
|
|
|
rsablob = (BCRYPT_RSAKEY_BLOB *)buf;
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(rsablob->Magic == BCRYPT_RSAPUBLIC_MAGIC, "got %#lx\n", rsablob->Magic);
|
|
|
|
ok(rsablob->BitLength == 512, "got %lu\n", rsablob->BitLength);
|
|
|
|
ok(rsablob->cbPublicExp == 3, "got %lu\n", rsablob->cbPublicExp);
|
|
|
|
ok(rsablob->cbModulus == 64, "got %lu\n", rsablob->cbModulus);
|
|
|
|
ok(!rsablob->cbPrime1, "got %lu\n", rsablob->cbPrime1);
|
|
|
|
ok(!rsablob->cbPrime2, "got %lu\n", rsablob->cbPrime2);
|
|
|
|
ok(size == sizeof(*rsablob) + rsablob->cbPublicExp + rsablob->cbModulus, "got %lu\n", size);
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDestroyKey(key);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(!ret, "got %#lx\n", ret);
|
2019-09-23 15:48:52 +02:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptImportKeyPair(alg, NULL, BCRYPT_RSAPUBLIC_BLOB, &key, buf, size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2019-09-23 15:48:52 +02:00
|
|
|
HeapFree(GetProcessHeap(), 0, buf);
|
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptVerifySignature(key, &pad, hash, sizeof(hash), sig, len, BCRYPT_PAD_PKCS1);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(!ret, "got %#lx\n", ret);
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDestroyKey(key);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(!ret, "got %#lx\n", ret);
|
2019-09-23 15:48:52 +02:00
|
|
|
|
2021-12-03 15:49:29 +01:00
|
|
|
/* import/export private key */
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptImportKeyPair(alg, NULL, BCRYPT_RSAPRIVATE_BLOB, &key, rsaPrivateBlob, sizeof(rsaPrivateBlob), 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2021-12-03 15:49:29 +01:00
|
|
|
|
|
|
|
size = 0;
|
|
|
|
buf = HeapAlloc(GetProcessHeap(), 0, sizeof(rsaPrivateBlob));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptExportKey(key, NULL, BCRYPT_RSAPRIVATE_BLOB, buf, sizeof(rsaPrivateBlob), &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
|
|
|
ok(size == sizeof(rsaPrivateBlob), "got %lu\n", size);
|
2021-12-03 15:49:29 +01:00
|
|
|
ok(!memcmp(buf, rsaPrivateBlob, size), "wrong data\n");
|
|
|
|
HeapFree(GetProcessHeap(), 0, buf);
|
2021-12-03 15:49:30 +01:00
|
|
|
BCryptDestroyKey(key);
|
2021-12-03 15:49:29 +01:00
|
|
|
|
|
|
|
/* import/export full private key */
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptImportKeyPair(alg, NULL, BCRYPT_RSAFULLPRIVATE_BLOB, &key, rsaFullPrivateBlob, sizeof(rsaFullPrivateBlob), 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2021-12-03 15:49:29 +01:00
|
|
|
|
|
|
|
size = 0;
|
|
|
|
buf = HeapAlloc(GetProcessHeap(), 0, sizeof(rsaFullPrivateBlob));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptExportKey(key, NULL, BCRYPT_RSAFULLPRIVATE_BLOB, buf, sizeof(rsaFullPrivateBlob), &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
|
|
|
ok(size == sizeof(rsaFullPrivateBlob), "got %lu\n", size);
|
2021-12-03 15:49:29 +01:00
|
|
|
ok(!memcmp(buf, rsaFullPrivateBlob, size), "wrong data\n");
|
|
|
|
HeapFree(GetProcessHeap(), 0, buf);
|
2021-12-03 15:49:30 +01:00
|
|
|
BCryptDestroyKey(key);
|
2021-12-03 15:49:29 +01:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptCloseAlgorithmProvider(alg, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(!ret, "got %#lx\n", ret);
|
2018-03-26 15:04:36 +02:00
|
|
|
}
|
|
|
|
|
2019-06-17 11:52:49 +02:00
|
|
|
static void test_RSA_SIGN(void)
|
|
|
|
{
|
|
|
|
BCRYPT_PKCS1_PADDING_INFO pad;
|
|
|
|
BCRYPT_ALG_HANDLE alg = NULL;
|
|
|
|
BCRYPT_KEY_HANDLE key = NULL;
|
2021-12-03 15:49:29 +01:00
|
|
|
BCRYPT_RSAKEY_BLOB *rsablob;
|
2019-06-17 11:52:49 +02:00
|
|
|
NTSTATUS ret;
|
2021-12-03 15:49:29 +01:00
|
|
|
ULONG size, size2;
|
|
|
|
BYTE *buf;
|
2019-06-17 11:52:49 +02:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptOpenAlgorithmProvider(&alg, BCRYPT_RSA_SIGN_ALGORITHM, NULL, 0);
|
2019-06-17 11:52:49 +02:00
|
|
|
if (ret)
|
|
|
|
{
|
2022-02-09 10:57:53 +01:00
|
|
|
win_skip("Failed to open RSA_SIGN provider: %#lx, skipping test\n", ret);
|
2019-06-17 11:52:49 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptImportKeyPair(alg, NULL, BCRYPT_RSAPUBLIC_BLOB, &key, rsaPublicBlob, sizeof(rsaPublicBlob), 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(!ret, "BCryptImportKeyPair failed: %#lx\n", ret);
|
2019-06-17 11:52:49 +02:00
|
|
|
|
|
|
|
pad.pszAlgId = BCRYPT_SHA1_ALGORITHM;
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptVerifySignature(key, &pad, rsaHash, sizeof(rsaHash), rsaSignature, sizeof(rsaSignature), BCRYPT_PAD_PKCS1);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(!ret, "BCryptVerifySignature failed: %#lx\n", ret);
|
2019-06-17 11:52:49 +02:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptVerifySignature(key, NULL, rsaHash, sizeof(rsaHash), rsaSignature, sizeof(rsaSignature), BCRYPT_PAD_PKCS1);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_INVALID_PARAMETER, "Expected STATUS_INVALID_PARAMETER, got %#lx\n", ret);
|
2019-06-17 11:52:49 +02:00
|
|
|
|
|
|
|
pad.pszAlgId = BCRYPT_SHA1_ALGORITHM;
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptVerifySignature(key, &pad, rsaHash, sizeof(rsaHash), rsaSignature, sizeof(rsaSignature), 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_INVALID_PARAMETER, "Expected STATUS_INVALID_PARAMETER, got %#lx\n", ret);
|
2019-06-17 11:52:49 +02:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptVerifySignature(key, NULL, rsaHash, sizeof(rsaHash), rsaSignature, sizeof(rsaSignature), 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_INVALID_PARAMETER, "Expected STATUS_INVALID_PARAMETER, got %#lx\n", ret);
|
2019-06-17 11:52:49 +02:00
|
|
|
|
|
|
|
pad.pszAlgId = BCRYPT_AES_ALGORITHM;
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptVerifySignature(key, &pad, rsaHash, sizeof(rsaHash), rsaSignature, sizeof(rsaSignature), BCRYPT_PAD_PKCS1);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_NOT_SUPPORTED, "Expected STATUS_NOT_SUPPORTED, got %#lx\n", ret);
|
2019-06-17 11:52:49 +02:00
|
|
|
|
|
|
|
pad.pszAlgId = NULL;
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptVerifySignature(key, &pad, rsaHash, sizeof(rsaHash), rsaSignature, sizeof(rsaSignature), BCRYPT_PAD_PKCS1);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_INVALID_SIGNATURE, "Expected STATUS_INVALID_SIGNATURE, got %#lx\n", ret);
|
2019-06-17 11:52:49 +02:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDestroyKey(key);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(!ret, "BCryptDestroyKey failed: %#lx\n", ret);
|
2019-06-17 11:52:49 +02:00
|
|
|
|
2021-12-03 15:49:29 +01:00
|
|
|
/* export private key */
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptGenerateKeyPair(alg, &key, 512, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2021-12-03 15:49:29 +01:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptFinalizeKeyPair(key, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2021-12-03 15:49:29 +01:00
|
|
|
|
|
|
|
size = 0;
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptExportKey(key, NULL, BCRYPT_RSAPRIVATE_BLOB, NULL, 0, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2021-12-03 15:49:29 +01:00
|
|
|
ok(size, "size not set\n");
|
|
|
|
|
|
|
|
buf = HeapAlloc(GetProcessHeap(), 0, size);
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptExportKey(key, NULL, BCRYPT_RSAPRIVATE_BLOB, buf, size, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2021-12-03 15:49:29 +01:00
|
|
|
rsablob = (BCRYPT_RSAKEY_BLOB *)buf;
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(rsablob->Magic == BCRYPT_RSAPRIVATE_MAGIC, "got %#lx\n", rsablob->Magic);
|
|
|
|
ok(rsablob->BitLength == 512, "got %lu\n", rsablob->BitLength);
|
|
|
|
ok(rsablob->cbPublicExp == 3, "got %lu\n", rsablob->cbPublicExp);
|
|
|
|
ok(rsablob->cbModulus == 64, "got %lu\n", rsablob->cbModulus);
|
|
|
|
ok(rsablob->cbPrime1 == 32, "got %lu\n", rsablob->cbPrime1);
|
|
|
|
ok(rsablob->cbPrime2 == 32, "got %lu\n", rsablob->cbPrime2);
|
2021-12-03 15:49:29 +01:00
|
|
|
size2 = sizeof(*rsablob) + rsablob->cbPublicExp + rsablob->cbModulus + rsablob->cbPrime1 + rsablob->cbPrime2;
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(size == size2, "got %lu expected %lu\n", size2, size);
|
2021-12-03 15:49:29 +01:00
|
|
|
HeapFree(GetProcessHeap(), 0, buf);
|
|
|
|
|
|
|
|
size = 0;
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptExportKey(key, NULL, BCRYPT_RSAFULLPRIVATE_BLOB, NULL, 0, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2021-12-03 15:49:29 +01:00
|
|
|
ok(size, "size not set\n");
|
|
|
|
|
|
|
|
buf = HeapAlloc(GetProcessHeap(), 0, size);
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptExportKey(key, NULL, BCRYPT_RSAFULLPRIVATE_BLOB, buf, size, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2021-12-03 15:49:29 +01:00
|
|
|
rsablob = (BCRYPT_RSAKEY_BLOB *)buf;
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(rsablob->Magic == BCRYPT_RSAFULLPRIVATE_MAGIC, "got %#lx\n", rsablob->Magic);
|
|
|
|
ok(rsablob->BitLength == 512, "got %lu\n", rsablob->BitLength);
|
|
|
|
ok(rsablob->cbPublicExp == 3, "got %lu\n", rsablob->cbPublicExp);
|
|
|
|
ok(rsablob->cbModulus == 64, "got %lu\n", rsablob->cbModulus);
|
|
|
|
ok(rsablob->cbPrime1 == 32, "got %lu\n", rsablob->cbPrime1);
|
|
|
|
ok(rsablob->cbPrime2 == 32, "got %lu\n", rsablob->cbPrime2);
|
2021-12-03 15:49:29 +01:00
|
|
|
size2 = sizeof(*rsablob) + rsablob->cbPublicExp + rsablob->cbModulus * 2 + rsablob->cbPrime1 * 3 + rsablob->cbPrime2 * 2;
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(size == size2, "got %lu expected %lu\n", size2, size);
|
2021-12-03 15:49:29 +01:00
|
|
|
HeapFree(GetProcessHeap(), 0, buf);
|
2021-12-03 15:49:30 +01:00
|
|
|
BCryptDestroyKey(key);
|
2021-12-03 15:49:29 +01:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptCloseAlgorithmProvider(alg, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(!ret, "BCryptCloseAlgorithmProvider failed: %#lx\n", ret);
|
2019-06-17 11:52:49 +02:00
|
|
|
}
|
|
|
|
|
2019-02-13 10:21:17 +01:00
|
|
|
static BYTE eccprivkey[] =
|
|
|
|
{
|
2020-01-07 21:22:47 +01:00
|
|
|
0x45, 0x43, 0x4b, 0x32, 0x20, 0x00, 0x00, 0x00,
|
|
|
|
0xfb, 0xbd, 0x3d, 0x20, 0x1b, 0x6d, 0x66, 0xb3, 0x7c, 0x9f, 0x89, 0xf3, 0xe4, 0x41, 0x16, 0xa5,
|
|
|
|
0x68, 0x52, 0x77, 0xac, 0xab, 0x55, 0xb2, 0x6c, 0xb0, 0x23, 0x55, 0xcb, 0x96, 0x14, 0xfd, 0x0b,
|
|
|
|
0x1c, 0xef, 0xdf, 0x07, 0x6d, 0x31, 0xaf, 0x39, 0xce, 0x8c, 0x8f, 0x9d, 0x75, 0xd0, 0x7b, 0xea,
|
|
|
|
0x81, 0xdc, 0x40, 0x21, 0x1f, 0x58, 0x22, 0x5f, 0x72, 0x55, 0xfc, 0x58, 0x8a, 0xeb, 0x88, 0x5d,
|
|
|
|
0x02, 0x09, 0x90, 0xd2, 0xe3, 0x36, 0xac, 0xfe, 0x83, 0x13, 0x6c, 0x88, 0x1a, 0xab, 0x9b, 0xdd,
|
|
|
|
0xaa, 0x8a, 0xee, 0x69, 0x9a, 0x6a, 0x62, 0x86, 0x6a, 0x13, 0x69, 0x88, 0xb7, 0xd5, 0xa3, 0xcd
|
|
|
|
};
|
|
|
|
|
|
|
|
static BYTE ecdh_pubkey[] =
|
|
|
|
{
|
|
|
|
0x45, 0x43, 0x4b, 0x31, 0x20, 0x00, 0x00, 0x00,
|
|
|
|
0x07, 0x61, 0x9d, 0x49, 0x63, 0x6b, 0x96, 0x94, 0xd1, 0x8f, 0xd1, 0x48, 0xcc, 0xcf, 0x72, 0x4d,
|
|
|
|
0xff, 0x43, 0xf4, 0x97, 0x0f, 0xa3, 0x8a, 0x72, 0xe9, 0xe0, 0xba, 0x87, 0x6d, 0xc3, 0x62, 0x15,
|
|
|
|
0xae, 0x65, 0xdd, 0x31, 0x51, 0xfc, 0x3b, 0xc9, 0x59, 0xa1, 0x0a, 0x92, 0x17, 0x2b, 0x64, 0x55,
|
|
|
|
0x03, 0x3e, 0x62, 0x1d, 0xac, 0x3e, 0x37, 0x40, 0x6a, 0x4c, 0xb6, 0x21, 0x3f, 0x73, 0x5c, 0xf5
|
|
|
|
};
|
|
|
|
|
|
|
|
/* little endian */
|
|
|
|
static BYTE ecdh_secret[] =
|
|
|
|
{
|
|
|
|
0x48, 0xb0, 0x11, 0xdb, 0x69, 0x4e, 0xb4, 0xf4, 0xf5, 0x3e, 0xe1, 0x9b, 0xca, 0x00, 0x04, 0xc8,
|
|
|
|
0x9b, 0x69, 0xaf, 0xd1, 0xaf, 0x1f, 0xc2, 0xd7, 0x83, 0x0a, 0xb7, 0xf8, 0x4f, 0x24, 0x32, 0x8e,
|
2019-02-13 10:21:17 +01:00
|
|
|
};
|
|
|
|
|
2020-01-07 21:22:48 +01:00
|
|
|
BCryptBuffer hash_param_buffers[] =
|
|
|
|
{
|
|
|
|
{
|
|
|
|
sizeof(BCRYPT_SHA1_ALGORITHM),
|
|
|
|
KDF_HASH_ALGORITHM,
|
|
|
|
(void *)BCRYPT_SHA1_ALGORITHM,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
BCryptBufferDesc hash_params =
|
|
|
|
{
|
|
|
|
BCRYPTBUFFER_VERSION,
|
|
|
|
ARRAY_SIZE(hash_param_buffers),
|
|
|
|
hash_param_buffers,
|
|
|
|
};
|
|
|
|
|
|
|
|
static BYTE hashed_secret[] =
|
|
|
|
{
|
|
|
|
0x1b, 0xe7, 0xbf, 0x0f, 0x65, 0x1e, 0xd0, 0x07, 0xf9, 0xf4, 0x77, 0x48, 0x48, 0x39, 0xd0, 0xf8,
|
|
|
|
0xf3, 0xce, 0xfc, 0x89
|
|
|
|
};
|
|
|
|
|
2019-02-13 10:21:15 +01:00
|
|
|
static void test_ECDH(void)
|
|
|
|
{
|
2019-02-13 10:21:16 +01:00
|
|
|
BYTE *buf;
|
|
|
|
BCRYPT_ECCKEY_BLOB *ecckey;
|
2019-02-13 10:21:15 +01:00
|
|
|
BCRYPT_ALG_HANDLE alg;
|
2019-02-13 10:21:17 +01:00
|
|
|
BCRYPT_KEY_HANDLE key, privkey, pubkey;
|
2020-01-07 21:22:47 +01:00
|
|
|
BCRYPT_SECRET_HANDLE secret;
|
2019-02-13 10:21:15 +01:00
|
|
|
NTSTATUS status;
|
2019-02-13 10:21:16 +01:00
|
|
|
ULONG size;
|
2019-02-13 10:21:15 +01:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
status = BCryptOpenAlgorithmProvider(&alg, BCRYPT_ECDH_P256_ALGORITHM, NULL, 0);
|
2019-02-13 10:21:15 +01:00
|
|
|
if (status)
|
|
|
|
{
|
2022-02-09 10:57:53 +01:00
|
|
|
skip("Failed to open BCRYPT_ECDH_P256_ALGORITHM provider %#lx\n", status);
|
2019-02-13 10:21:15 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
key = NULL;
|
2021-12-03 15:49:30 +01:00
|
|
|
status = BCryptGenerateKeyPair(alg, &key, 256, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(status == STATUS_SUCCESS, "got %#lx\n", status);
|
2019-02-13 10:21:15 +01:00
|
|
|
ok(key != NULL, "key not set\n");
|
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
status = BCryptFinalizeKeyPair(key, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(status == STATUS_SUCCESS, "got %#lx\n", status);
|
2019-02-13 10:21:15 +01:00
|
|
|
|
2019-02-13 10:21:16 +01:00
|
|
|
size = 0;
|
2021-12-03 15:49:30 +01:00
|
|
|
status = BCryptExportKey(key, NULL, BCRYPT_ECCPUBLIC_BLOB, NULL, 0, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(status == STATUS_SUCCESS, "got %#lx\n", status);
|
2019-02-13 10:21:16 +01:00
|
|
|
ok(size, "size not set\n");
|
|
|
|
|
|
|
|
buf = HeapAlloc(GetProcessHeap(), 0, size);
|
2021-12-03 15:49:30 +01:00
|
|
|
status = BCryptExportKey(key, NULL, BCRYPT_ECCPUBLIC_BLOB, buf, size, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(status == STATUS_SUCCESS, "got %#lx\n", status);
|
2019-02-13 10:21:16 +01:00
|
|
|
ecckey = (BCRYPT_ECCKEY_BLOB *)buf;
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ecckey->dwMagic == BCRYPT_ECDH_PUBLIC_P256_MAGIC, "got %#lx\n", ecckey->dwMagic);
|
|
|
|
ok(ecckey->cbKey == 32, "got %lu\n", ecckey->cbKey);
|
|
|
|
ok(size == sizeof(*ecckey) + ecckey->cbKey * 2, "got %lu\n", size);
|
2019-02-13 10:21:16 +01:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
status = BCryptImportKeyPair(alg, NULL, BCRYPT_ECCPUBLIC_BLOB, &pubkey, buf, size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(status == STATUS_SUCCESS, "got %#lx\n", status);
|
2019-02-13 10:21:16 +01:00
|
|
|
HeapFree(GetProcessHeap(), 0, buf);
|
|
|
|
|
2019-02-13 10:21:17 +01:00
|
|
|
size = 0;
|
2021-12-03 15:49:30 +01:00
|
|
|
status = BCryptExportKey(key, NULL, BCRYPT_ECCPRIVATE_BLOB, NULL, 0, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(status == STATUS_SUCCESS, "got %#lx\n", status);
|
2019-02-13 10:21:17 +01:00
|
|
|
ok(size, "size not set\n");
|
|
|
|
|
|
|
|
buf = HeapAlloc(GetProcessHeap(), 0, size);
|
2021-12-03 15:49:30 +01:00
|
|
|
status = BCryptExportKey(key, NULL, BCRYPT_ECCPRIVATE_BLOB, buf, size, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(status == STATUS_SUCCESS, "got %#lx\n", status);
|
2019-02-13 10:21:17 +01:00
|
|
|
ecckey = (BCRYPT_ECCKEY_BLOB *)buf;
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ecckey->dwMagic == BCRYPT_ECDH_PRIVATE_P256_MAGIC, "got %#lx\n", ecckey->dwMagic);
|
|
|
|
ok(ecckey->cbKey == 32, "got %lu\n", ecckey->cbKey);
|
|
|
|
ok(size == sizeof(*ecckey) + ecckey->cbKey * 3, "got %lu\n", size);
|
2019-02-13 10:21:17 +01:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
status = BCryptImportKeyPair(alg, NULL, BCRYPT_ECCPRIVATE_BLOB, &privkey, buf, size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(status == STATUS_SUCCESS, "got %#lx\n", status);
|
2019-02-13 10:21:17 +01:00
|
|
|
HeapFree(GetProcessHeap(), 0, buf);
|
2021-12-03 15:49:30 +01:00
|
|
|
BCryptDestroyKey(pubkey);
|
|
|
|
BCryptDestroyKey(privkey);
|
|
|
|
BCryptDestroyKey(key);
|
2019-02-13 10:21:17 +01:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
status = BCryptImportKeyPair(alg, NULL, BCRYPT_ECCPRIVATE_BLOB, &privkey, eccprivkey, sizeof(eccprivkey), 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(status == STATUS_SUCCESS, "got %#lx\n", status);
|
2019-02-13 10:21:17 +01:00
|
|
|
|
|
|
|
size = 0;
|
2021-12-03 15:49:30 +01:00
|
|
|
status = BCryptExportKey(privkey, NULL, BCRYPT_ECCPRIVATE_BLOB, NULL, 0, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(status == STATUS_SUCCESS, "got %#lx\n", status);
|
2019-02-13 10:21:17 +01:00
|
|
|
ok(size, "size not set\n");
|
|
|
|
|
|
|
|
buf = HeapAlloc(GetProcessHeap(), 0, size);
|
2021-12-03 15:49:30 +01:00
|
|
|
status = BCryptExportKey(privkey, NULL, BCRYPT_ECCPRIVATE_BLOB, buf, size, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(status == STATUS_SUCCESS, "got %#lx\n", status);
|
|
|
|
ok(size == sizeof(eccprivkey), "got %lu\n", size);
|
2019-02-13 10:21:17 +01:00
|
|
|
ok(!memcmp(buf, eccprivkey, size), "wrong data\n");
|
|
|
|
HeapFree(GetProcessHeap(), 0, buf);
|
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
status = BCryptImportKeyPair(alg, NULL, BCRYPT_ECCPUBLIC_BLOB, &pubkey, ecdh_pubkey, sizeof(ecdh_pubkey), 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(status == STATUS_SUCCESS, "got %#lx\n", status);
|
2020-01-07 21:22:47 +01:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
status = BCryptSecretAgreement(privkey, pubkey, &secret, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(status == STATUS_SUCCESS, "got %#lx\n", status);
|
2020-01-07 21:22:47 +01:00
|
|
|
|
|
|
|
if (status != STATUS_SUCCESS)
|
|
|
|
{
|
|
|
|
goto derive_end;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* verify result on windows 10 */
|
2021-12-03 15:49:30 +01:00
|
|
|
status = BCryptDeriveKey(secret, BCRYPT_KDF_RAW_SECRET, NULL, NULL, 0, &size, 0);
|
2020-01-07 21:22:47 +01:00
|
|
|
|
|
|
|
if (status == STATUS_NOT_SUPPORTED)
|
|
|
|
{
|
|
|
|
win_skip("BCRYPT_KDF_RAW_SECRET not supported\n");
|
2020-01-07 21:22:48 +01:00
|
|
|
goto raw_secret_end;
|
2020-01-07 21:22:47 +01:00
|
|
|
}
|
|
|
|
|
2022-02-09 10:57:53 +01:00
|
|
|
todo_wine ok(status == STATUS_SUCCESS, "got %#lx\n", status);
|
2020-01-07 21:22:47 +01:00
|
|
|
|
|
|
|
if (status != STATUS_SUCCESS)
|
|
|
|
{
|
2020-01-07 21:22:48 +01:00
|
|
|
goto raw_secret_end;
|
2020-01-07 21:22:47 +01:00
|
|
|
}
|
|
|
|
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(size == 32, "size of secret key incorrect, got %lu, expected 32\n", size);
|
2020-01-07 21:22:47 +01:00
|
|
|
buf = HeapAlloc(GetProcessHeap(), 0, size);
|
2021-12-03 15:49:30 +01:00
|
|
|
status = BCryptDeriveKey(secret, BCRYPT_KDF_RAW_SECRET, NULL, buf, size, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(status == STATUS_SUCCESS, "got %#lx\n", status);
|
2020-01-07 21:22:47 +01:00
|
|
|
ok(!(memcmp(ecdh_secret, buf, size)), "wrong data\n");
|
|
|
|
HeapFree(GetProcessHeap(), 0, buf);
|
|
|
|
|
2020-01-07 21:22:48 +01:00
|
|
|
raw_secret_end:
|
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
status = BCryptDeriveKey(secret, BCRYPT_KDF_HASH, &hash_params, NULL, 0, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
todo_wine ok (status == STATUS_SUCCESS, "got %#lx\n", status);
|
2020-01-07 21:22:48 +01:00
|
|
|
|
|
|
|
if (status != STATUS_SUCCESS)
|
|
|
|
{
|
|
|
|
goto derive_end;
|
|
|
|
}
|
|
|
|
|
2022-02-09 10:57:53 +01:00
|
|
|
ok (size == 20, "got %lu\n", size);
|
2020-01-07 21:22:48 +01:00
|
|
|
buf = HeapAlloc(GetProcessHeap(), 0, size);
|
2021-12-03 15:49:30 +01:00
|
|
|
status = BCryptDeriveKey(secret, BCRYPT_KDF_HASH, &hash_params, buf, size, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(status == STATUS_SUCCESS, "got %#lx\n", status);
|
2020-01-07 21:22:48 +01:00
|
|
|
ok(!(memcmp(hashed_secret, buf, size)), "wrong data\n");
|
|
|
|
HeapFree(GetProcessHeap(), 0, buf);
|
|
|
|
|
|
|
|
/* ulVersion is not verified */
|
|
|
|
hash_params.ulVersion = 0xdeadbeef;
|
2021-12-03 15:49:30 +01:00
|
|
|
status = BCryptDeriveKey(secret, BCRYPT_KDF_HASH, &hash_params, NULL, 0, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok (status == STATUS_SUCCESS, "got %#lx\n", status);
|
2020-01-07 21:22:48 +01:00
|
|
|
|
|
|
|
hash_params.ulVersion = BCRYPTBUFFER_VERSION;
|
|
|
|
hash_param_buffers[0].pvBuffer = (void*) L"INVALID";
|
|
|
|
hash_param_buffers[0].cbBuffer = sizeof(L"INVALID");
|
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
status = BCryptDeriveKey(secret, BCRYPT_KDF_HASH, &hash_params, NULL, 0, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok (status == STATUS_NOT_SUPPORTED || broken (status == STATUS_NOT_FOUND) /* < win8 */, "got %#lx\n", status);
|
2020-01-07 21:22:48 +01:00
|
|
|
|
|
|
|
hash_param_buffers[0].pvBuffer = (void*) BCRYPT_RNG_ALGORITHM;
|
|
|
|
hash_param_buffers[0].cbBuffer = sizeof(BCRYPT_RNG_ALGORITHM);
|
2021-12-03 15:49:30 +01:00
|
|
|
status = BCryptDeriveKey(secret, BCRYPT_KDF_HASH, &hash_params, NULL, 0, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok (status == STATUS_NOT_SUPPORTED, "got %#lx\n", status);
|
2020-01-07 21:22:48 +01:00
|
|
|
|
2020-01-07 21:22:47 +01:00
|
|
|
derive_end:
|
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
BCryptDestroySecret(secret);
|
|
|
|
BCryptDestroyKey(pubkey);
|
|
|
|
BCryptDestroyKey(privkey);
|
|
|
|
BCryptCloseAlgorithmProvider(alg, 0);
|
2019-02-13 10:21:15 +01:00
|
|
|
}
|
|
|
|
|
2019-06-17 11:52:48 +02:00
|
|
|
static void test_BCryptEnumContextFunctions(void)
|
|
|
|
{
|
|
|
|
CRYPT_CONTEXT_FUNCTIONS *buffer;
|
|
|
|
NTSTATUS status;
|
|
|
|
ULONG buflen;
|
|
|
|
|
|
|
|
buffer = NULL;
|
2021-12-03 15:49:30 +01:00
|
|
|
status = BCryptEnumContextFunctions( CRYPT_LOCAL, L"SSL", NCRYPT_SCHANNEL_INTERFACE, &buflen, &buffer );
|
2022-02-09 10:57:53 +01:00
|
|
|
todo_wine ok( status == STATUS_SUCCESS, "got %#lx\n", status);
|
2021-12-03 15:49:30 +01:00
|
|
|
if (status == STATUS_SUCCESS) BCryptFreeBuffer( buffer );
|
2019-06-17 11:52:48 +02:00
|
|
|
}
|
|
|
|
|
2019-09-23 15:48:52 +02:00
|
|
|
static BYTE rsapublic[] =
|
|
|
|
{
|
|
|
|
0x52, 0x53, 0x41, 0x31, 0x00, 0x08, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0xd5, 0xfe, 0xf6, 0x7a, 0x9a, 0xa1, 0x2d, 0xcf, 0x98,
|
|
|
|
0x60, 0xca, 0x38, 0x60, 0x0b, 0x74, 0x4c, 0x7e, 0xa1, 0x42, 0x64, 0xad, 0x05, 0xa5, 0x29, 0x25, 0xcb, 0xd5,
|
|
|
|
0x9c, 0xaf, 0x6f, 0x63, 0x85, 0x6d, 0x5b, 0x59, 0xe5, 0x17, 0x8f, 0xf9, 0x18, 0x90, 0xa7, 0x63, 0xae, 0xe0,
|
|
|
|
0x3a, 0x62, 0xf7, 0x98, 0x57, 0xe9, 0x91, 0xda, 0xfb, 0xd9, 0x36, 0x45, 0xe4, 0x9e, 0x75, 0xf6, 0x73, 0xc4,
|
|
|
|
0x99, 0x23, 0x21, 0x1b, 0x3d, 0xe1, 0xe0, 0xa6, 0xa0, 0x4a, 0x50, 0x2a, 0xcb, 0x2a, 0x50, 0xf0, 0x8b, 0x70,
|
|
|
|
0x9c, 0xe4, 0x1a, 0x14, 0x3b, 0xbe, 0x35, 0xa5, 0x5a, 0x91, 0xa3, 0xa1, 0x82, 0xea, 0x84, 0x4d, 0xe8, 0x62,
|
|
|
|
0x3b, 0x11, 0xec, 0x61, 0x09, 0x6c, 0xfe, 0xb2, 0xcc, 0x4b, 0xa8, 0xff, 0xaf, 0x73, 0x72, 0x05, 0x4e, 0x7e,
|
|
|
|
0xe5, 0x73, 0xdf, 0x24, 0xcf, 0x7f, 0x5d, 0xaf, 0x8a, 0xf0, 0xd8, 0xcb, 0x08, 0x1e, 0xf2, 0x36, 0x70, 0x8d,
|
|
|
|
0x1b, 0x9e, 0xc8, 0x98, 0x60, 0x54, 0xeb, 0x45, 0x34, 0x21, 0x43, 0x4d, 0x42, 0x0a, 0x3a, 0x2d, 0x0f, 0x0e,
|
|
|
|
0xd6, 0x0d, 0xe4, 0x2e, 0x8c, 0x31, 0x87, 0xa8, 0x09, 0x89, 0x61, 0x16, 0xca, 0x5b, 0xbe, 0x76, 0x69, 0xbb,
|
|
|
|
0xfd, 0x91, 0x63, 0xd2, 0x66, 0x57, 0x08, 0xef, 0xe2, 0x40, 0x67, 0xd7, 0x7f, 0x50, 0x15, 0x42, 0x33, 0x97,
|
|
|
|
0x54, 0x73, 0x47, 0xe7, 0x9c, 0x14, 0xa8, 0xb0, 0x3d, 0xc9, 0x23, 0xb0, 0x27, 0x3b, 0xe7, 0xdd, 0x5f, 0xd1,
|
|
|
|
0x4f, 0x31, 0x10, 0x7d, 0xdd, 0x69, 0x8e, 0xde, 0xa3, 0xe8, 0x92, 0x00, 0xfa, 0xa5, 0xa4, 0x40, 0x51, 0x23,
|
|
|
|
0x82, 0x84, 0xc7, 0xce, 0x19, 0x61, 0x26, 0xf1, 0xae, 0xf3, 0x90, 0x93, 0x98, 0x56, 0x23, 0x9a, 0xd1, 0xbd,
|
|
|
|
0xf2, 0xdf, 0xfd, 0x13, 0x9c, 0x30, 0x07, 0xf9, 0x5a, 0x2e, 0x00, 0xc6, 0x1f
|
|
|
|
};
|
|
|
|
|
|
|
|
static void test_BCryptSignHash(void)
|
|
|
|
{
|
|
|
|
static UCHAR hash[] =
|
|
|
|
{0x7e,0xe3,0x74,0xe7,0xc5,0x0b,0x6b,0x70,0xdb,0xab,0x32,0x6d,0x1d,0x51,0xd6,0x74,0x79,0x8e,0x5b,0x4b};
|
2019-12-06 15:42:50 +01:00
|
|
|
static UCHAR hash_sha256[] =
|
|
|
|
{0x25,0x2f,0x10,0xc8,0x36,0x10,0xeb,0xca,0x1a,0x05,0x9c,0x0b,0xae,0x82,0x55,0xeb,0xa2,0xf9,0x5b,0xe4,
|
|
|
|
0xd1,0xd7,0xbC,0xfA,0x89,0xd7,0x24,0x8a,0x82,0xd9,0xf1,0x11};
|
2019-09-23 15:48:52 +02:00
|
|
|
BCRYPT_PKCS1_PADDING_INFO pad;
|
|
|
|
BCRYPT_ALG_HANDLE alg;
|
|
|
|
BCRYPT_KEY_HANDLE key;
|
|
|
|
UCHAR sig[256];
|
|
|
|
NTSTATUS ret;
|
|
|
|
ULONG len;
|
|
|
|
|
2019-12-06 15:42:50 +01:00
|
|
|
/* RSA */
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptOpenAlgorithmProvider(&alg, BCRYPT_RSA_ALGORITHM, NULL, 0);
|
2019-09-23 15:48:52 +02:00
|
|
|
if (ret)
|
|
|
|
{
|
2022-02-09 10:57:53 +01:00
|
|
|
win_skip("failed to open RSA provider: %#lx\n", ret);
|
2019-09-23 15:48:52 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* public key */
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptImportKeyPair(alg, NULL, BCRYPT_RSAPUBLIC_BLOB, &key, rsapublic, sizeof(rsapublic), 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(!ret, "got %#lx\n", ret);
|
2019-09-23 15:48:52 +02:00
|
|
|
|
|
|
|
len = 0;
|
|
|
|
pad.pszAlgId = BCRYPT_SHA1_ALGORITHM;
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptSignHash(key, &pad, NULL, 0, NULL, 0, &len, BCRYPT_PAD_PKCS1);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(!ret, "got %#lx\n", ret);
|
|
|
|
ok(len == 256, "got %lu\n", len);
|
2019-09-23 15:48:52 +02:00
|
|
|
|
2022-01-20 15:18:45 +01:00
|
|
|
/* test len return when only output is NULL, as described in BCryptSignHash doc */
|
|
|
|
ret = BCryptSignHash(key, &pad, hash, sizeof(hash), NULL, 0, &len, BCRYPT_PAD_PKCS1);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(!ret, "got %#lx\n", ret);
|
|
|
|
ok(len == 256, "got %lu\n", len);
|
2022-01-20 15:18:45 +01:00
|
|
|
|
2019-09-23 15:48:52 +02:00
|
|
|
len = 0;
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptSignHash(key, &pad, hash, sizeof(hash), sig, sizeof(sig), &len, BCRYPT_PAD_PKCS1);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_INVALID_PARAMETER || broken(ret == STATUS_INTERNAL_ERROR) /* < win7 */, "got %#lx\n", ret);
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDestroyKey(key);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(!ret, "got %#lx\n", ret);
|
2019-09-23 15:48:52 +02:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptGenerateKeyPair(alg, &key, 512, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2019-09-23 15:48:52 +02:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptFinalizeKeyPair(key, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2019-09-23 15:48:52 +02:00
|
|
|
|
|
|
|
len = 0;
|
|
|
|
memset(sig, 0, sizeof(sig));
|
2019-12-06 15:42:50 +01:00
|
|
|
|
|
|
|
/* inference of padding info on RSA not supported */
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptSignHash(key, NULL, hash, sizeof(hash), sig, sizeof(sig), &len, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_INVALID_PARAMETER, "got %#lx\n", ret);
|
2019-12-06 15:42:50 +01:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptSignHash(key, &pad, hash, sizeof(hash), sig, 0, &len, BCRYPT_PAD_PKCS1);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_BUFFER_TOO_SMALL, "got %#lx\n", ret);
|
2019-12-06 15:42:50 +01:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptSignHash(key, &pad, hash, sizeof(hash), sig, sizeof(sig), &len, BCRYPT_PAD_PKCS1);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(!ret, "got %#lx\n", ret);
|
|
|
|
ok(len == 64, "got %lu\n", len);
|
2019-09-23 15:48:52 +02:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptVerifySignature(key, &pad, hash, sizeof(hash), sig, len, BCRYPT_PAD_PKCS1);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(!ret, "got %#lx\n", ret);
|
2019-09-23 15:48:52 +02:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDestroyKey(key);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(!ret, "got %#lx\n", ret);
|
2019-09-23 15:48:52 +02:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptCloseAlgorithmProvider(alg, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(!ret, "got %#lx\n", ret);
|
2019-12-06 15:42:50 +01:00
|
|
|
|
|
|
|
/* ECDSA */
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptOpenAlgorithmProvider(&alg, BCRYPT_ECDSA_P256_ALGORITHM, NULL, 0);
|
2019-12-06 15:42:50 +01:00
|
|
|
if (ret)
|
|
|
|
{
|
2022-02-09 10:57:53 +01:00
|
|
|
win_skip("failed to open ECDSA provider: %#lx\n", ret);
|
2019-12-06 15:42:50 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptGenerateKeyPair(alg, &key, 256, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2019-12-06 15:42:50 +01:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptFinalizeKeyPair(key, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2019-12-06 15:42:50 +01:00
|
|
|
|
|
|
|
memset(sig, 0, sizeof(sig));
|
|
|
|
len = 0;
|
|
|
|
|
|
|
|
/* automatically detects padding info */
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptSignHash(key, NULL, hash, sizeof(hash), sig, sizeof(sig), &len, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok (!ret, "got %#lx\n", ret);
|
|
|
|
ok (len == 64, "got %lu\n", len);
|
2019-12-06 15:42:50 +01:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptVerifySignature(key, NULL, hash, sizeof(hash), sig, len, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(!ret, "got %#lx\n", ret);
|
2019-12-06 15:42:50 +01:00
|
|
|
|
|
|
|
/* mismatch info (SHA-1 != SHA-256) */
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptSignHash(key, &pad, hash_sha256, sizeof(hash_sha256), sig, sizeof(sig), &len, BCRYPT_PAD_PKCS1);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok (ret == STATUS_INVALID_PARAMETER, "got %#lx\n", ret);
|
2019-12-06 15:42:50 +01:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDestroyKey(key);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(!ret, "got %#lx\n", ret);
|
2019-12-06 15:42:50 +01:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptCloseAlgorithmProvider(alg, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(!ret, "got %#lx\n", ret);
|
2019-09-23 15:48:52 +02:00
|
|
|
}
|
|
|
|
|
2019-09-30 17:12:57 +02:00
|
|
|
static void test_BCryptEnumAlgorithms(void)
|
|
|
|
{
|
|
|
|
BCRYPT_ALGORITHM_IDENTIFIER *list;
|
|
|
|
NTSTATUS ret;
|
|
|
|
ULONG count;
|
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptEnumAlgorithms(0, NULL, NULL, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_INVALID_PARAMETER, "got %#lx\n", ret);
|
2019-09-30 17:12:57 +02:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptEnumAlgorithms(0, &count, NULL, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_INVALID_PARAMETER, "got %#lx\n", ret);
|
2019-09-30 17:12:57 +02:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptEnumAlgorithms(0, NULL, &list, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_INVALID_PARAMETER, "got %#lx\n", ret);
|
2019-09-30 17:12:57 +02:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptEnumAlgorithms(~0u, &count, &list, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_INVALID_PARAMETER, "got %#lx\n", ret);
|
2019-09-30 17:12:57 +02:00
|
|
|
|
|
|
|
count = 0;
|
|
|
|
list = NULL;
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptEnumAlgorithms(0, &count, &list, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(!ret, "got %#lx\n", ret);
|
2019-09-30 17:12:57 +02:00
|
|
|
ok(list != NULL, "NULL list\n");
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(count, "got %lu\n", count);
|
2021-12-03 15:49:30 +01:00
|
|
|
BCryptFreeBuffer( list );
|
2019-09-30 17:12:57 +02:00
|
|
|
}
|
|
|
|
|
2019-11-06 10:16:54 +01:00
|
|
|
static void test_aes_vector(void)
|
|
|
|
{
|
|
|
|
static const UCHAR secret[] = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10};
|
|
|
|
static const UCHAR expect[] = {0xb0,0xcb,0xf5,0x80,0xd4,0xe3,0x55,0x23,0x6e,0x19,0x5b,0xdb,0xfe,0xe0,0x6c,0xd3};
|
|
|
|
static const UCHAR expect2[] = {0x06,0x0c,0x81,0xab,0xd4,0x28,0x80,0x42,0xce,0x30,0x56,0x17,0x15,0x00,0x9e,0xc1};
|
|
|
|
static const UCHAR expect3[] = {0x3e,0x99,0xbf,0x02,0xf5,0xd3,0xb8,0x81,0x91,0x4d,0x93,0xea,0xd4,0x92,0x93,0x46};
|
|
|
|
static UCHAR iv[16], input[] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p'};
|
|
|
|
UCHAR output[16];
|
|
|
|
BCRYPT_ALG_HANDLE alg;
|
|
|
|
BCRYPT_KEY_HANDLE key;
|
|
|
|
UCHAR data[sizeof(BCRYPT_KEY_DATA_BLOB_HEADER) + sizeof(secret)];
|
|
|
|
BCRYPT_KEY_DATA_BLOB_HEADER *blob = (BCRYPT_KEY_DATA_BLOB_HEADER *)data;
|
|
|
|
ULONG size;
|
|
|
|
NTSTATUS ret;
|
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptOpenAlgorithmProvider(&alg, BCRYPT_AES_ALGORITHM, NULL, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(!ret, "got %#lx\n", ret);
|
2019-11-06 10:16:54 +01:00
|
|
|
|
|
|
|
size = sizeof(BCRYPT_CHAIN_MODE_CBC);
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptSetProperty(alg, BCRYPT_CHAINING_MODE, (UCHAR *)BCRYPT_CHAIN_MODE_CBC, size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(!ret, "got %#lx\n", ret);
|
2019-11-06 10:16:54 +01:00
|
|
|
|
|
|
|
blob->dwMagic = BCRYPT_KEY_DATA_BLOB_MAGIC;
|
|
|
|
blob->dwVersion = BCRYPT_KEY_DATA_BLOB_VERSION1;
|
|
|
|
blob->cbKeyData = sizeof(secret);
|
|
|
|
memcpy(data + sizeof(*blob), secret, sizeof(secret));
|
|
|
|
size = sizeof(BCRYPT_KEY_DATA_BLOB_HEADER) + sizeof(secret);
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptImportKey(alg, NULL, BCRYPT_KEY_DATA_BLOB, &key, NULL, 0, data, size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(!ret || broken(ret == STATUS_INVALID_PARAMETER) /* vista */, "got %#lx\n", ret);
|
2019-11-06 10:16:54 +01:00
|
|
|
if (ret == STATUS_INVALID_PARAMETER)
|
|
|
|
{
|
|
|
|
win_skip("broken BCryptImportKey\n");
|
2021-12-03 15:49:30 +01:00
|
|
|
BCryptCloseAlgorithmProvider(alg, 0);
|
2019-11-06 10:16:54 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* zero initialization vector */
|
|
|
|
size = 0;
|
|
|
|
memset(output, 0, sizeof(output));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptEncrypt(key, input, sizeof(input), NULL, iv, sizeof(iv), output, sizeof(output), &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(!ret, "got %#lx\n", ret);
|
|
|
|
ok(size == 16, "got %lu\n", size);
|
2019-11-06 10:16:54 +01:00
|
|
|
ok(!memcmp(output, expect, sizeof(expect)), "wrong cipher text\n");
|
|
|
|
|
|
|
|
/* same initialization vector */
|
|
|
|
size = 0;
|
|
|
|
memset(output, 0, sizeof(output));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptEncrypt(key, input, sizeof(input), NULL, iv, sizeof(iv), output, sizeof(output), &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(!ret, "got %#lx\n", ret);
|
|
|
|
ok(size == 16, "got %lu\n", size);
|
2019-11-06 10:16:54 +01:00
|
|
|
ok(!memcmp(output, expect2, sizeof(expect2)), "wrong cipher text\n");
|
|
|
|
|
|
|
|
/* different initialization vector */
|
|
|
|
iv[0] = 0x1;
|
|
|
|
size = 0;
|
|
|
|
memset(output, 0, sizeof(output));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptEncrypt(key, input, sizeof(input), NULL, iv, sizeof(iv), output, sizeof(output), &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(!ret, "got %#lx\n", ret);
|
|
|
|
ok(size == 16, "got %lu\n", size);
|
2019-11-06 10:16:54 +01:00
|
|
|
todo_wine ok(!memcmp(output, expect3, sizeof(expect3)), "wrong cipher text\n");
|
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDestroyKey(key);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(!ret, "got %#lx\n", ret);
|
2019-11-06 10:16:54 +01:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptCloseAlgorithmProvider(alg, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(!ret, "got %#lx\n", ret);
|
2019-11-06 10:16:54 +01:00
|
|
|
}
|
|
|
|
|
2020-05-26 20:14:38 +02:00
|
|
|
static void test_BcryptDeriveKeyCapi(void)
|
|
|
|
{
|
|
|
|
static const UCHAR expect[] =
|
|
|
|
{0xda,0x39,0xa3,0xee,0x5e,0x6b,0x4b,0x0d,0x32,0x55,0xbf,0xef,0x95,0x60,0x18,0x90,0xaf,0xd8,0x07,0x09};
|
|
|
|
static const UCHAR expect2[] =
|
|
|
|
{0x9b,0x03,0x17,0x41,0xf4,0x75,0x11,0xac,0xff,0x22,0xee,0x40,0xbb,0xe8,0xf9,0x74,0x17,0x26,0xb6,0xf2,
|
|
|
|
0xf8,0xc7,0x88,0x02,0x9a,0xdc,0x0d,0xd7,0x83,0x58,0xea,0x65,0x2e,0x8b,0x85,0xc6,0xdb,0xb7,0xed,0x1c};
|
|
|
|
BCRYPT_ALG_HANDLE alg;
|
|
|
|
BCRYPT_HASH_HANDLE hash;
|
|
|
|
UCHAR key[40];
|
|
|
|
NTSTATUS ret;
|
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptOpenAlgorithmProvider(&alg, BCRYPT_SHA1_ALGORITHM, NULL, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(!ret, "got %#lx\n", ret);
|
2020-05-26 20:14:38 +02:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptCreateHash(alg, &hash, NULL, 0, NULL, 0, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(!ret || broken(ret == STATUS_INVALID_PARAMETER) /* win2k8 */, "got %#lx\n", ret);
|
2020-05-26 20:14:38 +02:00
|
|
|
if (ret == STATUS_INVALID_PARAMETER)
|
|
|
|
{
|
|
|
|
win_skip( "broken BCryptCreateHash\n" );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDeriveKeyCapi(NULL, NULL, NULL, 0, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_INVALID_PARAMETER || ret == STATUS_INVALID_HANDLE /* win7 */, "got %#lx\n", ret);
|
2020-05-26 20:14:38 +02:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDeriveKeyCapi(hash, NULL, NULL, 0, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_INVALID_PARAMETER || !ret /* win7 */, "got %#lx\n", ret);
|
2020-06-20 17:31:54 +02:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDestroyHash(hash);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(!ret, "got %#lx\n", ret);
|
2020-06-20 17:31:54 +02:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptCreateHash(alg, &hash, NULL, 0, NULL, 0, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(!ret, "got %#lx\n", ret);
|
2020-05-26 20:14:38 +02:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDeriveKeyCapi(hash, NULL, key, 0, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_INVALID_PARAMETER || !ret /* win7 */, "got %#lx\n", ret);
|
2020-06-20 17:31:54 +02:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDestroyHash(hash);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(!ret, "got %#lx\n", ret);
|
2020-05-26 20:14:38 +02:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptCreateHash(alg, &hash, NULL, 0, NULL, 0, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(!ret, "got %#lx\n", ret);
|
2020-06-20 17:31:54 +02:00
|
|
|
|
|
|
|
memset(key, 0, sizeof(key));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDeriveKeyCapi(hash, NULL, key, 41, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_INVALID_PARAMETER || !ret /* win7 */, "got %#lx\n", ret);
|
2020-06-20 17:31:54 +02:00
|
|
|
if (!ret)
|
|
|
|
ok(!memcmp(key, expect, sizeof(expect) - 1), "wrong key data\n");
|
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDestroyHash(hash);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(!ret, "got %#lx\n", ret);
|
2020-06-20 17:31:54 +02:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptCreateHash(alg, &hash, NULL, 0, NULL, 0, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(!ret, "got %#lx\n", ret);
|
2020-05-26 20:14:38 +02:00
|
|
|
|
|
|
|
memset(key, 0, sizeof(key));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDeriveKeyCapi(hash, NULL, key, 20, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(!ret, "got %#lx\n", ret);
|
2020-05-26 20:14:38 +02:00
|
|
|
ok(!memcmp(key, expect, sizeof(expect) - 1), "wrong key data\n");
|
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDeriveKeyCapi(hash, NULL, key, 20, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
todo_wine ok(ret == STATUS_INVALID_HANDLE, "got %#lx\n", ret);
|
2020-05-26 20:14:38 +02:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptHashData(hash, NULL, 0, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
todo_wine ok(ret == STATUS_INVALID_HANDLE, "got %#lx\n", ret);
|
2020-05-26 20:14:38 +02:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDestroyHash(hash);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(!ret, "got %#lx\n", ret);
|
2020-05-26 20:14:38 +02:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptCreateHash(alg, &hash, NULL, 0, NULL, 0, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(!ret, "got %#lx\n", ret);
|
2020-05-26 20:14:38 +02:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptHashData(hash, (UCHAR *)"test", 4, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(!ret, "got %#lx\n", ret);
|
2020-05-26 20:14:38 +02:00
|
|
|
|
|
|
|
/* padding */
|
|
|
|
memset(key, 0, sizeof(key));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDeriveKeyCapi(hash, NULL, key, 40, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(!ret, "got %#lx\n", ret);
|
2020-05-26 20:14:38 +02:00
|
|
|
ok(!memcmp(key, expect2, sizeof(expect2) - 1), "wrong key data\n");
|
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptCloseAlgorithmProvider(alg, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(!ret, "got %#lx\n", ret);
|
2020-05-26 20:14:38 +02:00
|
|
|
}
|
|
|
|
|
2020-05-29 11:29:06 +02:00
|
|
|
static UCHAR dsaHash[] =
|
|
|
|
{
|
|
|
|
0x7e,0xe3,0x74,0xe7,0xc5,0x0b,0x6b,0x70,0xdb,0xab,0x32,0x6d,0x1d,0x51,0xd6,0x74,0x79,0x8e,0x5b,0x4b
|
|
|
|
};
|
|
|
|
|
|
|
|
static UCHAR dsaSignature[] =
|
|
|
|
{
|
|
|
|
0x5f,0x95,0x1f,0x08,0x19,0x44,0xa5,0xab,0x28,0x11,0x51,0x68,0x82,0x9b,0xe4,0xc3,0x04,0x1b,0xc9,0xdc,
|
|
|
|
0x41,0x2a,0x89,0xd4,0x4a,0x8b,0x86,0xaf,0x98,0x2c,0x59,0x0b,0xd2,0x88,0xf6,0xe8,0x29,0x13,0x84,0x49
|
|
|
|
};
|
|
|
|
|
|
|
|
static UCHAR dsaPublicBlob[] =
|
|
|
|
{
|
|
|
|
0x44,0x53,0x50,0x42,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x8f,0xd2,0x92,0xbb,0x92,0xb9,0x00,0xc5,0xed,
|
|
|
|
0x52,0xcc,0x48,0x4a,0x44,0x1d,0xd3,0x74,0xfb,0x75,0xd1,0x7e,0xb6,0x24,0x9b,0x5d,0x57,0x0a,0x8a,0xc4,
|
|
|
|
0x5d,0xab,0x9c,0x26,0x86,0xc6,0x25,0x16,0x20,0xf9,0xa9,0x71,0xbc,0x1d,0x30,0xc4,0xef,0x8c,0xc4,0xdf,
|
|
|
|
0x1a,0xaf,0x96,0xdf,0x90,0xd8,0x85,0x9d,0xf9,0x2c,0x86,0x8c,0x91,0x39,0x6c,0x6d,0x11,0x4e,0x53,0x63,
|
|
|
|
0x2a,0x2b,0x26,0xa7,0xf9,0x76,0x74,0x51,0xbf,0x08,0x87,0x6f,0xe0,0x71,0x91,0x24,0x8a,0xc2,0x84,0x2d,
|
|
|
|
0x84,0x9c,0x5f,0x94,0xaa,0x38,0x53,0x77,0x84,0xba,0xbc,0xff,0x49,0x3a,0x08,0x0f,0x38,0xb5,0x91,0x5c,
|
|
|
|
0x06,0x15,0xa4,0x27,0xf4,0xa5,0x59,0xaa,0x1c,0x41,0xa3,0xa0,0xbb,0xf7,0x32,0x86,0xfb,0x94,0x41,0xff,
|
|
|
|
0xcd,0xed,0x69,0xeb,0xc6,0x5e,0xb6,0xa8,0x15,0x82,0x3b,0x60,0x1e,0x91,0x55,0xd5,0x2c,0xa5,0x74,0x5a,
|
|
|
|
0x65,0x8f,0xc6,0x56,0xc4,0x3f,0x4e,0xe3,0x3a,0x71,0xb2,0x63,0x66,0xa4,0x0d,0x0d,0xf9,0xdd,0x1e,0x48,
|
|
|
|
0x81,0xe9,0xbf,0x8f,0xbb,0x85,0x47,0x81,0x68,0x11,0xb5,0x91,0x6b,0xc4,0x05,0xef,0xa3,0xc7,0xbf,0x26,
|
|
|
|
0x53,0x4f,0xc4,0x10,0xfd,0xfa,0xed,0x61,0x64,0xd6,0x2e,0xad,0x04,0x3e,0x82,0xed,0xb2,0x22,0x76,0xd0,
|
|
|
|
0x44,0xad,0xc1,0x4c,0xde,0x33,0xa3,0x61,0x55,0xec,0x24,0xe5,0x79,0x45,0xcf,0x94,0x39,0x92,0x9f,0xd8,
|
|
|
|
0x24,0xce,0x85,0xb9
|
|
|
|
};
|
|
|
|
|
2020-06-03 10:50:37 +02:00
|
|
|
static UCHAR dssKey[] =
|
|
|
|
{
|
|
|
|
0x07,0x02,0x00,0x00,0x00,0x22,0x00,0x00,0x44,0x53,0x53,0x32,0x00,0x04,0x00,0x00,0x01,0xd1,0xfc,0x7a,
|
|
|
|
0x70,0x53,0xb2,0x48,0x70,0x23,0x19,0x1f,0x3c,0xe1,0x26,0x14,0x7e,0x9f,0x0f,0x7f,0x33,0x5e,0x2b,0xf7,
|
|
|
|
0xca,0x01,0x74,0x8c,0xb4,0xfd,0xf6,0x44,0x95,0x35,0x56,0xaa,0x4d,0x62,0x48,0xe2,0xd1,0xa2,0x7e,0x6e,
|
|
|
|
0xeb,0xd6,0xcc,0x7c,0xe8,0xfd,0x21,0x9a,0xa2,0xfd,0x7a,0x9d,0x1a,0x38,0x69,0x87,0x39,0x5a,0x91,0xc0,
|
|
|
|
0x52,0x2b,0x9f,0x2a,0x54,0x78,0x37,0x82,0x9a,0x70,0x57,0xab,0xec,0x93,0x8e,0xac,0x73,0x04,0xe8,0x53,
|
|
|
|
0x72,0x72,0x32,0xc6,0xcb,0xef,0x47,0x98,0x3c,0x56,0x49,0x62,0xcb,0xbb,0xe7,0x34,0x84,0xa6,0x72,0x3a,
|
|
|
|
0xbe,0x26,0x46,0x86,0xca,0xcb,0x35,0x62,0x4f,0x19,0x18,0x0b,0xb0,0x78,0xae,0xd5,0x42,0xdf,0x26,0xdb,
|
|
|
|
0x85,0x63,0x77,0x85,0x01,0x3b,0x32,0xbe,0x5c,0xf8,0x05,0xc8,0xde,0x17,0x7f,0xb9,0x03,0x82,0xfa,0xf1,
|
|
|
|
0x9e,0x32,0x73,0xfa,0x8d,0xea,0xa3,0x30,0x48,0xe2,0xdf,0x5a,0xcb,0x83,0x3d,0xff,0x56,0xe9,0xc0,0x94,
|
|
|
|
0xf8,0x6d,0xb3,0xaf,0x4a,0x97,0xb9,0x43,0x0e,0xd4,0x28,0x98,0x57,0x2e,0x3a,0xca,0xde,0x6f,0x45,0x0d,
|
|
|
|
0xfb,0x58,0xec,0x78,0x34,0x2e,0x46,0x4d,0xfe,0x98,0x02,0xbb,0xef,0x07,0x1a,0x13,0xb6,0xc2,0x2c,0x06,
|
|
|
|
0xd9,0x0c,0xc4,0xb0,0x4c,0x3a,0xfc,0x01,0x63,0xb5,0x5a,0x5d,0x2d,0x9c,0x47,0x04,0x67,0x51,0xf2,0x52,
|
|
|
|
0xf5,0x82,0x36,0xeb,0x6e,0x66,0x58,0x4c,0x10,0x2c,0x29,0x72,0x4a,0x6f,0x6b,0x6c,0xe0,0x93,0x31,0x42,
|
|
|
|
0xf6,0xda,0xfa,0x5b,0x22,0x43,0x9b,0x1a,0x98,0x71,0xe7,0x41,0x74,0xe9,0x12,0xa4,0x1f,0x27,0x0a,0x63,
|
|
|
|
0x94,0x49,0xd7,0xad,0xa5,0xc4,0x5c,0xc3,0xc9,0x70,0xb3,0x7b,0x16,0xb6,0x1d,0xd4,0x09,0xc4,0x9a,0x46,
|
|
|
|
0x2d,0x0e,0x75,0x07,0x31,0x7b,0xed,0x45,0xcd,0x99,0x84,0x14,0xf1,0x01,0x00,0x00,0x93,0xd5,0xa3,0xe4,
|
|
|
|
0x34,0x05,0xeb,0x98,0x3b,0x5f,0x2f,0x11,0xa4,0xa5,0xc4,0xff,0xfb,0x22,0x7c,0x54
|
|
|
|
};
|
|
|
|
|
2020-05-29 11:29:06 +02:00
|
|
|
static void test_DSA(void)
|
|
|
|
{
|
|
|
|
BCRYPT_ALG_HANDLE alg;
|
|
|
|
BCRYPT_KEY_HANDLE key;
|
|
|
|
BCRYPT_DSA_KEY_BLOB *dsablob;
|
|
|
|
UCHAR sig[40], schemes;
|
|
|
|
ULONG len, size;
|
|
|
|
NTSTATUS ret;
|
|
|
|
BYTE *buf;
|
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptOpenAlgorithmProvider(&alg, BCRYPT_DSA_ALGORITHM, NULL, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(!ret, "got %#lx\n", ret);
|
2020-05-29 11:29:06 +02:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptGetProperty(alg, L"PaddingSchemes", (UCHAR *)&schemes, sizeof(schemes), &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_NOT_SUPPORTED, "got %#lx\n", ret);
|
2020-05-29 11:29:06 +02:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptImportKeyPair(alg, NULL, BCRYPT_DSA_PUBLIC_BLOB, &key, dsaPublicBlob, sizeof(dsaPublicBlob), 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(!ret, "got %#lx\n", ret);
|
2020-05-29 11:29:06 +02:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptVerifySignature(key, NULL, dsaHash, sizeof(dsaHash), dsaSignature, sizeof(dsaSignature), 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(!ret, "got %#lx\n", ret);
|
2020-05-29 11:29:06 +02:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDestroyKey(key);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(!ret, "got %#lx\n", ret);
|
2020-05-29 11:29:06 +02:00
|
|
|
|
|
|
|
/* sign/verify with export/import round-trip */
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptGenerateKeyPair(alg, &key, 512, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2020-05-29 11:29:06 +02:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptFinalizeKeyPair(key, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2020-05-29 11:29:06 +02:00
|
|
|
|
|
|
|
len = 0;
|
|
|
|
memset(sig, 0, sizeof(sig));
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptSignHash(key, NULL, dsaHash, sizeof(dsaHash), sig, sizeof(sig), &len, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(!ret, "got %#lx\n", ret);
|
|
|
|
ok(len == 40, "got %lu\n", len);
|
2020-05-29 11:29:06 +02:00
|
|
|
|
|
|
|
size = 0;
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptExportKey(key, NULL, BCRYPT_DSA_PUBLIC_BLOB, NULL, 0, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2020-05-29 11:29:06 +02:00
|
|
|
ok(size, "size not set\n");
|
|
|
|
|
|
|
|
buf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptExportKey(key, NULL, BCRYPT_DSA_PUBLIC_BLOB, buf, size, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2020-05-29 11:29:06 +02:00
|
|
|
dsablob = (BCRYPT_DSA_KEY_BLOB *)buf;
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(dsablob->dwMagic == BCRYPT_DSA_PUBLIC_MAGIC, "got %#lx\n", dsablob->dwMagic);
|
|
|
|
ok(dsablob->cbKey == 64, "got %lu\n", dsablob->cbKey);
|
|
|
|
ok(size == sizeof(*dsablob) + dsablob->cbKey * 3, "got %lu\n", size);
|
2020-05-29 11:29:06 +02:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDestroyKey(key);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(!ret, "got %#lx\n", ret);
|
2020-05-29 11:29:06 +02:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptImportKeyPair(alg, NULL, BCRYPT_DSA_PUBLIC_BLOB, &key, buf, size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2020-05-29 11:29:06 +02:00
|
|
|
HeapFree(GetProcessHeap(), 0, buf);
|
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptVerifySignature(key, NULL, dsaHash, sizeof(dsaHash), sig, len, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(!ret, "got %#lx\n", ret);
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDestroyKey(key);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(!ret, "got %#lx\n", ret);
|
2020-05-29 11:29:06 +02:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptImportKeyPair(alg, NULL, LEGACY_DSA_V2_PRIVATE_BLOB, &key, dssKey, sizeof(dssKey), 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(!ret, "got %#lx\n", ret);
|
2020-06-03 10:50:37 +02:00
|
|
|
|
|
|
|
size = 0;
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptExportKey(key, NULL, LEGACY_DSA_V2_PRIVATE_BLOB, NULL, 0, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
2020-06-03 10:50:37 +02:00
|
|
|
ok(size, "size not set\n");
|
|
|
|
|
|
|
|
buf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptExportKey(key, NULL, LEGACY_DSA_V2_PRIVATE_BLOB, buf, size, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(ret == STATUS_SUCCESS, "got %#lx\n", ret);
|
|
|
|
ok(size == sizeof(dssKey), "got %lu expected %Iu\n", size, sizeof(dssKey));
|
2020-06-03 10:50:37 +02:00
|
|
|
ok(!memcmp(dssKey, buf, size), "wrong data\n");
|
|
|
|
HeapFree(GetProcessHeap(), 0, buf);
|
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptDestroyKey(key);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(!ret, "got %#lx\n", ret);
|
2020-10-19 11:24:10 +02:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
ret = BCryptCloseAlgorithmProvider(alg, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(!ret, "got %#lx\n", ret);
|
2020-05-29 11:29:06 +02:00
|
|
|
}
|
|
|
|
|
2020-07-18 00:54:01 +02:00
|
|
|
static void test_SecretAgreement(void)
|
|
|
|
{
|
|
|
|
BCRYPT_SECRET_HANDLE secret;
|
|
|
|
BCRYPT_ALG_HANDLE alg;
|
|
|
|
BCRYPT_KEY_HANDLE key;
|
|
|
|
NTSTATUS status;
|
|
|
|
ULONG size;
|
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
status = BCryptOpenAlgorithmProvider(&alg, BCRYPT_ECDH_P256_ALGORITHM, NULL, 0);
|
2020-07-18 00:54:01 +02:00
|
|
|
if (status)
|
|
|
|
{
|
2022-02-09 10:57:53 +01:00
|
|
|
skip("Failed to open BCRYPT_ECDH_P256_ALGORITHM provider %#lx\n", status);
|
2020-07-18 00:54:01 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
key = NULL;
|
2021-12-03 15:49:30 +01:00
|
|
|
status = BCryptGenerateKeyPair(alg, &key, 256, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(status == STATUS_SUCCESS, "got %#lx\n", status);
|
2020-07-18 00:54:01 +02:00
|
|
|
ok(key != NULL, "key not set\n");
|
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
status = BCryptFinalizeKeyPair(key, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(status == STATUS_SUCCESS, "got %#lx\n", status);
|
2020-07-18 00:54:01 +02:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
status = BCryptSecretAgreement(NULL, key, &secret, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(status == STATUS_INVALID_HANDLE, "got %#lx\n", status);
|
2020-07-18 00:54:01 +02:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
status = BCryptSecretAgreement(key, NULL, &secret, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(status == STATUS_INVALID_HANDLE, "got %#lx\n", status);
|
2020-07-18 00:54:01 +02:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
status = BCryptSecretAgreement(key, key, NULL, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(status == STATUS_INVALID_PARAMETER, "got %#lx\n", status);
|
2020-07-18 00:54:01 +02:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
status = BCryptSecretAgreement(key, key, &secret, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(status == STATUS_SUCCESS, "got %#lx\n", status);
|
2020-07-18 00:54:01 +02:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
status = BCryptDeriveKey(NULL, L"HASH", NULL, NULL, 0, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(status == STATUS_INVALID_HANDLE, "got %#lx\n", status);
|
2020-07-18 00:54:01 +02:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
status = BCryptDeriveKey(key, L"HASH", NULL, NULL, 0, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(status == STATUS_INVALID_HANDLE, "got %#lx\n", status);
|
2020-07-18 00:54:01 +02:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
status = BCryptDeriveKey(secret, NULL, NULL, NULL, 0, &size, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(status == STATUS_INVALID_PARAMETER, "got %#lx\n", status);
|
2020-07-18 00:54:01 +02:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
status = BCryptDeriveKey(secret, L"HASH", NULL, NULL, 0, &size, 0);
|
2020-07-18 00:54:01 +02:00
|
|
|
todo_wine
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(status == STATUS_SUCCESS, "got %#lx\n", status);
|
2020-07-18 00:54:01 +02:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
status = BCryptDestroyHash(secret);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(status == STATUS_INVALID_PARAMETER, "got %#lx\n", status);
|
2020-07-18 00:54:01 +02:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
status = BCryptDestroyKey(secret);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(status == STATUS_INVALID_HANDLE, "got %#lx\n", status);
|
2020-07-18 00:54:01 +02:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
status = BCryptDestroySecret(NULL);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(status == STATUS_INVALID_HANDLE, "got %#lx\n", status);
|
2020-07-18 00:54:01 +02:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
status = BCryptDestroySecret(alg);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(status == STATUS_INVALID_HANDLE, "got %#lx\n", status);
|
2020-07-18 00:54:01 +02:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
status = BCryptDestroySecret(secret);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(status == STATUS_SUCCESS, "got %#lx\n", status);
|
2020-07-18 00:54:01 +02:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
status = BCryptDestroyKey(key);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(status == STATUS_SUCCESS, "got %#lx\n", status);
|
2020-07-18 00:54:01 +02:00
|
|
|
|
2021-12-03 15:49:30 +01:00
|
|
|
status = BCryptCloseAlgorithmProvider(alg, 0);
|
2022-02-09 10:57:53 +01:00
|
|
|
ok(status == STATUS_SUCCESS, "got %#lx\n", status);
|
2020-07-18 00:54:01 +02:00
|
|
|
}
|
|
|
|
|
2014-02-03 19:24:48 +01:00
|
|
|
START_TEST(bcrypt)
|
|
|
|
{
|
2016-03-24 06:22:22 +01:00
|
|
|
HMODULE module;
|
|
|
|
|
2016-12-02 21:36:40 +01:00
|
|
|
module = LoadLibraryA("bcrypt.dll");
|
|
|
|
if (!module)
|
|
|
|
{
|
|
|
|
win_skip("bcrypt.dll not found\n");
|
|
|
|
return;
|
|
|
|
}
|
2019-02-13 10:21:15 +01:00
|
|
|
pBCryptHash = (void *)GetProcAddress(module, "BCryptHash");
|
2016-03-24 06:22:22 +01:00
|
|
|
|
2014-02-03 19:24:48 +01:00
|
|
|
test_BCryptGenRandom();
|
2014-08-16 00:18:06 +02:00
|
|
|
test_BCryptGetFipsAlgorithmMode();
|
2017-12-07 19:45:43 +01:00
|
|
|
test_hashes();
|
2019-02-13 10:21:18 +01:00
|
|
|
test_BcryptHash();
|
|
|
|
test_BcryptDeriveKeyPBKDF2();
|
2016-04-04 11:07:05 +02:00
|
|
|
test_rng();
|
2020-11-10 14:42:27 +01:00
|
|
|
test_3des();
|
2016-12-02 21:36:41 +01:00
|
|
|
test_aes();
|
2016-12-02 21:36:42 +01:00
|
|
|
test_BCryptGenerateSymmetricKey();
|
2016-12-02 21:36:43 +01:00
|
|
|
test_BCryptEncrypt();
|
|
|
|
test_BCryptDecrypt();
|
2018-02-28 13:33:06 +01:00
|
|
|
test_key_import_export();
|
2018-03-23 14:06:11 +01:00
|
|
|
test_ECDSA();
|
2018-03-26 15:04:36 +02:00
|
|
|
test_RSA();
|
2019-06-17 11:52:49 +02:00
|
|
|
test_RSA_SIGN();
|
2019-02-13 10:21:15 +01:00
|
|
|
test_ECDH();
|
2019-06-17 11:52:48 +02:00
|
|
|
test_BCryptEnumContextFunctions();
|
2019-09-23 15:48:52 +02:00
|
|
|
test_BCryptSignHash();
|
2019-09-30 17:12:57 +02:00
|
|
|
test_BCryptEnumAlgorithms();
|
2019-11-06 10:16:54 +01:00
|
|
|
test_aes_vector();
|
2020-05-26 20:14:38 +02:00
|
|
|
test_BcryptDeriveKeyCapi();
|
2020-05-29 11:29:06 +02:00
|
|
|
test_DSA();
|
2020-07-18 00:54:01 +02:00
|
|
|
test_SecretAgreement();
|
2016-03-24 06:22:22 +01:00
|
|
|
|
2016-12-02 21:36:40 +01:00
|
|
|
FreeLibrary(module);
|
2014-02-03 19:24:48 +01:00
|
|
|
}
|