crypto_cli.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. UNUSED(cli);
  145. int key_slot = 0;
  146. uint8_t iv[16];
  147. do {
  148. if(!args_read_int_and_trim(args, &key_slot) || !(key_slot > 0 && key_slot <= 100)) {
  149. printf("Incorrect or missing slot, expected int 1-100");
  150. break;
  151. }
  152. if(!furi_hal_crypto_store_load_key(key_slot, iv)) {
  153. printf("Unable to load key from slot %d", key_slot);
  154. break;
  155. }
  156. printf("Successfully loaded key from slot %d", key_slot);
  157. furi_hal_crypto_store_unload_key(key_slot);
  158. } while(0);
  159. }
  160. void crypto_cli_store_key(Cli* cli, string_t args) {
  161. UNUSED(cli);
  162. int key_slot = 0;
  163. int key_size = 0;
  164. string_t key_type;
  165. string_init(key_type);
  166. uint8_t data[32 + 12] = {};
  167. FuriHalCryptoKey key;
  168. key.data = data;
  169. size_t data_size = 0;
  170. do {
  171. if(!args_read_int_and_trim(args, &key_slot)) {
  172. printf("Incorrect or missing key type, expected master, simple or encrypted");
  173. break;
  174. }
  175. if(!args_read_string_and_trim(args, key_type)) {
  176. printf("Incorrect or missing key type, expected master, simple or encrypted");
  177. break;
  178. }
  179. if(string_cmp_str(key_type, "master") == 0) {
  180. if(key_slot != 0) {
  181. printf("Master keyslot must be is 0");
  182. break;
  183. }
  184. key.type = FuriHalCryptoKeyTypeMaster;
  185. } else if(string_cmp_str(key_type, "simple") == 0) {
  186. if(key_slot < 1 || key_slot > 99) {
  187. printf("Simple keyslot must be in range");
  188. break;
  189. }
  190. key.type = FuriHalCryptoKeyTypeSimple;
  191. } else if(string_cmp_str(key_type, "encrypted") == 0) {
  192. key.type = FuriHalCryptoKeyTypeEncrypted;
  193. data_size += 12;
  194. } else {
  195. printf("Incorrect or missing key type, expected master, simple or encrypted");
  196. break;
  197. }
  198. if(!args_read_int_and_trim(args, &key_size)) {
  199. printf("Incorrect or missing key size, expected 128 or 256");
  200. break;
  201. }
  202. if(key_size == 128) {
  203. key.size = FuriHalCryptoKeySize128;
  204. data_size += 16;
  205. } else if(key_size == 256) {
  206. key.size = FuriHalCryptoKeySize256;
  207. data_size += 32;
  208. } else {
  209. printf("Incorrect or missing key size, expected 128 or 256");
  210. }
  211. if(!args_read_hex_bytes(args, data, data_size)) {
  212. printf("Incorrect or missing key data, expected hex encoded key with or without IV.");
  213. break;
  214. }
  215. if(key_slot > 0) {
  216. uint8_t iv[16];
  217. if(key_slot > 1) {
  218. if(!furi_hal_crypto_store_load_key(key_slot - 1, iv)) {
  219. printf(
  220. "Slot %d before %d is empty, which is not allowed",
  221. key_slot - 1,
  222. key_slot);
  223. break;
  224. }
  225. furi_hal_crypto_store_unload_key(key_slot - 1);
  226. }
  227. if(furi_hal_crypto_store_load_key(key_slot, iv)) {
  228. furi_hal_crypto_store_unload_key(key_slot);
  229. printf("Key slot %d is already used", key_slot);
  230. break;
  231. }
  232. }
  233. uint8_t slot;
  234. if(furi_hal_crypto_store_add_key(&key, &slot)) {
  235. printf("Success. Stored to slot: %d", slot);
  236. } else {
  237. printf("Failure");
  238. }
  239. } while(0);
  240. string_clear(key_type);
  241. }
  242. static void crypto_cli(Cli* cli, string_t args, void* context) {
  243. UNUSED(context);
  244. string_t cmd;
  245. string_init(cmd);
  246. do {
  247. if(!args_read_string_and_trim(args, cmd)) {
  248. crypto_cli_print_usage();
  249. break;
  250. }
  251. if(string_cmp_str(cmd, "encrypt") == 0) {
  252. crypto_cli_encrypt(cli, args);
  253. break;
  254. }
  255. if(string_cmp_str(cmd, "decrypt") == 0) {
  256. crypto_cli_decrypt(cli, args);
  257. break;
  258. }
  259. if(string_cmp_str(cmd, "has_key") == 0) {
  260. crypto_cli_has_key(cli, args);
  261. break;
  262. }
  263. if(string_cmp_str(cmd, "store_key") == 0) {
  264. crypto_cli_store_key(cli, args);
  265. break;
  266. }
  267. crypto_cli_print_usage();
  268. } while(false);
  269. string_clear(cmd);
  270. }
  271. void crypto_on_system_start() {
  272. #ifdef SRV_CLI
  273. Cli* cli = furi_record_open(RECORD_CLI);
  274. cli_add_command(cli, "crypto", CliCommandFlagDefault, crypto_cli, NULL);
  275. furi_record_close(RECORD_CLI);
  276. #else
  277. UNUSED(crypto_cli);
  278. #endif
  279. }