wined3d: Pass a wined3d_device_context to wined3d_cs_emit_blt_sub_resource().
[wine/zf.git] / dlls / dssenh / tests / dssenh.c
blob4d33fc86cd8d979002eda2a3e52ecd85ba4034e3
1 /*
2 * Unit tests for dss functions
4 * Copyright (c) 2012 Marek Kamil Chmiel
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <string.h>
22 #include <stdio.h>
23 #include "ntstatus.h"
24 #define WIN32_NO_STATUS
25 #include "wine/test.h"
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winerror.h"
29 #include "wincrypt.h"
31 static void test_acquire_context(void)
32 { /* failure tests common between all four CSP providers */
34 HCRYPTPROV hProv = 0;
35 BOOL result;
37 /* cannot acquire provider with 0 as Prov Type and NULL as CSP name */
38 SetLastError(0xdeadbeef);
39 result = CryptAcquireContextA(&hProv, NULL, NULL, 0, 0);
40 ok(!result && GetLastError() == NTE_BAD_PROV_TYPE,
41 "Expected NTE_BAD_PROV_TYPE, got %08x\n", GetLastError());
43 SetLastError(0xdeadbeef);
44 result = CryptAcquireContextA(&hProv, NULL, NULL, 0, CRYPT_VERIFYCONTEXT);
45 ok(!result && GetLastError() == NTE_BAD_PROV_TYPE,
46 "Expected NTE_BAD_PROV_TYPE, got %08x\n", GetLastError());
48 /* flag allows us to delete a keyset, but not of an unknown provider */
49 SetLastError(0xdeadbeef);
50 result = CryptAcquireContextA(&hProv, NULL, NULL, 0, CRYPT_DELETEKEYSET);
51 ok(!result && GetLastError() == NTE_BAD_PROV_TYPE,
52 "Expected NTE_BAD_PROV_TYPE, got %08x\n", GetLastError());
54 /* cannot acquire along with PROV_RSA_SIG, not compatible */
55 SetLastError(0xdeadbeef);
56 result = CryptAcquireContextA(&hProv, NULL, MS_DEF_DSS_PROV_A, PROV_RSA_SIG, 0);
57 ok(!result && GetLastError() == NTE_PROV_TYPE_NO_MATCH,
58 "Expected NTE_PROV_TYPE_NO_MATCH, got %08x\n", GetLastError());
60 /* cannot acquire along with MS_DEF_RSA_SIG_PROV_A, not compatible */
61 SetLastError(0xdeadbeef);
62 result = CryptAcquireContextA(&hProv, NULL, MS_DEF_RSA_SIG_PROV_A, PROV_DSS, 0);
63 ok(!result && GetLastError() == NTE_KEYSET_NOT_DEF,
64 "Expected NTE_KEYSET_NOT_DEF, got %08x\n", GetLastError());
66 /* cannot acquire provider with 0 as Prov Type */
67 SetLastError(0xdeadbeef);
68 result = CryptAcquireContextA(&hProv, NULL, MS_DEF_DSS_PROV_A, 0, 0);
69 ok(!result && GetLastError() == NTE_BAD_PROV_TYPE,
70 "Expected NTE_BAD_PROV_TYPE, got %08x\n", GetLastError());
72 /* test base DSS provider (PROV_DSS) */
74 result = CryptAcquireContextA(
75 &hProv, NULL, MS_DEF_DSS_PROV_A, PROV_DSS, CRYPT_VERIFYCONTEXT);
76 if(!result)
78 skip("DSS csp is currently not available, skipping tests.\n");
79 return;
81 ok(result, "Expected no errors.\n");
83 result = CryptReleaseContext(hProv, 0);
84 ok(result, "Expected release of the provider.\n");
86 result = CryptAcquireContextA(
87 &hProv, NULL, MS_DEF_DSS_DH_PROV_A, PROV_DSS_DH, CRYPT_NEWKEYSET);
88 ok(result || GetLastError() == NTE_EXISTS, "Expected no errors or NTE_EXISTS\n");
90 if (result)
92 result = CryptReleaseContext(hProv, 0);
93 ok(result, "Expected release of the provider.\n");
96 result = CryptAcquireContextA(&hProv, NULL, NULL, PROV_DSS, 0);
97 ok(result, "Expected no errors.\n");
99 result = CryptReleaseContext(hProv, 0);
100 ok(result, "Expected release of the provider.\n");
102 result = CryptAcquireContextA(&hProv, NULL, MS_DEF_DSS_PROV_A, PROV_DSS, 0);
103 ok(result, "Expected no errors.\n");
105 /* test DSS Diffie Hellman provider (PROV_DSS_DH) */
107 result = CryptAcquireContextA(
108 &hProv, NULL, MS_DEF_DSS_DH_PROV_A, PROV_DSS_DH, CRYPT_VERIFYCONTEXT);
109 ok(result, "Expected no errors.\n");
111 result = CryptReleaseContext(hProv, 0);
112 ok(result, "Expected release of the provider.\n");
114 result = CryptAcquireContextA(&hProv, NULL, NULL, PROV_DSS_DH, 0);
115 ok(result, "Expected no errors.\n");
117 result = CryptReleaseContext(hProv, 0);
118 ok(result, "Expected release of the provider.\n");
120 result = CryptAcquireContextA(&hProv, NULL, MS_DEF_DSS_DH_PROV_A, PROV_DSS_DH, 0);
121 ok(result, "Expected no errors.\n");
123 /* test DSS Enhanced provider (MS_ENH_DSS_DH_PROV_A) */
125 SetLastError(0xdeadbeef);
126 result = CryptAcquireContextA(
127 &hProv, NULL, MS_ENH_DSS_DH_PROV_A, PROV_DSS_DH, CRYPT_VERIFYCONTEXT);
128 ok(result, "Expected no errors.\n");
130 result = CryptReleaseContext(hProv, 0);
131 ok(result, "Expected release of the provider.\n");
133 result = CryptAcquireContextA(&hProv, NULL, MS_ENH_DSS_DH_PROV_A, PROV_DSS_DH, 0);
134 ok(result, "Expected no errors.\n");
136 result = CryptReleaseContext(hProv, 0);
137 ok(result, "Expected release of the provider.\n");
139 /* test DSS Schannel provider (PROV_DH_SCHANNEL) */
141 result = CryptAcquireContextA(
142 &hProv, NULL, MS_DEF_DH_SCHANNEL_PROV_A, PROV_DH_SCHANNEL, CRYPT_VERIFYCONTEXT);
143 ok(result, "Expected no errors.\n");
145 result = CryptReleaseContext(hProv, 0);
146 ok(result, "Expected release of the provider.\n");
148 result = CryptAcquireContextA(&hProv, NULL, NULL, PROV_DH_SCHANNEL, 0);
149 ok(result, "Expected no errors.\n");
151 result = CryptReleaseContext(hProv, 0);
152 ok(result, "Expected release of the provider.\n");
154 result = CryptAcquireContextA(
155 &hProv, NULL, MS_DEF_DH_SCHANNEL_PROV_A, PROV_DH_SCHANNEL, 0);
156 ok(result, "Expected no errors.\n");
158 result = CryptReleaseContext(hProv, 0);
159 ok(result, "Expected release of the provider.\n");
161 /* failure tests, cannot acquire context because the key container already exists */
162 SetLastError(0xdeadbeef);
163 result = CryptAcquireContextA(
164 &hProv, NULL, MS_DEF_DSS_DH_PROV_A, PROV_DSS_DH, CRYPT_NEWKEYSET);
165 ok(!result && GetLastError() == NTE_EXISTS,
166 "Expected NTE_EXISTS, got %08x\n", GetLastError());
168 SetLastError(0xdeadbeef);
169 result = CryptAcquireContextA(
170 &hProv, NULL, MS_ENH_DSS_DH_PROV_A, PROV_DSS_DH, CRYPT_NEWKEYSET);
171 ok(!result && GetLastError() == NTE_EXISTS,
172 "Expected NTE_EXISTS, got %08x\n", GetLastError());
174 SetLastError(0xdeadbeef);
175 result = CryptAcquireContextA(
176 &hProv, NULL, MS_DEF_DH_SCHANNEL_PROV_A, PROV_DH_SCHANNEL, CRYPT_NEWKEYSET);
177 ok(!result && GetLastError() == NTE_EXISTS,
178 "Expected NTE_EXISTS, got %08x\n", GetLastError());
181 struct keylength_test {
182 ALG_ID algid;
183 DWORD flags;
184 BOOL expectedResult;
185 DWORD expectedError;
186 DWORD brokenError;
187 int todo_result;
188 int todo_error;
191 static const struct keylength_test baseDSS_keylength[] = {
192 /* AT_KEYEXCHANGE is not supported by the base DSS provider */
193 {AT_KEYEXCHANGE, 448 << 16, FALSE, NTE_BAD_ALGID, 0, 0, 1},
194 {AT_KEYEXCHANGE, 512 << 16, FALSE, NTE_BAD_ALGID, 0, 0, 1},
195 {AT_KEYEXCHANGE, 1024 << 16, FALSE, NTE_BAD_ALGID, 0, 0, 1},
196 {AT_KEYEXCHANGE, 1088 << 16, FALSE, NTE_BAD_ALGID, 0, 0, 1},
197 /* min 512 max 1024 increment by 64 */
198 {AT_SIGNATURE, 448 << 16, FALSE, NTE_BAD_FLAGS},
199 {AT_SIGNATURE, 512 << 16, TRUE},
200 {AT_SIGNATURE, 513 << 16, FALSE, STATUS_INVALID_PARAMETER, NTE_FAIL},
201 {AT_SIGNATURE, 768 << 16, TRUE},
202 {AT_SIGNATURE, 1024 << 16, TRUE},
203 {AT_SIGNATURE, 1088 << 16, FALSE, NTE_BAD_FLAGS},
204 /* CALG_DH_EPHEM is not supported by the base DSS provider */
205 {CALG_DH_EPHEM, 448 << 16, FALSE, NTE_BAD_ALGID, 0, 0, 1},
206 {CALG_DH_EPHEM, 512 << 16, FALSE, NTE_BAD_ALGID, 0, 0, 1},
207 {CALG_DH_EPHEM, 1024 << 16, FALSE, NTE_BAD_ALGID, 0, 0, 1},
208 {CALG_DH_EPHEM, 1088 << 16, FALSE, NTE_BAD_ALGID, 0, 0, 1},
209 /* CALG_DH_SF is not supported by the base DSS provider */
210 {CALG_DH_SF, 448 << 16, FALSE, NTE_BAD_ALGID, 0, 0, 1},
211 {CALG_DH_SF, 512 << 16, FALSE, NTE_BAD_ALGID, 0, 0, 1},
212 {CALG_DH_SF, 1024 << 16, FALSE, NTE_BAD_ALGID, 0, 0, 1},
213 {CALG_DH_SF, 1088 << 16, FALSE, NTE_BAD_ALGID, 0, 0, 1},
214 /* min 512 max 1024, increment by 64 */
215 {CALG_DSS_SIGN, 448 << 16, FALSE, NTE_BAD_FLAGS},
216 {CALG_DSS_SIGN, 512 << 16, TRUE},
217 {CALG_DSS_SIGN, 513 << 16, FALSE, STATUS_INVALID_PARAMETER, NTE_FAIL},
218 {CALG_DSS_SIGN, 768 << 16, TRUE},
219 {CALG_DSS_SIGN, 1024 << 16, TRUE},
220 {CALG_DSS_SIGN, 1088 << 16, FALSE, NTE_BAD_FLAGS}
223 static const struct keylength_test dssDH_keylength[] = {
224 /* min 512 max 1024, increment by 64 */
225 {AT_KEYEXCHANGE, 448 << 16, FALSE, NTE_BAD_FLAGS},
226 {AT_KEYEXCHANGE, 512 << 16, TRUE, 0, 0, 1},
227 {AT_KEYEXCHANGE, 513 << 16, FALSE, NTE_BAD_FLAGS, 0, 0, 1},
228 {AT_KEYEXCHANGE, 768 << 16, TRUE, 0, 0, 1},
229 {AT_KEYEXCHANGE, 1024 << 16, TRUE, 0, 0, 1},
230 {AT_KEYEXCHANGE, 1088 << 16, FALSE, NTE_BAD_FLAGS},
231 {AT_SIGNATURE, 448 << 16, FALSE, NTE_BAD_FLAGS},
232 {AT_SIGNATURE, 512 << 16, TRUE},
233 {AT_SIGNATURE, 513 << 16, FALSE, STATUS_INVALID_PARAMETER, NTE_FAIL},
234 {AT_SIGNATURE, 768 << 16, TRUE},
235 {AT_SIGNATURE, 1024 << 16, TRUE},
236 {AT_SIGNATURE, 1088 << 16, FALSE, NTE_BAD_FLAGS},
237 {CALG_DH_EPHEM, 448 << 16, FALSE, NTE_BAD_FLAGS},
238 {CALG_DH_EPHEM, 512 << 16, TRUE, 0, 0, 1},
239 {CALG_DH_EPHEM, 513 << 16, FALSE, NTE_BAD_FLAGS, 0, 0, 1},
240 {CALG_DH_EPHEM, 768 << 16, TRUE, 0, 0, 1},
241 {CALG_DH_EPHEM, 1024 << 16, TRUE, 0, 0, 1},
242 {CALG_DH_EPHEM, 1088 << 16, FALSE, NTE_BAD_FLAGS},
243 {CALG_DH_SF, 448 << 16, FALSE, NTE_BAD_FLAGS},
244 {CALG_DH_SF, 512 << 16, TRUE, 0, 0, 1},
245 {CALG_DH_SF, 513 << 16, FALSE, NTE_BAD_FLAGS, 0, 0, 1},
246 {CALG_DH_SF, 768 << 16, TRUE, 0, 0, 1},
247 {CALG_DH_SF, 1024 << 16, TRUE, 0, 0, 1},
248 {CALG_DH_SF, 1088 << 16, FALSE, NTE_BAD_FLAGS},
249 {CALG_DSS_SIGN, 448 << 16, FALSE, NTE_BAD_FLAGS},
250 {CALG_DSS_SIGN, 512 << 16, TRUE},
251 {CALG_DSS_SIGN, 513 << 16, FALSE, STATUS_INVALID_PARAMETER, NTE_FAIL},
252 {CALG_DSS_SIGN, 768 << 16, TRUE},
253 {CALG_DSS_SIGN, 1024 << 16, TRUE},
254 {CALG_DSS_SIGN, 1088 << 16, FALSE, NTE_BAD_FLAGS}
257 static const struct keylength_test dssENH_keylength[] = {
258 /* min 512 max 1024 (AT_KEYEXCHANGE, CALG_DH_EPHEM, CALG_DH_SF max 4096), increment by 64*/
259 {AT_KEYEXCHANGE, 448 << 16, FALSE, NTE_BAD_FLAGS},
260 {AT_KEYEXCHANGE, 512 << 16, TRUE, 0, 0, 1},
261 {AT_KEYEXCHANGE, 513 << 16, FALSE, NTE_BAD_FLAGS, 0, 0, 1},
262 {AT_KEYEXCHANGE, 768 << 16, TRUE, 0, 0, 1},
263 {AT_KEYEXCHANGE, 1024 << 16, TRUE, 0, 0, 1},
264 {AT_KEYEXCHANGE, 1088 << 16, TRUE, 0, 0, 1},
265 {AT_KEYEXCHANGE, 2048 << 16, TRUE, 0, 0, 1},
266 /* Keylength too large - test bot timeout.
267 {AT_KEYEXCHANGE, 3072 << 16, TRUE},
268 {AT_KEYEXCHANGE, 4096 << 16, TRUE}, */
269 {AT_KEYEXCHANGE, 4160 << 16, FALSE, NTE_BAD_FLAGS},
270 {AT_SIGNATURE, 448 << 16, FALSE, NTE_BAD_FLAGS},
271 {AT_SIGNATURE, 512 << 16, TRUE},
272 {AT_SIGNATURE, 513 << 16, FALSE, STATUS_INVALID_PARAMETER, NTE_FAIL},
273 {AT_SIGNATURE, 768 << 16, TRUE},
274 {AT_SIGNATURE, 1024 << 16, TRUE},
275 {AT_SIGNATURE, 1032 << 16, FALSE, NTE_BAD_FLAGS},
276 {CALG_DH_EPHEM, 448 << 16, FALSE, NTE_BAD_FLAGS},
277 {CALG_DH_EPHEM, 512 << 16, TRUE, 0, 0, 1},
278 {CALG_DH_EPHEM, 513 << 16, FALSE, NTE_BAD_FLAGS, 0, 0, 1},
279 {CALG_DH_EPHEM, 768 << 16, TRUE, 0, 0, 1},
280 {CALG_DH_EPHEM, 1024 << 16, TRUE, 0, 0, 1},
281 {CALG_DH_EPHEM, 1040 << 16, FALSE, NTE_BAD_FLAGS},
282 {CALG_DH_EPHEM, 1088 << 16, TRUE, 0, 0, 1},
283 {CALG_DH_EPHEM, 4160 << 16, FALSE, NTE_BAD_FLAGS},
284 {CALG_DH_SF, 448 << 16, FALSE, NTE_BAD_FLAGS},
285 {CALG_DH_SF, 512 << 16, TRUE, 0, 0, 1},
286 {CALG_DH_SF, 513 << 16, FALSE, NTE_BAD_FLAGS, 0, 0, 1},
287 {CALG_DH_SF, 768 << 16, TRUE, 0, 0, 1},
288 {CALG_DH_SF, 1024 << 16, TRUE, 0, 0, 1},
289 {CALG_DH_SF, 1032 << 16, FALSE, NTE_BAD_FLAGS},
290 {CALG_DH_SF, 1088 << 16, TRUE, 0, 0, 1},
291 {CALG_DH_SF, 4160 << 16, FALSE, NTE_BAD_FLAGS},
292 {CALG_DSS_SIGN, 448 << 16, FALSE, NTE_BAD_FLAGS},
293 {CALG_DSS_SIGN, 512 << 16, TRUE},
294 {CALG_DSS_SIGN, 513 << 16, FALSE, STATUS_INVALID_PARAMETER, NTE_FAIL},
295 {CALG_DSS_SIGN, 768 << 16, TRUE},
296 {CALG_DSS_SIGN, 1024 << 16, TRUE},
297 {CALG_DSS_SIGN, 1088 << 16, FALSE, NTE_BAD_FLAGS}
300 static void test_keylength_array(HCRYPTPROV hProv,const struct keylength_test *tests, int testLen)
302 HCRYPTKEY key;
303 BOOL result;
304 int i;
306 for (i = 0; i < testLen; i++)
308 SetLastError(0xdeadbeef);
309 result = CryptGenKey(hProv, tests[i].algid, tests[i].flags, &key);
311 /* success */
312 if (tests[i].expectedResult)
314 todo_wine_if (tests[i].todo_result) ok(result, "%d: got %08x\n", i, GetLastError());
315 if (result)
317 result = CryptDestroyKey(key);
318 ok(result, "%d: got %08x\n", i, GetLastError());
321 else
323 todo_wine_if (tests[i].todo_result) ok(!result, "%d: got %x\n", i, GetLastError());
324 todo_wine_if (tests[i].todo_error)
325 ok(GetLastError() == tests[i].expectedError ||
326 broken(GetLastError() == tests[i].brokenError), "%d: got %08x\n", i, GetLastError());
331 static void test_keylength(void)
333 HCRYPTPROV hProv = 0;
334 HCRYPTKEY key;
335 BOOL result;
337 /* acquire base dss provider */
338 result = CryptAcquireContextA(
339 &hProv, NULL, MS_DEF_DSS_PROV_A, PROV_DSS, CRYPT_VERIFYCONTEXT);
340 if(!result)
342 skip("DSSENH is currently not available, skipping key length tests.\n");
343 return;
345 ok(result, "Expected no errors.\n");
347 result = CryptGenKey(hProv, AT_SIGNATURE, 0, &key);
348 ok(result, "Expected no errors.\n");
350 result = CryptDestroyKey(key);
351 ok(result, "Expected no errors.\n");
353 /* perform keylength tests */
354 test_keylength_array(hProv, baseDSS_keylength, ARRAY_SIZE(baseDSS_keylength));
356 result = CryptReleaseContext(hProv, 0);
357 ok(result, "Expected release of CSP provider.\n");
359 /* acquire diffie hellman dss provider */
360 result = CryptAcquireContextA(
361 &hProv, NULL, MS_DEF_DSS_DH_PROV_A, PROV_DSS_DH, CRYPT_VERIFYCONTEXT);
362 ok(result, "Expected no errors.\n");
364 /* perform keylength tests */
365 test_keylength_array(hProv, dssDH_keylength, ARRAY_SIZE(dssDH_keylength));
367 result = CryptReleaseContext(hProv, 0);
368 ok(result, "Expected release of CSP provider.\n");
370 /* acquire enhanced dss provider */
371 SetLastError(0xdeadbeef);
372 result = CryptAcquireContextA(
373 &hProv, NULL, MS_ENH_DSS_DH_PROV_A, PROV_DSS_DH, CRYPT_VERIFYCONTEXT);
374 ok(result, "Expected no errors.\n");
376 /* perform keylength tests */
377 test_keylength_array(hProv, dssENH_keylength, ARRAY_SIZE(dssENH_keylength));
379 result = CryptReleaseContext(hProv, 0);
380 ok(result, "Expected release of CSP provider.\n");
382 /* acquire schannel dss provider */
383 result = CryptAcquireContextA(
384 &hProv, NULL, MS_DEF_DH_SCHANNEL_PROV_A, PROV_DH_SCHANNEL, CRYPT_VERIFYCONTEXT);
385 ok(result, "Expected no errors.\n");
387 /* perform keylength tests */
388 test_keylength_array(hProv, dssENH_keylength, ARRAY_SIZE(dssENH_keylength));
390 result = CryptReleaseContext(hProv, 0);
391 ok(result, "Expected release of CSP provider.\n");
394 struct hash_test {
395 ALG_ID algid;
396 BYTE* input;
397 DWORD dataLen;
398 BYTE hash[20];
399 DWORD hashLen;
402 static const char testHashVal1[] = "I love working with Wine";
403 static const char testHashVal2[] = "Wine is not an emulater.";
404 static const char testHashVal3[] = "";
406 static const struct hash_test hash_data[] = {
407 {CALG_MD5, (BYTE *)testHashVal1, sizeof(testHashVal1),
408 {0x4f, 0xf4, 0xd0, 0xdf, 0xe8, 0xf6, 0x6b, 0x1b,
409 0x87, 0xea, 0xca, 0x3d, 0xe8, 0x3c, 0xdd, 0xae}, 16},
410 {CALG_MD5, (BYTE *)testHashVal2, sizeof(testHashVal2),
411 {0x80, 0x5c, 0x1c, 0x0e, 0x79, 0x70, 0xd9, 0x38,
412 0x04, 0x46, 0x19, 0xbe, 0x38, 0x1f, 0xef, 0xe1}, 16},
413 {CALG_MD5, (BYTE *)testHashVal3, sizeof(testHashVal3),
414 {0x93, 0xb8, 0x85, 0xad, 0xfe, 0x0d, 0xa0, 0x89,
415 0xcd, 0xf6, 0x34, 0x90, 0x4f, 0xd5, 0x9f, 0x71}, 16},
416 {CALG_SHA, (BYTE *)testHashVal1, sizeof(testHashVal1),
417 {0x2a, 0xd0, 0xc9, 0x42, 0xfb, 0x73, 0x02, 0x48, 0xbb, 0x5f,
418 0xc2, 0xa4, 0x78, 0xdd, 0xe4, 0x3b, 0xfc, 0x76, 0xe9, 0xe2}, 20},
419 {CALG_SHA, (BYTE *)testHashVal2, sizeof(testHashVal2),
420 {0xfd, 0xfc, 0xab, 0x3a, 0xde, 0x33, 0x01, 0x38, 0xfe, 0xbb,
421 0xc3, 0x13, 0x84, 0x20, 0x9e, 0x55, 0x94, 0x8d, 0xc6, 0x05}, 20},
422 {CALG_SHA, (BYTE *)testHashVal3, sizeof(testHashVal3),
423 {0x5b, 0xa9, 0x3c, 0x9d, 0xb0, 0xcf, 0xf9, 0x3f, 0x52, 0xb5,
424 0x21, 0xd7, 0x42, 0x0e, 0x43, 0xf6, 0xed, 0xa2, 0x78, 0x4f}, 20}
427 static void test_hash(const struct hash_test *tests, int testLen)
429 HCRYPTPROV hProv = 0;
430 HCRYPTHASH hHash;
431 BYTE hashValue[36];
432 BOOL result;
433 int i;
435 result = CryptAcquireContextA(
436 &hProv, NULL, MS_ENH_DSS_DH_PROV_A, PROV_DSS_DH, CRYPT_VERIFYCONTEXT);
437 if(!result)
439 skip("DSSENH is currently not available, skipping hashing tests.\n");
440 return;
442 ok(result, "Expected no errors.\n");
444 for(i = 0; i < testLen; i++)
446 BYTE* data = tests[i].input;
447 DWORD dataLen = tests[i].dataLen;
448 DWORD hashLen;
450 /* test algid hash */
451 result = CryptCreateHash(hProv, tests[i].algid, 0, 0, &hHash);
452 ok(result, "Expected creation of a hash.\n");
454 result = CryptHashData(hHash, data, dataLen, 0);
455 ok(result, "Expected data to be added to hash.\n");
457 dataLen = sizeof(DWORD);
458 result = CryptGetHashParam(hHash, HP_HASHSIZE, (BYTE *)&hashLen, &dataLen, 0);
459 ok(result && (hashLen == tests[i].hashLen), "Expected %d hash len, got %d.Error: %x\n",
460 tests[i].hashLen, hashLen, GetLastError());
462 result = CryptGetHashParam(hHash, HP_HASHVAL, hashValue, &hashLen, 0);
463 ok(result, "Expected hash value return.\n");
465 ok(!memcmp(hashValue, tests[i].hash, tests[i].hashLen), "Incorrect hash output.\n");
467 result = CryptHashData(hHash, data, dataLen, 0);
468 ok(!result, "Should not be able to add to hash.\n");
470 result = CryptDestroyHash(hHash);
471 ok(result, "Expected destruction of hash.\n");
473 result = CryptReleaseContext(hProv, 0);
474 ok(result, "Expected release of the DSS Enhanced provider.\n");
477 struct encrypt_test {
478 ALG_ID algid;
479 DWORD keyLength;
480 const char *plain;
481 DWORD plainLen;
482 const BYTE *decrypted;
483 const BYTE *encrypted;
486 static const char dataToEncrypt1[] = "Great performance with Wine.";
487 static const char dataToEncrypt2[] = "Wine implements Windows API";
488 static const char dataToEncrypt3[] = "";
490 static const BYTE encrypted3DES_1[] = {
491 0x6c,0x60,0x19,0x41,0x27,0xc1,0x16,0x69, 0x6f,0x96,0x0c,0x2e,0xa4,0x5f,0xf5,0x6a,
492 0xed,0x4b,0xec,0xd4,0x92,0x0c,0xe2,0x34, 0xe1,0x4a,0xb5,0xe2,0x05,0x43,0xfe,0x17
494 static const BYTE encrypted3DES_2[] = {
495 0x17,0xeb,0x80,0xde,0xac,0x4d,0x9e,0xd0, 0xa9,0xae,0x74,0xb5,0x86,0x1a,0xea,0xb4,
496 0x96,0x27,0x5d,0x75,0x4f,0xdd,0x87,0x60, 0xfc,0xaf,0xa1,0x82,0x83,0x09,0xf1,0xca
498 static const BYTE encrypted3DES_3[] = {0xaf, 0x36, 0xc0, 0x3d, 0x78, 0x64, 0xc4, 0x4a};
500 static const BYTE encrypted3DES112_1[] = {
501 0xb3,0xf8,0x4b,0x08,0xd6,0x23,0xcb,0xca, 0x43,0x26,0xd9,0x9f,0x6b,0x99,0x09,0xe9,
502 0x8c,0x4c,0x7d,0xef,0x49,0xda,0x0b,0x44, 0xcc,0x8d,0x06,0x6b,0xed,0xb7,0xf1,0x67
504 static const BYTE encrypted3DES112_2[] = {
505 0xdc,0xcf,0x93,0x11,0x7a,0xe4,0xcd,0x3f, 0x11,0xd8,0xe0,0x1e,0xe0,0x8d,0x9c,0xba,
506 0x97,0x5d,0x74,0x4d,0x83,0x03,0x5c,0xf2, 0x01,0xaf,0xed,0x7a,0x87,0x8f,0x88,0x8b
508 static const BYTE encrypted3DES112_3[] = {0x04, 0xb3, 0x9c, 0x59, 0x48, 0xc7, 0x2f, 0xd1};
510 static const BYTE encryptedDES_1[] = {
511 0x3d,0xdc,0x54,0xaf,0x66,0x72,0x4e,0xef, 0x9d,0x35,0x02,0xc2,0x1a,0xf4,0x1f,0x01,
512 0xb1,0xaf,0x13,0xd9,0xbe,0x7b,0xd4,0xf3, 0xf5,0x9d,0x2a,0xd8,0x32,0x90,0xe9,0x0b
514 static const BYTE encryptedDES_2[] = {
515 0xa8,0x05,0xd7,0xe9,0x61,0xf4,0x6c,0xce, 0x95,0x2b,0x52,0x08,0x25,0x03,0x30,0xac,
516 0xd7,0xe7,0xd3,0x07,0xb2,0x68,0x63,0x7b, 0xe3,0xab,0x26,0x1e,0x5c,0xec,0x42,0x4f
518 static const BYTE encryptedDES_3[] = {0x35, 0x02, 0xbb, 0x7c, 0x43, 0x5b, 0xf5, 0x59};
520 static const BYTE encryptedRC2_1[] = {
521 0x9e,0xcb,0xa2,0x27,0xc2,0xec,0x10,0xe0, 0x94,0xb3,0xc3,0x9d,0x7d,0xe2,0x12,0xe4,
522 0xb0,0xde,0xd9,0x46,0xca,0x1f,0xa6,0xfa, 0xa4,0x79,0x08,0x59,0xa6,0x00,0x62,0x16
524 static const BYTE encryptedRC2_2[] = {
525 0x29,0x06,0xfd,0xa1,0xe0,0x88,0x89,0xb0, 0x4d,0x7f,0x96,0x9d,0x2c,0x44,0xa1,0xd2,
526 0xbe,0xc6,0xaf,0x10,0xb8,0x86,0x68,0x1b, 0x1d,0x9f,0x3c,0xc4,0x12,0x02,0xbc,0x73
528 static const BYTE encryptedRC2_3[] = {0x26,0x40,0x73,0xfe,0x13,0xbb,0x32,0xa8};
530 static const BYTE encryptedRC4_1[] = {
531 0x5a,0x48,0xeb,0x16,0x96,0x23,0x16,0xb7, 0xbb,0x36,0xe8,0x43,0x88,0x74,0xb1,0x9d,
532 0x96,0xf0,0x84,0x0f,0x5a,0x56,0xf9,0x62, 0xae,0xb5,0x4a,0xce,0x52
534 static const BYTE encryptedRC4_2[] = {
535 0x4a,0x53,0xe0,0x12,0xc2,0x6a,0x0b,0xa2, 0xa5,0x35,0xea,0x54,0x8b,0x61,0xac,0xde,
536 0xa4,0xb9,0x9d,0x02,0x41,0x49,0xaa,0x15, 0x86,0x8b,0x66,0xe0
538 static const BYTE encryptedRC4_3[] = {0x1d};
540 static const struct encrypt_test encrypt_data[] = {
541 {CALG_3DES, 168 << 16, dataToEncrypt1, sizeof(dataToEncrypt1), (BYTE *)dataToEncrypt1,
542 encrypted3DES_1},
543 {CALG_3DES, 168 << 16, dataToEncrypt2, sizeof(dataToEncrypt2), (BYTE *)dataToEncrypt2,
544 encrypted3DES_2},
545 {CALG_3DES, 168 << 16, dataToEncrypt3, sizeof(dataToEncrypt3), (BYTE *)dataToEncrypt3,
546 encrypted3DES_3},
547 {CALG_3DES_112, 112 << 16, dataToEncrypt1, sizeof(dataToEncrypt1), (BYTE *)dataToEncrypt1,
548 encrypted3DES112_1},
549 {CALG_3DES_112, 112 << 16, dataToEncrypt2, sizeof(dataToEncrypt2), (BYTE *)dataToEncrypt2,
550 encrypted3DES112_2},
551 {CALG_3DES_112, 112 << 16, dataToEncrypt3, sizeof(dataToEncrypt3), (BYTE *)dataToEncrypt3,
552 encrypted3DES112_3},
553 {CALG_DES, 56 << 16, dataToEncrypt1, sizeof(dataToEncrypt1), (BYTE *)dataToEncrypt1,
554 encryptedDES_1},
555 {CALG_DES, 56 << 16, dataToEncrypt2, sizeof(dataToEncrypt2), (BYTE *)dataToEncrypt2,
556 encryptedDES_2},
557 {CALG_DES, 56 << 16, dataToEncrypt3, sizeof(dataToEncrypt3), (BYTE *)dataToEncrypt3,
558 encryptedDES_3},
559 /* CALG_RC2 key unexpected results under Win2K when default key length is used, here we use
560 minimum length because Win2K's DSSENH provider has a different default key length compared
561 to the younger operating systems, though there is no default key len issue with CALG_RC4 */
562 {CALG_RC2, 40 << 16, dataToEncrypt1, sizeof(dataToEncrypt1), (BYTE *)dataToEncrypt1,
563 encryptedRC2_1},
564 {CALG_RC2, 40 << 16, dataToEncrypt2, sizeof(dataToEncrypt2), (BYTE *)dataToEncrypt2,
565 encryptedRC2_2},
566 {CALG_RC2, 40 << 16, dataToEncrypt3, sizeof(dataToEncrypt3), (BYTE *)dataToEncrypt3,
567 encryptedRC2_3},
568 {CALG_RC4, 40 << 16, dataToEncrypt1, sizeof(dataToEncrypt1), (BYTE *)dataToEncrypt1,
569 encryptedRC4_1},
570 {CALG_RC4, 40 << 16, dataToEncrypt2, sizeof(dataToEncrypt2), (BYTE *)dataToEncrypt2,
571 encryptedRC4_2},
572 {CALG_RC4, 40 << 16, dataToEncrypt3, sizeof(dataToEncrypt3), (BYTE *)dataToEncrypt3,
573 encryptedRC4_3}
576 static void test_data_encryption(const struct encrypt_test *tests, int testLen)
577 { /* Here we test the same encryption ciphers as the RSAENH cryptographic service provider */
578 HCRYPTPROV hProv = 0;
579 HCRYPTKEY pKey = 0;
580 HCRYPTHASH hHash;
581 const char dataToHash[] = "I love working with Wine";
582 unsigned char pbData[36];
583 DWORD dataLen;
584 BOOL result;
585 int i;
587 /* acquire dss enhanced provider */
588 SetLastError(0xdeadbeef);
589 result = CryptAcquireContextA(
590 &hProv, NULL, MS_ENH_DSS_DH_PROV_A, PROV_DSS_DH, CRYPT_VERIFYCONTEXT);
591 if (!result)
593 skip("DSSENH is currently not available, skipping encryption tests.\n");
594 return;
596 ok(result, "Expected no errors.\n");
598 /* testing various encryption algorithms */
599 for(i = 0; i < testLen; i++)
601 memcpy(pbData, tests[i].plain, tests[i].plainLen);
602 dataLen = tests[i].plainLen;
604 SetLastError(0xdeadbeef);
605 result = CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash);
606 ok(result, "Expected creation of a MD5 hash for key derivation.\n");
608 result = CryptHashData(hHash, (BYTE *)dataToHash, sizeof(dataToHash), 0);
609 ok(result, "Expected data to be added to hash for key derivation.\n");
611 /* Derive key */
612 result = CryptDeriveKey(hProv, tests[i].algid, hHash, tests[i].keyLength, &pKey);
613 if (!result)
615 skip("skipping encryption tests\n");
616 return;
618 ok(result, "Expected a derived key.\n");
620 result = CryptDestroyHash(hHash);
621 ok(result, "Expected destruction of hash after deriving key.\n");
623 /* testing CryptEncrypt with ALGID from array */
624 result = CryptEncrypt(pKey, 0, TRUE, 0, pbData, &dataLen, 36);
625 ok(result, "Expected data encryption.\n");
627 /* Verify we have received expected encrypted data */
628 ok(!memcmp(pbData, tests[i].encrypted, dataLen), "Incorrect encrypted data.\n");
630 result = CryptDecrypt(pKey, 0, TRUE, 0, pbData, &dataLen);
631 ok(result, "Expected data decryption.\n");
633 /* Verify we have received expected decrypted data */
634 ok(!memcmp(pbData, tests[i].decrypted, dataLen) ||
635 broken(tests[i].algid == CALG_RC4), "Incorrect decrypted data.\n");
637 result = CryptDestroyKey(pKey);
638 ok(result, "Expected no DestroyKey errors.\n");
640 result = CryptReleaseContext(hProv, 0);
641 ok(result, "Expected release of the provider\n");
644 struct ciphermode_test {
645 DWORD cipherMode;
646 BOOL expectedResult;
647 DWORD expectedError;
648 const BYTE *encrypted;
651 static const BYTE encryptedCFB[] = {
652 0x51,0x15,0x77,0xab,0x62,0x1f,0x7d,0xcb, 0x35,0x1e,0xd8,0xd3,0x2a,0x00,0xf0,0x94,
653 0x7c,0xa5,0x28,0xda,0xb8,0x81,0x15,0x99, 0xd1,0xd5,0x06,0x1d,0xd3,0x46,0x7e,0xca
655 static const BYTE encryptedCBC[] = {
656 0x8f,0x7b,0x56,0xeb,0xad,0x4d,0x76,0xc2, 0xd5,0x1d,0xf0,0x60,0x9d,0xde,0x96,0xe8,
657 0xb7,0x7b,0xeb,0x4b,0xee,0x3f,0xae,0x05, 0x20,0xf5,0xe0,0x75,0xa0,0x1d,0xf9,0x39
659 static const BYTE encryptedECB[] = {
660 0x8f,0x7b,0x56,0xeb,0xad,0x4d,0x76,0xc2, 0x8b,0xe0,0x4e,0xe4,0x98,0x4f,0xb8,0x3b,
661 0xf3,0xeb,0x6f,0x0a,0x57,0x91,0xdd,0xc7, 0x34,0x5d,0x4c,0xa3,0x7e,0x97,0xbf,0xee
664 static const struct ciphermode_test ciphermode_data[] = {
665 {CRYPT_MODE_CFB, TRUE, 0xdeadbeef, encryptedCFB}, /* Testing cipher block chaining */
666 {CRYPT_MODE_CBC, TRUE, 0xdeadbeef, encryptedCBC}, /* Testing cipher feedback */
667 {CRYPT_MODE_ECB, TRUE, 0xdeadbeef, encryptedECB}, /* Testing electronic codebook */
668 {CRYPT_MODE_OFB, FALSE, NTE_BAD_DATA}/* DSSENH does not support Output Feedback cipher mode */
671 static void test_cipher_modes(const struct ciphermode_test *tests, int testLen)
673 HCRYPTPROV hProv = 0;
674 HCRYPTKEY pKey = 0;
675 HCRYPTHASH hHash;
676 const char plainText[] = "Testing block cipher modes.";
677 const char dataToHash[] = "GSOC is awesome!";
678 unsigned char pbData[36];
679 int plainLen = sizeof(plainText), i;
680 DWORD mode, dataLen;
681 BOOL result;
683 /* acquire dss enhanced provider */
684 SetLastError(0xdeadbeef);
685 result = CryptAcquireContextA(
686 &hProv, NULL, MS_ENH_DSS_DH_PROV_A, PROV_DSS_DH, CRYPT_VERIFYCONTEXT);
687 if (!result)
689 skip("DSSENH is currently not available, skipping block cipher mode tests.\n");
690 return;
692 ok(result, "Expected no errors.\n");
694 SetLastError(0xdeadbeef);
695 result = CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash);
696 ok(result, "Expected creation of a MD5 hash for key derivation.\n");
698 result = CryptHashData(hHash, (BYTE *)dataToHash, sizeof(dataToHash), 0);
699 ok(result, "Expected data to be added to hash for key derivation.\n");
701 /* Derive a CALG_RC2 key, but could be any other encryption cipher */
702 result = CryptDeriveKey(hProv, CALG_RC2, hHash, 40 << 16, &pKey);
703 if (!result)
705 skip("skipping cipher mode tests\n");
706 return;
708 ok(result, "Expected a derived key.\n");
710 result = CryptDestroyHash(hHash);
711 ok(result, "Expected destruction of hash after deriving key.\n");
713 /* the default algorithm is CBC, test that without setting a mode */
714 mode = 0xdeadbeef;
715 dataLen = sizeof(mode);
716 result = CryptGetKeyParam(pKey, KP_MODE, (BYTE*)&mode, &dataLen, 0);
717 ok(result, "Expected getting of KP_MODE, got %x.\n", GetLastError());
718 ok(mode == CRYPT_MODE_CBC, "Default mode should be CBC\n");
720 memcpy(pbData, plainText, plainLen);
721 dataLen = plainLen;
722 result = CryptEncrypt(pKey, 0, TRUE, 0, pbData, &dataLen, 36);
723 ok(result, "Expected data encryption, got %x.\n", GetLastError());
725 /* Verify we have the correct encrypted data */
726 ok(!memcmp(pbData, tests[1].encrypted, dataLen), "Incorrect encrypted data.\n");
728 result = CryptDecrypt(pKey, 0, TRUE, 0, pbData, &dataLen);
729 ok(result, "Expected data decryption, got %x.\n", GetLastError());
731 /* Verify we have the correct decrypted data */
732 ok(!memcmp(pbData, (BYTE *)plainText, dataLen), "Incorrect decrypted data.\n");
734 /* test block cipher modes */
735 for(i = 0; i < testLen; i++)
737 SetLastError(0xdeadbeef);
738 dataLen = plainLen;
739 mode = tests[i].cipherMode;
740 memcpy(pbData, plainText, plainLen);
742 result = CryptSetKeyParam(pKey, KP_MODE, (BYTE*)&mode, 0);
743 if(tests[i].expectedResult)
745 ok(result, "Expected setting of KP_MODE, got %x.\n", GetLastError());
747 result = CryptEncrypt(pKey, 0, TRUE, 0, pbData, &dataLen, 36);
748 ok(result, "Expected data encryption, got %x.\n", GetLastError());
750 /* Verify we have the correct encrypted data */
751 ok(!memcmp(pbData, tests[i].encrypted, dataLen), "Incorrect encrypted data.\n");
753 result = CryptDecrypt(pKey, 0, TRUE, 0, pbData, &dataLen);
754 ok(result, "Expected data decryption, got %x.\n", GetLastError());
756 /* Verify we have the correct decrypted data */
757 ok(!memcmp(pbData, (BYTE *)plainText, dataLen), "Incorrect decrypted data.\n");
759 else
760 { /* Expected error */
761 ok(!result && GetLastError() == tests[i].expectedError, "Expected %d, got %x.\n",
762 tests[i].expectedError, GetLastError());
765 result = CryptDestroyKey(pKey);
766 ok(result, "Expected no DestroyKey errors.\n");
768 result = CryptReleaseContext(hProv, 0);
769 ok(result, "Expected release of the provider.\n");
772 struct signature_test {
773 const BYTE *privateKey;
774 DWORD keyLen;
775 BYTE* signData;
776 DWORD dataLen;
779 static const char dataToSign1[] = "Put your hands up for Cryptography :)";
780 static const char dataToSign2[] = "With DSSENH implemented, applications requiring it will now work.";
781 static const char dataToSign3[] = "";
783 static const BYTE AT_SIGNATURE_PrivateKey[] = {
784 0x07,0x02,0x00,0x00,0x00,0x22,0x00,0x00, 0x44,0x53,0x53,0x32,0x00,0x04,0x00,0x00,
785 0x01,0xd1,0xfc,0x7a,0x70,0x53,0xb2,0x48, 0x70,0x23,0x19,0x1f,0x3c,0xe1,0x26,0x14,
786 0x7e,0x9f,0x0f,0x7f,0x33,0x5e,0x2b,0xf7, 0xca,0x01,0x74,0x8c,0xb4,0xfd,0xf6,0x44,
787 0x95,0x35,0x56,0xaa,0x4d,0x62,0x48,0xe2, 0xd1,0xa2,0x7e,0x6e,0xeb,0xd6,0xcc,0x7c,
788 0xe8,0xfd,0x21,0x9a,0xa2,0xfd,0x7a,0x9d, 0x1a,0x38,0x69,0x87,0x39,0x5a,0x91,0xc0,
789 0x52,0x2b,0x9f,0x2a,0x54,0x78,0x37,0x82, 0x9a,0x70,0x57,0xab,0xec,0x93,0x8e,0xac,
790 0x73,0x04,0xe8,0x53,0x72,0x72,0x32,0xc6, 0xcb,0xef,0x47,0x98,0x3c,0x56,0x49,0x62,
791 0xcb,0xbb,0xe7,0x34,0x84,0xa6,0x72,0x3a, 0xbe,0x26,0x46,0x86,0xca,0xcb,0x35,0x62,
792 0x4f,0x19,0x18,0x0b,0xb0,0x78,0xae,0xd5, 0x42,0xdf,0x26,0xdb,0x85,0x63,0x77,0x85,
793 0x01,0x3b,0x32,0xbe,0x5c,0xf8,0x05,0xc8, 0xde,0x17,0x7f,0xb9,0x03,0x82,0xfa,0xf1,
794 0x9e,0x32,0x73,0xfa,0x8d,0xea,0xa3,0x30, 0x48,0xe2,0xdf,0x5a,0xcb,0x83,0x3d,0xff,
795 0x56,0xe9,0xc0,0x94,0xf8,0x6d,0xb3,0xaf, 0x4a,0x97,0xb9,0x43,0x0e,0xd4,0x28,0x98,
796 0x57,0x2e,0x3a,0xca,0xde,0x6f,0x45,0x0d, 0xfb,0x58,0xec,0x78,0x34,0x2e,0x46,0x4d,
797 0xfe,0x98,0x02,0xbb,0xef,0x07,0x1a,0x13, 0xb6,0xc2,0x2c,0x06,0xd9,0x0c,0xc4,0xb0,
798 0x4c,0x3a,0xfc,0x01,0x63,0xb5,0x5a,0x5d, 0x2d,0x9c,0x47,0x04,0x67,0x51,0xf2,0x52,
799 0xf5,0x82,0x36,0xeb,0x6e,0x66,0x58,0x4c, 0x10,0x2c,0x29,0x72,0x4a,0x6f,0x6b,0x6c,
800 0xe0,0x93,0x31,0x42,0xf6,0xda,0xfa,0x5b, 0x22,0x43,0x9b,0x1a,0x98,0x71,0xe7,0x41,
801 0x74,0xe9,0x12,0xa4,0x1f,0x27,0x0a,0x63, 0x94,0x49,0xd7,0xad,0xa5,0xc4,0x5c,0xc3,
802 0xc9,0x70,0xb3,0x7b,0x16,0xb6,0x1d,0xd4, 0x09,0xc4,0x9a,0x46,0x2d,0x0e,0x75,0x07,
803 0x31,0x7b,0xed,0x45,0xcd,0x99,0x84,0x14, 0xf1,0x01,0x00,0x00,0x93,0xd5,0xa3,0xe4,
804 0x34,0x05,0xeb,0x98,0x3b,0x5f,0x2f,0x11, 0xa4,0xa5,0xc4,0xff,0xfb,0x22,0x7c,0x54
807 static const BYTE DSS_SIGN_PrivateKey[] = {
808 0x07,0x02,0x00,0x00,0x00,0x22,0x00,0x00, 0x44,0x53,0x53,0x32,0x00,0x04,0x00,0x00,
809 0xf7,0x9e,0x89,0xa2,0xcd,0x0b,0x61,0xe0, 0xa3,0xe5,0x86,0x6b,0x04,0x98,0x80,0x9c,
810 0x36,0xc2,0x76,0x4e,0x22,0xd5,0x21,0xaa, 0x03,0x59,0xf4,0x95,0xb2,0x11,0x1f,0xa0,
811 0xc5,0xfc,0xbe,0x5d,0x1f,0x2e,0xf4,0x36, 0x40,0x48,0x81,0x51,0xb4,0x25,0x86,0xe0,
812 0x98,0xc8,0x4d,0xa0,0x08,0x99,0xa1,0x00, 0x45,0x1b,0x75,0x6b,0x0d,0x3e,0x7d,0x13,
813 0xd7,0x23,0x32,0x08,0xf4,0xeb,0x27,0x9e, 0xe9,0x05,0x5d,0xac,0xc8,0xd7,0x62,0x13,
814 0x43,0x2a,0x69,0x65,0xdc,0xe6,0x52,0xf9, 0x6a,0xe8,0x07,0xcf,0x3e,0xf8,0xc9,0x1d,
815 0x8e,0xdf,0x4e,0x9a,0xd1,0x48,0xf2,0xda, 0x9e,0xfa,0x92,0x5f,0x6d,0x57,0xf2,0xa4,
816 0x5f,0x60,0xce,0x92,0x7a,0x80,0x39,0x21, 0x9d,0x4d,0x3a,0x60,0x76,0x4c,0x2f,0xc0,
817 0xd3,0xf4,0x14,0x03,0x03,0x05,0xa9,0x0c, 0x57,0x72,0x4f,0x60,0x3c,0xe9,0x09,0x54,
818 0x0c,0x2a,0x56,0xda,0x30,0xb6,0x2e,0x6a, 0x96,0x7f,0x4a,0x8f,0x83,0x0a,0xb9,0x5c,
819 0xff,0x84,0xfa,0x0e,0x85,0x81,0x46,0xe9, 0x1c,0xbb,0x78,0x1d,0x78,0x25,0x00,0x8c,
820 0x78,0x56,0x68,0xe4,0x06,0x37,0xcc,0xc7, 0x22,0x27,0xee,0x0e,0xf8,0xca,0xfc,0x72,
821 0x0e,0xd6,0xe6,0x90,0x30,0x66,0x22,0xe2, 0xa2,0xbf,0x2e,0x35,0xbc,0xe7,0xd6,0x24,
822 0x6a,0x3d,0x06,0xe8,0xe2,0xbe,0x96,0xcc, 0x9a,0x08,0x06,0xb5,0x44,0x83,0xb0,0x7b,
823 0x70,0x7b,0x2d,0xc3,0x46,0x9a,0xc5,0x6b, 0xd9,0xde,0x9a,0x24,0xc9,0xea,0xf5,0x28,
824 0x69,0x8a,0x17,0xca,0xdf,0xc4,0x0e,0xa3, 0x08,0x22,0x99,0xd2,0x27,0xdc,0x9b,0x08,
825 0x54,0x4a,0xf9,0xb1,0x74,0x3a,0x9d,0xd9, 0xc2,0x82,0x21,0xf5,0x97,0x04,0x90,0x37,
826 0xda,0xd9,0xdc,0x19,0xad,0x83,0xcd,0x35, 0xb0,0x4e,0x06,0x68,0xd1,0x69,0x7e,0x73,
827 0x93,0xbe,0xa5,0x05,0xb3,0xcc,0xd2,0x51, 0x3c,0x00,0x00,0x00,0x16,0xe1,0xac,0x17,
828 0xdc,0x68,0xae,0x03,0xad,0xf7,0xb9,0xca, 0x0d,0xca,0x27,0xef,0x76,0xda,0xe5,0xcb
831 static const struct signature_test dssSign_data[] = {
832 {AT_SIGNATURE_PrivateKey, sizeof(AT_SIGNATURE_PrivateKey), (BYTE *)dataToSign1, sizeof(dataToSign1)},
833 {AT_SIGNATURE_PrivateKey, sizeof(AT_SIGNATURE_PrivateKey), (BYTE *)dataToSign2, sizeof(dataToSign2)},
834 {AT_SIGNATURE_PrivateKey, sizeof(AT_SIGNATURE_PrivateKey), (BYTE *)dataToSign3, sizeof(dataToSign3)},
835 {DSS_SIGN_PrivateKey, sizeof(DSS_SIGN_PrivateKey), (BYTE *)dataToSign1, sizeof(dataToSign1)},
836 {DSS_SIGN_PrivateKey, sizeof(DSS_SIGN_PrivateKey), (BYTE *)dataToSign2, sizeof(dataToSign2)},
837 {DSS_SIGN_PrivateKey, sizeof(DSS_SIGN_PrivateKey), (BYTE *)dataToSign3, sizeof(dataToSign3)}
840 static void test_signhash_array(HCRYPTPROV hProv, const struct signature_test *tests, int testLen)
842 HCRYPTHASH hHash1, hHash2;
843 HCRYPTKEY privKey = 0, pubKey = 0;
844 BYTE pubKeyBuffer[512];
845 BYTE signValue1[40], signValue2[40];
846 BYTE hashValue1[40], hashValue2[40];
847 DWORD hashLen1, hashLen2, pubKeyLen;
848 DWORD dataLen1, dataLen2;
849 BOOL result;
850 int i;
852 for (i = 0; i < testLen; i++)
854 DWORD signLen1 = tests[i].dataLen;
855 DWORD signLen2 = tests[i].dataLen;
857 /* Get a private key of array specified ALG_ID */
858 result = CryptImportKey(hProv, tests[i].privateKey, tests[i].keyLen, 0, 0, &privKey);
859 ok(result, "Failed to imported key, got %x\n", GetLastError());
861 /* Create hash object and add data for signature 1 */
862 result = CryptCreateHash(hProv, CALG_SHA, 0, 0, &hHash1);
863 ok(result, "Failed to create a hash, got %x\n", GetLastError());
865 result = CryptHashData(hHash1, tests[i].signData, signLen1, 0);
866 ok(result, "Failed to add data to hash, got %x\n", GetLastError());
868 /* Create hash object and add data for signature 2 */
869 result = CryptCreateHash(hProv, CALG_SHA, 0, 0, &hHash2);
870 ok(result, "Failed to create a hash, got %x\n", GetLastError());
872 result = CryptHashData(hHash2, tests[i].signData, signLen2, 0);
873 ok(result, "Failed to add data to hash, got %x\n", GetLastError());
875 /* Acquire hash length and hash value */
876 dataLen1 = sizeof(DWORD);
877 result = CryptGetHashParam(hHash1, HP_HASHSIZE, (BYTE *)&hashLen1, &dataLen1, 0);
878 ok(result, "Failed to get hash length, got %x\n", GetLastError());
880 result = CryptGetHashParam(hHash1, HP_HASHVAL, hashValue1, &hashLen1, 0);
881 ok(result, "Failed to return hash value.\n");
883 dataLen2 = sizeof(DWORD);
884 result = CryptGetHashParam(hHash2, HP_HASHSIZE, (BYTE *)&hashLen2, &dataLen2, 0);
885 ok(result, "Failed to get hash length, got %x\n", GetLastError());
887 result = CryptGetHashParam(hHash2, HP_HASHVAL, hashValue2, &hashLen2, 0);
888 ok(result, "Failed to return hash value.\n");
890 /* Compare hashes to ensure they are the same */
891 ok(hashLen1 == hashLen2, "Hash lengths were not the same.\n");
892 ok(!memcmp(hashValue1, hashValue2, hashLen2), "Hashes were not identical.\n");
894 /* Sign hash 1 */
895 signLen1 = 0;
896 result = CryptSignHashA(hHash1, AT_SIGNATURE, NULL, 0, NULL, &signLen1);
897 ok(result, "Failed to get signature length, got %x\n", GetLastError());
898 ok(signLen1 == 40, "Expected a 40-byte signature, got %d\n", signLen1);
900 result = CryptSignHashA(hHash1, AT_SIGNATURE, NULL, 0, signValue1, &signLen1);
901 ok(result, "Failed to sign hash, got %x\n", GetLastError());
903 /* Sign hash 2 */
904 signLen2 = 0;
905 result = CryptSignHashA(hHash2, AT_SIGNATURE, NULL, 0, NULL, &signLen2);
906 ok(result, "Failed to get signature length, got %x\n", GetLastError());
907 ok(signLen2 == 40, "Expected a 40-byte signature, got %d\n", signLen2);
909 result = CryptSignHashA(hHash2, AT_SIGNATURE, NULL, 0, signValue2, &signLen2);
910 ok(result, "Failed to sign hash2, got %x\n", GetLastError());
912 /* Compare signatures to ensure they are both different, because every DSS signature
913 should be different even if the input hash data is identical */
914 ok(memcmp(signValue1, signValue2, signLen2), "Expected two different signatures from "
915 "the same hash input.\n");
917 result = CryptExportKey(privKey, 0, PUBLICKEYBLOB, 0, NULL, &pubKeyLen);
918 ok(result, "Failed to acquire public key length, got %x\n", GetLastError());
920 /* Export the public key */
921 result = CryptExportKey(privKey, 0, PUBLICKEYBLOB, 0, pubKeyBuffer, &pubKeyLen);
922 ok(result, "Failed to export public key, got %x\n", GetLastError());
924 result = CryptDestroyHash(hHash1);
925 ok(result, "Failed to destroy hash1, got %x\n", GetLastError());
926 result = CryptDestroyHash(hHash2);
927 ok(result, "Failed to destroy hash2, got %x\n", GetLastError());
929 /* Destroy the private key */
930 result = CryptDestroyKey(privKey);
931 ok(result, "Failed to destroy private key, got %x\n", GetLastError());
933 /* Import the public key we obtained earlier */
934 result = CryptImportKey(hProv, pubKeyBuffer, pubKeyLen, 0, 0, &pubKey);
935 ok(result, "Failed to import public key, got %x\n", GetLastError());
937 result = CryptCreateHash(hProv, CALG_SHA, 0, 0, &hHash1);
938 ok(result, "Failed to create hash, got %x\n", GetLastError());
940 /* Hash the data to compare with the signed hash */
941 result = CryptHashData(hHash1, tests[i].signData, tests[i].dataLen, 0);
942 ok(result, "Failed to add data to hash1, got %x\n", GetLastError());
944 /* Verify signed hash 1 */
945 result = CryptVerifySignatureA(hHash1, signValue1, sizeof(signValue1), pubKey, NULL, 0);
946 if (!result)
948 skip("skipping sign tests\n");
949 return;
951 ok(result, "Failed to verify signature, got %x\n", GetLastError());
953 result = CryptCreateHash(hProv, CALG_SHA, 0, 0, &hHash2);
954 ok(result, "Failed to create hash, got %x\n", GetLastError());
956 /* Hash the data to compare with the signed hash */
957 result = CryptHashData(hHash2, tests[i].signData, tests[i].dataLen, 0);
958 ok(result, "Failed to add data to hash2, got %x\n", GetLastError());
960 /* Verify signed hash 2 */
961 result = CryptVerifySignatureA(hHash2, signValue2, sizeof(signValue2), pubKey, NULL, 0);
962 ok(result, "Failed to verify signature, got %x\n", GetLastError());
964 result = CryptDestroyHash(hHash1);
965 ok(result, "Failed to destroy hash1, got %x\n", GetLastError());
966 result = CryptDestroyHash(hHash2);
967 ok(result, "Failed to destroy hash2, got %x\n", GetLastError());
969 /* Destroy the public key */
970 result = CryptDestroyKey(pubKey);
971 ok(result, "Failed to destroy public key, got %x\n", GetLastError());
975 static void test_verify_signature(void)
977 HCRYPTPROV hProv = 0;
978 BOOL result;
980 /* acquire base dss provider */
981 result = CryptAcquireContextA(&hProv, NULL, MS_DEF_DSS_PROV_A, PROV_DSS, 0);
982 if(!result)
984 skip("DSSENH is currently not available, skipping signature verification tests.\n");
985 return;
987 ok(result, "Failed to acquire CSP.\n");
989 test_signhash_array(hProv, dssSign_data, ARRAY_SIZE(dssSign_data));
991 result = CryptReleaseContext(hProv, 0);
992 ok(result, "Failed to release CSP provider.\n");
994 /* acquire diffie hellman dss provider */
995 result = CryptAcquireContextA(&hProv, NULL, MS_DEF_DSS_DH_PROV_A, PROV_DSS_DH, 0);
996 ok(result, "Failed to acquire CSP.\n");
998 test_signhash_array(hProv, dssSign_data, ARRAY_SIZE(dssSign_data));
1000 result = CryptReleaseContext(hProv, 0);
1001 ok(result, "Failed to release CSP provider.\n");
1003 /* acquire enhanced dss provider */
1004 SetLastError(0xdeadbeef);
1005 result = CryptAcquireContextA(&hProv, NULL, MS_ENH_DSS_DH_PROV_A, PROV_DSS_DH, 0);
1006 ok(result, "Failed to acquire CSP.\n");
1008 test_signhash_array(hProv, dssSign_data, ARRAY_SIZE(dssSign_data));
1010 result = CryptReleaseContext(hProv, 0);
1011 ok(result, "Failed to release CSP provider.\n");
1013 /* acquire schannel dss provider */
1014 result = CryptAcquireContextA(&hProv, NULL, MS_DEF_DH_SCHANNEL_PROV_A, PROV_DH_SCHANNEL, 0);
1015 ok(result, "Failed to acquire CSP.\n");
1017 test_signhash_array(hProv, dssSign_data, ARRAY_SIZE(dssSign_data));
1019 result = CryptReleaseContext(hProv, 0);
1020 ok(result, "Failed to release CSP provider.\n");
1023 struct keyExchange_test {
1024 ALG_ID algid;
1025 const BYTE *primeNum;
1026 int primeLen;
1027 const BYTE *generatorNum;
1028 int generatorLen;
1031 /* AT_KEYEXCHANGE interprets to CALG_DH_SF by the DSSENH cryptographic service provider */
1032 static const BYTE primeAT_KEYEXCHANGE[] = {
1033 0x65,0x28,0x24,0xd8,0xbe,0x3f,0x95,0x93, 0x3c,0x4d,0x1b,0x51,0xe1,0x89,0x9a,0x90,
1034 0x5e,0xa0,0x6c,0x25,0xf0,0xb5,0x93,0x98, 0xba,0x76,0x9d,0x54,0x78,0xf6,0xdc,0x04,
1035 0xe1,0xd0,0x87,0x8f,0xa0,0xe4,0x2f,0xb4, 0x09,0x78,0x24,0xbf,0xc8,0x7f,0x59,0xf4,
1036 0x07,0xee,0x07,0x20,0x1b,0x2d,0x54,0x2a, 0xb5,0xb4,0x8f,0x8c,0x33,0x73,0xec,0xaf
1039 static const BYTE generatorAT_KEYEXCHANGE[] = {
1040 0xdc,0x62,0x20,0xe7,0x36,0xa2,0xa6,0xef, 0x77,0x91,0xa8,0xa3,0x6d,0x60,0x70,0x0d,
1041 0x1d,0x79,0xb1,0xbe,0xa8,0x87,0x69,0x39, 0x29,0xaa,0x54,0x27,0x05,0xe6,0x6f,0xa5,
1042 0xde,0x82,0x00,0x5d,0x87,0x1f,0x84,0xf7, 0x40,0xec,0x6f,0x15,0x64,0x02,0x0d,0xb8,
1043 0x50,0x48,0x94,0x66,0xb2,0x7d,0xbd,0xf2, 0x66,0xf8,0x40,0x62,0x94,0xbf,0x24,0x3b
1046 static const BYTE primeCALG_DH_EPHEM[] = {
1047 0x17,0x99,0xa9,0x79,0x31,0xb9,0x05,0xdd, 0x7f,0xf0,0x02,0x11,0x4d,0x0d,0xc3,0x81,
1048 0x8b,0x41,0x50,0x41,0x5f,0x07,0xe6,0x8d, 0x02,0xf9,0xaa,0x86,0x2a,0x07,0x07,0xea,
1049 0x0a,0x75,0xed,0x96,0xa7,0x85,0xee,0xac, 0xb1,0x71,0xbd,0x57,0x48,0xbd,0x41,0x0b,
1050 0xde,0x34,0xe2,0xba,0x5b,0x55,0x64,0x77, 0x84,0xfa,0x96,0xd1,0xaf,0x79,0x49,0x9d
1053 static const BYTE generatorCALG_DH_EPHEM[] = {
1054 0xc7,0x64,0x56,0xde,0xf7,0xb4,0xd3,0xd8, 0xa2,0xd4,0x12,0x2d,0x54,0x5c,0x54,0xc8,
1055 0x04,0x11,0x88,0x14,0x6c,0x9f,0x88,0x41, 0x82,0x93,0x32,0xb1,0x82,0x84,0x5b,0x07,
1056 0x55,0x13,0x82,0x7a,0x64,0x7b,0x12,0x09, 0xe2,0xa0,0x28,0x51,0xf4,0x7a,0xd9,0x26,
1057 0x86,0x95,0x5f,0xc0,0x9a,0x25,0xc2,0x7e, 0x91,0x14,0xdd,0x3c,0x4e,0x86,0x4f,0x6f
1060 static const BYTE primeCALG_DH_SF[] = {
1061 0x85,0xb8,0xa5,0x4a,0xcf,0x2b,0x7c,0x61, 0xb2,0x06,0x93,0x8a,0x87,0x37,0x58,0xb0,
1062 0x8d,0xc7,0x2a,0xa7,0x7f,0x0d,0x74,0xf9, 0x3a,0x7e,0xbc,0xab,0x3a,0x54,0xe4,0x11,
1063 0x69,0x6f,0xcd,0xea,0xad,0x32,0x44,0x4f, 0xee,0x54,0x69,0x8c,0x9c,0x3b,0x87,0x27,
1064 0x36,0x70,0x06,0xf3,0x4e,0xde,0x7f,0x9d, 0xa6,0xf2,0xad,0x43,0x90,0xdd,0xb5,0x9b
1067 static const BYTE generatorCALG_DH_SF[] = {
1068 0xea,0xdc,0xe0,0xbb,0x60,0x26,0xc6,0xb3, 0x93,0x6f,0x61,0xe6,0x7e,0xe2,0xee,0xd6,
1069 0xdb,0x3d,0xca,0xa8,0x31,0x46,0x8f,0x5d, 0xb4,0xaa,0x83,0xd3,0x52,0x10,0xcd,0xfb,
1070 0xfd,0xfc,0x14,0x89,0x0c,0xde,0xcf,0x54, 0x1d,0x05,0x8c,0xbe,0x4a,0xe4,0x37,0xb4,
1071 0xc0,0x15,0x75,0xc5,0xa2,0xfc,0x99,0xfc, 0xad,0x63,0xcb,0x7c,0xb8,0x20,0xdc,0x2b
1074 static const struct keyExchange_test baseDSSkey_data[] = {
1075 /* Cannot exchange keys using the base DSS provider, except on WinNT4 */
1076 {AT_KEYEXCHANGE, primeAT_KEYEXCHANGE, sizeof(primeAT_KEYEXCHANGE), generatorAT_KEYEXCHANGE,
1077 sizeof(generatorAT_KEYEXCHANGE)},
1078 {CALG_DH_EPHEM, primeCALG_DH_EPHEM, sizeof(primeCALG_DH_EPHEM), generatorCALG_DH_EPHEM,
1079 sizeof(generatorCALG_DH_EPHEM)},
1080 {CALG_DH_SF, primeCALG_DH_SF, sizeof(generatorCALG_DH_SF), generatorCALG_DH_SF,
1081 sizeof(generatorCALG_DH_SF)}
1084 static const struct keyExchange_test dssDHkey_data[] = {
1085 {AT_KEYEXCHANGE, primeAT_KEYEXCHANGE, sizeof(primeAT_KEYEXCHANGE), generatorAT_KEYEXCHANGE,
1086 sizeof(generatorAT_KEYEXCHANGE)},
1087 {CALG_DH_EPHEM, primeCALG_DH_EPHEM, sizeof(primeCALG_DH_EPHEM), generatorCALG_DH_EPHEM,
1088 sizeof(generatorCALG_DH_EPHEM)},
1089 {CALG_DH_SF, primeCALG_DH_SF, sizeof(generatorCALG_DH_SF), generatorCALG_DH_SF,
1090 sizeof(generatorCALG_DH_SF)}
1093 static void test_keyExchange_baseDSS(HCRYPTPROV hProv, const struct keyExchange_test *tests, int testLen)
1095 HCRYPTKEY privKey1 = 0, privKey2 = 0;
1096 HCRYPTKEY sessionKey1 = 0, sessionKey2 = 0;
1097 const char plainText[] = "Testing shared key.";
1098 unsigned char pbData1[36];
1099 unsigned char pbData2[36];
1100 BYTE pubKeyBuffer1[512], pubKeyBuffer2[512];
1101 DWORD pubKeyLen1, pubKeyLen2, dataLen;
1102 DATA_BLOB Prime, Generator;
1103 int plainLen = sizeof(plainText), i;
1104 ALG_ID algid;
1105 BOOL result;
1107 for(i = 0; i < testLen; i++)
1109 SetLastError(0xdeadbeef);
1111 /* Create the data blobs and the prime and generator */
1112 Prime.cbData = tests[i].primeLen;
1113 Prime.pbData = (BYTE *)tests[i].primeNum;
1114 Generator.cbData = tests[i].generatorLen;
1115 Generator.pbData = (BYTE *)tests[i].generatorNum;
1117 /* Generate key exchange keys for user1 and user2 */
1118 result = CryptGenKey(hProv, tests[i].algid, 512 << 16 | CRYPT_PREGEN, &privKey1);
1119 if (!result)
1121 skip("skipping key exchange tests\n");
1122 return;
1124 ok(!result && GetLastError() == NTE_BAD_ALGID,
1125 "Expected NTE_BAD_ALGID, got %x\n", GetLastError());
1127 result = CryptGenKey(hProv, tests[i].algid, 512 << 16 | CRYPT_PREGEN, &privKey2);
1128 ok(!result && GetLastError() == NTE_BAD_ALGID,
1129 "Expected NTE_BAD_ALGID, got %x\n", GetLastError());
1131 /* Set the prime and generator values, which are agreed upon */
1132 result = CryptSetKeyParam(privKey1, KP_P, (BYTE *)&Prime, 0);
1133 ok(!result && GetLastError() == ERROR_INVALID_PARAMETER,
1134 "Expected ERROR_INVALID_PARAMETER, got %x\n", GetLastError());
1136 result = CryptSetKeyParam(privKey2, KP_P, (BYTE *)&Prime, 0);
1137 ok(!result && GetLastError() == ERROR_INVALID_PARAMETER,
1138 "Expected ERROR_INVALID_PARAMETER, got %x\n", GetLastError());
1140 result = CryptSetKeyParam(privKey1, KP_G, (BYTE *)&Generator, 0);
1141 ok(!result && GetLastError() == ERROR_INVALID_PARAMETER,
1142 "Expected ERROR_INVALID_PARAMETER, got %x\n", GetLastError());
1144 result = CryptSetKeyParam(privKey2, KP_G, (BYTE *)&Generator, 0);
1145 ok(!result && GetLastError() == ERROR_INVALID_PARAMETER,
1146 "Expected ERROR_INVALID_PARAMETER, got %x\n", GetLastError());
1148 /* Generate the secret value for user1 and user2 */
1149 result = CryptSetKeyParam(privKey1, KP_X, NULL, 0);
1150 ok(!result && GetLastError() == ERROR_INVALID_PARAMETER,
1151 "Expected ERROR_INVALID_PARAMETER, got %x\n", GetLastError());
1153 result = CryptSetKeyParam(privKey2, KP_X, NULL, 0);
1154 ok(!result && GetLastError() == ERROR_INVALID_PARAMETER,
1155 "Expected ERROR_INVALID_PARAMETER, got %x\n", GetLastError());
1157 /* Acquire required size for the public keys */
1158 result = CryptExportKey(privKey1, 0, PUBLICKEYBLOB, 0, NULL, &pubKeyLen1);
1159 ok(!result && GetLastError() == ERROR_INVALID_PARAMETER,
1160 "Expected ERROR_INVALID_PARAMETER, got %x\n", GetLastError());
1162 result = CryptExportKey(privKey2, 0, PUBLICKEYBLOB, 0, NULL, &pubKeyLen2);
1163 ok(!result && GetLastError() == ERROR_INVALID_PARAMETER,
1164 "Expected ERROR_INVALID_PARAMETER, got %x\n", GetLastError());
1166 /* Export public key which will be calculated into the shared key */
1167 result = CryptExportKey(privKey1, 0, PUBLICKEYBLOB, 0, pubKeyBuffer1, &pubKeyLen1);
1168 ok(!result && GetLastError() == ERROR_INVALID_PARAMETER,
1169 "Expected ERROR_INVALID_PARAMETER, got %x\n", GetLastError());
1171 result = CryptExportKey(privKey2, 0, PUBLICKEYBLOB, 0, pubKeyBuffer2, &pubKeyLen2);
1172 ok(!result && GetLastError() == ERROR_INVALID_PARAMETER,
1173 "Expected ERROR_INVALID_PARAMETER, got %x\n", GetLastError());
1175 /* Import the public key and convert it into a shared key */
1176 result = CryptImportKey(hProv, pubKeyBuffer2, pubKeyLen2, privKey1, 0, &sessionKey1);
1177 ok((!result && GetLastError() == ERROR_INVALID_PARAMETER) ||
1178 broken(!result && GetLastError() == NTE_BAD_DATA) || /* Vista.64 */
1179 broken(!result && GetLastError() == NTE_BAD_TYPE) || /* Win2K-W2K8, Win7.64 */
1180 broken(!result && GetLastError() == NTE_BAD_ALGID), /* W7SP164 (32 bit dssenh) */
1181 "Expected ERROR_INVALID_PARAMETER, got %x\n", GetLastError());
1183 result = CryptImportKey(hProv, pubKeyBuffer1, pubKeyLen1, privKey2, 0, &sessionKey2);
1184 ok((!result && GetLastError() == ERROR_INVALID_PARAMETER) ||
1185 broken(!result && GetLastError() == NTE_BAD_DATA) || /* Win 7 */
1186 broken(!result && GetLastError() == NTE_BAD_TYPE) || /* Win2K-W2K8, Win7.64 */
1187 broken(!result && GetLastError() == NTE_BAD_ALGID), /* W7SP164 (32 bit dssenh) */
1188 "Expected ERROR_INVALID_PARAMETER, got %x\n", GetLastError());
1190 /* Set the shared key parameters to matching type */
1191 algid = CALG_RC4;
1192 result = CryptSetKeyParam(sessionKey1, KP_ALGID, (BYTE *)&algid, 0);
1193 ok(!result && GetLastError() == ERROR_INVALID_PARAMETER,
1194 "Expected ERROR_INVALID_PARAMETER, got %x\n", GetLastError());
1196 algid = CALG_RC4;
1197 result = CryptSetKeyParam(sessionKey2, KP_ALGID, (BYTE *)&algid, 0);
1198 ok(!result && GetLastError() == ERROR_INVALID_PARAMETER,
1199 "Expected ERROR_INVALID_PARAMETER, got %x\n", GetLastError());
1201 /* Encrypt some data and verify we are getting the same output */
1202 memcpy(pbData1, plainText, plainLen);
1203 dataLen = plainLen;
1205 result = CryptEncrypt(sessionKey1, 0, TRUE, 0, pbData1, &dataLen, 36);
1206 ok(!result && GetLastError() == ERROR_INVALID_PARAMETER,
1207 "Expected ERROR_INVALID_PARAMETER, got %x\n", GetLastError());
1209 result = CryptDecrypt(sessionKey2, 0, TRUE, 0, pbData1, &dataLen);
1210 ok(!result && GetLastError() == ERROR_INVALID_PARAMETER,
1211 "Expected ERROR_INVALID_PARAMETER, got %x\n", GetLastError());
1213 ok(!memcmp(pbData1, (BYTE *)plainText, sizeof(plainText)), "Incorrect decrypted data.\n");
1215 memcpy(pbData2, plainText, plainLen);
1216 dataLen = plainLen;
1218 result = CryptEncrypt(sessionKey2, 0, TRUE, 0, pbData2, &dataLen, 36);
1219 ok(!result && GetLastError() == ERROR_INVALID_PARAMETER,
1220 "Expected ERROR_INVALID_PARAMETER, got %x\n", GetLastError());
1222 result = CryptDecrypt(sessionKey1, 0, TRUE, 0, pbData2, &dataLen);
1223 ok(!result && GetLastError() == ERROR_INVALID_PARAMETER,
1224 "Expected ERROR_INVALID_PARAMETER, got %x\n", GetLastError());
1226 ok(!memcmp(pbData1, pbData2, dataLen), "Decrypted data is not identical.\n");
1228 /* Destroy all user keys */
1229 result = CryptDestroyKey(sessionKey1);
1230 ok(!result && GetLastError() == ERROR_INVALID_PARAMETER,
1231 "Expected ERROR_INVALID_PARAMETER, got %x\n", GetLastError());
1233 result = CryptDestroyKey(sessionKey2);
1234 ok(!result && GetLastError() == ERROR_INVALID_PARAMETER,
1235 "Expected ERROR_INVALID_PARAMETER, got %x\n", GetLastError());
1237 result = CryptDestroyKey(privKey1);
1238 ok(!result && GetLastError() == ERROR_INVALID_PARAMETER,
1239 "Expected ERROR_INVALID_PARAMETER, got %x\n", GetLastError());
1241 result = CryptDestroyKey(privKey2);
1242 ok(!result && GetLastError() == ERROR_INVALID_PARAMETER,
1243 "Expected ERROR_INVALID_PARAMETER, got %x\n", GetLastError());
1247 static void test_keyExchange_dssDH(HCRYPTPROV hProv, const struct keyExchange_test *tests, int testLen)
1249 HCRYPTKEY privKey1 = 0, privKey2 = 0;
1250 HCRYPTKEY sessionKey1 = 0, sessionKey2 = 0;
1251 const char plainText[] = "Testing shared key.";
1252 unsigned char pbData1[36];
1253 unsigned char pbData2[36];
1254 BYTE pubKeyBuffer1[512], pubKeyBuffer2[512];
1255 DWORD pubKeyLen1, pubKeyLen2, dataLen;
1256 DATA_BLOB Prime, Generator;
1257 int plainLen = sizeof(plainText), i;
1258 ALG_ID algid;
1259 BOOL result;
1261 for(i = 0; i < testLen; i++)
1263 SetLastError(0xdeadbeef);
1265 /* Create the data blobs and the prime and generator */
1266 Prime.cbData = tests[i].primeLen;
1267 Prime.pbData = (BYTE *)tests[i].primeNum;
1268 Generator.cbData = tests[i].generatorLen;
1269 Generator.pbData = (BYTE *)tests[i].generatorNum;
1271 /* Generate key exchange keys for user1 and user2 */
1272 result = CryptGenKey(hProv, tests[i].algid, 512 << 16 | CRYPT_PREGEN, &privKey1);
1273 if (!result)
1275 skip("skipping key exchange tests\n");
1276 return;
1278 ok(result, "Failed to generate a key for user1, got %x\n", GetLastError());
1280 result = CryptGenKey(hProv, tests[i].algid, 512 << 16 | CRYPT_PREGEN, &privKey2);
1281 ok(result, "Failed to generate a key for user2, got %x\n", GetLastError());
1283 /* Set the prime and generator values, which are agreed upon */
1284 result = CryptSetKeyParam(privKey1, KP_P, (BYTE *)&Prime, 0);
1285 ok(result, "Failed to set prime for user 1's key, got %x\n", GetLastError());
1287 result = CryptSetKeyParam(privKey2, KP_P, (BYTE *)&Prime, 0);
1288 ok(result, "Failed to set prime for user 2's key, got %x\n", GetLastError());
1290 result = CryptSetKeyParam(privKey1, KP_G, (BYTE *)&Generator, 0);
1291 ok(result, "Failed to set generator for user 1's key, got %x\n", GetLastError());
1293 result = CryptSetKeyParam(privKey2, KP_G, (BYTE *)&Generator, 0);
1294 ok(result, "Failed to set generator for user 2's key, got %x\n", GetLastError());
1296 /* Generate the secret value for user1 and user2 */
1297 result = CryptSetKeyParam(privKey1, KP_X, NULL, 0);
1298 ok(result, "Failed to set secret value for user 1, got %x\n", GetLastError());
1300 result = CryptSetKeyParam(privKey2, KP_X, NULL, 0);
1301 ok(result, "Failed to set secret value for user 2, got %x\n", GetLastError());
1303 /* Acquire required size for the public keys */
1304 result = CryptExportKey(privKey1, 0, PUBLICKEYBLOB, 0, NULL, &pubKeyLen1);
1305 ok(result, "Failed to acquire public key length for user 1, got %x\n", GetLastError());
1307 result = CryptExportKey(privKey2, 0, PUBLICKEYBLOB, 0, NULL, &pubKeyLen2);
1308 ok(result, "Failed to acquire public key length for user 2, got %x\n", GetLastError());
1310 /* Export public key which will be calculated into the shared key */
1311 result = CryptExportKey(privKey1, 0, PUBLICKEYBLOB, 0, pubKeyBuffer1, &pubKeyLen1);
1312 ok(result, "Failed to export public key for user 1, got %x\n", GetLastError());
1314 result = CryptExportKey(privKey2, 0, PUBLICKEYBLOB, 0, pubKeyBuffer2, &pubKeyLen2);
1315 ok(result, "Failed to export public key for user 2, got %x\n", GetLastError());
1317 /* Import the public key and convert it into a shared key */
1318 result = CryptImportKey(hProv, pubKeyBuffer2, pubKeyLen2, privKey1, 0, &sessionKey1);
1319 ok(result, "Failed to import key for user 1, got %x\n", GetLastError());
1321 result = CryptImportKey(hProv, pubKeyBuffer1, pubKeyLen1, privKey2, 0, &sessionKey2);
1322 ok(result, "Failed to import key for user 2, got %x\n", GetLastError());
1324 /* Set the shared key parameters to matching cipher type */
1325 algid = CALG_3DES;
1326 result = CryptSetKeyParam(sessionKey1, KP_ALGID, (BYTE *)&algid, 0);
1327 ok(result, "Failed to set session key for user 1, got %x\n", GetLastError());
1329 algid = CALG_3DES;
1330 result = CryptSetKeyParam(sessionKey2, KP_ALGID, (BYTE *)&algid, 0);
1331 ok(result, "Failed to set session key for user 2, got %x\n", GetLastError());
1333 /* Encrypt some data and verify we are getting the correct output */
1334 memcpy(pbData1, plainText, plainLen);
1335 dataLen = plainLen;
1337 result = CryptEncrypt(sessionKey1, 0, TRUE, 0, pbData1, &dataLen, 36);
1338 ok(result, "Failed to encrypt data, got %x.\n", GetLastError());
1340 result = CryptDecrypt(sessionKey2, 0, TRUE, 0, pbData1, &dataLen);
1341 ok(result, "Failed to decrypt data, got %x.\n", GetLastError());
1343 ok(!memcmp(pbData1, (BYTE *)plainText, sizeof(plainText)), "Incorrect decrypted data.\n");
1345 memcpy(pbData2, plainText, plainLen);
1346 dataLen = plainLen;
1348 result = CryptEncrypt(sessionKey2, 0, TRUE, 0, pbData2, &dataLen, 36);
1349 ok(result, "Failed to encrypt data, got %x.\n", GetLastError());
1351 result = CryptDecrypt(sessionKey1, 0, TRUE, 0, pbData2, &dataLen);
1352 ok(result, "Failed to decrypt data, got %x.\n", GetLastError());
1354 ok(!memcmp(pbData1, pbData2, dataLen), "Decrypted data is not identical.\n");
1356 /* Destroy all user keys */
1357 result = CryptDestroyKey(sessionKey1);
1358 ok(result, "Failed to destroy session key 1, got %x\n", GetLastError());
1360 result = CryptDestroyKey(sessionKey2);
1361 ok(result, "Failed to destroy session key 2, got %x\n", GetLastError());
1363 result = CryptDestroyKey(privKey1);
1364 ok(result, "Failed to destroy key private key 1, got %x\n", GetLastError());
1366 result = CryptDestroyKey(privKey2);
1367 ok(result, "Failed to destroy key private key 2, got %x\n", GetLastError());
1371 static void test_key_exchange(void)
1373 HCRYPTPROV hProv = 0;
1374 BOOL result;
1376 /* acquire base dss provider */
1377 result = CryptAcquireContextA(&hProv, NULL, MS_DEF_DSS_PROV_A, PROV_DSS, CRYPT_VERIFYCONTEXT);
1378 if(!result)
1380 skip("DSSENH is currently not available, skipping shared key tests.\n");
1381 return;
1383 ok(result, "Failed to acquire CSP.\n");
1385 test_keyExchange_baseDSS(hProv, baseDSSkey_data, ARRAY_SIZE(baseDSSkey_data));
1387 result = CryptReleaseContext(hProv, 0);
1388 ok(result, "Failed to release CSP provider.\n");
1390 /* acquire diffie hellman dss provider */
1391 result = CryptAcquireContextA(&hProv, NULL, MS_DEF_DSS_DH_PROV_A, PROV_DSS_DH,
1392 CRYPT_VERIFYCONTEXT);
1393 ok(result, "Failed to acquire CSP.\n");
1395 test_keyExchange_dssDH(hProv, dssDHkey_data, ARRAY_SIZE(dssDHkey_data));
1397 result = CryptReleaseContext(hProv, 0);
1398 ok(result, "Failed to release CSP provider.\n");
1400 /* acquire enhanced dss provider */
1401 result = CryptAcquireContextA(&hProv, NULL, MS_ENH_DSS_DH_PROV_A, PROV_DSS_DH,
1402 CRYPT_VERIFYCONTEXT);
1403 ok(result, "Failed to acquire CSP.\n");
1405 test_keyExchange_dssDH(hProv, dssDHkey_data, ARRAY_SIZE(dssDHkey_data));
1407 result = CryptReleaseContext(hProv, 0);
1408 ok(result, "Failed to release CSP provider.\n");
1410 /* acquire schannel dss provider */
1411 result = CryptAcquireContextA(&hProv, NULL, MS_DEF_DH_SCHANNEL_PROV_A, PROV_DH_SCHANNEL,
1412 CRYPT_VERIFYCONTEXT);
1413 ok(result, "Failed to acquire CSP.\n");
1415 test_keyExchange_dssDH(hProv, dssDHkey_data, ARRAY_SIZE(dssDHkey_data));
1417 result = CryptReleaseContext(hProv, 0);
1418 ok(result, "Failed to release CSP provider.\n");
1421 static void test_duplicate_hash(void)
1423 static const char expected[] =
1424 {0xb9,0x7b,0xed,0xd4,0x7b,0xd8,0xa0,0xcd,0x6c,0xba,0xce,0xe9,0xb1,0x36,0xbb,0x00,0x27,0xe3,0x95,0x21};
1425 HCRYPTPROV hprov;
1426 HCRYPTHASH hhash, hhash2;
1427 BYTE buf[20];
1428 DWORD len;
1429 BOOL result;
1431 result = CryptAcquireContextA(&hprov, NULL, MS_DEF_DSS_PROV_A, PROV_DSS, CRYPT_VERIFYCONTEXT);
1432 ok(result, "got %08x\n", GetLastError());
1434 result = CryptCreateHash(hprov, CALG_SHA, 0, 0, &hhash);
1435 ok(result, "got %08x\n", GetLastError());
1437 result = CryptHashData(hhash, (const BYTE *)"winetest", sizeof("winetest"), 0);
1438 ok(result, "got %08x\n", GetLastError());
1440 len = sizeof(buf);
1441 result = CryptGetHashParam(hhash, HP_HASHVAL, buf, &len, 0);
1442 ok(result, "got %08x\n", GetLastError());
1443 ok(!memcmp(buf, expected, sizeof(expected)), "wrong data\n");
1445 SetLastError(0xdeadbeef);
1446 result = CryptHashData(hhash, (const BYTE *)"winetest", sizeof("winetest"), 0);
1447 ok(!result, "success\n");
1448 ok(GetLastError() == NTE_BAD_HASH_STATE, "got %08x\n", GetLastError());
1450 result = CryptDuplicateHash(hhash, NULL, 0, &hhash2);
1451 ok(result, "got %08x\n", GetLastError());
1453 SetLastError(0xdeadbeef);
1454 result = CryptHashData(hhash2, (const BYTE *)"winetest", sizeof("winetest"), 0);
1455 ok(!result, "success\n");
1456 ok(GetLastError() == NTE_BAD_HASH_STATE, "got %08x\n", GetLastError());
1458 len = sizeof(buf);
1459 result = CryptGetHashParam(hhash2, HP_HASHVAL, buf, &len, 0);
1460 ok(result, "got %08x\n", GetLastError());
1461 ok(!memcmp(buf, expected, sizeof(expected)), "wrong data\n");
1463 result = CryptDestroyHash(hhash2);
1464 ok(result, "got %08x\n", GetLastError());
1466 result = CryptDestroyHash(hhash);
1467 ok(result, "got %08x\n", GetLastError());
1469 result = CryptReleaseContext(hprov, 0);
1470 ok(result, "got %08x\n", GetLastError());
1473 static void test_userkey(void)
1475 HCRYPTPROV hprov;
1476 HCRYPTKEY hkey;
1477 BOOL result;
1479 CryptAcquireContextA(&hprov, "winetest", MS_ENH_DSS_DH_PROV_A, PROV_DSS_DH, CRYPT_DELETEKEYSET);
1480 result = CryptAcquireContextA(&hprov, "winetest", MS_ENH_DSS_DH_PROV_A, PROV_DSS_DH, CRYPT_NEWKEYSET);
1481 ok(result, "got %08x\n", GetLastError());
1483 SetLastError(0xdeadbeef);
1484 result = CryptGetUserKey(hprov, AT_KEYEXCHANGE, &hkey);
1485 ok(!result, "success\n");
1486 ok(GetLastError() == NTE_NO_KEY, "got %08x\n", GetLastError());
1488 SetLastError(0xdeadbeef);
1489 result = CryptGetUserKey(hprov, AT_SIGNATURE, &hkey);
1490 ok(!result, "success\n");
1491 ok(GetLastError() == NTE_NO_KEY, "got %08x\n", GetLastError());
1493 result = CryptGenKey(hprov, AT_SIGNATURE, 1024 << 16, &hkey);
1494 ok(result, "got %08x\n", GetLastError());
1495 result = CryptDestroyKey(hkey);
1496 ok(result, "got %08x\n", GetLastError());
1498 result = CryptGetUserKey(hprov, AT_SIGNATURE, &hkey);
1499 ok(result, "got %08x\n", GetLastError());
1500 result = CryptDestroyKey(hkey);
1501 ok(result, "got %08x\n", GetLastError());
1503 SetLastError(0xdeadbeef);
1504 result = CryptGetUserKey(hprov, AT_KEYEXCHANGE, &hkey);
1505 ok(!result, "success\n");
1506 ok(GetLastError() == NTE_NO_KEY, "got %08x\n", GetLastError());
1508 result = CryptReleaseContext(hprov, 0);
1509 ok(result, "got %08x\n", GetLastError());
1511 hprov = 0xdeadbeef;
1512 result = CryptAcquireContextA(&hprov, "winetest", MS_ENH_DSS_DH_PROV_A, PROV_DSS_DH, CRYPT_DELETEKEYSET);
1513 ok(result, "got %08x\n", GetLastError());
1514 ok(!hprov, "got %08x\n", (DWORD)hprov);
1517 static void test_duplicate_key(void)
1519 HCRYPTPROV hprov;
1520 HCRYPTKEY hkey, hkey2;
1521 DWORD len;
1522 BOOL result;
1523 BYTE buf[512];
1525 result = CryptAcquireContextA(&hprov, NULL, MS_DEF_DSS_PROV_A, PROV_DSS, CRYPT_VERIFYCONTEXT);
1526 ok(result, "got %08x\n", GetLastError());
1528 result = CryptImportKey(hprov, DSS_SIGN_PrivateKey, sizeof(DSS_SIGN_PrivateKey), 0, CRYPT_EXPORTABLE, &hkey);
1529 ok(result, "got %08x\n", GetLastError());
1531 result = CryptDuplicateKey(hkey, NULL, 0, &hkey2);
1532 ok(result, "got %08x\n", GetLastError());
1534 len = sizeof(buf);
1535 result = CryptExportKey(hkey2, 0, PRIVATEKEYBLOB, 0, buf, &len);
1536 ok(result, "got %08x\n", GetLastError());
1537 ok(len == sizeof(DSS_SIGN_PrivateKey), "got %u\n", len);
1538 ok(!memcmp(buf, DSS_SIGN_PrivateKey, sizeof(DSS_SIGN_PrivateKey)), "wrong data\n");
1540 result = CryptDestroyKey(hkey2);
1541 ok(result, "got %08x\n", GetLastError());
1543 result = CryptDestroyKey(hkey);
1544 ok(result, "got %08x\n", GetLastError());
1547 START_TEST(dssenh)
1549 test_acquire_context();
1550 test_keylength();
1551 test_hash(hash_data, ARRAY_SIZE(hash_data));
1552 test_data_encryption(encrypt_data, ARRAY_SIZE(encrypt_data));
1553 test_cipher_modes(ciphermode_data, ARRAY_SIZE(ciphermode_data));
1554 test_verify_signature();
1555 test_key_exchange();
1556 test_duplicate_hash();
1557 test_userkey();
1558 test_duplicate_key();