]> pd.if.org Git - zpackage/blob - libtomcrypt/src/mac/omac/omac_done.c
remove f8 lrw xts modes
[zpackage] / libtomcrypt / src / mac / omac / omac_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 omac_done.c
13   OMAC1 support, terminate a stream, Tom St Denis
14 */
15
16 #ifdef LTC_OMAC
17
18 /**
19   Terminate an OMAC stream
20   @param omac   The OMAC state
21   @param out    [out] Destination for the authentication tag
22   @param outlen [in/out]  The max size and resulting size of the authentication tag
23   @return CRYPT_OK if successful
24 */
25 int omac_done(omac_state *omac, unsigned char *out, unsigned long *outlen)
26 {
27    int       err, mode;
28    unsigned  x;
29
30    LTC_ARGCHK(omac   != NULL);
31    LTC_ARGCHK(out    != NULL);
32    LTC_ARGCHK(outlen != NULL);
33    if ((err = cipher_is_valid(omac->cipher_idx)) != CRYPT_OK) {
34       return err;
35    }
36
37    if ((omac->buflen > (int)sizeof(omac->block)) || (omac->buflen < 0) ||
38        (omac->blklen > (int)sizeof(omac->block)) || (omac->buflen > omac->blklen)) {
39       return CRYPT_INVALID_ARG;
40    }
41
42    /* figure out mode */
43    if (omac->buflen != omac->blklen) {
44       /* add the 0x80 byte */
45       omac->block[omac->buflen++] = 0x80;
46
47       /* pad with 0x00 */
48       while (omac->buflen < omac->blklen) {
49          omac->block[omac->buflen++] = 0x00;
50       }
51       mode = 1;
52    } else {
53       mode = 0;
54    }
55
56    /* now xor prev + Lu[mode] */
57    for (x = 0; x < (unsigned)omac->blklen; x++) {
58        omac->block[x] ^= omac->prev[x] ^ omac->Lu[mode][x];
59    }
60
61    /* encrypt it */
62    if ((err = cipher_descriptor[omac->cipher_idx].ecb_encrypt(omac->block, omac->block, &omac->key)) != CRYPT_OK) {
63       return err;
64    }
65    cipher_descriptor[omac->cipher_idx].done(&omac->key);
66
67    /* output it */
68    for (x = 0; x < (unsigned)omac->blklen && x < *outlen; x++) {
69        out[x] = omac->block[x];
70    }
71    *outlen = x;
72
73 #ifdef LTC_CLEAN_STACK
74    zeromem(omac, sizeof(*omac));
75 #endif
76    return CRYPT_OK;
77 }
78
79 #endif
80
81
82 /* ref:         $Format:%D$ */
83 /* git commit:  $Format:%H$ */
84 /* commit time: $Format:%ai$ */