aes-internal-enc.c

Go to the documentation of this file.
00001 
00025 #include "includes.h"
00026 
00027 #include "common.h"
00028 #include "crypto.h"
00029 #include "aes_i.h"
00030 
00031 void rijndaelEncrypt(const u32 rk[/*44*/], const u8 pt[16], u8 ct[16])
00032 {
00033         u32 s0, s1, s2, s3, t0, t1, t2, t3;
00034         const int Nr = 10;
00035 #ifndef FULL_UNROLL
00036         int r;
00037 #endif /* ?FULL_UNROLL */
00038 
00039         /*
00040          * map byte array block to cipher state
00041          * and add initial round key:
00042          */
00043         s0 = GETU32(pt     ) ^ rk[0];
00044         s1 = GETU32(pt +  4) ^ rk[1];
00045         s2 = GETU32(pt +  8) ^ rk[2];
00046         s3 = GETU32(pt + 12) ^ rk[3];
00047 
00048 #define ROUND(i,d,s) \
00049 d##0 = TE0(s##0) ^ TE1(s##1) ^ TE2(s##2) ^ TE3(s##3) ^ rk[4 * i]; \
00050 d##1 = TE0(s##1) ^ TE1(s##2) ^ TE2(s##3) ^ TE3(s##0) ^ rk[4 * i + 1]; \
00051 d##2 = TE0(s##2) ^ TE1(s##3) ^ TE2(s##0) ^ TE3(s##1) ^ rk[4 * i + 2]; \
00052 d##3 = TE0(s##3) ^ TE1(s##0) ^ TE2(s##1) ^ TE3(s##2) ^ rk[4 * i + 3]
00053 
00054 #ifdef FULL_UNROLL
00055 
00056         ROUND(1,t,s);
00057         ROUND(2,s,t);
00058         ROUND(3,t,s);
00059         ROUND(4,s,t);
00060         ROUND(5,t,s);
00061         ROUND(6,s,t);
00062         ROUND(7,t,s);
00063         ROUND(8,s,t);
00064         ROUND(9,t,s);
00065 
00066         rk += Nr << 2;
00067 
00068 #else  /* !FULL_UNROLL */
00069 
00070         /* Nr - 1 full rounds: */
00071         r = Nr >> 1;
00072         for (;;) {
00073                 ROUND(1,t,s);
00074                 rk += 8;
00075                 if (--r == 0)
00076                         break;
00077                 ROUND(0,s,t);
00078         }
00079 
00080 #endif /* ?FULL_UNROLL */
00081 
00082 #undef ROUND
00083 
00084         /*
00085          * apply last round and
00086          * map cipher state to byte array block:
00087          */
00088         s0 = TE41(t0) ^ TE42(t1) ^ TE43(t2) ^ TE44(t3) ^ rk[0];
00089         PUTU32(ct     , s0);
00090         s1 = TE41(t1) ^ TE42(t2) ^ TE43(t3) ^ TE44(t0) ^ rk[1];
00091         PUTU32(ct +  4, s1);
00092         s2 = TE41(t2) ^ TE42(t3) ^ TE43(t0) ^ TE44(t1) ^ rk[2];
00093         PUTU32(ct +  8, s2);
00094         s3 = TE41(t3) ^ TE42(t0) ^ TE43(t1) ^ TE44(t2) ^ rk[3];
00095         PUTU32(ct + 12, s3);
00096 }
00097 
00098 
00099 void * aes_encrypt_init(const u8 *key, size_t len)
00100 {
00101         u32 *rk;
00102         if (len != 16)
00103                 return NULL;
00104         rk = os_malloc(AES_PRIV_SIZE);
00105         if (rk == NULL)
00106                 return NULL;
00107         rijndaelKeySetupEnc(rk, key);
00108         return rk;
00109 }
00110 
00111 
00112 void aes_encrypt(void *ctx, const u8 *plain, u8 *crypt)
00113 {
00114         rijndaelEncrypt(ctx, plain, crypt);
00115 }
00116 
00117 
00118 void aes_encrypt_deinit(void *ctx)
00119 {
00120         os_memset(ctx, 0, AES_PRIV_SIZE);
00121         os_free(ctx);
00122 }
00123 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines

Generated on Sat Nov 21 23:16:49 2009 for hostapd by  doxygen 1.6.1