keeloq.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /* Copyright (C) 2022-2023 Salvatore Sanfilippo -- All Rights Reserved
  2. * See the LICENSE file for information about the license.
  3. *
  4. * Microchip HCS200/HCS300/HSC301 KeeLoq, rolling code remotes.
  5. *
  6. * Usually 443.92 Mhz OOK, ~200us or ~400us pulse len, depending
  7. * on the configuration.
  8. *
  9. * Preamble: 12 pairs of alternating pulse/gap.
  10. * Sync: long gap of around 10 times the duration of the short-pulse.
  11. * Data: pulse width encoded data. Each bit takes three cycles:
  12. *
  13. * 0 = 110
  14. * 1 = 100
  15. *
  16. * There are a total of 66 bits transmitted.
  17. * 0..31: 32 bits of encrypted rolling code.
  18. * 32..59: Remote ID, 28 bits
  19. * 60..63: Buttons pressed
  20. * 64..64: Low battery if set
  21. * 65..65: Always set to 1
  22. *
  23. * Bits in bytes are inverted: least significant bit is first.
  24. * For some reason there is no checksum whatsoever, so we only decode
  25. * if we find everything well formed.
  26. */
  27. #include "../app.h"
  28. static bool decode(uint8_t* bits, uint32_t numbytes, uint32_t numbits, ProtoViewMsgInfo* info) {
  29. /* In the sync pattern, we require the 12 high/low pulses and at least
  30. * half the gap we expect (5 pulses times, one is the final zero in the
  31. * 24 symbols high/low sequence, then other 4). */
  32. const char* sync_pattern = "101010101010101010101010"
  33. "0000";
  34. uint8_t sync_len = 24 + 4;
  35. if(numbits - sync_len + sync_len < 3 * 66) return false;
  36. uint32_t off = bitmap_seek_bits(bits, numbytes, 0, numbits, sync_pattern);
  37. if(off == BITMAP_SEEK_NOT_FOUND) return false;
  38. info->start_off = off;
  39. off += sync_len; // Seek start of message.
  40. /* Now there is half the gap left, but we allow from 3 to 7, instead of 5
  41. * symbols of gap, to avoid missing the signal for a matter of wrong
  42. * timing. */
  43. uint8_t gap_len = 0;
  44. while(gap_len <= 7 && bitmap_get(bits, numbytes, off + gap_len) == 0) gap_len++;
  45. if(gap_len < 3 || gap_len > 7) return false;
  46. off += gap_len;
  47. FURI_LOG_E(TAG, "Keeloq preamble+sync found");
  48. uint8_t raw[9] = {0};
  49. uint32_t decoded = convert_from_line_code(
  50. raw, sizeof(raw), bits, numbytes, off, "110", "100"); /* Pulse width modulation. */
  51. FURI_LOG_E(TAG, "Keeloq decoded bits: %lu", decoded);
  52. if(decoded < 66) return false; /* Require the full 66 bits. */
  53. info->pulses_count = (off + 66 * 3) - info->start_off;
  54. bitmap_reverse_bytes_bits(raw, sizeof(raw)); /* Keeloq is LSB first. */
  55. int buttons = raw[7] >> 4;
  56. int lowbat = (raw[8] & 0x1) == 0; // Actual bit meaning: good battery level
  57. int alwaysone = (raw[8] & 0x2) != 0;
  58. fieldset_add_bytes(info->fieldset, "encr", raw, 8);
  59. raw[7] = raw[7] << 4; // Make ID bits contiguous
  60. fieldset_add_bytes(info->fieldset, "id", raw + 4, 7); // 28 bits, 7 nibbles
  61. fieldset_add_bin(info->fieldset, "s[2,1,0,3]", buttons, 4);
  62. fieldset_add_bin(info->fieldset, "low battery", lowbat, 1);
  63. fieldset_add_bin(info->fieldset, "always one", alwaysone, 1);
  64. return true;
  65. }
  66. static void get_fields(ProtoViewFieldSet* fieldset) {
  67. uint8_t remote_id[4] = {0xab, 0xcd, 0xef, 0xa0};
  68. uint8_t encr[4] = {0xab, 0xab, 0xab, 0xab};
  69. fieldset_add_bytes(fieldset, "encr", encr, 8);
  70. fieldset_add_bytes(fieldset, "id", remote_id, 7);
  71. fieldset_add_bin(fieldset, "s[2,1,0,3]", 2, 4);
  72. fieldset_add_bin(fieldset, "low battery", 0, 1);
  73. fieldset_add_bin(fieldset, "always one", 1, 1);
  74. }
  75. static void build_message(RawSamplesBuffer* samples, ProtoViewFieldSet* fieldset) {
  76. uint32_t te = 380; // Short pulse duration in microseconds.
  77. // Sync: 12 pairs of pulse/gap + 9 times gap
  78. for(int j = 0; j < 12; j++) {
  79. raw_samples_add(samples, true, te);
  80. raw_samples_add(samples, false, te);
  81. }
  82. raw_samples_add(samples, false, te * 9);
  83. // Data, 66 bits.
  84. uint8_t data[9] = {0};
  85. memcpy(data, fieldset->fields[0]->bytes, 4); // Encrypted part.
  86. memcpy(data + 4, fieldset->fields[1]->bytes, 4); // ID.
  87. data[7] = data[7] >> 4 | fieldset->fields[2]->uvalue << 4; // s[2,1,0,3]
  88. int low_battery = fieldset->fields[3] != 0;
  89. int always_one = fieldset->fields[4] != 0;
  90. low_battery = !low_battery; // Bit real meaning is good battery level.
  91. data[8] |= low_battery;
  92. data[8] |= (always_one << 1);
  93. bitmap_reverse_bytes_bits(data, sizeof(data)); /* Keeloq is LSB first. */
  94. for(int j = 0; j < 66; j++) {
  95. if(bitmap_get(data, 9, j)) {
  96. raw_samples_add(samples, true, te);
  97. raw_samples_add(samples, false, te * 2);
  98. } else {
  99. raw_samples_add(samples, true, te * 2);
  100. raw_samples_add(samples, false, te);
  101. }
  102. }
  103. }
  104. ProtoViewDecoder KeeloqDecoder =
  105. {.name = "Keeloq", .decode = decode, .get_fields = get_fields, .build_message = build_message};