summaryrefslogtreecommitdiffstats
path: root/include/crypto.h
diff options
context:
space:
mode:
authorSascha Hauer <s.hauer@pengutronix.de>2021-03-05 09:11:33 +0100
committerSascha Hauer <s.hauer@pengutronix.de>2021-07-30 19:50:36 +0200
commit610db8d457fe7f479e93fb4d88e869792133f9d5 (patch)
tree696db76a981c3d3f72edb95e6e77840326f4e259 /include/crypto.h
parentbfaf09f184246e543fdb0af38b318997a8774400 (diff)
downloadbarebox-610db8d457fe7f479e93fb4d88e869792133f9d5.tar.gz
barebox-610db8d457fe7f479e93fb4d88e869792133f9d5.tar.xz
crypto: add crypto_memneq()
This adds crypto_memneq() from Linux for the same reason it is present in Linux. From the commit message adding it: When comparing MAC hashes, AEAD authentication tags, or other hash values in the context of authentication or integrity checking, it is important not to leak timing information to a potential attacker, i.e. when communication happens over a network. Bytewise memory comparisons (such as memcmp) are usually optimized so that they return a nonzero value as soon as a mismatch is found. E.g, on x86_64/i5 for 512 bytes this can be ~50 cyc for a full mismatch and up to ~850 cyc for a full match (cold). This early-return behavior can leak timing information as a side channel, allowing an attacker to iteratively guess the correct result. This patch adds a new method crypto_memneq ("memory not equal to each other") to the crypto API that compares memory areas of the same length in roughly "constant time" (cache misses could change the timing, but since they don't reveal information about the content of the strings being compared, they are effectively benign). Iow, best and worst case behaviour take the same amount of time to complete (in contrast to memcmp). Note that crypto_memneq (unlike memcmp) can only be used to test for equality or inequality, NOT for lexicographical order. This, however, is not an issue for its use-cases within the crypto API. Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'include/crypto.h')
-rw-r--r--include/crypto.h17
1 files changed, 17 insertions, 0 deletions
diff --git a/include/crypto.h b/include/crypto.h
index ac70111cab..4aca467de9 100644
--- a/include/crypto.h
+++ b/include/crypto.h
@@ -24,4 +24,21 @@ struct ablkcipher_request {
void __iomem *src;
};
+noinline unsigned long __crypto_memneq(const void *a, const void *b, size_t size);
+
+/**
+ * crypto_memneq - Compare two areas of memory without leaking
+ * timing information.
+ *
+ * @a: One area of memory
+ * @b: Another area of memory
+ * @size: The size of the area.
+ *
+ * Returns 0 when data is equal, 1 otherwise.
+ */
+static inline int crypto_memneq(const void *a, const void *b, size_t size)
+{
+ return __crypto_memneq(a, b, size) != 0UL ? 1 : 0;
+}
+
#endif