ocf_cipher.cpp

来自「这是在PCA下的基于IPP库示例代码例子,在网上下了IPP的库之后,设置相关参数」· C++ 代码 · 共 106 行

CPP
106
字号
/*////               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.//*/#if defined( _OCF_ )#include "ocf_cipher.h"#include <string.h>////////////////////////////////////////////////////// OCF cipher//OCFCipher::OCFCipher(AlgoName a, const u8* pKey, int keyLen){   algo = a;   algoBlockSize = CipherBlockSize(a);      // get clone of crypto dev descriptor   fd = cdevClone();      if(-1 != fd) {      int isCipher = ocfCipherEnabled(fd, a);      int isSession = ocfOccupyCipherSession(fd, &ses, a, pKey, keyLen);      error = !isCipher || !isSession;   }   else      error = 1;}void OCFCipher::ResetKey(const u8* pKey, int keyLen){   error = !ocfVacateCipherSession(fd, &ses)        || !ocfOccupyCipherSession(fd, &ses, algo, pKey, keyLen);}OCFCipher::~OCFCipher(){   ocfVacateCipherSession(fd, &ses);   cdevCloneClose(fd);}int OCFCipher::doCipher(u8* pDst, const u8* pSrc, int srcLen){   Ipp8u newIV[MAX_BLKLEN];   switch (mode) {      case ECB:         error = !ocfCipherECB(fd, &ses, operation, pDst, pSrc, srcLen);         break;      case CBC:         if(DECRYPT==operation)            memcpy(newIV, pSrc+srcLen-algoBlockSize, algoBlockSize);                  error = !ocfCipherCBC(fd, &ses, operation, iv, pDst, pSrc, srcLen);                     if(ENCRYPT==operation)            memcpy(newIV, pDst+srcLen-algoBlockSize, algoBlockSize);                     if(!error)            memcpy(iv, newIV, algoBlockSize);         break;      case CFB:         if(DECRYPT==operation) {            if(srcLen<algoBlockSize) {               memcpy(newIV, iv+srcLen, algoBlockSize-srcLen);               memcpy(newIV+algoBlockSize-srcLen, pSrc, srcLen);            }            else               memcpy(newIV, pSrc+srcLen-algoBlockSize, algoBlockSize);         }                  error = !ocfCipherCFB(fd, &ses, operation, iv, param, pDst, pSrc, srcLen);                     if(ENCRYPT==operation) {            if(srcLen<algoBlockSize) {               memcpy(newIV, iv+srcLen, algoBlockSize-srcLen);               memcpy(newIV+algoBlockSize-srcLen, pDst, srcLen);            }            else               memcpy(newIV, pDst+srcLen-algoBlockSize, algoBlockSize);         }                     if(!error)            memcpy(iv, newIV, algoBlockSize);         break;      case CTR:         error = !ocfCipherCTR(fd, &ses, operation, iv, param, pDst, pSrc, srcLen);         break;      default:         error = 1; // unsupported mode         break;   }   return !error;}#endif // _OCF_

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?