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