1 | /* des3.h
|
---|
2 | *
|
---|
3 | * Copyright (C) 2006-2017 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 | /*!
|
---|
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) && \
|
---|
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 | typedef 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 | const byte* key_raw;
|
---|
104 | const byte* iv_raw;
|
---|
105 | WC_ASYNC_DEV asyncDev;
|
---|
106 | #endif
|
---|
107 | void* heap;
|
---|
108 | } Des3;
|
---|
109 | #endif /* HAVE_FIPS */
|
---|
110 |
|
---|
111 |
|
---|
112 | WOLFSSL_API int wc_Des_SetKey(Des* des, const byte* key,
|
---|
113 | const byte* iv, int dir);
|
---|
114 | WOLFSSL_API void wc_Des_SetIV(Des* des, const byte* iv);
|
---|
115 | WOLFSSL_API int wc_Des_CbcEncrypt(Des* des, byte* out,
|
---|
116 | const byte* in, word32 sz);
|
---|
117 | WOLFSSL_API int wc_Des_CbcDecrypt(Des* des, byte* out,
|
---|
118 | const byte* in, word32 sz);
|
---|
119 | WOLFSSL_API int wc_Des_EcbEncrypt(Des* des, byte* out,
|
---|
120 | const byte* in, word32 sz);
|
---|
121 | WOLFSSL_API int wc_Des3_EcbEncrypt(Des3* des, byte* out,
|
---|
122 | const byte* in, word32 sz);
|
---|
123 |
|
---|
124 | /* ECB decrypt same process as encrypt but with decrypt key */
|
---|
125 | #define wc_Des_EcbDecrypt wc_Des_EcbEncrypt
|
---|
126 | #define wc_Des3_EcbDecrypt wc_Des3_EcbEncrypt
|
---|
127 |
|
---|
128 | WOLFSSL_API int wc_Des3_SetKey(Des3* des, const byte* key,
|
---|
129 | const byte* iv,int dir);
|
---|
130 | WOLFSSL_API int wc_Des3_SetIV(Des3* des, const byte* iv);
|
---|
131 | WOLFSSL_API int wc_Des3_CbcEncrypt(Des3* des, byte* out,
|
---|
132 | const byte* in,word32 sz);
|
---|
133 | WOLFSSL_API int wc_Des3_CbcDecrypt(Des3* des, byte* out,
|
---|
134 | const byte* in,word32 sz);
|
---|
135 |
|
---|
136 | /* These are only required when using either:
|
---|
137 | static memory (WOLFSSL_STATIC_MEMORY) or asynchronous (WOLFSSL_ASYNC_CRYPT) */
|
---|
138 | WOLFSSL_API int wc_Des3Init(Des3*, void*, int);
|
---|
139 | WOLFSSL_API void wc_Des3Free(Des3*);
|
---|
140 |
|
---|
141 | #ifdef __cplusplus
|
---|
142 | } /* extern "C" */
|
---|
143 | #endif
|
---|
144 |
|
---|
145 | #endif /* NO_DES3 */
|
---|
146 | #endif /* WOLF_CRYPT_DES3_H */
|
---|
147 |
|
---|