.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
---|
1 | 2 | /* |
---|
2 | 3 | * Shared glue code for 128bit block ciphers |
---|
3 | 4 | * |
---|
.. | .. |
---|
7 | 8 | * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au> |
---|
8 | 9 | * CTR part based on code (crypto/ctr.c) by: |
---|
9 | 10 | * (C) Copyright IBM Corp. 2007 - Joy Latten <latten@us.ibm.com> |
---|
10 | | - * |
---|
11 | | - * This program is free software; you can redistribute it and/or modify |
---|
12 | | - * it under the terms of the GNU General Public License as published by |
---|
13 | | - * the Free Software Foundation; either version 2 of the License, or |
---|
14 | | - * (at your option) any later version. |
---|
15 | | - * |
---|
16 | | - * This program is distributed in the hope that it will be useful, |
---|
17 | | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
18 | | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
19 | | - * GNU General Public License for more details. |
---|
20 | | - * |
---|
21 | | - * You should have received a copy of the GNU General Public License |
---|
22 | | - * along with this program; if not, write to the Free Software |
---|
23 | | - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
---|
24 | | - * USA |
---|
25 | | - * |
---|
26 | 11 | */ |
---|
27 | 12 | |
---|
28 | 13 | #include <linux/module.h> |
---|
29 | 14 | #include <crypto/b128ops.h> |
---|
30 | 15 | #include <crypto/gf128mul.h> |
---|
31 | 16 | #include <crypto/internal/skcipher.h> |
---|
| 17 | +#include <crypto/scatterwalk.h> |
---|
32 | 18 | #include <crypto/xts.h> |
---|
33 | 19 | #include <asm/crypto/glue_helper.h> |
---|
34 | 20 | |
---|
.. | .. |
---|
148 | 134 | src -= num_blocks - 1; |
---|
149 | 135 | dst -= num_blocks - 1; |
---|
150 | 136 | |
---|
151 | | - gctx->funcs[i].fn_u.cbc(ctx, dst, src); |
---|
| 137 | + gctx->funcs[i].fn_u.cbc(ctx, (u8 *)dst, |
---|
| 138 | + (const u8 *)src); |
---|
152 | 139 | |
---|
153 | 140 | nbytes -= func_bytes; |
---|
154 | 141 | if (nbytes < bsize) |
---|
.. | .. |
---|
202 | 189 | |
---|
203 | 190 | /* Process multi-block batch */ |
---|
204 | 191 | do { |
---|
205 | | - gctx->funcs[i].fn_u.ctr(ctx, dst, src, &ctrblk); |
---|
| 192 | + gctx->funcs[i].fn_u.ctr(ctx, (u8 *)dst, |
---|
| 193 | + (const u8 *)src, |
---|
| 194 | + &ctrblk); |
---|
206 | 195 | src += num_blocks; |
---|
207 | 196 | dst += num_blocks; |
---|
208 | 197 | nbytes -= func_bytes; |
---|
.. | .. |
---|
224 | 213 | |
---|
225 | 214 | be128_to_le128(&ctrblk, (be128 *)walk.iv); |
---|
226 | 215 | memcpy(&tmp, walk.src.virt.addr, nbytes); |
---|
227 | | - gctx->funcs[gctx->num_funcs - 1].fn_u.ctr(ctx, &tmp, &tmp, |
---|
| 216 | + gctx->funcs[gctx->num_funcs - 1].fn_u.ctr(ctx, (u8 *)&tmp, |
---|
| 217 | + (const u8 *)&tmp, |
---|
228 | 218 | &ctrblk); |
---|
229 | 219 | memcpy(walk.dst.virt.addr, &tmp, nbytes); |
---|
230 | 220 | le128_to_be128((be128 *)walk.iv, &ctrblk); |
---|
.. | .. |
---|
254 | 244 | |
---|
255 | 245 | if (nbytes >= func_bytes) { |
---|
256 | 246 | do { |
---|
257 | | - gctx->funcs[i].fn_u.xts(ctx, dst, src, |
---|
| 247 | + gctx->funcs[i].fn_u.xts(ctx, (u8 *)dst, |
---|
| 248 | + (const u8 *)src, |
---|
258 | 249 | walk->iv); |
---|
259 | 250 | |
---|
260 | 251 | src += num_blocks; |
---|
.. | .. |
---|
274 | 265 | int glue_xts_req_128bit(const struct common_glue_ctx *gctx, |
---|
275 | 266 | struct skcipher_request *req, |
---|
276 | 267 | common_glue_func_t tweak_fn, void *tweak_ctx, |
---|
277 | | - void *crypt_ctx) |
---|
| 268 | + void *crypt_ctx, bool decrypt) |
---|
278 | 269 | { |
---|
| 270 | + const bool cts = (req->cryptlen % XTS_BLOCK_SIZE); |
---|
279 | 271 | const unsigned int bsize = 128 / 8; |
---|
| 272 | + struct skcipher_request subreq; |
---|
280 | 273 | struct skcipher_walk walk; |
---|
281 | 274 | bool fpu_enabled = false; |
---|
282 | | - unsigned int nbytes; |
---|
| 275 | + unsigned int nbytes, tail; |
---|
283 | 276 | int err; |
---|
| 277 | + |
---|
| 278 | + if (req->cryptlen < XTS_BLOCK_SIZE) |
---|
| 279 | + return -EINVAL; |
---|
| 280 | + |
---|
| 281 | + if (unlikely(cts)) { |
---|
| 282 | + struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
---|
| 283 | + |
---|
| 284 | + tail = req->cryptlen % XTS_BLOCK_SIZE + XTS_BLOCK_SIZE; |
---|
| 285 | + |
---|
| 286 | + skcipher_request_set_tfm(&subreq, tfm); |
---|
| 287 | + skcipher_request_set_callback(&subreq, |
---|
| 288 | + crypto_skcipher_get_flags(tfm), |
---|
| 289 | + NULL, NULL); |
---|
| 290 | + skcipher_request_set_crypt(&subreq, req->src, req->dst, |
---|
| 291 | + req->cryptlen - tail, req->iv); |
---|
| 292 | + req = &subreq; |
---|
| 293 | + } |
---|
284 | 294 | |
---|
285 | 295 | err = skcipher_walk_virt(&walk, req, false); |
---|
286 | 296 | nbytes = walk.nbytes; |
---|
287 | | - if (!nbytes) |
---|
| 297 | + if (err) |
---|
288 | 298 | return err; |
---|
289 | 299 | |
---|
290 | 300 | /* set minimum length to bsize, for tweak_fn */ |
---|
.. | .. |
---|
302 | 312 | nbytes = walk.nbytes; |
---|
303 | 313 | } |
---|
304 | 314 | |
---|
| 315 | + if (unlikely(cts)) { |
---|
| 316 | + u8 *next_tweak, *final_tweak = req->iv; |
---|
| 317 | + struct scatterlist *src, *dst; |
---|
| 318 | + struct scatterlist s[2], d[2]; |
---|
| 319 | + le128 b[2]; |
---|
| 320 | + |
---|
| 321 | + dst = src = scatterwalk_ffwd(s, req->src, req->cryptlen); |
---|
| 322 | + if (req->dst != req->src) |
---|
| 323 | + dst = scatterwalk_ffwd(d, req->dst, req->cryptlen); |
---|
| 324 | + |
---|
| 325 | + if (decrypt) { |
---|
| 326 | + next_tweak = memcpy(b, req->iv, XTS_BLOCK_SIZE); |
---|
| 327 | + gf128mul_x_ble(b, b); |
---|
| 328 | + } else { |
---|
| 329 | + next_tweak = req->iv; |
---|
| 330 | + } |
---|
| 331 | + |
---|
| 332 | + skcipher_request_set_crypt(&subreq, src, dst, XTS_BLOCK_SIZE, |
---|
| 333 | + next_tweak); |
---|
| 334 | + |
---|
| 335 | + err = skcipher_walk_virt(&walk, req, false) ?: |
---|
| 336 | + skcipher_walk_done(&walk, |
---|
| 337 | + __glue_xts_req_128bit(gctx, crypt_ctx, &walk)); |
---|
| 338 | + if (err) |
---|
| 339 | + goto out; |
---|
| 340 | + |
---|
| 341 | + scatterwalk_map_and_copy(b, dst, 0, XTS_BLOCK_SIZE, 0); |
---|
| 342 | + memcpy(b + 1, b, tail - XTS_BLOCK_SIZE); |
---|
| 343 | + scatterwalk_map_and_copy(b, src, XTS_BLOCK_SIZE, |
---|
| 344 | + tail - XTS_BLOCK_SIZE, 0); |
---|
| 345 | + scatterwalk_map_and_copy(b, dst, 0, tail, 1); |
---|
| 346 | + |
---|
| 347 | + skcipher_request_set_crypt(&subreq, dst, dst, XTS_BLOCK_SIZE, |
---|
| 348 | + final_tweak); |
---|
| 349 | + |
---|
| 350 | + err = skcipher_walk_virt(&walk, req, false) ?: |
---|
| 351 | + skcipher_walk_done(&walk, |
---|
| 352 | + __glue_xts_req_128bit(gctx, crypt_ctx, &walk)); |
---|
| 353 | + } |
---|
| 354 | + |
---|
| 355 | +out: |
---|
305 | 356 | glue_fpu_end(fpu_enabled); |
---|
306 | 357 | |
---|
307 | 358 | return err; |
---|
308 | 359 | } |
---|
309 | 360 | EXPORT_SYMBOL_GPL(glue_xts_req_128bit); |
---|
310 | 361 | |
---|
311 | | -void glue_xts_crypt_128bit_one(void *ctx, u128 *dst, const u128 *src, le128 *iv, |
---|
312 | | - common_glue_func_t fn) |
---|
| 362 | +void glue_xts_crypt_128bit_one(const void *ctx, u8 *dst, const u8 *src, |
---|
| 363 | + le128 *iv, common_glue_func_t fn) |
---|
313 | 364 | { |
---|
314 | 365 | le128 ivblk = *iv; |
---|
315 | 366 | |
---|
.. | .. |
---|
317 | 368 | gf128mul_x_ble(iv, &ivblk); |
---|
318 | 369 | |
---|
319 | 370 | /* CC <- T xor C */ |
---|
320 | | - u128_xor(dst, src, (u128 *)&ivblk); |
---|
| 371 | + u128_xor((u128 *)dst, (const u128 *)src, (u128 *)&ivblk); |
---|
321 | 372 | |
---|
322 | 373 | /* PP <- D(Key2,CC) */ |
---|
323 | | - fn(ctx, (u8 *)dst, (u8 *)dst); |
---|
| 374 | + fn(ctx, dst, dst); |
---|
324 | 375 | |
---|
325 | 376 | /* P <- T xor PP */ |
---|
326 | | - u128_xor(dst, dst, (u128 *)&ivblk); |
---|
| 377 | + u128_xor((u128 *)dst, (u128 *)dst, (u128 *)&ivblk); |
---|
327 | 378 | } |
---|
328 | 379 | EXPORT_SYMBOL_GPL(glue_xts_crypt_128bit_one); |
---|
329 | 380 | |
---|