Skip to content

Commit 9933e11

Browse files
committed
crypto: skcipher - Add missing API setkey checks
The API setkey checks for key sizes and alignment went AWOL during the skcipher conversion. This patch restores them. Cc: <stable@vger.kernel.org> Fixes: 4e6c3df ("crypto: skcipher - Add low-level skcipher...") Reported-by: Baozeng <sploving1@gmail.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 2ea659a commit 9933e11

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

crypto/skcipher.c

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,44 @@ static int crypto_init_skcipher_ops_ablkcipher(struct crypto_tfm *tfm)
764764
return 0;
765765
}
766766

767+
static int skcipher_setkey_unaligned(struct crypto_skcipher *tfm,
768+
const u8 *key, unsigned int keylen)
769+
{
770+
unsigned long alignmask = crypto_skcipher_alignmask(tfm);
771+
struct skcipher_alg *cipher = crypto_skcipher_alg(tfm);
772+
u8 *buffer, *alignbuffer;
773+
unsigned long absize;
774+
int ret;
775+
776+
absize = keylen + alignmask;
777+
buffer = kmalloc(absize, GFP_ATOMIC);
778+
if (!buffer)
779+
return -ENOMEM;
780+
781+
alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
782+
memcpy(alignbuffer, key, keylen);
783+
ret = cipher->setkey(tfm, alignbuffer, keylen);
784+
kzfree(buffer);
785+
return ret;
786+
}
787+
788+
static int skcipher_setkey(struct crypto_skcipher *tfm, const u8 *key,
789+
unsigned int keylen)
790+
{
791+
struct skcipher_alg *cipher = crypto_skcipher_alg(tfm);
792+
unsigned long alignmask = crypto_skcipher_alignmask(tfm);
793+
794+
if (keylen < cipher->min_keysize || keylen > cipher->max_keysize) {
795+
crypto_skcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
796+
return -EINVAL;
797+
}
798+
799+
if ((unsigned long)key & alignmask)
800+
return skcipher_setkey_unaligned(tfm, key, keylen);
801+
802+
return cipher->setkey(tfm, key, keylen);
803+
}
804+
767805
static void crypto_skcipher_exit_tfm(struct crypto_tfm *tfm)
768806
{
769807
struct crypto_skcipher *skcipher = __crypto_skcipher_cast(tfm);
@@ -784,7 +822,7 @@ static int crypto_skcipher_init_tfm(struct crypto_tfm *tfm)
784822
tfm->__crt_alg->cra_type == &crypto_givcipher_type)
785823
return crypto_init_skcipher_ops_ablkcipher(tfm);
786824

787-
skcipher->setkey = alg->setkey;
825+
skcipher->setkey = skcipher_setkey;
788826
skcipher->encrypt = alg->encrypt;
789827
skcipher->decrypt = alg->decrypt;
790828
skcipher->ivsize = alg->ivsize;

0 commit comments

Comments
 (0)