twisted_pear 2 лет назад
Родитель
Сommit
d3cc051267

+ 14 - 4
esubghz_chat.c

@@ -124,10 +124,6 @@ void enter_chat(ESubGhzChatState *state)
 	furi_string_cat_printf(state->chat_box_store, "\nEncrypted: %s",
 			(state->encrypted ? "yes" : "no"));
 
-	/* clear the text input buffer to remove a password or key */
-	crypto_explicit_bzero(state->text_input_store,
-			sizeof(state->text_input_store));
-
 	subghz_tx_rx_worker_start(state->subghz_worker, state->subghz_device,
 			state->frequency);
 
@@ -506,6 +502,11 @@ int32_t esubghz_chat(void)
 		goto err_alloc_ti;
 	}
 
+	state->hex_key_input = byte_input_alloc();
+	if (state->hex_key_input == NULL) {
+		goto err_alloc_hki;
+	}
+
 	if (!chat_box_alloc(state)) {
 		goto err_alloc_cb;
 	}
@@ -572,6 +573,8 @@ int32_t esubghz_chat(void)
 			menu_get_view(state->menu));
 	view_dispatcher_add_view(state->view_dispatcher, ESubGhzChatView_Input,
 			text_input_get_view(state->text_input));
+	view_dispatcher_add_view(state->view_dispatcher, ESubGhzChatView_HexKeyInput,
+			byte_input_get_view(state->hex_key_input));
 	view_dispatcher_add_view(state->view_dispatcher, ESubGhzChatView_ChatBox,
 			text_box_get_view(state->chat_box));
 	view_dispatcher_add_view(state->view_dispatcher, ESubGhzChatView_KeyDisplay,
@@ -606,6 +609,8 @@ int32_t esubghz_chat(void)
 			ESubGhzChatView_Menu);
 	view_dispatcher_remove_view(state->view_dispatcher,
 			ESubGhzChatView_Input);
+	view_dispatcher_remove_view(state->view_dispatcher,
+			ESubGhzChatView_HexKeyInput);
 	view_dispatcher_remove_view(state->view_dispatcher,
 			ESubGhzChatView_ChatBox);
 	view_dispatcher_remove_view(state->view_dispatcher,
@@ -617,6 +622,8 @@ int32_t esubghz_chat(void)
 	/* clear the key and potential password */
 	crypto_explicit_bzero(state->text_input_store,
 			sizeof(state->text_input_store));
+	crypto_explicit_bzero(state->hex_key_input_store,
+			sizeof(state->hex_key_input_store));
 	crypto_explicit_bzero(state->key_hex_str, sizeof(state->key_hex_str));
 	crypto_ctx_clear(state->crypto_ctx);
 
@@ -640,6 +647,9 @@ err_alloc_kd:
 	chat_box_free(state);
 
 err_alloc_cb:
+	byte_input_free(state->hex_key_input);
+
+err_alloc_hki:
 	text_input_free(state->text_input);
 
 err_alloc_ti:

+ 6 - 0
esubghz_chat_i.h

@@ -4,6 +4,7 @@
 #include <gui/view_dispatcher_i.h>
 #include <gui/view_port_i.h>
 #include <gui/scene_manager.h>
+#include <gui/modules/byte_input.h>
 #include <gui/modules/dialog_ex.h>
 #include <gui/modules/menu.h>
 #include <gui/modules/text_box.h>
@@ -37,6 +38,8 @@ typedef struct {
 	FuriString *chat_box_store;
 	TextInput *text_input;
 	char text_input_store[TEXT_INPUT_STORE_SIZE + 1];
+	ByteInput *hex_key_input;
+	uint8_t hex_key_input_store[KEY_BITS / 8];
 	DialogEx *key_display;
 	char key_hex_str[KEY_HEX_STR_SIZE + 1];
 
@@ -76,7 +79,9 @@ typedef enum {
 	ESubGhzChatEvent_FreqEntered,
 	ESubGhzChatEvent_KeyMenuNoEncryption,
 	ESubGhzChatEvent_KeyMenuPassword,
+	ESubGhzChatEvent_KeyMenuHexKey,
 	ESubGhzChatEvent_PassEntered,
+	ESubGhzChatEvent_HexKeyEntered,
 	ESubGhzChatEvent_MsgEntered,
 	ESubGhzChatEvent_GotoMsgInput,
 	ESubGhzChatEvent_GotoKeyDisplay,
@@ -86,6 +91,7 @@ typedef enum {
 typedef enum {
 	ESubGhzChatView_Menu,
 	ESubGhzChatView_Input,
+	ESubGhzChatView_HexKeyInput,
 	ESubGhzChatView_ChatBox,
 	ESubGhzChatView_KeyDisplay,
 } ESubGhzChatView;

+ 90 - 0
scenes/esubghz_chat_hex_key_input.c

@@ -0,0 +1,90 @@
+#include "../esubghz_chat_i.h"
+
+/* Sets the entered bytes as the key, enters the chat and sends a HexKeyEntered
+ * event to the scene manager. */
+static void hex_key_input_cb(void* context)
+{
+	furi_assert(context);
+	ESubGhzChatState* state = context;
+
+	/* initiate the crypto context */
+	bool ret = crypto_ctx_set_key(state->crypto_ctx,
+			state->hex_key_input_store);
+
+	/* cleanup */
+	crypto_explicit_bzero(state->hex_key_input_store,
+			sizeof(state->hex_key_input_store));
+
+	if (!ret) {
+		crypto_ctx_clear(state->crypto_ctx);
+		return;
+	}
+
+	state->encrypted = true;
+
+	enter_chat(state);
+
+	scene_manager_handle_custom_event(state->scene_manager,
+			ESubGhzChatEvent_HexKeyEntered);
+}
+
+/* Prepares the hex key input scene. */
+void scene_on_enter_hex_key_input(void* context)
+{
+	FURI_LOG_T(APPLICATION_NAME, "scene_on_enter_hex_key_input");
+
+	furi_assert(context);
+	ESubGhzChatState* state = context;
+
+	byte_input_set_result_callback(state->hex_key_input,
+			hex_key_input_cb,
+			NULL,
+			state,
+			state->hex_key_input_store,
+			sizeof(state->hex_key_input_store));
+
+	view_dispatcher_switch_to_view(state->view_dispatcher,
+			ESubGhzChatView_HexKeyInput);
+}
+
+/* Handles scene manager events for the hex key input scene. */
+bool scene_on_event_hex_key_input(void* context, SceneManagerEvent event)
+{
+	FURI_LOG_T(APPLICATION_NAME, "scene_on_event_hex_key_input");
+
+	furi_assert(context);
+	ESubGhzChatState* state = context;
+
+	bool consumed = false;
+
+	switch(event.type) {
+	case SceneManagerEventTypeCustom:
+		switch(event.event) {
+		/* switch to message input scene */
+		case ESubGhzChatEvent_HexKeyEntered:
+			scene_manager_next_scene(state->scene_manager,
+					ESubGhzChatScene_ChatInput);
+			consumed = true;
+			break;
+		}
+		break;
+
+	default:
+		consumed = false;
+		break;
+	}
+
+	return consumed;
+}
+
+/* Cleans up the hex key input scene. */
+void scene_on_exit_hex_key_input(void* context)
+{
+	FURI_LOG_T(APPLICATION_NAME, "scene_on_exit_hex_key_input");
+
+	furi_assert(context);
+	ESubGhzChatState* state = context;
+
+	crypto_explicit_bzero(state->hex_key_input_store,
+			sizeof(state->hex_key_input_store));
+}

+ 15 - 13
scenes/esubghz_chat_key_display.c

@@ -28,17 +28,21 @@ void scene_on_enter_key_display(void* context)
 		uint8_t key[KEY_BITS / 8];
 		crypto_ctx_get_key(state->crypto_ctx, key);
 		snprintf(state->key_hex_str, KEY_HEX_STR_SIZE,
-				"%02hX%02hX%02hX%02hX%02hX%02hX"
-				"%02hX%02hX%02hX%02hX%02hX\n"
-				"%02hX%02hX%02hX%02hX%02hX%02hX"
-				"%02hX%02hX%02hX%02hX%02hX\n"
-				"%02hX%02hX%02hX%02hX%02hX%02hX"
+				"%02hX%02hX%02hX%02hX"
+				"%02hX%02hX%02hX%02hX\n"
+				"%02hX%02hX%02hX%02hX"
+				"%02hX%02hX%02hX%02hX\n"
+				"%02hX%02hX%02hX%02hX"
+				"%02hX%02hX%02hX%02hX\n"
+				"%02hX%02hX%02hX%02hX"
 				"%02hX%02hX%02hX%02hX",
-				key[0], key[1], key[2], key[3], key[4], key[5],
-				key[6], key[7], key[8], key[9], key[10],
-				key[11], key[12], key[13], key[14], key[15], key[16],
-				key[17], key[18], key[19], key[20], key[21],
-				key[22], key[23], key[24], key[25], key[26], key[27],
+				key[0], key[1], key[2], key[3],
+				key[4], key[5], key[6], key[7],
+				key[8], key[9], key[10], key[11],
+				key[12], key[13], key[14], key[15],
+				key[16], key[17], key[18], key[19],
+				key[20], key[21], key[22], key[23],
+				key[24], key[25], key[26], key[27],
 				key[28], key[29], key[30], key[31]);
 		crypto_explicit_bzero(key, sizeof(key));
 	} else {
@@ -47,9 +51,7 @@ void scene_on_enter_key_display(void* context)
 
 	dialog_ex_reset(state->key_display);
 
-	dialog_ex_set_header(state->key_display, "KEY (HEX)", 64, 2,
-			AlignCenter, AlignTop);
-	dialog_ex_set_text(state->key_display, state->key_hex_str, 64, 16,
+	dialog_ex_set_text(state->key_display, state->key_hex_str, 64, 2,
 			AlignCenter, AlignTop);
 
 	dialog_ex_set_icon(state->key_display, 0, 0, NULL);

+ 22 - 0
scenes/esubghz_chat_key_menu.c

@@ -3,6 +3,7 @@
 typedef enum {
 	ESubGhzChatKeyMenuItems_NoEncryption,
 	ESubGhzChatKeyMenuItems_Password,
+	ESubGhzChatKeyMenuItems_HexKey,
 } ESubGhzChatKeyMenuItems;
 
 static void key_menu_cb(void* context, uint32_t index)
@@ -24,6 +25,11 @@ static void key_menu_cb(void* context, uint32_t index)
 				ESubGhzChatEvent_KeyMenuPassword);
 		break;
 
+	case ESubGhzChatKeyMenuItems_HexKey:
+		scene_manager_handle_custom_event(state->scene_manager,
+				ESubGhzChatEvent_KeyMenuHexKey);
+		break;
+
 	default:
 		break;
 	}
@@ -55,6 +61,14 @@ void scene_on_enter_key_menu(void* context)
 		key_menu_cb,
 		state
 	);
+	menu_add_item(
+		state->menu,
+		"Hex Key",
+		NULL,
+		ESubGhzChatKeyMenuItems_HexKey,
+		key_menu_cb,
+		state
+	);
 
 	view_dispatcher_switch_to_view(state->view_dispatcher, ESubGhzChatView_Menu);
 }
@@ -78,12 +92,20 @@ bool scene_on_event_key_menu(void* context, SceneManagerEvent event)
 					ESubGhzChatScene_ChatInput);
 			consumed = true;
 			break;
