Coverage Report

Created: 2024-11-21 07:03

/src/libtomcrypt/src/encauth/ccm/ccm_done.c
Line
Count
Source (jump to first uncovered line)
1
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2
/* SPDX-License-Identifier: Unlicense */
3
#include "tomcrypt_private.h"
4
5
#ifdef LTC_CCM_MODE
6
7
/**
8
  Terminate a CCM stream
9
  @param ccm     The CCM state
10
  @param tag     [out] The destination for the MAC tag
11
  @param taglen  [in/out]  The length of the MAC tag
12
  @return CRYPT_OK on success
13
 */
14
int ccm_done(ccm_state *ccm,
15
             unsigned char *tag,    unsigned long *taglen)
16
3
{
17
3
   unsigned long x, y;
18
3
   int            err;
19
20
3
   LTC_ARGCHK(ccm != NULL);
21
22
   /* Check all data have been processed */
23
3
   if (ccm->ptlen != ccm->current_ptlen) {
24
0
      return CRYPT_ERROR;
25
0
   }
26
27
3
   LTC_ARGCHK(tag    != NULL);
28
3
   LTC_ARGCHK(taglen != NULL);
29
30
3
   if (ccm->x != 0) {
31
3
      if ((err = cipher_descriptor[ccm->cipher].ecb_encrypt(ccm->PAD, ccm->PAD, &ccm->K)) != CRYPT_OK) {
32
0
         return err;
33
0
      }
34
3
   }
35
36
   /* setup CTR for the TAG (zero the count) */
37
12
   for (y = 15; y > 15 - ccm->L; y--) {
38
9
      ccm->ctr[y] = 0x00;
39
9
   }
40
3
   if ((err = cipher_descriptor[ccm->cipher].ecb_encrypt(ccm->ctr, ccm->CTRPAD, &ccm->K)) != CRYPT_OK) {
41
0
      return err;
42
0
   }
43
44
3
   cipher_descriptor[ccm->cipher].done(&ccm->K);
45
46
   /* store the TAG */
47
27
   for (x = 0; x < 16 && x < *taglen; x++) {
48
24
      tag[x] = ccm->PAD[x] ^ ccm->CTRPAD[x];
49
24
   }
50
3
   *taglen = x;
51
52
3
   return CRYPT_OK;
53
3
}
54
55
#endif