crypt32: Cast-qual warnings fix.
[wine] / dlls / crypt32 / tests / protectdata.c
1 /*
2  * Unit test suite for crypt32.dll's CryptProtectData/CryptUnprotectData
3  *
4  * Copyright 2005 Kees Cook <kees@outflux.net>
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <stdio.h>
22 #include <stdarg.h>
23 #include <windef.h>
24 #include <winbase.h>
25 #include <winerror.h>
26 #include <wincrypt.h>
27
28 #include "wine/test.h"
29
30 static char  secret[]     = "I am a super secret string that no one can see!";
31 static char  secret2[]    = "I am a super secret string indescribable string";
32 static char  key[]        = "Wibble wibble wibble";
33 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};
34 static BOOL protected = FALSE; /* if true, the unprotect tests can run */
35 static DATA_BLOB cipher;
36 static DATA_BLOB cipher_entropy;
37 static DATA_BLOB cipher_no_desc;
38
39 static void test_cryptprotectdata(void)
40 {
41     LONG r;
42     DATA_BLOB plain;
43     DATA_BLOB entropy;
44
45     plain.pbData=(void*)secret;
46     plain.cbData=strlen(secret)+1;
47
48     entropy.pbData=(void*)key;
49     entropy.cbData=strlen(key)+1;
50
51     SetLastError(0xDEADBEEF);
52     protected = CryptProtectData(NULL,desc,NULL,NULL,NULL,0,&cipher);
53     ok(!protected, "Encrypting without plain data source.\n");
54     r = GetLastError();
55     ok(r == ERROR_INVALID_PARAMETER, "Wrong (%u) GetLastError seen\n",r);
56
57     SetLastError(0xDEADBEEF);
58     protected = CryptProtectData(&plain,desc,NULL,NULL,NULL,0,NULL);
59     ok(!protected, "Encrypting without cipher destination.\n");
60     r = GetLastError();
61     ok(r == ERROR_INVALID_PARAMETER, "Wrong (%u) GetLastError seen\n",r);
62
63     cipher.pbData=NULL;
64     cipher.cbData=0;
65
66     /* without entropy */
67     SetLastError(0xDEADBEEF);
68     protected = CryptProtectData(&plain,desc,NULL,NULL,NULL,0,&cipher);
69     ok(protected, "Encrypting without entropy.\n");
70     r = GetLastError();
71     ok(r == ERROR_SUCCESS, "Wrong (%u) GetLastError seen\n",r);
72
73     cipher_entropy.pbData=NULL;
74     cipher_entropy.cbData=0;
75
76     /* with entropy */
77     SetLastError(0xDEADBEEF);
78     protected = CryptProtectData(&plain,desc,&entropy,NULL,NULL,0,&cipher_entropy);
79     ok(protected, "Encrypting with entropy.\n");
80     r = GetLastError();
81     ok(r == ERROR_SUCCESS, "Wrong (%u) GetLastError seen\n",r);
82
83     cipher_no_desc.pbData=NULL;
84     cipher_no_desc.cbData=0;
85
86     /* with entropy but no description */
87     plain.pbData=(void*)secret2;
88     plain.cbData=strlen(secret2)+1;
89     SetLastError(0xDEADBEEF);
90     protected = CryptProtectData(&plain,NULL,&entropy,NULL,NULL,0,&cipher_no_desc);
91     ok(protected, "Encrypting with entropy and no description.\n");
92     r = GetLastError();
93     ok(r == ERROR_SUCCESS, "Wrong (%u) GetLastError seen\n",r);
94 }
95
96 static void test_cryptunprotectdata(void)
97 {
98     LONG r;
99     DATA_BLOB plain;
100     DATA_BLOB entropy;
101     BOOL okay;
102     WCHAR * data_desc;
103
104     entropy.pbData=(void*)key;
105     entropy.cbData=strlen(key)+1;
106
107     ok(protected, "CryptProtectData failed to run, so I can't test its output\n");
108     if (!protected) return;
109
110     plain.pbData=NULL;
111     plain.cbData=0;
112
113     SetLastError(0xDEADBEEF);
114     okay = CryptUnprotectData(&cipher,NULL,NULL,NULL,NULL,0,NULL);
115     ok(!okay,"Decrypting without destination\n");
116     r = GetLastError();
117     ok(r == ERROR_INVALID_PARAMETER, "Wrong (%u) GetLastError seen\n",r);
118
119     SetLastError(0xDEADBEEF);
120     okay = CryptUnprotectData(NULL,NULL,NULL,NULL,NULL,0,&plain);
121     ok(!okay,"Decrypting without source\n");
122     r = GetLastError();
123     ok(r == ERROR_INVALID_PARAMETER, "Wrong (%u) GetLastError seen\n",r);
124
125     plain.pbData=NULL;
126     plain.cbData=0;
127
128     SetLastError(0xDEADBEEF);
129     okay = CryptUnprotectData(&cipher_entropy,NULL,NULL,NULL,NULL,0,&plain);
130     ok(!okay,"Decrypting without needed entropy\n");
131     r = GetLastError();
132     ok(r == ERROR_INVALID_DATA, "Wrong (%u) GetLastError seen\n", r);
133
134     plain.pbData=NULL;
135     plain.cbData=0;
136     data_desc=NULL;
137
138     /* without entropy */
139     SetLastError(0xDEADBEEF);
140     okay = CryptUnprotectData(&cipher,&data_desc,NULL,NULL,NULL,0,&plain);
141     ok(okay,"Decrypting without entropy\n");
142     r = GetLastError();
143     ok(r == ERROR_SUCCESS, "Wrong (%u) GetLastError seen\n",r);
144
145     ok(plain.pbData!=NULL,"Plain DATA_BLOB missing data\n");
146     ok(plain.cbData==strlen(secret)+1,"Plain DATA_BLOB wrong length\n");
147     ok(!strcmp((const char*)plain.pbData,secret),"Plain does not match secret\n");
148     ok(data_desc!=NULL,"Description not allocated\n");
149     ok(!lstrcmpW(data_desc,desc),"Description does not match\n");
150
151     LocalFree(plain.pbData);
152     LocalFree(data_desc);
153
154     plain.pbData=NULL;
155     plain.cbData=0;
156     data_desc=NULL;
157
158     /* with wrong entropy */
159     SetLastError(0xDEADBEEF);
160     okay = CryptUnprotectData(&cipher_entropy,&data_desc,&cipher_entropy,NULL,NULL,0,&plain);
161     ok(!okay,"Decrypting with wrong entropy\n");
162     r = GetLastError();
163     ok(r == ERROR_INVALID_DATA, "Wrong (%u) GetLastError seen\n",r);
164
165     /* with entropy */
166     SetLastError(0xDEADBEEF);
167     okay = CryptUnprotectData(&cipher_entropy,&data_desc,&entropy,NULL,NULL,0,&plain);
168     ok(okay,"Decrypting with entropy\n");
169     r = GetLastError();
170     ok(r == ERROR_SUCCESS, "Wrong (%u) GetLastError seen\n",r);
171
172     ok(plain.pbData!=NULL,"Plain DATA_BLOB missing data\n");
173     ok(plain.cbData==strlen(secret)+1,"Plain DATA_BLOB wrong length\n");
174     ok(!strcmp((const char*)plain.pbData,secret),"Plain does not match secret\n");
175     ok(data_desc!=NULL,"Description not allocated\n");
176     ok(!lstrcmpW(data_desc,desc),"Description does not match\n");
177
178     LocalFree(plain.pbData);
179     LocalFree(data_desc);
180
181     plain.pbData=NULL;
182     plain.cbData=0;
183     data_desc=NULL;
184
185     /* with entropy but no description */
186     SetLastError(0xDEADBEEF);
187     okay = CryptUnprotectData(&cipher_no_desc,&data_desc,&entropy,NULL,NULL,0,&plain);
188     ok(okay,"Decrypting with entropy and no description\n");
189     r = GetLastError();
190     ok(r == ERROR_SUCCESS, "Wrong (%u) GetLastError seen\n",r);
191
192     ok(plain.pbData!=NULL,"Plain DATA_BLOB missing data\n");
193     ok(plain.cbData==strlen(secret2)+1,"Plain DATA_BLOB wrong length\n");
194     ok(!strcmp((const char*)plain.pbData,secret2),"Plain does not match secret\n");
195     ok(data_desc!=NULL,"Description not allocated\n");
196     ok(data_desc[0]=='\0',"Description not empty\n");
197
198     LocalFree(plain.pbData);
199
200     plain.pbData=NULL;
201     plain.cbData=0;
202 }
203
204 START_TEST(protectdata)
205 {
206     protected=FALSE;
207
208     test_cryptprotectdata();
209     test_cryptunprotectdata();
210
211     /* deinit globals here */
212     if (cipher.pbData) LocalFree(cipher.pbData);
213     if (cipher_entropy.pbData) LocalFree(cipher_entropy.pbData);
214     if (cipher_no_desc.pbData) LocalFree(cipher_no_desc.pbData);
215 }