]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/commitdiff
crypto: hash - annotate algorithms taking optional key
authorEric Biggers <ebiggers@google.com>
Wed, 3 Jan 2018 19:16:26 +0000 (11:16 -0800)
committerSeth Forshee <seth.forshee@canonical.com>
Thu, 22 Feb 2018 14:15:52 +0000 (08:15 -0600)
BugLink: http://bugs.launchpad.net/bugs/1751064
commit a208fa8f33031b9e0aba44c7d1b7e68eb0cbd29e upstream.

We need to consistently enforce that keyed hashes cannot be used without
setting the key.  To do this we need a reliable way to determine whether
a given hash algorithm is keyed or not.  AF_ALG currently does this by
checking for the presence of a ->setkey() method.  However, this is
actually slightly broken because the CRC-32 algorithms implement
->setkey() but can also be used without a key.  (The CRC-32 "key" is not
actually a cryptographic key but rather represents the initial state.
If not overridden, then a default initial state is used.)

Prepare to fix this by introducing a flag CRYPTO_ALG_OPTIONAL_KEY which
indicates that the algorithm has a ->setkey() method, but it is not
required to be called.  Then set it on all the CRC-32 algorithms.

The same also applies to the Adler-32 implementation in Lustre.

Also, the cryptd and mcryptd templates have to pass through the flag
from their underlying algorithm.

Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Seth Forshee <seth.forshee@canonical.com>
15 files changed:
arch/arm/crypto/crc32-ce-glue.c
arch/arm64/crypto/crc32-ce-glue.c
arch/powerpc/crypto/crc32c-vpmsum_glue.c
arch/s390/crypto/crc32-vx.c
arch/sparc/crypto/crc32c_glue.c
arch/x86/crypto/crc32-pclmul_glue.c
arch/x86/crypto/crc32c-intel_glue.c
crypto/crc32_generic.c
crypto/crc32c_generic.c
crypto/cryptd.c
crypto/mcryptd.c
drivers/crypto/bfin_crc.c
drivers/crypto/stm32/stm32_crc32.c
drivers/staging/lustre/lnet/libcfs/linux/linux-crypto-adler.c
include/linux/crypto.h

index 1b0e0e86ee9c205b16d306f8a76ee9cac90cfee2..96e62ec105d061864e516eacefcbd55e983bfedd 100644 (file)
@@ -188,6 +188,7 @@ static struct shash_alg crc32_pmull_algs[] = { {
        .base.cra_name          = "crc32",
        .base.cra_driver_name   = "crc32-arm-ce",
        .base.cra_priority      = 200,
+       .base.cra_flags         = CRYPTO_ALG_OPTIONAL_KEY,
        .base.cra_blocksize     = 1,
        .base.cra_module        = THIS_MODULE,
 }, {
@@ -203,6 +204,7 @@ static struct shash_alg crc32_pmull_algs[] = { {
        .base.cra_name          = "crc32c",
        .base.cra_driver_name   = "crc32c-arm-ce",
        .base.cra_priority      = 200,
+       .base.cra_flags         = CRYPTO_ALG_OPTIONAL_KEY,
        .base.cra_blocksize     = 1,
        .base.cra_module        = THIS_MODULE,
 } };
index 624f4137918ce5563a8e57576f5e0cb5d1ea1cd9..34b4e3d46aab41b2d05d7e68e58d3cc1d7f4d304 100644 (file)
@@ -185,6 +185,7 @@ static struct shash_alg crc32_pmull_algs[] = { {
        .base.cra_name          = "crc32",
        .base.cra_driver_name   = "crc32-arm64-ce",
        .base.cra_priority      = 200,
+       .base.cra_flags         = CRYPTO_ALG_OPTIONAL_KEY,
        .base.cra_blocksize     = 1,
        .base.cra_module        = THIS_MODULE,
 }, {
@@ -200,6 +201,7 @@ static struct shash_alg crc32_pmull_algs[] = { {
        .base.cra_name          = "crc32c",
        .base.cra_driver_name   = "crc32c-arm64-ce",
        .base.cra_priority      = 200,
+       .base.cra_flags         = CRYPTO_ALG_OPTIONAL_KEY,
        .base.cra_blocksize     = 1,
        .base.cra_module        = THIS_MODULE,
 } };
index f058e0c3e4d4b4d1e22b84973ad8cc054958a3c2..fd1d6c83f0c02df09c4d097b000289c43b323c69 100644 (file)
@@ -141,6 +141,7 @@ static struct shash_alg alg = {
                .cra_name               = "crc32c",
                .cra_driver_name        = "crc32c-vpmsum",
                .cra_priority           = 200,
+               .cra_flags              = CRYPTO_ALG_OPTIONAL_KEY,
                .cra_blocksize          = CHKSUM_BLOCK_SIZE,
                .cra_ctxsize            = sizeof(u32),
                .cra_module             = THIS_MODULE,
index 436865926c26e00b0652c5330f45dd7365962703..423ee05887e6dae1588387855494eca11e242162 100644 (file)
@@ -239,6 +239,7 @@ static struct shash_alg crc32_vx_algs[] = {
                        .cra_name        = "crc32",
                        .cra_driver_name = "crc32-vx",
                        .cra_priority    = 200,
+                       .cra_flags       = CRYPTO_ALG_OPTIONAL_KEY,
                        .cra_blocksize   = CRC32_BLOCK_SIZE,
                        .cra_ctxsize     = sizeof(struct crc_ctx),
                        .cra_module      = THIS_MODULE,
@@ -259,6 +260,7 @@ static struct shash_alg crc32_vx_algs[] = {
                        .cra_name        = "crc32be",
                        .cra_driver_name = "crc32be-vx",
                        .cra_priority    = 200,
+                       .cra_flags       = CRYPTO_ALG_OPTIONAL_KEY,
                        .cra_blocksize   = CRC32_BLOCK_SIZE,
                        .cra_ctxsize     = sizeof(struct crc_ctx),
                        .cra_module      = THIS_MODULE,
@@ -279,6 +281,7 @@ static struct shash_alg crc32_vx_algs[] = {
                        .cra_name        = "crc32c",
                        .cra_driver_name = "crc32c-vx",
                        .cra_priority    = 200,
+                       .cra_flags       = CRYPTO_ALG_OPTIONAL_KEY,
                        .cra_blocksize   = CRC32_BLOCK_SIZE,
                        .cra_ctxsize     = sizeof(struct crc_ctx),
                        .cra_module      = THIS_MODULE,
index d1064e46efe8bea0b83d573e4b6eb0e35aef3596..8aa664638c3c0c4c80d98953f21161c060795054 100644 (file)
@@ -133,6 +133,7 @@ static struct shash_alg alg = {
                .cra_name               =       "crc32c",
                .cra_driver_name        =       "crc32c-sparc64",
                .cra_priority           =       SPARC_CR_OPCODE_PRIORITY,
+               .cra_flags              =       CRYPTO_ALG_OPTIONAL_KEY,
                .cra_blocksize          =       CHKSUM_BLOCK_SIZE,
                .cra_ctxsize            =       sizeof(u32),
                .cra_alignmask          =       7,
index 27226df3f7d8aa16a647a051d8ac19ef02ef5aa2..c8d9cdacbf1046bf35ba95da8ebd0730ce369bbd 100644 (file)
@@ -162,6 +162,7 @@ static struct shash_alg alg = {
                        .cra_name               = "crc32",
                        .cra_driver_name        = "crc32-pclmul",
                        .cra_priority           = 200,
+                       .cra_flags              = CRYPTO_ALG_OPTIONAL_KEY,
                        .cra_blocksize          = CHKSUM_BLOCK_SIZE,
                        .cra_ctxsize            = sizeof(u32),
                        .cra_module             = THIS_MODULE,
index c194d5717ae51e31e0f57170194adf0bce56ea5e..5773e11610723f74fc4f99d44a53d6ef8402d050 100644 (file)
@@ -226,6 +226,7 @@ static struct shash_alg alg = {
                .cra_name               =       "crc32c",
                .cra_driver_name        =       "crc32c-intel",
                .cra_priority           =       200,
+               .cra_flags              =       CRYPTO_ALG_OPTIONAL_KEY,
                .cra_blocksize          =       CHKSUM_BLOCK_SIZE,
                .cra_ctxsize            =       sizeof(u32),
                .cra_module             =       THIS_MODULE,
index aa2a25fc7482a28d0ef3fcb22cdd4edabeed8d98..718cbce8d1695f5a3c064fb7ab5618b6fe5c4e28 100644 (file)
@@ -133,6 +133,7 @@ static struct shash_alg alg = {
                .cra_name               = "crc32",
                .cra_driver_name        = "crc32-generic",
                .cra_priority           = 100,
+               .cra_flags              = CRYPTO_ALG_OPTIONAL_KEY,
                .cra_blocksize          = CHKSUM_BLOCK_SIZE,
                .cra_ctxsize            = sizeof(u32),
                .cra_module             = THIS_MODULE,
index 4c0a0e27187694c6d0d7281c1d61b2f25557f269..372320399622df00f680dc160425d7ce7b85ff81 100644 (file)
@@ -146,6 +146,7 @@ static struct shash_alg alg = {
                .cra_name               =       "crc32c",
                .cra_driver_name        =       "crc32c-generic",
                .cra_priority           =       100,
+               .cra_flags              =       CRYPTO_ALG_OPTIONAL_KEY,
                .cra_blocksize          =       CHKSUM_BLOCK_SIZE,
                .cra_alignmask          =       3,
                .cra_ctxsize            =       sizeof(struct chksum_ctx),
index 8e223fabd979259a4eb1fcf7240dbd5d46095513..c32b98b5daf8ce088d2b819e0f82a0c490d2a746 100644 (file)
@@ -893,10 +893,9 @@ static int cryptd_create_hash(struct crypto_template *tmpl, struct rtattr **tb,
        if (err)
                goto out_free_inst;
 
-       type = CRYPTO_ALG_ASYNC;
-       if (alg->cra_flags & CRYPTO_ALG_INTERNAL)
-               type |= CRYPTO_ALG_INTERNAL;
-       inst->alg.halg.base.cra_flags = type;
+       inst->alg.halg.base.cra_flags = CRYPTO_ALG_ASYNC |
+               (alg->cra_flags & (CRYPTO_ALG_INTERNAL |
+                                  CRYPTO_ALG_OPTIONAL_KEY));
 
        inst->alg.halg.digestsize = salg->digestsize;
        inst->alg.halg.statesize = salg->statesize;
index 6b4dd4b769426a4b2b9a2f9473c9fc8791312607..e0732d979e3b149b18b8f9cfc81656ae5ad93257 100644 (file)
@@ -517,10 +517,9 @@ static int mcryptd_create_hash(struct crypto_template *tmpl, struct rtattr **tb,
        if (err)
                goto out_free_inst;
 
-       type = CRYPTO_ALG_ASYNC;
-       if (alg->cra_flags & CRYPTO_ALG_INTERNAL)
-               type |= CRYPTO_ALG_INTERNAL;
-       inst->alg.halg.base.cra_flags = type;
+       inst->alg.halg.base.cra_flags = CRYPTO_ALG_ASYNC |
+               (alg->cra_flags & (CRYPTO_ALG_INTERNAL |
+                                  CRYPTO_ALG_OPTIONAL_KEY));
 
        inst->alg.halg.digestsize = halg->digestsize;
        inst->alg.halg.statesize = halg->statesize;
index a118b9bed669dfedbecd3557fb3f3c1dce39507a..bfbf8bf77f0334b798ec640699d72c7fdebc2754 100644 (file)
@@ -494,7 +494,8 @@ static struct ahash_alg algs = {
                .cra_driver_name        = DRIVER_NAME,
                .cra_priority           = 100,
                .cra_flags              = CRYPTO_ALG_TYPE_AHASH |
-                                               CRYPTO_ALG_ASYNC,
+                                               CRYPTO_ALG_ASYNC |
+                                               CRYPTO_ALG_OPTIONAL_KEY,
                .cra_blocksize          = CHKSUM_BLOCK_SIZE,
                .cra_ctxsize            = sizeof(struct bfin_crypto_crc_ctx),
                .cra_alignmask          = 3,
index 090582baecfe87ee0486b32a9e992281020e4dcf..8f09b8430893c6278c526d41be18cd1aa27f381a 100644 (file)
@@ -208,6 +208,7 @@ static struct shash_alg algs[] = {
                        .cra_name               = "crc32",
                        .cra_driver_name        = DRIVER_NAME,
                        .cra_priority           = 200,
+                       .cra_flags              = CRYPTO_ALG_OPTIONAL_KEY,
                        .cra_blocksize          = CHKSUM_BLOCK_SIZE,
                        .cra_alignmask          = 3,
                        .cra_ctxsize            = sizeof(struct stm32_crc_ctx),
@@ -229,6 +230,7 @@ static struct shash_alg algs[] = {
                        .cra_name               = "crc32c",
                        .cra_driver_name        = DRIVER_NAME,
                        .cra_priority           = 200,
+                       .cra_flags              = CRYPTO_ALG_OPTIONAL_KEY,
                        .cra_blocksize          = CHKSUM_BLOCK_SIZE,
                        .cra_alignmask          = 3,
                        .cra_ctxsize            = sizeof(struct stm32_crc_ctx),
index 2e5d311d2438dc341defb066d6af1d529103b1fa..db81ed527452850508f7d5e067d74ff7d86c9a3d 100644 (file)
@@ -120,6 +120,7 @@ static struct shash_alg alg = {
                .cra_name               = "adler32",
                .cra_driver_name        = "adler32-zlib",
                .cra_priority           = 100,
+               .cra_flags              = CRYPTO_ALG_OPTIONAL_KEY,
                .cra_blocksize          = CHKSUM_BLOCK_SIZE,
                .cra_ctxsize            = sizeof(u32),
                .cra_module             = THIS_MODULE,
index 78508ca4b108573a4dee3d33038e8e6224b6d30f..72e47b3ecdc86f0dc10578881e5e253f0880ba32 100644 (file)
  */
 #define CRYPTO_ALG_INTERNAL            0x00002000
 
+/*
+ * Set if the algorithm has a ->setkey() method but can be used without
+ * calling it first, i.e. there is a default key.
+ */
+#define CRYPTO_ALG_OPTIONAL_KEY                0x00004000
+
 /*
  * Transform masks and values (for crt_flags).
  */