📄 pgputil.cpp
字号:
}
err = PGPNewFileSpecFromFullPath( context, pathName, &info->outFile );
err = PGPAddJobOptions( event->job,
PGPOOutputDirectory(context, info->outFile ),
PGPOLastOption(context));
bHandled = TRUE;
}
}
break;
case kDecode_FileOutput:
err = PGPNewFileSpecFromFullPath( context, pathName, &info->outFile );
err = PGPAddJobOptions( event->job,
PGPOOutputFile(context, info->outFile ),
PGPOLastOption(context));
bHandled = TRUE;
break;
default:;
}
}
if(!bHandled)
{
if(info->outBuf)
{
PGPFreeData(info->outBuf);
info->outBuf = NULL;
info->outBufSize = 0;
}
err = PGPAddJobOptions( event->job,
PGPOAllocatedOutputBuffer( context, (void**)&info->outBuf, MAX_PGPSize, &info->outBufSize),
PGPOLastOption(context));
}
}
break ;
case kPGPEvent_SignatureEvent :// = 12, /* ----- Sig Event-----Signature status report */
{
PGPEventSignatureData *d = &event->data.signatureData;
}
break ;
case kPGPEvent_BeginLexEvent :// = 13, /*----- Begin Lex Event----- Initial event per lexical unit*/
{
PGPEventBeginLexData *d = &event->data.beginLexData;
}
break ;
case kPGPEvent_EndLexEvent :// = 14, /* ----- End Lex Event-----Final event per lexical unit */
{
PGPEventEndLexData *d = &event->data.endLexData;
}
break ;
case kPGPEvent_RecursionEvent :// = 15, /* Notification of recursive job creation */
break ;
case kPGPEvent_DetachedSignatureEvent :// = 16, /*----- Detached Sig Event----- Need input for verification of detached signature */
break ;
case kPGPEvent_KeyGenEvent :// = 17, /* ----- KeyGen Event-----Key generation progress */
{
PGPUInt32 state = event->data.keyGenData.state;
if (!bKeyGen )
bKeyGen = TRUE;
}
break ;
case kPGPEvent_KeyServerEvent :// = 18, /*----- Key Server Event----- Key Server progress */
{
PGPEventKeyServerData *d = &event->data.keyServerData;
}
break ;
case kPGPEvent_KeyServerSignEvent :// = 19, /* Key Server passphrase */
break ;
case kPGPEvent_KeyServerTLSEvent :// = 20, /* Key Server TLS event */
break ;
case kPGPEvent_KeyServerIdleEvent :// = 21, /* Idle during keyserver call */
break ;
case kPGPEvent_SocketsIdleEvent :// = 22, /* Idle during sockets */
break ;
case kPGPEvent_DecryptionEvent :// = 23, /* ----- Decryption Event-----Decryption data report */
{
PGPEventDecryptionData *d = &event->data.decryptionData;
char * cipher = cipher_algor_table(d->cipherAlgorithm);
unsigned int i = 0;
if(info->sessionKey)
{
PGPFreeData(info->sessionKey);
info->sessionKey = NULL;
info->sessionKeySize = 0;
}
info->sessionKey = (PGPByte*) PGPNewSecureData( PGPGetDefaultMemoryMgr(), d->sessionKeyLength, kPGPMemoryMgrFlags_None);
if( info->sessionKey == NULL)
{
err = kPGPError_OutOfMemory;
goto done;
}
info->sessionKeySize = d->sessionKeyLength;
memcpy(info->sessionKey , d->sessionKey , d->sessionKeyLength) ;
}
break ;
case kPGPEvent_EncryptionEvent :// = 24, /*----- Encryption Event----- Encryption data report */
//输出加密算法类型 密钥长度内容
{
PGPEventEncryptionData *d = &event->data.encryptionData;
char * cipher = cipher_algor_table(d->cipherAlgorithm);
}
break ;
case kPGPEvent_ToBeSignedEvent :// = 25, /* To-be-signed hash */
break ;
}
done :
return err ;
}
//初始化加密要求
PGPError ConsoleAcquireEntropy(PGPContextRef context , PGPUInt32 entropyNeeded ,PGPUInt32 * pEntropyAcquired ,PGPBoolean bOutputProgress)
{
PGPError err = kPGPError_NoErr;
PGPUInt32 entropyAcquired = 0;
time_t start = 0;
time_t current = 0;
if (entropyNeeded == 0)
return (kPGPError_NoErr);
time (&start);
while (entropyAcquired < entropyNeeded)
{
entropyAcquired = PGPContextReserveRandomBytes (context, entropyNeeded);
err = PGPGlobalRandomPoolAddSystemState ();
if (IsntPGPError (err))
goto done ;
time (¤t);
if (current > (start + 10))
break;
}
if (entropyAcquired < entropyNeeded)
return (kPGPError_OutOfEntropy);
done:
if (IsntPGPError (err))
{
if (pEntropyAcquired)
*pEntropyAcquired = entropyAcquired;
}
return (err);
}
/*---------------------------------------------------------------------------------------
print a list of supported algorithms
---------------------------------------------------------------------------------------*/
static PGPError OnListSdkFeatures(void)
{
PGPError err = kPGPError_NoErr;
PGPFlags flags;
PGPUInt32 numAlgs, i, cnt;
PGPSymmetricCipherInfo cipherInfo;
PGPPublicKeyAlgorithmInfo algorInfo;
PGPBoolean fipsMode;
/* check for FIPS mode */
err = PGPGetFeatureFlags(kPGPFeatures_GeneralSelector, &flags);
if (IsPGPError(err))
return err ;
fipsMode = PGPFeatureExists(flags, kPGPFeatureMask_FIPSmode);
err = PGPCountSymmetricCiphers (&numAlgs);
if (IsPGPError(err))
return err ;
for (cnt = 0, i = 0; i < numAlgs; i ++)
{
err = PGPGetIndexedSymmetricCipherInfo (i, &cipherInfo);
if (IsPGPError(err))
return err ;
/* if we are in FIPS mode, we ignore non FIPS ciphers */
//if( fipsMode && ((cipherInfo.info.flags & kPGPAlgorithmInfoFlags_FIPS) == 0))
// continue;
}
err = PGPCountPublicKeyAlgorithms (&numAlgs);
if (IsPGPError(err))
return err ;
for (i = 0; i < numAlgs; i ++)
{
err = PGPGetIndexedPublicKeyAlgorithmInfo (i, &algorInfo);
if (IsPGPError(err))
return err ;
/* if we are in FIPS mode, we ignore non FIPS Algorithms */
//if(fipsMode && ((algorInfo.info.flags & kPGPAlgorithmInfoFlags_FIPS) == 0))
// continue;
}
return err;
}
PGPError OnInit(PGPFlags options)
{
PGPError err = kPGPError_NoErr;
err = PGPsdkInit(options); //
err = OnListSdkFeatures() ;
err = PGPNewContext(kPGPsdkAPIVersion, &m_gContext);
/* make sure we have enough entropy */
err = ConsoleAcquireEntropy(m_gContext, PGPGlobalRandomPoolGetMinimumEntropy() / 8 , NULL, FALSE);
return err ;
}
PGPError ImportKeys(PGPContextRef context, const char *path, PGPInputFormat format, PGPKeyDBRef *keyDB )
{
PGPError err = kPGPError_NoErr;
PGPFileSpecRef fileSpec = kInvalidPGPFileSpecRef;
/* Create filespec to keys */
err = PGPNewFileSpecFromFullPath(context, path, &fileSpec);
if (IsPGPError(err))
{
if( PGPFileSpecRefIsValid(fileSpec))
PGPFreeFileSpec(fileSpec);
return err;
}
/* Read in the test key and get a ref to it */
err = PGPImport( context, keyDB,
PGPOInputFile( context, fileSpec ),
PGPOInputFormat( context, format),
PGPOLastOption( context ) );
if( PGPFileSpecRefIsValid(fileSpec))
PGPFreeFileSpec(fileSpec);
return err;
}
//static PGPContextRef m_gContext = kInvalidPGPContextRef ; // g
//static DecodeInfo m_gDecodeInfo ; //
//static PGPKeyDBRef m_gKeyDB = kInvalidPGPKeyDBRef; //
//static std::string m_gPwd ;
//static PGPFilterRef m_gFilter = kInvalidPGPFilterRef ;
//static PGPUInt32 m_gnumKeys = 0 ;
//static PGPKeySetRef m_guserKeySet = kInvalidPGPKeySetRef ;
//static PGPKeyDBObjRef m_gSignKey = kInvalidPGPKeyDBObjRef;
void Clearup()
{
m_gPwd = _T("") ;
m_gnumKeys = 0 ;
CleanUpDecodeInfo(&m_gDecodeInfo);
if( PGPKeySetRefIsValid (m_guserKeySet) )
PGPFreeKeySet(m_guserKeySet);
//if (PGPFilterRefIsValid(m_gFilter))
// PGPFreeFilter(m_gFilter) ;
if( PGPKeyDBRefIsValid( m_gKeyDB ) )
PGPFreeKeyDB( m_gKeyDB );
if (PGPContextRefIsValid(m_gContext))
PGPFreeContext(m_gContext) ;
PGPsdkCleanup() ;
}
static PGPError pgpCipherEncrypt(PGPContextRef context ,
PGPCipherAlgorithm algor ,
CipherMode mode ,
PGPByte *key ,
PGPByte *plaintext , PGPSize plaintextlen ,
PGPByte *initvector )
{
PGPSymmetricCipherContextRef cipher = kInvalidPGPSymmetricCipherContextRef;
//PGPSymmetricCipherContextRef cipher1 = kInvalidPGPSymmetricCipherContextRef;
PGPCFBContextRef cfb = kInvalidPGPCFBContextRef;
PGPCBCContextRef cbc = kInvalidPGPCBCContextRef;
//PGPCFBContextRef cfb1 = kInvalidPGPCFBContextRef;
//PGPCBCContextRef cbc1 = kInvalidPGPCBCContextRef;
PGPError err = kPGPError_NoErr;
PGPByte *out = NULL;
PGPSize keySize;
PGPSize blockSize;
PGPUInt32 i;
out = (PGPByte *)PGPNewSecureData( PGPPeekContextMemoryMgr(context) , plaintextlen , kPGPMemoryMgrFlags_None);
if (out == NULL)
goto done;
/* create and setup the cipher */
err = PGPNewSymmetricCipherContext(context, algor, &cipher);
if (IsPGPError(err))
goto done;
/* Get information about the cipher */
err = PGPGetSymmetricCipherSizes(cipher, &keySize, &blockSize);
if (IsPGPError(err))
goto done;
/* create and setup the cipher, init keys and IV */
switch(mode)
{
case kCipherModeECB:
err = PGPInitSymmetricCipher(cipher, key);
if (IsPGPError(err))
goto done;
////////////////////////////////////////////////////////////////////
for(i = 0; i < plaintextlen ; i += blockSize)
{
err = PGPSymmetricCipherEncrypt(cipher, plaintext + i, out + i);
if (IsPGPError(err))
goto done;
}
///////////////////////////////////////////////////////////////////
break;
case kCipherModeCBC:
/* create new CBC context */
//////////////////////////////////////////////////////////////////
err = PGPNewCBCContext(cipher , &cbc);
if (IsPGPError(err))
goto done;
cipher = kInvalidPGPSymmetricCipherContextRef;
// init CBC with IV and Key //
err = PGPInitCBC(cbc, key, initvector);
if (IsPGPError(err))
goto done;
for(i = 0; i < plaintextlen ; i += blockSize)
{
err = PGPCBCEncrypt(cbc, plaintext + i,blockSize, out + i);
if (IsPGPError(err))
goto done;
}
//////////////////////////////////////////////////////////////////
/*
err = PGPCopySymmetricCipherContext(cipher, &cipher1);
if (IsPGPError(err))
goto done;
err = PGPNewCBCContext(cipher1, &cbc);
if (IsPGPError(err))
goto done;
err = PGPCBCGetSymmetricCipher(cbc, &cipher1);
if (IsPGPError(err))
goto done;
cipher1 = kInvalidPGPSymmetricCipherContextRef;
// init CBC with IV and Key //
err = PGPInitCBC(cbc, key, initvector);
if (IsPGPError(err))
goto done;
// Save a copy for decryption later //
// err = PGPCopyCBCContext(cbc, &cbc1);*/
break;
case kCipherModeCFB:
// create new CFB context //
/////////////////////////////////////////////////////////////////
err = PGPNewCFBContext(cipher, 1, &cfb);
if (IsPGPError(err))
goto done;
cipher = kInvalidPGPSymmetricCipherContextRef;
// init CFB with IV and Key/
err = PGPInitCFB(cfb, key , initvector);
if (IsPGPError(err))
goto done;
for(i = 0; i < plaintextlen ; i += blockSize)
{
err = PGPCFBEncrypt(cfb, plaintext + i, blockSize, out + i);
if (IsPGPError(err))
goto done;
}
/////////////////////////////////////////////////////////////////
/*
err = PGPCopySymmetricCipherContext(cipher, &cipher1);
if (IsPGPError(err))
goto done;
err = PGPNewCFBContext(cipher1, 1, &cfb);
if (IsPGPError(err))
goto done;
err = PGPCFBGetSymmetricCipher(cfb, &cipher1);
if (IsPGPError(err))
goto done;
cipher1 = kInvalidPGPSymmetricCipherContextRef;
// init CFB with IV and Key/
err = PGPInitCFB(cfb, key , initvector);
if (IsPGPError(err))
goto done;
// Save a copy for decryption later //
//err = PGPCopyCFBContext(cfb, &cfb1); */
break;
}
/* // Perform Encryption according to mode //
for(i = 0; i < plaintextlen ; i += blockSize)
switch(mode)
{
case kCipherModeECB:
err = PGPSymmetricCipherEncrypt(cipher, plaintext + i, out + i);
if (IsPGPError(err))
goto done;
break;
case kCipherModeCBC:
err = PGPCBCEncrypt(cbc, plaintext + i,blockSize, out + i);
if (IsPGPError(err))
goto done;
break;
case kCipherModeCFB:
err = PGPCFBEncrypt(cfb, plaintext + i, blockSize, out + i);
if (IsPGPError(err))
goto done;
break;
}
*/
memcpy(plaintext , out , plaintextlen) ;
done:
if(out)
PGPFreeData(out);
if( PGPSymmetricCipherContextRefIsValid( cipher ) )
(void) PGPFreeSymmetricCipherContext( cipher );
if( PGPCFBContextRefIsValid( cfb ) )
(void) PGPFreeCFBContext(cfb);
if( PGPCBCContextRefIsValid( cbc ) )
(void) PGPFreeCBCContext(cbc);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -