Skip to content

Commit 50a3a9f

Browse files
ardbiesheuvelherbertx
authored andcommitted
crypto: blowfish - use unaligned accessors instead of alignmask
Instead of using an alignmask of 0x3 to ensure 32-bit alignment of the Blowfish input and output blocks, which propagates to mode drivers, and results in pointless copying on architectures that don't care about alignment, use the unaligned accessors, which will do the right thing on each respective architecture, avoiding the need for double buffering. Signed-off-by: Ard Biesheuvel <[email protected]> Signed-off-by: Herbert Xu <[email protected]>
1 parent 81d091a commit 50a3a9f

File tree

1 file changed

+9
-14
lines changed

1 file changed

+9
-14
lines changed

crypto/blowfish_generic.c

+9-14
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#include <linux/init.h>
1515
#include <linux/module.h>
1616
#include <linux/mm.h>
17-
#include <asm/byteorder.h>
17+
#include <asm/unaligned.h>
1818
#include <linux/crypto.h>
1919
#include <linux/types.h>
2020
#include <crypto/blowfish.h>
@@ -36,12 +36,10 @@
3636
static void bf_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
3737
{
3838
struct bf_ctx *ctx = crypto_tfm_ctx(tfm);
39-
const __be32 *in_blk = (const __be32 *)src;
40-
__be32 *const out_blk = (__be32 *)dst;
4139
const u32 *P = ctx->p;
4240
const u32 *S = ctx->s;
43-
u32 yl = be32_to_cpu(in_blk[0]);
44-
u32 yr = be32_to_cpu(in_blk[1]);
41+
u32 yl = get_unaligned_be32(src);
42+
u32 yr = get_unaligned_be32(src + 4);
4543

4644
ROUND(yr, yl, 0);
4745
ROUND(yl, yr, 1);
@@ -63,19 +61,17 @@ static void bf_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
6361
yl ^= P[16];
6462
yr ^= P[17];
6563

66-
out_blk[0] = cpu_to_be32(yr);
67-
out_blk[1] = cpu_to_be32(yl);
64+
put_unaligned_be32(yr, dst);
65+
put_unaligned_be32(yl, dst + 4);
6866
}
6967

7068
static void bf_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
7169
{
7270
struct bf_ctx *ctx = crypto_tfm_ctx(tfm);
73-
const __be32 *in_blk = (const __be32 *)src;
74-
__be32 *const out_blk = (__be32 *)dst;
7571
const u32 *P = ctx->p;
7672
const u32 *S = ctx->s;
77-
u32 yl = be32_to_cpu(in_blk[0]);
78-
u32 yr = be32_to_cpu(in_blk[1]);
73+
u32 yl = get_unaligned_be32(src);
74+
u32 yr = get_unaligned_be32(src + 4);
7975

8076
ROUND(yr, yl, 17);
8177
ROUND(yl, yr, 16);
@@ -97,8 +93,8 @@ static void bf_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
9793
yl ^= P[1];
9894
yr ^= P[0];
9995

100-
out_blk[0] = cpu_to_be32(yr);
101-
out_blk[1] = cpu_to_be32(yl);
96+
put_unaligned_be32(yr, dst);
97+
put_unaligned_be32(yl, dst + 4);
10298
}
10399

104100
static struct crypto_alg alg = {
@@ -108,7 +104,6 @@ static struct crypto_alg alg = {
108104
.cra_flags = CRYPTO_ALG_TYPE_CIPHER,
109105
.cra_blocksize = BF_BLOCK_SIZE,
110106
.cra_ctxsize = sizeof(struct bf_ctx),
111-
.cra_alignmask = 3,
112107
.cra_module = THIS_MODULE,
113108
.cra_u = { .cipher = {
114109
.cia_min_keysize = BF_MIN_KEY_SIZE,

0 commit comments

Comments
 (0)