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