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-2020 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 1.9.8
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_mode/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 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
74  //the block 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  for(i = 0; i < m; i++)
98  {
99  //Increment the current byte and propagate the carry if necessary
100  if(++(t[cipher->blockSize - 1 - i]) != 0)
101  break;
102  }
103 
104  //Next block
105  p += n;
106  c += n;
107  length -= n;
108  }
109 
110  //Successful encryption
111  return NO_ERROR;
112 }
113 
114 
115 /**
116  * @brief CTR decryption
117  * @param[in] cipher Cipher algorithm
118  * @param[in] context Cipher algorithm context
119  * @param[in] m Size in bits of the specific part of the block to be incremented
120  * @param[in,out] t Initial counter block
121  * @param[in] c Ciphertext to be decrypted
122  * @param[out] p Plaintext resulting from the decryption
123  * @param[in] length Total number of data bytes to be decrypted
124  * @return Error code
125  **/
126 
127 error_t ctrDecrypt(const CipherAlgo *cipher, void *context, uint_t m,
128  uint8_t *t, const uint8_t *c, uint8_t *p, size_t length)
129 {
130  size_t i;
131  size_t n;
132  uint8_t o[16];
133 
134  //The parameter must be a multiple of 8
135  if((m % 8) != 0)
137 
138  //Determine the size, in bytes, of the specific part of
139  //the block to be incremented
140  m = m / 8;
141 
142  //Check the resulting value
143  if(m > cipher->blockSize)
145 
146  //Process ciphertext
147  while(length > 0)
148  {
149  //CTR mode operates in a block-by-block fashion
150  n = MIN(length, cipher->blockSize);
151 
152  //Compute O(j) = CIPH(T(j))
153  cipher->encryptBlock(context, t, o);
154 
155  //Compute P(j) = C(j) XOR T(j)
156  for(i = 0; i < n; i++)
157  {
158  p[i] = c[i] ^ o[i];
159  }
160 
161  //Standard incrementing function
162  for(i = 0; i < m; i++)
163  {
164  //Increment the current byte and propagate the carry if necessary
165  if(++(t[cipher->blockSize - 1 - i]) != 0)
166  break;
167  }
168 
169  //Next block
170  c += n;
171  p += n;
172  length -= n;
173  }
174 
175  //Successful encryption
176  return NO_ERROR;
177 }
178 
179 #endif
uint8_t length
Definition: coap_common.h:190
uint8_t p
Definition: ndp.h:298
uint8_t o
size_t blockSize
Definition: crypto.h:1089
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:127
CipherAlgoEncryptBlock encryptBlock
Definition: crypto.h:1093
uint8_t t
Definition: llmnr_common.h:81
Invalid parameter.
Definition: error.h:47
error_t
Error codes.
Definition: error.h:42
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.
#define MIN(a, b)
Definition: os_port.h:62
uint8_t m
Definition: ndp.h:302
uint8_t n
Common interface for encryption algorithms.
Definition: crypto.h:1084
unsigned int uint_t
Definition: compiler_port.h:45
Counter(CTR) mode.
Success.
Definition: error.h:44
uint8_t c
Definition: ndp.h:513
Debugging facilities.