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 pmac_done.c
14   PMAC implementation, terminate a session, by Tom St Denis
15 */
16 
17 #ifdef LTC_PMAC
18 
pmac_done(pmac_state * pmac,unsigned char * out,unsigned long * outlen)19 int pmac_done(pmac_state *pmac, unsigned char *out, unsigned long *outlen)
20 {
21    int err, x;
22 
23    LTC_ARGCHK(pmac != NULL);
24    LTC_ARGCHK(out  != NULL);
25    if ((err = cipher_is_valid(pmac->cipher_idx)) != CRYPT_OK) {
26       return err;
27    }
28 
29    if ((pmac->buflen > (int)sizeof(pmac->block)) || (pmac->buflen < 0) ||
30        (pmac->block_len > (int)sizeof(pmac->block)) || (pmac->buflen > pmac->block_len)) {
31       return CRYPT_INVALID_ARG;
32    }
33 
34 
35    /* handle padding.  If multiple xor in L/x */
36 
37    if (pmac->buflen == pmac->block_len) {
38       /* xor Lr against the checksum */
39       for (x = 0; x < pmac->block_len; x++) {
40           pmac->checksum[x] ^= pmac->block[x] ^ pmac->Lr[x];
41       }
42    } else {
43       /* otherwise xor message bytes then the 0x80 byte */
44       for (x = 0; x < pmac->buflen; x++) {
45           pmac->checksum[x] ^= pmac->block[x];
46       }
47       pmac->checksum[x] ^= 0x80;
48    }
49 
50    /* encrypt it */
51    if ((err = cipher_descriptor[pmac->cipher_idx]->ecb_encrypt(pmac->checksum, pmac->checksum, &pmac->key)) != CRYPT_OK) {
52       return err;
53    }
54    cipher_descriptor[pmac->cipher_idx]->done(&pmac->key);
55 
56    /* store it */
57    for (x = 0; x < pmac->block_len && x < (int)*outlen; x++) {
58        out[x] = pmac->checksum[x];
59    }
60    *outlen = x;
61 
62 #ifdef LTC_CLEAN_STACK
63    zeromem(pmac, sizeof(*pmac));
64 #endif
65    return CRYPT_OK;
66 }
67 
68 #endif
69 
70 
71 /* ref:         $Format:%D$ */
72 /* git commit:  $Format:%H$ */
73 /* commit time: $Format:%ai$ */
74