Coverage Report

Created: 2025-07-11 06:12

/src/dropbear/libtomcrypt/src/modes/ctr/ctr_start.c
Line
Count
Source (jump to first uncovered line)
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 ctr_start.c
13
   CTR implementation, start chain, Tom St Denis
14
*/
15
16
17
#ifdef LTC_CTR_MODE
18
19
/**
20
   Initialize a CTR context
21
   @param cipher      The index of the cipher desired
22
   @param IV          The initialization vector
23
   @param key         The secret key
24
   @param keylen      The length of the secret key (octets)
25
   @param num_rounds  Number of rounds in the cipher desired (0 for default)
26
   @param ctr_mode    The counter mode (CTR_COUNTER_LITTLE_ENDIAN or CTR_COUNTER_BIG_ENDIAN)
27
   @param ctr         The CTR state to initialize
28
   @return CRYPT_OK if successful
29
*/
30
int ctr_start(               int   cipher,
31
              const unsigned char *IV,
32
              const unsigned char *key,       int keylen,
33
                             int  num_rounds, int ctr_mode,
34
                   symmetric_CTR *ctr)
35
22.6k
{
36
22.6k
   int x, err;
37
38
22.6k
   LTC_ARGCHK(IV  != NULL);
39
22.6k
   LTC_ARGCHK(key != NULL);
40
22.6k
   LTC_ARGCHK(ctr != NULL);
41
42
   /* bad param? */
43
22.6k
   if ((err = cipher_is_valid(cipher)) != CRYPT_OK) {
44
0
      return err;
45
0
   }
46
47
   /* ctrlen == counter width */
48
22.6k
   ctr->ctrlen   = (ctr_mode & 255) ? (ctr_mode & 255) : cipher_descriptor[cipher].block_length;
49
22.6k
   if (ctr->ctrlen > cipher_descriptor[cipher].block_length) {
50
0
      return CRYPT_INVALID_ARG;
51
0
   }
52
53
22.6k
   if ((ctr_mode & 0x1000) == CTR_COUNTER_BIG_ENDIAN) {
54
22.6k
      ctr->ctrlen = cipher_descriptor[cipher].block_length - ctr->ctrlen;
55
22.6k
   }
56
57
   /* setup cipher */
58
22.6k
   if ((err = cipher_descriptor[cipher].setup(key, keylen, num_rounds, &ctr->key)) != CRYPT_OK) {
59
0
      return err;
60
0
   }
61
62
   /* copy ctr */
63
22.6k
   ctr->blocklen = cipher_descriptor[cipher].block_length;
64
22.6k
   ctr->cipher   = cipher;
65
22.6k
   ctr->padlen   = 0;
66
22.6k
   ctr->mode     = ctr_mode & 0x1000;
67
384k
   for (x = 0; x < ctr->blocklen; x++) {
68
362k
       ctr->ctr[x] = IV[x];
69
362k
   }
70
71
22.6k
   if (ctr_mode & LTC_CTR_RFC3686) {
72
      /* increment the IV as per RFC 3686 */
73
0
      if (ctr->mode == CTR_COUNTER_LITTLE_ENDIAN) {
74
         /* little-endian */
75
0
         for (x = 0; x < ctr->ctrlen; x++) {
76
0
             ctr->ctr[x] = (ctr->ctr[x] + (unsigned char)1) & (unsigned char)255;
77
0
             if (ctr->ctr[x] != (unsigned char)0) {
78
0
                break;
79
0
             }
80
0
         }
81
0
      } else {
82
         /* big-endian */
83
0
         for (x = ctr->blocklen-1; x >= ctr->ctrlen; x--) {
84
0
             ctr->ctr[x] = (ctr->ctr[x] + (unsigned char)1) & (unsigned char)255;
85
0
             if (ctr->ctr[x] != (unsigned char)0) {
86
0
                break;
87
0
             }
88
0
         }
89
0
      }
90
0
   }
91
92
22.6k
   return cipher_descriptor[ctr->cipher].ecb_encrypt(ctr->ctr, ctr->pad, &ctr->key);
93
22.6k
}
94
95
#endif
96
97
/* ref:         $Format:%D$ */
98
/* git commit:  $Format:%H$ */
99
/* commit time: $Format:%ai$ */