crypto_cli.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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_type:str> <key_size:int> <key_data:hex>\t - Store key in secure enclave, returns allocated slot number !!! 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_push_back(input, '\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 = furi_alloc(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. printf("%02x", output[i]);
  68. }
  69. printf("\r\n");
  70. }
  71. free(output);
  72. } else {
  73. printf("No input");
  74. }
  75. string_clear(input);
  76. } while(0);
  77. if(key_loaded) {
  78. furi_hal_crypto_store_unload_key(key_slot);
  79. }
  80. }
  81. void crypto_cli_decrypt(Cli* cli, string_t args) {
  82. int key_slot = 0;
  83. bool key_loaded = false;
  84. uint8_t iv[16];
  85. do {
  86. if(!args_read_int_and_trim(args, &key_slot) || !(key_slot > 0 && key_slot <= 100)) {
  87. printf("Incorrect or missing slot, expected int 1-100");
  88. break;
  89. }
  90. if(!args_read_hex_bytes(args, iv, 16)) {
  91. printf("Incorrect or missing IV, expected 16 bytes in hex");
  92. break;
  93. }
  94. if(!furi_hal_crypto_store_load_key(key_slot, iv)) {
  95. printf("Unable to load key from slot %d", key_slot);
  96. break;
  97. }
  98. key_loaded = true;
  99. printf("Enter Hex-encoded data and press Ctrl+C to complete decryption:\r\n");
  100. string_t hex_input;
  101. string_init(hex_input);
  102. char c;
  103. while(cli_read(cli, (uint8_t*)&c, 1) == 1) {
  104. if(c == CliSymbolAsciiETX) {
  105. printf("\r\n");
  106. break;
  107. } else if(c >= 0x20 && c < 0x7F) {
  108. putc(c, stdout);
  109. fflush(stdout);
  110. string_push_back(hex_input, c);
  111. } else if(c == CliSymbolAsciiCR) {
  112. printf("\r\n");
  113. string_push_back(hex_input, '\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 = furi_alloc(size);
  121. uint8_t* output = furi_alloc(size);
  122. if(args_read_hex_bytes(hex_input, input, size) &&
  123. 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 input");
  128. }
  129. free(input);
  130. free(output);
  131. } else {
  132. printf("Invalid or empty input");
  133. }
  134. string_clear(hex_input);
  135. } while(0);
  136. if(key_loaded) {
  137. furi_hal_crypto_store_unload_key(key_slot);
  138. }
  139. }
  140. void crypto_cli_has_key(Cli* cli, string_t args) {
  141. int key_slot = 0;
  142. uint8_t iv[16];
  143. do {
  144. if(!args_read_int_and_trim(args, &key_slot) || !(key_slot > 0 && key_slot <= 100)) {
  145. printf("Incorrect or missing slot, expected int 1-100");
  146. break;
  147. }
  148. if(!furi_hal_crypto_store_load_key(key_slot, iv)) {
  149. printf("Unable to load key from slot %d", key_slot);
  150. break;
  151. }
  152. printf("Successfully loaded key from slot %d", key_slot);
  153. furi_hal_crypto_store_unload_key(key_slot);
  154. } while(0);
  155. }
  156. void crypto_cli_store_key(Cli* cli, string_t args) {
  157. int key_size = 0;
  158. string_t key_type;
  159. string_init(key_type);
  160. uint8_t data[32 + 12] = {};
  161. FuriHalCryptoKey key;
  162. key.data = data;
  163. size_t data_size = 0;
  164. do {
  165. if(!args_read_string_and_trim(args, key_type)) {
  166. printf("Incorrect or missing key type, expected master, simple or encrypted");
  167. break;
  168. }
  169. if(string_cmp_str(key_type, "master") == 0) {
  170. key.type = FuriHalCryptoKeyTypeMaster;
  171. } else if(string_cmp_str(key_type, "simple") == 0) {
  172. key.type = FuriHalCryptoKeyTypeSimple;
  173. } else if(string_cmp_str(key_type, "encrypted") == 0) {
  174. key.type = FuriHalCryptoKeyTypeEncrypted;
  175. data_size += 12;
  176. } else {
  177. printf("Incorrect or missing key type, expected master, simple or encrypted");
  178. break;
  179. }
  180. if(!args_read_int_and_trim(args, &key_size)) {
  181. printf("Incorrect or missing key size, expected 128 or 256");
  182. break;
  183. }
  184. if(key_size == 128) {
  185. key.size = FuriHalCryptoKeySize128;
  186. data_size += 16;
  187. } else if(key_size == 256) {
  188. key.size = FuriHalCryptoKeySize256;
  189. data_size += 32;
  190. } else {
  191. printf("Incorrect or missing key size, expected 128 or 256");
  192. }
  193. if(!args_read_hex_bytes(args, data, data_size)) {
  194. printf("Incorrect or missing key data, expected hex encoded key with or without IV.");
  195. break;
  196. }
  197. uint8_t slot;
  198. if(furi_hal_crypto_store_add_key(&key, &slot)) {
  199. printf("Success. Stored to slot: %d", slot);
  200. } else {
  201. printf("Failure");
  202. }
  203. } while(0);
  204. string_clear(key_type);
  205. }
  206. void crypto_cli(Cli* cli, string_t args, void* context) {
  207. string_t cmd;
  208. string_init(cmd);
  209. do {
  210. if(!args_read_string_and_trim(args, cmd)) {
  211. crypto_cli_print_usage();
  212. break;
  213. }
  214. if(string_cmp_str(cmd, "encrypt") == 0) {
  215. crypto_cli_encrypt(cli, args);
  216. break;
  217. }
  218. if(string_cmp_str(cmd, "decrypt") == 0) {
  219. crypto_cli_decrypt(cli, args);
  220. break;
  221. }
  222. if(string_cmp_str(cmd, "has_key") == 0) {
  223. crypto_cli_has_key(cli, args);
  224. break;
  225. }
  226. if(string_cmp_str(cmd, "store_key") == 0) {
  227. crypto_cli_store_key(cli, args);
  228. break;
  229. }
  230. crypto_cli_print_usage();
  231. } while(false);
  232. string_clear(cmd);
  233. }
  234. void crypto_cli_init() {
  235. Cli* cli = furi_record_open("cli");
  236. cli_add_command(cli, "crypto", CliCommandFlagDefault, crypto_cli, NULL);
  237. furi_record_close("cli");
  238. }