Path: blob/master/libs/tomcrypt/src/modes/f8/f8_encrypt.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_encrypt.c12F8 implementation, encrypt data, Tom St Denis13*/1415#ifdef LTC_F8_MODE1617/**18F8 encrypt19@param pt Plaintext20@param ct [out] Ciphertext21@param len Length of plaintext (octets)22@param f8 F8 state23@return CRYPT_OK if successful24*/25int f8_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, symmetric_F8 *f8)26{27int err, x;28unsigned char buf[MAXBLOCKSIZE];29LTC_ARGCHK(pt != NULL);30LTC_ARGCHK(ct != NULL);31LTC_ARGCHK(f8 != NULL);32if ((err = cipher_is_valid(f8->cipher)) != CRYPT_OK) {33return err;34}3536/* is blocklen/padlen valid? */37if (f8->blocklen < 0 || f8->blocklen > (int)sizeof(f8->IV) ||38f8->padlen < 0 || f8->padlen > (int)sizeof(f8->IV)) {39return CRYPT_INVALID_ARG;40}4142zeromem(buf, sizeof(buf));4344/* make sure the pad is empty */45if (f8->padlen == f8->blocklen) {46/* xor of IV, MIV and blockcnt == what goes into cipher */47STORE32H(f8->blockcnt, (buf+(f8->blocklen-4)));48++(f8->blockcnt);49for (x = 0; x < f8->blocklen; x++) {50f8->IV[x] ^= f8->MIV[x] ^ buf[x];51}52if ((err = cipher_descriptor[f8->cipher].ecb_encrypt(f8->IV, f8->IV, &f8->key)) != CRYPT_OK) {53return err;54}55f8->padlen = 0;56}5758#ifdef LTC_FAST59if (f8->padlen == 0) {60while (len >= (unsigned long)f8->blocklen) {61STORE32H(f8->blockcnt, (buf+(f8->blocklen-4)));62++(f8->blockcnt);63for (x = 0; x < f8->blocklen; x += sizeof(LTC_FAST_TYPE)) {64*(LTC_FAST_TYPE_PTR_CAST(&ct[x])) = *(LTC_FAST_TYPE_PTR_CAST(&pt[x])) ^ *(LTC_FAST_TYPE_PTR_CAST(&f8->IV[x]));65*(LTC_FAST_TYPE_PTR_CAST(&f8->IV[x])) ^= *(LTC_FAST_TYPE_PTR_CAST(&f8->MIV[x])) ^ *(LTC_FAST_TYPE_PTR_CAST(&buf[x]));66}67if ((err = cipher_descriptor[f8->cipher].ecb_encrypt(f8->IV, f8->IV, &f8->key)) != CRYPT_OK) {68return err;69}70len -= x;71pt += x;72ct += x;73}74}75#endif7677while (len > 0) {78if (f8->padlen == f8->blocklen) {79/* xor of IV, MIV and blockcnt == what goes into cipher */80STORE32H(f8->blockcnt, (buf+(f8->blocklen-4)));81++(f8->blockcnt);82for (x = 0; x < f8->blocklen; x++) {83f8->IV[x] ^= f8->MIV[x] ^ buf[x];84}85if ((err = cipher_descriptor[f8->cipher].ecb_encrypt(f8->IV, f8->IV, &f8->key)) != CRYPT_OK) {86return err;87}88f8->padlen = 0;89}90*ct++ = *pt++ ^ f8->IV[f8->padlen++];91--len;92}93return CRYPT_OK;94}9596#endif979899