]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blobdiff - crypto/ahash.c
crypto: ghash - fix unaligned memory access in ghash_setkey()
[mirror_ubuntu-bionic-kernel.git] / crypto / ahash.c
index 3a35d67de7d933dfdf4d1908ded5fead53c9a335..8cc326594d6c5a5af90ea4df3309278c9752fc8d 100644 (file)
@@ -86,19 +86,20 @@ static int hash_walk_new_entry(struct crypto_hash_walk *walk)
 int crypto_hash_walk_done(struct crypto_hash_walk *walk, int err)
 {
        unsigned int alignmask = walk->alignmask;
-       unsigned int nbytes = walk->entrylen;
 
        walk->data -= walk->offset;
 
-       if (nbytes && walk->offset & alignmask && !err) {
-               walk->offset = ALIGN(walk->offset, alignmask + 1);
-               walk->data += walk->offset;
-
-               nbytes = min(nbytes,
-                            ((unsigned int)(PAGE_SIZE)) - walk->offset);
-               walk->entrylen -= nbytes;
+       if (walk->entrylen && (walk->offset & alignmask) && !err) {
+               unsigned int nbytes;
 
-               return nbytes;
+               walk->offset = ALIGN(walk->offset, alignmask + 1);
+               nbytes = min(walk->entrylen,
+                            (unsigned int)(PAGE_SIZE - walk->offset));
+               if (nbytes) {
+                       walk->entrylen -= nbytes;
+                       walk->data += walk->offset;
+                       return nbytes;
+               }
        }
 
        if (walk->flags & CRYPTO_ALG_ASYNC)
@@ -115,7 +116,7 @@ int crypto_hash_walk_done(struct crypto_hash_walk *walk, int err)
        if (err)
                return err;
 
-       if (nbytes) {
+       if (walk->entrylen) {
                walk->offset = 0;
                walk->pg++;
                return hash_walk_next(walk);
@@ -189,23 +190,41 @@ static int ahash_setkey_unaligned(struct crypto_ahash *tfm, const u8 *key,
        return ret;
 }
 
+static int ahash_nosetkey(struct crypto_ahash *tfm, const u8 *key,
+                         unsigned int keylen)
+{
+       return -ENOSYS;
+}
+
+static void ahash_set_needkey(struct crypto_ahash *tfm)
+{
+       const struct hash_alg_common *alg = crypto_hash_alg_common(tfm);
+
+       if (tfm->setkey != ahash_nosetkey &&
+           !(alg->base.cra_flags & CRYPTO_ALG_OPTIONAL_KEY))
+               crypto_ahash_set_flags(tfm, CRYPTO_TFM_NEED_KEY);
+}
+
 int crypto_ahash_setkey(struct crypto_ahash *tfm, const u8 *key,
                        unsigned int keylen)
 {
        unsigned long alignmask = crypto_ahash_alignmask(tfm);
+       int err;
 
        if ((unsigned long)key & alignmask)
-               return ahash_setkey_unaligned(tfm, key, keylen);
+               err = ahash_setkey_unaligned(tfm, key, keylen);
+       else
+               err = tfm->setkey(tfm, key, keylen);
 
-       return tfm->setkey(tfm, key, keylen);
-}
-EXPORT_SYMBOL_GPL(crypto_ahash_setkey);
+       if (unlikely(err)) {
+               ahash_set_needkey(tfm);
+               return err;
+       }
 
-static int ahash_nosetkey(struct crypto_ahash *tfm, const u8 *key,
-                         unsigned int keylen)
-{
-       return -ENOSYS;
+       crypto_ahash_clear_flags(tfm, CRYPTO_TFM_NEED_KEY);
+       return 0;
 }
+EXPORT_SYMBOL_GPL(crypto_ahash_setkey);
 
 static inline unsigned int ahash_align_buffer_size(unsigned len,
                                                   unsigned long mask)
@@ -368,7 +387,12 @@ EXPORT_SYMBOL_GPL(crypto_ahash_finup);
 
 int crypto_ahash_digest(struct ahash_request *req)
 {
-       return crypto_ahash_op(req, crypto_ahash_reqtfm(req)->digest);
+       struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
+
+       if (crypto_ahash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
+               return -ENOKEY;
+
+       return crypto_ahash_op(req, tfm->digest);
 }
 EXPORT_SYMBOL_GPL(crypto_ahash_digest);
 
@@ -450,7 +474,6 @@ static int crypto_ahash_init_tfm(struct crypto_tfm *tfm)
        struct ahash_alg *alg = crypto_ahash_alg(hash);
 
        hash->setkey = ahash_nosetkey;
-       hash->has_setkey = false;
        hash->export = ahash_no_export;
        hash->import = ahash_no_import;
 
@@ -465,7 +488,7 @@ static int crypto_ahash_init_tfm(struct crypto_tfm *tfm)
 
        if (alg->setkey) {
                hash->setkey = alg->setkey;
-               hash->has_setkey = true;
+               ahash_set_needkey(hash);
        }
        if (alg->export)
                hash->export = alg->export;
@@ -649,5 +672,16 @@ struct hash_alg_common *ahash_attr_alg(struct rtattr *rta, u32 type, u32 mask)
 }
 EXPORT_SYMBOL_GPL(ahash_attr_alg);
 
+bool crypto_hash_alg_has_setkey(struct hash_alg_common *halg)
+{
+       struct crypto_alg *alg = &halg->base;
+
+       if (alg->cra_type != &crypto_ahash_type)
+               return crypto_shash_alg_has_setkey(__crypto_shash_alg(alg));
+
+       return __crypto_ahash_alg(alg)->setkey != NULL;
+}
+EXPORT_SYMBOL_GPL(crypto_hash_alg_has_setkey);
+
 MODULE_LICENSE("GPL");
 MODULE_DESCRIPTION("Asynchronous cryptographic hash type");