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