ctr.c
Go to the documentation of this file.
1 /**
2  * @file ctr.c
3  * @brief Counter(CTR) mode
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2026 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneCRYPTO Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @section Description
28  *
29  * The Counter (CTR) mode is a confidentiality mode that features the application
30  * of the forward cipher to a set of input blocks, called counters, to produce
31  * a sequence of output blocks that are exclusive-ORed with the plaintext to
32  * produce the ciphertext, and vice versa. Refer to SP 800-38A for more details
33  *
34  * @author Oryx Embedded SARL (www.oryx-embedded.com)
35  * @version 2.6.6
36  **/
37 
38 //Switch to the appropriate trace level
39 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
40 
41 //Dependencies
42 #include "core/crypto.h"
43 #include "cipher_modes/ctr.h"
44 #include "debug.h"
45 
46 //Check crypto library configuration
47 #if (CTR_SUPPORT == ENABLED)
48 
49 
50 /**
51  * @brief CTR encryption
52  * @param[in] cipher Cipher algorithm
53  * @param[in] context Cipher algorithm context
54  * @param[in] m Size in bits of the specific part of the block to be incremented
55  * @param[in,out] t Initial counter block
56  * @param[in] p Plaintext to be encrypted
57  * @param[out] c Ciphertext resulting from the encryption
58  * @param[in] length Total number of data bytes to be encrypted
59  * @return Error code
60  **/
61 
62 __weak_func error_t ctrEncrypt(const CipherAlgo *cipher, void *context, uint_t m,
63  uint8_t *t, const uint8_t *p, uint8_t *c, size_t length)
64 {
65  size_t i;
66  size_t n;
67  uint8_t o[16];
68 
69  //The parameter must be a multiple of 8
70  if((m % 8) != 0)
72 
73  //Determine the size, in bytes, of the specific part of the block
74  //to be incremented
75  m = m / 8;
76 
77  //Check the resulting value
78  if(m > cipher->blockSize)
80 
81  //Process plaintext
82  while(length > 0)
83  {
84  //CTR mode operates in a block-by-block fashion
85  n = MIN(length, cipher->blockSize);
86 
87  //Compute O(j) = CIPH(T(j))
88  cipher->encryptBlock(context, t, o);
89 
90  //Compute C(j) = P(j) XOR T(j)
91  for(i = 0; i < n; i++)
92  {
93  c[i] = p[i] ^ o[i];
94  }
95 
96  //Complete block?
97  if(n == cipher->blockSize)
98  {
99  //Standard incrementing function
100  ctrIncBlock(t, 1, cipher->blockSize, m);
101  }
102 
103  //Next block
104  p += n;
105  c += n;
106  length -= n;
107  }
108 
109  //Successful encryption
110  return NO_ERROR;
111 }
112 
113 
114 /**
115  * @brief CTR decryption
116  * @param[in] cipher Cipher algorithm
117  * @param[in] context Cipher algorithm context
118  * @param[in] m Size in bits of the specific part of the block to be incremented
119  * @param[in,out] t Initial counter block
120  * @param[in] c Ciphertext to be decrypted
121  * @param[out] p Plaintext resulting from the decryption
122  * @param[in] length Total number of data bytes to be decrypted
123  * @return Error code
124  **/
125 
126 error_t ctrDecrypt(const CipherAlgo *cipher, void *context, uint_t m,
127  uint8_t *t, const uint8_t *c, uint8_t *p, size_t length)
128 {
129  //Decryption is the same the as encryption with P and C interchanged
130  return ctrEncrypt(cipher, context, m, t, c, p, length);
131 }
132 
133 
134 /**
135  * @brief Increment counter block
136  * @param[in,out] ctr Pointer to the counter block
137  * @param[in] inc Value of the increment
138  * @param[in] blockSize Size of the block, in bytes
139  * @param[in] m Size of the specific part of the block to be incremented
140  **/
141 
142 void ctrIncBlock(uint8_t *ctr, uint32_t inc, size_t blockSize, size_t m)
143 {
144  size_t i;
145  uint32_t temp;
146 
147  //The function increments the right-most bytes of the block. The remaining
148  //left-most bytes remain unchanged
149  for(temp = inc, i = 1; i <= m; i++)
150  {
151  //Increment the current byte and propagate the carry
152  temp += ctr[blockSize - i];
153  ctr[blockSize - i] = temp & 0xFF;
154  temp >>= 8;
155  }
156 }
157 
158 #endif
uint8_t p
Definition: ndp.h:300
uint8_t t
Definition: lldp_ext_med.h:212
uint8_t o
size_t blockSize
Definition: crypto.h:1289
error_t ctrDecrypt(const CipherAlgo *cipher, void *context, uint_t m, uint8_t *t, const uint8_t *c, uint8_t *p, size_t length)
CTR decryption.
Definition: ctr.c:126
CipherAlgoEncryptBlock encryptBlock
Definition: crypto.h:1293
@ ERROR_INVALID_PARAMETER
Invalid parameter.
Definition: error.h:47
error_t
Error codes.
Definition: error.h:43
__weak_func error_t ctrEncrypt(const CipherAlgo *cipher, void *context, uint_t m, uint8_t *t, const uint8_t *p, uint8_t *c, size_t length)
CTR encryption.
Definition: ctr.c:62
General definitions for cryptographic algorithms.
uint8_t length
Definition: tcp.h:375
#define MIN(a, b)
Definition: os_port.h:63
uint8_t m
Definition: ndp.h:304
uint8_t n
Common interface for encryption algorithms.
Definition: crypto.h:1285
unsigned int uint_t
Definition: compiler_port.h:57
void ctrIncBlock(uint8_t *ctr, uint32_t inc, size_t blockSize, size_t m)
Increment counter block.
Definition: ctr.c:142
Counter(CTR) mode.
@ NO_ERROR
Success.
Definition: error.h:44
uint8_t c
Definition: ndp.h:514
Debugging facilities.