common: Introduce crypt-style password support
Subsystems (2)
cover common: Introduce crypt-style password support
This patchset introduces support for crypt-style passwords to unlock the console in autoboot mode.
The implementation of crypt-sha256 and crypt-sha512 originate from libxcrypt at https://github.com/besser82/libxcrypt.git
Version v4.4.17
Git commit hash 6b110bc
I didn't re-format those two files to make diffing to the original versions from libxcrypt easier, which leads to a huge load of checkpatch.pl warnings&errors. Please advise on whether they should be re-formatted or can be kept as is.
The remaining warnings from checkpatch.pl are intentional resp. open for discussion.
Cheers,
Steffen
Steffen Jaeckel (5):
lib: add crypt subsystem
common: integrate crypt-based passwords
common: Rename macro appropriately
cmd: allow disabling of timeout for password entry
configs: add new values to bcm963158 defconfig
cmd/Kconfig | 8 + common/Kconfig.boot | 23 ++- common/autoboot.c | 87 +++++++-- configs/bcm963158_ram_defconfig | 8 + include/crypt.h | 13 ++ lib/Kconfig | 1 + lib/Makefile | 1 + lib/crypt/Kconfig | 29 +++ lib/crypt/Makefile | 10 + lib/crypt/alg-sha256.h | 17 ++ lib/crypt/alg-sha512.h | 17 ++ lib/crypt/crypt-port.h | 28 +++ lib/crypt/crypt-sha256.c | 313 ++++++++++++++++++++++++++++++ lib/crypt/crypt-sha512.c | 328 ++++++++++++++++++++++++++++++++ lib/crypt/crypt.c | 73 +++++++ 15 files changed, 936 insertions(+), 20 deletions(-) create mode 100644 include/crypt.h create mode 100644 lib/crypt/Kconfig create mode 100644 lib/crypt/Makefile create mode 100644 lib/crypt/alg-sha256.h create mode 100644 lib/crypt/alg-sha512.h create mode 100644 lib/crypt/crypt-port.h create mode 100644 lib/crypt/crypt-sha256.c create mode 100644 lib/crypt/crypt-sha512.c create mode 100644 lib/crypt/crypt.c
Signature · 1 line
2.30.1
PATCH 1/5 lib: add crypt subsystem
Add the basic functionality required to support the standard crypt format.
The files crypt-sha256.c and crypt-sha512.c originate from libxcrypt and their formatting is therefor retained.
The integration is done via a crypt_compare() function in crypt.c.
``` libxcrypt $ git describe --long --always --all tags/v4.4.17-0-g6b110bc ```
---
include/crypt.h | 13 ++ lib/Kconfig | 1 + lib/Makefile | 1 + lib/crypt/Kconfig | 29 ++++ lib/crypt/Makefile | 10 ++ lib/crypt/alg-sha256.h | 17 ++ lib/crypt/alg-sha512.h | 17 ++ lib/crypt/crypt-port.h | 28 ++++ lib/crypt/crypt-sha256.c | 313 +++++++++++++++++++++++++++++++++++++ lib/crypt/crypt-sha512.c | 328 +++++++++++++++++++++++++++++++++++++++ lib/crypt/crypt.c | 73 +++++++++ 11 files changed, 830 insertions(+) create mode 100644 include/crypt.h create mode 100644 lib/crypt/Kconfig create mode 100644 lib/crypt/Makefile create mode 100644 lib/crypt/alg-sha256.h create mode 100644 lib/crypt/alg-sha512.h create mode 100644 lib/crypt/crypt-port.h create mode 100644 lib/crypt/crypt-sha256.c create mode 100644 lib/crypt/crypt-sha512.c create mode 100644 lib/crypt/crypt.c
include/crypt.h new file +13 -0
@@ -0,0 +1,13 @@ +/* SPDX-License-Identifier: GPL-2.0+ */ +/* Copyright (C) 2020 Steffen Jaeckel <[email protected]> */ + +/** +* Compare should with the processed passphrase. +* +* @should The crypt-style string to compare against +* @passphrase The plaintext passphrase +* @equal Pointer to an int where the result is stored +* '0' = unequal +* '1' = equal +*/ +void crypt_compare (const char *should,const char *passphrase,int *equal);
lib/Kconfig +1 -0
@@ -297,6 +297,7 @@ config AES source lib/rsa/Kconfig source lib/crypto/Kconfig +source lib/crypt/Kconfig config TPM bool "Trusted Platform Module (TPM) Support"
lib/Makefile +1 -0
@@ -64,6 +64,7 @@ obj-$(CONFIG_$(SPL_)RSA) += rsa/ obj-$(CONFIG_SHA1) += sha1.o obj-$(CONFIG_SHA256) += sha256.o obj-$(CONFIG_SHA512_ALGO) += sha512.o +obj-$(CONFIG_CRYPT_PW) += crypt/ obj-$(CONFIG_$(SPL_)ZLIB) += zlib/ obj-$(CONFIG_$(SPL_)ZSTD) += zstd/
lib/crypt/Kconfig new file +29 -0
@@ -0,0 +1,29 @@ +config CRYPT_PW + bool "Add crypt support for password-based unlock" + help + Enable support for crypt-style hashed passphrases. + This will then be used as the mechanism of choice to + verify whether the entered password to unlock the + console is correct or not. + To make it fully functional, one has also to enable + CONFIG_AUTOBOOT_KEYED and CONFIG_AUTOBOOT_ENCRYPTION + +if CRYPT_PW + +config CRYPT_PW_SHA256 + bool "Provide sha256crypt" + select SHA256 + select SHA256_ALGO + help + Enables support for the sha256crypt password-hashing algorithm. + The prefix is "$5$". + +config CRYPT_PW_SHA512 + bool "Provide sha512crypt" + select SHA512 + select SHA512_ALGO + help + Enables support for the sha512crypt password-hashing algorithm. + The prefix is "$6$". + +endif
lib/crypt/Makefile new file +10 -0
@@ -0,0 +1,10 @@ +# SPDX-License-Identifier: GPL-2.0+ +# +# Copyright (c) 2013, Google Inc. +# +# (C) Copyright 2000-2007 +# Wolfgang Denk, DENX Software Engineering, wd at denx.de. + +obj-$(CONFIG_CRYPT_PW) += crypt.o +obj-$(CONFIG_CRYPT_PW_SHA256) += crypt-sha256.o +obj-$(CONFIG_CRYPT_PW_SHA512) += crypt-sha512.o
lib/crypt/alg-sha256.h new file +17 -0
@@ -0,0 +1,17 @@ +/* SPDX-License-Identifier: GPL-2.0+ */ +/* Copyright (C) 2020 Steffen Jaeckel <[email protected]> */ + +#ifndef USE_HOSTCC +#include "common.h" +#else +#include <string.h> +#endif + +#include "u-boot/sha256.h" + +#define INCLUDE_sha256crypt 1 + +#define SHA256_CTX sha256_context +#define SHA256_Init sha256_starts +#define SHA256_Update (c, i, l) sha256_update(c, (const void *)i, l) +#define SHA256_Final (b, c) sha256_finish(c, b)
lib/crypt/alg-sha512.h new file +17 -0
@@ -0,0 +1,17 @@ +/* SPDX-License-Identifier: GPL-2.0+ */ +/* Copyright (C) 2020 Steffen Jaeckel <[email protected]> */ + +#ifndef USE_HOSTCC +#include "common.h" +#else +#include <string.h> +#endif + +#include "u-boot/sha512.h" + +#define INCLUDE_sha512crypt 1 + +#define SHA512_CTX sha512_context +#define SHA512_Init sha512_starts +#define SHA512_Update (c, i, l) sha512_update(c, (const void *)i, l) +#define SHA512_Final (b, c) sha512_finish(c, b)
lib/crypt/crypt-port.h new file +28 -0
@@ -0,0 +1,28 @@ +/* SPDX-License-Identifier: GPL-2.0+ */ +/* Copyright (C) 2020 Steffen Jaeckel <[email protected]> */ + +#include <linux/types.h> +#include <vsprintf.h> + +#define NO_GENSALT +#define CRYPT_OUTPUT_SIZE 384 +#define ALG_SPECIFIC_SIZE 8192 + +#define ARG_UNUSED (x) (x) + +#define static_assert (a, b) _Static_assert(a, b) + +#define strtoul (cp, endp, base) simple_strtoul(cp, endp, base) + +extern const unsigned char ascii64[65 ]; + +#define b64t ((const char *)ascii64) + +void crypt_sha256crypt_rn (const char *phrase,size_t phr_size, +const char *setting,size_t ARG_UNUSED (set_size ), +uint8_t *output,size_t out_size,void *scratch, +size_t scr_size); +void crypt_sha512crypt_rn (const char *phrase,size_t phr_size, +const char *setting,size_t ARG_UNUSED (set_size ), +uint8_t *output,size_t out_size,void *scratch, +size_t scr_size);
lib/crypt/crypt-sha256.c new file +313 -0
313 lines not on this page · raw message
lib/crypt/crypt-sha512.c new file +328 -0
328 lines not on this page · raw message
lib/crypt/crypt.c new file +73 -0
73 lines not on this page · raw message
Signature · 1 line
2.30.1
PATCH 1/5 lib: add crypt subsystem
Quoted 28 lines · Steffen Jaeckel
Add the basic functionality required to support the standard crypt format.
The filescrypt-sha256.candcrypt-sha512.coriginate from libxcrypt and their formatting is therefor retained.
The integration is done via acrypt_compare()function incrypt.c.``` libxcrypt $ git describe --long --always --all tags/v4.4.17-0-g6b110bc ```Signed-off-by Steffen Jaeckel <[email protected]>---
include/crypt.h | 13 ++ lib/Kconfig | 1 + lib/Makefile | 1 + lib/crypt/Kconfig | 29 ++++ lib/crypt/Makefile | 10 ++ lib/crypt/alg-sha256.h | 17 ++ lib/crypt/alg-sha512.h | 17 ++ lib/crypt/crypt-port.h | 28 ++++ lib/crypt/crypt-sha256.c | 313 +++++++++++++++++++++++++++++++++++++ lib/crypt/crypt-sha512.c | 328 +++++++++++++++++++++++++++++++++++++++ lib/crypt/crypt.c | 73 +++++++++ 11 files changed, 830 insertions(+) create mode 100644 include/crypt.h
Quoted 8 lines
create mode 100644 lib/crypt/Kconfig create mode 100644 lib/crypt/Makefile create mode 100644 lib/crypt/alg-sha256.h create mode 100644 lib/crypt/alg-sha512.h create mode 100644 lib/crypt/crypt-port.h create mode 100644 lib/crypt/crypt-sha256.c create mode 100644 lib/crypt/crypt-sha512.c create mode 100644 lib/crypt/crypt.c
This seems to use errno - is that necessary? Also are there any simple unit tests we could usefully bring over?
Regards,
Simon
PATCH 1/5 lib: add crypt subsystem
Hi Simon,
thanks for taking the time to review.
Quoted 39 lines · Simon Glass
Quoted 28 lines · Steffen Jaeckel
Add the basic functionality required to support the standard crypt format.
The filescrypt-sha256.candcrypt-sha512.coriginate from libxcrypt and their formatting is therefor retained.
The integration is done via acrypt_compare()function incrypt.c.``` libxcrypt $ git describe --long --always --all tags/v4.4.17-0-g6b110bc ```Signed-off-by Steffen Jaeckel <[email protected]>---
include/crypt.h | 13 ++ lib/Kconfig | 1 + lib/Makefile | 1 + lib/crypt/Kconfig | 29 ++++ lib/crypt/Makefile | 10 ++ lib/crypt/alg-sha256.h | 17 ++ lib/crypt/alg-sha512.h | 17 ++ lib/crypt/crypt-port.h | 28 ++++ lib/crypt/crypt-sha256.c | 313 +++++++++++++++++++++++++++++++++++++ lib/crypt/crypt-sha512.c | 328 +++++++++++++++++++++++++++++++++++++++ lib/crypt/crypt.c | 73 +++++++++ 11 files changed, 830 insertions(+) create mode 100644 include/crypt.hQuoted 8 lines
create mode 100644 lib/crypt/Kconfig create mode 100644 lib/crypt/Makefile create mode 100644 lib/crypt/alg-sha256.h create mode 100644 lib/crypt/alg-sha512.h create mode 100644 lib/crypt/crypt-port.h create mode 100644 lib/crypt/crypt-sha256.c create mode 100644 lib/crypt/crypt-sha512.c create mode 100644 lib/crypt/crypt.c
Quoted 2 lines
This seems to use errno - is that necessary? Also are there any simple unit tests we could usefully bring over?
Regarding errno - that's the way how libxcrypt works internally, I'm not sure whether we should really touch this ... the default crypt_alg_rn() function has a void return type, so either we have to keep using errno or we have to change the return type...
Regarding unit tests - good idea, I'll have a look.
PATCH 1/5 lib: add crypt subsystem
Hi Steffen,
Quoted 45 lines · Steffen Jaeckel
Hi Simon,
thanks for taking the time to review.
Quoted 39 lines · Simon Glass
Quoted 36 lines · Steffen Jaeckel
Add the basic functionality required to support the standard crypt format.
The filescrypt-sha256.candcrypt-sha512.coriginate from libxcrypt and their formatting is therefor retained.
The integration is done via acrypt_compare()function incrypt.c.``` libxcrypt $ git describe --long --always --all tags/v4.4.17-0-g6b110bc ```Signed-off-by Steffen Jaeckel <[email protected]>---
include/crypt.h | 13 ++ lib/Kconfig | 1 + lib/Makefile | 1 + lib/crypt/Kconfig | 29 ++++ lib/crypt/Makefile | 10 ++ lib/crypt/alg-sha256.h | 17 ++ lib/crypt/alg-sha512.h | 17 ++ lib/crypt/crypt-port.h | 28 ++++ lib/crypt/crypt-sha256.c | 313 +++++++++++++++++++++++++++++++++++++ lib/crypt/crypt-sha512.c | 328 +++++++++++++++++++++++++++++++++++++++ lib/crypt/crypt.c | 73 +++++++++ 11 files changed, 830 insertions(+) create mode 100644 include/crypt.h create mode 100644 lib/crypt/Kconfig create mode 100644 lib/crypt/Makefile create mode 100644 lib/crypt/alg-sha256.h create mode 100644 lib/crypt/alg-sha512.h create mode 100644 lib/crypt/crypt-port.h create mode 100644 lib/crypt/crypt-sha256.c create mode 100644 lib/crypt/crypt-sha512.c create mode 100644 lib/crypt/crypt.c
Quoted 7 lines
Quoted 2 lines
This seems to use errno - is that necessary? Also are there any simple unit tests we could usefully bring over?
Regarding errno - that's the way how libxcrypt works internally, I'm not sure whether we should really touch this ... the default
crypt_alg_rn()function has a void return type, so either we have to keep using errno or we have to change the return type...
Well you could add a wrapper function for U-Boot which returns errno, then make errno a static int in the library.
Quoted 2 lines
Regarding unit tests - good idea, I'll have a look.
Regards,
Simon
PATCH 2/5 common: integrate crypt-based passwords
In reply to Steffen JaeckelHook into the autoboot flow as an alternative to the existing mechanisms.
---
common/Kconfig.boot | 23 +++++++++++++--- common/autoboot.c | 67 +++++++++++++++++++++++++++++++++++++++------ 2 files changed, 77 insertions(+), 13 deletions(-)
common/Kconfig.boot +19 -4
@@ -802,10 +802,16 @@ config AUTOBOOT_ENCRYPTION depends on AUTOBOOT_KEYED help This option allows a string to be entered into U-Boot to stop the - autoboot. The string itself is hashed and compared against the hash - in the environment variable 'bootstopkeysha256'. If it matches then - boot stops and a command-line prompt is presented. - + autoboot. + The behavior depends whether CONFIG_CRYPT_PW is enabled or not. + In case CONFIG_CRYPT_PW is enabled, the string will be forwarded + to the crypt-based functionality and be compared against the + string in the environment variable 'bootstopkeycrypt'. + In case CONFIG_CRYPT_PW is disabled the string itself is hashed + and compared against the hash in the environment variable + 'bootstopkeysha256'. + If it matches in either case then boot stops and + a command-line prompt is presented. This provides a way to ship a secure production device which can also be accessed at the U-Boot command line.
@@ -843,6 +849,15 @@ config AUTOBOOT_KEYED_CTRLC Setting this variable provides an escape sequence from the limited "password" strings. +config AUTOBOOT_STOP_STR_CRYPT + string "Stop autobooting via crypt-hashed password" + depends on AUTOBOOT_KEYED && AUTOBOOT_ENCRYPTION + help + This option adds the feature to only stop the autobooting, + and therefore boot into the U-Boot prompt, when the input + string / password matches a values that is hashed via + one of support crypt options and saved in the environment. + config AUTOBOOT_STOP_STR_SHA256 string "Stop autobooting via SHA256 encrypted password" depends on AUTOBOOT_KEYED && AUTOBOOT_ENCRYPTION
common/autoboot.c +58 -9
99 lines not on this page · raw message
Signature · 1 line
2.30.1
PATCH 2/5 common: integrate crypt-based passwords
Hi Steffen,
Quoted 73 lines · Steffen Jaeckel
Hook into the autoboot flow as an alternative to the existing mechanisms.
Signed-off-by Steffen Jaeckel <[email protected]>---
common/Kconfig.boot | 23 +++++++++++++--- common/autoboot.c | 67 +++++++++++++++++++++++++++++++++++++++------ 2 files changed, 77 insertions(+), 13 deletions(-)common/Kconfig.boot +19
-4@@ -802,10 +802,16 @@ config AUTOBOOT_ENCRYPTION depends on AUTOBOOT_KEYED help This option allows a string to be entered into U-Boot to stop the - autoboot. The string itself is hashed and compared against the hash - in the environment variable 'bootstopkeysha256'. If it matches then - boot stops and a command-line prompt is presented. - + autoboot. + The behavior depends whether CONFIG_CRYPT_PW is enabled or not. + In case CONFIG_CRYPT_PW is enabled, the string will be forwarded + to the crypt-based functionality and be compared against the + string in the environment variable 'bootstopkeycrypt'. + In case CONFIG_CRYPT_PW is disabled the string itself is hashed + and compared against the hash in the environment variable + 'bootstopkeysha256'. + If it matches in either case then boot stops and + a command-line prompt is presented. This provides a way to ship a secure production device which can also be accessed at the U-Boot command line.@@ -843,6 +849,15 @@ config AUTOBOOT_KEYED_CTRLC Setting this variable provides an escape sequence from the limited "password" strings. +config AUTOBOOT_STOP_STR_CRYPT + string "Stop autobooting via crypt-hashed password" + depends on AUTOBOOT_KEYED && AUTOBOOT_ENCRYPTION + help + This option adds the feature to only stop the autobooting, + and therefore boot into the U-Boot prompt, when the input + string / password matches a values that is hashed via + one of support crypt options and saved in the environment. + config AUTOBOOT_STOP_STR_SHA256 string "Stop autobooting via SHA256 encrypted password" depends on AUTOBOOT_KEYED && AUTOBOOT_ENCRYPTIONcommon/autoboot.c +2
-4@@ -23,6 +23,7 @@#include <linux/delay.h> #include <u-boot/sha256.h> #include <bootcount.h> +#include <crypt.h> DECLARE_GLOBAL_DATA_PTR ;@@ -38,18 +39,62 @@ DECLARE_GLOBAL_DATA_PTR;static int stored_bootdelay;static int menukey; -#ifdef CONFIG_AUTOBOOT_ENCRYPTION -#define AUTOBOOT_STOP_STR_SHA256 CONFIG_AUTOBOOT_STOP_STR_SHA256 -#else -#define AUTOBOOT_STOP_STR_SHA256 "" +#if defined(CONFIG_AUTOBOOT_ENCRYPTION )
common/autoboot.c DECLARE_GLOBAL_DATA_PTR; · line 52
Quoted 10 lines
common/autoboot.c +9
-0+#if defined(CONFIG_CRYPT_PW ) && defined(CONFIG_AUTOBOOT_STOP_STR_CRYPT ) +#define AUTOBOOT_STOP_STR_ENC CONFIG_AUTOBOOT_STOP_STR_CRYPT +#define HAS_STOP_STR_CRYPT 1 +#elif defined(CONFIG_AUTOBOOT_STOP_STR_SHA256 ) +#define AUTOBOOT_STOP_STR_ENC CONFIG_AUTOBOOT_STOP_STR_SHA256 +#endif +#endif +#if !defined(AUTOBOOT_STOP_STR_ENC ) +#define AUTOBOOT_STOP_STR_ENC ""#endif
I wonder if we actually need all this now that things are in Kconfig? Can we use IS_ENABLED() in the code instead?
common/autoboot.c DECLARE_GLOBAL_DATA_PTR; · line 59
Quoted 8 lines
common/autoboot.c +1
-1-#ifdef CONFIG_USE_AUTOBOOT_MENUKEY #define AUTOBOOT_MENUKEY CONFIG_USE_AUTOBOOT_MENUKEY#else #define AUTOBOOT_MENUKEY 0#endif +static int passwd_abort_crypt (uint64_t etime)
Please add function comment
Also unsigned long if you can...if you need 64-bit on 32-bit machines it is u64...but why? That is a very large number of milliseconds!
common/autoboot.c DECLARE_GLOBAL_DATA_PTR; · line 63
Quoted 4 lines
common/autoboot.c +4
-0+{ +const char *crypt_env_str =env_get ("bootstopkeycrypt" ); +char presskey[MAX_DELAY_STOP_STR ]; +u_int presskey_len =0 ;
uint
Can you drop the presskey_ prefix on these. There is no other kind of key, so just 'key' and 'len' (or num_keys) is good enough.
common/autoboot.c DECLARE_GLOBAL_DATA_PTR; · line 66
Quoted 3 lines
common/autoboot.c +3
-0+int abort =0 ; + +if (IS_ENABLED (HAS_STOP_STR_CRYPT ) && !crypt_env_str)
We normally use IS_ENABLED with a CONFIG_....
common/autoboot.c DECLARE_GLOBAL_DATA_PTR; · line 74
Quoted 8 lines
common/autoboot.c +8
-0+ crypt_env_str =AUTOBOOT_STOP_STR_ENC ; + +if (!crypt_env_str) +return 0 ; + +/* +* We expect the stop-string to be newline terminated. +*/
/* ... */ is the single-line comment format
Quoted 54 lines
common/autoboot.c +31
-5+do { +if (tstc ()) { +/* Check for input string overflow */ +if (presskey_len >=MAX_DELAY_STOP_STR ) +return 0 ; + + presskey[presskey_len] =getchar (); + +if ((presskey[presskey_len] =='\r' ) || + (presskey[presskey_len] =='\n' )) { + presskey[presskey_len] ='\0' ; +crypt_compare (crypt_env_str, presskey, &abort); +/* you had one chance */ +break ; + }else { + presskey_len++; + } + } + }while (get_ticks () <= etime); + +return abort; +} +/* * Use a "constant-length" time compare function for this * hash compare: @@ -89,7 +134,7 @@ static int passwd_abort_sha256(uint64_t etime)int ret;if (sha_env_str ==NULL ) - sha_env_str =AUTOBOOT_STOP_STR_SHA256 ; + sha_env_str =AUTOBOOT_STOP_STR_ENC ; presskey =malloc_cache_aligned (MAX_DELAY_STOP_STR ); c =strstr (sha_env_str,":" );@@ -245,10 +290,14 @@ static int abortboot_key_sequence(int bootdelay)printf (CONFIG_AUTOBOOT_PROMPT , bootdelay);# endif -if (IS_ENABLED (CONFIG_AUTOBOOT_ENCRYPTION )) - abort =passwd_abort_sha256 (etime); -else +if (IS_ENABLED (CONFIG_AUTOBOOT_ENCRYPTION )) { +if (IS_ENABLED (CONFIG_CRYPT_PW )) + abort =passwd_abort_crypt (etime); +else + abort =passwd_abort_sha256 (etime); + }else { abort =passwd_abort_key (etime); + }if (!abort)debug_bootkeys ("key timeout\n" ); --
Quoted 2 lines
2.30.1
Regards,
SImon
PATCH 2/5 common: integrate crypt-based passwords
Quoted 82 lines · Simon Glass
Hi Steffen,
Quoted 78 lines · Steffen Jaeckel
Hook into the autoboot flow as an alternative to the existing mechanisms.
Signed-off-by Steffen Jaeckel <[email protected]>---
common/Kconfig.boot | 23 +++++++++++++--- common/autoboot.c | 67 +++++++++++++++++++++++++++++++++++++++------ 2 files changed, 77 insertions(+), 13 deletions(-)common/Kconfig.boot +19
-4@@ -802,10 +802,16 @@ config AUTOBOOT_ENCRYPTION depends on AUTOBOOT_KEYED help This option allows a string to be entered into U-Boot to stop the - autoboot. The string itself is hashed and compared against the hash - in the environment variable 'bootstopkeysha256'. If it matches then - boot stops and a command-line prompt is presented. - + autoboot. + The behavior depends whether CONFIG_CRYPT_PW is enabled or not. + In case CONFIG_CRYPT_PW is enabled, the string will be forwarded + to the crypt-based functionality and be compared against the + string in the environment variable 'bootstopkeycrypt'. + In case CONFIG_CRYPT_PW is disabled the string itself is hashed + and compared against the hash in the environment variable + 'bootstopkeysha256'. + If it matches in either case then boot stops and + a command-line prompt is presented. This provides a way to ship a secure production device which can also be accessed at the U-Boot command line.@@ -843,6 +849,15 @@ config AUTOBOOT_KEYED_CTRLC Setting this variable provides an escape sequence from the limited "password" strings. +config AUTOBOOT_STOP_STR_CRYPT + string "Stop autobooting via crypt-hashed password" + depends on AUTOBOOT_KEYED && AUTOBOOT_ENCRYPTION + help + This option adds the feature to only stop the autobooting, + and therefore boot into the U-Boot prompt, when the input + string / password matches a values that is hashed via + one of support crypt options and saved in the environment. + config AUTOBOOT_STOP_STR_SHA256 string "Stop autobooting via SHA256 encrypted password" depends on AUTOBOOT_KEYED && AUTOBOOT_ENCRYPTIONcommon/autoboot.c +7
-4@@ -23,6 +23,7 @@#include <linux/delay.h> #include <u-boot/sha256.h> #include <bootcount.h> +#include <crypt.h> DECLARE_GLOBAL_DATA_PTR ;@@ -38,18 +39,62 @@ DECLARE_GLOBAL_DATA_PTR;static int stored_bootdelay;static int menukey; -#ifdef CONFIG_AUTOBOOT_ENCRYPTION -#define AUTOBOOT_STOP_STR_SHA256 CONFIG_AUTOBOOT_STOP_STR_SHA256 -#else -#define AUTOBOOT_STOP_STR_SHA256 "" +#if defined(CONFIG_AUTOBOOT_ENCRYPTION ) +#if defined(CONFIG_CRYPT_PW ) && defined(CONFIG_AUTOBOOT_STOP_STR_CRYPT ) +#define AUTOBOOT_STOP_STR_ENC CONFIG_AUTOBOOT_STOP_STR_CRYPT +#define HAS_STOP_STR_CRYPT 1 +#elif defined(CONFIG_AUTOBOOT_STOP_STR_SHA256 ) +#define AUTOBOOT_STOP_STR_ENC CONFIG_AUTOBOOT_STOP_STR_SHA256
Quoted 8 lines
common/autoboot.c DECLARE_GLOBAL_DATA_PTR; · line 52
Quoted 5 lines
common/autoboot.c +4
-0+#endif +#endif +#if !defined(AUTOBOOT_STOP_STR_ENC ) +#define AUTOBOOT_STOP_STR_ENC ""#endif I wonder if we actually need all this now that things are in
Kconfig? Can we useIS_ENABLED()in the code instead?
The problem here is that CONFIG_AUTOBOOT_STOP_STR_{CRYPT,SHA256} are strings which can't be checked with IS_ENABLED().
Quoted 10 lines
common/autoboot.c DECLARE_GLOBAL_DATA_PTR; · line 59
Quoted 8 lines
common/autoboot.c +1
-1-#ifdef CONFIG_USE_AUTOBOOT_MENUKEY #define AUTOBOOT_MENUKEY CONFIG_USE_AUTOBOOT_MENUKEY#else #define AUTOBOOT_MENUKEY 0#endif +static int passwd_abort_crypt (uint64_t etime)Please add function comment
OK
Quoted 2 lines
Also unsigned long if you can...if you need 64-bit on 32-bit machines it is u64...but why? That is a very large number of milliseconds!
I've simply c&p'ed the prototype of the existing functions :) static int passwd_abort_sha256(uint64_t etime) static int passwd_abort_key(uint64_t etime)
Quoted 9 lines
common/autoboot.c DECLARE_GLOBAL_DATA_PTR; · line 63
Quoted 4 lines
common/autoboot.c +4
-0+{ +const char *crypt_env_str =env_get ("bootstopkeycrypt" ); +char presskey[MAX_DELAY_STOP_STR ]; +u_int presskey_len =0 ;uint
Can you drop the
presskey_prefix on these. There is no other kind of key, so just 'key' and 'len' (ornum_keys) is good enough.
same as above, this is a c&p from passwd_abort_sha256()
I had started by including my functionality into passwd_abort_sha256(), when I realized that it won't work like that I decided to c&p the entire implementation...
should I still change both? those variable names and the uint64_t in the arguments?
Quoted 6 lines
common/autoboot.c DECLARE_GLOBAL_DATA_PTR; · line 66
Quoted 4 lines
common/autoboot.c +3
-0+int abort =0 ; + +if (IS_ENABLED (HAS_STOP_STR_CRYPT ) && !crypt_env_str)We normally use
IS_ENABLEDwith aCONFIG_....
I don't see a different way than either this one or introducing a separate enable switch in Kconfig (which would in turn then either require different behavior for enabling CONFIG_AUTOBOOT_STOP_STR_SHA256 vs CONFIG_AUTOBOOT_STOP_STR_CRYPT or we'd have to break the existing flow of CONFIG_AUTOBOOT_STOP_STR_SHA256).
Quoted 11 lines
common/autoboot.c DECLARE_GLOBAL_DATA_PTR; · line 74
Quoted 9 lines
common/autoboot.c +8
-0+ crypt_env_str =AUTOBOOT_STOP_STR_ENC ; + +if (!crypt_env_str) +return 0 ; + +/* +* We expect the stop-string to be newline terminated. +*/ /* ... */ is the single-line comment format
OK
Quoted 61 lines
Quoted 55 lines
common/autoboot.c +31
-5+do { +if (tstc ()) { +/* Check for input string overflow */ +if (presskey_len >=MAX_DELAY_STOP_STR ) +return 0 ; + + presskey[presskey_len] =getchar (); + +if ((presskey[presskey_len] =='\r' ) || + (presskey[presskey_len] =='\n' )) { + presskey[presskey_len] ='\0' ; +crypt_compare (crypt_env_str, presskey, &abort); +/* you had one chance */ +break ; + }else { + presskey_len++; + } + } + }while (get_ticks () <= etime); + +return abort; +} +/* * Use a "constant-length" time compare function for this * hash compare: @@ -89,7 +134,7 @@ static int passwd_abort_sha256(uint64_t etime)int ret;if (sha_env_str ==NULL ) - sha_env_str =AUTOBOOT_STOP_STR_SHA256 ; + sha_env_str =AUTOBOOT_STOP_STR_ENC ; presskey =malloc_cache_aligned (MAX_DELAY_STOP_STR ); c =strstr (sha_env_str,":" );@@ -245,10 +290,14 @@ static int abortboot_key_sequence(int bootdelay)printf (CONFIG_AUTOBOOT_PROMPT , bootdelay);# endif -if (IS_ENABLED (CONFIG_AUTOBOOT_ENCRYPTION )) - abort =passwd_abort_sha256 (etime); -else +if (IS_ENABLED (CONFIG_AUTOBOOT_ENCRYPTION )) { +if (IS_ENABLED (CONFIG_CRYPT_PW )) + abort =passwd_abort_crypt (etime); +else + abort =passwd_abort_sha256 (etime); + }else { abort =passwd_abort_key (etime); + }if (!abort)debug_bootkeys ("key timeout\n" ); --Quoted 2 lines
2.30.1
Regards,
SImon
PATCH 2/5 common: integrate crypt-based passwords
Hi Steffen,
Quoted 93 lines · Steffen Jaeckel
Quoted 82 lines · Simon Glass
Hi Steffen,
Quoted 78 lines · Steffen Jaeckel
Hook into the autoboot flow as an alternative to the existing mechanisms.
Signed-off-by Steffen Jaeckel <[email protected]>---
common/Kconfig.boot | 23 +++++++++++++--- common/autoboot.c | 67 +++++++++++++++++++++++++++++++++++++++------ 2 files changed, 77 insertions(+), 13 deletions(-)common/Kconfig.boot +19
-4@@ -802,10 +802,16 @@ config AUTOBOOT_ENCRYPTION depends on AUTOBOOT_KEYED help This option allows a string to be entered into U-Boot to stop the - autoboot. The string itself is hashed and compared against the hash - in the environment variable 'bootstopkeysha256'. If it matches then - boot stops and a command-line prompt is presented. - + autoboot. + The behavior depends whether CONFIG_CRYPT_PW is enabled or not. + In case CONFIG_CRYPT_PW is enabled, the string will be forwarded + to the crypt-based functionality and be compared against the + string in the environment variable 'bootstopkeycrypt'. + In case CONFIG_CRYPT_PW is disabled the string itself is hashed + and compared against the hash in the environment variable + 'bootstopkeysha256'. + If it matches in either case then boot stops and + a command-line prompt is presented. This provides a way to ship a secure production device which can also be accessed at the U-Boot command line.@@ -843,6 +849,15 @@ config AUTOBOOT_KEYED_CTRLC Setting this variable provides an escape sequence from the limited "password" strings. +config AUTOBOOT_STOP_STR_CRYPT + string "Stop autobooting via crypt-hashed password" + depends on AUTOBOOT_KEYED && AUTOBOOT_ENCRYPTION + help + This option adds the feature to only stop the autobooting, + and therefore boot into the U-Boot prompt, when the input + string / password matches a values that is hashed via + one of support crypt options and saved in the environment. + config AUTOBOOT_STOP_STR_SHA256 string "Stop autobooting via SHA256 encrypted password" depends on AUTOBOOT_KEYED && AUTOBOOT_ENCRYPTIONcommon/autoboot.c +7
-4@@ -23,6 +23,7 @@#include <linux/delay.h> #include <u-boot/sha256.h> #include <bootcount.h> +#include <crypt.h> DECLARE_GLOBAL_DATA_PTR ;@@ -38,18 +39,62 @@ DECLARE_GLOBAL_DATA_PTR;static int stored_bootdelay;static int menukey; -#ifdef CONFIG_AUTOBOOT_ENCRYPTION -#define AUTOBOOT_STOP_STR_SHA256 CONFIG_AUTOBOOT_STOP_STR_SHA256 -#else -#define AUTOBOOT_STOP_STR_SHA256 "" +#if defined(CONFIG_AUTOBOOT_ENCRYPTION ) +#if defined(CONFIG_CRYPT_PW ) && defined(CONFIG_AUTOBOOT_STOP_STR_CRYPT ) +#define AUTOBOOT_STOP_STR_ENC CONFIG_AUTOBOOT_STOP_STR_CRYPT +#define HAS_STOP_STR_CRYPT 1 +#elif defined(CONFIG_AUTOBOOT_STOP_STR_SHA256 ) +#define AUTOBOOT_STOP_STR_ENC CONFIG_AUTOBOOT_STOP_STR_SHA256Quoted 8 lines
common/autoboot.c DECLARE_GLOBAL_DATA_PTR; · line 52
Quoted 5 lines
common/autoboot.c +4
-0+#endif +#endif +#if !defined(AUTOBOOT_STOP_STR_ENC ) +#define AUTOBOOT_STOP_STR_ENC ""#endif I wonder if we actually need all this now that things are in
Kconfig? Can we useIS_ENABLED()in the code instead?
Quoted 2 lines
The problem here is that CONFIG_AUTOBOOT_STOP_STR_{CRYPT,SHA256} are strings which can't be checked with
IS_ENABLED().
OK...then they should be split into a CONFIG that enables the feature and one that sets the value.
Quoted 16 lines
Quoted 9 lines
Quoted 9 lines
common/autoboot.c +0
-1-#ifdef CONFIG_USE_AUTOBOOT_MENUKEY #define AUTOBOOT_MENUKEY CONFIG_USE_AUTOBOOT_MENUKEY#else #define AUTOBOOT_MENUKEY 0#endif Quoted 3 lines
common/autoboot.c DECLARE_GLOBAL_DATA_PTR; · line 59
Quoted 1 line
common/autoboot.c +1
-0+static int passwd_abort_crypt (uint64_t etime)Please add function comment
OK
Quoted 6 lines
Quoted 2 lines
Also unsigned long if you can...if you need 64-bit on 32-bit machines it is u64...but why? That is a very large number of milliseconds!
I've simply c&p'ed the prototype of the existing functions :) static int
passwd_abort_sha256(uint64_t etime)static intpasswd_abort_key(uint64_t etime)
OK but it still seems wrong to me so I don't think we should copy it. Perhaps clean up the existing code?
Quoted 12 lines
Quoted 11 lines
common/autoboot.c DECLARE_GLOBAL_DATA_PTR; · line 63
Quoted 6 lines
common/autoboot.c +4
-0+{ +const char *crypt_env_str =env_get ("bootstopkeycrypt" ); +char presskey[MAX_DELAY_STOP_STR ]; +u_int presskey_len =0 ;uint
Can you drop the
presskey_prefix on these. There is no other kind of key, so just 'key' and 'len' (ornum_keys) is good enough.
Quoted 8 lines
same as above, this is a c&p from
passwd_abort_sha256()I had started by including my functionality into
passwd_abort_sha256(), when I realized that it won't work like that I decided to c&p the entire implementation...should I still change both? those variable names and the
uint64_tin the arguments?
Yes you can change the existing code in a separate patch.
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
common/autoboot.c +3
-0+int abort =0 ; + +if (IS_ENABLED (HAS_STOP_STR_CRYPT ) && !crypt_env_str)
Quoted 7 lines
Quoted 1 line
We normally use
IS_ENABLEDwith aCONFIG_....I don't see a different way than either this one or introducing a separate enable switch in
Kconfig(which would in turn then either require different behavior for enablingCONFIG_AUTOBOOT_STOP_STR_SHA256vsCONFIG_AUTOBOOT_STOP_STR_CRYPTor we'd have to break the existing flow ofCONFIG_AUTOBOOT_STOP_STR_SHA256).
My point was just that you should use HAS_STOP_STR_CRYPT , not IS_ENABLED(HAS_STOP_STR_CRYPT)
[...]
Regards,
Simon
PATCH 3/5 common: Rename macro appropriately
In reply to Steffen JaeckelWhile doing code-review internally this got nitpicked by 2 reviewers, so I decided to include this here.
---
common/autoboot.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)
common/autoboot.c +9 -9
@@ -27,7 +27,7 @@DECLARE_GLOBAL_DATA_PTR ; -#define MAX_DELAY_STOP_STR 64 +#define DELAY_STOP_STR_MAX_LENGTH 64#ifndef DEBUG_BOOTKEYS #define DEBUG_BOOTKEYS 0
@@ -59,7 +59,7 @@ static int menukey;static int passwd_abort_crypt (uint64_t etime) {const char *crypt_env_str =env_get ("bootstopkeycrypt" ); -char presskey[MAX_DELAY_STOP_STR ]; +char presskey[DELAY_STOP_STR_MAX_LENGTH ];u_int presskey_len =0 ;int abort =0 ;
@@ -75,7 +75,7 @@ static int passwd_abort_crypt(uint64_t etime) do { if (tstc()) { /* Check for input string overflow */ - if (presskey_len >= MAX_DELAY_STOP_STR) + if (presskey_len >= DELAY_STOP_STR_MAX_LENGTH) return 0; presskey[presskey_len] = getchar();
@@ -136,9 +136,9 @@ static int passwd_abort_sha256(uint64_t etime)if (sha_env_str ==NULL ) sha_env_str =AUTOBOOT_STOP_STR_ENC ; - presskey =malloc_cache_aligned (MAX_DELAY_STOP_STR ); + presskey =malloc_cache_aligned (DELAY_STOP_STR_MAX_LENGTH ); c =strstr (sha_env_str,":" ); -if (c && (c - sha_env_str <MAX_DELAY_STOP_STR )) { +if (c && (c - sha_env_str <DELAY_STOP_STR_MAX_LENGTH )) {/* preload presskey with salt */ memcpy (presskey, sha_env_str, c - sha_env_str); presskey_len = c - sha_env_str;
@@ -165,7 +165,7 @@ static int passwd_abort_sha256(uint64_t etime) do { if (tstc()) { /* Check for input string overflow */ - if (presskey_len >= MAX_DELAY_STOP_STR) { + if (presskey_len >= DELAY_STOP_STR_MAX_LENGTH) { free(presskey); free(sha); return 0;
@@ -209,7 +209,7 @@ static int passwd_abort_key(uint64_t etime) { .str = env_get("bootstopkey"), .retry = 0 }, }; -char presskey[MAX_DELAY_STOP_STR ]; +char presskey[DELAY_STOP_STR_MAX_LENGTH ];int presskey_len =0 ;int presskey_max =0 ;int i;
@@ -226,8 +226,8 @@ static int passwd_abort_key(uint64_t etime)for (i =0 ; i <sizeof (delaykey) /sizeof (delaykey[0 ]); i++) { delaykey[i].len = delaykey[i].str ==NULL ?0 :strlen (delaykey[i].str); - delaykey[i].len = delaykey[i].len >MAX_DELAY_STOP_STR ? -MAX_DELAY_STOP_STR : delaykey[i].len; + delaykey[i].len = delaykey[i].len >DELAY_STOP_STR_MAX_LENGTH ? +DELAY_STOP_STR_MAX_LENGTH : delaykey[i].len; presskey_max = presskey_max > delaykey[i].len ? presskey_max : delaykey[i].len;
Signature · 1 line
2.30.1
PATCH 3/5 common: Rename macro appropriately
Quoted 9 lines · Steffen Jaeckel
While doing code-review internally this got nitpicked by 2 reviewers, so I decided to include this here.
Signed-off-by Steffen Jaeckel <[email protected]>---
common/autoboot.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)
PATCH 4/5 cmd: allow disabling of timeout for password entry
In reply to Steffen JaeckelIn case a user has to enter a complicated password it is sometimes desireable to give the user more time than the default timeout. Enabling this feature will disable the timeout entirely in case the user presses the <Enter> key before entering any other character.
---
cmd/Kconfig | 8 ++++++++ common/autoboot.c | 8 +++++++- 2 files changed, 15 insertions(+), 1 deletion(-)
cmd/Kconfig +8 -0
@@ -177,6 +177,14 @@ config CMD_SBI help Display information about the SBI implementation. +config AUTOBOOT_NEVER_TIMEOUT + bool "Make the password entry never time-out" + depends on AUTOBOOT_KEYED && AUTOBOOT_ENCRYPTION + help + This option removes the timeout from the password entry + when the user first presses the <Enter> key before entering + any other character. + endmenu menu "Boot commands"
common/autoboot.c +7 -1
@@ -62,6 +62,7 @@ static int passwd_abort_crypt(uint64_t etime)char presskey[DELAY_STOP_STR_MAX_LENGTH ];u_int presskey_len =0 ;int abort =0 ; +int never_timeout =0 ;if (IS_ENABLED (HAS_STOP_STR_CRYPT ) && !crypt_env_str) crypt_env_str =AUTOBOOT_STOP_STR_ENC ;
@@ -82,6 +83,11 @@ static int passwd_abort_crypt(uint64_t etime)if ((presskey[presskey_len] =='\r' ) || (presskey[presskey_len] =='\n' )) { +if (IS_ENABLED (CONFIG_AUTOBOOT_NEVER_TIMEOUT ) && + !presskey_len) { + never_timeout =1 ; +continue ; + } presskey[presskey_len] ='\0' ;crypt_compare (crypt_env_str, presskey, &abort);/* you had one chance */
@@ -90,7 +96,7 @@ static int passwd_abort_crypt(uint64_t etime) presskey_len++; } } - }while (get_ticks () <= etime); + }while (never_timeout ||get_ticks () <= etime);return abort; }
Signature · 1 line
2.30.1
PATCH 4/5 cmd: allow disabling of timeout for password entry
Quoted 6 lines · Steffen Jaeckel
In case a user has to enter a complicated password it is sometimes desireable to give the user more time than the default timeout. Enabling this feature will disable the timeout entirely in case the user presses the <Enter> key before entering any other character.
Quoted 6 lines
Signed-off-by Steffen Jaeckel <[email protected]>---
cmd/Kconfig | 8 ++++++++ common/autoboot.c | 8 +++++++- 2 files changed, 15 insertions(+), 1 deletion(-)
I wonder if we could have some unit tests for this?
- Simon
PATCH 4/5 cmd: allow disabling of timeout for password entry
Quoted 11 lines · Simon Glass
Quoted 9 lines · Steffen Jaeckel
In case a user has to enter a complicated password it is sometimes desireable to give the user more time than the default timeout. Enabling this feature will disable the timeout entirely in case the user presses the <Enter> key before entering any other character.
Signed-off-by Steffen Jaeckel <[email protected]>---
Quoted 7 lines
Quoted 3 lines
cmd/Kconfig | 8 ++++++++ common/autoboot.c | 8 +++++++- 2 files changed, 15 insertions(+), 1 deletion(-)Reviewed-by Simon Glass <[email protected]>I wonder if we could have some unit tests for this?
As there are no tests yet for the autoboot part and I don't really feel comfortable adding one, I guess this has to wait ...
FYI: I've implemented the unit tests & your change requests but I'm waiting with a re-spin until we've sorted out the open points of Patch 2.
Cheers
Steffen
PATCH 5/5 configs: add new values to bcm963158 defconfig
In reply to Steffen JaeckelIn order to have at least one defconfig that enables all those newly added values.
---
configs/bcm963158_ram_defconfig | 8 ++++++++ 1 file changed, 8 insertions(+)
configs/bcm963158_ram_defconfig +8 -0
@@ -54,4 +54,12 @@ CONFIG_BCM63XX_HSSPI=y CONFIG_SYSRESET=y CONFIG_SYSRESET_WATCHDOG=y CONFIG_WDT_BCM6345=y +CONFIG_CRYPT_PW=y +CONFIG_CRYPT_PW_SHA256=y +CONFIG_AUTOBOOT_KEYED=y +CONFIG_AUTOBOOT_PROMPT="Enter password in %d seconds to stop autoboot\n" +CONFIG_AUTOBOOT_ENCRYPTION=y +# default password "password" +CONFIG_AUTOBOOT_STOP_STR_CRYPT="$5$rounds=640000$TM4lL4zXDG7F4aRX$JM7a9wmvodnA0WasjTztj6mxg.KVuk6doQ/eBhdcapB" +CONFIG_AUTOBOOT_NEVER_TIMEOUT=y # CONFIG_GENERATE_SMBIOS_TABLE is not set
Signature · 1 line
2.30.1