Home | History | Annotate | Download | only in f8
      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 f8_encrypt.c
     15   F8 implementation, encrypt data, Tom St Denis
     16 */
     17 
     18 #ifdef LTC_F8_MODE
     19 
     20 /**
     21   F8 encrypt
     22   @param pt     Plaintext
     23   @param ct     [out] Ciphertext
     24   @param len    Length of plaintext (octets)
     25   @param f8     F8 state
     26   @return CRYPT_OK if successful
     27 */
     28 int f8_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, symmetric_F8 *f8)
     29 {
     30    int           err, x;
     31    unsigned char buf[MAXBLOCKSIZE];
     32    LTC_ARGCHK(pt != NULL);
     33    LTC_ARGCHK(ct != NULL);
     34    LTC_ARGCHK(f8 != NULL);
     35    if ((err = cipher_is_valid(f8->cipher)) != CRYPT_OK) {
     36        return err;
     37    }
     38 
     39    /* is blocklen/padlen valid? */
     40    if (f8->blocklen < 0 || f8->blocklen > (int)sizeof(f8->IV) ||
     41        f8->padlen   < 0 || f8->padlen   > (int)sizeof(f8->IV)) {
     42       return CRYPT_INVALID_ARG;
     43    }
     44 
     45    zeromem(buf, sizeof(buf));
     46 
     47    /* make sure the pad is empty */
     48    if (f8->padlen == f8->blocklen) {
     49       /* xor of IV, MIV and blockcnt == what goes into cipher */
     50       STORE32H(f8->blockcnt, (buf+(f8->blocklen-4)));
     51       ++(f8->blockcnt);
     52       for (x = 0; x < f8->blocklen; x++) {
     53           f8->IV[x] ^= f8->MIV[x] ^ buf[x];
     54       }
     55       if ((err = cipher_descriptor[f8->cipher].ecb_encrypt(f8->IV, f8->IV, &f8->key)) != CRYPT_OK) {
     56          return err;
     57       }
     58       f8->padlen = 0;
     59    }
     60 
     61 #ifdef LTC_FAST
     62    if (f8->padlen == 0) {
     63       while (len >= (unsigned long)f8->blocklen) {
     64          STORE32H(f8->blockcnt, (buf+(f8->blocklen-4)));
     65          ++(f8->blockcnt);
     66          for (x = 0; x < f8->blocklen; x += sizeof(LTC_FAST_TYPE)) {
     67              *((LTC_FAST_TYPE*)(&ct[x])) = *((LTC_FAST_TYPE*)(&pt[x])) ^ *((LTC_FAST_TYPE*)(&f8->IV[x]));
     68              *((LTC_FAST_TYPE*)(&f8->IV[x])) ^= *((LTC_FAST_TYPE*)(&f8->MIV[x])) ^ *((LTC_FAST_TYPE*)(&buf[x]));
     69          }
     70          if ((err = cipher_descriptor[f8->cipher].ecb_encrypt(f8->IV, f8->IV, &f8->key)) != CRYPT_OK) {
     71             return err;
     72          }
     73          len -= x;
     74          pt  += x;
     75          ct  += x;
     76       }
     77    }
     78 #endif
     79 
     80    while (len > 0) {
     81        if (f8->padlen == f8->blocklen) {
     82           /* xor of IV, MIV and blockcnt == what goes into cipher */
     83           STORE32H(f8->blockcnt, (buf+(f8->blocklen-4)));
     84           ++(f8->blockcnt);
     85           for (x = 0; x < f8->blocklen; x++) {
     86               f8->IV[x] ^= f8->MIV[x] ^ buf[x];
     87           }
     88           if ((err = cipher_descriptor[f8->cipher].ecb_encrypt(f8->IV, f8->IV, &f8->key)) != CRYPT_OK) {
     89              return err;
     90           }
     91           f8->padlen = 0;
     92        }
     93        *ct++ = *pt++ ^ f8->IV[f8->padlen++];
     94        --len;
     95    }
     96    return CRYPT_OK;
     97 }
     98 
     99 #endif
    100 
    101 /* $Source: /cvs/libtom/libtomcrypt/src/modes/f8/f8_encrypt.c,v $ */
    102 /* $Revision: 1.6 $ */
    103 /* $Date: 2006/11/05 04:16:32 $ */
    104