des3.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /* des3.h
  2. *
  3. * Copyright (C) 2006-2023 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. /*!
  22. \file wolfssl/wolfcrypt/des3.h
  23. */
  24. #ifndef WOLF_CRYPT_DES3_H
  25. #define WOLF_CRYPT_DES3_H
  26. #include <wolfssl/wolfcrypt/types.h>
  27. #ifndef NO_DES3
  28. #if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  29. (HAVE_FIPS_VERSION == 2 || HAVE_FIPS_VERSION == 3)
  30. #include <wolfssl/wolfcrypt/fips.h>
  31. #endif /* HAVE_FIPS_VERSION >= 2 */
  32. #ifdef __cplusplus
  33. extern "C" {
  34. #endif
  35. /* these are required for FIPS and non-FIPS */
  36. enum {
  37. DES_KEY_SIZE = 8, /* des */
  38. DES3_KEY_SIZE = 24, /* 3 des ede */
  39. DES_IV_SIZE = 8 /* should be the same as DES_BLOCK_SIZE */
  40. };
  41. /* avoid redefinition of structs */
  42. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  43. HAVE_FIPS_VERSION >= 2)
  44. #ifdef WOLFSSL_ASYNC_CRYPT
  45. #include <wolfssl/wolfcrypt/async.h>
  46. #endif
  47. #ifdef WOLFSSL_SE050
  48. /* SE050 SDK also defines DES_BLOCK_SIZE */
  49. #undef DES_BLOCK_SIZE
  50. #endif
  51. enum {
  52. DES_ENC_TYPE = WC_CIPHER_DES, /* cipher unique type */
  53. DES3_ENC_TYPE = WC_CIPHER_DES3, /* cipher unique type */
  54. DES_BLOCK_SIZE = 8,
  55. DES_KS_SIZE = 32, /* internal DES key buffer size */
  56. DES_ENCRYPTION = 0,
  57. DES_DECRYPTION = 1
  58. };
  59. #define DES_IVLEN 8
  60. #define DES_KEYLEN 8
  61. #define DES3_IVLEN 8
  62. #define DES3_KEYLEN 24
  63. #if defined(STM32_CRYPTO)
  64. #include <wolfssl/wolfcrypt/port/st/stm32.h>
  65. enum {
  66. DES_CBC = 0,
  67. DES_ECB = 1
  68. };
  69. #endif
  70. /* DES encryption and decryption */
  71. typedef struct Des {
  72. word32 reg[DES_BLOCK_SIZE / sizeof(word32)]; /* for CBC mode */
  73. word32 tmp[DES_BLOCK_SIZE / sizeof(word32)]; /* same */
  74. word32 key[DES_KS_SIZE];
  75. } Des;
  76. /* DES3 encryption and decryption */
  77. struct Des3 {
  78. word32 key[3][DES_KS_SIZE];
  79. word32 reg[DES_BLOCK_SIZE / sizeof(word32)]; /* for CBC mode */
  80. word32 tmp[DES_BLOCK_SIZE / sizeof(word32)]; /* same */
  81. #ifdef WOLFSSL_ASYNC_CRYPT
  82. WC_ASYNC_DEV asyncDev;
  83. #endif
  84. #if defined(WOLF_CRYPTO_CB) || \
  85. (defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_3DES))
  86. word32 devKey[DES3_KEYLEN/sizeof(word32)]; /* raw key */
  87. #endif
  88. #ifdef WOLF_CRYPTO_CB
  89. int devId;
  90. void* devCtx;
  91. #endif
  92. void* heap;
  93. };
  94. #ifndef WC_DES3_TYPE_DEFINED
  95. typedef struct Des3 Des3;
  96. #define WC_DES3_TYPE_DEFINED
  97. #endif
  98. #endif /* HAVE_FIPS && HAVE_FIPS_VERSION >= 2 */
  99. WOLFSSL_API int wc_Des_SetKey(Des* des, const byte* key,
  100. const byte* iv, int dir);
  101. WOLFSSL_API void wc_Des_SetIV(Des* des, const byte* iv);
  102. WOLFSSL_API int wc_Des_CbcEncrypt(Des* des, byte* out,
  103. const byte* in, word32 sz);
  104. WOLFSSL_API int wc_Des_CbcDecrypt(Des* des, byte* out,
  105. const byte* in, word32 sz);
  106. WOLFSSL_API int wc_Des_EcbEncrypt(Des* des, byte* out,
  107. const byte* in, word32 sz);
  108. WOLFSSL_API int wc_Des3_EcbEncrypt(Des3* des, byte* out,
  109. const byte* in, word32 sz);
  110. /* ECB decrypt same process as encrypt but with decrypt key */
  111. #define wc_Des_EcbDecrypt wc_Des_EcbEncrypt
  112. #define wc_Des3_EcbDecrypt wc_Des3_EcbEncrypt
  113. WOLFSSL_API int wc_Des3_SetKey(Des3* des, const byte* key,
  114. const byte* iv,int dir);
  115. WOLFSSL_API int wc_Des3_SetIV(Des3* des, const byte* iv);
  116. WOLFSSL_API int wc_Des3_CbcEncrypt(Des3* des, byte* out,
  117. const byte* in,word32 sz);
  118. WOLFSSL_API int wc_Des3_CbcDecrypt(Des3* des, byte* out,
  119. const byte* in,word32 sz);
  120. /* These are only required when using either:
  121. static memory (WOLFSSL_STATIC_MEMORY) or asynchronous (WOLFSSL_ASYNC_CRYPT) */
  122. WOLFSSL_API int wc_Des3Init(Des3* des3, void* heap, int devId);
  123. WOLFSSL_API void wc_Des3Free(Des3* des3);
  124. #ifdef __cplusplus
  125. } /* extern "C" */
  126. #endif
  127. #endif /* NO_DES3 */
  128. #endif /* WOLF_CRYPT_DES3_H */