Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 2 | /* |
| 3 | * ChaCha20-Poly1305 AEAD, RFC7539 |
| 4 | * |
| 5 | * Copyright (C) 2015 Martin Willi |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <crypto/internal/aead.h> |
| 9 | #include <crypto/internal/hash.h> |
| 10 | #include <crypto/internal/skcipher.h> |
| 11 | #include <crypto/scatterwalk.h> |
Eric Biggers | 1ca1b91 | 2018-11-16 17:26:21 -0800 | [diff] [blame] | 12 | #include <crypto/chacha.h> |
Martin Willi | 2546f81 | 2015-07-16 19:14:05 +0200 | [diff] [blame] | 13 | #include <crypto/poly1305.h> |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 14 | #include <linux/err.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/module.h> |
| 18 | |
| 19 | #include "internal.h" |
| 20 | |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 21 | struct chachapoly_instance_ctx { |
| 22 | struct crypto_skcipher_spawn chacha; |
| 23 | struct crypto_ahash_spawn poly; |
| 24 | unsigned int saltlen; |
| 25 | }; |
| 26 | |
| 27 | struct chachapoly_ctx { |
Herbert Xu | 1e1f006 | 2016-07-12 13:17:40 +0800 | [diff] [blame] | 28 | struct crypto_skcipher *chacha; |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 29 | struct crypto_ahash *poly; |
| 30 | /* key bytes we use for the ChaCha20 IV */ |
| 31 | unsigned int saltlen; |
| 32 | u8 salt[]; |
| 33 | }; |
| 34 | |
| 35 | struct poly_req { |
| 36 | /* zero byte padding for AD/ciphertext, as needed */ |
| 37 | u8 pad[POLY1305_BLOCK_SIZE]; |
| 38 | /* tail data with AD/ciphertext lengths */ |
| 39 | struct { |
| 40 | __le64 assoclen; |
| 41 | __le64 cryptlen; |
| 42 | } tail; |
| 43 | struct scatterlist src[1]; |
| 44 | struct ahash_request req; /* must be last member */ |
| 45 | }; |
| 46 | |
| 47 | struct chacha_req { |
Eric Biggers | 1ca1b91 | 2018-11-16 17:26:21 -0800 | [diff] [blame] | 48 | u8 iv[CHACHA_IV_SIZE]; |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 49 | struct scatterlist src[1]; |
Herbert Xu | 1e1f006 | 2016-07-12 13:17:40 +0800 | [diff] [blame] | 50 | struct skcipher_request req; /* must be last member */ |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | struct chachapoly_req_ctx { |
Herbert Xu | 7479092 | 2015-07-16 12:35:08 +0800 | [diff] [blame] | 54 | struct scatterlist src[2]; |
| 55 | struct scatterlist dst[2]; |
Martin Willi | c2b7b20a | 2015-06-16 11:34:16 +0200 | [diff] [blame] | 56 | /* the key we generate for Poly1305 using Chacha20 */ |
| 57 | u8 key[POLY1305_KEY_SIZE]; |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 58 | /* calculated Poly1305 tag */ |
| 59 | u8 tag[POLY1305_DIGEST_SIZE]; |
| 60 | /* length of data to en/decrypt, without ICV */ |
| 61 | unsigned int cryptlen; |
Herbert Xu | 7479092 | 2015-07-16 12:35:08 +0800 | [diff] [blame] | 62 | /* Actual AD, excluding IV */ |
| 63 | unsigned int assoclen; |
Eric Biggers | 7545b6c | 2019-05-31 11:12:30 -0700 | [diff] [blame] | 64 | /* request flags, with MAY_SLEEP cleared if needed */ |
| 65 | u32 flags; |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 66 | union { |
| 67 | struct poly_req poly; |
| 68 | struct chacha_req chacha; |
| 69 | } u; |
| 70 | }; |
| 71 | |
| 72 | static inline void async_done_continue(struct aead_request *req, int err, |
| 73 | int (*cont)(struct aead_request *)) |
| 74 | { |
Eric Biggers | 7545b6c | 2019-05-31 11:12:30 -0700 | [diff] [blame] | 75 | if (!err) { |
| 76 | struct chachapoly_req_ctx *rctx = aead_request_ctx(req); |
| 77 | |
| 78 | rctx->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP; |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 79 | err = cont(req); |
Eric Biggers | 7545b6c | 2019-05-31 11:12:30 -0700 | [diff] [blame] | 80 | } |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 81 | |
| 82 | if (err != -EINPROGRESS && err != -EBUSY) |
| 83 | aead_request_complete(req, err); |
| 84 | } |
| 85 | |
| 86 | static void chacha_iv(u8 *iv, struct aead_request *req, u32 icb) |
| 87 | { |
| 88 | struct chachapoly_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req)); |
| 89 | __le32 leicb = cpu_to_le32(icb); |
| 90 | |
| 91 | memcpy(iv, &leicb, sizeof(leicb)); |
| 92 | memcpy(iv + sizeof(leicb), ctx->salt, ctx->saltlen); |
| 93 | memcpy(iv + sizeof(leicb) + ctx->saltlen, req->iv, |
Eric Biggers | 1ca1b91 | 2018-11-16 17:26:21 -0800 | [diff] [blame] | 94 | CHACHA_IV_SIZE - sizeof(leicb) - ctx->saltlen); |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | static int poly_verify_tag(struct aead_request *req) |
| 98 | { |
| 99 | struct chachapoly_req_ctx *rctx = aead_request_ctx(req); |
| 100 | u8 tag[sizeof(rctx->tag)]; |
| 101 | |
Herbert Xu | 7479092 | 2015-07-16 12:35:08 +0800 | [diff] [blame] | 102 | scatterwalk_map_and_copy(tag, req->src, |
| 103 | req->assoclen + rctx->cryptlen, |
| 104 | sizeof(tag), 0); |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 105 | if (crypto_memneq(tag, rctx->tag, sizeof(tag))) |
| 106 | return -EBADMSG; |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | static int poly_copy_tag(struct aead_request *req) |
| 111 | { |
| 112 | struct chachapoly_req_ctx *rctx = aead_request_ctx(req); |
| 113 | |
Herbert Xu | 7479092 | 2015-07-16 12:35:08 +0800 | [diff] [blame] | 114 | scatterwalk_map_and_copy(rctx->tag, req->dst, |
| 115 | req->assoclen + rctx->cryptlen, |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 116 | sizeof(rctx->tag), 1); |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | static void chacha_decrypt_done(struct crypto_async_request *areq, int err) |
| 121 | { |
| 122 | async_done_continue(areq->data, err, poly_verify_tag); |
| 123 | } |
| 124 | |
| 125 | static int chacha_decrypt(struct aead_request *req) |
| 126 | { |
| 127 | struct chachapoly_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req)); |
| 128 | struct chachapoly_req_ctx *rctx = aead_request_ctx(req); |
| 129 | struct chacha_req *creq = &rctx->u.chacha; |
Herbert Xu | 7479092 | 2015-07-16 12:35:08 +0800 | [diff] [blame] | 130 | struct scatterlist *src, *dst; |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 131 | int err; |
| 132 | |
Jason A. Donenfeld | 161151d | 2015-12-06 02:51:38 +0100 | [diff] [blame] | 133 | if (rctx->cryptlen == 0) |
| 134 | goto skip; |
| 135 | |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 136 | chacha_iv(creq->iv, req, 1); |
| 137 | |
Herbert Xu | 7479092 | 2015-07-16 12:35:08 +0800 | [diff] [blame] | 138 | src = scatterwalk_ffwd(rctx->src, req->src, req->assoclen); |
| 139 | dst = src; |
Eric Biggers | 76cadf2 | 2019-06-02 22:46:34 -0700 | [diff] [blame] | 140 | if (req->src != req->dst) |
Herbert Xu | 7479092 | 2015-07-16 12:35:08 +0800 | [diff] [blame] | 141 | dst = scatterwalk_ffwd(rctx->dst, req->dst, req->assoclen); |
Herbert Xu | 7479092 | 2015-07-16 12:35:08 +0800 | [diff] [blame] | 142 | |
Eric Biggers | 7545b6c | 2019-05-31 11:12:30 -0700 | [diff] [blame] | 143 | skcipher_request_set_callback(&creq->req, rctx->flags, |
Herbert Xu | 1e1f006 | 2016-07-12 13:17:40 +0800 | [diff] [blame] | 144 | chacha_decrypt_done, req); |
| 145 | skcipher_request_set_tfm(&creq->req, ctx->chacha); |
| 146 | skcipher_request_set_crypt(&creq->req, src, dst, |
| 147 | rctx->cryptlen, creq->iv); |
| 148 | err = crypto_skcipher_decrypt(&creq->req); |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 149 | if (err) |
| 150 | return err; |
| 151 | |
Jason A. Donenfeld | 161151d | 2015-12-06 02:51:38 +0100 | [diff] [blame] | 152 | skip: |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 153 | return poly_verify_tag(req); |
| 154 | } |
| 155 | |
| 156 | static int poly_tail_continue(struct aead_request *req) |
| 157 | { |
| 158 | struct chachapoly_req_ctx *rctx = aead_request_ctx(req); |
| 159 | |
| 160 | if (rctx->cryptlen == req->cryptlen) /* encrypting */ |
| 161 | return poly_copy_tag(req); |
| 162 | |
| 163 | return chacha_decrypt(req); |
| 164 | } |
| 165 | |
| 166 | static void poly_tail_done(struct crypto_async_request *areq, int err) |
| 167 | { |
| 168 | async_done_continue(areq->data, err, poly_tail_continue); |
| 169 | } |
| 170 | |
| 171 | static int poly_tail(struct aead_request *req) |
| 172 | { |
Herbert Xu | 7479092 | 2015-07-16 12:35:08 +0800 | [diff] [blame] | 173 | struct crypto_aead *tfm = crypto_aead_reqtfm(req); |
| 174 | struct chachapoly_ctx *ctx = crypto_aead_ctx(tfm); |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 175 | struct chachapoly_req_ctx *rctx = aead_request_ctx(req); |
| 176 | struct poly_req *preq = &rctx->u.poly; |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 177 | int err; |
| 178 | |
Eric Biggers | 76cadf2 | 2019-06-02 22:46:34 -0700 | [diff] [blame] | 179 | preq->tail.assoclen = cpu_to_le64(rctx->assoclen); |
| 180 | preq->tail.cryptlen = cpu_to_le64(rctx->cryptlen); |
| 181 | sg_init_one(preq->src, &preq->tail, sizeof(preq->tail)); |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 182 | |
Eric Biggers | 7545b6c | 2019-05-31 11:12:30 -0700 | [diff] [blame] | 183 | ahash_request_set_callback(&preq->req, rctx->flags, |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 184 | poly_tail_done, req); |
| 185 | ahash_request_set_tfm(&preq->req, ctx->poly); |
| 186 | ahash_request_set_crypt(&preq->req, preq->src, |
| 187 | rctx->tag, sizeof(preq->tail)); |
| 188 | |
| 189 | err = crypto_ahash_finup(&preq->req); |
| 190 | if (err) |
| 191 | return err; |
| 192 | |
| 193 | return poly_tail_continue(req); |
| 194 | } |
| 195 | |
| 196 | static void poly_cipherpad_done(struct crypto_async_request *areq, int err) |
| 197 | { |
| 198 | async_done_continue(areq->data, err, poly_tail); |
| 199 | } |
| 200 | |
| 201 | static int poly_cipherpad(struct aead_request *req) |
| 202 | { |
| 203 | struct chachapoly_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req)); |
| 204 | struct chachapoly_req_ctx *rctx = aead_request_ctx(req); |
| 205 | struct poly_req *preq = &rctx->u.poly; |
Eric Biggers | 76cadf2 | 2019-06-02 22:46:34 -0700 | [diff] [blame] | 206 | unsigned int padlen; |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 207 | int err; |
| 208 | |
Eric Biggers | 76cadf2 | 2019-06-02 22:46:34 -0700 | [diff] [blame] | 209 | padlen = -rctx->cryptlen % POLY1305_BLOCK_SIZE; |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 210 | memset(preq->pad, 0, sizeof(preq->pad)); |
Eric Biggers | 76cadf2 | 2019-06-02 22:46:34 -0700 | [diff] [blame] | 211 | sg_init_one(preq->src, preq->pad, padlen); |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 212 | |
Eric Biggers | 7545b6c | 2019-05-31 11:12:30 -0700 | [diff] [blame] | 213 | ahash_request_set_callback(&preq->req, rctx->flags, |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 214 | poly_cipherpad_done, req); |
| 215 | ahash_request_set_tfm(&preq->req, ctx->poly); |
| 216 | ahash_request_set_crypt(&preq->req, preq->src, NULL, padlen); |
| 217 | |
| 218 | err = crypto_ahash_update(&preq->req); |
| 219 | if (err) |
| 220 | return err; |
| 221 | |
| 222 | return poly_tail(req); |
| 223 | } |
| 224 | |
| 225 | static void poly_cipher_done(struct crypto_async_request *areq, int err) |
| 226 | { |
| 227 | async_done_continue(areq->data, err, poly_cipherpad); |
| 228 | } |
| 229 | |
| 230 | static int poly_cipher(struct aead_request *req) |
| 231 | { |
| 232 | struct chachapoly_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req)); |
| 233 | struct chachapoly_req_ctx *rctx = aead_request_ctx(req); |
| 234 | struct poly_req *preq = &rctx->u.poly; |
| 235 | struct scatterlist *crypt = req->src; |
| 236 | int err; |
| 237 | |
| 238 | if (rctx->cryptlen == req->cryptlen) /* encrypting */ |
| 239 | crypt = req->dst; |
| 240 | |
Herbert Xu | 7479092 | 2015-07-16 12:35:08 +0800 | [diff] [blame] | 241 | crypt = scatterwalk_ffwd(rctx->src, crypt, req->assoclen); |
| 242 | |
Eric Biggers | 7545b6c | 2019-05-31 11:12:30 -0700 | [diff] [blame] | 243 | ahash_request_set_callback(&preq->req, rctx->flags, |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 244 | poly_cipher_done, req); |
| 245 | ahash_request_set_tfm(&preq->req, ctx->poly); |
| 246 | ahash_request_set_crypt(&preq->req, crypt, NULL, rctx->cryptlen); |
| 247 | |
| 248 | err = crypto_ahash_update(&preq->req); |
| 249 | if (err) |
| 250 | return err; |
| 251 | |
| 252 | return poly_cipherpad(req); |
| 253 | } |
| 254 | |
| 255 | static void poly_adpad_done(struct crypto_async_request *areq, int err) |
| 256 | { |
| 257 | async_done_continue(areq->data, err, poly_cipher); |
| 258 | } |
| 259 | |
| 260 | static int poly_adpad(struct aead_request *req) |
| 261 | { |
| 262 | struct chachapoly_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req)); |
| 263 | struct chachapoly_req_ctx *rctx = aead_request_ctx(req); |
| 264 | struct poly_req *preq = &rctx->u.poly; |
Eric Biggers | 76cadf2 | 2019-06-02 22:46:34 -0700 | [diff] [blame] | 265 | unsigned int padlen; |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 266 | int err; |
| 267 | |
Eric Biggers | 76cadf2 | 2019-06-02 22:46:34 -0700 | [diff] [blame] | 268 | padlen = -rctx->assoclen % POLY1305_BLOCK_SIZE; |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 269 | memset(preq->pad, 0, sizeof(preq->pad)); |
Eric Biggers | 76cadf2 | 2019-06-02 22:46:34 -0700 | [diff] [blame] | 270 | sg_init_one(preq->src, preq->pad, padlen); |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 271 | |
Eric Biggers | 7545b6c | 2019-05-31 11:12:30 -0700 | [diff] [blame] | 272 | ahash_request_set_callback(&preq->req, rctx->flags, |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 273 | poly_adpad_done, req); |
| 274 | ahash_request_set_tfm(&preq->req, ctx->poly); |
| 275 | ahash_request_set_crypt(&preq->req, preq->src, NULL, padlen); |
| 276 | |
| 277 | err = crypto_ahash_update(&preq->req); |
| 278 | if (err) |
| 279 | return err; |
| 280 | |
| 281 | return poly_cipher(req); |
| 282 | } |
| 283 | |
| 284 | static void poly_ad_done(struct crypto_async_request *areq, int err) |
| 285 | { |
| 286 | async_done_continue(areq->data, err, poly_adpad); |
| 287 | } |
| 288 | |
| 289 | static int poly_ad(struct aead_request *req) |
| 290 | { |
| 291 | struct chachapoly_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req)); |
| 292 | struct chachapoly_req_ctx *rctx = aead_request_ctx(req); |
| 293 | struct poly_req *preq = &rctx->u.poly; |
| 294 | int err; |
| 295 | |
Eric Biggers | 7545b6c | 2019-05-31 11:12:30 -0700 | [diff] [blame] | 296 | ahash_request_set_callback(&preq->req, rctx->flags, |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 297 | poly_ad_done, req); |
| 298 | ahash_request_set_tfm(&preq->req, ctx->poly); |
Herbert Xu | 7479092 | 2015-07-16 12:35:08 +0800 | [diff] [blame] | 299 | ahash_request_set_crypt(&preq->req, req->src, NULL, rctx->assoclen); |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 300 | |
| 301 | err = crypto_ahash_update(&preq->req); |
| 302 | if (err) |
| 303 | return err; |
| 304 | |
| 305 | return poly_adpad(req); |
| 306 | } |
| 307 | |
Martin Willi | c2b7b20a | 2015-06-16 11:34:16 +0200 | [diff] [blame] | 308 | static void poly_setkey_done(struct crypto_async_request *areq, int err) |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 309 | { |
| 310 | async_done_continue(areq->data, err, poly_ad); |
| 311 | } |
| 312 | |
Martin Willi | c2b7b20a | 2015-06-16 11:34:16 +0200 | [diff] [blame] | 313 | static int poly_setkey(struct aead_request *req) |
| 314 | { |
| 315 | struct chachapoly_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req)); |
| 316 | struct chachapoly_req_ctx *rctx = aead_request_ctx(req); |
| 317 | struct poly_req *preq = &rctx->u.poly; |
| 318 | int err; |
| 319 | |
Eric Biggers | 76cadf2 | 2019-06-02 22:46:34 -0700 | [diff] [blame] | 320 | sg_init_one(preq->src, rctx->key, sizeof(rctx->key)); |
Martin Willi | c2b7b20a | 2015-06-16 11:34:16 +0200 | [diff] [blame] | 321 | |
Eric Biggers | 7545b6c | 2019-05-31 11:12:30 -0700 | [diff] [blame] | 322 | ahash_request_set_callback(&preq->req, rctx->flags, |
Martin Willi | c2b7b20a | 2015-06-16 11:34:16 +0200 | [diff] [blame] | 323 | poly_setkey_done, req); |
| 324 | ahash_request_set_tfm(&preq->req, ctx->poly); |
| 325 | ahash_request_set_crypt(&preq->req, preq->src, NULL, sizeof(rctx->key)); |
| 326 | |
| 327 | err = crypto_ahash_update(&preq->req); |
| 328 | if (err) |
| 329 | return err; |
| 330 | |
| 331 | return poly_ad(req); |
| 332 | } |
| 333 | |
| 334 | static void poly_init_done(struct crypto_async_request *areq, int err) |
| 335 | { |
| 336 | async_done_continue(areq->data, err, poly_setkey); |
| 337 | } |
| 338 | |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 339 | static int poly_init(struct aead_request *req) |
| 340 | { |
| 341 | struct chachapoly_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req)); |
| 342 | struct chachapoly_req_ctx *rctx = aead_request_ctx(req); |
| 343 | struct poly_req *preq = &rctx->u.poly; |
| 344 | int err; |
| 345 | |
Eric Biggers | 7545b6c | 2019-05-31 11:12:30 -0700 | [diff] [blame] | 346 | ahash_request_set_callback(&preq->req, rctx->flags, |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 347 | poly_init_done, req); |
| 348 | ahash_request_set_tfm(&preq->req, ctx->poly); |
| 349 | |
| 350 | err = crypto_ahash_init(&preq->req); |
| 351 | if (err) |
| 352 | return err; |
| 353 | |
Martin Willi | c2b7b20a | 2015-06-16 11:34:16 +0200 | [diff] [blame] | 354 | return poly_setkey(req); |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | static void poly_genkey_done(struct crypto_async_request *areq, int err) |
| 358 | { |
Martin Willi | c2b7b20a | 2015-06-16 11:34:16 +0200 | [diff] [blame] | 359 | async_done_continue(areq->data, err, poly_init); |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | static int poly_genkey(struct aead_request *req) |
| 363 | { |
Herbert Xu | 7479092 | 2015-07-16 12:35:08 +0800 | [diff] [blame] | 364 | struct crypto_aead *tfm = crypto_aead_reqtfm(req); |
| 365 | struct chachapoly_ctx *ctx = crypto_aead_ctx(tfm); |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 366 | struct chachapoly_req_ctx *rctx = aead_request_ctx(req); |
| 367 | struct chacha_req *creq = &rctx->u.chacha; |
| 368 | int err; |
| 369 | |
Herbert Xu | 7479092 | 2015-07-16 12:35:08 +0800 | [diff] [blame] | 370 | rctx->assoclen = req->assoclen; |
| 371 | |
| 372 | if (crypto_aead_ivsize(tfm) == 8) { |
| 373 | if (rctx->assoclen < 8) |
| 374 | return -EINVAL; |
| 375 | rctx->assoclen -= 8; |
| 376 | } |
| 377 | |
Martin Willi | c2b7b20a | 2015-06-16 11:34:16 +0200 | [diff] [blame] | 378 | memset(rctx->key, 0, sizeof(rctx->key)); |
Eric Biggers | 76cadf2 | 2019-06-02 22:46:34 -0700 | [diff] [blame] | 379 | sg_init_one(creq->src, rctx->key, sizeof(rctx->key)); |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 380 | |
| 381 | chacha_iv(creq->iv, req, 0); |
| 382 | |
Eric Biggers | 7545b6c | 2019-05-31 11:12:30 -0700 | [diff] [blame] | 383 | skcipher_request_set_callback(&creq->req, rctx->flags, |
Herbert Xu | 1e1f006 | 2016-07-12 13:17:40 +0800 | [diff] [blame] | 384 | poly_genkey_done, req); |
| 385 | skcipher_request_set_tfm(&creq->req, ctx->chacha); |
| 386 | skcipher_request_set_crypt(&creq->req, creq->src, creq->src, |
| 387 | POLY1305_KEY_SIZE, creq->iv); |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 388 | |
Herbert Xu | 1e1f006 | 2016-07-12 13:17:40 +0800 | [diff] [blame] | 389 | err = crypto_skcipher_decrypt(&creq->req); |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 390 | if (err) |
| 391 | return err; |
| 392 | |
Martin Willi | c2b7b20a | 2015-06-16 11:34:16 +0200 | [diff] [blame] | 393 | return poly_init(req); |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | static void chacha_encrypt_done(struct crypto_async_request *areq, int err) |
| 397 | { |
| 398 | async_done_continue(areq->data, err, poly_genkey); |
| 399 | } |
| 400 | |
| 401 | static int chacha_encrypt(struct aead_request *req) |
| 402 | { |
| 403 | struct chachapoly_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req)); |
| 404 | struct chachapoly_req_ctx *rctx = aead_request_ctx(req); |
| 405 | struct chacha_req *creq = &rctx->u.chacha; |
Herbert Xu | 7479092 | 2015-07-16 12:35:08 +0800 | [diff] [blame] | 406 | struct scatterlist *src, *dst; |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 407 | int err; |
| 408 | |
Jason A. Donenfeld | 161151d | 2015-12-06 02:51:38 +0100 | [diff] [blame] | 409 | if (req->cryptlen == 0) |
| 410 | goto skip; |
| 411 | |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 412 | chacha_iv(creq->iv, req, 1); |
| 413 | |
Herbert Xu | 7479092 | 2015-07-16 12:35:08 +0800 | [diff] [blame] | 414 | src = scatterwalk_ffwd(rctx->src, req->src, req->assoclen); |
| 415 | dst = src; |
Eric Biggers | 76cadf2 | 2019-06-02 22:46:34 -0700 | [diff] [blame] | 416 | if (req->src != req->dst) |
Herbert Xu | 7479092 | 2015-07-16 12:35:08 +0800 | [diff] [blame] | 417 | dst = scatterwalk_ffwd(rctx->dst, req->dst, req->assoclen); |
Herbert Xu | 7479092 | 2015-07-16 12:35:08 +0800 | [diff] [blame] | 418 | |
Eric Biggers | 7545b6c | 2019-05-31 11:12:30 -0700 | [diff] [blame] | 419 | skcipher_request_set_callback(&creq->req, rctx->flags, |
Herbert Xu | 1e1f006 | 2016-07-12 13:17:40 +0800 | [diff] [blame] | 420 | chacha_encrypt_done, req); |
| 421 | skcipher_request_set_tfm(&creq->req, ctx->chacha); |
| 422 | skcipher_request_set_crypt(&creq->req, src, dst, |
| 423 | req->cryptlen, creq->iv); |
| 424 | err = crypto_skcipher_encrypt(&creq->req); |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 425 | if (err) |
| 426 | return err; |
| 427 | |
Jason A. Donenfeld | 161151d | 2015-12-06 02:51:38 +0100 | [diff] [blame] | 428 | skip: |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 429 | return poly_genkey(req); |
| 430 | } |
| 431 | |
| 432 | static int chachapoly_encrypt(struct aead_request *req) |
| 433 | { |
| 434 | struct chachapoly_req_ctx *rctx = aead_request_ctx(req); |
| 435 | |
| 436 | rctx->cryptlen = req->cryptlen; |
Eric Biggers | 7545b6c | 2019-05-31 11:12:30 -0700 | [diff] [blame] | 437 | rctx->flags = aead_request_flags(req); |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 438 | |
| 439 | /* encrypt call chain: |
| 440 | * - chacha_encrypt/done() |
Martin Willi | c2b7b20a | 2015-06-16 11:34:16 +0200 | [diff] [blame] | 441 | * - poly_genkey/done() |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 442 | * - poly_init/done() |
Martin Willi | c2b7b20a | 2015-06-16 11:34:16 +0200 | [diff] [blame] | 443 | * - poly_setkey/done() |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 444 | * - poly_ad/done() |
| 445 | * - poly_adpad/done() |
| 446 | * - poly_cipher/done() |
| 447 | * - poly_cipherpad/done() |
| 448 | * - poly_tail/done/continue() |
| 449 | * - poly_copy_tag() |
| 450 | */ |
| 451 | return chacha_encrypt(req); |
| 452 | } |
| 453 | |
| 454 | static int chachapoly_decrypt(struct aead_request *req) |
| 455 | { |
| 456 | struct chachapoly_req_ctx *rctx = aead_request_ctx(req); |
| 457 | |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 458 | rctx->cryptlen = req->cryptlen - POLY1305_DIGEST_SIZE; |
Eric Biggers | 7545b6c | 2019-05-31 11:12:30 -0700 | [diff] [blame] | 459 | rctx->flags = aead_request_flags(req); |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 460 | |
| 461 | /* decrypt call chain: |
Martin Willi | c2b7b20a | 2015-06-16 11:34:16 +0200 | [diff] [blame] | 462 | * - poly_genkey/done() |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 463 | * - poly_init/done() |
Martin Willi | c2b7b20a | 2015-06-16 11:34:16 +0200 | [diff] [blame] | 464 | * - poly_setkey/done() |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 465 | * - poly_ad/done() |
| 466 | * - poly_adpad/done() |
| 467 | * - poly_cipher/done() |
| 468 | * - poly_cipherpad/done() |
| 469 | * - poly_tail/done/continue() |
| 470 | * - chacha_decrypt/done() |
| 471 | * - poly_verify_tag() |
| 472 | */ |
| 473 | return poly_genkey(req); |
| 474 | } |
| 475 | |
| 476 | static int chachapoly_setkey(struct crypto_aead *aead, const u8 *key, |
| 477 | unsigned int keylen) |
| 478 | { |
| 479 | struct chachapoly_ctx *ctx = crypto_aead_ctx(aead); |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 480 | |
Eric Biggers | 1ca1b91 | 2018-11-16 17:26:21 -0800 | [diff] [blame] | 481 | if (keylen != ctx->saltlen + CHACHA_KEY_SIZE) |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 482 | return -EINVAL; |
| 483 | |
| 484 | keylen -= ctx->saltlen; |
| 485 | memcpy(ctx->salt, key + keylen, ctx->saltlen); |
| 486 | |
Herbert Xu | 1e1f006 | 2016-07-12 13:17:40 +0800 | [diff] [blame] | 487 | crypto_skcipher_clear_flags(ctx->chacha, CRYPTO_TFM_REQ_MASK); |
| 488 | crypto_skcipher_set_flags(ctx->chacha, crypto_aead_get_flags(aead) & |
| 489 | CRYPTO_TFM_REQ_MASK); |
Eric Biggers | af5034e | 2019-12-30 21:19:38 -0600 | [diff] [blame] | 490 | return crypto_skcipher_setkey(ctx->chacha, key, keylen); |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | static int chachapoly_setauthsize(struct crypto_aead *tfm, |
| 494 | unsigned int authsize) |
| 495 | { |
| 496 | if (authsize != POLY1305_DIGEST_SIZE) |
| 497 | return -EINVAL; |
| 498 | |
| 499 | return 0; |
| 500 | } |
| 501 | |
Herbert Xu | 7479092 | 2015-07-16 12:35:08 +0800 | [diff] [blame] | 502 | static int chachapoly_init(struct crypto_aead *tfm) |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 503 | { |
Herbert Xu | 7479092 | 2015-07-16 12:35:08 +0800 | [diff] [blame] | 504 | struct aead_instance *inst = aead_alg_instance(tfm); |
| 505 | struct chachapoly_instance_ctx *ictx = aead_instance_ctx(inst); |
| 506 | struct chachapoly_ctx *ctx = crypto_aead_ctx(tfm); |
Herbert Xu | 1e1f006 | 2016-07-12 13:17:40 +0800 | [diff] [blame] | 507 | struct crypto_skcipher *chacha; |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 508 | struct crypto_ahash *poly; |
| 509 | unsigned long align; |
| 510 | |
| 511 | poly = crypto_spawn_ahash(&ictx->poly); |
| 512 | if (IS_ERR(poly)) |
| 513 | return PTR_ERR(poly); |
| 514 | |
Eric Biggers | 60425a8 | 2016-10-28 09:52:19 -0700 | [diff] [blame] | 515 | chacha = crypto_spawn_skcipher(&ictx->chacha); |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 516 | if (IS_ERR(chacha)) { |
| 517 | crypto_free_ahash(poly); |
| 518 | return PTR_ERR(chacha); |
| 519 | } |
| 520 | |
| 521 | ctx->chacha = chacha; |
| 522 | ctx->poly = poly; |
| 523 | ctx->saltlen = ictx->saltlen; |
| 524 | |
Herbert Xu | 7479092 | 2015-07-16 12:35:08 +0800 | [diff] [blame] | 525 | align = crypto_aead_alignmask(tfm); |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 526 | align &= ~(crypto_tfm_ctx_alignment() - 1); |
Herbert Xu | 7479092 | 2015-07-16 12:35:08 +0800 | [diff] [blame] | 527 | crypto_aead_set_reqsize( |
| 528 | tfm, |
| 529 | align + offsetof(struct chachapoly_req_ctx, u) + |
| 530 | max(offsetof(struct chacha_req, req) + |
Herbert Xu | 1e1f006 | 2016-07-12 13:17:40 +0800 | [diff] [blame] | 531 | sizeof(struct skcipher_request) + |
| 532 | crypto_skcipher_reqsize(chacha), |
Herbert Xu | 7479092 | 2015-07-16 12:35:08 +0800 | [diff] [blame] | 533 | offsetof(struct poly_req, req) + |
| 534 | sizeof(struct ahash_request) + |
| 535 | crypto_ahash_reqsize(poly))); |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 536 | |
| 537 | return 0; |
| 538 | } |
| 539 | |
Herbert Xu | 7479092 | 2015-07-16 12:35:08 +0800 | [diff] [blame] | 540 | static void chachapoly_exit(struct crypto_aead *tfm) |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 541 | { |
Herbert Xu | 7479092 | 2015-07-16 12:35:08 +0800 | [diff] [blame] | 542 | struct chachapoly_ctx *ctx = crypto_aead_ctx(tfm); |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 543 | |
| 544 | crypto_free_ahash(ctx->poly); |
Herbert Xu | 1e1f006 | 2016-07-12 13:17:40 +0800 | [diff] [blame] | 545 | crypto_free_skcipher(ctx->chacha); |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 546 | } |
| 547 | |
Herbert Xu | 7479092 | 2015-07-16 12:35:08 +0800 | [diff] [blame] | 548 | static void chachapoly_free(struct aead_instance *inst) |
| 549 | { |
| 550 | struct chachapoly_instance_ctx *ctx = aead_instance_ctx(inst); |
| 551 | |
| 552 | crypto_drop_skcipher(&ctx->chacha); |
| 553 | crypto_drop_ahash(&ctx->poly); |
| 554 | kfree(inst); |
| 555 | } |
| 556 | |
| 557 | static int chachapoly_create(struct crypto_template *tmpl, struct rtattr **tb, |
| 558 | const char *name, unsigned int ivsize) |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 559 | { |
| 560 | struct crypto_attr_type *algt; |
Eric Biggers | b9f76dd | 2020-01-02 19:58:45 -0800 | [diff] [blame^] | 561 | u32 mask; |
Herbert Xu | 7479092 | 2015-07-16 12:35:08 +0800 | [diff] [blame] | 562 | struct aead_instance *inst; |
Herbert Xu | 1e1f006 | 2016-07-12 13:17:40 +0800 | [diff] [blame] | 563 | struct skcipher_alg *chacha; |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 564 | struct crypto_alg *poly; |
Herbert Xu | 7479092 | 2015-07-16 12:35:08 +0800 | [diff] [blame] | 565 | struct hash_alg_common *poly_hash; |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 566 | struct chachapoly_instance_ctx *ctx; |
| 567 | const char *chacha_name, *poly_name; |
| 568 | int err; |
| 569 | |
| 570 | if (ivsize > CHACHAPOLY_IV_SIZE) |
Herbert Xu | 7479092 | 2015-07-16 12:35:08 +0800 | [diff] [blame] | 571 | return -EINVAL; |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 572 | |
| 573 | algt = crypto_get_attr_type(tb); |
| 574 | if (IS_ERR(algt)) |
Herbert Xu | 7479092 | 2015-07-16 12:35:08 +0800 | [diff] [blame] | 575 | return PTR_ERR(algt); |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 576 | |
Herbert Xu | 5e4b8c1 | 2015-08-13 17:29:06 +0800 | [diff] [blame] | 577 | if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask) |
Herbert Xu | 7479092 | 2015-07-16 12:35:08 +0800 | [diff] [blame] | 578 | return -EINVAL; |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 579 | |
Eric Biggers | b9f76dd | 2020-01-02 19:58:45 -0800 | [diff] [blame^] | 580 | mask = crypto_requires_sync(algt->type, algt->mask); |
| 581 | |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 582 | chacha_name = crypto_attr_alg_name(tb[1]); |
| 583 | if (IS_ERR(chacha_name)) |
Herbert Xu | 7479092 | 2015-07-16 12:35:08 +0800 | [diff] [blame] | 584 | return PTR_ERR(chacha_name); |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 585 | poly_name = crypto_attr_alg_name(tb[2]); |
| 586 | if (IS_ERR(poly_name)) |
Herbert Xu | 7479092 | 2015-07-16 12:35:08 +0800 | [diff] [blame] | 587 | return PTR_ERR(poly_name); |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 588 | |
| 589 | poly = crypto_find_alg(poly_name, &crypto_ahash_type, |
| 590 | CRYPTO_ALG_TYPE_HASH, |
Eric Biggers | b9f76dd | 2020-01-02 19:58:45 -0800 | [diff] [blame^] | 591 | CRYPTO_ALG_TYPE_AHASH_MASK | mask); |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 592 | if (IS_ERR(poly)) |
Herbert Xu | 7479092 | 2015-07-16 12:35:08 +0800 | [diff] [blame] | 593 | return PTR_ERR(poly); |
Eric Biggers | e57121d | 2017-12-11 12:15:17 -0800 | [diff] [blame] | 594 | poly_hash = __crypto_hash_alg_common(poly); |
| 595 | |
| 596 | err = -EINVAL; |
| 597 | if (poly_hash->digestsize != POLY1305_DIGEST_SIZE) |
| 598 | goto out_put_poly; |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 599 | |
| 600 | err = -ENOMEM; |
| 601 | inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL); |
| 602 | if (!inst) |
| 603 | goto out_put_poly; |
| 604 | |
Herbert Xu | 7479092 | 2015-07-16 12:35:08 +0800 | [diff] [blame] | 605 | ctx = aead_instance_ctx(inst); |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 606 | ctx->saltlen = CHACHAPOLY_IV_SIZE - ivsize; |
Herbert Xu | 7479092 | 2015-07-16 12:35:08 +0800 | [diff] [blame] | 607 | err = crypto_init_ahash_spawn(&ctx->poly, poly_hash, |
| 608 | aead_crypto_instance(inst)); |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 609 | if (err) |
| 610 | goto err_free_inst; |
| 611 | |
Eric Biggers | b9f76dd | 2020-01-02 19:58:45 -0800 | [diff] [blame^] | 612 | err = crypto_grab_skcipher(&ctx->chacha, aead_crypto_instance(inst), |
| 613 | chacha_name, 0, mask); |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 614 | if (err) |
| 615 | goto err_drop_poly; |
| 616 | |
Herbert Xu | 1e1f006 | 2016-07-12 13:17:40 +0800 | [diff] [blame] | 617 | chacha = crypto_spawn_skcipher_alg(&ctx->chacha); |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 618 | |
| 619 | err = -EINVAL; |
| 620 | /* Need 16-byte IV size, including Initial Block Counter value */ |
Eric Biggers | 1ca1b91 | 2018-11-16 17:26:21 -0800 | [diff] [blame] | 621 | if (crypto_skcipher_alg_ivsize(chacha) != CHACHA_IV_SIZE) |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 622 | goto out_drop_chacha; |
| 623 | /* Not a stream cipher? */ |
Herbert Xu | 1e1f006 | 2016-07-12 13:17:40 +0800 | [diff] [blame] | 624 | if (chacha->base.cra_blocksize != 1) |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 625 | goto out_drop_chacha; |
| 626 | |
| 627 | err = -ENAMETOOLONG; |
Herbert Xu | 7479092 | 2015-07-16 12:35:08 +0800 | [diff] [blame] | 628 | if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME, |
Eric Biggers | 5e27f38 | 2019-03-31 13:04:16 -0700 | [diff] [blame] | 629 | "%s(%s,%s)", name, chacha->base.cra_name, |
| 630 | poly->cra_name) >= CRYPTO_MAX_ALG_NAME) |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 631 | goto out_drop_chacha; |
Herbert Xu | 7479092 | 2015-07-16 12:35:08 +0800 | [diff] [blame] | 632 | if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME, |
Herbert Xu | 1e1f006 | 2016-07-12 13:17:40 +0800 | [diff] [blame] | 633 | "%s(%s,%s)", name, chacha->base.cra_driver_name, |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 634 | poly->cra_driver_name) >= CRYPTO_MAX_ALG_NAME) |
| 635 | goto out_drop_chacha; |
| 636 | |
Herbert Xu | 1e1f006 | 2016-07-12 13:17:40 +0800 | [diff] [blame] | 637 | inst->alg.base.cra_flags = (chacha->base.cra_flags | poly->cra_flags) & |
Herbert Xu | 7479092 | 2015-07-16 12:35:08 +0800 | [diff] [blame] | 638 | CRYPTO_ALG_ASYNC; |
Herbert Xu | 1e1f006 | 2016-07-12 13:17:40 +0800 | [diff] [blame] | 639 | inst->alg.base.cra_priority = (chacha->base.cra_priority + |
Herbert Xu | 7479092 | 2015-07-16 12:35:08 +0800 | [diff] [blame] | 640 | poly->cra_priority) / 2; |
| 641 | inst->alg.base.cra_blocksize = 1; |
Herbert Xu | 1e1f006 | 2016-07-12 13:17:40 +0800 | [diff] [blame] | 642 | inst->alg.base.cra_alignmask = chacha->base.cra_alignmask | |
Herbert Xu | 7479092 | 2015-07-16 12:35:08 +0800 | [diff] [blame] | 643 | poly->cra_alignmask; |
| 644 | inst->alg.base.cra_ctxsize = sizeof(struct chachapoly_ctx) + |
| 645 | ctx->saltlen; |
| 646 | inst->alg.ivsize = ivsize; |
Herbert Xu | 1e1f006 | 2016-07-12 13:17:40 +0800 | [diff] [blame] | 647 | inst->alg.chunksize = crypto_skcipher_alg_chunksize(chacha); |
Herbert Xu | 7479092 | 2015-07-16 12:35:08 +0800 | [diff] [blame] | 648 | inst->alg.maxauthsize = POLY1305_DIGEST_SIZE; |
| 649 | inst->alg.init = chachapoly_init; |
| 650 | inst->alg.exit = chachapoly_exit; |
| 651 | inst->alg.encrypt = chachapoly_encrypt; |
| 652 | inst->alg.decrypt = chachapoly_decrypt; |
| 653 | inst->alg.setkey = chachapoly_setkey; |
| 654 | inst->alg.setauthsize = chachapoly_setauthsize; |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 655 | |
Herbert Xu | 7479092 | 2015-07-16 12:35:08 +0800 | [diff] [blame] | 656 | inst->free = chachapoly_free; |
| 657 | |
| 658 | err = aead_register_instance(tmpl, inst); |
| 659 | if (err) |
| 660 | goto out_drop_chacha; |
| 661 | |
| 662 | out_put_poly: |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 663 | crypto_mod_put(poly); |
Herbert Xu | 7479092 | 2015-07-16 12:35:08 +0800 | [diff] [blame] | 664 | return err; |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 665 | |
| 666 | out_drop_chacha: |
| 667 | crypto_drop_skcipher(&ctx->chacha); |
| 668 | err_drop_poly: |
| 669 | crypto_drop_ahash(&ctx->poly); |
| 670 | err_free_inst: |
| 671 | kfree(inst); |
Herbert Xu | 7479092 | 2015-07-16 12:35:08 +0800 | [diff] [blame] | 672 | goto out_put_poly; |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 673 | } |
| 674 | |
Herbert Xu | 7479092 | 2015-07-16 12:35:08 +0800 | [diff] [blame] | 675 | static int rfc7539_create(struct crypto_template *tmpl, struct rtattr **tb) |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 676 | { |
Herbert Xu | 7479092 | 2015-07-16 12:35:08 +0800 | [diff] [blame] | 677 | return chachapoly_create(tmpl, tb, "rfc7539", 12); |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 678 | } |
| 679 | |
Herbert Xu | 7479092 | 2015-07-16 12:35:08 +0800 | [diff] [blame] | 680 | static int rfc7539esp_create(struct crypto_template *tmpl, struct rtattr **tb) |
Martin Willi | 4db4ad2 | 2015-06-01 13:44:02 +0200 | [diff] [blame] | 681 | { |
Herbert Xu | 7479092 | 2015-07-16 12:35:08 +0800 | [diff] [blame] | 682 | return chachapoly_create(tmpl, tb, "rfc7539esp", 8); |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 683 | } |
| 684 | |
Xiongfeng Wang | 1a5e02b | 2019-01-18 13:58:15 +0800 | [diff] [blame] | 685 | static struct crypto_template rfc7539_tmpls[] = { |
| 686 | { |
| 687 | .name = "rfc7539", |
| 688 | .create = rfc7539_create, |
| 689 | .module = THIS_MODULE, |
| 690 | }, { |
| 691 | .name = "rfc7539esp", |
| 692 | .create = rfc7539esp_create, |
| 693 | .module = THIS_MODULE, |
| 694 | }, |
Martin Willi | 4db4ad2 | 2015-06-01 13:44:02 +0200 | [diff] [blame] | 695 | }; |
| 696 | |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 697 | static int __init chacha20poly1305_module_init(void) |
| 698 | { |
Xiongfeng Wang | 1a5e02b | 2019-01-18 13:58:15 +0800 | [diff] [blame] | 699 | return crypto_register_templates(rfc7539_tmpls, |
| 700 | ARRAY_SIZE(rfc7539_tmpls)); |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | static void __exit chacha20poly1305_module_exit(void) |
| 704 | { |
Xiongfeng Wang | 1a5e02b | 2019-01-18 13:58:15 +0800 | [diff] [blame] | 705 | crypto_unregister_templates(rfc7539_tmpls, |
| 706 | ARRAY_SIZE(rfc7539_tmpls)); |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 707 | } |
| 708 | |
Eric Biggers | c4741b2 | 2019-04-11 21:57:42 -0700 | [diff] [blame] | 709 | subsys_initcall(chacha20poly1305_module_init); |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 710 | module_exit(chacha20poly1305_module_exit); |
| 711 | |
| 712 | MODULE_LICENSE("GPL"); |
| 713 | MODULE_AUTHOR("Martin Willi <martin@strongswan.org>"); |
| 714 | MODULE_DESCRIPTION("ChaCha20-Poly1305 AEAD"); |
Martin Willi | 71ebc4d | 2015-06-01 13:44:00 +0200 | [diff] [blame] | 715 | MODULE_ALIAS_CRYPTO("rfc7539"); |
Martin Willi | 4db4ad2 | 2015-06-01 13:44:02 +0200 | [diff] [blame] | 716 | MODULE_ALIAS_CRYPTO("rfc7539esp"); |