Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 1 | /* |
| 2 | * seqiv: Sequence Number IV Generator |
| 3 | * |
| 4 | * This generator generates an IV based on a sequence number by xoring it |
| 5 | * with a salt. This algorithm is mainly useful for CTR and similar modes. |
| 6 | * |
| 7 | * Copyright (c) 2007 Herbert Xu <herbert@gondor.apana.org.au> |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify it |
| 10 | * under the terms of the GNU General Public License as published by the Free |
| 11 | * Software Foundation; either version 2 of the License, or (at your option) |
| 12 | * any later version. |
| 13 | * |
| 14 | */ |
| 15 | |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 16 | #include <crypto/internal/aead.h> |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 17 | #include <crypto/internal/skcipher.h> |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 18 | #include <crypto/null.h> |
Herbert Xu | a0f000e | 2008-08-14 22:21:31 +1000 | [diff] [blame] | 19 | #include <crypto/rng.h> |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 20 | #include <crypto/scatterwalk.h> |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 21 | #include <linux/err.h> |
| 22 | #include <linux/init.h> |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/module.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 25 | #include <linux/slab.h> |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 26 | #include <linux/spinlock.h> |
| 27 | #include <linux/string.h> |
| 28 | |
Herbert Xu | dd04446 | 2015-05-23 15:41:56 +0800 | [diff] [blame] | 29 | struct seqniv_request_ctx { |
| 30 | struct scatterlist dst[2]; |
| 31 | struct aead_request subreq; |
| 32 | }; |
| 33 | |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 34 | struct seqiv_ctx { |
| 35 | spinlock_t lock; |
| 36 | u8 salt[] __attribute__ ((aligned(__alignof__(u32)))); |
| 37 | }; |
| 38 | |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 39 | struct seqiv_aead_ctx { |
| 40 | struct crypto_aead *child; |
| 41 | spinlock_t lock; |
| 42 | struct crypto_blkcipher *null; |
| 43 | u8 salt[] __attribute__ ((aligned(__alignof__(u32)))); |
| 44 | }; |
| 45 | |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 46 | static void seqiv_free(struct crypto_instance *inst); |
| 47 | |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 48 | static int seqiv_aead_setkey(struct crypto_aead *tfm, |
| 49 | const u8 *key, unsigned int keylen) |
| 50 | { |
| 51 | struct seqiv_aead_ctx *ctx = crypto_aead_ctx(tfm); |
| 52 | |
| 53 | return crypto_aead_setkey(ctx->child, key, keylen); |
| 54 | } |
| 55 | |
| 56 | static int seqiv_aead_setauthsize(struct crypto_aead *tfm, |
| 57 | unsigned int authsize) |
| 58 | { |
| 59 | struct seqiv_aead_ctx *ctx = crypto_aead_ctx(tfm); |
| 60 | |
| 61 | return crypto_aead_setauthsize(ctx->child, authsize); |
| 62 | } |
| 63 | |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 64 | static void seqiv_complete2(struct skcipher_givcrypt_request *req, int err) |
| 65 | { |
| 66 | struct ablkcipher_request *subreq = skcipher_givcrypt_reqctx(req); |
| 67 | struct crypto_ablkcipher *geniv; |
| 68 | |
| 69 | if (err == -EINPROGRESS) |
| 70 | return; |
| 71 | |
| 72 | if (err) |
| 73 | goto out; |
| 74 | |
| 75 | geniv = skcipher_givcrypt_reqtfm(req); |
| 76 | memcpy(req->creq.info, subreq->info, crypto_ablkcipher_ivsize(geniv)); |
| 77 | |
| 78 | out: |
| 79 | kfree(subreq->info); |
| 80 | } |
| 81 | |
| 82 | static void seqiv_complete(struct crypto_async_request *base, int err) |
| 83 | { |
| 84 | struct skcipher_givcrypt_request *req = base->data; |
| 85 | |
| 86 | seqiv_complete2(req, err); |
| 87 | skcipher_givcrypt_complete(req, err); |
| 88 | } |
| 89 | |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 90 | static void seqiv_aead_complete2(struct aead_givcrypt_request *req, int err) |
| 91 | { |
| 92 | struct aead_request *subreq = aead_givcrypt_reqctx(req); |
| 93 | struct crypto_aead *geniv; |
| 94 | |
| 95 | if (err == -EINPROGRESS) |
| 96 | return; |
| 97 | |
| 98 | if (err) |
| 99 | goto out; |
| 100 | |
| 101 | geniv = aead_givcrypt_reqtfm(req); |
| 102 | memcpy(req->areq.iv, subreq->iv, crypto_aead_ivsize(geniv)); |
| 103 | |
| 104 | out: |
| 105 | kfree(subreq->iv); |
| 106 | } |
| 107 | |
| 108 | static void seqiv_aead_complete(struct crypto_async_request *base, int err) |
| 109 | { |
| 110 | struct aead_givcrypt_request *req = base->data; |
| 111 | |
| 112 | seqiv_aead_complete2(req, err); |
| 113 | aead_givcrypt_complete(req, err); |
| 114 | } |
| 115 | |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 116 | static void seqiv_aead_encrypt_complete2(struct aead_request *req, int err) |
| 117 | { |
| 118 | struct aead_request *subreq = aead_request_ctx(req); |
| 119 | struct crypto_aead *geniv; |
| 120 | |
| 121 | if (err == -EINPROGRESS) |
| 122 | return; |
| 123 | |
| 124 | if (err) |
| 125 | goto out; |
| 126 | |
| 127 | geniv = crypto_aead_reqtfm(req); |
| 128 | memcpy(req->iv, subreq->iv, crypto_aead_ivsize(geniv)); |
| 129 | |
| 130 | out: |
| 131 | kzfree(subreq->iv); |
| 132 | } |
| 133 | |
| 134 | static void seqiv_aead_encrypt_complete(struct crypto_async_request *base, |
| 135 | int err) |
| 136 | { |
| 137 | struct aead_request *req = base->data; |
| 138 | |
| 139 | seqiv_aead_encrypt_complete2(req, err); |
| 140 | aead_request_complete(req, err); |
| 141 | } |
| 142 | |
Herbert Xu | dd04446 | 2015-05-23 15:41:56 +0800 | [diff] [blame] | 143 | static void seqniv_aead_encrypt_complete2(struct aead_request *req, int err) |
| 144 | { |
| 145 | unsigned int ivsize = 8; |
| 146 | u8 data[20]; |
| 147 | |
| 148 | if (err == -EINPROGRESS) |
| 149 | return; |
| 150 | |
| 151 | /* Swap IV and ESP header back to correct order. */ |
| 152 | scatterwalk_map_and_copy(data, req->dst, 0, req->assoclen + ivsize, 0); |
| 153 | scatterwalk_map_and_copy(data + ivsize, req->dst, 0, req->assoclen, 1); |
| 154 | scatterwalk_map_and_copy(data, req->dst, req->assoclen, ivsize, 1); |
| 155 | } |
| 156 | |
| 157 | static void seqniv_aead_encrypt_complete(struct crypto_async_request *base, |
| 158 | int err) |
| 159 | { |
| 160 | struct aead_request *req = base->data; |
| 161 | |
| 162 | seqniv_aead_encrypt_complete2(req, err); |
| 163 | aead_request_complete(req, err); |
| 164 | } |
| 165 | |
| 166 | static void seqniv_aead_decrypt_complete2(struct aead_request *req, int err) |
| 167 | { |
| 168 | u8 data[4]; |
| 169 | |
| 170 | if (err == -EINPROGRESS) |
| 171 | return; |
| 172 | |
| 173 | /* Move ESP header back to correct location. */ |
| 174 | scatterwalk_map_and_copy(data, req->dst, 16, req->assoclen - 8, 0); |
| 175 | scatterwalk_map_and_copy(data, req->dst, 8, req->assoclen - 8, 1); |
| 176 | } |
| 177 | |
| 178 | static void seqniv_aead_decrypt_complete(struct crypto_async_request *base, |
| 179 | int err) |
| 180 | { |
| 181 | struct aead_request *req = base->data; |
| 182 | |
| 183 | seqniv_aead_decrypt_complete2(req, err); |
| 184 | aead_request_complete(req, err); |
| 185 | } |
| 186 | |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 187 | static void seqiv_geniv(struct seqiv_ctx *ctx, u8 *info, u64 seq, |
| 188 | unsigned int ivsize) |
| 189 | { |
| 190 | unsigned int len = ivsize; |
| 191 | |
| 192 | if (ivsize > sizeof(u64)) { |
| 193 | memset(info, 0, ivsize - sizeof(u64)); |
| 194 | len = sizeof(u64); |
| 195 | } |
| 196 | seq = cpu_to_be64(seq); |
| 197 | memcpy(info + ivsize - len, &seq, len); |
| 198 | crypto_xor(info, ctx->salt, ivsize); |
| 199 | } |
| 200 | |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 201 | static int seqiv_givencrypt(struct skcipher_givcrypt_request *req) |
| 202 | { |
| 203 | struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req); |
| 204 | struct seqiv_ctx *ctx = crypto_ablkcipher_ctx(geniv); |
| 205 | struct ablkcipher_request *subreq = skcipher_givcrypt_reqctx(req); |
Mark Rustad | 3e3dc25 | 2014-07-25 02:53:38 -0700 | [diff] [blame] | 206 | crypto_completion_t compl; |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 207 | void *data; |
| 208 | u8 *info; |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 209 | unsigned int ivsize; |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 210 | int err; |
| 211 | |
| 212 | ablkcipher_request_set_tfm(subreq, skcipher_geniv_cipher(geniv)); |
| 213 | |
Mark Rustad | 3e3dc25 | 2014-07-25 02:53:38 -0700 | [diff] [blame] | 214 | compl = req->creq.base.complete; |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 215 | data = req->creq.base.data; |
| 216 | info = req->creq.info; |
| 217 | |
| 218 | ivsize = crypto_ablkcipher_ivsize(geniv); |
| 219 | |
| 220 | if (unlikely(!IS_ALIGNED((unsigned long)info, |
| 221 | crypto_ablkcipher_alignmask(geniv) + 1))) { |
| 222 | info = kmalloc(ivsize, req->creq.base.flags & |
| 223 | CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL: |
| 224 | GFP_ATOMIC); |
| 225 | if (!info) |
| 226 | return -ENOMEM; |
| 227 | |
Mark Rustad | 3e3dc25 | 2014-07-25 02:53:38 -0700 | [diff] [blame] | 228 | compl = seqiv_complete; |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 229 | data = req; |
| 230 | } |
| 231 | |
Mark Rustad | 3e3dc25 | 2014-07-25 02:53:38 -0700 | [diff] [blame] | 232 | ablkcipher_request_set_callback(subreq, req->creq.base.flags, compl, |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 233 | data); |
| 234 | ablkcipher_request_set_crypt(subreq, req->creq.src, req->creq.dst, |
| 235 | req->creq.nbytes, info); |
| 236 | |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 237 | seqiv_geniv(ctx, info, req->seq, ivsize); |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 238 | memcpy(req->giv, info, ivsize); |
| 239 | |
| 240 | err = crypto_ablkcipher_encrypt(subreq); |
| 241 | if (unlikely(info != req->creq.info)) |
| 242 | seqiv_complete2(req, err); |
| 243 | return err; |
| 244 | } |
| 245 | |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 246 | static int seqiv_aead_givencrypt(struct aead_givcrypt_request *req) |
| 247 | { |
| 248 | struct crypto_aead *geniv = aead_givcrypt_reqtfm(req); |
| 249 | struct seqiv_ctx *ctx = crypto_aead_ctx(geniv); |
| 250 | struct aead_request *areq = &req->areq; |
| 251 | struct aead_request *subreq = aead_givcrypt_reqctx(req); |
Mark Rustad | 3e3dc25 | 2014-07-25 02:53:38 -0700 | [diff] [blame] | 252 | crypto_completion_t compl; |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 253 | void *data; |
| 254 | u8 *info; |
| 255 | unsigned int ivsize; |
| 256 | int err; |
| 257 | |
| 258 | aead_request_set_tfm(subreq, aead_geniv_base(geniv)); |
| 259 | |
Mark Rustad | 3e3dc25 | 2014-07-25 02:53:38 -0700 | [diff] [blame] | 260 | compl = areq->base.complete; |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 261 | data = areq->base.data; |
| 262 | info = areq->iv; |
| 263 | |
| 264 | ivsize = crypto_aead_ivsize(geniv); |
| 265 | |
| 266 | if (unlikely(!IS_ALIGNED((unsigned long)info, |
| 267 | crypto_aead_alignmask(geniv) + 1))) { |
| 268 | info = kmalloc(ivsize, areq->base.flags & |
| 269 | CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL: |
| 270 | GFP_ATOMIC); |
| 271 | if (!info) |
| 272 | return -ENOMEM; |
| 273 | |
Mark Rustad | 3e3dc25 | 2014-07-25 02:53:38 -0700 | [diff] [blame] | 274 | compl = seqiv_aead_complete; |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 275 | data = req; |
| 276 | } |
| 277 | |
Mark Rustad | 3e3dc25 | 2014-07-25 02:53:38 -0700 | [diff] [blame] | 278 | aead_request_set_callback(subreq, areq->base.flags, compl, data); |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 279 | aead_request_set_crypt(subreq, areq->src, areq->dst, areq->cryptlen, |
| 280 | info); |
| 281 | aead_request_set_assoc(subreq, areq->assoc, areq->assoclen); |
| 282 | |
| 283 | seqiv_geniv(ctx, info, req->seq, ivsize); |
| 284 | memcpy(req->giv, info, ivsize); |
| 285 | |
| 286 | err = crypto_aead_encrypt(subreq); |
| 287 | if (unlikely(info != areq->iv)) |
| 288 | seqiv_aead_complete2(req, err); |
| 289 | return err; |
| 290 | } |
| 291 | |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 292 | static int seqiv_aead_encrypt_compat(struct aead_request *req) |
| 293 | { |
| 294 | struct crypto_aead *geniv = crypto_aead_reqtfm(req); |
| 295 | struct seqiv_aead_ctx *ctx = crypto_aead_ctx(geniv); |
Herbert Xu | dd04446 | 2015-05-23 15:41:56 +0800 | [diff] [blame] | 296 | struct seqniv_request_ctx *rctx = aead_request_ctx(req); |
| 297 | struct aead_request *subreq = &rctx->subreq; |
| 298 | struct scatterlist *dst; |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 299 | crypto_completion_t compl; |
| 300 | void *data; |
Herbert Xu | dd04446 | 2015-05-23 15:41:56 +0800 | [diff] [blame] | 301 | unsigned int ivsize = 8; |
| 302 | u8 buf[20] __attribute__ ((aligned(__alignof__(u32)))); |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 303 | int err; |
| 304 | |
Herbert Xu | dd04446 | 2015-05-23 15:41:56 +0800 | [diff] [blame] | 305 | if (req->cryptlen < ivsize) |
| 306 | return -EINVAL; |
| 307 | |
| 308 | /* ESP AD is at most 12 bytes (ESN). */ |
| 309 | if (req->assoclen > 12) |
| 310 | return -EINVAL; |
| 311 | |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 312 | aead_request_set_tfm(subreq, ctx->child); |
| 313 | |
Herbert Xu | dd04446 | 2015-05-23 15:41:56 +0800 | [diff] [blame] | 314 | compl = seqniv_aead_encrypt_complete; |
| 315 | data = req; |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 316 | |
Herbert Xu | dd04446 | 2015-05-23 15:41:56 +0800 | [diff] [blame] | 317 | if (req->src != req->dst) { |
Herbert Xu | dd04446 | 2015-05-23 15:41:56 +0800 | [diff] [blame] | 318 | struct blkcipher_desc desc = { |
| 319 | .tfm = ctx->null, |
| 320 | }; |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 321 | |
Herbert Xu | d0ad1b2 | 2015-05-27 14:37:36 +0800 | [diff] [blame^] | 322 | err = crypto_blkcipher_encrypt(&desc, req->dst, req->src, |
| 323 | req->assoclen + req->cryptlen); |
Herbert Xu | dd04446 | 2015-05-23 15:41:56 +0800 | [diff] [blame] | 324 | if (err) |
| 325 | return err; |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 326 | } |
| 327 | |
Herbert Xu | dd04446 | 2015-05-23 15:41:56 +0800 | [diff] [blame] | 328 | dst = scatterwalk_ffwd(rctx->dst, req->dst, ivsize); |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 329 | |
Herbert Xu | dd04446 | 2015-05-23 15:41:56 +0800 | [diff] [blame] | 330 | aead_request_set_callback(subreq, req->base.flags, compl, data); |
| 331 | aead_request_set_crypt(subreq, dst, dst, |
| 332 | req->cryptlen - ivsize, req->iv); |
Herbert Xu | 374d4ad | 2015-05-23 15:41:57 +0800 | [diff] [blame] | 333 | aead_request_set_ad(subreq, req->assoclen); |
Herbert Xu | dd04446 | 2015-05-23 15:41:56 +0800 | [diff] [blame] | 334 | |
| 335 | memcpy(buf, req->iv, ivsize); |
| 336 | crypto_xor(buf, ctx->salt, ivsize); |
| 337 | memcpy(req->iv, buf, ivsize); |
| 338 | |
| 339 | /* Swap order of IV and ESP AD for ICV generation. */ |
| 340 | scatterwalk_map_and_copy(buf + ivsize, req->dst, 0, req->assoclen, 0); |
| 341 | scatterwalk_map_and_copy(buf, req->dst, 0, req->assoclen + ivsize, 1); |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 342 | |
| 343 | err = crypto_aead_encrypt(subreq); |
Herbert Xu | dd04446 | 2015-05-23 15:41:56 +0800 | [diff] [blame] | 344 | seqniv_aead_encrypt_complete2(req, err); |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 345 | return err; |
| 346 | } |
| 347 | |
| 348 | static int seqiv_aead_encrypt(struct aead_request *req) |
| 349 | { |
| 350 | struct crypto_aead *geniv = crypto_aead_reqtfm(req); |
| 351 | struct seqiv_aead_ctx *ctx = crypto_aead_ctx(geniv); |
| 352 | struct aead_request *subreq = aead_request_ctx(req); |
| 353 | crypto_completion_t compl; |
| 354 | void *data; |
| 355 | u8 *info; |
Herbert Xu | dd04446 | 2015-05-23 15:41:56 +0800 | [diff] [blame] | 356 | unsigned int ivsize = 8; |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 357 | int err; |
| 358 | |
Herbert Xu | dd04446 | 2015-05-23 15:41:56 +0800 | [diff] [blame] | 359 | if (req->cryptlen < ivsize) |
| 360 | return -EINVAL; |
| 361 | |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 362 | aead_request_set_tfm(subreq, ctx->child); |
| 363 | |
| 364 | compl = req->base.complete; |
| 365 | data = req->base.data; |
| 366 | info = req->iv; |
| 367 | |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 368 | if (req->src != req->dst) { |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 369 | struct blkcipher_desc desc = { |
| 370 | .tfm = ctx->null, |
| 371 | }; |
| 372 | |
Herbert Xu | d0ad1b2 | 2015-05-27 14:37:36 +0800 | [diff] [blame^] | 373 | err = crypto_blkcipher_encrypt(&desc, req->dst, req->src, |
| 374 | req->assoclen + req->cryptlen); |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 375 | if (err) |
| 376 | return err; |
| 377 | } |
| 378 | |
| 379 | if (unlikely(!IS_ALIGNED((unsigned long)info, |
| 380 | crypto_aead_alignmask(geniv) + 1))) { |
| 381 | info = kmalloc(ivsize, req->base.flags & |
| 382 | CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL: |
| 383 | GFP_ATOMIC); |
| 384 | if (!info) |
| 385 | return -ENOMEM; |
| 386 | |
| 387 | memcpy(info, req->iv, ivsize); |
| 388 | compl = seqiv_aead_encrypt_complete; |
| 389 | data = req; |
| 390 | } |
| 391 | |
| 392 | aead_request_set_callback(subreq, req->base.flags, compl, data); |
| 393 | aead_request_set_crypt(subreq, req->dst, req->dst, |
| 394 | req->cryptlen - ivsize, info); |
Herbert Xu | 374d4ad | 2015-05-23 15:41:57 +0800 | [diff] [blame] | 395 | aead_request_set_ad(subreq, req->assoclen + ivsize); |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 396 | |
| 397 | crypto_xor(info, ctx->salt, ivsize); |
| 398 | scatterwalk_map_and_copy(info, req->dst, req->assoclen, ivsize, 1); |
| 399 | |
| 400 | err = crypto_aead_encrypt(subreq); |
| 401 | if (unlikely(info != req->iv)) |
| 402 | seqiv_aead_encrypt_complete2(req, err); |
| 403 | return err; |
| 404 | } |
| 405 | |
| 406 | static int seqiv_aead_decrypt_compat(struct aead_request *req) |
| 407 | { |
| 408 | struct crypto_aead *geniv = crypto_aead_reqtfm(req); |
| 409 | struct seqiv_aead_ctx *ctx = crypto_aead_ctx(geniv); |
Herbert Xu | dd04446 | 2015-05-23 15:41:56 +0800 | [diff] [blame] | 410 | struct seqniv_request_ctx *rctx = aead_request_ctx(req); |
| 411 | struct aead_request *subreq = &rctx->subreq; |
| 412 | struct scatterlist *dst; |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 413 | crypto_completion_t compl; |
| 414 | void *data; |
Herbert Xu | dd04446 | 2015-05-23 15:41:56 +0800 | [diff] [blame] | 415 | unsigned int ivsize = 8; |
| 416 | u8 buf[20]; |
| 417 | int err; |
| 418 | |
| 419 | if (req->cryptlen < ivsize + crypto_aead_authsize(geniv)) |
| 420 | return -EINVAL; |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 421 | |
| 422 | aead_request_set_tfm(subreq, ctx->child); |
| 423 | |
| 424 | compl = req->base.complete; |
| 425 | data = req->base.data; |
| 426 | |
Herbert Xu | dd04446 | 2015-05-23 15:41:56 +0800 | [diff] [blame] | 427 | if (req->assoclen > 12) |
| 428 | return -EINVAL; |
| 429 | else if (req->assoclen > 8) { |
| 430 | compl = seqniv_aead_decrypt_complete; |
| 431 | data = req; |
| 432 | } |
| 433 | |
| 434 | if (req->src != req->dst) { |
Herbert Xu | dd04446 | 2015-05-23 15:41:56 +0800 | [diff] [blame] | 435 | struct blkcipher_desc desc = { |
| 436 | .tfm = ctx->null, |
| 437 | }; |
| 438 | |
Herbert Xu | d0ad1b2 | 2015-05-27 14:37:36 +0800 | [diff] [blame^] | 439 | err = crypto_blkcipher_encrypt(&desc, req->dst, req->src, |
| 440 | req->assoclen + req->cryptlen); |
Herbert Xu | dd04446 | 2015-05-23 15:41:56 +0800 | [diff] [blame] | 441 | if (err) |
| 442 | return err; |
| 443 | } |
| 444 | |
| 445 | /* Move ESP AD forward for ICV generation. */ |
| 446 | scatterwalk_map_and_copy(buf, req->dst, 0, req->assoclen + ivsize, 0); |
| 447 | memcpy(req->iv, buf + req->assoclen, ivsize); |
| 448 | scatterwalk_map_and_copy(buf, req->dst, ivsize, req->assoclen, 1); |
| 449 | |
| 450 | dst = scatterwalk_ffwd(rctx->dst, req->dst, ivsize); |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 451 | |
| 452 | aead_request_set_callback(subreq, req->base.flags, compl, data); |
Herbert Xu | dd04446 | 2015-05-23 15:41:56 +0800 | [diff] [blame] | 453 | aead_request_set_crypt(subreq, dst, dst, |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 454 | req->cryptlen - ivsize, req->iv); |
Herbert Xu | 374d4ad | 2015-05-23 15:41:57 +0800 | [diff] [blame] | 455 | aead_request_set_ad(subreq, req->assoclen); |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 456 | |
Herbert Xu | dd04446 | 2015-05-23 15:41:56 +0800 | [diff] [blame] | 457 | err = crypto_aead_decrypt(subreq); |
| 458 | if (req->assoclen > 8) |
| 459 | seqniv_aead_decrypt_complete2(req, err); |
| 460 | return err; |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | static int seqiv_aead_decrypt(struct aead_request *req) |
| 464 | { |
| 465 | struct crypto_aead *geniv = crypto_aead_reqtfm(req); |
| 466 | struct seqiv_aead_ctx *ctx = crypto_aead_ctx(geniv); |
| 467 | struct aead_request *subreq = aead_request_ctx(req); |
| 468 | crypto_completion_t compl; |
| 469 | void *data; |
Herbert Xu | dd04446 | 2015-05-23 15:41:56 +0800 | [diff] [blame] | 470 | unsigned int ivsize = 8; |
| 471 | |
| 472 | if (req->cryptlen < ivsize + crypto_aead_authsize(geniv)) |
| 473 | return -EINVAL; |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 474 | |
| 475 | aead_request_set_tfm(subreq, ctx->child); |
| 476 | |
| 477 | compl = req->base.complete; |
| 478 | data = req->base.data; |
| 479 | |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 480 | aead_request_set_callback(subreq, req->base.flags, compl, data); |
| 481 | aead_request_set_crypt(subreq, req->src, req->dst, |
| 482 | req->cryptlen - ivsize, req->iv); |
Herbert Xu | 374d4ad | 2015-05-23 15:41:57 +0800 | [diff] [blame] | 483 | aead_request_set_ad(subreq, req->assoclen + ivsize); |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 484 | |
| 485 | scatterwalk_map_and_copy(req->iv, req->src, req->assoclen, ivsize, 0); |
| 486 | if (req->src != req->dst) |
| 487 | scatterwalk_map_and_copy(req->iv, req->dst, |
| 488 | req->assoclen, ivsize, 1); |
| 489 | |
| 490 | return crypto_aead_decrypt(subreq); |
| 491 | } |
| 492 | |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 493 | static int seqiv_givencrypt_first(struct skcipher_givcrypt_request *req) |
| 494 | { |
| 495 | struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req); |
| 496 | struct seqiv_ctx *ctx = crypto_ablkcipher_ctx(geniv); |
Herbert Xu | a0f000e | 2008-08-14 22:21:31 +1000 | [diff] [blame] | 497 | int err = 0; |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 498 | |
| 499 | spin_lock_bh(&ctx->lock); |
| 500 | if (crypto_ablkcipher_crt(geniv)->givencrypt != seqiv_givencrypt_first) |
| 501 | goto unlock; |
| 502 | |
| 503 | crypto_ablkcipher_crt(geniv)->givencrypt = seqiv_givencrypt; |
Herbert Xu | a0f000e | 2008-08-14 22:21:31 +1000 | [diff] [blame] | 504 | err = crypto_rng_get_bytes(crypto_default_rng, ctx->salt, |
| 505 | crypto_ablkcipher_ivsize(geniv)); |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 506 | |
| 507 | unlock: |
| 508 | spin_unlock_bh(&ctx->lock); |
| 509 | |
Herbert Xu | a0f000e | 2008-08-14 22:21:31 +1000 | [diff] [blame] | 510 | if (err) |
| 511 | return err; |
| 512 | |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 513 | return seqiv_givencrypt(req); |
| 514 | } |
| 515 | |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 516 | static int seqiv_aead_givencrypt_first(struct aead_givcrypt_request *req) |
| 517 | { |
| 518 | struct crypto_aead *geniv = aead_givcrypt_reqtfm(req); |
| 519 | struct seqiv_ctx *ctx = crypto_aead_ctx(geniv); |
Herbert Xu | a0f000e | 2008-08-14 22:21:31 +1000 | [diff] [blame] | 520 | int err = 0; |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 521 | |
| 522 | spin_lock_bh(&ctx->lock); |
| 523 | if (crypto_aead_crt(geniv)->givencrypt != seqiv_aead_givencrypt_first) |
| 524 | goto unlock; |
| 525 | |
| 526 | crypto_aead_crt(geniv)->givencrypt = seqiv_aead_givencrypt; |
Herbert Xu | a0f000e | 2008-08-14 22:21:31 +1000 | [diff] [blame] | 527 | err = crypto_rng_get_bytes(crypto_default_rng, ctx->salt, |
| 528 | crypto_aead_ivsize(geniv)); |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 529 | |
| 530 | unlock: |
| 531 | spin_unlock_bh(&ctx->lock); |
| 532 | |
Herbert Xu | a0f000e | 2008-08-14 22:21:31 +1000 | [diff] [blame] | 533 | if (err) |
| 534 | return err; |
| 535 | |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 536 | return seqiv_aead_givencrypt(req); |
| 537 | } |
| 538 | |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 539 | static int seqiv_aead_encrypt_compat_first(struct aead_request *req) |
| 540 | { |
| 541 | struct crypto_aead *geniv = crypto_aead_reqtfm(req); |
| 542 | struct seqiv_aead_ctx *ctx = crypto_aead_ctx(geniv); |
| 543 | int err = 0; |
| 544 | |
| 545 | spin_lock_bh(&ctx->lock); |
| 546 | if (geniv->encrypt != seqiv_aead_encrypt_compat_first) |
| 547 | goto unlock; |
| 548 | |
| 549 | geniv->encrypt = seqiv_aead_encrypt_compat; |
| 550 | err = crypto_rng_get_bytes(crypto_default_rng, ctx->salt, |
| 551 | crypto_aead_ivsize(geniv)); |
| 552 | |
| 553 | unlock: |
| 554 | spin_unlock_bh(&ctx->lock); |
| 555 | |
| 556 | if (err) |
| 557 | return err; |
| 558 | |
| 559 | return seqiv_aead_encrypt_compat(req); |
| 560 | } |
| 561 | |
| 562 | static int seqiv_aead_encrypt_first(struct aead_request *req) |
| 563 | { |
| 564 | struct crypto_aead *geniv = crypto_aead_reqtfm(req); |
| 565 | struct seqiv_aead_ctx *ctx = crypto_aead_ctx(geniv); |
| 566 | int err = 0; |
| 567 | |
| 568 | spin_lock_bh(&ctx->lock); |
| 569 | if (geniv->encrypt != seqiv_aead_encrypt_first) |
| 570 | goto unlock; |
| 571 | |
| 572 | geniv->encrypt = seqiv_aead_encrypt; |
| 573 | err = crypto_rng_get_bytes(crypto_default_rng, ctx->salt, |
| 574 | crypto_aead_ivsize(geniv)); |
| 575 | |
| 576 | unlock: |
| 577 | spin_unlock_bh(&ctx->lock); |
| 578 | |
| 579 | if (err) |
| 580 | return err; |
| 581 | |
| 582 | return seqiv_aead_encrypt(req); |
| 583 | } |
| 584 | |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 585 | static int seqiv_init(struct crypto_tfm *tfm) |
| 586 | { |
| 587 | struct crypto_ablkcipher *geniv = __crypto_ablkcipher_cast(tfm); |
| 588 | struct seqiv_ctx *ctx = crypto_ablkcipher_ctx(geniv); |
| 589 | |
| 590 | spin_lock_init(&ctx->lock); |
| 591 | |
| 592 | tfm->crt_ablkcipher.reqsize = sizeof(struct ablkcipher_request); |
| 593 | |
| 594 | return skcipher_geniv_init(tfm); |
| 595 | } |
| 596 | |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 597 | static int seqiv_old_aead_init(struct crypto_tfm *tfm) |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 598 | { |
| 599 | struct crypto_aead *geniv = __crypto_aead_cast(tfm); |
| 600 | struct seqiv_ctx *ctx = crypto_aead_ctx(geniv); |
| 601 | |
| 602 | spin_lock_init(&ctx->lock); |
| 603 | |
Herbert Xu | ba6d8e3 | 2015-05-11 17:48:03 +0800 | [diff] [blame] | 604 | crypto_aead_set_reqsize(__crypto_aead_cast(tfm), |
| 605 | sizeof(struct aead_request)); |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 606 | |
| 607 | return aead_geniv_init(tfm); |
| 608 | } |
| 609 | |
Herbert Xu | dd04446 | 2015-05-23 15:41:56 +0800 | [diff] [blame] | 610 | static int seqiv_aead_init_common(struct crypto_tfm *tfm, unsigned int reqsize) |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 611 | { |
| 612 | struct crypto_aead *geniv = __crypto_aead_cast(tfm); |
| 613 | struct seqiv_aead_ctx *ctx = crypto_aead_ctx(geniv); |
| 614 | int err; |
| 615 | |
| 616 | spin_lock_init(&ctx->lock); |
| 617 | |
| 618 | crypto_aead_set_reqsize(geniv, sizeof(struct aead_request)); |
| 619 | |
| 620 | ctx->null = crypto_get_default_null_skcipher(); |
| 621 | err = PTR_ERR(ctx->null); |
| 622 | if (IS_ERR(ctx->null)) |
| 623 | goto out; |
| 624 | |
| 625 | err = aead_geniv_init(tfm); |
| 626 | if (err) |
| 627 | goto drop_null; |
| 628 | |
| 629 | ctx->child = geniv->child; |
| 630 | geniv->child = geniv; |
| 631 | |
| 632 | out: |
| 633 | return err; |
| 634 | |
| 635 | drop_null: |
| 636 | crypto_put_default_null_skcipher(); |
| 637 | goto out; |
| 638 | } |
| 639 | |
Herbert Xu | dd04446 | 2015-05-23 15:41:56 +0800 | [diff] [blame] | 640 | static int seqiv_aead_init(struct crypto_tfm *tfm) |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 641 | { |
Herbert Xu | dd04446 | 2015-05-23 15:41:56 +0800 | [diff] [blame] | 642 | return seqiv_aead_init_common(tfm, sizeof(struct aead_request)); |
| 643 | } |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 644 | |
Herbert Xu | dd04446 | 2015-05-23 15:41:56 +0800 | [diff] [blame] | 645 | static int seqniv_aead_init(struct crypto_tfm *tfm) |
| 646 | { |
| 647 | return seqiv_aead_init_common(tfm, sizeof(struct seqniv_request_ctx)); |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | static void seqiv_aead_exit(struct crypto_tfm *tfm) |
| 651 | { |
| 652 | struct seqiv_aead_ctx *ctx = crypto_tfm_ctx(tfm); |
| 653 | |
| 654 | crypto_free_aead(ctx->child); |
| 655 | crypto_put_default_null_skcipher(); |
| 656 | } |
| 657 | |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 658 | static int seqiv_ablkcipher_create(struct crypto_template *tmpl, |
| 659 | struct rtattr **tb) |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 660 | { |
| 661 | struct crypto_instance *inst; |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 662 | int err; |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 663 | |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 664 | inst = skcipher_geniv_alloc(tmpl, tb, 0, 0); |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 665 | |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 666 | if (IS_ERR(inst)) |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 667 | return PTR_ERR(inst); |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 668 | |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 669 | err = -EINVAL; |
| 670 | if (inst->alg.cra_ablkcipher.ivsize < sizeof(u64)) |
| 671 | goto free_inst; |
Herbert Xu | c0ecf89 | 2015-01-16 19:51:20 +1100 | [diff] [blame] | 672 | |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 673 | inst->alg.cra_ablkcipher.givencrypt = seqiv_givencrypt_first; |
| 674 | |
| 675 | inst->alg.cra_init = seqiv_init; |
| 676 | inst->alg.cra_exit = skcipher_geniv_exit; |
| 677 | |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 678 | inst->alg.cra_ctxsize += inst->alg.cra_ablkcipher.ivsize; |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 679 | inst->alg.cra_ctxsize += sizeof(struct seqiv_ctx); |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 680 | |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 681 | inst->alg.cra_alignmask |= __alignof__(u32) - 1; |
| 682 | |
| 683 | err = crypto_register_instance(tmpl, inst); |
| 684 | if (err) |
| 685 | goto free_inst; |
| 686 | |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 687 | out: |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 688 | return err; |
| 689 | |
| 690 | free_inst: |
| 691 | skcipher_geniv_free(inst); |
| 692 | goto out; |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 693 | } |
| 694 | |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 695 | static int seqiv_old_aead_create(struct crypto_template *tmpl, |
| 696 | struct aead_instance *aead) |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 697 | { |
| 698 | struct crypto_instance *inst = aead_crypto_instance(aead); |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 699 | int err = -EINVAL; |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 700 | |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 701 | if (inst->alg.cra_aead.ivsize < sizeof(u64)) |
| 702 | goto free_inst; |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 703 | |
| 704 | inst->alg.cra_aead.givencrypt = seqiv_aead_givencrypt_first; |
| 705 | |
| 706 | inst->alg.cra_init = seqiv_old_aead_init; |
| 707 | inst->alg.cra_exit = aead_geniv_exit; |
| 708 | |
| 709 | inst->alg.cra_ctxsize = inst->alg.cra_aead.ivsize; |
| 710 | inst->alg.cra_ctxsize += sizeof(struct seqiv_ctx); |
| 711 | |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 712 | err = crypto_register_instance(tmpl, inst); |
| 713 | if (err) |
| 714 | goto free_inst; |
| 715 | |
| 716 | out: |
| 717 | return err; |
| 718 | |
| 719 | free_inst: |
| 720 | aead_geniv_free(aead); |
| 721 | goto out; |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 722 | } |
| 723 | |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 724 | static int seqiv_aead_create(struct crypto_template *tmpl, struct rtattr **tb) |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 725 | { |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 726 | struct aead_instance *inst; |
| 727 | struct crypto_aead_spawn *spawn; |
| 728 | struct aead_alg *alg; |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 729 | int err; |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 730 | |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 731 | inst = aead_geniv_alloc(tmpl, tb, 0, 0); |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 732 | |
| 733 | if (IS_ERR(inst)) |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 734 | return PTR_ERR(inst); |
| 735 | |
| 736 | inst->alg.base.cra_alignmask |= __alignof__(u32) - 1; |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 737 | |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 738 | if (inst->alg.base.cra_aead.encrypt) |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 739 | return seqiv_old_aead_create(tmpl, inst); |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 740 | |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 741 | err = -EINVAL; |
Herbert Xu | dd04446 | 2015-05-23 15:41:56 +0800 | [diff] [blame] | 742 | if (inst->alg.ivsize != sizeof(u64)) |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 743 | goto free_inst; |
Herbert Xu | c0ecf89 | 2015-01-16 19:51:20 +1100 | [diff] [blame] | 744 | |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 745 | spawn = aead_instance_ctx(inst); |
| 746 | alg = crypto_spawn_aead_alg(spawn); |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 747 | |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 748 | inst->alg.setkey = seqiv_aead_setkey; |
| 749 | inst->alg.setauthsize = seqiv_aead_setauthsize; |
| 750 | inst->alg.encrypt = seqiv_aead_encrypt_first; |
| 751 | inst->alg.decrypt = seqiv_aead_decrypt; |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 752 | |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 753 | inst->alg.base.cra_init = seqiv_aead_init; |
| 754 | inst->alg.base.cra_exit = seqiv_aead_exit; |
| 755 | |
| 756 | inst->alg.base.cra_ctxsize = sizeof(struct seqiv_aead_ctx); |
| 757 | inst->alg.base.cra_ctxsize += inst->alg.base.cra_aead.ivsize; |
| 758 | |
| 759 | if (alg->base.cra_aead.encrypt) { |
| 760 | inst->alg.encrypt = seqiv_aead_encrypt_compat_first; |
| 761 | inst->alg.decrypt = seqiv_aead_decrypt_compat; |
| 762 | |
Herbert Xu | dd04446 | 2015-05-23 15:41:56 +0800 | [diff] [blame] | 763 | inst->alg.base.cra_init = seqniv_aead_init; |
| 764 | inst->alg.base.cra_exit = seqiv_aead_exit; |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 765 | } |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 766 | |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 767 | err = aead_register_instance(tmpl, inst); |
| 768 | if (err) |
| 769 | goto free_inst; |
| 770 | |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 771 | out: |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 772 | return err; |
| 773 | |
| 774 | free_inst: |
| 775 | aead_geniv_free(inst); |
| 776 | goto out; |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 777 | } |
| 778 | |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 779 | static int seqiv_create(struct crypto_template *tmpl, struct rtattr **tb) |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 780 | { |
| 781 | struct crypto_attr_type *algt; |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 782 | int err; |
| 783 | |
| 784 | algt = crypto_get_attr_type(tb); |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 785 | if (IS_ERR(algt)) |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 786 | return PTR_ERR(algt); |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 787 | |
Herbert Xu | a0f000e | 2008-08-14 22:21:31 +1000 | [diff] [blame] | 788 | err = crypto_get_default_rng(); |
| 789 | if (err) |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 790 | return err; |
Herbert Xu | a0f000e | 2008-08-14 22:21:31 +1000 | [diff] [blame] | 791 | |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 792 | if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & CRYPTO_ALG_TYPE_MASK) |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 793 | err = seqiv_ablkcipher_create(tmpl, tb); |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 794 | else |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 795 | err = seqiv_aead_create(tmpl, tb); |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 796 | |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 797 | if (err) |
| 798 | crypto_put_default_rng(); |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 799 | |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 800 | return err; |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 801 | } |
| 802 | |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 803 | static int seqniv_create(struct crypto_template *tmpl, struct rtattr **tb) |
Herbert Xu | 3c08fee | 2015-05-21 15:11:14 +0800 | [diff] [blame] | 804 | { |
| 805 | struct aead_instance *inst; |
| 806 | struct crypto_aead_spawn *spawn; |
| 807 | struct aead_alg *alg; |
| 808 | int err; |
| 809 | |
| 810 | err = crypto_get_default_rng(); |
| 811 | if (err) |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 812 | return err; |
Herbert Xu | 3c08fee | 2015-05-21 15:11:14 +0800 | [diff] [blame] | 813 | |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 814 | inst = aead_geniv_alloc(tmpl, tb, 0, 0); |
| 815 | err = PTR_ERR(inst); |
Herbert Xu | 3c08fee | 2015-05-21 15:11:14 +0800 | [diff] [blame] | 816 | if (IS_ERR(inst)) |
| 817 | goto put_rng; |
| 818 | |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 819 | err = -EINVAL; |
Herbert Xu | dd04446 | 2015-05-23 15:41:56 +0800 | [diff] [blame] | 820 | if (inst->alg.ivsize != sizeof(u64)) |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 821 | goto free_inst; |
Herbert Xu | 3c08fee | 2015-05-21 15:11:14 +0800 | [diff] [blame] | 822 | |
| 823 | spawn = aead_instance_ctx(inst); |
| 824 | alg = crypto_spawn_aead_alg(spawn); |
| 825 | |
| 826 | inst->alg.setkey = seqiv_aead_setkey; |
| 827 | inst->alg.setauthsize = seqiv_aead_setauthsize; |
| 828 | inst->alg.encrypt = seqiv_aead_encrypt_compat_first; |
| 829 | inst->alg.decrypt = seqiv_aead_decrypt_compat; |
| 830 | |
Herbert Xu | dd04446 | 2015-05-23 15:41:56 +0800 | [diff] [blame] | 831 | inst->alg.base.cra_init = seqniv_aead_init; |
| 832 | inst->alg.base.cra_exit = seqiv_aead_exit; |
Herbert Xu | 3c08fee | 2015-05-21 15:11:14 +0800 | [diff] [blame] | 833 | |
| 834 | inst->alg.base.cra_alignmask |= __alignof__(u32) - 1; |
| 835 | inst->alg.base.cra_ctxsize = sizeof(struct seqiv_aead_ctx); |
| 836 | inst->alg.base.cra_ctxsize += inst->alg.base.cra_aead.ivsize; |
| 837 | |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 838 | err = aead_register_instance(tmpl, inst); |
| 839 | if (err) |
| 840 | goto free_inst; |
Herbert Xu | 3c08fee | 2015-05-21 15:11:14 +0800 | [diff] [blame] | 841 | |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 842 | out: |
| 843 | return err; |
| 844 | |
| 845 | free_inst: |
| 846 | aead_geniv_free(inst); |
Herbert Xu | 3c08fee | 2015-05-21 15:11:14 +0800 | [diff] [blame] | 847 | put_rng: |
| 848 | crypto_put_default_rng(); |
| 849 | goto out; |
| 850 | } |
| 851 | |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 852 | static void seqiv_free(struct crypto_instance *inst) |
| 853 | { |
| 854 | if ((inst->alg.cra_flags ^ CRYPTO_ALG_TYPE_AEAD) & CRYPTO_ALG_TYPE_MASK) |
| 855 | skcipher_geniv_free(inst); |
| 856 | else |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 857 | aead_geniv_free(aead_instance(inst)); |
Herbert Xu | a0f000e | 2008-08-14 22:21:31 +1000 | [diff] [blame] | 858 | crypto_put_default_rng(); |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 859 | } |
| 860 | |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 861 | static struct crypto_template seqiv_tmpl = { |
| 862 | .name = "seqiv", |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 863 | .create = seqiv_create, |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 864 | .free = seqiv_free, |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 865 | .module = THIS_MODULE, |
| 866 | }; |
| 867 | |
Herbert Xu | 3c08fee | 2015-05-21 15:11:14 +0800 | [diff] [blame] | 868 | static struct crypto_template seqniv_tmpl = { |
| 869 | .name = "seqniv", |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 870 | .create = seqniv_create, |
Herbert Xu | 3c08fee | 2015-05-21 15:11:14 +0800 | [diff] [blame] | 871 | .free = seqiv_free, |
| 872 | .module = THIS_MODULE, |
| 873 | }; |
| 874 | |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 875 | static int __init seqiv_module_init(void) |
| 876 | { |
Herbert Xu | 3c08fee | 2015-05-21 15:11:14 +0800 | [diff] [blame] | 877 | int err; |
| 878 | |
| 879 | err = crypto_register_template(&seqiv_tmpl); |
| 880 | if (err) |
| 881 | goto out; |
| 882 | |
| 883 | err = crypto_register_template(&seqniv_tmpl); |
| 884 | if (err) |
| 885 | goto out_undo_niv; |
| 886 | |
| 887 | out: |
| 888 | return err; |
| 889 | |
| 890 | out_undo_niv: |
| 891 | crypto_unregister_template(&seqiv_tmpl); |
| 892 | goto out; |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | static void __exit seqiv_module_exit(void) |
| 896 | { |
| 897 | crypto_unregister_template(&seqiv_tmpl); |
| 898 | } |
| 899 | |
| 900 | module_init(seqiv_module_init); |
| 901 | module_exit(seqiv_module_exit); |
| 902 | |
| 903 | MODULE_LICENSE("GPL"); |
| 904 | MODULE_DESCRIPTION("Sequence Number IV Generator"); |
Kees Cook | 4943ba1 | 2014-11-24 16:32:38 -0800 | [diff] [blame] | 905 | MODULE_ALIAS_CRYPTO("seqiv"); |
Herbert Xu | 3c08fee | 2015-05-21 15:11:14 +0800 | [diff] [blame] | 906 | MODULE_ALIAS_CRYPTO("seqniv"); |