blob: 108427026e7c7a6ab847d19ee4e69e26cfd18034 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Cryptographic API.
4 *
5 * Cipher operations.
6 *
7 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
Herbert Xuc774e932005-07-06 13:51:31 -07008 * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
Herbert Xuf1ddcaf2007-01-27 10:05:15 +110010
Salvatore Mesoraca6650c4d2018-04-09 15:54:47 +020011#include <crypto/algapi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/kernel.h>
13#include <linux/crypto.h>
14#include <linux/errno.h>
Herbert Xu791b4d52007-08-23 16:23:01 +080015#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Herbert Xu791b4d52007-08-23 16:23:01 +080019static int setkey_unaligned(struct crypto_tfm *tfm, const u8 *key,
20 unsigned int keylen)
Sebastian Siewiorca7c3932007-05-19 19:51:21 +100021{
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 Siewior06817172007-08-03 20:33:47 +080036 memset(alignbuffer, 0, keylen);
Sebastian Siewiorca7c3932007-05-19 19:51:21 +100037 kfree(buffer);
38 return ret;
39
40}
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042static int setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen)
43{
44 struct cipher_alg *cia = &tfm->__crt_alg->cra_cipher;
Sebastian Siewiorca7c3932007-05-19 19:51:21 +100045 unsigned long alignmask = crypto_tfm_alg_alignmask(tfm);
46
Herbert Xu560c06a2006-08-13 14:16:39 +100047 tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 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 Siewiorca7c3932007-05-19 19:51:21 +100051 }
52
53 if ((unsigned long)key & alignmask)
54 return setkey_unaligned(tfm, key, keylen);
55
56 return cia->cia_setkey(tfm, key, keylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057}
58
Herbert Xuf28776a2006-08-13 20:58:18 +100059static 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 Mesoraca6650c4d2018-04-09 15:54:47 +020066 u8 buffer[MAX_CIPHER_BLOCKSIZE + MAX_CIPHER_ALIGNMASK];
Herbert Xuf28776a2006-08-13 20:58:18 +100067 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
74static 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
88static 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 Torvalds1da177e2005-04-16 15:20:36 -0700102int crypto_init_cipher_ops(struct crypto_tfm *tfm)
103{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 struct cipher_tfm *ops = &tfm->crt_cipher;
Herbert Xuf28776a2006-08-13 20:58:18 +1000105 struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107 ops->cit_setkey = setkey;
Herbert Xuf28776a2006-08-13 20:58:18 +1000108 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 Torvalds1da177e2005-04-16 15:20:36 -0700112
Herbert Xuf1ddcaf2007-01-27 10:05:15 +1100113 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}