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 * Tom St Denis, tomstdenis@gmail.com, http://libtomcrypt.com
10 */
11#include "tomcrypt.h"
12
13/**
14  @file xcbc_done.c
15  XCBC Support, terminate the state
16*/
17
18#ifdef LTC_XCBC
19
20/** Terminate the XCBC-MAC state
21  @param xcbc     XCBC state to terminate
22  @param out      [out] Destination for the MAC tag
23  @param outlen   [in/out] Destination size and final tag size
24  Return CRYPT_OK on success
25*/
26int xcbc_done(xcbc_state *xcbc, unsigned char *out, unsigned long *outlen)
27{
28   int err, x;
29   LTC_ARGCHK(xcbc != NULL);
30   LTC_ARGCHK(out  != NULL);
31
32   /* check structure */
33   if ((err = cipher_is_valid(xcbc->cipher)) != CRYPT_OK) {
34      return err;
35   }
36
37   if ((xcbc->blocksize > cipher_descriptor[xcbc->cipher].block_length) || (xcbc->blocksize < 0) ||
38       (xcbc->buflen > xcbc->blocksize) || (xcbc->buflen < 0)) {
39      return CRYPT_INVALID_ARG;
40   }
41
42   /* which key do we use? */
43   if (xcbc->buflen == xcbc->blocksize) {
44      /* k2 */
45      for (x = 0; x < xcbc->blocksize; x++) {
46         xcbc->IV[x] ^= xcbc->K[1][x];
47      }
48   } else {
49      xcbc->IV[xcbc->buflen] ^= 0x80;
50      /* k3 */
51      for (x = 0; x < xcbc->blocksize; x++) {
52         xcbc->IV[x] ^= xcbc->K[2][x];
53      }
54   }
55
56   /* encrypt */
57   cipher_descriptor[xcbc->cipher].ecb_encrypt(xcbc->IV, xcbc->IV, &xcbc->key);
58   cipher_descriptor[xcbc->cipher].done(&xcbc->key);
59
60   /* extract tag */
61   for (x = 0; x < xcbc->blocksize && (unsigned long)x < *outlen; x++) {
62      out[x] = xcbc->IV[x];
63   }
64   *outlen = x;
65
66#ifdef LTC_CLEAN_STACK
67   zeromem(xcbc, sizeof(*xcbc));
68#endif
69   return CRYPT_OK;
70}
71
72#endif
73
74/* $Source: /cvs/libtom/libtomcrypt/src/mac/xcbc/xcbc_done.c,v $ */
75/* $Revision: 1.4 $ */
76/* $Date: 2006/11/07 03:23:46 $ */
77
78