|
19 | 19 | #include <crypto/algapi.h> |
20 | 20 | #include <crypto/internal/skcipher.h> |
21 | 21 | #include <crypto/xts.h> |
| 22 | +#include <crypto/gf128mul.h> |
| 23 | +#include <crypto/scatterwalk.h> |
22 | 24 |
|
23 | 25 | /* |
24 | 26 | * 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) |
327 | 329 |
|
328 | 330 | static int ppc_xts_encrypt(struct skcipher_request *req) |
329 | 331 | { |
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; |
331 | 366 | } |
332 | 367 |
|
333 | 368 | static int ppc_xts_decrypt(struct skcipher_request *req) |
334 | 369 | { |
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; |
336 | 413 | } |
337 | 414 |
|
338 | 415 | /* |
|
0 commit comments