📄 kgsha.cpp
字号:
/******************************************************************* * * Copyright (c) 1994-1999 Jetico, Inc., Finland * All rights reserved. * * File: kgsha.h * Revision: $Id: kgsha.cpp,v 1.1 2005/05/12 09:51:39 crypt Rel-1.6-5 $ * Created: * Description: main file of the libkgsha with functions that * may be called from external programs * *******************************************************************/#include <bc_types.h>#include <string.h>#include <bc_ioctl.h>#include "version.h"//#include "kgsha.h"#include "alg.h"#include "random.h"#include "kg_defs.h"#include "kblock.h"#include "kg_misc.h"#include "pwldlg.h"char kgsha_cpp[]="$Id: kgsha.cpp,v 1.1 2005/05/12 09:51:39 crypt Rel-1.6-5 $";/******************************************************* * * QueryFunction(); * * Purpose:: Check if the function is available for the * data block. * *******************************************************/BOOL QueryFunction( const BYTE **vDataBlock, /* block of data that is stored inside file-container in encrypted form */ const DWORD DataSize, /* size of the block of data */ const DWORD FunctionID /* identefier of function used as CerateKeyHandle(.., CreateFlag, ... ) */ ){ switch ( FunctionID ) { case CFLAG_CREATE_WITH_SINGLE_PASSWORD : return TRUE; case CFLAG_VERIFY_AND_LOAD_KEY : return TRUE; case CFLAG_CHANGE_PASSWORD : return TRUE; case CFLAG_ADD_NEW_PASSWORD : return TRUE; case CFLAG_REMOVE_ADDITIONAL_PASSWORD : return TRUE; default : return FALSE; }}/******************************************************* * * GetNameAndVersion() * *******************************************************/extern "C" BOOL GetNameAndVersion( char *Name, DWORD nameSize, DWORD *Major, DWORD *Minor ){ strncpy(Name, DISPLAY_NAME, nameSize - 1); Name[nameSize - 1] = 0; *Major = MAJOR_VERSION; *Minor = MINOR_VERSION; return TRUE; }/******************************************************* * * CreateKeyHandle() * * IMPORTANT: If CreateKeyHandle returns ERROR, the memory * reserved for DataBlock may be filled by wrong data * *******************************************************/extern "C" BOOL CreateKeyHandleEx( ALG_SERV Alg, /* what Encryption Algorithm will use the encryption key */ DWORD AlgKeyLength, // encryption key length for the Encryption Algorithm char *Text, // Text, containing filename of the container char *Caption, // Caption of the "get password" dialog window DWORD CreateFlag, // whether to create new key or open existing BYTE **vDataBlock, // block of data that is stored inside file-container in encrypted form DWORD *DataSize, // size of the block of data DWORD *KeyHandle, // returned Key Handle DWORD *ErrorCode, // if the function returns FALSE, look at the Error code DWORD *DataOffset ){ BOOL bResult, bHidPart; BYTE *key, *seed, *pool; //, containerId[ CONTAINER_ID_LENGTH ]; BYTE tmpKey[ MAXIMUM_KEY_SIZE_BYTES + 4 ]; char *password, *newPassword; DWORD offset; DWORD mask = 0, kbPosition = 0; DATA_BLOCK **DataBlock = (DATA_BLOCK **)vDataBlock; int iAttempts=0; key = seed = NULL; password = newPassword = NULL; if ( !Allocate( &seed, &password, &newPassword, &key, &pool ) ) { *ErrorCode = ERROR_INTERNAL_PROBLEM; return FALSE; } bResult = FALSE; // Assume error wxill occur // Test if given algorithm can be called if ( alg_verify( Alg ) ) { *ErrorCode = ERROR_INVALID_ALGORITHM; Free( seed, password, newPassword, key, pool ); return FALSE; } if ( !(CORRECT_CFLAG(CreateFlag)) ) { *ErrorCode = ERROR_INCORRECT_CREATE_FLAG; Free( seed, password, newPassword, key, pool ); return FALSE; } switch ( CreateFlag ) { // ************** Creating new Data Block and return Key Handle *************** case CFLAG_CREATE_WITH_SINGLE_PASSWORD: if ( !DataBlockAllocate( DataBlock, DataSize, Alg.alg_id) ) { *ErrorCode = ERROR_INTERNAL_PROBLEM; break; } if ( ((*ErrorCode) = GetPasswordWithVerification( Text, Caption, password )) != ERROR_NO ) { DataBlockFree( DataBlock ); break; } if ( ((*ErrorCode) = GetSeedValue( seed, SEED_LENGTH )) != ERROR_NO ) { DataBlockFree( DataBlock ); break; } if ( GenerateKey(key, MAXIMUM_KEY_SIZE_BYTES, seed, SEED_LENGTH) != ERROR_NO ) { *ErrorCode = ERROR_INTERNAL_PROBLEM; DataBlockFree( DataBlock ); break; } if ( DataBlockWriteKey(*DataBlock, Alg, AlgKeyLength, password, key, KATTRIBUTE_KEY_FULL, pool) != ERROR_NO ) { DataBlockFree( DataBlock ); *ErrorCode = ERROR_INTERNAL_PROBLEM; break; } /* verify all that we did before */ if ( ((*ErrorCode) = DataBlockVerifyPasswordAndGetKey( *DataBlock, Alg, AlgKeyLength, password, key, pool ) ) != ERROR_NO ) { break; } // Call Algorithm driver for expanding and storing the Key if ( alg_make_key(Alg, key, AlgKeyLength, pool, KeyHandle) ) { *ErrorCode = ERROR_INVALID_ALGORITHM; DataBlockFree( DataBlock ); break; } *ErrorCode = ERROR_NO; bResult = TRUE; break; // ************** Verify password and return Key Handle *************** case CFLAG_VERIFY_AND_LOAD_KEY: // 1 - minimum number of keys inside the block offset = 0; // If we find out that user entered password for // hidden container, offset won't be 0 if ( ((*ErrorCode) = DataBlockCheck(*DataBlock, *DataSize, Alg.alg_id)) != ERROR_NO ) { break; } if ( ((*ErrorCode) = GetPassword(Text, Caption, password )) != ERROR_NO ) { break; } // First Attempt to use password: is the key simply stored inside DataBlock? *ErrorCode = DataBlockVerifyPasswordAndGetKey(*DataBlock,Alg, AlgKeyLength,password, key, pool ); // Second Attempt to use password: is the password was entered for Hidden Part? if ( (*ErrorCode) == ERROR_INCORRECT_PASSWORD ) { mask = 0, kbPosition = 0; if ( ((*ErrorCode) = DataBlockVerifyPasswordAndGetKey_Hidden( *DataBlock, Alg, AlgKeyLength, password, key, pool, &offset, &mask, &kbPosition ) ) != ERROR_NO ) { break; } else { //HDLG_ShowInformationMessage( hWndParent ); } } else if ( (*ErrorCode) != ERROR_NO ) break; // Call Algorithm driver for expanding and storing the Key if ( alg_make_key(Alg, key, AlgKeyLength, pool, KeyHandle) ) { *ErrorCode = ERROR_INVALID_ALGORITHM; break; } *ErrorCode = ERROR_NO; *DataOffset = offset; bResult = TRUE; break; // ************** Change password for Regular or Hidden container *************** case CFLAG_CHANGE_PASSWORD: if ( ((*ErrorCode) = DataBlockCheck(*DataBlock, *DataSize, Alg.alg_id)) != ERROR_NO ) { break; } if ( ((*ErrorCode) = GetPassword(Text, "Enter old password: "/*Caption*/, password )) != ERROR_NO ) { break; } bHidPart = FALSE; if ( ((*ErrorCode) = DataBlockVerifyPasswordAndGetKey( *DataBlock, Alg, AlgKeyLength, password, key, pool ) ) != ERROR_NO ) { // May be, we are going to change password for hidden part? if ( ((*ErrorCode) = DataBlockVerifyPasswordAndGetKey_Hidden( *DataBlock, Alg, AlgKeyLength, password, key, pool, &offset, &mask, &kbPosition ) ) != ERROR_NO ) { break; // It is not a password for hidden part :-( } else { bHidPart = TRUE; } } if ( !bHidPart ) { if ( ((*ErrorCode) = GetPasswordWithVerification( Text, "Enter new password: "/*Caption*/, newPassword )) != ERROR_NO ) { break; } if ( ((*ErrorCode) = DataBlockChangePassword( *DataBlock, Alg, AlgKeyLength, password, newPassword, pool )) != ERROR_NO ) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -