Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of wolfSSL by
des3.c
00001 /* des3.c 00002 * 00003 * Copyright (C) 2006-2016 wolfSSL Inc. 00004 * 00005 * This file is part of wolfSSL. 00006 * 00007 * wolfSSL is free software; you can redistribute it and/or modify 00008 * it under the terms of the GNU General Public License as published by 00009 * the Free Software Foundation; either version 2 of the License, or 00010 * (at your option) any later version. 00011 * 00012 * wolfSSL is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * GNU General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU General Public License 00018 * along with this program; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA 00020 */ 00021 00022 00023 #ifdef HAVE_CONFIG_H 00024 #include <config.h> 00025 #endif 00026 00027 #include <wolfssl/wolfcrypt/settings.h> 00028 00029 00030 #ifndef NO_DES3 00031 00032 #include <wolfssl/wolfcrypt/des3.h> 00033 00034 /* fips wrapper calls, user can call direct */ 00035 #ifdef HAVE_FIPS 00036 int wc_Des_SetKey(Des* des, const byte* key, const byte* iv, int dir) 00037 { 00038 return Des_SetKey(des, key, iv, dir); 00039 } 00040 int wc_Des3_SetKey(Des3* des, const byte* key, const byte* iv, int dir) 00041 { 00042 return Des3_SetKey_fips(des, key, iv, dir); 00043 } 00044 int wc_Des_CbcEncrypt(Des* des, byte* out, const byte* in, word32 sz) 00045 { 00046 return Des_CbcEncrypt(des, out, in, sz); 00047 } 00048 int wc_Des_CbcDecrypt(Des* des, byte* out, const byte* in, word32 sz) 00049 { 00050 return Des_CbcDecrypt(des, out, in, sz); 00051 } 00052 int wc_Des3_CbcEncrypt(Des3* des, byte* out, const byte* in, word32 sz) 00053 { 00054 return Des3_CbcEncrypt_fips(des, out, in, sz); 00055 } 00056 int wc_Des3_CbcDecrypt(Des3* des, byte* out, const byte* in, word32 sz) 00057 { 00058 return Des3_CbcDecrypt_fips(des, out, in, sz); 00059 } 00060 00061 #ifdef WOLFSSL_DES_ECB 00062 /* One block, compatibility only */ 00063 int wc_Des_EcbEncrypt(Des* des, byte* out, const byte* in, word32 sz) 00064 { 00065 return Des_EcbEncrypt(des, out, in, sz); 00066 } 00067 int wc_Des3_EcbEncrypt(Des3* des, byte* out, const byte* in, word32 sz) 00068 { 00069 return Des3_EcbEncrypt(des, out, in, sz); 00070 } 00071 #endif /* WOLFSSL_DES_ECB */ 00072 00073 void wc_Des_SetIV(Des* des, const byte* iv) 00074 { 00075 Des_SetIV(des, iv); 00076 } 00077 int wc_Des3_SetIV(Des3* des, const byte* iv) 00078 { 00079 return Des3_SetIV_fips(des, iv); 00080 } 00081 00082 int wc_Des3Init(Des3* des3, void* heap, int devId) 00083 { 00084 (void)des3; 00085 (void)heap; 00086 (void)devId; 00087 /* FIPS doesn't support: 00088 return Des3Init(des3, heap, devId); */ 00089 return 0; 00090 } 00091 void wc_Des3Free(Des3* des3) 00092 { 00093 (void)des3; 00094 /* FIPS doesn't support: 00095 Des3Free(des3); */ 00096 } 00097 00098 #else /* build without fips */ 00099 00100 00101 #if defined(WOLFSSL_TI_CRYPT) 00102 #include <wolfcrypt/src/port/ti/ti-des3.c> 00103 #else 00104 00105 #include <wolfssl/wolfcrypt/error-crypt.h> 00106 #include <wolfssl/wolfcrypt/logging.h> 00107 00108 #ifdef NO_INLINE 00109 #include <wolfssl/wolfcrypt/misc.h> 00110 #else 00111 #define WOLFSSL_MISC_INCLUDED 00112 #include <wolfcrypt/src/misc.c> 00113 #endif 00114 00115 00116 /* Hardware Acceleration */ 00117 #if defined(STM32F2_CRYPTO) || defined(STM32F4_CRYPTO) 00118 00119 /* 00120 * STM32F2/F4 hardware DES/3DES support through the standard 00121 * peripheral library. (See note in README). 00122 */ 00123 00124 int wc_Des_SetKey(Des* des, const byte* key, const byte* iv, int dir) 00125 { 00126 word32 *dkey = des->key; 00127 00128 (void)dir; 00129 00130 XMEMCPY(dkey, key, 8); 00131 #ifndef WOLFSSL_STM32_CUBEMX 00132 ByteReverseWords(dkey, dkey, 8); 00133 #endif 00134 00135 wc_Des_SetIV(des, iv); 00136 00137 return 0; 00138 } 00139 00140 int wc_Des3_SetKey(Des3* des, const byte* key, const byte* iv, int dir) 00141 { 00142 #ifndef WOLFSSL_STM32_CUBEMX 00143 word32 *dkey1 = des->key[0]; 00144 word32 *dkey2 = des->key[1]; 00145 word32 *dkey3 = des->key[2]; 00146 00147 (void)dir; 00148 00149 XMEMCPY(dkey1, key, 8); /* set key 1 */ 00150 XMEMCPY(dkey2, key + 8, 8); /* set key 2 */ 00151 XMEMCPY(dkey3, key + 16, 8); /* set key 3 */ 00152 00153 ByteReverseWords(dkey1, dkey1, 8); 00154 ByteReverseWords(dkey2, dkey2, 8); 00155 ByteReverseWords(dkey3, dkey3, 8); 00156 #else 00157 (void)dir; 00158 XMEMCPY(des->key[0], key, DES3_KEYLEN); /* CUBEMX wants keys in sequential memory */ 00159 #endif 00160 00161 return wc_Des3_SetIV(des, iv); 00162 } 00163 00164 static void DesCrypt(Des* des, byte* out, const byte* in, word32 sz, 00165 int dir, int mode) 00166 { 00167 #ifdef WOLFSSL_STM32_CUBEMX 00168 CRYP_HandleTypeDef hcryp; 00169 00170 XMEMSET(&hcryp, 0, sizeof(CRYP_HandleTypeDef)); 00171 hcryp.Instance = CRYP; 00172 hcryp.Init.KeySize = CRYP_KEYSIZE_128B; 00173 hcryp.Init.DataType = CRYP_DATATYPE_8B; 00174 hcryp.Init.pKey = (uint8_t*)des->key; 00175 hcryp.Init.pInitVect = (uint8_t*)des->reg; 00176 00177 HAL_CRYP_Init(&hcryp); 00178 00179 while (sz > 0) 00180 { 00181 /* if input and output same will overwrite input iv */ 00182 XMEMCPY(des->tmp, in + sz - DES_BLOCK_SIZE, DES_BLOCK_SIZE); 00183 00184 if (mode == DES_CBC) { 00185 if (dir == DES_ENCRYPTION) { 00186 HAL_CRYP_DESCBC_Encrypt(&hcryp, (uint8_t*)in, 00187 DES_BLOCK_SIZE, out, STM32_HAL_TIMEOUT); 00188 } 00189 else { 00190 HAL_CRYP_DESCBC_Decrypt(&hcryp, (uint8_t*)in, 00191 DES_BLOCK_SIZE, out, STM32_HAL_TIMEOUT); 00192 } 00193 } 00194 else { 00195 if (dir == DES_ENCRYPTION) { 00196 HAL_CRYP_DESECB_Encrypt(&hcryp, (uint8_t*)in, 00197 DES_BLOCK_SIZE, out, STM32_HAL_TIMEOUT); 00198 } 00199 else { 00200 HAL_CRYP_DESECB_Decrypt(&hcryp, (uint8_t*)in, 00201 DES_BLOCK_SIZE, out, STM32_HAL_TIMEOUT); 00202 } 00203 } 00204 00205 /* store iv for next call */ 00206 XMEMCPY(des->reg, des->tmp, DES_BLOCK_SIZE); 00207 00208 sz -= DES_BLOCK_SIZE; 00209 in += DES_BLOCK_SIZE; 00210 out += DES_BLOCK_SIZE; 00211 } 00212 00213 HAL_CRYP_DeInit(&hcryp); 00214 #else 00215 word32 *dkey, *iv; 00216 CRYP_InitTypeDef DES_CRYP_InitStructure; 00217 CRYP_KeyInitTypeDef DES_CRYP_KeyInitStructure; 00218 CRYP_IVInitTypeDef DES_CRYP_IVInitStructure; 00219 00220 dkey = des->key; 00221 iv = des->reg; 00222 00223 /* crypto structure initialization */ 00224 CRYP_KeyStructInit(&DES_CRYP_KeyInitStructure); 00225 CRYP_StructInit(&DES_CRYP_InitStructure); 00226 CRYP_IVStructInit(&DES_CRYP_IVInitStructure); 00227 00228 /* reset registers to their default values */ 00229 CRYP_DeInit(); 00230 00231 /* set direction, mode, and datatype */ 00232 if (dir == DES_ENCRYPTION) { 00233 DES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Encrypt; 00234 } else { /* DES_DECRYPTION */ 00235 DES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Decrypt; 00236 } 00237 00238 if (mode == DES_CBC) { 00239 DES_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_DES_CBC; 00240 } else { /* DES_ECB */ 00241 DES_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_DES_ECB; 00242 } 00243 00244 DES_CRYP_InitStructure.CRYP_DataType = CRYP_DataType_8b; 00245 CRYP_Init(&DES_CRYP_InitStructure); 00246 00247 /* load key into correct registers */ 00248 DES_CRYP_KeyInitStructure.CRYP_Key1Left = dkey[0]; 00249 DES_CRYP_KeyInitStructure.CRYP_Key1Right = dkey[1]; 00250 CRYP_KeyInit(&DES_CRYP_KeyInitStructure); 00251 00252 /* set iv */ 00253 ByteReverseWords(iv, iv, DES_BLOCK_SIZE); 00254 DES_CRYP_IVInitStructure.CRYP_IV0Left = iv[0]; 00255 DES_CRYP_IVInitStructure.CRYP_IV0Right = iv[1]; 00256 CRYP_IVInit(&DES_CRYP_IVInitStructure); 00257 00258 /* enable crypto processor */ 00259 CRYP_Cmd(ENABLE); 00260 00261 while (sz > 0) 00262 { 00263 /* flush IN/OUT FIFOs */ 00264 CRYP_FIFOFlush(); 00265 00266 /* if input and output same will overwrite input iv */ 00267 XMEMCPY(des->tmp, in + sz - DES_BLOCK_SIZE, DES_BLOCK_SIZE); 00268 00269 CRYP_DataIn(*(uint32_t*)&in[0]); 00270 CRYP_DataIn(*(uint32_t*)&in[4]); 00271 00272 /* wait until the complete message has been processed */ 00273 while(CRYP_GetFlagStatus(CRYP_FLAG_BUSY) != RESET) {} 00274 00275 *(uint32_t*)&out[0] = CRYP_DataOut(); 00276 *(uint32_t*)&out[4] = CRYP_DataOut(); 00277 00278 /* store iv for next call */ 00279 XMEMCPY(des->reg, des->tmp, DES_BLOCK_SIZE); 00280 00281 sz -= DES_BLOCK_SIZE; 00282 in += DES_BLOCK_SIZE; 00283 out += DES_BLOCK_SIZE; 00284 } 00285 00286 /* disable crypto processor */ 00287 CRYP_Cmd(DISABLE); 00288 #endif /* WOLFSSL_STM32_CUBEMX */ 00289 } 00290 00291 int wc_Des_CbcEncrypt(Des* des, byte* out, const byte* in, word32 sz) 00292 { 00293 DesCrypt(des, out, in, sz, DES_ENCRYPTION, DES_CBC); 00294 return 0; 00295 } 00296 00297 int wc_Des_CbcDecrypt(Des* des, byte* out, const byte* in, word32 sz) 00298 { 00299 DesCrypt(des, out, in, sz, DES_DECRYPTION, DES_CBC); 00300 return 0; 00301 } 00302 00303 int wc_Des_EcbEncrypt(Des* des, byte* out, const byte* in, word32 sz) 00304 { 00305 DesCrypt(des, out, in, sz, DES_ENCRYPTION, DES_ECB); 00306 return 0; 00307 } 00308 00309 static void Des3Crypt(Des3* des, byte* out, const byte* in, word32 sz, 00310 int dir) 00311 { 00312 #ifdef WOLFSSL_STM32_CUBEMX 00313 CRYP_HandleTypeDef hcryp; 00314 00315 XMEMSET(&hcryp, 0, sizeof(CRYP_HandleTypeDef)); 00316 hcryp.Instance = CRYP; 00317 hcryp.Init.KeySize = CRYP_KEYSIZE_128B; 00318 hcryp.Init.DataType = CRYP_DATATYPE_8B; 00319 hcryp.Init.pKey = (uint8_t*)des->key; 00320 hcryp.Init.pInitVect = (uint8_t*)des->reg; 00321 00322 HAL_CRYP_Init(&hcryp); 00323 00324 while (sz > 0) 00325 { 00326 if (dir == DES_ENCRYPTION) { 00327 HAL_CRYP_TDESCBC_Encrypt(&hcryp, (byte*)in, 00328 DES_BLOCK_SIZE, out, STM32_HAL_TIMEOUT); 00329 } 00330 else { 00331 HAL_CRYP_TDESCBC_Decrypt(&hcryp, (byte*)in, 00332 DES_BLOCK_SIZE, out, STM32_HAL_TIMEOUT); 00333 } 00334 00335 /* store iv for next call */ 00336 XMEMCPY(des->reg, out + sz - DES_BLOCK_SIZE, DES_BLOCK_SIZE); 00337 00338 sz -= DES_BLOCK_SIZE; 00339 in += DES_BLOCK_SIZE; 00340 out += DES_BLOCK_SIZE; 00341 } 00342 00343 HAL_CRYP_DeInit(&hcryp); 00344 #else 00345 word32 *dkey1, *dkey2, *dkey3, *iv; 00346 CRYP_InitTypeDef DES3_CRYP_InitStructure; 00347 CRYP_KeyInitTypeDef DES3_CRYP_KeyInitStructure; 00348 CRYP_IVInitTypeDef DES3_CRYP_IVInitStructure; 00349 00350 dkey1 = des->key[0]; 00351 dkey2 = des->key[1]; 00352 dkey3 = des->key[2]; 00353 iv = des->reg; 00354 00355 /* crypto structure initialization */ 00356 CRYP_KeyStructInit(&DES3_CRYP_KeyInitStructure); 00357 CRYP_StructInit(&DES3_CRYP_InitStructure); 00358 CRYP_IVStructInit(&DES3_CRYP_IVInitStructure); 00359 00360 /* reset registers to their default values */ 00361 CRYP_DeInit(); 00362 00363 /* set direction, mode, and datatype */ 00364 if (dir == DES_ENCRYPTION) { 00365 DES3_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Encrypt; 00366 } else { 00367 DES3_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Decrypt; 00368 } 00369 00370 DES3_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_TDES_CBC; 00371 DES3_CRYP_InitStructure.CRYP_DataType = CRYP_DataType_8b; 00372 CRYP_Init(&DES3_CRYP_InitStructure); 00373 00374 /* load key into correct registers */ 00375 DES3_CRYP_KeyInitStructure.CRYP_Key1Left = dkey1[0]; 00376 DES3_CRYP_KeyInitStructure.CRYP_Key1Right = dkey1[1]; 00377 DES3_CRYP_KeyInitStructure.CRYP_Key2Left = dkey2[0]; 00378 DES3_CRYP_KeyInitStructure.CRYP_Key2Right = dkey2[1]; 00379 DES3_CRYP_KeyInitStructure.CRYP_Key3Left = dkey3[0]; 00380 DES3_CRYP_KeyInitStructure.CRYP_Key3Right = dkey3[1]; 00381 CRYP_KeyInit(&DES3_CRYP_KeyInitStructure); 00382 00383 /* set iv */ 00384 ByteReverseWords(iv, iv, DES_BLOCK_SIZE); 00385 DES3_CRYP_IVInitStructure.CRYP_IV0Left = iv[0]; 00386 DES3_CRYP_IVInitStructure.CRYP_IV0Right = iv[1]; 00387 CRYP_IVInit(&DES3_CRYP_IVInitStructure); 00388 00389 /* enable crypto processor */ 00390 CRYP_Cmd(ENABLE); 00391 00392 while (sz > 0) 00393 { 00394 /* flush IN/OUT FIFOs */ 00395 CRYP_FIFOFlush(); 00396 00397 CRYP_DataIn(*(uint32_t*)&in[0]); 00398 CRYP_DataIn(*(uint32_t*)&in[4]); 00399 00400 /* wait until the complete message has been processed */ 00401 while(CRYP_GetFlagStatus(CRYP_FLAG_BUSY) != RESET) {} 00402 00403 *(uint32_t*)&out[0] = CRYP_DataOut(); 00404 *(uint32_t*)&out[4] = CRYP_DataOut(); 00405 00406 /* store iv for next call */ 00407 XMEMCPY(des->reg, out + sz - DES_BLOCK_SIZE, DES_BLOCK_SIZE); 00408 00409 sz -= DES_BLOCK_SIZE; 00410 in += DES_BLOCK_SIZE; 00411 out += DES_BLOCK_SIZE; 00412 } 00413 00414 /* disable crypto processor */ 00415 CRYP_Cmd(DISABLE); 00416 #endif /* WOLFSSL_STM32_CUBEMX */ 00417 } 00418 00419 int wc_Des3_CbcEncrypt(Des3* des, byte* out, const byte* in, word32 sz) 00420 { 00421 Des3Crypt(des, out, in, sz, DES_ENCRYPTION); 00422 return 0; 00423 } 00424 00425 int wc_Des3_CbcDecrypt(Des3* des, byte* out, const byte* in, word32 sz) 00426 { 00427 Des3Crypt(des, out, in, sz, DES_DECRYPTION); 00428 return 0; 00429 } 00430 00431 #elif defined(HAVE_COLDFIRE_SEC) 00432 00433 #include <wolfssl/ctaocrypt/types.h> 00434 00435 #include "sec.h" 00436 #include "mcf5475_sec.h" 00437 #include "mcf5475_siu.h" 00438 00439 #if defined (HAVE_THREADX) 00440 #include "memory_pools.h" 00441 extern TX_BYTE_POOL mp_ncached; /* Non Cached memory pool */ 00442 #endif 00443 00444 #define DES_BUFFER_SIZE (DES_BLOCK_SIZE * 64) 00445 static unsigned char *desBuffIn = NULL; 00446 static unsigned char *desBuffOut = NULL; 00447 static byte *secIV; 00448 static byte *secKey; 00449 static volatile SECdescriptorType *secDesc; 00450 00451 static wolfSSL_Mutex Mutex_DesSEC; 00452 00453 #define SEC_DESC_DES_CBC_ENCRYPT 0x20500010 00454 #define SEC_DESC_DES_CBC_DECRYPT 0x20400010 00455 #define SEC_DESC_DES3_CBC_ENCRYPT 0x20700010 00456 #define SEC_DESC_DES3_CBC_DECRYPT 0x20600010 00457 00458 #define DES_IVLEN 8 00459 #define DES_KEYLEN 8 00460 #define DES3_IVLEN 8 00461 #define DES3_KEYLEN 24 00462 00463 extern volatile unsigned char __MBAR[]; 00464 00465 static void wc_Des_Cbc(byte* out, const byte* in, word32 sz, 00466 byte *key, byte *iv, word32 desc) 00467 { 00468 #ifdef DEBUG_WOLFSSL 00469 int ret; int stat1,stat2; 00470 #endif 00471 int size; 00472 volatile int v; 00473 00474 wc_LockMutex(&Mutex_DesSEC) ; 00475 00476 secDesc->length1 = 0x0; 00477 secDesc->pointer1 = NULL; 00478 if((desc==SEC_DESC_DES_CBC_ENCRYPT)||(desc==SEC_DESC_DES_CBC_DECRYPT)){ 00479 secDesc->length2 = DES_IVLEN; 00480 secDesc->length3 = DES_KEYLEN; 00481 } else { 00482 secDesc->length2 = DES3_IVLEN; 00483 secDesc->length3 = DES3_KEYLEN; 00484 } 00485 secDesc->pointer2 = secIV; 00486 secDesc->pointer3 = secKey; 00487 secDesc->pointer4 = desBuffIn; 00488 secDesc->pointer5 = desBuffOut; 00489 secDesc->length6 = 0; 00490 secDesc->pointer6 = NULL; 00491 secDesc->length7 = 0x0; 00492 secDesc->pointer7 = NULL; 00493 secDesc->nextDescriptorPtr = NULL; 00494 00495 while(sz) { 00496 XMEMCPY(secIV, iv, secDesc->length2); 00497 if((sz%DES_BUFFER_SIZE) == sz) { 00498 size = sz; 00499 sz = 0; 00500 } else { 00501 size = DES_BUFFER_SIZE; 00502 sz -= DES_BUFFER_SIZE; 00503 } 00504 00505 XMEMCPY(desBuffIn, in, size); 00506 XMEMCPY(secKey, key, secDesc->length3); 00507 00508 secDesc->header = desc; 00509 secDesc->length4 = size; 00510 secDesc->length5 = size; 00511 /* Point SEC to the location of the descriptor */ 00512 MCF_SEC_FR0 = (uint32)secDesc; 00513 /* Initialize SEC and wait for encryption to complete */ 00514 MCF_SEC_CCCR0 = 0x0000001a; 00515 /* poll SISR to determine when channel is complete */ 00516 v=0; 00517 while((secDesc->header>> 24) != 0xff) { 00518 if(v++ > 1000)break; 00519 } 00520 00521 #ifdef DEBUG_WOLFSSL 00522 ret = MCF_SEC_SISRH; 00523 stat1 = MCF_SEC_DSR; 00524 stat2 = MCF_SEC_DISR; 00525 if(ret & 0xe0000000) { 00526 /* db_printf("Des_Cbc(%x):ISRH=%08x, DSR=%08x, DISR=%08x\n", desc, ret, stat1, stat2); */ 00527 } 00528 #endif 00529 00530 XMEMCPY(out, desBuffOut, size); 00531 00532 if ((desc==SEC_DESC_DES3_CBC_ENCRYPT)||(desc==SEC_DESC_DES_CBC_ENCRYPT)) { 00533 XMEMCPY((void*)iv, (void*)&(out[size-secDesc->length2]), secDesc->length2); 00534 } else { 00535 XMEMCPY((void*)iv, (void*)&(in[size-secDesc->length2]), secDesc->length2); 00536 } 00537 00538 in += size; 00539 out += size; 00540 00541 } 00542 wc_UnLockMutex(&Mutex_DesSEC) ; 00543 00544 } 00545 00546 00547 int wc_Des_CbcEncrypt(Des* des, byte* out, const byte* in, word32 sz) 00548 { 00549 wc_Des_Cbc(out, in, sz, (byte *)des->key, (byte *)des->reg, SEC_DESC_DES_CBC_ENCRYPT); 00550 return 0; 00551 } 00552 00553 int wc_Des_CbcDecrypt(Des* des, byte* out, const byte* in, word32 sz) 00554 { 00555 wc_Des_Cbc(out, in, sz, (byte *)des->key, (byte *)des->reg, SEC_DESC_DES_CBC_DECRYPT); 00556 return 0; 00557 } 00558 00559 int wc_Des3_CbcEncrypt(Des3* des3, byte* out, const byte* in, word32 sz) 00560 { 00561 wc_Des_Cbc(out, in, sz, (byte *)des3->key, (byte *)des3->reg, SEC_DESC_DES3_CBC_ENCRYPT); 00562 return 0; 00563 } 00564 00565 00566 int wc_Des3_CbcDecrypt(Des3* des3, byte* out, const byte* in, word32 sz) 00567 { 00568 wc_Des_Cbc(out, in, sz, (byte *)des3->key, (byte *)des3->reg, SEC_DESC_DES3_CBC_DECRYPT); 00569 return 0; 00570 } 00571 00572 static void setParity(byte *buf, int len) 00573 { 00574 int i, j; 00575 byte v; 00576 int bits; 00577 00578 for (i=0; i<len; i++) { 00579 v = buf[i] >> 1; 00580 buf[i] = v << 1; 00581 bits = 0; 00582 for (j=0; j<7; j++) { 00583 bits += (v&0x1); 00584 v = v >> 1; 00585 } 00586 buf[i] |= (1 - (bits&0x1)); 00587 } 00588 00589 } 00590 00591 int wc_Des_SetKey(Des* des, const byte* key, const byte* iv, int dir) 00592 { 00593 if(desBuffIn == NULL) { 00594 #if defined (HAVE_THREADX) 00595 int s1, s2, s3, s4, s5; 00596 s5 = tx_byte_allocate(&mp_ncached,(void *)&secDesc, 00597 sizeof(SECdescriptorType), TX_NO_WAIT); 00598 s1 = tx_byte_allocate(&mp_ncached,(void *)&desBuffIn, DES_BUFFER_SIZE, TX_NO_WAIT); 00599 s2 = tx_byte_allocate(&mp_ncached,(void *)&desBuffOut, DES_BUFFER_SIZE, TX_NO_WAIT); 00600 /* Don't know des or des3 to be used. Allocate larger buffers */ 00601 s3 = tx_byte_allocate(&mp_ncached,(void *)&secKey, DES3_KEYLEN,TX_NO_WAIT); 00602 s4 = tx_byte_allocate(&mp_ncached,(void *)&secIV, DES3_IVLEN, TX_NO_WAIT); 00603 #else 00604 #warning "Allocate non-Cache buffers" 00605 #endif 00606 00607 InitMutex(&Mutex_DesSEC); 00608 } 00609 00610 XMEMCPY(des->key, key, DES_KEYLEN); 00611 setParity((byte *)des->key, DES_KEYLEN); 00612 00613 if (iv) { 00614 XMEMCPY(des->reg, iv, DES_IVLEN); 00615 } else { 00616 XMEMSET(des->reg, 0x0, DES_IVLEN); 00617 } 00618 return 0; 00619 } 00620 00621 int wc_Des3_SetKey(Des3* des3, const byte* key, const byte* iv, int dir) 00622 { 00623 00624 if(desBuffIn == NULL) { 00625 #if defined (HAVE_THREADX) 00626 int s1, s2, s3, s4, s5; 00627 s5 = tx_byte_allocate(&mp_ncached,(void *)&secDesc, 00628 sizeof(SECdescriptorType), TX_NO_WAIT); 00629 s1 = tx_byte_allocate(&mp_ncached,(void *)&desBuffIn, DES_BUFFER_SIZE, TX_NO_WAIT); 00630 s2 = tx_byte_allocate(&mp_ncached,(void *)&desBuffOut, DES_BUFFER_SIZE, TX_NO_WAIT); 00631 s3 = tx_byte_allocate(&mp_ncached,(void *)&secKey, DES3_KEYLEN,TX_NO_WAIT); 00632 s4 = tx_byte_allocate(&mp_ncached,(void *)&secIV, DES3_IVLEN, TX_NO_WAIT); 00633 #else 00634 #warning "Allocate non-Cache buffers" 00635 #endif 00636 00637 InitMutex(&Mutex_DesSEC); 00638 } 00639 00640 XMEMCPY(des3->key[0], key, DES3_KEYLEN); 00641 setParity((byte *)des3->key[0], DES3_KEYLEN); 00642 00643 if (iv) { 00644 XMEMCPY(des3->reg, iv, DES3_IVLEN); 00645 } else { 00646 XMEMSET(des3->reg, 0x0, DES3_IVLEN); 00647 } 00648 return 0; 00649 00650 } 00651 #elif (defined FREESCALE_LTC_DES) 00652 00653 #include "fsl_ltc.h" 00654 int wc_Des_SetKey(Des* des, const byte* key, const byte* iv, int dir) 00655 { 00656 byte* dkey = (byte*)des->key; 00657 00658 XMEMCPY(dkey, key, 8); 00659 00660 wc_Des_SetIV(des, iv); 00661 00662 return 0; 00663 } 00664 00665 int wc_Des3_SetKey(Des3* des, const byte* key, const byte* iv, int dir) 00666 { 00667 int ret = 0; 00668 byte* dkey1 = (byte*)des->key[0]; 00669 byte* dkey2 = (byte*)des->key[1]; 00670 byte* dkey3 = (byte*)des->key[2]; 00671 00672 XMEMCPY(dkey1, key, 8); /* set key 1 */ 00673 XMEMCPY(dkey2, key + 8, 8); /* set key 2 */ 00674 XMEMCPY(dkey3, key + 16, 8); /* set key 3 */ 00675 00676 ret = wc_Des3_SetIV(des, iv); 00677 if (ret != 0) 00678 return ret; 00679 00680 return ret; 00681 } 00682 00683 int wc_Des_CbcEncrypt(Des* des, byte* out, const byte* in, word32 sz) 00684 { 00685 status_t status; 00686 status = LTC_DES_EncryptCbc(LTC_BASE, in, out, sz, (byte*)des->reg, (byte*)des->key); 00687 if (status == kStatus_Success) 00688 return 0; 00689 else 00690 return -1; 00691 } 00692 00693 int wc_Des_CbcDecrypt(Des* des, byte* out, const byte* in, word32 sz) 00694 { 00695 status_t status; 00696 status = LTC_DES_DecryptCbc(LTC_BASE, in, out, sz, (byte*)des->reg, (byte*)des->key); 00697 if (status == kStatus_Success) 00698 return 0; 00699 else 00700 return -1; 00701 } 00702 00703 int wc_Des3_CbcEncrypt(Des3* des, byte* out, const byte* in, word32 sz) 00704 { 00705 status_t status; 00706 status = LTC_DES3_EncryptCbc(LTC_BASE, 00707 in, 00708 out, 00709 sz, 00710 (byte*)des->reg, 00711 (byte*)des->key[0], 00712 (byte*)des->key[1], 00713 (byte*)des->key[2]); 00714 if (status == kStatus_Success) 00715 return 0; 00716 else 00717 return -1; 00718 } 00719 00720 int wc_Des3_CbcDecrypt(Des3* des, byte* out, const byte* in, word32 sz) 00721 { 00722 status_t status; 00723 status = LTC_DES3_DecryptCbc(LTC_BASE, 00724 in, 00725 out, 00726 sz, 00727 (byte*)des->reg, 00728 (byte*)des->key[0], 00729 (byte*)des->key[1], 00730 (byte*)des->key[2]); 00731 if (status == kStatus_Success) 00732 return 0; 00733 else 00734 return -1; 00735 00736 } 00737 #elif defined(FREESCALE_MMCAU) 00738 /* 00739 * Freescale mmCAU hardware DES/3DES support through the CAU/mmCAU library. 00740 * Documentation located in ColdFire/ColdFire+ CAU and Kinetis mmCAU 00741 * Software Library User Guide (See note in README). 00742 */ 00743 #include "fsl_mmcau.h" 00744 00745 const unsigned char parityLookup[128] = { 00746 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, 00747 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, 00748 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, 00749 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 00750 }; 00751 00752 int wc_Des_SetKey(Des* des, const byte* key, const byte* iv, int dir) 00753 { 00754 int i = 0; 00755 byte* dkey = (byte*)des->key; 00756 00757 XMEMCPY(dkey, key, 8); 00758 00759 wc_Des_SetIV(des, iv); 00760 00761 /* fix key parity, if needed */ 00762 for (i = 0; i < 8; i++) { 00763 dkey[i] = ((dkey[i] & 0xFE) | parityLookup[dkey[i] >> 1]); 00764 } 00765 00766 return 0; 00767 } 00768 00769 int wc_Des3_SetKey(Des3* des, const byte* key, const byte* iv, int dir) 00770 { 00771 int i = 0, ret = 0; 00772 byte* dkey1 = (byte*)des->key[0]; 00773 byte* dkey2 = (byte*)des->key[1]; 00774 byte* dkey3 = (byte*)des->key[2]; 00775 00776 XMEMCPY(dkey1, key, 8); /* set key 1 */ 00777 XMEMCPY(dkey2, key + 8, 8); /* set key 2 */ 00778 XMEMCPY(dkey3, key + 16, 8); /* set key 3 */ 00779 00780 ret = wc_Des3_SetIV(des, iv); 00781 if (ret != 0) 00782 return ret; 00783 00784 /* fix key parity if needed */ 00785 for (i = 0; i < 8; i++) 00786 dkey1[i] = ((dkey1[i] & 0xFE) | parityLookup[dkey1[i] >> 1]); 00787 00788 for (i = 0; i < 8; i++) 00789 dkey2[i] = ((dkey2[i] & 0xFE) | parityLookup[dkey2[i] >> 1]); 00790 00791 for (i = 0; i < 8; i++) 00792 dkey3[i] = ((dkey3[i] & 0xFE) | parityLookup[dkey3[i] >> 1]); 00793 00794 return ret; 00795 } 00796 00797 int wc_Des_CbcEncrypt(Des* des, byte* out, const byte* in, word32 sz) 00798 { 00799 int i; 00800 int offset = 0; 00801 int len = sz; 00802 int ret = 0; 00803 byte *iv; 00804 byte temp_block[DES_BLOCK_SIZE]; 00805 00806 iv = (byte*)des->reg; 00807 00808 while (len > 0) 00809 { 00810 XMEMCPY(temp_block, in + offset, DES_BLOCK_SIZE); 00811 00812 /* XOR block with IV for CBC */ 00813 for (i = 0; i < DES_BLOCK_SIZE; i++) 00814 temp_block[i] ^= iv[i]; 00815 00816 ret = wolfSSL_CryptHwMutexLock(); 00817 if(ret != 0) { 00818 return ret; 00819 } 00820 MMCAU_DES_EncryptEcb(temp_block, (byte*)des->key, out + offset); 00821 wolfSSL_CryptHwMutexUnLock(); 00822 00823 len -= DES_BLOCK_SIZE; 00824 offset += DES_BLOCK_SIZE; 00825 00826 /* store IV for next block */ 00827 XMEMCPY(iv, out + offset - DES_BLOCK_SIZE, DES_BLOCK_SIZE); 00828 } 00829 00830 return ret; 00831 } 00832 00833 int wc_Des_CbcDecrypt(Des* des, byte* out, const byte* in, word32 sz) 00834 { 00835 int i; 00836 int offset = 0; 00837 int len = sz; 00838 int ret = 0; 00839 byte* iv; 00840 byte temp_block[DES_BLOCK_SIZE]; 00841 00842 iv = (byte*)des->reg; 00843 00844 while (len > 0) 00845 { 00846 XMEMCPY(temp_block, in + offset, DES_BLOCK_SIZE); 00847 00848 ret = wolfSSL_CryptHwMutexLock(); 00849 if(ret != 0) { 00850 return ret; 00851 } 00852 MMCAU_DES_DecryptEcb(in + offset, (byte*)des->key, out + offset); 00853 wolfSSL_CryptHwMutexUnLock(); 00854 00855 /* XOR block with IV for CBC */ 00856 for (i = 0; i < DES_BLOCK_SIZE; i++) 00857 (out + offset)[i] ^= iv[i]; 00858 00859 /* store IV for next block */ 00860 XMEMCPY(iv, temp_block, DES_BLOCK_SIZE); 00861 00862 len -= DES_BLOCK_SIZE; 00863 offset += DES_BLOCK_SIZE; 00864 } 00865 00866 return ret; 00867 } 00868 00869 int wc_Des3_CbcEncrypt(Des3* des, byte* out, const byte* in, word32 sz) 00870 { 00871 int i; 00872 int offset = 0; 00873 int len = sz; 00874 int ret = 0; 00875 00876 byte *iv; 00877 byte temp_block[DES_BLOCK_SIZE]; 00878 00879 iv = (byte*)des->reg; 00880 00881 while (len > 0) 00882 { 00883 XMEMCPY(temp_block, in + offset, DES_BLOCK_SIZE); 00884 00885 /* XOR block with IV for CBC */ 00886 for (i = 0; i < DES_BLOCK_SIZE; i++) 00887 temp_block[i] ^= iv[i]; 00888 00889 ret = wolfSSL_CryptHwMutexLock(); 00890 if(ret != 0) { 00891 return ret; 00892 } 00893 MMCAU_DES_EncryptEcb(temp_block , (byte*)des->key[0], out + offset); 00894 MMCAU_DES_DecryptEcb(out + offset, (byte*)des->key[1], out + offset); 00895 MMCAU_DES_EncryptEcb(out + offset, (byte*)des->key[2], out + offset); 00896 wolfSSL_CryptHwMutexUnLock(); 00897 00898 len -= DES_BLOCK_SIZE; 00899 offset += DES_BLOCK_SIZE; 00900 00901 /* store IV for next block */ 00902 XMEMCPY(iv, out + offset - DES_BLOCK_SIZE, DES_BLOCK_SIZE); 00903 } 00904 00905 return ret; 00906 } 00907 00908 int wc_Des3_CbcDecrypt(Des3* des, byte* out, const byte* in, word32 sz) 00909 { 00910 int i; 00911 int offset = 0; 00912 int len = sz; 00913 int ret = 0; 00914 00915 byte* iv; 00916 byte temp_block[DES_BLOCK_SIZE]; 00917 00918 iv = (byte*)des->reg; 00919 00920 while (len > 0) 00921 { 00922 XMEMCPY(temp_block, in + offset, DES_BLOCK_SIZE); 00923 00924 ret = wolfSSL_CryptHwMutexLock(); 00925 if(ret != 0) { 00926 return ret; 00927 } 00928 MMCAU_DES_DecryptEcb(in + offset , (byte*)des->key[2], out + offset); 00929 MMCAU_DES_EncryptEcb(out + offset, (byte*)des->key[1], out + offset); 00930 MMCAU_DES_DecryptEcb(out + offset, (byte*)des->key[0], out + offset); 00931 wolfSSL_CryptHwMutexUnLock(); 00932 00933 /* XOR block with IV for CBC */ 00934 for (i = 0; i < DES_BLOCK_SIZE; i++) 00935 (out + offset)[i] ^= iv[i]; 00936 00937 /* store IV for next block */ 00938 XMEMCPY(iv, temp_block, DES_BLOCK_SIZE); 00939 00940 len -= DES_BLOCK_SIZE; 00941 offset += DES_BLOCK_SIZE; 00942 } 00943 00944 return ret; 00945 } 00946 00947 00948 #elif defined(WOLFSSL_PIC32MZ_CRYPT) 00949 00950 #include "wolfssl/wolfcrypt/port/pic32/pic32mz-crypt.h" 00951 00952 int wc_Des_SetKey(Des* des, const byte* key, const byte* iv, int dir) 00953 { 00954 word32 *dkey = des->key; 00955 word32 *dreg = des->reg; 00956 00957 XMEMCPY((byte *)dkey, (byte *)key, 8); 00958 ByteReverseWords(dkey, dkey, 8); 00959 XMEMCPY((byte *)dreg, (byte *)iv, 8); 00960 ByteReverseWords(dreg, dreg, 8); 00961 00962 return 0; 00963 } 00964 00965 int wc_Des3_SetKey(Des3* des, const byte* key, const byte* iv, int dir) 00966 { 00967 word32 *dkey1 = des->key[0]; 00968 word32 *dreg = des->reg; 00969 00970 XMEMCPY(dkey1, key, 24); 00971 ByteReverseWords(dkey1, dkey1, 24); 00972 XMEMCPY(dreg, iv, 8); 00973 ByteReverseWords(dreg, dreg, 8); 00974 00975 return 0; 00976 } 00977 00978 void DesCrypt(word32 *key, word32 *iv, byte* out, const byte* in, word32 sz, 00979 int dir, int algo, int cryptoalgo) 00980 { 00981 securityAssociation *sa_p; 00982 bufferDescriptor *bd_p; 00983 const byte *in_p, *in_l; 00984 byte *out_p, *out_l; 00985 volatile securityAssociation sa __attribute__((aligned (8))); 00986 volatile bufferDescriptor bd __attribute__((aligned (8))); 00987 volatile int k; 00988 00989 /* get uncached address */ 00990 00991 in_l = in; 00992 out_l = out; 00993 sa_p = KVA0_TO_KVA1(&sa); 00994 bd_p = KVA0_TO_KVA1(&bd); 00995 in_p = KVA0_TO_KVA1(in_l); 00996 out_p= KVA0_TO_KVA1(out_l); 00997 00998 if(PIC32MZ_IF_RAM(in_p)) 00999 XMEMCPY((void *)in_p, (void *)in, sz); 01000 XMEMSET((void *)out_p, 0, sz); 01001 01002 /* Set up the Security Association */ 01003 XMEMSET((byte *)KVA0_TO_KVA1(&sa), 0, sizeof(sa)); 01004 sa_p->SA_CTRL.ALGO = algo; 01005 sa_p->SA_CTRL.LNC = 1; 01006 sa_p->SA_CTRL.LOADIV = 1; 01007 sa_p->SA_CTRL.FB = 1; 01008 sa_p->SA_CTRL.ENCTYPE = dir; /* Encryption/Decryption */ 01009 sa_p->SA_CTRL.CRYPTOALGO = cryptoalgo; 01010 sa_p->SA_CTRL.KEYSIZE = 1; /* KEY is 192 bits */ 01011 XMEMCPY((byte *)KVA0_TO_KVA1(&sa.SA_ENCKEY[algo==PIC32_ALGO_TDES ? 2 : 6]), 01012 (byte *)key, algo==PIC32_ALGO_TDES ? 24 : 8); 01013 XMEMCPY((byte *)KVA0_TO_KVA1(&sa.SA_ENCIV[2]), (byte *)iv, 8); 01014 01015 XMEMSET((byte *)KVA0_TO_KVA1(&bd), 0, sizeof(bd)); 01016 /* Set up the Buffer Descriptor */ 01017 bd_p->BD_CTRL.BUFLEN = sz; 01018 bd_p->BD_CTRL.LIFM = 1; 01019 bd_p->BD_CTRL.SA_FETCH_EN = 1; 01020 bd_p->BD_CTRL.LAST_BD = 1; 01021 bd_p->BD_CTRL.DESC_EN = 1; 01022 01023 bd_p->SA_ADDR = (unsigned int)KVA_TO_PA(&sa); /* (unsigned int)sa_p; */ 01024 bd_p->SRCADDR = (unsigned int)KVA_TO_PA(in); /* (unsigned int)in_p; */ 01025 bd_p->DSTADDR = (unsigned int)KVA_TO_PA(out); /* (unsigned int)out_p; */ 01026 bd_p->NXTPTR = (unsigned int)KVA_TO_PA(&bd); 01027 bd_p->MSGLEN = sz; 01028 01029 /* Fire in the hole! */ 01030 CECON = 1 << 6; 01031 while (CECON); 01032 01033 /* Run the engine */ 01034 CEBDPADDR = (unsigned int)KVA_TO_PA(&bd); /* (unsigned int)bd_p; */ 01035 CEINTEN = 0x07; 01036 CECON = 0x27; 01037 01038 WAIT_ENGINE; 01039 01040 if((cryptoalgo == PIC32_CRYPTOALGO_CBC) || 01041 (cryptoalgo == PIC32_CRYPTOALGO_TCBC)|| 01042 (cryptoalgo == PIC32_CRYPTOALGO_RCBC)) { 01043 /* set iv for the next call */ 01044 if(dir == PIC32_ENCRYPTION) { 01045 XMEMCPY((void *)iv, (void*)&(out_p[sz-DES_IVLEN]), DES_IVLEN); 01046 } else { 01047 ByteReverseWords((word32*)iv, (word32 *)&(in_p[sz-DES_IVLEN]), 01048 DES_IVLEN); 01049 } 01050 } 01051 01052 ByteReverseWords((word32*)out, (word32 *)KVA0_TO_KVA1(out), sz); 01053 } 01054 01055 int wc_Des_CbcEncrypt(Des* des, byte* out, const byte* in, word32 sz) 01056 { 01057 DesCrypt(des->key, des->reg, out, in, sz, 01058 PIC32_ENCRYPTION, PIC32_ALGO_DES, PIC32_CRYPTOALGO_CBC ); 01059 return 0; 01060 } 01061 01062 int wc_Des_CbcDecrypt(Des* des, byte* out, const byte* in, word32 sz) 01063 { 01064 DesCrypt(des->key, des->reg, out, in, sz, 01065 PIC32_DECRYPTION, PIC32_ALGO_DES, PIC32_CRYPTOALGO_CBC); 01066 return 0; 01067 } 01068 01069 int wc_Des3_CbcEncrypt(Des3* des, byte* out, const byte* in, word32 sz) 01070 { 01071 DesCrypt(des->key[0], des->reg, out, in, sz, 01072 PIC32_ENCRYPTION, PIC32_ALGO_TDES, PIC32_CRYPTOALGO_TCBC); 01073 return 0; 01074 } 01075 01076 int wc_Des3_CbcDecrypt(Des3* des, byte* out, const byte* in, word32 sz) 01077 { 01078 DesCrypt(des->key[0], des->reg, out, in, sz, 01079 PIC32_DECRYPTION, PIC32_ALGO_TDES, PIC32_CRYPTOALGO_TCBC); 01080 return 0; 01081 } 01082 01083 #else 01084 #define NEED_SOFT_DES 01085 01086 #endif 01087 01088 01089 #ifdef NEED_SOFT_DES 01090 01091 /* permuted choice table (key) */ 01092 static const byte pc1[] = { 01093 57, 49, 41, 33, 25, 17, 9, 01094 1, 58, 50, 42, 34, 26, 18, 01095 10, 2, 59, 51, 43, 35, 27, 01096 19, 11, 3, 60, 52, 44, 36, 01097 01098 63, 55, 47, 39, 31, 23, 15, 01099 7, 62, 54, 46, 38, 30, 22, 01100 14, 6, 61, 53, 45, 37, 29, 01101 21, 13, 5, 28, 20, 12, 4 01102 }; 01103 01104 /* number left rotations of pc1 */ 01105 static const byte totrot[] = { 01106 1,2,4,6,8,10,12,14,15,17,19,21,23,25,27,28 01107 }; 01108 01109 /* permuted choice key (table) */ 01110 static const byte pc2[] = { 01111 14, 17, 11, 24, 1, 5, 01112 3, 28, 15, 6, 21, 10, 01113 23, 19, 12, 4, 26, 8, 01114 16, 7, 27, 20, 13, 2, 01115 41, 52, 31, 37, 47, 55, 01116 30, 40, 51, 45, 33, 48, 01117 44, 49, 39, 56, 34, 53, 01118 46, 42, 50, 36, 29, 32 01119 }; 01120 01121 /* End of DES-defined tables */ 01122 01123 /* bit 0 is left-most in byte */ 01124 static const int bytebit[] = { 01125 0200,0100,040,020,010,04,02,01 01126 }; 01127 01128 static const word32 Spbox[8][64] = { 01129 { 0x01010400,0x00000000,0x00010000,0x01010404, 01130 0x01010004,0x00010404,0x00000004,0x00010000, 01131 0x00000400,0x01010400,0x01010404,0x00000400, 01132 0x01000404,0x01010004,0x01000000,0x00000004, 01133 0x00000404,0x01000400,0x01000400,0x00010400, 01134 0x00010400,0x01010000,0x01010000,0x01000404, 01135 0x00010004,0x01000004,0x01000004,0x00010004, 01136 0x00000000,0x00000404,0x00010404,0x01000000, 01137 0x00010000,0x01010404,0x00000004,0x01010000, 01138 0x01010400,0x01000000,0x01000000,0x00000400, 01139 0x01010004,0x00010000,0x00010400,0x01000004, 01140 0x00000400,0x00000004,0x01000404,0x00010404, 01141 0x01010404,0x00010004,0x01010000,0x01000404, 01142 0x01000004,0x00000404,0x00010404,0x01010400, 01143 0x00000404,0x01000400,0x01000400,0x00000000, 01144 0x00010004,0x00010400,0x00000000,0x01010004}, 01145 { 0x80108020,0x80008000,0x00008000,0x00108020, 01146 0x00100000,0x00000020,0x80100020,0x80008020, 01147 0x80000020,0x80108020,0x80108000,0x80000000, 01148 0x80008000,0x00100000,0x00000020,0x80100020, 01149 0x00108000,0x00100020,0x80008020,0x00000000, 01150 0x80000000,0x00008000,0x00108020,0x80100000, 01151 0x00100020,0x80000020,0x00000000,0x00108000, 01152 0x00008020,0x80108000,0x80100000,0x00008020, 01153 0x00000000,0x00108020,0x80100020,0x00100000, 01154 0x80008020,0x80100000,0x80108000,0x00008000, 01155 0x80100000,0x80008000,0x00000020,0x80108020, 01156 0x00108020,0x00000020,0x00008000,0x80000000, 01157 0x00008020,0x80108000,0x00100000,0x80000020, 01158 0x00100020,0x80008020,0x80000020,0x00100020, 01159 0x00108000,0x00000000,0x80008000,0x00008020, 01160 0x80000000,0x80100020,0x80108020,0x00108000}, 01161 { 0x00000208,0x08020200,0x00000000,0x08020008, 01162 0x08000200,0x00000000,0x00020208,0x08000200, 01163 0x00020008,0x08000008,0x08000008,0x00020000, 01164 0x08020208,0x00020008,0x08020000,0x00000208, 01165 0x08000000,0x00000008,0x08020200,0x00000200, 01166 0x00020200,0x08020000,0x08020008,0x00020208, 01167 0x08000208,0x00020200,0x00020000,0x08000208, 01168 0x00000008,0x08020208,0x00000200,0x08000000, 01169 0x08020200,0x08000000,0x00020008,0x00000208, 01170 0x00020000,0x08020200,0x08000200,0x00000000, 01171 0x00000200,0x00020008,0x08020208,0x08000200, 01172 0x08000008,0x00000200,0x00000000,0x08020008, 01173 0x08000208,0x00020000,0x08000000,0x08020208, 01174 0x00000008,0x00020208,0x00020200,0x08000008, 01175 0x08020000,0x08000208,0x00000208,0x08020000, 01176 0x00020208,0x00000008,0x08020008,0x00020200}, 01177 { 0x00802001,0x00002081,0x00002081,0x00000080, 01178 0x00802080,0x00800081,0x00800001,0x00002001, 01179 0x00000000,0x00802000,0x00802000,0x00802081, 01180 0x00000081,0x00000000,0x00800080,0x00800001, 01181 0x00000001,0x00002000,0x00800000,0x00802001, 01182 0x00000080,0x00800000,0x00002001,0x00002080, 01183 0x00800081,0x00000001,0x00002080,0x00800080, 01184 0x00002000,0x00802080,0x00802081,0x00000081, 01185 0x00800080,0x00800001,0x00802000,0x00802081, 01186 0x00000081,0x00000000,0x00000000,0x00802000, 01187 0x00002080,0x00800080,0x00800081,0x00000001, 01188 0x00802001,0x00002081,0x00002081,0x00000080, 01189 0x00802081,0x00000081,0x00000001,0x00002000, 01190 0x00800001,0x00002001,0x00802080,0x00800081, 01191 0x00002001,0x00002080,0x00800000,0x00802001, 01192 0x00000080,0x00800000,0x00002000,0x00802080}, 01193 { 0x00000100,0x02080100,0x02080000,0x42000100, 01194 0x00080000,0x00000100,0x40000000,0x02080000, 01195 0x40080100,0x00080000,0x02000100,0x40080100, 01196 0x42000100,0x42080000,0x00080100,0x40000000, 01197 0x02000000,0x40080000,0x40080000,0x00000000, 01198 0x40000100,0x42080100,0x42080100,0x02000100, 01199 0x42080000,0x40000100,0x00000000,0x42000000, 01200 0x02080100,0x02000000,0x42000000,0x00080100, 01201 0x00080000,0x42000100,0x00000100,0x02000000, 01202 0x40000000,0x02080000,0x42000100,0x40080100, 01203 0x02000100,0x40000000,0x42080000,0x02080100, 01204 0x40080100,0x00000100,0x02000000,0x42080000, 01205 0x42080100,0x00080100,0x42000000,0x42080100, 01206 0x02080000,0x00000000,0x40080000,0x42000000, 01207 0x00080100,0x02000100,0x40000100,0x00080000, 01208 0x00000000,0x40080000,0x02080100,0x40000100}, 01209 { 0x20000010,0x20400000,0x00004000,0x20404010, 01210 0x20400000,0x00000010,0x20404010,0x00400000, 01211 0x20004000,0x00404010,0x00400000,0x20000010, 01212 0x00400010,0x20004000,0x20000000,0x00004010, 01213 0x00000000,0x00400010,0x20004010,0x00004000, 01214 0x00404000,0x20004010,0x00000010,0x20400010, 01215 0x20400010,0x00000000,0x00404010,0x20404000, 01216 0x00004010,0x00404000,0x20404000,0x20000000, 01217 0x20004000,0x00000010,0x20400010,0x00404000, 01218 0x20404010,0x00400000,0x00004010,0x20000010, 01219 0x00400000,0x20004000,0x20000000,0x00004010, 01220 0x20000010,0x20404010,0x00404000,0x20400000, 01221 0x00404010,0x20404000,0x00000000,0x20400010, 01222 0x00000010,0x00004000,0x20400000,0x00404010, 01223 0x00004000,0x00400010,0x20004010,0x00000000, 01224 0x20404000,0x20000000,0x00400010,0x20004010}, 01225 { 0x00200000,0x04200002,0x04000802,0x00000000, 01226 0x00000800,0x04000802,0x00200802,0x04200800, 01227 0x04200802,0x00200000,0x00000000,0x04000002, 01228 0x00000002,0x04000000,0x04200002,0x00000802, 01229 0x04000800,0x00200802,0x00200002,0x04000800, 01230 0x04000002,0x04200000,0x04200800,0x00200002, 01231 0x04200000,0x00000800,0x00000802,0x04200802, 01232 0x00200800,0x00000002,0x04000000,0x00200800, 01233 0x04000000,0x00200800,0x00200000,0x04000802, 01234 0x04000802,0x04200002,0x04200002,0x00000002, 01235 0x00200002,0x04000000,0x04000800,0x00200000, 01236 0x04200800,0x00000802,0x00200802,0x04200800, 01237 0x00000802,0x04000002,0x04200802,0x04200000, 01238 0x00200800,0x00000000,0x00000002,0x04200802, 01239 0x00000000,0x00200802,0x04200000,0x00000800, 01240 0x04000002,0x04000800,0x00000800,0x00200002}, 01241 { 0x10001040,0x00001000,0x00040000,0x10041040, 01242 0x10000000,0x10001040,0x00000040,0x10000000, 01243 0x00040040,0x10040000,0x10041040,0x00041000, 01244 0x10041000,0x00041040,0x00001000,0x00000040, 01245 0x10040000,0x10000040,0x10001000,0x00001040, 01246 0x00041000,0x00040040,0x10040040,0x10041000, 01247 0x00001040,0x00000000,0x00000000,0x10040040, 01248 0x10000040,0x10001000,0x00041040,0x00040000, 01249 0x00041040,0x00040000,0x10041000,0x00001000, 01250 0x00000040,0x10040040,0x00001000,0x00041040, 01251 0x10001000,0x00000040,0x10000040,0x10040000, 01252 0x10040040,0x10000000,0x00040000,0x10001040, 01253 0x00000000,0x10041040,0x00040040,0x10000040, 01254 0x10040000,0x10001000,0x10001040,0x00000000, 01255 0x10041040,0x00041000,0x00041000,0x00001040, 01256 0x00001040,0x00040040,0x10000000,0x10041000} 01257 }; 01258 01259 static INLINE void IPERM(word32* left, word32* right) 01260 { 01261 word32 work; 01262 01263 *right = rotlFixed(*right, 4U); 01264 work = (*left ^ *right) & 0xf0f0f0f0; 01265 *left ^= work; 01266 01267 *right = rotrFixed(*right^work, 20U); 01268 work = (*left ^ *right) & 0xffff0000; 01269 *left ^= work; 01270 01271 *right = rotrFixed(*right^work, 18U); 01272 work = (*left ^ *right) & 0x33333333; 01273 *left ^= work; 01274 01275 *right = rotrFixed(*right^work, 6U); 01276 work = (*left ^ *right) & 0x00ff00ff; 01277 *left ^= work; 01278 01279 *right = rotlFixed(*right^work, 9U); 01280 work = (*left ^ *right) & 0xaaaaaaaa; 01281 *left = rotlFixed(*left^work, 1U); 01282 *right ^= work; 01283 } 01284 01285 static INLINE void FPERM(word32* left, word32* right) 01286 { 01287 word32 work; 01288 01289 *right = rotrFixed(*right, 1U); 01290 work = (*left ^ *right) & 0xaaaaaaaa; 01291 *right ^= work; 01292 01293 *left = rotrFixed(*left^work, 9U); 01294 work = (*left ^ *right) & 0x00ff00ff; 01295 *right ^= work; 01296 01297 *left = rotlFixed(*left^work, 6U); 01298 work = (*left ^ *right) & 0x33333333; 01299 *right ^= work; 01300 01301 *left = rotlFixed(*left^work, 18U); 01302 work = (*left ^ *right) & 0xffff0000; 01303 *right ^= work; 01304 01305 *left = rotlFixed(*left^work, 20U); 01306 work = (*left ^ *right) & 0xf0f0f0f0; 01307 *right ^= work; 01308 01309 *left = rotrFixed(*left^work, 4U); 01310 } 01311 01312 static int DesSetKey(const byte* key, int dir, word32* out) 01313 { 01314 #define DES_KEY_BUFFER_SIZE (56+56+8) 01315 #ifdef WOLFSSL_SMALL_STACK 01316 byte* buffer = (byte*)XMALLOC(DES_KEY_BUFFER_SIZE, NULL, DYNAMIC_TYPE_TMP_BUFFER); 01317 01318 if (buffer == NULL) 01319 return MEMORY_E; 01320 #else 01321 byte buffer[DES_KEY_BUFFER_SIZE]; 01322 #endif 01323 01324 { 01325 byte* const pc1m = buffer; /* place to modify pc1 into */ 01326 byte* const pcr = pc1m + 56; /* place to rotate pc1 into */ 01327 byte* const ks = pcr + 56; 01328 register int i, j, l; 01329 int m; 01330 01331 for (j = 0; j < 56; j++) { /* convert pc1 to bits of key */ 01332 l = pc1[j] - 1; /* integer bit location */ 01333 m = l & 07; /* find bit */ 01334 pc1m[j] = (key[l >> 3] & /* find which key byte l is in */ 01335 bytebit[m]) /* and which bit of that byte */ 01336 ? 1 : 0; /* and store 1-bit result */ 01337 } 01338 01339 for (i = 0; i < 16; i++) { /* key chunk for each iteration */ 01340 XMEMSET(ks, 0, 8); /* Clear key schedule */ 01341 01342 for (j = 0; j < 56; j++) /* rotate pc1 the right amount */ 01343 pcr[j] = 01344 pc1m[(l = j + totrot[i]) < (j < 28 ? 28 : 56) ? l : l-28]; 01345 01346 /* rotate left and right halves independently */ 01347 for (j = 0; j < 48; j++) { /* select bits individually */ 01348 if (pcr[pc2[j] - 1]) { /* check bit that goes to ks[j] */ 01349 l= j % 6; /* mask it in if it's there */ 01350 ks[j/6] |= bytebit[l] >> 2; 01351 } 01352 } 01353 01354 /* Now convert to odd/even interleaved form for use in F */ 01355 out[2*i] = ((word32) ks[0] << 24) 01356 | ((word32) ks[2] << 16) 01357 | ((word32) ks[4] << 8) 01358 | ((word32) ks[6]); 01359 01360 out[2*i + 1] = ((word32) ks[1] << 24) 01361 | ((word32) ks[3] << 16) 01362 | ((word32) ks[5] << 8) 01363 | ((word32) ks[7]); 01364 } 01365 01366 /* reverse key schedule order */ 01367 if (dir == DES_DECRYPTION) { 01368 for (i = 0; i < 16; i += 2) { 01369 word32 swap = out[i]; 01370 out[i] = out[DES_KS_SIZE - 2 - i]; 01371 out[DES_KS_SIZE - 2 - i] = swap; 01372 01373 swap = out[i + 1]; 01374 out[i + 1] = out[DES_KS_SIZE - 1 - i]; 01375 out[DES_KS_SIZE - 1 - i] = swap; 01376 } 01377 } 01378 01379 #ifdef WOLFSSL_SMALL_STACK 01380 XFREE(buffer, NULL, DYNAMIC_TYPE_TMP_BUFFER); 01381 #endif 01382 } 01383 01384 return 0; 01385 } 01386 01387 int wc_Des_SetKey(Des* des, const byte* key, const byte* iv, int dir) 01388 { 01389 wc_Des_SetIV(des, iv); 01390 01391 return DesSetKey(key, dir, des->key); 01392 } 01393 01394 int wc_Des3_SetKey(Des3* des, const byte* key, const byte* iv, int dir) 01395 { 01396 int ret; 01397 01398 #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_3DES) 01399 if (des->asyncDev.marker == WOLFSSL_ASYNC_MARKER_3DES) { 01400 /* key_raw holds orignal key copy */ 01401 des->key_raw = key; 01402 des->iv_raw = iv; 01403 01404 /* continue on to set normal key for smaller DES operations */ 01405 } 01406 #endif /* WOLFSSL_ASYNC_CRYPT */ 01407 01408 ret = DesSetKey(key + (dir == DES_ENCRYPTION ? 0:16), dir, des->key[0]); 01409 if (ret != 0) 01410 return ret; 01411 01412 ret = DesSetKey(key + 8, !dir, des->key[1]); 01413 if (ret != 0) 01414 return ret; 01415 01416 ret = DesSetKey(key + (dir == DES_DECRYPTION ? 0:16), dir, des->key[2]); 01417 if (ret != 0) 01418 return ret; 01419 01420 return wc_Des3_SetIV(des, iv); 01421 } 01422 01423 static void DesRawProcessBlock(word32* lIn, word32* rIn, const word32* kptr) 01424 { 01425 word32 l = *lIn, r = *rIn, i; 01426 01427 for (i=0; i<8; i++) 01428 { 01429 word32 work = rotrFixed(r, 4U) ^ kptr[4*i+0]; 01430 l ^= Spbox[6][(work) & 0x3f] 01431 ^ Spbox[4][(work >> 8) & 0x3f] 01432 ^ Spbox[2][(work >> 16) & 0x3f] 01433 ^ Spbox[0][(work >> 24) & 0x3f]; 01434 work = r ^ kptr[4*i+1]; 01435 l ^= Spbox[7][(work) & 0x3f] 01436 ^ Spbox[5][(work >> 8) & 0x3f] 01437 ^ Spbox[3][(work >> 16) & 0x3f] 01438 ^ Spbox[1][(work >> 24) & 0x3f]; 01439 01440 work = rotrFixed(l, 4U) ^ kptr[4*i+2]; 01441 r ^= Spbox[6][(work) & 0x3f] 01442 ^ Spbox[4][(work >> 8) & 0x3f] 01443 ^ Spbox[2][(work >> 16) & 0x3f] 01444 ^ Spbox[0][(work >> 24) & 0x3f]; 01445 work = l ^ kptr[4*i+3]; 01446 r ^= Spbox[7][(work) & 0x3f] 01447 ^ Spbox[5][(work >> 8) & 0x3f] 01448 ^ Spbox[3][(work >> 16) & 0x3f] 01449 ^ Spbox[1][(work >> 24) & 0x3f]; 01450 } 01451 01452 *lIn = l; *rIn = r; 01453 } 01454 01455 static void DesProcessBlock(Des* des, const byte* in, byte* out) 01456 { 01457 word32 l, r; 01458 01459 XMEMCPY(&l, in, sizeof(l)); 01460 XMEMCPY(&r, in + sizeof(l), sizeof(r)); 01461 #ifdef LITTLE_ENDIAN_ORDER 01462 l = ByteReverseWord32(l); 01463 r = ByteReverseWord32(r); 01464 #endif 01465 IPERM(&l,&r); 01466 01467 DesRawProcessBlock(&l, &r, des->key); 01468 01469 FPERM(&l,&r); 01470 #ifdef LITTLE_ENDIAN_ORDER 01471 l = ByteReverseWord32(l); 01472 r = ByteReverseWord32(r); 01473 #endif 01474 XMEMCPY(out, &r, sizeof(r)); 01475 XMEMCPY(out + sizeof(r), &l, sizeof(l)); 01476 } 01477 01478 static void Des3ProcessBlock(Des3* des, const byte* in, byte* out) 01479 { 01480 word32 l, r; 01481 01482 XMEMCPY(&l, in, sizeof(l)); 01483 XMEMCPY(&r, in + sizeof(l), sizeof(r)); 01484 #ifdef LITTLE_ENDIAN_ORDER 01485 l = ByteReverseWord32(l); 01486 r = ByteReverseWord32(r); 01487 #endif 01488 IPERM(&l,&r); 01489 01490 DesRawProcessBlock(&l, &r, des->key[0]); 01491 DesRawProcessBlock(&r, &l, des->key[1]); 01492 DesRawProcessBlock(&l, &r, des->key[2]); 01493 01494 FPERM(&l,&r); 01495 #ifdef LITTLE_ENDIAN_ORDER 01496 l = ByteReverseWord32(l); 01497 r = ByteReverseWord32(r); 01498 #endif 01499 XMEMCPY(out, &r, sizeof(r)); 01500 XMEMCPY(out + sizeof(r), &l, sizeof(l)); 01501 } 01502 01503 int wc_Des_CbcEncrypt(Des* des, byte* out, const byte* in, word32 sz) 01504 { 01505 word32 blocks = sz / DES_BLOCK_SIZE; 01506 01507 while (blocks--) { 01508 xorbuf((byte*)des->reg, in, DES_BLOCK_SIZE); 01509 DesProcessBlock(des, (byte*)des->reg, (byte*)des->reg); 01510 XMEMCPY(out, des->reg, DES_BLOCK_SIZE); 01511 01512 out += DES_BLOCK_SIZE; 01513 in += DES_BLOCK_SIZE; 01514 } 01515 return 0; 01516 } 01517 01518 int wc_Des_CbcDecrypt(Des* des, byte* out, const byte* in, word32 sz) 01519 { 01520 word32 blocks = sz / DES_BLOCK_SIZE; 01521 01522 while (blocks--) { 01523 XMEMCPY(des->tmp, in, DES_BLOCK_SIZE); 01524 DesProcessBlock(des, (byte*)des->tmp, out); 01525 xorbuf(out, (byte*)des->reg, DES_BLOCK_SIZE); 01526 XMEMCPY(des->reg, des->tmp, DES_BLOCK_SIZE); 01527 01528 out += DES_BLOCK_SIZE; 01529 in += DES_BLOCK_SIZE; 01530 } 01531 return 0; 01532 } 01533 01534 int wc_Des3_CbcEncrypt(Des3* des, byte* out, const byte* in, word32 sz) 01535 { 01536 word32 blocks; 01537 01538 #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_3DES) 01539 if (des->asyncDev.marker == WOLFSSL_ASYNC_MARKER_3DES && 01540 sz >= WC_ASYNC_THRESH_DES3_CBC) { 01541 #if defined(HAVE_CAVIUM) 01542 return NitroxDes3CbcEncrypt(des, out, in, sz); 01543 #elif defined(HAVE_INTEL_QA) 01544 return IntelQaSymDes3CbcEncrypt(&des->asyncDev, out, in, sz, 01545 des->key_raw, DES3_KEYLEN, (byte*)des->iv_raw, DES3_IVLEN); 01546 #else /* WOLFSSL_ASYNC_CRYPT_TEST */ 01547 WC_ASYNC_TEST* testDev = &des->asyncDev.test; 01548 if (testDev->type == ASYNC_TEST_NONE) { 01549 testDev->type = ASYNC_TEST_DES3_CBC_ENCRYPT; 01550 testDev->des.des = des; 01551 testDev->des.out = out; 01552 testDev->des.in = in; 01553 testDev->des.sz = sz; 01554 return WC_PENDING_E; 01555 } 01556 #endif 01557 } 01558 #endif /* WOLFSSL_ASYNC_CRYPT */ 01559 01560 blocks = sz / DES_BLOCK_SIZE; 01561 while (blocks--) { 01562 xorbuf((byte*)des->reg, in, DES_BLOCK_SIZE); 01563 Des3ProcessBlock(des, (byte*)des->reg, (byte*)des->reg); 01564 XMEMCPY(out, des->reg, DES_BLOCK_SIZE); 01565 01566 out += DES_BLOCK_SIZE; 01567 in += DES_BLOCK_SIZE; 01568 } 01569 return 0; 01570 } 01571 01572 01573 int wc_Des3_CbcDecrypt(Des3* des, byte* out, const byte* in, word32 sz) 01574 { 01575 word32 blocks; 01576 01577 #if defined(WOLFSSL_ASYNC_CRYPT) 01578 if (des->asyncDev.marker == WOLFSSL_ASYNC_MARKER_3DES && 01579 sz >= WC_ASYNC_THRESH_DES3_CBC) { 01580 #if defined(HAVE_CAVIUM) 01581 return NitroxDes3CbcDecrypt(des, out, in, sz); 01582 #elif defined(HAVE_INTEL_QA) 01583 return IntelQaSymDes3CbcDecrypt(&des->asyncDev, out, in, sz, 01584 des->key_raw, DES3_KEYLEN, (byte*)des->iv_raw, DES3_IVLEN); 01585 #else /* WOLFSSL_ASYNC_CRYPT_TEST */ 01586 WC_ASYNC_TEST* testDev = &des->asyncDev.test; 01587 if (testDev->type == ASYNC_TEST_NONE) { 01588 testDev->type = ASYNC_TEST_DES3_CBC_DECRYPT; 01589 testDev->des.des = des; 01590 testDev->des.out = out; 01591 testDev->des.in = in; 01592 testDev->des.sz = sz; 01593 return WC_PENDING_E; 01594 } 01595 #endif 01596 } 01597 #endif /* WOLFSSL_ASYNC_CRYPT */ 01598 01599 blocks = sz / DES_BLOCK_SIZE; 01600 while (blocks--) { 01601 XMEMCPY(des->tmp, in, DES_BLOCK_SIZE); 01602 Des3ProcessBlock(des, (byte*)des->tmp, out); 01603 xorbuf(out, (byte*)des->reg, DES_BLOCK_SIZE); 01604 XMEMCPY(des->reg, des->tmp, DES_BLOCK_SIZE); 01605 01606 out += DES_BLOCK_SIZE; 01607 in += DES_BLOCK_SIZE; 01608 } 01609 return 0; 01610 } 01611 01612 #ifdef WOLFSSL_DES_ECB 01613 /* One block, compatibility only */ 01614 int wc_Des_EcbEncrypt(Des* des, byte* out, const byte* in, word32 sz) 01615 { 01616 word32 blocks = sz / DES_BLOCK_SIZE; 01617 01618 if (des == NULL || out == NULL || in == NULL) { 01619 return BAD_FUNC_ARG; 01620 } 01621 01622 while (blocks--) { 01623 DesProcessBlock(des, in, out); 01624 01625 out += DES_BLOCK_SIZE; 01626 in += DES_BLOCK_SIZE; 01627 } 01628 return 0; 01629 } 01630 01631 int wc_Des3_EcbEncrypt(Des3* des, byte* out, const byte* in, word32 sz) 01632 { 01633 word32 blocks = sz / DES_BLOCK_SIZE; 01634 /* printf("wc_Des3_EcbEncrypt(%016x, %016x, %d)\n", 01635 *(unsigned long *)in, *(unsigned long *)out, sz) ; */ 01636 01637 if (des == NULL || out == NULL || in == NULL) { 01638 return BAD_FUNC_ARG; 01639 } 01640 01641 while (blocks--) { 01642 Des3ProcessBlock(des, in, out); 01643 01644 out += DES_BLOCK_SIZE; 01645 in += DES_BLOCK_SIZE; 01646 } 01647 return 0; 01648 } 01649 #endif /* WOLFSSL_DES_ECB */ 01650 01651 #endif /* NEED_SOFT_DES */ 01652 01653 01654 void wc_Des_SetIV(Des* des, const byte* iv) 01655 { 01656 if (des && iv) 01657 XMEMCPY(des->reg, iv, DES_BLOCK_SIZE); 01658 else if (des) 01659 XMEMSET(des->reg, 0, DES_BLOCK_SIZE); 01660 } 01661 01662 int wc_Des3_SetIV(Des3* des, const byte* iv) 01663 { 01664 if (des && iv) 01665 XMEMCPY(des->reg, iv, DES_BLOCK_SIZE); 01666 else if (des) 01667 XMEMSET(des->reg, 0, DES_BLOCK_SIZE); 01668 01669 return 0; 01670 } 01671 01672 01673 /* Initialize Des3 for use with async device */ 01674 int wc_Des3Init(Des3* des3, void* heap, int devId) 01675 { 01676 int ret = 0; 01677 if (des3 == NULL) 01678 return BAD_FUNC_ARG; 01679 01680 des3->heap = heap; 01681 01682 #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_3DES) 01683 ret = wolfAsync_DevCtxInit(&des3->asyncDev, WOLFSSL_ASYNC_MARKER_3DES, 01684 des3->heap, devId); 01685 #else 01686 (void)devId; 01687 #endif 01688 01689 return ret; 01690 } 01691 01692 /* Free Des3 from use with async device */ 01693 void wc_Des3Free(Des3* des3) 01694 { 01695 if (des3 == NULL) 01696 return; 01697 01698 #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_3DES) 01699 wolfAsync_DevCtxFree(&des3->asyncDev, WOLFSSL_ASYNC_MARKER_3DES); 01700 #endif /* WOLFSSL_ASYNC_CRYPT */ 01701 } 01702 01703 #endif /* WOLFSSL_TI_CRYPT */ 01704 #endif /* HAVE_FIPS */ 01705 #endif /* NO_DES3 */ 01706
Generated on Tue Jul 12 2022 23:30:55 by
1.7.2
