📄 desimpl.c
字号:
0x00820000L, 0x00020080L, 0x20020080L, 0x20800000L, 0x00000080L, 0x20820000L, 0x00820080L, 0x00000000L, 0x20000000L, 0x20800080L, 0x00020000L, 0x00820080L, }};int DESEncryptInit ( VoltAlgorithmObject *algObj, VoltKeyObject *keyObj ){ int status; VtItem *keyData; VoltCipherClassCtx *cipherCtx = (VoltCipherClassCtx *)(algObj->classCtx); VoltBlockCipherCtx *blockCtx = (VoltBlockCipherCtx *)(cipherCtx->localCipherCtx); VoltDesCtx *desCtx = (VoltDesCtx *)(blockCtx->algCtx); VOLT_DECLARE_ERROR_TYPE (errorType) VOLT_DECLARE_FNCT_LINE (fnctLine) do { /* Make sure the key matches the algorithm object. */ VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY) VOLT_SET_FNCT_LINE (fnctLine) status = VT_ERROR_INVALID_KEY_OBJ; if ((keyObj->keyType & VOLT_KEY_TYPE_MASK_SYM_ALG) != VOLT_KEY_ALG_DES) break; /* We need data (status is still set to VT_ERROR_INVALID_KEY_OBJ if we * can't get the data out). */ keyData = (VtItem *)(keyObj->keyData); if ((keyObj->keyType & VOLT_KEY_TYPE_MASK_DATA) != VOLT_KEY_TYPE_DATA) { VOLT_SET_FNCT_LINE (fnctLine) if (keyObj->GetKeyData == (VGetKeyData)0) break; VOLT_SET_ERROR_TYPE (errorType, 0) VOLT_SET_FNCT_LINE (fnctLine) status = keyObj->GetKeyData ((VtKeyObject)keyObj, (Pointer *)&keyData); if (status != 0) break; } DESInit (VOLT_DES_ENCRYPT, keyData, desCtx); status = 0; } while (0); VOLT_LOG_ERROR_COMPARE ( status, algObj->voltObject.libraryCtx, status, errorType, fnctLine, "DESEncryptInit", (char *)0) return (status);}int DESEncryptUpdate ( VoltAlgorithmObject *algObj, VtRandomObject random, unsigned char *dataToEncrypt, unsigned int dataToEncryptLen, unsigned char *encryptedData ){ VoltCipherClassCtx *cipherCtx = (VoltCipherClassCtx *)(algObj->classCtx); VoltBlockCipherCtx *blockCtx = (VoltBlockCipherCtx *)(cipherCtx->localCipherCtx); VoltDesCtx *desCtx = (VoltDesCtx *)(blockCtx->algCtx); /* So long as we have blocks to encrypt, call the EncryptBlock * routine. */ while (dataToEncryptLen >= 8) { desEncryptBlock (desCtx, dataToEncrypt, encryptedData); dataToEncryptLen -= 8; dataToEncrypt += 8; encryptedData += 8; } return (0);}void desEncryptBlock ( VoltDesCtx *ctx, unsigned char *inBlock, unsigned char *outBlock ){ UInt32 l, r, t, u; UInt32 *s; GET_UINT32( r, inBlock, 0 ); GET_UINT32( l, inBlock, 4 ); IP(r,l); /* Things have been modified so that the initial rotate is * done outside the loop. This required the * DES_SPtrans values in sp.h to be rotated 1 bit to the right. * One perl script later and things have a 5% speed up on a sparc2. * Thanks to Richard Outerbridge <71755.204@CompuServe.COM> * for pointing this out. */ /* clear the top bits on machines with 8byte longs */ /* shift left by 2 */ r = DES_ROTR(r,29) & 0xffffffffL; l = DES_ROTR(l,29) & 0xffffffffL; s = ctx->keyTable->deslong; D_ENCRYPT(l,r, 0); /* 1 */ D_ENCRYPT(r,l, 2); /* 2 */ D_ENCRYPT(l,r, 4); /* 3 */ D_ENCRYPT(r,l, 6); /* 4 */ D_ENCRYPT(l,r, 8); /* 5 */ D_ENCRYPT(r,l,10); /* 6 */ D_ENCRYPT(l,r,12); /* 7 */ D_ENCRYPT(r,l,14); /* 8 */ D_ENCRYPT(l,r,16); /* 9 */ D_ENCRYPT(r,l,18); /* 10 */ D_ENCRYPT(l,r,20); /* 11 */ D_ENCRYPT(r,l,22); /* 12 */ D_ENCRYPT(l,r,24); /* 13 */ D_ENCRYPT(r,l,26); /* 14 */ D_ENCRYPT(l,r,28); /* 15 */ D_ENCRYPT(r,l,30); /* 16 */ /* rotate and clear the top bits on machines with 8byte longs */ l = DES_ROTR(l,3) & 0xffffffffL; r = DES_ROTR(r,3) & 0xffffffffL; FP(r,l); PUT_UINT32( l, outBlock, 0 ); PUT_UINT32( r, outBlock, 4 ); l=r=t=u=0;}int DESDecryptInit ( VoltAlgorithmObject *algObj, VoltKeyObject *keyObj ){ int status; VtItem *keyData; VoltCipherClassCtx *cipherCtx = (VoltCipherClassCtx *)(algObj->classCtx); VoltBlockCipherCtx *blockCtx = (VoltBlockCipherCtx *)(cipherCtx->localCipherCtx); VoltDesCtx *desCtx = (VoltDesCtx *)(blockCtx->algCtx); VOLT_DECLARE_ERROR_TYPE (errorType) VOLT_DECLARE_FNCT_LINE (fnctLine) do { /* Make sure the key matches the algorithm object. */ VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY) VOLT_SET_FNCT_LINE (fnctLine) status = VT_ERROR_INVALID_KEY_OBJ; if ((keyObj->keyType & VOLT_KEY_TYPE_MASK_SYM_ALG) != VOLT_KEY_ALG_DES) break; /* We need data (status is still set to VT_ERROR_INVALID_KEY_OBJ if we * can't get the data out). */ keyData = (VtItem *)(keyObj->keyData); if ((keyObj->keyType & VOLT_KEY_TYPE_MASK_DATA) != VOLT_KEY_TYPE_DATA) { VOLT_SET_FNCT_LINE (fnctLine) if (keyObj->GetKeyData == (VGetKeyData)0) break; VOLT_SET_ERROR_TYPE (errorType, 0) VOLT_SET_FNCT_LINE (fnctLine) status = keyObj->GetKeyData ((VtKeyObject)keyObj, (Pointer *)&keyData); if (status != 0) break; } DESInit (VOLT_DES_DECRYPT, keyData, desCtx); status = 0; } while (0); VOLT_LOG_ERROR_COMPARE ( status, algObj->voltObject.libraryCtx, status, errorType, fnctLine, "DESDecryptInit", (char *)0) return (status);}int DESDecryptUpdate ( VoltAlgorithmObject *algObj, VtRandomObject random, unsigned char *dataToDecrypt, unsigned int dataToDecryptLen, unsigned char *decryptedData ){ VoltCipherClassCtx *cipherCtx = (VoltCipherClassCtx *)(algObj->classCtx); VoltBlockCipherCtx *blockCtx = (VoltBlockCipherCtx *)(cipherCtx->localCipherCtx); VoltDesCtx *desCtx = (VoltDesCtx *)(blockCtx->algCtx); /* So long as we have blocks to decrypt, call the DecryptBlock * routine. */ while (dataToDecryptLen >= 8) { desDecryptBlock (desCtx, dataToDecrypt, decryptedData); dataToDecryptLen -= 8; dataToDecrypt += 8; decryptedData += 8; } return (0);}void desDecryptBlock ( VoltDesCtx *ctx, unsigned char *inBlock, unsigned char *outBlock ){ UInt32 l, r, t, u; UInt32 *s; GET_UINT32( r, inBlock, 0 ); GET_UINT32( l, inBlock, 4 ); IP(r,l); /* Things have been modified so that the initial rotate is * done outside the loop. This required the * DES_SPtrans values in sp.h to be rotated 1 bit to the right. * One perl script later and things have a 5% speed up on a sparc2. * Thanks to Richard Outerbridge <71755.204@CompuServe.COM> * for pointing this out. */ /* clear the top bits on machines with 8byte longs */ /* shift left by 2 */ r = DES_ROTR(r,29) & 0xffffffffL; l = DES_ROTR(l,29) & 0xffffffffL; s = ctx->keyTable->deslong; D_ENCRYPT(l,r,30); /* 16 */ D_ENCRYPT(r,l,28); /* 15 */ D_ENCRYPT(l,r,26); /* 14 */ D_ENCRYPT(r,l,24); /* 13 */ D_ENCRYPT(l,r,22); /* 12 */ D_ENCRYPT(r,l,20); /* 11 */ D_ENCRYPT(l,r,18); /* 10 */ D_ENCRYPT(r,l,16); /* 9 */ D_ENCRYPT(l,r,14); /* 8 */ D_ENCRYPT(r,l,12); /* 7 */ D_ENCRYPT(l,r,10); /* 6 */ D_ENCRYPT(r,l, 8); /* 5 */ D_ENCRYPT(l,r, 6); /* 4 */ D_ENCRYPT(r,l, 4); /* 3 */ D_ENCRYPT(l,r, 2); /* 2 */ D_ENCRYPT(r,l, 0); /* 1 */ /* rotate and clear the top bits on machines with 8byte longs */ l = DES_ROTR(l,3) & 0xffffffffL; r = DES_ROTR(r,3) & 0xffffffffL; FP(r,l); PUT_UINT32( l, outBlock, 0 ); PUT_UINT32( r, outBlock, 4 ); l=r=t=u=0;}void DESInit ( unsigned int encryptFlag, VtItem *keyData, VoltDesCtx *desCtx ){ int index; int shifts2[16]= { 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0 }; UInt32 c, d, t, s, t2; UInt32 *k; GET_UINT32(c, keyData->data, 0); GET_UINT32(d, keyData->data, 4); k = &(desCtx->keyTable->deslong[0]); /* do PC1 in 47 simple operations :-) * Thanks to John Fletcher (john_fletcher@lccmail.ocf.llnl.gov) * for the inspiration. :-) */ PERM_OP (d,c,t,4,0x0f0f0f0fL); HPERM_OP(c,t,-2,0xcccc0000L); HPERM_OP(d,t,-2,0xcccc0000L); PERM_OP (d,c,t,1,0x55555555L); PERM_OP (c,d,t,8,0x00ff00ffL); PERM_OP (d,c,t,1,0x55555555L); d = (((d&0x000000ffL)<<16L)| (d&0x0000ff00L) | ((d&0x00ff0000L)>>16L) |((c&0xf0000000L)>>4L)); c &= 0x0fffffffL; for (index = 0; index < ITERATIONS; ++index) { if (shifts2[index]) { c = ((c>>2L)|(c<<26L)); d = ((d>>2L)|(d<<26L)); } else { c = ((c>>1L)|(c<<27L)); d = ((d>>1L)|(d<<27L)); } c &= 0x0fffffffL; d &= 0x0fffffffL; s = des_skb[0][ (c )&0x3f ] | des_skb[1][((c>> 6L)&0x03)|((c>> 7L)&0x3c)] | des_skb[2][((c>>13L)&0x0f)|((c>>14L)&0x30)] | des_skb[3][((c>>20L)&0x01)|((c>>21L)&0x06) | ((c>>22L)&0x38)]; t = des_skb[4][ (d )&0x3f ] | des_skb[5][((d>> 7L)&0x03)|((d>> 8L)&0x3c)] | des_skb[6][ (d>>15L)&0x3f ] | des_skb[7][((d>>21L)&0x0f)|((d>>22L)&0x30)]; /* table contained 0213 4657 */ t2 = ((t<<16L)|(s&0x0000ffffL))&0xffffffffL; *(k++) = DES_ROTR (t2,30) & 0xffffffffL; t2 = ((s>>16L)|(t&0xffff0000L)); *(k++) = DES_ROTR (t2,26) & 0xffffffffL; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -