aes.c File Reference

#include <string.h>
#include "crypto.h"

Go to the source code of this file.

Defines

#define rot1(x)   (((x) << 24) | ((x) >> 8))
 AES implementation - this is a small code version.
#define rot2(x)   (((x) << 16) | ((x) >> 16))
#define rot3(x)   (((x) << 8) | ((x) >> 24))
#define mt   0x80808080
#define ml   0x7f7f7f7f
#define mh   0xfefefefe
#define mm   0x1b1b1b1b
#define mul2(x, t)
#define inv_mix_col(x, f2, f4, f8, f9)
#define n2l(c, l)   l=ntohl(*c); c++
#define l2n(l, c)   *c++=htonl(l)

Functions

static unsigned char AES_xtime (uint32_t x)
void AES_set_key (AES_CTX *ctx, const uint8_t *key, const uint8_t *iv, AES_MODE mode)
 Set up AES with the key/iv and cipher size.
void AES_convert_key (AES_CTX *ctx)
 Change a key for decryption.
void AES_encrypt (const AES_CTX *ctx, uint32_t *data)
 Encrypt a single block (16 bytes) of data.
void AES_decrypt (const AES_CTX *ctx, uint32_t *data)
 Decrypt a single block (16 bytes) of data.

Variables

static const uint8_t aes_sbox [256]
static const uint8_t aes_isbox [256]
static const unsigned char Rcon [30]


Define Documentation

#define rot1 (  )     (((x) << 24) | ((x) >> 8))

AES implementation - this is a small code version.

There are much faster versions around but they are much larger in size (i.e. they use large submix tables).

Definition at line 31 of file aes.c.

#define rot2 (  )     (((x) << 16) | ((x) >> 16))

Definition at line 32 of file aes.c.

#define rot3 (  )     (((x) << 8) | ((x) >> 24))

Definition at line 33 of file aes.c.

#define mt   0x80808080

Definition at line 46 of file aes.c.

#define ml   0x7f7f7f7f

Definition at line 47 of file aes.c.

#define mh   0xfefefefe

Definition at line 48 of file aes.c.

#define mm   0x1b1b1b1b

Definition at line 49 of file aes.c.

#define mul2 ( x,
 ) 

Value:

((t)=((x)&mt), \
                        ((((x)+(x))&mh)^(((t)-((t)>>7))&mm)))

Definition at line 50 of file aes.c.

#define inv_mix_col ( x,
f2,
f4,
f8,
f9   ) 

Value:

(\
                        (f2)=mul2(x,f2), \
                        (f4)=mul2(f2,f4), \
                        (f8)=mul2(f4,f8), \
                        (f9)=(x)^(f8), \
                        (f8)=((f2)^(f4)^(f8)), \
                        (f2)^=(f9), \
                        (f4)^=(f9), \
                        (f8)^=rot3(f2), \
                        (f8)^=rot2(f4), \
                        (f8)^rot1(f9))

Definition at line 53 of file aes.c.

Referenced by AES_convert_key().

#define n2l ( c,
 )     l=ntohl(*c); c++

Definition at line 66 of file aes.c.

#define l2n ( l,
 )     *c++=htonl(l)

Definition at line 67 of file aes.c.


Function Documentation

static unsigned char AES_xtime ( uint32_t  x  )  [static]

Definition at line 157 of file aes.c.

Referenced by AES_decrypt(), and AES_encrypt().

00158 {
00159         return x = (x&0x80) ? (x<<1)^0x1b : x<<1;
00160 }

void AES_set_key ( AES_CTX ctx,
const uint8_t key,
const uint8_t iv,
AES_MODE  mode 
)

Set up AES with the key/iv and cipher size.

Definition at line 165 of file aes.c.

References AES_MODE_128, AES_MODE_256, aes_sbox, aes_key_st::iv, aes_key_st::key_size, aes_key_st::ks, memcpy, Rcon, and aes_key_st::rounds.

Referenced by aes_setkey().

00167 {
00168     int i, ii;
00169     uint32_t *W, tmp, tmp2;
00170     const unsigned char *ip;
00171     int words;
00172 
00173     switch (mode)
00174     {
00175         case AES_MODE_128:
00176             i = 10;
00177             words = 4;
00178             break;
00179 
00180         case AES_MODE_256:
00181             i = 14;
00182             words = 8;
00183             break;
00184 
00185         default:        /* fail silently */
00186             return;
00187     }
00188 
00189     ctx->rounds = i;
00190     ctx->key_size = words;
00191     W = ctx->ks;
00192     for (i = 0; i < words; i+=2)
00193     {
00194         W[i+0]= ((uint32_t)key[ 0]<<24)|
00195             ((uint32_t)key[ 1]<<16)|
00196             ((uint32_t)key[ 2]<< 8)|
00197             ((uint32_t)key[ 3]    );
00198         W[i+1]= ((uint32_t)key[ 4]<<24)|
00199             ((uint32_t)key[ 5]<<16)|
00200             ((uint32_t)key[ 6]<< 8)|
00201             ((uint32_t)key[ 7]    );
00202         key += 8;
00203     }
00204 
00205     ip = Rcon;
00206     ii = 4 * (ctx->rounds+1);
00207     for (i = words; i<ii; i++)
00208     {
00209         tmp = W[i-1];
00210 
00211         if ((i % words) == 0)
00212         {
00213             tmp2 =(uint32_t)aes_sbox[(tmp    )&0xff]<< 8;
00214             tmp2|=(uint32_t)aes_sbox[(tmp>> 8)&0xff]<<16;
00215             tmp2|=(uint32_t)aes_sbox[(tmp>>16)&0xff]<<24;
00216             tmp2|=(uint32_t)aes_sbox[(tmp>>24)     ];
00217             tmp=tmp2^(((unsigned int)*ip)<<24);
00218             ip++;
00219         }
00220 
00221         if ((words == 8) && ((i % words) == 4))
00222         {
00223             tmp2 =(uint32_t)aes_sbox[(tmp    )&0xff]    ;
00224             tmp2|=(uint32_t)aes_sbox[(tmp>> 8)&0xff]<< 8;
00225             tmp2|=(uint32_t)aes_sbox[(tmp>>16)&0xff]<<16;
00226             tmp2|=(uint32_t)aes_sbox[(tmp>>24)     ]<<24;
00227             tmp=tmp2;
00228         }
00229 
00230         W[i]=W[i-words]^tmp;
00231     }
00232 
00233     /* copy the iv across */
00234     memcpy(ctx->iv, iv, 16);
00235 }

void AES_convert_key ( AES_CTX ctx  ) 

Change a key for decryption.

Definition at line 240 of file aes.c.

References inv_mix_col, k, aes_key_st::ks, and aes_key_st::rounds.

Referenced by aes_decrypt().

00241 {
00242     int i;
00243     uint32_t *k,w,t1,t2,t3,t4;
00244 
00245     k = ctx->ks;
00246     k += 4;
00247 
00248     for (i=ctx->rounds*4; i>4; i--)
00249     {
00250         w= *k;
00251         w = inv_mix_col(w,t1,t2,t3,t4);
00252         *k++ =w;
00253     }
00254 }

void AES_encrypt ( const AES_CTX ctx,
uint32_t data 
)

Encrypt a single block (16 bytes) of data.

Definition at line 363 of file aes.c.

References aes_sbox, AES_xtime(), k, aes_key_st::ks, and aes_key_st::rounds.

Referenced by aes_encrypt().

00364 {
00365     /* To make this code smaller, generate the sbox entries on the fly.
00366      * This will have a really heavy effect upon performance.
00367      */
00368     uint32_t tmp[4];
00369     uint32_t tmp1, old_a0, a0, a1, a2, a3, row;
00370     int curr_rnd;
00371     int rounds = ctx->rounds; 
00372     const uint32_t *k = ctx->ks;
00373 
00374     /* Pre-round key addition */
00375     for (row = 0; row < 4; row++)
00376     {
00377         data[row] ^= *(k++);
00378     }
00379 
00380     /* Encrypt one block. */
00381     for (curr_rnd = 0; curr_rnd < rounds; curr_rnd++)
00382     {
00383         /* Perform ByteSub and ShiftRow operations together */
00384         for (row = 0; row < 4; row++)
00385         {
00386             a0 = (uint32_t)aes_sbox[(data[row%4]>>24)&0xFF];
00387             a1 = (uint32_t)aes_sbox[(data[(row+1)%4]>>16)&0xFF];
00388             a2 = (uint32_t)aes_sbox[(data[(row+2)%4]>>8)&0xFF]; 
00389             a3 = (uint32_t)aes_sbox[(data[(row+3)%4])&0xFF];
00390 
00391             /* Perform MixColumn iff not last round */
00392             if (curr_rnd < (rounds - 1))
00393             {
00394                 tmp1 = a0 ^ a1 ^ a2 ^ a3;
00395                 old_a0 = a0;
00396 
00397                 a0 ^= tmp1 ^ AES_xtime(a0 ^ a1);
00398                 a1 ^= tmp1 ^ AES_xtime(a1 ^ a2);
00399                 a2 ^= tmp1 ^ AES_xtime(a2 ^ a3);
00400                 a3 ^= tmp1 ^ AES_xtime(a3 ^ old_a0);
00401 
00402             }
00403 
00404             tmp[row] = ((a0 << 24) | (a1 << 16) | (a2 << 8) | a3);
00405         }
00406 
00407         /* KeyAddition - note that it is vital that this loop is separate from
00408            the MixColumn operation, which must be atomic...*/ 
00409         for (row = 0; row < 4; row++)
00410         {
00411             data[row] = tmp[row] ^ *(k++);
00412         }
00413     }
00414 }

void AES_decrypt ( const AES_CTX ctx,
uint32_t data 
)

Decrypt a single block (16 bytes) of data.

Definition at line 419 of file aes.c.

References aes_isbox, AES_xtime(), k, aes_key_st::ks, and aes_key_st::rounds.

Referenced by aes_decrypt().

00420 { 
00421     uint32_t tmp[4];
00422     uint32_t xt0,xt1,xt2,xt3,xt4,xt5,xt6;
00423     uint32_t a0, a1, a2, a3, row;
00424     int curr_rnd;
00425     int rounds = ctx->rounds;
00426     uint32_t *k = (uint32_t*)ctx->ks + ((rounds+1)*4);
00427 
00428     /* pre-round key addition */
00429     for (row=4; row > 0;row--)
00430     {
00431         data[row-1] ^= *(--k);
00432     }
00433 
00434     /* Decrypt one block */
00435     for (curr_rnd=0; curr_rnd < rounds; curr_rnd++)
00436     {
00437         /* Perform ByteSub and ShiftRow operations together */
00438         for (row = 4; row > 0; row--)
00439         {
00440             a0 = aes_isbox[(data[(row+3)%4]>>24)&0xFF];
00441             a1 = aes_isbox[(data[(row+2)%4]>>16)&0xFF];
00442             a2 = aes_isbox[(data[(row+1)%4]>>8)&0xFF];
00443             a3 = aes_isbox[(data[row%4])&0xFF];
00444 
00445             /* Perform MixColumn iff not last round */
00446             if (curr_rnd<(rounds-1))
00447             {
00448                 /* The MDS cofefficients (0x09, 0x0B, 0x0D, 0x0E)
00449                    are quite large compared to encryption; this 
00450                    operation slows decryption down noticeably. */
00451                 xt0 = AES_xtime(a0^a1);
00452                 xt1 = AES_xtime(a1^a2);
00453                 xt2 = AES_xtime(a2^a3);
00454                 xt3 = AES_xtime(a3^a0);
00455                 xt4 = AES_xtime(xt0^xt1);
00456                 xt5 = AES_xtime(xt1^xt2);
00457                 xt6 = AES_xtime(xt4^xt5);
00458 
00459                 xt0 ^= a1^a2^a3^xt4^xt6;
00460                 xt1 ^= a0^a2^a3^xt5^xt6;
00461                 xt2 ^= a0^a1^a3^xt4^xt6;
00462                 xt3 ^= a0^a1^a2^xt5^xt6;
00463                 tmp[row-1] = ((xt0<<24)|(xt1<<16)|(xt2<<8)|xt3);
00464             }
00465             else
00466                 tmp[row-1] = ((a0<<24)|(a1<<16)|(a2<<8)|a3);
00467         }
00468 
00469         for (row = 4; row > 0; row--)
00470         {
00471             data[row-1] = tmp[row-1] ^ *(--k);
00472         }
00473     }
00474 }


Variable Documentation

const uint8_t aes_sbox[256] [static]

Definition at line 72 of file aes.c.

Referenced by AES_encrypt(), and AES_set_key().

const uint8_t aes_isbox[256] [static]

Definition at line 111 of file aes.c.

Referenced by AES_decrypt().

const unsigned char Rcon[30] [static]

Initial value:

{
        0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,
        0x1b,0x36,0x6c,0xd8,0xab,0x4d,0x9a,0x2f,
        0x5e,0xbc,0x63,0xc6,0x97,0x35,0x6a,0xd4,
        0xb3,0x7d,0xfa,0xef,0xc5,0x91,
}

Definition at line 147 of file aes.c.

Referenced by AES_set_key().


Generated on Tue Apr 6 20:01:15 2010 for gPXE by  doxygen 1.5.7.1