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

Diff for /src/usr.bin/ssh/cipher.c between version 1.46 and 1.47

version 1.46, 2001/06/25 08:25:36 version 1.47, 2001/08/23 11:31:59
Line 283 
Line 283 
 static void  static void
 rijndael_setkey(CipherContext *cc, const u_char *key, u_int keylen)  rijndael_setkey(CipherContext *cc, const u_char *key, u_int keylen)
 {  {
         rijndael_set_key(&cc->u.rijndael.enc, (u4byte *)key, 8*keylen, 1);          rijndael_set_key(&cc->u.rijndael.enc, (char *)key, 8*keylen, 1);
         rijndael_set_key(&cc->u.rijndael.dec, (u4byte *)key, 8*keylen, 0);          rijndael_set_key(&cc->u.rijndael.dec, (char *)key, 8*keylen, 0);
 }  }
 static void  static void
 rijndael_setiv(CipherContext *cc, const u_char *iv, u_int ivlen)  rijndael_setiv(CipherContext *cc, const u_char *iv, u_int ivlen)
 {  {
         if (iv == NULL)          if (iv == NULL || ivlen != RIJNDAEL_BLOCKSIZE)
                 fatal("no IV for %s.", cc->cipher->name);                  fatal("bad/no IV for %s.", cc->cipher->name);
         memcpy((u_char *)cc->u.rijndael.iv, iv, RIJNDAEL_BLOCKSIZE);          memcpy(cc->u.rijndael.iv, iv, RIJNDAEL_BLOCKSIZE);
 }  }
 static void  static void
 rijndael_cbc_encrypt(CipherContext *cc, u_char *dest, const u_char *src,  rijndael_cbc_encrypt(CipherContext *cc, u_char *dest, const u_char *src,
     u_int len)      u_int len)
 {  {
         rijndael_ctx *ctx = &cc->u.rijndael.enc;          rijndael_ctx *ctx = &cc->u.rijndael.enc;
         u4byte *iv = cc->u.rijndael.iv;          u_char *iv = cc->u.rijndael.iv;
         u4byte in[4];          u_char in[RIJNDAEL_BLOCKSIZE];
         u4byte *cprev, *cnow, *plain;          u_char *cprev, *cnow, *plain;
         int i, blocks = len / RIJNDAEL_BLOCKSIZE;          int i, j, blocks = len / RIJNDAEL_BLOCKSIZE;
   
         if (len == 0)          if (len == 0)
                 return;                  return;
         if (len % RIJNDAEL_BLOCKSIZE)          if (len % RIJNDAEL_BLOCKSIZE)
                 fatal("rijndael_cbc_encrypt: bad len %d", len);                  fatal("rijndael_cbc_encrypt: bad len %d", len);
         cnow  = (u4byte*) dest;          cnow  = dest;
         plain = (u4byte*) src;          plain = (u_char *) src;
         cprev = iv;          cprev = iv;
         for(i = 0; i < blocks; i++, plain+=4, cnow+=4) {          for (i = 0; i < blocks; i++, plain+=RIJNDAEL_BLOCKSIZE,
                 in[0] = plain[0] ^ cprev[0];              cnow+=RIJNDAEL_BLOCKSIZE) {
                 in[1] = plain[1] ^ cprev[1];                  for (j = 0; j < RIJNDAEL_BLOCKSIZE; j++)
                 in[2] = plain[2] ^ cprev[2];                          in[j] = plain[j] ^ cprev[j];
                 in[3] = plain[3] ^ cprev[3];  
                 rijndael_encrypt(ctx, in, cnow);                  rijndael_encrypt(ctx, in, cnow);
                 cprev = cnow;                  cprev = cnow;
         }          }
         memcpy(iv, cprev, RIJNDAEL_BLOCKSIZE);          memcpy(iv, cprev, RIJNDAEL_BLOCKSIZE);
 }  }
   
 static void  static void
 rijndael_cbc_decrypt(CipherContext *cc, u_char *dest, const u_char *src,  rijndael_cbc_decrypt(CipherContext *cc, u_char *dest, const u_char *src,
     u_int len)      u_int len)
 {  {
         rijndael_ctx *ctx = &cc->u.rijndael.dec;          rijndael_ctx *ctx = &cc->u.rijndael.dec;
         u4byte *iv = cc->u.rijndael.iv;          u_char *iv = cc->u.rijndael.iv;
         u4byte ivsaved[4];          u_char ivsaved[RIJNDAEL_BLOCKSIZE];
         u4byte *cnow =  (u4byte*) (src+len-RIJNDAEL_BLOCKSIZE);          u_char *cnow  = (u_char *) (src+len-RIJNDAEL_BLOCKSIZE);
         u4byte *plain = (u4byte*) (dest+len-RIJNDAEL_BLOCKSIZE);          u_char *plain = dest+len-RIJNDAEL_BLOCKSIZE;
         u4byte *ivp;          u_char *ivp;
         int i, blocks = len / RIJNDAEL_BLOCKSIZE;          int i, j, blocks = len / RIJNDAEL_BLOCKSIZE;
   
         if (len == 0)          if (len == 0)
                 return;                  return;
         if (len % RIJNDAEL_BLOCKSIZE)          if (len % RIJNDAEL_BLOCKSIZE)
                 fatal("rijndael_cbc_decrypt: bad len %d", len);                  fatal("rijndael_cbc_decrypt: bad len %d", len);
         memcpy(ivsaved, cnow, RIJNDAEL_BLOCKSIZE);          memcpy(ivsaved, cnow, RIJNDAEL_BLOCKSIZE);
         for(i = blocks; i > 0; i--, cnow-=4, plain-=4) {          for (i = blocks; i > 0; i--, cnow-=RIJNDAEL_BLOCKSIZE,
               plain-=RIJNDAEL_BLOCKSIZE) {
                 rijndael_decrypt(ctx, cnow, plain);                  rijndael_decrypt(ctx, cnow, plain);
                 ivp =  (i == 1) ? iv : cnow-4;                  ivp = (i == 1) ? iv : cnow-RIJNDAEL_BLOCKSIZE;
                 plain[0] ^= ivp[0];                  for (j = 0; j < RIJNDAEL_BLOCKSIZE; j++)
                 plain[1] ^= ivp[1];                          plain[j] ^= ivp[j];
                 plain[2] ^= ivp[2];  
                 plain[3] ^= ivp[3];  
         }          }
         memcpy(iv, ivsaved, RIJNDAEL_BLOCKSIZE);          memcpy(iv, ivsaved, RIJNDAEL_BLOCKSIZE);
 }  }

Legend:
Removed from v.1.46  
changed lines
  Added in v.1.47