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 #ifdef LTC_CCM_MODE
13
14 /**
15 Terminate a CCM stream
16 @param ccm The CCM state
17 @param tag [out] The destination for the MAC tag
18 @param taglen [in/out] The length of the MAC tag
19 @return CRYPT_OK on success
20 */
ccm_done(ccm_state * ccm,unsigned char * tag,unsigned long * taglen)21 int ccm_done(ccm_state *ccm,
22 unsigned char *tag, unsigned long *taglen)
23 {
24 unsigned long x, y;
25 int err;
26
27 LTC_ARGCHK(ccm != NULL);
28
29 /* Check all data have been processed */
30 if (ccm->ptlen != ccm->current_ptlen) {
31 return CRYPT_ERROR;
32 }
33
34 LTC_ARGCHK(tag != NULL);
35 LTC_ARGCHK(taglen != NULL);
36
37 if (ccm->x != 0) {
38 if ((err = cipher_descriptor[ccm->cipher]->ecb_encrypt(ccm->PAD, ccm->PAD, &ccm->K)) != CRYPT_OK) {
39 return err;
40 }
41 }
42
43 /* setup CTR for the TAG (zero the count) */
44 for (y = 15; y > 15 - ccm->L; y--) {
45 ccm->ctr[y] = 0x00;
46 }
47 if ((err = cipher_descriptor[ccm->cipher]->ecb_encrypt(ccm->ctr, ccm->CTRPAD, &ccm->K)) != CRYPT_OK) {
48 return err;
49 }
50
51 cipher_descriptor[ccm->cipher]->done(&ccm->K);
52
53 /* store the TAG */
54 for (x = 0; x < 16 && x < *taglen; x++) {
55 tag[x] = ccm->PAD[x] ^ ccm->CTRPAD[x];
56 }
57 *taglen = x;
58
59 return CRYPT_OK;
60 }
61
62 #endif
63
64 /* ref: $Format:%D$ */
65 /* git commit: $Format:%H$ */
66 /* commit time: $Format:%ai$ */
67