ra2_crypto_cipher.c
Go to the documentation of this file.
1 /**
2  * @file ra2_crypto_cipher.c
3  * @brief RA2 cipher hardware accelerator
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  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.4.0
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
33 
34 //Dependencies
35 #include "hw_sce_private.h"
36 #include "hw_sce_aes_private.h"
37 #include "core/crypto.h"
42 #include "debug.h"
43 
44 //Check crypto library configuration
45 #if (RA2_CRYPTO_CIPHER_SUPPORT == ENABLED && AES_SUPPORT == ENABLED)
46 
47 
48 /**
49  * @brief Key expansion
50  * @param[in] context Pointer to the AES context to initialize
51  * @param[in] key Pointer to the key
52  * @param[in] keyLen Length of the key
53  * @return Error code
54  **/
55 
56 error_t aesInit(AesContext *context, const uint8_t *key, size_t keyLen)
57 {
58  fsp_err_t status;
59 
60  //Check parameters
61  if(context == NULL || key == NULL)
63 
64  //Initialize status code
65  status = FSP_SUCCESS;
66 
67  //Check the length of the key
68  if(keyLen == 16)
69  {
70  //10 rounds are required for 128-bit key
71  context->nr = 10;
72  //Copy the key
73  osMemcpy(context->ek, key, keyLen);
74  }
75  else if(keyLen == 32)
76  {
77  //14 rounds are required for 256-bit key
78  context->nr = 14;
79  //Copy the key
80  osMemcpy(context->ek, key, keyLen);
81  }
82  else
83  {
84  //192-bit keys are not supported
85  status = FSP_ERR_CRYPTO_NOT_IMPLEMENTED;
86  }
87 
88  //Return status code
89  return (status == FSP_SUCCESS) ? NO_ERROR : ERROR_FAILURE;
90 }
91 
92 
93 /**
94  * @brief Encrypt a 16-byte block using AES algorithm
95  * @param[in] context Pointer to the AES context
96  * @param[in] input Plaintext block to encrypt
97  * @param[out] output Ciphertext block resulting from encryption
98  **/
99 
100 void aesEncryptBlock(AesContext *context, const uint8_t *input, uint8_t *output)
101 {
102  fsp_err_t status;
103 
104  //Acquire exclusive access to the SCE module
106 
107  //Check the length of the key
108  if(context->nr == 10)
109  {
110  //Perform AES encryption (128-bit key)
111  status = HW_SCE_AES_128EcbEncrypt(context->ek, AES_BLOCK_SIZE / 4,
112  (const uint32_t *) input, (uint32_t *) output);
113  }
114  else if(context->nr == 14)
115  {
116  //Perform AES encryption (256-bit key)
117  status = HW_SCE_AES_256EcbEncrypt(context->ek, AES_BLOCK_SIZE / 4,
118  (const uint32_t *) input, (uint32_t *) output);
119  }
120  else
121  {
122  //192-bit keys are not supported
123  status = FSP_ERR_CRYPTO_NOT_IMPLEMENTED;
124  }
125 
126  //Check status code
127  if(status != FSP_SUCCESS)
128  {
129  osMemset(output, 0, AES_BLOCK_SIZE);
130  }
131 
132  //Release exclusive access to the SCE module
134 }
135 
136 
137 /**
138  * @brief Decrypt a 16-byte block using AES algorithm
139  * @param[in] context Pointer to the AES context
140  * @param[in] input Ciphertext block to decrypt
141  * @param[out] output Plaintext block resulting from decryption
142  **/
143 
144 void aesDecryptBlock(AesContext *context, const uint8_t *input, uint8_t *output)
145 {
146  fsp_err_t status;
147 
148  //Acquire exclusive access to the SCE module
150 
151  //Check the length of the key
152  if(context->nr == 10)
153  {
154  //Perform AES decryption (128-bit key)
155  status = HW_SCE_AES_128EcbDecrypt(context->ek, AES_BLOCK_SIZE / 4,
156  (const uint32_t *) input, (uint32_t *) output);
157  }
158  else if(context->nr == 14)
159  {
160  //Perform AES decryption (256-bit key)
161  status = HW_SCE_AES_256EcbDecrypt(context->ek, AES_BLOCK_SIZE / 4,
162  (const uint32_t *) input, (uint32_t *) output);
163  }
164  else
165  {
166  //192-bit keys are not supported
167  status = FSP_ERR_CRYPTO_NOT_IMPLEMENTED;
168  }
169 
170  //Check status code
171  if(status != FSP_SUCCESS)
172  {
173  osMemset(output, 0, AES_BLOCK_SIZE);
174  }
175 
176  //Release exclusive access to the SCE module
178 }
179 
180 
181 #if (ECB_SUPPORT == ENABLED)
182 
183 /**
184  * @brief ECB encryption
185  * @param[in] cipher Cipher algorithm
186  * @param[in] context Cipher algorithm context
187  * @param[in] p Plaintext to be encrypted
188  * @param[out] c Ciphertext resulting from the encryption
189  * @param[in] length Total number of data bytes to be encrypted
190  * @return Error code
191  **/
192 
193 error_t ecbEncrypt(const CipherAlgo *cipher, void *context,
194  const uint8_t *p, uint8_t *c, size_t length)
195 {
196  fsp_err_t status;
197 
198  //Initialize status code
199  status = FSP_SUCCESS;
200 
201  //AES cipher algorithm?
202  if(cipher == AES_CIPHER_ALGO)
203  {
204  //Check the length of the payload
205  if(length == 0)
206  {
207  //No data to process
208  }
209  else if((length % AES_BLOCK_SIZE) == 0)
210  {
211  AesContext *aesContext;
212 
213  //Point to the AES context
214  aesContext = (AesContext *) context;
215 
216  //Acquire exclusive access to the SCE module
218 
219  //Check the length of the key
220  if(aesContext->nr == 10)
221  {
222  //Perform AES-ECB encryption (128-bit key)
223  status = HW_SCE_AES_128EcbEncrypt(aesContext->ek, length / 4,
224  (const uint32_t *) p, (uint32_t *) c);
225  }
226  else if(aesContext->nr == 14)
227  {
228  //Perform AES-ECB encryption (256-bit key)
229  status = HW_SCE_AES_256EcbEncrypt(aesContext->ek, length / 4,
230  (const uint32_t *) p, (uint32_t *) c);
231  }
232  else
233  {
234  //192-bit keys are not supported
235  status = FSP_ERR_CRYPTO_NOT_IMPLEMENTED;
236  }
237 
238  //Release exclusive access to the SCE module
240  }
241  else
242  {
243  //The length of the payload must be a multiple of the block size
244  status = FSP_ERR_CRYPTO_INVALID_SIZE;
245  }
246  }
247  else
248  {
249  //ECB mode operates in a block-by-block fashion
250  while(length >= cipher->blockSize)
251  {
252  //Encrypt current block
253  cipher->encryptBlock(context, p, c);
254 
255  //Next block
256  p += cipher->blockSize;
257  c += cipher->blockSize;
258  length -= cipher->blockSize;
259  }
260 
261  //The length of the payload must be a multiple of the block size
262  if(length != 0)
263  {
264  status = FSP_ERR_CRYPTO_INVALID_SIZE;
265  }
266  }
267 
268  //Return status code
269  return (status == FSP_SUCCESS) ? NO_ERROR : ERROR_FAILURE;
270 }
271 
272 
273 /**
274  * @brief ECB decryption
275  * @param[in] cipher Cipher algorithm
276  * @param[in] context Cipher algorithm context
277  * @param[in] c Ciphertext to be decrypted
278  * @param[out] p Plaintext resulting from the decryption
279  * @param[in] length Total number of data bytes to be decrypted
280  * @return Error code
281  **/
282 
283 error_t ecbDecrypt(const CipherAlgo *cipher, void *context,
284  const uint8_t *c, uint8_t *p, size_t length)
285 {
286  fsp_err_t status;
287 
288  //Initialize status code
289  status = FSP_SUCCESS;
290 
291  //AES cipher algorithm?
292  if(cipher == AES_CIPHER_ALGO)
293  {
294  //Check the length of the payload
295  if(length == 0)
296  {
297  //No data to process
298  }
299  else if((length % AES_BLOCK_SIZE) == 0)
300  {
301  AesContext *aesContext;
302 
303  //Point to the AES context
304  aesContext = (AesContext *) context;
305 
306  //Acquire exclusive access to the SCE module
308 
309  //Check the length of the key
310  if(aesContext->nr == 10)
311  {
312  //Perform AES-ECB decryption (128-bit key)
313  status = HW_SCE_AES_128EcbDecrypt(aesContext->ek, length / 4,
314  (const uint32_t *) c, (uint32_t *) p);
315  }
316  else if(aesContext->nr == 14)
317  {
318  //Perform AES-ECB decryption (256-bit key)
319  status = HW_SCE_AES_256EcbDecrypt(aesContext->ek, length / 4,
320  (const uint32_t *) c, (uint32_t *) p);
321  }
322  else
323  {
324  //192-bit keys are not supported
325  status = FSP_ERR_CRYPTO_NOT_IMPLEMENTED;
326  }
327 
328  //Release exclusive access to the SCE module
330  }
331  else
332  {
333  //The length of the payload must be a multiple of the block size
334  status = FSP_ERR_CRYPTO_INVALID_SIZE;
335  }
336  }
337  else
338  {
339  //ECB mode operates in a block-by-block fashion
340  while(length >= cipher->blockSize)
341  {
342  //Decrypt current block
343  cipher->decryptBlock(context, c, p);
344 
345  //Next block
346  c += cipher->blockSize;
347  p += cipher->blockSize;
348  length -= cipher->blockSize;
349  }
350 
351  //The length of the payload must be a multiple of the block size
352  if(length != 0)
353  {
354  status = FSP_ERR_CRYPTO_INVALID_SIZE;
355  }
356  }
357 
358  //Return status code
359  return (status == FSP_SUCCESS) ? NO_ERROR : ERROR_FAILURE;
360 }
361 
362 #endif
363 #if (CBC_SUPPORT == ENABLED)
364 
365 /**
366  * @brief CBC encryption
367  * @param[in] cipher Cipher algorithm
368  * @param[in] context Cipher algorithm context
369  * @param[in,out] iv Initialization vector
370  * @param[in] p Plaintext to be encrypted
371  * @param[out] c Ciphertext resulting from the encryption
372  * @param[in] length Total number of data bytes to be encrypted
373  * @return Error code
374  **/
375 
376 error_t cbcEncrypt(const CipherAlgo *cipher, void *context,
377  uint8_t *iv, const uint8_t *p, uint8_t *c, size_t length)
378 {
379  fsp_err_t status;
380 
381  //Initialize status code
382  status = FSP_SUCCESS;
383 
384  //AES cipher algorithm?
385  if(cipher == AES_CIPHER_ALGO)
386  {
387  //Check the length of the payload
388  if(length == 0)
389  {
390  //No data to process
391  }
392  else if((length % AES_BLOCK_SIZE) == 0)
393  {
394  AesContext *aesContext;
395 
396  //Point to the AES context
397  aesContext = (AesContext *) context;
398 
399  //Acquire exclusive access to the SCE module
401 
402  //Check the length of the key
403  if(aesContext->nr == 10)
404  {
405  //Perform AES-CBC encryption (128-bit key)
406  status = HW_SCE_AES_128CbcEncrypt(aesContext->ek,
407  (const uint32_t *) iv, length / 4, (const uint32_t *) p,
408  (uint32_t *) c, (uint32_t *) iv);
409  }
410  else if(aesContext->nr == 14)
411  {
412  //Perform AES-CBC encryption (256-bit key)
413  status = HW_SCE_AES_256CbcEncrypt(aesContext->ek,
414  (const uint32_t *) iv, length / 4, (const uint32_t *) p,
415  (uint32_t *) c, (uint32_t *) iv);
416  }
417  else
418  {
419  //192-bit keys are not supported
420  status = FSP_ERR_CRYPTO_NOT_IMPLEMENTED;
421  }
422 
423  //Release exclusive access to the SCE module
425  }
426  else
427  {
428  //The length of the payload must be a multiple of the block size
429  status = FSP_ERR_CRYPTO_INVALID_SIZE;
430  }
431  }
432  else
433  {
434  size_t i;
435 
436  //CBC mode operates in a block-by-block fashion
437  while(length >= cipher->blockSize)
438  {
439  //XOR input block with IV contents
440  for(i = 0; i < cipher->blockSize; i++)
441  {
442  c[i] = p[i] ^ iv[i];
443  }
444 
445  //Encrypt the current block based upon the output of the previous
446  //encryption
447  cipher->encryptBlock(context, c, c);
448 
449  //Update IV with output block contents
450  osMemcpy(iv, c, cipher->blockSize);
451 
452  //Next block
453  p += cipher->blockSize;
454  c += cipher->blockSize;
455  length -= cipher->blockSize;
456  }
457 
458  //The length of the payload must be a multiple of the block size
459  if(length != 0)
460  {
461  status = FSP_ERR_CRYPTO_INVALID_SIZE;
462  }
463  }
464 
465  //Return status code
466  return (status == FSP_SUCCESS) ? NO_ERROR : ERROR_FAILURE;
467 }
468 
469 
470 /**
471  * @brief CBC decryption
472  * @param[in] cipher Cipher algorithm
473  * @param[in] context Cipher algorithm context
474  * @param[in,out] iv Initialization vector
475  * @param[in] c Ciphertext to be decrypted
476  * @param[out] p Plaintext resulting from the decryption
477  * @param[in] length Total number of data bytes to be decrypted
478  * @return Error code
479  **/
480 
481 error_t cbcDecrypt(const CipherAlgo *cipher, void *context,
482  uint8_t *iv, const uint8_t *c, uint8_t *p, size_t length)
483 {
484  fsp_err_t status;
485 
486  //Initialize status code
487  status = FSP_SUCCESS;
488 
489  //AES cipher algorithm?
490  if(cipher == AES_CIPHER_ALGO)
491  {
492  //Check the length of the payload
493  if(length == 0)
494  {
495  //No data to process
496  }
497  else if((length % AES_BLOCK_SIZE) == 0)
498  {
499  AesContext *aesContext;
500 
501  //Point to the AES context
502  aesContext = (AesContext *) context;
503 
504  //Acquire exclusive access to the SCE module
506 
507  //Check the length of the key
508  if(aesContext->nr == 10)
509  {
510  //Perform AES-CBC decryption (128-bit key)
511  status = HW_SCE_AES_128CbcDecrypt(aesContext->ek,
512  (const uint32_t *) iv, length / 4, (const uint32_t *) c,
513  (uint32_t *) p, (uint32_t *) iv);
514  }
515  else if(aesContext->nr == 14)
516  {
517  //Perform AES-CBC decryption (256-bit key)
518  status = HW_SCE_AES_256CbcDecrypt(aesContext->ek,
519  (const uint32_t *) iv, length / 4, (const uint32_t *) c,
520  (uint32_t *) p, (uint32_t *) iv);
521  }
522  else
523  {
524  //192-bit keys are not supported
525  status = FSP_ERR_CRYPTO_NOT_IMPLEMENTED;
526  }
527 
528  //Release exclusive access to the SCE module
530  }
531  else
532  {
533  //The length of the payload must be a multiple of the block size
534  status = FSP_ERR_CRYPTO_INVALID_SIZE;
535  }
536  }
537  else
538  {
539  size_t i;
540  uint8_t t[16];
541 
542  //CBC mode operates in a block-by-block fashion
543  while(length >= cipher->blockSize)
544  {
545  //Save input block
546  osMemcpy(t, c, cipher->blockSize);
547 
548  //Decrypt the current block
549  cipher->decryptBlock(context, c, p);
550 
551  //XOR output block with IV contents
552  for(i = 0; i < cipher->blockSize; i++)
553  {
554  p[i] ^= iv[i];
555  }
556 
557  //Update IV with input block contents
558  osMemcpy(iv, t, cipher->blockSize);
559 
560  //Next block
561  c += cipher->blockSize;
562  p += cipher->blockSize;
563  length -= cipher->blockSize;
564  }
565 
566  //The length of the payload must be a multiple of the block size
567  if(length != 0)
568  {
569  status = FSP_ERR_CRYPTO_INVALID_SIZE;
570  }
571  }
572 
573  //Return status code
574  return (status == FSP_SUCCESS) ? NO_ERROR : ERROR_FAILURE;
575 }
576 
577 #endif
578 #if (CTR_SUPPORT == ENABLED)
579 
580 /**
581  * @brief CTR encryption
582  * @param[in] cipher Cipher algorithm
583  * @param[in] context Cipher algorithm context
584  * @param[in] m Size in bits of the specific part of the block to be incremented
585  * @param[in,out] t Initial counter block
586  * @param[in] p Plaintext to be encrypted
587  * @param[out] c Ciphertext resulting from the encryption
588  * @param[in] length Total number of data bytes to be encrypted
589  * @return Error code
590  **/
591 
592 error_t ctrEncrypt(const CipherAlgo *cipher, void *context, uint_t m,
593  uint8_t *t, const uint8_t *p, uint8_t *c, size_t length)
594 {
595  fsp_err_t status;
596 
597  //Initialize status code
598  status = FSP_SUCCESS;
599 
600  //AES cipher algorithm?
601  if(cipher == AES_CIPHER_ALGO)
602  {
603  //Check the value of the parameter
604  if(m == (AES_BLOCK_SIZE * 8))
605  {
606  //Check the length of the payload
607  if(length == 0)
608  {
609  //No data to process
610  }
611  else if((length % AES_BLOCK_SIZE) == 0)
612  {
613  AesContext *aesContext;
614 
615  //Point to the AES context
616  aesContext = (AesContext *) context;
617 
618  //Acquire exclusive access to the SCE module
620 
621  //Check the length of the key
622  if(aesContext->nr == 10)
623  {
624  //Perform AES-CTR encryption (128-bit key)
625  status = HW_SCE_AES_128CtrEncrypt(aesContext->ek,
626  (const uint32_t *) t, length / 4, (const uint32_t *) p,
627  (uint32_t *) c, (uint32_t *) t);
628  }
629  else if(aesContext->nr == 14)
630  {
631  //Perform AES-CTR encryption (256-bit key)
632  status = HW_SCE_AES_256CtrEncrypt(aesContext->ek,
633  (const uint32_t *) t, length / 4, (const uint32_t *) p,
634  (uint32_t *) c, (uint32_t *) t);
635  }
636  else
637  {
638  //192-bit keys are not supported
639  status = FSP_ERR_CRYPTO_NOT_IMPLEMENTED;
640  }
641 
642  //Release exclusive access to the SCE module
644  }
645  else
646  {
647  //The length of the payload must be a multiple of the block size
648  status = FSP_ERR_CRYPTO_INVALID_SIZE;
649  }
650  }
651  else
652  {
653  //The value of the parameter is not valid
654  status = FSP_ERR_CRYPTO_NOT_IMPLEMENTED;
655  }
656  }
657  else
658  {
659  //Check the value of the parameter
660  if((m % 8) == 0 && m <= (cipher->blockSize * 8))
661  {
662  size_t i;
663  size_t n;
664  uint16_t temp;
665  uint8_t o[16];
666 
667  //Determine the size, in bytes, of the specific part of the block
668  //to be incremented
669  m = m / 8;
670 
671  //Process plaintext
672  while(length > 0)
673  {
674  //CTR mode operates in a block-by-block fashion
675  n = MIN(length, cipher->blockSize);
676 
677  //Compute O(j) = CIPH(T(j))
678  cipher->encryptBlock(context, t, o);
679 
680  //Compute C(j) = P(j) XOR T(j)
681  for(i = 0; i < n; i++)
682  {
683  c[i] = p[i] ^ o[i];
684  }
685 
686  //Standard incrementing function
687  for(temp = 1, i = 1; i <= m; i++)
688  {
689  //Increment the current byte and propagate the carry
690  temp += t[cipher->blockSize - i];
691  t[cipher->blockSize - i] = temp & 0xFF;
692  temp >>= 8;
693  }
694 
695  //Next block
696  p += n;
697  c += n;
698  length -= n;
699  }
700  }
701  else
702  {
703  //The value of the parameter is not valid
704  status = FSP_ERR_CRYPTO_NOT_IMPLEMENTED;
705  }
706  }
707 
708  //Return status code
709  return (status == FSP_SUCCESS) ? NO_ERROR : ERROR_FAILURE;
710 }
711 
712 #endif
713 #endif
#define AES_CIPHER_ALGO
Definition: aes.h:45
#define AES_BLOCK_SIZE
Definition: aes.h:43
Collection of AEAD algorithms.
Block cipher modes of operation.
unsigned int uint_t
Definition: compiler_port.h:50
General definitions for cryptographic algorithms.
Debugging facilities.
uint8_t n
uint8_t o
error_t
Error codes.
Definition: error.h:43
@ NO_ERROR
Success.
Definition: error.h:44
@ ERROR_FAILURE
Generic error code.
Definition: error.h:45
@ ERROR_INVALID_PARAMETER
Invalid parameter.
Definition: error.h:47
uint8_t iv[]
Definition: ike.h:1502
uint8_t t
Definition: lldp_ext_med.h:212
uint8_t c
Definition: ndp.h:514
uint8_t p
Definition: ndp.h:300
uint8_t m
Definition: ndp.h:304
#define osMemset(p, value, length)
Definition: os_port.h:135
#define osMemcpy(dest, src, length)
Definition: os_port.h:141
#define MIN(a, b)
Definition: os_port.h:63
void osAcquireMutex(OsMutex *mutex)
Acquire ownership of the specified mutex object.
void osReleaseMutex(OsMutex *mutex)
Release ownership of the specified mutex object.
OsMutex ra2CryptoMutex
Definition: ra2_crypto.c:41
RA2 hardware cryptographic accelerator (SCE)
error_t aesInit(AesContext *context, const uint8_t *key, size_t keyLen)
Key expansion.
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.
error_t cbcEncrypt(const CipherAlgo *cipher, void *context, uint8_t *iv, const uint8_t *p, uint8_t *c, size_t length)
CBC encryption.
error_t cbcDecrypt(const CipherAlgo *cipher, void *context, uint8_t *iv, const uint8_t *c, uint8_t *p, size_t length)
CBC decryption.
void aesDecryptBlock(AesContext *context, const uint8_t *input, uint8_t *output)
Decrypt a 16-byte block using AES algorithm.
void aesEncryptBlock(AesContext *context, const uint8_t *input, uint8_t *output)
Encrypt a 16-byte block using AES algorithm.
error_t ecbEncrypt(const CipherAlgo *cipher, void *context, const uint8_t *p, uint8_t *c, size_t length)
ECB encryption.
error_t ecbDecrypt(const CipherAlgo *cipher, void *context, const uint8_t *c, uint8_t *p, size_t length)
ECB decryption.
RA2 cipher hardware accelerator.
AES algorithm context.
Definition: aes.h:58
uint_t nr
Definition: aes.h:59
uint32_t ek[60]
Definition: aes.h:60
Common interface for encryption algorithms.
Definition: crypto.h:1036
CipherAlgoEncryptBlock encryptBlock
Definition: crypto.h:1044
CipherAlgoDecryptBlock decryptBlock
Definition: crypto.h:1045
size_t blockSize
Definition: crypto.h:1040
uint8_t length
Definition: tcp.h:368