/home/dko/projects/mobilec/trunk/src/security/xyssl-0.7/library/des.c File Reference

#include <string.h>
#include "xyssl/des.h"

Go to the source code of this file.

Defines

#define _CRT_SECURE_NO_DEPRECATE   1
#define GET_UINT32_BE(n, b, i)
#define PUT_UINT32_BE(n, b, i)
#define DES_IP(X, Y)
#define DES_FP(X, Y)
#define DES_ROUND(X, Y)

Functions

static void des_main_ks (unsigned long SK[32], unsigned char key[8])
void des_set_key (des_context *ctx, unsigned char key[8])
 DES key schedule (56-bit).
static void des_crypt (unsigned long SK[32], unsigned char input[8], unsigned char output[8])
void des_encrypt (des_context *ctx, unsigned char input[8], unsigned char output[8])
 DES block encryption (ECB mode).
void des_decrypt (des_context *ctx, unsigned char input[8], unsigned char output[8])
 DES block decryption (ECB mode).
void des_cbc_encrypt (des_context *ctx, unsigned char iv[8], unsigned char *input, unsigned char *output, int len)
 DES-CBC buffer encryption.
void des_cbc_decrypt (des_context *ctx, unsigned char iv[8], unsigned char *input, unsigned char *output, int len)
 DES-CBC buffer decryption.
void des3_set_2keys (des3_context *ctx, unsigned char key[16])
 Triple-DES key schedule (112-bit).
void des3_set_3keys (des3_context *ctx, unsigned char key[24])
 Triple-DES key schedule (168-bit).
static void des3_crypt (unsigned long SK[96], unsigned char input[8], unsigned char output[8])
void des3_encrypt (des3_context *ctx, unsigned char input[8], unsigned char output[8])
 Triple-DES block encryption (ECB mode).
void des3_decrypt (des3_context *ctx, unsigned char input[8], unsigned char output[8])
 Triple-DES block decryption (ECB mode).
void des3_cbc_encrypt (des3_context *ctx, unsigned char iv[8], unsigned char *input, unsigned char *output, int len)
 3DES-CBC buffer encryption
void des3_cbc_decrypt (des3_context *ctx, unsigned char iv[8], unsigned char *input, unsigned char *output, int len)
 3DES-CBC buffer decryption
int des_self_test (int verbose)

Variables

static const unsigned long SB1 [64]
static const unsigned long SB2 [64]
static const unsigned long SB3 [64]
static const unsigned long SB4 [64]
static const unsigned long SB5 [64]
static const unsigned long SB6 [64]
static const unsigned long SB7 [64]
static const unsigned long SB8 [64]
static const unsigned long LHs [16]
static const unsigned long RHs [16]
static const char _des_src [] = "_des_src"


Define Documentation

#define _CRT_SECURE_NO_DEPRECATE   1

Definition at line 28 of file des.c.

#define DES_FP ( X,
 ) 

Value:

{                                                               \
    X = ((X << 31) | (X >> 1)) & 0xFFFFFFFF;                    \
    T = (X ^ Y) & 0xAAAAAAAA; X ^= T; Y ^= T;                   \
    Y = ((Y << 31) | (Y >> 1)) & 0xFFFFFFFF;                    \
    T = ((Y >>  8) ^ X) & 0x00FF00FF; X ^= T; Y ^= (T <<  8);   \
    T = ((Y >>  2) ^ X) & 0x33333333; X ^= T; Y ^= (T <<  2);   \
    T = ((X >> 16) ^ Y) & 0x0000FFFF; Y ^= T; X ^= (T << 16);   \
    T = ((X >>  4) ^ Y) & 0x0F0F0F0F; Y ^= T; X ^= (T <<  4);   \
}

Definition at line 256 of file des.c.

Referenced by des3_crypt(), and des_crypt().

#define DES_IP ( X,
 ) 

Value:

{                                                               \
    T = ((X >>  4) ^ Y) & 0x0F0F0F0F; Y ^= T; X ^= (T <<  4);   \
    T = ((X >> 16) ^ Y) & 0x0000FFFF; Y ^= T; X ^= (T << 16);   \
    T = ((Y >>  2) ^ X) & 0x33333333; X ^= T; Y ^= (T <<  2);   \
    T = ((Y >>  8) ^ X) & 0x00FF00FF; X ^= T; Y ^= (T <<  8);   \
    Y = ((Y << 1) | (Y >> 31)) & 0xFFFFFFFF;                    \
    T = (X ^ Y) & 0xAAAAAAAA; Y ^= T; X ^= T;                   \
    X = ((X << 1) | (X >> 31)) & 0xFFFFFFFF;                    \
}

Definition at line 242 of file des.c.

Referenced by des3_crypt(), and des_crypt().

#define DES_ROUND ( X,
 ) 

Value:

{                                               \
    T = *SK++ ^ X;                              \
    Y ^= SB8[ (T      ) & 0x3F ] ^              \
         SB6[ (T >>  8) & 0x3F ] ^              \
         SB4[ (T >> 16) & 0x3F ] ^              \
         SB2[ (T >> 24) & 0x3F ];               \
                                                \
    T = *SK++ ^ ((X << 28) | (X >> 4));         \
    Y ^= SB7[ (T      ) & 0x3F ] ^              \
         SB5[ (T >>  8) & 0x3F ] ^              \
         SB3[ (T >> 16) & 0x3F ] ^              \
         SB1[ (T >> 24) & 0x3F ];               \
}

Definition at line 270 of file des.c.

Referenced by des3_crypt(), and des_crypt().

#define GET_UINT32_BE ( n,
b,
 ) 

Value:

{                                                       \
    (n) = ( (unsigned long) (b)[(i)    ] << 24 )        \
        | ( (unsigned long) (b)[(i) + 1] << 16 )        \
        | ( (unsigned long) (b)[(i) + 2] <<  8 )        \
        | ( (unsigned long) (b)[(i) + 3]       );       \
}

Definition at line 39 of file des.c.

#define PUT_UINT32_BE ( n,
b,
 ) 

Value:

{                                                       \
    (b)[(i)    ] = (unsigned char) ( (n) >> 24 );       \
    (b)[(i) + 1] = (unsigned char) ( (n) >> 16 );       \
    (b)[(i) + 2] = (unsigned char) ( (n) >>  8 );       \
    (b)[(i) + 3] = (unsigned char) ( (n)       );       \
}

Definition at line 48 of file des.c.


Function Documentation

void des3_cbc_decrypt ( des3_context ctx,
unsigned char  iv[8],
unsigned char *  input,
unsigned char *  output,
int  len 
)

3DES-CBC buffer decryption

Parameters:
ctx 3DES context
iv initialization vector (modified after use)
input buffer holding the ciphertext
output buffer holding the plaintext
len length of the data to be decrypted

Definition at line 611 of file des.c.

References des3_crypt(), and des3_context::dsk.

Referenced by ssl_decrypt_buf(), and x509_des3_decrypt().

void des3_cbc_encrypt ( des3_context ctx,
unsigned char  iv[8],
unsigned char *  input,
unsigned char *  output,
int  len 
)

3DES-CBC buffer encryption

Parameters:
ctx 3DES context
iv initialization vector (modified after use)
input buffer holding the plaintext
output buffer holding the ciphertext
len length of the data to be encrypted

Definition at line 586 of file des.c.

References des3_crypt(), and des3_context::esk.

Referenced by main(), and ssl_encrypt_buf().

static void des3_crypt ( unsigned long  SK[96],
unsigned char  input[8],
unsigned char  output[8] 
) [static]

Definition at line 519 of file des.c.

References DES_FP, DES_IP, DES_ROUND, GET_UINT32_BE, and PUT_UINT32_BE.

Referenced by des3_cbc_decrypt(), des3_cbc_encrypt(), des3_decrypt(), and des3_encrypt().

void des3_decrypt ( des3_context ctx,
unsigned char  input[8],
unsigned char  output[8] 
)

Triple-DES block decryption (ECB mode).

Parameters:
ctx 3DES context
input ciphertext block
output plaintext block

Definition at line 576 of file des.c.

References des3_crypt(), and des3_context::dsk.

void des3_encrypt ( des3_context ctx,
unsigned char  input[8],
unsigned char  output[8] 
)

Triple-DES block encryption (ECB mode).

Parameters:
ctx 3DES context
input plaintext block
output ciphertext block

Definition at line 566 of file des.c.

References des3_crypt(), and des3_context::esk.

void des3_set_2keys ( des3_context ctx,
unsigned char  key[16] 
)

Triple-DES key schedule (112-bit).

Parameters:
ctx 3DES context to be initialized
key 16-byte secret key

Definition at line 472 of file des.c.

References des_main_ks(), des3_context::dsk, and des3_context::esk.

void des3_set_3keys ( des3_context ctx,
unsigned char  key[24] 
)

Triple-DES key schedule (168-bit).

Parameters:
ctx 3DES context to be initialized
key 24-byte secret key

Definition at line 498 of file des.c.

References des_main_ks(), des3_context::dsk, and des3_context::esk.

Referenced by main(), ssl_derive_keys(), and x509_des3_decrypt().

void des_cbc_decrypt ( des_context ctx,
unsigned char  iv[8],
unsigned char *  input,
unsigned char *  output,
int  len 
)

DES-CBC buffer decryption.

Parameters:
ctx DES context
iv initialization vector (modified after use)
input buffer holding the ciphertext
output buffer holding the plaintext
len length of the data to be decrypted

Definition at line 444 of file des.c.

References des_crypt(), and des_context::dsk.

void des_cbc_encrypt ( des_context ctx,
unsigned char  iv[8],
unsigned char *  input,
unsigned char *  output,
int  len 
)

DES-CBC buffer encryption.

Parameters:
ctx DES context
iv initialization vector (modified after use)
input buffer holding the plaintext
output buffer holding the ciphertext
len length of the data to be encrypted

Definition at line 419 of file des.c.

References des_crypt(), and des_context::esk.

Referenced by main().

static void des_crypt ( unsigned long  SK[32],
unsigned char  input[8],
unsigned char  output[8] 
) [static]

Definition at line 370 of file des.c.

References DES_FP, DES_IP, DES_ROUND, GET_UINT32_BE, and PUT_UINT32_BE.

Referenced by des_cbc_decrypt(), des_cbc_encrypt(), des_decrypt(), and des_encrypt().

void des_decrypt ( des_context ctx,
unsigned char  input[8],
unsigned char  output[8] 
)

DES block decryption (ECB mode).

Parameters:
ctx DES context
input ciphertext block
output plaintext block

Definition at line 409 of file des.c.

References des_crypt(), and des_context::dsk.

void des_encrypt ( des_context ctx,
unsigned char  input[8],
unsigned char  output[8] 
)

DES block encryption (ECB mode).

Parameters:
ctx DES context
input plaintext block
output ciphertext block

Definition at line 399 of file des.c.

References des_crypt(), and des_context::esk.

static void des_main_ks ( unsigned long  SK[32],
unsigned char  key[8] 
) [static]

Definition at line 285 of file des.c.

References GET_UINT32_BE, LHs, and RHs.

Referenced by des3_set_2keys(), des3_set_3keys(), and des_set_key().

int des_self_test ( int  verbose  ) 

Definition at line 733 of file des.c.

Referenced by main().

void des_set_key ( des_context ctx,
unsigned char  key[8] 
)

DES key schedule (56-bit).

Parameters:
ctx DES context to be initialized
key 8-byte secret key

Definition at line 357 of file des.c.

References des_main_ks(), des_context::dsk, and des_context::esk.

Referenced by main().


Variable Documentation

const char _des_src[] = "_des_src" [static]

Definition at line 636 of file des.c.

const unsigned long LHs[16] [static]

Initial value:

{
    0x00000000, 0x00000001, 0x00000100, 0x00000101,
    0x00010000, 0x00010001, 0x00010100, 0x00010101,
    0x01000000, 0x01000001, 0x01000100, 0x01000101,
    0x01010000, 0x01010001, 0x01010100, 0x01010101
}

Definition at line 223 of file des.c.

Referenced by des_main_ks().

const unsigned long RHs[16] [static]

Initial value:

{
    0x00000000, 0x01000000, 0x00010000, 0x01010000,
    0x00000100, 0x01000100, 0x00010100, 0x01010100,
    0x00000001, 0x01000001, 0x00010001, 0x01010001,
    0x00000101, 0x01000101, 0x00010101, 0x01010101,
}

Definition at line 231 of file des.c.

Referenced by des_main_ks().

const unsigned long SB1[64] [static]

Initial value:

{
    0x01010400, 0x00000000, 0x00010000, 0x01010404,
    0x01010004, 0x00010404, 0x00000004, 0x00010000,
    0x00000400, 0x01010400, 0x01010404, 0x00000400,
    0x01000404, 0x01010004, 0x01000000, 0x00000004,
    0x00000404, 0x01000400, 0x01000400, 0x00010400,
    0x00010400, 0x01010000, 0x01010000, 0x01000404,
    0x00010004, 0x01000004, 0x01000004, 0x00010004,
    0x00000000, 0x00000404, 0x00010404, 0x01000000,
    0x00010000, 0x01010404, 0x00000004, 0x01010000,
    0x01010400, 0x01000000, 0x01000000, 0x00000400,
    0x01010004, 0x00010000, 0x00010400, 0x01000004,
    0x00000400, 0x00000004, 0x01000404, 0x00010404,
    0x01010404, 0x00010004, 0x01010000, 0x01000404,
    0x01000004, 0x00000404, 0x00010404, 0x01010400,
    0x00000404, 0x01000400, 0x01000400, 0x00000000,
    0x00010004, 0x00010400, 0x00000000, 0x01010004
}

