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