optimized_ikeys.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. //-----------------------------------------------------------------------------
  2. // Borrowed initially from https://github.com/holiman/loclass
  3. // Copyright (C) 2014 Martin Holst Swende
  4. // Copyright (C) Proxmark3 contributors. See AUTHORS.md for details.
  5. //
  6. // This program is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // This program is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU General Public License for more details.
  15. //
  16. // See LICENSE.txt for the text of the license.
  17. //-----------------------------------------------------------------------------
  18. // WARNING
  19. //
  20. // THIS CODE IS CREATED FOR EXPERIMENTATION AND EDUCATIONAL USE ONLY.
  21. //
  22. // USAGE OF THIS CODE IN OTHER WAYS MAY INFRINGE UPON THE INTELLECTUAL
  23. // PROPERTY OF OTHER PARTIES, SUCH AS INSIDE SECURE AND HID GLOBAL,
  24. // AND MAY EXPOSE YOU TO AN INFRINGEMENT ACTION FROM THOSE PARTIES.
  25. //
  26. // THIS CODE SHOULD NEVER BE USED TO INFRINGE PATENTS OR INTELLECTUAL PROPERTY RIGHTS.
  27. //-----------------------------------------------------------------------------
  28. // It is a reconstruction of the cipher engine used in iClass, and RFID techology.
  29. //
  30. // The implementation is based on the work performed by
  31. // Flavio D. Garcia, Gerhard de Koning Gans, Roel Verdult and
  32. // Milosch Meriac in the paper "Dismantling IClass".
  33. //-----------------------------------------------------------------------------
  34. /**
  35. From "Dismantling iclass":
  36. This section describes in detail the built-in key diversification algorithm of iClass.
  37. Besides the obvious purpose of deriving a card key from a master key, this
  38. algorithm intends to circumvent weaknesses in the cipher by preventing the
  39. usage of certain ‘weak’ keys. In order to compute a diversified key, the iClass
  40. reader first encrypts the card identity id with the master key K, using single
  41. DES. The resulting ciphertext is then input to a function called loclass_hash0 which
  42. outputs the diversified key k.
  43. k = loclass_hash0(DES enc (id, K))
  44. Here the DES encryption of id with master key K outputs a cryptogram c
  45. of 64 bits. These 64 bits are divided as c = x, y, z [0] , . . . , z [7] ∈ F 82 × F 82 × (F 62 ) 8
  46. which is used as input to the loclass_hash0 function. This function introduces some
  47. obfuscation by performing a number of permutations, complement and modulo
  48. operations, see Figure 2.5. Besides that, it checks for and removes patterns like
  49. similar key bytes, which could produce a strong bias in the cipher. Finally, the
  50. output of loclass_hash0 is the diversified card key k = k [0] , . . . , k [7] ∈ (F 82 ) 8 .
  51. **/
  52. #include "optimized_ikeys.h"
  53. #include <stdint.h>
  54. #include <stdbool.h>
  55. #include <inttypes.h>
  56. #include <mbedtls/des.h>
  57. #include "optimized_cipherutils.h"
  58. static const uint8_t loclass_pi[35] = {
  59. 0x0F, 0x17, 0x1B, 0x1D, 0x1E, 0x27, 0x2B, 0x2D,
  60. 0x2E, 0x33, 0x35, 0x39, 0x36, 0x3A, 0x3C, 0x47,
  61. 0x4B, 0x4D, 0x4E, 0x53, 0x55, 0x56, 0x59, 0x5A,
  62. 0x5C, 0x63, 0x65, 0x66, 0x69, 0x6A, 0x6C, 0x71,
  63. 0x72, 0x74, 0x78
  64. };
  65. /**
  66. * @brief The key diversification algorithm uses 6-bit bytes.
  67. * This implementation uses 64 bit uint to pack seven of them into one
  68. * variable. When they are there, they are placed as follows:
  69. * XXXX XXXX N0 .... N7, occupying the last 48 bits.
  70. *
  71. * This function picks out one from such a collection
  72. * @param all
  73. * @param n bitnumber
  74. * @return
  75. */
  76. static uint8_t loclass_getSixBitByte(uint64_t c, int n) {
  77. return (c >> (42 - 6 * n)) & 0x3F;
  78. }
  79. /**
  80. * @brief Puts back a six-bit 'byte' into a uint64_t.
  81. * @param c buffer
  82. * @param z the value to place there
  83. * @param n bitnumber.
  84. */
  85. static void loclass_pushbackSixBitByte(uint64_t *c, uint8_t z, int n) {
  86. //0x XXXX YYYY ZZZZ ZZZZ ZZZZ
  87. // ^z0 ^z7
  88. //z0: 1111 1100 0000 0000
  89. uint64_t masked = z & 0x3F;
  90. uint64_t eraser = 0x3F;
  91. masked <<= 42 - 6 * n;
  92. eraser <<= 42 - 6 * n;
  93. //masked <<= 6*n;
  94. //eraser <<= 6*n;
  95. eraser = ~eraser;
  96. (*c) &= eraser;
  97. (*c) |= masked;
  98. }
  99. /**
  100. * @brief Swaps the z-values.
  101. * If the input value has format XYZ0Z1...Z7, the output will have the format
  102. * XYZ7Z6...Z0 instead
  103. * @param c
  104. * @return
  105. */
  106. static uint64_t loclass_swapZvalues(uint64_t c) {
  107. uint64_t newz = 0;
  108. loclass_pushbackSixBitByte(&newz, loclass_getSixBitByte(c, 0), 7);
  109. loclass_pushbackSixBitByte(&newz, loclass_getSixBitByte(c, 1), 6);
  110. loclass_pushbackSixBitByte(&newz, loclass_getSixBitByte(c, 2), 5);
  111. loclass_pushbackSixBitByte(&newz, loclass_getSixBitByte(c, 3), 4);
  112. loclass_pushbackSixBitByte(&newz, loclass_getSixBitByte(c, 4), 3);
  113. loclass_pushbackSixBitByte(&newz, loclass_getSixBitByte(c, 5), 2);
  114. loclass_pushbackSixBitByte(&newz, loclass_getSixBitByte(c, 6), 1);
  115. loclass_pushbackSixBitByte(&newz, loclass_getSixBitByte(c, 7), 0);
  116. newz |= (c & 0xFFFF000000000000);
  117. return newz;
  118. }
  119. /**
  120. * @return 4 six-bit bytes chunked into a uint64_t,as 00..00a0a1a2a3
  121. */
  122. static uint64_t loclass_ck(int i, int j, uint64_t z) {
  123. if (i == 1 && j == -1) {
  124. // loclass_ck(1, −1, z [0] . . . z [3] ) = z [0] . . . z [3]
  125. return z;
  126. } else if (j == -1) {
  127. // loclass_ck(i, −1, z [0] . . . z [3] ) = loclass_ck(i − 1, i − 2, z [0] . . . z [3] )
  128. return loclass_ck(i - 1, i - 2, z);
  129. }
  130. if (loclass_getSixBitByte(z, i) == loclass_getSixBitByte(z, j)) {
  131. //loclass_ck(i, j − 1, z [0] . . . z [i] ← j . . . z [3] )
  132. uint64_t newz = 0;
  133. int c;
  134. for (c = 0; c < 4; c++) {
  135. uint8_t val = loclass_getSixBitByte(z, c);
  136. if (c == i)
  137. loclass_pushbackSixBitByte(&newz, j, c);
  138. else
  139. loclass_pushbackSixBitByte(&newz, val, c);
  140. }
  141. return loclass_ck(i, j - 1, newz);
  142. } else {
  143. return loclass_ck(i, j - 1, z);
  144. }
  145. }
  146. /**
  147. Definition 8.
  148. Let the function check : (F 62 ) 8 → (F 62 ) 8 be defined as
  149. check(z [0] . . . z [7] ) = loclass_ck(3, 2, z [0] . . . z [3] ) · loclass_ck(3, 2, z [4] . . . z [7] )
  150. where loclass_ck : N × N × (F 62 ) 4 → (F 62 ) 4 is defined as
  151. loclass_ck(1, −1, z [0] . . . z [3] ) = z [0] . . . z [3]
  152. loclass_ck(i, −1, z [0] . . . z [3] ) = loclass_ck(i − 1, i − 2, z [0] . . . z [3] )
  153. loclass_ck(i, j, z [0] . . . z [3] ) =
  154. loclass_ck(i, j − 1, z [0] . . . z [i] ← j . . . z [3] ), if z [i] = z [j] ;
  155. loclass_ck(i, j − 1, z [0] . . . z [3] ), otherwise
  156. otherwise.
  157. **/
  158. static uint64_t loclass_check(uint64_t z) {
  159. //These 64 bits are divided as c = x, y, z [0] , . . . , z [7]
  160. // loclass_ck(3, 2, z [0] . . . z [3] )
  161. uint64_t ck1 = loclass_ck(3, 2, z);
  162. // loclass_ck(3, 2, z [4] . . . z [7] )
  163. uint64_t ck2 = loclass_ck(3, 2, z << 24);
  164. //The loclass_ck function will place the values
  165. // in the middle of z.
  166. ck1 &= 0x00000000FFFFFF000000;
  167. ck2 &= 0x00000000FFFFFF000000;
  168. return ck1 | ck2 >> 24;
  169. }
  170. static void loclass_permute(LoclassBitstreamIn_t *p_in, uint64_t z, int l, int r, LoclassBitstreamOut_t *out) {
  171. if (loclass_bitsLeft(p_in) == 0)
  172. return;
  173. bool pn = loclass_tailBit(p_in);
  174. if (pn) { // pn = 1
  175. uint8_t zl = loclass_getSixBitByte(z, l);
  176. loclass_push6bits(out, zl + 1);
  177. loclass_permute(p_in, z, l + 1, r, out);
  178. } else { // otherwise
  179. uint8_t zr = loclass_getSixBitByte(z, r);
  180. loclass_push6bits(out, zr);
  181. loclass_permute(p_in, z, l, r + 1, out);
  182. }
  183. }
  184. /**
  185. * @brief
  186. *Definition 11. Let the function loclass_hash0 : F 82 × F 82 × (F 62 ) 8 → (F 82 ) 8 be defined as
  187. * loclass_hash0(x, y, z [0] . . . z [7] ) = k [0] . . . k [7] where
  188. * z'[i] = (z[i] mod (63-i)) + i i = 0...3
  189. * z'[i+4] = (z[i+4] mod (64-i)) + i i = 0...3
  190. * ẑ = check(z');
  191. * @param c
  192. * @param k this is where the diversified key is put (should be 8 bytes)
  193. * @return
  194. */
  195. void loclass_hash0(uint64_t c, uint8_t k[8]) {
  196. c = loclass_swapZvalues(c);
  197. //These 64 bits are divided as c = x, y, z [0] , . . . , z [7]
  198. // x = 8 bits
  199. // y = 8 bits
  200. // z0-z7 6 bits each : 48 bits
  201. uint8_t x = (c & 0xFF00000000000000) >> 56;
  202. uint8_t y = (c & 0x00FF000000000000) >> 48;
  203. uint64_t zP = 0;
  204. for (int n = 0; n < 4 ; n++) {
  205. uint8_t zn = loclass_getSixBitByte(c, n);
  206. uint8_t zn4 = loclass_getSixBitByte(c, n + 4);
  207. uint8_t _zn = (zn % (63 - n)) + n;
  208. uint8_t _zn4 = (zn4 % (64 - n)) + n;
  209. loclass_pushbackSixBitByte(&zP, _zn, n);
  210. loclass_pushbackSixBitByte(&zP, _zn4, n + 4);
  211. }
  212. uint64_t zCaret = loclass_check(zP);
  213. uint8_t p = loclass_pi[x % 35];
  214. if (x & 1) //Check if x7 is 1
  215. p = ~p;
  216. LoclassBitstreamIn_t p_in = { &p, 8, 0 };
  217. uint8_t outbuffer[] = {0, 0, 0, 0, 0, 0, 0, 0};
  218. LoclassBitstreamOut_t out = {outbuffer, 0, 0};
  219. loclass_permute(&p_in, zCaret, 0, 4, &out); //returns 48 bits? or 6 8-bytes
  220. //Out is now a buffer containing six-bit bytes, should be 48 bits
  221. // if all went well
  222. //Shift z-values down onto the lower segment
  223. uint64_t zTilde = loclass_x_bytes_to_num(outbuffer, sizeof(outbuffer));
  224. zTilde >>= 16;
  225. for (int i = 0; i < 8; i++) {
  226. // the key on index i is first a bit from y
  227. // then six bits from z,
  228. // then a bit from p
  229. // Init with zeroes
  230. k[i] = 0;
  231. // First, place yi leftmost in k
  232. //k[i] |= (y << i) & 0x80 ;
  233. // First, place y(7-i) leftmost in k
  234. k[i] |= (y << (7 - i)) & 0x80 ;
  235. uint8_t zTilde_i = loclass_getSixBitByte(zTilde, i);
  236. // zTildeI is now on the form 00XXXXXX
  237. // with one leftshift, it'll be
  238. // 0XXXXXX0
  239. // So after leftshift, we can OR it into k
  240. // However, when doing complement, we need to
  241. // again MASK 0XXXXXX0 (0x7E)
  242. zTilde_i <<= 1;
  243. //Finally, add bit from p or p-mod
  244. //Shift bit i into rightmost location (mask only after complement)
  245. uint8_t p_i = p >> i & 0x1;
  246. if (k[i]) { // yi = 1
  247. k[i] |= ~zTilde_i & 0x7E;
  248. k[i] |= p_i & 1;
  249. k[i] += 1;
  250. } else { // otherwise
  251. k[i] |= zTilde_i & 0x7E;
  252. k[i] |= (~p_i) & 1;
  253. }
  254. }
  255. }
  256. /**
  257. * @brief Performs Elite-class key diversification
  258. * @param csn
  259. * @param key
  260. * @param div_key
  261. */
  262. void loclass_diversifyKey(uint8_t *csn, const uint8_t *key, uint8_t *div_key) {
  263. mbedtls_des_context loclass_ctx_enc;
  264. // Prepare the DES key
  265. mbedtls_des_setkey_enc(&loclass_ctx_enc, key);
  266. uint8_t crypted_csn[8] = {0};
  267. // Calculate DES(CSN, KEY)
  268. mbedtls_des_crypt_ecb(&loclass_ctx_enc, csn, crypted_csn);
  269. //Calculate HASH0(DES))
  270. uint64_t c_csn = loclass_x_bytes_to_num(crypted_csn, sizeof(crypted_csn));
  271. loclass_hash0(c_csn, div_key);
  272. }