1. 13 6月, 2019 1 次提交
  2. 06 6月, 2019 2 次提交
    • N
      crypto: xxhash - Implement xxhash support · 67882e76
      Nikolay Borisov 提交于
      xxhash is currently implemented as a self-contained module in /lib.
      This patch enables that module to be used as part of the generic kernel
      crypto framework. It adds a simple wrapper to the 64bit version.
      
      I've also added test vectors (with help from Nick Terrell). The upstream
      xxhash code is tested by running hashing operation on random 222 byte
      data with seed values of 0 and a prime number. The upstream test
      suite can be found at https://github.com/Cyan4973/xxHash/blob/cf46e0c/xxhsum.c#L664
      
      Essentially hashing is run on data of length 0,1,14,222 with the
      aforementioned seed values 0 and prime 2654435761. The particular random
      222 byte string was provided to me by Nick Terrell by reading
      /dev/random and the checksums were calculated by the upstream xxsum
      utility with the following bash script:
      
      dd if=/dev/random of=TEST_VECTOR bs=1 count=222
      
      for a in 0 1; do
      	for l in 0 1 14 222; do
      		for s in 0 2654435761; do
      			echo algo $a length $l seed $s;
      			head -c $l TEST_VECTOR | ~/projects/kernel/xxHash/xxhsum -H$a -s$s
      		done
      	done
      done
      
      This produces output as follows:
      
      algo 0 length 0 seed 0
      02cc5d05  stdin
      algo 0 length 0 seed 2654435761
      02cc5d05  stdin
      algo 0 length 1 seed 0
      25201171  stdin
      algo 0 length 1 seed 2654435761
      25201171  stdin
      algo 0 length 14 seed 0
      c1d95975  stdin
      algo 0 length 14 seed 2654435761
      c1d95975  stdin
      algo 0 length 222 seed 0
      b38662a6  stdin
      algo 0 length 222 seed 2654435761
      b38662a6  stdin
      algo 1 length 0 seed 0
      ef46db3751d8e999  stdin
      algo 1 length 0 seed 2654435761
      ac75fda2929b17ef  stdin
      algo 1 length 1 seed 0
      27c3f04c2881203a  stdin
      algo 1 length 1 seed 2654435761
      4a15ed26415dfe4d  stdin
      algo 1 length 14 seed 0
      3d33dc700231dfad  stdin
      algo 1 length 14 seed 2654435761
      ea5f7ddef9a64f80  stdin
      algo 1 length 222 seed 0
      5f3d3c08ec2bef34  stdin
      algo 1 length 222 seed 2654435761
      6a9df59664c7ed62  stdin
      
      algo 1 is xx64 variant, algo 0 is the 32 bit variant which is currently
      not hooked up.
      Signed-off-by: NNikolay Borisov <nborisov@suse.com>
      Reviewed-by: NEric Biggers <ebiggers@kernel.org>
      Signed-off-by: NHerbert Xu <herbert@gondor.apana.org.au>
      67882e76
    • E
      crypto: testmgr - test the shash API · d8ea98aa
      Eric Biggers 提交于
      For hash algorithms implemented using the "shash" algorithm type, test
      both the ahash and shash APIs, not just the ahash API.
      
      Testing the ahash API already tests the shash API indirectly, which is
      normally good enough.  However, there have been corner cases where there
      have been shash bugs that don't get exposed through the ahash API.  So,
      update testmgr to test the shash API too.
      
      This would have detected the arm64 SHA-1 and SHA-2 bugs for which fixes
      were just sent out (https://patchwork.kernel.org/patch/10964843/ and
      https://patchwork.kernel.org/patch/10965089/):
      
          alg: shash: sha1-ce test failed (wrong result) on test vector 0, cfg="init+finup aligned buffer"
          alg: shash: sha224-ce test failed (wrong result) on test vector 0, cfg="init+finup aligned buffer"
          alg: shash: sha256-ce test failed (wrong result) on test vector 0, cfg="init+finup aligned buffer"
      
      This also would have detected the bugs fixed by commit 307508d1
      ("crypto: crct10dif-generic - fix use via crypto_shash_digest()") and
      commit dec3d0b1
      ("crypto: x86/crct10dif-pcl - fix use via crypto_shash_digest()").
      Signed-off-by: NEric Biggers <ebiggers@google.com>
      Acked-by: NArd Biesheuvel <ard.biesheuvel@linaro.org>
      Signed-off-by: NHerbert Xu <herbert@gondor.apana.org.au>
      d8ea98aa
  3. 25 4月, 2019 2 次提交
    • G
      crypto: testmgr - add missing self test entries for protected keys · f0372c00
      Gilad Ben-Yossef 提交于
      Mark sm4 and missing aes using protected keys which are indetical to
      same algs with no HW protected keys as tested.
      Signed-off-by: NGilad Ben-Yossef <gilad@benyossef.com>
      Signed-off-by: NHerbert Xu <herbert@gondor.apana.org.au>
      f0372c00
    • E
      crypto: shash - remove shash_desc::flags · 877b5691
      Eric Biggers 提交于
      The flags field in 'struct shash_desc' never actually does anything.
      The only ostensibly supported flag is CRYPTO_TFM_REQ_MAY_SLEEP.
      However, no shash algorithm ever sleeps, making this flag a no-op.
      
      With this being the case, inevitably some users who can't sleep wrongly
      pass MAY_SLEEP.  These would all need to be fixed if any shash algorithm
      actually started sleeping.  For example, the shash_ahash_*() functions,
      which wrap a shash algorithm with the ahash API, pass through MAY_SLEEP
      from the ahash API to the shash API.  However, the shash functions are
      called under kmap_atomic(), so actually they're assumed to never sleep.
      
      Even if it turns out that some users do need preemption points while
      hashing large buffers, we could easily provide a helper function
      crypto_shash_update_large() which divides the data into smaller chunks
      and calls crypto_shash_update() and cond_resched() for each chunk.  It's
      not necessary to have a flag in 'struct shash_desc', nor is it necessary
      to make individual shash algorithms aware of this at all.
      
      Therefore, remove shash_desc::flags, and document that the
      crypto_shash_*() functions can be called from any context.
      Signed-off-by: NEric Biggers <ebiggers@google.com>
      Signed-off-by: NHerbert Xu <herbert@gondor.apana.org.au>
      877b5691
  4. 18 4月, 2019 9 次提交
    • E
      crypto: testmgr - fuzz AEADs against their generic implementation · 40153b10
      Eric Biggers 提交于
      When the extra crypto self-tests are enabled, test each AEAD algorithm
      against its generic implementation when one is available.  This
      involves: checking the algorithm properties for consistency, then
      randomly generating test vectors using the generic implementation and
      running them against the implementation under test.  Both good and bad
      inputs are tested.
      Signed-off-by: NEric Biggers <ebiggers@google.com>
      Signed-off-by: NHerbert Xu <herbert@gondor.apana.org.au>
      40153b10
    • E
      crypto: testmgr - fuzz skciphers against their generic implementation · d435e10e
      Eric Biggers 提交于
      When the extra crypto self-tests are enabled, test each skcipher
      algorithm against its generic implementation when one is available.
      This involves: checking the algorithm properties for consistency, then
      randomly generating test vectors using the generic implementation and
      running them against the implementation under test.  Both good and bad
      inputs are tested.
      
      This has already detected a bug in the skcipher_walk API, a bug in the
      LRW template, and an inconsistency in the cts implementations.
      Signed-off-by: NEric Biggers <ebiggers@google.com>
      Signed-off-by: NHerbert Xu <herbert@gondor.apana.org.au>
      d435e10e
    • E
      crypto: testmgr - fuzz hashes against their generic implementation · 9a8a6b3f
      Eric Biggers 提交于
      When the extra crypto self-tests are enabled, test each hash algorithm
      against its generic implementation when one is available.  This
      involves: checking the algorithm properties for consistency, then
      randomly generating test vectors using the generic implementation and
      running them against the implementation under test.  Both good and bad
      inputs are tested.
      
      This has already detected a bug in the x86 implementation of poly1305,
      bugs in crct10dif, and an inconsistency in cbcmac.
      Signed-off-by: NEric Biggers <ebiggers@google.com>
      Signed-off-by: NHerbert Xu <herbert@gondor.apana.org.au>
      9a8a6b3f
    • E
      crypto: testmgr - add helpers for fuzzing against generic implementation · f2bb770a
      Eric Biggers 提交于
      Add some helper functions in preparation for fuzz testing algorithms
      against their generic implementation.
      Signed-off-by: NEric Biggers <ebiggers@google.com>
      Signed-off-by: NHerbert Xu <herbert@gondor.apana.org.au>
      f2bb770a
    • E
      crypto: testmgr - identify test vectors by name rather than number · 951d1332
      Eric Biggers 提交于
      In preparation for fuzz testing algorithms against their generic
      implementation, make error messages in testmgr identify test vectors by
      name rather than index.  Built-in test vectors are simply "named" by
      their index in testmgr.h, as before.  But (in later patches) generated
      test vectors will be given more descriptive names to help developers
      debug problems detected with them.
      Signed-off-by: NEric Biggers <ebiggers@google.com>
      Signed-off-by: NHerbert Xu <herbert@gondor.apana.org.au>
      951d1332
    • E
      crypto: testmgr - expand ability to test for errors · 5283a8ee
      Eric Biggers 提交于
      Update testmgr to support testing for specific errors from setkey() and
      digest() for hashes; setkey() and encrypt()/decrypt() for skciphers and
      ciphers; and setkey(), setauthsize(), and encrypt()/decrypt() for AEADs.
      This is useful because algorithms usually restrict the lengths or format
      of the message, key, and/or authentication tag in some way.  And bad
      inputs should be tested too, not just good inputs.
      
      As part of this change, remove the ambiguously-named 'fail' flag and
      replace it with 'setkey_error = -EINVAL' for the only test vector that
      used it -- the DES weak key test vector.  Note that this tightens the
      test to require -EINVAL rather than any error code, but AFAICS this
      won't cause any test failure.
      
      Other than that, these new fields aren't set on any test vectors yet.
      Later patches will do so.
      Signed-off-by: NEric Biggers <ebiggers@google.com>
      Signed-off-by: NHerbert Xu <herbert@gondor.apana.org.au>
      5283a8ee
    • V
      crypto: ecrdsa - add EC-RDSA test vectors to testmgr · 32fbdbd3
      Vitaly Chikunov 提交于
      Add testmgr test vectors for EC-RDSA algorithm for every of five
      supported parameters (curves). Because there are no officially published
      test vectors for the curves, the vectors are generated by gost-engine.
      Signed-off-by: NVitaly Chikunov <vt@altlinux.org>
      Signed-off-by: NHerbert Xu <herbert@gondor.apana.org.au>
      32fbdbd3
    • V
      X.509: parse public key parameters from x509 for akcipher · f1774cb8
      Vitaly Chikunov 提交于
      Some public key algorithms (like EC-DSA) keep in parameters field
      important data such as digest and curve OIDs (possibly more for
      different EC-DSA variants). Thus, just setting a public key (as
      for RSA) is not enough.
      
      Append parameters into the key stream for akcipher_set_{pub,priv}_key.
      Appended data is: (u32) algo OID, (u32) parameters length, parameters
      data.
      
      This does not affect current akcipher API nor RSA ciphers (they could
      ignore it). Idea of appending parameters to the key stream is by Herbert
      Xu.
      
      Cc: David Howells <dhowells@redhat.com>
      Cc: Denis Kenzior <denkenz@gmail.com>
      Cc: keyrings@vger.kernel.org
      Signed-off-by: NVitaly Chikunov <vt@altlinux.org>
      Reviewed-by: NDenis Kenzior <denkenz@gmail.com>
      Signed-off-by: NHerbert Xu <herbert@gondor.apana.org.au>
      f1774cb8
    • V
      crypto: akcipher - new verify API for public key algorithms · c7381b01
      Vitaly Chikunov 提交于
      Previous akcipher .verify() just `decrypts' (using RSA encrypt which is
      using public key) signature to uncover message hash, which was then
      compared in upper level public_key_verify_signature() with the expected
      hash value, which itself was never passed into verify().
      
      This approach was incompatible with EC-DSA family of algorithms,
      because, to verify a signature EC-DSA algorithm also needs a hash value
      as input; then it's used (together with a signature divided into halves
      `r||s') to produce a witness value, which is then compared with `r' to
      determine if the signature is correct. Thus, for EC-DSA, nor
      requirements of .verify() itself, nor its output expectations in
      public_key_verify_signature() wasn't sufficient.
      
      Make improved .verify() call which gets hash value as input and produce
      complete signature check without any output besides status.
      
      Now for the top level verification only crypto_akcipher_verify() needs
      to be called and its return value inspected.
      
      Make sure that `digest' is in kmalloc'd memory (in place of `output`) in
      {public,tpm}_key_verify_signature() as insisted by Herbert Xu, and will
      be changed in the following commit.
      
      Cc: David Howells <dhowells@redhat.com>
      Cc: keyrings@vger.kernel.org
      Signed-off-by: NVitaly Chikunov <vt@altlinux.org>
      Reviewed-by: NDenis Kenzior <denkenz@gmail.com>
      Signed-off-by: NHerbert Xu <herbert@gondor.apana.org.au>
      c7381b01
  5. 08 4月, 2019 1 次提交
    • E
      crypto: testmgr - add panic_on_fail module parameter · eda69b0c
      Eric Biggers 提交于
      Add a module parameter cryptomgr.panic_on_fail which causes the kernel
      to panic if any crypto self-tests fail.
      
      Use cases:
      
      - More easily detect crypto self-test failures by boot testing,
        e.g. on KernelCI.
      - Get a bug report if syzkaller manages to use the template system to
        instantiate an algorithm that fails its self-tests.
      
      The command-line option "fips=1" already does this, but it also makes
      other changes not wanted for general testing, such as disabling
      "unapproved" algorithms.  panic_on_fail just does what it says.
      Signed-off-by: NEric Biggers <ebiggers@google.com>
      Signed-off-by: NHerbert Xu <herbert@gondor.apana.org.au>
      eda69b0c
  6. 22 3月, 2019 3 次提交
  7. 22 2月, 2019 1 次提交
  8. 08 2月, 2019 9 次提交
    • E
      crypto: testmgr - check for aead_request corruption · a6e5ef9b
      Eric Biggers 提交于
      Check that algorithms do not change the aead_request structure, as users
      may rely on submitting the request again (e.g. after copying new data
      into the same source buffer) without reinitializing everything.
      Signed-off-by: NEric Biggers <ebiggers@google.com>
      Signed-off-by: NHerbert Xu <herbert@gondor.apana.org.au>
      a6e5ef9b
    • E
      crypto: testmgr - check for skcipher_request corruption · fa353c99
      Eric Biggers 提交于
      Check that algorithms do not change the skcipher_request structure, as
      users may rely on submitting the request again (e.g. after copying new
      data into the same source buffer) without reinitializing everything.
      Signed-off-by: NEric Biggers <ebiggers@google.com>
      Signed-off-by: NHerbert Xu <herbert@gondor.apana.org.au>
      fa353c99
    • E
      crypto: testmgr - convert hash testing to use testvec_configs · 4cc2dcf9
      Eric Biggers 提交于
      Convert alg_test_hash() to use the new test framework, adding a list of
      testvec_configs to test by default.  When the extra self-tests are
      enabled, randomly generated testvec_configs are tested as well.
      
      This improves hash test coverage mainly because now all algorithms have
      a variety of data layouts tested, whereas before each algorithm was
      responsible for declaring its own chunked test cases which were often
      missing or provided poor test coverage.  The new code also tests both
      the MAY_SLEEP and !MAY_SLEEP cases and buffers that cross pages.
      
      This already found bugs in the hash walk code and in the arm32 and arm64
      implementations of crct10dif.
      
      I removed the hash chunked test vectors that were the same as
      non-chunked ones, but left the ones that were unique.
      Signed-off-by: NEric Biggers <ebiggers@google.com>
      Signed-off-by: NHerbert Xu <herbert@gondor.apana.org.au>
      4cc2dcf9
    • E
      crypto: testmgr - convert aead testing to use testvec_configs · ed96804f
      Eric Biggers 提交于
      Convert alg_test_aead() to use the new test framework, using the same
      list of testvec_configs that skcipher testing uses.
      
      This significantly improves AEAD test coverage mainly because previously
      there was only very limited test coverage of the possible data layouts.
      Now the data layouts to test are listed in one place for all algorithms
      and optionally are also randomly generated.  In fact, only one AEAD
      algorithm (AES-GCM) even had a chunked test case before.
      
      This already found bugs in all the AEGIS and MORUS implementations, the
      x86 AES-GCM implementation, and the arm64 AES-CCM implementation.
      
      I removed the AEAD chunked test vectors that were the same as
      non-chunked ones, but left the ones that were unique.
      
      Note: the rewritten test code allocates an aead_request just once per
      algorithm rather than once per encryption/decryption, but some AEAD
      algorithms incorrectly change the tfm pointer in the request.  It's
      nontrivial to fix these, so to move forward I'm temporarily working
      around it by resetting the tfm pointer.  But they'll need to be fixed.
      Signed-off-by: NEric Biggers <ebiggers@google.com>
      Signed-off-by: NHerbert Xu <herbert@gondor.apana.org.au>
      ed96804f
    • E
      crypto: testmgr - convert skcipher testing to use testvec_configs · 4e7babba
      Eric Biggers 提交于
      Convert alg_test_skcipher() to use the new test framework, adding a list
      of testvec_configs to test by default.  When the extra self-tests are
      enabled, randomly generated testvec_configs are tested as well.
      
      This improves skcipher test coverage mainly because now all algorithms
      have a variety of data layouts tested, whereas before each algorithm was
      responsible for declaring its own chunked test cases which were often
      missing or provided poor test coverage.  The new code also tests both
      the MAY_SLEEP and !MAY_SLEEP cases, different IV alignments, and buffers
      that cross pages.
      
      This has already found a bug in the arm64 ctr-aes-neonbs algorithm.
      It would have easily found many past bugs.
      
      I removed the skcipher chunked test vectors that were the same as
      non-chunked ones, but left the ones that were unique.
      Signed-off-by: NEric Biggers <ebiggers@google.com>
      Signed-off-by: NHerbert Xu <herbert@gondor.apana.org.au>
      4e7babba
    • E
      crypto: testmgr - implement random testvec_config generation · 25f9dddb
      Eric Biggers 提交于
      Add functions that generate a random testvec_config, in preparation for
      using it for randomized fuzz tests.
      Signed-off-by: NEric Biggers <ebiggers@google.com>
      Signed-off-by: NHerbert Xu <herbert@gondor.apana.org.au>
      25f9dddb
    • E
      crypto: testmgr - introduce CONFIG_CRYPTO_MANAGER_EXTRA_TESTS · 5b2706a4
      Eric Biggers 提交于
      To achieve more comprehensive crypto test coverage, I'd like to add fuzz
      tests that use random data layouts and request flags.
      
      To be most effective these tests should be part of testmgr, so they
      automatically run on every algorithm registered with the crypto API.
      However, they will take much longer to run than the current tests and
      therefore will only really be intended to be run by developers, whereas
      the current tests have a wider audience.
      
      Therefore, add a new kconfig option CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
      that can be set by developers to enable these extra, expensive tests.
      
      Similar to the regular tests, also add a module parameter
      cryptomgr.noextratests to support disabling the tests.
      
      Finally, another module parameter cryptomgr.fuzz_iterations is added to
      control how many iterations the fuzz tests do.  Note: for now setting
      this to 0 will be equivalent to cryptomgr.noextratests=1.  But I opted
      for separate parameters to provide more flexibility to add other types
      of tests under the "extra tests" category in the future.
      Signed-off-by: NEric Biggers <ebiggers@google.com>
      Signed-off-by: NHerbert Xu <herbert@gondor.apana.org.au>
      5b2706a4
    • E
      crypto: testmgr - add testvec_config struct and helper functions · 3f47a03d
      Eric Biggers 提交于
      Crypto algorithms must produce the same output for the same input
      regardless of data layout, i.e. how the src and dst scatterlists are
      divided into chunks and how each chunk is aligned.  Request flags such
      as CRYPTO_TFM_REQ_MAY_SLEEP must not affect the result either.
      
      However, testing of this currently has many gaps.  For example,
      individual algorithms are responsible for providing their own chunked
      test vectors.  But many don't bother to do this or test only one or two
      cases, providing poor test coverage.  Also, other things such as
      misaligned IVs and CRYPTO_TFM_REQ_MAY_SLEEP are never tested at all.
      
      Test code is also duplicated between the chunked and non-chunked cases,
      making it difficult to make other improvements.
      
      To improve the situation, this patch series basically moves the chunk
      descriptions into the testmgr itself so that they are shared by all
      algorithms.  However, it's done in an extensible way via a new struct
      'testvec_config', which describes not just the scaled chunk lengths but
      also all other aspects of the crypto operation besides the data itself
      such as the buffer alignments, the request flags, whether the operation
      is in-place or not, the IV alignment, and for hash algorithms when to
      do each update() and when to use finup() vs. final() vs. digest().
      
      Then, this patch series makes skcipher, aead, and hash algorithms be
      tested against a list of default testvec_configs, replacing the current
      test code.  This improves overall test coverage, without reducing test
      performance too much.  Note that the test vectors themselves are not
      changed, except for removing the chunk lists.
      
      This series also adds randomized fuzz tests, enabled by a new kconfig
      option intended for developer use only, where skcipher, aead, and hash
      algorithms are tested against many randomly generated testvec_configs.
      This provides much more comprehensive test coverage.
      
      These improved tests have already exposed many bugs.
      
      To start it off, this initial patch adds the testvec_config and various
      helper functions that will be used by the skcipher, aead, and hash test
      code that will be converted to use the new testvec_config framework.
      Signed-off-by: NEric Biggers <ebiggers@google.com>
      Signed-off-by: NHerbert Xu <herbert@gondor.apana.org.au>
      3f47a03d
    • C
      crypto: testmgr - use kmemdup · e3d90e52
      Christopher Diaz Riveros 提交于
      Fixes coccinnelle alerts:
      
      /crypto/testmgr.c:2112:13-20: WARNING opportunity for kmemdup
      /crypto/testmgr.c:2130:13-20: WARNING opportunity for kmemdup
      /crypto/testmgr.c:2152:9-16: WARNING opportunity for kmemdup
      Signed-off-by: NChristopher Diaz Riveros <chrisadr@gentoo.org>
      Signed-off-by: NHerbert Xu <herbert@gondor.apana.org.au>
      e3d90e52
  9. 01 2月, 2019 2 次提交
    • M
      crypto: testmgr - mark crc32 checksum as FIPS allowed · a8a34416
      Milan Broz 提交于
      The CRC32 is not a cryptographic hash algorithm,
      so the FIPS restrictions should not apply to it.
      (The CRC32C variant is already allowed.)
      
      This CRC32 variant is used for in dm-crypt legacy TrueCrypt
      IV implementation (tcw); detected by cryptsetup test suite
      failure in FIPS mode.
      Signed-off-by: NMilan Broz <gmazyland@gmail.com>
      Reviewed-by: NStephan Mueller <smueller@chronox.de>
      Signed-off-by: NHerbert Xu <herbert@gondor.apana.org.au>
      a8a34416
    • E
      crypto: testmgr - skip crc32c context test for ahash algorithms · eb5e6730
      Eric Biggers 提交于
      Instantiating "cryptd(crc32c)" causes a crypto self-test failure because
      the crypto_alloc_shash() in alg_test_crc32c() fails.  This is because
      cryptd(crc32c) is an ahash algorithm, not a shash algorithm; so it can
      only be accessed through the ahash API, unlike shash algorithms which
      can be accessed through both the ahash and shash APIs.
      
      As the test is testing the shash descriptor format which is only
      applicable to shash algorithms, skip it for ahash algorithms.
      
      (Note that it's still important to fix crypto self-test failures even
       for weird algorithm instantiations like cryptd(crc32c) that no one
       would really use; in fips_enabled mode unprivileged users can use them
       to panic the kernel, and also they prevent treating a crypto self-test
       failure as a bug when fuzzing the kernel.)
      
      Fixes: 8e3ee85e ("crypto: crc32c - Test descriptor context format")
      Cc: stable@vger.kernel.org
      Signed-off-by: NEric Biggers <ebiggers@google.com>
      Signed-off-by: NHerbert Xu <herbert@gondor.apana.org.au>
      eb5e6730
  10. 25 1月, 2019 1 次提交
    • E
      crypto: clarify name of WEAK_KEY request flag · 231baecd
      Eric Biggers 提交于
      CRYPTO_TFM_REQ_WEAK_KEY confuses newcomers to the crypto API because it
      sounds like it is requesting a weak key.  Actually, it is requesting
      that weak keys be forbidden (for algorithms that have the notion of
      "weak keys"; currently only DES and XTS do).
      
      Also it is only one letter away from CRYPTO_TFM_RES_WEAK_KEY, with which
      it can be easily confused.  (This in fact happened in the UX500 driver,
      though just in some debugging messages.)
      
      Therefore, make the intent clear by renaming it to
      CRYPTO_TFM_REQ_FORBID_WEAK_KEYS.
      Signed-off-by: NEric Biggers <ebiggers@google.com>
      Signed-off-by: NHerbert Xu <herbert@gondor.apana.org.au>
      231baecd
  11. 18 1月, 2019 4 次提交
    • E
      crypto: testmgr - unify the AEAD encryption and decryption test vectors · a0d608ee
      Eric Biggers 提交于
      Currently testmgr has separate encryption and decryption test vectors
      for AEADs.  That's massively redundant, since usually the decryption
      tests are identical to the encryption tests, just with the input/result
      swapped.  And for some algorithms it was forgotten to add decryption
      test vectors, so for them currently only encryption is being tested.
      
      Therefore, eliminate the redundancy by removing the AEAD decryption test
      vectors and updating testmgr to test both AEAD encryption and decryption
      using what used to be the encryption test vectors.  Naming is adjusted
      accordingly: each aead_testvec now has a 'ptext' (plaintext), 'plen'
      (plaintext length), 'ctext' (ciphertext), and 'clen' (ciphertext length)
      instead of an 'input', 'ilen', 'result', and 'rlen'.  "Ciphertext" here
      refers to the full ciphertext, including the authentication tag.
      
      For now the scatterlist divisions are just given for the plaintext
      length, not also the ciphertext length.  For decryption, the last
      scatterlist element is just extended by the authentication tag length.
      
      In total, this removes over 5000 lines from testmgr.h, with no reduction
      in test coverage since prior patches already copied the few unique
      decryption test vectors into the encryption test vectors.
      
      The testmgr.h portion of this patch was automatically generated using
      the following awk script, except that I also manually updated the
      definition of 'struct aead_testvec' and fixed the location of the
      comment describing the AEGIS-128 test vectors.
      
          BEGIN { OTHER = 0; ENCVEC = 1; DECVEC = 2; DECVEC_TAIL = 3; mode = OTHER }
      
          /^static const struct aead_testvec.*_enc_/ { sub("_enc", ""); mode = ENCVEC }
          /^static const struct aead_testvec.*_dec_/ { mode = DECVEC }
          mode == ENCVEC {
              sub(/\.input[[:space:]]*=/,     ".ptext\t=")
              sub(/\.result[[:space:]]*=/,    ".ctext\t=")
              sub(/\.ilen[[:space:]]*=/,      ".plen\t=")
              sub(/\.rlen[[:space:]]*=/,      ".clen\t=")
              print
          }
          mode == DECVEC_TAIL && /[^[:space:]]/ { mode = OTHER }
          mode == OTHER                         { print }
          mode == ENCVEC && /^};/               { mode = OTHER }
          mode == DECVEC && /^};/               { mode = DECVEC_TAIL }
      
      Note that git's default diff algorithm gets confused by the testmgr.h
      portion of this patch, and reports too many lines added and removed.
      It's better viewed with 'git diff --minimal' (or 'git show --minimal'),
      which reports "2 files changed, 1235 insertions(+), 6491 deletions(-)".
      Signed-off-by: NEric Biggers <ebiggers@google.com>
      Signed-off-by: NHerbert Xu <herbert@gondor.apana.org.au>
      a0d608ee
    • E
      crypto: testmgr - skip AEAD encryption test vectors with novrfy set · 5bc3de58
      Eric Biggers 提交于
      In preparation for unifying the AEAD encryption and decryption test
      vectors, skip AEAD test vectors with the 'novrfy' (verification failure
      expected) flag set when testing encryption rather than decryption.
      These test vectors only make sense for decryption.
      Signed-off-by: NEric Biggers <ebiggers@google.com>
      Signed-off-by: NHerbert Xu <herbert@gondor.apana.org.au>
      5bc3de58
    • E
      crypto: testmgr - handle endianness correctly in alg_test_crc32c() · cb9dde88
      Eric Biggers 提交于
      The crc32c context is in CPU endianness, whereas the final digest is
      little endian.  alg_test_crc32c() got this mixed up.  Fix it.
      
      The test passes both before and after, but this patch fixes the
      following sparse warning:
      
          crypto/testmgr.c:1912:24: warning: cast to restricted __le32
      Signed-off-by: NEric Biggers <ebiggers@google.com>
      Signed-off-by: NHerbert Xu <herbert@gondor.apana.org.au>
      cb9dde88
    • V
      crypto: testmgr - split akcipher tests by a key type · 0507de94
      Vitaly Chikunov 提交于
      Before this, if akcipher_testvec have `public_key_vec' set to true
      (i.e. having a public key) only sign/encrypt test is performed, but
      verify/decrypt test is skipped.
      
      With a public key we could do encrypt and verify, but to sign and decrypt
      a private key is required.
      
      This logic is correct for encrypt/decrypt tests (decrypt is skipped if
      no private key). But incorrect for sign/verify tests - sign is performed
      no matter if there is no private key, but verify is skipped if there is
      a public key.
      
      Rework `test_akcipher_one' to arrange tests properly depending on value
      of `public_key_vec` and `siggen_sigver_test'.
      
      No tests were missed since there is only one sign/verify test (which
      have `siggen_sigver_test' set to true) and it has a private key, but
      future tests could benefit from this improvement.
      Signed-off-by: NVitaly Chikunov <vt@altlinux.org>
      Signed-off-by: NHerbert Xu <herbert@gondor.apana.org.au>
      0507de94
  12. 20 11月, 2018 4 次提交
    • E
      crypto: adiantum - add Adiantum support · 059c2a4d
      Eric Biggers 提交于
      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: NEric Biggers <ebiggers@google.com>
      Reviewed-by: NArd Biesheuvel <ard.biesheuvel@linaro.org>
      Signed-off-by: NHerbert Xu <herbert@gondor.apana.org.au>
      059c2a4d
    • E
      crypto: nhpoly1305 - add NHPoly1305 support · 26609a21
      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: NEric Biggers <ebiggers@google.com>
      Acked-by: NArd Biesheuvel <ard.biesheuvel@linaro.org>
      Signed-off-by: NHerbert Xu <herbert@gondor.apana.org.au>
      26609a21
    • E
      crypto: chacha - add XChaCha12 support · aa762409
      Eric Biggers 提交于
      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: NArd Biesheuvel <ard.biesheuvel@linaro.org>
      Acked-by: NMartin Willi <martin@strongswan.org>
      Signed-off-by: NEric Biggers <ebiggers@google.com>
      Signed-off-by: NHerbert Xu <herbert@gondor.apana.org.au>
      aa762409
    • E
      crypto: chacha20-generic - add XChaCha20 support · de61d7ae
      Eric Biggers 提交于
      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: NArd Biesheuvel <ard.biesheuvel@linaro.org>
      Acked-by: NMartin Willi <martin@strongswan.org>
      Signed-off-by: NEric Biggers <ebiggers@google.com>
      Signed-off-by: NHerbert Xu <herbert@gondor.apana.org.au>
      de61d7ae
  13. 16 11月, 2018 1 次提交