📄 crypt_file.cpp
字号:
/*
//
// INTEL CORPORATION PROPRIETARY INFORMATION
// This software is supplied under the terms of a license agreement or
// nondisclosure agreement with Intel Corporation and may not be copied
// or disclosed except in accordance with the terms of that agreement.
// Copyright (c) 2005 Intel Corporation. All Rights Reserved.
//
*/
#include <iostream>
using namespace std;
#include "cipherdefs.h"
#include "ipp_cipher_des.h"
#include "ipp_cipher_rij.h"
#include "ipp_cipher_blf.h"
#include "ipp_cipher_twf.h"
#include "ipp_cipher_arc4.h"
#include "ocf_cipher.h"
static
int convert(const char* s, Ipp8u* d, int* dlen, int maxLen)
{
if(1 & strlen(s)) {
cout <<"Odd number of hex digits!" <<endl;
return 0;
}
int len;
*dlen = 0;
for(len=0; *s && len<maxLen; s+=2,d++) {
Ipp32u n;
if(!sscanf(s, "%2x", &n))
return 0;
*d = (Ipp8u)n;
len++;
}
*dlen = len;
return 1;
}
static
void hexdump(const char *title, const Ipp8u* s, int len)
{
int n=0;
if(title)
cout <<title;
cout <<hex;
for(n=0; n<len; n++) {
int h = (s[n]>>4) &0xF;
int l = s[n] & 0xF;
cout <<' ' <<h <<l;
}
cout <<dec;
cout <<endl;
}
static
void cmd_info(void)
{
cout <<"crypt_file <mandatory_parameters> [optionals_parameters] input_filename" <<endl;
cout <<endl <<"mandatory_parameters are:" <<endl;
cout <<"-d{ENCRYPT|DECRYPT} - ENCRYPT or DECRYPT operation" <<endl;
cout <<"-c{DES|TDES|RIJ128|RIJ192|RIJ256|BLF} - enabled cipher" <<endl;
cout <<"-m{ECB|CBC|CFB|CTR} - enabled mode" <<endl;
cout <<"-ofilename - output file name" <<endl;
cout <<endl <<"optionals_parameters are:" <<endl;
cout <<"-kSECRET_KEY_STRING - cipher key (optional)" <<endl;
cout <<"-ivINIT_VECTOR_STRING - cipher IV (optional)" <<endl;
cout <<"-pVALUE - cipher CFB ot CTR mode parameter (optional)" <<endl;
cout <<"-sw - specifies software (IPP) cipher implementation" <<endl;
cout <<"-hw - specifies hardware (OCF) cipher implementation, if possible" <<endl;
}
//
// -d[irection]ENCRYPT/DECRYPT
// -c[ipher]DES/TDES/AES/BLF/TWF/RC4
// -m[ode]ECB/CBC/CFB/OFB/CTR
// -p[aram]value
// -k[ey]octstring
// -ivoctstring
// -o[ut]out_filename
// -sw
// -hw
// inp_filename
int main(int argc, char* argv[])
{
// skip program name
argc--;
argv++;
CipherOperation op = OPERATION_UNKNOWN;
AlgoName algo = ALGO_UNKNOWN;
CipherMode mode = MODE_UNKNOWN;
int mparam = 0;
Ipp8u key[MAX_KEYLEN];
int keyLen = 0;
Ipp8u iv[MAX_BLKLEN];
int ivLen = 0;
bool useSWimplementation = true;
FILE* inpFH;
FILE* outFH;
// clear key and IV
memset(key, 0, MAX_BLKLEN);
memset(iv, 0, MAX_BLKLEN);
// extract options
bool parError = false;
while(argc && !parError) {
if(0==memcmp("-d", *argv, sizeof("-d")-1)) {
op = 0==memcmp("ENCRYPT", *argv+2, sizeof("ENCRYPT")-1)? ENCRYPT :
0==memcmp("DECRYPT", *argv+2, sizeof("ENCRYPT")-1)? DECRYPT : OPERATION_UNKNOWN;
parError = op==OPERATION_UNKNOWN;
if(op==OPERATION_UNKNOWN)
cout <<"crypt_file: unrecognized option " <<'"' <<*argv+2 <<'"' <<endl;
}
else if(0==memcmp("-c", *argv, sizeof("-c")-1)) {
algo = 0==memcmp("DES", *argv+2, sizeof("DES")-1)? ALGO_DES:
0==memcmp("TDES", *argv+2, sizeof("TDES")-1)? ALGO_TDES:
0==memcmp("AES", *argv+2, sizeof("AES")-1)? ALGO_AES:
0==memcmp("RIJ128", *argv+2, sizeof("RIJ128")-1)? ALGO_AES:
0==memcmp("RIJ192", *argv+2, sizeof("RIJ192")-1)? ALGO_RIJ192:
0==memcmp("RIJ256", *argv+2, sizeof("RIJ256")-1)? ALGO_RIJ256:
0==memcmp("BLF", *argv+2, sizeof("BLF")-1)? ALGO_BLF:
#if defined(_IPP_v51_)
0==memcmp("RC4", *argv+2, sizeof("RC4")-1)? ALGO_RC4:
#endif
0==memcmp("TWF", *argv+2, sizeof("TWF")-1)? ALGO_TWF: ALGO_UNKNOWN;
parError = algo==ALGO_UNKNOWN;
if(algo==ALGO_UNKNOWN)
cout <<"crypt_file: unrecognized option " <<'"' <<*argv+2 <<'"' <<endl;
}
else if(0==memcmp("-m", *argv, sizeof("-m")-1)) {
mode = 0==memcmp("ECB", *argv+2, sizeof("ECB")-1)? ECB:
0==memcmp("CBC", *argv+2, sizeof("CBC")-1)? CBC:
0==memcmp("CFB", *argv+2, sizeof("CFB")-1)? CFB:
0==memcmp("CTR", *argv+2, sizeof("CTR")-1)? CTR: MODE_UNKNOWN;
parError = mode==MODE_UNKNOWN;
if(mode==MODE_UNKNOWN)
cout <<"crypt_file: unrecognized option " <<'"' <<*argv+2 <<'"' <<endl;
}
else if(0==memcmp("-p", *argv, sizeof("-p")-1)) {
sscanf(*argv+2, "%d", &mparam);
parError = mparam<=0;
if(0>=mparam)
cout <<"crypt_file: unrecognized option " <<'"' <<*argv <<'"' <<endl;
}
else if(0==memcmp("-k", *argv, sizeof("-k")-1)) {
parError = !convert(*argv+2, key, &keyLen, MAX_KEYLEN);
if(parError)
cout <<"crypt_file: unrecognized option " <<'"' <<*argv <<'"' <<endl;
}
else if(0==memcmp("-iv", *argv, sizeof("-iv")-1)) {
parError = !convert(*argv+3, iv, &ivLen, MAX_BLKLEN);
if(parError)
cout <<"crypt_file: unrecognized option " <<'"' <<*argv <<'"' <<endl;
}
else if(0==memcmp("-o", *argv, sizeof("-o")-1)) {
outFH = fopen(*argv+2, "wb");
parError = outFH==0;
if(parError)
cout <<"crypt_file: can not open " <<'"' <<*argv+2 <<'"' <<endl;
}
else if(0==memcmp("-sw", *argv, sizeof("-sw")-1)) {
useSWimplementation = true;
}
else if(0==memcmp("-hw", *argv, sizeof("-hw")-1)) {
useSWimplementation = false;
}
else {
inpFH = fopen(*argv, "rb");
parError = inpFH==0;
if(parError)
cout <<"crypt_file: can not open " <<'"' <<*argv <<'"' <<endl;
}
argc--;
argv++;
}
// test critical parameters
if(parError ||
OPERATION_UNKNOWN==op ||
ALGO_UNKNOWN==algo ||
MODE_UNKNOWN==mode ||
0==inpFH ||
0==outFH) {
cout <<"crypt_file: command line error" <<endl;
cout <<"crypt_file: command line format:" <<endl;
cmd_info();
return 1;
}
//
// inform about actual parameters:
//
// operation
cout <<"operation: " <<GetCipherOperationName(op) <<endl;
// cipher
cout <<" cipher: " <<GetAlgoName(algo) <<endl;
// key
keyLen = SuitableCipherKeyLen(algo, keyLen);
hexdump(" key: ", key, keyLen);
// mode
cout <<" mode: " <<GetCipherModeName(mode) <<endl;
// parameter
mparam = SuitableCipherParam(algo, mode, mparam);
cout <<"mode parm: " <<mparam <<endl;
// IV
ivLen = CipherBlockSize(algo);
hexdump(" iv: ", iv, ivLen);
// implementation
if(useSWimplementation)
cout <<"implement: SW" <<endl;
else
cout <<"implement: HW" <<endl;
// test crypto HW accessibility
#if !defined( _OCF_ )
if(!useSWimplementation) {
useSWimplementation = true;
cout <<"crypt_file: sw implementation forced" <<endl;
}
#else
if(!useSWimplementation) {
cdevFD = cdevOpen();
if(-1==cdevFD) {
useSWimplementation = true;
cout <<"crypt_file: can not open crypto device" <<endl;
cout <<"crypt_file: sw implementation forced" <<endl;
}
else {
if( !ocfCipherEnabled(cdevFD, algo) ) {
useSWimplementation = true;
cout <<"crypt_file: algorithm " <<GetAlgoName(algo) <<" disabled" <<endl;
cout <<"crypt_file: sw implementation forced" <<endl;
}
}
}
#endif
// create specified cipher
Cipher* pCipher;
#if defined( _OCF_ )
if(!useSWimplementation)
pCipher = new OCFCipher(algo, key, keyLen);
else
#endif
switch(algo) {
case ALGO_DES: pCipher = new DES(key); break;
case ALGO_TDES: pCipher = new TDES(key); break;
case ALGO_AES: pCipher = new AES(key, keyLen); break;
case ALGO_RIJ192:pCipher = new RIJ192(key, keyLen); break;
case ALGO_RIJ256:pCipher = new RIJ256(key, keyLen); break;
case ALGO_BLF: pCipher = new BLF(key, keyLen); break;
case ALGO_TWF: pCipher = new TWF(key, keyLen); break;
#if defined(_IPP_v51_)
case ALGO_RC4: pCipher = new RC4(key, keyLen); break;
#endif
}
// input and output buffers
const int bufSize = 256;
Ipp8u inpBuffer[bufSize];
Ipp8u outBuffer[bufSize];
int readLen;
int codeLen;
int writeLen;
//
// encode file
//
if(ENCRYPT==op) {
pCipher->EncryptInit(mode, iv, mparam);
pCipher->SetPadding(true);
while(!feof(inpFH)) {
// read input file
readLen = fread(inpBuffer, sizeof(Ipp8u), bufSize, inpFH);
// crypto operation
pCipher->EncryptUpdate(outBuffer, &codeLen, inpBuffer, readLen);
// write output file
writeLen = fwrite(outBuffer, sizeof(Ipp8u), codeLen, outFH);
}
pCipher->EncryptFinal(outBuffer, &codeLen);
writeLen = fwrite(outBuffer, sizeof(Ipp8u), codeLen, outFH);
}
//
// decode file
//
else {
pCipher->DecryptInit(mode, iv, mparam);
pCipher->SetPadding(true);
while(!feof(inpFH)) {
// read input file
readLen = fread(inpBuffer,sizeof(Ipp8u), bufSize, inpFH);
// crypto operation
pCipher->DecryptUpdate(outBuffer, &codeLen, inpBuffer, readLen);
// write output file
writeLen = fwrite(outBuffer, sizeof(Ipp8u), codeLen, outFH);
}
pCipher->DecryptFinal(outBuffer, &codeLen);
writeLen = fwrite(outBuffer, sizeof(Ipp8u), codeLen, outFH);
}
fclose(inpFH);
fclose(outFH);
#if defined( _OCF_ )
if(!useSWimplementation)
cdevClose();
#endif
delete pCipher;
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -