Path: blob/master/libs/tomcrypt/src/modes/f8/f8_start.c
5972 views
/* LibTomCrypt, modular cryptographic library -- Tom St Denis1*2* LibTomCrypt is a library that provides various cryptographic3* algorithms in a highly modular and flexible manner.4*5* The library is free for all purposes without any express6* guarantee it works.7*/8#include "tomcrypt.h"910/**11@file f8_start.c12F8 implementation, start chain, Tom St Denis13*/141516#ifdef LTC_F8_MODE1718/**19Initialize an F8 context20@param cipher The index of the cipher desired21@param IV The initialization vector22@param key The secret key23@param keylen The length of the secret key (octets)24@param salt_key The salting key for the IV25@param skeylen The length of the salting key (octets)26@param num_rounds Number of rounds in the cipher desired (0 for default)27@param f8 The F8 state to initialize28@return CRYPT_OK if successful29*/30int f8_start( int cipher, const unsigned char *IV,31const unsigned char *key, int keylen,32const unsigned char *salt_key, int skeylen,33int num_rounds, symmetric_F8 *f8)34{35int x, err;36unsigned char tkey[MAXBLOCKSIZE];3738LTC_ARGCHK(IV != NULL);39LTC_ARGCHK(key != NULL);40LTC_ARGCHK(salt_key != NULL);41LTC_ARGCHK(f8 != NULL);4243if ((err = cipher_is_valid(cipher)) != CRYPT_OK) {44return err;45}4647#ifdef LTC_FAST48if (cipher_descriptor[cipher].block_length % sizeof(LTC_FAST_TYPE)) {49return CRYPT_INVALID_ARG;50}51#endif5253/* copy details */54f8->blockcnt = 0;55f8->cipher = cipher;56f8->blocklen = cipher_descriptor[cipher].block_length;57f8->padlen = f8->blocklen;5859/* now get key ^ salt_key [extend salt_ket with 0x55 as required to match length] */60zeromem(tkey, sizeof(tkey));61for (x = 0; x < keylen && x < (int)sizeof(tkey); x++) {62tkey[x] = key[x];63}64for (x = 0; x < skeylen && x < (int)sizeof(tkey); x++) {65tkey[x] ^= salt_key[x];66}67for (; x < keylen && x < (int)sizeof(tkey); x++) {68tkey[x] ^= 0x55;69}7071/* now encrypt with tkey[0..keylen-1] the IV and use that as the IV */72if ((err = cipher_descriptor[cipher].setup(tkey, keylen, num_rounds, &f8->key)) != CRYPT_OK) {73return err;74}7576/* encrypt IV */77if ((err = cipher_descriptor[f8->cipher].ecb_encrypt(IV, f8->MIV, &f8->key)) != CRYPT_OK) {78cipher_descriptor[f8->cipher].done(&f8->key);79return err;80}81zeromem(tkey, sizeof(tkey));82zeromem(f8->IV, sizeof(f8->IV));8384/* terminate this cipher */85cipher_descriptor[f8->cipher].done(&f8->key);8687/* init the cipher */88return cipher_descriptor[cipher].setup(key, keylen, num_rounds, &f8->key);89}9091#endif929394