des3.h (5064B)
1 /* des3.h 2 * 3 * Copyright (C) 2006-2025 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 3 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 /*! 23 \file wolfssl/wolfcrypt/des3.h 24 */ 25 26 #ifndef WOLF_CRYPT_DES3_H 27 #define WOLF_CRYPT_DES3_H 28 29 #include <wolfssl/wolfcrypt/types.h> 30 31 #ifndef NO_DES3 32 33 #if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \ 34 (HAVE_FIPS_VERSION == 2 || HAVE_FIPS_VERSION == 3) 35 #include <wolfssl/wolfcrypt/fips.h> 36 #endif /* HAVE_FIPS_VERSION >= 2 */ 37 38 #ifdef __cplusplus 39 extern "C" { 40 #endif 41 42 /* these are required for FIPS and non-FIPS */ 43 enum { 44 DES_KEY_SIZE = 8, /* des */ 45 DES3_KEY_SIZE = 24, /* 3 des ede */ 46 DES_IV_SIZE = 8 /* should be the same as DES_BLOCK_SIZE */ 47 }; 48 49 50 /* avoid redefinition of structs */ 51 #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \ 52 HAVE_FIPS_VERSION >= 2) 53 54 #ifdef WOLFSSL_ASYNC_CRYPT 55 #include <wolfssl/wolfcrypt/async.h> 56 #endif 57 58 #ifdef WOLFSSL_SE050 59 /* SE050 SDK also defines DES_BLOCK_SIZE */ 60 #undef DES_BLOCK_SIZE 61 #endif 62 63 enum { 64 DES_ENC_TYPE = WC_CIPHER_DES, /* cipher unique type */ 65 DES3_ENC_TYPE = WC_CIPHER_DES3, /* cipher unique type */ 66 67 DES_BLOCK_SIZE = 8, 68 DES_KS_SIZE = 32, /* internal DES key buffer size */ 69 70 DES_ENCRYPTION = 0, 71 DES_DECRYPTION = 1 72 }; 73 74 #define DES_IVLEN 8 75 #define DES_KEYLEN 8 76 #define DES3_IVLEN 8 77 #define DES3_KEYLEN 24 78 79 80 #if defined(STM32_CRYPTO) 81 82 #include <wolfssl/wolfcrypt/port/st/stm32.h> 83 84 enum { 85 DES_CBC = 0, 86 DES_ECB = 1 87 }; 88 #endif 89 90 91 /* DES encryption and decryption */ 92 typedef struct Des { 93 word32 reg[DES_BLOCK_SIZE / sizeof(word32)]; /* for CBC mode */ 94 word32 tmp[DES_BLOCK_SIZE / sizeof(word32)]; /* same */ 95 word32 key[DES_KS_SIZE]; 96 } Des; 97 98 99 /* DES3 encryption and decryption */ 100 struct Des3 { 101 word32 key[3][DES_KS_SIZE]; 102 word32 reg[DES_BLOCK_SIZE / sizeof(word32)]; /* for CBC mode */ 103 word32 tmp[DES_BLOCK_SIZE / sizeof(word32)]; /* same */ 104 #ifdef WOLFSSL_ASYNC_CRYPT 105 WC_ASYNC_DEV asyncDev; 106 #endif 107 #if defined(WOLF_CRYPTO_CB) || \ 108 (defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_3DES)) 109 word32 devKey[DES3_KEYLEN/sizeof(word32)]; /* raw key */ 110 #endif 111 #ifdef WOLF_CRYPTO_CB 112 int devId; 113 void* devCtx; 114 #endif 115 void* heap; 116 }; 117 118 #ifndef WC_DES3_TYPE_DEFINED 119 typedef struct Des3 Des3; 120 #define WC_DES3_TYPE_DEFINED 121 #endif 122 #endif /* HAVE_FIPS && HAVE_FIPS_VERSION >= 2 */ 123 124 125 WOLFSSL_API int wc_Des_SetKey(Des* des, const byte* key, 126 const byte* iv, int dir); 127 WOLFSSL_API void wc_Des_SetIV(Des* des, const byte* iv); 128 WOLFSSL_API int wc_Des_CbcEncrypt(Des* des, byte* out, 129 const byte* in, word32 sz); 130 WOLFSSL_API int wc_Des_CbcDecrypt(Des* des, byte* out, 131 const byte* in, word32 sz); 132 WOLFSSL_API int wc_Des_EcbEncrypt(Des* des, byte* out, 133 const byte* in, word32 sz); 134 WOLFSSL_API int wc_Des3_EcbEncrypt(Des3* des, byte* out, 135 const byte* in, word32 sz); 136 137 #ifdef FREESCALE_MMCAU /* Has separate encrypt/decrypt functions */ 138 WOLFSSL_API int wc_Des_EcbDecrypt(Des* des, byte* out, 139 const byte* in, word32 sz); 140 WOLFSSL_API int wc_Des3_EcbDecrypt(Des3* des, byte* out, 141 const byte* in, word32 sz); 142 #else 143 /* ECB decrypt same process as encrypt but with decrypt key */ 144 #define wc_Des_EcbDecrypt wc_Des_EcbEncrypt 145 #define wc_Des3_EcbDecrypt wc_Des3_EcbEncrypt 146 #endif 147 148 WOLFSSL_API int wc_Des3_SetKey(Des3* des, const byte* key, 149 const byte* iv,int dir); 150 WOLFSSL_API int wc_Des3_SetIV(Des3* des, const byte* iv); 151 WOLFSSL_API int wc_Des3_CbcEncrypt(Des3* des, byte* out, 152 const byte* in,word32 sz); 153 WOLFSSL_API int wc_Des3_CbcDecrypt(Des3* des, byte* out, 154 const byte* in,word32 sz); 155 156 /* These are only required when using either: 157 static memory (WOLFSSL_STATIC_MEMORY) or asynchronous (WOLFSSL_ASYNC_CRYPT) */ 158 WOLFSSL_API int wc_Des3Init(Des3* des3, void* heap, int devId); 159 WOLFSSL_API void wc_Des3Free(Des3* des3); 160 161 #ifdef __cplusplus 162 } /* extern "C" */ 163 #endif 164 165 #endif /* NO_DES3 */ 166 #endif /* WOLF_CRYPT_DES3_H */ 167