1 | /* des3.c
|
---|
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 |
|
---|
22 |
|
---|
23 | #ifdef HAVE_CONFIG_H
|
---|
24 | #include <config.h>
|
---|
25 | #endif
|
---|
26 |
|
---|
27 | #include <wolfssl/wolfcrypt/settings.h>
|
---|
28 | #include <wolfssl/wolfcrypt/error-crypt.h>
|
---|
29 | #include <wolfssl/wolfcrypt/logging.h>
|
---|
30 |
|
---|
31 |
|
---|
32 | #ifndef NO_DES3
|
---|
33 |
|
---|
34 | #if defined(HAVE_FIPS) && \
|
---|
35 | defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
|
---|
36 |
|
---|
37 | /* set NO_WRAPPERS before headers, use direct internal f()s not wrappers */
|
---|
38 | #define FIPS_NO_WRAPPERS
|
---|
39 |
|
---|
40 | #ifdef USE_WINDOWS_API
|
---|
41 | #pragma code_seg(".fipsA$i")
|
---|
42 | #pragma const_seg(".fipsB$i")
|
---|
43 | #endif
|
---|
44 | #endif
|
---|
45 |
|
---|
46 | #include <wolfssl/wolfcrypt/des3.h>
|
---|
47 |
|
---|
48 | #ifdef WOLF_CRYPTO_CB
|
---|
49 | #include <wolfssl/wolfcrypt/cryptocb.h>
|
---|
50 | #endif
|
---|
51 |
|
---|
52 | /* fips wrapper calls, user can call direct */
|
---|
53 | #if defined(HAVE_FIPS) && \
|
---|
54 | (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2))
|
---|
55 |
|
---|
56 | int wc_Des_SetKey(Des* des, const byte* key, const byte* iv, int dir)
|
---|
57 | {
|
---|
58 | return Des_SetKey(des, key, iv, dir);
|
---|
59 | }
|
---|
60 | int wc_Des3_SetKey(Des3* des, const byte* key, const byte* iv, int dir)
|
---|
61 | {
|
---|
62 | if (des == NULL || key == NULL || dir < 0) {
|
---|
63 | return BAD_FUNC_ARG;
|
---|
64 | }
|
---|
65 |
|
---|
66 | return Des3_SetKey_fips(des, key, iv, dir);
|
---|
67 | }
|
---|
68 | int wc_Des_CbcEncrypt(Des* des, byte* out, const byte* in, word32 sz)
|
---|
69 | {
|
---|
70 | return Des_CbcEncrypt(des, out, in, sz);
|
---|
71 | }
|
---|
72 | int wc_Des_CbcDecrypt(Des* des, byte* out, const byte* in, word32 sz)
|
---|
73 | {
|
---|
74 | return Des_CbcDecrypt(des, out, in, sz);
|
---|
75 | }
|
---|
76 | int wc_Des3_CbcEncrypt(Des3* des, byte* out, const byte* in, word32 sz)
|
---|
77 | {
|
---|
78 | if (des == NULL || out == NULL || in == NULL) {
|
---|
79 | return BAD_FUNC_ARG;
|
---|
80 | }
|
---|
81 | return Des3_CbcEncrypt_fips(des, out, in, sz);
|
---|
82 | }
|
---|
83 | int wc_Des3_CbcDecrypt(Des3* des, byte* out, const byte* in, word32 sz)
|
---|
84 | {
|
---|
85 | if (des == NULL || out == NULL || in == NULL) {
|
---|
86 | return BAD_FUNC_ARG;
|
---|
87 | }
|
---|
88 | return Des3_CbcDecrypt_fips(des, out, in, sz);
|
---|
89 | }
|
---|
90 |
|
---|
91 | #ifdef WOLFSSL_DES_ECB
|
---|
92 | /* One block, compatibility only */
|
---|
93 | int wc_Des_EcbEncrypt(Des* des, byte* out, const byte* in, word32 sz)
|
---|
94 | {
|
---|
95 | return Des_EcbEncrypt(des, out, in, sz);
|
---|
96 | }
|
---|
97 | int wc_Des3_EcbEncrypt(Des3* des, byte* out, const byte* in, word32 sz)
|
---|
98 | {
|
---|
99 | return Des3_EcbEncrypt(des, out, in, sz);
|
---|
100 | }
|
---|
101 | #endif /* WOLFSSL_DES_ECB */
|
---|
102 |
|
---|
103 | void wc_Des_SetIV(Des* des, const byte* iv)
|
---|
104 | {
|
---|
105 | Des_SetIV(des, iv);
|
---|
106 | }
|
---|
107 | int wc_Des3_SetIV(Des3* des, const byte* iv)
|
---|
108 | {
|
---|
109 | return Des3_SetIV_fips(des, iv);
|
---|
110 | }
|
---|
111 |
|
---|
112 | int wc_Des3Init(Des3* des3, void* heap, int devId)
|
---|
113 | {
|
---|
114 | (void)des3;
|
---|
115 | (void)heap;
|
---|
116 | (void)devId;
|
---|
117 | /* FIPS doesn't support:
|
---|
118 | return Des3Init(des3, heap, devId); */
|
---|
119 | return 0;
|
---|
120 | }
|
---|
121 | void wc_Des3Free(Des3* des3)
|
---|
122 | {
|
---|
123 | (void)des3;
|
---|
124 | /* FIPS doesn't support:
|
---|
125 | Des3Free(des3); */
|
---|
126 | }
|
---|
127 |
|
---|
128 | #else /* else build without fips, or for FIPS v2 */
|
---|
129 |
|
---|
130 |
|
---|
131 | #if defined(WOLFSSL_TI_CRYPT)
|
---|
132 | #include <wolfcrypt/src/port/ti/ti-des3.c>
|
---|
133 | #else
|
---|
134 |
|
---|
135 |
|
---|
136 | #ifdef NO_INLINE
|
---|
137 | #include <wolfssl/wolfcrypt/misc.h>
|
---|
138 | #else
|
---|
139 | #define WOLFSSL_MISC_INCLUDED
|
---|
140 | #include <wolfcrypt/src/misc.c>
|
---|
141 | #endif
|
---|
142 |
|
---|
143 |
|
---|
144 | /* Hardware Acceleration */
|
---|
145 | #if defined(STM32_CRYPTO)
|
---|
146 |
|
---|
147 | /*
|
---|
148 | * STM32F2/F4 hardware DES/3DES support through the standard
|
---|
149 | * peripheral library. (See note in README).
|
---|
150 | */
|
---|
151 |
|
---|
152 | int wc_Des_SetKey(Des* des, const byte* key, const byte* iv, int dir)
|
---|
153 | {
|
---|
154 | word32 *dkey = des->key;
|
---|
155 |
|
---|
156 | (void)dir;
|
---|
157 |
|
---|
158 | XMEMCPY(dkey, key, 8);
|
---|
159 | #ifndef WOLFSSL_STM32_CUBEMX
|
---|
160 | ByteReverseWords(dkey, dkey, 8);
|
---|
161 | #endif
|
---|
162 |
|
---|
163 | wc_Des_SetIV(des, iv);
|
---|
164 |
|
---|
165 | return 0;
|
---|
166 | }
|
---|
167 |
|
---|
168 | int wc_Des3_SetKey(Des3* des, const byte* key, const byte* iv, int dir)
|
---|
169 | {
|
---|
170 | if (des == NULL || key == NULL)
|
---|
171 | return BAD_FUNC_ARG;
|
---|
172 |
|
---|
173 | (void)dir;
|
---|
174 |
|
---|
175 | #ifndef WOLFSSL_STM32_CUBEMX
|
---|
176 | {
|
---|
177 | word32 *dkey1 = des->key[0];
|
---|
178 | word32 *dkey2 = des->key[1];
|
---|
179 | word32 *dkey3 = des->key[2];
|
---|
180 |
|
---|
181 | XMEMCPY(dkey1, key, 8); /* set key 1 */
|
---|
182 | XMEMCPY(dkey2, key + 8, 8); /* set key 2 */
|
---|
183 | XMEMCPY(dkey3, key + 16, 8); /* set key 3 */
|
---|
184 |
|
---|
185 | ByteReverseWords(dkey1, dkey1, 8);
|
---|
186 | ByteReverseWords(dkey2, dkey2, 8);
|
---|
187 | ByteReverseWords(dkey3, dkey3, 8);
|
---|
188 | }
|
---|
189 | #else
|
---|
190 | XMEMCPY(des->key[0], key, DES3_KEYLEN); /* CUBEMX wants keys in sequential memory */
|
---|
191 | #endif
|
---|
192 |
|
---|
193 | return wc_Des3_SetIV(des, iv);
|
---|
194 | }
|
---|
195 |
|
---|
196 | static void DesCrypt(Des* des, byte* out, const byte* in, word32 sz,
|
---|
197 | int dir, int mode)
|
---|
198 | {
|
---|
199 | int ret;
|
---|
200 | #ifdef WOLFSSL_STM32_CUBEMX
|
---|
201 | CRYP_HandleTypeDef hcryp;
|
---|
202 | #else
|
---|
203 | word32 *dkey, *iv;
|
---|
204 | CRYP_InitTypeDef DES_CRYP_InitStructure;
|
---|
205 | CRYP_KeyInitTypeDef DES_CRYP_KeyInitStructure;
|
---|
206 | CRYP_IVInitTypeDef DES_CRYP_IVInitStructure;
|
---|
207 | #endif
|
---|
208 |
|
---|
209 | ret = wolfSSL_CryptHwMutexLock();
|
---|
210 | if (ret != 0) {
|
---|
211 | return;
|
---|
212 | }
|
---|
213 |
|
---|
214 | #ifdef WOLFSSL_STM32_CUBEMX
|
---|
215 | XMEMSET(&hcryp, 0, sizeof(CRYP_HandleTypeDef));
|
---|
216 | hcryp.Instance = CRYP;
|
---|
217 | hcryp.Init.KeySize = CRYP_KEYSIZE_128B;
|
---|
218 | hcryp.Init.DataType = CRYP_DATATYPE_8B;
|
---|
219 | hcryp.Init.pKey = (uint8_t*)des->key;
|
---|
220 | hcryp.Init.pInitVect = (uint8_t*)des->reg;
|
---|
221 |
|
---|
222 | HAL_CRYP_Init(&hcryp);
|
---|
223 |
|
---|
224 | while (sz > 0) {
|
---|
225 | /* if input and output same will overwrite input iv */
|
---|
226 | XMEMCPY(des->tmp, in + sz - DES_BLOCK_SIZE, DES_BLOCK_SIZE);
|
---|
227 |
|
---|
228 | if (mode == DES_CBC) {
|
---|
229 | if (dir == DES_ENCRYPTION) {
|
---|
230 | HAL_CRYP_DESCBC_Encrypt(&hcryp, (uint8_t*)in,
|
---|
231 | DES_BLOCK_SIZE, out, STM32_HAL_TIMEOUT);
|
---|
232 | }
|
---|
233 | else {
|
---|
234 | HAL_CRYP_DESCBC_Decrypt(&hcryp, (uint8_t*)in,
|
---|
235 | DES_BLOCK_SIZE, out, STM32_HAL_TIMEOUT);
|
---|
236 | }
|
---|
237 | }
|
---|
238 | else {
|
---|
239 | if (dir == DES_ENCRYPTION) {
|
---|
240 | HAL_CRYP_DESECB_Encrypt(&hcryp, (uint8_t*)in,
|
---|
241 | DES_BLOCK_SIZE, out, STM32_HAL_TIMEOUT);
|
---|
242 | }
|
---|
243 | else {
|
---|
244 | HAL_CRYP_DESECB_Decrypt(&hcryp, (uint8_t*)in,
|
---|
245 | DES_BLOCK_SIZE, out, STM32_HAL_TIMEOUT);
|
---|
246 | }
|
---|
247 | }
|
---|
248 |
|
---|
249 | /* store iv for next call */
|
---|
250 | XMEMCPY(des->reg, des->tmp, DES_BLOCK_SIZE);
|
---|
251 |
|
---|
252 | sz -= DES_BLOCK_SIZE;
|
---|
253 | in += DES_BLOCK_SIZE;
|
---|
254 | out += DES_BLOCK_SIZE;
|
---|
255 | }
|
---|
256 |
|
---|
257 | HAL_CRYP_DeInit(&hcryp);
|
---|
258 | #else
|
---|
259 | dkey = des->key;
|
---|
260 | iv = des->reg;
|
---|
261 |
|
---|
262 | /* crypto structure initialization */
|
---|
263 | CRYP_KeyStructInit(&DES_CRYP_KeyInitStructure);
|
---|
264 | CRYP_StructInit(&DES_CRYP_InitStructure);
|
---|
265 | CRYP_IVStructInit(&DES_CRYP_IVInitStructure);
|
---|
266 |
|
---|
267 | /* reset registers to their default values */
|
---|
268 | CRYP_DeInit();
|
---|
269 |
|
---|
270 | /* set direction, mode, and datatype */
|
---|
271 | if (dir == DES_ENCRYPTION) {
|
---|
272 | DES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Encrypt;
|
---|
273 | } else { /* DES_DECRYPTION */
|
---|
274 | DES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Decrypt;
|
---|
275 | }
|
---|
276 |
|
---|
277 | if (mode == DES_CBC) {
|
---|
278 | DES_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_DES_CBC;
|
---|
279 | } else { /* DES_ECB */
|
---|
280 | DES_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_DES_ECB;
|
---|
281 | }
|
---|
282 |
|
---|
283 | DES_CRYP_InitStructure.CRYP_DataType = CRYP_DataType_8b;
|
---|
284 | CRYP_Init(&DES_CRYP_InitStructure);
|
---|
285 |
|
---|
286 | /* load key into correct registers */
|
---|
287 | DES_CRYP_KeyInitStructure.CRYP_Key1Left = dkey[0];
|
---|
288 | DES_CRYP_KeyInitStructure.CRYP_Key1Right = dkey[1];
|
---|
289 | CRYP_KeyInit(&DES_CRYP_KeyInitStructure);
|
---|
290 |
|
---|
291 | /* set iv */
|
---|
292 | ByteReverseWords(iv, iv, DES_BLOCK_SIZE);
|
---|
293 | DES_CRYP_IVInitStructure.CRYP_IV0Left = iv[0];
|
---|
294 | DES_CRYP_IVInitStructure.CRYP_IV0Right = iv[1];
|
---|
295 | CRYP_IVInit(&DES_CRYP_IVInitStructure);
|
---|
296 |
|
---|
297 | /* enable crypto processor */
|
---|
298 | CRYP_Cmd(ENABLE);
|
---|
299 |
|
---|
300 | while (sz > 0) {
|
---|
301 | /* flush IN/OUT FIFOs */
|
---|
302 | CRYP_FIFOFlush();
|
---|
303 |
|
---|
304 | /* if input and output same will overwrite input iv */
|
---|
305 | XMEMCPY(des->tmp, in + sz - DES_BLOCK_SIZE, DES_BLOCK_SIZE);
|
---|
306 |
|
---|
307 | CRYP_DataIn(*(uint32_t*)&in[0]);
|
---|
308 | CRYP_DataIn(*(uint32_t*)&in[4]);
|
---|
309 |
|
---|
310 | /* wait until the complete message has been processed */
|
---|
311 | while(CRYP_GetFlagStatus(CRYP_FLAG_BUSY) != RESET) {}
|
---|
312 |
|
---|
313 | *(uint32_t*)&out[0] = CRYP_DataOut();
|
---|
314 | *(uint32_t*)&out[4] = CRYP_DataOut();
|
---|
315 |
|
---|
316 | /* store iv for next call */
|
---|
317 | XMEMCPY(des->reg, des->tmp, DES_BLOCK_SIZE);
|
---|
318 |
|
---|
319 | sz -= DES_BLOCK_SIZE;
|
---|
320 | in += DES_BLOCK_SIZE;
|
---|
321 | out += DES_BLOCK_SIZE;
|
---|
322 | }
|
---|
323 |
|
---|
324 | /* disable crypto processor */
|
---|
325 | CRYP_Cmd(DISABLE);
|
---|
326 | #endif /* WOLFSSL_STM32_CUBEMX */
|
---|
327 | wolfSSL_CryptHwMutexUnLock();
|
---|
328 | }
|
---|
329 |
|
---|
330 | int wc_Des_CbcEncrypt(Des* des, byte* out, const byte* in, word32 sz)
|
---|
331 | {
|
---|
332 | DesCrypt(des, out, in, sz, DES_ENCRYPTION, DES_CBC);
|
---|
333 | return 0;
|
---|
334 | }
|
---|
335 |
|
---|
336 | int wc_Des_CbcDecrypt(Des* des, byte* out, const byte* in, word32 sz)
|
---|
337 | {
|
---|
338 | DesCrypt(des, out, in, sz, DES_DECRYPTION, DES_CBC);
|
---|
339 | return 0;
|
---|
340 | }
|
---|
341 |
|
---|
342 | int wc_Des_EcbEncrypt(Des* des, byte* out, const byte* in, word32 sz)
|
---|
343 | {
|
---|
344 | DesCrypt(des, out, in, sz, DES_ENCRYPTION, DES_ECB);
|
---|
345 | return 0;
|
---|
346 | }
|
---|
347 |
|
---|
348 | static int Des3Crypt(Des3* des, byte* out, const byte* in, word32 sz,
|
---|
349 | int dir)
|
---|
350 | {
|
---|
351 | if (des == NULL || out == NULL || in == NULL)
|
---|
352 | return BAD_FUNC_ARG;
|
---|
353 |
|
---|
354 | #ifdef WOLFSSL_STM32_CUBEMX
|
---|
355 | {
|
---|
356 | CRYP_HandleTypeDef hcryp;
|
---|
357 |
|
---|
358 | XMEMSET(&hcryp, 0, sizeof(CRYP_HandleTypeDef));
|
---|
359 | hcryp.Instance = CRYP;
|
---|
360 | hcryp.Init.KeySize = CRYP_KEYSIZE_128B;
|
---|
361 | hcryp.Init.DataType = CRYP_DATATYPE_8B;
|
---|
362 | hcryp.Init.pKey = (uint8_t*)des->key;
|
---|
363 | hcryp.Init.pInitVect = (uint8_t*)des->reg;
|
---|
364 |
|
---|
365 | HAL_CRYP_Init(&hcryp);
|
---|
366 |
|
---|
367 | while (sz > 0)
|
---|
368 | {
|
---|
369 | if (dir == DES_ENCRYPTION) {
|
---|
370 | HAL_CRYP_TDESCBC_Encrypt(&hcryp, (byte*)in,
|
---|
371 | DES_BLOCK_SIZE, out, STM32_HAL_TIMEOUT);
|
---|
372 | }
|
---|
373 | else {
|
---|
374 | HAL_CRYP_TDESCBC_Decrypt(&hcryp, (byte*)in,
|
---|
375 | DES_BLOCK_SIZE, out, STM32_HAL_TIMEOUT);
|
---|
376 | }
|
---|
377 |
|
---|
378 | /* store iv for next call */
|
---|
379 | XMEMCPY(des->reg, out + sz - DES_BLOCK_SIZE, DES_BLOCK_SIZE);
|
---|
380 |
|
---|
381 | sz -= DES_BLOCK_SIZE;
|
---|
382 | in += DES_BLOCK_SIZE;
|
---|
383 | out += DES_BLOCK_SIZE;
|
---|
384 | }
|
---|
385 |
|
---|
386 | HAL_CRYP_DeInit(&hcryp);
|
---|
387 | }
|
---|
388 | #else
|
---|
389 | {
|
---|
390 | word32 *dkey1, *dkey2, *dkey3, *iv;
|
---|
391 | CRYP_InitTypeDef DES3_CRYP_InitStructure;
|
---|
392 | CRYP_KeyInitTypeDef DES3_CRYP_KeyInitStructure;
|
---|
393 | CRYP_IVInitTypeDef DES3_CRYP_IVInitStructure;
|
---|
394 |
|
---|
395 | dkey1 = des->key[0];
|
---|
396 | dkey2 = des->key[1];
|
---|
397 | dkey3 = des->key[2];
|
---|
398 | iv = des->reg;
|
---|
399 |
|
---|
400 | /* crypto structure initialization */
|
---|
401 | CRYP_KeyStructInit(&DES3_CRYP_KeyInitStructure);
|
---|
402 | CRYP_StructInit(&DES3_CRYP_InitStructure);
|
---|
403 | CRYP_IVStructInit(&DES3_CRYP_IVInitStructure);
|
---|
404 |
|
---|
405 | /* reset registers to their default values */
|
---|
406 | CRYP_DeInit();
|
---|
407 |
|
---|
408 | /* set direction, mode, and datatype */
|
---|
409 | if (dir == DES_ENCRYPTION) {
|
---|
410 | DES3_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Encrypt;
|
---|
411 | } else {
|
---|
412 | DES3_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Decrypt;
|
---|
413 | }
|
---|
414 |
|
---|
415 | DES3_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_TDES_CBC;
|
---|
416 | DES3_CRYP_InitStructure.CRYP_DataType = CRYP_DataType_8b;
|
---|
417 | CRYP_Init(&DES3_CRYP_InitStructure);
|
---|
418 |
|
---|
419 | /* load key into correct registers */
|
---|
420 | DES3_CRYP_KeyInitStructure.CRYP_Key1Left = dkey1[0];
|
---|
421 | DES3_CRYP_KeyInitStructure.CRYP_Key1Right = dkey1[1];
|
---|
422 | DES3_CRYP_KeyInitStructure.CRYP_Key2Left = dkey2[0];
|
---|
423 | DES3_CRYP_KeyInitStructure.CRYP_Key2Right = dkey2[1];
|
---|
424 | DES3_CRYP_KeyInitStructure.CRYP_Key3Left = dkey3[0];
|
---|
425 | DES3_CRYP_KeyInitStructure.CRYP_Key3Right = dkey3[1];
|
---|
426 | CRYP_KeyInit(&DES3_CRYP_KeyInitStructure);
|
---|
427 |
|
---|
428 | /* set iv */
|
---|
429 | ByteReverseWords(iv, iv, DES_BLOCK_SIZE);
|
---|
430 | DES3_CRYP_IVInitStructure.CRYP_IV0Left = iv[0];
|
---|
431 | DES3_CRYP_IVInitStructure.CRYP_IV0Right = iv[1];
|
---|
432 | CRYP_IVInit(&DES3_CRYP_IVInitStructure);
|
---|
433 |
|
---|
434 | /* enable crypto processor */
|
---|
435 | CRYP_Cmd(ENABLE);
|
---|
436 |
|
---|
437 | while (sz > 0)
|
---|
438 | {
|
---|
439 | /* flush IN/OUT FIFOs */
|
---|
440 | CRYP_FIFOFlush();
|
---|
441 |
|
---|
442 | CRYP_DataIn(*(uint32_t*)&in[0]);
|
---|
443 | CRYP_DataIn(*(uint32_t*)&in[4]);
|
---|
444 |
|
---|
445 | /* wait until the complete message has been processed */
|
---|
446 | while(CRYP_GetFlagStatus(CRYP_FLAG_BUSY) != RESET) {}
|
---|
447 |
|
---|
448 | *(uint32_t*)&out[0] = CRYP_DataOut();
|
---|
449 | *(uint32_t*)&out[4] = CRYP_DataOut();
|
---|
450 |
|
---|
451 | /* store iv for next call */
|
---|
452 | XMEMCPY(des->reg, out + sz - DES_BLOCK_SIZE, DES_BLOCK_SIZE);
|
---|
453 |
|
---|
454 | sz -= DES_BLOCK_SIZE;
|
---|
455 | in += DES_BLOCK_SIZE;
|
---|
456 | out += DES_BLOCK_SIZE;
|
---|
457 | }
|
---|
458 |
|
---|
459 | /* disable crypto processor */
|
---|
460 | CRYP_Cmd(DISABLE);
|
---|
461 | }
|
---|
462 | #endif /* WOLFSSL_STM32_CUBEMX */
|
---|
463 | return 0;
|
---|
464 | }
|
---|
465 |
|
---|
466 | int wc_Des3_CbcEncrypt(Des3* des, byte* out, const byte* in, word32 sz)
|
---|
467 | {
|
---|
468 | return Des3Crypt(des, out, in, sz, DES_ENCRYPTION);
|
---|
469 | }
|
---|
470 |
|
---|
471 | int wc_Des3_CbcDecrypt(Des3* des, byte* out, const byte* in, word32 sz)
|
---|
472 | {
|
---|
473 | return Des3Crypt(des, out, in, sz, DES_DECRYPTION);
|
---|
474 | }
|
---|
475 |
|
---|
476 | #elif defined(HAVE_COLDFIRE_SEC)
|
---|
477 |
|
---|
478 | #include <wolfssl/ctaocrypt/types.h>
|
---|
479 |
|
---|
480 | #include "sec.h"
|
---|
481 | #include "mcf5475_sec.h"
|
---|
482 | #include "mcf5475_siu.h"
|
---|
483 |
|
---|
484 | #if defined (HAVE_THREADX)
|
---|
485 | #include "memory_pools.h"
|
---|
486 | extern TX_BYTE_POOL mp_ncached; /* Non Cached memory pool */
|
---|
487 | #endif
|
---|
488 |
|
---|
489 | #define DES_BUFFER_SIZE (DES_BLOCK_SIZE * 64)
|
---|
490 | static unsigned char *desBuffIn = NULL;
|
---|
491 | static unsigned char *desBuffOut = NULL;
|
---|
492 | static byte *secIV;
|
---|
493 | static byte *secKey;
|
---|
494 | static volatile SECdescriptorType *secDesc;
|
---|
495 |
|
---|
496 | static wolfSSL_Mutex Mutex_DesSEC;
|
---|
497 |
|
---|
498 | #define SEC_DESC_DES_CBC_ENCRYPT 0x20500010
|
---|
499 | #define SEC_DESC_DES_CBC_DECRYPT 0x20400010
|
---|
500 | #define SEC_DESC_DES3_CBC_ENCRYPT 0x20700010
|
---|
501 | #define SEC_DESC_DES3_CBC_DECRYPT 0x20600010
|
---|
502 |
|
---|
503 | #define DES_IVLEN 8
|
---|
504 | #define DES_KEYLEN 8
|
---|
505 | #define DES3_IVLEN 8
|
---|
506 | #define DES3_KEYLEN 24
|
---|
507 |
|
---|
508 | extern volatile unsigned char __MBAR[];
|
---|
509 |
|
---|
510 | static void wc_Des_Cbc(byte* out, const byte* in, word32 sz,
|
---|
511 | byte *key, byte *iv, word32 desc)
|
---|
512 | {
|
---|
513 | #ifdef DEBUG_WOLFSSL
|
---|
514 | int ret; int stat1,stat2;
|
---|
515 | #endif
|
---|
516 | int size;
|
---|
517 | volatile int v;
|
---|
518 |
|
---|
519 | wc_LockMutex(&Mutex_DesSEC) ;
|
---|
520 |
|
---|
521 | secDesc->length1 = 0x0;
|
---|
522 | secDesc->pointer1 = NULL;
|
---|
523 | if((desc==SEC_DESC_DES_CBC_ENCRYPT)||(desc==SEC_DESC_DES_CBC_DECRYPT)){
|
---|
524 | secDesc->length2 = DES_IVLEN;
|
---|
525 | secDesc->length3 = DES_KEYLEN;
|
---|
526 | } else {
|
---|
527 | secDesc->length2 = DES3_IVLEN;
|
---|
528 | secDesc->length3 = DES3_KEYLEN;
|
---|
529 | }
|
---|
530 | secDesc->pointer2 = secIV;
|
---|
531 | secDesc->pointer3 = secKey;
|
---|
532 | secDesc->pointer4 = desBuffIn;
|
---|
533 | secDesc->pointer5 = desBuffOut;
|
---|
534 | secDesc->length6 = 0;
|
---|
535 | secDesc->pointer6 = NULL;
|
---|
536 | secDesc->length7 = 0x0;
|
---|
537 | secDesc->pointer7 = NULL;
|
---|
538 | secDesc->nextDescriptorPtr = NULL;
|
---|
539 |
|
---|
540 | while(sz) {
|
---|
541 | XMEMCPY(secIV, iv, secDesc->length2);
|
---|
542 | if((sz%DES_BUFFER_SIZE) == sz) {
|
---|
543 | size = sz;
|
---|
544 | sz = 0;
|
---|
545 | } else {
|
---|
546 | size = DES_BUFFER_SIZE;
|
---|
547 | sz -= DES_BUFFER_SIZE;
|
---|
548 | }
|
---|
549 |
|
---|
550 | XMEMCPY(desBuffIn, in, size);
|
---|
551 | XMEMCPY(secKey, key, secDesc->length3);
|
---|
552 |
|
---|
553 | secDesc->header = desc;
|
---|
554 | secDesc->length4 = size;
|
---|
555 | secDesc->length5 = size;
|
---|
556 | /* Point SEC to the location of the descriptor */
|
---|
557 | MCF_SEC_FR0 = (uint32)secDesc;
|
---|
558 | /* Initialize SEC and wait for encryption to complete */
|
---|
559 | MCF_SEC_CCCR0 = 0x0000001a;
|
---|
560 | /* poll SISR to determine when channel is complete */
|
---|
561 | v=0;
|
---|
562 | while((secDesc->header>> 24) != 0xff) {
|
---|
563 | if(v++ > 1000)break;
|
---|
564 | }
|
---|
565 |
|
---|
566 | #ifdef DEBUG_WOLFSSL
|
---|
567 | ret = MCF_SEC_SISRH;
|
---|
568 | stat1 = MCF_SEC_DSR;
|
---|
569 | stat2 = MCF_SEC_DISR;
|
---|
570 | if(ret & 0xe0000000) {
|
---|
571 | /* db_printf("Des_Cbc(%x):ISRH=%08x, DSR=%08x, DISR=%08x\n", desc, ret, stat1, stat2); */
|
---|
572 | }
|
---|
573 | #endif
|
---|
574 |
|
---|
575 | XMEMCPY(out, desBuffOut, size);
|
---|
576 |
|
---|
577 | if ((desc==SEC_DESC_DES3_CBC_ENCRYPT)||(desc==SEC_DESC_DES_CBC_ENCRYPT)) {
|
---|
578 | XMEMCPY((void*)iv, (void*)&(out[size-secDesc->length2]), secDesc->length2);
|
---|
579 | } else {
|
---|
580 | XMEMCPY((void*)iv, (void*)&(in[size-secDesc->length2]), secDesc->length2);
|
---|
581 | }
|
---|
582 |
|
---|
583 | in += size;
|
---|
584 | out += size;
|
---|
585 |
|
---|
586 | }
|
---|
587 | wc_UnLockMutex(&Mutex_DesSEC) ;
|
---|
588 |
|
---|
589 | }
|
---|
590 |
|
---|
591 |
|
---|
592 | int wc_Des_CbcEncrypt(Des* des, byte* out, const byte* in, word32 sz)
|
---|
593 | {
|
---|
594 | wc_Des_Cbc(out, in, sz, (byte *)des->key, (byte *)des->reg, SEC_DESC_DES_CBC_ENCRYPT);
|
---|
595 | return 0;
|
---|
596 | }
|
---|
597 |
|
---|
598 | int wc_Des_CbcDecrypt(Des* des, byte* out, const byte* in, word32 sz)
|
---|
599 | {
|
---|
600 | wc_Des_Cbc(out, in, sz, (byte *)des->key, (byte *)des->reg, SEC_DESC_DES_CBC_DECRYPT);
|
---|
601 | return 0;
|
---|
602 | }
|
---|
603 |
|
---|
604 | int wc_Des3_CbcEncrypt(Des3* des3, byte* out, const byte* in, word32 sz)
|
---|
605 | {
|
---|
606 | wc_Des_Cbc(out, in, sz, (byte *)des3->key, (byte *)des3->reg, SEC_DESC_DES3_CBC_ENCRYPT);
|
---|
607 | return 0;
|
---|
608 | }
|
---|
609 |
|
---|
610 |
|
---|
611 | int wc_Des3_CbcDecrypt(Des3* des3, byte* out, const byte* in, word32 sz)
|
---|
612 | {
|
---|
613 | wc_Des_Cbc(out, in, sz, (byte *)des3->key, (byte *)des3->reg, SEC_DESC_DES3_CBC_DECRYPT);
|
---|
614 | return 0;
|
---|
615 | }
|
---|
616 |
|
---|
617 | static void setParity(byte *buf, int len)
|
---|
618 | {
|
---|
619 | int i, j;
|
---|
620 | byte v;
|
---|
621 | int bits;
|
---|
622 |
|
---|
623 | for (i=0; i<len; i++) {
|
---|
624 | v = buf[i] >> 1;
|
---|
625 | buf[i] = v << 1;
|
---|
626 | bits = 0;
|
---|
627 | for (j=0; j<7; j++) {
|
---|
628 | bits += (v&0x1);
|
---|
629 | v = v >> 1;
|
---|
630 | }
|
---|
631 | buf[i] |= (1 - (bits&0x1));
|
---|
632 | }
|
---|
633 |
|
---|
634 | }
|
---|
635 |
|
---|
636 | int wc_Des_SetKey(Des* des, const byte* key, const byte* iv, int dir)
|
---|
637 | {
|
---|
638 | if(desBuffIn == NULL) {
|
---|
639 | #if defined (HAVE_THREADX)
|
---|
640 | int s1, s2, s3, s4, s5;
|
---|
641 | s5 = tx_byte_allocate(&mp_ncached,(void *)&secDesc,
|
---|
642 | sizeof(SECdescriptorType), TX_NO_WAIT);
|
---|
643 | s1 = tx_byte_allocate(&mp_ncached,(void *)&desBuffIn, DES_BUFFER_SIZE, TX_NO_WAIT);
|
---|
644 | s2 = tx_byte_allocate(&mp_ncached,(void *)&desBuffOut, DES_BUFFER_SIZE, TX_NO_WAIT);
|
---|
645 | /* Don't know des or des3 to be used. Allocate larger buffers */
|
---|
646 | s3 = tx_byte_allocate(&mp_ncached,(void *)&secKey, DES3_KEYLEN,TX_NO_WAIT);
|
---|
647 | s4 = tx_byte_allocate(&mp_ncached,(void *)&secIV, DES3_IVLEN, TX_NO_WAIT);
|
---|
648 | #else
|
---|
649 | #warning "Allocate non-Cache buffers"
|
---|
650 | #endif
|
---|
651 |
|
---|
652 | InitMutex(&Mutex_DesSEC);
|
---|
653 | }
|
---|
654 |
|
---|
655 | XMEMCPY(des->key, key, DES_KEYLEN);
|
---|
656 | setParity((byte *)des->key, DES_KEYLEN);
|
---|
657 |
|
---|
658 | if (iv) {
|
---|
659 | XMEMCPY(des->reg, iv, DES_IVLEN);
|
---|
660 | } else {
|
---|
661 | XMEMSET(des->reg, 0x0, DES_IVLEN);
|
---|
662 | }
|
---|
663 | return 0;
|
---|
664 | }
|
---|
665 |
|
---|
666 | int wc_Des3_SetKey(Des3* des3, const byte* key, const byte* iv, int dir)
|
---|
667 | {
|
---|
668 | if (des3 == NULL || key == NULL) {
|
---|
669 | return BAD_FUNC_ARG;
|
---|
670 | }
|
---|
671 |
|
---|
672 | if (desBuffIn == NULL) {
|
---|
673 | #if defined (HAVE_THREADX)
|
---|
674 | int s1, s2, s3, s4, s5;
|
---|
675 | s5 = tx_byte_allocate(&mp_ncached,(void *)&secDesc,
|
---|
676 | sizeof(SECdescriptorType), TX_NO_WAIT);
|
---|
677 | s1 = tx_byte_allocate(&mp_ncached,(void *)&desBuffIn, DES_BUFFER_SIZE, TX_NO_WAIT);
|
---|
678 | s2 = tx_byte_allocate(&mp_ncached,(void *)&desBuffOut, DES_BUFFER_SIZE, TX_NO_WAIT);
|
---|
679 | s3 = tx_byte_allocate(&mp_ncached,(void *)&secKey, DES3_KEYLEN,TX_NO_WAIT);
|
---|
680 | s4 = tx_byte_allocate(&mp_ncached,(void *)&secIV, DES3_IVLEN, TX_NO_WAIT);
|
---|
681 | #else
|
---|
682 | #warning "Allocate non-Cache buffers"
|
---|
683 | #endif
|
---|
684 |
|
---|
685 | InitMutex(&Mutex_DesSEC);
|
---|
686 | }
|
---|
687 |
|
---|
688 | XMEMCPY(des3->key[0], key, DES3_KEYLEN);
|
---|
689 | setParity((byte *)des3->key[0], DES3_KEYLEN);
|
---|
690 |
|
---|
691 | if (iv) {
|
---|
692 | XMEMCPY(des3->reg, iv, DES3_IVLEN);
|
---|
693 | } else {
|
---|
694 | XMEMSET(des3->reg, 0x0, DES3_IVLEN);
|
---|
695 | }
|
---|
696 | return 0;
|
---|
697 |
|
---|
698 | }
|
---|
699 | #elif defined(FREESCALE_LTC_DES)
|
---|
700 |
|
---|
701 | #include "fsl_ltc.h"
|
---|
702 | int wc_Des_SetKey(Des* des, const byte* key, const byte* iv, int dir)
|
---|
703 | {
|
---|
704 | byte* dkey;
|
---|
705 |
|
---|
706 | if (des == NULL || key == NULL) {
|
---|
707 | return BAD_FUNC_ARG;
|
---|
708 | }
|
---|
709 |
|
---|
710 | dkey = (byte*)des->key;
|
---|
711 |
|
---|
712 | XMEMCPY(dkey, key, 8);
|
---|
713 |
|
---|
714 | wc_Des_SetIV(des, iv);
|
---|
715 |
|
---|
716 | return 0;
|
---|
717 | }
|
---|
718 |
|
---|
719 | int wc_Des3_SetKey(Des3* des, const byte* key, const byte* iv, int dir)
|
---|
720 | {
|
---|
721 | int ret = 0;
|
---|
722 | byte* dkey1 = (byte*)des->key[0];
|
---|
723 | byte* dkey2 = (byte*)des->key[1];
|
---|
724 | byte* dkey3 = (byte*)des->key[2];
|
---|
725 |
|
---|
726 | XMEMCPY(dkey1, key, 8); /* set key 1 */
|
---|
727 | XMEMCPY(dkey2, key + 8, 8); /* set key 2 */
|
---|
728 | XMEMCPY(dkey3, key + 16, 8); /* set key 3 */
|
---|
729 |
|
---|
730 | ret = wc_Des3_SetIV(des, iv);
|
---|
731 | if (ret != 0)
|
---|
732 | return ret;
|
---|
733 |
|
---|
734 | return ret;
|
---|
735 | }
|
---|
736 |
|
---|
737 | int wc_Des_CbcEncrypt(Des* des, byte* out, const byte* in, word32 sz)
|
---|
738 | {
|
---|
739 | status_t status;
|
---|
740 | status = LTC_DES_EncryptCbc(LTC_BASE, in, out, sz, (byte*)des->reg, (byte*)des->key);
|
---|
741 | if (status == kStatus_Success)
|
---|
742 | return 0;
|
---|
743 | else
|
---|
744 | return -1;
|
---|
745 | }
|
---|
746 |
|
---|
747 | int wc_Des_CbcDecrypt(Des* des, byte* out, const byte* in, word32 sz)
|
---|
748 | {
|
---|
749 | status_t status;
|
---|
750 | status = LTC_DES_DecryptCbc(LTC_BASE, in, out, sz, (byte*)des->reg, (byte*)des->key);
|
---|
751 | if (status == kStatus_Success)
|
---|
752 | return 0;
|
---|
753 | else
|
---|
754 | return -1;
|
---|
755 | }
|
---|
756 |
|
---|
757 | int wc_Des3_CbcEncrypt(Des3* des, byte* out, const byte* in, word32 sz)
|
---|
758 | {
|
---|
759 | status_t status;
|
---|
760 | status = LTC_DES3_EncryptCbc(LTC_BASE,
|
---|
761 | in,
|
---|
762 | out,
|
---|
763 | sz,
|
---|
764 | (byte*)des->reg,
|
---|
765 | (byte*)des->key[0],
|
---|
766 | (byte*)des->key[1],
|
---|
767 | (byte*)des->key[2]);
|
---|
768 | if (status == kStatus_Success)
|
---|
769 | return 0;
|
---|
770 | else
|
---|
771 | return -1;
|
---|
772 | }
|
---|
773 |
|
---|
774 | int wc_Des3_CbcDecrypt(Des3* des, byte* out, const byte* in, word32 sz)
|
---|
775 | {
|
---|
776 | status_t status;
|
---|
777 | status = LTC_DES3_DecryptCbc(LTC_BASE,
|
---|
778 | in,
|
---|
779 | out,
|
---|
780 | sz,
|
---|
781 | (byte*)des->reg,
|
---|
782 | (byte*)des->key[0],
|
---|
783 | (byte*)des->key[1],
|
---|
784 | (byte*)des->key[2]);
|
---|
785 | if (status == kStatus_Success)
|
---|
786 | return 0;
|
---|
787 | else
|
---|
788 | return -1;
|
---|
789 |
|
---|
790 | }
|
---|
791 |
|
---|
792 | #elif defined(FREESCALE_MMCAU)
|
---|
793 | /*
|
---|
794 | * Freescale mmCAU hardware DES/3DES support through the CAU/mmCAU library.
|
---|
795 | * Documentation located in ColdFire/ColdFire+ CAU and Kinetis mmCAU
|
---|
796 | * Software Library User Guide (See note in README).
|
---|
797 | */
|
---|
798 | #ifdef FREESCALE_MMCAU_CLASSIC
|
---|
799 | #include "cau_api.h"
|
---|
800 | #else
|
---|
801 | #include "fsl_mmcau.h"
|
---|
802 | #endif
|
---|
803 |
|
---|
804 | const unsigned char parityLookup[128] = {
|
---|
805 | 1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,
|
---|
806 | 0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,
|
---|
807 | 0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,
|
---|
808 | 1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0
|
---|
809 | };
|
---|
810 |
|
---|
811 | int wc_Des_SetKey(Des* des, const byte* key, const byte* iv, int dir)
|
---|
812 | {
|
---|
813 | int i = 0;
|
---|
814 | byte* dkey;
|
---|
815 |
|
---|
816 |
|
---|
817 | if (des == NULL || key == NULL) {
|
---|
818 | return BAD_FUNC_ARG;
|
---|
819 | }
|
---|
820 |
|
---|
821 | dkey = (byte*)des->key;
|
---|
822 |
|
---|
823 | XMEMCPY(dkey, key, 8);
|
---|
824 |
|
---|
825 | wc_Des_SetIV(des, iv);
|
---|
826 |
|
---|
827 | /* fix key parity, if needed */
|
---|
828 | for (i = 0; i < 8; i++) {
|
---|
829 | dkey[i] = ((dkey[i] & 0xFE) | parityLookup[dkey[i] >> 1]);
|
---|
830 | }
|
---|
831 |
|
---|
832 | return 0;
|
---|
833 | }
|
---|
834 |
|
---|
835 | int wc_Des3_SetKey(Des3* des, const byte* key, const byte* iv, int dir)
|
---|
836 | {
|
---|
837 | int i = 0, ret = 0;
|
---|
838 | byte* dkey1 = (byte*)des->key[0];
|
---|
839 | byte* dkey2 = (byte*)des->key[1];
|
---|
840 | byte* dkey3 = (byte*)des->key[2];
|
---|
841 |
|
---|
842 | XMEMCPY(dkey1, key, 8); /* set key 1 */
|
---|
843 | XMEMCPY(dkey2, key + 8, 8); /* set key 2 */
|
---|
844 | XMEMCPY(dkey3, key + 16, 8); /* set key 3 */
|
---|
845 |
|
---|
846 | ret = wc_Des3_SetIV(des, iv);
|
---|
847 | if (ret != 0)
|
---|
848 | return ret;
|
---|
849 |
|
---|
850 | /* fix key parity if needed */
|
---|
851 | for (i = 0; i < 8; i++)
|
---|
852 | dkey1[i] = ((dkey1[i] & 0xFE) | parityLookup[dkey1[i] >> 1]);
|
---|
853 |
|
---|
854 | for (i = 0; i < 8; i++)
|
---|
855 | dkey2[i] = ((dkey2[i] & 0xFE) | parityLookup[dkey2[i] >> 1]);
|
---|
856 |
|
---|
857 | for (i = 0; i < 8; i++)
|
---|
858 | dkey3[i] = ((dkey3[i] & 0xFE) | parityLookup[dkey3[i] >> 1]);
|
---|
859 |
|
---|
860 | return ret;
|
---|
861 | }
|
---|
862 |
|
---|
863 | int wc_Des_CbcEncrypt(Des* des, byte* out, const byte* in, word32 sz)
|
---|
864 | {
|
---|
865 | int i;
|
---|
866 | int offset = 0;
|
---|
867 | int len = sz;
|
---|
868 | int ret = 0;
|
---|
869 | byte *iv;
|
---|
870 | byte temp_block[DES_BLOCK_SIZE];
|
---|
871 |
|
---|
872 | iv = (byte*)des->reg;
|
---|
873 |
|
---|
874 | #ifdef FREESCALE_MMCAU_CLASSIC
|
---|
875 | if ((wolfssl_word)out % WOLFSSL_MMCAU_ALIGNMENT) {
|
---|
876 | WOLFSSL_MSG("Bad cau_des_encrypt alignment");
|
---|
877 | return BAD_ALIGN_E;
|
---|
878 | }
|
---|
879 | #endif
|
---|
880 |
|
---|
881 | while (len > 0)
|
---|
882 | {
|
---|
883 | XMEMCPY(temp_block, in + offset, DES_BLOCK_SIZE);
|
---|
884 |
|
---|
885 | /* XOR block with IV for CBC */
|
---|
886 | for (i = 0; i < DES_BLOCK_SIZE; i++)
|
---|
887 | temp_block[i] ^= iv[i];
|
---|
888 |
|
---|
889 | ret = wolfSSL_CryptHwMutexLock();
|
---|
890 | if(ret != 0) {
|
---|
891 | return ret;
|
---|
892 | }
|
---|
893 | #ifdef FREESCALE_MMCAU_CLASSIC
|
---|
894 | cau_des_encrypt(temp_block, (byte*)des->key, out + offset);
|
---|
895 | #else
|
---|
896 | MMCAU_DES_EncryptEcb(temp_block, (byte*)des->key, out + offset);
|
---|
897 | #endif
|
---|
898 | wolfSSL_CryptHwMutexUnLock();
|
---|
899 |
|
---|
900 | len -= DES_BLOCK_SIZE;
|
---|
901 | offset += DES_BLOCK_SIZE;
|
---|
902 |
|
---|
903 | /* store IV for next block */
|
---|
904 | XMEMCPY(iv, out + offset - DES_BLOCK_SIZE, DES_BLOCK_SIZE);
|
---|
905 | }
|
---|
906 |
|
---|
907 | return ret;
|
---|
908 | }
|
---|
909 |
|
---|
910 | int wc_Des_CbcDecrypt(Des* des, byte* out, const byte* in, word32 sz)
|
---|
911 | {
|
---|
912 | int i;
|
---|
913 | int offset = 0;
|
---|
914 | int len = sz;
|
---|
915 | int ret = 0;
|
---|
916 | byte* iv;
|
---|
917 | byte temp_block[DES_BLOCK_SIZE];
|
---|
918 |
|
---|
919 | iv = (byte*)des->reg;
|
---|
920 |
|
---|
921 | #ifdef FREESCALE_MMCAU_CLASSIC
|
---|
922 | if ((wolfssl_word)out % WOLFSSL_MMCAU_ALIGNMENT) {
|
---|
923 | WOLFSSL_MSG("Bad cau_des_decrypt alignment");
|
---|
924 | return BAD_ALIGN_E;
|
---|
925 | }
|
---|
926 | #endif
|
---|
927 |
|
---|
928 | while (len > 0)
|
---|
929 | {
|
---|
930 | XMEMCPY(temp_block, in + offset, DES_BLOCK_SIZE);
|
---|
931 |
|
---|
932 | ret = wolfSSL_CryptHwMutexLock();
|
---|
933 | if(ret != 0) {
|
---|
934 | return ret;
|
---|
935 | }
|
---|
936 |
|
---|
937 | #ifdef FREESCALE_MMCAU_CLASSIC
|
---|
938 | cau_des_decrypt(in + offset, (byte*)des->key, out + offset);
|
---|
939 | #else
|
---|
940 | MMCAU_DES_DecryptEcb(in + offset, (byte*)des->key, out + offset);
|
---|
941 | #endif
|
---|
942 | wolfSSL_CryptHwMutexUnLock();
|
---|
943 |
|
---|
944 | /* XOR block with IV for CBC */
|
---|
945 | for (i = 0; i < DES_BLOCK_SIZE; i++)
|
---|
946 | (out + offset)[i] ^= iv[i];
|
---|
947 |
|
---|
948 | /* store IV for next block */
|
---|
949 | XMEMCPY(iv, temp_block, DES_BLOCK_SIZE);
|
---|
950 |
|
---|
951 | len -= DES_BLOCK_SIZE;
|
---|
952 | offset += DES_BLOCK_SIZE;
|
---|
953 | }
|
---|
954 |
|
---|
955 | return ret;
|
---|
956 | }
|
---|
957 |
|
---|
958 | int wc_Des3_CbcEncrypt(Des3* des, byte* out, const byte* in, word32 sz)
|
---|
959 | {
|
---|
960 | int i;
|
---|
961 | int offset = 0;
|
---|
962 | int len = sz;
|
---|
963 | int ret = 0;
|
---|
964 |
|
---|
965 | byte *iv;
|
---|
966 | byte temp_block[DES_BLOCK_SIZE];
|
---|
967 |
|
---|
968 | iv = (byte*)des->reg;
|
---|
969 |
|
---|
970 | #ifdef FREESCALE_MMCAU_CLASSIC
|
---|
971 | if ((wolfssl_word)out % WOLFSSL_MMCAU_ALIGNMENT) {
|
---|
972 | WOLFSSL_MSG("Bad 3ede cau_des_encrypt alignment");
|
---|
973 | return BAD_ALIGN_E;
|
---|
974 | }
|
---|
975 | #endif
|
---|
976 |
|
---|
977 | while (len > 0)
|
---|
978 | {
|
---|
979 | XMEMCPY(temp_block, in + offset, DES_BLOCK_SIZE);
|
---|
980 |
|
---|
981 | /* XOR block with IV for CBC */
|
---|
982 | for (i = 0; i < DES_BLOCK_SIZE; i++)
|
---|
983 | temp_block[i] ^= iv[i];
|
---|
984 |
|
---|
985 | ret = wolfSSL_CryptHwMutexLock();
|
---|
986 | if(ret != 0) {
|
---|
987 | return ret;
|
---|
988 | }
|
---|
989 | #ifdef FREESCALE_MMCAU_CLASSIC
|
---|
990 | cau_des_encrypt(temp_block, (byte*)des->key[0], out + offset);
|
---|
991 | cau_des_decrypt(out + offset, (byte*)des->key[1], out + offset);
|
---|
992 | cau_des_encrypt(out + offset, (byte*)des->key[2], out + offset);
|
---|
993 | #else
|
---|
994 | MMCAU_DES_EncryptEcb(temp_block , (byte*)des->key[0], out + offset);
|
---|
995 | MMCAU_DES_DecryptEcb(out + offset, (byte*)des->key[1], out + offset);
|
---|
996 | MMCAU_DES_EncryptEcb(out + offset, (byte*)des->key[2], out + offset);
|
---|
997 | #endif
|
---|
998 | wolfSSL_CryptHwMutexUnLock();
|
---|
999 |
|
---|
1000 | len -= DES_BLOCK_SIZE;
|
---|
1001 | offset += DES_BLOCK_SIZE;
|
---|
1002 |
|
---|
1003 | /* store IV for next block */
|
---|
1004 | XMEMCPY(iv, out + offset - DES_BLOCK_SIZE, DES_BLOCK_SIZE);
|
---|
1005 | }
|
---|
1006 |
|
---|
1007 | return ret;
|
---|
1008 | }
|
---|
1009 |
|
---|
1010 | int wc_Des3_CbcDecrypt(Des3* des, byte* out, const byte* in, word32 sz)
|
---|
1011 | {
|
---|
1012 | int i;
|
---|
1013 | int offset = 0;
|
---|
1014 | int len = sz;
|
---|
1015 | int ret = 0;
|
---|
1016 |
|
---|
1017 | byte* iv;
|
---|
1018 | byte temp_block[DES_BLOCK_SIZE];
|
---|
1019 |
|
---|
1020 | iv = (byte*)des->reg;
|
---|
1021 |
|
---|
1022 | #ifdef FREESCALE_MMCAU_CLASSIC
|
---|
1023 | if ((wolfssl_word)out % WOLFSSL_MMCAU_ALIGNMENT) {
|
---|
1024 | WOLFSSL_MSG("Bad 3ede cau_des_decrypt alignment");
|
---|
1025 | return BAD_ALIGN_E;
|
---|
1026 | }
|
---|
1027 | #endif
|
---|
1028 |
|
---|
1029 | while (len > 0)
|
---|
1030 | {
|
---|
1031 | XMEMCPY(temp_block, in + offset, DES_BLOCK_SIZE);
|
---|
1032 |
|
---|
1033 | ret = wolfSSL_CryptHwMutexLock();
|
---|
1034 | if(ret != 0) {
|
---|
1035 | return ret;
|
---|
1036 | }
|
---|
1037 | #ifdef FREESCALE_MMCAU_CLASSIC
|
---|
1038 | cau_des_decrypt(in + offset, (byte*)des->key[2], out + offset);
|
---|
1039 | cau_des_encrypt(out + offset, (byte*)des->key[1], out + offset);
|
---|
1040 | cau_des_decrypt(out + offset, (byte*)des->key[0], out + offset);
|
---|
1041 | #else
|
---|
1042 | MMCAU_DES_DecryptEcb(in + offset , (byte*)des->key[2], out + offset);
|
---|
1043 | MMCAU_DES_EncryptEcb(out + offset, (byte*)des->key[1], out + offset);
|
---|
1044 | MMCAU_DES_DecryptEcb(out + offset, (byte*)des->key[0], out + offset);
|
---|
1045 | #endif
|
---|
1046 | wolfSSL_CryptHwMutexUnLock();
|
---|
1047 |
|
---|
1048 | /* XOR block with IV for CBC */
|
---|
1049 | for (i = 0; i < DES_BLOCK_SIZE; i++)
|
---|
1050 | (out + offset)[i] ^= iv[i];
|
---|
1051 |
|
---|
1052 | /* store IV for next block */
|
---|
1053 | XMEMCPY(iv, temp_block, DES_BLOCK_SIZE);
|
---|
1054 |
|
---|
1055 | len -= DES_BLOCK_SIZE;
|
---|
1056 | offset += DES_BLOCK_SIZE;
|
---|
1057 | }
|
---|
1058 |
|
---|
1059 | return ret;
|
---|
1060 | }
|
---|
1061 |
|
---|
1062 |
|
---|
1063 | #elif defined(WOLFSSL_PIC32MZ_CRYPT)
|
---|
1064 |
|
---|
1065 | /* PIC32MZ DES hardware requires size multiple of block size */
|
---|
1066 | #include <wolfssl/wolfcrypt/port/pic32/pic32mz-crypt.h>
|
---|
1067 |
|
---|
1068 | int wc_Des_SetKey(Des* des, const byte* key, const byte* iv, int dir)
|
---|
1069 | {
|
---|
1070 | if (des == NULL || key == NULL || iv == NULL)
|
---|
1071 | return BAD_FUNC_ARG;
|
---|
1072 |
|
---|
1073 | XMEMCPY(des->key, key, DES_KEYLEN);
|
---|
1074 | XMEMCPY(des->reg, iv, DES_IVLEN);
|
---|
1075 |
|
---|
1076 | return 0;
|
---|
1077 | }
|
---|
1078 |
|
---|
1079 | int wc_Des3_SetKey(Des3* des, const byte* key, const byte* iv, int dir)
|
---|
1080 | {
|
---|
1081 | if (des == NULL || key == NULL || iv == NULL)
|
---|
1082 | return BAD_FUNC_ARG;
|
---|
1083 |
|
---|
1084 | XMEMCPY(des->key[0], key, DES3_KEYLEN);
|
---|
1085 | XMEMCPY(des->reg, iv, DES3_IVLEN);
|
---|
1086 |
|
---|
1087 | return 0;
|
---|
1088 | }
|
---|
1089 |
|
---|
1090 | int wc_Des_CbcEncrypt(Des* des, byte* out, const byte* in, word32 sz)
|
---|
1091 | {
|
---|
1092 | word32 blocks = sz / DES_BLOCK_SIZE;
|
---|
1093 |
|
---|
1094 | if (des == NULL || out == NULL || in == NULL)
|
---|
1095 | return BAD_FUNC_ARG;
|
---|
1096 |
|
---|
1097 | return wc_Pic32DesCrypt(des->key, DES_KEYLEN, des->reg, DES_IVLEN,
|
---|
1098 | out, in, (blocks * DES_BLOCK_SIZE),
|
---|
1099 | PIC32_ENCRYPTION, PIC32_ALGO_DES, PIC32_CRYPTOALGO_CBC);
|
---|
1100 | }
|
---|
1101 |
|
---|
1102 | int wc_Des_CbcDecrypt(Des* des, byte* out, const byte* in, word32 sz)
|
---|
1103 | {
|
---|
1104 | word32 blocks = sz / DES_BLOCK_SIZE;
|
---|
1105 |
|
---|
1106 | if (des == NULL || out == NULL || in == NULL)
|
---|
1107 | return BAD_FUNC_ARG;
|
---|
1108 |
|
---|
1109 | return wc_Pic32DesCrypt(des->key, DES_KEYLEN, des->reg, DES_IVLEN,
|
---|
1110 | out, in, (blocks * DES_BLOCK_SIZE),
|
---|
1111 | PIC32_DECRYPTION, PIC32_ALGO_DES, PIC32_CRYPTOALGO_CBC);
|
---|
1112 | }
|
---|
1113 |
|
---|
1114 | int wc_Des3_CbcEncrypt(Des3* des, byte* out, const byte* in, word32 sz)
|
---|
1115 | {
|
---|
1116 | word32 blocks = sz / DES_BLOCK_SIZE;
|
---|
1117 |
|
---|
1118 | if (des == NULL || out == NULL || in == NULL)
|
---|
1119 | return BAD_FUNC_ARG;
|
---|
1120 |
|
---|
1121 | return wc_Pic32DesCrypt(des->key[0], DES3_KEYLEN, des->reg, DES3_IVLEN,
|
---|
1122 | out, in, (blocks * DES_BLOCK_SIZE),
|
---|
1123 | PIC32_ENCRYPTION, PIC32_ALGO_TDES, PIC32_CRYPTOALGO_TCBC);
|
---|
1124 | }
|
---|
1125 |
|
---|
1126 | int wc_Des3_CbcDecrypt(Des3* des, byte* out, const byte* in, word32 sz)
|
---|
1127 | {
|
---|
1128 | word32 blocks = sz / DES_BLOCK_SIZE;
|
---|
1129 |
|
---|
1130 | if (des == NULL || out == NULL || in == NULL)
|
---|
1131 | return BAD_FUNC_ARG;
|
---|
1132 |
|
---|
1133 | return wc_Pic32DesCrypt(des->key[0], DES3_KEYLEN, des->reg, DES3_IVLEN,
|
---|
1134 | out, in, (blocks * DES_BLOCK_SIZE),
|
---|
1135 | PIC32_DECRYPTION, PIC32_ALGO_TDES, PIC32_CRYPTOALGO_TCBC);
|
---|
1136 | }
|
---|
1137 |
|
---|
1138 | #ifdef WOLFSSL_DES_ECB
|
---|
1139 | int wc_Des_EcbEncrypt(Des* des, byte* out, const byte* in, word32 sz)
|
---|
1140 | {
|
---|
1141 | word32 blocks = sz / DES_BLOCK_SIZE;
|
---|
1142 |
|
---|
1143 | if (des == NULL || out == NULL || in == NULL)
|
---|
1144 | return BAD_FUNC_ARG;
|
---|
1145 |
|
---|
1146 | return wc_Pic32DesCrypt(des->key, DES_KEYLEN, des->reg, DES_IVLEN,
|
---|
1147 | out, in, (blocks * DES_BLOCK_SIZE),
|
---|
1148 | PIC32_ENCRYPTION, PIC32_ALGO_DES, PIC32_CRYPTOALGO_ECB);
|
---|
1149 | }
|
---|
1150 |
|
---|
1151 | int wc_Des3_EcbEncrypt(Des3* des, byte* out, const byte* in, word32 sz)
|
---|
1152 | {
|
---|
1153 | word32 blocks = sz / DES_BLOCK_SIZE;
|
---|
1154 |
|
---|
1155 | if (des == NULL || out == NULL || in == NULL)
|
---|
1156 | return BAD_FUNC_ARG;
|
---|
1157 |
|
---|
1158 | return wc_Pic32DesCrypt(des->key[0], DES3_KEYLEN, des->reg, DES3_IVLEN,
|
---|
1159 | out, in, (blocks * DES_BLOCK_SIZE),
|
---|
1160 | PIC32_ENCRYPTION, PIC32_ALGO_TDES, PIC32_CRYPTOALGO_TECB);
|
---|
1161 | }
|
---|
1162 | #endif /* WOLFSSL_DES_ECB */
|
---|
1163 |
|
---|
1164 | #else
|
---|
1165 | #define NEED_SOFT_DES
|
---|
1166 |
|
---|
1167 | #endif
|
---|
1168 |
|
---|
1169 |
|
---|
1170 | #ifdef NEED_SOFT_DES
|
---|
1171 |
|
---|
1172 | /* permuted choice table (key) */
|
---|
1173 | static const FLASH_QUALIFIER byte pc1[] = {
|
---|
1174 | 57, 49, 41, 33, 25, 17, 9,
|
---|
1175 | 1, 58, 50, 42, 34, 26, 18,
|
---|
1176 | 10, 2, 59, 51, 43, 35, 27,
|
---|
1177 | 19, 11, 3, 60, 52, 44, 36,
|
---|
1178 |
|
---|
1179 | 63, 55, 47, 39, 31, 23, 15,
|
---|
1180 | 7, 62, 54, 46, 38, 30, 22,
|
---|
1181 | 14, 6, 61, 53, 45, 37, 29,
|
---|
1182 | 21, 13, 5, 28, 20, 12, 4
|
---|
1183 | };
|
---|
1184 |
|
---|
1185 | /* number left rotations of pc1 */
|
---|
1186 | static const FLASH_QUALIFIER byte totrot[] = {
|
---|
1187 | 1,2,4,6,8,10,12,14,15,17,19,21,23,25,27,28
|
---|
1188 | };
|
---|
1189 |
|
---|
1190 | /* permuted choice key (table) */
|
---|
1191 | static const FLASH_QUALIFIER byte pc2[] = {
|
---|
1192 | 14, 17, 11, 24, 1, 5,
|
---|
1193 | 3, 28, 15, 6, 21, 10,
|
---|
1194 | 23, 19, 12, 4, 26, 8,
|
---|
1195 | 16, 7, 27, 20, 13, 2,
|
---|
1196 | 41, 52, 31, 37, 47, 55,
|
---|
1197 | 30, 40, 51, 45, 33, 48,
|
---|
1198 | 44, 49, 39, 56, 34, 53,
|
---|
1199 | 46, 42, 50, 36, 29, 32
|
---|
1200 | };
|
---|
1201 |
|
---|
1202 | /* End of DES-defined tables */
|
---|
1203 |
|
---|
1204 | /* bit 0 is left-most in byte */
|
---|
1205 | static const FLASH_QUALIFIER int bytebit[] = {
|
---|
1206 | 0200,0100,040,020,010,04,02,01
|
---|
1207 | };
|
---|
1208 |
|
---|
1209 | static const FLASH_QUALIFIER word32 Spbox[8][64] = {
|
---|
1210 | { 0x01010400,0x00000000,0x00010000,0x01010404,
|
---|
1211 | 0x01010004,0x00010404,0x00000004,0x00010000,
|
---|
1212 | 0x00000400,0x01010400,0x01010404,0x00000400,
|
---|
1213 | 0x01000404,0x01010004,0x01000000,0x00000004,
|
---|
1214 | 0x00000404,0x01000400,0x01000400,0x00010400,
|
---|
1215 | 0x00010400,0x01010000,0x01010000,0x01000404,
|
---|
1216 | 0x00010004,0x01000004,0x01000004,0x00010004,
|
---|
1217 | 0x00000000,0x00000404,0x00010404,0x01000000,
|
---|
1218 | 0x00010000,0x01010404,0x00000004,0x01010000,
|
---|
1219 | 0x01010400,0x01000000,0x01000000,0x00000400,
|
---|
1220 | 0x01010004,0x00010000,0x00010400,0x01000004,
|
---|
1221 | 0x00000400,0x00000004,0x01000404,0x00010404,
|
---|
1222 | 0x01010404,0x00010004,0x01010000,0x01000404,
|
---|
1223 | 0x01000004,0x00000404,0x00010404,0x01010400,
|
---|
1224 | 0x00000404,0x01000400,0x01000400,0x00000000,
|
---|
1225 | 0x00010004,0x00010400,0x00000000,0x01010004},
|
---|
1226 | { 0x80108020,0x80008000,0x00008000,0x00108020,
|
---|
1227 | 0x00100000,0x00000020,0x80100020,0x80008020,
|
---|
1228 | 0x80000020,0x80108020,0x80108000,0x80000000,
|
---|
1229 | 0x80008000,0x00100000,0x00000020,0x80100020,
|
---|
1230 | 0x00108000,0x00100020,0x80008020,0x00000000,
|
---|
1231 | 0x80000000,0x00008000,0x00108020,0x80100000,
|
---|
1232 | 0x00100020,0x80000020,0x00000000,0x00108000,
|
---|
1233 | 0x00008020,0x80108000,0x80100000,0x00008020,
|
---|
1234 | 0x00000000,0x00108020,0x80100020,0x00100000,
|
---|
1235 | 0x80008020,0x80100000,0x80108000,0x00008000,
|
---|
1236 | 0x80100000,0x80008000,0x00000020,0x80108020,
|
---|
1237 | 0x00108020,0x00000020,0x00008000,0x80000000,
|
---|
1238 | 0x00008020,0x80108000,0x00100000,0x80000020,
|
---|
1239 | 0x00100020,0x80008020,0x80000020,0x00100020,
|
---|
1240 | 0x00108000,0x00000000,0x80008000,0x00008020,
|
---|
1241 | 0x80000000,0x80100020,0x80108020,0x00108000},
|
---|
1242 | { 0x00000208,0x08020200,0x00000000,0x08020008,
|
---|
1243 | 0x08000200,0x00000000,0x00020208,0x08000200,
|
---|
1244 | 0x00020008,0x08000008,0x08000008,0x00020000,
|
---|
1245 | 0x08020208,0x00020008,0x08020000,0x00000208,
|
---|
1246 | 0x08000000,0x00000008,0x08020200,0x00000200,
|
---|
1247 | 0x00020200,0x08020000,0x08020008,0x00020208,
|
---|
1248 | 0x08000208,0x00020200,0x00020000,0x08000208,
|
---|
1249 | 0x00000008,0x08020208,0x00000200,0x08000000,
|
---|
1250 | 0x08020200,0x08000000,0x00020008,0x00000208,
|
---|
1251 | 0x00020000,0x08020200,0x08000200,0x00000000,
|
---|
1252 | 0x00000200,0x00020008,0x08020208,0x08000200,
|
---|
1253 | 0x08000008,0x00000200,0x00000000,0x08020008,
|
---|
1254 | 0x08000208,0x00020000,0x08000000,0x08020208,
|
---|
1255 | 0x00000008,0x00020208,0x00020200,0x08000008,
|
---|
1256 | 0x08020000,0x08000208,0x00000208,0x08020000,
|
---|
1257 | 0x00020208,0x00000008,0x08020008,0x00020200},
|
---|
1258 | { 0x00802001,0x00002081,0x00002081,0x00000080,
|
---|
1259 | 0x00802080,0x00800081,0x00800001,0x00002001,
|
---|
1260 | 0x00000000,0x00802000,0x00802000,0x00802081,
|
---|
1261 | 0x00000081,0x00000000,0x00800080,0x00800001,
|
---|
1262 | 0x00000001,0x00002000,0x00800000,0x00802001,
|
---|
1263 | 0x00000080,0x00800000,0x00002001,0x00002080,
|
---|
1264 | 0x00800081,0x00000001,0x00002080,0x00800080,
|
---|
1265 | 0x00002000,0x00802080,0x00802081,0x00000081,
|
---|
1266 | 0x00800080,0x00800001,0x00802000,0x00802081,
|
---|
1267 | 0x00000081,0x00000000,0x00000000,0x00802000,
|
---|
1268 | 0x00002080,0x00800080,0x00800081,0x00000001,
|
---|
1269 | 0x00802001,0x00002081,0x00002081,0x00000080,
|
---|
1270 | 0x00802081,0x00000081,0x00000001,0x00002000,
|
---|
1271 | 0x00800001,0x00002001,0x00802080,0x00800081,
|
---|
1272 | 0x00002001,0x00002080,0x00800000,0x00802001,
|
---|
1273 | 0x00000080,0x00800000,0x00002000,0x00802080},
|
---|
1274 | { 0x00000100,0x02080100,0x02080000,0x42000100,
|
---|
1275 | 0x00080000,0x00000100,0x40000000,0x02080000,
|
---|
1276 | 0x40080100,0x00080000,0x02000100,0x40080100,
|
---|
1277 | 0x42000100,0x42080000,0x00080100,0x40000000,
|
---|
1278 | 0x02000000,0x40080000,0x40080000,0x00000000,
|
---|
1279 | 0x40000100,0x42080100,0x42080100,0x02000100,
|
---|
1280 | 0x42080000,0x40000100,0x00000000,0x42000000,
|
---|
1281 | 0x02080100,0x02000000,0x42000000,0x00080100,
|
---|
1282 | 0x00080000,0x42000100,0x00000100,0x02000000,
|
---|
1283 | 0x40000000,0x02080000,0x42000100,0x40080100,
|
---|
1284 | 0x02000100,0x40000000,0x42080000,0x02080100,
|
---|
1285 | 0x40080100,0x00000100,0x02000000,0x42080000,
|
---|
1286 | 0x42080100,0x00080100,0x42000000,0x42080100,
|
---|
1287 | 0x02080000,0x00000000,0x40080000,0x42000000,
|
---|
1288 | 0x00080100,0x02000100,0x40000100,0x00080000,
|
---|
1289 | 0x00000000,0x40080000,0x02080100,0x40000100},
|
---|
1290 | { 0x20000010,0x20400000,0x00004000,0x20404010,
|
---|
1291 | 0x20400000,0x00000010,0x20404010,0x00400000,
|
---|
1292 | 0x20004000,0x00404010,0x00400000,0x20000010,
|
---|
1293 | 0x00400010,0x20004000,0x20000000,0x00004010,
|
---|
1294 | 0x00000000,0x00400010,0x20004010,0x00004000,
|
---|
1295 | 0x00404000,0x20004010,0x00000010,0x20400010,
|
---|
1296 | 0x20400010,0x00000000,0x00404010,0x20404000,
|
---|
1297 | 0x00004010,0x00404000,0x20404000,0x20000000,
|
---|
1298 | 0x20004000,0x00000010,0x20400010,0x00404000,
|
---|
1299 | 0x20404010,0x00400000,0x00004010,0x20000010,
|
---|
1300 | 0x00400000,0x20004000,0x20000000,0x00004010,
|
---|
1301 | 0x20000010,0x20404010,0x00404000,0x20400000,
|
---|
1302 | 0x00404010,0x20404000,0x00000000,0x20400010,
|
---|
1303 | 0x00000010,0x00004000,0x20400000,0x00404010,
|
---|
1304 | 0x00004000,0x00400010,0x20004010,0x00000000,
|
---|
1305 | 0x20404000,0x20000000,0x00400010,0x20004010},
|
---|
1306 | { 0x00200000,0x04200002,0x04000802,0x00000000,
|
---|
1307 | 0x00000800,0x04000802,0x00200802,0x04200800,
|
---|
1308 | 0x04200802,0x00200000,0x00000000,0x04000002,
|
---|
1309 | 0x00000002,0x04000000,0x04200002,0x00000802,
|
---|
1310 | 0x04000800,0x00200802,0x00200002,0x04000800,
|
---|
1311 | 0x04000002,0x04200000,0x04200800,0x00200002,
|
---|
1312 | 0x04200000,0x00000800,0x00000802,0x04200802,
|
---|
1313 | 0x00200800,0x00000002,0x04000000,0x00200800,
|
---|
1314 | 0x04000000,0x00200800,0x00200000,0x04000802,
|
---|
1315 | 0x04000802,0x04200002,0x04200002,0x00000002,
|
---|
1316 | 0x00200002,0x04000000,0x04000800,0x00200000,
|
---|
1317 | 0x04200800,0x00000802,0x00200802,0x04200800,
|
---|
1318 | 0x00000802,0x04000002,0x04200802,0x04200000,
|
---|
1319 | 0x00200800,0x00000000,0x00000002,0x04200802,
|
---|
1320 | 0x00000000,0x00200802,0x04200000,0x00000800,
|
---|
1321 | 0x04000002,0x04000800,0x00000800,0x00200002},
|
---|
1322 | { 0x10001040,0x00001000,0x00040000,0x10041040,
|
---|
1323 | 0x10000000,0x10001040,0x00000040,0x10000000,
|
---|
1324 | 0x00040040,0x10040000,0x10041040,0x00041000,
|
---|
1325 | 0x10041000,0x00041040,0x00001000,0x00000040,
|
---|
1326 | 0x10040000,0x10000040,0x10001000,0x00001040,
|
---|
1327 | 0x00041000,0x00040040,0x10040040,0x10041000,
|
---|
1328 | 0x00001040,0x00000000,0x00000000,0x10040040,
|
---|
1329 | 0x10000040,0x10001000,0x00041040,0x00040000,
|
---|
1330 | 0x00041040,0x00040000,0x10041000,0x00001000,
|
---|
1331 | 0x00000040,0x10040040,0x00001000,0x00041040,
|
---|
1332 | 0x10001000,0x00000040,0x10000040,0x10040000,
|
---|
1333 | 0x10040040,0x10000000,0x00040000,0x10001040,
|
---|
1334 | 0x00000000,0x10041040,0x00040040,0x10000040,
|
---|
1335 | 0x10040000,0x10001000,0x10001040,0x00000000,
|
---|
1336 | 0x10041040,0x00041000,0x00041000,0x00001040,
|
---|
1337 | 0x00001040,0x00040040,0x10000000,0x10041000}
|
---|
1338 | };
|
---|
1339 |
|
---|
1340 | static WC_INLINE void IPERM(word32* left, word32* right)
|
---|
1341 | {
|
---|
1342 | word32 work;
|
---|
1343 |
|
---|
1344 | *right = rotlFixed(*right, 4U);
|
---|
1345 | work = (*left ^ *right) & 0xf0f0f0f0;
|
---|
1346 | *left ^= work;
|
---|
1347 |
|
---|
1348 | *right = rotrFixed(*right^work, 20U);
|
---|
1349 | work = (*left ^ *right) & 0xffff0000;
|
---|
1350 | *left ^= work;
|
---|
1351 |
|
---|
1352 | *right = rotrFixed(*right^work, 18U);
|
---|
1353 | work = (*left ^ *right) & 0x33333333;
|
---|
1354 | *left ^= work;
|
---|
1355 |
|
---|
1356 | *right = rotrFixed(*right^work, 6U);
|
---|
1357 | work = (*left ^ *right) & 0x00ff00ff;
|
---|
1358 | *left ^= work;
|
---|
1359 |
|
---|
1360 | *right = rotlFixed(*right^work, 9U);
|
---|
1361 | work = (*left ^ *right) & 0xaaaaaaaa;
|
---|
1362 | *left = rotlFixed(*left^work, 1U);
|
---|
1363 | *right ^= work;
|
---|
1364 | }
|
---|
1365 |
|
---|
1366 | static WC_INLINE void FPERM(word32* left, word32* right)
|
---|
1367 | {
|
---|
1368 | word32 work;
|
---|
1369 |
|
---|
1370 | *right = rotrFixed(*right, 1U);
|
---|
1371 | work = (*left ^ *right) & 0xaaaaaaaa;
|
---|
1372 | *right ^= work;
|
---|
1373 |
|
---|
1374 | *left = rotrFixed(*left^work, 9U);
|
---|
1375 | work = (*left ^ *right) & 0x00ff00ff;
|
---|
1376 | *right ^= work;
|
---|
1377 |
|
---|
1378 | *left = rotlFixed(*left^work, 6U);
|
---|
1379 | work = (*left ^ *right) & 0x33333333;
|
---|
1380 | *right ^= work;
|
---|
1381 |
|
---|
1382 | *left = rotlFixed(*left^work, 18U);
|
---|
1383 | work = (*left ^ *right) & 0xffff0000;
|
---|
1384 | *right ^= work;
|
---|
1385 |
|
---|
1386 | *left = rotlFixed(*left^work, 20U);
|
---|
1387 | work = (*left ^ *right) & 0xf0f0f0f0;
|
---|
1388 | *right ^= work;
|
---|
1389 |
|
---|
1390 | *left = rotrFixed(*left^work, 4U);
|
---|
1391 | }
|
---|
1392 |
|
---|
1393 | static int DesSetKey(const byte* key, int dir, word32* out)
|
---|
1394 | {
|
---|
1395 | #define DES_KEY_BUFFER_SIZE (56+56+8)
|
---|
1396 | #ifdef WOLFSSL_SMALL_STACK
|
---|
1397 | byte* buffer = (byte*)XMALLOC(DES_KEY_BUFFER_SIZE, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
---|
1398 |
|
---|
1399 | if (buffer == NULL)
|
---|
1400 | return MEMORY_E;
|
---|
1401 | #else
|
---|
1402 | byte buffer[DES_KEY_BUFFER_SIZE];
|
---|
1403 | #endif
|
---|
1404 |
|
---|
1405 | {
|
---|
1406 | byte* const pc1m = buffer; /* place to modify pc1 into */
|
---|
1407 | byte* const pcr = pc1m + 56; /* place to rotate pc1 into */
|
---|
1408 | byte* const ks = pcr + 56;
|
---|
1409 | register int i, j, l;
|
---|
1410 | int m;
|
---|
1411 |
|
---|
1412 | for (j = 0; j < 56; j++) { /* convert pc1 to bits of key */
|
---|
1413 | l = pc1[j] - 1; /* integer bit location */
|
---|
1414 | m = l & 07; /* find bit */
|
---|
1415 | pc1m[j] = (key[l >> 3] & /* find which key byte l is in */
|
---|
1416 | bytebit[m]) /* and which bit of that byte */
|
---|
1417 | ? 1 : 0; /* and store 1-bit result */
|
---|
1418 | }
|
---|
1419 |
|
---|
1420 | for (i = 0; i < 16; i++) { /* key chunk for each iteration */
|
---|
1421 | XMEMSET(ks, 0, 8); /* Clear key schedule */
|
---|
1422 |
|
---|
1423 | for (j = 0; j < 56; j++) /* rotate pc1 the right amount */
|
---|
1424 | pcr[j] =
|
---|
1425 | pc1m[(l = j + totrot[i]) < (j < 28 ? 28 : 56) ? l : l-28];
|
---|
1426 |
|
---|
1427 | /* rotate left and right halves independently */
|
---|
1428 | for (j = 0; j < 48; j++) { /* select bits individually */
|
---|
1429 | if (pcr[pc2[j] - 1]) { /* check bit that goes to ks[j] */
|
---|
1430 | l= j % 6; /* mask it in if it's there */
|
---|
1431 | ks[j/6] |= bytebit[l] >> 2;
|
---|
1432 | }
|
---|
1433 | }
|
---|
1434 |
|
---|
1435 | /* Now convert to odd/even interleaved form for use in F */
|
---|
1436 | out[2*i] = ((word32) ks[0] << 24)
|
---|
1437 | | ((word32) ks[2] << 16)
|
---|
1438 | | ((word32) ks[4] << 8)
|
---|
1439 | | ((word32) ks[6]);
|
---|
1440 |
|
---|
1441 | out[2*i + 1] = ((word32) ks[1] << 24)
|
---|
1442 | | ((word32) ks[3] << 16)
|
---|
1443 | | ((word32) ks[5] << 8)
|
---|
1444 | | ((word32) ks[7]);
|
---|
1445 | }
|
---|
1446 |
|
---|
1447 | /* reverse key schedule order */
|
---|
1448 | if (dir == DES_DECRYPTION) {
|
---|
1449 | for (i = 0; i < 16; i += 2) {
|
---|
1450 | word32 swap = out[i];
|
---|
1451 | out[i] = out[DES_KS_SIZE - 2 - i];
|
---|
1452 | out[DES_KS_SIZE - 2 - i] = swap;
|
---|
1453 |
|
---|
1454 | swap = out[i + 1];
|
---|
1455 | out[i + 1] = out[DES_KS_SIZE - 1 - i];
|
---|
1456 | out[DES_KS_SIZE - 1 - i] = swap;
|
---|
1457 | }
|
---|
1458 | }
|
---|
1459 |
|
---|
1460 | #ifdef WOLFSSL_SMALL_STACK
|
---|
1461 | XFREE(buffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
---|
1462 | #endif
|
---|
1463 | }
|
---|
1464 |
|
---|
1465 | return 0;
|
---|
1466 | }
|
---|
1467 |
|
---|
1468 | int wc_Des_SetKey(Des* des, const byte* key, const byte* iv, int dir)
|
---|
1469 | {
|
---|
1470 | wc_Des_SetIV(des, iv);
|
---|
1471 |
|
---|
1472 | return DesSetKey(key, dir, des->key);
|
---|
1473 | }
|
---|
1474 |
|
---|
1475 | int wc_Des3_SetKey(Des3* des, const byte* key, const byte* iv, int dir)
|
---|
1476 | {
|
---|
1477 | int ret;
|
---|
1478 |
|
---|
1479 | if (des == NULL || key == NULL || dir < 0) {
|
---|
1480 | return BAD_FUNC_ARG;
|
---|
1481 | }
|
---|
1482 |
|
---|
1483 | #if defined(WOLF_CRYPTO_CB) || \
|
---|
1484 | (defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_3DES))
|
---|
1485 | #ifdef WOLF_CRYPTO_CB
|
---|
1486 | if (des->devId != INVALID_DEVID)
|
---|
1487 | #endif
|
---|
1488 | {
|
---|
1489 | XMEMCPY(des->devKey, key, DES3_KEYLEN);
|
---|
1490 | }
|
---|
1491 | #endif
|
---|
1492 |
|
---|
1493 | ret = DesSetKey(key + (dir == DES_ENCRYPTION ? 0:16), dir, des->key[0]);
|
---|
1494 | if (ret != 0)
|
---|
1495 | return ret;
|
---|
1496 |
|
---|
1497 | ret = DesSetKey(key + 8, !dir, des->key[1]);
|
---|
1498 | if (ret != 0)
|
---|
1499 | return ret;
|
---|
1500 |
|
---|
1501 | ret = DesSetKey(key + (dir == DES_DECRYPTION ? 0:16), dir, des->key[2]);
|
---|
1502 | if (ret != 0)
|
---|
1503 | return ret;
|
---|
1504 |
|
---|
1505 | return wc_Des3_SetIV(des, iv);
|
---|
1506 | }
|
---|
1507 |
|
---|
1508 | static void DesRawProcessBlock(word32* lIn, word32* rIn, const word32* kptr)
|
---|
1509 | {
|
---|
1510 | word32 l = *lIn, r = *rIn, i;
|
---|
1511 |
|
---|
1512 | for (i=0; i<8; i++)
|
---|
1513 | {
|
---|
1514 | word32 work = rotrFixed(r, 4U) ^ kptr[4*i+0];
|
---|
1515 | l ^= Spbox[6][(work) & 0x3f]
|
---|
1516 | ^ Spbox[4][(work >> 8) & 0x3f]
|
---|
1517 | ^ Spbox[2][(work >> 16) & 0x3f]
|
---|
1518 | ^ Spbox[0][(work >> 24) & 0x3f];
|
---|
1519 | work = r ^ kptr[4*i+1];
|
---|
1520 | l ^= Spbox[7][(work) & 0x3f]
|
---|
1521 | ^ Spbox[5][(work >> 8) & 0x3f]
|
---|
1522 | ^ Spbox[3][(work >> 16) & 0x3f]
|
---|
1523 | ^ Spbox[1][(work >> 24) & 0x3f];
|
---|
1524 |
|
---|
1525 | work = rotrFixed(l, 4U) ^ kptr[4*i+2];
|
---|
1526 | r ^= Spbox[6][(work) & 0x3f]
|
---|
1527 | ^ Spbox[4][(work >> 8) & 0x3f]
|
---|
1528 | ^ Spbox[2][(work >> 16) & 0x3f]
|
---|
1529 | ^ Spbox[0][(work >> 24) & 0x3f];
|
---|
1530 | work = l ^ kptr[4*i+3];
|
---|
1531 | r ^= Spbox[7][(work) & 0x3f]
|
---|
1532 | ^ Spbox[5][(work >> 8) & 0x3f]
|
---|
1533 | ^ Spbox[3][(work >> 16) & 0x3f]
|
---|
1534 | ^ Spbox[1][(work >> 24) & 0x3f];
|
---|
1535 | }
|
---|
1536 |
|
---|
1537 | *lIn = l; *rIn = r;
|
---|
1538 | }
|
---|
1539 |
|
---|
1540 | static void DesProcessBlock(Des* des, const byte* in, byte* out)
|
---|
1541 | {
|
---|
1542 | word32 l, r;
|
---|
1543 |
|
---|
1544 | XMEMCPY(&l, in, sizeof(l));
|
---|
1545 | XMEMCPY(&r, in + sizeof(l), sizeof(r));
|
---|
1546 | #ifdef LITTLE_ENDIAN_ORDER
|
---|
1547 | l = ByteReverseWord32(l);
|
---|
1548 | r = ByteReverseWord32(r);
|
---|
1549 | #endif
|
---|
1550 | IPERM(&l,&r);
|
---|
1551 |
|
---|
1552 | DesRawProcessBlock(&l, &r, des->key);
|
---|
1553 |
|
---|
1554 | FPERM(&l,&r);
|
---|
1555 | #ifdef LITTLE_ENDIAN_ORDER
|
---|
1556 | l = ByteReverseWord32(l);
|
---|
1557 | r = ByteReverseWord32(r);
|
---|
1558 | #endif
|
---|
1559 | XMEMCPY(out, &r, sizeof(r));
|
---|
1560 | XMEMCPY(out + sizeof(r), &l, sizeof(l));
|
---|
1561 | }
|
---|
1562 |
|
---|
1563 | static void Des3ProcessBlock(Des3* des, const byte* in, byte* out)
|
---|
1564 | {
|
---|
1565 | word32 l, r;
|
---|
1566 |
|
---|
1567 | XMEMCPY(&l, in, sizeof(l));
|
---|
1568 | XMEMCPY(&r, in + sizeof(l), sizeof(r));
|
---|
1569 | #ifdef LITTLE_ENDIAN_ORDER
|
---|
1570 | l = ByteReverseWord32(l);
|
---|
1571 | r = ByteReverseWord32(r);
|
---|
1572 | #endif
|
---|
1573 | IPERM(&l,&r);
|
---|
1574 |
|
---|
1575 | DesRawProcessBlock(&l, &r, des->key[0]);
|
---|
1576 | DesRawProcessBlock(&r, &l, des->key[1]);
|
---|
1577 | DesRawProcessBlock(&l, &r, des->key[2]);
|
---|
1578 |
|
---|
1579 | FPERM(&l,&r);
|
---|
1580 | #ifdef LITTLE_ENDIAN_ORDER
|
---|
1581 | l = ByteReverseWord32(l);
|
---|
1582 | r = ByteReverseWord32(r);
|
---|
1583 | #endif
|
---|
1584 | XMEMCPY(out, &r, sizeof(r));
|
---|
1585 | XMEMCPY(out + sizeof(r), &l, sizeof(l));
|
---|
1586 | }
|
---|
1587 |
|
---|
1588 | int wc_Des_CbcEncrypt(Des* des, byte* out, const byte* in, word32 sz)
|
---|
1589 | {
|
---|
1590 | word32 blocks = sz / DES_BLOCK_SIZE;
|
---|
1591 |
|
---|
1592 | while (blocks--) {
|
---|
1593 | xorbuf((byte*)des->reg, in, DES_BLOCK_SIZE);
|
---|
1594 | DesProcessBlock(des, (byte*)des->reg, (byte*)des->reg);
|
---|
1595 | XMEMCPY(out, des->reg, DES_BLOCK_SIZE);
|
---|
1596 |
|
---|
1597 | out += DES_BLOCK_SIZE;
|
---|
1598 | in += DES_BLOCK_SIZE;
|
---|
1599 | }
|
---|
1600 | return 0;
|
---|
1601 | }
|
---|
1602 |
|
---|
1603 | int wc_Des_CbcDecrypt(Des* des, byte* out, const byte* in, word32 sz)
|
---|
1604 | {
|
---|
1605 | word32 blocks = sz / DES_BLOCK_SIZE;
|
---|
1606 |
|
---|
1607 | while (blocks--) {
|
---|
1608 | XMEMCPY(des->tmp, in, DES_BLOCK_SIZE);
|
---|
1609 | DesProcessBlock(des, (byte*)des->tmp, out);
|
---|
1610 | xorbuf(out, (byte*)des->reg, DES_BLOCK_SIZE);
|
---|
1611 | XMEMCPY(des->reg, des->tmp, DES_BLOCK_SIZE);
|
---|
1612 |
|
---|
1613 | out += DES_BLOCK_SIZE;
|
---|
1614 | in += DES_BLOCK_SIZE;
|
---|
1615 | }
|
---|
1616 | return 0;
|
---|
1617 | }
|
---|
1618 |
|
---|
1619 | int wc_Des3_CbcEncrypt(Des3* des, byte* out, const byte* in, word32 sz)
|
---|
1620 | {
|
---|
1621 | word32 blocks;
|
---|
1622 |
|
---|
1623 | if (des == NULL || out == NULL || in == NULL) {
|
---|
1624 | return BAD_FUNC_ARG;
|
---|
1625 | }
|
---|
1626 |
|
---|
1627 | #ifdef WOLF_CRYPTO_CB
|
---|
1628 | if (des->devId != INVALID_DEVID) {
|
---|
1629 | int ret = wc_CryptoCb_Des3Encrypt(des, out, in, sz);
|
---|
1630 | if (ret != CRYPTOCB_UNAVAILABLE)
|
---|
1631 | return ret;
|
---|
1632 | /* fall-through when unavailable */
|
---|
1633 | }
|
---|
1634 | #endif
|
---|
1635 |
|
---|
1636 | #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_3DES)
|
---|
1637 | if (des->asyncDev.marker == WOLFSSL_ASYNC_MARKER_3DES &&
|
---|
1638 | sz >= WC_ASYNC_THRESH_DES3_CBC) {
|
---|
1639 | #if defined(HAVE_CAVIUM)
|
---|
1640 | return NitroxDes3CbcEncrypt(des, out, in, sz);
|
---|
1641 | #elif defined(HAVE_INTEL_QA)
|
---|
1642 | return IntelQaSymDes3CbcEncrypt(&des->asyncDev, out, in, sz,
|
---|
1643 | (const byte*)des->devKey, DES3_KEYLEN, (byte*)des->reg, DES3_IVLEN);
|
---|
1644 | #else /* WOLFSSL_ASYNC_CRYPT_TEST */
|
---|
1645 | if (wc_AsyncTestInit(&des->asyncDev, ASYNC_TEST_DES3_CBC_ENCRYPT)) {
|
---|
1646 | WC_ASYNC_TEST* testDev = &des->asyncDev.test;
|
---|
1647 | testDev->des.des = des;
|
---|
1648 | testDev->des.out = out;
|
---|
1649 | testDev->des.in = in;
|
---|
1650 | testDev->des.sz = sz;
|
---|
1651 | return WC_PENDING_E;
|
---|
1652 | }
|
---|
1653 | #endif
|
---|
1654 | }
|
---|
1655 | #endif /* WOLFSSL_ASYNC_CRYPT */
|
---|
1656 |
|
---|
1657 | blocks = sz / DES_BLOCK_SIZE;
|
---|
1658 | while (blocks--) {
|
---|
1659 | xorbuf((byte*)des->reg, in, DES_BLOCK_SIZE);
|
---|
1660 | Des3ProcessBlock(des, (byte*)des->reg, (byte*)des->reg);
|
---|
1661 | XMEMCPY(out, des->reg, DES_BLOCK_SIZE);
|
---|
1662 |
|
---|
1663 | out += DES_BLOCK_SIZE;
|
---|
1664 | in += DES_BLOCK_SIZE;
|
---|
1665 | }
|
---|
1666 | return 0;
|
---|
1667 | }
|
---|
1668 |
|
---|
1669 |
|
---|
1670 | int wc_Des3_CbcDecrypt(Des3* des, byte* out, const byte* in, word32 sz)
|
---|
1671 | {
|
---|
1672 | word32 blocks;
|
---|
1673 |
|
---|
1674 | if (des == NULL || out == NULL || in == NULL) {
|
---|
1675 | return BAD_FUNC_ARG;
|
---|
1676 | }
|
---|
1677 |
|
---|
1678 | #ifdef WOLF_CRYPTO_CB
|
---|
1679 | if (des->devId != INVALID_DEVID) {
|
---|
1680 | int ret = wc_CryptoCb_Des3Decrypt(des, out, in, sz);
|
---|
1681 | if (ret != CRYPTOCB_UNAVAILABLE)
|
---|
1682 | return ret;
|
---|
1683 | /* fall-through when unavailable */
|
---|
1684 | }
|
---|
1685 | #endif
|
---|
1686 |
|
---|
1687 | #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_3DES)
|
---|
1688 | if (des->asyncDev.marker == WOLFSSL_ASYNC_MARKER_3DES &&
|
---|
1689 | sz >= WC_ASYNC_THRESH_DES3_CBC) {
|
---|
1690 | #if defined(HAVE_CAVIUM)
|
---|
1691 | return NitroxDes3CbcDecrypt(des, out, in, sz);
|
---|
1692 | #elif defined(HAVE_INTEL_QA)
|
---|
1693 | return IntelQaSymDes3CbcDecrypt(&des->asyncDev, out, in, sz,
|
---|
1694 | (const byte*)des->devKey, DES3_KEYLEN, (byte*)des->reg, DES3_IVLEN);
|
---|
1695 | #else /* WOLFSSL_ASYNC_CRYPT_TEST */
|
---|
1696 | if (wc_AsyncTestInit(&des->asyncDev, ASYNC_TEST_DES3_CBC_DECRYPT)) {
|
---|
1697 | WC_ASYNC_TEST* testDev = &des->asyncDev.test;
|
---|
1698 | testDev->des.des = des;
|
---|
1699 | testDev->des.out = out;
|
---|
1700 | testDev->des.in = in;
|
---|
1701 | testDev->des.sz = sz;
|
---|
1702 | return WC_PENDING_E;
|
---|
1703 | }
|
---|
1704 | #endif
|
---|
1705 | }
|
---|
1706 | #endif /* WOLFSSL_ASYNC_CRYPT */
|
---|
1707 |
|
---|
1708 | blocks = sz / DES_BLOCK_SIZE;
|
---|
1709 | while (blocks--) {
|
---|
1710 | XMEMCPY(des->tmp, in, DES_BLOCK_SIZE);
|
---|
1711 | Des3ProcessBlock(des, (byte*)des->tmp, out);
|
---|
1712 | xorbuf(out, (byte*)des->reg, DES_BLOCK_SIZE);
|
---|
1713 | XMEMCPY(des->reg, des->tmp, DES_BLOCK_SIZE);
|
---|
1714 |
|
---|
1715 | out += DES_BLOCK_SIZE;
|
---|
1716 | in += DES_BLOCK_SIZE;
|
---|
1717 | }
|
---|
1718 | return 0;
|
---|
1719 | }
|
---|
1720 |
|
---|
1721 | #ifdef WOLFSSL_DES_ECB
|
---|
1722 | /* One block, compatibility only */
|
---|
1723 | int wc_Des_EcbEncrypt(Des* des, byte* out, const byte* in, word32 sz)
|
---|
1724 | {
|
---|
1725 | word32 blocks = sz / DES_BLOCK_SIZE;
|
---|
1726 |
|
---|
1727 | if (des == NULL || out == NULL || in == NULL) {
|
---|
1728 | return BAD_FUNC_ARG;
|
---|
1729 | }
|
---|
1730 |
|
---|
1731 | while (blocks--) {
|
---|
1732 | DesProcessBlock(des, in, out);
|
---|
1733 |
|
---|
1734 | out += DES_BLOCK_SIZE;
|
---|
1735 | in += DES_BLOCK_SIZE;
|
---|
1736 | }
|
---|
1737 | return 0;
|
---|
1738 | }
|
---|
1739 |
|
---|
1740 | int wc_Des3_EcbEncrypt(Des3* des, byte* out, const byte* in, word32 sz)
|
---|
1741 | {
|
---|
1742 | word32 blocks = sz / DES_BLOCK_SIZE;
|
---|
1743 |
|
---|
1744 | if (des == NULL || out == NULL || in == NULL) {
|
---|
1745 | return BAD_FUNC_ARG;
|
---|
1746 | }
|
---|
1747 |
|
---|
1748 | while (blocks--) {
|
---|
1749 | Des3ProcessBlock(des, in, out);
|
---|
1750 |
|
---|
1751 | out += DES_BLOCK_SIZE;
|
---|
1752 | in += DES_BLOCK_SIZE;
|
---|
1753 | }
|
---|
1754 | return 0;
|
---|
1755 | }
|
---|
1756 | #endif /* WOLFSSL_DES_ECB */
|
---|
1757 |
|
---|
1758 | #endif /* NEED_SOFT_DES */
|
---|
1759 |
|
---|
1760 |
|
---|
1761 | void wc_Des_SetIV(Des* des, const byte* iv)
|
---|
1762 | {
|
---|
1763 | if (des && iv)
|
---|
1764 | XMEMCPY(des->reg, iv, DES_BLOCK_SIZE);
|
---|
1765 | else if (des)
|
---|
1766 | XMEMSET(des->reg, 0, DES_BLOCK_SIZE);
|
---|
1767 | }
|
---|
1768 |
|
---|
1769 | int wc_Des3_SetIV(Des3* des, const byte* iv)
|
---|
1770 | {
|
---|
1771 | if (des == NULL) {
|
---|
1772 | return BAD_FUNC_ARG;
|
---|
1773 | }
|
---|
1774 | if (des && iv)
|
---|
1775 | XMEMCPY(des->reg, iv, DES_BLOCK_SIZE);
|
---|
1776 | else if (des)
|
---|
1777 | XMEMSET(des->reg, 0, DES_BLOCK_SIZE);
|
---|
1778 |
|
---|
1779 | return 0;
|
---|
1780 | }
|
---|
1781 |
|
---|
1782 |
|
---|
1783 | /* Initialize Des3 for use with async device */
|
---|
1784 | int wc_Des3Init(Des3* des3, void* heap, int devId)
|
---|
1785 | {
|
---|
1786 | int ret = 0;
|
---|
1787 | if (des3 == NULL)
|
---|
1788 | return BAD_FUNC_ARG;
|
---|
1789 |
|
---|
1790 | des3->heap = heap;
|
---|
1791 |
|
---|
1792 | #ifdef WOLF_CRYPTO_CB
|
---|
1793 | des3->devId = devId;
|
---|
1794 | des3->devCtx = NULL;
|
---|
1795 | #else
|
---|
1796 | (void)devId;
|
---|
1797 | #endif
|
---|
1798 |
|
---|
1799 | #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_3DES)
|
---|
1800 | ret = wolfAsync_DevCtxInit(&des3->asyncDev, WOLFSSL_ASYNC_MARKER_3DES,
|
---|
1801 | des3->heap, devId);
|
---|
1802 | #endif
|
---|
1803 |
|
---|
1804 | return ret;
|
---|
1805 | }
|
---|
1806 |
|
---|
1807 | /* Free Des3 from use with async device */
|
---|
1808 | void wc_Des3Free(Des3* des3)
|
---|
1809 | {
|
---|
1810 | if (des3 == NULL)
|
---|
1811 | return;
|
---|
1812 |
|
---|
1813 | #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_3DES)
|
---|
1814 | wolfAsync_DevCtxFree(&des3->asyncDev, WOLFSSL_ASYNC_MARKER_3DES);
|
---|
1815 | #endif /* WOLFSSL_ASYNC_CRYPT */
|
---|
1816 | #if defined(WOLF_CRYPTO_CB) || \
|
---|
1817 | (defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_3DES))
|
---|
1818 | ForceZero(des3->devKey, sizeof(des3->devKey));
|
---|
1819 | #endif
|
---|
1820 | }
|
---|
1821 |
|
---|
1822 | #endif /* WOLFSSL_TI_CRYPT */
|
---|
1823 | #endif /* HAVE_FIPS */
|
---|
1824 | #endif /* NO_DES3 */
|
---|