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 | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 17 | #include <crypto/scatterwalk.h> |
Herbert Xu | 3a01d0e | 2016-07-12 13:17:50 +0800 | [diff] [blame] | 18 | #include <crypto/skcipher.h> |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 19 | #include <linux/err.h> |
| 20 | #include <linux/init.h> |
| 21 | #include <linux/kernel.h> |
| 22 | #include <linux/module.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 23 | #include <linux/slab.h> |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 24 | #include <linux/string.h> |
| 25 | |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 26 | static void seqiv_free(struct crypto_instance *inst); |
| 27 | |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 28 | static void seqiv_aead_encrypt_complete2(struct aead_request *req, int err) |
| 29 | { |
| 30 | struct aead_request *subreq = aead_request_ctx(req); |
| 31 | struct crypto_aead *geniv; |
| 32 | |
| 33 | if (err == -EINPROGRESS) |
| 34 | return; |
| 35 | |
| 36 | if (err) |
| 37 | goto out; |
| 38 | |
| 39 | geniv = crypto_aead_reqtfm(req); |
| 40 | memcpy(req->iv, subreq->iv, crypto_aead_ivsize(geniv)); |
| 41 | |
| 42 | out: |
| 43 | kzfree(subreq->iv); |
| 44 | } |
| 45 | |
| 46 | static void seqiv_aead_encrypt_complete(struct crypto_async_request *base, |
| 47 | int err) |
| 48 | { |
| 49 | struct aead_request *req = base->data; |
| 50 | |
| 51 | seqiv_aead_encrypt_complete2(req, err); |
| 52 | aead_request_complete(req, err); |
| 53 | } |
| 54 | |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 55 | static int seqiv_aead_encrypt(struct aead_request *req) |
| 56 | { |
| 57 | struct crypto_aead *geniv = crypto_aead_reqtfm(req); |
Herbert Xu | 659e7f5 | 2015-08-13 17:28:59 +0800 | [diff] [blame] | 58 | struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv); |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 59 | struct aead_request *subreq = aead_request_ctx(req); |
| 60 | crypto_completion_t compl; |
| 61 | void *data; |
| 62 | u8 *info; |
Herbert Xu | dd04446 | 2015-05-23 15:41:56 +0800 | [diff] [blame] | 63 | unsigned int ivsize = 8; |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 64 | int err; |
| 65 | |
Herbert Xu | dd04446 | 2015-05-23 15:41:56 +0800 | [diff] [blame] | 66 | if (req->cryptlen < ivsize) |
| 67 | return -EINVAL; |
| 68 | |
Herbert Xu | 659e7f5 | 2015-08-13 17:28:59 +0800 | [diff] [blame] | 69 | aead_request_set_tfm(subreq, ctx->child); |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 70 | |
| 71 | compl = req->base.complete; |
| 72 | data = req->base.data; |
| 73 | info = req->iv; |
| 74 | |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 75 | if (req->src != req->dst) { |
Herbert Xu | ef22871 | 2016-07-12 13:17:45 +0800 | [diff] [blame] | 76 | SKCIPHER_REQUEST_ON_STACK(nreq, ctx->sknull); |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 77 | |
Herbert Xu | ef22871 | 2016-07-12 13:17:45 +0800 | [diff] [blame] | 78 | skcipher_request_set_tfm(nreq, ctx->sknull); |
| 79 | skcipher_request_set_callback(nreq, req->base.flags, |
| 80 | NULL, NULL); |
| 81 | skcipher_request_set_crypt(nreq, req->src, req->dst, |
| 82 | req->assoclen + req->cryptlen, |
| 83 | NULL); |
| 84 | |
| 85 | err = crypto_skcipher_encrypt(nreq); |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 86 | if (err) |
| 87 | return err; |
| 88 | } |
| 89 | |
| 90 | if (unlikely(!IS_ALIGNED((unsigned long)info, |
| 91 | crypto_aead_alignmask(geniv) + 1))) { |
| 92 | info = kmalloc(ivsize, req->base.flags & |
| 93 | CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL: |
| 94 | GFP_ATOMIC); |
| 95 | if (!info) |
| 96 | return -ENOMEM; |
| 97 | |
| 98 | memcpy(info, req->iv, ivsize); |
| 99 | compl = seqiv_aead_encrypt_complete; |
| 100 | data = req; |
| 101 | } |
| 102 | |
| 103 | aead_request_set_callback(subreq, req->base.flags, compl, data); |
| 104 | aead_request_set_crypt(subreq, req->dst, req->dst, |
| 105 | req->cryptlen - ivsize, info); |
Herbert Xu | 374d4ad | 2015-05-23 15:41:57 +0800 | [diff] [blame] | 106 | aead_request_set_ad(subreq, req->assoclen + ivsize); |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 107 | |
| 108 | crypto_xor(info, ctx->salt, ivsize); |
| 109 | scatterwalk_map_and_copy(info, req->dst, req->assoclen, ivsize, 1); |
| 110 | |
| 111 | err = crypto_aead_encrypt(subreq); |
| 112 | if (unlikely(info != req->iv)) |
| 113 | seqiv_aead_encrypt_complete2(req, err); |
| 114 | return err; |
| 115 | } |
| 116 | |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 117 | static int seqiv_aead_decrypt(struct aead_request *req) |
| 118 | { |
| 119 | struct crypto_aead *geniv = crypto_aead_reqtfm(req); |
Herbert Xu | 659e7f5 | 2015-08-13 17:28:59 +0800 | [diff] [blame] | 120 | struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv); |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 121 | struct aead_request *subreq = aead_request_ctx(req); |
| 122 | crypto_completion_t compl; |
| 123 | void *data; |
Herbert Xu | dd04446 | 2015-05-23 15:41:56 +0800 | [diff] [blame] | 124 | unsigned int ivsize = 8; |
| 125 | |
| 126 | if (req->cryptlen < ivsize + crypto_aead_authsize(geniv)) |
| 127 | return -EINVAL; |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 128 | |
Herbert Xu | 659e7f5 | 2015-08-13 17:28:59 +0800 | [diff] [blame] | 129 | aead_request_set_tfm(subreq, ctx->child); |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 130 | |
| 131 | compl = req->base.complete; |
| 132 | data = req->base.data; |
| 133 | |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 134 | aead_request_set_callback(subreq, req->base.flags, compl, data); |
| 135 | aead_request_set_crypt(subreq, req->src, req->dst, |
| 136 | req->cryptlen - ivsize, req->iv); |
Herbert Xu | 374d4ad | 2015-05-23 15:41:57 +0800 | [diff] [blame] | 137 | aead_request_set_ad(subreq, req->assoclen + ivsize); |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 138 | |
| 139 | scatterwalk_map_and_copy(req->iv, req->src, req->assoclen, ivsize, 0); |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 140 | |
| 141 | return crypto_aead_decrypt(subreq); |
| 142 | } |
| 143 | |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 144 | static int seqiv_aead_create(struct crypto_template *tmpl, struct rtattr **tb) |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 145 | { |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 146 | struct aead_instance *inst; |
| 147 | struct crypto_aead_spawn *spawn; |
| 148 | struct aead_alg *alg; |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 149 | int err; |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 150 | |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 151 | inst = aead_geniv_alloc(tmpl, tb, 0, 0); |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 152 | |
| 153 | if (IS_ERR(inst)) |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 154 | return PTR_ERR(inst); |
| 155 | |
Herbert Xu | 661cfd0 | 2015-05-27 14:37:37 +0800 | [diff] [blame] | 156 | spawn = aead_instance_ctx(inst); |
| 157 | alg = crypto_spawn_aead_alg(spawn); |
| 158 | |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 159 | err = -EINVAL; |
Herbert Xu | dd04446 | 2015-05-23 15:41:56 +0800 | [diff] [blame] | 160 | if (inst->alg.ivsize != sizeof(u64)) |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 161 | goto free_inst; |
Herbert Xu | c0ecf89 | 2015-01-16 19:51:20 +1100 | [diff] [blame] | 162 | |
Herbert Xu | b7dcfab | 2015-06-03 14:49:27 +0800 | [diff] [blame] | 163 | inst->alg.encrypt = seqiv_aead_encrypt; |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 164 | inst->alg.decrypt = seqiv_aead_decrypt; |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 165 | |
Herbert Xu | 659e7f5 | 2015-08-13 17:28:59 +0800 | [diff] [blame] | 166 | inst->alg.init = aead_init_geniv; |
| 167 | inst->alg.exit = aead_exit_geniv; |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 168 | |
Herbert Xu | 659e7f5 | 2015-08-13 17:28:59 +0800 | [diff] [blame] | 169 | inst->alg.base.cra_ctxsize = sizeof(struct aead_geniv_ctx); |
Herbert Xu | 5964f26 | 2015-07-09 07:17:22 +0800 | [diff] [blame] | 170 | inst->alg.base.cra_ctxsize += inst->alg.ivsize; |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 171 | |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 172 | err = aead_register_instance(tmpl, inst); |
| 173 | if (err) |
| 174 | goto free_inst; |
| 175 | |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 176 | out: |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 177 | return err; |
| 178 | |
| 179 | free_inst: |
| 180 | aead_geniv_free(inst); |
| 181 | goto out; |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 182 | } |
| 183 | |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 184 | static int seqiv_create(struct crypto_template *tmpl, struct rtattr **tb) |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 185 | { |
| 186 | struct crypto_attr_type *algt; |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 187 | |
| 188 | algt = crypto_get_attr_type(tb); |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 189 | if (IS_ERR(algt)) |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 190 | return PTR_ERR(algt); |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 191 | |
| 192 | if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & CRYPTO_ALG_TYPE_MASK) |
Herbert Xu | 3a01d0e | 2016-07-12 13:17:50 +0800 | [diff] [blame] | 193 | return -EINVAL; |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 194 | |
Herbert Xu | 3a01d0e | 2016-07-12 13:17:50 +0800 | [diff] [blame] | 195 | return seqiv_aead_create(tmpl, tb); |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | static void seqiv_free(struct crypto_instance *inst) |
| 199 | { |
Herbert Xu | 3a01d0e | 2016-07-12 13:17:50 +0800 | [diff] [blame] | 200 | aead_geniv_free(aead_instance(inst)); |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 201 | } |
| 202 | |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 203 | static struct crypto_template seqiv_tmpl = { |
| 204 | .name = "seqiv", |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 205 | .create = seqiv_create, |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 206 | .free = seqiv_free, |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 207 | .module = THIS_MODULE, |
| 208 | }; |
| 209 | |
| 210 | static int __init seqiv_module_init(void) |
| 211 | { |
Herbert Xu | 8a2cd1c | 2015-08-13 17:28:53 +0800 | [diff] [blame] | 212 | return crypto_register_template(&seqiv_tmpl); |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | static void __exit seqiv_module_exit(void) |
| 216 | { |
| 217 | crypto_unregister_template(&seqiv_tmpl); |
| 218 | } |
| 219 | |
| 220 | module_init(seqiv_module_init); |
| 221 | module_exit(seqiv_module_exit); |
| 222 | |
| 223 | MODULE_LICENSE("GPL"); |
| 224 | MODULE_DESCRIPTION("Sequence Number IV Generator"); |
Kees Cook | 4943ba1 | 2014-11-24 16:32:38 -0800 | [diff] [blame] | 225 | MODULE_ALIAS_CRYPTO("seqiv"); |