891 lines · 31.0 KB · C
RawDownload
/* ---- SYMMETRIC KEY STUFF ----- * * We put each of the ciphers scheduled keys in their own structs then we put all of  * the key formats in one union.  This makes the function prototypes easier to use. */#ifdef LTC_BLOWFISHstruct blowfish_key {   ulong32 S[4][256];   ulong32 K[18];};#endif #ifdef LTC_RC5struct rc5_key {   int rounds;   ulong32 K[50];};#endif #ifdef LTC_RC6struct rc6_key {   ulong32 K[44];};#endif #ifdef LTC_SAFERPstruct saferp_key {   unsigned char K[33][16];   long rounds;};#endif #ifdef LTC_RIJNDAELstruct rijndael_key {   ulong32 eK[60], dK[60];   int Nr;};#endif #ifdef LTC_KSEEDstruct kseed_key {    ulong32 K[32], dK[32];};#endif #ifdef LTC_KASUMIstruct kasumi_key {    ulong32 KLi1[8], KLi2[8],            KOi1[8], KOi2[8], KOi3[8],            KIi1[8], KIi2[8], KIi3[8];};#endif #ifdef LTC_XTEAstruct xtea_key {   unsigned long A[32], B[32];};#endif #ifdef LTC_TWOFISH#ifndef LTC_TWOFISH_SMALL   struct twofish_key {      ulong32 S[4][256], K[40];   };#else   struct twofish_key {      ulong32 K[40];      unsigned char S[32], start;   };#endif#endif #ifdef LTC_SAFER#define LTC_SAFER_K64_DEFAULT_NOF_ROUNDS     6#define LTC_SAFER_K128_DEFAULT_NOF_ROUNDS   10#define LTC_SAFER_SK64_DEFAULT_NOF_ROUNDS    8#define LTC_SAFER_SK128_DEFAULT_NOF_ROUNDS  10#define LTC_SAFER_MAX_NOF_ROUNDS            13#define LTC_SAFER_BLOCK_LEN                  8#define LTC_SAFER_KEY_LEN     (1 + LTC_SAFER_BLOCK_LEN * (1 + 2 * LTC_SAFER_MAX_NOF_ROUNDS))typedef unsigned char safer_block_t[LTC_SAFER_BLOCK_LEN];typedef unsigned char safer_key_t[LTC_SAFER_KEY_LEN];struct safer_key { safer_key_t key; };#endif #ifdef LTC_RC2struct rc2_key { unsigned xkey[64]; };#endif #ifdef LTC_DESstruct des_key {    ulong32 ek[32], dk[32];}; struct des3_key {    ulong32 ek[3][32], dk[3][32];};#endif #ifdef LTC_CAST5struct cast5_key {    ulong32 K[32], keylen;};#endif #ifdef LTC_NOEKEONstruct noekeon_key {    ulong32 K[4], dK[4];};#endif #ifdef LTC_SKIPJACK struct skipjack_key {    unsigned char key[10];};#endif #ifdef LTC_KHAZADstruct khazad_key {   ulong64 roundKeyEnc[8 + 1];    ulong64 roundKeyDec[8 + 1]; };#endif #ifdef LTC_ANUBISstruct anubis_key {    int keyBits;    int R;    ulong32 roundKeyEnc[18 + 1][4];    ulong32 roundKeyDec[18 + 1][4]; }; #endif #ifdef LTC_MULTI2struct multi2_key {    int N;    ulong32 uk[8];};#endif typedef union Symmetric_key {#ifdef LTC_DES   struct des_key des;   struct des3_key des3;#endif#ifdef LTC_RC2   struct rc2_key rc2;#endif#ifdef LTC_SAFER   struct safer_key safer;#endif#ifdef LTC_TWOFISH   struct twofish_key  twofish;#endif#ifdef LTC_BLOWFISH   struct blowfish_key blowfish;#endif#ifdef LTC_RC5   struct rc5_key      rc5;#endif#ifdef LTC_RC6   struct rc6_key      rc6;#endif#ifdef LTC_SAFERP   struct saferp_key   saferp;#endif#ifdef LTC_RIJNDAEL   struct rijndael_key rijndael;#endif#ifdef LTC_XTEA   struct xtea_key     xtea;#endif#ifdef LTC_CAST5   struct cast5_key    cast5;#endif#ifdef LTC_NOEKEON   struct noekeon_key  noekeon;#endif   #ifdef LTC_SKIPJACK   struct skipjack_key skipjack;#endif#ifdef LTC_KHAZAD   struct khazad_key   khazad;#endif#ifdef LTC_ANUBIS   struct anubis_key   anubis;#endif#ifdef LTC_KSEED   struct kseed_key    kseed;#endif#ifdef LTC_KASUMI   struct kasumi_key   kasumi;#endif  #ifdef LTC_MULTI2   struct multi2_key   multi2;#endif   void   *data;} symmetric_key; #ifdef LTC_ECB_MODE/** A block cipher ECB structure */typedef struct {   /** The index of the cipher chosen */   int                 cipher,    /** The block size of the given cipher */                       blocklen;   /** The scheduled key */                          symmetric_key       key;} symmetric_ECB;#endif #ifdef LTC_CFB_MODE/** A block cipher CFB structure */typedef struct {   /** The index of the cipher chosen */   int                 cipher,    /** The block size of the given cipher */                                               blocklen,    /** The padding offset */                       padlen;   /** The current IV */   unsigned char       IV[MAXBLOCKSIZE],    /** The pad used to encrypt/decrypt */                        pad[MAXBLOCKSIZE];   /** The scheduled key */   symmetric_key       key;} symmetric_CFB;#endif #ifdef LTC_OFB_MODE/** A block cipher OFB structure */typedef struct {   /** The index of the cipher chosen */   int                 cipher,    /** The block size of the given cipher */                                               blocklen,    /** The padding offset */                       padlen;   /** The current IV */   unsigned char       IV[MAXBLOCKSIZE];   /** The scheduled key */   symmetric_key       key;} symmetric_OFB;#endif #ifdef LTC_CBC_MODE/** A block cipher CBC structure */typedef struct {   /** The index of the cipher chosen */   int                 cipher,    /** The block size of the given cipher */                                               blocklen;   /** The current IV */   unsigned char       IV[MAXBLOCKSIZE];   /** The scheduled key */   symmetric_key       key;} symmetric_CBC;#endif  #ifdef LTC_CTR_MODE/** A block cipher CTR structure */typedef struct {   /** The index of the cipher chosen */   int                 cipher,   /** The block size of the given cipher */                                               blocklen,    /** The padding offset */                       padlen,    /** The mode (endianess) of the CTR, 0==little, 1==big */                       mode,   /** counter width */                       ctrlen;    /** The counter */                          unsigned char       ctr[MAXBLOCKSIZE],    /** The pad used to encrypt/decrypt */                                              pad[MAXBLOCKSIZE];   /** The scheduled key */   symmetric_key       key;} symmetric_CTR;#endif  #ifdef LTC_LRW_MODE/** A LRW structure */typedef struct {    /** The index of the cipher chosen (must be a 128-bit block cipher) */    int               cipher;     /** The current IV */    unsigned char     IV[16],     /** the tweak key */                      tweak[16],     /** The current pad, it's the product of the first 15 bytes against the tweak key */                      pad[16];     /** The scheduled symmetric key */    symmetric_key     key; #ifdef LRW_TABLES    /** The pre-computed multiplication table */    unsigned char     PC[16][256][16];#endif} symmetric_LRW;#endif #ifdef LTC_F8_MODE/** A block cipher F8 structure */typedef struct {   /** The index of the cipher chosen */   int                 cipher,    /** The block size of the given cipher */                                               blocklen,    /** The padding offset */                       padlen;   /** The current IV */   unsigned char       IV[MAXBLOCKSIZE],                       MIV[MAXBLOCKSIZE];   /** Current block count */   ulong32             blockcnt;   /** The scheduled key */   symmetric_key       key;} symmetric_F8;#endif  /** cipher descriptor table, last entry has "name == NULL" to mark the end of table */extern struct ltc_cipher_descriptor {   /** name of cipher */   char *name;   /** internal ID */   unsigned char ID;   /** min keysize (octets) */   int  min_key_length,    /** max keysize (octets) */        max_key_length,    /** block size (octets) */        block_length,    /** default number of rounds */        default_rounds;   /** Setup the cipher       @param key         The input symmetric key      @param keylen      The length of the input key (octets)      @param num_rounds  The requested number of rounds (0==default)      @param skey        [out] The destination of the scheduled key      @return CRYPT_OK if successful   */   int  (*setup)(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);   /** Encrypt a block      @param pt      The plaintext      @param ct      [out] The ciphertext      @param skey    The scheduled key      @return CRYPT_OK if successful   */   int (*ecb_encrypt)(const unsigned char *pt, unsigned char *ct, symmetric_key *skey);   /** Decrypt a block      @param ct      The ciphertext      @param pt      [out] The plaintext      @param skey    The scheduled key      @return CRYPT_OK if successful   */   int (*ecb_decrypt)(const unsigned char *ct, unsigned char *pt, symmetric_key *skey);   /** Test the block cipher       @return CRYPT_OK if successful, CRYPT_NOP if self-testing has been disabled   */   int (*test)(void);    /** Terminate the context       @param skey    The scheduled key   */   void (*done)(symmetric_key *skey);          /** Determine a key size       @param keysize    [in/out] The size of the key desired and the suggested size       @return CRYPT_OK if successful   */   int  (*keysize)(int *keysize); /** Accelerators **/   /** Accelerated ECB encryption        @param pt      Plaintext       @param ct      Ciphertext       @param blocks  The number of complete blocks to process       @param skey    The scheduled key context       @return CRYPT_OK if successful   */   int (*accel_ecb_encrypt)(const unsigned char *pt, unsigned char *ct, unsigned long blocks, symmetric_key *skey);    /** Accelerated ECB decryption        @param pt      Plaintext       @param ct      Ciphertext       @param blocks  The number of complete blocks to process       @param skey    The scheduled key context       @return CRYPT_OK if successful   */   int (*accel_ecb_decrypt)(const unsigned char *ct, unsigned char *pt, unsigned long blocks, symmetric_key *skey);    /** Accelerated CBC encryption        @param pt      Plaintext       @param ct      Ciphertext       @param blocks  The number of complete blocks to process       @param IV      The initial value (input/output)       @param skey    The scheduled key context       @return CRYPT_OK if successful   */   int (*accel_cbc_encrypt)(const unsigned char *pt, unsigned char *ct, unsigned long blocks, unsigned char *IV, symmetric_key *skey);    /** Accelerated CBC decryption        @param pt      Plaintext       @param ct      Ciphertext       @param blocks  The number of complete blocks to process       @param IV      The initial value (input/output)       @param skey    The scheduled key context       @return CRYPT_OK if successful   */   int (*accel_cbc_decrypt)(const unsigned char *ct, unsigned char *pt, unsigned long blocks, unsigned char *IV, symmetric_key *skey);    /** Accelerated CTR encryption        @param pt      Plaintext       @param ct      Ciphertext       @param blocks  The number of complete blocks to process       @param IV      The initial value (input/output)       @param mode    little or big endian counter (mode=0 or mode=1)       @param skey    The scheduled key context       @return CRYPT_OK if successful   */   int (*accel_ctr_encrypt)(const unsigned char *pt, unsigned char *ct, unsigned long blocks, unsigned char *IV, int mode, symmetric_key *skey);    /** Accelerated LRW        @param pt      Plaintext       @param ct      Ciphertext       @param blocks  The number of complete blocks to process       @param IV      The initial value (input/output)       @param tweak   The LRW tweak       @param skey    The scheduled key context       @return CRYPT_OK if successful   */   int (*accel_lrw_encrypt)(const unsigned char *pt, unsigned char *ct, unsigned long blocks, unsigned char *IV, const unsigned char *tweak, symmetric_key *skey);    /** Accelerated LRW        @param ct      Ciphertext       @param pt      Plaintext       @param blocks  The number of complete blocks to process       @param IV      The initial value (input/output)       @param tweak   The LRW tweak       @param skey    The scheduled key context       @return CRYPT_OK if successful   */   int (*accel_lrw_decrypt)(const unsigned char *ct, unsigned char *pt, unsigned long blocks, unsigned char *IV, const unsigned char *tweak, symmetric_key *skey);    /** Accelerated CCM packet (one-shot)       @param key        The secret key to use       @param keylen     The length of the secret key (octets)       @param uskey      A previously scheduled key [optional can be NULL]       @param nonce      The session nonce [use once]       @param noncelen   The length of the nonce       @param header     The header for the session       @param headerlen  The length of the header (octets)       @param pt         [out] The plaintext       @param ptlen      The length of the plaintext (octets)       @param ct         [out] The ciphertext       @param tag        [out] The destination tag       @param taglen     [in/out] The max size and resulting size of the authentication tag       @param direction  Encrypt or Decrypt direction (0 or 1)       @return CRYPT_OK if successful   */   int (*accel_ccm_memory)(       const unsigned char *key,    unsigned long keylen,       symmetric_key       *uskey,       const unsigned char *nonce,  unsigned long noncelen,       const unsigned char *header, unsigned long headerlen,             unsigned char *pt,     unsigned long ptlen,             unsigned char *ct,             unsigned char *tag,    unsigned long *taglen,                       int  direction);    /** Accelerated GCM packet (one shot)       @param key        The secret key       @param keylen     The length of the secret key       @param IV         The initial vector        @param IVlen      The length of the initial vector       @param adata      The additional authentication data (header)       @param adatalen   The length of the adata       @param pt         The plaintext       @param ptlen      The length of the plaintext (ciphertext length is the same)       @param ct         The ciphertext       @param tag        [out] The MAC tag       @param taglen     [in/out] The MAC tag length       @param direction  Encrypt or Decrypt mode (GCM_ENCRYPT or GCM_DECRYPT)       @return CRYPT_OK on success   */   int (*accel_gcm_memory)(       const unsigned char *key,    unsigned long keylen,       const unsigned char *IV,     unsigned long IVlen,       const unsigned char *adata,  unsigned long adatalen,             unsigned char *pt,     unsigned long ptlen,             unsigned char *ct, 

Lines 1–500 of 891.