ecb.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /**
  2. * @file ecb.c
  3. * @brief Electronic Codebook (ECB) mode
  4. *
  5. * @section License
  6. *
  7. * SPDX-License-Identifier: GPL-2.0-or-later
  8. *
  9. * Copyright (C) 2010-2023 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. * The Electronic Codebook (ECB) mode is a confidentiality mode that features,
  30. * for a given key, the assignment of a fixed ciphertext block to each
  31. * plaintext block, analogous to the assignment of code words in a codebook.
  32. * Refer to SP 800-38A for more details
  33. *
  34. * @author Oryx Embedded SARL (www.oryx-embedded.com)
  35. * @version 2.2.4
  36. **/
  37. //Switch to the appropriate trace level
  38. #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
  39. //Dependencies
  40. #include "core/crypto.h"
  41. #include "cipher_modes/ecb.h"
  42. #include "debug.h"
  43. //Check crypto library configuration
  44. #if (ECB_SUPPORT == ENABLED)
  45. /**
  46. * @brief ECB encryption
  47. * @param[in] cipher Cipher algorithm
  48. * @param[in] context Cipher algorithm context
  49. * @param[in] p Plaintext to be encrypted
  50. * @param[out] c Ciphertext resulting from the encryption
  51. * @param[in] length Total number of data bytes to be encrypted
  52. * @return Error code
  53. **/
  54. error_t ecbEncrypt(const CipherAlgo *cipher, void *context,
  55. const uint8_t *p, uint8_t *c, size_t length)
  56. {
  57. //ECB mode operates in a block-by-block fashion
  58. while(length >= cipher->blockSize)
  59. {
  60. //Encrypt current block
  61. cipher->encryptBlock(context, p, c);
  62. //Next block
  63. p += cipher->blockSize;
  64. c += cipher->blockSize;
  65. length -= cipher->blockSize;
  66. }
  67. //The plaintext must be a multiple of the block size
  68. if(length != 0)
  69. return ERROR_INVALID_LENGTH;
  70. //Successful encryption
  71. return WINDOWS_NO_ERROR;
  72. }
  73. /**
  74. * @brief ECB decryption
  75. * @param[in] cipher Cipher algorithm
  76. * @param[in] context Cipher algorithm context
  77. * @param[in] c Ciphertext to be decrypted
  78. * @param[out] p Plaintext resulting from the decryption
  79. * @param[in] length Total number of data bytes to be decrypted
  80. * @return Error code
  81. **/
  82. error_t ecbDecrypt(const CipherAlgo *cipher, void *context,
  83. const uint8_t *c, uint8_t *p, size_t length)
  84. {
  85. //ECB mode operates in a block-by-block fashion
  86. while(length >= cipher->blockSize)
  87. {
  88. //Decrypt current block
  89. cipher->decryptBlock(context, c, p);
  90. //Next block
  91. c += cipher->blockSize;
  92. p += cipher->blockSize;
  93. length -= cipher->blockSize;
  94. }
  95. //The ciphertext must be a multiple of the block size
  96. if(length != 0)
  97. return ERROR_INVALID_LENGTH;
  98. //Successful encryption
  99. return WINDOWS_NO_ERROR;
  100. }
  101. #endif