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.0
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  uint16_t temp;
68  uint8_t o[16];
69 
70  //The parameter must be a multiple of 8
71  if((m % 8) != 0)
73 
74  //Determine the size, in bytes, of the specific part of the block
75  //to be incremented
76  m = m / 8;
77 
78  //Check the resulting value
79  if(m > cipher->blockSize)
81 
82  //Process plaintext
83  while(length > 0)
84  {
85  //CTR mode operates in a block-by-block fashion
86  n = MIN(length, cipher->blockSize);
87 
88  //Compute O(j) = CIPH(T(j))
89  cipher->encryptBlock(context, t, o);
90 
91  //Compute C(j) = P(j) XOR T(j)
92  for(i = 0; i < n; i++)
93  {
94  c[i] = p[i] ^ o[i];
95  }
96 
97  //Standard incrementing function
98  for(temp = 1, i = 1; i <= m; i++)
99  {
100  //Increment the current byte and propagate the carry
101  temp += t[cipher->blockSize - i];
102  t[cipher->blockSize - i] = temp & 0xFF;
103  temp >>= 8;
104  }
105 
106  //Next block
107  p += n;
108  c += n;
109  length -= n;
110  }
111 
112  //Successful encryption
113  return NO_ERROR;
114 }
115 
116 
117 /**
118  * @brief CTR decryption
119  * @param[in] cipher Cipher algorithm
120  * @param[in] context Cipher algorithm context
121  * @param[in] m Size in bits of the specific part of the block to be incremented
122  * @param[in,out] t Initial counter block
123  * @param[in] c Ciphertext to be decrypted
124  * @param[out] p Plaintext resulting from the decryption
125  * @param[in] length Total number of data bytes to be decrypted
126  * @return Error code
127  **/
128 
129 error_t ctrDecrypt(const CipherAlgo *cipher, void *context, uint_t m,
130  uint8_t *t, const uint8_t *c, uint8_t *p, size_t length)
131 {
132  //Decryption is the same the as encryption with P and C interchanged
133  return ctrEncrypt(cipher, context, m, t, c, p, length);
134 }
135 
136 #endif
unsigned int uint_t
Definition: compiler_port.h:50
General definitions for cryptographic algorithms.
__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
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:129
Counter(CTR) mode.
Debugging facilities.
uint8_t n
uint8_t o
error_t
Error codes.
Definition: error.h:43
@ NO_ERROR
Success.
Definition: error.h:44
@ ERROR_INVALID_PARAMETER
Invalid parameter.
Definition: error.h:47
uint8_t t
Definition: lldp_ext_med.h:212
uint8_t c
Definition: ndp.h:514
uint8_t p
Definition: ndp.h:300
uint8_t m
Definition: ndp.h:304
#define MIN(a, b)
Definition: os_port.h:63
Common interface for encryption algorithms.
Definition: crypto.h:1036
CipherAlgoEncryptBlock encryptBlock
Definition: crypto.h:1044
size_t blockSize
Definition: crypto.h:1040
uint8_t length
Definition: tcp.h:368