09 Jan, 2020

40 commits

  • All instances need to have a ->free() method, but people could forget to
    set it and then not notice if the instance is never unregistered. To
    help detect this bug earlier, don't allow an instance without a ->free()
    method to be registered, and complain loudly if someone tries to do it.

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

    Eric Biggers
     
  • Now that all templates provide a ->create() method which creates an
    instance, installs a strongly-typed ->free() method directly to it, and
    registers it, the older ->alloc() and ->free() methods in
    'struct crypto_template' are no longer used. Remove them.

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

    Eric Biggers
     
  • Convert shash_free_instance() and its users to the new way of freeing
    instances, where a ->free() method is installed to the instance struct
    itself. This replaces the weakly-typed method crypto_template::free().

    This will allow removing support for the old way of freeing instances.

    Also give shash_free_instance() a more descriptive name to reflect that
    it's only for instances with a single spawn, not for any instance.

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

    Eric Biggers
     
  • Convert the "cryptd" template to the new way of freeing instances, where
    a ->free() method is installed to the instance struct itself. This
    replaces the weakly-typed method crypto_template::free().

    This will allow removing support for the old way of freeing instances.

    Note that the 'default' case in cryptd_free() was already unreachable.
    So, we aren't missing anything by keeping only the ahash and aead parts.

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

    Eric Biggers
     
  • Convert the "seqiv" template to the new way of freeing instances where a
    ->free() method is installed to the instance struct itself. Also remove
    the unused implementation of the old way of freeing instances from the
    "echainiv" template, since it's already using the new way too.

    In doing this, also simplify the code by making the helper function
    aead_geniv_alloc() install the ->free() method, instead of making seqiv
    and echainiv do this themselves. This is analogous to how
    skcipher_alloc_instance_simple() works.

    This will allow removing support for the old way of freeing instances.

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

    Eric Biggers
     
  • Add support to shash and ahash for the new way of freeing instances
    (already used for skcipher, aead, and akcipher) where a ->free() method
    is installed to the instance struct itself. These methods are more
    strongly-typed than crypto_template::free(), which they replace.

    This will allow removing support for the old way of freeing instances.

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

    Eric Biggers
     
  • Now that crypto_init_spawn() is only called by crypto_grab_spawn(),
    simplify things by moving its functionality into crypto_grab_spawn().

    In the process of doing this, also be more consistent about when the
    spawn and instance are updated, and remove the crypto_spawn::dropref
    flag since now it's always set.

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

    Eric Biggers
     
  • Now that all the templates that need ahash spawns have been converted to
    use crypto_grab_ahash() rather than look up the algorithm directly,
    crypto_ahash_type is no longer used outside of ahash.c. Make it static.

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

    Eric Biggers
     
  • Remove lots of helper functions that were previously used for
    instantiating crypto templates, but are now unused:

    - crypto_get_attr_alg() and similar functions looked up an inner
    algorithm directly from a template parameter. These were replaced
    with getting the algorithm's name, then calling crypto_grab_*().

    - crypto_init_spawn2() and similar functions initialized a spawn, given
    an algorithm. Similarly, these were replaced with crypto_grab_*().

    - crypto_alloc_instance() and similar functions allocated an instance
    with a single spawn, given the inner algorithm. These aren't useful
    anymore since crypto_grab_*() need the instance allocated first.

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

    Eric Biggers
     
  • Now that all users of single-block cipher spawns have been converted to
    use 'struct crypto_cipher_spawn' rather than the less specifically typed
    'struct crypto_spawn', make crypto_spawn_cipher() take a pointer to a
    'struct crypto_cipher_spawn' rather than a 'struct crypto_spawn'.

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

    Eric Biggers
     
  • Make the xcbc template use the new function crypto_grab_cipher() to
    initialize its cipher spawn.

    This is needed to make all spawns be initialized in a consistent way.

    This required making xcbc_create() allocate the instance directly rather
    than use shash_alloc_instance().

    Also simplify the error handling by taking advantage of crypto_drop_*()
    now accepting (as a no-op) spawns that haven't been initialized yet, and
    by taking advantage of crypto_grab_*() now handling ERR_PTR() names.

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

    Eric Biggers
     
  • Make the vmac64 template use the new function crypto_grab_cipher() to
    initialize its cipher spawn.

    This is needed to make all spawns be initialized in a consistent way.

    This required making vmac_create() allocate the instance directly rather
    than use shash_alloc_instance().

    Also simplify the error handling by taking advantage of crypto_drop_*()
    now accepting (as a no-op) spawns that haven't been initialized yet, and
    by taking advantage of crypto_grab_*() now handling ERR_PTR() names.

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

    Eric Biggers
     
  • Make the cmac template use the new function crypto_grab_cipher() to
    initialize its cipher spawn.

    This is needed to make all spawns be initialized in a consistent way.

    This required making cmac_create() allocate the instance directly rather
    than use shash_alloc_instance().

    Also simplify the error handling by taking advantage of crypto_drop_*()
    now accepting (as a no-op) spawns that haven't been initialized yet, and
    by taking advantage of crypto_grab_*() now handling ERR_PTR() names.

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

    Eric Biggers
     
  • Make the cbcmac template use the new function crypto_grab_cipher() to
    initialize its cipher spawn.

    This is needed to make all spawns be initialized in a consistent way.

    This required making cbcmac_create() allocate the instance directly
    rather than use shash_alloc_instance().

    Also simplify the error handling by taking advantage of crypto_drop_*()
    now accepting (as a no-op) spawns that haven't been initialized yet, and
    by taking advantage of crypto_grab_*() now handling ERR_PTR() names.

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

    Eric Biggers
     
  • Make skcipher_alloc_instance_simple() use the new function
    crypto_grab_cipher() to initialize its cipher spawn.

    This is needed to make all spawns be initialized in a consistent way.

    Also simplify the error handling by taking advantage of crypto_drop_*()
    now accepting (as a no-op) spawns that haven't been initialized yet, and
    by taking advantage of crypto_grab_*() now handling ERR_PTR() names.

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

    Eric Biggers
     
  • Make the rfc7539 and rfc7539esp templates use the new function
    crypto_grab_ahash() to initialize their ahash spawn.

    This is needed to make all spawns be initialized in a consistent way.

    Also simplify the error handling by taking advantage of crypto_drop_*()
    now accepting (as a no-op) spawns that haven't been initialized yet, and
    by taking advantage of crypto_grab_*() now handling ERR_PTR() names.

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

    Eric Biggers
     
  • Make the ccm and ccm_base templates use the new function
    crypto_grab_ahash() to initialize their ahash spawn.

    This is needed to make all spawns be initialized in a consistent way.

    Also simplify the error handling by taking advantage of crypto_drop_*()
    now accepting (as a no-op) spawns that haven't been initialized yet.

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

    Eric Biggers
     
  • Make the gcm and gcm_base templates use the new function
    crypto_grab_ahash() to initialize their ahash spawn.

    This is needed to make all spawns be initialized in a consistent way.

    Also simplify the error handling by taking advantage of crypto_drop_*()
    now accepting (as a no-op) spawns that haven't been initialized yet.

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

    Eric Biggers
     
  • Make the authencesn template use the new function crypto_grab_ahash() to
    initialize its ahash spawn.

    This is needed to make all spawns be initialized in a consistent way.

    Also simplify the error handling by taking advantage of crypto_drop_*()
    now accepting (as a no-op) spawns that haven't been initialized yet, and
    by taking advantage of crypto_grab_*() now handling ERR_PTR() names.

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

    Eric Biggers
     
  • Make the authenc template use the new function crypto_grab_ahash() to
    initialize its ahash spawn.

    This is needed to make all spawns be initialized in a consistent way.

    Also simplify the error handling by taking advantage of crypto_drop_*()
    now accepting (as a no-op) spawns that haven't been initialized yet, and
    by taking advantage of crypto_grab_*() now handling ERR_PTR() names.

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

    Eric Biggers
     
  • Make the hmac template use the new function crypto_grab_shash() to
    initialize its shash spawn.

    This is needed to make all spawns be initialized in a consistent way.

    This required making hmac_create() allocate the instance directly rather
    than use shash_alloc_instance().

    Also simplify the error handling by taking advantage of crypto_drop_*()
    now accepting (as a no-op) spawns that haven't been initialized yet, and
    by taking advantage of crypto_grab_*() now handling ERR_PTR() names.

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

    Eric Biggers
     
  • Make the cryptd template (in the hash case) use the new function
    crypto_grab_shash() to initialize its shash spawn.

    This is needed to make all spawns be initialized in a consistent way.

    This required making cryptd_create_hash() allocate the instance directly
    rather than use cryptd_alloc_instance().

    Also simplify the error handling by taking advantage of crypto_drop_*()
    now accepting (as a no-op) spawns that haven't been initialized yet, and
    by taking advantage of crypto_grab_*() now handling ERR_PTR() names.

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

    Eric Biggers
     
  • Make the adiantum template use the new functions crypto_grab_cipher()
    and crypto_grab_shash() to initialize its cipher and shash spawns.

    This is needed to make all spawns be initialized in a consistent way.

    Also simplify the error handling by taking advantage of crypto_drop_*()
    now accepting (as a no-op) spawns that haven't been initialized yet, and
    by taking advantage of crypto_grab_*() now handling ERR_PTR() names.

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

    Eric Biggers
     
  • Currently, "cipher" (single-block cipher) spawns are usually initialized
    by using crypto_get_attr_alg() to look up the algorithm, then calling
    crypto_init_spawn(). In one case, crypto_grab_spawn() is used directly.

    The former way is different from how skcipher, aead, and akcipher spawns
    are initialized (they use crypto_grab_*()), and for no good reason.
    This difference introduces unnecessary complexity.

    The crypto_grab_*() functions used to have some problems, like not
    holding a reference to the algorithm and requiring the caller to
    initialize spawn->base.inst. But those problems are fixed now.

    Also, the cipher spawns are not strongly typed; e.g., the API requires
    that the user manually specify the flags CRYPTO_ALG_TYPE_CIPHER and
    CRYPTO_ALG_TYPE_MASK. Though the "cipher" algorithm type itself isn't
    yet strongly typed, we can start by making the spawns strongly typed.

    So, let's introduce a new 'struct crypto_cipher_spawn', and functions
    crypto_grab_cipher() and crypto_drop_cipher() to grab and drop them.

    Later patches will convert all cipher spawns to use these, then make
    crypto_spawn_cipher() take 'struct crypto_cipher_spawn' as well, instead
    of a bare 'struct crypto_spawn' as it currently does.

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

    Eric Biggers
     
  • Currently, ahash spawns are initialized by using ahash_attr_alg() or
    crypto_find_alg() to look up the ahash algorithm, then calling
    crypto_init_ahash_spawn().

    This is different from how skcipher, aead, and akcipher spawns are
    initialized (they use crypto_grab_*()), and for no good reason. This
    difference introduces unnecessary complexity.

    The crypto_grab_*() functions used to have some problems, like not
    holding a reference to the algorithm and requiring the caller to
    initialize spawn->base.inst. But those problems are fixed now.

    So, let's introduce crypto_grab_ahash() so that we can convert all
    templates to the same way of initializing their spawns.

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

    Eric Biggers
     
  • Currently, shash spawns are initialized by using shash_attr_alg() or
    crypto_alg_mod_lookup() to look up the shash algorithm, then calling
    crypto_init_shash_spawn().

    This is different from how skcipher, aead, and akcipher spawns are
    initialized (they use crypto_grab_*()), and for no good reason. This
    difference introduces unnecessary complexity.

    The crypto_grab_*() functions used to have some problems, like not
    holding a reference to the algorithm and requiring the caller to
    initialize spawn->base.inst. But those problems are fixed now.

    So, let's introduce crypto_grab_shash() so that we can convert all
    templates to the same way of initializing their spawns.

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

    Eric Biggers
     
  • Currently, crypto_spawn::inst is first used temporarily to pass the
    instance to crypto_grab_spawn(). Then crypto_init_spawn() overwrites it
    with crypto_spawn::next, which shares the same union. Finally,
    crypto_spawn::inst is set again when the instance is registered.

    Make this less convoluted by just passing the instance as an argument to
    crypto_grab_spawn() instead.

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

    Eric Biggers
     
  • Initializing a crypto_akcipher_spawn currently requires:

    1. Set spawn->base.inst to point to the instance.
    2. Call crypto_grab_akcipher().

    But there's no reason for these steps to be separate, and in fact this
    unneeded complication has caused at least one bug, the one fixed by
    commit 6db43410179b ("crypto: adiantum - initialize crypto_spawn::inst")

    So just make crypto_grab_akcipher() take the instance as an argument.

    To keep the function call from getting too unwieldy due to this extra
    argument, also introduce a 'mask' variable into pkcs1pad_create().

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

    Eric Biggers
     
  • Initializing a crypto_aead_spawn currently requires:

    1. Set spawn->base.inst to point to the instance.
    2. Call crypto_grab_aead().

    But there's no reason for these steps to be separate, and in fact this
    unneeded complication has caused at least one bug, the one fixed by
    commit 6db43410179b ("crypto: adiantum - initialize crypto_spawn::inst")

    So just make crypto_grab_aead() take the instance as an argument.

    To keep the function calls from getting too unwieldy due to this extra
    argument, also introduce a 'mask' variable into the affected places
    which weren't already using one.

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

    Eric Biggers
     
  • Initializing a crypto_skcipher_spawn currently requires:

    1. Set spawn->base.inst to point to the instance.
    2. Call crypto_grab_skcipher().

    But there's no reason for these steps to be separate, and in fact this
    unneeded complication has caused at least one bug, the one fixed by
    commit 6db43410179b ("crypto: adiantum - initialize crypto_spawn::inst")

    So just make crypto_grab_skcipher() take the instance as an argument.

    To keep the function calls from getting too unwieldy due to this extra
    argument, also introduce a 'mask' variable into the affected places
    which weren't already using one.

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

    Eric Biggers
     
  • Define struct ahash_instance in a way analogous to struct
    skcipher_instance, struct aead_instance, and struct akcipher_instance,
    where the struct is defined to include both the algorithm structure at
    the beginning and the additional crypto_instance fields at the end.

    This is needed to allow allocating ahash instances directly using
    kzalloc(sizeof(*inst) + sizeof(*ictx), ...) in the same way as skcipher,
    aead, and akcipher instances. In turn, that's needed to make spawns be
    initialized in a consistent way everywhere.

    Also take advantage of the addition of the base instance to struct
    ahash_instance by simplifying the ahash_crypto_instance() and
    ahash_instance() functions.

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

    Eric Biggers
     
  • Define struct shash_instance in a way analogous to struct
    skcipher_instance, struct aead_instance, and struct akcipher_instance,
    where the struct is defined to include both the algorithm structure at
    the beginning and the additional crypto_instance fields at the end.

    This is needed to allow allocating shash instances directly using
    kzalloc(sizeof(*inst) + sizeof(*ictx), ...) in the same way as skcipher,
    aead, and akcipher instances. In turn, that's needed to make spawns be
    initialized in a consistent way everywhere.

    Also take advantage of the addition of the base instance to struct
    shash_instance by simplifying the shash_crypto_instance() and
    shash_instance() functions.

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

    Eric Biggers
     
  • To allow further simplifying template ->create() functions, make
    crypto_grab_spawn() handle an ERR_PTR() name by passing back the error.

    For most templates, this will allow the result of crypto_attr_alg_name()
    to be passed directly to crypto_grab_*(), rather than first having to
    assign it to a variable [where it can then potentially be misused, as it
    was in the rfc7539 template prior to commit 5e27f38f1f3f ("crypto:
    chacha20poly1305 - set cra_name correctly")] and check it for error.

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

    Eric Biggers
     
  • Make crypto_drop_spawn() do nothing when the spawn hasn't been
    initialized with an algorithm yet. This will allow simplifying error
    handling in all the template ->create() functions, since on error they
    will be able to just call their usual "free instance" function, rather
    than having to handle dropping just the spawns that have been
    initialized so far.

    This does assume the spawn starts out zero-filled, but that's always the
    case since instances are allocated with kzalloc(). And some other code
    already assumes this anyway.

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

    Eric Biggers
     
  • Remove Gary R Hook as CCP maintainer.

    Signed-off-by: Herbert Xu

    Gary R Hook
     
  • The driver should use GFP_KERNEL for the bigger allocation
    during the driver's crypto4xx_probe() and not GFP_ATOMIC in
    my opinion.

    Signed-off-by: Christian Lamparter
    Signed-off-by: Herbert Xu

    Christian Lamparter
     
  • With recent kernels (>5.2), the driver fails to probe, as the
    allocation of the driver's scatter buffer fails with -ENOMEM.

    This happens in crypto4xx_build_sdr(). Where the driver tries
    to get 512KiB (=PPC4XX_SD_BUFFER_SIZE * PPC4XX_NUM_SD) of
    continuous memory. This big chunk is by design, since the driver
    uses this circumstance in the crypto4xx_copy_pkt_to_dst() to
    its advantage:
    "all scatter-buffers are all neatly organized in one big
    continuous ringbuffer; So scatterwalk_map_and_copy() can be
    instructed to copy a range of buffers in one go."

    The PowerPC arch does not have support for DMA_CMA. Hence,
    this patch reorganizes the order in which the memory
    allocations are done. Since the driver itself is responsible
    for some of the issues.

    Signed-off-by: Christian Lamparter
    Signed-off-by: Herbert Xu

    Christian Lamparter
     
  • The CRYPTO_TFM_RES_* flags were apparently meant as a way to make the
    ->setkey() functions provide more information about errors. But these
    flags weren't actually being used or tested, and in many cases they
    weren't being set correctly anyway. So they've now been removed.

    Also, if someone ever actually needs to start better distinguishing
    ->setkey() errors (which is somewhat unlikely, as this has been unneeded
    for a long time), we'd be much better off just defining different return
    values, like -EINVAL if the key is invalid for the algorithm vs.
    -EKEYREJECTED if the key was rejected by a policy like "no weak keys".
    That would be much simpler, less error-prone, and easier to test.

    So just remove CRYPTO_TFM_RES_MASK and all the unneeded logic that
    propagates these flags around.

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

    Eric Biggers
     
  • The CRYPTO_TFM_RES_WEAK_KEY flag was apparently meant as a way to make
    the ->setkey() functions provide more information about errors.

    However, no one actually checks for this flag, which makes it pointless.
    There are also no tests that verify that all algorithms actually set (or
    don't set) it correctly.

    This is also the last remaining CRYPTO_TFM_RES_* flag, which means that
    it's the only thing still needing all the boilerplate code which
    propagates these flags around from child => parent tfms.

    And if someone ever needs to distinguish this error in the future (which
    is somewhat unlikely, as it's been unneeded for a long time), it would
    be much better to just define a new return value like -EKEYREJECTED.
    That would be much simpler, less error-prone, and easier to test.

    So just remove this flag.

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

    Eric Biggers
     
  • The CRYPTO_TFM_RES_BAD_KEY_LEN flag was apparently meant as a way to
    make the ->setkey() functions provide more information about errors.

    However, no one actually checks for this flag, which makes it pointless.

    Also, many algorithms fail to set this flag when given a bad length key.
    Reviewing just the generic implementations, this is the case for
    aes-fixed-time, cbcmac, echainiv, nhpoly1305, pcrypt, rfc3686, rfc4309,
    rfc7539, rfc7539esp, salsa20, seqiv, and xcbc. But there are probably
    many more in arch/*/crypto/ and drivers/crypto/.

    Some algorithms can even set this flag when the key is the correct
    length. For example, authenc and authencesn set it when the key payload
    is malformed in any way (not just a bad length), the atmel-sha and ccree
    drivers can set it if a memory allocation fails, and the chelsio driver
    sets it for bad auth tag lengths, not just bad key lengths.

    So even if someone actually wanted to start checking this flag (which
    seems unlikely, since it's been unused for a long time), there would be
    a lot of work needed to get it working correctly. But it would probably
    be much better to go back to the drawing board and just define different
    return values, like -EINVAL if the key is invalid for the algorithm vs.
    -EKEYREJECTED if the key was rejected by a policy like "no weak keys".
    That would be much simpler, less error-prone, and easier to test.

    So just remove this flag.

    Signed-off-by: Eric Biggers
    Reviewed-by: Horia Geantă
    Signed-off-by: Herbert Xu

    Eric Biggers