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 | 661cfd0 | 2015-05-27 14:37:37 +0800 | [diff] [blame] | 16 | #include <crypto/internal/geniv.h> |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 17 | #include <crypto/internal/skcipher.h> |
Herbert Xu | a0f000e | 2008-08-14 22:21:31 +1000 | [diff] [blame] | 18 | #include <crypto/rng.h> |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 19 | #include <crypto/scatterwalk.h> |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 20 | #include <linux/err.h> |
| 21 | #include <linux/init.h> |
| 22 | #include <linux/kernel.h> |
| 23 | #include <linux/module.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 24 | #include <linux/slab.h> |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 25 | #include <linux/spinlock.h> |
| 26 | #include <linux/string.h> |
| 27 | |
| 28 | struct seqiv_ctx { |
| 29 | spinlock_t lock; |
| 30 | u8 salt[] __attribute__ ((aligned(__alignof__(u32)))); |
| 31 | }; |
| 32 | |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 33 | static void seqiv_free(struct crypto_instance *inst); |
| 34 | |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 35 | static void seqiv_complete2(struct skcipher_givcrypt_request *req, int err) |
| 36 | { |
| 37 | struct ablkcipher_request *subreq = skcipher_givcrypt_reqctx(req); |
| 38 | struct crypto_ablkcipher *geniv; |
| 39 | |
| 40 | if (err == -EINPROGRESS) |
| 41 | return; |
| 42 | |
| 43 | if (err) |
| 44 | goto out; |
| 45 | |
| 46 | geniv = skcipher_givcrypt_reqtfm(req); |
| 47 | memcpy(req->creq.info, subreq->info, crypto_ablkcipher_ivsize(geniv)); |
| 48 | |
| 49 | out: |
| 50 | kfree(subreq->info); |
| 51 | } |
| 52 | |
| 53 | static void seqiv_complete(struct crypto_async_request *base, int err) |
| 54 | { |
| 55 | struct skcipher_givcrypt_request *req = base->data; |
| 56 | |
| 57 | seqiv_complete2(req, err); |
| 58 | skcipher_givcrypt_complete(req, err); |
| 59 | } |
| 60 | |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 61 | static void seqiv_aead_encrypt_complete2(struct aead_request *req, int err) |
| 62 | { |
| 63 | struct aead_request *subreq = aead_request_ctx(req); |
| 64 | struct crypto_aead *geniv; |
| 65 | |
| 66 | if (err == -EINPROGRESS) |
| 67 | return; |
| 68 | |
| 69 | if (err) |
| 70 | goto out; |
| 71 | |
| 72 | geniv = crypto_aead_reqtfm(req); |
| 73 | memcpy(req->iv, subreq->iv, crypto_aead_ivsize(geniv)); |
| 74 | |
| 75 | out: |
| 76 | kzfree(subreq->iv); |
| 77 | } |
| 78 | |
| 79 | static void seqiv_aead_encrypt_complete(struct crypto_async_request *base, |
| 80 | int err) |
| 81 | { |
| 82 | struct aead_request *req = base->data; |
| 83 | |
| 84 | seqiv_aead_encrypt_complete2(req, err); |
| 85 | aead_request_complete(req, err); |
| 86 | } |
| 87 | |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 88 | static void seqiv_geniv(struct seqiv_ctx *ctx, u8 *info, u64 seq, |
| 89 | unsigned int ivsize) |
| 90 | { |
| 91 | unsigned int len = ivsize; |
| 92 | |
| 93 | if (ivsize > sizeof(u64)) { |
| 94 | memset(info, 0, ivsize - sizeof(u64)); |
| 95 | len = sizeof(u64); |
| 96 | } |
| 97 | seq = cpu_to_be64(seq); |
| 98 | memcpy(info + ivsize - len, &seq, len); |
| 99 | crypto_xor(info, ctx->salt, ivsize); |
| 100 | } |
| 101 | |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 102 | static int seqiv_givencrypt(struct skcipher_givcrypt_request *req) |
| 103 | { |
| 104 | struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req); |
| 105 | struct seqiv_ctx *ctx = crypto_ablkcipher_ctx(geniv); |
| 106 | struct ablkcipher_request *subreq = skcipher_givcrypt_reqctx(req); |
Mark Rustad | 3e3dc25 | 2014-07-25 02:53:38 -0700 | [diff] [blame] | 107 | crypto_completion_t compl; |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 108 | void *data; |
| 109 | u8 *info; |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 110 | unsigned int ivsize; |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 111 | int err; |
| 112 | |
| 113 | ablkcipher_request_set_tfm(subreq, skcipher_geniv_cipher(geniv)); |
| 114 | |
Mark Rustad | 3e3dc25 | 2014-07-25 02:53:38 -0700 | [diff] [blame] | 115 | compl = req->creq.base.complete; |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 116 | data = req->creq.base.data; |
| 117 | info = req->creq.info; |
| 118 | |
| 119 | ivsize = crypto_ablkcipher_ivsize(geniv); |
| 120 | |
| 121 | if (unlikely(!IS_ALIGNED((unsigned long)info, |
| 122 | crypto_ablkcipher_alignmask(geniv) + 1))) { |
| 123 | info = kmalloc(ivsize, req->creq.base.flags & |
| 124 | CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL: |
| 125 | GFP_ATOMIC); |
| 126 | if (!info) |
| 127 | return -ENOMEM; |
| 128 | |
Mark Rustad | 3e3dc25 | 2014-07-25 02:53:38 -0700 | [diff] [blame] | 129 | compl = seqiv_complete; |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 130 | data = req; |
| 131 | } |
| 132 | |
Mark Rustad | 3e3dc25 | 2014-07-25 02:53:38 -0700 | [diff] [blame] | 133 | ablkcipher_request_set_callback(subreq, req->creq.base.flags, compl, |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 134 | data); |
| 135 | ablkcipher_request_set_crypt(subreq, req->creq.src, req->creq.dst, |
| 136 | req->creq.nbytes, info); |
| 137 | |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 138 | seqiv_geniv(ctx, info, req->seq, ivsize); |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 139 | memcpy(req->giv, info, ivsize); |
| 140 | |
| 141 | err = crypto_ablkcipher_encrypt(subreq); |
| 142 | if (unlikely(info != req->creq.info)) |
| 143 | seqiv_complete2(req, err); |
| 144 | return err; |
| 145 | } |
| 146 | |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 147 | static int seqiv_aead_encrypt(struct aead_request *req) |
| 148 | { |
| 149 | struct crypto_aead *geniv = crypto_aead_reqtfm(req); |
Herbert Xu | 659e7f5 | 2015-08-13 17:28:59 +0800 | [diff] [blame] | 150 | struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv); |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 151 | struct aead_request *subreq = aead_request_ctx(req); |
| 152 | crypto_completion_t compl; |
| 153 | void *data; |
| 154 | u8 *info; |
Herbert Xu | dd04446 | 2015-05-23 15:41:56 +0800 | [diff] [blame] | 155 | unsigned int ivsize = 8; |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 156 | int err; |
| 157 | |
Herbert Xu | dd04446 | 2015-05-23 15:41:56 +0800 | [diff] [blame] | 158 | if (req->cryptlen < ivsize) |
| 159 | return -EINVAL; |
| 160 | |
Herbert Xu | 659e7f5 | 2015-08-13 17:28:59 +0800 | [diff] [blame] | 161 | aead_request_set_tfm(subreq, ctx->child); |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 162 | |
| 163 | compl = req->base.complete; |
| 164 | data = req->base.data; |
| 165 | info = req->iv; |
| 166 | |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 167 | if (req->src != req->dst) { |
Herbert Xu | ef22871 | 2016-07-12 13:17:45 +0800 | [diff] [blame^] | 168 | SKCIPHER_REQUEST_ON_STACK(nreq, ctx->sknull); |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 169 | |
Herbert Xu | ef22871 | 2016-07-12 13:17:45 +0800 | [diff] [blame^] | 170 | skcipher_request_set_tfm(nreq, ctx->sknull); |
| 171 | skcipher_request_set_callback(nreq, req->base.flags, |
| 172 | NULL, NULL); |
| 173 | skcipher_request_set_crypt(nreq, req->src, req->dst, |
| 174 | req->assoclen + req->cryptlen, |
| 175 | NULL); |
| 176 | |
| 177 | err = crypto_skcipher_encrypt(nreq); |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 178 | if (err) |
| 179 | return err; |
| 180 | } |
| 181 | |
| 182 | if (unlikely(!IS_ALIGNED((unsigned long)info, |
| 183 | crypto_aead_alignmask(geniv) + 1))) { |
| 184 | info = kmalloc(ivsize, req->base.flags & |
| 185 | CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL: |
| 186 | GFP_ATOMIC); |
| 187 | if (!info) |
| 188 | return -ENOMEM; |
| 189 | |
| 190 | memcpy(info, req->iv, ivsize); |
| 191 | compl = seqiv_aead_encrypt_complete; |
| 192 | data = req; |
| 193 | } |
| 194 | |
| 195 | aead_request_set_callback(subreq, req->base.flags, compl, data); |
| 196 | aead_request_set_crypt(subreq, req->dst, req->dst, |
| 197 | req->cryptlen - ivsize, info); |
Herbert Xu | 374d4ad | 2015-05-23 15:41:57 +0800 | [diff] [blame] | 198 | aead_request_set_ad(subreq, req->assoclen + ivsize); |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 199 | |
| 200 | crypto_xor(info, ctx->salt, ivsize); |
| 201 | scatterwalk_map_and_copy(info, req->dst, req->assoclen, ivsize, 1); |
| 202 | |
| 203 | err = crypto_aead_encrypt(subreq); |
| 204 | if (unlikely(info != req->iv)) |
| 205 | seqiv_aead_encrypt_complete2(req, err); |
| 206 | return err; |
| 207 | } |
| 208 | |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 209 | static int seqiv_aead_decrypt(struct aead_request *req) |
| 210 | { |
| 211 | struct crypto_aead *geniv = crypto_aead_reqtfm(req); |
Herbert Xu | 659e7f5 | 2015-08-13 17:28:59 +0800 | [diff] [blame] | 212 | struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv); |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 213 | struct aead_request *subreq = aead_request_ctx(req); |
| 214 | crypto_completion_t compl; |
| 215 | void *data; |
Herbert Xu | dd04446 | 2015-05-23 15:41:56 +0800 | [diff] [blame] | 216 | unsigned int ivsize = 8; |
| 217 | |
| 218 | if (req->cryptlen < ivsize + crypto_aead_authsize(geniv)) |
| 219 | return -EINVAL; |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 220 | |
Herbert Xu | 659e7f5 | 2015-08-13 17:28:59 +0800 | [diff] [blame] | 221 | aead_request_set_tfm(subreq, ctx->child); |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 222 | |
| 223 | compl = req->base.complete; |
| 224 | data = req->base.data; |
| 225 | |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 226 | aead_request_set_callback(subreq, req->base.flags, compl, data); |
| 227 | aead_request_set_crypt(subreq, req->src, req->dst, |
| 228 | req->cryptlen - ivsize, req->iv); |
Herbert Xu | 374d4ad | 2015-05-23 15:41:57 +0800 | [diff] [blame] | 229 | aead_request_set_ad(subreq, req->assoclen + ivsize); |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 230 | |
| 231 | scatterwalk_map_and_copy(req->iv, req->src, req->assoclen, ivsize, 0); |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 232 | |
| 233 | return crypto_aead_decrypt(subreq); |
| 234 | } |
| 235 | |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 236 | static int seqiv_init(struct crypto_tfm *tfm) |
| 237 | { |
| 238 | struct crypto_ablkcipher *geniv = __crypto_ablkcipher_cast(tfm); |
| 239 | struct seqiv_ctx *ctx = crypto_ablkcipher_ctx(geniv); |
Herbert Xu | eeee12a | 2015-06-21 19:11:49 +0800 | [diff] [blame] | 240 | int err; |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 241 | |
| 242 | spin_lock_init(&ctx->lock); |
| 243 | |
| 244 | tfm->crt_ablkcipher.reqsize = sizeof(struct ablkcipher_request); |
| 245 | |
Herbert Xu | eeee12a | 2015-06-21 19:11:49 +0800 | [diff] [blame] | 246 | err = 0; |
| 247 | if (!crypto_get_default_rng()) { |
| 248 | crypto_ablkcipher_crt(geniv)->givencrypt = seqiv_givencrypt; |
| 249 | err = crypto_rng_get_bytes(crypto_default_rng, ctx->salt, |
| 250 | crypto_ablkcipher_ivsize(geniv)); |
| 251 | crypto_put_default_rng(); |
| 252 | } |
| 253 | |
| 254 | return err ?: skcipher_geniv_init(tfm); |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 255 | } |
| 256 | |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 257 | static int seqiv_ablkcipher_create(struct crypto_template *tmpl, |
| 258 | struct rtattr **tb) |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 259 | { |
| 260 | struct crypto_instance *inst; |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 261 | int err; |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 262 | |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 263 | inst = skcipher_geniv_alloc(tmpl, tb, 0, 0); |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 264 | |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 265 | if (IS_ERR(inst)) |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 266 | return PTR_ERR(inst); |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 267 | |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 268 | err = -EINVAL; |
| 269 | if (inst->alg.cra_ablkcipher.ivsize < sizeof(u64)) |
| 270 | goto free_inst; |
Herbert Xu | c0ecf89 | 2015-01-16 19:51:20 +1100 | [diff] [blame] | 271 | |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 272 | inst->alg.cra_init = seqiv_init; |
| 273 | inst->alg.cra_exit = skcipher_geniv_exit; |
| 274 | |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 275 | inst->alg.cra_ctxsize += inst->alg.cra_ablkcipher.ivsize; |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 276 | inst->alg.cra_ctxsize += sizeof(struct seqiv_ctx); |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 277 | |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 278 | inst->alg.cra_alignmask |= __alignof__(u32) - 1; |
| 279 | |
| 280 | err = crypto_register_instance(tmpl, inst); |
| 281 | if (err) |
| 282 | goto free_inst; |
| 283 | |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 284 | out: |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 285 | return err; |
| 286 | |
| 287 | free_inst: |
| 288 | skcipher_geniv_free(inst); |
| 289 | goto out; |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 290 | } |
| 291 | |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 292 | static int seqiv_aead_create(struct crypto_template *tmpl, struct rtattr **tb) |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 293 | { |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 294 | struct aead_instance *inst; |
| 295 | struct crypto_aead_spawn *spawn; |
| 296 | struct aead_alg *alg; |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 297 | int err; |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 298 | |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 299 | inst = aead_geniv_alloc(tmpl, tb, 0, 0); |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 300 | |
| 301 | if (IS_ERR(inst)) |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 302 | return PTR_ERR(inst); |
| 303 | |
| 304 | inst->alg.base.cra_alignmask |= __alignof__(u32) - 1; |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 305 | |
Herbert Xu | 661cfd0 | 2015-05-27 14:37:37 +0800 | [diff] [blame] | 306 | spawn = aead_instance_ctx(inst); |
| 307 | alg = crypto_spawn_aead_alg(spawn); |
| 308 | |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 309 | err = -EINVAL; |
Herbert Xu | dd04446 | 2015-05-23 15:41:56 +0800 | [diff] [blame] | 310 | if (inst->alg.ivsize != sizeof(u64)) |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 311 | goto free_inst; |
Herbert Xu | c0ecf89 | 2015-01-16 19:51:20 +1100 | [diff] [blame] | 312 | |
Herbert Xu | b7dcfab | 2015-06-03 14:49:27 +0800 | [diff] [blame] | 313 | inst->alg.encrypt = seqiv_aead_encrypt; |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 314 | inst->alg.decrypt = seqiv_aead_decrypt; |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 315 | |
Herbert Xu | 659e7f5 | 2015-08-13 17:28:59 +0800 | [diff] [blame] | 316 | inst->alg.init = aead_init_geniv; |
| 317 | inst->alg.exit = aead_exit_geniv; |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 318 | |
Herbert Xu | 659e7f5 | 2015-08-13 17:28:59 +0800 | [diff] [blame] | 319 | inst->alg.base.cra_ctxsize = sizeof(struct aead_geniv_ctx); |
Herbert Xu | 5964f26 | 2015-07-09 07:17:22 +0800 | [diff] [blame] | 320 | inst->alg.base.cra_ctxsize += inst->alg.ivsize; |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 321 | |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 322 | err = aead_register_instance(tmpl, inst); |
| 323 | if (err) |
| 324 | goto free_inst; |
| 325 | |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 326 | out: |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 327 | return err; |
| 328 | |
| 329 | free_inst: |
| 330 | aead_geniv_free(inst); |
| 331 | goto out; |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 332 | } |
| 333 | |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 334 | static int seqiv_create(struct crypto_template *tmpl, struct rtattr **tb) |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 335 | { |
| 336 | struct crypto_attr_type *algt; |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 337 | int err; |
| 338 | |
| 339 | algt = crypto_get_attr_type(tb); |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 340 | if (IS_ERR(algt)) |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 341 | return PTR_ERR(algt); |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 342 | |
| 343 | if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & CRYPTO_ALG_TYPE_MASK) |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 344 | err = seqiv_ablkcipher_create(tmpl, tb); |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 345 | else |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 346 | err = seqiv_aead_create(tmpl, tb); |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 347 | |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 348 | return err; |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | static void seqiv_free(struct crypto_instance *inst) |
| 352 | { |
| 353 | if ((inst->alg.cra_flags ^ CRYPTO_ALG_TYPE_AEAD) & CRYPTO_ALG_TYPE_MASK) |
| 354 | skcipher_geniv_free(inst); |
| 355 | else |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 356 | aead_geniv_free(aead_instance(inst)); |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 357 | } |
| 358 | |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 359 | static struct crypto_template seqiv_tmpl = { |
| 360 | .name = "seqiv", |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 361 | .create = seqiv_create, |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 362 | .free = seqiv_free, |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 363 | .module = THIS_MODULE, |
| 364 | }; |
| 365 | |
| 366 | static int __init seqiv_module_init(void) |
| 367 | { |
Herbert Xu | 8a2cd1c | 2015-08-13 17:28:53 +0800 | [diff] [blame] | 368 | return crypto_register_template(&seqiv_tmpl); |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | static void __exit seqiv_module_exit(void) |
| 372 | { |
| 373 | crypto_unregister_template(&seqiv_tmpl); |
| 374 | } |
| 375 | |
| 376 | module_init(seqiv_module_init); |
| 377 | module_exit(seqiv_module_exit); |
| 378 | |
| 379 | MODULE_LICENSE("GPL"); |
| 380 | MODULE_DESCRIPTION("Sequence Number IV Generator"); |
Kees Cook | 4943ba1 | 2014-11-24 16:32:38 -0800 | [diff] [blame] | 381 | MODULE_ALIAS_CRYPTO("seqiv"); |