des3.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /**
  2. * @file des3.c
  3. * @brief Triple DES (Triple Data Encryption Algorithm)
  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. * Triple DES is an encryption algorithm designed to encipher and decipher blocks
  30. * of 64 bits under control of a 192-bit key. Refer to FIPS 46-3 for more details
  31. *
  32. * @author Oryx Embedded SARL (www.oryx-embedded.com)
  33. * @version 2.2.4
  34. **/
  35. //Switch to the appropriate trace level
  36. #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
  37. //Dependencies
  38. #include "core/crypto.h"
  39. #include "cipher/des3.h"
  40. #include "cipher/des.h"
  41. //Check crypto library configuration
  42. #if (DES3_SUPPORT == ENABLED)
  43. //Common interface for encryption algorithms
  44. const CipherAlgo des3CipherAlgo =
  45. {
  46. "3DES",
  47. sizeof(Des3Context),
  48. CIPHER_ALGO_TYPE_BLOCK,
  49. DES3_BLOCK_SIZE,
  50. (CipherAlgoInit) des3Init,
  51. NULL,
  52. NULL,
  53. (CipherAlgoEncryptBlock) des3EncryptBlock,
  54. (CipherAlgoDecryptBlock) des3DecryptBlock,
  55. (CipherAlgoDeinit) des3Deinit
  56. };
  57. /**
  58. * @brief Initialize a Triple DES context using the supplied key
  59. * @param[in] context Pointer to the Triple DES context to initialize
  60. * @param[in] key Pointer to the key
  61. * @param[in] keyLen Length of the key
  62. * @return Error code
  63. **/
  64. error_t des3Init(Des3Context *context, const uint8_t *key,
  65. size_t keyLen)
  66. {
  67. //Check parameters
  68. if(context == NULL || key == NULL)
  69. return WINDOWS_ERROR_INVALID_PARAMETER;
  70. //Check key length
  71. if(keyLen == 8)
  72. {
  73. //This option provides backward compatibility with DES, because the
  74. //first and second DES operations cancel out
  75. desInit(&context->k1, key, 8);
  76. desInit(&context->k2, key, 8);
  77. desInit(&context->k3, key, 8);
  78. }
  79. else if(keyLen == 16)
  80. {
  81. //If the key length is 128 bits including parity, the first 8 bytes of the
  82. //encoding represent the key used for the two outer DES operations, and
  83. //the second 8 bytes represent the key used for the inner DES operation
  84. desInit(&context->k1, key, 8);
  85. desInit(&context->k2, key + 8, 8);
  86. desInit(&context->k3, key, 8);
  87. }
  88. else if(keyLen == 24)
  89. {
  90. //If the key length is 192 bits including parity, then 3 independent DES
  91. //keys are represented, in the order in which they are used for encryption
  92. desInit(&context->k1, key, 8);
  93. desInit(&context->k2, key + 8, 8);
  94. desInit(&context->k3, key + 16, 8);
  95. }
  96. else
  97. {
  98. //The length of the key is not valid
  99. return ERROR_INVALID_KEY_LENGTH;
  100. }
  101. //No error to report
  102. return WINDOWS_NO_ERROR;
  103. }
  104. /**
  105. * @brief Encrypt a 8-byte block using Triple DES algorithm
  106. * @param[in] context Pointer to the Triple DES context
  107. * @param[in] input Plaintext block to encrypt
  108. * @param[out] output Ciphertext block resulting from encryption
  109. **/
  110. void des3EncryptBlock(Des3Context *context, const uint8_t *input,
  111. uint8_t *output)
  112. {
  113. //The first pass is a DES encryption
  114. desEncryptBlock(&context->k1, input, output);
  115. //The second pass is a DES decryption of the first ciphertext result
  116. desDecryptBlock(&context->k2, output, output);
  117. //The third pass is a DES encryption of the second pass result
  118. desEncryptBlock(&context->k3, output, output);
  119. }
  120. /**
  121. * @brief Decrypt a 8-byte block using Triple DES algorithm
  122. * @param[in] context Pointer to the Triple DES context
  123. * @param[in] input Ciphertext block to decrypt
  124. * @param[out] output Plaintext block resulting from decryption
  125. **/
  126. void des3DecryptBlock(Des3Context *context, const uint8_t *input,
  127. uint8_t *output)
  128. {
  129. //The first pass is a DES decryption
  130. desDecryptBlock(&context->k3, input, output);
  131. //The second pass is a DES encryption of the first pass result
  132. desEncryptBlock(&context->k2, output, output);
  133. //The third pass is a DES decryption of the second ciphertext result
  134. desDecryptBlock(&context->k1, output, output);
  135. }
  136. /**
  137. * @brief Release Triple DES context
  138. * @param[in] context Pointer to the Triple DES context
  139. **/
  140. __weak_func void des3Deinit(Des3Context *context)
  141. {
  142. //Clear Triple DES context
  143. osMemset(context, 0, sizeof(Des3Context));
  144. }
  145. #endif