/home/dko/Projects/mobilec/src/security/xyssl-0.9/library/aes.c File Reference

#include "xyssl/config.h"
#include "xyssl/aes.h"
#include "xyssl/padlock.h"
#include <string.h>
#include <stdio.h>

Go to the source code of this file.

Defines

#define GET_ULONG_LE(n, b, i)
#define PUT_ULONG_LE(n, b, i)
#define ROTL8(x)   ( ( x << 8 ) & 0xFFFFFFFF ) | ( x >> 24 )
#define XTIME(x)   ( ( x << 1 ) ^ ( ( x & 0x80 ) ? 0x1B : 0x00 ) )
#define MUL(x, y)   ( ( x && y ) ? pow[(log[x]+log[y]) % 255] : 0 )
#define AES_FROUND(X0, X1, X2, X3, Y0, Y1, Y2, Y3)
#define AES_RROUND(X0, X1, X2, X3, Y0, Y1, Y2, Y3)

Functions

static void aes_gen_tables (void)
void aes_setkey_enc (aes_context *ctx, unsigned char *key, int keysize)
 AES key schedule (encryption).
void aes_setkey_dec (aes_context *ctx, unsigned char *key, int keysize)
 AES key schedule (decryption).
void aes_crypt_ecb (aes_context *ctx, int mode, unsigned char input[16], unsigned char output[16])
 AES-ECB block encryption/decryption.
void aes_crypt_cbc (aes_context *ctx, int mode, int length, unsigned char iv[16], unsigned char *input, unsigned char *output)
 AES-CBC buffer encryption/decryption.
void aes_crypt_cfb (aes_context *ctx, int mode, int length, int *iv_off, unsigned char iv[16], unsigned char *input, unsigned char *output)
 AES-CFB buffer encryption/decryption.
int aes_self_test (int verbose)
 Checkup routine.

Variables

static unsigned char FSb [256]
static unsigned long FT0 [256]
static unsigned long FT1 [256]
static unsigned long FT2 [256]
static unsigned long FT3 [256]
static unsigned char RSb [256]
static unsigned long RT0 [256]
static unsigned long RT1 [256]
static unsigned long RT2 [256]
static unsigned long RT3 [256]
static unsigned long RCON [10]
static int aes_init_done = 0
static const unsigned char aes_test_ecb_dec [3][16]
static const unsigned char aes_test_ecb_enc [3][16]
static const unsigned char aes_test_cbc_dec [3][16]
static const unsigned char aes_test_cbc_enc [3][16]
static const unsigned char aes_test_cfb_dec [3][16]
static const unsigned char aes_test_cfb_enc [3][16]

Define Documentation

#define AES_FROUND ( X0,
X1,
X2,
X3,
Y0,
Y1,
Y2,
Y3   ) 
Value:
{                                               \
    X0 = *RK++ ^ FT0[ ( Y0       ) & 0xFF ] ^   \
                 FT1[ ( Y1 >>  8 ) & 0xFF ] ^   \
                 FT2[ ( Y2 >> 16 ) & 0xFF ] ^   \
                 FT3[ ( Y3 >> 24 ) & 0xFF ];    \
                                                \
    X1 = *RK++ ^ FT0[ ( Y1       ) & 0xFF ] ^   \
                 FT1[ ( Y2 >>  8 ) & 0xFF ] ^   \
                 FT2[ ( Y3 >> 16 ) & 0xFF ] ^   \
                 FT3[ ( Y0 >> 24 ) & 0xFF ];    \
                                                \
    X2 = *RK++ ^ FT0[ ( Y2       ) & 0xFF ] ^   \
                 FT1[ ( Y3 >>  8 ) & 0xFF ] ^   \
                 FT2[ ( Y0 >> 16 ) & 0xFF ] ^   \
                 FT3[ ( Y1 >> 24 ) & 0xFF ];    \
                                                \
    X3 = *RK++ ^ FT0[ ( Y3       ) & 0xFF ] ^   \
                 FT1[ ( Y0 >>  8 ) & 0xFF ] ^   \
                 FT2[ ( Y1 >> 16 ) & 0xFF ] ^   \
                 FT3[ ( Y2 >> 24 ) & 0xFF ];    \
}

Definition at line 590 of file aes.c.

Referenced by aes_crypt_ecb().

#define AES_RROUND ( X0,
X1,
X2,
X3,
Y0,
Y1,
Y2,
Y3   ) 
Value:
{                                               \
    X0 = *RK++ ^ RT0[ ( Y0       ) & 0xFF ] ^   \
                 RT1[ ( Y3 >>  8 ) & 0xFF ] ^   \
                 RT2[ ( Y2 >> 16 ) & 0xFF ] ^   \
                 RT3[ ( Y1 >> 24 ) & 0xFF ];    \
                                                \
    X1 = *RK++ ^ RT0[ ( Y1       ) & 0xFF ] ^   \
                 RT1[ ( Y0 >>  8 ) & 0xFF ] ^   \
                 RT2[ ( Y3 >> 16 ) & 0xFF ] ^   \
                 RT3[ ( Y2 >> 24 ) & 0xFF ];    \
                                                \
    X2 = *RK++ ^ RT0[ ( Y2       ) & 0xFF ] ^   \
                 RT1[ ( Y1 >>  8 ) & 0xFF ] ^   \
                 RT2[ ( Y0 >> 16 ) & 0xFF ] ^   \
                 RT3[ ( Y3 >> 24 ) & 0xFF ];    \
                                                \
    X3 = *RK++ ^ RT0[ ( Y3       ) & 0xFF ] ^   \
                 RT1[ ( Y2 >>  8 ) & 0xFF ] ^   \
                 RT2[ ( Y1 >> 16 ) & 0xFF ] ^   \
                 RT3[ ( Y0 >> 24 ) & 0xFF ];    \
}

Definition at line 613 of file aes.c.

Referenced by aes_crypt_ecb().

#define GET_ULONG_LE ( n,
b,
 ) 
Value:
{                                                       \
    (n) = ( (unsigned long) (b)[(i)    ]       )        \
        | ( (unsigned long) (b)[(i) + 1] <<  8 )        \
        | ( (unsigned long) (b)[(i) + 2] << 16 )        \
        | ( (unsigned long) (b)[(i) + 3] << 24 );       \
}

Definition at line 40 of file aes.c.

Referenced by aes_crypt_ecb(), aes_setkey_enc(), and md5_process().

#define MUL ( x,
 )     ( ( x && y ) ? pow[(log[x]+log[y]) % 255] : 0 )

Definition at line 354 of file aes.c.

Referenced by aes_gen_tables().

#define PUT_ULONG_LE ( n,
b,
 ) 
Value:
{                                                       \
    (b)[(i)    ] = (unsigned char) ( (n)       );       \
    (b)[(i) + 1] = (unsigned char) ( (n) >>  8 );       \
    (b)[(i) + 2] = (unsigned char) ( (n) >> 16 );       \
    (b)[(i) + 3] = (unsigned char) ( (n) >> 24 );       \
}

Definition at line 50 of file aes.c.

Referenced by aes_crypt_ecb(), and md5_finish().

#define ROTL8 (  )     ( ( x << 8 ) & 0xFFFFFFFF ) | ( x >> 24 )

Definition at line 352 of file aes.c.

Referenced by aes_gen_tables().

#define XTIME (  )     ( ( x << 1 ) ^ ( ( x & 0x80 ) ? 0x1B : 0x00 ) )

Definition at line 353 of file aes.c.

Referenced by aes_gen_tables().


Function Documentation

void aes_crypt_cbc ( aes_context ctx,
int  mode,
int  length,
unsigned char  iv[16],
unsigned char *  input,
unsigned char *  output 
)

AES-CBC buffer encryption/decryption.

Parameters:
ctx AES context
mode AES_ENCRYPT or AES_DECRYPT
length length of the input data
iv initialization vector (updated after use)
input buffer holding the input data
output buffer holding the output data

Definition at line 732 of file aes.c.

References aes_crypt_ecb(), and AES_DECRYPT.

Referenced by aes_self_test(), main(), ssl_decrypt_buf(), and ssl_encrypt_buf().

void aes_crypt_cfb ( aes_context ctx,
int  mode,
int  length,
int iv_off,
unsigned char  iv[16],
unsigned char *  input,
unsigned char *  output 
)

AES-CFB buffer encryption/decryption.

Parameters:
ctx AES context
mode AES_ENCRYPT or AES_DECRYPT
length length of the input data
iv_off offset in IV (updated after use)
iv initialization vector (updated after use)
input buffer holding the input data
output buffer holding the output data

Definition at line 787 of file aes.c.

References aes_crypt_ecb(), AES_DECRYPT, and AES_ENCRYPT.

Referenced by aes_self_test().

void aes_crypt_ecb ( aes_context ctx,
int  mode,
unsigned char  input[16],
unsigned char  output[16] 
)

AES-ECB block encryption/decryption.

Parameters:
ctx AES context
mode AES_ENCRYPT or AES_DECRYPT
input 16-byte input block
output 16-byte output block

Definition at line 639 of file aes.c.

References AES_DECRYPT, AES_FROUND, AES_RROUND, FSb, GET_ULONG_LE, aes_context::nr, PUT_ULONG_LE, aes_context::rk, and RSb.

Referenced by aes_crypt_cbc(), aes_crypt_cfb(), aes_en_de(), aes_self_test(), and main().

static void aes_gen_tables ( void   )  [static]

Definition at line 358 of file aes.c.

References FSb, FT0, FT1, FT2, FT3, MUL, RCON, ROTL8, RSb, RT0, RT1, RT2, RT3, and XTIME.

Referenced by aes_setkey_enc().

int aes_self_test ( int  verbose  ) 

Checkup routine.

Returns:
0 if successful, or 1 if the test failed

Definition at line 902 of file aes.c.

References aes_crypt_cbc(), aes_crypt_cfb(), aes_crypt_ecb(), AES_DECRYPT, aes_setkey_dec(), aes_setkey_enc(), aes_test_cbc_dec, aes_test_cbc_enc, aes_test_cfb_dec, aes_test_cfb_enc, aes_test_ecb_dec, aes_test_ecb_enc, buf, and prv.

Referenced by main().

void aes_setkey_dec ( aes_context ctx,
unsigned char *  key,
int  keysize 
)

AES key schedule (decryption).

Parameters:
ctx AES context to be initialized
key decryption key
keysize must be 128, 192 or 256

Definition at line 542 of file aes.c.

References aes_setkey_enc(), aes_context::buf, FSb, aes_context::nr, aes_context::rk, RT0, RT1, RT2, and RT3.

Referenced by aes_en_de(), aes_self_test(), main(), and ssl_derive_keys().

void aes_setkey_enc ( aes_context ctx,
unsigned char *  key,
int  keysize 
)

AES key schedule (encryption).

Parameters:
ctx AES context to be initialized
key encryption key
keysize must be 128, 192 or 256

Definition at line 439 of file aes.c.

References aes_gen_tables(), aes_init_done, aes_context::buf, FSb, GET_ULONG_LE, aes_context::nr, RCON, and aes_context::rk.

Referenced by aes_en_de(), aes_self_test(), aes_setkey_dec(), main(), and ssl_derive_keys().


Variable Documentation

int aes_init_done = 0 [static]

Definition at line 356 of file aes.c.

Referenced by aes_setkey_enc().

const unsigned char aes_test_cbc_dec[3][16] [static]
Initial value:
{
    { 0xFA, 0xCA, 0x37, 0xE0, 0xB0, 0xC8, 0x53, 0x73,
      0xDF, 0x70, 0x6E, 0x73, 0xF7, 0xC9, 0xAF, 0x86 },
    { 0x5D, 0xF6, 0x78, 0xDD, 0x17, 0xBA, 0x4E, 0x75,
      0xB6, 0x17, 0x68, 0xC6, 0xAD, 0xEF, 0x7C, 0x7B },
    { 0x48, 0x04, 0xE1, 0x81, 0x8F, 0xE6, 0x29, 0x75,
      0x19, 0xA3, 0xE8, 0x8C, 0x57, 0x31, 0x04, 0x13 }
}

Definition at line 856 of file aes.c.

Referenced by aes_self_test().

const unsigned char aes_test_cbc_enc[3][16] [static]
Initial value:
{
    { 0x8A, 0x05, 0xFC, 0x5E, 0x09, 0x5A, 0xF4, 0x84,
      0x8A, 0x08, 0xD3, 0x28, 0xD3, 0x68, 0x8E, 0x3D },
    { 0x7B, 0xD9, 0x66, 0xD5, 0x3A, 0xD8, 0xC1, 0xBB,
      0x85, 0xD2, 0xAD, 0xFA, 0xE8, 0x7B, 0xB1, 0x04 },
    { 0xFE, 0x3C, 0x53, 0x65, 0x3E, 0x2F, 0x45, 0xB5,
      0x6F, 0xCD, 0x88, 0xB2, 0xCC, 0x89, 0x8F, 0xF0 }
}

Definition at line 866 of file aes.c.

Referenced by aes_self_test().

const unsigned char aes_test_cfb_dec[3][16] [static]
Initial value:
{
    { 0xBA, 0x75, 0x0C, 0xC9, 0x77, 0xF8, 0xD4, 0xE1,
      0x3E, 0x0F, 0xB5, 0x46, 0x2E, 0xA6, 0x33, 0xF6 },
    { 0xDB, 0x40, 0x4A, 0x98, 0x7B, 0xAA, 0xA3, 0xF3,
      0x92, 0x35, 0xAD, 0x58, 0x09, 0x9B, 0xFF, 0x6E },
    { 0xA8, 0x17, 0x41, 0x0E, 0x76, 0x71, 0x60, 0xE5,
      0xFD, 0x37, 0xC5, 0x43, 0xCC, 0xC8, 0xD6, 0xDA }
}

Definition at line 879 of file aes.c.

Referenced by aes_self_test().

const unsigned char aes_test_cfb_enc[3][16] [static]
Initial value:
{
    { 0x45, 0x62, 0xC5, 0xA1, 0xF9, 0x10, 0x8F, 0xE0,
      0x87, 0x24, 0x25, 0x68, 0xB5, 0x12, 0xF3, 0x8B },
    { 0xB8, 0xD4, 0xD5, 0x09, 0xF5, 0xEE, 0x08, 0x38,
      0x48, 0x9B, 0x9D, 0xAD, 0x11, 0xB4, 0x2E, 0xD2 },
    { 0xE9, 0x10, 0x80, 0xDA, 0xEE, 0x2D, 0x81, 0xD9,
      0x41, 0x78, 0x91, 0xD5, 0x98, 0x78, 0xE1, 0xFA }
}

Definition at line 889 of file aes.c.

Referenced by aes_self_test().

const unsigned char aes_test_ecb_dec[3][16] [static]
Initial value:
{
    { 0x44, 0x41, 0x6A, 0xC2, 0xD1, 0xF5, 0x3C, 0x58,
      0x33, 0x03, 0x91, 0x7E, 0x6B, 0xE9, 0xEB, 0xE0 },
    { 0x48, 0xE3, 0x1E, 0x9E, 0x25, 0x67, 0x18, 0xF2,
      0x92, 0x29, 0x31, 0x9C, 0x19, 0xF1, 0x5B, 0xA4 },
    { 0x05, 0x8C, 0xCF, 0xFD, 0xBB, 0xCB, 0x38, 0x2D,
      0x1F, 0x6F, 0x56, 0x58, 0x5D, 0x8A, 0x4A, 0xDE }
}

Definition at line 836 of file aes.c.

Referenced by aes_self_test().

const unsigned char aes_test_ecb_enc[3][16] [static]
Initial value:
{
    { 0xC3, 0x4C, 0x05, 0x2C, 0xC0, 0xDA, 0x8D, 0x73,
      0x45, 0x1A, 0xFE, 0x5F, 0x03, 0xBE, 0x29, 0x7F },
    { 0xF3, 0xF6, 0x75, 0x2A, 0xE8, 0xD7, 0x83, 0x11,
      0x38, 0xF0, 0x41, 0x56, 0x06, 0x31, 0xB1, 0x14 },
    { 0x8B, 0x79, 0xEE, 0xCC, 0x93, 0xA0, 0xEE, 0x5D,
      0xFF, 0x30, 0xB4, 0xEA, 0x21, 0x63, 0x6D, 0xA4 }
}

Definition at line 846 of file aes.c.

Referenced by aes_self_test().

unsigned char FSb[256] [static]

Definition at line 329 of file aes.c.

Referenced by aes_crypt_ecb(), aes_gen_tables(), aes_setkey_dec(), and aes_setkey_enc().

unsigned long FT0[256] [static]

Definition at line 330 of file aes.c.

Referenced by aes_gen_tables().

unsigned long FT1[256] [static]

Definition at line 331 of file aes.c.

Referenced by aes_gen_tables().

unsigned long FT2[256] [static]

Definition at line 332 of file aes.c.

Referenced by aes_gen_tables().

unsigned long FT3[256] [static]

Definition at line 333 of file aes.c.

Referenced by aes_gen_tables().

unsigned long RCON[10] [static]

Definition at line 347 of file aes.c.

Referenced by aes_gen_tables(), and aes_setkey_enc().

unsigned char RSb[256] [static]

Definition at line 338 of file aes.c.

Referenced by aes_crypt_ecb(), and aes_gen_tables().

unsigned long RT0[256] [static]

Definition at line 339 of file aes.c.

Referenced by aes_gen_tables(), and aes_setkey_dec().

unsigned long RT1[256] [static]

Definition at line 340 of file aes.c.

Referenced by aes_gen_tables(), and aes_setkey_dec().

unsigned long RT2[256] [static]

Definition at line 341 of file aes.c.

Referenced by aes_gen_tables(), and aes_setkey_dec().

unsigned long RT3[256] [static]

Definition at line 342 of file aes.c.

Referenced by aes_gen_tables(), and aes_setkey_dec().

Generated on Tue Dec 28 15:15:29 2010 for Mobile-C by  doxygen 1.6.3