1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
3 #include "tomcrypt_private.h"
4 
5 /**
6   @file f9_process.c
7   f9 Support, process blocks with f9
8 */
9 
10 #ifdef LTC_F9_MODE
11 
12 /** Process data through f9-MAC
13   @param f9       The f9-MAC state
14   @param in       Input data to process
15   @param inlen    Length of input in octets
16   Return CRYPT_OK on success
17 */
f9_process(f9_state * f9,const unsigned char * in,unsigned long inlen)18 int f9_process(f9_state *f9, const unsigned char *in, unsigned long inlen)
19 {
20    int err, x;
21 
22    LTC_ARGCHK(f9 != NULL);
23    LTC_ARGCHK(in   != NULL);
24 
25    /* check structure */
26    if ((err = cipher_is_valid(f9->cipher)) != CRYPT_OK) {
27       return err;
28    }
29 
30    if ((f9->blocksize > cipher_descriptor[f9->cipher]->block_length) || (f9->blocksize < 0) ||
31        (f9->buflen > f9->blocksize) || (f9->buflen < 0)) {
32       return CRYPT_INVALID_ARG;
33    }
34 
35 #ifdef LTC_FAST
36    if (f9->buflen == 0) {
37        while (inlen >= (unsigned long)f9->blocksize) {
38            for (x = 0; x < f9->blocksize; x += sizeof(LTC_FAST_TYPE)) {
39               *(LTC_FAST_TYPE_PTR_CAST(&(f9->IV[x]))) ^= *(LTC_FAST_TYPE_PTR_CAST(&(in[x])));
40            }
41            cipher_descriptor[f9->cipher]->ecb_encrypt(f9->IV, f9->IV, &f9->key);
42            for (x = 0; x < f9->blocksize; x += sizeof(LTC_FAST_TYPE)) {
43               *(LTC_FAST_TYPE_PTR_CAST(&(f9->ACC[x]))) ^= *(LTC_FAST_TYPE_PTR_CAST(&(f9->IV[x])));
44            }
45            in    += f9->blocksize;
46            inlen -= f9->blocksize;
47        }
48    }
49 #endif
50 
51    while (inlen) {
52       if (f9->buflen == f9->blocksize) {
53          cipher_descriptor[f9->cipher]->ecb_encrypt(f9->IV, f9->IV, &f9->key);
54          for (x = 0; x < f9->blocksize; x++) {
55             f9->ACC[x] ^= f9->IV[x];
56          }
57          f9->buflen = 0;
58       }
59       f9->IV[f9->buflen++] ^= *in++;
60       --inlen;
61    }
62    return CRYPT_OK;
63 }
64 
65 #endif
66 
67