blob: 57836c30a49a6ecdae55e9266b6eeb3de52387a0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Cryptographic API.
3 *
4 * Cipher operations.
5 *
6 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
Herbert Xuc774e932005-07-06 13:51:31 -07007 * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the Free
Richard Hartmannd150975b2010-02-16 20:31:37 +080011 * Software Foundation; either version 2 of the License, or (at your option)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * any later version.
13 *
14 */
Herbert Xuf1ddcaf2007-01-27 10:05:15 +110015
Salvatore Mesoraca6650c4d2018-04-09 15:54:47 +020016#include <crypto/algapi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/kernel.h>
18#include <linux/crypto.h>
19#include <linux/errno.h>
Herbert Xu791b4d52007-08-23 16:23:01 +080020#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Herbert Xu791b4d52007-08-23 16:23:01 +080024static int setkey_unaligned(struct crypto_tfm *tfm, const u8 *key,
25 unsigned int keylen)
Sebastian Siewiorca7c3932007-05-19 19:51:21 +100026{
27 struct cipher_alg *cia = &tfm->__crt_alg->cra_cipher;
28 unsigned long alignmask = crypto_tfm_alg_alignmask(tfm);
29 int ret;
30 u8 *buffer, *alignbuffer;
31 unsigned long absize;
32
33 absize = keylen + alignmask;
34 buffer = kmalloc(absize, GFP_ATOMIC);
35 if (!buffer)
36 return -ENOMEM;
37
38 alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
39 memcpy(alignbuffer, key, keylen);
40 ret = cia->cia_setkey(tfm, alignbuffer, keylen);
Sebastian Siewior06817172007-08-03 20:33:47 +080041 memset(alignbuffer, 0, keylen);
Sebastian Siewiorca7c3932007-05-19 19:51:21 +100042 kfree(buffer);
43 return ret;
44
45}
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047static int setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen)
48{
49 struct cipher_alg *cia = &tfm->__crt_alg->cra_cipher;
Sebastian Siewiorca7c3932007-05-19 19:51:21 +100050 unsigned long alignmask = crypto_tfm_alg_alignmask(tfm);
51
Herbert Xu560c06a2006-08-13 14:16:39 +100052 tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 if (keylen < cia->cia_min_keysize || keylen > cia->cia_max_keysize) {
54 tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
55 return -EINVAL;
Sebastian Siewiorca7c3932007-05-19 19:51:21 +100056 }
57
58 if ((unsigned long)key & alignmask)
59 return setkey_unaligned(tfm, key, keylen);
60
61 return cia->cia_setkey(tfm, key, keylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062}
63
Herbert Xuf28776a2006-08-13 20:58:18 +100064static void cipher_crypt_unaligned(void (*fn)(struct crypto_tfm *, u8 *,
65 const u8 *),
66 struct crypto_tfm *tfm,
67 u8 *dst, const u8 *src)
68{
69 unsigned long alignmask = crypto_tfm_alg_alignmask(tfm);
70 unsigned int size = crypto_tfm_alg_blocksize(tfm);
Salvatore Mesoraca6650c4d2018-04-09 15:54:47 +020071 u8 buffer[MAX_CIPHER_BLOCKSIZE + MAX_CIPHER_ALIGNMASK];
Herbert Xuf28776a2006-08-13 20:58:18 +100072 u8 *tmp = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
73
74 memcpy(tmp, src, size);
75 fn(tfm, tmp, tmp);
76 memcpy(dst, tmp, size);
77}
78
79static void cipher_encrypt_unaligned(struct crypto_tfm *tfm,
80 u8 *dst, const u8 *src)
81{
82 unsigned long alignmask = crypto_tfm_alg_alignmask(tfm);
83 struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher;
84
85 if (unlikely(((unsigned long)dst | (unsigned long)src) & alignmask)) {
86 cipher_crypt_unaligned(cipher->cia_encrypt, tfm, dst, src);
87 return;
88 }
89
90 cipher->cia_encrypt(tfm, dst, src);
91}
92
93static void cipher_decrypt_unaligned(struct crypto_tfm *tfm,
94 u8 *dst, const u8 *src)
95{
96 unsigned long alignmask = crypto_tfm_alg_alignmask(tfm);
97 struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher;
98
99 if (unlikely(((unsigned long)dst | (unsigned long)src) & alignmask)) {
100 cipher_crypt_unaligned(cipher->cia_decrypt, tfm, dst, src);
101 return;
102 }
103
104 cipher->cia_decrypt(tfm, dst, src);
105}
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107int crypto_init_cipher_ops(struct crypto_tfm *tfm)
108{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 struct cipher_tfm *ops = &tfm->crt_cipher;
Herbert Xuf28776a2006-08-13 20:58:18 +1000110 struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112 ops->cit_setkey = setkey;
Herbert Xuf28776a2006-08-13 20:58:18 +1000113 ops->cit_encrypt_one = crypto_tfm_alg_alignmask(tfm) ?
114 cipher_encrypt_unaligned : cipher->cia_encrypt;
115 ops->cit_decrypt_one = crypto_tfm_alg_alignmask(tfm) ?
116 cipher_decrypt_unaligned : cipher->cia_decrypt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Herbert Xuf1ddcaf2007-01-27 10:05:15 +1100118 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}