des.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  1. /*
  2. * FIPS-46-3 compliant Triple-DES implementation
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. * not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. /*
  20. * DES, on which TDES is based, was originally designed by Horst Feistel
  21. * at IBM in 1974, and was adopted as a standard by NIST (formerly NBS).
  22. *
  23. * http://csrc.nist.gov/publications/fips/fips46-3/fips46-3.pdf
  24. */
  25. #include "common.h"
  26. #if defined(MBEDTLS_DES_C)
  27. #include "mbedtls/des.h"
  28. #include "mbedtls/error.h"
  29. #include "mbedtls/platform_util.h"
  30. #include <string.h>
  31. #include "mbedtls/platform.h"
  32. #if !defined(MBEDTLS_DES_ALT)
  33. /*
  34. * Expanded DES S-boxes
  35. */
  36. static const uint32_t SB1[64] =
  37. {
  38. 0x01010400, 0x00000000, 0x00010000, 0x01010404,
  39. 0x01010004, 0x00010404, 0x00000004, 0x00010000,
  40. 0x00000400, 0x01010400, 0x01010404, 0x00000400,
  41. 0x01000404, 0x01010004, 0x01000000, 0x00000004,
  42. 0x00000404, 0x01000400, 0x01000400, 0x00010400,
  43. 0x00010400, 0x01010000, 0x01010000, 0x01000404,
  44. 0x00010004, 0x01000004, 0x01000004, 0x00010004,
  45. 0x00000000, 0x00000404, 0x00010404, 0x01000000,
  46. 0x00010000, 0x01010404, 0x00000004, 0x01010000,
  47. 0x01010400, 0x01000000, 0x01000000, 0x00000400,
  48. 0x01010004, 0x00010000, 0x00010400, 0x01000004,
  49. 0x00000400, 0x00000004, 0x01000404, 0x00010404,
  50. 0x01010404, 0x00010004, 0x01010000, 0x01000404,
  51. 0x01000004, 0x00000404, 0x00010404, 0x01010400,
  52. 0x00000404, 0x01000400, 0x01000400, 0x00000000,
  53. 0x00010004, 0x00010400, 0x00000000, 0x01010004
  54. };
  55. static const uint32_t SB2[64] =
  56. {
  57. 0x80108020, 0x80008000, 0x00008000, 0x00108020,
  58. 0x00100000, 0x00000020, 0x80100020, 0x80008020,
  59. 0x80000020, 0x80108020, 0x80108000, 0x80000000,
  60. 0x80008000, 0x00100000, 0x00000020, 0x80100020,
  61. 0x00108000, 0x00100020, 0x80008020, 0x00000000,
  62. 0x80000000, 0x00008000, 0x00108020, 0x80100000,
  63. 0x00100020, 0x80000020, 0x00000000, 0x00108000,
  64. 0x00008020, 0x80108000, 0x80100000, 0x00008020,
  65. 0x00000000, 0x00108020, 0x80100020, 0x00100000,
  66. 0x80008020, 0x80100000, 0x80108000, 0x00008000,
  67. 0x80100000, 0x80008000, 0x00000020, 0x80108020,
  68. 0x00108020, 0x00000020, 0x00008000, 0x80000000,
  69. 0x00008020, 0x80108000, 0x00100000, 0x80000020,
  70. 0x00100020, 0x80008020, 0x80000020, 0x00100020,
  71. 0x00108000, 0x00000000, 0x80008000, 0x00008020,
  72. 0x80000000, 0x80100020, 0x80108020, 0x00108000
  73. };
  74. static const uint32_t SB3[64] =
  75. {
  76. 0x00000208, 0x08020200, 0x00000000, 0x08020008,
  77. 0x08000200, 0x00000000, 0x00020208, 0x08000200,
  78. 0x00020008, 0x08000008, 0x08000008, 0x00020000,
  79. 0x08020208, 0x00020008, 0x08020000, 0x00000208,
  80. 0x08000000, 0x00000008, 0x08020200, 0x00000200,
  81. 0x00020200, 0x08020000, 0x08020008, 0x00020208,
  82. 0x08000208, 0x00020200, 0x00020000, 0x08000208,
  83. 0x00000008, 0x08020208, 0x00000200, 0x08000000,
  84. 0x08020200, 0x08000000, 0x00020008, 0x00000208,
  85. 0x00020000, 0x08020200, 0x08000200, 0x00000000,
  86. 0x00000200, 0x00020008, 0x08020208, 0x08000200,
  87. 0x08000008, 0x00000200, 0x00000000, 0x08020008,
  88. 0x08000208, 0x00020000, 0x08000000, 0x08020208,
  89. 0x00000008, 0x00020208, 0x00020200, 0x08000008,
  90. 0x08020000, 0x08000208, 0x00000208, 0x08020000,
  91. 0x00020208, 0x00000008, 0x08020008, 0x00020200
  92. };
  93. static const uint32_t SB4[64] =
  94. {
  95. 0x00802001, 0x00002081, 0x00002081, 0x00000080,
  96. 0x00802080, 0x00800081, 0x00800001, 0x00002001,
  97. 0x00000000, 0x00802000, 0x00802000, 0x00802081,
  98. 0x00000081, 0x00000000, 0x00800080, 0x00800001,
  99. 0x00000001, 0x00002000, 0x00800000, 0x00802001,
  100. 0x00000080, 0x00800000, 0x00002001, 0x00002080,
  101. 0x00800081, 0x00000001, 0x00002080, 0x00800080,
  102. 0x00002000, 0x00802080, 0x00802081, 0x00000081,
  103. 0x00800080, 0x00800001, 0x00802000, 0x00802081,
  104. 0x00000081, 0x00000000, 0x00000000, 0x00802000,
  105. 0x00002080, 0x00800080, 0x00800081, 0x00000001,
  106. 0x00802001, 0x00002081, 0x00002081, 0x00000080,
  107. 0x00802081, 0x00000081, 0x00000001, 0x00002000,
  108. 0x00800001, 0x00002001, 0x00802080, 0x00800081,
  109. 0x00002001, 0x00002080, 0x00800000, 0x00802001,
  110. 0x00000080, 0x00800000, 0x00002000, 0x00802080
  111. };
  112. static const uint32_t SB5[64] =
  113. {
  114. 0x00000100, 0x02080100, 0x02080000, 0x42000100,
  115. 0x00080000, 0x00000100, 0x40000000, 0x02080000,
  116. 0x40080100, 0x00080000, 0x02000100, 0x40080100,
  117. 0x42000100, 0x42080000, 0x00080100, 0x40000000,
  118. 0x02000000, 0x40080000, 0x40080000, 0x00000000,
  119. 0x40000100, 0x42080100, 0x42080100, 0x02000100,
  120. 0x42080000, 0x40000100, 0x00000000, 0x42000000,
  121. 0x02080100, 0x02000000, 0x42000000, 0x00080100,
  122. 0x00080000, 0x42000100, 0x00000100, 0x02000000,
  123. 0x40000000, 0x02080000, 0x42000100, 0x40080100,
  124. 0x02000100, 0x40000000, 0x42080000, 0x02080100,
  125. 0x40080100, 0x00000100, 0x02000000, 0x42080000,
  126. 0x42080100, 0x00080100, 0x42000000, 0x42080100,
  127. 0x02080000, 0x00000000, 0x40080000, 0x42000000,
  128. 0x00080100, 0x02000100, 0x40000100, 0x00080000,
  129. 0x00000000, 0x40080000, 0x02080100, 0x40000100
  130. };
  131. static const uint32_t SB6[64] =
  132. {
  133. 0x20000010, 0x20400000, 0x00004000, 0x20404010,
  134. 0x20400000, 0x00000010, 0x20404010, 0x00400000,
  135. 0x20004000, 0x00404010, 0x00400000, 0x20000010,
  136. 0x00400010, 0x20004000, 0x20000000, 0x00004010,
  137. 0x00000000, 0x00400010, 0x20004010, 0x00004000,
  138. 0x00404000, 0x20004010, 0x00000010, 0x20400010,
  139. 0x20400010, 0x00000000, 0x00404010, 0x20404000,
  140. 0x00004010, 0x00404000, 0x20404000, 0x20000000,
  141. 0x20004000, 0x00000010, 0x20400010, 0x00404000,
  142. 0x20404010, 0x00400000, 0x00004010, 0x20000010,
  143. 0x00400000, 0x20004000, 0x20000000, 0x00004010,
  144. 0x20000010, 0x20404010, 0x00404000, 0x20400000,
  145. 0x00404010, 0x20404000, 0x00000000, 0x20400010,
  146. 0x00000010, 0x00004000, 0x20400000, 0x00404010,
  147. 0x00004000, 0x00400010, 0x20004010, 0x00000000,
  148. 0x20404000, 0x20000000, 0x00400010, 0x20004010
  149. };
  150. static const uint32_t SB7[64] =
  151. {
  152. 0x00200000, 0x04200002, 0x04000802, 0x00000000,
  153. 0x00000800, 0x04000802, 0x00200802, 0x04200800,
  154. 0x04200802, 0x00200000, 0x00000000, 0x04000002,
  155. 0x00000002, 0x04000000, 0x04200002, 0x00000802,
  156. 0x04000800, 0x00200802, 0x00200002, 0x04000800,
  157. 0x04000002, 0x04200000, 0x04200800, 0x00200002,
  158. 0x04200000, 0x00000800, 0x00000802, 0x04200802,
  159. 0x00200800, 0x00000002, 0x04000000, 0x00200800,
  160. 0x04000000, 0x00200800, 0x00200000, 0x04000802,
  161. 0x04000802, 0x04200002, 0x04200002, 0x00000002,
  162. 0x00200002, 0x04000000, 0x04000800, 0x00200000,
  163. 0x04200800, 0x00000802, 0x00200802, 0x04200800,
  164. 0x00000802, 0x04000002, 0x04200802, 0x04200000,
  165. 0x00200800, 0x00000000, 0x00000002, 0x04200802,
  166. 0x00000000, 0x00200802, 0x04200000, 0x00000800,
  167. 0x04000002, 0x04000800, 0x00000800, 0x00200002
  168. };
  169. static const uint32_t SB8[64] =
  170. {
  171. 0x10001040, 0x00001000, 0x00040000, 0x10041040,
  172. 0x10000000, 0x10001040, 0x00000040, 0x10000000,
  173. 0x00040040, 0x10040000, 0x10041040, 0x00041000,
  174. 0x10041000, 0x00041040, 0x00001000, 0x00000040,
  175. 0x10040000, 0x10000040, 0x10001000, 0x00001040,
  176. 0x00041000, 0x00040040, 0x10040040, 0x10041000,
  177. 0x00001040, 0x00000000, 0x00000000, 0x10040040,
  178. 0x10000040, 0x10001000, 0x00041040, 0x00040000,
  179. 0x00041040, 0x00040000, 0x10041000, 0x00001000,
  180. 0x00000040, 0x10040040, 0x00001000, 0x00041040,
  181. 0x10001000, 0x00000040, 0x10000040, 0x10040000,
  182. 0x10040040, 0x10000000, 0x00040000, 0x10001040,
  183. 0x00000000, 0x10041040, 0x00040040, 0x10000040,
  184. 0x10040000, 0x10001000, 0x10001040, 0x00000000,
  185. 0x10041040, 0x00041000, 0x00041000, 0x00001040,
  186. 0x00001040, 0x00040040, 0x10000000, 0x10041000
  187. };
  188. /*
  189. * PC1: left and right halves bit-swap
  190. */
  191. static const uint32_t LHs[16] =
  192. {
  193. 0x00000000, 0x00000001, 0x00000100, 0x00000101,
  194. 0x00010000, 0x00010001, 0x00010100, 0x00010101,
  195. 0x01000000, 0x01000001, 0x01000100, 0x01000101,
  196. 0x01010000, 0x01010001, 0x01010100, 0x01010101
  197. };
  198. static const uint32_t RHs[16] =
  199. {
  200. 0x00000000, 0x01000000, 0x00010000, 0x01010000,
  201. 0x00000100, 0x01000100, 0x00010100, 0x01010100,
  202. 0x00000001, 0x01000001, 0x00010001, 0x01010001,
  203. 0x00000101, 0x01000101, 0x00010101, 0x01010101,
  204. };
  205. /*
  206. * Initial Permutation macro
  207. */
  208. #define DES_IP(X,Y) \
  209. do \
  210. { \
  211. T = (((X) >> 4) ^ (Y)) & 0x0F0F0F0F; (Y) ^= T; (X) ^= (T << 4); \
  212. T = (((X) >> 16) ^ (Y)) & 0x0000FFFF; (Y) ^= T; (X) ^= (T << 16); \
  213. T = (((Y) >> 2) ^ (X)) & 0x33333333; (X) ^= T; (Y) ^= (T << 2); \
  214. T = (((Y) >> 8) ^ (X)) & 0x00FF00FF; (X) ^= T; (Y) ^= (T << 8); \
  215. (Y) = (((Y) << 1) | ((Y) >> 31)) & 0xFFFFFFFF; \
  216. T = ((X) ^ (Y)) & 0xAAAAAAAA; (Y) ^= T; (X) ^= T; \
  217. (X) = (((X) << 1) | ((X) >> 31)) & 0xFFFFFFFF; \
  218. } while( 0 )
  219. /*
  220. * Final Permutation macro
  221. */
  222. #define DES_FP(X,Y) \
  223. do \
  224. { \
  225. (X) = (((X) << 31) | ((X) >> 1)) & 0xFFFFFFFF; \
  226. T = ((X) ^ (Y)) & 0xAAAAAAAA; (X) ^= T; (Y) ^= T; \
  227. (Y) = (((Y) << 31) | ((Y) >> 1)) & 0xFFFFFFFF; \
  228. T = (((Y) >> 8) ^ (X)) & 0x00FF00FF; (X) ^= T; (Y) ^= (T << 8); \
  229. T = (((Y) >> 2) ^ (X)) & 0x33333333; (X) ^= T; (Y) ^= (T << 2); \
  230. T = (((X) >> 16) ^ (Y)) & 0x0000FFFF; (Y) ^= T; (X) ^= (T << 16); \
  231. T = (((X) >> 4) ^ (Y)) & 0x0F0F0F0F; (Y) ^= T; (X) ^= (T << 4); \
  232. } while( 0 )
  233. /*
  234. * DES round macro
  235. */
  236. #define DES_ROUND(X,Y) \
  237. do \
  238. { \
  239. T = *SK++ ^ (X); \
  240. (Y) ^= SB8[ (T ) & 0x3F ] ^ \
  241. SB6[ (T >> 8) & 0x3F ] ^ \
  242. SB4[ (T >> 16) & 0x3F ] ^ \
  243. SB2[ (T >> 24) & 0x3F ]; \
  244. \
  245. T = *SK++ ^ (((X) << 28) | ((X) >> 4)); \
  246. (Y) ^= SB7[ (T ) & 0x3F ] ^ \
  247. SB5[ (T >> 8) & 0x3F ] ^ \
  248. SB3[ (T >> 16) & 0x3F ] ^ \
  249. SB1[ (T >> 24) & 0x3F ]; \
  250. } while( 0 )
  251. #define SWAP(a,b) \
  252. do \
  253. { \
  254. uint32_t t = (a); (a) = (b); (b) = t; t = 0; \
  255. } while( 0 )
  256. void mbedtls_des_init( mbedtls_des_context *ctx )
  257. {
  258. memset( ctx, 0, sizeof( mbedtls_des_context ) );
  259. }
  260. void mbedtls_des_free( mbedtls_des_context *ctx )
  261. {
  262. if( ctx == NULL )
  263. return;
  264. mbedtls_platform_zeroize( ctx, sizeof( mbedtls_des_context ) );
  265. }
  266. void mbedtls_des3_init( mbedtls_des3_context *ctx )
  267. {
  268. memset( ctx, 0, sizeof( mbedtls_des3_context ) );
  269. }
  270. void mbedtls_des3_free( mbedtls_des3_context *ctx )
  271. {
  272. if( ctx == NULL )
  273. return;
  274. mbedtls_platform_zeroize( ctx, sizeof( mbedtls_des3_context ) );
  275. }
  276. static const unsigned char odd_parity_table[128] = { 1, 2, 4, 7, 8,
  277. 11, 13, 14, 16, 19, 21, 22, 25, 26, 28, 31, 32, 35, 37, 38, 41, 42, 44,
  278. 47, 49, 50, 52, 55, 56, 59, 61, 62, 64, 67, 69, 70, 73, 74, 76, 79, 81,
  279. 82, 84, 87, 88, 91, 93, 94, 97, 98, 100, 103, 104, 107, 109, 110, 112,
  280. 115, 117, 118, 121, 122, 124, 127, 128, 131, 133, 134, 137, 138, 140,
  281. 143, 145, 146, 148, 151, 152, 155, 157, 158, 161, 162, 164, 167, 168,
  282. 171, 173, 174, 176, 179, 181, 182, 185, 186, 188, 191, 193, 194, 196,
  283. 199, 200, 203, 205, 206, 208, 211, 213, 214, 217, 218, 220, 223, 224,
  284. 227, 229, 230, 233, 234, 236, 239, 241, 242, 244, 247, 248, 251, 253,
  285. 254 };
  286. void mbedtls_des_key_set_parity( unsigned char key[MBEDTLS_DES_KEY_SIZE] )
  287. {
  288. int i;
  289. for( i = 0; i < MBEDTLS_DES_KEY_SIZE; i++ )
  290. key[i] = odd_parity_table[key[i] / 2];
  291. }
  292. /*
  293. * Check the given key's parity, returns 1 on failure, 0 on SUCCESS
  294. */
  295. int mbedtls_des_key_check_key_parity( const unsigned char key[MBEDTLS_DES_KEY_SIZE] )
  296. {
  297. int i;
  298. for( i = 0; i < MBEDTLS_DES_KEY_SIZE; i++ )
  299. if( key[i] != odd_parity_table[key[i] / 2] )
  300. return( 1 );
  301. return( 0 );
  302. }
  303. /*
  304. * Table of weak and semi-weak keys
  305. *
  306. * Source: http://en.wikipedia.org/wiki/Weak_key
  307. *
  308. * Weak:
  309. * Alternating ones + zeros (0x0101010101010101)
  310. * Alternating 'F' + 'E' (0xFEFEFEFEFEFEFEFE)
  311. * '0xE0E0E0E0F1F1F1F1'
  312. * '0x1F1F1F1F0E0E0E0E'
  313. *
  314. * Semi-weak:
  315. * 0x011F011F010E010E and 0x1F011F010E010E01
  316. * 0x01E001E001F101F1 and 0xE001E001F101F101
  317. * 0x01FE01FE01FE01FE and 0xFE01FE01FE01FE01
  318. * 0x1FE01FE00EF10EF1 and 0xE01FE01FF10EF10E
  319. * 0x1FFE1FFE0EFE0EFE and 0xFE1FFE1FFE0EFE0E
  320. * 0xE0FEE0FEF1FEF1FE and 0xFEE0FEE0FEF1FEF1
  321. *
  322. */
  323. #define WEAK_KEY_COUNT 16
  324. static const unsigned char weak_key_table[WEAK_KEY_COUNT][MBEDTLS_DES_KEY_SIZE] =
  325. {
  326. { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 },
  327. { 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE },
  328. { 0x1F, 0x1F, 0x1F, 0x1F, 0x0E, 0x0E, 0x0E, 0x0E },
  329. { 0xE0, 0xE0, 0xE0, 0xE0, 0xF1, 0xF1, 0xF1, 0xF1 },
  330. { 0x01, 0x1F, 0x01, 0x1F, 0x01, 0x0E, 0x01, 0x0E },
  331. { 0x1F, 0x01, 0x1F, 0x01, 0x0E, 0x01, 0x0E, 0x01 },
  332. { 0x01, 0xE0, 0x01, 0xE0, 0x01, 0xF1, 0x01, 0xF1 },
  333. { 0xE0, 0x01, 0xE0, 0x01, 0xF1, 0x01, 0xF1, 0x01 },
  334. { 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE },
  335. { 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01 },
  336. { 0x1F, 0xE0, 0x1F, 0xE0, 0x0E, 0xF1, 0x0E, 0xF1 },
  337. { 0xE0, 0x1F, 0xE0, 0x1F, 0xF1, 0x0E, 0xF1, 0x0E },
  338. { 0x1F, 0xFE, 0x1F, 0xFE, 0x0E, 0xFE, 0x0E, 0xFE },
  339. { 0xFE, 0x1F, 0xFE, 0x1F, 0xFE, 0x0E, 0xFE, 0x0E },
  340. { 0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1, 0xFE },
  341. { 0xFE, 0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1 }
  342. };
  343. int mbedtls_des_key_check_weak( const unsigned char key[MBEDTLS_DES_KEY_SIZE] )
  344. {
  345. int i;
  346. for( i = 0; i < WEAK_KEY_COUNT; i++ )
  347. if( memcmp( weak_key_table[i], key, MBEDTLS_DES_KEY_SIZE) == 0 )
  348. return( 1 );
  349. return( 0 );
  350. }
  351. #if !defined(MBEDTLS_DES_SETKEY_ALT)
  352. void mbedtls_des_setkey( uint32_t SK[32], const unsigned char key[MBEDTLS_DES_KEY_SIZE] )
  353. {
  354. int i;
  355. uint32_t X, Y, T;
  356. X = MBEDTLS_GET_UINT32_BE( key, 0 );
  357. Y = MBEDTLS_GET_UINT32_BE( key, 4 );
  358. /*
  359. * Permuted Choice 1
  360. */
  361. T = ((Y >> 4) ^ X) & 0x0F0F0F0F; X ^= T; Y ^= (T << 4);
  362. T = ((Y ) ^ X) & 0x10101010; X ^= T; Y ^= (T );
  363. X = (LHs[ (X ) & 0xF] << 3) | (LHs[ (X >> 8) & 0xF ] << 2)
  364. | (LHs[ (X >> 16) & 0xF] << 1) | (LHs[ (X >> 24) & 0xF ] )
  365. | (LHs[ (X >> 5) & 0xF] << 7) | (LHs[ (X >> 13) & 0xF ] << 6)
  366. | (LHs[ (X >> 21) & 0xF] << 5) | (LHs[ (X >> 29) & 0xF ] << 4);
  367. Y = (RHs[ (Y >> 1) & 0xF] << 3) | (RHs[ (Y >> 9) & 0xF ] << 2)
  368. | (RHs[ (Y >> 17) & 0xF] << 1) | (RHs[ (Y >> 25) & 0xF ] )
  369. | (RHs[ (Y >> 4) & 0xF] << 7) | (RHs[ (Y >> 12) & 0xF ] << 6)
  370. | (RHs[ (Y >> 20) & 0xF] << 5) | (RHs[ (Y >> 28) & 0xF ] << 4);
  371. X &= 0x0FFFFFFF;
  372. Y &= 0x0FFFFFFF;
  373. /*
  374. * calculate subkeys
  375. */
  376. for( i = 0; i < 16; i++ )
  377. {
  378. if( i < 2 || i == 8 || i == 15 )
  379. {
  380. X = ((X << 1) | (X >> 27)) & 0x0FFFFFFF;
  381. Y = ((Y << 1) | (Y >> 27)) & 0x0FFFFFFF;
  382. }
  383. else
  384. {
  385. X = ((X << 2) | (X >> 26)) & 0x0FFFFFFF;
  386. Y = ((Y << 2) | (Y >> 26)) & 0x0FFFFFFF;
  387. }
  388. *SK++ = ((X << 4) & 0x24000000) | ((X << 28) & 0x10000000)
  389. | ((X << 14) & 0x08000000) | ((X << 18) & 0x02080000)
  390. | ((X << 6) & 0x01000000) | ((X << 9) & 0x00200000)
  391. | ((X >> 1) & 0x00100000) | ((X << 10) & 0x00040000)
  392. | ((X << 2) & 0x00020000) | ((X >> 10) & 0x00010000)
  393. | ((Y >> 13) & 0x00002000) | ((Y >> 4) & 0x00001000)
  394. | ((Y << 6) & 0x00000800) | ((Y >> 1) & 0x00000400)
  395. | ((Y >> 14) & 0x00000200) | ((Y ) & 0x00000100)
  396. | ((Y >> 5) & 0x00000020) | ((Y >> 10) & 0x00000010)
  397. | ((Y >> 3) & 0x00000008) | ((Y >> 18) & 0x00000004)
  398. | ((Y >> 26) & 0x00000002) | ((Y >> 24) & 0x00000001);
  399. *SK++ = ((X << 15) & 0x20000000) | ((X << 17) & 0x10000000)
  400. | ((X << 10) & 0x08000000) | ((X << 22) & 0x04000000)
  401. | ((X >> 2) & 0x02000000) | ((X << 1) & 0x01000000)
  402. | ((X << 16) & 0x00200000) | ((X << 11) & 0x00100000)
  403. | ((X << 3) & 0x00080000) | ((X >> 6) & 0x00040000)
  404. | ((X << 15) & 0x00020000) | ((X >> 4) & 0x00010000)
  405. | ((Y >> 2) & 0x00002000) | ((Y << 8) & 0x00001000)
  406. | ((Y >> 14) & 0x00000808) | ((Y >> 9) & 0x00000400)
  407. | ((Y ) & 0x00000200) | ((Y << 7) & 0x00000100)
  408. | ((Y >> 7) & 0x00000020) | ((Y >> 3) & 0x00000011)
  409. | ((Y << 2) & 0x00000004) | ((Y >> 21) & 0x00000002);
  410. }
  411. }
  412. #endif /* !MBEDTLS_DES_SETKEY_ALT */
  413. /*
  414. * DES key schedule (56-bit, encryption)
  415. */
  416. int mbedtls_des_setkey_enc( mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] )
  417. {
  418. mbedtls_des_setkey( ctx->sk, key );
  419. return( 0 );
  420. }
  421. /*
  422. * DES key schedule (56-bit, decryption)
  423. */
  424. int mbedtls_des_setkey_dec( mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] )
  425. {
  426. int i;
  427. mbedtls_des_setkey( ctx->sk, key );
  428. for( i = 0; i < 16; i += 2 )
  429. {
  430. SWAP( ctx->sk[i ], ctx->sk[30 - i] );
  431. SWAP( ctx->sk[i + 1], ctx->sk[31 - i] );
  432. }
  433. return( 0 );
  434. }
  435. static void des3_set2key( uint32_t esk[96],
  436. uint32_t dsk[96],
  437. const unsigned char key[MBEDTLS_DES_KEY_SIZE*2] )
  438. {
  439. int i;
  440. mbedtls_des_setkey( esk, key );
  441. mbedtls_des_setkey( dsk + 32, key + 8 );
  442. for( i = 0; i < 32; i += 2 )
  443. {
  444. dsk[i ] = esk[30 - i];
  445. dsk[i + 1] = esk[31 - i];
  446. esk[i + 32] = dsk[62 - i];
  447. esk[i + 33] = dsk[63 - i];
  448. esk[i + 64] = esk[i ];
  449. esk[i + 65] = esk[i + 1];
  450. dsk[i + 64] = dsk[i ];
  451. dsk[i + 65] = dsk[i + 1];
  452. }
  453. }
  454. /*
  455. * Triple-DES key schedule (112-bit, encryption)
  456. */
  457. int mbedtls_des3_set2key_enc( mbedtls_des3_context *ctx,
  458. const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] )
  459. {
  460. uint32_t sk[96];
  461. des3_set2key( ctx->sk, sk, key );
  462. mbedtls_platform_zeroize( sk, sizeof( sk ) );
  463. return( 0 );
  464. }
  465. /*
  466. * Triple-DES key schedule (112-bit, decryption)
  467. */
  468. int mbedtls_des3_set2key_dec( mbedtls_des3_context *ctx,
  469. const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] )
  470. {
  471. uint32_t sk[96];
  472. des3_set2key( sk, ctx->sk, key );
  473. mbedtls_platform_zeroize( sk, sizeof( sk ) );
  474. return( 0 );
  475. }
  476. static void des3_set3key( uint32_t esk[96],
  477. uint32_t dsk[96],
  478. const unsigned char key[24] )
  479. {
  480. int i;
  481. mbedtls_des_setkey( esk, key );
  482. mbedtls_des_setkey( dsk + 32, key + 8 );
  483. mbedtls_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. /*
  495. * Triple-DES key schedule (168-bit, encryption)
  496. */
  497. int mbedtls_des3_set3key_enc( mbedtls_des3_context *ctx,
  498. const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] )
  499. {
  500. uint32_t sk[96];
  501. des3_set3key( ctx->sk, sk, key );
  502. mbedtls_platform_zeroize( sk, sizeof( sk ) );
  503. return( 0 );
  504. }
  505. /*
  506. * Triple-DES key schedule (168-bit, decryption)
  507. */
  508. int mbedtls_des3_set3key_dec( mbedtls_des3_context *ctx,
  509. const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] )
  510. {
  511. uint32_t sk[96];
  512. des3_set3key( sk, ctx->sk, key );
  513. mbedtls_platform_zeroize( sk, sizeof( sk ) );
  514. return( 0 );
  515. }
  516. /*
  517. * DES-ECB block encryption/decryption
  518. */
  519. #if !defined(MBEDTLS_DES_CRYPT_ECB_ALT)
  520. int mbedtls_des_crypt_ecb( mbedtls_des_context *ctx,
  521. const unsigned char input[8],
  522. unsigned char output[8] )
  523. {
  524. int i;
  525. uint32_t X, Y, T, *SK;
  526. SK = ctx->sk;
  527. X = MBEDTLS_GET_UINT32_BE( input, 0 );
  528. Y = MBEDTLS_GET_UINT32_BE( input, 4 );
  529. DES_IP( X, Y );
  530. for( i = 0; i < 8; i++ )
  531. {
  532. DES_ROUND( Y, X );
  533. DES_ROUND( X, Y );
  534. }
  535. DES_FP( Y, X );
  536. MBEDTLS_PUT_UINT32_BE( Y, output, 0 );
  537. MBEDTLS_PUT_UINT32_BE( X, output, 4 );
  538. return( 0 );
  539. }
  540. #endif /* !MBEDTLS_DES_CRYPT_ECB_ALT */
  541. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  542. /*
  543. * DES-CBC buffer encryption/decryption
  544. */
  545. int mbedtls_des_crypt_cbc( mbedtls_des_context *ctx,
  546. int mode,
  547. size_t length,
  548. unsigned char iv[8],
  549. const unsigned char *input,
  550. unsigned char *output )
  551. {
  552. int i;
  553. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  554. unsigned char temp[8];
  555. if( length % 8 )
  556. return( MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH );
  557. if( mode == MBEDTLS_DES_ENCRYPT )
  558. {
  559. while( length > 0 )
  560. {
  561. for( i = 0; i < 8; i++ )
  562. output[i] = (unsigned char)( input[i] ^ iv[i] );
  563. ret = mbedtls_des_crypt_ecb( ctx, output, output );
  564. if( ret != 0 )
  565. goto exit;
  566. memcpy( iv, output, 8 );
  567. input += 8;
  568. output += 8;
  569. length -= 8;
  570. }
  571. }
  572. else /* MBEDTLS_DES_DECRYPT */
  573. {
  574. while( length > 0 )
  575. {
  576. memcpy( temp, input, 8 );
  577. ret = mbedtls_des_crypt_ecb( ctx, input, output );
  578. if( ret != 0 )
  579. goto exit;
  580. for( i = 0; i < 8; i++ )
  581. output[i] = (unsigned char)( output[i] ^ iv[i] );
  582. memcpy( iv, temp, 8 );
  583. input += 8;
  584. output += 8;
  585. length -= 8;
  586. }
  587. }
  588. ret = 0;
  589. exit:
  590. return( ret );
  591. }
  592. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  593. /*
  594. * 3DES-ECB block encryption/decryption
  595. */
  596. #if !defined(MBEDTLS_DES3_CRYPT_ECB_ALT)
  597. int mbedtls_des3_crypt_ecb( mbedtls_des3_context *ctx,
  598. const unsigned char input[8],
  599. unsigned char output[8] )
  600. {
  601. int i;
  602. uint32_t X, Y, T, *SK;
  603. SK = ctx->sk;
  604. X = MBEDTLS_GET_UINT32_BE( input, 0 );
  605. Y = MBEDTLS_GET_UINT32_BE( input, 4 );
  606. DES_IP( X, Y );
  607. for( i = 0; i < 8; i++ )
  608. {
  609. DES_ROUND( Y, X );
  610. DES_ROUND( X, Y );
  611. }
  612. for( i = 0; i < 8; i++ )
  613. {
  614. DES_ROUND( X, Y );
  615. DES_ROUND( Y, X );
  616. }
  617. for( i = 0; i < 8; i++ )
  618. {
  619. DES_ROUND( Y, X );
  620. DES_ROUND( X, Y );
  621. }
  622. DES_FP( Y, X );
  623. MBEDTLS_PUT_UINT32_BE( Y, output, 0 );
  624. MBEDTLS_PUT_UINT32_BE( X, output, 4 );
  625. return( 0 );
  626. }
  627. #endif /* !MBEDTLS_DES3_CRYPT_ECB_ALT */
  628. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  629. /*
  630. * 3DES-CBC buffer encryption/decryption
  631. */
  632. int mbedtls_des3_crypt_cbc( mbedtls_des3_context *ctx,
  633. int mode,
  634. size_t length,
  635. unsigned char iv[8],
  636. const unsigned char *input,
  637. unsigned char *output )
  638. {
  639. int i;
  640. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  641. unsigned char temp[8];
  642. if( length % 8 )
  643. return( MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH );
  644. if( mode == MBEDTLS_DES_ENCRYPT )
  645. {
  646. while( length > 0 )
  647. {
  648. for( i = 0; i < 8; i++ )
  649. output[i] = (unsigned char)( input[i] ^ iv[i] );
  650. ret = mbedtls_des3_crypt_ecb( ctx, output, output );
  651. if( ret != 0 )
  652. goto exit;
  653. memcpy( iv, output, 8 );
  654. input += 8;
  655. output += 8;
  656. length -= 8;
  657. }
  658. }
  659. else /* MBEDTLS_DES_DECRYPT */
  660. {
  661. while( length > 0 )
  662. {
  663. memcpy( temp, input, 8 );
  664. ret = mbedtls_des3_crypt_ecb( ctx, input, output );
  665. if( ret != 0 )
  666. goto exit;
  667. for( i = 0; i < 8; i++ )
  668. output[i] = (unsigned char)( output[i] ^ iv[i] );
  669. memcpy( iv, temp, 8 );
  670. input += 8;
  671. output += 8;
  672. length -= 8;
  673. }
  674. }
  675. ret = 0;
  676. exit:
  677. return( ret );
  678. }
  679. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  680. #endif /* !MBEDTLS_DES_ALT */
  681. #if defined(MBEDTLS_SELF_TEST)
  682. /*
  683. * DES and 3DES test vectors from:
  684. *
  685. * http://csrc.nist.gov/groups/STM/cavp/documents/des/tripledes-vectors.zip
  686. */
  687. static const unsigned char des3_test_keys[24] =
  688. {
  689. 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
  690. 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01,
  691. 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23
  692. };
  693. static const unsigned char des3_test_buf[8] =
  694. {
  695. 0x4E, 0x6F, 0x77, 0x20, 0x69, 0x73, 0x20, 0x74
  696. };
  697. static const unsigned char des3_test_ecb_dec[3][8] =
  698. {
  699. { 0x37, 0x2B, 0x98, 0xBF, 0x52, 0x65, 0xB0, 0x59 },
  700. { 0xC2, 0x10, 0x19, 0x9C, 0x38, 0x5A, 0x65, 0xA1 },
  701. { 0xA2, 0x70, 0x56, 0x68, 0x69, 0xE5, 0x15, 0x1D }
  702. };
  703. static const unsigned char des3_test_ecb_enc[3][8] =
  704. {
  705. { 0x1C, 0xD5, 0x97, 0xEA, 0x84, 0x26, 0x73, 0xFB },
  706. { 0xB3, 0x92, 0x4D, 0xF3, 0xC5, 0xB5, 0x42, 0x93 },
  707. { 0xDA, 0x37, 0x64, 0x41, 0xBA, 0x6F, 0x62, 0x6F }
  708. };
  709. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  710. static const unsigned char des3_test_iv[8] =
  711. {
  712. 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF,
  713. };
  714. static const unsigned char des3_test_cbc_dec[3][8] =
  715. {
  716. { 0x58, 0xD9, 0x48, 0xEF, 0x85, 0x14, 0x65, 0x9A },
  717. { 0x5F, 0xC8, 0x78, 0xD4, 0xD7, 0x92, 0xD9, 0x54 },
  718. { 0x25, 0xF9, 0x75, 0x85, 0xA8, 0x1E, 0x48, 0xBF }
  719. };
  720. static const unsigned char des3_test_cbc_enc[3][8] =
  721. {
  722. { 0x91, 0x1C, 0x6D, 0xCF, 0x48, 0xA7, 0xC3, 0x4D },
  723. { 0x60, 0x1A, 0x76, 0x8F, 0xA1, 0xF9, 0x66, 0xF1 },
  724. { 0xA1, 0x50, 0x0F, 0x99, 0xB2, 0xCD, 0x64, 0x76 }
  725. };
  726. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  727. /*
  728. * Checkup routine
  729. */
  730. int mbedtls_des_self_test( int verbose )
  731. {
  732. int i, j, u, v, ret = 0;
  733. mbedtls_des_context ctx;
  734. mbedtls_des3_context ctx3;
  735. unsigned char buf[8];
  736. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  737. unsigned char prv[8];
  738. unsigned char iv[8];
  739. #endif
  740. mbedtls_des_init( &ctx );
  741. mbedtls_des3_init( &ctx3 );
  742. /*
  743. * ECB mode
  744. */
  745. for( i = 0; i < 6; i++ )
  746. {
  747. u = i >> 1;
  748. v = i & 1;
  749. if( verbose != 0 )
  750. mbedtls_printf( " DES%c-ECB-%3d (%s): ",
  751. ( u == 0 ) ? ' ' : '3', 56 + u * 56,
  752. ( v == MBEDTLS_DES_DECRYPT ) ? "dec" : "enc" );
  753. memcpy( buf, des3_test_buf, 8 );
  754. switch( i )
  755. {
  756. case 0:
  757. ret = mbedtls_des_setkey_dec( &ctx, des3_test_keys );
  758. break;
  759. case 1:
  760. ret = mbedtls_des_setkey_enc( &ctx, des3_test_keys );
  761. break;
  762. case 2:
  763. ret = mbedtls_des3_set2key_dec( &ctx3, des3_test_keys );
  764. break;
  765. case 3:
  766. ret = mbedtls_des3_set2key_enc( &ctx3, des3_test_keys );
  767. break;
  768. case 4:
  769. ret = mbedtls_des3_set3key_dec( &ctx3, des3_test_keys );
  770. break;
  771. case 5:
  772. ret = mbedtls_des3_set3key_enc( &ctx3, des3_test_keys );
  773. break;
  774. default:
  775. return( 1 );
  776. }
  777. if( ret != 0 )
  778. goto exit;
  779. for( j = 0; j < 100; j++ )
  780. {
  781. if( u == 0 )
  782. ret = mbedtls_des_crypt_ecb( &ctx, buf, buf );
  783. else
  784. ret = mbedtls_des3_crypt_ecb( &ctx3, buf, buf );
  785. if( ret != 0 )
  786. goto exit;
  787. }
  788. if( ( v == MBEDTLS_DES_DECRYPT &&
  789. memcmp( buf, des3_test_ecb_dec[u], 8 ) != 0 ) ||
  790. ( v != MBEDTLS_DES_DECRYPT &&
  791. memcmp( buf, des3_test_ecb_enc[u], 8 ) != 0 ) )
  792. {
  793. if( verbose != 0 )
  794. mbedtls_printf( "failed\n" );
  795. ret = 1;
  796. goto exit;
  797. }
  798. if( verbose != 0 )
  799. mbedtls_printf( "passed\n" );
  800. }
  801. if( verbose != 0 )
  802. mbedtls_printf( "\n" );
  803. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  804. /*
  805. * CBC mode
  806. */
  807. for( i = 0; i < 6; i++ )
  808. {
  809. u = i >> 1;
  810. v = i & 1;
  811. if( verbose != 0 )
  812. mbedtls_printf( " DES%c-CBC-%3d (%s): ",
  813. ( u == 0 ) ? ' ' : '3', 56 + u * 56,
  814. ( v == MBEDTLS_DES_DECRYPT ) ? "dec" : "enc" );
  815. memcpy( iv, des3_test_iv, 8 );
  816. memcpy( prv, des3_test_iv, 8 );
  817. memcpy( buf, des3_test_buf, 8 );
  818. switch( i )
  819. {
  820. case 0:
  821. ret = mbedtls_des_setkey_dec( &ctx, des3_test_keys );
  822. break;
  823. case 1:
  824. ret = mbedtls_des_setkey_enc( &ctx, des3_test_keys );
  825. break;
  826. case 2:
  827. ret = mbedtls_des3_set2key_dec( &ctx3, des3_test_keys );
  828. break;
  829. case 3:
  830. ret = mbedtls_des3_set2key_enc( &ctx3, des3_test_keys );
  831. break;
  832. case 4:
  833. ret = mbedtls_des3_set3key_dec( &ctx3, des3_test_keys );
  834. break;
  835. case 5:
  836. ret = mbedtls_des3_set3key_enc( &ctx3, des3_test_keys );
  837. break;
  838. default:
  839. return( 1 );
  840. }
  841. if( ret != 0 )
  842. goto exit;
  843. if( v == MBEDTLS_DES_DECRYPT )
  844. {
  845. for( j = 0; j < 100; j++ )
  846. {
  847. if( u == 0 )
  848. ret = mbedtls_des_crypt_cbc( &ctx, v, 8, iv, buf, buf );
  849. else
  850. ret = mbedtls_des3_crypt_cbc( &ctx3, v, 8, iv, buf, buf );
  851. if( ret != 0 )
  852. goto exit;
  853. }
  854. }
  855. else
  856. {
  857. for( j = 0; j < 100; j++ )
  858. {
  859. unsigned char tmp[8];
  860. if( u == 0 )
  861. ret = mbedtls_des_crypt_cbc( &ctx, v, 8, iv, buf, buf );
  862. else
  863. ret = mbedtls_des3_crypt_cbc( &ctx3, v, 8, iv, buf, buf );
  864. if( ret != 0 )
  865. goto exit;
  866. memcpy( tmp, prv, 8 );
  867. memcpy( prv, buf, 8 );
  868. memcpy( buf, tmp, 8 );
  869. }
  870. memcpy( buf, prv, 8 );
  871. }
  872. if( ( v == MBEDTLS_DES_DECRYPT &&
  873. memcmp( buf, des3_test_cbc_dec[u], 8 ) != 0 ) ||
  874. ( v != MBEDTLS_DES_DECRYPT &&
  875. memcmp( buf, des3_test_cbc_enc[u], 8 ) != 0 ) )
  876. {
  877. if( verbose != 0 )
  878. mbedtls_printf( "failed\n" );
  879. ret = 1;
  880. goto exit;
  881. }
  882. if( verbose != 0 )
  883. mbedtls_printf( "passed\n" );
  884. }
  885. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  886. if( verbose != 0 )
  887. mbedtls_printf( "\n" );
  888. exit:
  889. mbedtls_des_free( &ctx );
  890. mbedtls_des3_free( &ctx3 );
  891. if( ret != 0 )
  892. ret = 1;
  893. return( ret );
  894. }
  895. #endif /* MBEDTLS_SELF_TEST */
  896. #endif /* MBEDTLS_DES_C */