Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Authenc: Simple AEAD wrapper for IPsec |
| 4 | * |
Herbert Xu | 92d95ba | 2015-07-30 17:53:16 +0800 | [diff] [blame] | 5 | * Copyright (c) 2007-2015 Herbert Xu <herbert@gondor.apana.org.au> |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 6 | */ |
| 7 | |
Herbert Xu | 68acbf8 | 2015-05-11 17:47:41 +0800 | [diff] [blame] | 8 | #include <crypto/internal/aead.h> |
Herbert Xu | 5f7082e | 2008-08-31 22:21:09 +1000 | [diff] [blame] | 9 | #include <crypto/internal/hash.h> |
Herbert Xu | 9ffde35a | 2007-12-17 20:12:49 +0800 | [diff] [blame] | 10 | #include <crypto/internal/skcipher.h> |
Herbert Xu | e236d4a | 2007-11-22 23:11:53 +0800 | [diff] [blame] | 11 | #include <crypto/authenc.h> |
Herbert Xu | 92d95ba | 2015-07-30 17:53:16 +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> |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 14 | #include <linux/err.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/module.h> |
Herbert Xu | e236d4a | 2007-11-22 23:11:53 +0800 | [diff] [blame] | 18 | #include <linux/rtnetlink.h> |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 19 | #include <linux/slab.h> |
| 20 | #include <linux/spinlock.h> |
| 21 | |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 22 | struct authenc_instance_ctx { |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 23 | struct crypto_ahash_spawn auth; |
Herbert Xu | 9ffde35a | 2007-12-17 20:12:49 +0800 | [diff] [blame] | 24 | struct crypto_skcipher_spawn enc; |
Herbert Xu | 92d95ba | 2015-07-30 17:53:16 +0800 | [diff] [blame] | 25 | unsigned int reqoff; |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 26 | }; |
| 27 | |
| 28 | struct crypto_authenc_ctx { |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 29 | struct crypto_ahash *auth; |
Herbert Xu | 7217d49 | 2016-07-12 13:17:34 +0800 | [diff] [blame] | 30 | struct crypto_skcipher *enc; |
Kees Cook | 8d60539 | 2018-09-18 19:10:51 -0700 | [diff] [blame] | 31 | struct crypto_sync_skcipher *null; |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 32 | }; |
| 33 | |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 34 | struct authenc_request_ctx { |
Herbert Xu | 92d95ba | 2015-07-30 17:53:16 +0800 | [diff] [blame] | 35 | struct scatterlist src[2]; |
| 36 | struct scatterlist dst[2]; |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 37 | char tail[]; |
| 38 | }; |
| 39 | |
Herbert Xu | 180ce7e | 2010-04-26 09:14:05 +0800 | [diff] [blame] | 40 | static void authenc_request_complete(struct aead_request *req, int err) |
| 41 | { |
| 42 | if (err != -EINPROGRESS) |
| 43 | aead_request_complete(req, err); |
| 44 | } |
| 45 | |
Mathias Krause | bc6e2bd | 2013-10-15 13:49:30 +0200 | [diff] [blame] | 46 | int crypto_authenc_extractkeys(struct crypto_authenc_keys *keys, const u8 *key, |
| 47 | unsigned int keylen) |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 48 | { |
Mathias Krause | bc6e2bd | 2013-10-15 13:49:30 +0200 | [diff] [blame] | 49 | struct rtattr *rta = (struct rtattr *)key; |
Herbert Xu | e236d4a | 2007-11-22 23:11:53 +0800 | [diff] [blame] | 50 | struct crypto_authenc_key_param *param; |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 51 | |
Herbert Xu | 12dc5e6 | 2007-12-10 10:55:21 +0800 | [diff] [blame] | 52 | if (!RTA_OK(rta, keylen)) |
Mathias Krause | bc6e2bd | 2013-10-15 13:49:30 +0200 | [diff] [blame] | 53 | return -EINVAL; |
Herbert Xu | e236d4a | 2007-11-22 23:11:53 +0800 | [diff] [blame] | 54 | if (rta->rta_type != CRYPTO_AUTHENC_KEYA_PARAM) |
Mathias Krause | bc6e2bd | 2013-10-15 13:49:30 +0200 | [diff] [blame] | 55 | return -EINVAL; |
Eric Biggers | 8f9c469 | 2018-12-16 23:23:22 -0800 | [diff] [blame] | 56 | |
| 57 | /* |
| 58 | * RTA_OK() didn't align the rtattr's payload when validating that it |
| 59 | * fits in the buffer. Yet, the keys should start on the next 4-byte |
| 60 | * aligned boundary. To avoid confusion, require that the rtattr |
| 61 | * payload be exactly the param struct, which has a 4-byte aligned size. |
| 62 | */ |
| 63 | if (RTA_PAYLOAD(rta) != sizeof(*param)) |
Mathias Krause | bc6e2bd | 2013-10-15 13:49:30 +0200 | [diff] [blame] | 64 | return -EINVAL; |
Eric Biggers | 8f9c469 | 2018-12-16 23:23:22 -0800 | [diff] [blame] | 65 | BUILD_BUG_ON(sizeof(*param) % RTA_ALIGNTO); |
Herbert Xu | e236d4a | 2007-11-22 23:11:53 +0800 | [diff] [blame] | 66 | |
| 67 | param = RTA_DATA(rta); |
Mathias Krause | bc6e2bd | 2013-10-15 13:49:30 +0200 | [diff] [blame] | 68 | keys->enckeylen = be32_to_cpu(param->enckeylen); |
Herbert Xu | e236d4a | 2007-11-22 23:11:53 +0800 | [diff] [blame] | 69 | |
Eric Biggers | 8f9c469 | 2018-12-16 23:23:22 -0800 | [diff] [blame] | 70 | key += rta->rta_len; |
| 71 | keylen -= rta->rta_len; |
Herbert Xu | e236d4a | 2007-11-22 23:11:53 +0800 | [diff] [blame] | 72 | |
Mathias Krause | bc6e2bd | 2013-10-15 13:49:30 +0200 | [diff] [blame] | 73 | if (keylen < keys->enckeylen) |
| 74 | return -EINVAL; |
Herbert Xu | e236d4a | 2007-11-22 23:11:53 +0800 | [diff] [blame] | 75 | |
Mathias Krause | bc6e2bd | 2013-10-15 13:49:30 +0200 | [diff] [blame] | 76 | keys->authkeylen = keylen - keys->enckeylen; |
| 77 | keys->authkey = key; |
| 78 | keys->enckey = key + keys->authkeylen; |
| 79 | |
| 80 | return 0; |
| 81 | } |
| 82 | EXPORT_SYMBOL_GPL(crypto_authenc_extractkeys); |
| 83 | |
| 84 | static int crypto_authenc_setkey(struct crypto_aead *authenc, const u8 *key, |
| 85 | unsigned int keylen) |
| 86 | { |
| 87 | struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc); |
| 88 | struct crypto_ahash *auth = ctx->auth; |
Herbert Xu | 7217d49 | 2016-07-12 13:17:34 +0800 | [diff] [blame] | 89 | struct crypto_skcipher *enc = ctx->enc; |
Mathias Krause | bc6e2bd | 2013-10-15 13:49:30 +0200 | [diff] [blame] | 90 | struct crypto_authenc_keys keys; |
| 91 | int err = -EINVAL; |
| 92 | |
| 93 | if (crypto_authenc_extractkeys(&keys, key, keylen) != 0) |
Eric Biggers | 674f368 | 2019-12-30 21:19:36 -0600 | [diff] [blame] | 94 | goto out; |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 95 | |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 96 | crypto_ahash_clear_flags(auth, CRYPTO_TFM_REQ_MASK); |
| 97 | crypto_ahash_set_flags(auth, crypto_aead_get_flags(authenc) & |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 98 | CRYPTO_TFM_REQ_MASK); |
Mathias Krause | bc6e2bd | 2013-10-15 13:49:30 +0200 | [diff] [blame] | 99 | err = crypto_ahash_setkey(auth, keys.authkey, keys.authkeylen); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 100 | if (err) |
| 101 | goto out; |
| 102 | |
Herbert Xu | 7217d49 | 2016-07-12 13:17:34 +0800 | [diff] [blame] | 103 | crypto_skcipher_clear_flags(enc, CRYPTO_TFM_REQ_MASK); |
| 104 | crypto_skcipher_set_flags(enc, crypto_aead_get_flags(authenc) & |
| 105 | CRYPTO_TFM_REQ_MASK); |
| 106 | err = crypto_skcipher_setkey(enc, keys.enckey, keys.enckeylen); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 107 | out: |
Tudor-Dan Ambarus | ad2fdcd | 2018-04-03 09:39:00 +0300 | [diff] [blame] | 108 | memzero_explicit(&keys, sizeof(keys)); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 109 | return err; |
| 110 | } |
| 111 | |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 112 | static void authenc_geniv_ahash_done(struct crypto_async_request *areq, int err) |
| 113 | { |
| 114 | struct aead_request *req = areq->data; |
| 115 | struct crypto_aead *authenc = crypto_aead_reqtfm(req); |
Herbert Xu | 92d95ba | 2015-07-30 17:53:16 +0800 | [diff] [blame] | 116 | struct aead_instance *inst = aead_alg_instance(authenc); |
| 117 | struct authenc_instance_ctx *ictx = aead_instance_ctx(inst); |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 118 | struct authenc_request_ctx *areq_ctx = aead_request_ctx(req); |
Herbert Xu | 92d95ba | 2015-07-30 17:53:16 +0800 | [diff] [blame] | 119 | struct ahash_request *ahreq = (void *)(areq_ctx->tail + ictx->reqoff); |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 120 | |
| 121 | if (err) |
| 122 | goto out; |
| 123 | |
Herbert Xu | 92d95ba | 2015-07-30 17:53:16 +0800 | [diff] [blame] | 124 | scatterwalk_map_and_copy(ahreq->result, req->dst, |
| 125 | req->assoclen + req->cryptlen, |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 126 | crypto_aead_authsize(authenc), 1); |
| 127 | |
| 128 | out: |
| 129 | aead_request_complete(req, err); |
| 130 | } |
| 131 | |
Herbert Xu | 92d95ba | 2015-07-30 17:53:16 +0800 | [diff] [blame] | 132 | static int crypto_authenc_genicv(struct aead_request *req, unsigned int flags) |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 133 | { |
| 134 | struct crypto_aead *authenc = crypto_aead_reqtfm(req); |
Herbert Xu | 92d95ba | 2015-07-30 17:53:16 +0800 | [diff] [blame] | 135 | struct aead_instance *inst = aead_alg_instance(authenc); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 136 | struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc); |
Herbert Xu | 92d95ba | 2015-07-30 17:53:16 +0800 | [diff] [blame] | 137 | struct authenc_instance_ctx *ictx = aead_instance_ctx(inst); |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 138 | struct crypto_ahash *auth = ctx->auth; |
| 139 | struct authenc_request_ctx *areq_ctx = aead_request_ctx(req); |
Herbert Xu | 92d95ba | 2015-07-30 17:53:16 +0800 | [diff] [blame] | 140 | struct ahash_request *ahreq = (void *)(areq_ctx->tail + ictx->reqoff); |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 141 | u8 *hash = areq_ctx->tail; |
| 142 | int err; |
| 143 | |
| 144 | hash = (u8 *)ALIGN((unsigned long)hash + crypto_ahash_alignmask(auth), |
| 145 | crypto_ahash_alignmask(auth) + 1); |
| 146 | |
| 147 | ahash_request_set_tfm(ahreq, auth); |
Herbert Xu | 92d95ba | 2015-07-30 17:53:16 +0800 | [diff] [blame] | 148 | ahash_request_set_crypt(ahreq, req->dst, hash, |
| 149 | req->assoclen + req->cryptlen); |
| 150 | ahash_request_set_callback(ahreq, flags, |
| 151 | authenc_geniv_ahash_done, req); |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 152 | |
| 153 | err = crypto_ahash_digest(ahreq); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 154 | if (err) |
Herbert Xu | 92d95ba | 2015-07-30 17:53:16 +0800 | [diff] [blame] | 155 | return err; |
Herbert Xu | 7c3d703 | 2007-12-10 16:15:41 +0800 | [diff] [blame] | 156 | |
Herbert Xu | 92d95ba | 2015-07-30 17:53:16 +0800 | [diff] [blame] | 157 | scatterwalk_map_and_copy(hash, req->dst, req->assoclen + req->cryptlen, |
Herbert Xu | 7ba683a | 2007-12-02 18:49:21 +1100 | [diff] [blame] | 158 | crypto_aead_authsize(authenc), 1); |
Herbert Xu | 92d95ba | 2015-07-30 17:53:16 +0800 | [diff] [blame] | 159 | |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | static void crypto_authenc_encrypt_done(struct crypto_async_request *req, |
| 164 | int err) |
| 165 | { |
Herbert Xu | a697690 | 2008-08-23 01:04:06 +1000 | [diff] [blame] | 166 | struct aead_request *areq = req->data; |
| 167 | |
Herbert Xu | 92d95ba | 2015-07-30 17:53:16 +0800 | [diff] [blame] | 168 | if (err) |
| 169 | goto out; |
Herbert Xu | e56dd56 | 2007-12-10 16:20:24 +0800 | [diff] [blame] | 170 | |
Herbert Xu | 92d95ba | 2015-07-30 17:53:16 +0800 | [diff] [blame] | 171 | err = crypto_authenc_genicv(areq, 0); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 172 | |
Herbert Xu | 92d95ba | 2015-07-30 17:53:16 +0800 | [diff] [blame] | 173 | out: |
Herbert Xu | 180ce7e | 2010-04-26 09:14:05 +0800 | [diff] [blame] | 174 | authenc_request_complete(areq, err); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 175 | } |
| 176 | |
Herbert Xu | 92d95ba | 2015-07-30 17:53:16 +0800 | [diff] [blame] | 177 | static int crypto_authenc_copy_assoc(struct aead_request *req) |
| 178 | { |
| 179 | struct crypto_aead *authenc = crypto_aead_reqtfm(req); |
| 180 | struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc); |
Kees Cook | 8d60539 | 2018-09-18 19:10:51 -0700 | [diff] [blame] | 181 | SYNC_SKCIPHER_REQUEST_ON_STACK(skreq, ctx->null); |
Herbert Xu | 92d95ba | 2015-07-30 17:53:16 +0800 | [diff] [blame] | 182 | |
Kees Cook | 8d60539 | 2018-09-18 19:10:51 -0700 | [diff] [blame] | 183 | skcipher_request_set_sync_tfm(skreq, ctx->null); |
Herbert Xu | 7217d49 | 2016-07-12 13:17:34 +0800 | [diff] [blame] | 184 | skcipher_request_set_callback(skreq, aead_request_flags(req), |
| 185 | NULL, NULL); |
| 186 | skcipher_request_set_crypt(skreq, req->src, req->dst, req->assoclen, |
| 187 | NULL); |
| 188 | |
| 189 | return crypto_skcipher_encrypt(skreq); |
Herbert Xu | 92d95ba | 2015-07-30 17:53:16 +0800 | [diff] [blame] | 190 | } |
| 191 | |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 192 | static int crypto_authenc_encrypt(struct aead_request *req) |
| 193 | { |
| 194 | struct crypto_aead *authenc = crypto_aead_reqtfm(req); |
Herbert Xu | 92d95ba | 2015-07-30 17:53:16 +0800 | [diff] [blame] | 195 | struct aead_instance *inst = aead_alg_instance(authenc); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 196 | struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc); |
Herbert Xu | 92d95ba | 2015-07-30 17:53:16 +0800 | [diff] [blame] | 197 | struct authenc_instance_ctx *ictx = aead_instance_ctx(inst); |
Steffen Klassert | 50beceb | 2010-03-03 22:41:08 +0800 | [diff] [blame] | 198 | struct authenc_request_ctx *areq_ctx = aead_request_ctx(req); |
Herbert Xu | 7217d49 | 2016-07-12 13:17:34 +0800 | [diff] [blame] | 199 | struct crypto_skcipher *enc = ctx->enc; |
Herbert Xu | e56dd56 | 2007-12-10 16:20:24 +0800 | [diff] [blame] | 200 | unsigned int cryptlen = req->cryptlen; |
Herbert Xu | 7217d49 | 2016-07-12 13:17:34 +0800 | [diff] [blame] | 201 | struct skcipher_request *skreq = (void *)(areq_ctx->tail + |
| 202 | ictx->reqoff); |
Herbert Xu | 92d95ba | 2015-07-30 17:53:16 +0800 | [diff] [blame] | 203 | struct scatterlist *src, *dst; |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 204 | int err; |
| 205 | |
Herbert Xu | 92d95ba | 2015-07-30 17:53:16 +0800 | [diff] [blame] | 206 | src = scatterwalk_ffwd(areq_ctx->src, req->src, req->assoclen); |
| 207 | dst = src; |
| 208 | |
| 209 | if (req->src != req->dst) { |
| 210 | err = crypto_authenc_copy_assoc(req); |
| 211 | if (err) |
| 212 | return err; |
| 213 | |
Herbert Xu | 92d95ba | 2015-07-30 17:53:16 +0800 | [diff] [blame] | 214 | dst = scatterwalk_ffwd(areq_ctx->dst, req->dst, req->assoclen); |
| 215 | } |
| 216 | |
Herbert Xu | 7217d49 | 2016-07-12 13:17:34 +0800 | [diff] [blame] | 217 | skcipher_request_set_tfm(skreq, enc); |
| 218 | skcipher_request_set_callback(skreq, aead_request_flags(req), |
| 219 | crypto_authenc_encrypt_done, req); |
| 220 | skcipher_request_set_crypt(skreq, src, dst, cryptlen, req->iv); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 221 | |
Herbert Xu | 7217d49 | 2016-07-12 13:17:34 +0800 | [diff] [blame] | 222 | err = crypto_skcipher_encrypt(skreq); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 223 | if (err) |
| 224 | return err; |
| 225 | |
Herbert Xu | 92d95ba | 2015-07-30 17:53:16 +0800 | [diff] [blame] | 226 | return crypto_authenc_genicv(req, aead_request_flags(req)); |
Herbert Xu | e56dd56 | 2007-12-10 16:20:24 +0800 | [diff] [blame] | 227 | } |
| 228 | |
Herbert Xu | 92d95ba | 2015-07-30 17:53:16 +0800 | [diff] [blame] | 229 | static int crypto_authenc_decrypt_tail(struct aead_request *req, |
| 230 | unsigned int flags) |
Herbert Xu | e56dd56 | 2007-12-10 16:20:24 +0800 | [diff] [blame] | 231 | { |
Herbert Xu | 92d95ba | 2015-07-30 17:53:16 +0800 | [diff] [blame] | 232 | struct crypto_aead *authenc = crypto_aead_reqtfm(req); |
| 233 | struct aead_instance *inst = aead_alg_instance(authenc); |
Herbert Xu | e56dd56 | 2007-12-10 16:20:24 +0800 | [diff] [blame] | 234 | struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc); |
Herbert Xu | 92d95ba | 2015-07-30 17:53:16 +0800 | [diff] [blame] | 235 | struct authenc_instance_ctx *ictx = aead_instance_ctx(inst); |
| 236 | struct authenc_request_ctx *areq_ctx = aead_request_ctx(req); |
| 237 | struct ahash_request *ahreq = (void *)(areq_ctx->tail + ictx->reqoff); |
Herbert Xu | 7217d49 | 2016-07-12 13:17:34 +0800 | [diff] [blame] | 238 | struct skcipher_request *skreq = (void *)(areq_ctx->tail + |
| 239 | ictx->reqoff); |
Herbert Xu | 92d95ba | 2015-07-30 17:53:16 +0800 | [diff] [blame] | 240 | unsigned int authsize = crypto_aead_authsize(authenc); |
| 241 | u8 *ihash = ahreq->result + authsize; |
| 242 | struct scatterlist *src, *dst; |
Herbert Xu | e56dd56 | 2007-12-10 16:20:24 +0800 | [diff] [blame] | 243 | |
Herbert Xu | 92d95ba | 2015-07-30 17:53:16 +0800 | [diff] [blame] | 244 | scatterwalk_map_and_copy(ihash, req->src, ahreq->nbytes, authsize, 0); |
Herbert Xu | e56dd56 | 2007-12-10 16:20:24 +0800 | [diff] [blame] | 245 | |
Herbert Xu | 92d95ba | 2015-07-30 17:53:16 +0800 | [diff] [blame] | 246 | if (crypto_memneq(ihash, ahreq->result, authsize)) |
| 247 | return -EBADMSG; |
| 248 | |
Herbert Xu | 92d95ba | 2015-07-30 17:53:16 +0800 | [diff] [blame] | 249 | src = scatterwalk_ffwd(areq_ctx->src, req->src, req->assoclen); |
| 250 | dst = src; |
| 251 | |
Harsh Jain | c34252f | 2016-06-29 00:24:43 +0530 | [diff] [blame] | 252 | if (req->src != req->dst) |
Herbert Xu | 92d95ba | 2015-07-30 17:53:16 +0800 | [diff] [blame] | 253 | dst = scatterwalk_ffwd(areq_ctx->dst, req->dst, req->assoclen); |
Herbert Xu | 92d95ba | 2015-07-30 17:53:16 +0800 | [diff] [blame] | 254 | |
Herbert Xu | 7217d49 | 2016-07-12 13:17:34 +0800 | [diff] [blame] | 255 | skcipher_request_set_tfm(skreq, ctx->enc); |
| 256 | skcipher_request_set_callback(skreq, aead_request_flags(req), |
| 257 | req->base.complete, req->base.data); |
| 258 | skcipher_request_set_crypt(skreq, src, dst, |
| 259 | req->cryptlen - authsize, req->iv); |
Herbert Xu | 92d95ba | 2015-07-30 17:53:16 +0800 | [diff] [blame] | 260 | |
Herbert Xu | 7217d49 | 2016-07-12 13:17:34 +0800 | [diff] [blame] | 261 | return crypto_skcipher_decrypt(skreq); |
Herbert Xu | 92d95ba | 2015-07-30 17:53:16 +0800 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | static void authenc_verify_ahash_done(struct crypto_async_request *areq, |
| 265 | int err) |
| 266 | { |
| 267 | struct aead_request *req = areq->data; |
| 268 | |
Herbert Xu | e56dd56 | 2007-12-10 16:20:24 +0800 | [diff] [blame] | 269 | if (err) |
Herbert Xu | 92d95ba | 2015-07-30 17:53:16 +0800 | [diff] [blame] | 270 | goto out; |
Herbert Xu | e56dd56 | 2007-12-10 16:20:24 +0800 | [diff] [blame] | 271 | |
Herbert Xu | 92d95ba | 2015-07-30 17:53:16 +0800 | [diff] [blame] | 272 | err = crypto_authenc_decrypt_tail(req, 0); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 273 | |
Herbert Xu | 92d95ba | 2015-07-30 17:53:16 +0800 | [diff] [blame] | 274 | out: |
| 275 | authenc_request_complete(req, err); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | static int crypto_authenc_decrypt(struct aead_request *req) |
| 279 | { |
| 280 | struct crypto_aead *authenc = crypto_aead_reqtfm(req); |
Herbert Xu | 481f34a | 2007-12-04 20:04:21 +1100 | [diff] [blame] | 281 | unsigned int authsize = crypto_aead_authsize(authenc); |
Herbert Xu | 92d95ba | 2015-07-30 17:53:16 +0800 | [diff] [blame] | 282 | struct aead_instance *inst = aead_alg_instance(authenc); |
| 283 | struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc); |
| 284 | struct authenc_instance_ctx *ictx = aead_instance_ctx(inst); |
| 285 | struct crypto_ahash *auth = ctx->auth; |
| 286 | struct authenc_request_ctx *areq_ctx = aead_request_ctx(req); |
| 287 | struct ahash_request *ahreq = (void *)(areq_ctx->tail + ictx->reqoff); |
| 288 | u8 *hash = areq_ctx->tail; |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 289 | int err; |
| 290 | |
Herbert Xu | 92d95ba | 2015-07-30 17:53:16 +0800 | [diff] [blame] | 291 | hash = (u8 *)ALIGN((unsigned long)hash + crypto_ahash_alignmask(auth), |
| 292 | crypto_ahash_alignmask(auth) + 1); |
Herbert Xu | 481f34a | 2007-12-04 20:04:21 +1100 | [diff] [blame] | 293 | |
Herbert Xu | 92d95ba | 2015-07-30 17:53:16 +0800 | [diff] [blame] | 294 | ahash_request_set_tfm(ahreq, auth); |
| 295 | ahash_request_set_crypt(ahreq, req->src, hash, |
| 296 | req->assoclen + req->cryptlen - authsize); |
| 297 | ahash_request_set_callback(ahreq, aead_request_flags(req), |
| 298 | authenc_verify_ahash_done, req); |
| 299 | |
| 300 | err = crypto_ahash_digest(ahreq); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 301 | if (err) |
| 302 | return err; |
| 303 | |
Herbert Xu | 92d95ba | 2015-07-30 17:53:16 +0800 | [diff] [blame] | 304 | return crypto_authenc_decrypt_tail(req, aead_request_flags(req)); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 305 | } |
| 306 | |
Herbert Xu | 92d95ba | 2015-07-30 17:53:16 +0800 | [diff] [blame] | 307 | static int crypto_authenc_init_tfm(struct crypto_aead *tfm) |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 308 | { |
Herbert Xu | 92d95ba | 2015-07-30 17:53:16 +0800 | [diff] [blame] | 309 | struct aead_instance *inst = aead_alg_instance(tfm); |
| 310 | struct authenc_instance_ctx *ictx = aead_instance_ctx(inst); |
| 311 | struct crypto_authenc_ctx *ctx = crypto_aead_ctx(tfm); |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 312 | struct crypto_ahash *auth; |
Herbert Xu | 7217d49 | 2016-07-12 13:17:34 +0800 | [diff] [blame] | 313 | struct crypto_skcipher *enc; |
Kees Cook | 8d60539 | 2018-09-18 19:10:51 -0700 | [diff] [blame] | 314 | struct crypto_sync_skcipher *null; |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 315 | int err; |
| 316 | |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 317 | auth = crypto_spawn_ahash(&ictx->auth); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 318 | if (IS_ERR(auth)) |
| 319 | return PTR_ERR(auth); |
| 320 | |
Eric Biggers | 60425a8 | 2016-10-28 09:52:19 -0700 | [diff] [blame] | 321 | enc = crypto_spawn_skcipher(&ictx->enc); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 322 | err = PTR_ERR(enc); |
| 323 | if (IS_ERR(enc)) |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 324 | goto err_free_ahash; |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 325 | |
Eric Biggers | 3a2d4fb | 2017-12-07 10:56:34 -0800 | [diff] [blame] | 326 | null = crypto_get_default_null_skcipher(); |
Herbert Xu | 92d95ba | 2015-07-30 17:53:16 +0800 | [diff] [blame] | 327 | err = PTR_ERR(null); |
| 328 | if (IS_ERR(null)) |
| 329 | goto err_free_skcipher; |
| 330 | |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 331 | ctx->auth = auth; |
| 332 | ctx->enc = enc; |
Herbert Xu | 92d95ba | 2015-07-30 17:53:16 +0800 | [diff] [blame] | 333 | ctx->null = null; |
Richard Hartmann | f3542e6 | 2010-02-16 20:27:20 +0800 | [diff] [blame] | 334 | |
Herbert Xu | 92d95ba | 2015-07-30 17:53:16 +0800 | [diff] [blame] | 335 | crypto_aead_set_reqsize( |
| 336 | tfm, |
Herbert Xu | 25df919 | 2015-05-11 17:47:53 +0800 | [diff] [blame] | 337 | sizeof(struct authenc_request_ctx) + |
Herbert Xu | 92d95ba | 2015-07-30 17:53:16 +0800 | [diff] [blame] | 338 | ictx->reqoff + |
Herbert Xu | 25df919 | 2015-05-11 17:47:53 +0800 | [diff] [blame] | 339 | max_t(unsigned int, |
Herbert Xu | 92d95ba | 2015-07-30 17:53:16 +0800 | [diff] [blame] | 340 | crypto_ahash_reqsize(auth) + |
| 341 | sizeof(struct ahash_request), |
Herbert Xu | 7217d49 | 2016-07-12 13:17:34 +0800 | [diff] [blame] | 342 | sizeof(struct skcipher_request) + |
| 343 | crypto_skcipher_reqsize(enc))); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 344 | |
| 345 | return 0; |
| 346 | |
Herbert Xu | 92d95ba | 2015-07-30 17:53:16 +0800 | [diff] [blame] | 347 | err_free_skcipher: |
Herbert Xu | 7217d49 | 2016-07-12 13:17:34 +0800 | [diff] [blame] | 348 | crypto_free_skcipher(enc); |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 349 | err_free_ahash: |
| 350 | crypto_free_ahash(auth); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 351 | return err; |
| 352 | } |
| 353 | |
Herbert Xu | 92d95ba | 2015-07-30 17:53:16 +0800 | [diff] [blame] | 354 | static void crypto_authenc_exit_tfm(struct crypto_aead *tfm) |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 355 | { |
Herbert Xu | 92d95ba | 2015-07-30 17:53:16 +0800 | [diff] [blame] | 356 | struct crypto_authenc_ctx *ctx = crypto_aead_ctx(tfm); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 357 | |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 358 | crypto_free_ahash(ctx->auth); |
Herbert Xu | 7217d49 | 2016-07-12 13:17:34 +0800 | [diff] [blame] | 359 | crypto_free_skcipher(ctx->enc); |
Eric Biggers | 3a2d4fb | 2017-12-07 10:56:34 -0800 | [diff] [blame] | 360 | crypto_put_default_null_skcipher(); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 361 | } |
| 362 | |
Herbert Xu | 92d95ba | 2015-07-30 17:53:16 +0800 | [diff] [blame] | 363 | static void crypto_authenc_free(struct aead_instance *inst) |
| 364 | { |
| 365 | struct authenc_instance_ctx *ctx = aead_instance_ctx(inst); |
| 366 | |
| 367 | crypto_drop_skcipher(&ctx->enc); |
| 368 | crypto_drop_ahash(&ctx->auth); |
| 369 | kfree(inst); |
| 370 | } |
| 371 | |
| 372 | static int crypto_authenc_create(struct crypto_template *tmpl, |
| 373 | struct rtattr **tb) |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 374 | { |
Eric Biggers | b9f76dd | 2020-01-02 19:58:45 -0800 | [diff] [blame] | 375 | u32 mask; |
Herbert Xu | 92d95ba | 2015-07-30 17:53:16 +0800 | [diff] [blame] | 376 | struct aead_instance *inst; |
Eric Biggers | 37a861a | 2020-01-02 19:58:55 -0800 | [diff] [blame] | 377 | struct authenc_instance_ctx *ctx; |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 378 | struct hash_alg_common *auth; |
| 379 | struct crypto_alg *auth_base; |
Herbert Xu | 7217d49 | 2016-07-12 13:17:34 +0800 | [diff] [blame] | 380 | struct skcipher_alg *enc; |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 381 | int err; |
| 382 | |
Eric Biggers | 7bcb2c9 | 2020-07-09 23:20:38 -0700 | [diff] [blame] | 383 | err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_AEAD, &mask); |
| 384 | if (err) |
| 385 | return err; |
Eric Biggers | b9f76dd | 2020-01-02 19:58:45 -0800 | [diff] [blame] | 386 | |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 387 | inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 388 | if (!inst) |
Eric Biggers | 37a861a | 2020-01-02 19:58:55 -0800 | [diff] [blame] | 389 | return -ENOMEM; |
Herbert Xu | 92d95ba | 2015-07-30 17:53:16 +0800 | [diff] [blame] | 390 | ctx = aead_instance_ctx(inst); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 391 | |
Eric Biggers | 37a861a | 2020-01-02 19:58:55 -0800 | [diff] [blame] | 392 | err = crypto_grab_ahash(&ctx->auth, aead_crypto_instance(inst), |
| 393 | crypto_attr_alg_name(tb[1]), 0, mask); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 394 | if (err) |
| 395 | goto err_free_inst; |
Eric Biggers | 37a861a | 2020-01-02 19:58:55 -0800 | [diff] [blame] | 396 | auth = crypto_spawn_ahash_alg(&ctx->auth); |
| 397 | auth_base = &auth->base; |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 398 | |
Eric Biggers | b9f76dd | 2020-01-02 19:58:45 -0800 | [diff] [blame] | 399 | err = crypto_grab_skcipher(&ctx->enc, aead_crypto_instance(inst), |
Eric Biggers | 37a861a | 2020-01-02 19:58:55 -0800 | [diff] [blame] | 400 | crypto_attr_alg_name(tb[2]), 0, mask); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 401 | if (err) |
Eric Biggers | 37a861a | 2020-01-02 19:58:55 -0800 | [diff] [blame] | 402 | goto err_free_inst; |
Herbert Xu | 7217d49 | 2016-07-12 13:17:34 +0800 | [diff] [blame] | 403 | enc = crypto_spawn_skcipher_alg(&ctx->enc); |
Herbert Xu | 9ffde35a | 2007-12-17 20:12:49 +0800 | [diff] [blame] | 404 | |
Herbert Xu | 92d95ba | 2015-07-30 17:53:16 +0800 | [diff] [blame] | 405 | ctx->reqoff = ALIGN(2 * auth->digestsize + auth_base->cra_alignmask, |
| 406 | auth_base->cra_alignmask + 1); |
| 407 | |
Herbert Xu | 9ffde35a | 2007-12-17 20:12:49 +0800 | [diff] [blame] | 408 | err = -ENAMETOOLONG; |
Herbert Xu | 92d95ba | 2015-07-30 17:53:16 +0800 | [diff] [blame] | 409 | if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME, |
Herbert Xu | 7217d49 | 2016-07-12 13:17:34 +0800 | [diff] [blame] | 410 | "authenc(%s,%s)", auth_base->cra_name, |
| 411 | enc->base.cra_name) >= |
Herbert Xu | 9ffde35a | 2007-12-17 20:12:49 +0800 | [diff] [blame] | 412 | CRYPTO_MAX_ALG_NAME) |
Eric Biggers | 37a861a | 2020-01-02 19:58:55 -0800 | [diff] [blame] | 413 | goto err_free_inst; |
Herbert Xu | 9ffde35a | 2007-12-17 20:12:49 +0800 | [diff] [blame] | 414 | |
Herbert Xu | 92d95ba | 2015-07-30 17:53:16 +0800 | [diff] [blame] | 415 | if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME, |
Steffen Klassert | cbdcf80 | 2009-08-05 19:35:34 +1000 | [diff] [blame] | 416 | "authenc(%s,%s)", auth_base->cra_driver_name, |
Herbert Xu | 7217d49 | 2016-07-12 13:17:34 +0800 | [diff] [blame] | 417 | enc->base.cra_driver_name) >= CRYPTO_MAX_ALG_NAME) |
Eric Biggers | 37a861a | 2020-01-02 19:58:55 -0800 | [diff] [blame] | 418 | goto err_free_inst; |
Herbert Xu | 9ffde35a | 2007-12-17 20:12:49 +0800 | [diff] [blame] | 419 | |
Herbert Xu | 7217d49 | 2016-07-12 13:17:34 +0800 | [diff] [blame] | 420 | inst->alg.base.cra_priority = enc->base.cra_priority * 10 + |
Herbert Xu | 92d95ba | 2015-07-30 17:53:16 +0800 | [diff] [blame] | 421 | auth_base->cra_priority; |
Herbert Xu | 7217d49 | 2016-07-12 13:17:34 +0800 | [diff] [blame] | 422 | inst->alg.base.cra_blocksize = enc->base.cra_blocksize; |
Herbert Xu | 92d95ba | 2015-07-30 17:53:16 +0800 | [diff] [blame] | 423 | inst->alg.base.cra_alignmask = auth_base->cra_alignmask | |
Herbert Xu | 7217d49 | 2016-07-12 13:17:34 +0800 | [diff] [blame] | 424 | enc->base.cra_alignmask; |
Herbert Xu | 92d95ba | 2015-07-30 17:53:16 +0800 | [diff] [blame] | 425 | inst->alg.base.cra_ctxsize = sizeof(struct crypto_authenc_ctx); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 426 | |
Herbert Xu | 7217d49 | 2016-07-12 13:17:34 +0800 | [diff] [blame] | 427 | inst->alg.ivsize = crypto_skcipher_alg_ivsize(enc); |
| 428 | inst->alg.chunksize = crypto_skcipher_alg_chunksize(enc); |
Herbert Xu | 92d95ba | 2015-07-30 17:53:16 +0800 | [diff] [blame] | 429 | inst->alg.maxauthsize = auth->digestsize; |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 430 | |
Herbert Xu | 92d95ba | 2015-07-30 17:53:16 +0800 | [diff] [blame] | 431 | inst->alg.init = crypto_authenc_init_tfm; |
| 432 | inst->alg.exit = crypto_authenc_exit_tfm; |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 433 | |
Herbert Xu | 92d95ba | 2015-07-30 17:53:16 +0800 | [diff] [blame] | 434 | inst->alg.setkey = crypto_authenc_setkey; |
| 435 | inst->alg.encrypt = crypto_authenc_encrypt; |
| 436 | inst->alg.decrypt = crypto_authenc_decrypt; |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 437 | |
Herbert Xu | 92d95ba | 2015-07-30 17:53:16 +0800 | [diff] [blame] | 438 | inst->free = crypto_authenc_free; |
| 439 | |
| 440 | err = aead_register_instance(tmpl, inst); |
Eric Biggers | 37a861a | 2020-01-02 19:58:55 -0800 | [diff] [blame] | 441 | if (err) { |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 442 | err_free_inst: |
Eric Biggers | 37a861a | 2020-01-02 19:58:55 -0800 | [diff] [blame] | 443 | crypto_authenc_free(inst); |
| 444 | } |
| 445 | return err; |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 446 | } |
| 447 | |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 448 | static struct crypto_template crypto_authenc_tmpl = { |
| 449 | .name = "authenc", |
Herbert Xu | 92d95ba | 2015-07-30 17:53:16 +0800 | [diff] [blame] | 450 | .create = crypto_authenc_create, |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 451 | .module = THIS_MODULE, |
| 452 | }; |
| 453 | |
| 454 | static int __init crypto_authenc_module_init(void) |
| 455 | { |
| 456 | return crypto_register_template(&crypto_authenc_tmpl); |
| 457 | } |
| 458 | |
| 459 | static void __exit crypto_authenc_module_exit(void) |
| 460 | { |
| 461 | crypto_unregister_template(&crypto_authenc_tmpl); |
| 462 | } |
| 463 | |
Eric Biggers | c4741b2 | 2019-04-11 21:57:42 -0700 | [diff] [blame] | 464 | subsys_initcall(crypto_authenc_module_init); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 465 | module_exit(crypto_authenc_module_exit); |
| 466 | |
| 467 | MODULE_LICENSE("GPL"); |
| 468 | MODULE_DESCRIPTION("Simple AEAD wrapper for IPsec"); |
Kees Cook | 4943ba1 | 2014-11-24 16:32:38 -0800 | [diff] [blame] | 469 | MODULE_ALIAS_CRYPTO("authenc"); |