Release 0.9.61.
[wine/gsoc-2012-control.git] / dlls / crypt32 / tests / protectdata.c
blobd0c662e93410a7133d4faee4f8d58b064c5404b1
1 /*
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
21 #include <stdio.h>
22 #include <stdarg.h>
23 #include <windef.h>
24 #include <winbase.h>
25 #include <winerror.h>
26 #include <wincrypt.h>
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)
44 LONG r;
45 DATA_BLOB plain;
46 DATA_BLOB entropy;
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");
57 r = GetLastError();
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");
63 r = GetLastError();
64 ok(r == ERROR_INVALID_PARAMETER, "Wrong (%u) GetLastError seen\n",r);
66 cipher.pbData=NULL;
67 cipher.cbData=0;
69 /* without entropy */
70 SetLastError(0xDEADBEEF);
71 protected = pCryptProtectData(&plain,desc,NULL,NULL,NULL,0,&cipher);
72 ok(protected, "Encrypting without entropy.\n");
73 r = GetLastError();
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;
81 /* with entropy */
82 SetLastError(0xDEADBEEF);
83 protected = pCryptProtectData(&plain,desc,&entropy,NULL,NULL,0,&cipher_entropy);
84 ok(protected, "Encrypting with entropy.\n");
85 r = GetLastError();
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);
98 r = GetLastError();
99 if (protected)
101 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
103 else
105 /* fails in win2k */
106 ok(r == ERROR_INVALID_PARAMETER,
107 "Expected ERROR_INVALID_PARAMETER, got %d\n", r);
111 static void test_cryptunprotectdata(void)
113 LONG r;
114 DATA_BLOB plain;
115 DATA_BLOB entropy;
116 BOOL okay;
117 WCHAR * data_desc;
119 entropy.pbData=(void*)key;
120 entropy.cbData=strlen(key)+1;
122 /* fails in win2k */
123 if (!protected)
125 skip("CryptProtectData failed to run\\n");
126 return;
129 plain.pbData=NULL;
130 plain.cbData=0;
132 SetLastError(0xDEADBEEF);
133 okay = pCryptUnprotectData(&cipher,NULL,NULL,NULL,NULL,0,NULL);
134 ok(!okay,"Decrypting without destination\n");
135 r = GetLastError();
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");
141 r = GetLastError();
142 ok(r == ERROR_INVALID_PARAMETER, "Wrong (%u) GetLastError seen\n",r);
144 plain.pbData=NULL;
145 plain.cbData=0;
147 SetLastError(0xDEADBEEF);
148 okay = pCryptUnprotectData(&cipher_entropy,NULL,NULL,NULL,NULL,0,&plain);
149 ok(!okay,"Decrypting without needed entropy\n");
150 r = GetLastError();
151 ok(r == ERROR_INVALID_DATA, "Wrong (%u) GetLastError seen\n", r);
153 plain.pbData=NULL;
154 plain.cbData=0;
155 data_desc=NULL;
157 /* without entropy */
158 SetLastError(0xDEADBEEF);
159 okay = pCryptUnprotectData(&cipher,&data_desc,NULL,NULL,NULL,0,&plain);
160 ok(okay,"Decrypting without entropy\n");
161 r = GetLastError();
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);
173 plain.pbData=NULL;
174 plain.cbData=0;
175 data_desc=NULL;
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");
181 r = GetLastError();
182 ok(r == ERROR_INVALID_DATA, "Wrong (%u) GetLastError seen\n",r);
184 /* with entropy */
185 SetLastError(0xDEADBEEF);
186 okay = pCryptUnprotectData(&cipher_entropy,&data_desc,&entropy,NULL,NULL,0,&plain);
187 ok(okay,"Decrypting with entropy\n");
188 r = GetLastError();
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);
200 plain.pbData=NULL;
201 plain.cbData=0;
202 data_desc=NULL;
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");
208 r = GetLastError();
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);
220 plain.pbData=NULL;
221 plain.cbData=0;
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");
233 return;
236 protected=FALSE;
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);