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