]> pd.if.org Git - zpackage/blob - libtomcrypt/src/mac/f9/f9_done.c
8d2ccb056b330f3470bf9bf693787cdc126ce2e7
[zpackage] / libtomcrypt / src / mac / f9 / f9_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 f9_done.c
13   f9 Support, terminate the state
14 */
15
16 #ifdef LTC_F9_MODE
17
18 /** Terminate the f9-MAC state
19   @param f9     f9 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 f9_done(f9_state *f9, unsigned char *out, unsigned long *outlen)
25 {
26    int err, x;
27    LTC_ARGCHK(f9 != NULL);
28    LTC_ARGCHK(out  != NULL);
29
30    /* check structure */
31    if ((err = cipher_is_valid(f9->cipher)) != CRYPT_OK) {
32       return err;
33    }
34
35    if ((f9->blocksize > cipher_descriptor[f9->cipher].block_length) || (f9->blocksize < 0) ||
36        (f9->buflen > f9->blocksize) || (f9->buflen < 0)) {
37       return CRYPT_INVALID_ARG;
38    }
39
40    if (f9->buflen != 0) {
41       /* encrypt */
42       cipher_descriptor[f9->cipher].ecb_encrypt(f9->IV, f9->IV, &f9->key);
43       f9->buflen = 0;
44       for (x = 0; x < f9->blocksize; x++) {
45          f9->ACC[x] ^= f9->IV[x];
46       }
47    }
48
49    /* schedule modified key */
50    if ((err = cipher_descriptor[f9->cipher].setup(f9->akey, f9->keylen, 0, &f9->key)) != CRYPT_OK) {
51       return err;
52    }
53
54    /* encrypt the ACC */
55    cipher_descriptor[f9->cipher].ecb_encrypt(f9->ACC, f9->ACC, &f9->key);
56    cipher_descriptor[f9->cipher].done(&f9->key);
57
58    /* extract tag */
59    for (x = 0; x < f9->blocksize && (unsigned long)x < *outlen; x++) {
60       out[x] = f9->ACC[x];
61    }
62    *outlen = x;
63
64 #ifdef LTC_CLEAN_STACK
65    zeromem(f9, sizeof(*f9));
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