📄 cipher.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 "cipher.h"#include <string.h>//// Cipher//int Cipher::Init(CipherOperation op, CipherMode m, const Ipp8u* pIV, int parm){ status = ippStsNoErr; operation = op; mode = m; param = 0; padding = false; dataSgmntSize = algoBlockSize; switch (m) { case CFB: dataSgmntSize = parm; case CTR: param = parm; case ECB: case CBC: default: break; } if(pIV) memcpy(iv, pIV, algoBlockSize); ibufLen = 0; obufUsed = false; return 1;}int Cipher::EncryptUpdate(Ipp8u* pDst, int* pDstLen, const Ipp8u* pSrc, int srcLen){ *pDstLen = 0; // no input - no action if(!srcLen) return 1; // partial block is empty & input length is muptiple by data_block_size if(0==ibufLen && 0==srcLen%dataSgmntSize) { if( !doCipher(pDst, pSrc, srcLen) ) return 0; *pDstLen = srcLen; return 1; } // partial block isn't empty if(ibufLen) { // input length isn't enough to fill partial block if(dataSgmntSize > ibufLen+srcLen) { memcpy(ibuf+ibufLen, pSrc, srcLen); ibufLen += srcLen; return 1; } // input length is enough to fill partial block (and probably even more) else { int n = dataSgmntSize-ibufLen; memcpy(ibuf+ibufLen, pSrc, n); if( !doCipher(pDst, ibuf, dataSgmntSize) ) { return 0; } else { pSrc += n; srcLen -= n; pDst += dataSgmntSize; *pDstLen += dataSgmntSize; ibufLen = 0; } } } // input length (probably) isn't multiple by data_block_size ibufLen = srcLen%dataSgmntSize; srcLen -= ibufLen; if(srcLen) { if( !doCipher(pDst, pSrc, srcLen) ) return 0; *pDstLen += srcLen; } if(ibufLen) memcpy(ibuf, pSrc+srcLen, ibufLen); return 1;}void Cipher::AddPadding(void){ // use PKCS#7 padding convention Ipp8u filler = (Ipp8u)(dataSgmntSize-ibufLen); for(int i=ibufLen; i<dataSgmntSize; i++) ibuf[i] = filler; ibufLen = dataSgmntSize;}int Cipher::EncryptFinal(Ipp8u* pDst, int* pDstLen){ *pDstLen = 0; // 1==data_block_size - padding is meaningless (no purpose to serve) if(1==dataSgmntSize) return 1; // if padding is "on" use any padding convention if(padding) AddPadding(); // and try to encrypt non- empty block // note: OCF and CTR modes permit to encrypt partial block if( !ibufLen || doCipher(pDst, ibuf, ibufLen) ) { *pDstLen = ibufLen; ibufLen = 0; return 1; } return 0;}int Cipher::DecryptUpdate(Ipp8u* pDst, int* pDstLen, const Ipp8u* pSrc, int srcLen){ *pDstLen = 0; // no input - no action if(!srcLen) return 1; // if padding is "off" just decrypt input if(!padding) return EncryptUpdate(pDst, pDstLen, pSrc, srcLen); // // paddind is "on" // // note: we dont know when padding was switched "on" // // at first, copy last block of previous DecryptUpdate() from the obuf if(obufUsed) { memcpy(pDst, obuf, dataSgmntSize); pDst += dataSgmntSize; *pDstLen = dataSgmntSize; obufUsed = false; } // and then, do decryption for the current request int tmpLen; if( !EncryptUpdate(pDst, &tmpLen, pSrc, srcLen) ) return 0; // multiple of data_block_size has decrypted - save the last decrypted block if(1<dataSgmntSize && !ibufLen) { tmpLen -= dataSgmntSize; memcpy(obuf, pDst+tmpLen, dataSgmntSize); obufUsed = true; } *pDstLen += tmpLen; return 1;}int Cipher::TrimPadding(void){ Ipp8u filler = obuf[dataSgmntSize-1]; if(filler>dataSgmntSize) return -1; // bad encrypt for(int n=0; n<filler; n++) if(obuf[dataSgmntSize-1-n] != filler) return -1; return dataSgmntSize-filler;}int Cipher::DecryptFinal(Ipp8u* pDst, int* pDstLen){ *pDstLen = 0; // 1==data_block_size - padding is meaningless (no purpose to serve) if(1==dataSgmntSize) return 1; // if padding is "off" returns depend on infill of partial buffer: if(!padding) { // try to encrypt non- empty block // note: OCF and CTR modes permit to encrypt partial block if( !ibufLen || doCipher(pDst, ibuf, ibufLen) ) { *pDstLen = ibufLen; ibufLen = 0; return 1; } return 0; } if(ibufLen || !obufUsed) return 0; // wrong final block length int remLen = TrimPadding(); if(0>remLen) return 0; memcpy(pDst, obuf, remLen); *pDstLen = remLen; return 1;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -