crypto1.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #include "crypto1.h"
  2. #include "nfc_util.h"
  3. #include <furi.h>
  4. // Algorithm from https://github.com/RfidResearchGroup/proxmark3.git
  5. #define SWAPENDIAN(x) \
  6. ((x) = ((x) >> 8 & 0xff00ff) | ((x) & 0xff00ff) << 8, (x) = (x) >> 16 | (x) << 16)
  7. #define LF_POLY_ODD (0x29CE5C)
  8. #define LF_POLY_EVEN (0x870804)
  9. #define BEBIT(x, n) FURI_BIT(x, (n) ^ 24)
  10. void crypto1_reset(Crypto1* crypto1) {
  11. furi_assert(crypto1);
  12. crypto1->even = 0;
  13. crypto1->odd = 0;
  14. }
  15. void crypto1_init(Crypto1* crypto1, uint64_t key) {
  16. furi_assert(crypto1);
  17. crypto1->even = 0;
  18. crypto1->odd = 0;
  19. for(int8_t i = 47; i > 0; i -= 2) {
  20. crypto1->odd = crypto1->odd << 1 | FURI_BIT(key, (i - 1) ^ 7);
  21. crypto1->even = crypto1->even << 1 | FURI_BIT(key, i ^ 7);
  22. }
  23. }
  24. uint32_t crypto1_filter(uint32_t in) {
  25. uint32_t out = 0;
  26. out = 0xf22c0 >> (in & 0xf) & 16;
  27. out |= 0x6c9c0 >> (in >> 4 & 0xf) & 8;
  28. out |= 0x3c8b0 >> (in >> 8 & 0xf) & 4;
  29. out |= 0x1e458 >> (in >> 12 & 0xf) & 2;
  30. out |= 0x0d938 >> (in >> 16 & 0xf) & 1;
  31. return FURI_BIT(0xEC57E80A, out);
  32. }
  33. uint8_t crypto1_bit(Crypto1* crypto1, uint8_t in, int is_encrypted) {
  34. furi_assert(crypto1);
  35. uint8_t out = crypto1_filter(crypto1->odd);
  36. uint32_t feed = out & (!!is_encrypted);
  37. feed ^= !!in;
  38. feed ^= LF_POLY_ODD & crypto1->odd;
  39. feed ^= LF_POLY_EVEN & crypto1->even;
  40. crypto1->even = crypto1->even << 1 | (nfc_util_even_parity32(feed));
  41. FURI_SWAP(crypto1->odd, crypto1->even);
  42. return out;
  43. }
  44. uint8_t crypto1_byte(Crypto1* crypto1, uint8_t in, int is_encrypted) {
  45. furi_assert(crypto1);
  46. uint8_t out = 0;
  47. for(uint8_t i = 0; i < 8; i++) {
  48. out |= crypto1_bit(crypto1, FURI_BIT(in, i), is_encrypted) << i;
  49. }
  50. return out;
  51. }
  52. uint32_t crypto1_word(Crypto1* crypto1, uint32_t in, int is_encrypted) {
  53. furi_assert(crypto1);
  54. uint32_t out = 0;
  55. for(uint8_t i = 0; i < 32; i++) {
  56. out |= crypto1_bit(crypto1, BEBIT(in, i), is_encrypted) << (24 ^ i);
  57. }
  58. return out;
  59. }
  60. uint32_t prng_successor(uint32_t x, uint32_t n) {
  61. SWAPENDIAN(x);
  62. while(n--)
  63. x = x >> 1 | (x >> 16 ^ x >> 18 ^ x >> 19 ^ x >> 21) << 31;
  64. return SWAPENDIAN(x);
  65. }
  66. void crypto1_decrypt(
  67. Crypto1* crypto,
  68. uint8_t* encrypted_data,
  69. uint16_t encrypted_data_bits,
  70. uint8_t* decrypted_data) {
  71. furi_assert(crypto);
  72. furi_assert(encrypted_data);
  73. furi_assert(decrypted_data);
  74. if(encrypted_data_bits < 8) {
  75. uint8_t decrypted_byte = 0;
  76. decrypted_byte |= (crypto1_bit(crypto, 0, 0) ^ FURI_BIT(encrypted_data[0], 0)) << 0;
  77. decrypted_byte |= (crypto1_bit(crypto, 0, 0) ^ FURI_BIT(encrypted_data[0], 1)) << 1;
  78. decrypted_byte |= (crypto1_bit(crypto, 0, 0) ^ FURI_BIT(encrypted_data[0], 2)) << 2;
  79. decrypted_byte |= (crypto1_bit(crypto, 0, 0) ^ FURI_BIT(encrypted_data[0], 3)) << 3;
  80. decrypted_data[0] = decrypted_byte;
  81. } else {
  82. for(size_t i = 0; i < encrypted_data_bits / 8; i++) {
  83. decrypted_data[i] = crypto1_byte(crypto, 0, 0) ^ encrypted_data[i];
  84. }
  85. }
  86. }
  87. void crypto1_encrypt(
  88. Crypto1* crypto,
  89. uint8_t* keystream,
  90. uint8_t* plain_data,
  91. uint16_t plain_data_bits,
  92. uint8_t* encrypted_data,
  93. uint8_t* encrypted_parity) {
  94. furi_assert(crypto);
  95. furi_assert(plain_data);
  96. furi_assert(encrypted_data);
  97. furi_assert(encrypted_parity);
  98. if(plain_data_bits < 8) {
  99. encrypted_data[0] = 0;
  100. for(size_t i = 0; i < plain_data_bits; i++) {
  101. encrypted_data[0] |= (crypto1_bit(crypto, 0, 0) ^ FURI_BIT(plain_data[0], i)) << i;
  102. }
  103. } else {
  104. memset(encrypted_parity, 0, plain_data_bits / 8 + 1);
  105. for(uint8_t i = 0; i < plain_data_bits / 8; i++) {
  106. encrypted_data[i] = crypto1_byte(crypto, keystream ? keystream[i] : 0, 0) ^
  107. plain_data[i];
  108. encrypted_parity[i / 8] |=
  109. (((crypto1_filter(crypto->odd) ^ nfc_util_odd_parity8(plain_data[i])) & 0x01)
  110. << (7 - (i & 0x0007)));
  111. }
  112. }
  113. }