Key.hpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * Copyright (c) 2018 shchmue
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #pragma once
  17. #include <string>
  18. #include <vector>
  19. #include <switch/types.h>
  20. #include <stdio.h>
  21. typedef std::vector<u8> byte_vector;
  22. class Key {
  23. public:
  24. Key(std::string name, u64 xx_hash, byte_vector hash, u8 length, byte_vector key);
  25. // init with hash only
  26. Key(std::string name, u64 xx_hash, byte_vector hash, u8 length);
  27. // init with key only
  28. Key(std::string name, u8 length, byte_vector key);
  29. // temp key, no name stored
  30. Key(byte_vector key, u8 length);
  31. // key to be assigned later
  32. Key(std::string name, u8 length);
  33. // for declaration only
  34. Key();
  35. bool found() const { return is_found; }
  36. void set_found() { is_found = true; }
  37. // write key to file
  38. void save_key(FILE *file);
  39. static const size_t get_saved_key_count() { return saved_key_count; }
  40. // return CTR-decrypted data
  41. byte_vector aes_decrypt_ctr(const byte_vector &data, byte_vector iv);
  42. // return ECB-decrypted data
  43. byte_vector aes_decrypt_ecb(const byte_vector &data);
  44. // return CMAC of data
  45. byte_vector cmac(byte_vector data);
  46. // find key in buffer by hash, optionally specify start offset
  47. void find_key(const byte_vector &buffer, size_t start = 0);
  48. // get key encryption key
  49. byte_vector generate_kek(Key &master_key, const Key &kek_seed, const Key &key_seed);
  50. byte_vector key;
  51. std::string name;
  52. u64 xx_hash;
  53. byte_vector hash;
  54. u8 length;
  55. bool is_found = false;
  56. private:
  57. static size_t saved_key_count;
  58. };