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-2024 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.4.4
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  //Standard incrementing function
97  ctrIncBlock(t, 1, cipher->blockSize, m);
98 
99  //Next block
100  p += n;
101  c += n;
102  length -= n;
103  }
104 
105  //Successful encryption
106  return NO_ERROR;
107 }
108 
109 
110 /**
111  * @brief CTR decryption
112  * @param[in] cipher Cipher algorithm
113  * @param[in] context Cipher algorithm context
114  * @param[in] m Size in bits of the specific part of the block to be incremented
115  * @param[in,out] t Initial counter block
116  * @param[in] c Ciphertext to be decrypted
117  * @param[out] p Plaintext resulting from the decryption
118  * @param[in] length Total number of data bytes to be decrypted
119  * @return Error code
120  **/
121 
122 error_t ctrDecrypt(const CipherAlgo *cipher, void *context, uint_t m,
123  uint8_t *t, const uint8_t *c, uint8_t *p, size_t length)
124 {
125  //Decryption is the same the as encryption with P and C interchanged
126  return ctrEncrypt(cipher, context, m, t, c, p, length);
127 }
128 
129 
130 /**
131  * @brief Increment counter block
132  * @param[in,out] ctr Pointer to the counter block
133  * @param[in] inc Value of the increment
134  * @param[in] blockSize Size of the block, in bytes
135  * @param[in] m Size of the specific part of the block to be incremented
136  **/
137 
138 void ctrIncBlock(uint8_t *ctr, uint32_t inc, size_t blockSize, size_t m)
139 {
140  size_t i;
141  uint32_t temp;
142 
143  //The function increments the right-most bytes of the block. The remaining
144  //left-most bytes remain unchanged
145  for(temp = inc, i = 1; i <= m; i++)
146  {
147  //Increment the current byte and propagate the carry
148  temp += ctr[blockSize - i];
149  ctr[blockSize - i] = temp & 0xFF;
150  temp >>= 8;
151  }
152 }
153 
154 #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:1072
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:122
CipherAlgoEncryptBlock encryptBlock
Definition: crypto.h:1076
@ 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:368
#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:1068
unsigned int uint_t
Definition: compiler_port.h:50
void ctrIncBlock(uint8_t *ctr, uint32_t inc, size_t blockSize, size_t m)
Increment counter block.
Definition: ctr.c:138
Counter(CTR) mode.
@ NO_ERROR
Success.
Definition: error.h:44
uint8_t c
Definition: ndp.h:514
Debugging facilities.