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/scatterwalk.h> |
Herbert Xu | 0e8bff4 | 2016-07-12 13:17:43 +0800 | [diff] [blame] | 23 | #include <crypto/skcipher.h> |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 24 | #include <linux/err.h> |
| 25 | #include <linux/init.h> |
| 26 | #include <linux/kernel.h> |
| 27 | #include <linux/mm.h> |
| 28 | #include <linux/module.h> |
| 29 | #include <linux/percpu.h> |
| 30 | #include <linux/spinlock.h> |
| 31 | #include <linux/string.h> |
| 32 | |
| 33 | #define MAX_IV_SIZE 16 |
| 34 | |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 35 | static DEFINE_PER_CPU(u32 [MAX_IV_SIZE / sizeof(u32)], echainiv_iv); |
| 36 | |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 37 | /* 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] | 38 | static void echainiv_read_iv(u8 *dst, unsigned size) |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 39 | { |
| 40 | u32 *a = (u32 *)dst; |
| 41 | u32 __percpu *b = echainiv_iv; |
| 42 | |
| 43 | for (; size >= 4; size -= 4) { |
| 44 | *a++ = this_cpu_read(*b); |
| 45 | b++; |
| 46 | } |
| 47 | } |
| 48 | |
Wu Fengguang | 622ff87 | 2015-05-23 11:22:47 +0800 | [diff] [blame] | 49 | static void echainiv_write_iv(const u8 *src, unsigned size) |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 50 | { |
| 51 | const u32 *a = (const u32 *)src; |
| 52 | u32 __percpu *b = echainiv_iv; |
| 53 | |
| 54 | for (; size >= 4; size -= 4) { |
| 55 | this_cpu_write(*b, *a); |
| 56 | a++; |
| 57 | b++; |
| 58 | } |
| 59 | } |
| 60 | |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 61 | static void echainiv_encrypt_complete2(struct aead_request *req, int err) |
| 62 | { |
| 63 | struct aead_request *subreq = aead_request_ctx(req); |
| 64 | struct crypto_aead *geniv; |
| 65 | unsigned int ivsize; |
| 66 | |
| 67 | if (err == -EINPROGRESS) |
| 68 | return; |
| 69 | |
| 70 | if (err) |
| 71 | goto out; |
| 72 | |
| 73 | geniv = crypto_aead_reqtfm(req); |
| 74 | ivsize = crypto_aead_ivsize(geniv); |
| 75 | |
| 76 | echainiv_write_iv(subreq->iv, ivsize); |
| 77 | |
| 78 | if (req->iv != subreq->iv) |
| 79 | memcpy(req->iv, subreq->iv, ivsize); |
| 80 | |
| 81 | out: |
| 82 | if (req->iv != subreq->iv) |
| 83 | kzfree(subreq->iv); |
| 84 | } |
| 85 | |
| 86 | static void echainiv_encrypt_complete(struct crypto_async_request *base, |
| 87 | int err) |
| 88 | { |
| 89 | struct aead_request *req = base->data; |
| 90 | |
| 91 | echainiv_encrypt_complete2(req, err); |
| 92 | aead_request_complete(req, err); |
| 93 | } |
| 94 | |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 95 | static int echainiv_encrypt(struct aead_request *req) |
| 96 | { |
| 97 | struct crypto_aead *geniv = crypto_aead_reqtfm(req); |
Herbert Xu | 376e0d6 | 2015-08-13 17:29:01 +0800 | [diff] [blame] | 98 | struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv); |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 99 | struct aead_request *subreq = aead_request_ctx(req); |
| 100 | crypto_completion_t compl; |
| 101 | void *data; |
| 102 | u8 *info; |
Herbert Xu | 823655c | 2015-05-23 15:41:54 +0800 | [diff] [blame] | 103 | unsigned int ivsize = crypto_aead_ivsize(geniv); |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 104 | int err; |
| 105 | |
Herbert Xu | 823655c | 2015-05-23 15:41:54 +0800 | [diff] [blame] | 106 | if (req->cryptlen < ivsize) |
| 107 | return -EINVAL; |
| 108 | |
Herbert Xu | 376e0d6 | 2015-08-13 17:29:01 +0800 | [diff] [blame] | 109 | aead_request_set_tfm(subreq, ctx->child); |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 110 | |
| 111 | compl = echainiv_encrypt_complete; |
| 112 | data = req; |
| 113 | info = req->iv; |
| 114 | |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 115 | if (req->src != req->dst) { |
Herbert Xu | 0e8bff4 | 2016-07-12 13:17:43 +0800 | [diff] [blame] | 116 | SKCIPHER_REQUEST_ON_STACK(nreq, ctx->sknull); |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 117 | |
Herbert Xu | 0e8bff4 | 2016-07-12 13:17:43 +0800 | [diff] [blame] | 118 | skcipher_request_set_tfm(nreq, ctx->sknull); |
| 119 | skcipher_request_set_callback(nreq, req->base.flags, |
| 120 | NULL, NULL); |
| 121 | skcipher_request_set_crypt(nreq, req->src, req->dst, |
| 122 | req->assoclen + req->cryptlen, |
| 123 | NULL); |
| 124 | |
| 125 | err = crypto_skcipher_encrypt(nreq); |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 126 | if (err) |
| 127 | return err; |
| 128 | } |
| 129 | |
| 130 | if (unlikely(!IS_ALIGNED((unsigned long)info, |
| 131 | crypto_aead_alignmask(geniv) + 1))) { |
| 132 | info = kmalloc(ivsize, req->base.flags & |
| 133 | CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL: |
| 134 | GFP_ATOMIC); |
| 135 | if (!info) |
| 136 | return -ENOMEM; |
| 137 | |
| 138 | memcpy(info, req->iv, ivsize); |
| 139 | } |
| 140 | |
| 141 | aead_request_set_callback(subreq, req->base.flags, compl, data); |
| 142 | aead_request_set_crypt(subreq, req->dst, req->dst, |
Herbert Xu | 5499b1a | 2015-07-09 07:17:20 +0800 | [diff] [blame] | 143 | req->cryptlen, info); |
| 144 | aead_request_set_ad(subreq, req->assoclen); |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 145 | |
| 146 | crypto_xor(info, ctx->salt, ivsize); |
| 147 | scatterwalk_map_and_copy(info, req->dst, req->assoclen, ivsize, 1); |
| 148 | echainiv_read_iv(info, ivsize); |
| 149 | |
| 150 | err = crypto_aead_encrypt(subreq); |
| 151 | echainiv_encrypt_complete2(req, err); |
| 152 | return err; |
| 153 | } |
| 154 | |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 155 | static int echainiv_decrypt(struct aead_request *req) |
| 156 | { |
| 157 | struct crypto_aead *geniv = crypto_aead_reqtfm(req); |
Herbert Xu | 376e0d6 | 2015-08-13 17:29:01 +0800 | [diff] [blame] | 158 | struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv); |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 159 | struct aead_request *subreq = aead_request_ctx(req); |
| 160 | crypto_completion_t compl; |
| 161 | void *data; |
Herbert Xu | 823655c | 2015-05-23 15:41:54 +0800 | [diff] [blame] | 162 | unsigned int ivsize = crypto_aead_ivsize(geniv); |
| 163 | |
Herbert Xu | 5499b1a | 2015-07-09 07:17:20 +0800 | [diff] [blame] | 164 | if (req->cryptlen < ivsize) |
Herbert Xu | 823655c | 2015-05-23 15:41:54 +0800 | [diff] [blame] | 165 | return -EINVAL; |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 166 | |
Herbert Xu | 376e0d6 | 2015-08-13 17:29:01 +0800 | [diff] [blame] | 167 | aead_request_set_tfm(subreq, ctx->child); |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 168 | |
| 169 | compl = req->base.complete; |
| 170 | data = req->base.data; |
| 171 | |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 172 | aead_request_set_callback(subreq, req->base.flags, compl, data); |
| 173 | aead_request_set_crypt(subreq, req->src, req->dst, |
| 174 | req->cryptlen - ivsize, req->iv); |
Herbert Xu | 374d4ad | 2015-05-23 15:41:57 +0800 | [diff] [blame] | 175 | aead_request_set_ad(subreq, req->assoclen + ivsize); |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 176 | |
| 177 | scatterwalk_map_and_copy(req->iv, req->src, req->assoclen, ivsize, 0); |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 178 | |
| 179 | return crypto_aead_decrypt(subreq); |
| 180 | } |
| 181 | |
Herbert Xu | 1e419c7 | 2015-05-23 15:41:52 +0800 | [diff] [blame] | 182 | static int echainiv_aead_create(struct crypto_template *tmpl, |
| 183 | struct rtattr **tb) |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 184 | { |
| 185 | struct aead_instance *inst; |
| 186 | struct crypto_aead_spawn *spawn; |
| 187 | struct aead_alg *alg; |
Herbert Xu | 1e419c7 | 2015-05-23 15:41:52 +0800 | [diff] [blame] | 188 | int err; |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 189 | |
Herbert Xu | 1e419c7 | 2015-05-23 15:41:52 +0800 | [diff] [blame] | 190 | inst = aead_geniv_alloc(tmpl, tb, 0, 0); |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 191 | |
| 192 | if (IS_ERR(inst)) |
Herbert Xu | 1e419c7 | 2015-05-23 15:41:52 +0800 | [diff] [blame] | 193 | return PTR_ERR(inst); |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 194 | |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 195 | spawn = aead_instance_ctx(inst); |
| 196 | alg = crypto_spawn_aead_alg(spawn); |
| 197 | |
Herbert Xu | d97de47 | 2015-05-27 14:37:33 +0800 | [diff] [blame] | 198 | err = -EINVAL; |
| 199 | if (inst->alg.ivsize & (sizeof(u32) - 1) || |
| 200 | inst->alg.ivsize > MAX_IV_SIZE) |
| 201 | goto free_inst; |
| 202 | |
Herbert Xu | f261c5f | 2015-06-03 14:49:24 +0800 | [diff] [blame] | 203 | inst->alg.encrypt = echainiv_encrypt; |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 204 | inst->alg.decrypt = echainiv_decrypt; |
| 205 | |
Herbert Xu | 376e0d6 | 2015-08-13 17:29:01 +0800 | [diff] [blame] | 206 | inst->alg.init = aead_init_geniv; |
| 207 | inst->alg.exit = aead_exit_geniv; |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 208 | |
| 209 | inst->alg.base.cra_alignmask |= __alignof__(u32) - 1; |
Herbert Xu | 376e0d6 | 2015-08-13 17:29:01 +0800 | [diff] [blame] | 210 | inst->alg.base.cra_ctxsize = sizeof(struct aead_geniv_ctx); |
Herbert Xu | 9d03aee | 2015-05-27 14:37:34 +0800 | [diff] [blame] | 211 | inst->alg.base.cra_ctxsize += inst->alg.ivsize; |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 212 | |
Herbert Xu | 5499b1a | 2015-07-09 07:17:20 +0800 | [diff] [blame] | 213 | inst->free = aead_geniv_free; |
| 214 | |
Herbert Xu | 1e419c7 | 2015-05-23 15:41:52 +0800 | [diff] [blame] | 215 | err = aead_register_instance(tmpl, inst); |
| 216 | if (err) |
| 217 | goto free_inst; |
| 218 | |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 219 | out: |
Herbert Xu | 1e419c7 | 2015-05-23 15:41:52 +0800 | [diff] [blame] | 220 | return err; |
| 221 | |
| 222 | free_inst: |
| 223 | aead_geniv_free(inst); |
| 224 | goto out; |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 225 | } |
| 226 | |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 227 | static void echainiv_free(struct crypto_instance *inst) |
| 228 | { |
| 229 | aead_geniv_free(aead_instance(inst)); |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | static struct crypto_template echainiv_tmpl = { |
| 233 | .name = "echainiv", |
Herbert Xu | 9fcc704 | 2015-06-21 19:11:50 +0800 | [diff] [blame] | 234 | .create = echainiv_aead_create, |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 235 | .free = echainiv_free, |
| 236 | .module = THIS_MODULE, |
| 237 | }; |
| 238 | |
| 239 | static int __init echainiv_module_init(void) |
| 240 | { |
| 241 | return crypto_register_template(&echainiv_tmpl); |
| 242 | } |
| 243 | |
| 244 | static void __exit echainiv_module_exit(void) |
| 245 | { |
| 246 | crypto_unregister_template(&echainiv_tmpl); |
| 247 | } |
| 248 | |
| 249 | module_init(echainiv_module_init); |
| 250 | module_exit(echainiv_module_exit); |
| 251 | |
| 252 | MODULE_LICENSE("GPL"); |
| 253 | MODULE_DESCRIPTION("Encrypted Chain IV Generator"); |
| 254 | MODULE_ALIAS_CRYPTO("echainiv"); |