rc6.c
Go to the documentation of this file.
1 /**
2  * @file rc6.c
3  * @brief RC6-32/20 block cipher
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2019 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  * RC6 is a symmetric key block cipher derived from RC5
30  *
31  * @author Oryx Embedded SARL (www.oryx-embedded.com)
32  * @version 1.9.6
33  **/
34 
35 //Switch to the appropriate trace level
36 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
37 
38 //Dependencies
39 #include "core/crypto.h"
40 #include "cipher/rc6.h"
41 
42 //Check crypto library configuration
43 #if (RC6_SUPPORT == ENABLED)
44 
45 //RC6 magic constants
46 #define P32 0xB7E15163
47 #define Q32 0x9E3779B9
48 
49 //Common interface for encryption algorithms
51 {
52  "RC6",
53  sizeof(Rc6Context),
57  NULL,
58  NULL,
61 };
62 
63 
64 /**
65  * @brief Initialize a RC6 context using the supplied key
66  * @param[in] context Pointer to the RC6 context to initialize
67  * @param[in] key Pointer to the key
68  * @param[in] keyLen Length of the key
69  * @return Error code
70  **/
71 
72 error_t rc6Init(Rc6Context *context, const uint8_t *key, size_t keyLen)
73 {
74  uint_t c;
75  uint_t i;
76  uint_t j;
77  uint_t s;
78  uint_t v;
79  uint32_t a;
80  uint32_t b;
81 
82  //Check parameters
83  if(context == NULL || key == NULL)
85 
86  //Invalid key length?
87  if(keyLen > RC6_MAX_KEY_SIZE)
89 
90  //Convert the secret key from bytes to words
91  cryptoMemset(context->l, 0, RC6_MAX_KEY_SIZE);
92  cryptoMemcpy(context->l, key, keyLen);
93 
94  //Calculate the length of the key in words
95  c = (keyLen > 0) ? (keyLen + 3) / 4 : 1;
96 
97  //Initialize the first element of S
98  context->s[0] = P32;
99 
100  //Initialize array S to a particular fixed pseudo random bit pattern
101  for(i = 1; i < (2 * RC6_NB_ROUNDS + 4); i++)
102  {
103  context->s[i] = context->s[i - 1] + Q32;
104  }
105 
106  //Initialize variables
107  i = 0;
108  j = 0;
109  a = 0;
110  b = 0;
111 
112  //Number of iterations
113  v = 3 * MAX(c, 2 * RC6_NB_ROUNDS + 4);
114 
115  //Key expansion
116  for(s = 0; s < v; s++)
117  {
118  context->s[i] += a + b;
119  context->s[i] = ROL32(context->s[i], 3);
120  a = context->s[i];
121 
122  context->l[j] += a + b;
123  context->l[j] = ROL32(context->l[j], (a + b) % 32);
124  b = context->l[j];
125 
126  if(++i >= (2 * RC6_NB_ROUNDS + 4))
127  i = 0;
128  if(++j >= c)
129  j = 0;
130  }
131 
132  //No error to report
133  return NO_ERROR;
134 }
135 
136 
137 /**
138  * @brief Encrypt a 16-byte block using RC6 algorithm
139  * @param[in] context Pointer to the RC6 context
140  * @param[in] input Plaintext block to encrypt
141  * @param[out] output Ciphertext block resulting from encryption
142  **/
143 
144 void rc6EncryptBlock(Rc6Context *context, const uint8_t *input, uint8_t *output)
145 {
146  uint_t i;
147  uint32_t t;
148  uint32_t u;
149 
150  //Load the 4 working registers with the plaintext
151  uint32_t a = LOAD32LE(input + 0);
152  uint32_t b = LOAD32LE(input + 4);
153  uint32_t c = LOAD32LE(input + 8);
154  uint32_t d = LOAD32LE(input + 12);
155 
156  //First, update B and D
157  b += context->s[0];
158  d += context->s[1];
159 
160  //Apply 20 rounds
161  for(i = 1; i <= RC6_NB_ROUNDS; i++)
162  {
163  t = (b * (2 * b + 1));
164  t = ROL32(t, 5);
165 
166  u = (d * (2 * d + 1));
167  u = ROL32(u, 5);
168 
169  a ^= t;
170  a = ROL32(a, u % 32) + context->s[2 * i];
171 
172  c ^= u;
173  c = ROL32(c, t % 32) + context->s[2 * i + 1];
174 
175  t = a;
176  a = b;
177  b = c;
178  c = d;
179  d = t;
180  }
181 
182  //Update A and C
183  a += context->s[2 * RC6_NB_ROUNDS + 2];
184  c += context->s[2 * RC6_NB_ROUNDS + 3];
185 
186  //The resulting value is the ciphertext
187  STORE32LE(a, output + 0);
188  STORE32LE(b, output + 4);
189  STORE32LE(c, output + 8);
190  STORE32LE(d, output + 12);
191 }
192 
193 
194 /**
195  * @brief Decrypt a 16-byte block using RC6 algorithm
196  * @param[in] context Pointer to the RC6 context
197  * @param[in] input Ciphertext block to decrypt
198  * @param[out] output Plaintext block resulting from decryption
199  **/
200 
201 void rc6DecryptBlock(Rc6Context *context, const uint8_t *input, uint8_t *output)
202 {
203  uint_t i;
204  uint32_t t;
205  uint32_t u;
206 
207  //Load the 4 working registers with the ciphertext
208  uint32_t a = LOAD32LE(input + 0);
209  uint32_t b = LOAD32LE(input + 4);
210  uint32_t c = LOAD32LE(input + 8);
211  uint32_t d = LOAD32LE(input + 12);
212 
213  //First, update C and A
214  c -= context->s[2 * RC6_NB_ROUNDS + 3];
215  a -= context->s[2 * RC6_NB_ROUNDS + 2];
216 
217  //Apply 20 rounds
218  for(i = RC6_NB_ROUNDS; i > 0; i--)
219  {
220  t = d;
221  d = c;
222  c = b;
223  b = a;
224  a = t;
225 
226  u = (d * (2 * d + 1));
227  u = ROL32(u, 5);
228 
229  t = (b * (2 * b + 1));
230  t = ROL32(t, 5);
231 
232  c -= context->s[2 * i + 1];
233  c = ROR32(c, t % 32) ^ u;
234 
235  a -= context->s[2 * i];
236  a = ROR32(a, u % 32) ^ t;
237  }
238 
239  //Update D and B
240  d -= context->s[1];
241  b -= context->s[0];
242 
243  //The resulting value is the plaintext
244  STORE32LE(a, output + 0);
245  STORE32LE(b, output + 4);
246  STORE32LE(c, output + 8);
247  STORE32LE(d, output + 12);
248 }
249 
250 #endif
#define RC6_BLOCK_SIZE
Definition: rc6.h:38
#define ROR32(a, n)
Definition: crypto.h:923
uint8_t a
Definition: ndp.h:410
void(* CipherAlgoEncryptBlock)(void *context, const uint8_t *input, uint8_t *output)
Definition: crypto.h:1103
RC6-32/20 block cipher.
uint8_t b[6]
Definition: dtls_misc.h:139
@ CIPHER_ALGO_TYPE_BLOCK
Definition: crypto.h:1069
#define STORE32LE(a, p)
Definition: cpu_endian.h:263
#define RC6_MAX_KEY_SIZE
Definition: rc6.h:40
void rc6EncryptBlock(Rc6Context *context, const uint8_t *input, uint8_t *output)
Encrypt a 16-byte block using RC6 algorithm.
Definition: rc6.c:144
void rc6DecryptBlock(Rc6Context *context, const uint8_t *input, uint8_t *output)
Decrypt a 16-byte block using RC6 algorithm.
Definition: rc6.c:201
uint8_t t
Definition: llmnr_common.h:81
RC6 algorithm context.
Definition: rc6.h:57
@ ERROR_INVALID_PARAMETER
Invalid parameter.
Definition: error.h:47
error_t
Error codes.
Definition: error.h:42
error_t rc6Init(Rc6Context *context, const uint8_t *key, size_t keyLen)
Initialize a RC6 context using the supplied key.
Definition: rc6.c:72
void(* CipherAlgoDecryptBlock)(void *context, const uint8_t *input, uint8_t *output)
Definition: crypto.h:1104
const CipherAlgo rc6CipherAlgo
Definition: rc6.c:50
@ ERROR_INVALID_KEY_LENGTH
Definition: error.h:105
General definitions for cryptographic algorithms.
uint32_t l[RC6_MAX_KEY_SIZE/4]
Definition: rc6.h:59
#define RC6_NB_ROUNDS
Definition: rc6.h:42
error_t(* CipherAlgoInit)(void *context, const uint8_t *key, size_t keyLen)
Definition: crypto.h:1100
#define cryptoMemset(p, value, length)
Definition: crypto.h:636
#define MAX(a, b)
Definition: os_port.h:66
#define Q32
Definition: rc6.c:47
#define ROL32(a, n)
Definition: crypto.h:917
#define cryptoMemcpy(dest, src, length)
Definition: crypto.h:642
uint32_t s[2 *RC6_NB_ROUNDS+4]
Definition: rc6.h:60
uint8_t s
Common interface for encryption algorithms.
Definition: crypto.h:1150
#define LOAD32LE(p)
Definition: cpu_endian.h:187
#define P32
Definition: rc6.c:46
unsigned int uint_t
Definition: compiler_port.h:45
@ NO_ERROR
Success.
Definition: error.h:44
uint8_t c
Definition: ndp.h:513