Skip to content

Commit d0be072

Browse files
Ard Biesheuvelherbertx
authored andcommitted
crypto: powerpc/spe-xts - implement support for ciphertext stealing
Add the logic to deal with input sizes that are not a round multiple of the AES block size, as described by the XTS spec. This brings the SPE implementation in line with other kernel drivers that have been updated recently to take this into account. Cc: Eric Biggers <ebiggers@google.com> Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 7f725f4 commit d0be072

1 file changed

Lines changed: 79 additions & 2 deletions

File tree

‎arch/powerpc/crypto/aes-spe-glue.c‎

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#include <crypto/algapi.h>
2020
#include <crypto/internal/skcipher.h>
2121
#include <crypto/xts.h>
22+
#include <crypto/gf128mul.h>
23+
#include <crypto/scatterwalk.h>
2224

2325
/*
2426
* MAX_BYTES defines the number of bytes that are allowed to be processed
@@ -327,12 +329,87 @@ static int ppc_xts_crypt(struct skcipher_request *req, bool enc)
327329

328330
static int ppc_xts_encrypt(struct skcipher_request *req)
329331
{
330-
return ppc_xts_crypt(req, true);
332+
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
333+
struct ppc_xts_ctx *ctx = crypto_skcipher_ctx(tfm);
334+
int tail = req->cryptlen % AES_BLOCK_SIZE;
335+
int offset = req->cryptlen - tail - AES_BLOCK_SIZE;
336+
struct skcipher_request subreq;
337+
u8 b[2][AES_BLOCK_SIZE];
338+
int err;
339+
340+
if (req->cryptlen < AES_BLOCK_SIZE)
341+
return -EINVAL;
342+
343+
if (tail) {
344+
subreq = *req;
345+
skcipher_request_set_crypt(&subreq, req->src, req->dst,
346+
req->cryptlen - tail, req->iv);
347+
req = &subreq;
348+
}
349+
350+
err = ppc_xts_crypt(req, true);
351+
if (err || !tail)
352+
return err;
353+
354+
scatterwalk_map_and_copy(b[0], req->dst, offset, AES_BLOCK_SIZE, 0);
355+
memcpy(b[1], b[0], tail);
356+
scatterwalk_map_and_copy(b[0], req->src, offset + AES_BLOCK_SIZE, tail, 0);
357+
358+
spe_begin();
359+
ppc_encrypt_xts(b[0], b[0], ctx->key_enc, ctx->rounds, AES_BLOCK_SIZE,
360+
req->iv, NULL);
361+
spe_end();
362+
363+
scatterwalk_map_and_copy(b[0], req->dst, offset, AES_BLOCK_SIZE + tail, 1);
364+
365+
return 0;
331366
}
332367

333368
static int ppc_xts_decrypt(struct skcipher_request *req)
334369
{
335-
return ppc_xts_crypt(req, false);
370+
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
371+
struct ppc_xts_ctx *ctx = crypto_skcipher_ctx(tfm);
372+
int tail = req->cryptlen % AES_BLOCK_SIZE;
373+
int offset = req->cryptlen - tail - AES_BLOCK_SIZE;
374+
struct skcipher_request subreq;
375+
u8 b[3][AES_BLOCK_SIZE];
376+
le128 twk;
377+
int err;
378+
379+
if (req->cryptlen < AES_BLOCK_SIZE)
380+
return -EINVAL;
381+
382+
if (tail) {
383+
subreq = *req;
384+
skcipher_request_set_crypt(&subreq, req->src, req->dst,
385+
offset, req->iv);
386+
req = &subreq;
387+
}
388+
389+
err = ppc_xts_crypt(req, false);
390+
if (err || !tail)
391+
return err;
392+
393+
scatterwalk_map_and_copy(b[1], req->src, offset, AES_BLOCK_SIZE + tail, 0);
394+
395+
spe_begin();
396+
if (!offset)
397+
ppc_encrypt_ecb(req->iv, req->iv, ctx->key_twk, ctx->rounds,
398+
AES_BLOCK_SIZE);
399+
400+
gf128mul_x_ble(&twk, (le128 *)req->iv);
401+
402+
ppc_decrypt_xts(b[1], b[1], ctx->key_dec, ctx->rounds, AES_BLOCK_SIZE,
403+
(u8 *)&twk, NULL);
404+
memcpy(b[0], b[2], tail);
405+
memcpy(b[0] + tail, b[1] + tail, AES_BLOCK_SIZE - tail);
406+
ppc_decrypt_xts(b[0], b[0], ctx->key_dec, ctx->rounds, AES_BLOCK_SIZE,
407+
req->iv, NULL);
408+
spe_end();
409+
410+
scatterwalk_map_and_copy(b[0], req->dst, offset, AES_BLOCK_SIZE + tail, 1);
411+
412+
return 0;
336413
}
337414

338415
/*

0 commit comments

Comments
 (0)