EaseFilter Demo Project
AESEncryption.cpp
Go to the documentation of this file.
1 //
3 // (C) Copyright 2011 EaseFilter Technologies Inc.
4 // All Rights Reserved
5 //
6 // This software is part of a licensed software product and may
7 // only be used or copied in accordance with the terms of that license.
8 //
10 
11 #include "stdafx.h"
12 #include "tools.h"
13 #include "FilterAPI.h"
14 
15 WCHAR* testFolder = L"c:\\filterTest";
16 WCHAR* testFile = L"c:\\filterTest\\testEncryptfile1.bin";
17 WCHAR* copyFile = L"c:\\filterTest\\testEncryptfile1.copy.bin";
18 
19 //Add clear text data to the file.
20 unsigned char iv[] = {0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff};// Initialization vector
21 unsigned char clearText[] = {0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a};// clear text
22 
23 //256 bit test key
24 unsigned char key[] = {0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4};// 32bytes encrytpion key
25 unsigned char cipherText[] = {0x60,0x1e,0xc3,0x13,0x77,0x57,0x89,0xa5,0xb7,0xa7,0xf5,0x04,0xbb,0xf3,0xd2,0x28};// cipher text
26 
27 
28 BOOL
29 CreateTestFile(BOOL bypassFilterDriver )
30 {
31  LARGE_INTEGER ByteOffset = {0};
32  OVERLAPPED Overlapped = {0};
33  DWORD dwTransferred = 0;
34  int nError = ERROR_SUCCESS;
35  BOOL ret = FALSE;
36  DWORD dwFlagsAndAttributes = FILE_FLAG_NO_BUFFERING;
37 
38  //Create the test folder.
39  ret = CreateDirectory(testFolder,NULL);
40 
41  if( ret == 0 )
42  {
43  nError = GetLastError();
44  if( nError != ERROR_ALREADY_EXISTS )
45  {
46  PrintErrorMessage(L"Create test folder failed.", nError);
47  return FALSE;
48  }
49  }
50 
51  if( bypassFilterDriver )
52  {
53  dwFlagsAndAttributes |= FILE_ATTRIBUTE_REPARSE_POINT|
54  FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_NO_BUFFERING|FILE_FLAG_OPEN_NO_RECALL;
55  }
56 
57  //Create the test file,if it exist, overwrite it.
58  HANDLE pFile = CreateFile(testFile,GENERIC_WRITE,NULL,NULL,CREATE_ALWAYS,dwFlagsAndAttributes,NULL);
59 
60  if( pFile == INVALID_HANDLE_VALUE )
61  {
62  PrintErrorMessage(L"Create test file failed.", GetLastError());
63  return FALSE;
64  }
65 
66  //since we open the file without buffering,we need to write the file with sector align length
67  DWORD bufferLength = 65536;
68  unsigned char* buffer = (unsigned char*)_aligned_malloc(bufferLength,bufferLength);
69  if( NULL == buffer )
70  {
71  ret = FALSE;
72  PrintErrorMessage(L"Allocate memory failed with insufficient resources.",0);
73  goto EXIT;
74  }
75 
76  ZeroMemory(buffer,65536);
77 
78  RtlCopyMemory(buffer,clearText,sizeof(clearText));
79 
80  // Write test data to the test file.
81  if(!WriteFile(pFile, buffer,bufferLength, &dwTransferred, NULL))
82  {
83  nError = GetLastError();
84  PrintErrorMessage(L"WriteFile failed.", nError);
85  ret = FALSE;
86  goto EXIT;
87  }
88 
89  SetFileSize(pFile,sizeof(clearText));
90 
91  ret = TRUE;
92 
93 EXIT:
94 
95  CloseHandle(pFile);
96 
97  if( buffer != NULL )
98  {
99  _aligned_free( buffer);
100  }
101 
102  return ret;
103 
104 }
105 
106 BOOL
107 VerifyRawData(BOOL isEncrypted)
108 {
109  HANDLE pFile = INVALID_HANDLE_VALUE;
110 
111  ULONG ivLength = 16;
112  BYTE ivTag[16];
113  BOOL ret = FALSE;
114 
115  if(isEncrypted )
116  {
117  ret = GetIVTag(testFile,&ivLength,ivTag);
118  if(!ret)
119  {
120  PrintLastErrorMessage( L"121 VerifyRawData GetIVTag failed.");
121  return FALSE;
122  }
123  }
124 
125  //open test file bypass the filter driver.
126  pFile = CreateFile( testFile,
127  GENERIC_READ,NULL,NULL,
128  OPEN_EXISTING,
129  FILE_ATTRIBUTE_ENCRYPTED|FILE_ATTRIBUTE_REPARSE_POINT|
130  FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_NO_BUFFERING|FILE_FLAG_OPEN_NO_RECALL,
131  NULL);
132 
133  if( pFile == INVALID_HANDLE_VALUE )
134  {
135  PrintErrorMessage(L"Open test file failed.", GetLastError());
136  return FALSE;
137  }
138 
139  //since we open the file without buffering,we need to read the file with sector align length
140  DWORD bufferLength = 65536;
141  unsigned char* buffer = (unsigned char*)_aligned_malloc(bufferLength,bufferLength);
142  if( NULL == buffer )
143  {
144  ret = FALSE;
145  PrintErrorMessage(L"Allocate memory failed with insufficient resources.",0);
146  goto EXIT;
147  }
148 
149  ret = ReadFile(pFile,buffer,bufferLength,&bufferLength,NULL);
150  if(0 == ret)
151  {
152  PrintErrorMessage(L"Read test file failed.",GetLastError());
153  goto EXIT;
154  }
155 
156  if( isEncrypted && ivLength == 0)
157  {
158  ret = FALSE;
159  PrintErrorMessage(L"VerifyRawData failed, the encrypted file doesn't have iv tag.",0);
160  goto EXIT;
161  }
162 
163  if( isEncrypted )
164  {
165  if( memcmp(buffer,cipherText,bufferLength) == 0)
166  {
167  ret = TRUE;
168  }
169  else
170  {
171  printf("Compare encrypted data failed.\r\nCipher data:");
172  for(int i = 0; i < sizeof(cipherText); i++)
173  {
174  printf("%2x",cipherText[i]);
175  }
176 
177  printf("\r\nRaw data:");
178  for(int i = 0; i < sizeof(cipherText); i++)
179  {
180  printf("%2x",buffer[i]);
181  }
182 
183  printf("\r\n");
184 
185  ret = FALSE;
186  }
187  }
188  else
189  {
190  if( memcmp(buffer,clearText,bufferLength) == 0)
191  {
192  ret = TRUE;
193  }
194  else
195  {
196  printf("Compare decrypted data failed.\r\nClear data:");
197  for(int i = 0; i < sizeof(clearText); i++)
198  {
199  printf("%2x",clearText[i]);
200  }
201 
202  printf("\r\nRaw data:");
203  for(int i = 0; i < sizeof(clearText); i++)
204  {
205  printf("%2x",buffer[i]);
206  }
207 
208  printf("\r\n");
209 
210  ret = FALSE;
211  }
212  }
213 
214 EXIT:
215 
216  if( pFile != INVALID_HANDLE_VALUE )
217  {
218  CloseHandle(pFile);
219  }
220 
221  if( buffer != NULL )
222  {
223  _aligned_free( buffer);
224  }
225 
226 
227  return ret;
228 
229 }
230 
231 BOOL
233 {
234  HANDLE pFile = INVALID_HANDLE_VALUE;
235  HANDLE pFile2 = INVALID_HANDLE_VALUE;
236  BOOL ret = FALSE;
237  ULONG ivLength = 16;
238  BYTE ivTag[16];
239  unsigned char* buffer = NULL;
240 
241  ret = GetIVTag(testFile,&ivLength,ivTag);
242  if(!ret)
243  {
244  PrintLastErrorMessage(L"GetIVTag failed.");
245  return FALSE;
246  }
247 
248  if( ivLength == 0 )
249  {
250  PrintErrorMessage(L"The file is not encrypted.",0);
251  return FALSE;
252  }
253 
254  //open test file bypass the filter driver.
255  pFile = CreateFile( testFile,
256  GENERIC_READ,NULL,NULL,
257  OPEN_EXISTING,
258  FILE_ATTRIBUTE_ENCRYPTED|FILE_ATTRIBUTE_REPARSE_POINT|
259  FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_NO_BUFFERING|FILE_FLAG_OPEN_NO_RECALL,
260  NULL);
261 
262  if( pFile == INVALID_HANDLE_VALUE )
263  {
264  PrintErrorMessage(L"Open test file failed.", GetLastError());
265  return FALSE;
266  }
267 
268  LARGE_INTEGER fileSize;
269  if( !GetFileSizeEx(pFile,&fileSize))
270  {
271  PrintErrorMessage(L"Get test file size failed.", GetLastError());
272  ret = FALSE;
273  goto EXIT;
274  }
275 
276  //Create test file bypass the filter driver.
277  pFile2 = CreateFile( copyFile,
278  GENERIC_WRITE,NULL,NULL,
279  CREATE_ALWAYS,
280  FILE_ATTRIBUTE_ENCRYPTED|FILE_ATTRIBUTE_REPARSE_POINT|
281  FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_NO_BUFFERING|FILE_FLAG_OPEN_NO_RECALL,
282  NULL);
283 
284  if( pFile2 == INVALID_HANDLE_VALUE )
285  {
286  PrintErrorMessage(L"Create test copy file failed.", GetLastError());
287  return FALSE;
288  }
289 
290  DWORD dwTransferred = 0;
291  DWORD bufferLength = 65536;
292  buffer = (unsigned char*)_aligned_malloc(bufferLength,bufferLength);
293  if( NULL == buffer )
294  {
295  ret = FALSE;
296  PrintErrorMessage(L"Allocate memory failed with insufficient resources.",0);
297  goto EXIT;
298  }
299 
300  ZeroMemory(buffer,65536);
301 
302  ret = ReadFile(pFile,buffer,bufferLength,&dwTransferred,NULL);
303  if(0 == ret)
304  {
305  PrintErrorMessage(L"Read test file failed.",GetLastError());
306  goto EXIT;
307  }
308 
309  // Write test data to the test file.
310  if(!WriteFile(pFile2, buffer, bufferLength, &dwTransferred, NULL))
311  {
312  PrintErrorMessage(L"WriteFile failed.", GetLastError());
313  ret = FALSE;
314  goto EXIT;
315  }
316 
317  //set back the correct file size
318  ret = SetFileSize(pFile2,fileSize.QuadPart);
319  if(!ret)
320  {
321  PrintLastErrorMessage( L"SetFileSize failed.");
322  }
323 
324  if(pFile2 != INVALID_HANDLE_VALUE)
325  {
326  CloseHandle(pFile2);
327  }
328 
329  //add the iv tag to the encrypted file, or the filter driver can't recognize the encrypted file.
330  ret = AddIVTag(copyFile,ivLength,ivTag);
331  if(!ret)
332  {
333  PrintLastErrorMessage( L"AddIVTag failed.");
334  }
335 
336 EXIT:
337 
338  if(pFile != INVALID_HANDLE_VALUE)
339  {
340  CloseHandle(pFile);
341  }
342 
343  if(pFile2 != INVALID_HANDLE_VALUE)
344  {
345  CloseHandle(pFile2);
346  }
347 
348  if( buffer != NULL )
349  {
350  _aligned_free( buffer);
351  }
352 
353  return ret;
354 }
355 
356 BOOL
358 {
359  BOOL ret = FALSE;
360  HANDLE pFile = INVALID_HANDLE_VALUE;
361 
362  pFile = CreateFile( copyFile,
363  GENERIC_READ,NULL,NULL,
364  OPEN_EXISTING,
365  FILE_ATTRIBUTE_NORMAL,
366  NULL);
367 
368  if( pFile == INVALID_HANDLE_VALUE )
369  {
370  PrintErrorMessage(L"Open encrypted file failed.", GetLastError());
371  return FALSE;
372  }
373 
374  DWORD bufferLength = 65536;
375  unsigned char* buffer = (unsigned char*)_aligned_malloc(bufferLength,bufferLength);
376  if( NULL == buffer )
377  {
378  ret = FALSE;
379  PrintErrorMessage(L"Allocate memory failed with insufficient resources.",0);
380  goto EXIT;
381  }
382 
383  ZeroMemory(buffer,65536);
384 
385  ret = ReadFile(pFile,buffer,bufferLength,&bufferLength,NULL);
386  if(0 == ret)
387  {
388  PrintErrorMessage(L"Read test file failed.",GetLastError());
389  goto EXIT;
390  }
391 
392  if( memcmp(buffer,clearText,bufferLength) == 0)
393  {
394  ret = TRUE;
395  }
396  else
397  {
398  printf("Verified filter driver decryption data failed.\r\nclearText data:");
399  for(int i = 0; i < sizeof(clearText); i++)
400  {
401  printf("%2x",clearText[i]);
402  }
403 
404  printf("\r\nReturn data:");
405  for(int i = 0; i < sizeof(clearText); i++)
406  {
407  printf("%2x",buffer[i]);
408  }
409 
410  printf("\r\n");
411 
412  ret = FALSE;
413 
414  }
415 
416 EXIT:
417 
418  if( pFile != INVALID_HANDLE_VALUE )
419  {
420  CloseHandle(pFile);
421  }
422 
423  if( buffer != NULL )
424  {
425  _aligned_free( buffer);
426  }
427 
428 
429  return ret;
430 
431 }
432 
433 VOID
435 {
436  DeleteFile(testFile);
437  DeleteFile(copyFile);
438 
439  //create a new test file
440  if(!CreateTestFile(TRUE))
441  {
442  return;
443  }
444  else
445  {
446  PrintPassedMessage(L"Created test file passed.\n");
447  }
448 
449  if(!AESEncryptFile(testFile,sizeof(key),key,sizeof(iv),iv,TRUE))
450  {
451  PrintLastErrorMessage( L"447 AESEncryptFile failed.");
452  return;
453  }
454  else
455  {
456  PrintPassedMessage(L"452 Encrypted test file passed.\n");
457  }
458 
459  if(!VerifyRawData(TRUE))
460  {
461  PrintFailedMessage(L"457 Verified AESEncryptFile test failed.\n");
462  return;
463  }
464  else
465  {
466  PrintPassedMessage(L"462 Verified encrypted file data passed.\n");
467  }
468 
469  if(!CopyEncryptedFile())
470  {
471  return;
472  }
473  else
474  {
475  PrintPassedMessage(L"471 Copied encrypted file passed.\n");
476  }
477 
478  if(!AESDecryptFile(testFile,sizeof(key),key,sizeof(iv),iv))
479  {
480  PrintLastErrorMessage( L"476 AESDecryptFile failed.");
481  return;
482  }
483  else
484  {
485  PrintPassedMessage(L"481 Decrypted file passed.\n");
486  }
487 
488  if(!VerifyRawData(FALSE))
489  {
490  PrintFailedMessage(L"486 Verified AESDecryptFile test failed.\n");
491  return;
492  }
493  else
494  {
495  PrintPassedMessage(L"491 Verified decrypted file data passed.\n");
496  }
497 
499  {
500  PrintFailedMessage(L"496 Verified filter driver decryption test failed.\n");
501  return;
502  }
503  else
504  {
505  PrintPassedMessage(L"501 Verified filter driver decryption test passed.\n");
506  }
507 
508 
509  //create a new test file
510  //test the filter driver encrypt the file, the filter driver should be enabled, and didn't exclude the current process Id.
511  if(!CreateTestFile(FALSE))
512  {
513  PrintFailedMessage(L"513 Created encrypted test file with filter driver failed.\n");
514  return;
515  }
516  else
517  {
518  PrintPassedMessage(L"518 Created encrypted test file with filter driver passed.\n");
519  }
520 
521  //decrypt the filter driver encrypted file with decryption API
522  if(!AESDecryptFile(testFile,sizeof(key),key,0,NULL))
523  {
524  PrintLastErrorMessage( L"538 AESDecryptFile filter driver encrypted file failed.");
525  return;
526  }
527 
528  if(!VerifyRawData(FALSE))
529  {
530  PrintFailedMessage(L"548 Verified filter driver encryption test failed.\n");
531  return;
532  }
533  else
534  {
535  PrintPassedMessage(L"553 Verified filter driver encryption test passed.\n");
536  }
537 
538  PrintPassedMessage(L"Encryption unit test passed.\n");
539  wprintf(L"\r\n");
540 
541 }
unsigned char key[]
BYTE ULONG bufferLength
Definition: FilterAPI.h:812
void PrintErrorMessage(LPWSTR message, DWORD errorCode)
Definition: Tools.cpp:93
WCHAR * copyFile
PULONG BYTE * ivTag
Definition: FilterAPI.h:778
BOOL CopyEncryptedFile()
ULONG BYTE ULONG ivLength
Definition: FilterAPI.h:740
BOOL VerifyRawData(BOOL isEncrypted)
unsigned char iv[]
BOOL VerifyFilterDriverDecryptData()
WCHAR * testFolder
LONGLONG fileSize
Definition: FilterAPI.h:684
void PrintLastErrorMessage(WCHAR *message)
Definition: Tools.cpp:49
void PrintPassedMessage(WCHAR *message)
Definition: Tools.cpp:33
DWORD DWORD DWORD DWORD dwFlagsAndAttributes
Definition: FilterAPI.h:674
void PrintFailedMessage(WCHAR *message)
Definition: Tools.cpp:39
unsigned char clearText[]
VOID EncryptionUnitTest()
WCHAR * testFile
unsigned char cipherText[]
BOOL CreateTestFile(BOOL bypassFilterDriver)

Social Network


Services Overview

Architect, implement and test file system filter drivers for a wide range of functionality. We can offer several levels of assistance to meet your specific.

Contact Us

You are welcome to contact us for salse or partnership.

Sales: sales@easefilter.com
Support: support@easefilter.com
Info: info@easefilter.com