Kyle Swenson | 8d8f654 | 2021-03-15 11:02:55 -0600 | [diff] [blame] | 1 | /* |
| 2 | * echainiv: Encrypted Chain IV Generator |
| 3 | * |
| 4 | * This generator generates an IV based on a sequence number by multiplying |
| 5 | * it 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 | |
| 21 | #include <crypto/internal/geniv.h> |
| 22 | #include <crypto/scatterwalk.h> |
| 23 | #include <linux/err.h> |
| 24 | #include <linux/init.h> |
| 25 | #include <linux/kernel.h> |
| 26 | #include <linux/module.h> |
| 27 | #include <linux/slab.h> |
| 28 | #include <linux/string.h> |
| 29 | |
| 30 | static int echainiv_encrypt(struct aead_request *req) |
| 31 | { |
| 32 | struct crypto_aead *geniv = crypto_aead_reqtfm(req); |
| 33 | struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv); |
| 34 | struct aead_request *subreq = aead_request_ctx(req); |
| 35 | __be64 nseqno; |
| 36 | u64 seqno; |
| 37 | u8 *info; |
| 38 | unsigned int ivsize = crypto_aead_ivsize(geniv); |
| 39 | int err; |
| 40 | |
| 41 | if (req->cryptlen < ivsize) |
| 42 | return -EINVAL; |
| 43 | |
| 44 | aead_request_set_tfm(subreq, ctx->child); |
| 45 | |
| 46 | info = req->iv; |
| 47 | |
| 48 | if (req->src != req->dst) { |
| 49 | struct blkcipher_desc desc = { |
| 50 | .tfm = ctx->null, |
| 51 | }; |
| 52 | |
| 53 | err = crypto_blkcipher_encrypt( |
| 54 | &desc, req->dst, req->src, |
| 55 | req->assoclen + req->cryptlen); |
| 56 | if (err) |
| 57 | return err; |
| 58 | } |
| 59 | |
| 60 | aead_request_set_callback(subreq, req->base.flags, |
| 61 | req->base.complete, req->base.data); |
| 62 | aead_request_set_crypt(subreq, req->dst, req->dst, |
| 63 | req->cryptlen, info); |
| 64 | aead_request_set_ad(subreq, req->assoclen); |
| 65 | |
| 66 | memcpy(&nseqno, info + ivsize - 8, 8); |
| 67 | seqno = be64_to_cpu(nseqno); |
| 68 | memset(info, 0, ivsize); |
| 69 | |
| 70 | scatterwalk_map_and_copy(info, req->dst, req->assoclen, ivsize, 1); |
| 71 | |
| 72 | do { |
| 73 | u64 a; |
| 74 | |
| 75 | memcpy(&a, ctx->salt + ivsize - 8, 8); |
| 76 | |
| 77 | a |= 1; |
| 78 | a *= seqno; |
| 79 | |
| 80 | memcpy(info + ivsize - 8, &a, 8); |
| 81 | } while ((ivsize -= 8)); |
| 82 | |
| 83 | return crypto_aead_encrypt(subreq); |
| 84 | } |
| 85 | |
| 86 | static int echainiv_decrypt(struct aead_request *req) |
| 87 | { |
| 88 | struct crypto_aead *geniv = crypto_aead_reqtfm(req); |
| 89 | struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv); |
| 90 | struct aead_request *subreq = aead_request_ctx(req); |
| 91 | crypto_completion_t compl; |
| 92 | void *data; |
| 93 | unsigned int ivsize = crypto_aead_ivsize(geniv); |
| 94 | |
| 95 | if (req->cryptlen < ivsize) |
| 96 | return -EINVAL; |
| 97 | |
| 98 | aead_request_set_tfm(subreq, ctx->child); |
| 99 | |
| 100 | compl = req->base.complete; |
| 101 | data = req->base.data; |
| 102 | |
| 103 | aead_request_set_callback(subreq, req->base.flags, compl, data); |
| 104 | aead_request_set_crypt(subreq, req->src, req->dst, |
| 105 | req->cryptlen - ivsize, req->iv); |
| 106 | aead_request_set_ad(subreq, req->assoclen + ivsize); |
| 107 | |
| 108 | scatterwalk_map_and_copy(req->iv, req->src, req->assoclen, ivsize, 0); |
| 109 | |
| 110 | return crypto_aead_decrypt(subreq); |
| 111 | } |
| 112 | |
| 113 | static int echainiv_aead_create(struct crypto_template *tmpl, |
| 114 | struct rtattr **tb) |
| 115 | { |
| 116 | struct aead_instance *inst; |
| 117 | struct crypto_aead_spawn *spawn; |
| 118 | struct aead_alg *alg; |
| 119 | int err; |
| 120 | |
| 121 | inst = aead_geniv_alloc(tmpl, tb, 0, 0); |
| 122 | |
| 123 | if (IS_ERR(inst)) |
| 124 | return PTR_ERR(inst); |
| 125 | |
| 126 | spawn = aead_instance_ctx(inst); |
| 127 | alg = crypto_spawn_aead_alg(spawn); |
| 128 | |
| 129 | err = -EINVAL; |
| 130 | if (inst->alg.ivsize & (sizeof(u64) - 1) || !inst->alg.ivsize) |
| 131 | goto free_inst; |
| 132 | |
| 133 | inst->alg.encrypt = echainiv_encrypt; |
| 134 | inst->alg.decrypt = echainiv_decrypt; |
| 135 | |
| 136 | inst->alg.init = aead_init_geniv; |
| 137 | inst->alg.exit = aead_exit_geniv; |
| 138 | |
| 139 | inst->alg.base.cra_ctxsize = sizeof(struct aead_geniv_ctx); |
| 140 | inst->alg.base.cra_ctxsize += inst->alg.ivsize; |
| 141 | |
| 142 | inst->free = aead_geniv_free; |
| 143 | |
| 144 | err = aead_register_instance(tmpl, inst); |
| 145 | if (err) |
| 146 | goto free_inst; |
| 147 | |
| 148 | out: |
| 149 | return err; |
| 150 | |
| 151 | free_inst: |
| 152 | aead_geniv_free(inst); |
| 153 | goto out; |
| 154 | } |
| 155 | |
| 156 | static void echainiv_free(struct crypto_instance *inst) |
| 157 | { |
| 158 | aead_geniv_free(aead_instance(inst)); |
| 159 | } |
| 160 | |
| 161 | static struct crypto_template echainiv_tmpl = { |
| 162 | .name = "echainiv", |
| 163 | .create = echainiv_aead_create, |
| 164 | .free = echainiv_free, |
| 165 | .module = THIS_MODULE, |
| 166 | }; |
| 167 | |
| 168 | static int __init echainiv_module_init(void) |
| 169 | { |
| 170 | return crypto_register_template(&echainiv_tmpl); |
| 171 | } |
| 172 | |
| 173 | static void __exit echainiv_module_exit(void) |
| 174 | { |
| 175 | crypto_unregister_template(&echainiv_tmpl); |
| 176 | } |
| 177 | |
| 178 | module_init(echainiv_module_init); |
| 179 | module_exit(echainiv_module_exit); |
| 180 | |
| 181 | MODULE_LICENSE("GPL"); |
| 182 | MODULE_DESCRIPTION("Encrypted Chain IV Generator"); |
| 183 | MODULE_ALIAS_CRYPTO("echainiv"); |