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-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  * RC6 is a symmetric key block cipher derived from RC5
30  *
31  * @author Oryx Embedded SARL (www.oryx-embedded.com)
32  * @version 2.4.0
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,
62 };
63 
64 
65 /**
66  * @brief Initialize a RC6 context using the supplied key
67  * @param[in] context Pointer to the RC6 context to initialize
68  * @param[in] key Pointer to the key
69  * @param[in] keyLen Length of the key
70  * @return Error code
71  **/
72 
73 error_t rc6Init(Rc6Context *context, const uint8_t *key, size_t keyLen)
74 {
75  uint_t c;
76  uint_t i;
77  uint_t j;
78  uint_t s;
79  uint_t v;
80  uint32_t a;
81  uint32_t b;
82 
83  //Check parameters
84  if(context == NULL || key == NULL)
86 
87  //Invalid key length?
88  if(keyLen > RC6_MAX_KEY_SIZE)
90 
91  //Convert the secret key from bytes to words
92  osMemset(context->l, 0, RC6_MAX_KEY_SIZE);
93  osMemcpy(context->l, key, keyLen);
94 
95  //Calculate the length of the key in words
96  c = (keyLen > 0) ? (keyLen + 3) / 4 : 1;
97 
98  //Initialize the first element of S
99  context->s[0] = P32;
100 
101  //Initialize array S to a particular fixed pseudo random bit pattern
102  for(i = 1; i < (2 * RC6_NB_ROUNDS + 4); i++)
103  {
104  context->s[i] = context->s[i - 1] + Q32;
105  }
106 
107  //Initialize variables
108  i = 0;
109  j = 0;
110  a = 0;
111  b = 0;
112 
113  //Number of iterations
114  v = 3 * MAX(c, 2 * RC6_NB_ROUNDS + 4);
115 
116  //Key expansion
117  for(s = 0; s < v; s++)
118  {
119  context->s[i] += a + b;
120  context->s[i] = ROL32(context->s[i], 3);
121  a = context->s[i];
122 
123  context->l[j] += a + b;
124  context->l[j] = ROL32(context->l[j], (a + b) % 32);
125  b = context->l[j];
126 
127  if(++i >= (2 * RC6_NB_ROUNDS + 4))
128  {
129  i = 0;
130  }
131 
132  if(++j >= c)
133  {
134  j = 0;
135  }
136  }
137 
138  //No error to report
139  return NO_ERROR;
140 }
141 
142 
143 /**
144  * @brief Encrypt a 16-byte block using RC6 algorithm
145  * @param[in] context Pointer to the RC6 context
146  * @param[in] input Plaintext block to encrypt
147  * @param[out] output Ciphertext block resulting from encryption
148  **/
149 
150 void rc6EncryptBlock(Rc6Context *context, const uint8_t *input,
151  uint8_t *output)
152 {
153  uint_t i;
154  uint32_t t;
155  uint32_t u;
156 
157  //Load the 4 working registers with the plaintext
158  uint32_t a = LOAD32LE(input + 0);
159  uint32_t b = LOAD32LE(input + 4);
160  uint32_t c = LOAD32LE(input + 8);
161  uint32_t d = LOAD32LE(input + 12);
162 
163  //First, update B and D
164  b += context->s[0];
165  d += context->s[1];
166 
167  //Apply 20 rounds
168  for(i = 1; i <= RC6_NB_ROUNDS; i++)
169  {
170  t = (b * (2 * b + 1));
171  t = ROL32(t, 5);
172 
173  u = (d * (2 * d + 1));
174  u = ROL32(u, 5);
175 
176  a ^= t;
177  a = ROL32(a, u % 32) + context->s[2 * i];
178 
179  c ^= u;
180  c = ROL32(c, t % 32) + context->s[2 * i + 1];
181 
182  t = a;
183  a = b;
184  b = c;
185  c = d;
186  d = t;
187  }
188 
189  //Update A and C
190  a += context->s[2 * RC6_NB_ROUNDS + 2];
191  c += context->s[2 * RC6_NB_ROUNDS + 3];
192 
193  //The resulting value is the ciphertext
194  STORE32LE(a, output + 0);
195  STORE32LE(b, output + 4);
196  STORE32LE(c, output + 8);
197  STORE32LE(d, output + 12);
198 }
199 
200 
201 /**
202  * @brief Decrypt a 16-byte block using RC6 algorithm
203  * @param[in] context Pointer to the RC6 context
204  * @param[in] input Ciphertext block to decrypt
205  * @param[out] output Plaintext block resulting from decryption
206  **/
207 
208 void rc6DecryptBlock(Rc6Context *context, const uint8_t *input,
209  uint8_t *output)
210 {
211  uint_t i;
212  uint32_t t;
213  uint32_t u;
214 
215  //Load the 4 working registers with the ciphertext
216  uint32_t a = LOAD32LE(input + 0);
217  uint32_t b = LOAD32LE(input + 4);
218  uint32_t c = LOAD32LE(input + 8);
219  uint32_t d = LOAD32LE(input + 12);
220 
221  //First, update C and A
222  c -= context->s[2 * RC6_NB_ROUNDS + 3];
223  a -= context->s[2 * RC6_NB_ROUNDS + 2];
224 
225  //Apply 20 rounds
226  for(i = RC6_NB_ROUNDS; i > 0; i--)
227  {
228  t = d;
229  d = c;
230  c = b;
231  b = a;
232  a = t;
233 
234  u = (d * (2 * d + 1));
235  u = ROL32(u, 5);
236 
237  t = (b * (2 * b + 1));
238  t = ROL32(t, 5);
239 
240  c -= context->s[2 * i + 1];
241  c = ROR32(c, t % 32) ^ u;
242 
243  a -= context->s[2 * i];
244  a = ROR32(a, u % 32) ^ t;
245  }
246 
247  //Update D and B
248  d -= context->s[1];
249  b -= context->s[0];
250 
251  //The resulting value is the plaintext
252  STORE32LE(a, output + 0);
253  STORE32LE(b, output + 4);
254  STORE32LE(c, output + 8);
255  STORE32LE(d, output + 12);
256 }
257 
258 
259 /**
260  * @brief Release RC6 context
261  * @param[in] context Pointer to the RC6 context
262  **/
263 
264 void rc6Deinit(Rc6Context *context)
265 {
266  //Clear RC6 context
267  osMemset(context, 0, sizeof(Rc6Context));
268 }
269 
270 #endif
unsigned int uint_t
Definition: compiler_port.h:50
#define LOAD32LE(p)
Definition: cpu_endian.h:203
#define STORE32LE(a, p)
Definition: cpu_endian.h:279
General definitions for cryptographic algorithms.
void(* CipherAlgoDeinit)(void *context)
Definition: crypto.h:983
void(* CipherAlgoDecryptBlock)(void *context, const uint8_t *input, uint8_t *output)
Definition: crypto.h:980
error_t(* CipherAlgoInit)(void *context, const uint8_t *key, size_t keyLen)
Definition: crypto.h:968
#define ROR32(a, n)
Definition: crypto.h:782
void(* CipherAlgoEncryptBlock)(void *context, const uint8_t *input, uint8_t *output)
Definition: crypto.h:977
#define ROL32(a, n)
Definition: crypto.h:776
@ CIPHER_ALGO_TYPE_BLOCK
Definition: crypto.h:932
error_t
Error codes.
Definition: error.h:43
@ ERROR_INVALID_KEY_LENGTH
Definition: error.h:107
@ NO_ERROR
Success.
Definition: error.h:44
@ ERROR_INVALID_PARAMETER
Invalid parameter.
Definition: error.h:47
uint8_t u
Definition: lldp_ext_med.h:213
uint8_t t
Definition: lldp_ext_med.h:212
uint8_t b
Definition: nbns_common.h:104
uint8_t c
Definition: ndp.h:514
uint8_t s
Definition: ndp.h:345
uint8_t a
Definition: ndp.h:411
#define osMemset(p, value, length)
Definition: os_port.h:135
#define osMemcpy(dest, src, length)
Definition: os_port.h:141
#define MAX(a, b)
Definition: os_port.h:67
const CipherAlgo rc6CipherAlgo
Definition: rc6.c:50
error_t rc6Init(Rc6Context *context, const uint8_t *key, size_t keyLen)
Initialize a RC6 context using the supplied key.
Definition: rc6.c:73
#define P32
Definition: rc6.c:46
void rc6DecryptBlock(Rc6Context *context, const uint8_t *input, uint8_t *output)
Decrypt a 16-byte block using RC6 algorithm.
Definition: rc6.c:208
void rc6EncryptBlock(Rc6Context *context, const uint8_t *input, uint8_t *output)
Encrypt a 16-byte block using RC6 algorithm.
Definition: rc6.c:150
void rc6Deinit(Rc6Context *context)
Release RC6 context.
Definition: rc6.c:264
#define Q32
Definition: rc6.c:47
RC6-32/20 block cipher.
#define RC6_NB_ROUNDS
Definition: rc6.h:42
#define RC6_BLOCK_SIZE
Definition: rc6.h:38
#define RC6_MAX_KEY_SIZE
Definition: rc6.h:40
Common interface for encryption algorithms.
Definition: crypto.h:1036
RC6 algorithm context.
Definition: rc6.h:58
uint32_t l[RC6_MAX_KEY_SIZE/4]
Definition: rc6.h:59
uint32_t s[2 *RC6_NB_ROUNDS+4]
Definition: rc6.h:60