picopass_elite_keygen.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #include "picopass_elite_keygen.h"
  2. /* Based on https://youtu.be/MKSXSKQHz6o?si=DEKkW60x858pUI0a&t=600 */
  3. #define INITIAL_SEED 0x429080
  4. uint32_t seed = INITIAL_SEED;
  5. uint8_t key_state[8];
  6. bool prepared = false;
  7. void picopass_elite_reset() {
  8. memset(key_state, 0, sizeof(key_state));
  9. seed = INITIAL_SEED;
  10. prepared = false;
  11. }
  12. uint32_t picopass_elite_lcg() {
  13. uint32_t mod = 0x1000000; // 2^24,
  14. uint32_t a = 0xFD43FD;
  15. uint32_t c = 0xC39EC3;
  16. return (a * seed + c) % mod;
  17. }
  18. uint32_t picopass_elite_rng() {
  19. seed = picopass_elite_lcg();
  20. return seed;
  21. }
  22. uint8_t picopass_elite_nextByte() {
  23. return (picopass_elite_rng() >> 16) & 0xFF;
  24. }
  25. void picopass_elite_nextKey(uint8_t* key) {
  26. if(prepared) {
  27. for(size_t i = 0; i < 7; i++) {
  28. key_state[i] = key_state[i + 1];
  29. }
  30. key_state[7] = picopass_elite_nextByte();
  31. } else {
  32. for(size_t i = 0; i < 8; i++) {
  33. key_state[i] = picopass_elite_nextByte();
  34. }
  35. prepared = true;
  36. }
  37. memcpy(key, key_state, 8);
  38. }
  39. /*
  40. int main() {
  41. size_t limit = 700;
  42. for (size_t i = 0; i < limit; i++) {
  43. nextKey();
  44. printKey(key);
  45. // printf("%04lx: %08x\n", i, nextByte());
  46. }
  47. return 0;
  48. }
  49. */