#include "limits.h"
Go to the source code of this file.
Data Structures | |
struct | aes_decrypt_ctx |
struct | aes_encrypt_ctx |
Defines | |
#define | AES_128 |
#define | AES_BLOCK_SIZE 16 |
#define | AES_DECRYPT |
#define | AES_ENCRYPT |
#define | AES_ERR_CHK |
#define | aes_error -1 |
#define | aes_good 0 |
#define | aes_ret int |
#define | aes_rval aes_ret |
#define | KS_LENGTH 64 |
#define | N_COLS 4 |
Functions | |
aes_rval | aes_decrypt (const void *in_blk, void *out_blk, const aes_decrypt_ctx cx[1]) |
aes_rval | aes_decrypt_key128 (const void *in_key, aes_decrypt_ctx cx[1]) |
aes_rval | aes_encrypt (const void *in_blk, void *out_blk, const aes_encrypt_ctx cx[1]) |
aes_rval | aes_encrypt_key128 (const void *in_key, aes_encrypt_ctx cx[1]) |
void | gen_tabs (void) |
Definition in file aes_internal.h.
#define AES_128 |
Definition at line 60 of file aes_internal.h.
#define AES_BLOCK_SIZE 16 |
Definition at line 85 of file aes_internal.h.
#define AES_DECRYPT |
Definition at line 68 of file aes_internal.h.
#define AES_ENCRYPT |
Definition at line 67 of file aes_internal.h.
#define AES_ERR_CHK |
Definition at line 69 of file aes_internal.h.
#define aes_error -1 |
#define aes_good 0 |
Definition at line 96 of file aes_internal.h.
Referenced by aes_decrypt(), aes_decrypt_key128(), aes_encrypt(), and aes_encrypt_key128().
#define aes_ret int |
Definition at line 95 of file aes_internal.h.
#define aes_rval aes_ret |
Definition at line 103 of file aes_internal.h.
#define KS_LENGTH 64 |
Definition at line 92 of file aes_internal.h.
#define N_COLS 4 |
Definition at line 86 of file aes_internal.h.
Referenced by aes_decrypt(), aes_decrypt_key128(), aes_encrypt(), and aes_encrypt_key128().
aes_rval aes_decrypt | ( | const void * | in_blk, | |
void * | out_blk, | |||
const aes_decrypt_ctx | cx[1] | |||
) |
Definition at line 239 of file aescrypt.c.
References aes_error, aes_good, inv_lrnd, inv_rnd, aes_decrypt_ctx::ks, l_copy, locals, N_COLS, round, state_in, and state_out.
00240 { aes_32t locals(b0, b1); 00241 #ifdef dec_imvars 00242 dec_imvars; /* declare variables for inv_mcol() if needed */ 00243 #endif 00244 00245 aes_32t nr = (cx->ks[45] ^ cx->ks[52] ^ cx->ks[53] ? cx->ks[52] : 14); 00246 const aes_32t *kp = cx->ks + nr * N_COLS; 00247 00248 #ifdef AES_ERR_CHK 00249 if( (nr != 10 || !(cx->ks[0] | cx->ks[3] | cx->ks[4])) 00250 && (nr != 12 || !(cx->ks[0] | cx->ks[5] | cx->ks[6])) 00251 && (nr != 14 || !(cx->ks[0] | cx->ks[7] | cx->ks[8])) ) 00252 return aes_error; 00253 #endif 00254 00255 state_in(b0, in_blk, kp); 00256 00257 #if (DEC_UNROLL == FULL) 00258 00259 switch(nr) 00260 { 00261 case 14: 00262 round(inv_rnd, b1, b0, kp - 1 * N_COLS); 00263 round(inv_rnd, b0, b1, kp - 2 * N_COLS); 00264 kp -= 2 * N_COLS; 00265 case 12: 00266 round(inv_rnd, b1, b0, kp - 1 * N_COLS); 00267 round(inv_rnd, b0, b1, kp - 2 * N_COLS); 00268 kp -= 2 * N_COLS; 00269 case 10: 00270 round(inv_rnd, b1, b0, kp - 1 * N_COLS); 00271 round(inv_rnd, b0, b1, kp - 2 * N_COLS); 00272 round(inv_rnd, b1, b0, kp - 3 * N_COLS); 00273 round(inv_rnd, b0, b1, kp - 4 * N_COLS); 00274 round(inv_rnd, b1, b0, kp - 5 * N_COLS); 00275 round(inv_rnd, b0, b1, kp - 6 * N_COLS); 00276 round(inv_rnd, b1, b0, kp - 7 * N_COLS); 00277 round(inv_rnd, b0, b1, kp - 8 * N_COLS); 00278 round(inv_rnd, b1, b0, kp - 9 * N_COLS); 00279 round(inv_lrnd, b0, b1, kp - 10 * N_COLS); 00280 } 00281 00282 #else 00283 00284 #if (DEC_UNROLL == PARTIAL) 00285 { aes_32t rnd; 00286 for(rnd = 0; rnd < (nr >> 1) - 1; ++rnd) 00287 { 00288 kp -= N_COLS; 00289 round(inv_rnd, b1, b0, kp); 00290 kp -= N_COLS; 00291 round(inv_rnd, b0, b1, kp); 00292 } 00293 kp -= N_COLS; 00294 round(inv_rnd, b1, b0, kp); 00295 #else 00296 { aes_32t rnd; 00297 for(rnd = 0; rnd < nr - 1; ++rnd) 00298 { 00299 kp -= N_COLS; 00300 round(inv_rnd, b1, b0, kp); 00301 l_copy(b0, b1); 00302 } 00303 #endif 00304 kp -= N_COLS; 00305 round(inv_lrnd, b0, b1, kp); 00306 } 00307 #endif 00308 00309 state_out(out_blk, b0); 00310 #ifdef AES_ERR_CHK 00311 return aes_good; 00312 #endif 00313 }
aes_rval aes_decrypt_key128 | ( | const void * | in_key, | |
aes_decrypt_ctx | cx[1] | |||
) |
Definition at line 320 of file aeskey.c.
References aes_good, DEC_ROUND, inv_mcol, kd4, kdf4, kdl4, ke4, aes_decrypt_ctx::ks, N_COLS, NO_TABLES, and word_in.
00321 { aes_32t ss[5]; 00322 #ifdef d_vars 00323 d_vars; 00324 #endif 00325 cx->ks[0] = ss[0] = word_in(in_key, 0); 00326 cx->ks[1] = ss[1] = word_in(in_key, 1); 00327 cx->ks[2] = ss[2] = word_in(in_key, 2); 00328 cx->ks[3] = ss[3] = word_in(in_key, 3); 00329 00330 #if DEC_UNROLL == NONE 00331 { aes_32t i; 00332 00333 for(i = 0; i < (11 * N_COLS - 1) / 4; ++i) 00334 ke4(cx->ks, i); 00335 #if !(DEC_ROUND == NO_TABLES) 00336 for(i = N_COLS; i < 10 * N_COLS; ++i) 00337 cx->ks[i] = inv_mcol(cx->ks[i]); 00338 #endif 00339 } 00340 #else 00341 kdf4(cx->ks, 0); kd4(cx->ks, 1); 00342 kd4(cx->ks, 2); kd4(cx->ks, 3); 00343 kd4(cx->ks, 4); kd4(cx->ks, 5); 00344 kd4(cx->ks, 6); kd4(cx->ks, 7); 00345 kd4(cx->ks, 8); kdl4(cx->ks, 9); 00346 #endif 00347 00348 /* cx->ks[45] ^ cx->ks[52] ^ cx->ks[53] is zero for a 256 bit */ 00349 /* key and must be non-zero for 128 and 192 bits keys */ 00350 cx->ks[53] = cx->ks[45] = 0; 00351 cx->ks[52] = 10; 00352 #ifdef AES_ERR_CHK 00353 return aes_good; 00354 #endif 00355 }
aes_rval aes_encrypt | ( | const void * | in_blk, | |
void * | out_blk, | |||
const aes_encrypt_ctx | cx[1] | |||
) |
Definition at line 115 of file aescrypt.c.
References aes_error, aes_good, dec_fmvars, fwd_lrnd, fwd_rnd, aes_encrypt_ctx::ks, l_copy, locals, N_COLS, round, state_in, and state_out.
00116 { aes_32t locals(b0, b1); 00117 const aes_32t *kp = cx->ks; 00118 #ifdef dec_fmvars 00119 dec_fmvars; /* declare variables for fwd_mcol() if needed */ 00120 #endif 00121 00122 aes_32t nr = (kp[45] ^ kp[52] ^ kp[53] ? kp[52] : 14); 00123 00124 #ifdef AES_ERR_CHK 00125 if( (nr != 10 || !(kp[0] | kp[3] | kp[4])) 00126 && (nr != 12 || !(kp[0] | kp[5] | kp[6])) 00127 && (nr != 14 || !(kp[0] | kp[7] | kp[8])) ) 00128 return aes_error; 00129 #endif 00130 00131 state_in(b0, in_blk, kp); 00132 00133 #if (ENC_UNROLL == FULL) 00134 00135 switch(nr) 00136 { 00137 case 14: 00138 round(fwd_rnd, b1, b0, kp + 1 * N_COLS); 00139 round(fwd_rnd, b0, b1, kp + 2 * N_COLS); 00140 kp += 2 * N_COLS; 00141 case 12: 00142 round(fwd_rnd, b1, b0, kp + 1 * N_COLS); 00143 round(fwd_rnd, b0, b1, kp + 2 * N_COLS); 00144 kp += 2 * N_COLS; 00145 case 10: 00146 round(fwd_rnd, b1, b0, kp + 1 * N_COLS); 00147 round(fwd_rnd, b0, b1, kp + 2 * N_COLS); 00148 round(fwd_rnd, b1, b0, kp + 3 * N_COLS); 00149 round(fwd_rnd, b0, b1, kp + 4 * N_COLS); 00150 round(fwd_rnd, b1, b0, kp + 5 * N_COLS); 00151 round(fwd_rnd, b0, b1, kp + 6 * N_COLS); 00152 round(fwd_rnd, b1, b0, kp + 7 * N_COLS); 00153 round(fwd_rnd, b0, b1, kp + 8 * N_COLS); 00154 round(fwd_rnd, b1, b0, kp + 9 * N_COLS); 00155 round(fwd_lrnd, b0, b1, kp +10 * N_COLS); 00156 } 00157 00158 #else 00159 00160 #if (ENC_UNROLL == PARTIAL) 00161 { aes_32t rnd; 00162 for(rnd = 0; rnd < (nr >> 1) - 1; ++rnd) 00163 { 00164 kp += N_COLS; 00165 round(fwd_rnd, b1, b0, kp); 00166 kp += N_COLS; 00167 round(fwd_rnd, b0, b1, kp); 00168 } 00169 kp += N_COLS; 00170 round(fwd_rnd, b1, b0, kp); 00171 #else 00172 { aes_32t rnd; 00173 for(rnd = 0; rnd < nr - 1; ++rnd) 00174 { 00175 kp += N_COLS; 00176 round(fwd_rnd, b1, b0, kp); 00177 l_copy(b0, b1); 00178 } 00179 #endif 00180 kp += N_COLS; 00181 round(fwd_lrnd, b0, b1, kp); 00182 } 00183 #endif 00184 00185 state_out(out_blk, b0); 00186 #ifdef AES_ERR_CHK 00187 return aes_good; 00188 #endif 00189 }
aes_rval aes_encrypt_key128 | ( | const void * | in_key, | |
aes_encrypt_ctx | cx[1] | |||
) |
Definition at line 105 of file aeskey.c.
References aes_good, ke4, kel4, aes_encrypt_ctx::ks, N_COLS, and word_in.
00106 { aes_32t ss[4]; 00107 00108 cx->ks[0] = ss[0] = word_in(in_key, 0); 00109 cx->ks[1] = ss[1] = word_in(in_key, 1); 00110 cx->ks[2] = ss[2] = word_in(in_key, 2); 00111 cx->ks[3] = ss[3] = word_in(in_key, 3); 00112 00113 #if ENC_UNROLL == NONE 00114 { aes_32t i; 00115 00116 for(i = 0; i < ((11 * N_COLS - 1) / 4); ++i) 00117 ke4(cx->ks, i); 00118 } 00119 #else 00120 ke4(cx->ks, 0); ke4(cx->ks, 1); 00121 ke4(cx->ks, 2); ke4(cx->ks, 3); 00122 ke4(cx->ks, 4); ke4(cx->ks, 5); 00123 ke4(cx->ks, 6); ke4(cx->ks, 7); 00124 ke4(cx->ks, 8); kel4(cx->ks, 9); 00125 #endif 00126 00127 /* cx->ks[45] ^ cx->ks[52] ^ cx->ks[53] is zero for a 256 bit */ 00128 /* key and must be non-zero for 128 and 192 bits keys */ 00129 cx->ks[53] = cx->ks[45] = 0; 00130 cx->ks[52] = 10; 00131 #ifdef AES_ERR_CHK 00132 return aes_good; 00133 #endif 00134 }