Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

crypto: cast6 - use unaligned accessors instead of alignmask

Instead of using an alignmask of 0x3 to ensure 32-bit alignment of the
CAST6 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 <ardb@kernel.org>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

authored by

Ard Biesheuvel and committed by
Herbert Xu
80879dd9 24a2ee44

+17 -22
+17 -22
crypto/cast6_generic.c
··· 10 10 */ 11 11 12 12 13 - #include <asm/byteorder.h> 13 + #include <asm/unaligned.h> 14 14 #include <linux/init.h> 15 15 #include <linux/crypto.h> 16 16 #include <linux/module.h> ··· 172 172 void __cast6_encrypt(const void *ctx, u8 *outbuf, const u8 *inbuf) 173 173 { 174 174 const struct cast6_ctx *c = ctx; 175 - const __be32 *src = (const __be32 *)inbuf; 176 - __be32 *dst = (__be32 *)outbuf; 177 175 u32 block[4]; 178 176 const u32 *Km; 179 177 const u8 *Kr; 180 178 181 - block[0] = be32_to_cpu(src[0]); 182 - block[1] = be32_to_cpu(src[1]); 183 - block[2] = be32_to_cpu(src[2]); 184 - block[3] = be32_to_cpu(src[3]); 179 + block[0] = get_unaligned_be32(inbuf); 180 + block[1] = get_unaligned_be32(inbuf + 4); 181 + block[2] = get_unaligned_be32(inbuf + 8); 182 + block[3] = get_unaligned_be32(inbuf + 12); 185 183 186 184 Km = c->Km[0]; Kr = c->Kr[0]; Q(block, Kr, Km); 187 185 Km = c->Km[1]; Kr = c->Kr[1]; Q(block, Kr, Km); ··· 194 196 Km = c->Km[10]; Kr = c->Kr[10]; QBAR(block, Kr, Km); 195 197 Km = c->Km[11]; Kr = c->Kr[11]; QBAR(block, Kr, Km); 196 198 197 - dst[0] = cpu_to_be32(block[0]); 198 - dst[1] = cpu_to_be32(block[1]); 199 - dst[2] = cpu_to_be32(block[2]); 200 - dst[3] = cpu_to_be32(block[3]); 199 + put_unaligned_be32(block[0], outbuf); 200 + put_unaligned_be32(block[1], outbuf + 4); 201 + put_unaligned_be32(block[2], outbuf + 8); 202 + put_unaligned_be32(block[3], outbuf + 12); 201 203 } 202 204 EXPORT_SYMBOL_GPL(__cast6_encrypt); 203 205 ··· 209 211 void __cast6_decrypt(const void *ctx, u8 *outbuf, const u8 *inbuf) 210 212 { 211 213 const struct cast6_ctx *c = ctx; 212 - const __be32 *src = (const __be32 *)inbuf; 213 - __be32 *dst = (__be32 *)outbuf; 214 214 u32 block[4]; 215 215 const u32 *Km; 216 216 const u8 *Kr; 217 217 218 - block[0] = be32_to_cpu(src[0]); 219 - block[1] = be32_to_cpu(src[1]); 220 - block[2] = be32_to_cpu(src[2]); 221 - block[3] = be32_to_cpu(src[3]); 218 + block[0] = get_unaligned_be32(inbuf); 219 + block[1] = get_unaligned_be32(inbuf + 4); 220 + block[2] = get_unaligned_be32(inbuf + 8); 221 + block[3] = get_unaligned_be32(inbuf + 12); 222 222 223 223 Km = c->Km[11]; Kr = c->Kr[11]; Q(block, Kr, Km); 224 224 Km = c->Km[10]; Kr = c->Kr[10]; Q(block, Kr, Km); ··· 231 235 Km = c->Km[1]; Kr = c->Kr[1]; QBAR(block, Kr, Km); 232 236 Km = c->Km[0]; Kr = c->Kr[0]; QBAR(block, Kr, Km); 233 237 234 - dst[0] = cpu_to_be32(block[0]); 235 - dst[1] = cpu_to_be32(block[1]); 236 - dst[2] = cpu_to_be32(block[2]); 237 - dst[3] = cpu_to_be32(block[3]); 238 + put_unaligned_be32(block[0], outbuf); 239 + put_unaligned_be32(block[1], outbuf + 4); 240 + put_unaligned_be32(block[2], outbuf + 8); 241 + put_unaligned_be32(block[3], outbuf + 12); 238 242 } 239 243 EXPORT_SYMBOL_GPL(__cast6_decrypt); 240 244 ··· 250 254 .cra_flags = CRYPTO_ALG_TYPE_CIPHER, 251 255 .cra_blocksize = CAST6_BLOCK_SIZE, 252 256 .cra_ctxsize = sizeof(struct cast6_ctx), 253 - .cra_alignmask = 3, 254 257 .cra_module = THIS_MODULE, 255 258 .cra_u = { 256 259 .cipher = {