des3.c 58 KB

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