+
 		/* switch to password input scene */
 		case ESubGhzChatEvent_KeyMenuPassword:
 			scene_manager_next_scene(state->scene_manager,
 					ESubGhzChatScene_PassInput);
 			consumed = true;
 			break;
+
+		/* switch to hex key input scene */
+		case ESubGhzChatEvent_KeyMenuHexKey:
+			scene_manager_next_scene(state->scene_manager,
+					ESubGhzChatScene_HexKeyInput);
+			consumed = true;
+			break;
 		}
 		break;
 

+ 5 - 0
scenes/esubghz_chat_pass_input.c

@@ -6,6 +6,9 @@ static void pass_input_cb(void *context)
 	furi_assert(context);
 	ESubGhzChatState* state = context;
 
+	crypto_explicit_bzero(state->text_input_store,
+			sizeof(state->text_input_store));
+
 	enter_chat(state);
 
 	scene_manager_handle_custom_event(state->scene_manager,
@@ -118,4 +121,6 @@ void scene_on_exit_pass_input(void* context)
 	ESubGhzChatState* state = context;
 
 	text_input_reset(state->text_input);
+	crypto_explicit_bzero(state->text_input_store,
+			sizeof(state->text_input_store));
 }

+ 1 - 0
scenes/esubghz_chat_scene_config.h

@@ -1,6 +1,7 @@
 ADD_SCENE(esubghz_chat, freq_input, FreqInput)
 ADD_SCENE(esubghz_chat, key_menu, KeyMenu)
 ADD_SCENE(esubghz_chat, pass_input, PassInput)
+ADD_SCENE(esubghz_chat, hex_key_input, HexKeyInput)
 ADD_SCENE(esubghz_chat, chat_input, ChatInput)
 ADD_SCENE(esubghz_chat, chat_box, ChatBox)
 ADD_SCENE(esubghz_chat, key_display, KeyDisplay)