crypto_cli.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. #include <furi_hal.h>
  2. #include <furi.h>
  3. #include <lib/toolbox/args.h>
  4. #include <cli/cli.h>
  5. void crypto_cli_print_usage() {
  6. printf("Usage:\r\n");
  7. printf("crypto <cmd> <args>\r\n");
  8. printf("Cmd list:\r\n");
  9. printf(
  10. "\tencrypt <key_slot:int> <iv:hex>\t - Using key from secure enclave and IV encrypt plain text with AES256CBC and encode to hex\r\n");
  11. printf(
  12. "\tdecrypt <key_slot:int> <iv:hex>\t - Using key from secure enclave and IV decrypt hex encoded encrypted with AES256CBC data to plain text\r\n");
  13. printf("\thas_key <key_slot:int>\t - Check if secure enclave has key in slot\r\n");
  14. printf(
  15. "\tstore_key <key_slot:int> <key_type:str> <key_size:int> <key_data:hex>\t - Store key in secure enclave. !!! NON-REVERSABLE OPERATION - READ MANUAL FIRST !!!\r\n");
  16. };
  17. void crypto_cli_encrypt(Cli* cli, string_t args) {
  18. int key_slot = 0;
  19. bool key_loaded = false;
  20. uint8_t iv[16];
  21. do {
  22. if(!args_read_int_and_trim(args, &key_slot) || !(key_slot > 0 && key_slot <= 100)) {
  23. printf("Incorrect or missing slot, expected int 1-100");
  24. break;
  25. }
  26. if(!args_read_hex_bytes(args, iv, 16)) {
  27. printf("Incorrect or missing IV, expected 16 bytes in hex");
  28. break;
  29. }
  30. if(!furi_hal_crypto_store_load_key(key_slot, iv)) {
  31. printf("Unable to load key from slot %d", key_slot);
  32. break;
  33. }
  34. key_loaded = true;
  35. printf("Enter plain text and press Ctrl+C to complete encryption:\r\n");
  36. string_t input;
  37. string_init(input);
  38. char c;
  39. while(cli_read(cli, (uint8_t*)&c, 1) == 1) {
  40. if(c == CliSymbolAsciiETX) {
  41. printf("\r\n");
  42. break;
  43. } else if(c >= 0x20 && c < 0x7F) {
  44. putc(c, stdout);
  45. fflush(stdout);
  46. string_push_back(input, c);
  47. } else if(c == CliSymbolAsciiCR) {
  48. printf("\r\n");
  49. string_cat_str(input, "\r\n");
  50. }
  51. }
  52. size_t size = string_size(input);
  53. if(size > 0) {
  54. // C-string null termination and block alignments
  55. size++;
  56. size_t remain = size % 16;
  57. if(remain) {
  58. size = size - remain + 16;
  59. }
  60. string_reserve(input, size);
  61. uint8_t* output = malloc(size);
  62. if(!furi_hal_crypto_encrypt((const uint8_t*)string_get_cstr(input), output, size)) {
  63. printf("Failed to encrypt input");
  64. } else {
  65. printf("Hex-encoded encrypted data:\r\n");
  66. for(size_t i = 0; i < size; i++) {
  67. if(i % 80 == 0) printf("\r\n");
  68. printf("%02x", output[i]);
  69. }
  70. printf("\r\n");
  71. }
  72. free(output);
  73. } else {
  74. printf("No input");
  75. }
  76. string_clear(input);
  77. } while(0);
  78. if(key_loaded) {
  79. furi_hal_crypto_store_unload_key(key_slot);
  80. }
  81. }
  82. void crypto_cli_decrypt(Cli* cli, string_t args) {
  83. int key_slot = 0;
  84. bool key_loaded = false;
  85. uint8_t iv[16];
  86. do {
  87. if(!args_read_int_and_trim(args, &key_slot) || !(key_slot > 0 && key_slot <= 100)) {
  88. printf("Incorrect or missing slot, expected int 1-100");
  89. break;
  90. }
  91. if(!args_read_hex_bytes(args, iv, 16)) {
  92. printf("Incorrect or missing IV, expected 16 bytes in hex");
  93. break;
  94. }
  95. if(!furi_hal_crypto_store_load_key(key_slot, iv)) {
  96. printf("Unable to load key from slot %d", key_slot);
  97. break;
  98. }
  99. key_loaded = true;
  100. printf("Enter Hex-encoded data and press Ctrl+C to complete decryption:\r\n");
  101. string_t hex_input;
  102. string_init(hex_input);
  103. char c;
  104. while(cli_read(cli, (uint8_t*)&c, 1) == 1) {
  105. if(c == CliSymbolAsciiETX) {
  106. printf("\r\n");
  107. break;
  108. } else if(c >= 0x20 && c < 0x7F) {
  109. putc(c, stdout);
  110. fflush(stdout);
  111. string_push_back(hex_input, c);
  112. } else if(c == CliSymbolAsciiCR) {
  113. printf("\r\n");
  114. }
  115. }
  116. string_strim(hex_input);
  117. size_t hex_size = string_size(hex_input);
  118. if(hex_size > 0 && hex_size % 2 == 0) {
  119. size_t size = hex_size / 2;
  120. uint8_t* input = malloc(size);
  121. uint8_t* output = malloc(size);
  122. if(args_read_hex_bytes(hex_input, input, size)) {
  123. if(furi_hal_crypto_decrypt(input, output, size)) {
  124. printf("Decrypted data:\r\n");
  125. printf("%s\r\n", output);
  126. } else {
  127. printf("Failed to decrypt\r\n");
  128. }
  129. } else {
  130. printf("Failed to parse input");
  131. }
  132. free(input);
  133. free(output);
  134. } else {
  135. printf("Invalid or empty input");
  136. }
  137. string_clear(hex_input);
  138. } while(0);
  139. if(key_loaded) {
  140. furi_hal_crypto_store_unload_key(key_slot);
  141. }
  142. }
  143. void crypto_cli_has_key(Cli* cli, string_t args) {
  144. int key_slot = 0;
  145. uint8_t iv[16];
  146. do {
  147. if(!args_read_int_and_trim(args, &key_slot) || !(key_slot > 0 && key_slot <= 100)) {
  148. printf("Incorrect or missing slot, expected int 1-100");
  149. break;
  150. }
  151. if(!furi_hal_crypto_store_load_key(key_slot, iv)) {
  152. printf("Unable to load key from slot %d", key_slot);
  153. break;
  154. }
  155. printf("Successfully loaded key from slot %d", key_slot);
  156. furi_hal_crypto_store_unload_key(key_slot);
  157. } while(0);
  158. }
  159. void crypto_cli_store_key(Cli* cli, string_t args) {
  160. int key_slot = 0;
  161. int key_size = 0;
  162. string_t key_type;
  163. string_init(key_type);
  164. uint8_t data[32 + 12] = {};
  165. FuriHalCryptoKey key;
  166. key.data = data;
  167. size_t data_size = 0;
  168. do {
  169. if(!args_read_int_and_trim(args, &key_slot)) {
  170. printf("Incorrect or missing key type, expected master, simple or encrypted");
  171. break;
  172. }
  173. if(!args_read_string_and_trim(args, key_type)) {
  174. printf("Incorrect or missing key type, expected master, simple or encrypted");
  175. break;
  176. }
  177. if(string_cmp_str(key_type, "master") == 0) {
  178. if(key_slot != 0) {
  179. printf("Master keyslot must be is 0");
  180. break;
  181. }
  182. key.type = FuriHalCryptoKeyTypeMaster;
  183. } else if(string_cmp_str(key_type, "simple") == 0) {
  184. if(key_slot < 1 || key_slot > 99) {
  185. printf("Simple keyslot must be in range");
  186. break;
  187. }
  188. key.type = FuriHalCryptoKeyTypeSimple;
  189. } else if(string_cmp_str(key_type, "encrypted") == 0) {
  190. key.type = FuriHalCryptoKeyTypeEncrypted;
  191. data_size += 12;
  192. } else {
  193. printf("Incorrect or missing key type, expected master, simple or encrypted");
  194. break;
  195. }
  196. if(!args_read_int_and_trim(args, &key_size)) {
  197. printf("Incorrect or missing key size, expected 128 or 256");
  198. break;
  199. }
  200. if(key_size == 128) {
  201. key.size = FuriHalCryptoKeySize128;
  202. data_size += 16;
  203. } else if(key_size == 256) {
  204. key.size = FuriHalCryptoKeySize256;
  205. data_size += 32;
  206. } else {
  207. printf("Incorrect or missing key size, expected 128 or 256");
  208. }
  209. if(!args_read_hex_bytes(args, data, data_size)) {
  210. printf("Incorrect or missing key data, expected hex encoded key with or without IV.");
  211. break;
  212. }
  213. if(key_slot > 0) {
  214. uint8_t iv[16];
  215. if(key_slot > 1) {
  216. if(!furi_hal_crypto_store_load_key(key_slot - 1, iv)) {
  217. printf(
  218. "Slot %d before %d is empty, which is not allowed",
  219. key_slot - 1,
  220. key_slot);
  221. break;
  222. }
  223. furi_hal_crypto_store_unload_key(key_slot - 1);
  224. }
  225. if(furi_hal_crypto_store_load_key(key_slot, iv)) {
  226. furi_hal_crypto_store_unload_key(key_slot);
  227. printf("Key slot %d is already used", key_slot);
  228. break;
  229. }
  230. }
  231. uint8_t slot;
  232. if(furi_hal_crypto_store_add_key(&key, &slot)) {
  233. printf("Success. Stored to slot: %d", slot);
  234. } else {
  235. printf("Failure");
  236. }
  237. } while(0);
  238. string_clear(key_type);
  239. }
  240. static void crypto_cli(Cli* cli, string_t args, void* context) {
  241. string_t cmd;
  242. string_init(cmd);
  243. do {
  244. if(!args_read_string_and_trim(args, cmd)) {
  245. crypto_cli_print_usage();
  246. break;
  247. }
  248. if(string_cmp_str(cmd, "encrypt") == 0) {
  249. crypto_cli_encrypt(cli, args);
  250. break;
  251. }
  252. if(string_cmp_str(cmd, "decrypt") == 0) {
  253. crypto_cli_decrypt(cli, args);
  254. break;
  255. }
  256. if(string_cmp_str(cmd, "has_key") == 0) {
  257. crypto_cli_has_key(cli, args);
  258. break;
  259. }
  260. if(string_cmp_str(cmd, "store_key") == 0) {
  261. crypto_cli_store_key(cli, args);
  262. break;
  263. }
  264. crypto_cli_print_usage();
  265. } while(false);
  266. string_clear(cmd);
  267. }
  268. void crypto_on_system_start() {
  269. #ifdef SRV_CLI
  270. Cli* cli = furi_record_open("cli");
  271. cli_add_command(cli, "crypto", CliCommandFlagDefault, crypto_cli, NULL);
  272. furi_record_close("cli");
  273. #else
  274. UNUSED(crypto_cli);
  275. #endif
  276. }