optimized_ikeys.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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] = {0x0F, 0x17, 0x1B, 0x1D, 0x1E, 0x27, 0x2B, 0x2D, 0x2E,
  59. 0x33, 0x35, 0x39, 0x36, 0x3A, 0x3C, 0x47, 0x4B, 0x4D,
  60. 0x4E, 0x53, 0x55, 0x56, 0x59, 0x5A, 0x5C, 0x63, 0x65,
  61. 0x66, 0x69, 0x6A, 0x6C, 0x71, 0x72, 0x74, 0x78};
  62. /**
  63. * @brief The key diversification algorithm uses 6-bit bytes.
  64. * This implementation uses 64 bit uint to pack seven of them into one
  65. * variable. When they are there, they are placed as follows:
  66. * XXXX XXXX N0 .... N7, occupying the last 48 bits.
  67. *
  68. * This function picks out one from such a collection
  69. * @param all
  70. * @param n bitnumber
  71. * @return
  72. */
  73. static uint8_t loclass_getSixBitByte(uint64_t c, int n) {
  74. return (c >> (42 - 6 * n)) & 0x3F;
  75. }
  76. /**
  77. * @brief Puts back a six-bit 'byte' into a uint64_t.
  78. * @param c buffer
  79. * @param z the value to place there
  80. * @param n bitnumber.
  81. */
  82. static void loclass_pushbackSixBitByte(uint64_t* c, uint8_t z, int n) {
  83. //0x XXXX YYYY ZZZZ ZZZZ ZZZZ
  84. // ^z0 ^z7
  85. //z0: 1111 1100 0000 0000
  86. uint64_t masked = z & 0x3F;
  87. uint64_t eraser = 0x3F;
  88. masked <<= 42 - 6 * n;
  89. eraser <<= 42 - 6 * n;
  90. //masked <<= 6*n;
  91. //eraser <<= 6*n;
  92. eraser = ~eraser;
  93. (*c) &= eraser;
  94. (*c) |= masked;
  95. }
  96. /**
  97. * @brief Swaps the z-values.
  98. * If the input value has format XYZ0Z1...Z7, the output will have the format
  99. * XYZ7Z6...Z0 instead
  100. * @param c
  101. * @return
  102. */
  103. static uint64_t loclass_swapZvalues(uint64_t c) {
  104. uint64_t newz = 0;
  105. loclass_pushbackSixBitByte(&newz, loclass_getSixBitByte(c, 0), 7);
  106. loclass_pushbackSixBitByte(&newz, loclass_getSixBitByte(c, 1), 6);
  107. loclass_pushbackSixBitByte(&newz, loclass_getSixBitByte(c, 2), 5);
  108. loclass_pushbackSixBitByte(&newz, loclass_getSixBitByte(c, 3), 4);
  109. loclass_pushbackSixBitByte(&newz, loclass_getSixBitByte(c, 4), 3);
  110. loclass_pushbackSixBitByte(&newz, loclass_getSixBitByte(c, 5), 2);
  111. loclass_pushbackSixBitByte(&newz, loclass_getSixBitByte(c, 6), 1);
  112. loclass_pushbackSixBitByte(&newz, loclass_getSixBitByte(c, 7), 0);
  113. newz |= (c & 0xFFFF000000000000);
  114. return newz;
  115. }
  116. /**
  117. * @return 4 six-bit bytes chunked into a uint64_t,as 00..00a0a1a2a3
  118. */
  119. static uint64_t loclass_ck(int i, int j, uint64_t z) {
  120. if(i == 1 && j == -1) {
  121. // loclass_ck(1, −1, z [0] . . . z [3] ) = z [0] . . . z [3]
  122. return z;
  123. } else if(j == -1) {
  124. // loclass_ck(i, −1, z [0] . . . z [3] ) = loclass_ck(i − 1, i − 2, z [0] . . . z [3] )
  125. return loclass_ck(i - 1, i - 2, z);
  126. }
  127. if(loclass_getSixBitByte(z, i) == loclass_getSixBitByte(z, j)) {
  128. //loclass_ck(i, j − 1, z [0] . . . z [i] ← j . . . z [3] )
  129. uint64_t newz = 0;
  130. int c;
  131. for(c = 0; c < 4; c++) {
  132. uint8_t val = loclass_getSixBitByte(z, c);
  133. if(c == i)
  134. loclass_pushbackSixBitByte(&newz, j, c);
  135. else
  136. loclass_pushbackSixBitByte(&newz, val, c);
  137. }
  138. return loclass_ck(i, j - 1, newz);
  139. } else {
  140. return loclass_ck(i, j - 1, z);
  141. }
  142. }
  143. /**
  144. Definition 8.
  145. Let the function check : (F 62 ) 8 → (F 62 ) 8 be defined as
  146. check(z [0] . . . z [7] ) = loclass_ck(3, 2, z [0] . . . z [3] ) · loclass_ck(3, 2, z [4] . . . z [7] )
  147. where loclass_ck : N × N × (F 62 ) 4 → (F 62 ) 4 is defined as
  148. loclass_ck(1, −1, z [0] . . . z [3] ) = z [0] . . . z [3]
  149. loclass_ck(i, −1, z [0] . . . z [3] ) = loclass_ck(i − 1, i − 2, z [0] . . . z [3] )
  150. loclass_ck(i, j, z [0] . . . z [3] ) =
  151. loclass_ck(i, j − 1, z [0] . . . z [i] ← j . . . z [3] ), if z [i] = z [j] ;
  152. loclass_ck(i, j − 1, z [0] . . . z [3] ), otherwise
  153. otherwise.
  154. **/
  155. static uint64_t loclass_check(uint64_t z) {
  156. //These 64 bits are divided as c = x, y, z [0] , . . . , z [7]
  157. // loclass_ck(3, 2, z [0] . . . z [3] )
  158. uint64_t ck1 = loclass_ck(3, 2, z);
  159. // loclass_ck(3, 2, z [4] . . . z [7] )
  160. uint64_t ck2 = loclass_ck(3, 2, z << 24);
  161. //The loclass_ck function will place the values
  162. // in the middle of z.
  163. ck1 &= 0x00000000FFFFFF000000;
  164. ck2 &= 0x00000000FFFFFF000000;
  165. return ck1 | ck2 >> 24;
  166. }
  167. static void loclass_permute(
  168. LoclassBitstreamIn_t* p_in,
  169. uint64_t z,
  170. int l,
  171. int r,
  172. LoclassBitstreamOut_t* out) {
  173. if(loclass_bitsLeft(p_in) == 0) return;
  174. bool pn = loclass_tailBit(p_in);
  175. if(pn) { // pn = 1
  176. uint8_t zl = loclass_getSixBitByte(z, l);
  177. loclass_push6bits(out, zl + 1);
  178. loclass_permute(p_in, z, l + 1, r, out);
  179. } else { // otherwise
  180. uint8_t zr = loclass_getSixBitByte(z, r);
  181. loclass_push6bits(out, zr);
  182. loclass_permute(p_in, z, l, r + 1, out);
  183. }
  184. }
  185. /**
  186. * @brief
  187. *Definition 11. Let the function loclass_hash0 : F 82 × F 82 × (F 62 ) 8 → (F 82 ) 8 be defined as
  188. * loclass_hash0(x, y, z [0] . . . z [7] ) = k [0] . . . k [7] where
  189. * z'[i] = (z[i] mod (63-i)) + i i = 0...3
  190. * z'[i+4] = (z[i+4] mod (64-i)) + i i = 0...3
  191. * ẑ = check(z');
  192. * @param c
  193. * @param k this is where the diversified key is put (should be 8 bytes)
  194. * @return
  195. */
  196. void loclass_hash0(uint64_t c, uint8_t k[8]) {
  197. c = loclass_swapZvalues(c);
  198. //These 64 bits are divided as c = x, y, z [0] , . . . , z [7]
  199. // x = 8 bits
  200. // y = 8 bits
  201. // z0-z7 6 bits each : 48 bits
  202. uint8_t x = (c & 0xFF00000000000000) >> 56;
  203. uint8_t y = (c & 0x00FF000000000000) >> 48;
  204. uint64_t zP = 0;
  205. for(int n = 0; n < 4; n++) {
  206. uint8_t zn = loclass_getSixBitByte(c, n);
  207. uint8_t zn4 = loclass_getSixBitByte(c, n + 4);
  208. uint8_t _zn = (zn % (63 - n)) + n;
  209. uint8_t _zn4 = (zn4 % (64 - n)) + n;
  210. loclass_pushbackSixBitByte(&zP, _zn, n);
  211. loclass_pushbackSixBitByte(&zP, _zn4, n + 4);
  212. }
  213. uint64_t zCaret = loclass_check(zP);
  214. uint8_t p = loclass_pi[x % 35];
  215. if(x & 1) //Check if x7 is 1
  216. p = ~p;
  217. LoclassBitstreamIn_t p_in = {&p, 8, 0};
  218. uint8_t outbuffer[] = {0, 0, 0, 0, 0, 0, 0, 0};
  219. LoclassBitstreamOut_t out = {outbuffer, 0, 0};
  220. loclass_permute(&p_in, zCaret, 0, 4, &out); //returns 48 bits? or 6 8-bytes
  221. //Out is now a buffer containing six-bit bytes, should be 48 bits
  222. // if all went well
  223. //Shift z-values down onto the lower segment
  224. uint64_t zTilde = loclass_x_bytes_to_num(outbuffer, sizeof(outbuffer));
  225. zTilde >>= 16;
  226. for(int i = 0; i < 8; i++) {
  227. // the key on index i is first a bit from y
  228. // then six bits from z,
  229. // then a bit from p
  230. // Init with zeroes
  231. k[i] = 0;
  232. // First, place yi leftmost in k
  233. //k[i] |= (y << i) & 0x80 ;
  234. // First, place y(7-i) leftmost in k
  235. k[i] |= (y << (7 - i)) & 0x80;
  236. uint8_t zTilde_i = loclass_getSixBitByte(zTilde, i);
  237. // zTildeI is now on the form 00XXXXXX
  238. // with one leftshift, it'll be
  239. // 0XXXXXX0
  240. // So after leftshift, we can OR it into k
  241. // However, when doing complement, we need to
  242. // again MASK 0XXXXXX0 (0x7E)
  243. zTilde_i <<= 1;
  244. //Finally, add bit from p or p-mod
  245. //Shift bit i into rightmost location (mask only after complement)
  246. uint8_t p_i = p >> i & 0x1;
  247. if(k[i]) { // yi = 1
  248. k[i] |= ~zTilde_i & 0x7E;
  249. k[i] |= p_i & 1;
  250. k[i] += 1;
  251. } else { // otherwise
  252. k[i] |= zTilde_i & 0x7E;
  253. k[i] |= (~p_i) & 1;
  254. }
  255. }
  256. }
  257. /**
  258. * @brief Performs Elite-class key diversification
  259. * @param csn
  260. * @param key
  261. * @param div_key
  262. */
  263. void loclass_diversifyKey(const uint8_t* csn, const uint8_t* key, uint8_t* div_key) {
  264. mbedtls_des_context loclass_ctx_enc;
  265. // Prepare the DES key
  266. mbedtls_des_setkey_enc(&loclass_ctx_enc, key);
  267. uint8_t crypted_csn[8] = {0};
  268. // Calculate DES(CSN, KEY)
  269. mbedtls_des_crypt_ecb(&loclass_ctx_enc, csn, crypted_csn);
  270. //Calculate HASH0(DES))
  271. uint64_t c_csn = loclass_x_bytes_to_num(crypted_csn, sizeof(crypted_csn));
  272. loclass_hash0(c_csn, div_key);
  273. }