10 Jan, 2019

4 commits

  • sm3_compress() calls rol32() with shift >= 32, which causes undefined
    behavior. This is easily detected by enabling CONFIG_UBSAN.

    Explicitly AND with 31 to make the behavior well defined.

    Fixes: 4f0fc1600edb ("crypto: sm3 - add OSCCA SM3 secure hash")
    Cc: # v4.15+
    Cc: Gilad Ben-Yossef
    Signed-off-by: Eric Biggers
    Signed-off-by: Herbert Xu

    Eric Biggers
     
  • crypto_grab_*() doesn't set crypto_spawn::inst, so templates must set it
    beforehand. Otherwise it will be left NULL, which causes a crash in
    certain cases where algorithms are dynamically loaded/unloaded. E.g.
    with CONFIG_CRYPTO_CHACHA20_X86_64=m, the following caused a crash:

    insmod chacha-x86_64.ko
    python -c 'import socket; socket.socket(socket.AF_ALG, 5, 0).bind(("skcipher", "adiantum(xchacha12,aes)"))'
    rmmod chacha-x86_64.ko
    python -c 'import socket; socket.socket(socket.AF_ALG, 5, 0).bind(("skcipher", "adiantum(xchacha12,aes)"))'

    Fixes: 059c2a4d8e16 ("crypto: adiantum - add Adiantum support")
    Signed-off-by: Eric Biggers
    Signed-off-by: Herbert Xu

    Eric Biggers
     
  • Authencesn template in decrypt path unconditionally calls aead_request_complete
    after ahash_verify which leads to following kernel panic in after decryption.

    [ 338.539800] BUG: unable to handle kernel NULL pointer dereference at 0000000000000004
    [ 338.548372] PGD 0 P4D 0
    [ 338.551157] Oops: 0000 [#1] SMP PTI
    [ 338.554919] CPU: 0 PID: 0 Comm: swapper/0 Kdump: loaded Tainted: G W I 4.19.7+ #13
    [ 338.564431] Hardware name: Supermicro X8ST3/X8ST3, BIOS 2.0 07/29/10
    [ 338.572212] RIP: 0010:esp_input_done2+0x350/0x410 [esp4]
    [ 338.578030] Code: ff 0f b6 68 10 48 8b 83 c8 00 00 00 e9 8e fe ff ff 8b 04 25 04 00 00 00 83 e8 01 48 98 48 8b 3c c5 10 00 00 00 e9 f7 fd ff ff 04 25 04 00 00 00 83 e8 01 48 98 4c 8b 24 c5 10 00 00 00 e9 3b
    [ 338.598547] RSP: 0018:ffff911c97803c00 EFLAGS: 00010246
    [ 338.604268] RAX: 0000000000000002 RBX: ffff911c4469ee00 RCX: 0000000000000000
    [ 338.612090] RDX: 0000000000000000 RSI: 0000000000000130 RDI: ffff911b87c20400
    [ 338.619874] RBP: 0000000000000000 R08: ffff911b87c20498 R09: 000000000000000a
    [ 338.627610] R10: 0000000000000001 R11: 0000000000000004 R12: 0000000000000000
    [ 338.635402] R13: ffff911c89590000 R14: ffff911c91730000 R15: 0000000000000000
    [ 338.643234] FS: 0000000000000000(0000) GS:ffff911c97800000(0000) knlGS:0000000000000000
    [ 338.652047] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
    [ 338.658299] CR2: 0000000000000004 CR3: 00000001ec20a000 CR4: 00000000000006f0
    [ 338.666382] Call Trace:
    [ 338.669051]
    [ 338.671254] esp_input_done+0x12/0x20 [esp4]
    [ 338.675922] chcr_handle_resp+0x3b5/0x790 [chcr]
    [ 338.680949] cpl_fw6_pld_handler+0x37/0x60 [chcr]
    [ 338.686080] chcr_uld_rx_handler+0x22/0x50 [chcr]
    [ 338.691233] uldrx_handler+0x8c/0xc0 [cxgb4]
    [ 338.695923] process_responses+0x2f0/0x5d0 [cxgb4]
    [ 338.701177] ? bitmap_find_next_zero_area_off+0x3a/0x90
    [ 338.706882] ? matrix_alloc_area.constprop.7+0x60/0x90
    [ 338.712517] ? apic_update_irq_cfg+0x82/0xf0
    [ 338.717177] napi_rx_handler+0x14/0xe0 [cxgb4]
    [ 338.722015] net_rx_action+0x2aa/0x3e0
    [ 338.726136] __do_softirq+0xcb/0x280
    [ 338.730054] irq_exit+0xde/0xf0
    [ 338.733504] do_IRQ+0x54/0xd0
    [ 338.736745] common_interrupt+0xf/0xf

    Fixes: 104880a6b470 ("crypto: authencesn - Convert to new AEAD...")
    Signed-off-by: Harsh Jain
    Cc: stable@vger.kernel.org
    Signed-off-by: Herbert Xu

    Harsh Jain
     
  • Keys for "authenc" AEADs are formatted as an rtattr containing a 4-byte
    'enckeylen', followed by an authentication key and an encryption key.
    crypto_authenc_extractkeys() parses the key to find the inner keys.

    However, it fails to consider the case where the rtattr's payload is
    longer than 4 bytes but not 4-byte aligned, and where the key ends
    before the next 4-byte aligned boundary. In this case, 'keylen -=
    RTA_ALIGN(rta->rta_len);' underflows to a value near UINT_MAX. This
    causes a buffer overread and crash during crypto_ahash_setkey().

    Fix it by restricting the rtattr payload to the expected size.

    Reproducer using AF_ALG:

    #include
    #include
    #include

    int main()
    {
    int fd;
    struct sockaddr_alg addr = {
    .salg_type = "aead",
    .salg_name = "authenc(hmac(sha256),cbc(aes))",
    };
    struct {
    struct rtattr attr;
    __be32 enckeylen;
    char keys[1];
    } __attribute__((packed)) key = {
    .attr.rta_len = sizeof(key),
    .attr.rta_type = 1 /* CRYPTO_AUTHENC_KEYA_PARAM */,
    };

    fd = socket(AF_ALG, SOCK_SEQPACKET, 0);
    bind(fd, (void *)&addr, sizeof(addr));
    setsockopt(fd, SOL_ALG, ALG_SET_KEY, &key, sizeof(key));
    }

    It caused:

    BUG: unable to handle kernel paging request at ffff88007ffdc000
    PGD 2e01067 P4D 2e01067 PUD 2e04067 PMD 2e05067 PTE 0
    Oops: 0000 [#1] SMP
    CPU: 0 PID: 883 Comm: authenc Not tainted 4.20.0-rc1-00108-g00c9fe37a7f27 #13
    Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.12.0-20181126_142135-anatol 04/01/2014
    RIP: 0010:sha256_ni_transform+0xb3/0x330 arch/x86/crypto/sha256_ni_asm.S:155
    [...]
    Call Trace:
    sha256_ni_finup+0x10/0x20 arch/x86/crypto/sha256_ssse3_glue.c:321
    crypto_shash_finup+0x1a/0x30 crypto/shash.c:178
    shash_digest_unaligned+0x45/0x60 crypto/shash.c:186
    crypto_shash_digest+0x24/0x40 crypto/shash.c:202
    hmac_setkey+0x135/0x1e0 crypto/hmac.c:66
    crypto_shash_setkey+0x2b/0xb0 crypto/shash.c:66
    shash_async_setkey+0x10/0x20 crypto/shash.c:223
    crypto_ahash_setkey+0x2d/0xa0 crypto/ahash.c:202
    crypto_authenc_setkey+0x68/0x100 crypto/authenc.c:96
    crypto_aead_setkey+0x2a/0xc0 crypto/aead.c:62
    aead_setkey+0xc/0x10 crypto/algif_aead.c:526
    alg_setkey crypto/af_alg.c:223 [inline]
    alg_setsockopt+0xfe/0x130 crypto/af_alg.c:256
    __sys_setsockopt+0x6d/0xd0 net/socket.c:1902
    __do_sys_setsockopt net/socket.c:1913 [inline]
    __se_sys_setsockopt net/socket.c:1910 [inline]
    __x64_sys_setsockopt+0x1f/0x30 net/socket.c:1910
    do_syscall_64+0x4a/0x180 arch/x86/entry/common.c:290
    entry_SYSCALL_64_after_hwframe+0x49/0xbe

    Fixes: e236d4a89a2f ("[CRYPTO] authenc: Move enckeylen into key itself")
    Cc: # v2.6.25+
    Signed-off-by: Eric Biggers
    Signed-off-by: Herbert Xu

    Eric Biggers
     

30 Dec, 2018

1 commit

  • Pull Kconfig updates from Masahiro Yamada:

    - support -y option for merge_config.sh to avoid downgrading =y to =m

    - remove S_OTHER symbol type, and touch include/config/*.h files correctly

    - fix file name and line number in lexer warnings

    - fix memory leak when EOF is encountered in quotation

    - resolve all shift/reduce conflicts of the parser

    - warn no new line at end of file

    - make 'source' statement more strict to take only string literal

    - rewrite the lexer and remove the keyword lookup table

    - convert to SPDX License Identifier

    - compile C files independently instead of including them from zconf.y

    - fix various warnings of gconfig

    - misc cleanups

    * tag 'kconfig-v4.21' of git://git.kernel.org/pub/scm/linux/kernel/git/masahiroy/linux-kbuild: (39 commits)
    kconfig: surround dbg_sym_flags with #ifdef DEBUG to fix gconf warning
    kconfig: split images.c out of qconf.cc/gconf.c to fix gconf warnings
    kconfig: add static qualifiers to fix gconf warnings
    kconfig: split the lexer out of zconf.y
    kconfig: split some C files out of zconf.y
    kconfig: convert to SPDX License Identifier
    kconfig: remove keyword lookup table entirely
    kconfig: update current_pos in the second lexer
    kconfig: switch to ASSIGN_VAL state in the second lexer
    kconfig: stop associating kconf_id with yylval
    kconfig: refactor end token rules
    kconfig: stop supporting '.' and '/' in unquoted words
    treewide: surround Kconfig file paths with double quotes
    microblaze: surround string default in Kconfig with double quotes
    kconfig: use T_WORD instead of T_VARIABLE for variables
    kconfig: use specific tokens instead of T_ASSIGN for assignments
    kconfig: refactor scanning and parsing "option" properties
    kconfig: use distinct tokens for type and default properties
    kconfig: remove redundant token defines
    kconfig: rename depends_list to comment_option_list
    ...

    Linus Torvalds
     

28 Dec, 2018

1 commit

  • Pull crypto updates from Herbert Xu:
    "API:
    - Add 1472-byte test to tcrypt for IPsec
    - Reintroduced crypto stats interface with numerous changes
    - Support incremental algorithm dumps

    Algorithms:
    - Add xchacha12/20
    - Add nhpoly1305
    - Add adiantum
    - Add streebog hash
    - Mark cts(cbc(aes)) as FIPS allowed

    Drivers:
    - Improve performance of arm64/chacha20
    - Improve performance of x86/chacha20
    - Add NEON-accelerated nhpoly1305
    - Add SSE2 accelerated nhpoly1305
    - Add AVX2 accelerated nhpoly1305
    - Add support for 192/256-bit keys in gcmaes AVX
    - Add SG support in gcmaes AVX
    - ESN for inline IPsec tx in chcr
    - Add support for CryptoCell 703 in ccree
    - Add support for CryptoCell 713 in ccree
    - Add SM4 support in ccree
    - Add SM3 support in ccree
    - Add support for chacha20 in caam/qi2
    - Add support for chacha20 + poly1305 in caam/jr
    - Add support for chacha20 + poly1305 in caam/qi2
    - Add AEAD cipher support in cavium/nitrox"

    * 'linus' of git://git.kernel.org/pub/scm/linux/kernel/git/herbert/crypto-2.6: (130 commits)
    crypto: skcipher - remove remnants of internal IV generators
    crypto: cavium/nitrox - Fix build with !CONFIG_DEBUG_FS
    crypto: salsa20-generic - don't unnecessarily use atomic walk
    crypto: skcipher - add might_sleep() to skcipher_walk_virt()
    crypto: x86/chacha - avoid sleeping under kernel_fpu_begin()
    crypto: cavium/nitrox - Added AEAD cipher support
    crypto: mxc-scc - fix build warnings on ARM64
    crypto: api - document missing stats member
    crypto: user - remove unused dump functions
    crypto: chelsio - Fix wrong error counter increments
    crypto: chelsio - Reset counters on cxgb4 Detach
    crypto: chelsio - Handle PCI shutdown event
    crypto: chelsio - cleanup:send addr as value in function argument
    crypto: chelsio - Use same value for both channel in single WR
    crypto: chelsio - Swap location of AAD and IV sent in WR
    crypto: chelsio - remove set but not used variable 'kctx_len'
    crypto: ux500 - Use proper enum in hash_set_dma_transfer
    crypto: ux500 - Use proper enum in cryp_set_dma_transfer
    crypto: aesni - Add scatter/gather avx stubs, and use them in C
    crypto: aesni - Introduce partial block macro
    ..

    Linus Torvalds
     

27 Dec, 2018

1 commit

  • Pull RCU updates from Ingo Molnar:
    "The biggest RCU changes in this cycle were:

    - Convert RCU's BUG_ON() and similar calls to WARN_ON() and similar.

    - Replace calls of RCU-bh and RCU-sched update-side functions to
    their vanilla RCU counterparts. This series is a step towards
    complete removal of the RCU-bh and RCU-sched update-side functions.

    ( Note that some of these conversions are going upstream via their
    respective maintainers. )

    - Documentation updates, including a number of flavor-consolidation
    updates from Joel Fernandes.

    - Miscellaneous fixes.

    - Automate generation of the initrd filesystem used for rcutorture
    testing.

    - Convert spin_is_locked() assertions to instead use lockdep.

    ( Note that some of these conversions are going upstream via their
    respective maintainers. )

    - SRCU updates, especially including a fix from Dennis Krein for a
    bag-on-head-class bug.

    - RCU torture-test updates"

    * 'core-rcu-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip: (112 commits)
    rcutorture: Don't do busted forward-progress testing
    rcutorture: Use 100ms buckets for forward-progress callback histograms
    rcutorture: Recover from OOM during forward-progress tests
    rcutorture: Print forward-progress test age upon failure
    rcutorture: Print time since GP end upon forward-progress failure
    rcutorture: Print histogram of CB invocation at OOM time
    rcutorture: Print GP age upon forward-progress failure
    rcu: Print per-CPU callback counts for forward-progress failures
    rcu: Account for nocb-CPU callback counts in RCU CPU stall warnings
    rcutorture: Dump grace-period diagnostics upon forward-progress OOM
    rcutorture: Prepare for asynchronous access to rcu_fwd_startat
    torture: Remove unnecessary "ret" variables
    rcutorture: Affinity forward-progress test to avoid housekeeping CPUs
    rcutorture: Break up too-long rcu_torture_fwd_prog() function
    rcutorture: Remove cbflood facility
    torture: Bring any extra CPUs online during kernel startup
    rcutorture: Add call_rcu() flooding forward-progress tests
    rcutorture/formal: Replace synchronize_sched() with synchronize_rcu()
    tools/kernel.h: Replace synchronize_sched() with synchronize_rcu()
    net/decnet: Replace rcu_barrier_bh() with rcu_barrier()
    ...

    Linus Torvalds
     

23 Dec, 2018

4 commits

  • Remove dead code related to internal IV generators, which are no longer
    used since they've been replaced with the "seqiv" and "echainiv"
    templates. The removed code includes:

    - The "givcipher" (GIVCIPHER) algorithm type. No algorithms are
    registered with this type anymore, so it's unneeded.

    - The "const char *geniv" member of aead_alg, ablkcipher_alg, and
    blkcipher_alg. A few algorithms still set this, but it isn't used
    anymore except to show via /proc/crypto and CRYPTO_MSG_GETALG.
    Just hardcode "" or "" in those cases.

    - The 'skcipher_givcrypt_request' structure, which is never used.

    Signed-off-by: Eric Biggers
    Signed-off-by: Herbert Xu

    Eric Biggers
     
  • salsa20-generic doesn't use SIMD instructions or otherwise disable
    preemption, so passing atomic=true to skcipher_walk_virt() is
    unnecessary.

    Signed-off-by: Eric Biggers
    Signed-off-by: Herbert Xu

    Eric Biggers
     
  • skcipher_walk_virt() can still sleep even with atomic=true, since that
    only affects the later calls to skcipher_walk_done(). But,
    skcipher_walk_virt() only has to allocate memory for some input data
    layouts, so incorrectly calling it with preemption disabled can go
    undetected. Use might_sleep() so that it's detected reliably.

    Signed-off-by: Eric Biggers
    Signed-off-by: Herbert Xu

    Eric Biggers
     
  • This patch removes unused dump functions for crypto_user_stats.
    There are remains of the copy/paste of crypto_user_base to
    crypto_user_stat and I forgot to remove them.

    Signed-off-by: Corentin Labbe
    Signed-off-by: Herbert Xu

    Corentin Labbe
     

21 Dec, 2018

1 commit

  • The Kconfig lexer supports special characters such as '.' and '/' in
    the parameter context. In my understanding, the reason is just to
    support bare file paths in the source statement.

    I do not see a good reason to complicate Kconfig for the room of
    ambiguity.

    The majority of code already surrounds file paths with double quotes,
    and it makes sense since file paths are constant string literals.

    Make it treewide consistent now.

    Signed-off-by: Masahiro Yamada
    Acked-by: Wolfram Sang
    Acked-by: Geert Uytterhoeven
    Acked-by: Ingo Molnar

    Masahiro Yamada
     

13 Dec, 2018

11 commits

  • crypto_alg_mod_lookup() takes a reference to the hash algorithm but
    crypto_init_shash_spawn() doesn't take ownership of it, hence the
    reference needs to be dropped in adiantum_create().

    Fixes: 059c2a4d8e16 ("crypto: adiantum - add Adiantum support")
    Signed-off-by: Eric Biggers
    Signed-off-by: Herbert Xu

    Eric Biggers
     
  • CRYPTO_MSG_GETALG in NLM_F_DUMP mode sometimes doesn't return all
    registered crypto algorithms, because it doesn't support incremental
    dumps. crypto_dump_report() only permits itself to be called once, yet
    the netlink subsystem allocates at most ~64 KiB for the skb being dumped
    to. Thus only the first recvmsg() returns data, and it may only include
    a subset of the crypto algorithms even if the user buffer passed to
    recvmsg() is large enough to hold all of them.

    Fix this by using one of the arguments in the netlink_callback structure
    to keep track of the current position in the algorithm list. Then
    userspace can do multiple recvmsg() on the socket after sending the dump
    request. This is the way netlink dumps work elsewhere in the kernel;
    it's unclear why this was different (probably just an oversight).

    Also fix an integer overflow when calculating the dump buffer size hint.

    Fixes: a38f7907b926 ("crypto: Add userspace configuration API")
    Signed-off-by: Eric Biggers
    Signed-off-by: Herbert Xu

    Eric Biggers
     
  • The 2018-11-28 revision of the Adiantum paper has revised some notation:

    - 'M' was replaced with 'L' (meaning "Left", for the left-hand part of
    the message) in the definition of Adiantum hashing, to avoid confusion
    with the full message
    - ε-almost-∆-universal is now abbreviated as ε-∆U instead of εA∆U
    - "block" is now used only to mean block cipher and Poly1305 blocks

    Also, Adiantum hashing was moved from the appendix to the main paper.

    To avoid confusion, update relevant comments in the code to match.

    Signed-off-by: Eric Biggers
    Signed-off-by: Herbert Xu

    Eric Biggers
     
  • The kernel's ChaCha20 uses the RFC7539 convention of the nonce being 12
    bytes rather than 8, so actually I only appended 12 random bytes (not
    16) to its test vectors to form 24-byte nonces for the XChaCha20 test
    vectors. The other 4 bytes were just from zero-padding the stream
    position to 8 bytes. Fix the comments above the test vectors.

    Signed-off-by: Eric Biggers
    Signed-off-by: Herbert Xu

    Eric Biggers
     
  • There is a draft specification for XChaCha20 being worked on. Add the
    XChaCha20 test vector from the appendix so that we can be extra sure the
    kernel's implementation is compatible.

    I also recomputed the ciphertext with XChaCha12 and added it there too,
    to keep the tests for XChaCha20 and XChaCha12 in sync.

    Signed-off-by: Eric Biggers
    Signed-off-by: Herbert Xu

    Eric Biggers
     
  • Now that the x86_64 SIMD implementations of ChaCha20 and XChaCha20 have
    been refactored to support varying the number of rounds, add support for
    XChaCha12. This is identical to XChaCha20 except for the number of
    rounds, which is 12 instead of 20. This can be used by Adiantum.

    Reviewed-by: Martin Willi
    Signed-off-by: Eric Biggers
    Signed-off-by: Herbert Xu

    Eric Biggers
     
  • Add an XChaCha20 implementation that is hooked up to the x86_64 SIMD
    implementations of ChaCha20. This can be used by Adiantum.

    An SSSE3 implementation of single-block HChaCha20 is also added so that
    XChaCha20 can use it rather than the generic implementation. This
    required refactoring the ChaCha permutation into its own function.

    Signed-off-by: Eric Biggers
    Signed-off-by: Herbert Xu

    Eric Biggers
     
  • Add a 64-bit AVX2 implementation of NHPoly1305, an ε-almost-∆-universal
    hash function used in the Adiantum encryption mode. For now, only the
    NH portion is actually AVX2-accelerated; the Poly1305 part is less
    performance-critical so is just implemented in C.

    Signed-off-by: Eric Biggers
    Signed-off-by: Herbert Xu

    Eric Biggers
     
  • Add a 64-bit SSE2 implementation of NHPoly1305, an ε-almost-∆-universal
    hash function used in the Adiantum encryption mode. For now, only the
    NH portion is actually SSE2-accelerated; the Poly1305 part is less
    performance-critical so is just implemented in C.

    Signed-off-by: Eric Biggers
    Signed-off-by: Herbert Xu

    Eric Biggers
     
  • If the stream cipher implementation is asynchronous, then the Adiantum
    instance must be flagged as asynchronous as well. Otherwise someone
    asking for a synchronous algorithm can get an asynchronous algorithm.

    There are no asynchronous xchacha12 or xchacha20 implementations yet
    which makes this largely a theoretical issue, but it should be fixed.

    Fixes: 059c2a4d8e16 ("crypto: adiantum - add Adiantum support")
    Signed-off-by: Eric Biggers
    Signed-off-by: Herbert Xu

    Eric Biggers
     
  • In order to have better coverage of algorithms operating on block
    sizes that are in the ballpark of a VPN packet, add 1472 to the
    block_sizes array.

    Signed-off-by: Ard Biesheuvel
    Signed-off-by: Herbert Xu

    Ard Biesheuvel
     

07 Dec, 2018

12 commits


04 Dec, 2018

1 commit

  • …k/linux-rcu into core/rcu

    Pull RCU changes from Paul E. McKenney:

    - Convert RCU's BUG_ON() and similar calls to WARN_ON() and similar.

    - Replace calls of RCU-bh and RCU-sched update-side functions
    to their vanilla RCU counterparts. This series is a step
    towards complete removal of the RCU-bh and RCU-sched update-side
    functions.

    ( Note that some of these conversions are going upstream via their
    respective maintainers. )

    - Documentation updates, including a number of flavor-consolidation
    updates from Joel Fernandes.

    - Miscellaneous fixes.

    - Automate generation of the initrd filesystem used for
    rcutorture testing.

    - Convert spin_is_locked() assertions to instead use lockdep.

    ( Note that some of these conversions are going upstream via their
    respective maintainers. )

    - SRCU updates, especially including a fix from Dennis Krein
    for a bag-on-head-class bug.

    - RCU torture-test updates.

    Signed-off-by: Ingo Molnar <mingo@kernel.org>

    Ingo Molnar
     

29 Nov, 2018

1 commit

  • In multiple functions, the algorithm fields are read after its reference
    is dropped through crypto_mod_put. In this case, the algorithm memory
    may be freed, resulting in use-after-free bugs. This patch delays the
    put operation until the algorithm is never used.

    Fixes: 79c65d179a40 ("crypto: cbc - Convert to skcipher")
    Fixes: a7d85e06ed80 ("crypto: cfb - add support for Cipher FeedBack mode")
    Fixes: 043a44001b9e ("crypto: pcbc - Convert to skcipher")
    Cc:
    Signed-off-by: Pan Bian
    Signed-off-by: Herbert Xu

    Pan Bian
     

28 Nov, 2018

1 commit


20 Nov, 2018

2 commits

  • Add support for the Adiantum encryption mode. Adiantum was designed by
    Paul Crowley and is specified by our paper:

    Adiantum: length-preserving encryption for entry-level processors
    (https://eprint.iacr.org/2018/720.pdf)

    See our paper for full details; this patch only provides an overview.

    Adiantum is a tweakable, length-preserving encryption mode designed for
    fast and secure disk encryption, especially on CPUs without dedicated
    crypto instructions. Adiantum encrypts each sector using the XChaCha12
    stream cipher, two passes of an ε-almost-∆-universal (εA∆U) hash
    function, and an invocation of the AES-256 block cipher on a single
    16-byte block. On CPUs without AES instructions, Adiantum is much
    faster than AES-XTS; for example, on ARM Cortex-A7, on 4096-byte sectors
    Adiantum encryption is about 4 times faster than AES-256-XTS encryption,
    and decryption about 5 times faster.

    Adiantum is a specialization of the more general HBSH construction. Our
    earlier proposal, HPolyC, was also a HBSH specialization, but it used a
    different εA∆U hash function, one based on Poly1305 only. Adiantum's
    εA∆U hash function, which is based primarily on the "NH" hash function
    like that used in UMAC (RFC4418), is about twice as fast as HPolyC's;
    consequently, Adiantum is about 20% faster than HPolyC.

    This speed comes with no loss of security: Adiantum is provably just as
    secure as HPolyC, in fact slightly *more* secure. Like HPolyC,
    Adiantum's security is reducible to that of XChaCha12 and AES-256,
    subject to a security bound. XChaCha12 itself has a security reduction
    to ChaCha12. Therefore, one need not "trust" Adiantum; one need only
    trust ChaCha12 and AES-256. Note that the εA∆U hash function is only
    used for its proven combinatorical properties so cannot be "broken".

    Adiantum is also a true wide-block encryption mode, so flipping any
    plaintext bit in the sector scrambles the entire ciphertext, and vice
    versa. No other such mode is available in the kernel currently; doing
    the same with XTS scrambles only 16 bytes. Adiantum also supports
    arbitrary-length tweaks and naturally supports any length input >= 16
    bytes without needing "ciphertext stealing".

    For the stream cipher, Adiantum uses XChaCha12 rather than XChaCha20 in
    order to make encryption feasible on the widest range of devices.
    Although the 20-round variant is quite popular, the best known attacks
    on ChaCha are on only 7 rounds, so ChaCha12 still has a substantial
    security margin; in fact, larger than AES-256's. 12-round Salsa20 is
    also the eSTREAM recommendation. For the block cipher, Adiantum uses
    AES-256, despite it having a lower security margin than XChaCha12 and
    needing table lookups, due to AES's extensive adoption and analysis
    making it the obvious first choice. Nevertheless, for flexibility this
    patch also permits the "adiantum" template to be instantiated with
    XChaCha20 and/or with an alternate block cipher.

    We need Adiantum support in the kernel for use in dm-crypt and fscrypt,
    where currently the only other suitable options are block cipher modes
    such as AES-XTS. A big problem with this is that many low-end mobile
    devices (e.g. Android Go phones sold primarily in developing countries,
    as well as some smartwatches) still have CPUs that lack AES
    instructions, e.g. ARM Cortex-A7. Sadly, AES-XTS encryption is much too
    slow to be viable on these devices. We did find that some "lightweight"
    block ciphers are fast enough, but these suffer from problems such as
    not having much cryptanalysis or being too controversial.

    The ChaCha stream cipher has excellent performance but is insecure to
    use directly for disk encryption, since each sector's IV is reused each
    time it is overwritten. Even restricting the threat model to offline
    attacks only isn't enough, since modern flash storage devices don't
    guarantee that "overwrites" are really overwrites, due to wear-leveling.
    Adiantum avoids this problem by constructing a
    "tweakable super-pseudorandom permutation"; this is the strongest
    possible security model for length-preserving encryption.

    Of course, storing random nonces along with the ciphertext would be the
    ideal solution. But doing that with existing hardware and filesystems
    runs into major practical problems; in most cases it would require data
    journaling (like dm-integrity) which severely degrades performance.
    Thus, for now length-preserving encryption is still needed.

    Signed-off-by: Eric Biggers
    Reviewed-by: Ard Biesheuvel
    Signed-off-by: Herbert Xu

    Eric Biggers
     
  • Add a generic implementation of NHPoly1305, an ε-almost-∆-universal hash
    function used in the Adiantum encryption mode.

    CONFIG_NHPOLY1305 is not selectable by itself since there won't be any
    real reason to enable it without also enabling Adiantum support.

    Signed-off-by: Eric Biggers
    Acked-by: Ard Biesheuvel
    Signed-off-by: Herbert Xu

    Eric Biggers