[BACK]Return to rijndael.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Diff for /src/usr.bin/ssh/rijndael.c between version 1.6 and 1.7

version 1.6, 2000/12/09 13:48:31 version 1.7, 2001/02/04 15:32:24
Line 54 
Line 54 
   
 #define bswap(x)    ((rotl(x, 8) & 0x00ff00ff) | (rotr(x, 8) & 0xff00ff00))  #define bswap(x)    ((rotl(x, 8) & 0x00ff00ff) | (rotr(x, 8) & 0xff00ff00))
   
 /* Extract byte from a 32 bit quantity (little endian notation)     */  /* Extract byte from a 32 bit quantity (little endian notation)     */
   
 #define byte(x,n)   ((u1byte)((x) >> (8 * n)))  #define byte(x,n)   ((u1byte)((x) >> (8 * n)))
   
Line 89 
Line 89 
   
 #define f_rn(bo, bi, n, k)                          \  #define f_rn(bo, bi, n, k)                          \
     bo[n] =  ft_tab[0][byte(bi[n],0)] ^             \      bo[n] =  ft_tab[0][byte(bi[n],0)] ^             \
              ft_tab[1][byte(bi[(n + 1) & 3],1)] ^   \               ft_tab[1][byte(bi[(n + 1) & 3],1)] ^   \
              ft_tab[2][byte(bi[(n + 2) & 3],2)] ^   \               ft_tab[2][byte(bi[(n + 2) & 3],2)] ^   \
              ft_tab[3][byte(bi[(n + 3) & 3],3)] ^ *(k + n)               ft_tab[3][byte(bi[(n + 3) & 3],3)] ^ *(k + n)
   
 #define i_rn(bo, bi, n, k)                          \  #define i_rn(bo, bi, n, k)                          \
     bo[n] =  it_tab[0][byte(bi[n],0)] ^             \      bo[n] =  it_tab[0][byte(bi[n],0)] ^             \
              it_tab[1][byte(bi[(n + 3) & 3],1)] ^   \               it_tab[1][byte(bi[(n + 3) & 3],1)] ^   \
              it_tab[2][byte(bi[(n + 2) & 3],2)] ^   \               it_tab[2][byte(bi[(n + 2) & 3],2)] ^   \
              it_tab[3][byte(bi[(n + 1) & 3],3)] ^ *(k + n)               it_tab[3][byte(bi[(n + 1) & 3],3)] ^ *(k + n)
   
 #ifdef LARGE_TABLES  #ifdef LARGE_TABLES
   
Line 109 
Line 109 
   
 #define f_rl(bo, bi, n, k)                          \  #define f_rl(bo, bi, n, k)                          \
     bo[n] =  fl_tab[0][byte(bi[n],0)] ^             \      bo[n] =  fl_tab[0][byte(bi[n],0)] ^             \
              fl_tab[1][byte(bi[(n + 1) & 3],1)] ^   \               fl_tab[1][byte(bi[(n + 1) & 3],1)] ^   \
              fl_tab[2][byte(bi[(n + 2) & 3],2)] ^   \               fl_tab[2][byte(bi[(n + 2) & 3],2)] ^   \
              fl_tab[3][byte(bi[(n + 3) & 3],3)] ^ *(k + n)               fl_tab[3][byte(bi[(n + 3) & 3],3)] ^ *(k + n)
   
 #define i_rl(bo, bi, n, k)                          \  #define i_rl(bo, bi, n, k)                          \
     bo[n] =  il_tab[0][byte(bi[n],0)] ^             \      bo[n] =  il_tab[0][byte(bi[n],0)] ^             \
              il_tab[1][byte(bi[(n + 3) & 3],1)] ^   \               il_tab[1][byte(bi[(n + 3) & 3],1)] ^   \
              il_tab[2][byte(bi[(n + 2) & 3],2)] ^   \               il_tab[2][byte(bi[(n + 2) & 3],2)] ^   \
              il_tab[3][byte(bi[(n + 1) & 3],3)] ^ *(k + n)               il_tab[3][byte(bi[(n + 1) & 3],3)] ^ *(k + n)
   
 #else  #else
   
Line 129 
Line 129 
   
 #define f_rl(bo, bi, n, k)                                      \  #define f_rl(bo, bi, n, k)                                      \
     bo[n] = (u4byte)sbx_tab[byte(bi[n],0)] ^                    \      bo[n] = (u4byte)sbx_tab[byte(bi[n],0)] ^                    \
         rotl(((u4byte)sbx_tab[byte(bi[(n + 1) & 3],1)]),  8) ^  \          rotl(((u4byte)sbx_tab[byte(bi[(n + 1) & 3],1)]),  8) ^  \
         rotl(((u4byte)sbx_tab[byte(bi[(n + 2) & 3],2)]), 16) ^  \          rotl(((u4byte)sbx_tab[byte(bi[(n + 2) & 3],2)]), 16) ^  \
         rotl(((u4byte)sbx_tab[byte(bi[(n + 3) & 3],3)]), 24) ^ *(k + n)          rotl(((u4byte)sbx_tab[byte(bi[(n + 3) & 3],3)]), 24) ^ *(k + n)
   
 #define i_rl(bo, bi, n, k)                                      \  #define i_rl(bo, bi, n, k)                                      \
     bo[n] = (u4byte)isb_tab[byte(bi[n],0)] ^                    \      bo[n] = (u4byte)isb_tab[byte(bi[n],0)] ^                    \
         rotl(((u4byte)isb_tab[byte(bi[(n + 3) & 3],1)]),  8) ^  \          rotl(((u4byte)isb_tab[byte(bi[(n + 3) & 3],1)]),  8) ^  \
         rotl(((u4byte)isb_tab[byte(bi[(n + 2) & 3],2)]), 16) ^  \          rotl(((u4byte)isb_tab[byte(bi[(n + 2) & 3],2)]), 16) ^  \
         rotl(((u4byte)isb_tab[byte(bi[(n + 1) & 3],3)]), 24) ^ *(k + n)          rotl(((u4byte)isb_tab[byte(bi[(n + 1) & 3],3)]), 24) ^ *(k + n)
   
 #endif  #endif
   
Line 160 
Line 160 
         log_tab[1] = 0; p = 1;          log_tab[1] = 0; p = 1;
   
         for(i = 0; i < 10; ++i) {          for(i = 0; i < 10; ++i) {
                 rco_tab[i] = p;                  rco_tab[i] = p;
   
                 p = (p << 1) ^ (p & 0x80 ? 0x1b : 0);                  p = (p << 1) ^ (p & 0x80 ? 0x1b : 0);
         }          }
Line 172 
Line 172 
         /* least significant end of a byte.                     */          /* least significant end of a byte.                     */
   
         for(i = 0; i < 256; ++i) {          for(i = 0; i < 256; ++i) {
                 p = (i ? pow_tab[255 - log_tab[i]] : 0); q = p;                  p = (i ? pow_tab[255 - log_tab[i]] : 0); q = p;
                 q = (q >> 7) | (q << 1); p ^= q;                  q = (q >> 7) | (q << 1); p ^= q;
                 q = (q >> 7) | (q << 1); p ^= q;                  q = (q >> 7) | (q << 1); p ^= q;
                 q = (q >> 7) | (q << 1); p ^= q;                  q = (q >> 7) | (q << 1); p ^= q;
                 q = (q >> 7) | (q << 1); p ^= q ^ 0x63;                  q = (q >> 7) | (q << 1); p ^= q ^ 0x63;
                 sbx_tab[i] = (u1byte)p; isb_tab[p] = (u1byte)i;                  sbx_tab[i] = (u1byte)p; isb_tab[p] = (u1byte)i;
         }          }
   
         for(i = 0; i < 256; ++i) {          for(i = 0; i < 256; ++i) {
                 p = sbx_tab[i];                  p = sbx_tab[i];
   
 #ifdef  LARGE_TABLES  #ifdef  LARGE_TABLES
   
                 t = p; fl_tab[0][i] = t;                  t = p; fl_tab[0][i] = t;
                 fl_tab[1][i] = rotl(t,  8);                  fl_tab[1][i] = rotl(t,  8);
                 fl_tab[2][i] = rotl(t, 16);                  fl_tab[2][i] = rotl(t, 16);
Line 194 
Line 194 
                         ((u4byte)p <<  8) |                          ((u4byte)p <<  8) |
                         ((u4byte)p << 16) |                          ((u4byte)p << 16) |
                         ((u4byte)ff_mult(3, p) << 24);                          ((u4byte)ff_mult(3, p) << 24);
   
                 ft_tab[0][i] = t;                  ft_tab[0][i] = t;
                 ft_tab[1][i] = rotl(t,  8);                  ft_tab[1][i] = rotl(t,  8);
                 ft_tab[2][i] = rotl(t, 16);                  ft_tab[2][i] = rotl(t, 16);
                 ft_tab[3][i] = rotl(t, 24);                  ft_tab[3][i] = rotl(t, 24);
   
                 p = isb_tab[i];                  p = isb_tab[i];
   
 #ifdef  LARGE_TABLES  #ifdef  LARGE_TABLES
   
                 t = p; il_tab[0][i] = t;                  t = p; il_tab[0][i] = t;
                 il_tab[1][i] = rotl(t,  8);                  il_tab[1][i] = rotl(t,  8);
                 il_tab[2][i] = rotl(t, 16);                  il_tab[2][i] = rotl(t, 16);
                 il_tab[3][i] = rotl(t, 24);                  il_tab[3][i] = rotl(t, 24);
 #endif  #endif
                 t = ((u4byte)ff_mult(14, p)) |                  t = ((u4byte)ff_mult(14, p)) |
                         ((u4byte)ff_mult( 9, p) <<  8) |                          ((u4byte)ff_mult( 9, p) <<  8) |
                         ((u4byte)ff_mult(13, p) << 16) |                          ((u4byte)ff_mult(13, p) << 16) |
                         ((u4byte)ff_mult(11, p) << 24);                          ((u4byte)ff_mult(11, p) << 24);
   
                 it_tab[0][i] = t;                  it_tab[0][i] = t;
                 it_tab[1][i] = rotl(t,  8);                  it_tab[1][i] = rotl(t,  8);
                 it_tab[2][i] = rotl(t, 16);                  it_tab[2][i] = rotl(t, 16);
                 it_tab[3][i] = rotl(t, 24);                  it_tab[3][i] = rotl(t, 24);
         }          }
   
         tab_gen = 1;          tab_gen = 1;
Line 232 
Line 232 
     t   = w ^ (x);          \      t   = w ^ (x);          \
    (y)  = u ^ v ^ w;        \     (y)  = u ^ v ^ w;        \
    (y) ^= rotr(u ^ t,  8) ^ \     (y) ^= rotr(u ^ t,  8) ^ \
           rotr(v ^ t, 16) ^ \            rotr(v ^ t, 16) ^ \
           rotr(t,24)            rotr(t,24)
   
 /* initialise the key schedule from the user supplied key   */  /* initialise the key schedule from the user supplied key   */
   
Line 271 
Line 271 
 rijndael_ctx *  rijndael_ctx *
 rijndael_set_key(rijndael_ctx *ctx, const u4byte *in_key, const u4byte key_len,  rijndael_set_key(rijndael_ctx *ctx, const u4byte *in_key, const u4byte key_len,
                  int encrypt)                   int encrypt)
 {  {
         u4byte  i, t, u, v, w;          u4byte  i, t, u, v, w;
         u4byte *e_key = ctx->e_key;          u4byte *e_key = ctx->e_key;
         u4byte *d_key = ctx->d_key;          u4byte *d_key = ctx->d_key;
Line 285 
Line 285 
   
         e_key[0] = io_swap(in_key[0]); e_key[1] = io_swap(in_key[1]);          e_key[0] = io_swap(in_key[0]); e_key[1] = io_swap(in_key[1]);
         e_key[2] = io_swap(in_key[2]); e_key[3] = io_swap(in_key[3]);          e_key[2] = io_swap(in_key[2]); e_key[3] = io_swap(in_key[3]);
   
         switch(ctx->k_len) {          switch(ctx->k_len) {
         case 4: t = e_key[3];          case 4: t = e_key[3];
                 for(i = 0; i < 10; ++i)                  for(i = 0; i < 10; ++i)
                         loop4(i);                          loop4(i);
                 break;                  break;
   
         case 6: e_key[4] = io_swap(in_key[4]); t = e_key[5] = io_swap(in_key[5]);          case 6: e_key[4] = io_swap(in_key[4]); t = e_key[5] = io_swap(in_key[5]);
                 for(i = 0; i < 8; ++i)                  for(i = 0; i < 8; ++i)
                         loop6(i);                          loop6(i);
                 break;                  break;
   
         case 8: e_key[4] = io_swap(in_key[4]); e_key[5] = io_swap(in_key[5]);          case 8: e_key[4] = io_swap(in_key[4]); e_key[5] = io_swap(in_key[5]);
                 e_key[6] = io_swap(in_key[6]); t = e_key[7] = io_swap(in_key[7]);                  e_key[6] = io_swap(in_key[6]); t = e_key[7] = io_swap(in_key[7]);
                 for(i = 0; i < 7; ++i)                  for(i = 0; i < 7; ++i)
                         loop8(i);                          loop8(i);
                 break;                  break;
         }          }
   
         if (!encrypt) {          if (!encrypt) {
Line 333 
Line 333 
   
 void  void
 rijndael_encrypt(rijndael_ctx *ctx, const u4byte *in_blk, u4byte *out_blk)  rijndael_encrypt(rijndael_ctx *ctx, const u4byte *in_blk, u4byte *out_blk)
 {  {
         u4byte k_len = ctx->k_len;          u4byte k_len = ctx->k_len;
         u4byte *e_key = ctx->e_key;          u4byte *e_key = ctx->e_key;
         u4byte  b0[4], b1[4], *kp;          u4byte  b0[4], b1[4], *kp;
Line 380 
Line 380 
   
 void  void
 rijndael_decrypt(rijndael_ctx *ctx, const u4byte *in_blk, u4byte *out_blk)  rijndael_decrypt(rijndael_ctx *ctx, const u4byte *in_blk, u4byte *out_blk)
 {  {
         u4byte  b0[4], b1[4], *kp;          u4byte  b0[4], b1[4], *kp;
         u4byte k_len = ctx->k_len;          u4byte k_len = ctx->k_len;
         u4byte *e_key = ctx->e_key;          u4byte *e_key = ctx->e_key;

Legend:
Removed from v.1.6  
changed lines
  Added in v.1.7