summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/Kconfig.debug53
-rw-r--r--lib/Kconfig.kasan9
-rw-r--r--lib/Kconfig.ubsan128
-rw-r--r--lib/Makefile4
-rw-r--r--lib/cmdline.c16
-rw-r--r--lib/cmdline_kunit.c100
-rw-r--r--lib/cpumask.c18
-rw-r--r--lib/crypto/blake2s-selftest.c2
-rw-r--r--lib/crypto/blake2s.c2
-rw-r--r--lib/crypto/curve25519.c2
-rw-r--r--lib/crypto/sha256.c214
-rw-r--r--lib/digsig.c2
-rw-r--r--lib/dump_stack.c2
-rw-r--r--lib/dynamic_debug.c9
-rw-r--r--lib/errname.c1
-rw-r--r--lib/error-inject.c2
-rw-r--r--lib/errseq.c1
-rw-r--r--lib/find_bit.c3
-rw-r--r--lib/fonts/font_10x18.c1
-rw-r--r--lib/fonts/font_6x10.c1
-rw-r--r--lib/fonts/font_6x11.c1
-rw-r--r--lib/fonts/font_6x8.c1
-rw-r--r--lib/fonts/font_7x14.c1
-rw-r--r--lib/fonts/font_8x16.c1
-rw-r--r--lib/fonts/font_8x8.c1
-rw-r--r--lib/fonts/font_acorn_8x8.c1
-rw-r--r--lib/fonts/font_mini_4x6.c1
-rw-r--r--lib/fonts/font_pearl_8x8.c1
-rw-r--r--lib/fonts/font_sun12x22.c1
-rw-r--r--lib/fonts/font_sun8x16.c1
-rw-r--r--lib/fonts/font_ter16x32.c1
-rw-r--r--lib/kunit/test.c59
-rw-r--r--lib/locking-selftest.c98
-rw-r--r--lib/lz4/lz4_decompress.c6
-rw-r--r--lib/lz4/lz4defs.h1
-rw-r--r--lib/lzo/lzo1x_compress.c2
-rw-r--r--lib/math/div64.c4
-rw-r--r--lib/math/int_pow.c2
-rw-r--r--lib/math/int_sqrt.c3
-rw-r--r--lib/math/reciprocal_div.c9
-rw-r--r--lib/mpi/ec.c3
-rw-r--r--lib/nlattr.c42
-rw-r--r--lib/sha1.c2
-rw-r--r--lib/smp_processor_id.c5
-rw-r--r--lib/stackdepot.c11
-rw-r--r--lib/string.c4
-rw-r--r--lib/strncpy_from_user.c19
-rw-r--r--lib/syscall.c11
-rw-r--r--lib/test_firmware.c9
-rw-r--r--lib/test_free_pages.c5
-rw-r--r--lib/test_kasan_module.c29
-rw-r--r--lib/test_kmod.c26
-rw-r--r--lib/test_lockup.c16
-rw-r--r--lib/test_ubsan.c74
-rw-r--r--lib/ubsan.c2
-rw-r--r--lib/zlib_dfltcc/dfltcc_inflate.c3
56 files changed, 732 insertions, 294 deletions
diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
index c789b39ed527..7d7097c5dc58 100644
--- a/lib/Kconfig.debug
+++ b/lib/Kconfig.debug
@@ -274,6 +274,15 @@ config DEBUG_INFO_BTF
Turning this on expects presence of pahole tool, which will convert
DWARF type info into equivalent deduplicated BTF type info.
+config PAHOLE_HAS_SPLIT_BTF
+ def_bool $(success, test `$(PAHOLE) --version | sed -E 's/v([0-9]+)\.([0-9]+)/\1\2/'` -ge "119")
+
+config DEBUG_INFO_BTF_MODULES
+ def_bool y
+ depends on DEBUG_INFO_BTF && MODULES && PAHOLE_HAS_SPLIT_BTF
+ help
+ Generate compact split BTF type information for kernel modules.
+
config GDB_SCRIPTS
bool "Provide GDB scripts for kernel debugging"
help
@@ -849,9 +858,31 @@ config DEBUG_PER_CPU_MAPS
Say N if unsure.
+config DEBUG_KMAP_LOCAL
+ bool "Debug kmap_local temporary mappings"
+ depends on DEBUG_KERNEL && KMAP_LOCAL
+ help
+ This option enables additional error checking for the kmap_local
+ infrastructure. Disable for production use.
+
+config ARCH_SUPPORTS_KMAP_LOCAL_FORCE_MAP
+ bool
+
+config DEBUG_KMAP_LOCAL_FORCE_MAP
+ bool "Enforce kmap_local temporary mappings"
+ depends on DEBUG_KERNEL && ARCH_SUPPORTS_KMAP_LOCAL_FORCE_MAP
+ select KMAP_LOCAL
+ select DEBUG_KMAP_LOCAL
+ help
+ This option enforces temporary mappings through the kmap_local
+ mechanism for non-highmem pages and on non-highmem systems.
+ Disable this for production systems!
+
config DEBUG_HIGHMEM
bool "Highmem debugging"
depends on DEBUG_KERNEL && HIGHMEM
+ select DEBUG_KMAP_LOCAL_FORCE_MAP if ARCH_SUPPORTS_KMAP_LOCAL_FORCE_MAP
+ select DEBUG_KMAP_LOCAL
help
This option enables additional error checking for high memory
systems. Disable for production systems.
@@ -2226,6 +2257,17 @@ config BITFIELD_KUNIT
If unsure, say N.
+config RESOURCE_KUNIT_TEST
+ tristate "KUnit test for resource API"
+ depends on KUNIT
+ help
+ This builds the resource API unit test.
+ Tests the logic of API provided by resource.c and ioport.h.
+ For more information on KUnit and unit tests in general please refer
+ to the KUnit documentation in Documentation/dev-tools/kunit/.
+
+ If unsure, say N.
+
config SYSCTL_KUNIT_TEST
tristate "KUnit test for sysctl" if !KUNIT_ALL_TESTS
depends on KUNIT
@@ -2269,6 +2311,17 @@ config LINEAR_RANGES_TEST
If unsure, say N.
+config CMDLINE_KUNIT_TEST
+ tristate "KUnit test for cmdline API"
+ depends on KUNIT
+ help
+ This builds the cmdline API unit test.
+ Tests the logic of API provided by cmdline.c.
+ For more information on KUnit and unit tests in general please refer
+ to the KUnit documentation in Documentation/dev-tools/kunit/.
+
+ If unsure, say N.
+
config BITS_TEST
tristate "KUnit test for bits.h"
depends on KUNIT
diff --git a/lib/Kconfig.kasan b/lib/Kconfig.kasan
index 542a9c18398e..8fb097057fec 100644
--- a/lib/Kconfig.kasan
+++ b/lib/Kconfig.kasan
@@ -136,15 +136,6 @@ config KASAN_STACK
default 1 if KASAN_STACK_ENABLE || CC_IS_GCC
default 0
-config KASAN_S390_4_LEVEL_PAGING
- bool "KASan: use 4-level paging"
- depends on S390
- help
- Compiling the kernel with KASan disables automatic 3-level vs
- 4-level paging selection. 3-level paging is used by default (up
- to 3TB of RAM with KASan enabled). This options allows to force
- 4-level paging instead.
-
config KASAN_SW_TAGS_IDENTIFY
bool "Enable memory corruption identification"
depends on KASAN_SW_TAGS
diff --git a/lib/Kconfig.ubsan b/lib/Kconfig.ubsan
index 58f8d03d037b..8b635fd75fe4 100644
--- a/lib/Kconfig.ubsan
+++ b/lib/Kconfig.ubsan
@@ -14,6 +14,7 @@ if UBSAN
config UBSAN_TRAP
bool "On Sanitizer warnings, abort the running kernel code"
+ depends on !COMPILE_TEST
depends on $(cc-option, -fsanitize-undefined-trap-on-error)
help
Building kernels with Sanitizer features enabled tends to grow
@@ -36,10 +37,17 @@ config UBSAN_KCOV_BROKEN
See https://bugs.llvm.org/show_bug.cgi?id=45831 for the status
in newer releases.
+config CC_HAS_UBSAN_BOUNDS
+ def_bool $(cc-option,-fsanitize=bounds)
+
+config CC_HAS_UBSAN_ARRAY_BOUNDS
+ def_bool $(cc-option,-fsanitize=array-bounds)
+
config UBSAN_BOUNDS
bool "Perform array index bounds checking"
default UBSAN
depends on !UBSAN_KCOV_BROKEN
+ depends on CC_HAS_UBSAN_ARRAY_BOUNDS || CC_HAS_UBSAN_BOUNDS
help
This option enables detection of directly indexed out of bounds
array accesses, where the array size is known at compile time.
@@ -47,36 +55,121 @@ config UBSAN_BOUNDS
to the {str,mem}*cpy() family of functions (that is addressed
by CONFIG_FORTIFY_SOURCE).
+config UBSAN_ONLY_BOUNDS
+ def_bool CC_HAS_UBSAN_BOUNDS && !CC_HAS_UBSAN_ARRAY_BOUNDS
+ depends on UBSAN_BOUNDS
+ help
+ This is a weird case: Clang's -fsanitize=bounds includes
+ -fsanitize=local-bounds, but it's trapping-only, so for
+ Clang, we must use -fsanitize=array-bounds when we want
+ traditional array bounds checking enabled. For GCC, we
+ want -fsanitize=bounds.
+
+config UBSAN_ARRAY_BOUNDS
+ def_bool CC_HAS_UBSAN_ARRAY_BOUNDS
+ depends on UBSAN_BOUNDS
+
config UBSAN_LOCAL_BOUNDS
bool "Perform array local bounds checking"
depends on UBSAN_TRAP
- depends on CC_IS_CLANG
depends on !UBSAN_KCOV_BROKEN
+ depends on $(cc-option,-fsanitize=local-bounds)
help
This option enables -fsanitize=local-bounds which traps when an
- exception/error is detected. Therefore, it should be enabled only
- if trapping is expected.
+ exception/error is detected. Therefore, it may only be enabled
+ with CONFIG_UBSAN_TRAP.
+
Enabling this option detects errors due to accesses through a
pointer that is derived from an object of a statically-known size,
where an added offset (which may not be known statically) is
out-of-bounds.
-config UBSAN_MISC
- bool "Enable all other Undefined Behavior sanity checks"
+config UBSAN_SHIFT
+ bool "Perform checking for bit-shift overflows"
+ default UBSAN
+ depends on $(cc-option,-fsanitize=shift)
+ help
+ This option enables -fsanitize=shift which checks for bit-shift
+ operations that overflow to the left or go switch to negative
+ for signed types.
+
+config UBSAN_DIV_ZERO
+ bool "Perform checking for integer divide-by-zero"
+ depends on $(cc-option,-fsanitize=integer-divide-by-zero)
+ help
+ This option enables -fsanitize=integer-divide-by-zero which checks
+ for integer division by zero. This is effectively redundant with the
+ kernel's existing exception handling, though it can provide greater
+ debugging information under CONFIG_UBSAN_REPORT_FULL.
+
+config UBSAN_UNREACHABLE
+ bool "Perform checking for unreachable code"
+ # objtool already handles unreachable checking and gets angry about
+ # seeing UBSan instrumentation located in unreachable places.
+ depends on !STACK_VALIDATION
+ depends on $(cc-option,-fsanitize=unreachable)
+ help
+ This option enables -fsanitize=unreachable which checks for control
+ flow reaching an expected-to-be-unreachable position.
+
+config UBSAN_SIGNED_OVERFLOW
+ bool "Perform checking for signed arithmetic overflow"
+ default UBSAN
+ depends on $(cc-option,-fsanitize=signed-integer-overflow)
+ help
+ This option enables -fsanitize=signed-integer-overflow which checks
+ for overflow of any arithmetic operations with signed integers.
+
+config UBSAN_UNSIGNED_OVERFLOW
+ bool "Perform checking for unsigned arithmetic overflow"
+ depends on $(cc-option,-fsanitize=unsigned-integer-overflow)
+ help
+ This option enables -fsanitize=unsigned-integer-overflow which checks
+ for overflow of any arithmetic operations with unsigned integers. This
+ currently causes x86 to fail to boot.
+
+config UBSAN_OBJECT_SIZE
+ bool "Perform checking for accesses beyond the end of objects"
+ default UBSAN
+ # gcc hugely expands stack usage with -fsanitize=object-size
+ # https://lore.kernel.org/lkml/CAHk-=wjPasyJrDuwDnpHJS2TuQfExwe=px-SzLeN8GFMAQJPmQ@mail.gmail.com/
+ depends on !CC_IS_GCC
+ depends on $(cc-option,-fsanitize=object-size)
+ help
+ This option enables -fsanitize=object-size which checks for accesses
+ beyond the end of objects where the optimizer can determine both the
+ object being operated on and its size, usually seen with bad downcasts,
+ or access to struct members from NULL pointers.
+
+config UBSAN_BOOL
+ bool "Perform checking for non-boolean values used as boolean"
+ default UBSAN
+ depends on $(cc-option,-fsanitize=bool)
+ help
+ This option enables -fsanitize=bool which checks for boolean values being
+ loaded that are neither 0 nor 1.
+
+config UBSAN_ENUM
+ bool "Perform checking for out of bounds enum values"
default UBSAN
+ depends on $(cc-option,-fsanitize=enum)
help
- This option enables all sanity checks that don't have their
- own Kconfig options. Disable this if you only want to have
- individually selected checks.
+ This option enables -fsanitize=enum which checks for values being loaded
+ into an enum that are outside the range of given values for the given enum.
+
+config UBSAN_ALIGNMENT
+ bool "Perform checking for misaligned pointer usage"
+ default !HAVE_EFFICIENT_UNALIGNED_ACCESS
+ depends on !UBSAN_TRAP && !COMPILE_TEST
+ depends on $(cc-option,-fsanitize=alignment)
+ help
+ This option enables the check of unaligned memory accesses.
+ Enabling this option on architectures that support unaligned
+ accesses may produce a lot of false positives.
config UBSAN_SANITIZE_ALL
bool "Enable instrumentation for the entire kernel"
depends on ARCH_HAS_UBSAN_SANITIZE_ALL
-
- # We build with -Wno-maybe-uninitilzed, but we still want to
- # use -Wmaybe-uninitilized in allmodconfig builds.
- # So dependsy bellow used to disable this option in allmodconfig
- depends on !COMPILE_TEST
default y
help
This option activates instrumentation for the entire kernel.
@@ -85,15 +178,6 @@ config UBSAN_SANITIZE_ALL
Enabling this option will get kernel image size increased
significantly.
-config UBSAN_ALIGNMENT
- bool "Enable checks for pointers alignment"
- default !HAVE_EFFICIENT_UNALIGNED_ACCESS
- depends on !UBSAN_TRAP
- help
- This option enables the check of unaligned memory accesses.
- Enabling this option on architectures that support unaligned
- accesses may produce a lot of false positives.
-
config TEST_UBSAN
tristate "Module for testing for undefined behavior detection"
depends on m
diff --git a/lib/Makefile b/lib/Makefile
index ce45af50983a..8598e8796edf 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -107,7 +107,7 @@ obj-$(CONFIG_TEST_FREE_PAGES) += test_free_pages.o
# off the generation of FPU/SSE* instructions for kernel proper but FPU_FLAGS
# get appended last to CFLAGS and thus override those previous compiler options.
#
-FPU_CFLAGS := -mhard-float -msse -msse2
+FPU_CFLAGS := -msse -msse2
ifdef CONFIG_CC_IS_GCC
# Stack alignment mismatch, proceed with caution.
# GCC < 7.1 cannot compile code using `double` and -mpreferred-stack-boundary=3
@@ -120,6 +120,7 @@ ifdef CONFIG_CC_IS_GCC
# -mpreferred-stack-boundary=3 is not between 4 and 12
#
# can be triggered. Otherwise gcc doesn't complain.
+FPU_CFLAGS += -mhard-float
FPU_CFLAGS += $(call cc-option,-msse -mpreferred-stack-boundary=3,-mpreferred-stack-boundary=4)
endif
@@ -352,3 +353,4 @@ obj-$(CONFIG_BITFIELD_KUNIT) += bitfield_kunit.o
obj-$(CONFIG_LIST_KUNIT_TEST) += list-test.o
obj-$(CONFIG_LINEAR_RANGES_TEST) += test_linear_ranges.o
obj-$(CONFIG_BITS_TEST) += test_bits.o
+obj-$(CONFIG_CMDLINE_KUNIT_TEST) += cmdline_kunit.o
diff --git a/lib/cmdline.c b/lib/cmdline.c
index d11fdc579170..b390dd03363b 100644
--- a/lib/cmdline.c
+++ b/lib/cmdline.c
@@ -35,25 +35,37 @@ static int get_range(char **str, int *pint, int n)
/**
* get_option - Parse integer from an option string
* @str: option string
- * @pint: (output) integer value parsed from @str
+ * @pint: (optional output) integer value parsed from @str
*
* Read an int from an option string; if available accept a subsequent
* comma as well.
*
+ * When @pint is NULL the function can be used as a validator of
+ * the current option in the string.
+ *
* Return values:
* 0 - no int in string
* 1 - int found, no subsequent comma
* 2 - int found including a subsequent comma
* 3 - hyphen found to denote a range
+ *
+ * Leading hyphen without integer is no integer case, but we consume it
+ * for the sake of simplification.
*/
int get_option(char **str, int *pint)
{
char *cur = *str;
+ int value;
if (!cur || !(*cur))
return 0;
- *pint = simple_strtol(cur, str, 0);
+ if (*cur == '-')
+ value = -simple_strtoull(++cur, str, 0);
+ else
+ value = simple_strtoull(cur, str, 0);
+ if (pint)
+ *pint = value;
if (cur == *str)
return 0;
if (**str == ',') {
diff --git a/lib/cmdline_kunit.c b/lib/cmdline_kunit.c
new file mode 100644
index 000000000000..550e7a47fd24
--- /dev/null
+++ b/lib/cmdline_kunit.c
@@ -0,0 +1,100 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Test cases for API provided by cmdline.c
+ */
+
+#include <kunit/test.h>
+#include <linux/kernel.h>
+#include <linux/random.h>
+#include <linux/string.h>
+
+static const char *cmdline_test_strings[] = {
+ "\"\"", "" , "=" , "\"-", "," , "-," , ",-" , "-" ,
+ "+," , "--", ",,", "''" , "\"\",", "\",\"", "-\"\"", "\"",
+};
+
+static const int cmdline_test_values[] = {
+ 1, 1, 1, 1, 2, 3, 2, 3,
+ 1, 3, 2, 1, 1, 1, 3, 1,
+};
+
+static void cmdline_do_one_test(struct kunit *test, const char *in, int rc, int offset)
+{
+ const char *fmt = "Pattern: %s";
+ const char *out = in;
+ int dummy;
+ int ret;
+
+ ret = get_option((char **)&out, &dummy);
+
+ KUNIT_EXPECT_EQ_MSG(test, ret, rc, fmt, in);
+ KUNIT_EXPECT_PTR_EQ_MSG(test, out, in + offset, fmt, in);
+}
+
+static void cmdline_test_noint(struct kunit *test)
+{
+ unsigned int i = 0;
+
+ do {
+ const char *str = cmdline_test_strings[i];
+ int rc = 0;
+ int offset;
+
+ /* Only first and leading '-' will advance the pointer */
+ offset = !!(*str == '-');
+ cmdline_do_one_test(test, str, rc, offset);
+ } while (++i < ARRAY_SIZE(cmdline_test_strings));
+}
+
+static void cmdline_test_lead_int(struct kunit *test)
+{
+ unsigned int i = 0;
+ char in[32];
+
+ do {
+ const char *str = cmdline_test_strings[i];
+ int rc = cmdline_test_values[i];
+ int offset;
+
+ sprintf(in, "%u%s", get_random_int() % 256, str);
+ /* Only first '-' after the number will advance the pointer */
+ offset = strlen(in) - strlen(str) + !!(rc == 2);
+ cmdline_do_one_test(test, in, rc, offset);
+ } while (++i < ARRAY_SIZE(cmdline_test_strings));
+}
+
+static void cmdline_test_tail_int(struct kunit *test)
+{
+ unsigned int i = 0;
+ char in[32];
+
+ do {
+ const char *str = cmdline_test_strings[i];
+ /* When "" or "-" the result will be valid integer */
+ int rc = strcmp(str, "") ? (strcmp(str, "-") ? 0 : 1) : 1;
+ int offset;
+
+ sprintf(in, "%s%u", str, get_random_int() % 256);
+ /*
+ * Only first and leading '-' not followed by integer
+ * will advance the pointer.
+ */
+ offset = rc ? strlen(in) : !!(*str == '-');
+ cmdline_do_one_test(test, in, rc, offset);
+ } while (++i < ARRAY_SIZE(cmdline_test_strings));
+}
+
+static struct kunit_case cmdline_test_cases[] = {
+ KUNIT_CASE(cmdline_test_noint),
+ KUNIT_CASE(cmdline_test_lead_int),
+ KUNIT_CASE(cmdline_test_tail_int),
+ {}
+};
+
+static struct kunit_suite cmdline_test_suite = {
+ .name = "cmdline",
+ .test_cases = cmdline_test_cases,
+};
+kunit_test_suite(cmdline_test_suite);
+
+MODULE_LICENSE("GPL");
diff --git a/lib/cpumask.c b/lib/cpumask.c
index 85da6ab4fbb5..35924025097b 100644
--- a/lib/cpumask.c
+++ b/lib/cpumask.c
@@ -267,3 +267,21 @@ int cpumask_any_and_distribute(const struct cpumask *src1p,
return next;
}
EXPORT_SYMBOL(cpumask_any_and_distribute);
+
+int cpumask_any_distribute(const struct cpumask *srcp)
+{
+ int next, prev;
+
+ /* NOTE: our first selection will skip 0. */
+ prev = __this_cpu_read(distribute_cpu_mask_prev);
+
+ next = cpumask_next(prev, srcp);
+ if (next >= nr_cpu_ids)
+ next = cpumask_first(srcp);
+
+ if (next < nr_cpu_ids)
+ __this_cpu_write(distribute_cpu_mask_prev, next);
+
+ return next;
+}
+EXPORT_SYMBOL(cpumask_any_distribute);
diff --git a/lib/crypto/blake2s-selftest.c b/lib/crypto/blake2s-selftest.c
index 79ef404a990d..5d9ea53be973 100644
--- a/lib/crypto/blake2s-selftest.c
+++ b/lib/crypto/blake2s-selftest.c
@@ -3,7 +3,7 @@
* Copyright (C) 2015-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
*/
-#include <crypto/blake2s.h>
+#include <crypto/internal/blake2s.h>
#include <linux/string.h>
/*
diff --git a/lib/crypto/blake2s.c b/lib/crypto/blake2s.c
index 41025a30c524..6a4b6b78d630 100644
--- a/lib/crypto/blake2s.c
+++ b/lib/crypto/blake2s.c
@@ -17,8 +17,6 @@
#include <linux/bug.h>
#include <asm/unaligned.h>
-bool blake2s_selftest(void);
-
void blake2s_update(struct blake2s_state *state, const u8 *in, size_t inlen)
{
const size_t fill = BLAKE2S_BLOCK_SIZE - state->buflen;
diff --git a/lib/crypto/curve25519.c b/lib/crypto/curve25519.c
index 288a62cd29b2..fb29739e8c29 100644
--- a/lib/crypto/curve25519.c
+++ b/lib/crypto/curve25519.c
@@ -13,8 +13,6 @@
#include <linux/module.h>
#include <linux/init.h>
-bool curve25519_selftest(void);
-
static int __init mod_init(void)
{
if (!IS_ENABLED(CONFIG_CRYPTO_MANAGER_DISABLE_TESTS) &&
diff --git a/lib/crypto/sha256.c b/lib/crypto/sha256.c
index 2321f6cb322f..72a4b0b1df28 100644
--- a/lib/crypto/sha256.c
+++ b/lib/crypto/sha256.c
@@ -15,9 +15,28 @@
#include <linux/export.h>
#include <linux/module.h>
#include <linux/string.h>
-#include <crypto/sha.h>
+#include <crypto/sha2.h>
#include <asm/unaligned.h>
+static const u32 SHA256_K[] = {
+ 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
+ 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
+ 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
+ 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
+ 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
+ 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
+ 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
+ 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
+ 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
+ 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
+ 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
+ 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
+ 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
+ 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
+ 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
+ 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
+};
+
static inline u32 Ch(u32 x, u32 y, u32 z)
{
return z ^ (x & (y ^ z));
@@ -43,173 +62,68 @@ static inline void BLEND_OP(int I, u32 *W)
W[I] = s1(W[I-2]) + W[I-7] + s0(W[I-15]) + W[I-16];
}
-static void sha256_transform(u32 *state, const u8 *input)
+#define SHA256_ROUND(i, a, b, c, d, e, f, g, h) do { \
+ u32 t1, t2; \
+ t1 = h + e1(e) + Ch(e, f, g) + SHA256_K[i] + W[i]; \
+ t2 = e0(a) + Maj(a, b, c); \
+ d += t1; \
+ h = t1 + t2; \
+} while (0)
+
+static void sha256_transform(u32 *state, const u8 *input, u32 *W)
{
- u32 a, b, c, d, e, f, g, h, t1, t2;
- u32 W[64];
+ u32 a, b, c, d, e, f, g, h;
int i;
/* load the input */
- for (i = 0; i < 16; i++)
- LOAD_OP(i, W, input);
+ for (i = 0; i < 16; i += 8) {
+ LOAD_OP(i + 0, W, input);
+ LOAD_OP(i + 1, W, input);
+ LOAD_OP(i + 2, W, input);
+ LOAD_OP(i + 3, W, input);
+ LOAD_OP(i + 4, W, input);
+ LOAD_OP(i + 5, W, input);
+ LOAD_OP(i + 6, W, input);
+ LOAD_OP(i + 7, W, input);
+ }
/* now blend */
- for (i = 16; i < 64; i++)
- BLEND_OP(i, W);
+ for (i = 16; i < 64; i += 8) {
+ BLEND_OP(i + 0, W);
+ BLEND_OP(i + 1, W);
+ BLEND_OP(i + 2, W);
+ BLEND_OP(i + 3, W);
+ BLEND_OP(i + 4, W);
+ BLEND_OP(i + 5, W);
+ BLEND_OP(i + 6, W);
+ BLEND_OP(i + 7, W);
+ }
/* load the state into our registers */
a = state[0]; b = state[1]; c = state[2]; d = state[3];
e = state[4]; f = state[5]; g = state[6]; h = state[7];
/* now iterate */
- t1 = h + e1(e) + Ch(e, f, g) + 0x428a2f98 + W[0];
- t2 = e0(a) + Maj(a, b, c); d += t1; h = t1 + t2;
- t1 = g + e1(d) + Ch(d, e, f) + 0x71374491 + W[1];
- t2 = e0(h) + Maj(h, a, b); c += t1; g = t1 + t2;
- t1 = f + e1(c) + Ch(c, d, e) + 0xb5c0fbcf + W[2];
- t2 = e0(g) + Maj(g, h, a); b += t1; f = t1 + t2;
- t1 = e + e1(b) + Ch(b, c, d) + 0xe9b5dba5 + W[3];
- t2 = e0(f) + Maj(f, g, h); a += t1; e = t1 + t2;