1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
6 * The library is free for all purposes without any express
13 F8 implementation, encrypt data, Tom St Denis
21 @param ct [out] Ciphertext
22 @param len Length of plaintext (octets)
24 @return CRYPT_OK if successful
26 int f8_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, symmetric_F8 *f8)
29 unsigned char buf[MAXBLOCKSIZE];
30 LTC_ARGCHK(pt != NULL);
31 LTC_ARGCHK(ct != NULL);
32 LTC_ARGCHK(f8 != NULL);
33 if ((err = cipher_is_valid(f8->cipher)) != CRYPT_OK) {
37 /* is blocklen/padlen valid? */
38 if (f8->blocklen < 0 || f8->blocklen > (int)sizeof(f8->IV) ||
39 f8->padlen < 0 || f8->padlen > (int)sizeof(f8->IV)) {
40 return CRYPT_INVALID_ARG;
43 zeromem(buf, sizeof(buf));
45 /* make sure the pad is empty */
46 if (f8->padlen == f8->blocklen) {
47 /* xor of IV, MIV and blockcnt == what goes into cipher */
48 STORE32H(f8->blockcnt, (buf+(f8->blocklen-4)));
50 for (x = 0; x < f8->blocklen; x++) {
51 f8->IV[x] ^= f8->MIV[x] ^ buf[x];
53 if ((err = cipher_descriptor[f8->cipher].ecb_encrypt(f8->IV, f8->IV, &f8->key)) != CRYPT_OK) {
60 if (f8->padlen == 0) {
61 while (len >= (unsigned long)f8->blocklen) {
62 STORE32H(f8->blockcnt, (buf+(f8->blocklen-4)));
64 for (x = 0; x < f8->blocklen; x += sizeof(LTC_FAST_TYPE)) {
65 *(LTC_FAST_TYPE_PTR_CAST(&ct[x])) = *(LTC_FAST_TYPE_PTR_CAST(&pt[x])) ^ *(LTC_FAST_TYPE_PTR_CAST(&f8->IV[x]));
66 *(LTC_FAST_TYPE_PTR_CAST(&f8->IV[x])) ^= *(LTC_FAST_TYPE_PTR_CAST(&f8->MIV[x])) ^ *(LTC_FAST_TYPE_PTR_CAST(&buf[x]));
68 if ((err = cipher_descriptor[f8->cipher].ecb_encrypt(f8->IV, f8->IV, &f8->key)) != CRYPT_OK) {
79 if (f8->padlen == f8->blocklen) {
80 /* xor of IV, MIV and blockcnt == what goes into cipher */
81 STORE32H(f8->blockcnt, (buf+(f8->blocklen-4)));
83 for (x = 0; x < f8->blocklen; x++) {
84 f8->IV[x] ^= f8->MIV[x] ^ buf[x];
86 if ((err = cipher_descriptor[f8->cipher].ecb_encrypt(f8->IV, f8->IV, &f8->key)) != CRYPT_OK) {
91 *ct++ = *pt++ ^ f8->IV[f8->padlen++];
99 /* ref: $Format:%D$ */
100 /* git commit: $Format:%H$ */
101 /* commit time: $Format:%ai$ */