Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 1 | /* |
| 2 | * echainiv: Encrypted Chain IV Generator |
| 3 | * |
| 4 | * This generator generates an IV based on a sequence number by xoring it |
| 5 | * with a salt and then encrypting it with the same key as used to encrypt |
| 6 | * the plain text. This algorithm requires that the block size be equal |
| 7 | * to the IV size. It is mainly useful for CBC. |
| 8 | * |
| 9 | * This generator can only be used by algorithms where authentication |
| 10 | * is performed after encryption (i.e., authenc). |
| 11 | * |
| 12 | * Copyright (c) 2015 Herbert Xu <herbert@gondor.apana.org.au> |
| 13 | * |
| 14 | * This program is free software; you can redistribute it and/or modify it |
| 15 | * under the terms of the GNU General Public License as published by the Free |
| 16 | * Software Foundation; either version 2 of the License, or (at your option) |
| 17 | * any later version. |
| 18 | * |
| 19 | */ |
| 20 | |
Herbert Xu | d97de47 | 2015-05-27 14:37:33 +0800 | [diff] [blame] | 21 | #include <crypto/internal/geniv.h> |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 22 | #include <crypto/null.h> |
| 23 | #include <crypto/rng.h> |
| 24 | #include <crypto/scatterwalk.h> |
| 25 | #include <linux/err.h> |
| 26 | #include <linux/init.h> |
| 27 | #include <linux/kernel.h> |
| 28 | #include <linux/mm.h> |
| 29 | #include <linux/module.h> |
| 30 | #include <linux/percpu.h> |
| 31 | #include <linux/spinlock.h> |
| 32 | #include <linux/string.h> |
| 33 | |
| 34 | #define MAX_IV_SIZE 16 |
| 35 | |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 36 | struct echainiv_ctx { |
Herbert Xu | d97de47 | 2015-05-27 14:37:33 +0800 | [diff] [blame] | 37 | /* aead_geniv_ctx must be first the element */ |
| 38 | struct aead_geniv_ctx geniv; |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 39 | struct crypto_blkcipher *null; |
| 40 | u8 salt[] __attribute__ ((aligned(__alignof__(u32)))); |
| 41 | }; |
| 42 | |
| 43 | static DEFINE_PER_CPU(u32 [MAX_IV_SIZE / sizeof(u32)], echainiv_iv); |
| 44 | |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 45 | /* We don't care if we get preempted and read/write IVs from the next CPU. */ |
Wu Fengguang | 622ff87 | 2015-05-23 11:22:47 +0800 | [diff] [blame] | 46 | static void echainiv_read_iv(u8 *dst, unsigned size) |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 47 | { |
| 48 | u32 *a = (u32 *)dst; |
| 49 | u32 __percpu *b = echainiv_iv; |
| 50 | |
| 51 | for (; size >= 4; size -= 4) { |
| 52 | *a++ = this_cpu_read(*b); |
| 53 | b++; |
| 54 | } |
| 55 | } |
| 56 | |
Wu Fengguang | 622ff87 | 2015-05-23 11:22:47 +0800 | [diff] [blame] | 57 | static void echainiv_write_iv(const u8 *src, unsigned size) |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 58 | { |
| 59 | const u32 *a = (const u32 *)src; |
| 60 | u32 __percpu *b = echainiv_iv; |
| 61 | |
| 62 | for (; size >= 4; size -= 4) { |
| 63 | this_cpu_write(*b, *a); |
| 64 | a++; |
| 65 | b++; |
| 66 | } |
| 67 | } |
| 68 | |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 69 | static void echainiv_encrypt_complete2(struct aead_request *req, int err) |
| 70 | { |
| 71 | struct aead_request *subreq = aead_request_ctx(req); |
| 72 | struct crypto_aead *geniv; |
| 73 | unsigned int ivsize; |
| 74 | |
| 75 | if (err == -EINPROGRESS) |
| 76 | return; |
| 77 | |
| 78 | if (err) |
| 79 | goto out; |
| 80 | |
| 81 | geniv = crypto_aead_reqtfm(req); |
| 82 | ivsize = crypto_aead_ivsize(geniv); |
| 83 | |
| 84 | echainiv_write_iv(subreq->iv, ivsize); |
| 85 | |
| 86 | if (req->iv != subreq->iv) |
| 87 | memcpy(req->iv, subreq->iv, ivsize); |
| 88 | |
| 89 | out: |
| 90 | if (req->iv != subreq->iv) |
| 91 | kzfree(subreq->iv); |
| 92 | } |
| 93 | |
| 94 | static void echainiv_encrypt_complete(struct crypto_async_request *base, |
| 95 | int err) |
| 96 | { |
| 97 | struct aead_request *req = base->data; |
| 98 | |
| 99 | echainiv_encrypt_complete2(req, err); |
| 100 | aead_request_complete(req, err); |
| 101 | } |
| 102 | |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 103 | static int echainiv_encrypt(struct aead_request *req) |
| 104 | { |
| 105 | struct crypto_aead *geniv = crypto_aead_reqtfm(req); |
| 106 | struct echainiv_ctx *ctx = crypto_aead_ctx(geniv); |
| 107 | struct aead_request *subreq = aead_request_ctx(req); |
| 108 | crypto_completion_t compl; |
| 109 | void *data; |
| 110 | u8 *info; |
Herbert Xu | 823655c | 2015-05-23 15:41:54 +0800 | [diff] [blame] | 111 | unsigned int ivsize = crypto_aead_ivsize(geniv); |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 112 | int err; |
| 113 | |
Herbert Xu | 823655c | 2015-05-23 15:41:54 +0800 | [diff] [blame] | 114 | if (req->cryptlen < ivsize) |
| 115 | return -EINVAL; |
| 116 | |
Herbert Xu | d97de47 | 2015-05-27 14:37:33 +0800 | [diff] [blame] | 117 | aead_request_set_tfm(subreq, ctx->geniv.child); |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 118 | |
| 119 | compl = echainiv_encrypt_complete; |
| 120 | data = req; |
| 121 | info = req->iv; |
| 122 | |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 123 | if (req->src != req->dst) { |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 124 | struct blkcipher_desc desc = { |
| 125 | .tfm = ctx->null, |
| 126 | }; |
| 127 | |
| 128 | err = crypto_blkcipher_encrypt( |
Herbert Xu | 838c9d56 | 2015-05-27 14:37:31 +0800 | [diff] [blame] | 129 | &desc, req->dst, req->src, |
| 130 | req->assoclen + req->cryptlen); |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 131 | if (err) |
| 132 | return err; |
| 133 | } |
| 134 | |
| 135 | if (unlikely(!IS_ALIGNED((unsigned long)info, |
| 136 | crypto_aead_alignmask(geniv) + 1))) { |
| 137 | info = kmalloc(ivsize, req->base.flags & |
| 138 | CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL: |
| 139 | GFP_ATOMIC); |
| 140 | if (!info) |
| 141 | return -ENOMEM; |
| 142 | |
| 143 | memcpy(info, req->iv, ivsize); |
| 144 | } |
| 145 | |
| 146 | aead_request_set_callback(subreq, req->base.flags, compl, data); |
| 147 | aead_request_set_crypt(subreq, req->dst, req->dst, |
Herbert Xu | 5499b1a | 2015-07-09 07:17:20 +0800 | [diff] [blame] | 148 | req->cryptlen, info); |
| 149 | aead_request_set_ad(subreq, req->assoclen); |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 150 | |
| 151 | crypto_xor(info, ctx->salt, ivsize); |
| 152 | scatterwalk_map_and_copy(info, req->dst, req->assoclen, ivsize, 1); |
| 153 | echainiv_read_iv(info, ivsize); |
| 154 | |
| 155 | err = crypto_aead_encrypt(subreq); |
| 156 | echainiv_encrypt_complete2(req, err); |
| 157 | return err; |
| 158 | } |
| 159 | |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 160 | static int echainiv_decrypt(struct aead_request *req) |
| 161 | { |
| 162 | struct crypto_aead *geniv = crypto_aead_reqtfm(req); |
| 163 | struct echainiv_ctx *ctx = crypto_aead_ctx(geniv); |
| 164 | struct aead_request *subreq = aead_request_ctx(req); |
| 165 | crypto_completion_t compl; |
| 166 | void *data; |
Herbert Xu | 823655c | 2015-05-23 15:41:54 +0800 | [diff] [blame] | 167 | unsigned int ivsize = crypto_aead_ivsize(geniv); |
| 168 | |
Herbert Xu | 5499b1a | 2015-07-09 07:17:20 +0800 | [diff] [blame] | 169 | if (req->cryptlen < ivsize) |
Herbert Xu | 823655c | 2015-05-23 15:41:54 +0800 | [diff] [blame] | 170 | return -EINVAL; |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 171 | |
Herbert Xu | d97de47 | 2015-05-27 14:37:33 +0800 | [diff] [blame] | 172 | aead_request_set_tfm(subreq, ctx->geniv.child); |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 173 | |
| 174 | compl = req->base.complete; |
| 175 | data = req->base.data; |
| 176 | |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 177 | aead_request_set_callback(subreq, req->base.flags, compl, data); |
| 178 | aead_request_set_crypt(subreq, req->src, req->dst, |
| 179 | req->cryptlen - ivsize, req->iv); |
Herbert Xu | 374d4ad | 2015-05-23 15:41:57 +0800 | [diff] [blame] | 180 | aead_request_set_ad(subreq, req->assoclen + ivsize); |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 181 | |
| 182 | scatterwalk_map_and_copy(req->iv, req->src, req->assoclen, ivsize, 0); |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 183 | |
| 184 | return crypto_aead_decrypt(subreq); |
| 185 | } |
| 186 | |
Herbert Xu | 5499b1a | 2015-07-09 07:17:20 +0800 | [diff] [blame] | 187 | static int echainiv_init(struct crypto_aead *geniv) |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 188 | { |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 189 | struct echainiv_ctx *ctx = crypto_aead_ctx(geniv); |
| 190 | int err; |
| 191 | |
Herbert Xu | d97de47 | 2015-05-27 14:37:33 +0800 | [diff] [blame] | 192 | spin_lock_init(&ctx->geniv.lock); |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 193 | |
| 194 | crypto_aead_set_reqsize(geniv, sizeof(struct aead_request)); |
| 195 | |
Herbert Xu | 9fcc704 | 2015-06-21 19:11:50 +0800 | [diff] [blame] | 196 | err = crypto_get_default_rng(); |
| 197 | if (err) |
| 198 | goto out; |
| 199 | |
Herbert Xu | f261c5f | 2015-06-03 14:49:24 +0800 | [diff] [blame] | 200 | err = crypto_rng_get_bytes(crypto_default_rng, ctx->salt, |
| 201 | crypto_aead_ivsize(geniv)); |
Herbert Xu | 9fcc704 | 2015-06-21 19:11:50 +0800 | [diff] [blame] | 202 | crypto_put_default_rng(); |
Herbert Xu | f261c5f | 2015-06-03 14:49:24 +0800 | [diff] [blame] | 203 | if (err) |
| 204 | goto out; |
| 205 | |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 206 | ctx->null = crypto_get_default_null_skcipher(); |
| 207 | err = PTR_ERR(ctx->null); |
| 208 | if (IS_ERR(ctx->null)) |
| 209 | goto out; |
| 210 | |
Herbert Xu | 5499b1a | 2015-07-09 07:17:20 +0800 | [diff] [blame] | 211 | err = aead_geniv_init(crypto_aead_tfm(geniv)); |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 212 | if (err) |
| 213 | goto drop_null; |
| 214 | |
Herbert Xu | d97de47 | 2015-05-27 14:37:33 +0800 | [diff] [blame] | 215 | ctx->geniv.child = geniv->child; |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 216 | geniv->child = geniv; |
| 217 | |
| 218 | out: |
| 219 | return err; |
| 220 | |
| 221 | drop_null: |
| 222 | crypto_put_default_null_skcipher(); |
| 223 | goto out; |
| 224 | } |
| 225 | |
Herbert Xu | 5499b1a | 2015-07-09 07:17:20 +0800 | [diff] [blame] | 226 | static void echainiv_exit(struct crypto_aead *tfm) |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 227 | { |
Herbert Xu | 5499b1a | 2015-07-09 07:17:20 +0800 | [diff] [blame] | 228 | struct echainiv_ctx *ctx = crypto_aead_ctx(tfm); |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 229 | |
Herbert Xu | d97de47 | 2015-05-27 14:37:33 +0800 | [diff] [blame] | 230 | crypto_free_aead(ctx->geniv.child); |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 231 | crypto_put_default_null_skcipher(); |
| 232 | } |
| 233 | |
Herbert Xu | 1e419c7 | 2015-05-23 15:41:52 +0800 | [diff] [blame] | 234 | static int echainiv_aead_create(struct crypto_template *tmpl, |
| 235 | struct rtattr **tb) |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 236 | { |
| 237 | struct aead_instance *inst; |
| 238 | struct crypto_aead_spawn *spawn; |
| 239 | struct aead_alg *alg; |
Herbert Xu | 1e419c7 | 2015-05-23 15:41:52 +0800 | [diff] [blame] | 240 | int err; |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 241 | |
Herbert Xu | 1e419c7 | 2015-05-23 15:41:52 +0800 | [diff] [blame] | 242 | inst = aead_geniv_alloc(tmpl, tb, 0, 0); |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 243 | |
| 244 | if (IS_ERR(inst)) |
Herbert Xu | 1e419c7 | 2015-05-23 15:41:52 +0800 | [diff] [blame] | 245 | return PTR_ERR(inst); |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 246 | |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 247 | spawn = aead_instance_ctx(inst); |
| 248 | alg = crypto_spawn_aead_alg(spawn); |
| 249 | |
Herbert Xu | d97de47 | 2015-05-27 14:37:33 +0800 | [diff] [blame] | 250 | err = -EINVAL; |
| 251 | if (inst->alg.ivsize & (sizeof(u32) - 1) || |
| 252 | inst->alg.ivsize > MAX_IV_SIZE) |
| 253 | goto free_inst; |
| 254 | |
Herbert Xu | f261c5f | 2015-06-03 14:49:24 +0800 | [diff] [blame] | 255 | inst->alg.encrypt = echainiv_encrypt; |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 256 | inst->alg.decrypt = echainiv_decrypt; |
| 257 | |
Herbert Xu | 5499b1a | 2015-07-09 07:17:20 +0800 | [diff] [blame] | 258 | inst->alg.init = echainiv_init; |
| 259 | inst->alg.exit = echainiv_exit; |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 260 | |
| 261 | inst->alg.base.cra_alignmask |= __alignof__(u32) - 1; |
| 262 | inst->alg.base.cra_ctxsize = sizeof(struct echainiv_ctx); |
Herbert Xu | 9d03aee | 2015-05-27 14:37:34 +0800 | [diff] [blame] | 263 | inst->alg.base.cra_ctxsize += inst->alg.ivsize; |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 264 | |
Herbert Xu | 5499b1a | 2015-07-09 07:17:20 +0800 | [diff] [blame] | 265 | inst->free = aead_geniv_free; |
| 266 | |
Herbert Xu | 1e419c7 | 2015-05-23 15:41:52 +0800 | [diff] [blame] | 267 | err = aead_register_instance(tmpl, inst); |
| 268 | if (err) |
| 269 | goto free_inst; |
| 270 | |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 271 | out: |
Herbert Xu | 1e419c7 | 2015-05-23 15:41:52 +0800 | [diff] [blame] | 272 | return err; |
| 273 | |
| 274 | free_inst: |
| 275 | aead_geniv_free(inst); |
| 276 | goto out; |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 277 | } |
| 278 | |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 279 | static void echainiv_free(struct crypto_instance *inst) |
| 280 | { |
| 281 | aead_geniv_free(aead_instance(inst)); |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | static struct crypto_template echainiv_tmpl = { |
| 285 | .name = "echainiv", |
Herbert Xu | 9fcc704 | 2015-06-21 19:11:50 +0800 | [diff] [blame] | 286 | .create = echainiv_aead_create, |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 287 | .free = echainiv_free, |
| 288 | .module = THIS_MODULE, |
| 289 | }; |
| 290 | |
| 291 | static int __init echainiv_module_init(void) |
| 292 | { |
| 293 | return crypto_register_template(&echainiv_tmpl); |
| 294 | } |
| 295 | |
| 296 | static void __exit echainiv_module_exit(void) |
| 297 | { |
| 298 | crypto_unregister_template(&echainiv_tmpl); |
| 299 | } |
| 300 | |
| 301 | module_init(echainiv_module_init); |
| 302 | module_exit(echainiv_module_exit); |
| 303 | |
| 304 | MODULE_LICENSE("GPL"); |
| 305 | MODULE_DESCRIPTION("Encrypted Chain IV Generator"); |
| 306 | MODULE_ALIAS_CRYPTO("echainiv"); |