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, "Wrong (%u) GetLastError seen\n",r);
76 cipher_entropy.pbData=NULL;
77 cipher_entropy.cbData=0;
80 SetLastError(0xDEADBEEF);
81 protected = pCryptProtectData(&plain,desc,&entropy,NULL,NULL,0,&cipher_entropy);
82 ok(protected, "Encrypting with entropy.\n");
84 ok(r == ERROR_SUCCESS, "Wrong (%u) GetLastError seen\n",r);
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);
94 ok(protected, "Encrypting with entropy and no description.\n");
96 ok(r == ERROR_SUCCESS, "Wrong (%u) GetLastError seen\n",r);
99 static void test_cryptunprotectdata(void)
107 entropy.pbData=(void*)key;
108 entropy.cbData=strlen(key)+1;
110 ok(protected, "CryptProtectData failed to run, so I can't test its output\n");
111 if (!protected) return;
116 SetLastError(0xDEADBEEF);
117 okay = pCryptUnprotectData(&cipher,NULL,NULL,NULL,NULL,0,NULL);
118 ok(!okay,"Decrypting without destination\n");
120 ok(r == ERROR_INVALID_PARAMETER, "Wrong (%u) GetLastError seen\n",r);
122 SetLastError(0xDEADBEEF);
123 okay = pCryptUnprotectData(NULL,NULL,NULL,NULL,NULL,0,&plain);
124 ok(!okay,"Decrypting without source\n");
126 ok(r == ERROR_INVALID_PARAMETER, "Wrong (%u) GetLastError seen\n",r);
131 SetLastError(0xDEADBEEF);
132 okay = pCryptUnprotectData(&cipher_entropy,NULL,NULL,NULL,NULL,0,&plain);
133 ok(!okay,"Decrypting without needed entropy\n");
135 ok(r == ERROR_INVALID_DATA, "Wrong (%u) GetLastError seen\n", r);
141 /* without entropy */
142 SetLastError(0xDEADBEEF);
143 okay = pCryptUnprotectData(&cipher,&data_desc,NULL,NULL,NULL,0,&plain);
144 ok(okay,"Decrypting without entropy\n");
146 ok(r == ERROR_SUCCESS, "Wrong (%u) GetLastError seen\n",r);
148 ok(plain.pbData!=NULL,"Plain DATA_BLOB missing data\n");
149 ok(plain.cbData==strlen(secret)+1,"Plain DATA_BLOB wrong length\n");
150 ok(!strcmp((const char*)plain.pbData,secret),"Plain does not match secret\n");
151 ok(data_desc!=NULL,"Description not allocated\n");
152 ok(!lstrcmpW(data_desc,desc),"Description does not match\n");
154 LocalFree(plain.pbData);
155 LocalFree(data_desc);
161 /* with wrong entropy */
162 SetLastError(0xDEADBEEF);
163 okay = pCryptUnprotectData(&cipher_entropy,&data_desc,&cipher_entropy,NULL,NULL,0,&plain);
164 ok(!okay,"Decrypting with wrong entropy\n");
166 ok(r == ERROR_INVALID_DATA, "Wrong (%u) GetLastError seen\n",r);
169 SetLastError(0xDEADBEEF);
170 okay = pCryptUnprotectData(&cipher_entropy,&data_desc,&entropy,NULL,NULL,0,&plain);
171 ok(okay,"Decrypting with entropy\n");
173 ok(r == ERROR_SUCCESS, "Wrong (%u) GetLastError seen\n",r);
175 ok(plain.pbData!=NULL,"Plain DATA_BLOB missing data\n");
176 ok(plain.cbData==strlen(secret)+1,"Plain DATA_BLOB wrong length\n");
177 ok(!strcmp((const char*)plain.pbData,secret),"Plain does not match secret\n");
178 ok(data_desc!=NULL,"Description not allocated\n");
179 ok(!lstrcmpW(data_desc,desc),"Description does not match\n");
181 LocalFree(plain.pbData);
182 LocalFree(data_desc);
188 /* with entropy but no description */
189 SetLastError(0xDEADBEEF);
190 okay = pCryptUnprotectData(&cipher_no_desc,&data_desc,&entropy,NULL,NULL,0,&plain);
191 ok(okay,"Decrypting with entropy and no description\n");
193 ok(r == ERROR_SUCCESS, "Wrong (%u) GetLastError seen\n",r);
195 ok(plain.pbData!=NULL,"Plain DATA_BLOB missing data\n");
196 ok(plain.cbData==strlen(secret2)+1,"Plain DATA_BLOB wrong length\n");
197 ok(!strcmp((const char*)plain.pbData,secret2),"Plain does not match secret\n");
198 ok(data_desc!=NULL,"Description not allocated\n");
199 ok(data_desc[0]=='\0',"Description not empty\n");
201 LocalFree(data_desc);
202 LocalFree(plain.pbData);
208 START_TEST(protectdata)
210 HMODULE hCrypt32 = GetModuleHandleA("crypt32.dll");
211 hCrypt32 = GetModuleHandleA("crypt32.dll");
212 pCryptProtectData = (void*)GetProcAddress(hCrypt32, "CryptProtectData");
213 pCryptUnprotectData = (void*)GetProcAddress(hCrypt32, "CryptUnprotectData");
214 if (!pCryptProtectData || !pCryptUnprotectData)
216 skip("Crypt(Un)ProtectData() is not available\n");
221 test_cryptprotectdata();
222 test_cryptunprotectdata();
224 /* deinit globals here */
225 if (cipher.pbData) LocalFree(cipher.pbData);
226 if (cipher_entropy.pbData) LocalFree(cipher_entropy.pbData);
227 if (cipher_no_desc.pbData) LocalFree(cipher_no_desc.pbData);