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 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
;
39 static void test_cryptprotectdata(void)
45 plain
.pbData
=(void*)secret
;
46 plain
.cbData
=strlen(secret
)+1;
48 entropy
.pbData
=(void*)key
;
49 entropy
.cbData
=strlen(key
)+1;
51 SetLastError(0xDEADBEEF);
52 protected = CryptProtectData(NULL
,desc
,NULL
,NULL
,NULL
,0,&cipher
);
53 ok(!protected, "Encrypting without plain data source.\n");
55 ok(r
== ERROR_INVALID_PARAMETER
, "Wrong (%u) GetLastError seen\n",r
);
57 SetLastError(0xDEADBEEF);
58 protected = CryptProtectData(&plain
,desc
,NULL
,NULL
,NULL
,0,NULL
);
59 ok(!protected, "Encrypting without cipher destination.\n");
61 ok(r
== ERROR_INVALID_PARAMETER
, "Wrong (%u) GetLastError seen\n",r
);
67 SetLastError(0xDEADBEEF);
68 protected = CryptProtectData(&plain
,desc
,NULL
,NULL
,NULL
,0,&cipher
);
69 ok(protected, "Encrypting without entropy.\n");
71 ok(r
== ERROR_SUCCESS
, "Wrong (%u) GetLastError seen\n",r
);
73 cipher_entropy
.pbData
=NULL
;
74 cipher_entropy
.cbData
=0;
77 SetLastError(0xDEADBEEF);
78 protected = CryptProtectData(&plain
,desc
,&entropy
,NULL
,NULL
,0,&cipher_entropy
);
79 ok(protected, "Encrypting with entropy.\n");
81 ok(r
== ERROR_SUCCESS
, "Wrong (%u) GetLastError seen\n",r
);
83 cipher_no_desc
.pbData
=NULL
;
84 cipher_no_desc
.cbData
=0;
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");
93 ok(r
== ERROR_SUCCESS
, "Wrong (%u) GetLastError seen\n",r
);
96 static void test_cryptunprotectdata(void)
104 entropy
.pbData
=(void*)key
;
105 entropy
.cbData
=strlen(key
)+1;
107 ok(protected, "CryptProtectData failed to run, so I can't test its output\n");
108 if (!protected) return;
113 SetLastError(0xDEADBEEF);
114 okay
= CryptUnprotectData(&cipher
,NULL
,NULL
,NULL
,NULL
,0,NULL
);
115 ok(!okay
,"Decrypting without destination\n");
117 ok(r
== ERROR_INVALID_PARAMETER
, "Wrong (%u) GetLastError seen\n",r
);
119 SetLastError(0xDEADBEEF);
120 okay
= CryptUnprotectData(NULL
,NULL
,NULL
,NULL
,NULL
,0,&plain
);
121 ok(!okay
,"Decrypting without source\n");
123 ok(r
== ERROR_INVALID_PARAMETER
, "Wrong (%u) GetLastError seen\n",r
);
128 SetLastError(0xDEADBEEF);
129 okay
= CryptUnprotectData(&cipher_entropy
,NULL
,NULL
,NULL
,NULL
,0,&plain
);
130 ok(!okay
,"Decrypting without needed entropy\n");
132 ok(r
== ERROR_INVALID_DATA
, "Wrong (%u) GetLastError seen\n", r
);
138 /* without entropy */
139 SetLastError(0xDEADBEEF);
140 okay
= CryptUnprotectData(&cipher
,&data_desc
,NULL
,NULL
,NULL
,0,&plain
);
141 ok(okay
,"Decrypting without entropy\n");
143 ok(r
== ERROR_SUCCESS
, "Wrong (%u) GetLastError seen\n",r
);
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");
151 LocalFree(plain
.pbData
);
152 LocalFree(data_desc
);
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");
163 ok(r
== ERROR_INVALID_DATA
, "Wrong (%u) GetLastError seen\n",r
);
166 SetLastError(0xDEADBEEF);
167 okay
= CryptUnprotectData(&cipher_entropy
,&data_desc
,&entropy
,NULL
,NULL
,0,&plain
);
168 ok(okay
,"Decrypting with entropy\n");
170 ok(r
== ERROR_SUCCESS
, "Wrong (%u) GetLastError seen\n",r
);
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");
178 LocalFree(plain
.pbData
);
179 LocalFree(data_desc
);
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");
190 ok(r
== ERROR_SUCCESS
, "Wrong (%u) GetLastError seen\n",r
);
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");
198 LocalFree(plain
.pbData
);
204 START_TEST(protectdata
)
208 test_cryptprotectdata();
209 test_cryptunprotectdata();
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
);