des3.c 58 KB

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