des3.c 60 KB

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