Sat Aug 6 00:39:33 2011

Asterisk developer's documentation


aescrypt.c File Reference

This file contains the code for implementing encryption and decryption for AES (Rijndael) for block and key sizes of 16, 24 and 32 bytes. It can optionally be replaced by code written in assembler using NASM. For further details see the file aesopt.h. More...

#include "aesopt.h"

Go to the source code of this file.

Defines

#define fwd_lrnd(y, x, k, c)   (s(y,c) = (k)[c] ^ four_tables(x,t_use(f,l),fwd_var,rf1,c))
#define fwd_rnd(y, x, k, c)   (s(y,c) = (k)[c] ^ four_tables(x,t_use(f,n),fwd_var,rf1,c))
#define fwd_var(x, r, c)
#define inv_lrnd(y, x, k, c)   (s(y,c) = (k)[c] ^ four_tables(x,t_use(i,l),inv_var,rf1,c))
#define inv_rnd(y, x, k, c)   (s(y,c) = (k)[c] ^ four_tables(x,t_use(i,n),inv_var,rf1,c))
#define inv_var(x, r, c)
#define l_copy(y, x)
#define locals(y, x)   x[4],y[4]
#define round(rm, y, x, k)   rm(y,x,k,0); rm(y,x,k,1); rm(y,x,k,2); rm(y,x,k,3)
#define si(y, x, k, c)   (s(y,c) = word_in(x, c) ^ (k)[c])
#define so(y, x, c)   word_out(y, c, s(x,c))
#define state_in(y, x, k)   si(y,x,k,0); si(y,x,k,1); si(y,x,k,2); si(y,x,k,3)
#define state_out(y, x)   so(y,x,0); so(y,x,1); so(y,x,2); so(y,x,3)

Functions

aes_rval aes_decrypt (const void *in_blk, void *out_blk, const aes_decrypt_ctx cx[1])
aes_rval aes_encrypt (const void *in_blk, void *out_blk, const aes_encrypt_ctx cx[1])


Detailed Description

This file contains the code for implementing encryption and decryption for AES (Rijndael) for block and key sizes of 16, 24 and 32 bytes. It can optionally be replaced by code written in assembler using NASM. For further details see the file aesopt.h.

Author:
Dr Brian Gladman <brg@gladman.me.uk>

Definition in file aescrypt.c.


Define Documentation

#define fwd_lrnd ( y,
x,
k,
 )     (s(y,c) = (k)[c] ^ four_tables(x,t_use(f,l),fwd_var,rf1,c))

Definition at line 106 of file aescrypt.c.

Referenced by aes_encrypt().

#define fwd_rnd ( y,
x,
k,
 )     (s(y,c) = (k)[c] ^ four_tables(x,t_use(f,n),fwd_var,rf1,c))

Definition at line 97 of file aescrypt.c.

Referenced by aes_encrypt().

#define fwd_var ( x,
r,
 ) 

Value:

( r == 0 ? ( c == 0 ? s(x,0) : c == 1 ? s(x,1) : c == 2 ? s(x,2) : s(x,3))\
 : r == 1 ? ( c == 0 ? s(x,1) : c == 1 ? s(x,2) : c == 2 ? s(x,3) : s(x,0))\
 : r == 2 ? ( c == 0 ? s(x,2) : c == 1 ? s(x,3) : c == 2 ? s(x,0) : s(x,1))\
 :          ( c == 0 ? s(x,3) : c == 1 ? s(x,0) : c == 2 ? s(x,1) : s(x,2)))

Definition at line 89 of file aescrypt.c.

#define inv_lrnd ( y,
x,
k,
 )     (s(y,c) = (k)[c] ^ four_tables(x,t_use(i,l),inv_var,rf1,c))

Definition at line 230 of file aescrypt.c.

Referenced by aes_decrypt().

#define inv_rnd ( y,
x,
k,
 )     (s(y,c) = (k)[c] ^ four_tables(x,t_use(i,n),inv_var,rf1,c))

Definition at line 221 of file aescrypt.c.

Referenced by aes_decrypt().

#define inv_var ( x,
r,
 ) 

Value:

( r == 0 ? ( c == 0 ? s(x,0) : c == 1 ? s(x,1) : c == 2 ? s(x,2) : s(x,3))\
 : r == 1 ? ( c == 0 ? s(x,3) : c == 1 ? s(x,0) : c == 2 ? s(x,1) : s(x,2))\
 : r == 2 ? ( c == 0 ? s(x,2) : c == 1 ? s(x,3) : c == 2 ? s(x,0) : s(x,1))\
 :          ( c == 0 ? s(x,1) : c == 1 ? s(x,2) : c == 2 ? s(x,3) : s(x,0)))

Definition at line 213 of file aescrypt.c.

#define l_copy ( y,
 ) 

Value:

s(y,0) = s(x,0); s(y,1) = s(x,1); \
                        s(y,2) = s(x,2); s(y,3) = s(x,3);

Definition at line 61 of file aescrypt.c.

Referenced by aes_decrypt(), and aes_encrypt().

#define locals ( y,
 )     x[4],y[4]

Definition at line 56 of file aescrypt.c.

Referenced by aes_decrypt(), aes_encrypt(), load_module(), local_alloc(), local_hangup(), local_request(), locals_show(), and unload_module().

#define round ( rm,
y,
x,
 )     rm(y,x,k,0); rm(y,x,k,1); rm(y,x,k,2); rm(y,x,k,3)

Definition at line 65 of file aescrypt.c.

Referenced by aes_decrypt(), and aes_encrypt().

#define si ( y,
x,
k,
 )     (s(y,c) = word_in(x, c) ^ (k)[c])

Definition at line 52 of file aescrypt.c.

#define so ( y,
x,
 )     word_out(y, c, s(x,c))

Definition at line 53 of file aescrypt.c.

#define state_in ( y,
x,
 )     si(y,x,k,0); si(y,x,k,1); si(y,x,k,2); si(y,x,k,3)

Definition at line 63 of file aescrypt.c.

Referenced by aes_decrypt(), and aes_encrypt().

#define state_out ( y,
 )     so(y,x,0); so(y,x,1); so(y,x,2); so(y,x,3)

Definition at line 64 of file aescrypt.c.

Referenced by aes_decrypt(), and aes_encrypt().


Function Documentation

aes_rval aes_decrypt ( const void *  in_blk,
void *  out_blk,
const aes_decrypt_ctx  cx[1] 
)

Definition at line 237 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.

Referenced by decrypt_memcpy(), and memcpy_decrypt().

00238 {   aes_32t        locals(b0, b1);
00239 #ifdef dec_imvars
00240     dec_imvars; /* declare variables for inv_mcol() if needed */
00241 #endif
00242 
00243     aes_32t nr = (cx->ks[45] ^ cx->ks[52] ^ cx->ks[53] ? cx->ks[52] : 14);
00244     const aes_32t *kp = cx->ks + nr * N_COLS;
00245 
00246 #ifdef AES_ERR_CHK
00247     if(   (nr != 10 || !(cx->ks[0] | cx->ks[3] | cx->ks[4])) 
00248        && (nr != 12 || !(cx->ks[0] | cx->ks[5] | cx->ks[6]))
00249        && (nr != 14 || !(cx->ks[0] | cx->ks[7] | cx->ks[8])) )
00250         return aes_error;
00251 #endif
00252 
00253     state_in(b0, in_blk, kp);
00254 
00255 #if (DEC_UNROLL == FULL)
00256 
00257     switch(nr)
00258     {
00259     case 14:
00260         round(inv_rnd,  b1, b0, kp -  1 * N_COLS);
00261         round(inv_rnd,  b0, b1, kp -  2 * N_COLS);
00262         kp -= 2 * N_COLS;
00263     case 12:
00264         round(inv_rnd,  b1, b0, kp -  1 * N_COLS);
00265         round(inv_rnd,  b0, b1, kp -  2 * N_COLS);
00266         kp -= 2 * N_COLS;
00267     case 10:
00268         round(inv_rnd,  b1, b0, kp -  1 * N_COLS);
00269         round(inv_rnd,  b0, b1, kp -  2 * N_COLS);
00270         round(inv_rnd,  b1, b0, kp -  3 * N_COLS);
00271         round(inv_rnd,  b0, b1, kp -  4 * N_COLS);
00272         round(inv_rnd,  b1, b0, kp -  5 * N_COLS);
00273         round(inv_rnd,  b0, b1, kp -  6 * N_COLS);
00274         round(inv_rnd,  b1, b0, kp -  7 * N_COLS);
00275         round(inv_rnd,  b0, b1, kp -  8 * N_COLS);
00276         round(inv_rnd,  b1, b0, kp -  9 * N_COLS);
00277         round(inv_lrnd, b0, b1, kp - 10 * N_COLS);
00278     }
00279 
00280 #else
00281 
00282 #if (DEC_UNROLL == PARTIAL)
00283     {   aes_32t    rnd;
00284         for(rnd = 0; rnd < (nr >> 1) - 1; ++rnd)
00285         {
00286             kp -= N_COLS;
00287             round(inv_rnd, b1, b0, kp);
00288             kp -= N_COLS;
00289             round(inv_rnd, b0, b1, kp);
00290         }
00291         kp -= N_COLS;
00292         round(inv_rnd, b1, b0, kp);
00293 #else
00294     {   aes_32t    rnd;
00295         for(rnd = 0; rnd < nr - 1; ++rnd)
00296         {
00297             kp -= N_COLS;
00298             round(inv_rnd, b1, b0, kp);
00299             l_copy(b0, b1);
00300         }
00301 #endif
00302         kp -= N_COLS;
00303         round(inv_lrnd, b0, b1, kp);
00304     }
00305 #endif
00306 
00307     state_out(out_blk, b0);
00308 #ifdef AES_ERR_CHK
00309     return aes_good;
00310 #endif
00311 }

aes_rval aes_encrypt ( const void *  in_blk,
void *  out_blk,
const aes_encrypt_ctx  cx[1] 
)

Definition at line 113 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.

Referenced by encrypt_memcpy(), and memcpy_encrypt().

00114 {   aes_32t         locals(b0, b1);
00115     const aes_32t   *kp = cx->ks;
00116 #ifdef dec_fmvars
00117     dec_fmvars; /* declare variables for fwd_mcol() if needed */
00118 #endif
00119 
00120     aes_32t nr = (kp[45] ^ kp[52] ^ kp[53] ? kp[52] : 14);
00121 
00122 #ifdef AES_ERR_CHK
00123     if(   (nr != 10 || !(kp[0] | kp[3] | kp[4])) 
00124        && (nr != 12 || !(kp[0] | kp[5] | kp[6]))
00125        && (nr != 14 || !(kp[0] | kp[7] | kp[8])) )
00126         return aes_error;
00127 #endif
00128 
00129     state_in(b0, in_blk, kp);
00130 
00131 #if (ENC_UNROLL == FULL)
00132 
00133     switch(nr)
00134     {
00135     case 14:
00136         round(fwd_rnd,  b1, b0, kp + 1 * N_COLS);
00137         round(fwd_rnd,  b0, b1, kp + 2 * N_COLS);
00138         kp += 2 * N_COLS;
00139     case 12:
00140         round(fwd_rnd,  b1, b0, kp + 1 * N_COLS);
00141         round(fwd_rnd,  b0, b1, kp + 2 * N_COLS);
00142         kp += 2 * N_COLS;
00143     case 10:
00144         round(fwd_rnd,  b1, b0, kp + 1 * N_COLS);
00145         round(fwd_rnd,  b0, b1, kp + 2 * N_COLS);
00146         round(fwd_rnd,  b1, b0, kp + 3 * N_COLS);
00147         round(fwd_rnd,  b0, b1, kp + 4 * N_COLS);
00148         round(fwd_rnd,  b1, b0, kp + 5 * N_COLS);
00149         round(fwd_rnd,  b0, b1, kp + 6 * N_COLS);
00150         round(fwd_rnd,  b1, b0, kp + 7 * N_COLS);
00151         round(fwd_rnd,  b0, b1, kp + 8 * N_COLS);
00152         round(fwd_rnd,  b1, b0, kp + 9 * N_COLS);
00153         round(fwd_lrnd, b0, b1, kp +10 * N_COLS);
00154     }
00155 
00156 #else
00157 
00158 #if (ENC_UNROLL == PARTIAL)
00159     {   aes_32t    rnd;
00160         for(rnd = 0; rnd < (nr >> 1) - 1; ++rnd)
00161         {
00162             kp += N_COLS;
00163             round(fwd_rnd, b1, b0, kp);
00164             kp += N_COLS;
00165             round(fwd_rnd, b0, b1, kp);
00166         }
00167         kp += N_COLS;
00168         round(fwd_rnd,  b1, b0, kp);
00169 #else
00170     {   aes_32t    rnd;
00171         for(rnd = 0; rnd < nr - 1; ++rnd)
00172         {
00173             kp += N_COLS;
00174             round(fwd_rnd, b1, b0, kp);
00175             l_copy(b0, b1);
00176         }
00177 #endif
00178         kp += N_COLS;
00179         round(fwd_lrnd, b0, b1, kp);
00180     }
00181 #endif
00182 
00183     state_out(out_blk, b0);
00184 #ifdef AES_ERR_CHK
00185     return aes_good;
00186 #endif
00187 }


Generated on Sat Aug 6 00:39:33 2011 for Asterisk - the Open Source PBX by  doxygen 1.4.7