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 xcbc_process.c
14   XCBC Support, process blocks with XCBC
15 */
16 
17 #ifdef LTC_XCBC
18 
19 /** Process data through XCBC-MAC
20   @param xcbc     The XCBC-MAC state
21   @param in       Input data to process
22   @param inlen    Length of input in octets
23   Return CRYPT_OK on success
24 */
xcbc_process(xcbc_state * xcbc,const unsigned char * in,unsigned long inlen)25 int xcbc_process(xcbc_state *xcbc, const unsigned char *in, unsigned long inlen)
26 {
27    int err;
28 #ifdef LTC_FAST
29    int x;
30 #endif
31 
32    LTC_ARGCHK(xcbc != NULL);
33    LTC_ARGCHK(in   != NULL);
34 
35    /* check structure */
36    if ((err = cipher_is_valid(xcbc->cipher)) != CRYPT_OK) {
37       return err;
38    }
39 
40    if ((xcbc->blocksize > cipher_descriptor[xcbc->cipher]->block_length) || (xcbc->blocksize < 0) ||
41        (xcbc->buflen > xcbc->blocksize) || (xcbc->buflen < 0)) {
42       return CRYPT_INVALID_ARG;
43    }
44 
45 #ifdef LTC_FAST
46    if (xcbc->buflen == 0) {
47        while (inlen > (unsigned long)xcbc->blocksize) {
48            for (x = 0; x < xcbc->blocksize; x += sizeof(LTC_FAST_TYPE)) {
49               *(LTC_FAST_TYPE_PTR_CAST(&(xcbc->IV[x]))) ^= *(LTC_FAST_TYPE_PTR_CAST(&(in[x])));
50            }
51            cipher_descriptor[xcbc->cipher]->ecb_encrypt(xcbc->IV, xcbc->IV, &xcbc->key);
52            in    += xcbc->blocksize;
53            inlen -= xcbc->blocksize;
54        }
55    }
56 #endif
57 
58    while (inlen) {
59       if (xcbc->buflen == xcbc->blocksize) {
60          cipher_descriptor[xcbc->cipher]->ecb_encrypt(xcbc->IV, xcbc->IV, &xcbc->key);
61          xcbc->buflen = 0;
62       }
63       xcbc->IV[xcbc->buflen++] ^= *in++;
64       --inlen;
65    }
66    return CRYPT_OK;
67 }
68 
69 #endif
70 
71 /* ref:         $Format:%D$ */
72 /* git commit:  $Format:%H$ */
73 /* commit time: $Format:%ai$ */
74 
75