Skip to content

Commit db887e6

Browse files
authored
Enhancement and fixes of "Secure" feature (#16958)
1 parent baa8d07 commit db887e6

File tree

8 files changed

+379
-2
lines changed

8 files changed

+379
-2
lines changed

quantum/process_keycode/process_secure.c

+8-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77

88
bool preprocess_secure(uint16_t keycode, keyrecord_t *record) {
99
if (secure_is_unlocking()) {
10-
if (!record->event.pressed) {
10+
// !pressed will trigger on any already held keys (such as layer keys),
11+
// and cause the request secure check to prematurely fail.
12+
if (record->event.pressed) {
1113
secure_keypress_event(record->event.key.row, record->event.key.col);
1214
}
1315

@@ -33,7 +35,11 @@ bool process_secure(uint16_t keycode, keyrecord_t *record) {
3335
secure_is_locked() ? secure_unlock() : secure_lock();
3436
return false;
3537
}
38+
if (keycode == SECURE_REQUEST) {
39+
secure_request_unlock();
40+
return false;
41+
}
3642
}
3743
#endif
3844
return true;
39-
}
45+
}

quantum/quantum.c

+13
Original file line numberDiff line numberDiff line change
@@ -571,3 +571,16 @@ const char *get_u16_str(uint16_t curr_num, char curr_pad) {
571571
last_pad = curr_pad;
572572
return get_numeric_str(buf, sizeof(buf), curr_num, curr_pad);
573573
}
574+
575+
#if defined(SECURE_ENABLE)
576+
void secure_hook_quantum(secure_status_t secure_status) {
577+
// If keys are being held when this is triggered, they may not be released properly
578+
// this can result in stuck keys, mods and layers. To prevent that, manually
579+
// clear these, when it is triggered.
580+
581+
if (secure_status == SECURE_PENDING) {
582+
clear_keyboard();
583+
layer_clear();
584+
}
585+
}
586+
#endif

quantum/quantum_keycodes.h

+1
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,7 @@ enum quantum_keycodes {
601601
SECURE_LOCK,
602602
SECURE_UNLOCK,
603603
SECURE_TOGGLE,
604+
SECURE_REQUEST,
604605

605606
CAPS_WORD,
606607

quantum/secure.c

+15
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,32 @@ static secure_status_t secure_status = SECURE_LOCKED;
2323
static uint32_t unlock_time = 0;
2424
static uint32_t idle_time = 0;
2525

26+
static void secure_hook(secure_status_t secure_status) {
27+
secure_hook_quantum(secure_status);
28+
secure_hook_kb(secure_status);
29+
}
30+
2631
secure_status_t secure_get_status(void) {
2732
return secure_status;
2833
}
2934

3035
void secure_lock(void) {
3136
secure_status = SECURE_LOCKED;
37+
secure_hook(secure_status);
3238
}
3339

3440
void secure_unlock(void) {
3541
secure_status = SECURE_UNLOCKED;
3642
idle_time = timer_read32();
43+
secure_hook(secure_status);
3744
}
3845

3946
void secure_request_unlock(void) {
4047
if (secure_status == SECURE_LOCKED) {
4148
secure_status = SECURE_PENDING;
4249
unlock_time = timer_read32();
4350
}
51+
secure_hook(secure_status);
4452
}
4553

4654
void secure_activity_event(void) {
@@ -85,3 +93,10 @@ void secure_task(void) {
8593
}
8694
#endif
8795
}
96+
97+
__attribute__((weak)) bool secure_hook_user(secure_status_t secure_status) {
98+
return true;
99+
}
100+
__attribute__((weak)) bool secure_hook_kb(secure_status_t secure_status) {
101+
return secure_hook_user(secure_status);
102+
}

quantum/secure.h

+12
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,15 @@ void secure_keypress_event(uint8_t row, uint8_t col);
6565
/** \brief Handle various secure subsystem background tasks
6666
*/
6767
void secure_task(void);
68+
69+
/** \brief quantum hook called when changing secure status device
70+
*/
71+
void secure_hook_quantum(secure_status_t secure_status);
72+
73+
/** \brief user hook called when changing secure status device
74+
*/
75+
bool secure_hook_user(secure_status_t secure_status);
76+
77+
/** \brief keyboard hook called when changing secure status device
78+
*/
79+
bool secure_hook_kb(secure_status_t secure_status);

tests/secure/config.h

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/* Copyright 2021 Stefan Kerkmann
2+
*
3+
* This program is free software: you can redistribute it and/or modify
4+
* it under the terms of the GNU General Public License as published by
5+
* the Free Software Foundation, either version 2 of the License, or
6+
* (at your option) any later version.
7+
*
8+
* This program is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
* GNU General Public License for more details.
12+
*
13+
* You should have received a copy of the GNU General Public License
14+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
*/
16+
17+
#pragma once
18+
19+
#include "test_common.h"
20+
21+
// clang-format off
22+
#define SECURE_UNLOCK_SEQUENCE \
23+
{ \
24+
{0, 1}, \
25+
{0, 2}, \
26+
{0, 3}, \
27+
{0, 4} \
28+
}
29+
// clang-format on
30+
31+
#define SECURE_UNLOCK_TIMEOUT 20
32+
#define SECURE_IDLE_TIMEOUT 50

tests/secure/test.mk

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Copyright 2021 Stefan Kerkmann
2+
#
3+
# This program is free software: you can redistribute it and/or modify
4+
# it under the terms of the GNU General Public License as published by
5+
# the Free Software Foundation, either version 2 of the License, or
6+
# (at your option) any later version.
7+
#
8+
# This program is distributed in the hope that it will be useful,
9+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
# GNU General Public License for more details.
12+
#
13+
# You should have received a copy of the GNU General Public License
14+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
16+
# --------------------------------------------------------------------------------
17+
# Keep this file, even if it is empty, as a marker that this folder contains tests
18+
# --------------------------------------------------------------------------------
19+
20+
SECURE_ENABLE = yes

0 commit comments

Comments
 (0)