crypto: hash - annotate algorithms taking optional key
authorEric Biggers <ebiggers@google.com>
Wed, 3 Jan 2018 19:16:26 +0000 (11:16 -0800)
committerHerbert Xu <herbert@gondor.apana.org.au>
Fri, 12 Jan 2018 12:03:35 +0000 (23:03 +1100)
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.

Cc: stable@vger.kernel.org
Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
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 457ae3e66a41e1cda7c36275884d40ea08641efc..addca7bae33f40e9111fc855a5851537373a03f9 100644 (file)
@@ -896,10 +896,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 ace346b976b3fff280c580ea2981d4869da20f6d..fe5129d6ff4ef1fdc804d9cdcbd71c09bf410995 100644 (file)
@@ -516,10 +516,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 231e59f90d32c6ad93544513b0695ad5b363de22..d2e33a90825b4d358783879935e0a64df0192541 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).
  */