Definition at line 60 of file des.c.

const unsigned long SB2[64] [static]

Initial value:

{
    0x80108020, 0x80008000, 0x00008000, 0x00108020,
    0x00100000, 0x00000020, 0x80100020, 0x80008020,
    0x80000020, 0x80108020, 0x80108000, 0x80000000,
    0x80008000, 0x00100000, 0x00000020, 0x80100020,
    0x00108000, 0x00100020, 0x80008020, 0x00000000,
    0x80000000, 0x00008000, 0x00108020, 0x80100000,
    0x00100020, 0x80000020, 0x00000000, 0x00108000,
    0x00008020, 0x80108000, 0x80100000, 0x00008020,
    0x00000000, 0x00108020, 0x80100020, 0x00100000,
    0x80008020, 0x80100000, 0x80108000, 0x00008000,
    0x80100000, 0x80008000, 0x00000020, 0x80108020,
    0x00108020, 0x00000020, 0x00008000, 0x80000000,
    0x00008020, 0x80108000, 0x00100000, 0x80000020,
    0x00100020, 0x80008020, 0x80000020, 0x00100020,
    0x00108000, 0x00000000, 0x80008000, 0x00008020,
    0x80000000, 0x80100020, 0x80108020, 0x00108000
}

Definition at line 80 of file des.c.

const unsigned long SB3[64] [static]

Initial value:

{
    0x00000208, 0x08020200, 0x00000000, 0x08020008,
    0x08000200, 0x00000000, 0x00020208, 0x08000200,
    0x00020008, 0x08000008, 0x08000008, 0x00020000,
    0x08020208, 0x00020008, 0x08020000, 0x00000208,
    0x08000000, 0x00000008, 0x08020200, 0x00000200,
    0x00020200, 0x08020000, 0x08020008, 0x00020208,
    0x08000208, 0x00020200, 0x00020000, 0x08000208,
    0x00000008, 0x08020208, 0x00000200, 0x08000000,
    0x08020200, 0x08000000, 0x00020008, 0x00000208,
    0x00020000, 0x08020200, 0x08000200, 0x00000000,
    0x00000200, 0x00020008, 0x08020208, 0x08000200,
    0x08000008, 0x00000200, 0x00000000, 0x08020008,
    0x08000208, 0x00020000, 0x08000000, 0x08020208,
    0x00000008, 0x00020208, 0x00020200, 0x08000008,
    0x08020000, 0x08000208, 0x00000208, 0x08020000,
    0x00020208, 0x00000008, 0x08020008, 0x00020200
}

Definition at line 100 of file des.c.

const unsigned long SB4[64] [static]

Initial value:

{
    0x00802001, 0x00002081, 0x00002081, 0x00000080,
    0x00802080, 0x00800081, 0x00800001, 0x00002001,
    0x00000000, 0x00802000, 0x00802000, 0x00802081,
    0x00000081, 0x00000000, 0x00800080, 0x00800001,
    0x00000001, 0x00002000, 0x00800000, 0x00802001,
    0x00000080, 0x00800000, 0x00002001, 0x00002080,
    0x00800081, 0x00000001, 0x00002080, 0x00800080,
    0x00002000, 0x00802080, 0x00802081, 0x00000081,
    0x00800080, 0x00800001, 0x00802000, 0x00802081,
    0x00000081, 0x00000000, 0x00000000, 0x00802000,
    0x00002080, 0x00800080, 0x00800081, 0x00000001,
    0x00802001, 0x00002081, 0x00002081, 0x00000080,
    0x00802081, 0x00000081, 0x00000001, 0x00002000,
    0x00800001, 0x00002001, 0x00802080, 0x00800081,
    0x00002001, 0x00002080, 0x00800000, 0x00802001,
    0x00000080, 0x00800000, 0x00002000, 0x00802080
}

Definition at line 120 of file des.c.

const unsigned long SB5[64] [static]

Initial value:

