sama5d3_crypto_cipher.c
Go to the documentation of this file.
1 /**
2  * @file sama5d3_crypto_cipher.c
3  * @brief SAMA5D3 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 "sama5d3x.h"
36 #include "core/crypto.h"
41 #include "debug.h"
42 
43 //Check crypto library configuration
44 #if (SAMA5D3_CRYPTO_CIPHER_SUPPORT == ENABLED)
45 
46 
47 /**
48  * @brief Encrypt/decrypt a 16-byte block using DES algorithm
49  * @param[in] input Input block to be encrypted/decrypted
50  * @param[out] output Resulting block
51  **/
52 
53 void desProcessDataBlock(const uint8_t *input, uint8_t *output)
54 {
55  uint32_t *p;
56 
57  //Write input block
58  p = (uint32_t *) input;
59  TDES->TDES_IDATAR[0] = p[0];
60  TDES->TDES_IDATAR[1] = p[1];
61 
62  //Start encryption/decryption
63  TDES->TDES_CR = TDES_CR_START;
64 
65  //When processing completes, the DATRDY flag is raised
66  while((TDES->TDES_ISR & TDES_ISR_DATRDY) == 0)
67  {
68  }
69 
70  //Read output block
71  p = (uint32_t *) output;
72  p[0] = TDES->TDES_ODATAR[0];
73  p[1] = TDES->TDES_ODATAR[1];
74 }
75 
76 
77 #if (DES_SUPPORT == ENABLED)
78 
79 /**
80  * @brief Perform DES encryption or decryption
81  * @param[in] context DES algorithm context
82  * @param[in,out] iv Initialization vector
83  * @param[in] input Data to be encrypted/decrypted
84  * @param[out] output Data resulting from the encryption/decryption process
85  * @param[in] length Total number of data bytes to be processed
86  * @param[in] mode Operation mode
87  **/
88 
89 void desProcessData(DesContext *context, uint8_t *iv, const uint8_t *input,
90  uint8_t *output, size_t length, uint32_t mode)
91 {
92  uint32_t *p;
93 
94  //Acquire exclusive access to the TDES module
96 
97  //Perform software reset
98  TDES->TDES_CR = TDES_CR_SWRST;
99 
100  //Set operation mode
101  TDES->TDES_MR = TDES_MR_SMOD_MANUAL_START | TDES_MR_TDESMOD(0) | mode;
102 
103  //Set encryption key
104  TDES->TDES_KEY1WR[0] = context->ks[0];
105  TDES->TDES_KEY1WR[1] = context->ks[1];
106 
107  //Valid initialization vector?
108  if(iv != NULL)
109  {
110  //Set initialization vector
111  p = (uint32_t *) iv;
112  TDES->TDES_IVR[0] = p[0];
113  TDES->TDES_IVR[1] = p[1];
114  }
115 
116  //Process data
117  while(length >= DES_BLOCK_SIZE)
118  {
119  //The data is encrypted block by block
120  desProcessDataBlock(input, output);
121 
122  //Next block
123  input += DES_BLOCK_SIZE;
124  output += DES_BLOCK_SIZE;
126  }
127 
128  //Process final block of data
129  if(length > 0)
130  {
131  uint8_t buffer[DES_BLOCK_SIZE];
132 
133  //Copy input data
134  osMemset(buffer, 0, DES_BLOCK_SIZE);
135  osMemcpy(buffer, input, length);
136 
137  //Encrypt the final block of data
138  desProcessDataBlock(buffer, buffer);
139 
140  //Copy output data
141  osMemcpy(output, buffer, length);
142  }
143 
144  //Release exclusive access to the TDES module
146 }
147 
148 
149 /**
150  * @brief Initialize a DES context using the supplied key
151  * @param[in] context Pointer to the DES context to initialize
152  * @param[in] key Pointer to the key
153  * @param[in] keyLen Length of the key (must be set to 8)
154  * @return Error code
155  **/
156 
157 error_t desInit(DesContext *context, const uint8_t *key, size_t keyLen)
158 {
159  //Check parameters
160  if(context == NULL || key == NULL)
162 
163  //Invalid key length?
164  if(keyLen != 8)
166 
167  //Copy the key
168  osMemcpy(context->ks, key, keyLen);
169 
170  //No error to report
171  return NO_ERROR;
172 }
173 
174 
175 /**
176  * @brief Encrypt a 8-byte block using DES algorithm
177  * @param[in] context Pointer to the DES context
178  * @param[in] input Plaintext block to encrypt
179  * @param[out] output Ciphertext block resulting from encryption
180  **/
181 
182 void desEncryptBlock(DesContext *context, const uint8_t *input, uint8_t *output)
183 {
184  //Perform DES encryption
185  desProcessData(context, NULL, input, output, DES_BLOCK_SIZE,
186  TDES_MR_CIPHER | TDES_MR_OPMOD_ECB);
187 }
188 
189 
190 /**
191  * @brief Decrypt a 8-byte block using DES algorithm
192  * @param[in] context Pointer to the DES context
193  * @param[in] input Ciphertext block to decrypt
194  * @param[out] output Plaintext block resulting from decryption
195  **/
196 
197 void desDecryptBlock(DesContext *context, const uint8_t *input, uint8_t *output)
198 {
199  //Perform DES decryption
200  desProcessData(context, NULL, input, output, DES_BLOCK_SIZE,
201  TDES_MR_OPMOD_ECB);
202 }
203 
204 #endif
205 #if (DES3_SUPPORT == ENABLED)
206 
207 /**
208  * @brief Perform Triple DES encryption or decryption
209  * @param[in] context DES algorithm context
210  * @param[in,out] iv Initialization vector
211  * @param[in] input Data to be encrypted/decrypted
212  * @param[out] output Data resulting from the encryption/decryption process
213  * @param[in] length Total number of data bytes to be processed
214  * @param[in] mode Operation mode
215  **/
216 
217 void des3ProcessData(Des3Context *context, uint8_t *iv, const uint8_t *input,
218  uint8_t *output, size_t length, uint32_t mode)
219 {
220  uint32_t *p;
221 
222  //Acquire exclusive access to the TDES module
224 
225  //Perform software reset
226  TDES->TDES_CR = TDES_CR_SWRST;
227 
228  //Set operation mode
229  TDES->TDES_MR = TDES_MR_SMOD_MANUAL_START | TDES_MR_TDESMOD(1) | mode;
230 
231  //Set encryption key
232  TDES->TDES_KEY1WR[0] = context->k1.ks[0];
233  TDES->TDES_KEY1WR[1] = context->k1.ks[1];
234  TDES->TDES_KEY2WR[0] = context->k2.ks[0];
235  TDES->TDES_KEY2WR[1] = context->k2.ks[1];
236  TDES->TDES_KEY3WR[0] = context->k3.ks[0];
237  TDES->TDES_KEY3WR[1] = context->k3.ks[1];
238 
239  //Valid initialization vector?
240  if(iv != NULL)
241  {
242  //Set initialization vector
243  p = (uint32_t *) iv;
244  TDES->TDES_IVR[0] = p[0];
245  TDES->TDES_IVR[1] = p[1];
246  }
247 
248  //Process data
249  while(length >= DES3_BLOCK_SIZE)
250  {
251  //The data is encrypted block by block
252  desProcessDataBlock(input, output);
253 
254  //Next block
255  input += DES3_BLOCK_SIZE;
256  output += DES3_BLOCK_SIZE;
258  }
259 
260  //Process final block of data
261  if(length > 0)
262  {
263  uint8_t buffer[DES3_BLOCK_SIZE];
264 
265  //Copy input data
266  osMemset(buffer, 0, DES3_BLOCK_SIZE);
267  osMemcpy(buffer, input, length);
268 
269  //Encrypt the final block of data
270  desProcessDataBlock(buffer, buffer);
271 
272  //Copy output data
273  osMemcpy(output, buffer, length);
274  }
275 
276  //Release exclusive access to the TDES module
278 }
279 
280 
281 /**
282  * @brief Initialize a Triple DES context using the supplied key
283  * @param[in] context Pointer to the Triple DES context to initialize
284  * @param[in] key Pointer to the key
285  * @param[in] keyLen Length of the key
286  * @return Error code
287  **/
288 
289 error_t des3Init(Des3Context *context, const uint8_t *key, size_t keyLen)
290 {
291  //Check parameters
292  if(context == NULL || key == NULL)
294 
295  //Check key length
296  if(keyLen == 8)
297  {
298  //This option provides backward compatibility with DES, because the
299  //first and second DES operations cancel out
300  osMemcpy(context->k1.ks, key, 8);
301  osMemcpy(context->k2.ks, key, 8);
302  osMemcpy(context->k3.ks, key, 8);
303  }
304  else if(keyLen == 16)
305  {
306  //If the key length is 128 bits including parity, the first 8 bytes of the
307  //encoding represent the key used for the two outer DES operations, and
308  //the second 8 bytes represent the key used for the inner DES operation
309  osMemcpy(context->k1.ks, key, 8);
310  osMemcpy(context->k2.ks, key + 8, 8);
311  osMemcpy(context->k3.ks, key, 8);
312  }
313  else if(keyLen == 24)
314  {
315  //If the key length is 192 bits including parity, then 3 independent DES
316  //keys are represented, in the order in which they are used for encryption
317  osMemcpy(context->k1.ks, key, 8);
318  osMemcpy(context->k2.ks, key + 8, 8);
319  osMemcpy(context->k3.ks, key + 16, 8);
320  }
321  else
322  {
323  //The length of the key is not valid
325  }
326 
327  //No error to report
328  return NO_ERROR;
329 }
330 
331 
332 /**
333  * @brief Encrypt a 8-byte block using Triple DES algorithm
334  * @param[in] context Pointer to the Triple DES context
335  * @param[in] input Plaintext block to encrypt
336  * @param[out] output Ciphertext block resulting from encryption
337  **/
338 
339 void des3EncryptBlock(Des3Context *context, const uint8_t *input, uint8_t *output)
340 {
341  //Perform Triple DES encryption
342  des3ProcessData(context, NULL, input, output, DES3_BLOCK_SIZE,
343  TDES_MR_CIPHER | TDES_MR_OPMOD_ECB);
344 }
345 
346 
347 /**
348  * @brief Decrypt a 8-byte block using Triple DES algorithm
349  * @param[in] context Pointer to the Triple DES context
350  * @param[in] input Ciphertext block to decrypt
351  * @param[out] output Plaintext block resulting from decryption
352  **/
353 
354 void des3DecryptBlock(Des3Context *context, const uint8_t *input, uint8_t *output)
355 {
356  //Perform Triple DES decryption
357  des3ProcessData(context, NULL, input, output, DES3_BLOCK_SIZE,
358  TDES_MR_OPMOD_ECB);
359 }
360 
361 #endif
362 #if (AES_SUPPORT == ENABLED)
363 
364 /**
365  * @brief Load AES key
366  * @param[in] context AES algorithm context
367  **/
368 
369 void aesLoadKey(AesContext *context)
370 {
371  uint32_t temp;
372 
373  //Read mode register
374  temp = AES->AES_MR & ~AES_MR_KEYSIZE_Msk;
375 
376  //Check the length of the key
377  if(context->nr == 10)
378  {
379  //10 rounds are required for 128-bit key
380  AES->AES_MR = temp | AES_MR_KEYSIZE_AES128;
381 
382  //Set the 128-bit encryption key
383  AES->AES_KEYWR[0] = context->ek[0];
384  AES->AES_KEYWR[1] = context->ek[1];
385  AES->AES_KEYWR[2] = context->ek[2];
386  AES->AES_KEYWR[3] = context->ek[3];
387  }
388  else if(context->nr == 12)
389  {
390  //12 rounds are required for 192-bit key
391  AES->AES_MR = temp | AES_MR_KEYSIZE_AES192;
392 
393  //Set the 192-bit encryption key
394  AES->AES_KEYWR[0] = context->ek[0];
395  AES->AES_KEYWR[1] = context->ek[1];
396  AES->AES_KEYWR[2] = context->ek[2];
397  AES->AES_KEYWR[3] = context->ek[3];
398  AES->AES_KEYWR[4] = context->ek[4];
399  AES->AES_KEYWR[5] = context->ek[5];
400  }
401  else
402  {
403  //14 rounds are required for 256-bit key
404  AES->AES_MR = temp | AES_MR_KEYSIZE_AES256;
405 
406  //Set the 256-bit encryption key
407  AES->AES_KEYWR[0] = context->ek[0];
408  AES->AES_KEYWR[1] = context->ek[1];
409  AES->AES_KEYWR[2] = context->ek[2];
410  AES->AES_KEYWR[3] = context->ek[3];
411  AES->AES_KEYWR[4] = context->ek[4];
412  AES->AES_KEYWR[5] = context->ek[5];
413  AES->AES_KEYWR[6] = context->ek[6];
414  AES->AES_KEYWR[7] = context->ek[7];
415  }
416 }
417 
418 
419 /**
420  * @brief Encrypt/decrypt a 16-byte block using AES algorithm
421  * @param[in] input Input block to be encrypted/decrypted
422  * @param[out] output Resulting block
423  **/
424 
425 void aesProcessDataBlock(const uint8_t *input, uint8_t *output)
426 {
427  uint32_t *p;
428 
429  //Write input block
430  p = (uint32_t *) input;
431  AES->AES_IDATAR[0] = p[0];
432  AES->AES_IDATAR[1] = p[1];
433  AES->AES_IDATAR[2] = p[2];
434  AES->AES_IDATAR[3] = p[3];
435 
436  //Start encryption/decryption
437  AES->AES_CR = AES_CR_START;
438 
439  //When processing completes, the DATRDY flag is raised
440  while((AES->AES_ISR & AES_ISR_DATRDY) == 0)
441  {
442  }
443 
444  //Read output block
445  p = (uint32_t *) output;
446  p[0] = AES->AES_ODATAR[0];
447  p[1] = AES->AES_ODATAR[1];
448  p[2] = AES->AES_ODATAR[2];
449  p[3] = AES->AES_ODATAR[3];
450 }
451 
452 
453 /**
454  * @brief Perform AES encryption or decryption
455  * @param[in] context AES algorithm context
456  * @param[in] iv Initialization vector
457  * @param[in] input Data to be encrypted/decrypted
458  * @param[out] output Data resulting from the encryption/decryption process
459  * @param[in] length Total number of data bytes to be processed
460  * @param[in] mode Operation mode
461  **/
462 
463 void aesProcessData(AesContext *context, uint8_t *iv, const uint8_t *input,
464  uint8_t *output, size_t length, uint32_t mode)
465 {
466  uint32_t *p;
467 
468  //Acquire exclusive access to the AES module
470 
471  //Perform software reset
472  AES->AES_CR = AES_CR_SWRST;
473 
474  //Set operation mode
475  AES->AES_MR = AES_MR_SMOD_MANUAL_START | mode;
476  //Set encryption key
477  aesLoadKey(context);
478 
479  //Valid initialization vector?
480  if(iv != NULL)
481  {
482  //Set initialization vector
483  p = (uint32_t *) iv;
484  AES->AES_IVR[0] = p[0];
485  AES->AES_IVR[1] = p[1];
486  AES->AES_IVR[2] = p[2];
487  AES->AES_IVR[3] = p[3];
488  }
489 
490  //Process data
491  while(length >= AES_BLOCK_SIZE)
492  {
493  //The data is encrypted block by block
494  aesProcessDataBlock(input, output);
495 
496  //Next block
497  input += AES_BLOCK_SIZE;
498  output += AES_BLOCK_SIZE;
500  }
501 
502  //Process final block of data
503  if(length > 0)
504  {
505  uint8_t buffer[AES_BLOCK_SIZE];
506 
507  //Copy input data
508  osMemset(buffer, 0, AES_BLOCK_SIZE);
509  osMemcpy(buffer, input, length);
510 
511  //Encrypt the final block of data
512  aesProcessDataBlock(buffer, buffer);
513 
514  //Copy output data
515  osMemcpy(output, buffer, length);
516  }
517 
518  //Release exclusive access to the AES module
520 }
521 
522 
523 /**
524  * @brief Key expansion
525  * @param[in] context Pointer to the AES context to initialize
526  * @param[in] key Pointer to the key
527  * @param[in] keyLen Length of the key
528  * @return Error code
529  **/
530 
531 error_t aesInit(AesContext *context, const uint8_t *key, size_t keyLen)
532 {
533  //Check parameters
534  if(context == NULL || key == NULL)
536 
537  //Check the length of the key
538  if(keyLen == 16)
539  {
540  //10 rounds are required for 128-bit key
541  context->nr = 10;
542  }
543  else if(keyLen == 24)
544  {
545  //12 rounds are required for 192-bit key
546  context->nr = 12;
547  }
548  else if(keyLen == 32)
549  {
550  //14 rounds are required for 256-bit key
551  context->nr = 14;
552  }
553  else
554  {
555  //Report an error
557  }
558 
559  //Copy the original key
560  osMemcpy(context->ek, key, keyLen);
561 
562  //No error to report
563  return NO_ERROR;
564 }
565 
566 
567 /**
568  * @brief Encrypt a 16-byte block using AES algorithm
569  * @param[in] context Pointer to the AES context
570  * @param[in] input Plaintext block to encrypt
571  * @param[out] output Ciphertext block resulting from encryption
572  **/
573 
574 void aesEncryptBlock(AesContext *context, const uint8_t *input, uint8_t *output)
575 {
576  //Perform AES encryption
577  aesProcessData(context, NULL, input, output, AES_BLOCK_SIZE, AES_MR_CIPHER |
578  AES_MR_OPMOD_ECB);
579 }
580 
581 
582 /**
583  * @brief Decrypt a 16-byte block using AES algorithm
584  * @param[in] context Pointer to the AES context
585  * @param[in] input Ciphertext block to decrypt
586  * @param[out] output Plaintext block resulting from decryption
587  **/
588 
589 void aesDecryptBlock(AesContext *context, const uint8_t *input, uint8_t *output)
590 {
591  //Perform AES decryption
592  aesProcessData(context, NULL, input, output, AES_BLOCK_SIZE,
593  AES_MR_OPMOD_ECB);
594 }
595 
596 #endif
597 #if (ECB_SUPPORT == ENABLED)
598 
599 /**
600  * @brief ECB encryption
601  * @param[in] cipher Cipher algorithm
602  * @param[in] context Cipher algorithm context
603  * @param[in] p Plaintext to be encrypted
604  * @param[out] c Ciphertext resulting from the encryption
605  * @param[in] length Total number of data bytes to be encrypted
606  * @return Error code
607  **/
608 
609 error_t ecbEncrypt(const CipherAlgo *cipher, void *context,
610  const uint8_t *p, uint8_t *c, size_t length)
611 {
612  error_t error;
613 
614  //Initialize status code
615  error = NO_ERROR;
616 
617 #if (DES_SUPPORT == ENABLED)
618  //DES cipher algorithm?
619  if(cipher == DES_CIPHER_ALGO)
620  {
621  //Check the length of the payload
622  if(length == 0)
623  {
624  //No data to process
625  }
626  else if((length % DES_BLOCK_SIZE) == 0)
627  {
628  //Encrypt payload data
629  desProcessData(context, NULL, p, c, length, TDES_MR_CIPHER |
630  TDES_MR_OPMOD_ECB);
631  }
632  else
633  {
634  //The length of the payload must be a multiple of the block size
635  error = ERROR_INVALID_LENGTH;
636  }
637  }
638  else
639 #endif
640 #if (DES3_SUPPORT == ENABLED)
641  //Triple DES cipher algorithm?
642  if(cipher == DES3_CIPHER_ALGO)
643  {
644  //Check the length of the payload
645  if(length == 0)
646  {
647  //No data to process
648  }
649  else if((length % DES3_BLOCK_SIZE) == 0)
650  {
651  //Encrypt payload data
652  des3ProcessData(context, NULL, p, c, length, TDES_MR_CIPHER |
653  TDES_MR_OPMOD_ECB);
654  }
655  else
656  {
657  //The length of the payload must be a multiple of the block size
658  error = ERROR_INVALID_LENGTH;
659  }
660  }
661  else
662 #endif
663 #if (AES_SUPPORT == ENABLED)
664  //AES cipher algorithm?
665  if(cipher == AES_CIPHER_ALGO)
666  {
667  //Check the length of the payload
668  if(length == 0)
669  {
670  //No data to process
671  }
672  else if((length % AES_BLOCK_SIZE) == 0)
673  {
674  //Encrypt payload data
675  aesProcessData(context, NULL, p, c, length, AES_MR_CIPHER |
676  AES_MR_OPMOD_ECB);
677  }
678  else
679  {
680  //The length of the payload must be a multiple of the block size
681  error = ERROR_INVALID_LENGTH;
682  }
683  }
684  else
685 #endif
686  //Unknown cipher algorithm?
687  {
688  //ECB mode operates in a block-by-block fashion
689  while(length >= cipher->blockSize)
690  {
691  //Encrypt current block
692  cipher->encryptBlock(context, p, c);
693 
694  //Next block
695  p += cipher->blockSize;
696  c += cipher->blockSize;
697  length -= cipher->blockSize;
698  }
699 
700  //The length of the payload must be a multiple of the block size
701  if(length != 0)
702  {
703  error = ERROR_INVALID_LENGTH;
704  }
705  }
706 
707  //Return status code
708  return error;
709 }
710 
711 
712 /**
713  * @brief ECB decryption
714  * @param[in] cipher Cipher algorithm
715  * @param[in] context Cipher algorithm context
716  * @param[in] c Ciphertext to be decrypted
717  * @param[out] p Plaintext resulting from the decryption
718  * @param[in] length Total number of data bytes to be decrypted
719  * @return Error code
720  **/
721 
722 error_t ecbDecrypt(const CipherAlgo *cipher, void *context,
723  const uint8_t *c, uint8_t *p, size_t length)
724 {
725  error_t error;
726 
727  //Initialize status code
728  error = NO_ERROR;
729 
730 #if (DES_SUPPORT == ENABLED)
731  //DES cipher algorithm?
732  if(cipher == DES_CIPHER_ALGO)
733  {
734  //Check the length of the payload
735  if(length == 0)
736  {
737  //No data to process
738  }
739  else if((length % DES_BLOCK_SIZE) == 0)
740  {
741  //Decrypt payload data
742  desProcessData(context, NULL, c, p, length, TDES_MR_OPMOD_ECB);
743  }
744  else
745  {
746  //The length of the payload must be a multiple of the block size
747  error = ERROR_INVALID_LENGTH;
748  }
749  }
750  else
751 #endif
752 #if (DES3_SUPPORT == ENABLED)
753  //Triple DES cipher algorithm?
754  if(cipher == DES3_CIPHER_ALGO)
755  {
756  //Check the length of the payload
757  if(length == 0)
758  {
759  //No data to process
760  }
761  else if((length % DES3_BLOCK_SIZE) == 0)
762  {
763  //Decrypt payload data
764  des3ProcessData(context, NULL, c, p, length, TDES_MR_OPMOD_ECB);
765  }
766  else
767  {
768  //The length of the payload must be a multiple of the block size
769  error = ERROR_INVALID_LENGTH;
770  }
771  }
772  else
773 #endif
774 #if (AES_SUPPORT == ENABLED)
775  //AES cipher algorithm?
776  if(cipher == AES_CIPHER_ALGO)
777  {
778  //Check the length of the payload
779  if(length == 0)
780  {
781  //No data to process
782  }
783  else if((length % AES_BLOCK_SIZE) == 0)
784  {
785  //Decrypt payload data
786  aesProcessData(context, NULL, c, p, length, AES_MR_OPMOD_ECB);
787  }
788  else
789  {
790  //The length of the payload must be a multiple of the block size
791  error = ERROR_INVALID_LENGTH;
792  }
793  }
794  else
795 #endif
796  //Unknown cipher algorithm?
797  {
798  //ECB mode operates in a block-by-block fashion
799  while(length >= cipher->blockSize)
800  {
801  //Decrypt current block
802  cipher->decryptBlock(context, c, p);
803 
804  //Next block
805  c += cipher->blockSize;
806  p += cipher->blockSize;
807  length -= cipher->blockSize;
808  }
809 
810  //The length of the payload must be a multiple of the block size
811  if(length != 0)
812  {
813  error = ERROR_INVALID_LENGTH;
814  }
815  }
816 
817  //Return status code
818  return error;
819 }
820 
821 #endif
822 #if (CBC_SUPPORT == ENABLED)
823 
824 /**
825  * @brief CBC encryption
826  * @param[in] cipher Cipher algorithm
827  * @param[in] context Cipher algorithm context
828  * @param[in,out] iv Initialization vector
829  * @param[in] p Plaintext to be encrypted
830  * @param[out] c Ciphertext resulting from the encryption
831  * @param[in] length Total number of data bytes to be encrypted
832  * @return Error code
833  **/
834 
835 error_t cbcEncrypt(const CipherAlgo *cipher, void *context,
836  uint8_t *iv, const uint8_t *p, uint8_t *c, size_t length)
837 {
838  error_t error;
839 
840  //Initialize status code
841  error = NO_ERROR;
842 
843 #if (DES_SUPPORT == ENABLED)
844  //DES cipher algorithm?
845  if(cipher == DES_CIPHER_ALGO)
846  {
847  //Check the length of the payload
848  if(length == 0)
849  {
850  //No data to process
851  }
852  else if((length % DES_BLOCK_SIZE) == 0)
853  {
854  //Encrypt payload data
855  desProcessData(context, iv, p, c, length, TDES_MR_CIPHER |
856  TDES_MR_OPMOD_CBC);
857 
858  //Update the value of the initialization vector
860  }
861  else
862  {
863  //The length of the payload must be a multiple of the block size
864  error = ERROR_INVALID_LENGTH;
865  }
866  }
867  else
868 #endif
869 #if (DES3_SUPPORT == ENABLED)
870  //Triple DES cipher algorithm?
871  if(cipher == DES3_CIPHER_ALGO)
872  {
873  //Check the length of the payload
874  if(length == 0)
875  {
876  //No data to process
877  }
878  else if((length % DES3_BLOCK_SIZE) == 0)
879  {
880  //Encrypt payload data
881  des3ProcessData(context, iv, p, c, length, TDES_MR_CIPHER |
882  TDES_MR_OPMOD_CBC);
883 
884  //Update the value of the initialization vector
886  }
887  else
888  {
889  //The length of the payload must be a multiple of the block size
890  error = ERROR_INVALID_LENGTH;
891  }
892  }
893  else
894 #endif
895 #if (AES_SUPPORT == ENABLED)
896  //AES cipher algorithm?
897  if(cipher == AES_CIPHER_ALGO)
898  {
899  //Check the length of the payload
900  if(length == 0)
901  {
902  //No data to process
903  }
904  else if((length % AES_BLOCK_SIZE) == 0)
905  {
906  //Encrypt payload data
907  aesProcessData(context, iv, p, c, length, AES_MR_CIPHER |
908  AES_MR_OPMOD_CBC);
909 
910  //Update the value of the initialization vector
912  }
913  else
914  {
915  //The length of the payload must be a multiple of the block size
916  error = ERROR_INVALID_LENGTH;
917  }
918  }
919  else
920 #endif
921  //Unknown cipher algorithm?
922  {
923  size_t i;
924 
925  //CBC mode operates in a block-by-block fashion
926  while(length >= cipher->blockSize)
927  {
928  //XOR input block with IV contents
929  for(i = 0; i < cipher->blockSize; i++)
930  {
931  c[i] = p[i] ^ iv[i];
932  }
933 
934  //Encrypt the current block based upon the output of the previous
935  //encryption
936  cipher->encryptBlock(context, c, c);
937 
938  //Update IV with output block contents
939  osMemcpy(iv, c, cipher->blockSize);
940 
941  //Next block
942  p += cipher->blockSize;
943  c += cipher->blockSize;
944  length -= cipher->blockSize;
945  }
946 
947  //The length of the payload must be a multiple of the block size
948  if(length != 0)
949  {
950  error = ERROR_INVALID_LENGTH;
951  }
952  }
953 
954  //Return status code
955  return error;
956 }
957 
958 
959 /**
960  * @brief CBC decryption
961  * @param[in] cipher Cipher algorithm
962  * @param[in] context Cipher algorithm context
963  * @param[in,out] iv Initialization vector
964  * @param[in] c Ciphertext to be decrypted
965  * @param[out] p Plaintext resulting from the decryption
966  * @param[in] length Total number of data bytes to be decrypted
967  * @return Error code
968  **/
969 
970 error_t cbcDecrypt(const CipherAlgo *cipher, void *context,
971  uint8_t *iv, const uint8_t *c, uint8_t *p, size_t length)
972 {
973  error_t error;
974 
975  //Initialize status code
976  error = NO_ERROR;
977 
978 #if (DES_SUPPORT == ENABLED)
979  //DES cipher algorithm?
980  if(cipher == DES_CIPHER_ALGO)
981  {
982  //Check the length of the payload
983  if(length == 0)
984  {
985  //No data to process
986  }
987  else if((length % DES_BLOCK_SIZE) == 0)
988  {
989  uint8_t block[DES_BLOCK_SIZE];
990 
991  //Save the last input block
993 
994  //Decrypt payload data
995  desProcessData(context, iv, c, p, length, TDES_MR_OPMOD_CBC);
996 
997  //Update the value of the initialization vector
999  }
1000  else
1001  {
1002  //The length of the payload must be a multiple of the block size
1003  error = ERROR_INVALID_LENGTH;
1004  }
1005  }
1006  else
1007 #endif
1008 #if (DES3_SUPPORT == ENABLED)
1009  //Triple DES cipher algorithm?
1010  if(cipher == DES3_CIPHER_ALGO)
1011  {
1012  //Check the length of the payload
1013  if(length == 0)
1014  {
1015  //No data to process
1016  }
1017  else if((length % DES3_BLOCK_SIZE) == 0)
1018  {
1019  uint8_t block[DES3_BLOCK_SIZE];
1020 
1021  //Save the last input block
1023 
1024  //Decrypt payload data
1025  des3ProcessData(context, iv, c, p, length, TDES_MR_OPMOD_CBC);
1026 
1027  //Update the value of the initialization vector
1029  }
1030  else
1031  {
1032  //The length of the payload must be a multiple of the block size
1033  error = ERROR_INVALID_LENGTH;
1034  }
1035  }
1036  else
1037 #endif
1038 #if (AES_SUPPORT == ENABLED)
1039  //AES cipher algorithm?
1040  if(cipher == AES_CIPHER_ALGO)
1041  {
1042  //Check the length of the payload
1043  if(length == 0)
1044  {
1045  //No data to process
1046  }
1047  else if((length % AES_BLOCK_SIZE) == 0)
1048  {
1049  uint8_t block[AES_BLOCK_SIZE];
1050 
1051  //Save the last input block
1053 
1054  //Decrypt payload data
1055  aesProcessData(context, iv, c, p, length, AES_MR_OPMOD_CBC);
1056 
1057  //Update the value of the initialization vector
1059  }
1060  else
1061  {
1062  //The length of the payload must be a multiple of the block size
1063  error = ERROR_INVALID_LENGTH;
1064  }
1065  }
1066  else
1067 #endif
1068  //Unknown cipher algorithm?
1069  {
1070  size_t i;
1071  uint8_t t[16];
1072 
1073  //CBC mode operates in a block-by-block fashion
1074  while(length >= cipher->blockSize)
1075  {
1076  //Save input block
1077  osMemcpy(t, c, cipher->blockSize);
1078 
1079  //Decrypt the current block
1080  cipher->decryptBlock(context, c, p);
1081 
1082  //XOR output block with IV contents
1083  for(i = 0; i < cipher->blockSize; i++)
1084  {
1085  p[i] ^= iv[i];
1086  }
1087 
1088  //Update IV with input block contents
1089  osMemcpy(iv, t, cipher->blockSize);
1090 
1091  //Next block
1092  c += cipher->blockSize;
1093  p += cipher->blockSize;
1094  length -= cipher->blockSize;
1095  }
1096 
1097  //The length of the payload must be a multiple of the block size
1098  if(length != 0)
1099  {
1100  error = ERROR_INVALID_LENGTH;
1101  }
1102  }
1103 
1104  //Return status code
1105  return error;
1106 }
1107 
1108 #endif
1109 #if (CFB_SUPPORT == ENABLED)
1110 
1111 /**
1112  * @brief CFB encryption
1113  * @param[in] cipher Cipher algorithm
1114  * @param[in] context Cipher algorithm context
1115  * @param[in] s Size of the plaintext and ciphertext segments
1116  * @param[in,out] iv Initialization vector
1117  * @param[in] p Plaintext to be encrypted
1118  * @param[out] c Ciphertext resulting from the encryption
1119  * @param[in] length Total number of data bytes to be encrypted
1120  * @return Error code
1121  **/
1122 
1123 error_t cfbEncrypt(const CipherAlgo *cipher, void *context, uint_t s,
1124  uint8_t *iv, const uint8_t *p, uint8_t *c, size_t length)
1125 {
1126  error_t error;
1127 
1128  //Initialize status code
1129  error = NO_ERROR;
1130 
1131 #if (DES_SUPPORT == ENABLED)
1132  //DES cipher algorithm?
1133  if(cipher == DES_CIPHER_ALGO)
1134  {
1135  //Check the value of the parameter
1136  if(s == (DES_BLOCK_SIZE * 8))
1137  {
1138  //Check the length of the payload
1139  if(length > 0)
1140  {
1141  //Encrypt payload data
1142  desProcessData(context, iv, p, c, length, TDES_MR_CIPHER |
1143  TDES_MR_OPMOD_CFB | TDES_MR_CFBS_SIZE_64BIT);
1144  }
1145  else
1146  {
1147  //No data to process
1148  }
1149  }
1150  else
1151  {
1152  //The value of the parameter is not valid
1153  error = ERROR_INVALID_PARAMETER;
1154  }
1155  }
1156  else
1157 #endif
1158 #if (DES3_SUPPORT == ENABLED)
1159  //Triple DES cipher algorithm?
1160  if(cipher == DES3_CIPHER_ALGO)
1161  {
1162  //Check the value of the parameter
1163  if(s == (DES3_BLOCK_SIZE * 8))
1164  {
1165  //Check the length of the payload
1166  if(length > 0)
1167  {
1168  //Encrypt payload data
1169  des3ProcessData(context, iv, p, c, length, TDES_MR_CIPHER |
1170  TDES_MR_OPMOD_CFB | TDES_MR_CFBS_SIZE_64BIT);
1171  }
1172  else
1173  {
1174  //No data to process
1175  }
1176  }
1177  else
1178  {
1179  //The value of the parameter is not valid
1180  error = ERROR_INVALID_PARAMETER;
1181  }
1182  }
1183  else
1184 #endif
1185 #if (AES_SUPPORT == ENABLED)
1186  //AES cipher algorithm?
1187  if(cipher == AES_CIPHER_ALGO)
1188  {
1189  //Check the value of the parameter
1190  if(s == (AES_BLOCK_SIZE * 8))
1191  {
1192  //Check the length of the payload
1193  if(length > 0)
1194  {
1195  //Encrypt payload data
1196  aesProcessData(context, iv, p, c, length, AES_MR_CIPHER |
1197  AES_MR_OPMOD_CFB | AES_MR_CFBS_SIZE_128BIT);
1198  }
1199  else
1200  {
1201  //No data to process
1202  }
1203  }
1204  else
1205  {
1206  //The value of the parameter is not valid
1207  error = ERROR_INVALID_PARAMETER;
1208  }
1209  }
1210  else
1211 #endif
1212  //Unknown cipher algorithm?
1213  {
1214  //Check the value of the parameter
1215  if((s % 8) == 0 && s >= 1 && s <= (cipher->blockSize * 8))
1216  {
1217  size_t i;
1218  size_t n;
1219  uint8_t o[16];
1220 
1221  //Determine the size, in bytes, of the plaintext and ciphertext segments
1222  s = s / 8;
1223 
1224  //Process each plaintext segment
1225  while(length > 0)
1226  {
1227  //Compute the number of bytes to process at a time
1228  n = MIN(length, s);
1229 
1230  //Compute O(j) = CIPH(I(j))
1231  cipher->encryptBlock(context, iv, o);
1232 
1233  //Compute C(j) = P(j) XOR MSB(O(j))
1234  for(i = 0; i < n; i++)
1235  {
1236  c[i] = p[i] ^ o[i];
1237  }
1238 
1239  //Compute I(j+1) = LSB(I(j)) | C(j)
1240  osMemmove(iv, iv + s, cipher->blockSize - s);
1241  osMemcpy(iv + cipher->blockSize - s, c, s);
1242 
1243  //Next block
1244  p += n;
1245  c += n;
1246  length -= n;
1247  }
1248  }
1249  else
1250  {
1251  //The value of the parameter is not valid
1252  error = ERROR_INVALID_PARAMETER;
1253  }
1254  }
1255 
1256  //Return status code
1257  return error;
1258 }
1259 
1260 
1261 /**
1262  * @brief CFB decryption
1263  * @param[in] cipher Cipher algorithm
1264  * @param[in] context Cipher algorithm context
1265  * @param[in] s Size of the plaintext and ciphertext segments
1266  * @param[in,out] iv Initialization vector
1267  * @param[in] c Ciphertext to be decrypted
1268  * @param[out] p Plaintext resulting from the decryption
1269  * @param[in] length Total number of data bytes to be decrypted
1270  * @return Error code
1271  **/
1272 
1273 error_t cfbDecrypt(const CipherAlgo *cipher, void *context, uint_t s,
1274  uint8_t *iv, const uint8_t *c, uint8_t *p, size_t length)
1275 {
1276  error_t error;
1277 
1278  //Initialize status code
1279  error = NO_ERROR;
1280 
1281 #if (DES_SUPPORT == ENABLED)
1282  //DES cipher algorithm?
1283  if(cipher == DES_CIPHER_ALGO)
1284  {
1285  //Check the value of the parameter
1286  if(s == (DES_BLOCK_SIZE * 8))
1287  {
1288  //Check the length of the payload
1289  if(length > 0)
1290  {
1291  //Decrypt payload data
1292  desProcessData(context, iv, c, p, length, TDES_MR_OPMOD_CFB |
1293  TDES_MR_CFBS_SIZE_64BIT);
1294  }
1295  else
1296  {
1297  //No data to process
1298  }
1299  }
1300  else
1301  {
1302  //The value of the parameter is not valid
1303  error = ERROR_INVALID_PARAMETER;
1304  }
1305  }
1306  else
1307 #endif
1308 #if (DES3_SUPPORT == ENABLED)
1309  //Triple DES cipher algorithm?
1310  if(cipher == DES3_CIPHER_ALGO)
1311  {
1312  //Check the value of the parameter
1313  if(s == (DES3_BLOCK_SIZE * 8))
1314  {
1315  //Check the length of the payload
1316  if(length > 0)
1317  {
1318  //Decrypt payload data
1319  des3ProcessData(context, iv, c, p, length, TDES_MR_OPMOD_CFB |
1320  TDES_MR_CFBS_SIZE_64BIT);
1321  }
1322  else
1323  {
1324  //No data to process
1325  }
1326  }
1327  else
1328  {
1329  //The value of the parameter is not valid
1330  error = ERROR_INVALID_PARAMETER;
1331  }
1332  }
1333  else
1334 #endif
1335 #if (AES_SUPPORT == ENABLED)
1336  //AES cipher algorithm?
1337  if(cipher == AES_CIPHER_ALGO)
1338  {
1339  //Check the value of the parameter
1340  if(s == (AES_BLOCK_SIZE * 8))
1341  {
1342  //Check the length of the payload
1343  if(length > 0)
1344  {
1345  //Decrypt payload data
1346  aesProcessData(context, iv, c, p, length, AES_MR_OPMOD_CFB |
1347  AES_MR_CFBS_SIZE_128BIT);
1348  }
1349  else
1350  {
1351  //No data to process
1352  }
1353  }
1354  else
1355  {
1356  //The value of the parameter is not valid
1357  error = ERROR_INVALID_PARAMETER;
1358  }
1359  }
1360  else
1361 #endif
1362  //Unknown cipher algorithm?
1363  {
1364  //Check the value of the parameter
1365  if((s % 8) == 0 && s >= 1 && s <= (cipher->blockSize * 8))
1366  {
1367  size_t i;
1368  size_t n;
1369  uint8_t o[16];
1370 
1371  //Determine the size, in bytes, of the plaintext and ciphertext segments
1372  s = s / 8;
1373 
1374  //Process each ciphertext segment
1375  while(length > 0)
1376  {
1377  //Compute the number of bytes to process at a time
1378  n = MIN(length, s);
1379 
1380  //Compute O(j) = CIPH(I(j))
1381  cipher->encryptBlock(context, iv, o);
1382 
1383  //Compute I(j+1) = LSB(I(j)) | C(j)
1384  osMemmove(iv, iv + s, cipher->blockSize - s);
1385  osMemcpy(iv + cipher->blockSize - s, c, s);
1386 
1387  //Compute P(j) = C(j) XOR MSB(O(j))
1388  for(i = 0; i < n; i++)
1389  {
1390  p[i] = c[i] ^ o[i];
1391  }
1392 
1393  //Next block
1394  c += n;
1395  p += n;
1396  length -= n;
1397  }
1398  }
1399  else
1400  {
1401  //The value of the parameter is not valid
1402  error = ERROR_INVALID_PARAMETER;
1403  }
1404  }
1405 
1406  //Return status code
1407  return error;
1408 }
1409 
1410 #endif
1411 #if (OFB_SUPPORT == ENABLED)
1412 
1413 /**
1414  * @brief OFB encryption
1415  * @param[in] cipher Cipher algorithm
1416  * @param[in] context Cipher algorithm context
1417  * @param[in] s Size of the plaintext and ciphertext segments
1418  * @param[in,out] iv Initialization vector
1419  * @param[in] p Plaintext to be encrypted
1420  * @param[out] c Ciphertext resulting from the encryption
1421  * @param[in] length Total number of data bytes to be encrypted
1422  * @return Error code
1423  **/
1424 
1425 error_t ofbEncrypt(const CipherAlgo *cipher, void *context, uint_t s,
1426  uint8_t *iv, const uint8_t *p, uint8_t *c, size_t length)
1427 {
1428  error_t error;
1429 
1430  //Initialize status code
1431  error = NO_ERROR;
1432 
1433 #if (DES_SUPPORT == ENABLED)
1434  //DES cipher algorithm?
1435  if(cipher == DES_CIPHER_ALGO)
1436  {
1437  //Check the value of the parameter
1438  if(s == (DES_BLOCK_SIZE * 8))
1439  {
1440  //Check the length of the payload
1441  if(length > 0)
1442  {
1443  //Encrypt payload data
1444  desProcessData(context, iv, p, c, length, TDES_MR_CIPHER |
1445  TDES_MR_OPMOD_OFB);
1446  }
1447  else
1448  {
1449  //No data to process
1450  }
1451  }
1452  else
1453  {
1454  //The value of the parameter is not valid
1455  error = ERROR_INVALID_PARAMETER;
1456  }
1457  }
1458  else
1459 #endif
1460 #if (DES3_SUPPORT == ENABLED)
1461  //Triple DES cipher algorithm?
1462  if(cipher == DES3_CIPHER_ALGO)
1463  {
1464  //Check the value of the parameter
1465  if(s == (DES3_BLOCK_SIZE * 8))
1466  {
1467  //Check the length of the payload
1468  if(length > 0)
1469  {
1470  //Encrypt payload data
1471  des3ProcessData(context, iv, p, c, length, TDES_MR_CIPHER |
1472  TDES_MR_OPMOD_OFB);
1473  }
1474  else
1475  {
1476  //No data to process
1477  }
1478  }
1479  else
1480  {
1481  //The value of the parameter is not valid
1482  error = ERROR_INVALID_PARAMETER;
1483  }
1484  }
1485  else
1486 #endif
1487 #if (AES_SUPPORT == ENABLED)
1488  //AES cipher algorithm?
1489  if(cipher == AES_CIPHER_ALGO)
1490  {
1491  //Check the value of the parameter
1492  if(s == (AES_BLOCK_SIZE * 8))
1493  {
1494  //Check the length of the payload
1495  if(length > 0)
1496  {
1497  //Encrypt payload data
1498  aesProcessData(context, iv, p, c, length, AES_MR_CIPHER |
1499  AES_MR_OPMOD_OFB);
1500  }
1501  else
1502  {
1503  //No data to process
1504  }
1505  }
1506  else
1507  {
1508  //The value of the parameter is not valid
1509  error = ERROR_INVALID_PARAMETER;
1510  }
1511  }
1512  else
1513 #endif
1514  //Unknown cipher algorithm?
1515  {
1516  //Check the value of the parameter
1517  if((s % 8) == 0 && s >= 1 && s <= (cipher->blockSize * 8))
1518  {
1519  size_t i;
1520  size_t n;
1521  uint8_t o[16];
1522 
1523  //Determine the size, in bytes, of the plaintext and ciphertext segments
1524  s = s / 8;
1525 
1526  //Process each plaintext segment
1527  while(length > 0)
1528  {
1529  //Compute the number of bytes to process at a time
1530  n = MIN(length, s);
1531 
1532  //Compute O(j) = CIPH(I(j))
1533  cipher->encryptBlock(context, iv, o);
1534 
1535  //Compute C(j) = P(j) XOR MSB(O(j))
1536  for(i = 0; i < n; i++)
1537  {
1538  c[i] = p[i] ^ o[i];
1539  }
1540 
1541  //Compute I(j+1) = LSB(I(j)) | O(j)
1542  osMemmove(iv, iv + s, cipher->blockSize - s);
1543  osMemcpy(iv + cipher->blockSize - s, o, s);
1544 
1545  //Next block
1546  p += n;
1547  c += n;
1548  length -= n;
1549  }
1550  }
1551  else
1552  {
1553  //The value of the parameter is not valid
1554  error = ERROR_INVALID_PARAMETER;
1555  }
1556  }
1557 
1558  //Return status code
1559  return error;
1560 }
1561 
1562 #endif
1563 #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.
#define DES3_CIPHER_ALGO
Definition: des3.h:46
#define DES3_BLOCK_SIZE
Definition: des3.h:44
#define DES_BLOCK_SIZE
Definition: des.h:43
#define DES_CIPHER_ALGO
Definition: des.h:45
uint8_t n
uint8_t o
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_LENGTH
Definition: error.h:111
@ 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 s
Definition: ndp.h:345
uint8_t p
Definition: ndp.h:300
#define osMemset(p, value, length)
Definition: os_port.h:135
#define osMemmove(dest, src, length)
Definition: os_port.h:147
#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 sama5d3CryptoMutex
SAMA5D3 hardware cryptographic accelerator.
void des3ProcessData(Des3Context *context, uint8_t *iv, const uint8_t *input, uint8_t *output, size_t length, uint32_t mode)
Perform Triple DES encryption or decryption.
void des3DecryptBlock(Des3Context *context, const uint8_t *input, uint8_t *output)
Decrypt a 8-byte block using Triple DES algorithm.
error_t aesInit(AesContext *context, const uint8_t *key, size_t keyLen)
Key expansion.
void aesLoadKey(AesContext *context)
Load AES key.
void desDecryptBlock(DesContext *context, const uint8_t *input, uint8_t *output)
Decrypt a 8-byte block using DES algorithm.
void desProcessDataBlock(const uint8_t *input, uint8_t *output)
Encrypt/decrypt a 16-byte block using DES algorithm.
void aesProcessData(AesContext *context, uint8_t *iv, const uint8_t *input, uint8_t *output, size_t length, uint32_t mode)
Perform AES encryption or decryption.
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 des3Init(Des3Context *context, const uint8_t *key, size_t keyLen)
Initialize a Triple DES context using the supplied key.
void des3EncryptBlock(Des3Context *context, const uint8_t *input, uint8_t *output)
Encrypt a 8-byte block using Triple DES algorithm.
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.
error_t cfbDecrypt(const CipherAlgo *cipher, void *context, uint_t s, uint8_t *iv, const uint8_t *c, uint8_t *p, size_t length)
CFB decryption.
void aesEncryptBlock(AesContext *context, const uint8_t *input, uint8_t *output)
Encrypt a 16-byte block using AES algorithm.
void desEncryptBlock(DesContext *context, const uint8_t *input, uint8_t *output)
Encrypt a 8-byte block using DES algorithm.
error_t ecbEncrypt(const CipherAlgo *cipher, void *context, const uint8_t *p, uint8_t *c, size_t length)
ECB encryption.
void desProcessData(DesContext *context, uint8_t *iv, const uint8_t *input, uint8_t *output, size_t length, uint32_t mode)
Perform DES encryption or decryption.
error_t cfbEncrypt(const CipherAlgo *cipher, void *context, uint_t s, uint8_t *iv, const uint8_t *p, uint8_t *c, size_t length)
CFB encryption.
void aesProcessDataBlock(const uint8_t *input, uint8_t *output)
Encrypt/decrypt a 16-byte block using AES algorithm.
error_t ofbEncrypt(const CipherAlgo *cipher, void *context, uint_t s, uint8_t *iv, const uint8_t *p, uint8_t *c, size_t length)
OFB encryption.
error_t desInit(DesContext *context, const uint8_t *key, size_t keyLen)
Initialize a DES context using the supplied key.
error_t ecbDecrypt(const CipherAlgo *cipher, void *context, const uint8_t *c, uint8_t *p, size_t length)
ECB decryption.
SAMA5D3 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
Triple DES algorithm context.
Definition: des3.h:59
DesContext k2
Definition: des3.h:61
DesContext k3
Definition: des3.h:62
DesContext k1
Definition: des3.h:60
DES algorithm context.
Definition: des.h:58
uint32_t ks[32]
Definition: des.h:59
uint8_t length
Definition: tcp.h:368
uint16_t block
Definition: tftp_common.h:115