1 // SPDX-License-Identifier: BSD-2-Clause
2 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3  *
4  * LibTomCrypt is a library that provides various cryptographic
5  * algorithms in a highly modular and flexible manner.
6  *
7  * The library is free for all purposes without any express
8  * guarantee it works.
9  */
10 #include "tomcrypt_private.h"
11 
12 /**
13   @file f9_process.c
14   f9 Support, process blocks with f9
15 */
16 
17 #ifdef LTC_F9_MODE
18 
19 /** Process data through f9-MAC
20   @param f9       The f9-MAC state
21   @param in       Input data to process
22   @param inlen    Length of input in octets
23   Return CRYPT_OK on success
24 */
f9_process(f9_state * f9,const unsigned char * in,unsigned long inlen)25 int f9_process(f9_state *f9, const unsigned char *in, unsigned long inlen)
26 {
27    int err, x;
28 
29    LTC_ARGCHK(f9 != NULL);
30    LTC_ARGCHK(in   != NULL);
31 
32    /* check structure */
33    if ((err = cipher_is_valid(f9->cipher)) != CRYPT_OK) {
34       return err;
35    }
36 
37    if ((f9->blocksize > cipher_descriptor[f9->cipher]->block_length) || (f9->blocksize < 0) ||
38        (f9->buflen > f9->blocksize) || (f9->buflen < 0)) {
39       return CRYPT_INVALID_ARG;
40    }
41 
42 #ifdef LTC_FAST
43    if (f9->buflen == 0) {
44        while (inlen >= (unsigned long)f9->blocksize) {
45            for (x = 0; x < f9->blocksize; x += sizeof(LTC_FAST_TYPE)) {
46               *(LTC_FAST_TYPE_PTR_CAST(&(f9->IV[x]))) ^= *(LTC_FAST_TYPE_PTR_CAST(&(in[x])));
47            }
48            cipher_descriptor[f9->cipher]->ecb_encrypt(f9->IV, f9->IV, &f9->key);
49            for (x = 0; x < f9->blocksize; x += sizeof(LTC_FAST_TYPE)) {
50               *(LTC_FAST_TYPE_PTR_CAST(&(f9->ACC[x]))) ^= *(LTC_FAST_TYPE_PTR_CAST(&(f9->IV[x])));
51            }
52            in    += f9->blocksize;
53            inlen -= f9->blocksize;
54        }
55    }
56 #endif
57 
58    while (inlen) {
59       if (f9->buflen == f9->blocksize) {
60          cipher_descriptor[f9->cipher]->ecb_encrypt(f9->IV, f9->IV, &f9->key);
61          for (x = 0; x < f9->blocksize; x++) {
62             f9->ACC[x] ^= f9->IV[x];
63          }
64          f9->buflen = 0;
65       }
66       f9->IV[f9->buflen++] ^= *in++;
67       --inlen;
68    }
69    return CRYPT_OK;
70 }
71 
72 #endif
73 
74 /* ref:         $Format:%D$ */
75 /* git commit:  $Format:%H$ */
76 /* commit time: $Format:%ai$ */
77 
78