{
    0x00000100, 0x02080100, 0x02080000, 0x42000100,
    0x00080000, 0x00000100, 0x40000000, 0x02080000,
    0x40080100, 0x00080000, 0x02000100, 0x40080100,
    0x42000100, 0x42080000, 0x00080100, 0x40000000,
    0x02000000, 0x40080000, 0x40080000, 0x00000000,
    0x40000100, 0x42080100, 0x42080100, 0x02000100,
    0x42080000, 0x40000100, 0x00000000, 0x42000000,
    0x02080100, 0x02000000, 0x42000000, 0x00080100,
    0x00080000, 0x42000100, 0x00000100, 0x02000000,
    0x40000000, 0x02080000, 0x42000100, 0x40080100,
    0x02000100, 0x40000000, 0x42080000, 0x02080100,
    0x40080100, 0x00000100, 0x02000000, 0x42080000,
    0x42080100, 0x00080100, 0x42000000, 0x42080100,
    0x02080000, 0x00000000, 0x40080000, 0x42000000,
    0x00080100, 0x02000100, 0x40000100, 0x00080000,
    0x00000000, 0x40080000, 0x02080100, 0x40000100
}

Definition at line 140 of file des.c.

const unsigned long SB6[64] [static]

Initial value:

{
    0x20000010, 0x20400000, 0x00004000, 0x20404010,
    0x20400000, 0x00000010, 0x20404010, 0x00400000,
    0x20004000, 0x00404010, 0x00400000, 0x20000010,
    0x00400010, 0x20004000, 0x20000000, 0x00004010,
    0x00000000, 0x00400010, 0x20004010, 0x00004000,
    0x00404000, 0x20004010, 0x00000010, 0x20400010,
    0x20400010, 0x00000000, 0x00404010, 0x20404000,
    0x00004010, 0x00404000, 0x20404000, 0x20000000,
    0x20004000, 0x00000010, 0x20400010, 0x00404000,
    0x20404010, 0x00400000, 0x00004010, 0x20000010,
    0x00400000, 0x20004000, 0x20000000, 0x00004010,
    0x20000010, 0x20404010, 0x00404000, 0x20400000,
    0x00404010, 0x20404000, 0x00000000, 0x20400010,
    0x00000010, 0x00004000, 0x20400000, 0x00404010,
    0x00004000, 0x00400010, 0x20004010, 0x00000000,
    0x20404000, 0x20000000, 0x00400010, 0x20004010
}

Definition at line 160 of file des.c.

const unsigned long SB7[64] [static]

Initial value:

{
    0x00200000, 0x04200002, 0x04000802, 0x00000000,
    0x00000800, 0x04000802, 0x00200802, 0x04200800,
    0x04200802, 0x00200000, 0x00000000, 0x04000002,
    0x00000002, 0x04000000, 0x04200002, 0x00000802,
    0x04000800, 0x00200802, 0x00200002, 0x04000800,
    0x04000002, 0x04200000, 0x04200800, 0x00200002,
    0x04200000, 0x00000800, 0x00000802, 0x04200802,
    0x00200800, 0x00000002, 0x04000000, 0x00200800,
    0x04000000, 0x00200800, 0x00200000, 0x04000802,
    0x04000802, 0x04200002, 0x04200002, 0x00000002,
    0x00200002, 0x04000000, 0x04000800, 0x00200000,
    0x04200800, 0x00000802, 0x00200802, 0x04200800,
    0x00000802, 0x04000002, 0x04200802, 0x04200000,
    0x00200800, 0x00000000, 0x00000002, 0x04200802,
    0x00000000, 0x00200802, 0x04200000, 0x00000800,
    0x04000002, 0x04000800, 0x00000800, 0x00200002
}

Definition at line 180 of file des.c.

const unsigned long SB8[64] [static]

Initial value:

{
    0x10001040, 0x00001000, 0x00040000, 0x10041040,
    0x10000000, 0x10001040, 0x00000040, 0x10000000,
    0x00040040, 0x10040000, 0x10041040, 0x00041000,
    0x10041000, 0x00041040, 0x00001000, 0x00000040,
    0x10040000, 0x10000040, 0x10001000, 0x00001040,
    0x00041000, 0x00040040, 0x10040040, 0x10041000,
    0x00001040, 0x00000000, 0x00000000, 0x10040040,
    0x10000040, 0x10001000, 0x00041040, 0x00040000,
    0x00041040, 0x00040000, 0x10041000, 0x00001000,
    0x00000040, 0x10040040, 0x00001000, 0x00041040,
    0x10001000, 0x00000040, 0x10000040, 0x10040000,
    0x10040040, 0x10000000, 0x00040000, 0x10001040,
    0x00000000, 0x10041040, 0x00040040, 0x10000040,
    0x10040000, 0x10001000, 0x10001040, 0x00000000,
    0x10041040, 0x00041000, 0x00041000, 0x00001040,
    0x00001040, 0x00040040, 0x10000000, 0x10041000
}

Definition at line 200 of file des.c.


Generated on Fri May 16 14:49:56 2008 for Mobile-C by  doxygen 1.5.4