Forum | Documentation | Website | Blog

Skip to content
Snippets Groups Projects
  1. Nov 22, 2019
  2. Nov 16, 2019
    • Ard Biesheuvel's avatar
      crypto: chacha - unexport chacha_generic routines · 22cf7053
      Ard Biesheuvel authored
      
      Now that all users of generic ChaCha code have moved to the core library,
      there is no longer a need for the generic ChaCha skcpiher driver to
      export parts of it implementation for reuse by other drivers. So drop
      the exports, and make the symbols static.
      
      Signed-off-by: default avatarArd Biesheuvel <ardb@kernel.org>
      Signed-off-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>
      22cf7053
    • Ard Biesheuvel's avatar
      crypto: chacha - move existing library code into lib/crypto · 5fb8ef25
      Ard Biesheuvel authored
      
      Currently, our generic ChaCha implementation consists of a permute
      function in lib/chacha.c that operates on the 64-byte ChaCha state
      directly [and which is always included into the core kernel since it
      is used by the /dev/random driver], and the crypto API plumbing to
      expose it as a skcipher.
      
      In order to support in-kernel users that need the ChaCha streamcipher
      but have no need [or tolerance] for going through the abstractions of
      the crypto API, let's expose the streamcipher bits via a library API
      as well, in a way that permits the implementation to be superseded by
      an architecture specific one if provided.
      
      So move the streamcipher code into a separate module in lib/crypto,
      and expose the init() and crypt() routines to users of the library.
      
      Signed-off-by: default avatarArd Biesheuvel <ardb@kernel.org>
      Signed-off-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>
      5fb8ef25
  3. Jun 13, 2019
  4. May 30, 2019
  5. Apr 18, 2019
    • Eric Biggers's avatar
      crypto: run initcalls for generic implementations earlier · c4741b23
      Eric Biggers authored
      
      Use subsys_initcall for registration of all templates and generic
      algorithm implementations, rather than module_init.  Then change
      cryptomgr to use arch_initcall, to place it before the subsys_initcalls.
      
      This is needed so that when both a generic and optimized implementation
      of an algorithm are built into the kernel (not loadable modules), the
      generic implementation is registered before the optimized one.
      Otherwise, the self-tests for the optimized implementation are unable to
      allocate the generic implementation for the new comparison fuzz tests.
      
      Note that on arm, a side effect of this change is that self-tests for
      generic implementations may run before the unaligned access handler has
      been installed.  So, unaligned accesses will crash the kernel.  This is
      arguably a good thing as it makes it easier to detect that type of bug.
      
      Signed-off-by: default avatarEric Biggers <ebiggers@google.com>
      Signed-off-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>
      c4741b23
  6. Mar 22, 2019
    • Eric Biggers's avatar
      crypto: chacha-generic - use crypto_xor_cpy() · 29d97dec
      Eric Biggers authored
      
      In chacha_docrypt(), use crypto_xor_cpy() instead of crypto_xor().
      This avoids having to memcpy() the src buffer to the dst buffer.
      
      Signed-off-by: default avatarEric Biggers <ebiggers@google.com>
      Signed-off-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>
      29d97dec
    • Eric Biggers's avatar
      crypto: chacha-generic - fix use as arm64 no-NEON fallback · 7aceaaef
      Eric Biggers authored
      The arm64 implementations of ChaCha and XChaCha are failing the extra
      crypto self-tests following my patches to test the !may_use_simd() code
      paths, which previously were untested.  The problem is as follows:
      
      When !may_use_simd(), the arm64 NEON implementations fall back to the
      generic implementation, which uses the skcipher_walk API to iterate
      through the src/dst scatterlists.  Due to how the skcipher_walk API
      works, walk.stride is set from the skcipher_alg actually being used,
      which in this case is the arm64 NEON algorithm.  Thus walk.stride is
      5*CHACHA_BLOCK_SIZE, not CHACHA_BLOCK_SIZE.
      
      This unnecessarily large stride shouldn't cause an actual problem.
      However, the generic implementation computes round_down(nbytes,
      walk.stride).  round_down() assumes the round amount is a power of 2,
      which 5*CHACHA_BLOCK_SIZE is not, so it gives the wrong result.
      
      This causes the following case in skcipher_walk_done() to be hit,
      causing a WARN() and failing the encryption operation:
      
      	if (WARN_ON(err)) {
      		/* unexpected case; didn't process all bytes */
      		err = -EINVAL;
      		goto finish;
      	}
      
      Fix it by rounding down to CHACHA_BLOCK_SIZE instead of walk.stride.
      
      (Or we could replace round_down() with rounddown(), but that would add a
      slow division operation every time, which I think we should avoid.)
      
      Fixes: 2fe55987
      
       ("crypto: arm64/chacha - use combined SIMD/ALU routine for more speed")
      Cc: <stable@vger.kernel.org> # v5.0+
      Signed-off-by: default avatarEric Biggers <ebiggers@google.com>
      Reviewed-by: default avatarArd Biesheuvel <ard.biesheuvel@linaro.org>
      Signed-off-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>
      7aceaaef
  7. Nov 20, 2018
    • Eric Biggers's avatar
      crypto: chacha - add XChaCha12 support · aa762409
      Eric Biggers authored
      Now that the generic implementation of ChaCha20 has been refactored to
      allow varying the number of rounds, add support for XChaCha12, which is
      the XSalsa construction applied to ChaCha12.  ChaCha12 is one of the
      three ciphers specified by the original ChaCha paper
      (https://cr.yp.to/chacha/chacha-20080128.pdf
      
      : "ChaCha, a variant of
      Salsa20"), alongside ChaCha8 and ChaCha20.  ChaCha12 is faster than
      ChaCha20 but has a lower, but still large, security margin.
      
      We need XChaCha12 support so that it can be used in the Adiantum
      encryption mode, which enables disk/file encryption on low-end mobile
      devices where AES-XTS is too slow as the CPUs lack AES instructions.
      
      We'd prefer XChaCha20 (the more popular variant), but it's too slow on
      some of our target devices, so at least in some cases we do need the
      XChaCha12-based version.  In more detail, the problem is that Adiantum
      is still much slower than we're happy with, and encryption still has a
      quite noticeable effect on the feel of low-end devices.  Users and
      vendors push back hard against encryption that degrades the user
      experience, which always risks encryption being disabled entirely.  So
      we need to choose the fastest option that gives us a solid margin of
      security, and here that's XChaCha12.  The best known attack on ChaCha
      breaks only 7 rounds and has 2^235 time complexity, so ChaCha12's
      security margin is still better than AES-256's.  Much has been learned
      about cryptanalysis of ARX ciphers since Salsa20 was originally designed
      in 2005, and it now seems we can be comfortable with a smaller number of
      rounds.  The eSTREAM project also suggests the 12-round version of
      Salsa20 as providing the best balance among the different variants:
      combining very good performance with a "comfortable margin of security".
      
      Note that it would be trivial to add vanilla ChaCha12 in addition to
      XChaCha12.  However, it's unneeded for now and therefore is omitted.
      
      As discussed in the patch that introduced XChaCha20 support, I
      considered splitting the code into separate chacha-common, chacha20,
      xchacha20, and xchacha12 modules, so that these algorithms could be
      enabled/disabled independently.  However, since nearly all the code is
      shared anyway, I ultimately decided there would have been little benefit
      to the added complexity.
      
      Reviewed-by: default avatarArd Biesheuvel <ard.biesheuvel@linaro.org>
      Acked-by: default avatarMartin Willi <martin@strongswan.org>
      Signed-off-by: default avatarEric Biggers <ebiggers@google.com>
      Signed-off-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>
      aa762409
    • Eric Biggers's avatar
      crypto: chacha20-generic - refactor to allow varying number of rounds · 1ca1b917
      Eric Biggers authored
      
      In preparation for adding XChaCha12 support, rename/refactor
      chacha20-generic to support different numbers of rounds.  The
      justification for needing XChaCha12 support is explained in more detail
      in the patch "crypto: chacha - add XChaCha12 support".
      
      The only difference between ChaCha{8,12,20} are the number of rounds
      itself; all other parts of the algorithm are the same.  Therefore,
      remove the "20" from all definitions, structures, functions, files, etc.
      that will be shared by all ChaCha versions.
      
      Also make ->setkey() store the round count in the chacha_ctx (previously
      chacha20_ctx).  The generic code then passes the round count through to
      chacha_block().  There will be a ->setkey() function for each explicitly
      allowed round count; the encrypt/decrypt functions will be the same.  I
      decided not to do it the opposite way (same ->setkey() function for all
      round counts, with different encrypt/decrypt functions) because that
      would have required more boilerplate code in architecture-specific
      implementations of ChaCha and XChaCha.
      
      Reviewed-by: default avatarArd Biesheuvel <ard.biesheuvel@linaro.org>
      Acked-by: default avatarMartin Willi <martin@strongswan.org>
      Signed-off-by: default avatarEric Biggers <ebiggers@google.com>
      Signed-off-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>
      1ca1b917
    • Eric Biggers's avatar
      crypto: chacha20-generic - add XChaCha20 support · de61d7ae
      Eric Biggers authored
      Add support for the XChaCha20 stream cipher.  XChaCha20 is the
      application of the XSalsa20 construction
      (https://cr.yp.to/snuffle/xsalsa-20081128.pdf
      
      ) to ChaCha20 rather than
      to Salsa20.  XChaCha20 extends ChaCha20's nonce length from 64 bits (or
      96 bits, depending on convention) to 192 bits, while provably retaining
      ChaCha20's security.  XChaCha20 uses the ChaCha20 permutation to map the
      key and first 128 nonce bits to a 256-bit subkey.  Then, it does the
      ChaCha20 stream cipher with the subkey and remaining 64 bits of nonce.
      
      We need XChaCha support in order to add support for the Adiantum
      encryption mode.  Note that to meet our performance requirements, we
      actually plan to primarily use the variant XChaCha12.  But we believe
      it's wise to first add XChaCha20 as a baseline with a higher security
      margin, in case there are any situations where it can be used.
      Supporting both variants is straightforward.
      
      Since XChaCha20's subkey differs for each request, XChaCha20 can't be a
      template that wraps ChaCha20; that would require re-keying the
      underlying ChaCha20 for every request, which wouldn't be thread-safe.
      Instead, we make XChaCha20 its own top-level algorithm which calls the
      ChaCha20 streaming implementation internally.
      
      Similar to the existing ChaCha20 implementation, we define the IV to be
      the nonce and stream position concatenated together.  This allows users
      to seek to any position in the stream.
      
      I considered splitting the code into separate chacha20-common, chacha20,
      and xchacha20 modules, so that chacha20 and xchacha20 could be
      enabled/disabled independently.  However, since nearly all the code is
      shared anyway, I ultimately decided there would have been little benefit
      to the added complexity of separate modules.
      
      Reviewed-by: default avatarArd Biesheuvel <ard.biesheuvel@linaro.org>
      Acked-by: default avatarMartin Willi <martin@strongswan.org>
      Signed-off-by: default avatarEric Biggers <ebiggers@google.com>
      Signed-off-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>
      de61d7ae
    • Eric Biggers's avatar
      crypto: chacha20-generic - don't unnecessarily use atomic walk · 5e04542a
      Eric Biggers authored
      
      chacha20-generic doesn't use SIMD instructions or otherwise disable
      preemption, so passing atomic=true to skcipher_walk_virt() is
      unnecessary.
      
      Suggested-by: default avatarArd Biesheuvel <ard.biesheuvel@linaro.org>
      Acked-by: default avatarMartin Willi <martin@strongswan.org>
      Signed-off-by: default avatarEric Biggers <ebiggers@google.com>
      Signed-off-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>
      5e04542a
  8. Sep 21, 2018
    • Eric Biggers's avatar
      crypto: chacha20 - Fix chacha20_block() keystream alignment (again) · a5e9f557
      Eric Biggers authored
      In commit 9f480fae
      
       ("crypto: chacha20 - Fix keystream alignment for
      chacha20_block()"), I had missed that chacha20_block() can be called
      directly on the buffer passed to get_random_bytes(), which can have any
      alignment.  So, while my commit didn't break anything, it didn't fully
      solve the alignment problems.
      
      Revert my solution and just update chacha20_block() to use
      put_unaligned_le32(), so the output buffer need not be aligned.
      This is simpler, and on many CPUs it's the same speed.
      
      But, I kept the 'tmp' buffers in extract_crng_user() and
      _get_random_bytes() 4-byte aligned, since that alignment is actually
      needed for _crng_backtrack_protect() too.
      
      Reported-by: default avatarStephan Müller <smueller@chronox.de>
      Cc: Theodore Ts'o <tytso@mit.edu>
      Signed-off-by: default avatarEric Biggers <ebiggers@google.com>
      Signed-off-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>
      a5e9f557
  9. Nov 29, 2017
  10. Aug 22, 2017
    • Ard Biesheuvel's avatar
      crypto: chacha20 - fix handling of chunked input · 4de43726
      Ard Biesheuvel authored
      Commit 9ae433bc ("crypto: chacha20 - convert generic and x86 versions
      to skcipher") ported the existing chacha20 code to use the new skcipher
      API, and introduced a bug along the way. Unfortunately, the tcrypt tests
      did not catch the error, and it was only found recently by Tobias.
      
      Stefan kindly diagnosed the error, and proposed a fix which is similar
      to the one below, with the exception that 'walk.stride' is used rather
      than the hardcoded block size. This does not actually matter in this
      case, but it's a better example of how to use the skcipher walk API.
      
      Fixes: 9ae433bc
      
       ("crypto: chacha20 - convert generic and x86 ...")
      Cc: <stable@vger.kernel.org> # v4.11+
      Cc: Steffen Klassert <steffen.klassert@secunet.com>
      Reported-by: default avatarTobias Brunner <tobias@strongswan.org>
      Signed-off-by: default avatarArd Biesheuvel <ard.biesheuvel@linaro.org>
      Signed-off-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>
      4de43726
  11. Dec 27, 2016
  12. Jul 03, 2016
  13. Jul 17, 2015
  14. Jun 04, 2015