Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 2 | /* |
| 3 | * GCM: Galois/Counter Mode. |
| 4 | * |
| 5 | * Copyright (c) 2007 Nokia Siemens Networks - Mikko Herranen <mh1@iki.fi> |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 6 | */ |
| 7 | |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 8 | #include <crypto/gf128mul.h> |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 9 | #include <crypto/internal/aead.h> |
Herbert Xu | 1472e5e | 2007-12-07 19:26:11 +0800 | [diff] [blame] | 10 | #include <crypto/internal/skcipher.h> |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 11 | #include <crypto/internal/hash.h> |
Herbert Xu | 17db854 | 2015-05-21 15:11:11 +0800 | [diff] [blame] | 12 | #include <crypto/null.h> |
Herbert Xu | 42c271c | 2007-12-07 18:52:49 +0800 | [diff] [blame] | 13 | #include <crypto/scatterwalk.h> |
Corentin LABBE | e0ab7e9 | 2017-08-22 10:08:17 +0200 | [diff] [blame] | 14 | #include <crypto/gcm.h> |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 15 | #include <crypto/hash.h> |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 16 | #include <linux/err.h> |
| 17 | #include <linux/init.h> |
| 18 | #include <linux/kernel.h> |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/slab.h> |
| 21 | |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 22 | struct gcm_instance_ctx { |
Herbert Xu | 1472e5e | 2007-12-07 19:26:11 +0800 | [diff] [blame] | 23 | struct crypto_skcipher_spawn ctr; |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 24 | struct crypto_ahash_spawn ghash; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 25 | }; |
| 26 | |
| 27 | struct crypto_gcm_ctx { |
Herbert Xu | 16f37ecd | 2016-07-12 13:17:39 +0800 | [diff] [blame] | 28 | struct crypto_skcipher *ctr; |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 29 | struct crypto_ahash *ghash; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 30 | }; |
| 31 | |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 32 | struct crypto_rfc4106_ctx { |
| 33 | struct crypto_aead *child; |
| 34 | u8 nonce[4]; |
| 35 | }; |
| 36 | |
Herbert Xu | 7b05a37 | 2015-07-09 07:17:30 +0800 | [diff] [blame] | 37 | struct crypto_rfc4106_req_ctx { |
| 38 | struct scatterlist src[3]; |
| 39 | struct scatterlist dst[3]; |
| 40 | struct aead_request subreq; |
| 41 | }; |
| 42 | |
Jussi Kivilinna | 9489667d | 2013-04-07 16:43:41 +0300 | [diff] [blame] | 43 | struct crypto_rfc4543_instance_ctx { |
| 44 | struct crypto_aead_spawn aead; |
Jussi Kivilinna | 9489667d | 2013-04-07 16:43:41 +0300 | [diff] [blame] | 45 | }; |
| 46 | |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 47 | struct crypto_rfc4543_ctx { |
| 48 | struct crypto_aead *child; |
Kees Cook | 8d60539 | 2018-09-18 19:10:51 -0700 | [diff] [blame] | 49 | struct crypto_sync_skcipher *null; |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 50 | u8 nonce[4]; |
| 51 | }; |
| 52 | |
| 53 | struct crypto_rfc4543_req_ctx { |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 54 | struct aead_request subreq; |
| 55 | }; |
| 56 | |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 57 | struct crypto_gcm_ghash_ctx { |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 58 | unsigned int cryptlen; |
| 59 | struct scatterlist *src; |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 60 | int (*complete)(struct aead_request *req, u32 flags); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | struct crypto_gcm_req_priv_ctx { |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 64 | u8 iv[16]; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 65 | u8 auth_tag[16]; |
Herbert Xu | 6160b28 | 2007-12-04 19:17:50 +1100 | [diff] [blame] | 66 | u8 iauth_tag[16]; |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 67 | struct scatterlist src[3]; |
| 68 | struct scatterlist dst[3]; |
| 69 | struct scatterlist sg; |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 70 | struct crypto_gcm_ghash_ctx ghash_ctx; |
| 71 | union { |
| 72 | struct ahash_request ahreq; |
Herbert Xu | 16f37ecd | 2016-07-12 13:17:39 +0800 | [diff] [blame] | 73 | struct skcipher_request skreq; |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 74 | } u; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 75 | }; |
| 76 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 77 | static struct { |
| 78 | u8 buf[16]; |
| 79 | struct scatterlist sg; |
| 80 | } *gcm_zeroes; |
| 81 | |
| 82 | static int crypto_rfc4543_copy_src_to_dst(struct aead_request *req, bool enc); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 83 | |
Herbert Xu | 2589469 | 2007-12-12 19:16:38 +0800 | [diff] [blame] | 84 | static inline struct crypto_gcm_req_priv_ctx *crypto_gcm_reqctx( |
| 85 | struct aead_request *req) |
| 86 | { |
| 87 | unsigned long align = crypto_aead_alignmask(crypto_aead_reqtfm(req)); |
| 88 | |
| 89 | return (void *)PTR_ALIGN((u8 *)aead_request_ctx(req), align + 1); |
| 90 | } |
| 91 | |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 92 | static int crypto_gcm_setkey(struct crypto_aead *aead, const u8 *key, |
| 93 | unsigned int keylen) |
| 94 | { |
| 95 | struct crypto_gcm_ctx *ctx = crypto_aead_ctx(aead); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 96 | struct crypto_ahash *ghash = ctx->ghash; |
Herbert Xu | 16f37ecd | 2016-07-12 13:17:39 +0800 | [diff] [blame] | 97 | struct crypto_skcipher *ctr = ctx->ctr; |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 98 | struct { |
| 99 | be128 hash; |
Ondrej Mosnáček | 50d2e6d | 2016-09-23 10:47:32 +0200 | [diff] [blame] | 100 | u8 iv[16]; |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 101 | |
Gilad Ben-Yossef | 76c6739 | 2017-10-18 08:00:42 +0100 | [diff] [blame] | 102 | struct crypto_wait wait; |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 103 | |
| 104 | struct scatterlist sg[1]; |
Herbert Xu | 16f37ecd | 2016-07-12 13:17:39 +0800 | [diff] [blame] | 105 | struct skcipher_request req; |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 106 | } *data; |
| 107 | int err; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 108 | |
Herbert Xu | 16f37ecd | 2016-07-12 13:17:39 +0800 | [diff] [blame] | 109 | crypto_skcipher_clear_flags(ctr, CRYPTO_TFM_REQ_MASK); |
| 110 | crypto_skcipher_set_flags(ctr, crypto_aead_get_flags(aead) & |
| 111 | CRYPTO_TFM_REQ_MASK); |
| 112 | err = crypto_skcipher_setkey(ctr, key, keylen); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 113 | if (err) |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 114 | return err; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 115 | |
Herbert Xu | 16f37ecd | 2016-07-12 13:17:39 +0800 | [diff] [blame] | 116 | data = kzalloc(sizeof(*data) + crypto_skcipher_reqsize(ctr), |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 117 | GFP_KERNEL); |
| 118 | if (!data) |
| 119 | return -ENOMEM; |
| 120 | |
Gilad Ben-Yossef | 76c6739 | 2017-10-18 08:00:42 +0100 | [diff] [blame] | 121 | crypto_init_wait(&data->wait); |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 122 | sg_init_one(data->sg, &data->hash, sizeof(data->hash)); |
Herbert Xu | 16f37ecd | 2016-07-12 13:17:39 +0800 | [diff] [blame] | 123 | skcipher_request_set_tfm(&data->req, ctr); |
| 124 | skcipher_request_set_callback(&data->req, CRYPTO_TFM_REQ_MAY_SLEEP | |
| 125 | CRYPTO_TFM_REQ_MAY_BACKLOG, |
Gilad Ben-Yossef | 76c6739 | 2017-10-18 08:00:42 +0100 | [diff] [blame] | 126 | crypto_req_done, |
| 127 | &data->wait); |
Herbert Xu | 16f37ecd | 2016-07-12 13:17:39 +0800 | [diff] [blame] | 128 | skcipher_request_set_crypt(&data->req, data->sg, data->sg, |
| 129 | sizeof(data->hash), data->iv); |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 130 | |
Gilad Ben-Yossef | 76c6739 | 2017-10-18 08:00:42 +0100 | [diff] [blame] | 131 | err = crypto_wait_req(crypto_skcipher_encrypt(&data->req), |
| 132 | &data->wait); |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 133 | |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 134 | if (err) |
| 135 | goto out; |
| 136 | |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 137 | crypto_ahash_clear_flags(ghash, CRYPTO_TFM_REQ_MASK); |
| 138 | crypto_ahash_set_flags(ghash, crypto_aead_get_flags(aead) & |
| 139 | CRYPTO_TFM_REQ_MASK); |
| 140 | err = crypto_ahash_setkey(ghash, (u8 *)&data->hash, sizeof(be128)); |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 141 | out: |
Waiman Long | 453431a | 2020-08-06 23:18:13 -0700 | [diff] [blame] | 142 | kfree_sensitive(data); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 143 | return err; |
| 144 | } |
| 145 | |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 146 | static int crypto_gcm_setauthsize(struct crypto_aead *tfm, |
| 147 | unsigned int authsize) |
| 148 | { |
Iuliana Prodan | 65526f6 | 2019-07-31 16:05:54 +0300 | [diff] [blame] | 149 | return crypto_gcm_check_authsize(authsize); |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 150 | } |
| 151 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 152 | static void crypto_gcm_init_common(struct aead_request *req) |
| 153 | { |
| 154 | struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req); |
| 155 | __be32 counter = cpu_to_be32(1); |
| 156 | struct scatterlist *sg; |
| 157 | |
| 158 | memset(pctx->auth_tag, 0, sizeof(pctx->auth_tag)); |
Corentin LABBE | e0ab7e9 | 2017-08-22 10:08:17 +0200 | [diff] [blame] | 159 | memcpy(pctx->iv, req->iv, GCM_AES_IV_SIZE); |
| 160 | memcpy(pctx->iv + GCM_AES_IV_SIZE, &counter, 4); |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 161 | |
| 162 | sg_init_table(pctx->src, 3); |
| 163 | sg_set_buf(pctx->src, pctx->auth_tag, sizeof(pctx->auth_tag)); |
| 164 | sg = scatterwalk_ffwd(pctx->src + 1, req->src, req->assoclen); |
| 165 | if (sg != pctx->src + 1) |
Dan Williams | c56f6d1 | 2015-08-07 18:15:13 +0200 | [diff] [blame] | 166 | sg_chain(pctx->src, 2, sg); |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 167 | |
| 168 | if (req->src != req->dst) { |
| 169 | sg_init_table(pctx->dst, 3); |
| 170 | sg_set_buf(pctx->dst, pctx->auth_tag, sizeof(pctx->auth_tag)); |
| 171 | sg = scatterwalk_ffwd(pctx->dst + 1, req->dst, req->assoclen); |
| 172 | if (sg != pctx->dst + 1) |
Dan Williams | c56f6d1 | 2015-08-07 18:15:13 +0200 | [diff] [blame] | 173 | sg_chain(pctx->dst, 2, sg); |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | |
| 177 | static void crypto_gcm_init_crypt(struct aead_request *req, |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 178 | unsigned int cryptlen) |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 179 | { |
| 180 | struct crypto_aead *aead = crypto_aead_reqtfm(req); |
| 181 | struct crypto_gcm_ctx *ctx = crypto_aead_ctx(aead); |
Herbert Xu | 2589469 | 2007-12-12 19:16:38 +0800 | [diff] [blame] | 182 | struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req); |
Herbert Xu | 16f37ecd | 2016-07-12 13:17:39 +0800 | [diff] [blame] | 183 | struct skcipher_request *skreq = &pctx->u.skreq; |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 184 | struct scatterlist *dst; |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 185 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 186 | dst = req->src == req->dst ? pctx->src : pctx->dst; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 187 | |
Herbert Xu | 16f37ecd | 2016-07-12 13:17:39 +0800 | [diff] [blame] | 188 | skcipher_request_set_tfm(skreq, ctx->ctr); |
| 189 | skcipher_request_set_crypt(skreq, pctx->src, dst, |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 190 | cryptlen + sizeof(pctx->auth_tag), |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 191 | pctx->iv); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 192 | } |
| 193 | |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 194 | static inline unsigned int gcm_remain(unsigned int len) |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 195 | { |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 196 | len &= 0xfU; |
| 197 | return len ? 16 - len : 0; |
| 198 | } |
| 199 | |
| 200 | static void gcm_hash_len_done(struct crypto_async_request *areq, int err); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 201 | |
| 202 | static int gcm_hash_update(struct aead_request *req, |
Mark Rustad | 3e3dc25 | 2014-07-25 02:53:38 -0700 | [diff] [blame] | 203 | crypto_completion_t compl, |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 204 | struct scatterlist *src, |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 205 | unsigned int len, u32 flags) |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 206 | { |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 207 | struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 208 | struct ahash_request *ahreq = &pctx->u.ahreq; |
| 209 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 210 | ahash_request_set_callback(ahreq, flags, compl, req); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 211 | ahash_request_set_crypt(ahreq, src, NULL, len); |
| 212 | |
| 213 | return crypto_ahash_update(ahreq); |
| 214 | } |
| 215 | |
| 216 | static int gcm_hash_remain(struct aead_request *req, |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 217 | unsigned int remain, |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 218 | crypto_completion_t compl, u32 flags) |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 219 | { |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 220 | return gcm_hash_update(req, compl, &gcm_zeroes->sg, remain, flags); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 221 | } |
| 222 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 223 | static int gcm_hash_len(struct aead_request *req, u32 flags) |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 224 | { |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 225 | struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 226 | struct ahash_request *ahreq = &pctx->u.ahreq; |
| 227 | struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx; |
Eric Biggers | 1866655 | 2019-01-10 12:17:52 -0800 | [diff] [blame] | 228 | be128 lengths; |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 229 | |
| 230 | lengths.a = cpu_to_be64(req->assoclen * 8); |
| 231 | lengths.b = cpu_to_be64(gctx->cryptlen * 8); |
| 232 | memcpy(pctx->iauth_tag, &lengths, 16); |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 233 | sg_init_one(&pctx->sg, pctx->iauth_tag, 16); |
| 234 | ahash_request_set_callback(ahreq, flags, gcm_hash_len_done, req); |
| 235 | ahash_request_set_crypt(ahreq, &pctx->sg, |
| 236 | pctx->iauth_tag, sizeof(lengths)); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 237 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 238 | return crypto_ahash_finup(ahreq); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 239 | } |
| 240 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 241 | static int gcm_hash_len_continue(struct aead_request *req, u32 flags) |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 242 | { |
Herbert Xu | 2589469 | 2007-12-12 19:16:38 +0800 | [diff] [blame] | 243 | struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 244 | struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 245 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 246 | return gctx->complete(req, flags); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 247 | } |
| 248 | |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 249 | static void gcm_hash_len_done(struct crypto_async_request *areq, int err) |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 250 | { |
| 251 | struct aead_request *req = areq->data; |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 252 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 253 | if (err) |
| 254 | goto out; |
| 255 | |
| 256 | err = gcm_hash_len_continue(req, 0); |
| 257 | if (err == -EINPROGRESS) |
| 258 | return; |
| 259 | |
| 260 | out: |
| 261 | aead_request_complete(req, err); |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 262 | } |
| 263 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 264 | static int gcm_hash_crypt_remain_continue(struct aead_request *req, u32 flags) |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 265 | { |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 266 | return gcm_hash_len(req, flags) ?: |
| 267 | gcm_hash_len_continue(req, flags); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 268 | } |
| 269 | |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 270 | static void gcm_hash_crypt_remain_done(struct crypto_async_request *areq, |
| 271 | int err) |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 272 | { |
| 273 | struct aead_request *req = areq->data; |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 274 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 275 | if (err) |
| 276 | goto out; |
| 277 | |
| 278 | err = gcm_hash_crypt_remain_continue(req, 0); |
| 279 | if (err == -EINPROGRESS) |
| 280 | return; |
| 281 | |
| 282 | out: |
| 283 | aead_request_complete(req, err); |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 284 | } |
| 285 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 286 | static int gcm_hash_crypt_continue(struct aead_request *req, u32 flags) |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 287 | { |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 288 | struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req); |
| 289 | struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx; |
| 290 | unsigned int remain; |
| 291 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 292 | remain = gcm_remain(gctx->cryptlen); |
| 293 | if (remain) |
| 294 | return gcm_hash_remain(req, remain, |
| 295 | gcm_hash_crypt_remain_done, flags) ?: |
| 296 | gcm_hash_crypt_remain_continue(req, flags); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 297 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 298 | return gcm_hash_crypt_remain_continue(req, flags); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 299 | } |
| 300 | |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 301 | static void gcm_hash_crypt_done(struct crypto_async_request *areq, int err) |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 302 | { |
| 303 | struct aead_request *req = areq->data; |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 304 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 305 | if (err) |
| 306 | goto out; |
| 307 | |
| 308 | err = gcm_hash_crypt_continue(req, 0); |
| 309 | if (err == -EINPROGRESS) |
| 310 | return; |
| 311 | |
| 312 | out: |
| 313 | aead_request_complete(req, err); |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 314 | } |
| 315 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 316 | static int gcm_hash_assoc_remain_continue(struct aead_request *req, u32 flags) |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 317 | { |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 318 | struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req); |
| 319 | struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx; |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 320 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 321 | if (gctx->cryptlen) |
| 322 | return gcm_hash_update(req, gcm_hash_crypt_done, |
| 323 | gctx->src, gctx->cryptlen, flags) ?: |
| 324 | gcm_hash_crypt_continue(req, flags); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 325 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 326 | return gcm_hash_crypt_remain_continue(req, flags); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 327 | } |
| 328 | |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 329 | static void gcm_hash_assoc_remain_done(struct crypto_async_request *areq, |
| 330 | int err) |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 331 | { |
| 332 | struct aead_request *req = areq->data; |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 333 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 334 | if (err) |
| 335 | goto out; |
| 336 | |
| 337 | err = gcm_hash_assoc_remain_continue(req, 0); |
| 338 | if (err == -EINPROGRESS) |
| 339 | return; |
| 340 | |
| 341 | out: |
| 342 | aead_request_complete(req, err); |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 343 | } |
| 344 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 345 | static int gcm_hash_assoc_continue(struct aead_request *req, u32 flags) |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 346 | { |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 347 | unsigned int remain; |
| 348 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 349 | remain = gcm_remain(req->assoclen); |
| 350 | if (remain) |
| 351 | return gcm_hash_remain(req, remain, |
| 352 | gcm_hash_assoc_remain_done, flags) ?: |
| 353 | gcm_hash_assoc_remain_continue(req, flags); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 354 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 355 | return gcm_hash_assoc_remain_continue(req, flags); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 356 | } |
| 357 | |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 358 | static void gcm_hash_assoc_done(struct crypto_async_request *areq, int err) |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 359 | { |
| 360 | struct aead_request *req = areq->data; |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 361 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 362 | if (err) |
| 363 | goto out; |
| 364 | |
| 365 | err = gcm_hash_assoc_continue(req, 0); |
| 366 | if (err == -EINPROGRESS) |
| 367 | return; |
| 368 | |
| 369 | out: |
| 370 | aead_request_complete(req, err); |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 371 | } |
| 372 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 373 | static int gcm_hash_init_continue(struct aead_request *req, u32 flags) |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 374 | { |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 375 | if (req->assoclen) |
| 376 | return gcm_hash_update(req, gcm_hash_assoc_done, |
| 377 | req->src, req->assoclen, flags) ?: |
| 378 | gcm_hash_assoc_continue(req, flags); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 379 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 380 | return gcm_hash_assoc_remain_continue(req, flags); |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | static void gcm_hash_init_done(struct crypto_async_request *areq, int err) |
| 384 | { |
| 385 | struct aead_request *req = areq->data; |
| 386 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 387 | if (err) |
| 388 | goto out; |
| 389 | |
| 390 | err = gcm_hash_init_continue(req, 0); |
| 391 | if (err == -EINPROGRESS) |
| 392 | return; |
| 393 | |
| 394 | out: |
| 395 | aead_request_complete(req, err); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 396 | } |
| 397 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 398 | static int gcm_hash(struct aead_request *req, u32 flags) |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 399 | { |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 400 | struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 401 | struct ahash_request *ahreq = &pctx->u.ahreq; |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 402 | struct crypto_gcm_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req)); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 403 | |
| 404 | ahash_request_set_tfm(ahreq, ctx->ghash); |
| 405 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 406 | ahash_request_set_callback(ahreq, flags, gcm_hash_init_done, req); |
| 407 | return crypto_ahash_init(ahreq) ?: |
| 408 | gcm_hash_init_continue(req, flags); |
Herbert Xu | 6160b28 | 2007-12-04 19:17:50 +1100 | [diff] [blame] | 409 | } |
| 410 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 411 | static int gcm_enc_copy_hash(struct aead_request *req, u32 flags) |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 412 | { |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 413 | struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 414 | struct crypto_aead *aead = crypto_aead_reqtfm(req); |
| 415 | u8 *auth_tag = pctx->auth_tag; |
| 416 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 417 | crypto_xor(auth_tag, pctx->iauth_tag, 16); |
| 418 | scatterwalk_map_and_copy(auth_tag, req->dst, |
| 419 | req->assoclen + req->cryptlen, |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 420 | crypto_aead_authsize(aead), 1); |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 421 | return 0; |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 422 | } |
| 423 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 424 | static int gcm_encrypt_continue(struct aead_request *req, u32 flags) |
Herbert Xu | 6160b28 | 2007-12-04 19:17:50 +1100 | [diff] [blame] | 425 | { |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 426 | struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req); |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 427 | struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx; |
Herbert Xu | 6160b28 | 2007-12-04 19:17:50 +1100 | [diff] [blame] | 428 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 429 | gctx->src = sg_next(req->src == req->dst ? pctx->src : pctx->dst); |
| 430 | gctx->cryptlen = req->cryptlen; |
| 431 | gctx->complete = gcm_enc_copy_hash; |
Herbert Xu | 6160b28 | 2007-12-04 19:17:50 +1100 | [diff] [blame] | 432 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 433 | return gcm_hash(req, flags); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 434 | } |
| 435 | |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 436 | static void gcm_encrypt_done(struct crypto_async_request *areq, int err) |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 437 | { |
| 438 | struct aead_request *req = areq->data; |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 439 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 440 | if (err) |
| 441 | goto out; |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 442 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 443 | err = gcm_encrypt_continue(req, 0); |
| 444 | if (err == -EINPROGRESS) |
| 445 | return; |
| 446 | |
| 447 | out: |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 448 | aead_request_complete(req, err); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 449 | } |
| 450 | |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 451 | static int crypto_gcm_encrypt(struct aead_request *req) |
| 452 | { |
Herbert Xu | 2589469 | 2007-12-12 19:16:38 +0800 | [diff] [blame] | 453 | struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req); |
Herbert Xu | 16f37ecd | 2016-07-12 13:17:39 +0800 | [diff] [blame] | 454 | struct skcipher_request *skreq = &pctx->u.skreq; |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 455 | u32 flags = aead_request_flags(req); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 456 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 457 | crypto_gcm_init_common(req); |
| 458 | crypto_gcm_init_crypt(req, req->cryptlen); |
Herbert Xu | 16f37ecd | 2016-07-12 13:17:39 +0800 | [diff] [blame] | 459 | skcipher_request_set_callback(skreq, flags, gcm_encrypt_done, req); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 460 | |
Herbert Xu | 16f37ecd | 2016-07-12 13:17:39 +0800 | [diff] [blame] | 461 | return crypto_skcipher_encrypt(skreq) ?: |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 462 | gcm_encrypt_continue(req, flags); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 463 | } |
| 464 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 465 | static int crypto_gcm_verify(struct aead_request *req) |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 466 | { |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 467 | struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req); |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 468 | struct crypto_aead *aead = crypto_aead_reqtfm(req); |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 469 | u8 *auth_tag = pctx->auth_tag; |
| 470 | u8 *iauth_tag = pctx->iauth_tag; |
| 471 | unsigned int authsize = crypto_aead_authsize(aead); |
| 472 | unsigned int cryptlen = req->cryptlen - authsize; |
| 473 | |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 474 | crypto_xor(auth_tag, iauth_tag, 16); |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 475 | scatterwalk_map_and_copy(iauth_tag, req->src, |
| 476 | req->assoclen + cryptlen, authsize, 0); |
James Yonan | 6bf37e5 | 2013-09-26 02:20:39 -0600 | [diff] [blame] | 477 | return crypto_memneq(iauth_tag, auth_tag, authsize) ? -EBADMSG : 0; |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 478 | } |
| 479 | |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 480 | static void gcm_decrypt_done(struct crypto_async_request *areq, int err) |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 481 | { |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 482 | struct aead_request *req = areq->data; |
| 483 | |
| 484 | if (!err) |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 485 | err = crypto_gcm_verify(req); |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 486 | |
| 487 | aead_request_complete(req, err); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 488 | } |
| 489 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 490 | static int gcm_dec_hash_continue(struct aead_request *req, u32 flags) |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 491 | { |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 492 | struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req); |
Herbert Xu | 16f37ecd | 2016-07-12 13:17:39 +0800 | [diff] [blame] | 493 | struct skcipher_request *skreq = &pctx->u.skreq; |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 494 | struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx; |
| 495 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 496 | crypto_gcm_init_crypt(req, gctx->cryptlen); |
Herbert Xu | 16f37ecd | 2016-07-12 13:17:39 +0800 | [diff] [blame] | 497 | skcipher_request_set_callback(skreq, flags, gcm_decrypt_done, req); |
| 498 | return crypto_skcipher_decrypt(skreq) ?: crypto_gcm_verify(req); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 499 | } |
| 500 | |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 501 | static int crypto_gcm_decrypt(struct aead_request *req) |
| 502 | { |
Herbert Xu | 6160b28 | 2007-12-04 19:17:50 +1100 | [diff] [blame] | 503 | struct crypto_aead *aead = crypto_aead_reqtfm(req); |
Herbert Xu | 2589469 | 2007-12-12 19:16:38 +0800 | [diff] [blame] | 504 | struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 505 | struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx; |
Herbert Xu | 6160b28 | 2007-12-04 19:17:50 +1100 | [diff] [blame] | 506 | unsigned int authsize = crypto_aead_authsize(aead); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 507 | unsigned int cryptlen = req->cryptlen; |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 508 | u32 flags = aead_request_flags(req); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 509 | |
Herbert Xu | 6160b28 | 2007-12-04 19:17:50 +1100 | [diff] [blame] | 510 | cryptlen -= authsize; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 511 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 512 | crypto_gcm_init_common(req); |
| 513 | |
| 514 | gctx->src = sg_next(pctx->src); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 515 | gctx->cryptlen = cryptlen; |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 516 | gctx->complete = gcm_dec_hash_continue; |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 517 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 518 | return gcm_hash(req, flags); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 519 | } |
| 520 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 521 | static int crypto_gcm_init_tfm(struct crypto_aead *tfm) |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 522 | { |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 523 | struct aead_instance *inst = aead_alg_instance(tfm); |
| 524 | struct gcm_instance_ctx *ictx = aead_instance_ctx(inst); |
| 525 | struct crypto_gcm_ctx *ctx = crypto_aead_ctx(tfm); |
Herbert Xu | 16f37ecd | 2016-07-12 13:17:39 +0800 | [diff] [blame] | 526 | struct crypto_skcipher *ctr; |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 527 | struct crypto_ahash *ghash; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 528 | unsigned long align; |
| 529 | int err; |
| 530 | |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 531 | ghash = crypto_spawn_ahash(&ictx->ghash); |
| 532 | if (IS_ERR(ghash)) |
| 533 | return PTR_ERR(ghash); |
| 534 | |
Eric Biggers | 60425a8 | 2016-10-28 09:52:19 -0700 | [diff] [blame] | 535 | ctr = crypto_spawn_skcipher(&ictx->ctr); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 536 | err = PTR_ERR(ctr); |
| 537 | if (IS_ERR(ctr)) |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 538 | goto err_free_hash; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 539 | |
| 540 | ctx->ctr = ctr; |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 541 | ctx->ghash = ghash; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 542 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 543 | align = crypto_aead_alignmask(tfm); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 544 | align &= ~(crypto_tfm_ctx_alignment() - 1); |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 545 | crypto_aead_set_reqsize(tfm, |
Herbert Xu | 5d72336 | 2015-05-11 17:47:59 +0800 | [diff] [blame] | 546 | align + offsetof(struct crypto_gcm_req_priv_ctx, u) + |
Herbert Xu | 16f37ecd | 2016-07-12 13:17:39 +0800 | [diff] [blame] | 547 | max(sizeof(struct skcipher_request) + |
| 548 | crypto_skcipher_reqsize(ctr), |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 549 | sizeof(struct ahash_request) + |
Herbert Xu | 5d72336 | 2015-05-11 17:47:59 +0800 | [diff] [blame] | 550 | crypto_ahash_reqsize(ghash))); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 551 | |
| 552 | return 0; |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 553 | |
| 554 | err_free_hash: |
| 555 | crypto_free_ahash(ghash); |
| 556 | return err; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 557 | } |
| 558 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 559 | static void crypto_gcm_exit_tfm(struct crypto_aead *tfm) |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 560 | { |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 561 | struct crypto_gcm_ctx *ctx = crypto_aead_ctx(tfm); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 562 | |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 563 | crypto_free_ahash(ctx->ghash); |
Herbert Xu | 16f37ecd | 2016-07-12 13:17:39 +0800 | [diff] [blame] | 564 | crypto_free_skcipher(ctx->ctr); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 565 | } |
| 566 | |
Herbert Xu | 7b05a37 | 2015-07-09 07:17:30 +0800 | [diff] [blame] | 567 | static void crypto_gcm_free(struct aead_instance *inst) |
| 568 | { |
| 569 | struct gcm_instance_ctx *ctx = aead_instance_ctx(inst); |
| 570 | |
| 571 | crypto_drop_skcipher(&ctx->ctr); |
| 572 | crypto_drop_ahash(&ctx->ghash); |
| 573 | kfree(inst); |
| 574 | } |
| 575 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 576 | static int crypto_gcm_create_common(struct crypto_template *tmpl, |
| 577 | struct rtattr **tb, |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 578 | const char *ctr_name, |
| 579 | const char *ghash_name) |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 580 | { |
Eric Biggers | b9f76dd | 2020-01-02 19:58:45 -0800 | [diff] [blame] | 581 | u32 mask; |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 582 | struct aead_instance *inst; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 583 | struct gcm_instance_ctx *ctx; |
Eric Biggers | ab6ffd3 | 2020-01-02 19:58:57 -0800 | [diff] [blame] | 584 | struct skcipher_alg *ctr; |
| 585 | struct hash_alg_common *ghash; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 586 | int err; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 587 | |
Eric Biggers | 7bcb2c9 | 2020-07-09 23:20:38 -0700 | [diff] [blame] | 588 | err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_AEAD, &mask); |
| 589 | if (err) |
| 590 | return err; |
Eric Biggers | b9f76dd | 2020-01-02 19:58:45 -0800 | [diff] [blame] | 591 | |
Herbert Xu | 1472e5e | 2007-12-07 19:26:11 +0800 | [diff] [blame] | 592 | inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL); |
| 593 | if (!inst) |
Eric Biggers | ab6ffd3 | 2020-01-02 19:58:57 -0800 | [diff] [blame] | 594 | return -ENOMEM; |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 595 | ctx = aead_instance_ctx(inst); |
Eric Biggers | ab6ffd3 | 2020-01-02 19:58:57 -0800 | [diff] [blame] | 596 | |
| 597 | err = crypto_grab_ahash(&ctx->ghash, aead_crypto_instance(inst), |
| 598 | ghash_name, 0, mask); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 599 | if (err) |
| 600 | goto err_free_inst; |
Eric Biggers | ab6ffd3 | 2020-01-02 19:58:57 -0800 | [diff] [blame] | 601 | ghash = crypto_spawn_ahash_alg(&ctx->ghash); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 602 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 603 | err = -EINVAL; |
Eric Biggers | f699594 | 2019-04-18 14:43:02 -0700 | [diff] [blame] | 604 | if (strcmp(ghash->base.cra_name, "ghash") != 0 || |
| 605 | ghash->digestsize != 16) |
Eric Biggers | ab6ffd3 | 2020-01-02 19:58:57 -0800 | [diff] [blame] | 606 | goto err_free_inst; |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 607 | |
Eric Biggers | b9f76dd | 2020-01-02 19:58:45 -0800 | [diff] [blame] | 608 | err = crypto_grab_skcipher(&ctx->ctr, aead_crypto_instance(inst), |
| 609 | ctr_name, 0, mask); |
Herbert Xu | 1472e5e | 2007-12-07 19:26:11 +0800 | [diff] [blame] | 610 | if (err) |
Eric Biggers | ab6ffd3 | 2020-01-02 19:58:57 -0800 | [diff] [blame] | 611 | goto err_free_inst; |
Herbert Xu | 16f37ecd | 2016-07-12 13:17:39 +0800 | [diff] [blame] | 612 | ctr = crypto_spawn_skcipher_alg(&ctx->ctr); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 613 | |
Eric Biggers | f699594 | 2019-04-18 14:43:02 -0700 | [diff] [blame] | 614 | /* The skcipher algorithm must be CTR mode, using 16-byte blocks. */ |
Wei Yongjun | 9b40f79 | 2016-10-17 15:10:06 +0000 | [diff] [blame] | 615 | err = -EINVAL; |
Eric Biggers | f699594 | 2019-04-18 14:43:02 -0700 | [diff] [blame] | 616 | if (strncmp(ctr->base.cra_name, "ctr(", 4) != 0 || |
| 617 | crypto_skcipher_alg_ivsize(ctr) != 16 || |
| 618 | ctr->base.cra_blocksize != 1) |
Eric Biggers | ab6ffd3 | 2020-01-02 19:58:57 -0800 | [diff] [blame] | 619 | goto err_free_inst; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 620 | |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 621 | err = -ENAMETOOLONG; |
Eric Biggers | f699594 | 2019-04-18 14:43:02 -0700 | [diff] [blame] | 622 | if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME, |
| 623 | "gcm(%s", ctr->base.cra_name + 4) >= CRYPTO_MAX_ALG_NAME) |
Eric Biggers | ab6ffd3 | 2020-01-02 19:58:57 -0800 | [diff] [blame] | 624 | goto err_free_inst; |
Eric Biggers | f699594 | 2019-04-18 14:43:02 -0700 | [diff] [blame] | 625 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 626 | if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME, |
Herbert Xu | 16f37ecd | 2016-07-12 13:17:39 +0800 | [diff] [blame] | 627 | "gcm_base(%s,%s)", ctr->base.cra_driver_name, |
Eric Biggers | ab6ffd3 | 2020-01-02 19:58:57 -0800 | [diff] [blame] | 628 | ghash->base.cra_driver_name) >= |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 629 | CRYPTO_MAX_ALG_NAME) |
Eric Biggers | ab6ffd3 | 2020-01-02 19:58:57 -0800 | [diff] [blame] | 630 | goto err_free_inst; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 631 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 632 | inst->alg.base.cra_priority = (ghash->base.cra_priority + |
Herbert Xu | 16f37ecd | 2016-07-12 13:17:39 +0800 | [diff] [blame] | 633 | ctr->base.cra_priority) / 2; |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 634 | inst->alg.base.cra_blocksize = 1; |
| 635 | inst->alg.base.cra_alignmask = ghash->base.cra_alignmask | |
Herbert Xu | 16f37ecd | 2016-07-12 13:17:39 +0800 | [diff] [blame] | 636 | ctr->base.cra_alignmask; |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 637 | inst->alg.base.cra_ctxsize = sizeof(struct crypto_gcm_ctx); |
Corentin LABBE | e0ab7e9 | 2017-08-22 10:08:17 +0200 | [diff] [blame] | 638 | inst->alg.ivsize = GCM_AES_IV_SIZE; |
Herbert Xu | 16f37ecd | 2016-07-12 13:17:39 +0800 | [diff] [blame] | 639 | inst->alg.chunksize = crypto_skcipher_alg_chunksize(ctr); |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 640 | inst->alg.maxauthsize = 16; |
| 641 | inst->alg.init = crypto_gcm_init_tfm; |
| 642 | inst->alg.exit = crypto_gcm_exit_tfm; |
| 643 | inst->alg.setkey = crypto_gcm_setkey; |
| 644 | inst->alg.setauthsize = crypto_gcm_setauthsize; |
| 645 | inst->alg.encrypt = crypto_gcm_encrypt; |
| 646 | inst->alg.decrypt = crypto_gcm_decrypt; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 647 | |
Herbert Xu | 7b05a37 | 2015-07-09 07:17:30 +0800 | [diff] [blame] | 648 | inst->free = crypto_gcm_free; |
| 649 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 650 | err = aead_register_instance(tmpl, inst); |
Eric Biggers | ab6ffd3 | 2020-01-02 19:58:57 -0800 | [diff] [blame] | 651 | if (err) { |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 652 | err_free_inst: |
Eric Biggers | ab6ffd3 | 2020-01-02 19:58:57 -0800 | [diff] [blame] | 653 | crypto_gcm_free(inst); |
| 654 | } |
| 655 | return err; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 656 | } |
| 657 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 658 | static int crypto_gcm_create(struct crypto_template *tmpl, struct rtattr **tb) |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 659 | { |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 660 | const char *cipher_name; |
| 661 | char ctr_name[CRYPTO_MAX_ALG_NAME]; |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 662 | |
| 663 | cipher_name = crypto_attr_alg_name(tb[1]); |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 664 | if (IS_ERR(cipher_name)) |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 665 | return PTR_ERR(cipher_name); |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 666 | |
| 667 | if (snprintf(ctr_name, CRYPTO_MAX_ALG_NAME, "ctr(%s)", cipher_name) >= |
| 668 | CRYPTO_MAX_ALG_NAME) |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 669 | return -ENAMETOOLONG; |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 670 | |
Eric Biggers | f699594 | 2019-04-18 14:43:02 -0700 | [diff] [blame] | 671 | return crypto_gcm_create_common(tmpl, tb, ctr_name, "ghash"); |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 672 | } |
| 673 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 674 | static int crypto_gcm_base_create(struct crypto_template *tmpl, |
| 675 | struct rtattr **tb) |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 676 | { |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 677 | const char *ctr_name; |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 678 | const char *ghash_name; |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 679 | |
| 680 | ctr_name = crypto_attr_alg_name(tb[1]); |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 681 | if (IS_ERR(ctr_name)) |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 682 | return PTR_ERR(ctr_name); |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 683 | |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 684 | ghash_name = crypto_attr_alg_name(tb[2]); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 685 | if (IS_ERR(ghash_name)) |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 686 | return PTR_ERR(ghash_name); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 687 | |
Eric Biggers | f699594 | 2019-04-18 14:43:02 -0700 | [diff] [blame] | 688 | return crypto_gcm_create_common(tmpl, tb, ctr_name, ghash_name); |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 689 | } |
| 690 | |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 691 | static int crypto_rfc4106_setkey(struct crypto_aead *parent, const u8 *key, |
| 692 | unsigned int keylen) |
| 693 | { |
| 694 | struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(parent); |
| 695 | struct crypto_aead *child = ctx->child; |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 696 | |
| 697 | if (keylen < 4) |
| 698 | return -EINVAL; |
| 699 | |
| 700 | keylen -= 4; |
| 701 | memcpy(ctx->nonce, key + keylen, 4); |
| 702 | |
| 703 | crypto_aead_clear_flags(child, CRYPTO_TFM_REQ_MASK); |
| 704 | crypto_aead_set_flags(child, crypto_aead_get_flags(parent) & |
| 705 | CRYPTO_TFM_REQ_MASK); |
Eric Biggers | af5034e | 2019-12-30 21:19:38 -0600 | [diff] [blame] | 706 | return crypto_aead_setkey(child, key, keylen); |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | static int crypto_rfc4106_setauthsize(struct crypto_aead *parent, |
| 710 | unsigned int authsize) |
| 711 | { |
| 712 | struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(parent); |
Iuliana Prodan | 65526f6 | 2019-07-31 16:05:54 +0300 | [diff] [blame] | 713 | int err; |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 714 | |
Iuliana Prodan | 65526f6 | 2019-07-31 16:05:54 +0300 | [diff] [blame] | 715 | err = crypto_rfc4106_check_authsize(authsize); |
| 716 | if (err) |
| 717 | return err; |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 718 | |
| 719 | return crypto_aead_setauthsize(ctx->child, authsize); |
| 720 | } |
| 721 | |
| 722 | static struct aead_request *crypto_rfc4106_crypt(struct aead_request *req) |
| 723 | { |
Herbert Xu | 7b05a37 | 2015-07-09 07:17:30 +0800 | [diff] [blame] | 724 | struct crypto_rfc4106_req_ctx *rctx = aead_request_ctx(req); |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 725 | struct crypto_aead *aead = crypto_aead_reqtfm(req); |
| 726 | struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(aead); |
Herbert Xu | 7b05a37 | 2015-07-09 07:17:30 +0800 | [diff] [blame] | 727 | struct aead_request *subreq = &rctx->subreq; |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 728 | struct crypto_aead *child = ctx->child; |
Herbert Xu | 7b05a37 | 2015-07-09 07:17:30 +0800 | [diff] [blame] | 729 | struct scatterlist *sg; |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 730 | u8 *iv = PTR_ALIGN((u8 *)(subreq + 1) + crypto_aead_reqsize(child), |
| 731 | crypto_aead_alignmask(child) + 1); |
| 732 | |
Corentin LABBE | e0ab7e9 | 2017-08-22 10:08:17 +0200 | [diff] [blame] | 733 | scatterwalk_map_and_copy(iv + GCM_AES_IV_SIZE, req->src, 0, req->assoclen - 8, 0); |
Herbert Xu | 7b05a37 | 2015-07-09 07:17:30 +0800 | [diff] [blame] | 734 | |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 735 | memcpy(iv, ctx->nonce, 4); |
| 736 | memcpy(iv + 4, req->iv, 8); |
| 737 | |
Herbert Xu | 7b05a37 | 2015-07-09 07:17:30 +0800 | [diff] [blame] | 738 | sg_init_table(rctx->src, 3); |
Corentin LABBE | e0ab7e9 | 2017-08-22 10:08:17 +0200 | [diff] [blame] | 739 | sg_set_buf(rctx->src, iv + GCM_AES_IV_SIZE, req->assoclen - 8); |
Herbert Xu | 7b05a37 | 2015-07-09 07:17:30 +0800 | [diff] [blame] | 740 | sg = scatterwalk_ffwd(rctx->src + 1, req->src, req->assoclen); |
| 741 | if (sg != rctx->src + 1) |
| 742 | sg_chain(rctx->src, 2, sg); |
| 743 | |
| 744 | if (req->src != req->dst) { |
| 745 | sg_init_table(rctx->dst, 3); |
Corentin LABBE | e0ab7e9 | 2017-08-22 10:08:17 +0200 | [diff] [blame] | 746 | sg_set_buf(rctx->dst, iv + GCM_AES_IV_SIZE, req->assoclen - 8); |
Herbert Xu | 7b05a37 | 2015-07-09 07:17:30 +0800 | [diff] [blame] | 747 | sg = scatterwalk_ffwd(rctx->dst + 1, req->dst, req->assoclen); |
| 748 | if (sg != rctx->dst + 1) |
| 749 | sg_chain(rctx->dst, 2, sg); |
| 750 | } |
| 751 | |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 752 | aead_request_set_tfm(subreq, child); |
| 753 | aead_request_set_callback(subreq, req->base.flags, req->base.complete, |
| 754 | req->base.data); |
Herbert Xu | 7b05a37 | 2015-07-09 07:17:30 +0800 | [diff] [blame] | 755 | aead_request_set_crypt(subreq, rctx->src, |
| 756 | req->src == req->dst ? rctx->src : rctx->dst, |
| 757 | req->cryptlen, iv); |
| 758 | aead_request_set_ad(subreq, req->assoclen - 8); |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 759 | |
| 760 | return subreq; |
| 761 | } |
| 762 | |
| 763 | static int crypto_rfc4106_encrypt(struct aead_request *req) |
| 764 | { |
Iuliana Prodan | 65526f6 | 2019-07-31 16:05:54 +0300 | [diff] [blame] | 765 | int err; |
| 766 | |
| 767 | err = crypto_ipsec_check_assoclen(req->assoclen); |
| 768 | if (err) |
| 769 | return err; |
Herbert Xu | 7b05a37 | 2015-07-09 07:17:30 +0800 | [diff] [blame] | 770 | |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 771 | req = crypto_rfc4106_crypt(req); |
| 772 | |
| 773 | return crypto_aead_encrypt(req); |
| 774 | } |
| 775 | |
| 776 | static int crypto_rfc4106_decrypt(struct aead_request *req) |
| 777 | { |
Iuliana Prodan | 65526f6 | 2019-07-31 16:05:54 +0300 | [diff] [blame] | 778 | int err; |
| 779 | |
| 780 | err = crypto_ipsec_check_assoclen(req->assoclen); |
| 781 | if (err) |
| 782 | return err; |
Herbert Xu | 7b05a37 | 2015-07-09 07:17:30 +0800 | [diff] [blame] | 783 | |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 784 | req = crypto_rfc4106_crypt(req); |
| 785 | |
| 786 | return crypto_aead_decrypt(req); |
| 787 | } |
| 788 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 789 | static int crypto_rfc4106_init_tfm(struct crypto_aead *tfm) |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 790 | { |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 791 | struct aead_instance *inst = aead_alg_instance(tfm); |
| 792 | struct crypto_aead_spawn *spawn = aead_instance_ctx(inst); |
| 793 | struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(tfm); |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 794 | struct crypto_aead *aead; |
| 795 | unsigned long align; |
| 796 | |
| 797 | aead = crypto_spawn_aead(spawn); |
| 798 | if (IS_ERR(aead)) |
| 799 | return PTR_ERR(aead); |
| 800 | |
| 801 | ctx->child = aead; |
| 802 | |
| 803 | align = crypto_aead_alignmask(aead); |
| 804 | align &= ~(crypto_tfm_ctx_alignment() - 1); |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 805 | crypto_aead_set_reqsize( |
| 806 | tfm, |
Herbert Xu | 7b05a37 | 2015-07-09 07:17:30 +0800 | [diff] [blame] | 807 | sizeof(struct crypto_rfc4106_req_ctx) + |
Herbert Xu | 5d72336 | 2015-05-11 17:47:59 +0800 | [diff] [blame] | 808 | ALIGN(crypto_aead_reqsize(aead), crypto_tfm_ctx_alignment()) + |
Herbert Xu | 7b05a37 | 2015-07-09 07:17:30 +0800 | [diff] [blame] | 809 | align + 24); |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 810 | |
| 811 | return 0; |
| 812 | } |
| 813 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 814 | static void crypto_rfc4106_exit_tfm(struct crypto_aead *tfm) |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 815 | { |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 816 | struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(tfm); |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 817 | |
| 818 | crypto_free_aead(ctx->child); |
| 819 | } |
| 820 | |
Herbert Xu | 7b05a37 | 2015-07-09 07:17:30 +0800 | [diff] [blame] | 821 | static void crypto_rfc4106_free(struct aead_instance *inst) |
| 822 | { |
| 823 | crypto_drop_aead(aead_instance_ctx(inst)); |
| 824 | kfree(inst); |
| 825 | } |
| 826 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 827 | static int crypto_rfc4106_create(struct crypto_template *tmpl, |
| 828 | struct rtattr **tb) |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 829 | { |
Eric Biggers | cd900f0 | 2020-01-02 19:58:46 -0800 | [diff] [blame] | 830 | u32 mask; |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 831 | struct aead_instance *inst; |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 832 | struct crypto_aead_spawn *spawn; |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 833 | struct aead_alg *alg; |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 834 | int err; |
| 835 | |
Eric Biggers | 7bcb2c9 | 2020-07-09 23:20:38 -0700 | [diff] [blame] | 836 | err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_AEAD, &mask); |
| 837 | if (err) |
| 838 | return err; |
Eric Biggers | cd900f0 | 2020-01-02 19:58:46 -0800 | [diff] [blame] | 839 | |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 840 | inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL); |
| 841 | if (!inst) |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 842 | return -ENOMEM; |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 843 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 844 | spawn = aead_instance_ctx(inst); |
Eric Biggers | cd900f0 | 2020-01-02 19:58:46 -0800 | [diff] [blame] | 845 | err = crypto_grab_aead(spawn, aead_crypto_instance(inst), |
Eric Biggers | 959ac1c | 2020-02-25 20:59:18 -0800 | [diff] [blame] | 846 | crypto_attr_alg_name(tb[1]), 0, mask); |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 847 | if (err) |
Eric Biggers | 959ac1c | 2020-02-25 20:59:18 -0800 | [diff] [blame] | 848 | goto err_free_inst; |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 849 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 850 | alg = crypto_spawn_aead_alg(spawn); |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 851 | |
| 852 | err = -EINVAL; |
| 853 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 854 | /* Underlying IV size must be 12. */ |
Corentin LABBE | e0ab7e9 | 2017-08-22 10:08:17 +0200 | [diff] [blame] | 855 | if (crypto_aead_alg_ivsize(alg) != GCM_AES_IV_SIZE) |
Eric Biggers | 959ac1c | 2020-02-25 20:59:18 -0800 | [diff] [blame] | 856 | goto err_free_inst; |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 857 | |
| 858 | /* Not a stream cipher? */ |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 859 | if (alg->base.cra_blocksize != 1) |
Eric Biggers | 959ac1c | 2020-02-25 20:59:18 -0800 | [diff] [blame] | 860 | goto err_free_inst; |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 861 | |
| 862 | err = -ENAMETOOLONG; |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 863 | if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME, |
| 864 | "rfc4106(%s)", alg->base.cra_name) >= |
| 865 | CRYPTO_MAX_ALG_NAME || |
| 866 | snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME, |
| 867 | "rfc4106(%s)", alg->base.cra_driver_name) >= |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 868 | CRYPTO_MAX_ALG_NAME) |
Eric Biggers | 959ac1c | 2020-02-25 20:59:18 -0800 | [diff] [blame] | 869 | goto err_free_inst; |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 870 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 871 | inst->alg.base.cra_priority = alg->base.cra_priority; |
| 872 | inst->alg.base.cra_blocksize = 1; |
| 873 | inst->alg.base.cra_alignmask = alg->base.cra_alignmask; |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 874 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 875 | inst->alg.base.cra_ctxsize = sizeof(struct crypto_rfc4106_ctx); |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 876 | |
Corentin LABBE | e0ab7e9 | 2017-08-22 10:08:17 +0200 | [diff] [blame] | 877 | inst->alg.ivsize = GCM_RFC4106_IV_SIZE; |
Herbert Xu | 16f37ecd | 2016-07-12 13:17:39 +0800 | [diff] [blame] | 878 | inst->alg.chunksize = crypto_aead_alg_chunksize(alg); |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 879 | inst->alg.maxauthsize = crypto_aead_alg_maxauthsize(alg); |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 880 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 881 | inst->alg.init = crypto_rfc4106_init_tfm; |
| 882 | inst->alg.exit = crypto_rfc4106_exit_tfm; |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 883 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 884 | inst->alg.setkey = crypto_rfc4106_setkey; |
| 885 | inst->alg.setauthsize = crypto_rfc4106_setauthsize; |
| 886 | inst->alg.encrypt = crypto_rfc4106_encrypt; |
| 887 | inst->alg.decrypt = crypto_rfc4106_decrypt; |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 888 | |
Herbert Xu | 7b05a37 | 2015-07-09 07:17:30 +0800 | [diff] [blame] | 889 | inst->free = crypto_rfc4106_free; |
| 890 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 891 | err = aead_register_instance(tmpl, inst); |
Eric Biggers | 959ac1c | 2020-02-25 20:59:18 -0800 | [diff] [blame] | 892 | if (err) { |
| 893 | err_free_inst: |
| 894 | crypto_rfc4106_free(inst); |
| 895 | } |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 896 | return err; |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 897 | } |
| 898 | |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 899 | static int crypto_rfc4543_setkey(struct crypto_aead *parent, const u8 *key, |
| 900 | unsigned int keylen) |
| 901 | { |
| 902 | struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(parent); |
| 903 | struct crypto_aead *child = ctx->child; |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 904 | |
| 905 | if (keylen < 4) |
| 906 | return -EINVAL; |
| 907 | |
| 908 | keylen -= 4; |
| 909 | memcpy(ctx->nonce, key + keylen, 4); |
| 910 | |
| 911 | crypto_aead_clear_flags(child, CRYPTO_TFM_REQ_MASK); |
| 912 | crypto_aead_set_flags(child, crypto_aead_get_flags(parent) & |
| 913 | CRYPTO_TFM_REQ_MASK); |
Eric Biggers | af5034e | 2019-12-30 21:19:38 -0600 | [diff] [blame] | 914 | return crypto_aead_setkey(child, key, keylen); |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 915 | } |
| 916 | |
| 917 | static int crypto_rfc4543_setauthsize(struct crypto_aead *parent, |
| 918 | unsigned int authsize) |
| 919 | { |
| 920 | struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(parent); |
| 921 | |
| 922 | if (authsize != 16) |
| 923 | return -EINVAL; |
| 924 | |
| 925 | return crypto_aead_setauthsize(ctx->child, authsize); |
| 926 | } |
| 927 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 928 | static int crypto_rfc4543_crypt(struct aead_request *req, bool enc) |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 929 | { |
| 930 | struct crypto_aead *aead = crypto_aead_reqtfm(req); |
| 931 | struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(aead); |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 932 | struct crypto_rfc4543_req_ctx *rctx = aead_request_ctx(req); |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 933 | struct aead_request *subreq = &rctx->subreq; |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 934 | unsigned int authsize = crypto_aead_authsize(aead); |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 935 | u8 *iv = PTR_ALIGN((u8 *)(rctx + 1) + crypto_aead_reqsize(ctx->child), |
| 936 | crypto_aead_alignmask(ctx->child) + 1); |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 937 | int err; |
| 938 | |
| 939 | if (req->src != req->dst) { |
| 940 | err = crypto_rfc4543_copy_src_to_dst(req, enc); |
| 941 | if (err) |
| 942 | return err; |
| 943 | } |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 944 | |
| 945 | memcpy(iv, ctx->nonce, 4); |
| 946 | memcpy(iv + 4, req->iv, 8); |
| 947 | |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 948 | aead_request_set_tfm(subreq, ctx->child); |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 949 | aead_request_set_callback(subreq, req->base.flags, |
| 950 | req->base.complete, req->base.data); |
| 951 | aead_request_set_crypt(subreq, req->src, req->dst, |
| 952 | enc ? 0 : authsize, iv); |
| 953 | aead_request_set_ad(subreq, req->assoclen + req->cryptlen - |
| 954 | subreq->cryptlen); |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 955 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 956 | return enc ? crypto_aead_encrypt(subreq) : crypto_aead_decrypt(subreq); |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 957 | } |
| 958 | |
Jussi Kivilinna | 9489667d | 2013-04-07 16:43:41 +0300 | [diff] [blame] | 959 | static int crypto_rfc4543_copy_src_to_dst(struct aead_request *req, bool enc) |
| 960 | { |
| 961 | struct crypto_aead *aead = crypto_aead_reqtfm(req); |
| 962 | struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(aead); |
| 963 | unsigned int authsize = crypto_aead_authsize(aead); |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 964 | unsigned int nbytes = req->assoclen + req->cryptlen - |
| 965 | (enc ? 0 : authsize); |
Kees Cook | 8d60539 | 2018-09-18 19:10:51 -0700 | [diff] [blame] | 966 | SYNC_SKCIPHER_REQUEST_ON_STACK(nreq, ctx->null); |
Jussi Kivilinna | 9489667d | 2013-04-07 16:43:41 +0300 | [diff] [blame] | 967 | |
Kees Cook | 8d60539 | 2018-09-18 19:10:51 -0700 | [diff] [blame] | 968 | skcipher_request_set_sync_tfm(nreq, ctx->null); |
Herbert Xu | 16f37ecd | 2016-07-12 13:17:39 +0800 | [diff] [blame] | 969 | skcipher_request_set_callback(nreq, req->base.flags, NULL, NULL); |
| 970 | skcipher_request_set_crypt(nreq, req->src, req->dst, nbytes, NULL); |
| 971 | |
| 972 | return crypto_skcipher_encrypt(nreq); |
Jussi Kivilinna | 9489667d | 2013-04-07 16:43:41 +0300 | [diff] [blame] | 973 | } |
| 974 | |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 975 | static int crypto_rfc4543_encrypt(struct aead_request *req) |
| 976 | { |
Iuliana Prodan | 74bf81d | 2019-08-02 11:47:33 +0300 | [diff] [blame] | 977 | return crypto_ipsec_check_assoclen(req->assoclen) ?: |
| 978 | crypto_rfc4543_crypt(req, true); |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 979 | } |
| 980 | |
| 981 | static int crypto_rfc4543_decrypt(struct aead_request *req) |
| 982 | { |
Iuliana Prodan | 74bf81d | 2019-08-02 11:47:33 +0300 | [diff] [blame] | 983 | return crypto_ipsec_check_assoclen(req->assoclen) ?: |
| 984 | crypto_rfc4543_crypt(req, false); |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 985 | } |
| 986 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 987 | static int crypto_rfc4543_init_tfm(struct crypto_aead *tfm) |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 988 | { |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 989 | struct aead_instance *inst = aead_alg_instance(tfm); |
| 990 | struct crypto_rfc4543_instance_ctx *ictx = aead_instance_ctx(inst); |
Jussi Kivilinna | 9489667d | 2013-04-07 16:43:41 +0300 | [diff] [blame] | 991 | struct crypto_aead_spawn *spawn = &ictx->aead; |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 992 | struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(tfm); |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 993 | struct crypto_aead *aead; |
Kees Cook | 8d60539 | 2018-09-18 19:10:51 -0700 | [diff] [blame] | 994 | struct crypto_sync_skcipher *null; |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 995 | unsigned long align; |
Jussi Kivilinna | 9489667d | 2013-04-07 16:43:41 +0300 | [diff] [blame] | 996 | int err = 0; |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 997 | |
| 998 | aead = crypto_spawn_aead(spawn); |
| 999 | if (IS_ERR(aead)) |
| 1000 | return PTR_ERR(aead); |
| 1001 | |
Eric Biggers | 3a2d4fb | 2017-12-07 10:56:34 -0800 | [diff] [blame] | 1002 | null = crypto_get_default_null_skcipher(); |
Jussi Kivilinna | 9489667d | 2013-04-07 16:43:41 +0300 | [diff] [blame] | 1003 | err = PTR_ERR(null); |
| 1004 | if (IS_ERR(null)) |
| 1005 | goto err_free_aead; |
| 1006 | |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1007 | ctx->child = aead; |
Jussi Kivilinna | 9489667d | 2013-04-07 16:43:41 +0300 | [diff] [blame] | 1008 | ctx->null = null; |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1009 | |
| 1010 | align = crypto_aead_alignmask(aead); |
| 1011 | align &= ~(crypto_tfm_ctx_alignment() - 1); |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 1012 | crypto_aead_set_reqsize( |
| 1013 | tfm, |
Herbert Xu | 5d72336 | 2015-05-11 17:47:59 +0800 | [diff] [blame] | 1014 | sizeof(struct crypto_rfc4543_req_ctx) + |
| 1015 | ALIGN(crypto_aead_reqsize(aead), crypto_tfm_ctx_alignment()) + |
Corentin LABBE | e0ab7e9 | 2017-08-22 10:08:17 +0200 | [diff] [blame] | 1016 | align + GCM_AES_IV_SIZE); |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1017 | |
| 1018 | return 0; |
Jussi Kivilinna | 9489667d | 2013-04-07 16:43:41 +0300 | [diff] [blame] | 1019 | |
| 1020 | err_free_aead: |
| 1021 | crypto_free_aead(aead); |
| 1022 | return err; |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1023 | } |
| 1024 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 1025 | static void crypto_rfc4543_exit_tfm(struct crypto_aead *tfm) |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1026 | { |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 1027 | struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(tfm); |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1028 | |
| 1029 | crypto_free_aead(ctx->child); |
Eric Biggers | 3a2d4fb | 2017-12-07 10:56:34 -0800 | [diff] [blame] | 1030 | crypto_put_default_null_skcipher(); |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1031 | } |
| 1032 | |
Herbert Xu | 7b05a37 | 2015-07-09 07:17:30 +0800 | [diff] [blame] | 1033 | static void crypto_rfc4543_free(struct aead_instance *inst) |
| 1034 | { |
| 1035 | struct crypto_rfc4543_instance_ctx *ctx = aead_instance_ctx(inst); |
| 1036 | |
| 1037 | crypto_drop_aead(&ctx->aead); |
| 1038 | |
| 1039 | kfree(inst); |
| 1040 | } |
| 1041 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 1042 | static int crypto_rfc4543_create(struct crypto_template *tmpl, |
| 1043 | struct rtattr **tb) |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1044 | { |
Eric Biggers | cd900f0 | 2020-01-02 19:58:46 -0800 | [diff] [blame] | 1045 | u32 mask; |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 1046 | struct aead_instance *inst; |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 1047 | struct aead_alg *alg; |
Jussi Kivilinna | 9489667d | 2013-04-07 16:43:41 +0300 | [diff] [blame] | 1048 | struct crypto_rfc4543_instance_ctx *ctx; |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1049 | int err; |
| 1050 | |
Eric Biggers | 7bcb2c9 | 2020-07-09 23:20:38 -0700 | [diff] [blame] | 1051 | err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_AEAD, &mask); |
| 1052 | if (err) |
| 1053 | return err; |
Eric Biggers | cd900f0 | 2020-01-02 19:58:46 -0800 | [diff] [blame] | 1054 | |
Jussi Kivilinna | 9489667d | 2013-04-07 16:43:41 +0300 | [diff] [blame] | 1055 | inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL); |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1056 | if (!inst) |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 1057 | return -ENOMEM; |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1058 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 1059 | ctx = aead_instance_ctx(inst); |
Eric Biggers | c4caa56 | 2020-02-25 20:59:19 -0800 | [diff] [blame] | 1060 | err = crypto_grab_aead(&ctx->aead, aead_crypto_instance(inst), |
| 1061 | crypto_attr_alg_name(tb[1]), 0, mask); |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1062 | if (err) |
Eric Biggers | c4caa56 | 2020-02-25 20:59:19 -0800 | [diff] [blame] | 1063 | goto err_free_inst; |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1064 | |
Eric Biggers | c4caa56 | 2020-02-25 20:59:19 -0800 | [diff] [blame] | 1065 | alg = crypto_spawn_aead_alg(&ctx->aead); |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1066 | |
| 1067 | err = -EINVAL; |
| 1068 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 1069 | /* Underlying IV size must be 12. */ |
Corentin LABBE | e0ab7e9 | 2017-08-22 10:08:17 +0200 | [diff] [blame] | 1070 | if (crypto_aead_alg_ivsize(alg) != GCM_AES_IV_SIZE) |
Eric Biggers | c4caa56 | 2020-02-25 20:59:19 -0800 | [diff] [blame] | 1071 | goto err_free_inst; |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1072 | |
| 1073 | /* Not a stream cipher? */ |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 1074 | if (alg->base.cra_blocksize != 1) |
Eric Biggers | c4caa56 | 2020-02-25 20:59:19 -0800 | [diff] [blame] | 1075 | goto err_free_inst; |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1076 | |
| 1077 | err = -ENAMETOOLONG; |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 1078 | if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME, |
| 1079 | "rfc4543(%s)", alg->base.cra_name) >= |
| 1080 | CRYPTO_MAX_ALG_NAME || |
| 1081 | snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME, |
| 1082 | "rfc4543(%s)", alg->base.cra_driver_name) >= |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1083 | CRYPTO_MAX_ALG_NAME) |
Eric Biggers | c4caa56 | 2020-02-25 20:59:19 -0800 | [diff] [blame] | 1084 | goto err_free_inst; |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1085 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 1086 | inst->alg.base.cra_priority = alg->base.cra_priority; |
| 1087 | inst->alg.base.cra_blocksize = 1; |
| 1088 | inst->alg.base.cra_alignmask = alg->base.cra_alignmask; |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1089 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 1090 | inst->alg.base.cra_ctxsize = sizeof(struct crypto_rfc4543_ctx); |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1091 | |
Corentin LABBE | e0ab7e9 | 2017-08-22 10:08:17 +0200 | [diff] [blame] | 1092 | inst->alg.ivsize = GCM_RFC4543_IV_SIZE; |
Herbert Xu | 16f37ecd | 2016-07-12 13:17:39 +0800 | [diff] [blame] | 1093 | inst->alg.chunksize = crypto_aead_alg_chunksize(alg); |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 1094 | inst->alg.maxauthsize = crypto_aead_alg_maxauthsize(alg); |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1095 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 1096 | inst->alg.init = crypto_rfc4543_init_tfm; |
| 1097 | inst->alg.exit = crypto_rfc4543_exit_tfm; |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1098 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 1099 | inst->alg.setkey = crypto_rfc4543_setkey; |
| 1100 | inst->alg.setauthsize = crypto_rfc4543_setauthsize; |
| 1101 | inst->alg.encrypt = crypto_rfc4543_encrypt; |
| 1102 | inst->alg.decrypt = crypto_rfc4543_decrypt; |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1103 | |
Eric Biggers | c4caa56 | 2020-02-25 20:59:19 -0800 | [diff] [blame] | 1104 | inst->free = crypto_rfc4543_free; |
Herbert Xu | 7b05a37 | 2015-07-09 07:17:30 +0800 | [diff] [blame] | 1105 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 1106 | err = aead_register_instance(tmpl, inst); |
Eric Biggers | c4caa56 | 2020-02-25 20:59:19 -0800 | [diff] [blame] | 1107 | if (err) { |
| 1108 | err_free_inst: |
| 1109 | crypto_rfc4543_free(inst); |
| 1110 | } |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 1111 | return err; |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1112 | } |
| 1113 | |
Xiongfeng Wang | 56a00d9 | 2019-01-18 13:58:13 +0800 | [diff] [blame] | 1114 | static struct crypto_template crypto_gcm_tmpls[] = { |
| 1115 | { |
| 1116 | .name = "gcm_base", |
| 1117 | .create = crypto_gcm_base_create, |
| 1118 | .module = THIS_MODULE, |
| 1119 | }, { |
| 1120 | .name = "gcm", |
| 1121 | .create = crypto_gcm_create, |
| 1122 | .module = THIS_MODULE, |
| 1123 | }, { |
| 1124 | .name = "rfc4106", |
| 1125 | .create = crypto_rfc4106_create, |
| 1126 | .module = THIS_MODULE, |
| 1127 | }, { |
| 1128 | .name = "rfc4543", |
| 1129 | .create = crypto_rfc4543_create, |
| 1130 | .module = THIS_MODULE, |
| 1131 | }, |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1132 | }; |
| 1133 | |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 1134 | static int __init crypto_gcm_module_init(void) |
| 1135 | { |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 1136 | int err; |
| 1137 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 1138 | gcm_zeroes = kzalloc(sizeof(*gcm_zeroes), GFP_KERNEL); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 1139 | if (!gcm_zeroes) |
| 1140 | return -ENOMEM; |
| 1141 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 1142 | sg_init_one(&gcm_zeroes->sg, gcm_zeroes->buf, sizeof(gcm_zeroes->buf)); |
| 1143 | |
Xiongfeng Wang | 56a00d9 | 2019-01-18 13:58:13 +0800 | [diff] [blame] | 1144 | err = crypto_register_templates(crypto_gcm_tmpls, |
| 1145 | ARRAY_SIZE(crypto_gcm_tmpls)); |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 1146 | if (err) |
Xiongfeng Wang | 56a00d9 | 2019-01-18 13:58:13 +0800 | [diff] [blame] | 1147 | kfree(gcm_zeroes); |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 1148 | |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 1149 | return err; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 1150 | } |
| 1151 | |
| 1152 | static void __exit crypto_gcm_module_exit(void) |
| 1153 | { |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 1154 | kfree(gcm_zeroes); |
Xiongfeng Wang | 56a00d9 | 2019-01-18 13:58:13 +0800 | [diff] [blame] | 1155 | crypto_unregister_templates(crypto_gcm_tmpls, |
| 1156 | ARRAY_SIZE(crypto_gcm_tmpls)); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 1157 | } |
| 1158 | |
Eric Biggers | c4741b2 | 2019-04-11 21:57:42 -0700 | [diff] [blame] | 1159 | subsys_initcall(crypto_gcm_module_init); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 1160 | module_exit(crypto_gcm_module_exit); |
| 1161 | |
| 1162 | MODULE_LICENSE("GPL"); |
| 1163 | MODULE_DESCRIPTION("Galois/Counter Mode"); |
| 1164 | MODULE_AUTHOR("Mikko Herranen <mh1@iki.fi>"); |
Kees Cook | 5d26a10 | 2014-11-20 17:05:53 -0800 | [diff] [blame] | 1165 | MODULE_ALIAS_CRYPTO("gcm_base"); |
| 1166 | MODULE_ALIAS_CRYPTO("rfc4106"); |
| 1167 | MODULE_ALIAS_CRYPTO("rfc4543"); |
Kees Cook | 4943ba1 | 2014-11-24 16:32:38 -0800 | [diff] [blame] | 1168 | MODULE_ALIAS_CRYPTO("gcm"); |