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