⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 des.h

📁 IBE是一种非对称密码技术
💻 H
字号:
/* Copyright 2003-2006, Voltage Security, all rights reserved.
 */

#include "vibecrypto.h"
#include "environment.h"
#include "base.h"
#include "libctx.h"
#include "algobj.h"
#include "keyobj.h"

#ifndef _DES_H
#define _DES_H

#ifdef __cplusplus
extern "C" {
#endif

/* Implements VEncryptInit.
 */
int VOLT_CALLING_CONV DESEncryptInit VOLT_PROTO_LIST ((
   VoltAlgorithmObject *algObj,
   VoltKeyObject *keyObj
));

/* Implements VEncryptUpdate.
 */
int VOLT_CALLING_CONV DESEncryptUpdate VOLT_PROTO_LIST ((
   VoltAlgorithmObject *algObj,
   VtRandomObject random,
   unsigned char *dataToEncrypt,
   unsigned int dataToEncryptLen,
   unsigned char *encryptedData
));

/* Implments VDecryptInit.
 */
int VOLT_CALLING_CONV DESDecryptInit VOLT_PROTO_LIST ((
   VoltAlgorithmObject *algObj,
   VoltKeyObject *keyObj
));

/* Implements VDecryptUpdate.
 */
int VOLT_CALLING_CONV DESDecryptUpdate VOLT_PROTO_LIST ((
   VoltAlgorithmObject *algObj,
   VtRandomObject random,
   unsigned char *dataToDecrypt,
   unsigned int dataToDecryptLen,
   unsigned char *decryptedData
));

/* Implements VCtxDestroy.
 */
void VOLT_CALLING_CONV DESAlgCtxDestroy VOLT_PROTO_LIST ((
   Pointer obj,
   Pointer ctx
));

/* The algCtx for DES looks like this.
 */
typedef struct
{
  union
  {
    unsigned char cblock[8];
    UInt32 deslong[2];
  } keyTable[16];
} VoltDesCtx;

/* Implements VCtxDestroy.
 */
void VOLT_CALLING_CONV DESKeyDataDestroy VOLT_PROTO_LIST ((
   Pointer obj,
   Pointer ctx
));

/* Initializes for encryption or decryption
 *
 * @param encryptFlag Tells whether initializing for encrypt or
 * decrypt. Must be one of the flags VOLT_DES_ENCRYPT or
 * VOLT_DES_DECRYPT.
 * @param keyData The key bytes (not a handle).
 * @param desCtx The context to initialize.
 * @return none
 */
void VOLT_CALLING_CONV DESInit VOLT_PROTO_LIST ((
   unsigned int encryptFlag,
   VtItem *keyData,
   VoltDesCtx *desCtx
));

/* The following are the possible values for the encryptFlag.
 */
#define VOLT_DES_ENCRYPT   1
#define VOLT_DES_DECRYPT   2

/* Encrypt one block of data.
 *
 * @param ctx The context (contains key table).
 * @param inBlock The block of input.
 * @param outBlock Where the encrypted block should go.
 * @return none.
 */
void VOLT_CALLING_CONV desEncryptBlock VOLT_PROTO_LIST ((
   VoltDesCtx *ctx,
   unsigned char *inBlock,
   unsigned char *outBlock
));

/* Decrypt one block of data.
 *
 * @param ctx The context (contains key table).
 * @param inBlock The block of input.
 * @param outBlock Where the decrypted block should go.
 * @return none.
 */
void VOLT_CALLING_CONV desDecryptBlock VOLT_PROTO_LIST ((
   VoltDesCtx *ctx,
   unsigned char *inBlock,
   unsigned char *outBlock
));

/* Set the parity bits in the data of the VtItem.
 * <p>This function does no argument checking, it is the responsibility
 * of the caller not to make mistakes.
 * <p>The key can be DES (8 bytes) or 3DES (24 bytes). The function
 * will simply set the parity bits in each byte given.
 *
 * @param keyData The VtItem containing the data and its length.
 * @return None.
 */
void VOLT_CALLING_CONV VoltSetDESParity VOLT_PROTO_LIST ((
   VtItem *keyData
));

/*************************************************************/
/* Triple DES                                                */
/*************************************************************/

/* Implements VEncryptInit.
 */
int VOLT_CALLING_CONV TripleDesEdeEncryptInit VOLT_PROTO_LIST ((
   VoltAlgorithmObject *algObj,
   VoltKeyObject *keyObj
));

/* Implements VEncryptUpdate.
 */
int VOLT_CALLING_CONV TripleDesEdeEncryptUpdate VOLT_PROTO_LIST ((
   VoltAlgorithmObject *algObj,
   VtRandomObject random,
   unsigned char *dataToEncrypt,
   unsigned int dataToEncryptLen,
   unsigned char *encryptedData
));

/* Implments VDecryptInit.
 */
int VOLT_CALLING_CONV TripleDesEdeDecryptInit VOLT_PROTO_LIST ((
   VoltAlgorithmObject *algObj,
   VoltKeyObject *keyObj
));

/* Implements VDecryptUpdate.
 */
int VOLT_CALLING_CONV TripleDesEdeDecryptUpdate VOLT_PROTO_LIST ((
   VoltAlgorithmObject *algObj,
   VtRandomObject random,
   unsigned char *dataToDecrypt,
   unsigned int dataToDecryptLen,
   unsigned char *decryptedData
));

/* Implements VCtxDestroy.
 */
void VOLT_CALLING_CONV TripleDESAlgCtxDestroy VOLT_PROTO_LIST ((
   Pointer obj,
   Pointer ctx
));

/* The algCtx for Triple DES looks like this.
 */
typedef struct
{
  VoltDesCtx des1;
  VoltDesCtx des2;
  VoltDesCtx des3;
  unsigned char temp[8];
} VoltTripleDesCtx;

/* Implements VCtxDestroy.
 */
void VOLT_CALLING_CONV TripleDESKeyDataDestroy VOLT_PROTO_LIST ((
   Pointer obj,
   Pointer ctx
));

#if VOLT_ENDIAN == VOLT_BIG_ENDIAN
#define DES_GET_UINT32(_buf,_value) VOLT_GET_BIG_ENDIAN_UINT32(_buf,_value)
#else
#define DES_GET_UINT32(_buf,_value) VOLT_GET_LITTLE_ENDIAN_UINT32(_buf,_value)
#endif

#if VOLT_ENDIAN == VOLT_BIG_ENDIAN
#define DES_SET_UINT32(_value,_buf) VOLT_SET_BIG_ENDIAN_UINT32(_value,_buf)
#else
#define DES_SET_UINT32(_value,_buf) VOLT_SET_LITTLE_ENDIAN_UINT32(_value,_buf)
#endif

#define ITERATIONS 16
#define HALF_ITERATIONS 8

#define PERM_OP(a,b,t,n,m) ((t)=((((a)>>(n))^(b))&(m)),\
                            (b)^=(t),\
                            (a)^=((t)<<(n)))
