]> pd.if.org Git - zpackage/blob - libtomcrypt/src/mac/xcbc/xcbc_done.c
remove omac
[zpackage] / libtomcrypt / src / mac / xcbc / xcbc_done.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 xcbc_done.c
13   XCBC Support, terminate the state
14 */
15
16 #ifdef LTC_XCBC
17
18 /** Terminate the XCBC-MAC state
19   @param xcbc     XCBC state to terminate
20   @param out      [out] Destination for the MAC tag
21   @param outlen   [in/out] Destination size and final tag size
22   Return CRYPT_OK on success
23 */
24 int xcbc_done(xcbc_state *xcbc, unsigned char *out, unsigned long *outlen)
25 {
26    int err, x;
27    LTC_ARGCHK(xcbc != NULL);
28    LTC_ARGCHK(out  != NULL);
29
30    /* check structure */
31    if ((err = cipher_is_valid(xcbc->cipher)) != CRYPT_OK) {
32       return err;
33    }
34
35    if ((xcbc->blocksize > cipher_descriptor[xcbc->cipher].block_length) || (xcbc->blocksize < 0) ||
36        (xcbc->buflen > xcbc->blocksize) || (xcbc->buflen < 0)) {
37       return CRYPT_INVALID_ARG;
38    }
39
40    /* which key do we use? */
41    if (xcbc->buflen == xcbc->blocksize) {
42       /* k2 */
43       for (x = 0; x < xcbc->blocksize; x++) {
44          xcbc->IV[x] ^= xcbc->K[1][x];
45       }
46    } else {
47       xcbc->IV[xcbc->buflen] ^= 0x80;
48       /* k3 */
49       for (x = 0; x < xcbc->blocksize; x++) {
50          xcbc->IV[x] ^= xcbc->K[2][x];
51       }
52    }
53
54    /* encrypt */
55    cipher_descriptor[xcbc->cipher].ecb_encrypt(xcbc->IV, xcbc->IV, &xcbc->key);
56    cipher_descriptor[xcbc->cipher].done(&xcbc->key);
57
58    /* extract tag */
59    for (x = 0; x < xcbc->blocksize && (unsigned long)x < *outlen; x++) {
60       out[x] = xcbc->IV[x];
61    }
62    *outlen = x;
63
64 #ifdef LTC_CLEAN_STACK
65    zeromem(xcbc, sizeof(*xcbc));
66 #endif
67    return CRYPT_OK;
68 }
69
70 #endif
71
72 /* ref:         $Format:%D$ */
73 /* git commit:  $Format:%H$ */
74 /* commit time: $Format:%ai$ */
75