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 ok(r == ERROR_SUCCESS ||
87 r == ERROR_IO_PENDING, /* win2k */
88 "Expected ERROR_SUCCESS or ERROR_IO_PENDING, got %d\n",r);
90 cipher_no_desc.pbData=NULL;
91 cipher_no_desc.cbData=0;
93 /* with entropy but no description */
94 plain.pbData=(void*)secret2;
95 plain.cbData=strlen(secret2)+1;
96 SetLastError(0xDEADBEEF);
97 protected = pCryptProtectData(&plain,NULL,&entropy,NULL,NULL,0,&cipher_no_desc);
101 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
106 ok(r == ERROR_INVALID_PARAMETER,
107 "Expected ERROR_INVALID_PARAMETER, got %d\n", r);
111 static void test_cryptunprotectdata(void)
119 entropy.pbData=(void*)key;
120 entropy.cbData=strlen(key)+1;
125 skip("CryptProtectData failed to run\n");
132 SetLastError(0xDEADBEEF);
133 okay = pCryptUnprotectData(&cipher,NULL,NULL,NULL,NULL,0,NULL);
134 ok(!okay,"Decrypting without destination\n");
136 ok(r == ERROR_INVALID_PARAMETER, "Wrong (%u) GetLastError seen\n",r);
138 SetLastError(0xDEADBEEF);
139 okay = pCryptUnprotectData(NULL,NULL,NULL,NULL,NULL,0,&plain);
140 ok(!okay,"Decrypting without source\n");
142 ok(r == ERROR_INVALID_PARAMETER, "Wrong (%u) GetLastError seen\n",r);
147 SetLastError(0xDEADBEEF);
148 okay = pCryptUnprotectData(&cipher_entropy,NULL,NULL,NULL,NULL,0,&plain);
149 ok(!okay,"Decrypting without needed entropy\n");
151 ok(r == ERROR_INVALID_DATA, "Wrong (%u) GetLastError seen\n", r);
157 /* without entropy */
158 SetLastError(0xDEADBEEF);
159 okay = pCryptUnprotectData(&cipher,&data_desc,NULL,NULL,NULL,0,&plain);
160 ok(okay,"Decrypting without entropy\n");
162 ok(r == ERROR_SUCCESS, "Wrong (%u) GetLastError seen\n",r);
164 ok(plain.pbData!=NULL,"Plain DATA_BLOB missing data\n");
165 ok(plain.cbData==strlen(secret)+1,"Plain DATA_BLOB wrong length\n");
166 ok(!strcmp((const char*)plain.pbData,secret),"Plain does not match secret\n");
167 ok(data_desc!=NULL,"Description not allocated\n");
168 ok(!lstrcmpW(data_desc,desc),"Description does not match\n");
170 LocalFree(plain.pbData);
171 LocalFree(data_desc);
177 /* with wrong entropy */
178 SetLastError(0xDEADBEEF);
179 okay = pCryptUnprotectData(&cipher_entropy,&data_desc,&cipher_entropy,NULL,NULL,0,&plain);
180 ok(!okay,"Decrypting with wrong entropy\n");
182 ok(r == ERROR_INVALID_DATA, "Wrong (%u) GetLastError seen\n",r);
185 SetLastError(0xDEADBEEF);
186 okay = pCryptUnprotectData(&cipher_entropy,&data_desc,&entropy,NULL,NULL,0,&plain);
187 ok(okay,"Decrypting with entropy\n");
189 ok(r == ERROR_SUCCESS, "Wrong (%u) GetLastError seen\n",r);
191 ok(plain.pbData!=NULL,"Plain DATA_BLOB missing data\n");
192 ok(plain.cbData==strlen(secret)+1,"Plain DATA_BLOB wrong length\n");
193 ok(!strcmp((const char*)plain.pbData,secret),"Plain does not match secret\n");
194 ok(data_desc!=NULL,"Description not allocated\n");
195 ok(!lstrcmpW(data_desc,desc),"Description does not match\n");
197 LocalFree(plain.pbData);
198 LocalFree(data_desc);
204 /* with entropy but no description */
205 SetLastError(0xDEADBEEF);
206 okay = pCryptUnprotectData(&cipher_no_desc,&data_desc,&entropy,NULL,NULL,0,&plain);
207 ok(okay,"Decrypting with entropy and no description\n");
209 ok(r == ERROR_SUCCESS, "Wrong (%u) GetLastError seen\n",r);
211 ok(plain.pbData!=NULL,"Plain DATA_BLOB missing data\n");
212 ok(plain.cbData==strlen(secret2)+1,"Plain DATA_BLOB wrong length\n");
213 ok(!strcmp((const char*)plain.pbData,secret2),"Plain does not match secret\n");
214 ok(data_desc!=NULL,"Description not allocated\n");
215 ok(data_desc[0]=='\0',"Description not empty\n");
217 LocalFree(data_desc);
218 LocalFree(plain.pbData);
224 START_TEST(protectdata)
226 HMODULE hCrypt32 = GetModuleHandleA("crypt32.dll");
227 hCrypt32 = GetModuleHandleA("crypt32.dll");
228 pCryptProtectData = (void*)GetProcAddress(hCrypt32, "CryptProtectData");
229 pCryptUnprotectData = (void*)GetProcAddress(hCrypt32, "CryptUnprotectData");
230 if (!pCryptProtectData || !pCryptUnprotectData)
232 skip("Crypt(Un)ProtectData() is not available\n");
237 test_cryptprotectdata();
238 test_cryptunprotectdata();
240 /* deinit globals here */
241 if (cipher.pbData) LocalFree(cipher.pbData);
242 if (cipher_entropy.pbData) LocalFree(cipher_entropy.pbData);
243 if (cipher_no_desc.pbData) LocalFree(cipher_no_desc.pbData);