| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * Cryptographic API. |
|---|
| 3 | 4 | * |
|---|
| 4 | | - * Cipher operations. |
|---|
| 5 | + * Single-block cipher operations. |
|---|
| 5 | 6 | * |
|---|
| 6 | 7 | * Copyright (c) 2002 James Morris <jmorris@intercode.com.au> |
|---|
| 7 | 8 | * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au> |
|---|
| 8 | | - * |
|---|
| 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 |
|---|
| 11 | | - * Software Foundation; either version 2 of the License, or (at your option) |
|---|
| 12 | | - * any later version. |
|---|
| 13 | | - * |
|---|
| 14 | 9 | */ |
|---|
| 15 | 10 | |
|---|
| 16 | 11 | #include <crypto/algapi.h> |
|---|
| 12 | +#include <crypto/internal/cipher.h> |
|---|
| 17 | 13 | #include <linux/kernel.h> |
|---|
| 18 | 14 | #include <linux/crypto.h> |
|---|
| 19 | 15 | #include <linux/errno.h> |
|---|
| .. | .. |
|---|
| 21 | 17 | #include <linux/string.h> |
|---|
| 22 | 18 | #include "internal.h" |
|---|
| 23 | 19 | |
|---|
| 24 | | -static int setkey_unaligned(struct crypto_tfm *tfm, const u8 *key, |
|---|
| 20 | +static int setkey_unaligned(struct crypto_cipher *tfm, const u8 *key, |
|---|
| 25 | 21 | unsigned int keylen) |
|---|
| 26 | 22 | { |
|---|
| 27 | | - struct cipher_alg *cia = &tfm->__crt_alg->cra_cipher; |
|---|
| 28 | | - unsigned long alignmask = crypto_tfm_alg_alignmask(tfm); |
|---|
| 23 | + struct cipher_alg *cia = crypto_cipher_alg(tfm); |
|---|
| 24 | + unsigned long alignmask = crypto_cipher_alignmask(tfm); |
|---|
| 29 | 25 | int ret; |
|---|
| 30 | 26 | u8 *buffer, *alignbuffer; |
|---|
| 31 | 27 | unsigned long absize; |
|---|
| .. | .. |
|---|
| 37 | 33 | |
|---|
| 38 | 34 | alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1); |
|---|
| 39 | 35 | memcpy(alignbuffer, key, keylen); |
|---|
| 40 | | - ret = cia->cia_setkey(tfm, alignbuffer, keylen); |
|---|
| 36 | + ret = cia->cia_setkey(crypto_cipher_tfm(tfm), alignbuffer, keylen); |
|---|
| 41 | 37 | memset(alignbuffer, 0, keylen); |
|---|
| 42 | 38 | kfree(buffer); |
|---|
| 43 | 39 | return ret; |
|---|
| 44 | 40 | |
|---|
| 45 | 41 | } |
|---|
| 46 | 42 | |
|---|
| 47 | | -static int setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen) |
|---|
| 43 | +int crypto_cipher_setkey(struct crypto_cipher *tfm, |
|---|
| 44 | + const u8 *key, unsigned int keylen) |
|---|
| 48 | 45 | { |
|---|
| 49 | | - struct cipher_alg *cia = &tfm->__crt_alg->cra_cipher; |
|---|
| 50 | | - unsigned long alignmask = crypto_tfm_alg_alignmask(tfm); |
|---|
| 46 | + struct cipher_alg *cia = crypto_cipher_alg(tfm); |
|---|
| 47 | + unsigned long alignmask = crypto_cipher_alignmask(tfm); |
|---|
| 51 | 48 | |
|---|
| 52 | | - tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK; |
|---|
| 53 | | - if (keylen < cia->cia_min_keysize || keylen > cia->cia_max_keysize) { |
|---|
| 54 | | - tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN; |
|---|
| 49 | + if (keylen < cia->cia_min_keysize || keylen > cia->cia_max_keysize) |
|---|
| 55 | 50 | return -EINVAL; |
|---|
| 56 | | - } |
|---|
| 57 | 51 | |
|---|
| 58 | 52 | if ((unsigned long)key & alignmask) |
|---|
| 59 | 53 | return setkey_unaligned(tfm, key, keylen); |
|---|
| 60 | 54 | |
|---|
| 61 | | - return cia->cia_setkey(tfm, key, keylen); |
|---|
| 55 | + return cia->cia_setkey(crypto_cipher_tfm(tfm), key, keylen); |
|---|
| 62 | 56 | } |
|---|
| 57 | +EXPORT_SYMBOL_NS_GPL(crypto_cipher_setkey, CRYPTO_INTERNAL); |
|---|
| 63 | 58 | |
|---|
| 64 | | -static void cipher_crypt_unaligned(void (*fn)(struct crypto_tfm *, u8 *, |
|---|
| 65 | | - const u8 *), |
|---|
| 66 | | - struct crypto_tfm *tfm, |
|---|
| 67 | | - u8 *dst, const u8 *src) |
|---|
| 59 | +static inline void cipher_crypt_one(struct crypto_cipher *tfm, |
|---|
| 60 | + u8 *dst, const u8 *src, bool enc) |
|---|
| 68 | 61 | { |
|---|
| 69 | | - unsigned long alignmask = crypto_tfm_alg_alignmask(tfm); |
|---|
| 70 | | - unsigned int size = crypto_tfm_alg_blocksize(tfm); |
|---|
| 71 | | - u8 buffer[MAX_CIPHER_BLOCKSIZE + MAX_CIPHER_ALIGNMASK]; |
|---|
| 72 | | - 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 | | - |
|---|
| 79 | | -static 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; |
|---|
| 62 | + unsigned long alignmask = crypto_cipher_alignmask(tfm); |
|---|
| 63 | + struct cipher_alg *cia = crypto_cipher_alg(tfm); |
|---|
| 64 | + void (*fn)(struct crypto_tfm *, u8 *, const u8 *) = |
|---|
| 65 | + enc ? cia->cia_encrypt : cia->cia_decrypt; |
|---|
| 84 | 66 | |
|---|
| 85 | 67 | if (unlikely(((unsigned long)dst | (unsigned long)src) & alignmask)) { |
|---|
| 86 | | - cipher_crypt_unaligned(cipher->cia_encrypt, tfm, dst, src); |
|---|
| 87 | | - return; |
|---|
| 68 | + unsigned int bs = crypto_cipher_blocksize(tfm); |
|---|
| 69 | + u8 buffer[MAX_CIPHER_BLOCKSIZE + MAX_CIPHER_ALIGNMASK]; |
|---|
| 70 | + u8 *tmp = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1); |
|---|
| 71 | + |
|---|
| 72 | + memcpy(tmp, src, bs); |
|---|
| 73 | + fn(crypto_cipher_tfm(tfm), tmp, tmp); |
|---|
| 74 | + memcpy(dst, tmp, bs); |
|---|
| 75 | + } else { |
|---|
| 76 | + fn(crypto_cipher_tfm(tfm), dst, src); |
|---|
| 88 | 77 | } |
|---|
| 89 | | - |
|---|
| 90 | | - cipher->cia_encrypt(tfm, dst, src); |
|---|
| 91 | 78 | } |
|---|
| 92 | 79 | |
|---|
| 93 | | -static void cipher_decrypt_unaligned(struct crypto_tfm *tfm, |
|---|
| 94 | | - u8 *dst, const u8 *src) |
|---|
| 80 | +void crypto_cipher_encrypt_one(struct crypto_cipher *tfm, |
|---|
| 81 | + u8 *dst, const u8 *src) |
|---|
| 95 | 82 | { |
|---|
| 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); |
|---|
| 83 | + cipher_crypt_one(tfm, dst, src, true); |
|---|
| 105 | 84 | } |
|---|
| 85 | +EXPORT_SYMBOL_NS_GPL(crypto_cipher_encrypt_one, CRYPTO_INTERNAL); |
|---|
| 106 | 86 | |
|---|
| 107 | | -int crypto_init_cipher_ops(struct crypto_tfm *tfm) |
|---|
| 87 | +void crypto_cipher_decrypt_one(struct crypto_cipher *tfm, |
|---|
| 88 | + u8 *dst, const u8 *src) |
|---|
| 108 | 89 | { |
|---|
| 109 | | - struct cipher_tfm *ops = &tfm->crt_cipher; |
|---|
| 110 | | - struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher; |
|---|
| 111 | | - |
|---|
| 112 | | - ops->cit_setkey = setkey; |
|---|
| 113 | | - 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; |
|---|
| 117 | | - |
|---|
| 118 | | - return 0; |
|---|
| 90 | + cipher_crypt_one(tfm, dst, src, false); |
|---|
| 119 | 91 | } |
|---|
| 92 | +EXPORT_SYMBOL_NS_GPL(crypto_cipher_decrypt_one, CRYPTO_INTERNAL); |
|---|