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-2022 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 2.1.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  osMemset(context->l, 0, RC6_MAX_KEY_SIZE);
92  osMemcpy(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  {
128  i = 0;
129  }
130 
131  if(++j >= c)
132  {
133  j = 0;
134  }
135  }
136 
137  //No error to report
138  return NO_ERROR;
139 }
140 
141 
142 /**
143  * @brief Encrypt a 16-byte block using RC6 algorithm
144  * @param[in] context Pointer to the RC6 context
145  * @param[in] input Plaintext block to encrypt
146  * @param[out] output Ciphertext block resulting from encryption
147  **/
148 
149 void rc6EncryptBlock(Rc6Context *context, const uint8_t *input, uint8_t *output)
150 {
151  uint_t i;
152  uint32_t t;
153  uint32_t u;
154 
155  //Load the 4 working registers with the plaintext
156  uint32_t a = LOAD32LE(input + 0);
157  uint32_t b = LOAD32LE(input + 4);
158  uint32_t c = LOAD32LE(input + 8);
159  uint32_t d = LOAD32LE(input + 12);
160 
161  //First, update B and D
162  b += context->s[0];
163  d += context->s[1];
164 
165  //Apply 20 rounds
166  for(i = 1; i <= RC6_NB_ROUNDS; i++)
167  {
168  t = (b * (2 * b + 1));
169  t = ROL32(t, 5);
170 
171  u = (d * (2 * d + 1));
172  u = ROL32(u, 5);
173 
174  a ^= t;
175  a = ROL32(a, u % 32) + context->s[2 * i];
176 
177  c ^= u;
178  c = ROL32(c, t % 32) + context->s[2 * i + 1];
179 
180  t = a;
181  a = b;
182  b = c;
183  c = d;
184  d = t;
185  }
186 
187  //Update A and C
188  a += context->s[2 * RC6_NB_ROUNDS + 2];
189  c += context->s[2 * RC6_NB_ROUNDS + 3];
190 
191  //The resulting value is the ciphertext
192  STORE32LE(a, output + 0);
193  STORE32LE(b, output + 4);
194  STORE32LE(c, output + 8);
195  STORE32LE(d, output + 12);
196 }
197 
198 
199 /**
200  * @brief Decrypt a 16-byte block using RC6 algorithm
201  * @param[in] context Pointer to the RC6 context
202  * @param[in] input Ciphertext block to decrypt
203  * @param[out] output Plaintext block resulting from decryption
204  **/
205 
206 void rc6DecryptBlock(Rc6Context *context, const uint8_t *input, uint8_t *output)
207 {
208  uint_t i;
209  uint32_t t;
210  uint32_t u;
211 
212  //Load the 4 working registers with the ciphertext
213  uint32_t a = LOAD32LE(input + 0);
214  uint32_t b = LOAD32LE(input + 4);
215  uint32_t c = LOAD32LE(input + 8);
216  uint32_t d = LOAD32LE(input + 12);
217 
218  //First, update C and A
219  c -= context->s[2 * RC6_NB_ROUNDS + 3];
220  a -= context->s[2 * RC6_NB_ROUNDS + 2];
221 
222  //Apply 20 rounds
223  for(i = RC6_NB_ROUNDS; i > 0; i--)
224  {
225  t = d;
226  d = c;
227  c = b;
228  b = a;
229  a = t;
230 
231  u = (d * (2 * d + 1));
232  u = ROL32(u, 5);
233 
234  t = (b * (2 * b + 1));
235  t = ROL32(t, 5);
236 
237  c -= context->s[2 * i + 1];
238  c = ROR32(c, t % 32) ^ u;
239 
240  a -= context->s[2 * i];
241  a = ROR32(a, u % 32) ^ t;
242  }
243 
244  //Update D and B
245  d -= context->s[1];
246  b -= context->s[0];
247 
248  //The resulting value is the plaintext
249  STORE32LE(a, output + 0);
250  STORE32LE(b, output + 4);
251  STORE32LE(c, output + 8);
252  STORE32LE(d, output + 12);
253 }
254 
255 #endif
#define RC6_BLOCK_SIZE
Definition: rc6.h:38
#define ROR32(a, n)
Definition: crypto.h:656
uint8_t a
Definition: ndp.h:409
void(* CipherAlgoEncryptBlock)(void *context, const uint8_t *input, uint8_t *output)
Definition: crypto.h:836
RC6-32/20 block cipher.
@ CIPHER_ALGO_TYPE_BLOCK
Definition: crypto.h:802
uint8_t t
Definition: lldp_ext_med.h:210
#define STORE32LE(a, p)
Definition: cpu_endian.h:279
#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:149
void rc6DecryptBlock(Rc6Context *context, const uint8_t *input, uint8_t *output)
Decrypt a 16-byte block using RC6 algorithm.
Definition: rc6.c:206
RC6 algorithm context.
Definition: rc6.h:58
@ ERROR_INVALID_PARAMETER
Invalid parameter.
Definition: error.h:47
#define osMemcpy(dest, src, length)
Definition: os_port.h:137
error_t
Error codes.
Definition: error.h:43
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:837
const CipherAlgo rc6CipherAlgo
Definition: rc6.c:50
@ ERROR_INVALID_KEY_LENGTH
Definition: error.h:106
General definitions for cryptographic algorithms.
uint32_t l[RC6_MAX_KEY_SIZE/4]
Definition: rc6.h:59
uint8_t u
Definition: lldp_ext_med.h:211
#define RC6_NB_ROUNDS
Definition: rc6.h:42
error_t(* CipherAlgoInit)(void *context, const uint8_t *key, size_t keyLen)
Definition: crypto.h:833
uint8_t b[6]
Definition: ethernet.h:190
#define MAX(a, b)
Definition: os_port.h:66
#define Q32
Definition: rc6.c:47
#define ROL32(a, n)
Definition: crypto.h:650
uint32_t s[2 *RC6_NB_ROUNDS+4]
Definition: rc6.h:60
uint8_t s
Common interface for encryption algorithms.
Definition: crypto.h:874
#define LOAD32LE(p)
Definition: cpu_endian.h:203
#define P32
Definition: rc6.c:46
unsigned int uint_t
Definition: compiler_port.h:45
#define osMemset(p, value, length)
Definition: os_port.h:131
@ NO_ERROR
Success.
Definition: error.h:44
uint8_t c
Definition: ndp.h:512