des.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255
  1. /*
  2. * FIPS-46-3 compliant Triple-DES implementation
  3. *
  4. * Copyright (C) 2006-2010, Brainspark B.V.
  5. *
  6. * This file is part of PolarSSL (http://www.polarssl.org)
  7. * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
  8. *
  9. * All rights reserved.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License along
  22. * with this program; if not, write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  24. */
  25. /*
  26. * DES, on which TDES is based, was originally designed by Horst Feistel
  27. * at IBM in 1974, and was adopted as a standard by NIST (formerly NBS).
  28. *
  29. * http://csrc.nist.gov/publications/fips/fips46-3/fips46-3.pdf
  30. */
  31. /* Note: This file has been modified by ST's MCD Application Team, to support
  32. the hardware crypto engine embedded in STM32F417xx */
  33. #include "config.h"
  34. #if defined(POLARSSL_DES_C)
  35. #include "main.h"
  36. #include "polarssl/des.h"
  37. #ifdef USE_STM32F4XX_HW_CRYPTO /* use HW Crypto */
  38. CRYP_InitTypeDef DES3_CRYP_InitStructure;
  39. CRYP_KeyInitTypeDef DES3_CRYP_KeyInitStructure;
  40. CRYP_IVInitTypeDef DES3_CRYP_IVInitStructure;
  41. #endif /* USE_STM32F4XX_HW_CRYPTO */
  42. /*
  43. * 32-bit integer manipulation macros (big endian)
  44. */
  45. #ifndef GET_ULONG_BE
  46. #define GET_ULONG_BE(n,b,i) \
  47. { \
  48. (n) = ( (unsigned long) (b)[(i) ] << 24 ) \
  49. | ( (unsigned long) (b)[(i) + 1] << 16 ) \
  50. | ( (unsigned long) (b)[(i) + 2] << 8 ) \
  51. | ( (unsigned long) (b)[(i) + 3] ); \
  52. }
  53. #endif
  54. #ifndef PUT_ULONG_BE
  55. #define PUT_ULONG_BE(n,b,i) \
  56. { \
  57. (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
  58. (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
  59. (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
  60. (b)[(i) + 3] = (unsigned char) ( (n) ); \
  61. }
  62. #endif
  63. /*
  64. * Expanded DES S-boxes
  65. */
  66. static const unsigned long SB1[64] =
  67. {
  68. 0x01010400, 0x00000000, 0x00010000, 0x01010404,
  69. 0x01010004, 0x00010404, 0x00000004, 0x00010000,
  70. 0x00000400, 0x01010400, 0x01010404, 0x00000400,
  71. 0x01000404, 0x01010004, 0x01000000, 0x00000004,
  72. 0x00000404, 0x01000400, 0x01000400, 0x00010400,
  73. 0x00010400, 0x01010000, 0x01010000, 0x01000404,
  74. 0x00010004, 0x01000004, 0x01000004, 0x00010004,
  75. 0x00000000, 0x00000404, 0x00010404, 0x01000000,
  76. 0x00010000, 0x01010404, 0x00000004, 0x01010000,
  77. 0x01010400, 0x01000000, 0x01000000, 0x00000400,
  78. 0x01010004, 0x00010000, 0x00010400, 0x01000004,
  79. 0x00000400, 0x00000004, 0x01000404, 0x00010404,
  80. 0x01010404, 0x00010004, 0x01010000, 0x01000404,
  81. 0x01000004, 0x00000404, 0x00010404, 0x01010400,
  82. 0x00000404, 0x01000400, 0x01000400, 0x00000000,
  83. 0x00010004, 0x00010400, 0x00000000, 0x01010004
  84. };
  85. static const unsigned long SB2[64] =
  86. {
  87. 0x80108020, 0x80008000, 0x00008000, 0x00108020,
  88. 0x00100000, 0x00000020, 0x80100020, 0x80008020,
  89. 0x80000020, 0x80108020, 0x80108000, 0x80000000,
  90. 0x80008000, 0x00100000, 0x00000020, 0x80100020,
  91. 0x00108000, 0x00100020, 0x80008020, 0x00000000,
  92. 0x80000000, 0x00008000, 0x00108020, 0x80100000,
  93. 0x00100020, 0x80000020, 0x00000000, 0x00108000,
  94. 0x00008020, 0x80108000, 0x80100000, 0x00008020,
  95. 0x00000000, 0x00108020, 0x80100020, 0x00100000,
  96. 0x80008020, 0x80100000, 0x80108000, 0x00008000,
  97. 0x80100000, 0x80008000, 0x00000020, 0x80108020,
  98. 0x00108020, 0x00000020, 0x00008000, 0x80000000,
  99. 0x00008020, 0x80108000, 0x00100000, 0x80000020,
  100. 0x00100020, 0x80008020, 0x80000020, 0x00100020,
  101. 0x00108000, 0x00000000, 0x80008000, 0x00008020,
  102. 0x80000000, 0x80100020, 0x80108020, 0x00108000
  103. };
  104. static const unsigned long SB3[64] =
  105. {
  106. 0x00000208, 0x08020200, 0x00000000, 0x08020008,
  107. 0x08000200, 0x00000000, 0x00020208, 0x08000200,
  108. 0x00020008, 0x08000008, 0x08000008, 0x00020000,
  109. 0x08020208, 0x00020008, 0x08020000, 0x00000208,
  110. 0x08000000, 0x00000008, 0x08020200, 0x00000200,
  111. 0x00020200, 0x08020000, 0x08020008, 0x00020208,
  112. 0x08000208, 0x00020200, 0x00020000, 0x08000208,
  113. 0x00000008, 0x08020208, 0x00000200, 0x08000000,
  114. 0x08020200, 0x08000000, 0x00020008, 0x00000208,
  115. 0x00020000, 0x08020200, 0x08000200, 0x00000000,
  116. 0x00000200, 0x00020008, 0x08020208, 0x08000200,
  117. 0x08000008, 0x00000200, 0x00000000, 0x08020008,
  118. 0x08000208, 0x00020000, 0x08000000, 0x08020208,
  119. 0x00000008, 0x00020208, 0x00020200, 0x08000008,
  120. 0x08020000, 0x08000208, 0x00000208, 0x08020000,
  121. 0x00020208, 0x00000008, 0x08020008, 0x00020200
  122. };
  123. static const unsigned long SB4[64] =
  124. {
  125. 0x00802001, 0x00002081, 0x00002081, 0x00000080,
  126. 0x00802080, 0x00800081, 0x00800001, 0x00002001,
  127. 0x00000000, 0x00802000, 0x00802000, 0x00802081,
  128. 0x00000081, 0x00000000, 0x00800080, 0x00800001,
  129. 0x00000001, 0x00002000, 0x00800000, 0x00802001,
  130. 0x00000080, 0x00800000, 0x00002001, 0x00002080,
  131. 0x00800081, 0x00000001, 0x00002080, 0x00800080,
  132. 0x00002000, 0x00802080, 0x00802081, 0x00000081,
  133. 0x00800080, 0x00800001, 0x00802000, 0x00802081,
  134. 0x00000081, 0x00000000, 0x00000000, 0x00802000,
  135. 0x00002080, 0x00800080, 0x00800081, 0x00000001,
  136. 0x00802001, 0x00002081, 0x00002081, 0x00000080,
  137. 0x00802081, 0x00000081, 0x00000001, 0x00002000,
  138. 0x00800001, 0x00002001, 0x00802080, 0x00800081,
  139. 0x00002001, 0x00002080, 0x00800000, 0x00802001,
  140. 0x00000080, 0x00800000, 0x00002000, 0x00802080
  141. };
  142. static const unsigned long SB5[64] =
  143. {
  144. 0x00000100, 0x02080100, 0x02080000, 0x42000100,
  145. 0x00080000, 0x00000100, 0x40000000, 0x02080000,
  146. 0x40080100, 0x00080000, 0x02000100, 0x40080100,
  147. 0x42000100, 0x42080000, 0x00080100, 0x40000000,
  148. 0x02000000, 0x40080000, 0x40080000, 0x00000000,
  149. 0x40000100, 0x42080100, 0x42080100, 0x02000100,
  150. 0x42080000, 0x40000100, 0x00000000, 0x42000000,
  151. 0x02080100, 0x02000000, 0x42000000, 0x00080100,
  152. 0x00080000, 0x42000100, 0x00000100, 0x02000000,
  153. 0x40000000, 0x02080000, 0x42000100, 0x40080100,
  154. 0x02000100, 0x40000000, 0x42080000, 0x02080100,
  155. 0x40080100, 0x00000100, 0x02000000, 0x42080000,
  156. 0x42080100, 0x00080100, 0x42000000, 0x42080100,
  157. 0x02080000, 0x00000000, 0x40080000, 0x42000000,
  158. 0x00080100, 0x02000100, 0x40000100, 0x00080000,
  159. 0x00000000, 0x40080000, 0x02080100, 0x40000100
  160. };
  161. static const unsigned long SB6[64] =
  162. {
  163. 0x20000010, 0x20400000, 0x00004000, 0x20404010,
  164. 0x20400000, 0x00000010, 0x20404010, 0x00400000,
  165. 0x20004000, 0x00404010, 0x00400000, 0x20000010,
  166. 0x00400010, 0x20004000, 0x20000000, 0x00004010,
  167. 0x00000000, 0x00400010, 0x20004010, 0x00004000,
  168. 0x00404000, 0x20004010, 0x00000010, 0x20400010,
  169. 0x20400010, 0x00000000, 0x00404010, 0x20404000,
  170. 0x00004010, 0x00404000, 0x20404000, 0x20000000,
  171. 0x20004000, 0x00000010, 0x20400010, 0x00404000,
  172. 0x20404010, 0x00400000, 0x00004010, 0x20000010,
  173. 0x00400000, 0x20004000, 0x20000000, 0x00004010,
  174. 0x20000010, 0x20404010, 0x00404000, 0x20400000,
  175. 0x00404010, 0x20404000, 0x00000000, 0x20400010,
  176. 0x00000010, 0x00004000, 0x20400000, 0x00404010,
  177. 0x00004000, 0x00400010, 0x20004010, 0x00000000,
  178. 0x20404000, 0x20000000, 0x00400010, 0x20004010
  179. };
  180. static const unsigned long SB7[64] =
  181. {
  182. 0x00200000, 0x04200002, 0x04000802, 0x00000000,
  183. 0x00000800, 0x04000802, 0x00200802, 0x04200800,
  184. 0x04200802, 0x00200000, 0x00000000, 0x04000002,
  185. 0x00000002, 0x04000000, 0x04200002, 0x00000802,
  186. 0x04000800, 0x00200802, 0x00200002, 0x04000800,
  187. 0x04000002, 0x04200000, 0x04200800, 0x00200002,
  188. 0x04200000, 0x00000800, 0x00000802, 0x04200802,
  189. 0x00200800, 0x00000002, 0x04000000, 0x00200800,
  190. 0x04000000, 0x00200800, 0x00200000, 0x04000802,
  191. 0x04000802, 0x04200002, 0x04200002, 0x00000002,
  192. 0x00200002, 0x04000000, 0x04000800, 0x00200000,
  193. 0x04200800, 0x00000802, 0x00200802, 0x04200800,
  194. 0x00000802, 0x04000002, 0x04200802, 0x04200000,
  195. 0x00200800, 0x00000000, 0x00000002, 0x04200802,
  196. 0x00000000, 0x00200802, 0x04200000, 0x00000800,
  197. 0x04000002, 0x04000800, 0x00000800, 0x00200002
  198. };
  199. static const unsigned long SB8[64] =
  200. {
  201. 0x10001040, 0x00001000, 0x00040000, 0x10041040,
  202. 0x10000000, 0x10001040, 0x00000040, 0x10000000,
  203. 0x00040040, 0x10040000, 0x10041040, 0x00041000,
  204. 0x10041000, 0x00041040, 0x00001000, 0x00000040,
  205. 0x10040000, 0x10000040, 0x10001000, 0x00001040,
  206. 0x00041000, 0x00040040, 0x10040040, 0x10041000,
  207. 0x00001040, 0x00000000, 0x00000000, 0x10040040,
  208. 0x10000040, 0x10001000, 0x00041040, 0x00040000,
  209. 0x00041040, 0x00040000, 0x10041000, 0x00001000,
  210. 0x00000040, 0x10040040, 0x00001000, 0x00041040,
  211. 0x10001000, 0x00000040, 0x10000040, 0x10040000,
  212. 0x10040040, 0x10000000, 0x00040000, 0x10001040,
  213. 0x00000000, 0x10041040, 0x00040040, 0x10000040,
  214. 0x10040000, 0x10001000, 0x10001040, 0x00000000,
  215. 0x10041040, 0x00041000, 0x00041000, 0x00001040,
  216. 0x00001040, 0x00040040, 0x10000000, 0x10041000
  217. };
  218. /*
  219. * PC1: left and right halves bit-swap
  220. */
  221. static const unsigned long LHs[16] =
  222. {
  223. 0x00000000, 0x00000001, 0x00000100, 0x00000101,
  224. 0x00010000, 0x00010001, 0x00010100, 0x00010101,
  225. 0x01000000, 0x01000001, 0x01000100, 0x01000101,
  226. 0x01010000, 0x01010001, 0x01010100, 0x01010101
  227. };
  228. static const unsigned long RHs[16] =
  229. {
  230. 0x00000000, 0x01000000, 0x00010000, 0x01010000,
  231. 0x00000100, 0x01000100, 0x00010100, 0x01010100,
  232. 0x00000001, 0x01000001, 0x00010001, 0x01010001,
  233. 0x00000101, 0x01000101, 0x00010101, 0x01010101,
  234. };
  235. /*
  236. * Initial Permutation macro
  237. */
  238. #define DES_IP(X,Y) \
  239. { \
  240. T = ((X >> 4) ^ Y) & 0x0F0F0F0F; Y ^= T; X ^= (T << 4); \
  241. T = ((X >> 16) ^ Y) & 0x0000FFFF; Y ^= T; X ^= (T << 16); \
  242. T = ((Y >> 2) ^ X) & 0x33333333; X ^= T; Y ^= (T << 2); \
  243. T = ((Y >> 8) ^ X) & 0x00FF00FF; X ^= T; Y ^= (T << 8); \
  244. Y = ((Y << 1) | (Y >> 31)) & 0xFFFFFFFF; \
  245. T = (X ^ Y) & 0xAAAAAAAA; Y ^= T; X ^= T; \
  246. X = ((X << 1) | (X >> 31)) & 0xFFFFFFFF; \
  247. }
  248. /*
  249. * Final Permutation macro
  250. */
  251. #define DES_FP(X,Y) \
  252. { \
  253. X = ((X << 31) | (X >> 1)) & 0xFFFFFFFF; \
  254. T = (X ^ Y) & 0xAAAAAAAA; X ^= T; Y ^= T; \
  255. Y = ((Y << 31) | (Y >> 1)) & 0xFFFFFFFF; \
  256. T = ((Y >> 8) ^ X) & 0x00FF00FF; X ^= T; Y ^= (T << 8); \
  257. T = ((Y >> 2) ^ X) & 0x33333333; X ^= T; Y ^= (T << 2); \
  258. T = ((X >> 16) ^ Y) & 0x0000FFFF; Y ^= T; X ^= (T << 16); \
  259. T = ((X >> 4) ^ Y) & 0x0F0F0F0F; Y ^= T; X ^= (T << 4); \
  260. }
  261. /*
  262. * DES round macro
  263. */
  264. #define DES_ROUND(X,Y) \
  265. { \
  266. T = *SK++ ^ X; \
  267. Y ^= SB8[ (T ) & 0x3F ] ^ \
  268. SB6[ (T >> 8) & 0x3F ] ^ \
  269. SB4[ (T >> 16) & 0x3F ] ^ \
  270. SB2[ (T >> 24) & 0x3F ]; \
  271. \
  272. T = *SK++ ^ ((X << 28) | (X >> 4)); \
  273. Y ^= SB7[ (T ) & 0x3F ] ^ \
  274. SB5[ (T >> 8) & 0x3F ] ^ \
  275. SB3[ (T >> 16) & 0x3F ] ^ \
  276. SB1[ (T >> 24) & 0x3F ]; \
  277. }
  278. #define SWAP(a,b) { unsigned long t = a; a = b; b = t; t = 0; }
  279. static const unsigned char odd_parity_table[128] = { 1, 2, 4, 7, 8,
  280. 11, 13, 14, 16, 19, 21, 22, 25, 26, 28, 31, 32, 35, 37, 38, 41, 42, 44,
  281. 47, 49, 50, 52, 55, 56, 59, 61, 62, 64, 67, 69, 70, 73, 74, 76, 79, 81,
  282. 82, 84, 87, 88, 91, 93, 94, 97, 98, 100, 103, 104, 107, 109, 110, 112,
  283. 115, 117, 118, 121, 122, 124, 127, 128, 131, 133, 134, 137, 138, 140,
  284. 143, 145, 146, 148, 151, 152, 155, 157, 158, 161, 162, 164, 167, 168,
  285. 171, 173, 174, 176, 179, 181, 182, 185, 186, 188, 191, 193, 194, 196,
  286. 199, 200, 203, 205, 206, 208, 211, 213, 214, 217, 218, 220, 223, 224,
  287. 227, 229, 230, 233, 234, 236, 239, 241, 242, 244, 247, 248, 251, 253,
  288. 254 };
  289. void des_key_set_parity( unsigned char key[DES_KEY_SIZE] )
  290. {
  291. int i;
  292. for( i = 0; i < DES_KEY_SIZE; i++ )
  293. key[i] = odd_parity_table[key[i] / 2];
  294. }
  295. /*
  296. * Check the given key's parity, returns 1 on failure, 0 on SUCCESS
  297. */
  298. int des_key_check_key_parity( const unsigned char key[DES_KEY_SIZE] )
  299. {
  300. int i;
  301. for( i = 0; i < DES_KEY_SIZE; i++ )
  302. if ( key[i] != odd_parity_table[key[i] / 2] )
  303. return( 1 );
  304. return( 0 );
  305. }
  306. /*
  307. * Table of weak and semi-weak keys
  308. *
  309. * Source: http://en.wikipedia.org/wiki/Weak_key
  310. *
  311. * Weak:
  312. * Alternating ones + zeros (0x0101010101010101)
  313. * Alternating 'F' + 'E' (0xFEFEFEFEFEFEFEFE)
  314. * '0xE0E0E0E0F1F1F1F1'
  315. * '0x1F1F1F1F0E0E0E0E'
  316. *
  317. * Semi-weak:
  318. * 0x011F011F010E010E and 0x1F011F010E010E01
  319. * 0x01E001E001F101F1 and 0xE001E001F101F101
  320. * 0x01FE01FE01FE01FE and 0xFE01FE01FE01FE01
  321. * 0x1FE01FE00EF10EF1 and 0xE01FE01FF10EF10E
  322. * 0x1FFE1FFE0EFE0EFE and 0xFE1FFE1FFE0EFE0E
  323. * 0xE0FEE0FEF1FEF1FE and 0xFEE0FEE0FEF1FEF1
  324. *
  325. */
  326. #define WEAK_KEY_COUNT 16
  327. static const unsigned char weak_key_table[WEAK_KEY_COUNT][DES_KEY_SIZE] =
  328. {
  329. { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 },
  330. { 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE },
  331. { 0x1F, 0x1F, 0x1F, 0x1F, 0x0E, 0x0E, 0x0E, 0x0E },
  332. { 0xE0, 0xE0, 0xE0, 0xE0, 0xF1, 0xF1, 0xF1, 0xF1 },
  333. { 0x01, 0x1F, 0x01, 0x1F, 0x01, 0x0E, 0x01, 0x0E },
  334. { 0x1F, 0x01, 0x1F, 0x01, 0x0E, 0x01, 0x0E, 0x01 },
  335. { 0x01, 0xE0, 0x01, 0xE0, 0x01, 0xF1, 0x01, 0xF1 },
  336. { 0xE0, 0x01, 0xE0, 0x01, 0xF1, 0x01, 0xF1, 0x01 },
  337. { 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE },
  338. { 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01 },
  339. { 0x1F, 0xE0, 0x1F, 0xE0, 0x0E, 0xF1, 0x0E, 0xF1 },
  340. { 0xE0, 0x1F, 0xE0, 0x1F, 0xF1, 0x0E, 0xF1, 0x0E },
  341. { 0x1F, 0xFE, 0x1F, 0xFE, 0x0E, 0xFE, 0x0E, 0xFE },
  342. { 0xFE, 0x1F, 0xFE, 0x1F, 0xFE, 0x0E, 0xFE, 0x0E },
  343. { 0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1, 0xFE },
  344. { 0xFE, 0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1 }
  345. };
  346. int des_key_check_weak( const unsigned char key[DES_KEY_SIZE] )
  347. {
  348. int i;
  349. for( i = 0; i < WEAK_KEY_COUNT; i++ )
  350. if( memcmp( weak_key_table[i], key, DES_KEY_SIZE) == 0)
  351. return( 1 );
  352. return( 0 );
  353. }
  354. static void des_setkey( unsigned long SK[32], const unsigned char key[DES_KEY_SIZE] )
  355. {
  356. int i;
  357. unsigned long X, Y, T;
  358. GET_ULONG_BE( X, key, 0 );
  359. GET_ULONG_BE( Y, key, 4 );
  360. /*
  361. * Permuted Choice 1
  362. */
  363. T = ((Y >> 4) ^ X) & 0x0F0F0F0F; X ^= T; Y ^= (T << 4);
  364. T = ((Y ) ^ X) & 0x10101010; X ^= T; Y ^= (T );
  365. X = (LHs[ (X ) & 0xF] << 3) | (LHs[ (X >> 8) & 0xF ] << 2)
  366. | (LHs[ (X >> 16) & 0xF] << 1) | (LHs[ (X >> 24) & 0xF ] )
  367. | (LHs[ (X >> 5) & 0xF] << 7) | (LHs[ (X >> 13) & 0xF ] << 6)
  368. | (LHs[ (X >> 21) & 0xF] << 5) | (LHs[ (X >> 29) & 0xF ] << 4);
  369. Y = (RHs[ (Y >> 1) & 0xF] << 3) | (RHs[ (Y >> 9) & 0xF ] << 2)
  370. | (RHs[ (Y >> 17) & 0xF] << 1) | (RHs[ (Y >> 25) & 0xF ] )
  371. | (RHs[ (Y >> 4) & 0xF] << 7) | (RHs[ (Y >> 12) & 0xF ] << 6)
  372. | (RHs[ (Y >> 20) & 0xF] << 5) | (RHs[ (Y >> 28) & 0xF ] << 4);
  373. X &= 0x0FFFFFFF;
  374. Y &= 0x0FFFFFFF;
  375. /*
  376. * calculate subkeys
  377. */
  378. for( i = 0; i < 16; i++ )
  379. {
  380. if( i < 2 || i == 8 || i == 15 )
  381. {
  382. X = ((X << 1) | (X >> 27)) & 0x0FFFFFFF;
  383. Y = ((Y << 1) | (Y >> 27)) & 0x0FFFFFFF;
  384. }
  385. else
  386. {
  387. X = ((X << 2) | (X >> 26)) & 0x0FFFFFFF;
  388. Y = ((Y << 2) | (Y >> 26)) & 0x0FFFFFFF;
  389. }
  390. *SK++ = ((X << 4) & 0x24000000) | ((X << 28) & 0x10000000)
  391. | ((X << 14) & 0x08000000) | ((X << 18) & 0x02080000)
  392. | ((X << 6) & 0x01000000) | ((X << 9) & 0x00200000)
  393. | ((X >> 1) & 0x00100000) | ((X << 10) & 0x00040000)
  394. | ((X << 2) & 0x00020000) | ((X >> 10) & 0x00010000)
  395. | ((Y >> 13) & 0x00002000) | ((Y >> 4) & 0x00001000)
  396. | ((Y << 6) & 0x00000800) | ((Y >> 1) & 0x00000400)
  397. | ((Y >> 14) & 0x00000200) | ((Y ) & 0x00000100)
  398. | ((Y >> 5) & 0x00000020) | ((Y >> 10) & 0x00000010)
  399. | ((Y >> 3) & 0x00000008) | ((Y >> 18) & 0x00000004)
  400. | ((Y >> 26) & 0x00000002) | ((Y >> 24) & 0x00000001);
  401. *SK++ = ((X << 15) & 0x20000000) | ((X << 17) & 0x10000000)
  402. | ((X << 10) & 0x08000000) | ((X << 22) & 0x04000000)
  403. | ((X >> 2) & 0x02000000) | ((X << 1) & 0x01000000)
  404. | ((X << 16) & 0x00200000) | ((X << 11) & 0x00100000)
  405. | ((X << 3) & 0x00080000) | ((X >> 6) & 0x00040000)
  406. | ((X << 15) & 0x00020000) | ((X >> 4) & 0x00010000)
  407. | ((Y >> 2) & 0x00002000) | ((Y << 8) & 0x00001000)
  408. | ((Y >> 14) & 0x00000808) | ((Y >> 9) & 0x00000400)
  409. | ((Y ) & 0x00000200) | ((Y << 7) & 0x00000100)
  410. | ((Y >> 7) & 0x00000020) | ((Y >> 3) & 0x00000011)
  411. | ((Y << 2) & 0x00000004) | ((Y >> 21) & 0x00000002);
  412. }
  413. }
  414. /*
  415. * DES key schedule (56-bit, encryption)
  416. */
  417. int des_setkey_enc( des_context *ctx, const unsigned char key[DES_KEY_SIZE] )
  418. {
  419. des_setkey( ctx->sk, key );
  420. return( 0 );
  421. }
  422. /*
  423. * DES key schedule (56-bit, decryption)
  424. */
  425. int des_setkey_dec( des_context *ctx, const unsigned char key[DES_KEY_SIZE] )
  426. {
  427. int i;
  428. des_setkey( ctx->sk, key );
  429. for( i = 0; i < 16; i += 2 )
  430. {
  431. SWAP( ctx->sk[i ], ctx->sk[30 - i] );
  432. SWAP( ctx->sk[i + 1], ctx->sk[31 - i] );
  433. }
  434. return( 0 );
  435. }
  436. static void des3_set2key( unsigned long esk[96],
  437. unsigned long dsk[96],
  438. const unsigned char key[DES_KEY_SIZE*2] )
  439. {
  440. int i;
  441. des_setkey( esk, key );
  442. des_setkey( dsk + 32, key + 8 );
  443. for( i = 0; i < 32; i += 2 )
  444. {
  445. dsk[i ] = esk[30 - i];
  446. dsk[i + 1] = esk[31 - i];
  447. esk[i + 32] = dsk[62 - i];
  448. esk[i + 33] = dsk[63 - i];
  449. esk[i + 64] = esk[i ];
  450. esk[i + 65] = esk[i + 1];
  451. dsk[i + 64] = dsk[i ];
  452. dsk[i + 65] = dsk[i + 1];
  453. }
  454. }
  455. /*
  456. * Triple-DES key schedule (112-bit, encryption)
  457. */
  458. int des3_set2key_enc( des3_context *ctx, const unsigned char key[DES_KEY_SIZE * 2] )
  459. {
  460. unsigned long sk[96];
  461. des3_set2key( ctx->sk, sk, key );
  462. memset( sk, 0, sizeof( sk ) );
  463. return( 0 );
  464. }
  465. /*
  466. * Triple-DES key schedule (112-bit, decryption)
  467. */
  468. int des3_set2key_dec( des3_context *ctx, const unsigned char key[DES_KEY_SIZE * 2] )
  469. {
  470. unsigned long sk[96];
  471. des3_set2key( sk, ctx->sk, key );
  472. memset( sk, 0, sizeof( sk ) );
  473. return( 0 );
  474. }
  475. #ifndef USE_STM32F4XX_HW_CRYPTO /* use SW Crypto */
  476. static void des3_set3key( unsigned long esk[96],
  477. unsigned long dsk[96],
  478. const unsigned char key[24] )
  479. {
  480. int i;
  481. des_setkey( esk, key );
  482. des_setkey( dsk + 32, key + 8 );
  483. des_setkey( esk + 64, key + 16 );
  484. for( i = 0; i < 32; i += 2 )
  485. {
  486. dsk[i ] = esk[94 - i];
  487. dsk[i + 1] = esk[95 - i];
  488. esk[i + 32] = dsk[62 - i];
  489. esk[i + 33] = dsk[63 - i];
  490. dsk[i + 64] = esk[30 - i];
  491. dsk[i + 65] = esk[31 - i];
  492. }
  493. }
  494. #endif /* USE_STM32F4XX_HW_CRYPTO */
  495. /*
  496. * Triple-DES key schedule (168-bit, encryption)
  497. */
  498. int des3_set3key_enc( des3_context *ctx, const unsigned char key[DES_KEY_SIZE * 3] )
  499. {
  500. #ifdef USE_STM32F4XX_HW_CRYPTO /* use HW Crypto */
  501. memcpy(ctx->tdes_enc_key, key, 24);
  502. #else /* use SW Crypto */
  503. unsigned long sk[96];
  504. des3_set3key( ctx->sk, sk, key );
  505. memset( sk, 0, sizeof( sk ) );
  506. #endif /* USE_STM32F4XX_HW_CRYPTO */
  507. return( 0 );
  508. }
  509. /*
  510. * Triple-DES key schedule (168-bit, decryption)
  511. */
  512. int des3_set3key_dec( des3_context *ctx, const unsigned char key[DES_KEY_SIZE * 3] )
  513. {
  514. #ifdef USE_STM32F4XX_HW_CRYPTO /* use HW Crypto */
  515. memcpy(ctx->tdes_dec_key, key, 24);
  516. #else /* use SW Crypto */
  517. unsigned long sk[96];
  518. des3_set3key( sk, ctx->sk, key );
  519. memset( sk, 0, sizeof( sk ) );
  520. #endif /* USE_STM32F4XX_HW_CRYPTO */
  521. return( 0 );
  522. }
  523. /*
  524. * DES-ECB block encryption/decryption
  525. */
  526. int des_crypt_ecb( des_context *ctx,
  527. const unsigned char input[8],
  528. unsigned char output[8] )
  529. {
  530. int i;
  531. unsigned long X, Y, T, *SK;
  532. SK = ctx->sk;
  533. GET_ULONG_BE( X, input, 0 );
  534. GET_ULONG_BE( Y, input, 4 );
  535. DES_IP( X, Y );
  536. for( i = 0; i < 8; i++ )
  537. {
  538. DES_ROUND( Y, X );
  539. DES_ROUND( X, Y );
  540. }
  541. DES_FP( Y, X );
  542. PUT_ULONG_BE( Y, output, 0 );
  543. PUT_ULONG_BE( X, output, 4 );
  544. return( 0 );
  545. }
  546. /*
  547. * DES-CBC buffer encryption/decryption
  548. */
  549. int des_crypt_cbc( des_context *ctx,
  550. int mode,
  551. size_t length,
  552. unsigned char iv[8],
  553. const unsigned char *input,
  554. unsigned char *output )
  555. {
  556. int i;
  557. unsigned char temp[8];
  558. if( length % 8 )
  559. return( POLARSSL_ERR_DES_INVALID_INPUT_LENGTH );
  560. if( mode == DES_ENCRYPT )
  561. {
  562. while( length > 0 )
  563. {
  564. for( i = 0; i < 8; i++ )
  565. output[i] = (unsigned char)( input[i] ^ iv[i] );
  566. des_crypt_ecb( ctx, output, output );
  567. memcpy( iv, output, 8 );
  568. input += 8;
  569. output += 8;
  570. length -= 8;
  571. }
  572. }
  573. else /* DES_DECRYPT */
  574. {
  575. while( length > 0 )
  576. {
  577. memcpy( temp, input, 8 );
  578. des_crypt_ecb( ctx, input, output );
  579. for( i = 0; i < 8; i++ )
  580. output[i] = (unsigned char)( output[i] ^ iv[i] );
  581. memcpy( iv, temp, 8 );
  582. input += 8;
  583. output += 8;
  584. length -= 8;
  585. }
  586. }
  587. return( 0 );
  588. }
  589. /*
  590. * 3DES-ECB block encryption/decryption
  591. */
  592. int des3_crypt_ecb( des3_context *ctx,
  593. const unsigned char input[8],
  594. unsigned char output[8] )
  595. {
  596. #ifdef USE_STM32F4XX_HW_CRYPTO /* use HW Crypto */
  597. unsigned char *tdeskey; /* 3DES encryption/decryption key */
  598. if( ctx->mode == DES_ENCRYPT ) /* 3DES encryption */
  599. {
  600. /* Encryption key */
  601. tdeskey = ctx->tdes_enc_key;
  602. /* Deinitializes the CRYP peripheral */
  603. CRYP_DeInit();
  604. /* Crypto structures initialisation*/
  605. CRYP_StructInit(&DES3_CRYP_InitStructure);
  606. CRYP_KeyStructInit(&DES3_CRYP_KeyInitStructure);
  607. /* Crypto Init for Encryption process */
  608. DES3_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Encrypt;
  609. DES3_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_TDES_ECB;
  610. DES3_CRYP_InitStructure.CRYP_DataType = CRYP_DataType_8b;
  611. CRYP_Init(&DES3_CRYP_InitStructure);
  612. /* Key Initialisation */
  613. DES3_CRYP_KeyInitStructure.CRYP_Key1Left = __REV(*(uint32_t*)&tdeskey[0]);
  614. DES3_CRYP_KeyInitStructure.CRYP_Key1Right= __REV(*(uint32_t*)&tdeskey[4]);
  615. DES3_CRYP_KeyInitStructure.CRYP_Key2Left = __REV(*(uint32_t*)&tdeskey[8]);
  616. DES3_CRYP_KeyInitStructure.CRYP_Key2Right= __REV(*(uint32_t*)&tdeskey[12]);
  617. DES3_CRYP_KeyInitStructure.CRYP_Key3Left = __REV(*(uint32_t*)&tdeskey[16]);
  618. DES3_CRYP_KeyInitStructure.CRYP_Key3Right= __REV(*(uint32_t*)&tdeskey[20]);
  619. CRYP_KeyInit(& DES3_CRYP_KeyInitStructure);
  620. /* Flush IN/OUT FIFO */
  621. CRYP_FIFOFlush();
  622. /* Write the input block in the input FIFO */
  623. CRYP_DataIn(*(uint32_t*)&input[0]);
  624. CRYP_DataIn(*(uint32_t*)&input[4]);
  625. /* Enable Crypto processor */
  626. CRYP_Cmd(ENABLE);
  627. /* Wait until the complete message has been processed */
  628. while(CRYP_GetFlagStatus(CRYP_FLAG_BUSY)!= RESET);
  629. /* Read the output block from the output FIFO */
  630. *(uint32_t*)&output[0] = CRYP_DataOut();
  631. *(uint32_t*)&output[4] = CRYP_DataOut();
  632. /* Disable Crypto */
  633. CRYP_Cmd(DISABLE);
  634. }
  635. else if( ctx->mode == DES_DECRYPT ) /* 3DES decryption */
  636. {
  637. /* Decryption key */
  638. tdeskey = ctx->tdes_dec_key;
  639. /* Deinitializes the CRYP peripheral */
  640. CRYP_DeInit();
  641. /* Crypto structures initialisation*/
  642. CRYP_StructInit(&DES3_CRYP_InitStructure);
  643. CRYP_KeyStructInit(&DES3_CRYP_KeyInitStructure);
  644. /* Crypto Init for Encryption process */
  645. DES3_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Decrypt;
  646. DES3_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_TDES_ECB;
  647. DES3_CRYP_InitStructure.CRYP_DataType = CRYP_DataType_8b;
  648. CRYP_Init(&DES3_CRYP_InitStructure);
  649. /* Key Initialisation */
  650. DES3_CRYP_KeyInitStructure.CRYP_Key1Left = __REV(*(uint32_t*)&tdeskey[0]);
  651. DES3_CRYP_KeyInitStructure.CRYP_Key1Right= __REV(*(uint32_t*)&tdeskey[4]);
  652. DES3_CRYP_KeyInitStructure.CRYP_Key2Left = __REV(*(uint32_t*)&tdeskey[8]);
  653. DES3_CRYP_KeyInitStructure.CRYP_Key2Right= __REV(*(uint32_t*)&tdeskey[12]);
  654. DES3_CRYP_KeyInitStructure.CRYP_Key3Left = __REV(*(uint32_t*)&tdeskey[16]);
  655. DES3_CRYP_KeyInitStructure.CRYP_Key3Right= __REV(*(uint32_t*)&tdeskey[20]);
  656. CRYP_KeyInit(& DES3_CRYP_KeyInitStructure);
  657. /* Flush IN/OUT FIFO */
  658. CRYP_FIFOFlush();
  659. /* Write the input block in the input FIFO */
  660. CRYP_DataIn(*(uint32_t*)&input[0]);
  661. CRYP_DataIn(*(uint32_t*)&input[4]);
  662. /* Enable Crypto processor */
  663. CRYP_Cmd(ENABLE);
  664. /* Wait until the complete message has been processed */
  665. while(CRYP_GetFlagStatus(CRYP_FLAG_BUSY)!= RESET);
  666. /* Read the output block from the output FIFO */
  667. *(uint32_t*)&output[0] = CRYP_DataOut();
  668. *(uint32_t*)&output[4] = CRYP_DataOut();
  669. /* Disable Crypto */
  670. CRYP_Cmd(DISABLE);
  671. }
  672. #else /* use SW Crypto */
  673. int i;
  674. unsigned long X, Y, T, *SK;
  675. SK = ctx->sk;
  676. GET_ULONG_BE( X, input, 0 );
  677. GET_ULONG_BE( Y, input, 4 );
  678. DES_IP( X, Y );
  679. for( i = 0; i < 8; i++ )
  680. {
  681. DES_ROUND( Y, X );
  682. DES_ROUND( X, Y );
  683. }
  684. for( i = 0; i < 8; i++ )
  685. {
  686. DES_ROUND( X, Y );
  687. DES_ROUND( Y, X );
  688. }
  689. for( i = 0; i < 8; i++ )
  690. {
  691. DES_ROUND( Y, X );
  692. DES_ROUND( X, Y );
  693. }
  694. DES_FP( Y, X );
  695. PUT_ULONG_BE( Y, output, 0 );
  696. PUT_ULONG_BE( X, output, 4 );
  697. #endif /* USE_STM32F4XX_HW_CRYPTO */
  698. return( 0 );
  699. }
  700. /*
  701. * 3DES-CBC buffer encryption/decryption
  702. */
  703. int des3_crypt_cbc( des3_context *ctx,
  704. int mode,
  705. size_t length,
  706. unsigned char iv[8],
  707. const unsigned char *input,
  708. unsigned char *output )
  709. {
  710. #ifdef USE_STM32F4XX_HW_CRYPTO /* use HW Crypto */
  711. unsigned char *tdeskey; /* 3DES encryption/decryption key */
  712. if( mode == DES_ENCRYPT ) /* 3DES encryption */
  713. {
  714. ctx->mode = DES_ENCRYPT;
  715. /* Encryption key */
  716. tdeskey = ctx->tdes_enc_key;
  717. /* Deinitializes the CRYP peripheral */
  718. CRYP_DeInit();
  719. /* Crypto structures initialisation*/
  720. CRYP_StructInit(&DES3_CRYP_InitStructure);
  721. CRYP_KeyStructInit(&DES3_CRYP_KeyInitStructure);
  722. CRYP_IVStructInit(&DES3_CRYP_IVInitStructure);
  723. /* Crypto Init for Encryption process */
  724. DES3_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Encrypt;
  725. DES3_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_TDES_CBC;
  726. DES3_CRYP_InitStructure.CRYP_DataType = CRYP_DataType_8b;
  727. CRYP_Init(&DES3_CRYP_InitStructure);
  728. /* Key Initialisation */
  729. DES3_CRYP_KeyInitStructure.CRYP_Key1Left = __REV(*(uint32_t*)&tdeskey[0]);
  730. DES3_CRYP_KeyInitStructure.CRYP_Key1Right= __REV(*(uint32_t*)&tdeskey[4]);
  731. DES3_CRYP_KeyInitStructure.CRYP_Key2Left = __REV(*(uint32_t*)&tdeskey[8]);
  732. DES3_CRYP_KeyInitStructure.CRYP_Key2Right= __REV(*(uint32_t*)&tdeskey[12]);
  733. DES3_CRYP_KeyInitStructure.CRYP_Key3Left = __REV(*(uint32_t*)&tdeskey[16]);
  734. DES3_CRYP_KeyInitStructure.CRYP_Key3Right= __REV(*(uint32_t*)&tdeskey[20]);
  735. CRYP_KeyInit(& DES3_CRYP_KeyInitStructure);
  736. /* Initialization Vectors */
  737. DES3_CRYP_IVInitStructure.CRYP_IV0Left = __REV(*(uint32_t*)&iv[0]);
  738. DES3_CRYP_IVInitStructure.CRYP_IV0Right= __REV(*(uint32_t*)&iv[4]);
  739. CRYP_IVInit(&DES3_CRYP_IVInitStructure);
  740. while(length > 0)
  741. {
  742. /* Flush IN/OUT FIFO */
  743. CRYP_FIFOFlush();
  744. /* Write the input block in the input FIFO */
  745. CRYP_DataIn(*(uint32_t*)&input[0]);
  746. CRYP_DataIn(*(uint32_t*)&input[4]);
  747. /* Enable Crypto processor */
  748. CRYP_Cmd(ENABLE);
  749. /* Wait until the complete message has been processed */
  750. while(CRYP_GetFlagStatus(CRYP_FLAG_BUSY)!= RESET);
  751. /* Read the output block from the output FIFO */
  752. *(uint32_t*)&output[0] = CRYP_DataOut();
  753. *(uint32_t*)&output[4] = CRYP_DataOut();
  754. /* Update the initialization Vector */
  755. memcpy(iv, output, 8);
  756. input += 8;
  757. output += 8;
  758. length -= 8;
  759. }
  760. /* Disable Crypto */
  761. CRYP_Cmd(DISABLE);
  762. }
  763. else /* 3DES decryption */
  764. {
  765. ctx->mode = DES_DECRYPT;
  766. /* Decryption key */
  767. tdeskey = ctx->tdes_dec_key;
  768. /* Deinitializes the CRYP peripheral */
  769. CRYP_DeInit();
  770. /* Crypto structures initialisation*/
  771. CRYP_StructInit(&DES3_CRYP_InitStructure);
  772. CRYP_KeyStructInit(&DES3_CRYP_KeyInitStructure);
  773. CRYP_IVStructInit(&DES3_CRYP_IVInitStructure);
  774. /* Crypto Init for Encryption process */
  775. DES3_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Decrypt;
  776. DES3_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_TDES_CBC;
  777. DES3_CRYP_InitStructure.CRYP_DataType = CRYP_DataType_8b;
  778. CRYP_Init(&DES3_CRYP_InitStructure);
  779. /* Key Initialisation */
  780. DES3_CRYP_KeyInitStructure.CRYP_Key1Left = __REV(*(uint32_t*)&tdeskey[0]);
  781. DES3_CRYP_KeyInitStructure.CRYP_Key1Right= __REV(*(uint32_t*)&tdeskey[4]);
  782. DES3_CRYP_KeyInitStructure.CRYP_Key2Left = __REV(*(uint32_t*)&tdeskey[8]);
  783. DES3_CRYP_KeyInitStructure.CRYP_Key2Right= __REV(*(uint32_t*)&tdeskey[12]);
  784. DES3_CRYP_KeyInitStructure.CRYP_Key3Left = __REV(*(uint32_t*)&tdeskey[16]);
  785. DES3_CRYP_KeyInitStructure.CRYP_Key3Right= __REV(*(uint32_t*)&tdeskey[20]);
  786. CRYP_KeyInit(& DES3_CRYP_KeyInitStructure);
  787. /* Initialization Vectors */
  788. DES3_CRYP_IVInitStructure.CRYP_IV0Left = __REV(*(uint32_t*)&iv[0]);
  789. DES3_CRYP_IVInitStructure.CRYP_IV0Right= __REV(*(uint32_t*)&iv[4]);
  790. CRYP_IVInit(&DES3_CRYP_IVInitStructure);
  791. while(length > 0)
  792. {
  793. /* Update the initialization Vector */
  794. memcpy(iv, input, 8);
  795. /* Flush IN/OUT FIFO */
  796. CRYP_FIFOFlush();
  797. /* Write the input block in the input FIFO */
  798. CRYP_DataIn(*(uint32_t*)&input[0]);
  799. CRYP_DataIn(*(uint32_t*)&input[4]);
  800. /* Enable Crypto processor */
  801. CRYP_Cmd(ENABLE);
  802. /* Wait until the complete message has been processed */
  803. while(CRYP_GetFlagStatus(CRYP_FLAG_BUSY)!= RESET);
  804. /* Read the output block from the output FIFO */
  805. *(uint32_t*)&output[0] = CRYP_DataOut();
  806. *(uint32_t*)&output[4] = CRYP_DataOut();
  807. input += 8;
  808. output += 8;
  809. length -= 8;
  810. }
  811. /* Disable Crypto */
  812. CRYP_Cmd(DISABLE);
  813. }
  814. #else /* use SW Crypto */
  815. int i;
  816. unsigned char temp[8];
  817. if( length % 8 )
  818. return( POLARSSL_ERR_DES_INVALID_INPUT_LENGTH );
  819. if( mode == DES_ENCRYPT )
  820. {
  821. ctx->mode = DES_ENCRYPT; /* Added by ST's MCD Application Team */
  822. while( length > 0 )
  823. {
  824. for( i = 0; i < 8; i++ )
  825. output[i] = (unsigned char)( input[i] ^ iv[i] );
  826. des3_crypt_ecb( ctx, output, output );
  827. memcpy( iv, output, 8 );
  828. input += 8;
  829. output += 8;
  830. length -= 8;
  831. }
  832. }
  833. else /* DES_DECRYPT */
  834. {
  835. ctx->mode = DES_DECRYPT; /* Added by ST's MCD Application Team */
  836. while( length > 0 )
  837. {
  838. memcpy( temp, input, 8 );
  839. des3_crypt_ecb( ctx, input, output );
  840. for( i = 0; i < 8; i++ )
  841. output[i] = (unsigned char)( output[i] ^ iv[i] );
  842. memcpy( iv, temp, 8 );
  843. input += 8;
  844. output += 8;
  845. length -= 8;
  846. }
  847. }
  848. #endif /* USE_STM32F4XX_HW_CRYPTO */
  849. return( 0 );
  850. }
  851. #if defined(POLARSSL_SELF_TEST)
  852. #ifdef PRINTF_STDLIB
  853. #include <stdio.h>
  854. #endif
  855. #ifdef PRINTF_CUSTOM
  856. #include "tinystdio.h"
  857. #endif
  858. /*
  859. * DES and 3DES test vectors from:
  860. *
  861. * http://csrc.nist.gov/groups/STM/cavp/documents/des/tripledes-vectors.zip
  862. */
  863. static const unsigned char des3_test_keys[24] =
  864. {
  865. 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
  866. 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01,
  867. 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23
  868. };
  869. static const unsigned char des3_test_iv[8] =
  870. {
  871. 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF,
  872. };
  873. static const unsigned char des3_test_buf[8] =
  874. {
  875. 0x4E, 0x6F, 0x77, 0x20, 0x69, 0x73, 0x20, 0x74
  876. };
  877. static const unsigned char des3_test_ecb_dec[3][8] =
  878. {
  879. { 0xCD, 0xD6, 0x4F, 0x2F, 0x94, 0x27, 0xC1, 0x5D },
  880. { 0x69, 0x96, 0xC8, 0xFA, 0x47, 0xA2, 0xAB, 0xEB },
  881. { 0x83, 0x25, 0x39, 0x76, 0x44, 0x09, 0x1A, 0x0A }
  882. };
  883. static const unsigned char des3_test_ecb_enc[3][8] =
  884. {
  885. { 0x6A, 0x2A, 0x19, 0xF4, 0x1E, 0xCA, 0x85, 0x4B },
  886. { 0x03, 0xE6, 0x9F, 0x5B, 0xFA, 0x58, 0xEB, 0x42 },
  887. { 0xDD, 0x17, 0xE8, 0xB8, 0xB4, 0x37, 0xD2, 0x32 }
  888. };
  889. static const unsigned char des3_test_cbc_dec[3][8] =
  890. {
  891. { 0x12, 0x9F, 0x40, 0xB9, 0xD2, 0x00, 0x56, 0xB3 },
  892. { 0x47, 0x0E, 0xFC, 0x9A, 0x6B, 0x8E, 0xE3, 0x93 },
  893. { 0xC5, 0xCE, 0xCF, 0x63, 0xEC, 0xEC, 0x51, 0x4C }
  894. };
  895. static const unsigned char des3_test_cbc_enc[3][8] =
  896. {
  897. { 0x54, 0xF1, 0x5A, 0xF6, 0xEB, 0xE3, 0xA4, 0xB4 },
  898. { 0x35, 0x76, 0x11, 0x56, 0x5F, 0xA1, 0x8E, 0x4D },
  899. { 0xCB, 0x19, 0x1F, 0x85, 0xD1, 0xED, 0x84, 0x39 }
  900. };
  901. /*
  902. * Checkup routine
  903. */
  904. int des_self_test( int verbose )
  905. {
  906. int i, j, u, v;
  907. des_context ctx;
  908. des3_context ctx3;
  909. unsigned char key[24];
  910. unsigned char buf[8];
  911. unsigned char prv[8];
  912. unsigned char iv[8];
  913. memset( key, 0, 24 );
  914. /*
  915. * ECB mode
  916. */
  917. for( i = 0; i < 6; i++ )
  918. {
  919. u = i >> 1;
  920. v = i & 1;
  921. if( verbose != 0 )
  922. printf( " DES%c-ECB-%3d (%s): ",
  923. ( u == 0 ) ? ' ' : '3', 56 + u * 56,
  924. ( v == DES_DECRYPT ) ? "dec" : "enc" );
  925. memcpy( buf, des3_test_buf, 8 );
  926. switch( i )
  927. {
  928. case 0:
  929. des_setkey_dec( &ctx, (unsigned char *) des3_test_keys );
  930. break;
  931. case 1:
  932. des_setkey_enc( &ctx, (unsigned char *) des3_test_keys );
  933. break;
  934. case 2:
  935. des3_set2key_dec( &ctx3, (unsigned char *) des3_test_keys );
  936. break;
  937. case 3:
  938. des3_set2key_enc( &ctx3, (unsigned char *) des3_test_keys );
  939. break;
  940. case 4:
  941. des3_set3key_dec( &ctx3, (unsigned char *) des3_test_keys );
  942. break;
  943. case 5:
  944. des3_set3key_enc( &ctx3, (unsigned char *) des3_test_keys );
  945. break;
  946. default:
  947. return( 1 );
  948. }
  949. for( j = 0; j < 10000; j++ )
  950. {
  951. if( u == 0 )
  952. des_crypt_ecb( &ctx, buf, buf );
  953. else
  954. des3_crypt_ecb( &ctx3, buf, buf );
  955. }
  956. if( ( v == DES_DECRYPT &&
  957. memcmp( buf, des3_test_ecb_dec[u], 8 ) != 0 ) ||
  958. ( v != DES_DECRYPT &&
  959. memcmp( buf, des3_test_ecb_enc[u], 8 ) != 0 ) )
  960. {
  961. if( verbose != 0 )
  962. printf( "failed\n" );
  963. return( 1 );
  964. }
  965. if( verbose != 0 )
  966. printf( "passed\n" );
  967. }
  968. if( verbose != 0 )
  969. printf( "\n" );
  970. /*
  971. * CBC mode
  972. */
  973. for( i = 0; i < 6; i++ )
  974. {
  975. u = i >> 1;
  976. v = i & 1;
  977. if( verbose != 0 )
  978. printf( " DES%c-CBC-%3d (%s): ",
  979. ( u == 0 ) ? ' ' : '3', 56 + u * 56,
  980. ( v == DES_DECRYPT ) ? "dec" : "enc" );
  981. memcpy( iv, des3_test_iv, 8 );
  982. memcpy( prv, des3_test_iv, 8 );
  983. memcpy( buf, des3_test_buf, 8 );
  984. switch( i )
  985. {
  986. case 0:
  987. des_setkey_dec( &ctx, (unsigned char *) des3_test_keys );
  988. break;
  989. case 1:
  990. des_setkey_enc( &ctx, (unsigned char *) des3_test_keys );
  991. break;
  992. case 2:
  993. des3_set2key_dec( &ctx3, (unsigned char *) des3_test_keys );
  994. break;
  995. case 3:
  996. des3_set2key_enc( &ctx3, (unsigned char *) des3_test_keys );
  997. break;
  998. case 4:
  999. des3_set3key_dec( &ctx3, (unsigned char *) des3_test_keys );
  1000. break;
  1001. case 5:
  1002. des3_set3key_enc( &ctx3, (unsigned char *) des3_test_keys );
  1003. break;
  1004. default:
  1005. return( 1 );
  1006. }
  1007. if( v == DES_DECRYPT )
  1008. {
  1009. for( j = 0; j < 10000; j++ )
  1010. {
  1011. if( u == 0 )
  1012. des_crypt_cbc( &ctx, v, 8, iv, buf, buf );
  1013. else
  1014. des3_crypt_cbc( &ctx3, v, 8, iv, buf, buf );
  1015. }
  1016. }
  1017. else
  1018. {
  1019. for( j = 0; j < 10000; j++ )
  1020. {
  1021. unsigned char tmp[8];
  1022. if( u == 0 )
  1023. des_crypt_cbc( &ctx, v, 8, iv, buf, buf );
  1024. else
  1025. des3_crypt_cbc( &ctx3, v, 8, iv, buf, buf );
  1026. memcpy( tmp, prv, 8 );
  1027. memcpy( prv, buf, 8 );
  1028. memcpy( buf, tmp, 8 );
  1029. }
  1030. memcpy( buf, prv, 8 );
  1031. }
  1032. if( ( v == DES_DECRYPT &&
  1033. memcmp( buf, des3_test_cbc_dec[u], 8 ) != 0 ) ||
  1034. ( v != DES_DECRYPT &&
  1035. memcmp( buf, des3_test_cbc_enc[u], 8 ) != 0 ) )
  1036. {
  1037. if( verbose != 0 )
  1038. printf( "failed\n" );
  1039. return( 1 );
  1040. }
  1041. if( verbose != 0 )
  1042. printf( "passed\n" );
  1043. }
  1044. if( verbose != 0 )
  1045. printf( "\n" );
  1046. return( 0 );
  1047. }
  1048. #endif
  1049. #endif