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