crypto_cli.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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, FuriString* 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. FuriString* input;
  37. input = furi_string_alloc();
  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. furi_string_push_back(input, c);
  47. } else if(c == CliSymbolAsciiCR) {
  48. printf("\r\n");
  49. furi_string_cat(input, "\r\n");
  50. }
  51. }
  52. size_t size = furi_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. furi_string_reserve(input, size);
  61. uint8_t* output = malloc(size);
  62. if(!furi_hal_crypto_encrypt(
  63. (const uint8_t*)furi_string_get_cstr(input), output, size)) {
  64. printf("Failed to encrypt input");
  65. } else {
  66. printf("Hex-encoded encrypted data:\r\n");
  67. for(size_t i = 0; i < size; i++) {
  68. if(i % 80 == 0) printf("\r\n");
  69. printf("%02x", output[i]);
  70. }
  71. printf("\r\n");
  72. }
  73. free(output);
  74. } else {
  75. printf("No input");
  76. }
  77. furi_string_free(input);
  78. } while(0);
  79. if(key_loaded) {
  80. furi_hal_crypto_store_unload_key(key_slot);
  81. }
  82. }
  83. void crypto_cli_decrypt(Cli* cli, FuriString* args) {
  84. int key_slot = 0;
  85. bool key_loaded = false;
  86. uint8_t iv[16];
  87. do {
  88. if(!args_read_int_and_trim(args, &key_slot) || !(key_slot > 0 && key_slot <= 100)) {
  89. printf("Incorrect or missing slot, expected int 1-100");
  90. break;
  91. }
  92. if(!args_read_hex_bytes(args, iv, 16)) {
  93. printf("Incorrect or missing IV, expected 16 bytes in hex");
  94. break;
  95. }
  96. if(!furi_hal_crypto_store_load_key(key_slot, iv)) {
  97. printf("Unable to load key from slot %d", key_slot);
  98. break;
  99. }
  100. key_loaded = true;
  101. printf("Enter Hex-encoded data and press Ctrl+C to complete decryption:\r\n");
  102. FuriString* hex_input;
  103. hex_input = furi_string_alloc();
  104. char c;
  105. while(cli_read(cli, (uint8_t*)&c, 1) == 1) {
  106. if(c == CliSymbolAsciiETX) {
  107. printf("\r\n");
  108. break;
  109. } else if(c >= 0x20 && c < 0x7F) {
  110. putc(c, stdout);
  111. fflush(stdout);
  112. furi_string_push_back(hex_input, c);
  113. } else if(c == CliSymbolAsciiCR) {
  114. printf("\r\n");
  115. }
  116. }
  117. furi_string_trim(hex_input);
  118. size_t hex_size = furi_string_size(hex_input);
  119. if(hex_size > 0 && hex_size % 2 == 0) {
  120. size_t size = hex_size / 2;
  121. uint8_t* input = malloc(size);
  122. uint8_t* output = malloc(size);
  123. if(args_read_hex_bytes(hex_input, input, size)) {
  124. if(furi_hal_crypto_decrypt(input, output, size)) {
  125. printf("Decrypted data:\r\n");
  126. printf("%s\r\n", output);
  127. } else {
  128. printf("Failed to decrypt\r\n");
  129. }
  130. } else {
  131. printf("Failed to parse input");
  132. }
  133. free(input);
  134. free(output);
  135. } else {
  136. printf("Invalid or empty input");
  137. }
  138. furi_string_free(hex_input);
  139. } while(0);
  140. if(key_loaded) {
  141. furi_hal_crypto_store_unload_key(key_slot);
  142. }
  143. }
  144. void crypto_cli_has_key(Cli* cli, FuriString* args) {
  145. UNUSED(cli);
  146. int key_slot = 0;
  147. uint8_t iv[16] = {0};
  148. do {
  149. if(!args_read_int_and_trim(args, &key_slot) || !(key_slot > 0 && key_slot <= 100)) {
  150. printf("Incorrect or missing slot, expected int 1-100");
  151. break;
  152. }
  153. if(!furi_hal_crypto_store_load_key(key_slot, iv)) {
  154. printf("Unable to load key from slot %d", key_slot);
  155. break;
  156. }
  157. printf("Successfully loaded key from slot %d", key_slot);
  158. furi_hal_crypto_store_unload_key(key_slot);
  159. } while(0);
  160. }
  161. void crypto_cli_store_key(Cli* cli, FuriString* args) {
  162. UNUSED(cli);
  163. int key_slot = 0;
  164. int key_size = 0;
  165. FuriString* key_type;
  166. key_type = furi_string_alloc();
  167. uint8_t data[32 + 12] = {};
  168. FuriHalCryptoKey key;
  169. key.data = data;
  170. size_t data_size = 0;
  171. do {
  172. if(!args_read_int_and_trim(args, &key_slot)) {
  173. printf("Incorrect or missing key type, expected master, simple or encrypted");
  174. break;
  175. }
  176. if(!args_read_string_and_trim(args, key_type)) {
  177. printf("Incorrect or missing key type, expected master, simple or encrypted");
  178. break;
  179. }
  180. if(furi_string_cmp_str(key_type, "master") == 0) {
  181. if(key_slot != 0) {
  182. printf("Master keyslot must be is 0");
  183. break;
  184. }
  185. key.type = FuriHalCryptoKeyTypeMaster;
  186. } else if(furi_string_cmp_str(key_type, "simple") == 0) {
  187. if(key_slot < 1 || key_slot > 99) {
  188. printf("Simple keyslot must be in range");
  189. break;
  190. }
  191. key.type = FuriHalCryptoKeyTypeSimple;
  192. } else if(furi_string_cmp_str(key_type, "encrypted") == 0) {
  193. key.type = FuriHalCryptoKeyTypeEncrypted;
  194. data_size += 12;
  195. } else {
  196. printf("Incorrect or missing key type, expected master, simple or encrypted");
  197. break;
  198. }
  199. if(!args_read_int_and_trim(args, &key_size)) {
  200. printf("Incorrect or missing key size, expected 128 or 256");
  201. break;
  202. }
  203. if(key_size == 128) {
  204. key.size = FuriHalCryptoKeySize128;
  205. data_size += 16;
  206. } else if(key_size == 256) {
  207. key.size = FuriHalCryptoKeySize256;
  208. data_size += 32;
  209. } else {
  210. printf("Incorrect or missing key size, expected 128 or 256");
  211. }
  212. if(!args_read_hex_bytes(args, data, data_size)) {
  213. printf("Incorrect or missing key data, expected hex encoded key with or without IV.");
  214. break;
  215. }
  216. if(key_slot > 0) {
  217. uint8_t iv[16] = {0};
  218. if(key_slot > 1) {
  219. if(!furi_hal_crypto_store_load_key(key_slot - 1, iv)) {
  220. printf(
  221. "Slot %d before %d is empty, which is not allowed",
  222. key_slot - 1,
  223. key_slot);
  224. break;
  225. }
  226. furi_hal_crypto_store_unload_key(key_slot - 1);
  227. }
  228. if(furi_hal_crypto_store_load_key(key_slot, iv)) {
  229. furi_hal_crypto_store_unload_key(key_slot);
  230. printf("Key slot %d is already used", key_slot);
  231. break;
  232. }
  233. }
  234. uint8_t slot;
  235. if(furi_hal_crypto_store_add_key(&key, &slot)) {
  236. printf("Success. Stored to slot: %d", slot);
  237. } else {
  238. printf("Failure");
  239. }
  240. } while(0);
  241. furi_string_free(key_type);
  242. }
  243. static void crypto_cli(Cli* cli, FuriString* args, void* context) {
  244. UNUSED(context);
  245. FuriString* cmd;
  246. cmd = furi_string_alloc();
  247. do {
  248. if(!args_read_string_and_trim(args, cmd)) {
  249. crypto_cli_print_usage();
  250. break;
  251. }
  252. if(furi_string_cmp_str(cmd, "encrypt") == 0) {
  253. crypto_cli_encrypt(cli, args);
  254. break;
  255. }
  256. if(furi_string_cmp_str(cmd, "decrypt") == 0) {
  257. crypto_cli_decrypt(cli, args);
  258. break;
  259. }
  260. if(furi_string_cmp_str(cmd, "has_key") == 0) {
  261. crypto_cli_has_key(cli, args);
  262. break;
  263. }
  264. if(furi_string_cmp_str(cmd, "store_key") == 0) {
  265. crypto_cli_store_key(cli, args);
  266. break;
  267. }
  268. crypto_cli_print_usage();
  269. } while(false);
  270. furi_string_free(cmd);
  271. }
  272. void crypto_on_system_start() {
  273. #ifdef SRV_CLI
  274. Cli* cli = furi_record_open(RECORD_CLI);
  275. cli_add_command(cli, "crypto", CliCommandFlagDefault, crypto_cli, NULL);
  276. furi_record_close(RECORD_CLI);
  277. #else
  278. UNUSED(crypto_cli);
  279. #endif
  280. }