des3.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* des3.h
  2. *
  3. * Copyright (C) 2006-2022 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. #if defined(HAVE_FIPS) && \
  33. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2))
  34. /* included for fips @wc_fips */
  35. #include <cyassl/ctaocrypt/des3.h>
  36. #endif
  37. #ifdef __cplusplus
  38. extern "C" {
  39. #endif
  40. /* these are required for FIPS and non-FIPS */
  41. enum {
  42. DES_KEY_SIZE = 8, /* des */
  43. DES3_KEY_SIZE = 24, /* 3 des ede */
  44. DES_IV_SIZE = 8, /* should be the same as DES_BLOCK_SIZE */
  45. };
  46. /* avoid redefinition of structs */
  47. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  48. HAVE_FIPS_VERSION >= 2)
  49. #ifdef WOLFSSL_ASYNC_CRYPT
  50. #include <wolfssl/wolfcrypt/async.h>
  51. #endif
  52. enum {
  53. DES_ENC_TYPE = WC_CIPHER_DES, /* cipher unique type */
  54. DES3_ENC_TYPE = WC_CIPHER_DES3, /* cipher unique type */
  55. DES_BLOCK_SIZE = 8,
  56. DES_KS_SIZE = 32, /* internal DES key buffer size */
  57. DES_ENCRYPTION = 0,
  58. DES_DECRYPTION = 1
  59. };
  60. #define DES_IVLEN 8
  61. #define DES_KEYLEN 8
  62. #define DES3_IVLEN 8
  63. #define DES3_KEYLEN 24
  64. #if defined(STM32_CRYPTO)
  65. #include <wolfssl/wolfcrypt/port/st/stm32.h>
  66. enum {
  67. DES_CBC = 0,
  68. DES_ECB = 1
  69. };
  70. #endif
  71. /* DES encryption and decryption */
  72. typedef struct Des {
  73. word32 reg[DES_BLOCK_SIZE / sizeof(word32)]; /* for CBC mode */
  74. word32 tmp[DES_BLOCK_SIZE / sizeof(word32)]; /* same */
  75. word32 key[DES_KS_SIZE];
  76. } Des;
  77. /* DES3 encryption and decryption */
  78. struct Des3 {
  79. word32 key[3][DES_KS_SIZE];
  80. word32 reg[DES_BLOCK_SIZE / sizeof(word32)]; /* for CBC mode */
  81. word32 tmp[DES_BLOCK_SIZE / sizeof(word32)]; /* same */
  82. #ifdef WOLFSSL_ASYNC_CRYPT
  83. WC_ASYNC_DEV asyncDev;
  84. #endif
  85. #if defined(WOLF_CRYPTO_CB) || \
  86. (defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_3DES))
  87. word32 devKey[DES3_KEYLEN/sizeof(word32)]; /* raw key */
  88. #endif
  89. #ifdef WOLF_CRYPTO_CB
  90. int devId;
  91. void* devCtx;
  92. #endif
  93. void* heap;
  94. };
  95. #ifndef WC_DES3_TYPE_DEFINED
  96. typedef struct Des3 Des3;
  97. #define WC_DES3_TYPE_DEFINED
  98. #endif
  99. #endif /* HAVE_FIPS && HAVE_FIPS_VERSION >= 2 */
  100. WOLFSSL_API int wc_Des_SetKey(Des* des, const byte* key,
  101. const byte* iv, int dir);
  102. WOLFSSL_API void wc_Des_SetIV(Des* des, const byte* iv);
  103. WOLFSSL_API int wc_Des_CbcEncrypt(Des* des, byte* out,
  104. const byte* in, word32 sz);
  105. WOLFSSL_API int wc_Des_CbcDecrypt(Des* des, byte* out,
  106. const byte* in, word32 sz);
  107. WOLFSSL_API int wc_Des_EcbEncrypt(Des* des, byte* out,
  108. const byte* in, word32 sz);
  109. WOLFSSL_API int wc_Des3_EcbEncrypt(Des3* des, byte* out,
  110. const byte* in, word32 sz);
  111. /* ECB decrypt same process as encrypt but with decrypt key */
  112. #define wc_Des_EcbDecrypt wc_Des_EcbEncrypt
  113. #define wc_Des3_EcbDecrypt wc_Des3_EcbEncrypt
  114. WOLFSSL_API int wc_Des3_SetKey(Des3* des, const byte* key,
  115. const byte* iv,int dir);
  116. WOLFSSL_API int wc_Des3_SetIV(Des3* des, const byte* iv);
  117. WOLFSSL_API int wc_Des3_CbcEncrypt(Des3* des, byte* out,
  118. const byte* in,word32 sz);
  119. WOLFSSL_API int wc_Des3_CbcDecrypt(Des3* des, byte* out,
  120. const byte* in,word32 sz);
  121. /* These are only required when using either:
  122. static memory (WOLFSSL_STATIC_MEMORY) or asynchronous (WOLFSSL_ASYNC_CRYPT) */
  123. WOLFSSL_API int wc_Des3Init(Des3* des3, void* heap, int devId);
  124. WOLFSSL_API void wc_Des3Free(Des3* des3);
  125. #ifdef __cplusplus
  126. } /* extern "C" */
  127. #endif
  128. #endif /* NO_DES3 */
  129. #endif /* WOLF_CRYPT_DES3_H */