Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 5th, 2005, 6:59 PM   #1
c0ldshadow
Unverified User
 
c0ldshadow's Avatar
 
Join Date: Jun 2005
Location: NJ
Posts: 23
Rep Power: 0 c0ldshadow is on a distinguished road
SpcFileWipe algorithm in Secure Programming Cookbook not working

hey, havent been posting at any forums forever.. im starting to get back to coding though and this will be my forum of choice. greetings to dawei, thedark, and nxxion...

well, i was reading the book "secure programming cookbook" , and i figured instead of manually copying the code for a secure file deletion algorithm that looked interesting i'd google for SpcFileWipe---the name of the algorithm in the book. i found someone else who had typed out some of the algorithms from the book.

the particular problem with the algorithm im having is that the following section gives an error (GetLastError returns -2146893804 (-0x7FF6FFEC in hex i believe)) ..

  if (CryptAcquireContext(&hProvider, 0, 0, 0,      CRYPT_VERIFYCONTEXT)==FALSE)
	{
 	 	char asd[80];
		wsprintf(asd,"%d",GetLastError());
		MessageBox(0,asd,asd,0);
    	        return FALSE;
	}


this link didn't show anything about this error return code message...
http://msdn.microsoft.com/library/de...irecontext.asp

i tried changing the following code to this, and the error no longer happens but program goes into some sort of infinite loop:

  if (CryptAcquireContext(&hProvider, 0, 0, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)==FALSE)
	{
 	 	char asd[80];
		wsprintf(asd,"%d",GetLastError());
		MessageBox(0,asd,asd,0);
    	return FALSE;
	}


this is the entire source code of the algorithms in the book..
i do not understand everything fully, however i have not studied this stuff in depth yet... if anyone sees anything stupid with the code that simply jumps out please let me know... no emergency for help here heh. curious what's wrong with this script however... mainly just looking to get back into some coding and posting at a c++ forum again.. im going to watch some baseball but ill check back later and look over the code myself a bit more as well..

regards, --c0ldshadow


#include <windows.h>
#include <wincrypt.h>
//#include "spc.h"
#define SPC_WIPE_BUFSIZE 4096

static BOOL RandomPass(HANDLE hFile, HCRYPTPROV hProvider, DWORD dwFileSize)
{
  BYTE  pbBuffer[SPC_WIPE_BUFSIZE];
  DWORD cbBuffer, cbTotalWritten, cbWritten;

  if (SetFilePointer(hFile, 0, 0, FILE_BEGIN) == 0xFFFFFFFF) return FALSE;
  while (dwFileSize > 0) {
    cbBuffer = (dwFileSize > sizeof(pbBuffer) ? sizeof(pbBuffer) : dwFileSize);
    if (!CryptGenRandom(hProvider, cbBuffer, pbBuffer)) return FALSE;
    for (cbTotalWritten = 0;  cbBuffer > 0;  cbTotalWritten += cbWritten)
      if (!WriteFile(hFile, pbBuffer + cbTotalWritten, cbBuffer - cbTotalWritten,
                     &cbWritten, 0)) return FALSE;
    dwFileSize -= cbTotalWritten;
  }
  return TRUE;
}

static BOOL PatternPass(HANDLE hFile, BYTE *pbBuffer, DWORD cbBuffer, DWORD dwFileSize) {
  DWORD cbTotalWritten, cbWrite, cbWritten;

  if (!cbBuffer || SetFilePointer(hFile, 0, 0, FILE_BEGIN) == 0xFFFFFFFF) return FALSE;
  while (dwFileSize > 0) {
    cbWrite = (dwFileSize > cbBuffer ? cbBuffer : dwFileSize);
    for (cbTotalWritten = 0;  cbWrite > 0;  cbTotalWritten += cbWritten)
      if (!WriteFile(hFile, pbBuffer + cbTotalWritten, cbWrite - cbTotalWritten,
                     &cbWritten, 0)) return FALSE;
    dwFileSize -= cbTotalWritten;
  }
  return TRUE;
}

