Skip to content

Commit 45fe93d

Browse files
Ard Biesheuvelherbertx
authored andcommitted
crypto: algapi - make crypto_xor() take separate dst and src arguments
There are quite a number of occurrences in the kernel of the pattern if (dst != src) memcpy(dst, src, walk.total % AES_BLOCK_SIZE); crypto_xor(dst, final, walk.total % AES_BLOCK_SIZE); or crypto_xor(keystream, src, nbytes); memcpy(dst, keystream, nbytes); where crypto_xor() is preceded or followed by a memcpy() invocation that is only there because crypto_xor() uses its output parameter as one of the inputs. To avoid having to add new instances of this pattern in the arm64 code, which will be refactored to implement non-SIMD fallbacks, add an alternative implementation called crypto_xor_cpy(), taking separate input and output arguments. This removes the need for the separate memcpy(). Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent a7c391f commit 45fe93d

14 files changed

Lines changed: 42 additions & 40 deletions

File tree

‎arch/arm/crypto/aes-ce-glue.c‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,9 +285,7 @@ static int ctr_encrypt(struct skcipher_request *req)
285285

286286
ce_aes_ctr_encrypt(tail, NULL, (u8 *)ctx->key_enc,
287287
num_rounds(ctx), blocks, walk.iv);
288-
if (tdst != tsrc)
289-
memcpy(tdst, tsrc, nbytes);
290-
crypto_xor(tdst, tail, nbytes);
288+
crypto_xor_cpy(tdst, tsrc, tail, nbytes);
291289
err = skcipher_walk_done(&walk, 0);
292290
}
293291
kernel_neon_end();

‎arch/arm/crypto/aes-neonbs-glue.c‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,8 @@ static int ctr_encrypt(struct skcipher_request *req)
221221
u8 *dst = walk.dst.virt.addr + blocks * AES_BLOCK_SIZE;
222222
u8 *src = walk.src.virt.addr + blocks * AES_BLOCK_SIZE;
223223

224-
if (dst != src)
225-
memcpy(dst, src, walk.total % AES_BLOCK_SIZE);
226-
crypto_xor(dst, final, walk.total % AES_BLOCK_SIZE);
224+
crypto_xor_cpy(dst, src, final,
225+
walk.total % AES_BLOCK_SIZE);
227226

228227
err = skcipher_walk_done(&walk, 0);
229228
break;

‎arch/arm64/crypto/aes-glue.c‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,7 @@ static int ctr_encrypt(struct skcipher_request *req)
241241

242242
aes_ctr_encrypt(tail, NULL, (u8 *)ctx->key_enc, rounds,
243243
blocks, walk.iv, first);
244-
if (tdst != tsrc)
245-
memcpy(tdst, tsrc, nbytes);
246-
crypto_xor(tdst, tail, nbytes);
244+
crypto_xor_cpy(tdst, tsrc, tail, nbytes);
247245
err = skcipher_walk_done(&walk, 0);
248246
}
249247
kernel_neon_end();

‎arch/arm64/crypto/aes-neonbs-glue.c‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,8 @@ static int ctr_encrypt(struct skcipher_request *req)
224224
u8 *dst = walk.dst.virt.addr + blocks * AES_BLOCK_SIZE;
225225
u8 *src = walk.src.virt.addr + blocks * AES_BLOCK_SIZE;
226226

227-
if (dst != src)
228-
memcpy(dst, src, walk.total % AES_BLOCK_SIZE);
229-
crypto_xor(dst, final, walk.total % AES_BLOCK_SIZE);
227+
crypto_xor_cpy(dst, src, final,
228+
walk.total % AES_BLOCK_SIZE);
230229

231230
err = skcipher_walk_done(&walk, 0);
232231
break;

‎arch/sparc/crypto/aes_glue.c‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,7 @@ static void ctr_crypt_final(struct crypto_sparc64_aes_ctx *ctx,
344344

345345
ctx->ops->ecb_encrypt(&ctx->key[0], (const u64 *)ctrblk,
346346
keystream, AES_BLOCK_SIZE);
347-
crypto_xor((u8 *) keystream, src, nbytes);
348-
memcpy(dst, keystream, nbytes);
347+
crypto_xor_cpy(dst, (u8 *) keystream, src, nbytes);
349348
crypto_inc(ctrblk, AES_BLOCK_SIZE);
350349
}
351350

‎arch/x86/crypto/aesni-intel_glue.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -475,8 +475,8 @@ static void ctr_crypt_final(struct crypto_aes_ctx *ctx,
475475
unsigned int nbytes = walk->nbytes;
476476

477477
aesni_enc(ctx, keystream, ctrblk);
478-
crypto_xor(keystream, src, nbytes);
479-
memcpy(dst, keystream, nbytes);
478+
crypto_xor_cpy(dst, keystream, src, nbytes);
479+
480480
crypto_inc(ctrblk, AES_BLOCK_SIZE);
481481
}
482482

‎arch/x86/crypto/blowfish_glue.c‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,7 @@ static void ctr_crypt_final(struct bf_ctx *ctx, struct blkcipher_walk *walk)
271271
unsigned int nbytes = walk->nbytes;
272272

273273
blowfish_enc_blk(ctx, keystream, ctrblk);
274-
crypto_xor(keystream, src, nbytes);
275-
memcpy(dst, keystream, nbytes);
274+
crypto_xor_cpy(dst, keystream, src, nbytes);
276275

277276
crypto_inc(ctrblk, BF_BLOCK_SIZE);
278277
}

‎arch/x86/crypto/cast5_avx_glue.c‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,7 @@ static void ctr_crypt_final(struct blkcipher_desc *desc,
256256
unsigned int nbytes = walk->nbytes;
257257

258258
__cast5_encrypt(ctx, keystream, ctrblk);
259-
crypto_xor(keystream, src, nbytes);
260-
memcpy(dst, keystream, nbytes);
259+
crypto_xor_cpy(dst, keystream, src, nbytes);
261260

262261
crypto_inc(ctrblk, CAST5_BLOCK_SIZE);
263262
}

‎arch/x86/crypto/des3_ede_glue.c‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,7 @@ static void ctr_crypt_final(struct des3_ede_x86_ctx *ctx,
277277
unsigned int nbytes = walk->nbytes;
278278

279279
des3_ede_enc_blk(ctx, keystream, ctrblk);
280-
crypto_xor(keystream, src, nbytes);
281-
memcpy(dst, keystream, nbytes);
280+
crypto_xor_cpy(dst, keystream, src, nbytes);
282281

283282
crypto_inc(ctrblk, DES3_EDE_BLOCK_SIZE);
284283
}

‎crypto/ctr.c‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ static void crypto_ctr_crypt_final(struct blkcipher_walk *walk,
6565
unsigned int nbytes = walk->nbytes;
6666

6767
crypto_cipher_encrypt_one(tfm, keystream, ctrblk);
68-
crypto_xor(keystream, src, nbytes);
69-
memcpy(dst, keystream, nbytes);
68+
crypto_xor_cpy(dst, keystream, src, nbytes);
7069

7170
crypto_inc(ctrblk, bsize);
7271
}

0 commit comments

Comments
 (0)