Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Cryptographic API. |
| 4 | * |
| 5 | * Cipher operations. |
| 6 | * |
| 7 | * Copyright (c) 2002 James Morris <jmorris@intercode.com.au> |
Herbert Xu | c774e93 | 2005-07-06 13:51:31 -0700 | [diff] [blame] | 8 | * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | */ |
Herbert Xu | f1ddcaf | 2007-01-27 10:05:15 +1100 | [diff] [blame] | 10 | |
Salvatore Mesoraca | 6650c4d | 2018-04-09 15:54:47 +0200 | [diff] [blame] | 11 | #include <crypto/algapi.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/kernel.h> |
| 13 | #include <linux/crypto.h> |
| 14 | #include <linux/errno.h> |
Herbert Xu | 791b4d5 | 2007-08-23 16:23:01 +0800 | [diff] [blame] | 15 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/string.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include "internal.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | |
Herbert Xu | 791b4d5 | 2007-08-23 16:23:01 +0800 | [diff] [blame] | 19 | static int setkey_unaligned(struct crypto_tfm *tfm, const u8 *key, |
| 20 | unsigned int keylen) |
Sebastian Siewior | ca7c393 | 2007-05-19 19:51:21 +1000 | [diff] [blame] | 21 | { |
| 22 | struct cipher_alg *cia = &tfm->__crt_alg->cra_cipher; |
| 23 | unsigned long alignmask = crypto_tfm_alg_alignmask(tfm); |
| 24 | int ret; |
| 25 | u8 *buffer, *alignbuffer; |
| 26 | unsigned long absize; |
| 27 | |
| 28 | absize = keylen + alignmask; |
| 29 | buffer = kmalloc(absize, GFP_ATOMIC); |
| 30 | if (!buffer) |
| 31 | return -ENOMEM; |
| 32 | |
| 33 | alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1); |
| 34 | memcpy(alignbuffer, key, keylen); |
| 35 | ret = cia->cia_setkey(tfm, alignbuffer, keylen); |
Sebastian Siewior | 0681717 | 2007-08-03 20:33:47 +0800 | [diff] [blame] | 36 | memset(alignbuffer, 0, keylen); |
Sebastian Siewior | ca7c393 | 2007-05-19 19:51:21 +1000 | [diff] [blame] | 37 | kfree(buffer); |
| 38 | return ret; |
| 39 | |
| 40 | } |
| 41 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | static int setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen) |
| 43 | { |
| 44 | struct cipher_alg *cia = &tfm->__crt_alg->cra_cipher; |
Sebastian Siewior | ca7c393 | 2007-05-19 19:51:21 +1000 | [diff] [blame] | 45 | unsigned long alignmask = crypto_tfm_alg_alignmask(tfm); |
| 46 | |
Herbert Xu | 560c06a | 2006-08-13 14:16:39 +1000 | [diff] [blame] | 47 | tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | if (keylen < cia->cia_min_keysize || keylen > cia->cia_max_keysize) { |
| 49 | tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN; |
| 50 | return -EINVAL; |
Sebastian Siewior | ca7c393 | 2007-05-19 19:51:21 +1000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | if ((unsigned long)key & alignmask) |
| 54 | return setkey_unaligned(tfm, key, keylen); |
| 55 | |
| 56 | return cia->cia_setkey(tfm, key, keylen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Herbert Xu | f28776a | 2006-08-13 20:58:18 +1000 | [diff] [blame] | 59 | static void cipher_crypt_unaligned(void (*fn)(struct crypto_tfm *, u8 *, |
| 60 | const u8 *), |
| 61 | struct crypto_tfm *tfm, |
| 62 | u8 *dst, const u8 *src) |
| 63 | { |
| 64 | unsigned long alignmask = crypto_tfm_alg_alignmask(tfm); |
| 65 | unsigned int size = crypto_tfm_alg_blocksize(tfm); |
Salvatore Mesoraca | 6650c4d | 2018-04-09 15:54:47 +0200 | [diff] [blame] | 66 | u8 buffer[MAX_CIPHER_BLOCKSIZE + MAX_CIPHER_ALIGNMASK]; |
Herbert Xu | f28776a | 2006-08-13 20:58:18 +1000 | [diff] [blame] | 67 | u8 *tmp = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1); |
| 68 | |
| 69 | memcpy(tmp, src, size); |
| 70 | fn(tfm, tmp, tmp); |
| 71 | memcpy(dst, tmp, size); |
| 72 | } |
| 73 | |
| 74 | static void cipher_encrypt_unaligned(struct crypto_tfm *tfm, |
| 75 | u8 *dst, const u8 *src) |
| 76 | { |
| 77 | unsigned long alignmask = crypto_tfm_alg_alignmask(tfm); |
| 78 | struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher; |
| 79 | |
| 80 | if (unlikely(((unsigned long)dst | (unsigned long)src) & alignmask)) { |
| 81 | cipher_crypt_unaligned(cipher->cia_encrypt, tfm, dst, src); |
| 82 | return; |
| 83 | } |
| 84 | |
| 85 | cipher->cia_encrypt(tfm, dst, src); |
| 86 | } |
| 87 | |
| 88 | static void cipher_decrypt_unaligned(struct crypto_tfm *tfm, |
| 89 | u8 *dst, const u8 *src) |
| 90 | { |
| 91 | unsigned long alignmask = crypto_tfm_alg_alignmask(tfm); |
| 92 | struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher; |
| 93 | |
| 94 | if (unlikely(((unsigned long)dst | (unsigned long)src) & alignmask)) { |
| 95 | cipher_crypt_unaligned(cipher->cia_decrypt, tfm, dst, src); |
| 96 | return; |
| 97 | } |
| 98 | |
| 99 | cipher->cia_decrypt(tfm, dst, src); |
| 100 | } |
| 101 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | int crypto_init_cipher_ops(struct crypto_tfm *tfm) |
| 103 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | struct cipher_tfm *ops = &tfm->crt_cipher; |
Herbert Xu | f28776a | 2006-08-13 20:58:18 +1000 | [diff] [blame] | 105 | struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
| 107 | ops->cit_setkey = setkey; |
Herbert Xu | f28776a | 2006-08-13 20:58:18 +1000 | [diff] [blame] | 108 | ops->cit_encrypt_one = crypto_tfm_alg_alignmask(tfm) ? |
| 109 | cipher_encrypt_unaligned : cipher->cia_encrypt; |
| 110 | ops->cit_decrypt_one = crypto_tfm_alg_alignmask(tfm) ? |
| 111 | cipher_decrypt_unaligned : cipher->cia_decrypt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | |
Herbert Xu | f1ddcaf | 2007-01-27 10:05:15 +1100 | [diff] [blame] | 113 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | } |