android_kernel_samsung_msm8226/crypto
James Yonan 72c40f2b75 crypto: crypto_memneq - add equality testing of memory regions w/o timing leaks
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.

We tried to locate all of the places in the crypto API where memcmp was
being used for authentication or integrity checking, and convert them
over to crypto_memneq.

crypto_memneq is declared noinline, placed in its own source file,
and compiled with optimizations that might increase code size disabled
("Os") because a smart compiler (or LTO) might notice that the return
value is always compared against zero/nonzero, and might then
reintroduce the same early-return optimization that we are trying to
avoid.

Using #pragma or __attribute__ optimization annotations of the code
for disabling optimization was avoided as it seems to be considered
broken or unmaintained for long time in GCC [1]. Therefore, we work
around that by specifying the compile flag for memneq.o directly in
the Makefile. We found that this seems to be most appropriate.

As we use ("Os"), this patch also provides a loop-free "fast-path" for
frequently used 16 byte digests. Similarly to kernel library string
functions, leave an option for future even further optimized architecture
specific assembler implementations.

This was a joint work of James Yonan and Daniel Borkmann. Also thanks
for feedback from Florian Weimer on this and earlier proposals [2].

  [1] http://gcc.gnu.org/ml/gcc/2012-07/msg00211.html
  [2] https://lkml.org/lkml/2013/2/10/131

Change-Id: Ic56362242ad941c1bf1c1199ee5f7d05a2e144eb
Signed-off-by: James Yonan <james@openvpn.net>
Signed-off-by: Daniel Borkmann <dborkman@redhat.com>
Cc: Florian Weimer <fw@deneb.enyo.de>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: Kevin F. Haggerty <haggertk@lineageos.org>
2020-01-06 08:40:48 +01:00
..
async_tx crypto: remove the second argument of k[un]map_atomic() 2012-03-20 21:48:16 +08:00
ablk_helper.c crypto: create generic version of ablk_helper 2015-12-23 16:54:28 +05:30
ablkcipher.c Merge tag 'v3.4.113' into lineage-16.0 2019-08-05 14:20:47 +02:00
aead.c Merge tag 'v3.4.113' into lineage-16.0 2019-08-05 14:20:47 +02:00
aes_generic.c crypto: aes_generic - Fix checkpatch errors 2010-02-16 20:34:59 +08:00
af_alg.c Merge tag 'v3.4.113' into lineage-16.0 2019-08-05 14:20:47 +02:00
ahash.c crypto: ahash - Fix EINPROGRESS notification callback 2019-08-06 12:26:39 +02:00
algapi.c Merge tag 'v3.4.113' into lineage-16.0 2019-08-05 14:20:47 +02:00
algboss.c misc: Import SM-G900H kernel source code 2019-08-02 15:14:10 +02:00
algif_hash.c Merge tag 'v3.4.113' into lineage-16.0 2019-08-05 14:20:47 +02:00
algif_skcipher.c Merge tag 'v3.4.113' into lineage-16.0 2019-08-05 14:20:47 +02:00
ansi_cprng.c misc: Import SM-G900H kernel source code 2019-08-02 15:14:10 +02:00
anubis.c crypto: anubis - Fix checkpatch errors 2010-02-16 20:25:39 +08:00
api.c Merge tag 'v3.4.113' into lineage-16.0 2019-08-05 14:20:47 +02:00
arc4.c crypto: arc4 - Fixed coding style issues 2011-06-30 07:44:05 +08:00
authenc.c crypto: crypto_memneq - add equality testing of memory regions w/o timing leaks 2020-01-06 08:40:48 +01:00
authencesn.c crypto: crypto_memneq - add equality testing of memory regions w/o timing leaks 2020-01-06 08:40:48 +01:00
blkcipher.c Merge tag 'v3.4.113' into lineage-16.0 2019-08-05 14:20:47 +02:00
blowfish_common.c crypto: blowfish - split generic and common c code 2011-09-22 21:25:25 +10:00
blowfish_generic.c crypto: blowfish - rename C-version to blowfish_generic 2011-09-22 21:25:26 +10:00
camellia_generic.c crypto: camellia - rename camellia.c to camellia_generic.c 2012-03-14 17:25:55 +08:00
cast5.c crypto: cast5 - simplify if-statements 2010-11-13 21:47:55 +09:00
cast6.c crypto: cast6 - Fix checkpatch errors 2010-02-16 20:29:45 +08:00
cbc.c misc: Import SM-G900H kernel source code 2019-08-02 15:14:10 +02:00
ccm.c crypto: crypto_memneq - add equality testing of memory regions w/o timing leaks 2020-01-06 08:40:48 +01:00
chainiv.c misc: Import SM-G900H kernel source code 2019-08-02 15:14:10 +02:00
cipher.c misc: Import SM-G900H kernel source code 2019-08-02 15:14:10 +02:00
compress.c crypto: compress - Fix checkpatch errors 2010-02-16 20:31:04 +08:00
crc32c.c crypto: crc32c should use library implementation 2012-03-23 16:58:38 -07:00
cryptd.c Merge tag 'v3.4.113' into lineage-16.0 2019-08-05 14:20:47 +02:00
crypto_null.c crypto: crypto_null - Fix checkpatch errors 2010-02-16 20:31:54 +08:00
crypto_user.c Merge tag 'v3.4.113' into lineage-16.0 2019-08-05 14:20:47 +02:00
crypto_wq.c Merge tag 'v3.4.113' into lineage-16.0 2019-08-05 14:20:47 +02:00
ctr.c crypto: Use ERR_CAST 2010-05-26 10:36:51 +10:00
cts.c
deflate.c net+crypto: Use vmalloc for zlib inflate buffers. 2011-06-29 05:48:41 -07:00
des_generic.c Blackfin: Rename DES PC2() symbol to avoid collision 2010-10-07 14:08:50 +01:00
drbg.c misc: Import SM-G900H kernel source code 2019-08-02 15:14:10 +02:00
ecb.c crypto: ecb - Fix checkpatch errors 2010-02-16 20:33:49 +08:00
eseqiv.c misc: Import SM-G900H kernel source code 2019-08-02 15:14:10 +02:00
fcrypt.c crypto: fcrypt - Fix checkpatch errors 2010-02-16 20:34:07 +08:00
fips.c misc: Import SM-G900H kernel source code 2019-08-02 15:14:10 +02:00
fips_integrity.c misc: Import SM-G900H kernel source code 2019-08-02 15:14:10 +02:00
first_file.c misc: Import SM-G900H kernel source code 2019-08-02 15:14:10 +02:00
gcm.c crypto: crypto_memneq - add equality testing of memory regions w/o timing leaks 2020-01-06 08:40:48 +01:00
gf128mul.c crypto: gf128mul - fix call to memset() 2011-07-08 17:21:21 +08:00
ghash-generic.c crypto: ghash - Avoid null pointer dereference if no key is set 2011-10-21 13:18:42 +02:00
hmac.c misc: Import SM-G900H kernel source code 2019-08-02 15:14:10 +02:00
internal.h crypto: Allow kernel to compile without CONFIG_CRYPTO_FIPS 2020-01-06 08:40:43 +01:00
Kconfig crypto: add lz4 Cryptographic API 2019-08-06 08:17:38 +02:00
khazad.c
krng.c
last_file.c misc: Import SM-G900H kernel source code 2019-08-02 15:14:10 +02:00
lrw.c crypto: lrw - add interface for parallelized cipher implementions 2011-11-09 11:50:31 +08:00
lz4.c crypto: lz4,lz4hc - fix decompression 2019-08-06 08:17:38 +02:00
lz4hc.c crypto: lz4,lz4hc - fix decompression 2019-08-06 08:17:38 +02:00
lzo.c
Makefile crypto: crypto_memneq - add equality testing of memory regions w/o timing leaks 2020-01-06 08:40:48 +01:00
md4.c crypto: add module.h to those files that are explicitly using it 2011-10-31 19:31:11 -04:00
md5.c crypto: Move md5_transform to lib/md5.c 2011-08-06 18:32:45 -07:00
memneq.c crypto: crypto_memneq - add equality testing of memory regions w/o timing leaks 2020-01-06 08:40:48 +01:00
michael_mic.c
pcbc.c
pcompress.c Merge tag 'v3.4.113' into lineage-16.0 2019-08-05 14:20:47 +02:00
pcrypt.c crypto: pcrypt - Use the online cpumask as the default 2012-03-29 19:52:47 +08:00
proc.c crypto: Allow kernel to compile without CONFIG_CRYPTO_FIPS 2020-01-06 08:40:43 +01:00
ripemd.h
rmd128.c crypto: ripemd - Set module author and update email address 2011-01-04 23:34:03 +11:00
rmd160.c crypto: ripemd - Set module author and update email address 2011-01-04 23:34:03 +11:00
rmd256.c crypto: ripemd - Set module author and update email address 2011-01-04 23:34:03 +11:00
rmd320.c crypto: ripemd - Set module author and update email address 2011-01-04 23:34:03 +11:00
rng.c Merge tag 'v3.4.113' into lineage-16.0 2019-08-05 14:20:47 +02:00
salsa20_generic.c
scatterwalk.c crypto: remove the second argument of k[un]map_atomic() 2012-03-20 21:48:16 +08:00
seed.c
seqiv.c misc: Import SM-G900H kernel source code 2019-08-02 15:14:10 +02:00
serpent_generic.c crypto: serpent - rename serpent.c to serpent_generic.c 2011-11-30 16:12:44 +08:00
sha1_generic.c crypto: sha1 - export sha1_update for reuse 2011-08-10 19:00:28 +08:00
sha256_generic.c crypto: sha256 - Expose SHA256 generic routine to be callable externally. 2015-12-23 17:09:15 +05:30
sha512_generic.c crypto: sha512 - Expose generic sha512 routine to be callable from other modules 2015-12-23 17:11:14 +05:30
shash.c Merge tag 'v3.4.113' into lineage-16.0 2019-08-05 14:20:47 +02:00
tcrypt.c crypto: Allow kernel to compile without CONFIG_CRYPTO_FIPS 2020-01-06 08:40:43 +01:00
tcrypt.h crypto: tcrypt - add xts(serpent) tests 2011-11-09 11:57:56 +08:00
tea.c
testmgr.c misc: Import SM-G900H kernel source code 2019-08-02 15:14:10 +02:00
testmgr.h Merge tag 'v3.4.113' into lineage-16.0 2019-08-05 14:20:47 +02:00
tgr192.c
twofish_common.c crypto: twofish-x86_64-3way - add lrw support 2011-11-09 11:53:32 +08:00
twofish_generic.c crypto: twofish: Rename twofish to twofish_generic and add an alias 2010-06-03 21:02:51 +10:00
vmac.c crypto: add module.h to those files that are explicitly using it 2011-10-31 19:31:11 -04:00
wp512.c crypto: whirlpool - count rounds from 0 2011-10-21 14:24:16 +02:00
xcbc.c crypto: add module.h to those files that are explicitly using it 2011-10-31 19:31:11 -04:00
xor.c include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h 2010-03-30 22:02:32 +09:00
xts.c crypto: xts: add interface for parallelized cipher implementations 2011-11-09 11:56:06 +08:00
zlib.c net+crypto: Use vmalloc for zlib inflate buffers. 2011-06-29 05:48:41 -07:00