Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /* crapto1.c
  2. This program is free software; you can redistribute it and/or
  3. modify it under the terms of the GNU General Public License
  4. as published by the Free Software Foundation; either version 2
  5. of the License, or (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program; if not, write to the Free Software
  12. Foundation, Inc., 51 Franklin Street, Fifth Floor,
  13. Boston, MA 02110-1301, US$
  14. Copyright (C) 2008-2008 bla <blapost@gmail.com>
  15. */
  16. #include "crapto1.h"
  17. #include <stdlib.h>
  18. #if !defined LOWMEM && defined __GNUC__
  19. static uint8_t filterlut[1 << 20];
  20. static void __attribute__((constructor)) fill_lut(void)
  21. {
  22. uint32_t i;
  23. for (i = 0; i < 1 << 20; ++i)
  24. filterlut[i] = filter(i);
  25. }
  26. #define filter(x) (filterlut[(x) & 0xfffff])
  27. #endif
  28. static void quicksort(uint32_t *const start, uint32_t *const stop)
  29. {
  30. uint32_t *it = start + 1, *rit = stop;
  31. if (it > rit)
  32. return;
  33. while (it < rit)
  34. if (*it <= *start)
  35. ++it;
  36. else if (*rit > *start)
  37. --rit;
  38. else
  39. *it ^= (*it ^= *rit, *rit ^= *it);
  40. if (*rit >= *start)
  41. --rit;
  42. if (rit != start)
  43. *rit ^= (*rit ^= *start, *start ^= *rit);
  44. quicksort(start, rit - 1);
  45. quicksort(rit + 1, stop);
  46. }
  47. /** binsearch
  48. * Binary search for the first occurence of *stop's MSB in sorted [start,stop]
  49. */
  50. static inline uint32_t *
  51. binsearch(uint32_t *start, uint32_t *stop)
  52. {
  53. uint32_t mid, val = *stop & 0xff000000;
  54. while (start != stop)
  55. if (start[mid = (stop - start) >> 1] > val)
  56. stop = &start[mid];
  57. else
  58. start += mid + 1;
  59. return start;
  60. }
  61. /** update_contribution
  62. * helper, calculates the partial linear feedback contributions and puts in MSB
  63. */
  64. static inline void
  65. update_contribution(uint32_t *item, const uint32_t mask1, const uint32_t mask2)
  66. {
  67. uint32_t p = *item >> 25;
  68. p = p << 1 | parity(*item & mask1);
  69. p = p << 1 | parity(*item & mask2);
  70. *item = p << 24 | (*item & 0xffffff);
  71. }
  72. /** extend_table
  73. * using a bit of the keystream extend the table of possible lfsr states
  74. */
  75. static inline void
  76. extend_table(uint32_t *tbl, uint32_t **end, int bit, int m1, int m2, uint32_t in)
  77. {
  78. in <<= 24;
  79. for (*tbl <<= 1; tbl <= *end; *++tbl <<= 1)
  80. if (filter(*tbl) ^ filter(*tbl | 1)) {
  81. *tbl |= filter(*tbl) ^ bit;
  82. update_contribution(tbl, m1, m2);
  83. *tbl ^= in;
  84. } else if (filter(*tbl) == bit) {
  85. *++*end = tbl[1];
  86. tbl[1] = tbl[0] | 1;
  87. update_contribution(tbl, m1, m2);
  88. *tbl++ ^= in;
  89. update_contribution(tbl, m1, m2);
  90. *tbl ^= in;
  91. } else
  92. *tbl-- = *(*end)--;
  93. }
  94. /** extend_table_simple
  95. * using a bit of the keystream extend the table of possible lfsr states
  96. */
  97. static inline void
  98. extend_table_simple(uint32_t *tbl, uint32_t **end, int bit)
  99. {
  100. for (*tbl <<= 1; tbl <= *end; *++tbl <<= 1)
  101. if (filter(*tbl) ^ filter(*tbl | 1)) {
  102. *tbl |= filter(*tbl) ^ bit;
  103. } else if (filter(*tbl) == bit) {
  104. *++*end = *++tbl;
  105. *tbl = tbl[-1] | 1;
  106. } else
  107. *tbl-- = *(*end)--;
  108. }
  109. /** recover
  110. * recursively narrow down the search space, 4 bits of keystream at a time
  111. */
  112. static struct Crypto1State *
  113. recover(uint32_t *o_head, uint32_t *o_tail, uint32_t oks,
  114. uint32_t *e_head, uint32_t *e_tail, uint32_t eks, int rem,
  115. struct Crypto1State *sl, uint32_t in) {
  116. uint32_t *o, *e, i;
  117. if (rem == -1) {
  118. for (e = e_head; e <= e_tail; ++e) {
  119. *e = *e << 1 ^ parity(*e & LF_POLY_EVEN) ^ !!(in & 4);
  120. for (o = o_head; o <= o_tail; ++o, ++sl) {
  121. sl->even = *o;
  122. sl->odd = *e ^ parity(*o & LF_POLY_ODD);
  123. sl[1].odd = sl[1].even = 0;
  124. }
  125. }
  126. return sl;
  127. }
  128. for (i = 0; i < 4 && rem--; i++) {
  129. extend_table(o_head, &o_tail, (oks >>= 1) & 1,
  130. LF_POLY_EVEN << 1 | 1, LF_POLY_ODD << 1, 0);
  131. if (o_head > o_tail)
  132. return sl;
  133. extend_table(e_head, &e_tail, (eks >>= 1) & 1,
  134. LF_POLY_ODD, LF_POLY_EVEN << 1 | 1, (in >>= 2) & 3);
  135. if (e_head > e_tail)
  136. return sl;
  137. }
  138. quicksort(o_head, o_tail);
  139. quicksort(e_head, e_tail);
  140. while (o_tail >= o_head && e_tail >= e_head)
  141. if (((*o_tail ^ *e_tail) >> 24) == 0) {
  142. o_tail = binsearch(o_head, o = o_tail);
  143. e_tail = binsearch(e_head, e = e_tail);
  144. sl = recover(o_tail--, o, oks,
  145. e_tail--, e, eks, rem, sl, in);
  146. } else if (*o_tail > *e_tail)
  147. o_tail = binsearch(o_head, o_tail) - 1;
  148. else
  149. e_tail = binsearch(e_head, e_tail) - 1;
  150. return sl;
  151. }
  152. /** lfsr_recovery
  153. * recover the state of the lfsr given 32 bits of the keystream
  154. * additionally you can use the in parameter to specify the value
  155. * that was fed into the lfsr at the time the keystream was generated
  156. */
  157. struct Crypto1State *lfsr_recovery32(uint32_t ks2, uint32_t in) {
  158. struct Crypto1State *statelist;
  159. uint32_t *odd_head = 0, *odd_tail = 0, oks = 0;
  160. uint32_t *even_head = 0, *even_tail = 0, eks = 0;
  161. int i;
  162. for (i = 31; i >= 0; i -= 2)
  163. oks = oks << 1 | BEBIT(ks2, i);
  164. for (i = 30; i >= 0; i -= 2)
  165. eks = eks << 1 | BEBIT(ks2, i);
  166. odd_head = odd_tail = malloc(sizeof(uint32_t) << 21);
  167. even_head = even_tail = malloc(sizeof(uint32_t) << 21);
  168. statelist = malloc(sizeof(struct Crypto1State) << 18);
  169. if (!odd_tail-- || !even_tail-- || !statelist)
  170. goto out;
  171. statelist->odd = statelist->even = 0;
  172. for (i = 1 << 20; i >= 0; --i) {
  173. if (filter(i) == (oks & 1))
  174. *++odd_tail = i;
  175. if (filter(i) == (eks & 1))
  176. *++even_tail = i;
  177. }
  178. for (i = 0; i < 4; i++) {
  179. extend_table_simple(odd_head, &odd_tail, (oks >>= 1) & 1);
  180. extend_table_simple(even_head, &even_tail, (eks >>= 1) & 1);
  181. }
  182. in = (in >> 16 & 0xff) | (in << 16) | (in & 0xff00);
  183. recover(odd_head, odd_tail, oks,
  184. even_head, even_tail, eks, 11, statelist, in << 1);
  185. out:
  186. free(odd_head);
  187. free(even_head);
  188. return statelist;
  189. }
  190. static const uint32_t S1[] = { 0x62141, 0x310A0, 0x18850, 0x0C428, 0x06214,
  191. 0x0310A, 0x85E30, 0xC69AD, 0x634D6, 0xB5CDE, 0xDE8DA, 0x6F46D, 0xB3C83,
  192. 0x59E41, 0xA8995, 0xD027F, 0x6813F, 0x3409F, 0x9E6FA
  193. };
  194. static const uint32_t S2[] = { 0x3A557B00, 0x5D2ABD80, 0x2E955EC0, 0x174AAF60,
  195. 0x0BA557B0, 0x05D2ABD8, 0x0449DE68, 0x048464B0, 0x42423258, 0x278192A8,
  196. 0x156042D0, 0x0AB02168, 0x43F89B30, 0x61FC4D98, 0x765EAD48, 0x7D8FDD20,
  197. 0x7EC7EE90, 0x7F63F748, 0x79117020
  198. };
  199. static const uint32_t T1[] = {
  200. 0x4F37D, 0x279BE, 0x97A6A, 0x4BD35, 0x25E9A, 0x12F4D, 0x097A6, 0x80D66,
  201. 0xC4006, 0x62003, 0xB56B4, 0x5AB5A, 0xA9318, 0xD0F39, 0x6879C, 0xB057B,
  202. 0x582BD, 0x2C15E, 0x160AF, 0x8F6E2, 0xC3DC4, 0xE5857, 0x72C2B, 0x39615,
  203. 0x98DBF, 0xC806A, 0xE0680, 0x70340, 0x381A0, 0x98665, 0x4C332, 0xA272C
  204. };
  205. static const uint32_t T2[] = { 0x3C88B810, 0x5E445C08, 0x2982A580, 0x14C152C0,
  206. 0x4A60A960, 0x253054B0, 0x52982A58, 0x2FEC9EA8, 0x1156C4D0, 0x08AB6268,
  207. 0x42F53AB0, 0x217A9D58, 0x161DC528, 0x0DAE6910, 0x46D73488, 0x25CB11C0,
  208. 0x52E588E0, 0x6972C470, 0x34B96238, 0x5CFC3A98, 0x28DE96C8, 0x12CFC0E0,
  209. 0x4967E070, 0x64B3F038, 0x74F97398, 0x7CDC3248, 0x38CE92A0, 0x1C674950,
  210. 0x0E33A4A8, 0x01B959D0, 0x40DCACE8, 0x26CEDDF0
  211. };
  212. static const uint32_t C1[] = { 0x846B5, 0x4235A, 0x211AD};
  213. static const uint32_t C2[] = { 0x1A822E0, 0x21A822E0, 0x21A822E0};
  214. /** Reverse 64 bits of keystream into possible cipher states
  215. * Variation mentioned in the paper. Somewhat optimized version
  216. */
  217. struct Crypto1State *lfsr_recovery64(uint32_t ks2, uint32_t ks3) {
  218. struct Crypto1State *statelist, *sl;
  219. uint8_t oks[32], eks[32], hi[32];
  220. uint32_t low = 0, win = 0;
  221. uint32_t *tail, table[1 << 16];
  222. int i, j;
  223. sl = statelist = malloc(sizeof(struct Crypto1State) << 4);
  224. if (!sl)
  225. return 0;
  226. sl->odd = sl->even = 0;
  227. for (i = 30; i >= 0; i -= 2) {
  228. oks[i >> 1] = BIT(ks2, i ^ 24);
  229. oks[16 + (i >> 1)] = BIT(ks3, i ^ 24);
  230. }
  231. for (i = 31; i >= 0; i -= 2) {
  232. eks[i >> 1] = BIT(ks2, i ^ 24);
  233. eks[16 + (i >> 1)] = BIT(ks3, i ^ 24);
  234. }
  235. for (i = 0xfffff; i >= 0; --i) {
  236. if (filter(i) != oks[0])
  237. continue;
  238. *(tail = table) = i;
  239. for (j = 1; tail >= table && j < 29; ++j)
  240. extend_table_simple(table, &tail, oks[j]);
  241. if (tail < table)
  242. continue;
  243. for (j = 0; j < 19; ++j)
  244. low = low << 1 | parity(i & S1[j]);
  245. for (j = 0; j < 32; ++j)
  246. hi[j] = parity(i & T1[j]);
  247. for (; tail >= table; --tail) {
  248. for (j = 0; j < 3; ++j) {
  249. *tail = *tail << 1;
  250. *tail |= parity((i & C1[j]) ^(*tail & C2[j]));
  251. if (filter(*tail) != oks[29 + j])
  252. goto continue2;
  253. }
  254. for (j = 0; j < 19; ++j)
  255. win = win << 1 | parity(*tail & S2[j]);
  256. win ^= low;
  257. for (j = 0; j < 32; ++j) {
  258. win = win << 1 ^ hi[j] ^ parity(*tail & T2[j]);
  259. if (filter(win) != eks[j])
  260. goto continue2;
  261. }
  262. *tail = *tail << 1 | parity(LF_POLY_EVEN & *tail);
  263. sl->odd = *tail ^ parity(LF_POLY_ODD & win);
  264. sl->even = win;
  265. ++sl;
  266. sl->odd = sl->even = 0;
  267. continue2:
  268. ;
  269. }
  270. }
  271. return statelist;
  272. }
  273. uint8_t lfsr_rollback_bit(struct Crypto1State *s, uint32_t in, int fb);
  274. uint8_t lfsr_rollback_byte(struct Crypto1State *s, uint32_t in, int fb);
  275. uint32_t *lfsr_prefix_ks(uint8_t ks[8], int isodd);
  276. /** lfsr_rollback_bit
  277. * Rollback the shift register in order to get previous states
  278. */
  279. uint8_t lfsr_rollback_bit(struct Crypto1State *s, uint32_t in, int fb)
  280. {
  281. int out;
  282. uint8_t ret;
  283. s->odd &= 0xffffff;
  284. s->odd ^= (s->odd ^= s->even, s->even ^= s->odd);
  285. out = s->even & 1;
  286. out ^= LF_POLY_EVEN & (s->even >>= 1);
  287. out ^= LF_POLY_ODD & s->odd;
  288. out ^= !!in;
  289. out ^= (ret = filter(s->odd)) & !!fb;
  290. s->even |= parity(out) << 23;
  291. return ret;
  292. }
  293. /** lfsr_rollback_byte
  294. * Rollback the shift register in order to get previous states
  295. */
  296. uint8_t lfsr_rollback_byte(struct Crypto1State *s, uint32_t in, int fb)
  297. {
  298. int i;
  299. uint8_t ret = 0;
  300. for (i = 7; i >= 0; --i)
  301. ret |= lfsr_rollback_bit(s, BIT(in, i), fb) << i;
  302. return ret;
  303. }
  304. /** lfsr_rollback_word
  305. * Rollback the shift register in order to get previous states
  306. */
  307. uint32_t lfsr_rollback_word(struct Crypto1State *s, uint32_t in, int fb)
  308. {
  309. int i;
  310. uint32_t ret = 0;
  311. for (i = 31; i >= 0; --i)
  312. ret |= lfsr_rollback_bit(s, BEBIT(in, i), fb) << (i ^ 24);
  313. return ret;
  314. }
  315. /** nonce_distance
  316. * x,y valid tag nonces, then prng_successor(x, nonce_distance(x, y)) = y
  317. */
  318. static uint16_t *dist = 0;
  319. int nonce_distance(uint32_t from, uint32_t to)
  320. {
  321. uint16_t x, i;
  322. if (!dist) {
  323. dist = malloc(2 << 16);
  324. if (!dist)
  325. return -1;
  326. for (x = i = 1; i; ++i) {
  327. dist[(x & 0xff) << 8 | x >> 8] = i;
  328. x = x >> 1 | (x ^ x >> 2 ^ x >> 3 ^ x >> 5) << 15;
  329. }
  330. }
  331. return (65535 + dist[to >> 16] - dist[from >> 16]) % 65535;
  332. }
  333. static uint32_t fastfwd[2][8] = {
  334. { 0, 0x4BC53, 0xECB1, 0x450E2, 0x25E29, 0x6E27A, 0x2B298, 0x60ECB},
  335. { 0, 0x1D962, 0x4BC53, 0x56531, 0xECB1, 0x135D3, 0x450E2, 0x58980}
  336. };
  337. /** lfsr_prefix_ks
  338. *
  339. * Is an exported helper function from the common prefix attack
  340. * Described in the "dark side" paper. It returns an -1 terminated array
  341. * of possible partial(21 bit) secret state.
  342. * The required keystream(ks) needs to contain the keystream that was used to
  343. * encrypt the NACK which is observed when varying only the 4 last bits of Nr
  344. * only correct iff [NR_3] ^ NR_3 does not depend on Nr_3
  345. */
  346. uint32_t *lfsr_prefix_ks(uint8_t ks[8], int isodd)
  347. {
  348. uint32_t c, entry, *candidates = malloc(4 << 21);
  349. int i, size = (1 << 21) - 1;
  350. if (!candidates)
  351. return 0;
  352. for (i = 0; i <= size; ++i)
  353. candidates[i] = i;
  354. for (c = 0; c < 8; ++c)
  355. for (i = 0; i <= size; ++i) {
  356. entry = candidates[i] ^ fastfwd[isodd][c];
  357. if (filter(entry >> 1) != BIT(ks[c], isodd) ||
  358. filter(entry) != BIT(ks[c], isodd + 2))
  359. candidates[i--] = candidates[size--];
  360. }
  361. candidates[size + 1] = -1;
  362. return candidates;
  363. }
  364. /** check_pfx_parity
  365. * helper function which eliminates possible secret states using parity bits
  366. */
  367. static struct Crypto1State *
  368. check_pfx_parity(uint32_t prefix, uint32_t rresp, uint8_t parities[8][8],
  369. uint32_t odd, uint32_t even, struct Crypto1State *sl) {
  370. uint32_t ks1, nr, ks2, rr, ks3, c, good = 1;
  371. for (c = 0; good && c < 8; ++c) {
  372. sl->odd = odd ^ fastfwd[1][c];
  373. sl->even = even ^ fastfwd[0][c];
  374. lfsr_rollback_bit(sl, 0, 0);
  375. lfsr_rollback_bit(sl, 0, 0);
  376. ks3 = lfsr_rollback_bit(sl, 0, 0);
  377. ks2 = lfsr_rollback_word(sl, 0, 0);
  378. ks1 = lfsr_rollback_word(sl, prefix | c << 5, 1);
  379. nr = ks1 ^(prefix | c << 5);
  380. rr = ks2 ^ rresp;
  381. good &= parity(nr & 0x000000ff) ^ parities[c][3] ^ BIT(ks2, 24);
  382. good &= parity(rr & 0xff000000) ^ parities[c][4] ^ BIT(ks2, 16);
  383. good &= parity(rr & 0x00ff0000) ^ parities[c][5] ^ BIT(ks2, 8);
  384. good &= parity(rr & 0x0000ff00) ^ parities[c][6] ^ BIT(ks2, 0);
  385. good &= parity(rr & 0x000000ff) ^ parities[c][7] ^ ks3;
  386. }
  387. return sl + good;
  388. }
  389. /** lfsr_common_prefix
  390. * Implentation of the common prefix attack.
  391. * Requires the 29 bit constant prefix used as reader nonce (pfx)
  392. * The reader response used (rr)
  393. * The keystream used to encrypt the observed NACK's (ks)
  394. * The parity bits (par)
  395. * It returns a zero terminated list of possible cipher states after the
  396. * tag nonce was fed in
  397. */
  398. struct Crypto1State *
  399. lfsr_common_prefix(uint32_t pfx, uint32_t rr, uint8_t ks[8], uint8_t par[8][8]) {
  400. struct Crypto1State *statelist, *s;
  401. uint32_t *odd, *even, *o, *e, top;
  402. odd = lfsr_prefix_ks(ks, 1);
  403. even = lfsr_prefix_ks(ks, 0);
  404. s = statelist = malloc((sizeof *statelist) << 20);
  405. if (!s || !odd || !even) {
  406. free(odd);
  407. free(even);
  408. free(statelist);
  409. return 0;
  410. }
  411. for (o = odd; *o + 1; ++o)
  412. for (e = even; *e + 1; ++e)
  413. for (top = 0; top < 64; ++top) {
  414. *o += 1 << 21;
  415. *e += (!(top & 7) + 1) << 21;
  416. s = check_pfx_parity(pfx, rr, par, *o, *e, s);
  417. }
  418. s->odd = s->even = 0;
  419. free(odd);
  420. free(even);
  421. return statelist;
  422. }