2 * Unit test suite for crypt32.dll's CryptProtectData/CryptUnprotectData
4 * Copyright 2005 Kees Cook <kees@outflux.net>
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
28 #include "wine/test.h"
30 static BOOL (WINAPI
*pCryptProtectData
)(DATA_BLOB
*,LPCWSTR
,DATA_BLOB
*,PVOID
,CRYPTPROTECT_PROMPTSTRUCT
*,DWORD
,DATA_BLOB
*);
31 static BOOL (WINAPI
*pCryptUnprotectData
)(DATA_BLOB
*,LPWSTR
*,DATA_BLOB
*,PVOID
,CRYPTPROTECT_PROMPTSTRUCT
*,DWORD
,DATA_BLOB
*);
33 static char secret
[] = "I am a super secret string that no one can see!";
34 static char secret2
[] = "I am a super secret string indescribable string";
35 static char key
[] = "Wibble wibble wibble";
36 static const WCHAR desc
[] = {'U','l','t','r','a',' ','s','e','c','r','e','t',' ','t','e','s','t',' ','m','e','s','s','a','g','e',0};
37 static BOOL
protected = FALSE
; /* if true, the unprotect tests can run */
38 static DATA_BLOB cipher
;
39 static DATA_BLOB cipher_entropy
;
40 static DATA_BLOB cipher_no_desc
;
42 static void test_cryptprotectdata(void)
48 plain
.pbData
=(void*)secret
;
49 plain
.cbData
=strlen(secret
)+1;
51 entropy
.pbData
=(void*)key
;
52 entropy
.cbData
=strlen(key
)+1;
54 SetLastError(0xDEADBEEF);
55 protected = pCryptProtectData(NULL
,desc
,NULL
,NULL
,NULL
,0,&cipher
);
56 ok(!protected, "Encrypting without plain data source.\n");
58 ok(r
== ERROR_INVALID_PARAMETER
, "Wrong (%u) GetLastError seen\n",r
);
60 SetLastError(0xDEADBEEF);
61 protected = pCryptProtectData(&plain
,desc
,NULL
,NULL
,NULL
,0,NULL
);
62 ok(!protected, "Encrypting without cipher destination.\n");
64 ok(r
== ERROR_INVALID_PARAMETER
, "Wrong (%u) GetLastError seen\n",r
);
70 SetLastError(0xDEADBEEF);
71 protected = pCryptProtectData(&plain
,desc
,NULL
,NULL
,NULL
,0,&cipher
);
72 ok(protected, "Encrypting without entropy.\n");
74 ok(r
== ERROR_SUCCESS
||
75 r
== ERROR_IO_PENDING
, /* win2k */
76 "Expected ERROR_SUCCESS or ERROR_IO_PENDING, got %d\n",r
);
78 cipher_entropy
.pbData
=NULL
;
79 cipher_entropy
.cbData
=0;
82 SetLastError(0xDEADBEEF);
83 protected = pCryptProtectData(&plain
,desc
,&entropy
,NULL
,NULL
,0,&cipher_entropy
);
84 ok(protected, "Encrypting with entropy.\n");
86 cipher_no_desc
.pbData
=NULL
;
87 cipher_no_desc
.cbData
=0;
89 /* with entropy but no description */
90 plain
.pbData
=(void*)secret2
;
91 plain
.cbData
=strlen(secret2
)+1;
92 SetLastError(0xDEADBEEF);
93 protected = pCryptProtectData(&plain
,NULL
,&entropy
,NULL
,NULL
,0,&cipher_no_desc
);
97 ok(GetLastError() == ERROR_INVALID_PARAMETER
,
98 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
102 static void test_cryptunprotectdata(void)
110 entropy
.pbData
=(void*)key
;
111 entropy
.cbData
=strlen(key
)+1;
116 skip("CryptProtectData failed to run\n");
123 SetLastError(0xDEADBEEF);
124 okay
= pCryptUnprotectData(&cipher
,NULL
,NULL
,NULL
,NULL
,0,NULL
);
125 ok(!okay
,"Decrypting without destination\n");
127 ok(r
== ERROR_INVALID_PARAMETER
, "Wrong (%u) GetLastError seen\n",r
);
129 SetLastError(0xDEADBEEF);
130 okay
= pCryptUnprotectData(NULL
,NULL
,NULL
,NULL
,NULL
,0,&plain
);
131 ok(!okay
,"Decrypting without source\n");
133 ok(r
== ERROR_INVALID_PARAMETER
, "Wrong (%u) GetLastError seen\n",r
);
138 SetLastError(0xDEADBEEF);
139 okay
= pCryptUnprotectData(&cipher_entropy
,NULL
,NULL
,NULL
,NULL
,0,&plain
);
140 ok(!okay
,"Decrypting without needed entropy\n");
142 ok(r
== ERROR_INVALID_DATA
, "Wrong (%u) GetLastError seen\n", r
);
148 /* without entropy */
149 SetLastError(0xDEADBEEF);
150 okay
= pCryptUnprotectData(&cipher
,&data_desc
,NULL
,NULL
,NULL
,0,&plain
);
151 ok(okay
,"Decrypting without entropy\n");
153 ok(plain
.pbData
!=NULL
,"Plain DATA_BLOB missing data\n");
154 ok(plain
.cbData
==strlen(secret
)+1,"Plain DATA_BLOB wrong length\n");
155 ok(!strcmp((const char*)plain
.pbData
,secret
),"Plain does not match secret\n");
156 ok(data_desc
!=NULL
,"Description not allocated\n");
157 ok(!lstrcmpW(data_desc
,desc
),"Description does not match\n");
159 LocalFree(plain
.pbData
);
160 LocalFree(data_desc
);
166 /* with wrong entropy */
167 SetLastError(0xDEADBEEF);
168 okay
= pCryptUnprotectData(&cipher_entropy
,&data_desc
,&cipher_entropy
,NULL
,NULL
,0,&plain
);
169 ok(!okay
,"Decrypting with wrong entropy\n");
171 ok(r
== ERROR_INVALID_DATA
, "Wrong (%u) GetLastError seen\n",r
);
174 SetLastError(0xDEADBEEF);
175 okay
= pCryptUnprotectData(&cipher_entropy
,&data_desc
,&entropy
,NULL
,NULL
,0,&plain
);
176 ok(okay
,"Decrypting with entropy\n");
178 ok(plain
.pbData
!=NULL
,"Plain DATA_BLOB missing data\n");
179 ok(plain
.cbData
==strlen(secret
)+1,"Plain DATA_BLOB wrong length\n");
180 ok(!strcmp((const char*)plain
.pbData
,secret
),"Plain does not match secret\n");
181 ok(data_desc
!=NULL
,"Description not allocated\n");
182 ok(!lstrcmpW(data_desc
,desc
),"Description does not match\n");
184 LocalFree(plain
.pbData
);
185 LocalFree(data_desc
);
191 /* with entropy but no description */
192 SetLastError(0xDEADBEEF);
193 okay
= pCryptUnprotectData(&cipher_no_desc
,&data_desc
,&entropy
,NULL
,NULL
,0,&plain
);
194 ok(okay
,"Decrypting with entropy and no description\n");
196 ok(plain
.pbData
!=NULL
,"Plain DATA_BLOB missing data\n");
197 ok(plain
.cbData
==strlen(secret2
)+1,"Plain DATA_BLOB wrong length\n");
198 ok(!strcmp((const char*)plain
.pbData
,secret2
),"Plain does not match secret\n");
199 ok(data_desc
!=NULL
,"Description not allocated\n");
200 ok(data_desc
[0]=='\0',"Description not empty\n");
202 LocalFree(data_desc
);
203 LocalFree(plain
.pbData
);
209 START_TEST(protectdata
)
211 HMODULE hCrypt32
= GetModuleHandleA("crypt32.dll");
212 hCrypt32
= GetModuleHandleA("crypt32.dll");
213 pCryptProtectData
= (void*)GetProcAddress(hCrypt32
, "CryptProtectData");
214 pCryptUnprotectData
= (void*)GetProcAddress(hCrypt32
, "CryptUnprotectData");
215 if (!pCryptProtectData
|| !pCryptUnprotectData
)
217 skip("Crypt(Un)ProtectData() is not available\n");
222 test_cryptprotectdata();
223 test_cryptunprotectdata();
225 /* deinit globals here */
226 if (cipher
.pbData
) LocalFree(cipher
.pbData
);
227 if (cipher_entropy
.pbData
) LocalFree(cipher_entropy
.pbData
);
228 if (cipher_no_desc
.pbData
) LocalFree(cipher_no_desc
.pbData
);