des3.h
Go to the documentation of this file.
1 /* des3.h
2  *
3  * Copyright (C) 2006-2020 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 
26 #ifndef WOLF_CRYPT_DES3_H
27 #define WOLF_CRYPT_DES3_H
28 
30 
31 #ifndef NO_DES3
32 
33 #if defined(HAVE_FIPS) && \
34  defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
35  #include <wolfssl/wolfcrypt/fips.h>
36 #endif /* HAVE_FIPS_VERSION >= 2 */
37 
38 #if defined(HAVE_FIPS) && \
39  (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2))
40 /* included for fips @wc_fips */
41 #include <cyassl/ctaocrypt/des3.h>
42 #endif
43 
44 #ifdef __cplusplus
45  extern "C" {
46 #endif
47 
48 /* these are required for FIPS and non-FIPS */
49 enum {
50  DES_KEY_SIZE = 8, /* des */
51  DES3_KEY_SIZE = 24, /* 3 des ede */
52  DES_IV_SIZE = 8, /* should be the same as DES_BLOCK_SIZE */
53 };
54 
55 
56 /* avoid redefinition of structs */
57 #if !defined(HAVE_FIPS) || \
58  (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
59 
60 #ifdef WOLFSSL_ASYNC_CRYPT
61  #include <wolfssl/wolfcrypt/async.h>
62 #endif
63 
64 enum {
65  DES_ENC_TYPE = WC_CIPHER_DES, /* cipher unique type */
66  DES3_ENC_TYPE = WC_CIPHER_DES3, /* cipher unique type */
67 
68  DES_BLOCK_SIZE = 8,
69  DES_KS_SIZE = 32, /* internal DES key buffer size */
70 
71  DES_ENCRYPTION = 0,
72  DES_DECRYPTION = 1
73 };
74 
75 #define DES_IVLEN 8
76 #define DES_KEYLEN 8
77 #define DES3_IVLEN 8
78 #define DES3_KEYLEN 24
79 
80 
81 #if defined(STM32_CRYPTO)
82 enum {
83  DES_CBC = 0,
84  DES_ECB = 1
85 };
86 #endif
87 
88 
89 /* DES encryption and decryption */
90 typedef struct Des {
91  word32 reg[DES_BLOCK_SIZE / sizeof(word32)]; /* for CBC mode */
92  word32 tmp[DES_BLOCK_SIZE / sizeof(word32)]; /* same */
93  word32 key[DES_KS_SIZE];
94 } Des;
95 
96 
97 /* DES3 encryption and decryption */
98 struct Des3 {
99  word32 key[3][DES_KS_SIZE];
100  word32 reg[DES_BLOCK_SIZE / sizeof(word32)]; /* for CBC mode */
101  word32 tmp[DES_BLOCK_SIZE / sizeof(word32)]; /* same */
102 #ifdef WOLFSSL_ASYNC_CRYPT
103  WC_ASYNC_DEV asyncDev;
104 #endif
105 #if defined(WOLF_CRYPTO_CB) || \
106  (defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_3DES))
107  word32 devKey[DES3_KEYLEN/sizeof(word32)]; /* raw key */
108 #endif
109 #ifdef WOLF_CRYPTO_CB
110  int devId;
111  void* devCtx;
112 #endif
113  void* heap;
114 };
115 
116 #ifndef WC_DES3_TYPE_DEFINED
117  typedef struct Des3 Des3;
118  #define WC_DES3_TYPE_DEFINED
119 #endif
120 #endif /* HAVE_FIPS */
121 
122 
123 WOLFSSL_API int wc_Des_SetKey(Des* des, const byte* key,
124  const byte* iv, int dir);
125 WOLFSSL_API void wc_Des_SetIV(Des* des, const byte* iv);
126 WOLFSSL_API int wc_Des_CbcEncrypt(Des* des, byte* out,
127  const byte* in, word32 sz);
128 WOLFSSL_API int wc_Des_CbcDecrypt(Des* des, byte* out,
129  const byte* in, word32 sz);
130 WOLFSSL_API int wc_Des_EcbEncrypt(Des* des, byte* out,
131  const byte* in, word32 sz);
132 WOLFSSL_API int wc_Des3_EcbEncrypt(Des3* des, byte* out,
133  const byte* in, word32 sz);
134 
135 /* ECB decrypt same process as encrypt but with decrypt key */
136 #define wc_Des_EcbDecrypt wc_Des_EcbEncrypt
137 #define wc_Des3_EcbDecrypt wc_Des3_EcbEncrypt
138 
139 WOLFSSL_API int wc_Des3_SetKey(Des3* des, const byte* key,
140  const byte* iv,int dir);
141 WOLFSSL_API int wc_Des3_SetIV(Des3* des, const byte* iv);
142 WOLFSSL_API int wc_Des3_CbcEncrypt(Des3* des, byte* out,
143  const byte* in,word32 sz);
144 WOLFSSL_API int wc_Des3_CbcDecrypt(Des3* des, byte* out,
145  const byte* in,word32 sz);
146 
147 /* These are only required when using either:
148  static memory (WOLFSSL_STATIC_MEMORY) or asynchronous (WOLFSSL_ASYNC_CRYPT) */
149 WOLFSSL_API int wc_Des3Init(Des3*, void*, int);
150 WOLFSSL_API void wc_Des3Free(Des3*);
151 
152 #ifdef __cplusplus
153  } /* extern "C" */
154 #endif
155 
156 #endif /* NO_DES3 */
157 #endif /* WOLF_CRYPT_DES3_H */
158 
WOLFSSL_API int wc_Des3_CbcDecrypt(Des3 *des, byte *out, const byte *in, word32 sz)
This function decrypts the input ciphertext, in, and stores the resulting plaintext in the output buf...
Definition: des3.c:471
WOLFSSL_API int wc_Des3_CbcEncrypt(Des3 *des, byte *out, const byte *in, word32 sz)
This function encrypts the input message, in, and stores the result in the output buffer...
Definition: des3.c:465
WOLFSSL_API void wc_Des_SetIV(Des *des, const byte *iv)
This function sets the initialization vector (iv) for the Des structure given as argument. When passed a NULL iv, it sets the initialization vector to 0.
Definition: des3.c:1762
Definition: des3.h:90
WOLFSSL_API int wc_Des3_SetKey(Des3 *des, const byte *key, const byte *iv, int dir)
This function sets the key and initialization vector (iv) for the Des3 structure given as argument...
Definition: des3.c:168
WOLFSSL_API int wc_Des_SetKey(Des *des, const byte *key, const byte *iv, int dir)
This function sets the key and initialization vector (iv) for the Des structure given as argument...
Definition: des3.c:152
WOLFSSL_API int wc_Des3_EcbEncrypt(Des3 *des, byte *out, const byte *in, word32 sz)
This function encrypts the input message, in, and stores the result in the output buffer...
Definition: des3.c:1152
WOLFSSL_API int wc_Des_CbcEncrypt(Des *des, byte *out, const byte *in, word32 sz)
This function encrypts the input message, in, and stores the result in the output buffer...
Definition: des3.c:330
WOLFSSL_API int wc_Des3_SetIV(Des3 *des, const byte *iv)
This function sets the initialization vector (iv) for the Des3 structure given as argument...
Definition: des3.c:1770
WOLFSSL_API int wc_Des_CbcDecrypt(Des *des, byte *out, const byte *in, word32 sz)
This function decrypts the input ciphertext, in, and stores the resulting plaintext in the output buf...
Definition: des3.c:336
Definition: des3.h:98
WOLFSSL_API int wc_Des_EcbEncrypt(Des *des, byte *out, const byte *in, word32 sz)
This function encrypts the input message, in, and stores the result in the output buffer...
Definition: des3.c:342