optimized_cipherutils.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. #include "optimized_cipherutils.h"
  35. #include <stdint.h>
  36. /**
  37. *
  38. * @brief Return and remove the first bit (x0) in the stream : <x0 x1 x2 x3 ... xn >
  39. * @param stream
  40. * @return
  41. */
  42. bool loclass_headBit(LoclassBitstreamIn_t* stream) {
  43. int bytepos = stream->position >> 3; // divide by 8
  44. int bitpos = (stream->position++) & 7; // mask out 00000111
  45. return (*(stream->buffer + bytepos) >> (7 - bitpos)) & 1;
  46. }
  47. /**
  48. * @brief Return and remove the last bit (xn) in the stream: <x0 x1 x2 ... xn>
  49. * @param stream
  50. * @return
  51. */
  52. bool loclass_tailBit(LoclassBitstreamIn_t* stream) {
  53. int bitpos = stream->numbits - 1 - (stream->position++);
  54. int bytepos = bitpos >> 3;
  55. bitpos &= 7;
  56. return (*(stream->buffer + bytepos) >> (7 - bitpos)) & 1;
  57. }
  58. /**
  59. * @brief Pushes bit onto the stream
  60. * @param stream
  61. * @param bit
  62. */
  63. void loclass_pushBit(LoclassBitstreamOut_t* stream, bool bit) {
  64. int bytepos = stream->position >> 3; // divide by 8
  65. int bitpos = stream->position & 7;
  66. *(stream->buffer + bytepos) |= (bit) << (7 - bitpos);
  67. stream->position++;
  68. stream->numbits++;
  69. }
  70. /**
  71. * @brief Pushes the lower six bits onto the stream
  72. * as b0 b1 b2 b3 b4 b5 b6
  73. * @param stream
  74. * @param bits
  75. */
  76. void loclass_push6bits(LoclassBitstreamOut_t* stream, uint8_t bits) {
  77. loclass_pushBit(stream, bits & 0x20);
  78. loclass_pushBit(stream, bits & 0x10);
  79. loclass_pushBit(stream, bits & 0x08);
  80. loclass_pushBit(stream, bits & 0x04);
  81. loclass_pushBit(stream, bits & 0x02);
  82. loclass_pushBit(stream, bits & 0x01);
  83. }
  84. /**
  85. * @brief loclass_bitsLeft
  86. * @param stream
  87. * @return number of bits left in stream
  88. */
  89. int loclass_bitsLeft(LoclassBitstreamIn_t* stream) {
  90. return stream->numbits - stream->position;
  91. }
  92. /**
  93. * @brief numBits
  94. * @param stream
  95. * @return Number of bits stored in stream
  96. */
  97. void loclass_x_num_to_bytes(uint64_t n, size_t len, uint8_t* dest) {
  98. while(len--) {
  99. dest[len] = (uint8_t)n;
  100. n >>= 8;
  101. }
  102. }
  103. uint64_t loclass_x_bytes_to_num(uint8_t* src, size_t len) {
  104. uint64_t num = 0;
  105. while(len--) {
  106. num = (num << 8) | (*src);
  107. src++;
  108. }
  109. return num;
  110. }
  111. uint8_t loclass_reversebytes(uint8_t b) {
  112. b = (b & 0xF0) >> 4 | (b & 0x0F) << 4;
  113. b = (b & 0xCC) >> 2 | (b & 0x33) << 2;
  114. b = (b & 0xAA) >> 1 | (b & 0x55) << 1;
  115. return b;
  116. }
  117. void loclass_reverse_arraybytes(uint8_t* arr, size_t len) {
  118. uint8_t i;
  119. for(i = 0; i < len; i++) {
  120. arr[i] = loclass_reversebytes(arr[i]);
  121. }
  122. }
  123. void loclass_reverse_arraycopy(uint8_t* arr, uint8_t* dest, size_t len) {
  124. uint8_t i;
  125. for(i = 0; i < len; i++) {
  126. dest[i] = loclass_reversebytes(arr[i]);
  127. }
  128. }