#define HPERM_OP(a,t,n,m) ((t)=((((a)<<(16-(n)))^(a))&(m)),\
                           (a)=(a)^(t)^(t>>(16-(n))))

/* Rotate a 32-bit value left by count.
 */
#define DES_ROTL(_value,_count) VOLT_UINT32_ROTL(_value,_count)
/* Rotate a 32-bit value right by count.
 */
#define DES_ROTR(_value,_count) VOLT_UINT32_ROTR(_value,_count)

#define IP(l,r) \
    { \
      UInt32 tt; \
      PERM_OP(r,l,tt, 4,0x0f0f0f0fL); \
      PERM_OP(l,r,tt,16,0x0000ffffL); \
      PERM_OP(r,l,tt, 2,0x33333333L); \
      PERM_OP(l,r,tt, 8,0x00ff00ffL); \
      PERM_OP(r,l,tt, 1,0x55555555L); \
    }

#define FP(l,r) \
    { \
      UInt32 tt; \
      PERM_OP(l,r,tt, 1,0x55555555L); \
      PERM_OP(r,l,tt, 8,0x00ff00ffL); \
      PERM_OP(l,r,tt, 2,0x33333333L); \
      PERM_OP(r,l,tt,16,0x0000ffffL); \
      PERM_OP(l,r,tt, 4,0x0f0f0f0fL); \
    }

#define LOAD_DATA_tmp(a,b,c,d,e,f) LOAD_DATA(a,b,c,d,e,f,g)
#define LOAD_DATA(R,S,u,t,E0,E1,tmp) \
    u=R^s[S  ]; \
    t=R^s[S+1]

#define D_ENCRYPT(LL,R,S) {\
                            LOAD_DATA_tmp(R,S,u,t,E0,E1); \
                            t = DES_ROTR(t,4); \
                            LL^=\
                              V_DES_SPtrans[0][(u>> 2L)&0x3f]^ \
                              V_DES_SPtrans[2][(u>>10L)&0x3f]^ \
                              V_DES_SPtrans[4][(u>>18L)&0x3f]^ \
                              V_DES_SPtrans[6][(u>>26L)&0x3f]^ \
                              V_DES_SPtrans[1][(t>> 2L)&0x3f]^ \
                              V_DES_SPtrans[3][(t>>10L)&0x3f]^ \
                              V_DES_SPtrans[5][(t>>18L)&0x3f]^ \
                              V_DES_SPtrans[7][(t>>26L)&0x3f]; }

#ifdef __cplusplus
}
#endif

#endif /* _DES_H */

⌨️ 快捷键说明

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