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

crypto: xts - Convert to skcipher

This patch converts xts over to the skcipher interface. It also
optimises the implementation to be based on ECB instead of the
underlying cipher. For compatibility the existing naming scheme
of xts(aes) is maintained as opposed to the more obvious one of
xts(ecb(aes)).

Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

+448 -189
+424 -187
crypto/xts.c
··· 13 13 * Software Foundation; either version 2 of the License, or (at your option) 14 14 * any later version. 15 15 */ 16 - #include <crypto/algapi.h> 16 + #include <crypto/internal/skcipher.h> 17 + #include <crypto/scatterwalk.h> 17 18 #include <linux/err.h> 18 19 #include <linux/init.h> 19 20 #include <linux/kernel.h> ··· 26 25 #include <crypto/b128ops.h> 27 26 #include <crypto/gf128mul.h> 28 27 28 + #define XTS_BUFFER_SIZE 128u 29 + 29 30 struct priv { 30 - struct crypto_cipher *child; 31 + struct crypto_skcipher *child; 31 32 struct crypto_cipher *tweak; 32 33 }; 33 34 34 - static int setkey(struct crypto_tfm *parent, const u8 *key, 35 + struct xts_instance_ctx { 36 + struct crypto_skcipher_spawn spawn; 37 + char name[CRYPTO_MAX_ALG_NAME]; 38 + }; 39 + 40 + struct rctx { 41 + be128 buf[XTS_BUFFER_SIZE / sizeof(be128)]; 42 + 43 + be128 t; 44 + 45 + be128 *ext; 46 + 47 + struct scatterlist srcbuf[2]; 48 + struct scatterlist dstbuf[2]; 49 + struct scatterlist *src; 50 + struct scatterlist *dst; 51 + 52 + unsigned int left; 53 + 54 + struct skcipher_request subreq; 55 + }; 56 + 57 + static int setkey(struct crypto_skcipher *parent, const u8 *key, 35 58 unsigned int keylen) 36 59 { 37 - struct priv *ctx = crypto_tfm_ctx(parent); 38 - struct crypto_cipher *child = ctx->tweak; 60 + struct priv *ctx = crypto_skcipher_ctx(parent); 61 + struct crypto_skcipher *child; 62 + struct crypto_cipher *tweak; 39 63 int err; 40 64 41 - err = xts_check_key(parent, key, keylen); 65 + err = xts_verify_key(parent, key, keylen); 42 66 if (err) 43 67 return err; 68 + 69 + keylen /= 2; 44 70 45 71 /* we need two cipher instances: one to compute the initial 'tweak' 46 72 * by encrypting the IV (usually the 'plain' iv) and the other 47 73 * one to encrypt and decrypt the data */ 48 74 49 75 /* tweak cipher, uses Key2 i.e. the second half of *key */ 50 - crypto_cipher_clear_flags(child, CRYPTO_TFM_REQ_MASK); 51 - crypto_cipher_set_flags(child, crypto_tfm_get_flags(parent) & 76 + tweak = ctx->tweak; 77 + crypto_cipher_clear_flags(tweak, CRYPTO_TFM_REQ_MASK); 78 + crypto_cipher_set_flags(tweak, crypto_skcipher_get_flags(parent) & 52 79 CRYPTO_TFM_REQ_MASK); 53 - err = crypto_cipher_setkey(child, key + keylen/2, keylen/2); 80 + err = crypto_cipher_setkey(tweak, key + keylen, keylen); 81 + crypto_skcipher_set_flags(parent, crypto_cipher_get_flags(tweak) & 82 + CRYPTO_TFM_RES_MASK); 54 83 if (err) 55 84 return err; 56 - 57 - crypto_tfm_set_flags(parent, crypto_cipher_get_flags(child) & 58 - CRYPTO_TFM_RES_MASK); 59 - 60 - child = ctx->child; 61 85 62 86 /* data cipher, uses Key1 i.e. the first half of *key */ 63 - crypto_cipher_clear_flags(child, CRYPTO_TFM_REQ_MASK); 64 - crypto_cipher_set_flags(child, crypto_tfm_get_flags(parent) & 65 - CRYPTO_TFM_REQ_MASK); 66 - err = crypto_cipher_setkey(child, key, keylen/2); 67 - if (err) 68 - return err; 69 - 70 - crypto_tfm_set_flags(parent, crypto_cipher_get_flags(child) & 71 - CRYPTO_TFM_RES_MASK); 72 - 73 - return 0; 74 - } 75 - 76 - struct sinfo { 77 - be128 *t; 78 - struct crypto_tfm *tfm; 79 - void (*fn)(struct crypto_tfm *, u8 *, const u8 *); 80 - }; 81 - 82 - static inline void xts_round(struct sinfo *s, void *dst, const void *src) 83 - { 84 - be128_xor(dst, s->t, src); /* PP <- T xor P */ 85 - s->fn(s->tfm, dst, dst); /* CC <- E(Key1,PP) */ 86 - be128_xor(dst, dst, s->t); /* C <- T xor CC */ 87 - } 88 - 89 - static int crypt(struct blkcipher_desc *d, 90 - struct blkcipher_walk *w, struct priv *ctx, 91 - void (*tw)(struct crypto_tfm *, u8 *, const u8 *), 92 - void (*fn)(struct crypto_tfm *, u8 *, const u8 *)) 93 - { 94 - int err; 95 - unsigned int avail; 96 - const int bs = XTS_BLOCK_SIZE; 97 - struct sinfo s = { 98 - .tfm = crypto_cipher_tfm(ctx->child), 99 - .fn = fn 100 - }; 101 - u8 *wsrc; 102 - u8 *wdst; 103 - 104 - err = blkcipher_walk_virt(d, w); 105 - if (!w->nbytes) 106 - return err; 107 - 108 - s.t = (be128 *)w->iv; 109 - avail = w->nbytes; 110 - 111 - wsrc = w->src.virt.addr; 112 - wdst = w->dst.virt.addr; 113 - 114 - /* calculate first value of T */ 115 - tw(crypto_cipher_tfm(ctx->tweak), w->iv, w->iv); 116 - 117 - goto first; 118 - 119 - for (;;) { 120 - do { 121 - gf128mul_x_ble(s.t, s.t); 122 - 123 - first: 124 - xts_round(&s, wdst, wsrc); 125 - 126 - wsrc += bs; 127 - wdst += bs; 128 - } while ((avail -= bs) >= bs); 129 - 130 - err = blkcipher_walk_done(d, w, avail); 131 - if (!w->nbytes) 132 - break; 133 - 134 - avail = w->nbytes; 135 - 136 - wsrc = w->src.virt.addr; 137 - wdst = w->dst.virt.addr; 138 - } 87 + child = ctx->child; 88 + crypto_skcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK); 89 + crypto_skcipher_set_flags(child, crypto_skcipher_get_flags(parent) & 90 + CRYPTO_TFM_REQ_MASK); 91 + err = crypto_skcipher_setkey(child, key, keylen); 92 + crypto_skcipher_set_flags(parent, crypto_skcipher_get_flags(child) & 93 + CRYPTO_TFM_RES_MASK); 139 94 140 95 return err; 141 96 } 142 97 143 - static int encrypt(struct blkcipher_desc *desc, struct scatterlist *dst, 144 - struct scatterlist *src, unsigned int nbytes) 98 + static int post_crypt(struct skcipher_request *req) 145 99 { 146 - struct priv *ctx = crypto_blkcipher_ctx(desc->tfm); 147 - struct blkcipher_walk w; 100 + struct rctx *rctx = skcipher_request_ctx(req); 101 + be128 *buf = rctx->ext ?: rctx->buf; 102 + struct skcipher_request *subreq; 103 + const int bs = XTS_BLOCK_SIZE; 104 + struct skcipher_walk w; 105 + struct scatterlist *sg; 106 + unsigned offset; 107 + int err; 148 108 149 - blkcipher_walk_init(&w, dst, src, nbytes); 150 - return crypt(desc, &w, ctx, crypto_cipher_alg(ctx->tweak)->cia_encrypt, 151 - crypto_cipher_alg(ctx->child)->cia_encrypt); 109 + subreq = &rctx->subreq; 110 + err = skcipher_walk_virt(&w, subreq, false); 111 + 112 + while (w.nbytes) { 113 + unsigned int avail = w.nbytes; 114 + be128 *wdst; 115 + 116 + wdst = w.dst.virt.addr; 117 + 118 + do { 119 + be128_xor(wdst, buf++, wdst); 120 + wdst++; 121 + } while ((avail -= bs) >= bs); 122 + 123 + err = skcipher_walk_done(&w, avail); 124 + } 125 + 126 + rctx->left -= subreq->cryptlen; 127 + 128 + if (err || !rctx->left) 129 + goto out; 130 + 131 + rctx->dst = rctx->dstbuf; 132 + 133 + scatterwalk_done(&w.out, 0, 1); 134 + sg = w.out.sg; 135 + offset = w.out.offset; 136 + 137 + if (rctx->dst != sg) { 138 + rctx->dst[0] = *sg; 139 + sg_unmark_end(rctx->dst); 140 + scatterwalk_crypto_chain(rctx->dst, sg_next(sg), 0, 2); 141 + } 142 + rctx->dst[0].length -= offset - sg->offset; 143 + rctx->dst[0].offset = offset; 144 + 145 + out: 146 + return err; 152 147 } 153 148 154 - static int decrypt(struct blkcipher_desc *desc, struct scatterlist *dst, 155 - struct scatterlist *src, unsigned int nbytes) 149 + static int pre_crypt(struct skcipher_request *req) 156 150 { 157 - struct priv *ctx = crypto_blkcipher_ctx(desc->tfm); 158 - struct blkcipher_walk w; 151 + struct rctx *rctx = skcipher_request_ctx(req); 152 + be128 *buf = rctx->ext ?: rctx->buf; 153 + struct skcipher_request *subreq; 154 + const int bs = XTS_BLOCK_SIZE; 155 + struct skcipher_walk w; 156 + struct scatterlist *sg; 157 + unsigned cryptlen; 158 + unsigned offset; 159 + bool more; 160 + int err; 159 161 160 - blkcipher_walk_init(&w, dst, src, nbytes); 161 - return crypt(desc, &w, ctx, crypto_cipher_alg(ctx->tweak)->cia_encrypt, 162 - crypto_cipher_alg(ctx->child)->cia_decrypt); 162 + subreq = &rctx->subreq; 163 + cryptlen = subreq->cryptlen; 164 + 165 + more = rctx->left > cryptlen; 166 + if (!more) 167 + cryptlen = rctx->left; 168 + 169 + skcipher_request_set_crypt(subreq, rctx->src, rctx->dst, 170 + cryptlen, NULL); 171 + 172 + err = skcipher_walk_virt(&w, subreq, false); 173 + 174 + while (w.nbytes) { 175 + unsigned int avail = w.nbytes; 176 + be128 *wsrc; 177 + be128 *wdst; 178 + 179 + wsrc = w.src.virt.addr; 180 + wdst = w.dst.virt.addr; 181 + 182 + do { 183 + *buf++ = rctx->t; 184 + be128_xor(wdst++, &rctx->t, wsrc++); 185 + gf128mul_x_ble(&rctx->t, &rctx->t); 186 + } while ((avail -= bs) >= bs); 187 + 188 + err = skcipher_walk_done(&w, avail); 189 + } 190 + 191 + skcipher_request_set_crypt(subreq, rctx->dst, rctx->dst, 192 + cryptlen, NULL); 193 + 194 + if (err || !more) 195 + goto out; 196 + 197 + rctx->src = rctx->srcbuf; 198 + 199 + scatterwalk_done(&w.in, 0, 1); 200 + sg = w.in.sg; 201 + offset = w.in.offset; 202 + 203 + if (rctx->src != sg) { 204 + rctx->src[0] = *sg; 205 + sg_unmark_end(rctx->src); 206 + scatterwalk_crypto_chain(rctx->src, sg_next(sg), 0, 2); 207 + } 208 + rctx->src[0].length -= offset - sg->offset; 209 + rctx->src[0].offset = offset; 210 + 211 + out: 212 + return err; 213 + } 214 + 215 + static int init_crypt(struct skcipher_request *req, crypto_completion_t done) 216 + { 217 + struct priv *ctx = crypto_skcipher_ctx(crypto_skcipher_reqtfm(req)); 218 + struct rctx *rctx = skcipher_request_ctx(req); 219 + struct skcipher_request *subreq; 220 + gfp_t gfp; 221 + 222 + subreq = &rctx->subreq; 223 + skcipher_request_set_tfm(subreq, ctx->child); 224 + skcipher_request_set_callback(subreq, req->base.flags, done, req); 225 + 226 + gfp = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL : 227 + GFP_ATOMIC; 228 + rctx->ext = NULL; 229 + 230 + subreq->cryptlen = XTS_BUFFER_SIZE; 231 + if (req->cryptlen > XTS_BUFFER_SIZE) { 232 + subreq->cryptlen = min(req->cryptlen, (unsigned)PAGE_SIZE); 233 + rctx->ext = kmalloc(subreq->cryptlen, gfp); 234 + } 235 + 236 + rctx->src = req->src; 237 + rctx->dst = req->dst; 238 + rctx->left = req->cryptlen; 239 + 240 + /* calculate first value of T */ 241 + crypto_cipher_encrypt_one(ctx->tweak, (u8 *)&rctx->t, req->iv); 242 + 243 + return 0; 244 + } 245 + 246 + static void exit_crypt(struct skcipher_request *req) 247 + { 248 + struct rctx *rctx = skcipher_request_ctx(req); 249 + 250 + rctx->left = 0; 251 + 252 + if (rctx->ext) 253 + kzfree(rctx->ext); 254 + } 255 + 256 + static int do_encrypt(struct skcipher_request *req, int err) 257 + { 258 + struct rctx *rctx = skcipher_request_ctx(req); 259 + struct skcipher_request *subreq; 260 + 261 + subreq = &rctx->subreq; 262 + 263 + while (!err && rctx->left) { 264 + err = pre_crypt(req) ?: 265 + crypto_skcipher_encrypt(subreq) ?: 266 + post_crypt(req); 267 + 268 + if (err == -EINPROGRESS || 269 + (err == -EBUSY && 270 + req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)) 271 + return err; 272 + } 273 + 274 + exit_crypt(req); 275 + return err; 276 + } 277 + 278 + static void encrypt_done(struct crypto_async_request *areq, int err) 279 + { 280 + struct skcipher_request *req = areq->data; 281 + struct skcipher_request *subreq; 282 + struct rctx *rctx; 283 + 284 + rctx = skcipher_request_ctx(req); 285 + subreq = &rctx->subreq; 286 + subreq->base.flags &= CRYPTO_TFM_REQ_MAY_BACKLOG; 287 + 288 + err = do_encrypt(req, err ?: post_crypt(req)); 289 + if (rctx->left) 290 + return; 291 + 292 + skcipher_request_complete(req, err); 293 + } 294 + 295 + static int encrypt(struct skcipher_request *req) 296 + { 297 + return do_encrypt(req, init_crypt(req, encrypt_done)); 298 + } 299 + 300 + static int do_decrypt(struct skcipher_request *req, int err) 301 + { 302 + struct rctx *rctx = skcipher_request_ctx(req); 303 + struct skcipher_request *subreq; 304 + 305 + subreq = &rctx->subreq; 306 + 307 + while (!err && rctx->left) { 308 + err = pre_crypt(req) ?: 309 + crypto_skcipher_decrypt(subreq) ?: 310 + post_crypt(req); 311 + 312 + if (err == -EINPROGRESS || 313 + (err == -EBUSY && 314 + req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)) 315 + return err; 316 + } 317 + 318 + exit_crypt(req); 319 + return err; 320 + } 321 + 322 + static void decrypt_done(struct crypto_async_request *areq, int err) 323 + { 324 + struct skcipher_request *req = areq->data; 325 + struct skcipher_request *subreq; 326 + struct rctx *rctx; 327 + 328 + rctx = skcipher_request_ctx(req); 329 + subreq = &rctx->subreq; 330 + subreq->base.flags &= CRYPTO_TFM_REQ_MAY_BACKLOG; 331 + 332 + err = do_decrypt(req, err ?: post_crypt(req)); 333 + if (rctx->left) 334 + return; 335 + 336 + skcipher_request_complete(req, err); 337 + } 338 + 339 + static int decrypt(struct skcipher_request *req) 340 + { 341 + return do_decrypt(req, init_crypt(req, decrypt_done)); 163 342 } 164 343 165 344 int xts_crypt(struct blkcipher_desc *desc, struct scatterlist *sdst, ··· 414 233 } 415 234 EXPORT_SYMBOL_GPL(xts_crypt); 416 235 417 - static int init_tfm(struct crypto_tfm *tfm) 236 + static int init_tfm(struct crypto_skcipher *tfm) 418 237 { 419 - struct crypto_cipher *cipher; 420 - struct crypto_instance *inst = (void *)tfm->__crt_alg; 421 - struct crypto_spawn *spawn = crypto_instance_ctx(inst); 422 - struct priv *ctx = crypto_tfm_ctx(tfm); 423 - u32 *flags = &tfm->crt_flags; 238 + struct skcipher_instance *inst = skcipher_alg_instance(tfm); 239 + struct xts_instance_ctx *ictx = skcipher_instance_ctx(inst); 240 + struct priv *ctx = crypto_skcipher_ctx(tfm); 241 + struct crypto_skcipher *child; 242 + struct crypto_cipher *tweak; 424 243 425 - cipher = crypto_spawn_cipher(spawn); 426 - if (IS_ERR(cipher)) 427 - return PTR_ERR(cipher); 244 + child = crypto_spawn_skcipher(&ictx->spawn); 245 + if (IS_ERR(child)) 246 + return PTR_ERR(child); 428 247 429 - if (crypto_cipher_blocksize(cipher) != XTS_BLOCK_SIZE) { 430 - *flags |= CRYPTO_TFM_RES_BAD_BLOCK_LEN; 431 - crypto_free_cipher(cipher); 432 - return -EINVAL; 248 + ctx->child = child; 249 + 250 + tweak = crypto_alloc_cipher(ictx->name, 0, 0); 251 + if (IS_ERR(tweak)) { 252 + crypto_free_skcipher(ctx->child); 253 + return PTR_ERR(tweak); 433 254 } 434 255 435 - ctx->child = cipher; 256 + ctx->tweak = tweak; 436 257 437 - cipher = crypto_spawn_cipher(spawn); 438 - if (IS_ERR(cipher)) { 439 - crypto_free_cipher(ctx->child); 440 - return PTR_ERR(cipher); 441 - } 442 - 443 - /* this check isn't really needed, leave it here just in case */ 444 - if (crypto_cipher_blocksize(cipher) != XTS_BLOCK_SIZE) { 445 - crypto_free_cipher(cipher); 446 - crypto_free_cipher(ctx->child); 447 - *flags |= CRYPTO_TFM_RES_BAD_BLOCK_LEN; 448 - return -EINVAL; 449 - } 450 - 451 - ctx->tweak = cipher; 258 + crypto_skcipher_set_reqsize(tfm, crypto_skcipher_reqsize(child) + 259 + sizeof(struct rctx)); 452 260 453 261 return 0; 454 262 } 455 263 456 - static void exit_tfm(struct crypto_tfm *tfm) 264 + static void exit_tfm(struct crypto_skcipher *tfm) 457 265 { 458 - struct priv *ctx = crypto_tfm_ctx(tfm); 459 - crypto_free_cipher(ctx->child); 266 + struct priv *ctx = crypto_skcipher_ctx(tfm); 267 + 268 + crypto_free_skcipher(ctx->child); 460 269 crypto_free_cipher(ctx->tweak); 461 270 } 462 271 463 - static struct crypto_instance *alloc(struct rtattr **tb) 272 + static void free(struct skcipher_instance *inst) 464 273 { 465 - struct crypto_instance *inst; 466 - struct crypto_alg *alg; 467 - int err; 468 - 469 - err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_BLKCIPHER); 470 - if (err) 471 - return ERR_PTR(err); 472 - 473 - alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER, 474 - CRYPTO_ALG_TYPE_MASK); 475 - if (IS_ERR(alg)) 476 - return ERR_CAST(alg); 477 - 478 - inst = crypto_alloc_instance("xts", alg); 479 - if (IS_ERR(inst)) 480 - goto out_put_alg; 481 - 482 - inst->alg.cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER; 483 - inst->alg.cra_priority = alg->cra_priority; 484 - inst->alg.cra_blocksize = alg->cra_blocksize; 485 - 486 - if (alg->cra_alignmask < 7) 487 - inst->alg.cra_alignmask = 7; 488 - else 489 - inst->alg.cra_alignmask = alg->cra_alignmask; 490 - 491 - inst->alg.cra_type = &crypto_blkcipher_type; 492 - 493 - inst->alg.cra_blkcipher.ivsize = alg->cra_blocksize; 494 - inst->alg.cra_blkcipher.min_keysize = 495 - 2 * alg->cra_cipher.cia_min_keysize; 496 - inst->alg.cra_blkcipher.max_keysize = 497 - 2 * alg->cra_cipher.cia_max_keysize; 498 - 499 - inst->alg.cra_ctxsize = sizeof(struct priv); 500 - 501 - inst->alg.cra_init = init_tfm; 502 - inst->alg.cra_exit = exit_tfm; 503 - 504 - inst->alg.cra_blkcipher.setkey = setkey; 505 - inst->alg.cra_blkcipher.encrypt = encrypt; 506 - inst->alg.cra_blkcipher.decrypt = decrypt; 507 - 508 - out_put_alg: 509 - crypto_mod_put(alg); 510 - return inst; 274 + crypto_drop_skcipher(skcipher_instance_ctx(inst)); 275 + kfree(inst); 511 276 } 512 277 513 - static void free(struct crypto_instance *inst) 278 + static int create(struct crypto_template *tmpl, struct rtattr **tb) 514 279 { 515 - crypto_drop_spawn(crypto_instance_ctx(inst)); 280 + struct skcipher_instance *inst; 281 + struct crypto_attr_type *algt; 282 + struct xts_instance_ctx *ctx; 283 + struct skcipher_alg *alg; 284 + const char *cipher_name; 285 + int err; 286 + 287 + algt = crypto_get_attr_type(tb); 288 + if (IS_ERR(algt)) 289 + return PTR_ERR(algt); 290 + 291 + if ((algt->type ^ CRYPTO_ALG_TYPE_SKCIPHER) & algt->mask) 292 + return -EINVAL; 293 + 294 + cipher_name = crypto_attr_alg_name(tb[1]); 295 + if (IS_ERR(cipher_name)) 296 + return PTR_ERR(cipher_name); 297 + 298 + inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL); 299 + if (!inst) 300 + return -ENOMEM; 301 + 302 + ctx = skcipher_instance_ctx(inst); 303 + 304 + crypto_set_skcipher_spawn(&ctx->spawn, skcipher_crypto_instance(inst)); 305 + err = crypto_grab_skcipher(&ctx->spawn, cipher_name, 0, 306 + crypto_requires_sync(algt->type, 307 + algt->mask)); 308 + if (err == -ENOENT) { 309 + err = -ENAMETOOLONG; 310 + if (snprintf(ctx->name, CRYPTO_MAX_ALG_NAME, "ecb(%s)", 311 + cipher_name) >= CRYPTO_MAX_ALG_NAME) 312 + goto err_free_inst; 313 + 314 + err = crypto_grab_skcipher(&ctx->spawn, ctx->name, 0, 315 + crypto_requires_sync(algt->type, 316 + algt->mask)); 317 + } 318 + 319 + if (err) 320 + goto err_free_inst; 321 + 322 + alg = crypto_skcipher_spawn_alg(&ctx->spawn); 323 + 324 + err = -EINVAL; 325 + if (alg->base.cra_blocksize != XTS_BLOCK_SIZE) 326 + goto err_drop_spawn; 327 + 328 + if (crypto_skcipher_alg_ivsize(alg)) 329 + goto err_drop_spawn; 330 + 331 + err = crypto_inst_setname(skcipher_crypto_instance(inst), "xts", 332 + &alg->base); 333 + if (err) 334 + goto err_drop_spawn; 335 + 336 + err = -EINVAL; 337 + cipher_name = alg->base.cra_name; 338 + 339 + /* Alas we screwed up the naming so we have to mangle the 340 + * cipher name. 341 + */ 342 + if (!strncmp(cipher_name, "ecb(", 4)) { 343 + unsigned len; 344 + 345 + len = strlcpy(ctx->name, cipher_name + 4, sizeof(ctx->name)); 346 + if (len < 2 || len >= sizeof(ctx->name)) 347 + goto err_drop_spawn; 348 + 349 + if (ctx->name[len - 1] != ')') 350 + goto err_drop_spawn; 351 + 352 + ctx->name[len - 1] = 0; 353 + 354 + if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME, 355 + "xts(%s)", ctx->name) >= CRYPTO_MAX_ALG_NAME) 356 + return -ENAMETOOLONG; 357 + } else 358 + goto err_drop_spawn; 359 + 360 + inst->alg.base.cra_flags = alg->base.cra_flags & CRYPTO_ALG_ASYNC; 361 + inst->alg.base.cra_priority = alg->base.cra_priority; 362 + inst->alg.base.cra_blocksize = XTS_BLOCK_SIZE; 363 + inst->alg.base.cra_alignmask = alg->base.cra_alignmask | 364 + (__alignof__(u64) - 1); 365 + 366 + inst->alg.ivsize = XTS_BLOCK_SIZE; 367 + inst->alg.min_keysize = crypto_skcipher_alg_min_keysize(alg) * 2; 368 + inst->alg.max_keysize = crypto_skcipher_alg_max_keysize(alg) * 2; 369 + 370 + inst->alg.base.cra_ctxsize = sizeof(struct priv); 371 + 372 + inst->alg.init = init_tfm; 373 + inst->alg.exit = exit_tfm; 374 + 375 + inst->alg.setkey = setkey; 376 + inst->alg.encrypt = encrypt; 377 + inst->alg.decrypt = decrypt; 378 + 379 + inst->free = free; 380 + 381 + err = skcipher_register_instance(tmpl, inst); 382 + if (err) 383 + goto err_drop_spawn; 384 + 385 + out: 386 + return err; 387 + 388 + err_drop_spawn: 389 + crypto_drop_skcipher(&ctx->spawn); 390 + err_free_inst: 516 391 kfree(inst); 392 + goto out; 517 393 } 518 394 519 395 static struct crypto_template crypto_tmpl = { 520 396 .name = "xts", 521 - .alloc = alloc, 522 - .free = free, 397 + .create = create, 523 398 .module = THIS_MODULE, 524 399 }; 525 400
+24 -2
include/crypto/xts.h
··· 2 2 #define _CRYPTO_XTS_H 3 3 4 4 #include <crypto/b128ops.h> 5 - #include <linux/crypto.h> 6 - #include <crypto/algapi.h> 5 + #include <crypto/internal/skcipher.h> 7 6 #include <linux/fips.h> 8 7 9 8 struct scatterlist; ··· 44 45 if (fips_enabled && 45 46 !crypto_memneq(key, key + (keylen / 2), keylen / 2)) { 46 47 *flags |= CRYPTO_TFM_RES_WEAK_KEY; 48 + return -EINVAL; 49 + } 50 + 51 + return 0; 52 + } 53 + 54 + static inline int xts_verify_key(struct crypto_skcipher *tfm, 55 + const u8 *key, unsigned int keylen) 56 + { 57 + /* 58 + * key consists of keys of equal size concatenated, therefore 59 + * the length must be even. 60 + */ 61 + if (keylen % 2) { 62 + crypto_skcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN); 63 + return -EINVAL; 64 + } 65 + 66 + /* ensure that the AES and tweak key are not identical */ 67 + if ((fips_enabled || crypto_skcipher_get_flags(tfm) & 68 + CRYPTO_TFM_REQ_WEAK_KEY) && 69 + !crypto_memneq(key, key + (keylen / 2), keylen / 2)) { 70 + crypto_skcipher_set_flags(tfm, CRYPTO_TFM_RES_WEAK_KEY); 47 71 return -EINVAL; 48 72 } 49 73