BOOL SpcWipeFile(HANDLE hFile) {
  BYTE       pbBuffer[SPC_WIPE_BUFSIZE];
  DWORD      dwCount, dwFileSize, dwIndex, dwPass;
  HCRYPTPROV hProvider;

  static BYTE  pbSinglePats[16] = {
    0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
    0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
  };
  static BYTE  pbTriplePats[6][3] = {
    { 0x92, 0x49, 0x24 }, { 0x49, 0x24, 0x92 }, { 0x24, 0x92, 0x49 },
    { 0x6d, 0xb6, 0xdb }, { 0xb6, 0xdb, 0x6d }, { 0xdb, 0x6d, 0xb6 }
  };
  static DWORD cbPattern = sizeof(pbTriplePats[0]);

  if ((dwFileSize = GetFileSize(hFile, 0)) == INVALID_FILE_SIZE)

{

return FALSE;

}
  if (!dwFileSize) return TRUE;

  if (CryptAcquireContext(&hProvider, 0, 0, 0, CRYPT_VERIFYCONTEXT)==FALSE)
	{
 	 	char asd[80];
		wsprintf(asd,"%d",GetLastError());
		MessageBox(0,asd,asd,0);
    	return FALSE;
	}
  for (dwPass = 0;  dwPass < 4;  dwPass++)
    if (!RandomPass(hFile, hProvider, dwFileSize)) {
      CryptReleaseContext(hProvider, 0);
      return FALSE;
    }

  memset(pbBuffer, pbSinglePats[5], sizeof(pbBuffer));
  if (!PatternPass(hFile, pbBuffer, sizeof(pbBuffer), dwFileSize)) {
    CryptReleaseContext(hProvider, 0);
    return FALSE;
  }
  memset(pbBuffer, pbSinglePats[10], sizeof(pbBuffer));
  if (!PatternPass(hFile, pbBuffer, sizeof(pbBuffer), dwFileSize)) {
    CryptReleaseContext(hProvider, 0);
    return FALSE;
  }

  cbPattern = sizeof(pbTriplePats[0]);
  for (dwPass = 0;  dwPass < 3;  dwPass++) {
    dwCount   = sizeof(pbBuffer) / cbPattern;
    for (dwIndex = 0;  dwIndex < dwCount;  dwIndex++)
      CopyMemory(pbBuffer + (dwIndex * cbPattern), pbTriplePats[dwPass],
                  cbPattern);
    if (!PatternPass(hFile, pbBuffer, cbPattern * dwCount, dwFileSize)) {
      CryptReleaseContext(hProvider, 0);
      return FALSE;
    }
  }

  for (dwPass = 0;  dwPass < sizeof(pbSinglePats);  dwPass++) {
    memset(pbBuffer, pbSinglePats[dwPass], sizeof(pbBuffer));
    if (!PatternPass(hFile, pbBuffer, sizeof(pbBuffer), dwFileSize)) {
      CryptReleaseContext(hProvider, 0);
      return FALSE;
    }
  }

  for (dwPass = 0;  dwPass < sizeof(pbTriplePats) / cbPattern;  dwPass++) {
    dwCount   = sizeof(pbBuffer) / cbPattern;
    for (dwIndex = 0;  dwIndex < dwCount;  dwIndex++)
      CopyMemory(pbBuffer + (dwIndex * cbPattern), pbTriplePats[dwPass],
                  cbPattern);
    if (!PatternPass(hFile, pbBuffer, cbPattern * dwCount, dwFileSize)) {
      CryptReleaseContext(hProvider, 0);
      return FALSE;
    }
  }

  for (dwPass = 0;  dwPass < 4;  dwPass++)
    if (!RandomPass(hFile, hProvider, dwFileSize)) {
      CryptReleaseContext(hProvider, 0);
      return FALSE;
    }

  CryptReleaseContext(hProvider, 0);
  return TRUE;
}
__________________
DeepTide

The way is shut.
It was made by those who are dead
and the Dead keep it.
The way is shut.
c0ldshadow is offline   Reply With Quote
Old Aug 7th, 2005, 8:40 PM   #2
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 893
Rep Power: 4 The Dark is on a distinguished road
-2146893804 comes out as hex FFFFFFFF80090014, which is "Invalid provider type specified." according to the error lookup tool that came with VS7.
Quote:
The value of the dwProvType parameter is out of range. All provider types must be from 1 to 999, inclusive.
You seem to be passing 0 in, which is not valid. I don't know if that helps any.
The Dark is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:53 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC