📄 ocfcp.c
字号:
/*//// 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 <string.h>#include "ocfcp.h"/*// map cipher algorithm into OCF cipher*/u32 ocfCipher[] = { CRYPTO_DES_CBC, /* ALGO_DES */ CRYPTO_3DES_CBC, /* ALGO_TDES */ CRYPTO_AES_CBC, /* ALGO_AES (ALGO_RIJ128) */ ALGO_UNKNOWN, /* ALGO_RIJ192 disabled */ ALGO_UNKNOWN, /* ALGO_RIJ256 disabled */ BLF_BLKLEN, /* ALGO_BLF*/ ALGO_UNKNOWN, /* ALGO_TWF disabled */ CRYPTO_ARC4, /* ALGO_RC4 */};/*// ocfCipherID ocf cipher ID// ocfCipherOperation ocf crypto operation (COP_ENCRYPT ot COP_DECRYPT)*/int ocfCipherID(AlgoName algo){ return ocfCipher[algo];}int ocfCipherOperation(CipherOperation op){ return ENCRYPT==op? COP_ENCRYPT : COP_DECRYPT;}/*// Returns size of cipher data block size */int ocfCipherBlockSizeByID(int id){ int n; int blkSize; for(n=0, blkSize=0; n<sizeof(ocfCipher)/sizeof(ocfCipher[0]); n++) { if(ocfCipher[n]==id) { blkSize = CipherBlockSize(n); break; } } return blkSize;}/* Crypro Device descriptor */int cdevFD = -1;/*// Open and Close crypto device*/int cdevOpen(void){ /* try to open HW device */ if(-1 == cdevFD) { cdevFD= open("/dev/crypto", O_RDWR); if(-1 != cdevFD) /* set the close-on-exec flag */ fcntl(cdevFD, F_SETFD, 1); } return cdevFD;}void cdevClose(void){ if(-1 != cdevFD) close(cdevFD); cdevFD= -1;}/*// Clone and Close cloned crypto device descriptor*/int cdevClone(void){ int fd; return -1 == ioctl(cdevFD, CRIOGET, &fd)? -1 : fd;}void cdevCloneClose(int fd){ close(fd);}/*// Occupy and Vacate session.//// Returns: 1(success) / 0(unsuccess)*/int ocfOccupyCipherSession(int fd, OCFSession* pSess, AlgoName algo, const u8* pKey, int keyLen){ memset(pSess, 0, sizeof(OCFSession)); pSess->cipher = ocfCipherID(algo); pSess->key = (u8*)pKey; pSess->keylen = keyLen; return OCF_ERROR != ioctl(fd, CIOCGSESSION, pSess);}int ocfVacateCipherSession(int fd, OCFSession* pSess){ return OCF_ERROR != ioctl(fd, CIOCFSESSION, &(pSess->ses));}/*// ocfCipherEnabled//// Returns: 1(success) / 0(unsuccess)*/int ocfCipherEnabled(int fd, AlgoName algo){ /* try to occupy/vacate session */ OCFSession ses; u8 key[MAX_BLKLEN]; return ocfOccupyCipherSession(fd, &ses, algo, key, CipherKeyLenMin(algo)) && ocfVacateCipherSession(fd, &ses);}/*// ocfCipherECB//// [inp] fd - crypto dev descriptin// [inp] pSes - pointer to the occupied serrion// [inp] op - operation {ENCODE|DECODE}// [out] pDst - pointer to the octet ciphertext// [inp] pSrc - pointer to the octet plaintext// [int] srcLen- length of the plaintext (in octets)//// Returns: 1(success) / 0(unsuccess)*/int ocfCipherECB(int fd, OCFSession* pSes, CipherOperation op, u8* pDst, const u8* pSrc, int srcLen){ int blockSize = ocfCipherBlockSizeByID(pSes->cipher); /* test input parameters */ if(srcLen % blockSize) return 0; { int retCode = OCF_SUCCESS; u8 IV[MAX_BLKLEN]; struct crypt_op cop; memset(&cop, 0, sizeof(cop)); memset(IV, 0, blockSize); /* perform operations */ cop.ses = pSes->ses; cop.op = ocfCipherOperation(op); cop.len = blockSize; cop.iv = IV; while(srcLen>=blockSize && retCode!=OCF_ERROR) { cop.src = (u8*)pSrc; cop.dst = pDst; retCode = ioctl(fd, CIOCCRYPT, &cop); pSrc += blockSize; pDst += blockSize; srcLen -= blockSize; } return retCode!=OCF_ERROR; }}/*// ocfCipherCBC//// [inp] fd - crypto dev descriptin// [inp] pSes - pointer to the occupied serrion// [inp] op - operation {ENCODE|DECODE}// [inp] pIV - init vector// [out] pDst - pointer to the octet ciphertext// [inp] pSrc - pointer to the octet plaintext// [int] srcLen- length of the plaintext (in octets)//// Returns: 1(success) / 0(unsuccess)*/int ocfCipherCBC(int fd, OCFSession* pSes, CipherOperation op, const u8* pIV, u8* pDst, const u8* pSrc, int srcLen){ int blockSize = ocfCipherBlockSizeByID(pSes->cipher); /* test input parameters */ if(srcLen % blockSize) return 0; { int retCode = OCF_SUCCESS; u8 IV[MAX_BLKLEN]; struct crypt_op cop; memset(&cop, 0, sizeof(cop)); memset(IV, 0, blockSize); /* perform operations */ cop.ses = pSes->ses; cop.op = ocfCipherOperation(op); cop.iv = (u8*)pIV; cop.dst = pDst; cop.src = (u8*)pSrc; cop.len = srcLen; retCode = ioctl(fd, CIOCCRYPT, &cop); return retCode!=OCF_ERROR; }}/*// ocfCipherCFB//// [inp] fd - crypto dev descriptin// [inp] pSes - pointer to the occupied serrion// [inp] op - operation {ENCODE|DECODE}// [inp] pIV - init vector// [out] pDst - pointer to the octet ciphertext// [inp] pSrc - pointer to the octet plaintext// [int] srcLen- length of the plaintext (in octets)//// Returns: 1(success) / 0(unsuccess)*/int ocfCipherCFB(int fd, OCFSession* pSes, CipherOperation op, const u8* pIV, int cfbSize, u8* pDst, const u8* pSrc, int srcLen){ int blockSize = ocfCipherBlockSizeByID(pSes->cipher); /* test input parameters */ if(srcLen % cfbSize) return 0; { int retCode = OCF_SUCCESS; u8 IV[MAX_BLKLEN]; u8 OUT[MAX_BLKLEN]; u8 INP[2*MAX_BLKLEN]; struct crypt_op cop; memset(IV, 0, blockSize); memcpy(INP, pIV, blockSize); /* perform operations */ memset(&cop, 0, sizeof(cop)); cop.ses = pSes->ses; cop.op = COP_ENCRYPT; cop.iv = IV; cop.src = INP; cop.dst = OUT; cop.len = blockSize; if(ENCRYPT==op) { while(srcLen>=cfbSize && retCode!=OCF_ERROR) { int n; /* encryption */ retCode = ioctl(fd, CIOCCRYPT, &cop); /* store output and put feedback into the input buffer (tmpInp) */ for(n=0; n<cfbSize; n++) { pDst[n] = (u8)( OUT[n] ^ pSrc[n] ); INP[blockSize+n] = pDst[n]; } /* shift input buffer (inp) for the next CFB operation */ memcpy(INP, INP+cfbSize, blockSize); pSrc += cfbSize; pDst += cfbSize; srcLen -= cfbSize; } } else { while(srcLen>=cfbSize && retCode!=OCF_ERROR) { int n; /* decryption */ retCode = ioctl(fd, CIOCCRYPT, &cop); /* store output and put feedback into the input buffer (tmpInp) */ for(n=0; n<cfbSize; n++) { INP[blockSize+n] = pSrc[n]; pDst[n] = (u8)( OUT[n] ^ pSrc[n] ); } /* shift input buffer (inp) for the next CFB operation */ memcpy(INP, INP+cfbSize, blockSize); pSrc += cfbSize; pDst += cfbSize; srcLen -= cfbSize; } } return retCode!=OCF_ERROR; }}/*// ocfCipherCTR//// [inp] fd - crypto dev descriptin// [inp] pSes - pointer to the occupied serrion// [inp] op - operation {ENCODE|DECODE}// [inp] pIV - init vector// [inp] ctrSize - coutnrt length (in bits)// [out] pDst - pointer to the octet ciphertext// [inp] pSrc - pointer to the octet plaintext// [int] srcLen- length of the plaintext (in octets)//// Returns: 1(success) / 0(unsuccess)*/#define BITS2WORD8_SIZE(x) (((x)+ 7)>>3)staticinline void XorBlock(u8* pDst, const u8* pSrc1, const u8* pSrc2, int len){ int n; for(n=0; n<len; n++) pDst[n] = (u8)( pSrc1[n]^pSrc2[n] );}int ocfCipherCTR(int fd, OCFSession* pSes, CipherOperation op, u8* pIV, int ctrSize, u8* pDst, const u8* pSrc, int srcLen){ int blockSize = ocfCipherBlockSizeByID(pSes->cipher); int retCode = OCF_SUCCESS; struct crypt_op cop; u8 IV[MAX_BLKLEN]; u8 inpBlk[MAX_BLKLEN]; u8 outBlk[MAX_BLKLEN]; memcpy(inpBlk, pIV, blockSize); memset(IV, 0, blockSize); memset(&cop, 0, sizeof(cop)); /* perform operations */ cop.ses = pSes->ses; cop.op = COP_ENCRYPT; cop.len = blockSize; cop.iv = IV; /* do cipher block-by-block */ while(srcLen && retCode!=OCF_ERROR) { int locLen = srcLen<blockSize? srcLen : blockSize; /* encrypt counter block */ cop.src = inpBlk; cop.dst = outBlk; retCode = ioctl(fd, CIOCCRYPT, &cop); /* compute ciphertext block */ XorBlock(pDst, pSrc, outBlk, locLen); pSrc += locLen; pDst += locLen; srcLen -= locLen; /* encrement counter block */ StdIncrement(inpBlk, blockSize*8, ctrSize); } if(retCode!=OCF_ERROR) memcpy(pIV, inpBlk, blockSize); return retCode!=OCF_ERROR;}#endif /* _OCF_ */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -