1 | /* wc_encrypt.c
|
---|
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 | #ifdef HAVE_CONFIG_H
|
---|
24 | #include <config.h>
|
---|
25 | #endif
|
---|
26 |
|
---|
27 | #include <wolfssl/wolfcrypt/settings.h>
|
---|
28 | #include <wolfssl/wolfcrypt/aes.h>
|
---|
29 | #include <wolfssl/wolfcrypt/des3.h>
|
---|
30 | #include <wolfssl/wolfcrypt/wc_encrypt.h>
|
---|
31 | #include <wolfssl/wolfcrypt/error-crypt.h>
|
---|
32 |
|
---|
33 |
|
---|
34 | #if !defined(NO_AES) && defined(HAVE_AES_CBC)
|
---|
35 | #ifdef HAVE_AES_DECRYPT
|
---|
36 | int wc_AesCbcDecryptWithKey(byte* out, const byte* in, word32 inSz,
|
---|
37 | const byte* key, word32 keySz, const byte* iv)
|
---|
38 | {
|
---|
39 | int ret = 0;
|
---|
40 | #ifdef WOLFSSL_SMALL_STACK
|
---|
41 | Aes* aes = NULL;
|
---|
42 | #else
|
---|
43 | Aes aes[1];
|
---|
44 | #endif
|
---|
45 |
|
---|
46 | if (out == NULL || in == NULL || key == NULL || iv == NULL) {
|
---|
47 | return BAD_FUNC_ARG;
|
---|
48 | }
|
---|
49 |
|
---|
50 | #ifdef WOLFSSL_SMALL_STACK
|
---|
51 | aes = (Aes*)XMALLOC(sizeof(Aes), NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
---|
52 | if (aes == NULL)
|
---|
53 | return MEMORY_E;
|
---|
54 | #endif
|
---|
55 |
|
---|
56 | ret = wc_AesInit(aes, NULL, INVALID_DEVID);
|
---|
57 | if (ret == 0) {
|
---|
58 | ret = wc_AesSetKey(aes, key, keySz, iv, AES_DECRYPTION);
|
---|
59 | if (ret == 0)
|
---|
60 | ret = wc_AesCbcDecrypt(aes, out, in, inSz);
|
---|
61 |
|
---|
62 | wc_AesFree(aes);
|
---|
63 | }
|
---|
64 |
|
---|
65 | #ifdef WOLFSSL_SMALL_STACK
|
---|
66 | XFREE(aes, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
---|
67 | #endif
|
---|
68 |
|
---|
69 | return ret;
|
---|
70 | }
|
---|
71 | #endif /* HAVE_AES_DECRYPT */
|
---|
72 |
|
---|
73 | int wc_AesCbcEncryptWithKey(byte* out, const byte* in, word32 inSz,
|
---|
74 | const byte* key, word32 keySz, const byte* iv)
|
---|
75 | {
|
---|
76 | int ret = 0;
|
---|
77 | #ifdef WOLFSSL_SMALL_STACK
|
---|
78 | Aes* aes = NULL;
|
---|
79 | #else
|
---|
80 | Aes aes[1];
|
---|
81 | #endif
|
---|
82 |
|
---|
83 | #ifdef WOLFSSL_SMALL_STACK
|
---|
84 | aes = (Aes*)XMALLOC(sizeof(Aes), NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
---|
85 | if (aes == NULL)
|
---|
86 | return MEMORY_E;
|
---|
87 | #endif
|
---|
88 |
|
---|
89 | ret = wc_AesInit(aes, NULL, INVALID_DEVID);
|
---|
90 | if (ret == 0) {
|
---|
91 | ret = wc_AesSetKey(aes, key, keySz, iv, AES_ENCRYPTION);
|
---|
92 | if (ret == 0)
|
---|
93 | ret = wc_AesCbcEncrypt(aes, out, in, inSz);
|
---|
94 |
|
---|
95 | wc_AesFree(aes);
|
---|
96 | }
|
---|
97 |
|
---|
98 | #ifdef WOLFSSL_SMALL_STACK
|
---|
99 | XFREE(aes, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
---|
100 | #endif
|
---|
101 |
|
---|
102 | return ret;
|
---|
103 | }
|
---|
104 | #endif /* !NO_AES && HAVE_AES_CBC */
|
---|
105 |
|
---|
106 |
|
---|
107 | #ifndef NO_DES3
|
---|
108 | int wc_Des_CbcEncryptWithKey(byte* out, const byte* in, word32 sz,
|
---|
109 | const byte* key, const byte* iv)
|
---|
110 | {
|
---|
111 | int ret = 0;
|
---|
112 | #ifdef WOLFSSL_SMALL_STACK
|
---|
113 | Des* des = NULL;
|
---|
114 | #else
|
---|
115 | Des des[1];
|
---|
116 | #endif
|
---|
117 |
|
---|
118 | #ifdef WOLFSSL_SMALL_STACK
|
---|
119 | des = (Des*)XMALLOC(sizeof(Des), NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
---|
120 | if (des == NULL)
|
---|
121 | return MEMORY_E;
|
---|
122 | #endif
|
---|
123 |
|
---|
124 | ret = wc_Des_SetKey(des, key, iv, DES_ENCRYPTION);
|
---|
125 | if (ret == 0)
|
---|
126 | ret = wc_Des_CbcEncrypt(des, out, in, sz);
|
---|
127 |
|
---|
128 | #ifdef WOLFSSL_SMALL_STACK
|
---|
129 | XFREE(des, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
---|
130 | #endif
|
---|
131 |
|
---|
132 | return ret;
|
---|
133 | }
|
---|
134 |
|
---|
135 | int wc_Des_CbcDecryptWithKey(byte* out, const byte* in, word32 sz,
|
---|
136 | const byte* key, const byte* iv)
|
---|
137 | {
|
---|
138 | int ret = 0;
|
---|
139 | #ifdef WOLFSSL_SMALL_STACK
|
---|
140 | Des* des = NULL;
|
---|
141 | #else
|
---|
142 | Des des[1];
|
---|
143 | #endif
|
---|
144 |
|
---|
145 | #ifdef WOLFSSL_SMALL_STACK
|
---|
146 | des = (Des*)XMALLOC(sizeof(Des), NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
---|
147 | if (des == NULL)
|
---|
148 | return MEMORY_E;
|
---|
149 | #endif
|
---|
150 |
|
---|
151 | ret = wc_Des_SetKey(des, key, iv, DES_DECRYPTION);
|
---|
152 | if (ret == 0)
|
---|
153 | ret = wc_Des_CbcDecrypt(des, out, in, sz);
|
---|
154 |
|
---|
155 | #ifdef WOLFSSL_SMALL_STACK
|
---|
156 | XFREE(des, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
---|
157 | #endif
|
---|
158 |
|
---|
159 | return ret;
|
---|
160 | }
|
---|
161 |
|
---|
162 |
|
---|
163 | int wc_Des3_CbcEncryptWithKey(byte* out, const byte* in, word32 sz,
|
---|
164 | const byte* key, const byte* iv)
|
---|
165 | {
|
---|
166 | int ret = 0;
|
---|
167 | #ifdef WOLFSSL_SMALL_STACK
|
---|
168 | Des3* des3 = NULL;
|
---|
169 | #else
|
---|
170 | Des3 des3[1];
|
---|
171 | #endif
|
---|
172 |
|
---|
173 | #ifdef WOLFSSL_SMALL_STACK
|
---|
174 | des3 = (Des3*)XMALLOC(sizeof(Des3), NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
---|
175 | if (des3 == NULL)
|
---|
176 | return MEMORY_E;
|
---|
177 | #endif
|
---|
178 |
|
---|
179 | ret = wc_Des3Init(des3, NULL, INVALID_DEVID);
|
---|
180 | if (ret == 0) {
|
---|
181 | ret = wc_Des3_SetKey(des3, key, iv, DES_ENCRYPTION);
|
---|
182 | if (ret == 0)
|
---|
183 | ret = wc_Des3_CbcEncrypt(des3, out, in, sz);
|
---|
184 | wc_Des3Free(des3);
|
---|
185 | }
|
---|
186 |
|
---|
187 | #ifdef WOLFSSL_SMALL_STACK
|
---|
188 | XFREE(des3, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
---|
189 | #endif
|
---|
190 |
|
---|
191 | return ret;
|
---|
192 | }
|
---|
193 |
|
---|
194 |
|
---|
195 | int wc_Des3_CbcDecryptWithKey(byte* out, const byte* in, word32 sz,
|
---|
196 | const byte* key, const byte* iv)
|
---|
197 | {
|
---|
198 | int ret = 0;
|
---|
199 | #ifdef WOLFSSL_SMALL_STACK
|
---|
200 | Des3* des3 = NULL;
|
---|
201 | #else
|
---|
202 | Des3 des3[1];
|
---|
203 | #endif
|
---|
204 |
|
---|
205 | #ifdef WOLFSSL_SMALL_STACK
|
---|
206 | des3 = (Des3*)XMALLOC(sizeof(Des3), NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
---|
207 | if (des3 == NULL)
|
---|
208 | return MEMORY_E;
|
---|
209 | #endif
|
---|
210 |
|
---|
211 | ret = wc_Des3Init(des3, NULL, INVALID_DEVID);
|
---|
212 | if (ret == 0) {
|
---|
213 | ret = wc_Des3_SetKey(des3, key, iv, DES_DECRYPTION);
|
---|
214 | if (ret == 0)
|
---|
215 | ret = wc_Des3_CbcDecrypt(des3, out, in, sz);
|
---|
216 | wc_Des3Free(des3);
|
---|
217 | }
|
---|
218 |
|
---|
219 | #ifdef WOLFSSL_SMALL_STACK
|
---|
220 | XFREE(des3, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
---|
221 | #endif
|
---|
222 |
|
---|
223 | return ret;
|
---|
224 | }
|
---|
225 |
|
---|
226 | #endif /* !NO_DES3 */
|
---|