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_start.c
     15    F8 implementation, start chain, Tom St Denis
     16 */
     17 
     18 
     19 #ifdef LTC_F8_MODE
     20 
     21 /**
     22    Initialize an F8 context
     23    @param cipher      The index of the cipher desired
     24    @param IV          The initial vector
     25    @param key         The secret key
     26    @param keylen      The length of the secret key (octets)
     27    @param salt_key    The salting key for the IV
     28    @param skeylen     The length of the salting key (octets)
     29    @param num_rounds  Number of rounds in the cipher desired (0 for default)
     30    @param f8          The F8 state to initialize
     31    @return CRYPT_OK if successful
     32 */
     33 int f8_start(                int  cipher, const unsigned char *IV,
     34              const unsigned char *key,                    int  keylen,
     35              const unsigned char *salt_key,               int  skeylen,
     36                              int  num_rounds,   symmetric_F8  *f8)
     37 {
     38    int           x, err;
     39    unsigned char tkey[MAXBLOCKSIZE];
     40 
     41    LTC_ARGCHK(IV       != NULL);
     42    LTC_ARGCHK(key      != NULL);
     43    LTC_ARGCHK(salt_key != NULL);
     44    LTC_ARGCHK(f8       != NULL);
     45 
     46    if ((err = cipher_is_valid(cipher)) != CRYPT_OK) {
     47       return err;
     48    }
     49 
     50 #ifdef LTC_FAST
     51    if (cipher_descriptor[cipher].block_length % sizeof(LTC_FAST_TYPE)) {
     52       return CRYPT_INVALID_ARG;
     53    }
     54 #endif
     55 
     56    /* copy details */
     57    f8->blockcnt = 0;
     58    f8->cipher   = cipher;
     59    f8->blocklen = cipher_descriptor[cipher].block_length;
     60    f8->padlen   = f8->blocklen;
     61 
     62    /* now get key ^ salt_key [extend salt_ket with 0x55 as required to match length] */
     63    zeromem(tkey, sizeof(tkey));
     64    for (x = 0; x < keylen && x < (int)sizeof(tkey); x++) {
     65        tkey[x] = key[x];
     66    }
     67    for (x = 0; x < skeylen && x < (int)sizeof(tkey); x++) {
     68        tkey[x] ^= salt_key[x];
     69    }
     70    for (; x < keylen && x < (int)sizeof(tkey); x++) {
     71        tkey[x] ^= 0x55;
     72    }
     73 
     74    /* now encrypt with tkey[0..keylen-1] the IV and use that as the IV */
     75    if ((err = cipher_descriptor[cipher].setup(tkey, keylen, num_rounds, &f8->key)) != CRYPT_OK) {
     76       return err;
     77    }
     78 
     79    /* encrypt IV */
     80    if ((err = cipher_descriptor[f8->cipher].ecb_encrypt(IV, f8->MIV, &f8->key)) != CRYPT_OK) {
     81       cipher_descriptor[f8->cipher].done(&f8->key);
     82       return err;
     83    }
     84    zeromem(tkey, sizeof(tkey));
     85    zeromem(f8->IV, sizeof(f8->IV));
     86 
     87    /* terminate this cipher */
     88    cipher_descriptor[f8->cipher].done(&f8->key);
     89 
     90    /* init the cipher */
     91    return cipher_descriptor[cipher].setup(key, keylen, num_rounds, &f8->key);
     92 }
     93 
     94 #endif
     95 
     96 /* $Source: /cvs/libtom/libtomcrypt/src/modes/f8/f8_start.c,v $ */
     97 /* $Revision: 1.7 $ */
     98 /* $Date: 2006/11/05 01:36:43 $ */
     99