Home | History | Annotate | Download | only in lrw
      1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
      2  *
      3  * LibTomCrypt is a library that provides various cryptographic
      4  * algorithms in a highly modular and flexible manner.
      5  *
      6  * The library is free for all purposes without any express
      7  * guarantee it works.
      8  *
      9  * Tom St Denis, tomstdenis (at) gmail.com, http://libtomcrypt.com
     10  */
     11 #include "tomcrypt.h"
     12 
     13 /**
     14    @file lrw_process.c
     15    LRW_MODE implementation, Encrypt/decrypt blocks, Tom St Denis
     16 */
     17 
     18 #ifdef LTC_LRW_MODE
     19 
     20 /**
     21   Process blocks with LRW, since decrypt/encrypt are largely the same they share this code.
     22   @param pt        The "input" data
     23   @param ct        [out] The "output" data
     24   @param len       The length of the input, must be a multiple of 128-bits (16 octets)
     25   @param mode      LRW_ENCRYPT or LRW_DECRYPT
     26   @param lrw       The LRW state
     27   @return  CRYPT_OK if successful
     28 */
     29 int lrw_process(const unsigned char *pt, unsigned char *ct, unsigned long len, int mode, symmetric_LRW *lrw)
     30 {
     31    unsigned char prod[16];
     32    int           x, err;
     33 #ifdef LRW_TABLES
     34    int           y;
     35 #endif
     36 
     37    LTC_ARGCHK(pt  != NULL);
     38    LTC_ARGCHK(ct  != NULL);
     39    LTC_ARGCHK(lrw != NULL);
     40 
     41    if (len & 15) {
     42       return CRYPT_INVALID_ARG;
     43    }
     44 
     45    while (len) {
     46       /* copy pad */
     47       XMEMCPY(prod, lrw->pad, 16);
     48 
     49       /* increment IV */
     50       for (x = 15; x >= 0; x--) {
     51           lrw->IV[x] = (lrw->IV[x] + 1) & 255;
     52           if (lrw->IV[x]) {
     53               break;
     54           }
     55       }
     56 
     57       /* update pad */
     58 #ifdef LRW_TABLES
     59       /* for each byte changed we undo it's affect on the pad then add the new product */
     60       for (; x < 16; x++) {
     61 #ifdef LTC_FAST
     62           for (y = 0; y < 16; y += sizeof(LTC_FAST_TYPE)) {
     63               *((LTC_FAST_TYPE *)(lrw->pad + y)) ^= *((LTC_FAST_TYPE *)(&lrw->PC[x][lrw->IV[x]][y])) ^ *((LTC_FAST_TYPE *)(&lrw->PC[x][(lrw->IV[x]-1)&255][y]));
     64           }
     65 #else
     66           for (y = 0; y < 16; y++) {
     67               lrw->pad[y] ^= lrw->PC[x][lrw->IV[x]][y] ^ lrw->PC[x][(lrw->IV[x]-1)&255][y];
     68           }
     69 #endif
     70       }
     71 #else
     72       gcm_gf_mult(lrw->tweak, lrw->IV, lrw->pad);
     73 #endif
     74 
     75       /* xor prod */
     76 #ifdef LTC_FAST
     77       for (x = 0; x < 16; x += sizeof(LTC_FAST_TYPE)) {
     78            *((LTC_FAST_TYPE *)(ct + x)) = *((LTC_FAST_TYPE *)(pt + x)) ^ *((LTC_FAST_TYPE *)(prod + x));
     79       }
     80 #else
     81       for (x = 0; x < 16; x++) {
     82          ct[x] = pt[x] ^ prod[x];
     83       }
     84 #endif
     85 
     86       /* send through cipher */
     87       if (mode == LRW_ENCRYPT) {
     88          if ((err = cipher_descriptor[lrw->cipher].ecb_encrypt(ct, ct, &lrw->key)) != CRYPT_OK) {
     89             return err;
     90          }
     91       } else {
     92          if ((err = cipher_descriptor[lrw->cipher].ecb_decrypt(ct, ct, &lrw->key)) != CRYPT_OK) {
     93             return err;
     94          }
     95       }
     96 
     97       /* xor prod */
     98 #ifdef LTC_FAST
     99       for (x = 0; x < 16; x += sizeof(LTC_FAST_TYPE)) {
    100            *((LTC_FAST_TYPE *)(ct + x)) = *((LTC_FAST_TYPE *)(ct + x)) ^ *((LTC_FAST_TYPE *)(prod + x));
    101       }
    102 #else
    103       for (x = 0; x < 16; x++) {
    104          ct[x] = ct[x] ^ prod[x];
    105       }
    106 #endif
    107 
    108       /* move to next */
    109       pt  += 16;
    110       ct  += 16;
    111       len -= 16;
    112    }
    113 
    114    return CRYPT_OK;
    115 }
    116 
    117 #endif
    118 /* $Source: /cvs/libtom/libtomcrypt/src/modes/lrw/lrw_process.c,v $ */
    119 /* $Revision: 1.10 $ */
    120 /* $Date: 2006/06/29 01:53:13 $ */
    121