Back to home page

LXR

 
 

    


0001 /*
0002  * GCM: Galois/Counter Mode.
0003  *
0004  * Copyright (c) 2007 Nokia Siemens Networks - Mikko Herranen <mh1@iki.fi>
0005  *
0006  * This program is free software; you can redistribute it and/or modify it
0007  * under the terms of the GNU General Public License version 2 as published
0008  * by the Free Software Foundation.
0009  */
0010 
0011 #include <crypto/gf128mul.h>
0012 #include <crypto/internal/aead.h>
0013 #include <crypto/internal/skcipher.h>
0014 #include <crypto/internal/hash.h>
0015 #include <crypto/null.h>
0016 #include <crypto/scatterwalk.h>
0017 #include <crypto/hash.h>
0018 #include "internal.h"
0019 #include <linux/completion.h>
0020 #include <linux/err.h>
0021 #include <linux/init.h>
0022 #include <linux/kernel.h>
0023 #include <linux/module.h>
0024 #include <linux/slab.h>
0025 
0026 struct gcm_instance_ctx {
0027     struct crypto_skcipher_spawn ctr;
0028     struct crypto_ahash_spawn ghash;
0029 };
0030 
0031 struct crypto_gcm_ctx {
0032     struct crypto_skcipher *ctr;
0033     struct crypto_ahash *ghash;
0034 };
0035 
0036 struct crypto_rfc4106_ctx {
0037     struct crypto_aead *child;
0038     u8 nonce[4];
0039 };
0040 
0041 struct crypto_rfc4106_req_ctx {
0042     struct scatterlist src[3];
0043     struct scatterlist dst[3];
0044     struct aead_request subreq;
0045 };
0046 
0047 struct crypto_rfc4543_instance_ctx {
0048     struct crypto_aead_spawn aead;
0049 };
0050 
0051 struct crypto_rfc4543_ctx {
0052     struct crypto_aead *child;
0053     struct crypto_skcipher *null;
0054     u8 nonce[4];
0055 };
0056 
0057 struct crypto_rfc4543_req_ctx {
0058     struct aead_request subreq;
0059 };
0060 
0061 struct crypto_gcm_ghash_ctx {
0062     unsigned int cryptlen;
0063     struct scatterlist *src;
0064     int (*complete)(struct aead_request *req, u32 flags);
0065 };
0066 
0067 struct crypto_gcm_req_priv_ctx {
0068     u8 iv[16];
0069     u8 auth_tag[16];
0070     u8 iauth_tag[16];
0071     struct scatterlist src[3];
0072     struct scatterlist dst[3];
0073     struct scatterlist sg;
0074     struct crypto_gcm_ghash_ctx ghash_ctx;
0075     union {
0076         struct ahash_request ahreq;
0077         struct skcipher_request skreq;
0078     } u;
0079 };
0080 
0081 struct crypto_gcm_setkey_result {
0082     int err;
0083     struct completion completion;
0084 };
0085 
0086 static struct {
0087     u8 buf[16];
0088     struct scatterlist sg;
0089 } *gcm_zeroes;
0090 
0091 static int crypto_rfc4543_copy_src_to_dst(struct aead_request *req, bool enc);
0092 
0093 static inline struct crypto_gcm_req_priv_ctx *crypto_gcm_reqctx(
0094     struct aead_request *req)
0095 {
0096     unsigned long align = crypto_aead_alignmask(crypto_aead_reqtfm(req));
0097 
0098     return (void *)PTR_ALIGN((u8 *)aead_request_ctx(req), align + 1);
0099 }
0100 
0101 static void crypto_gcm_setkey_done(struct crypto_async_request *req, int err)
0102 {
0103     struct crypto_gcm_setkey_result *result = req->data;
0104 
0105     if (err == -EINPROGRESS)
0106         return;
0107 
0108     result->err = err;
0109     complete(&result->completion);
0110 }
0111 
0112 static int crypto_gcm_setkey(struct crypto_aead *aead, const u8 *key,
0113                  unsigned int keylen)
0114 {
0115     struct crypto_gcm_ctx *ctx = crypto_aead_ctx(aead);
0116     struct crypto_ahash *ghash = ctx->ghash;
0117     struct crypto_skcipher *ctr = ctx->ctr;
0118     struct {
0119         be128 hash;
0120         u8 iv[16];
0121 
0122         struct crypto_gcm_setkey_result result;
0123 
0124         struct scatterlist sg[1];
0125         struct skcipher_request req;
0126     } *data;
0127     int err;
0128 
0129     crypto_skcipher_clear_flags(ctr, CRYPTO_TFM_REQ_MASK);
0130     crypto_skcipher_set_flags(ctr, crypto_aead_get_flags(aead) &
0131                        CRYPTO_TFM_REQ_MASK);
0132     err = crypto_skcipher_setkey(ctr, key, keylen);
0133     crypto_aead_set_flags(aead, crypto_skcipher_get_flags(ctr) &
0134                     CRYPTO_TFM_RES_MASK);
0135     if (err)
0136         return err;
0137 
0138     data = kzalloc(sizeof(*data) + crypto_skcipher_reqsize(ctr),
0139                GFP_KERNEL);
0140     if (!data)
0141         return -ENOMEM;
0142 
0143     init_completion(&data->result.completion);
0144     sg_init_one(data->sg, &data->hash, sizeof(data->hash));
0145     skcipher_request_set_tfm(&data->req, ctr);
0146     skcipher_request_set_callback(&data->req, CRYPTO_TFM_REQ_MAY_SLEEP |
0147                           CRYPTO_TFM_REQ_MAY_BACKLOG,
0148                       crypto_gcm_setkey_done,
0149                       &data->result);
0150     skcipher_request_set_crypt(&data->req, data->sg, data->sg,
0151                    sizeof(data->hash), data->iv);
0152 
0153     err = crypto_skcipher_encrypt(&data->req);
0154     if (err == -EINPROGRESS || err == -EBUSY) {
0155         err = wait_for_completion_interruptible(
0156             &data->result.completion);
0157         if (!err)
0158             err = data->result.err;
0159     }
0160 
0161     if (err)
0162         goto out;
0163 
0164     crypto_ahash_clear_flags(ghash, CRYPTO_TFM_REQ_MASK);
0165     crypto_ahash_set_flags(ghash, crypto_aead_get_flags(aead) &
0166                    CRYPTO_TFM_REQ_MASK);
0167     err = crypto_ahash_setkey(ghash, (u8 *)&data->hash, sizeof(be128));
0168     crypto_aead_set_flags(aead, crypto_ahash_get_flags(ghash) &
0169                   CRYPTO_TFM_RES_MASK);
0170 
0171 out:
0172     kzfree(data);
0173     return err;
0174 }
0175 
0176 static int crypto_gcm_setauthsize(struct crypto_aead *tfm,
0177                   unsigned int authsize)
0178 {
0179     switch (authsize) {
0180     case 4:
0181     case 8:
0182     case 12:
0183     case 13:
0184     case 14:
0185     case 15:
0186     case 16:
0187         break;
0188     default:
0189         return -EINVAL;
0190     }
0191 
0192     return 0;
0193 }
0194 
0195 static void crypto_gcm_init_common(struct aead_request *req)
0196 {
0197     struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
0198     __be32 counter = cpu_to_be32(1);
0199     struct scatterlist *sg;
0200 
0201     memset(pctx->auth_tag, 0, sizeof(pctx->auth_tag));
0202     memcpy(pctx->iv, req->iv, 12);
0203     memcpy(pctx->iv + 12, &counter, 4);
0204 
0205     sg_init_table(pctx->src, 3);
0206     sg_set_buf(pctx->src, pctx->auth_tag, sizeof(pctx->auth_tag));
0207     sg = scatterwalk_ffwd(pctx->src + 1, req->src, req->assoclen);
0208     if (sg != pctx->src + 1)
0209         sg_chain(pctx->src, 2, sg);
0210 
0211     if (req->src != req->dst) {
0212         sg_init_table(pctx->dst, 3);
0213         sg_set_buf(pctx->dst, pctx->auth_tag, sizeof(pctx->auth_tag));
0214         sg = scatterwalk_ffwd(pctx->dst + 1, req->dst, req->assoclen);
0215         if (sg != pctx->dst + 1)
0216             sg_chain(pctx->dst, 2, sg);
0217     }
0218 }
0219 
0220 static void crypto_gcm_init_crypt(struct aead_request *req,
0221                   unsigned int cryptlen)
0222 {
0223     struct crypto_aead *aead = crypto_aead_reqtfm(req);
0224     struct crypto_gcm_ctx *ctx = crypto_aead_ctx(aead);
0225     struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
0226     struct skcipher_request *skreq = &pctx->u.skreq;
0227     struct scatterlist *dst;
0228 
0229     dst = req->src == req->dst ? pctx->src : pctx->dst;
0230 
0231     skcipher_request_set_tfm(skreq, ctx->ctr);
0232     skcipher_request_set_crypt(skreq, pctx->src, dst,
0233                      cryptlen + sizeof(pctx->auth_tag),
0234                      pctx->iv);
0235 }
0236 
0237 static inline unsigned int gcm_remain(unsigned int len)
0238 {
0239     len &= 0xfU;
0240     return len ? 16 - len : 0;
0241 }
0242 
0243 static void gcm_hash_len_done(struct crypto_async_request *areq, int err);
0244 
0245 static int gcm_hash_update(struct aead_request *req,
0246                crypto_completion_t compl,
0247                struct scatterlist *src,
0248                unsigned int len, u32 flags)
0249 {
0250     struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
0251     struct ahash_request *ahreq = &pctx->u.ahreq;
0252 
0253     ahash_request_set_callback(ahreq, flags, compl, req);
0254     ahash_request_set_crypt(ahreq, src, NULL, len);
0255 
0256     return crypto_ahash_update(ahreq);
0257 }
0258 
0259 static int gcm_hash_remain(struct aead_request *req,
0260                unsigned int remain,
0261                crypto_completion_t compl, u32 flags)
0262 {
0263     return gcm_hash_update(req, compl, &gcm_zeroes->sg, remain, flags);
0264 }
0265 
0266 static int gcm_hash_len(struct aead_request *req, u32 flags)
0267 {
0268     struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
0269     struct ahash_request *ahreq = &pctx->u.ahreq;
0270     struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
0271     u128 lengths;
0272 
0273     lengths.a = cpu_to_be64(req->assoclen * 8);
0274     lengths.b = cpu_to_be64(gctx->cryptlen * 8);
0275     memcpy(pctx->iauth_tag, &lengths, 16);
0276     sg_init_one(&pctx->sg, pctx->iauth_tag, 16);
0277     ahash_request_set_callback(ahreq, flags, gcm_hash_len_done, req);
0278     ahash_request_set_crypt(ahreq, &pctx->sg,
0279                 pctx->iauth_tag, sizeof(lengths));
0280 
0281     return crypto_ahash_finup(ahreq);
0282 }
0283 
0284 static int gcm_hash_len_continue(struct aead_request *req, u32 flags)
0285 {
0286     struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
0287     struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
0288 
0289     return gctx->complete(req, flags);
0290 }
0291 
0292 static void gcm_hash_len_done(struct crypto_async_request *areq, int err)
0293 {
0294     struct aead_request *req = areq->data;
0295 
0296     if (err)
0297         goto out;
0298 
0299     err = gcm_hash_len_continue(req, 0);
0300     if (err == -EINPROGRESS)
0301         return;
0302 
0303 out:
0304     aead_request_complete(req, err);
0305 }
0306 
0307 static int gcm_hash_crypt_remain_continue(struct aead_request *req, u32 flags)
0308 {
0309     return gcm_hash_len(req, flags) ?:
0310            gcm_hash_len_continue(req, flags);
0311 }
0312 
0313 static void gcm_hash_crypt_remain_done(struct crypto_async_request *areq,
0314                        int err)
0315 {
0316     struct aead_request *req = areq->data;
0317 
0318     if (err)
0319         goto out;
0320 
0321     err = gcm_hash_crypt_remain_continue(req, 0);
0322     if (err == -EINPROGRESS)
0323         return;
0324 
0325 out:
0326     aead_request_complete(req, err);
0327 }
0328 
0329 static int gcm_hash_crypt_continue(struct aead_request *req, u32 flags)
0330 {
0331     struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
0332     struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
0333     unsigned int remain;
0334 
0335     remain = gcm_remain(gctx->cryptlen);
0336     if (remain)
0337         return gcm_hash_remain(req, remain,
0338                        gcm_hash_crypt_remain_done, flags) ?:
0339                gcm_hash_crypt_remain_continue(req, flags);
0340 
0341     return gcm_hash_crypt_remain_continue(req, flags);
0342 }
0343 
0344 static void gcm_hash_crypt_done(struct crypto_async_request *areq, int err)
0345 {
0346     struct aead_request *req = areq->data;
0347 
0348     if (err)
0349         goto out;
0350 
0351     err = gcm_hash_crypt_continue(req, 0);
0352     if (err == -EINPROGRESS)
0353         return;
0354 
0355 out:
0356     aead_request_complete(req, err);
0357 }
0358 
0359 static int gcm_hash_assoc_remain_continue(struct aead_request *req, u32 flags)
0360 {
0361     struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
0362     struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
0363 
0364     if (gctx->cryptlen)
0365         return gcm_hash_update(req, gcm_hash_crypt_done,
0366                        gctx->src, gctx->cryptlen, flags) ?:
0367                gcm_hash_crypt_continue(req, flags);
0368 
0369     return gcm_hash_crypt_remain_continue(req, flags);
0370 }
0371 
0372 static void gcm_hash_assoc_remain_done(struct crypto_async_request *areq,
0373                        int err)
0374 {
0375     struct aead_request *req = areq->data;
0376 
0377     if (err)
0378         goto out;
0379 
0380     err = gcm_hash_assoc_remain_continue(req, 0);
0381     if (err == -EINPROGRESS)
0382         return;
0383 
0384 out:
0385     aead_request_complete(req, err);
0386 }
0387 
0388 static int gcm_hash_assoc_continue(struct aead_request *req, u32 flags)
0389 {
0390     unsigned int remain;
0391 
0392     remain = gcm_remain(req->assoclen);
0393     if (remain)
0394         return gcm_hash_remain(req, remain,
0395                        gcm_hash_assoc_remain_done, flags) ?:
0396                gcm_hash_assoc_remain_continue(req, flags);
0397 
0398     return gcm_hash_assoc_remain_continue(req, flags);
0399 }
0400 
0401 static void gcm_hash_assoc_done(struct crypto_async_request *areq, int err)
0402 {
0403     struct aead_request *req = areq->data;
0404 
0405     if (err)
0406         goto out;
0407 
0408     err = gcm_hash_assoc_continue(req, 0);
0409     if (err == -EINPROGRESS)
0410         return;
0411 
0412 out:
0413     aead_request_complete(req, err);
0414 }
0415 
0416 static int gcm_hash_init_continue(struct aead_request *req, u32 flags)
0417 {
0418     if (req->assoclen)
0419         return gcm_hash_update(req, gcm_hash_assoc_done,
0420                        req->src, req->assoclen, flags) ?:
0421                gcm_hash_assoc_continue(req, flags);
0422 
0423     return gcm_hash_assoc_remain_continue(req, flags);
0424 }
0425 
0426 static void gcm_hash_init_done(struct crypto_async_request *areq, int err)
0427 {
0428     struct aead_request *req = areq->data;
0429 
0430     if (err)
0431         goto out;
0432 
0433     err = gcm_hash_init_continue(req, 0);
0434     if (err == -EINPROGRESS)
0435         return;
0436 
0437 out:
0438     aead_request_complete(req, err);
0439 }
0440 
0441 static int gcm_hash(struct aead_request *req, u32 flags)
0442 {
0443     struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
0444     struct ahash_request *ahreq = &pctx->u.ahreq;
0445     struct crypto_gcm_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
0446 
0447     ahash_request_set_tfm(ahreq, ctx->ghash);
0448 
0449     ahash_request_set_callback(ahreq, flags, gcm_hash_init_done, req);
0450     return crypto_ahash_init(ahreq) ?:
0451            gcm_hash_init_continue(req, flags);
0452 }
0453 
0454 static int gcm_enc_copy_hash(struct aead_request *req, u32 flags)
0455 {
0456     struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
0457     struct crypto_aead *aead = crypto_aead_reqtfm(req);
0458     u8 *auth_tag = pctx->auth_tag;
0459 
0460     crypto_xor(auth_tag, pctx->iauth_tag, 16);
0461     scatterwalk_map_and_copy(auth_tag, req->dst,
0462                  req->assoclen + req->cryptlen,
0463                  crypto_aead_authsize(aead), 1);
0464     return 0;
0465 }
0466 
0467 static int gcm_encrypt_continue(struct aead_request *req, u32 flags)
0468 {
0469     struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
0470     struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
0471 
0472     gctx->src = sg_next(req->src == req->dst ? pctx->src : pctx->dst);
0473     gctx->cryptlen = req->cryptlen;
0474     gctx->complete = gcm_enc_copy_hash;
0475 
0476     return gcm_hash(req, flags);
0477 }
0478 
0479 static void gcm_encrypt_done(struct crypto_async_request *areq, int err)
0480 {
0481     struct aead_request *req = areq->data;
0482 
0483     if (err)
0484         goto out;
0485 
0486     err = gcm_encrypt_continue(req, 0);
0487     if (err == -EINPROGRESS)
0488         return;
0489 
0490 out:
0491     aead_request_complete(req, err);
0492 }
0493 
0494 static int crypto_gcm_encrypt(struct aead_request *req)
0495 {
0496     struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
0497     struct skcipher_request *skreq = &pctx->u.skreq;
0498     u32 flags = aead_request_flags(req);
0499 
0500     crypto_gcm_init_common(req);
0501     crypto_gcm_init_crypt(req, req->cryptlen);
0502     skcipher_request_set_callback(skreq, flags, gcm_encrypt_done, req);
0503 
0504     return crypto_skcipher_encrypt(skreq) ?:
0505            gcm_encrypt_continue(req, flags);
0506 }
0507 
0508 static int crypto_gcm_verify(struct aead_request *req)
0509 {
0510     struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
0511     struct crypto_aead *aead = crypto_aead_reqtfm(req);
0512     u8 *auth_tag = pctx->auth_tag;
0513     u8 *iauth_tag = pctx->iauth_tag;
0514     unsigned int authsize = crypto_aead_authsize(aead);
0515     unsigned int cryptlen = req->cryptlen - authsize;
0516 
0517     crypto_xor(auth_tag, iauth_tag, 16);
0518     scatterwalk_map_and_copy(iauth_tag, req->src,
0519                  req->assoclen + cryptlen, authsize, 0);
0520     return crypto_memneq(iauth_tag, auth_tag, authsize) ? -EBADMSG : 0;
0521 }
0522 
0523 static void gcm_decrypt_done(struct crypto_async_request *areq, int err)
0524 {
0525     struct aead_request *req = areq->data;
0526 
0527     if (!err)
0528         err = crypto_gcm_verify(req);
0529 
0530     aead_request_complete(req, err);
0531 }
0532 
0533 static int gcm_dec_hash_continue(struct aead_request *req, u32 flags)
0534 {
0535     struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
0536     struct skcipher_request *skreq = &pctx->u.skreq;
0537     struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
0538 
0539     crypto_gcm_init_crypt(req, gctx->cryptlen);
0540     skcipher_request_set_callback(skreq, flags, gcm_decrypt_done, req);
0541     return crypto_skcipher_decrypt(skreq) ?: crypto_gcm_verify(req);
0542 }
0543 
0544 static int crypto_gcm_decrypt(struct aead_request *req)
0545 {
0546     struct crypto_aead *aead = crypto_aead_reqtfm(req);
0547     struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
0548     struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
0549     unsigned int authsize = crypto_aead_authsize(aead);
0550     unsigned int cryptlen = req->cryptlen;
0551     u32 flags = aead_request_flags(req);
0552 
0553     cryptlen -= authsize;
0554 
0555     crypto_gcm_init_common(req);
0556 
0557     gctx->src = sg_next(pctx->src);
0558     gctx->cryptlen = cryptlen;
0559     gctx->complete = gcm_dec_hash_continue;
0560 
0561     return gcm_hash(req, flags);
0562 }
0563 
0564 static int crypto_gcm_init_tfm(struct crypto_aead *tfm)
0565 {
0566     struct aead_instance *inst = aead_alg_instance(tfm);
0567     struct gcm_instance_ctx *ictx = aead_instance_ctx(inst);
0568     struct crypto_gcm_ctx *ctx = crypto_aead_ctx(tfm);
0569     struct crypto_skcipher *ctr;
0570     struct crypto_ahash *ghash;
0571     unsigned long align;
0572     int err;
0573 
0574     ghash = crypto_spawn_ahash(&ictx->ghash);
0575     if (IS_ERR(ghash))
0576         return PTR_ERR(ghash);
0577 
0578     ctr = crypto_spawn_skcipher(&ictx->ctr);
0579     err = PTR_ERR(ctr);
0580     if (IS_ERR(ctr))
0581         goto err_free_hash;
0582 
0583     ctx->ctr = ctr;
0584     ctx->ghash = ghash;
0585 
0586     align = crypto_aead_alignmask(tfm);
0587     align &= ~(crypto_tfm_ctx_alignment() - 1);
0588     crypto_aead_set_reqsize(tfm,
0589         align + offsetof(struct crypto_gcm_req_priv_ctx, u) +
0590         max(sizeof(struct skcipher_request) +
0591             crypto_skcipher_reqsize(ctr),
0592             sizeof(struct ahash_request) +
0593             crypto_ahash_reqsize(ghash)));
0594 
0595     return 0;
0596 
0597 err_free_hash:
0598     crypto_free_ahash(ghash);
0599     return err;
0600 }
0601 
0602 static void crypto_gcm_exit_tfm(struct crypto_aead *tfm)
0603 {
0604     struct crypto_gcm_ctx *ctx = crypto_aead_ctx(tfm);
0605 
0606     crypto_free_ahash(ctx->ghash);
0607     crypto_free_skcipher(ctx->ctr);
0608 }
0609 
0610 static void crypto_gcm_free(struct aead_instance *inst)
0611 {
0612     struct gcm_instance_ctx *ctx = aead_instance_ctx(inst);
0613 
0614     crypto_drop_skcipher(&ctx->ctr);
0615     crypto_drop_ahash(&ctx->ghash);
0616     kfree(inst);
0617 }
0618 
0619 static int crypto_gcm_create_common(struct crypto_template *tmpl,
0620                     struct rtattr **tb,
0621                     const char *full_name,
0622                     const char *ctr_name,
0623                     const char *ghash_name)
0624 {
0625     struct crypto_attr_type *algt;
0626     struct aead_instance *inst;
0627     struct skcipher_alg *ctr;
0628     struct crypto_alg *ghash_alg;
0629     struct hash_alg_common *ghash;
0630     struct gcm_instance_ctx *ctx;
0631     int err;
0632 
0633     algt = crypto_get_attr_type(tb);
0634     if (IS_ERR(algt))
0635         return PTR_ERR(algt);
0636 
0637     if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
0638         return -EINVAL;
0639 
0640     ghash_alg = crypto_find_alg(ghash_name, &crypto_ahash_type,
0641                     CRYPTO_ALG_TYPE_HASH,
0642                     CRYPTO_ALG_TYPE_AHASH_MASK |
0643                     crypto_requires_sync(algt->type,
0644                              algt->mask));
0645     if (IS_ERR(ghash_alg))
0646         return PTR_ERR(ghash_alg);
0647 
0648     ghash = __crypto_hash_alg_common(ghash_alg);
0649 
0650     err = -ENOMEM;
0651     inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
0652     if (!inst)
0653         goto out_put_ghash;
0654 
0655     ctx = aead_instance_ctx(inst);
0656     err = crypto_init_ahash_spawn(&ctx->ghash, ghash,
0657                       aead_crypto_instance(inst));
0658     if (err)
0659         goto err_free_inst;
0660 
0661     err = -EINVAL;
0662     if (ghash->digestsize != 16)
0663         goto err_drop_ghash;
0664 
0665     crypto_set_skcipher_spawn(&ctx->ctr, aead_crypto_instance(inst));
0666     err = crypto_grab_skcipher(&ctx->ctr, ctr_name, 0,
0667                    crypto_requires_sync(algt->type,
0668                             algt->mask));
0669     if (err)
0670         goto err_drop_ghash;
0671 
0672     ctr = crypto_spawn_skcipher_alg(&ctx->ctr);
0673 
0674     /* We only support 16-byte blocks. */
0675     err = -EINVAL;
0676     if (crypto_skcipher_alg_ivsize(ctr) != 16)
0677         goto out_put_ctr;
0678 
0679     /* Not a stream cipher? */
0680     if (ctr->base.cra_blocksize != 1)
0681         goto out_put_ctr;
0682 
0683     err = -ENAMETOOLONG;
0684     if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
0685              "gcm_base(%s,%s)", ctr->base.cra_driver_name,
0686              ghash_alg->cra_driver_name) >=
0687         CRYPTO_MAX_ALG_NAME)
0688         goto out_put_ctr;
0689 
0690     memcpy(inst->alg.base.cra_name, full_name, CRYPTO_MAX_ALG_NAME);
0691 
0692     inst->alg.base.cra_flags = (ghash->base.cra_flags |
0693                     ctr->base.cra_flags) & CRYPTO_ALG_ASYNC;
0694     inst->alg.base.cra_priority = (ghash->base.cra_priority +
0695                        ctr->base.cra_priority) / 2;
0696     inst->alg.base.cra_blocksize = 1;
0697     inst->alg.base.cra_alignmask = ghash->base.cra_alignmask |
0698                        ctr->base.cra_alignmask;
0699     inst->alg.base.cra_ctxsize = sizeof(struct crypto_gcm_ctx);
0700     inst->alg.ivsize = 12;
0701     inst->alg.chunksize = crypto_skcipher_alg_chunksize(ctr);
0702     inst->alg.maxauthsize = 16;
0703     inst->alg.init = crypto_gcm_init_tfm;
0704     inst->alg.exit = crypto_gcm_exit_tfm;
0705     inst->alg.setkey = crypto_gcm_setkey;
0706     inst->alg.setauthsize = crypto_gcm_setauthsize;
0707     inst->alg.encrypt = crypto_gcm_encrypt;
0708     inst->alg.decrypt = crypto_gcm_decrypt;
0709 
0710     inst->free = crypto_gcm_free;
0711 
0712     err = aead_register_instance(tmpl, inst);
0713     if (err)
0714         goto out_put_ctr;
0715 
0716 out_put_ghash:
0717     crypto_mod_put(ghash_alg);
0718     return err;
0719 
0720 out_put_ctr:
0721     crypto_drop_skcipher(&ctx->ctr);
0722 err_drop_ghash:
0723     crypto_drop_ahash(&ctx->ghash);
0724 err_free_inst:
0725     kfree(inst);
0726     goto out_put_ghash;
0727 }
0728 
0729 static int crypto_gcm_create(struct crypto_template *tmpl, struct rtattr **tb)
0730 {
0731     const char *cipher_name;
0732     char ctr_name[CRYPTO_MAX_ALG_NAME];
0733     char full_name[CRYPTO_MAX_ALG_NAME];
0734 
0735     cipher_name = crypto_attr_alg_name(tb[1]);
0736     if (IS_ERR(cipher_name))
0737         return PTR_ERR(cipher_name);
0738 
0739     if (snprintf(ctr_name, CRYPTO_MAX_ALG_NAME, "ctr(%s)", cipher_name) >=
0740         CRYPTO_MAX_ALG_NAME)
0741         return -ENAMETOOLONG;
0742 
0743     if (snprintf(full_name, CRYPTO_MAX_ALG_NAME, "gcm(%s)", cipher_name) >=
0744         CRYPTO_MAX_ALG_NAME)
0745         return -ENAMETOOLONG;
0746 
0747     return crypto_gcm_create_common(tmpl, tb, full_name,
0748                     ctr_name, "ghash");
0749 }
0750 
0751 static struct crypto_template crypto_gcm_tmpl = {
0752     .name = "gcm",
0753     .create = crypto_gcm_create,
0754     .module = THIS_MODULE,
0755 };
0756 
0757 static int crypto_gcm_base_create(struct crypto_template *tmpl,
0758                   struct rtattr **tb)
0759 {
0760     const char *ctr_name;
0761     const char *ghash_name;
0762     char full_name[CRYPTO_MAX_ALG_NAME];
0763 
0764     ctr_name = crypto_attr_alg_name(tb[1]);
0765     if (IS_ERR(ctr_name))
0766         return PTR_ERR(ctr_name);
0767 
0768     ghash_name = crypto_attr_alg_name(tb[2]);
0769     if (IS_ERR(ghash_name))
0770         return PTR_ERR(ghash_name);
0771 
0772     if (snprintf(full_name, CRYPTO_MAX_ALG_NAME, "gcm_base(%s,%s)",
0773              ctr_name, ghash_name) >= CRYPTO_MAX_ALG_NAME)
0774         return -ENAMETOOLONG;
0775 
0776     return crypto_gcm_create_common(tmpl, tb, full_name,
0777                     ctr_name, ghash_name);
0778 }
0779 
0780 static struct crypto_template crypto_gcm_base_tmpl = {
0781     .name = "gcm_base",
0782     .create = crypto_gcm_base_create,
0783     .module = THIS_MODULE,
0784 };
0785 
0786 static int crypto_rfc4106_setkey(struct crypto_aead *parent, const u8 *key,
0787                  unsigned int keylen)
0788 {
0789     struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(parent);
0790     struct crypto_aead *child = ctx->child;
0791     int err;
0792 
0793     if (keylen < 4)
0794         return -EINVAL;
0795 
0796     keylen -= 4;
0797     memcpy(ctx->nonce, key + keylen, 4);
0798 
0799     crypto_aead_clear_flags(child, CRYPTO_TFM_REQ_MASK);
0800     crypto_aead_set_flags(child, crypto_aead_get_flags(parent) &
0801                      CRYPTO_TFM_REQ_MASK);
0802     err = crypto_aead_setkey(child, key, keylen);
0803     crypto_aead_set_flags(parent, crypto_aead_get_flags(child) &
0804                       CRYPTO_TFM_RES_MASK);
0805 
0806     return err;
0807 }
0808 
0809 static int crypto_rfc4106_setauthsize(struct crypto_aead *parent,
0810                       unsigned int authsize)
0811 {
0812     struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(parent);
0813 
0814     switch (authsize) {
0815     case 8:
0816     case 12:
0817     case 16:
0818         break;
0819     default:
0820         return -EINVAL;
0821     }
0822 
0823     return crypto_aead_setauthsize(ctx->child, authsize);
0824 }
0825 
0826 static struct aead_request *crypto_rfc4106_crypt(struct aead_request *req)
0827 {
0828     struct crypto_rfc4106_req_ctx *rctx = aead_request_ctx(req);
0829     struct crypto_aead *aead = crypto_aead_reqtfm(req);
0830     struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(aead);
0831     struct aead_request *subreq = &rctx->subreq;
0832     struct crypto_aead *child = ctx->child;
0833     struct scatterlist *sg;
0834     u8 *iv = PTR_ALIGN((u8 *)(subreq + 1) + crypto_aead_reqsize(child),
0835                crypto_aead_alignmask(child) + 1);
0836 
0837     scatterwalk_map_and_copy(iv + 12, req->src, 0, req->assoclen - 8, 0);
0838 
0839     memcpy(iv, ctx->nonce, 4);
0840     memcpy(iv + 4, req->iv, 8);
0841 
0842     sg_init_table(rctx->src, 3);
0843     sg_set_buf(rctx->src, iv + 12, req->assoclen - 8);
0844     sg = scatterwalk_ffwd(rctx->src + 1, req->src, req->assoclen);
0845     if (sg != rctx->src + 1)
0846         sg_chain(rctx->src, 2, sg);
0847 
0848     if (req->src != req->dst) {
0849         sg_init_table(rctx->dst, 3);
0850         sg_set_buf(rctx->dst, iv + 12, req->assoclen - 8);
0851         sg = scatterwalk_ffwd(rctx->dst + 1, req->dst, req->assoclen);
0852         if (sg != rctx->dst + 1)
0853             sg_chain(rctx->dst, 2, sg);
0854     }
0855 
0856     aead_request_set_tfm(subreq, child);
0857     aead_request_set_callback(subreq, req->base.flags, req->base.complete,
0858                   req->base.data);
0859     aead_request_set_crypt(subreq, rctx->src,
0860                    req->src == req->dst ? rctx->src : rctx->dst,
0861                    req->cryptlen, iv);
0862     aead_request_set_ad(subreq, req->assoclen - 8);
0863 
0864     return subreq;
0865 }
0866 
0867 static int crypto_rfc4106_encrypt(struct aead_request *req)
0868 {
0869     if (req->assoclen != 16 && req->assoclen != 20)
0870         return -EINVAL;
0871 
0872     req = crypto_rfc4106_crypt(req);
0873 
0874     return crypto_aead_encrypt(req);
0875 }
0876 
0877 static int crypto_rfc4106_decrypt(struct aead_request *req)
0878 {
0879     if (req->assoclen != 16 && req->assoclen != 20)
0880         return -EINVAL;
0881 
0882     req = crypto_rfc4106_crypt(req);
0883 
0884     return crypto_aead_decrypt(req);
0885 }
0886 
0887 static int crypto_rfc4106_init_tfm(struct crypto_aead *tfm)
0888 {
0889     struct aead_instance *inst = aead_alg_instance(tfm);
0890     struct crypto_aead_spawn *spawn = aead_instance_ctx(inst);
0891     struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(tfm);
0892     struct crypto_aead *aead;
0893     unsigned long align;
0894 
0895     aead = crypto_spawn_aead(spawn);
0896     if (IS_ERR(aead))
0897         return PTR_ERR(aead);
0898 
0899     ctx->child = aead;
0900 
0901     align = crypto_aead_alignmask(aead);
0902     align &= ~(crypto_tfm_ctx_alignment() - 1);
0903     crypto_aead_set_reqsize(
0904         tfm,
0905         sizeof(struct crypto_rfc4106_req_ctx) +
0906         ALIGN(crypto_aead_reqsize(aead), crypto_tfm_ctx_alignment()) +
0907         align + 24);
0908 
0909     return 0;
0910 }
0911 
0912 static void crypto_rfc4106_exit_tfm(struct crypto_aead *tfm)
0913 {
0914     struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(tfm);
0915 
0916     crypto_free_aead(ctx->child);
0917 }
0918 
0919 static void crypto_rfc4106_free(struct aead_instance *inst)
0920 {
0921     crypto_drop_aead(aead_instance_ctx(inst));
0922     kfree(inst);
0923 }
0924 
0925 static int crypto_rfc4106_create(struct crypto_template *tmpl,
0926                  struct rtattr **tb)
0927 {
0928     struct crypto_attr_type *algt;
0929     struct aead_instance *inst;
0930     struct crypto_aead_spawn *spawn;
0931     struct aead_alg *alg;
0932     const char *ccm_name;
0933     int err;
0934 
0935     algt = crypto_get_attr_type(tb);
0936     if (IS_ERR(algt))
0937         return PTR_ERR(algt);
0938 
0939     if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
0940         return -EINVAL;
0941 
0942     ccm_name = crypto_attr_alg_name(tb[1]);
0943     if (IS_ERR(ccm_name))
0944         return PTR_ERR(ccm_name);
0945 
0946     inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
0947     if (!inst)
0948         return -ENOMEM;
0949 
0950     spawn = aead_instance_ctx(inst);
0951     crypto_set_aead_spawn(spawn, aead_crypto_instance(inst));
0952     err = crypto_grab_aead(spawn, ccm_name, 0,
0953                    crypto_requires_sync(algt->type, algt->mask));
0954     if (err)
0955         goto out_free_inst;
0956 
0957     alg = crypto_spawn_aead_alg(spawn);
0958 
0959     err = -EINVAL;
0960 
0961     /* Underlying IV size must be 12. */
0962     if (crypto_aead_alg_ivsize(alg) != 12)
0963         goto out_drop_alg;
0964 
0965     /* Not a stream cipher? */
0966     if (alg->base.cra_blocksize != 1)
0967         goto out_drop_alg;
0968 
0969     err = -ENAMETOOLONG;
0970     if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
0971              "rfc4106(%s)", alg->base.cra_name) >=
0972         CRYPTO_MAX_ALG_NAME ||
0973         snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
0974              "rfc4106(%s)", alg->base.cra_driver_name) >=
0975         CRYPTO_MAX_ALG_NAME)
0976         goto out_drop_alg;
0977 
0978     inst->alg.base.cra_flags = alg->base.cra_flags & CRYPTO_ALG_ASYNC;
0979     inst->alg.base.cra_priority = alg->base.cra_priority;
0980     inst->alg.base.cra_blocksize = 1;
0981     inst->alg.base.cra_alignmask = alg->base.cra_alignmask;
0982 
0983     inst->alg.base.cra_ctxsize = sizeof(struct crypto_rfc4106_ctx);
0984 
0985     inst->alg.ivsize = 8;
0986     inst->alg.chunksize = crypto_aead_alg_chunksize(alg);
0987     inst->alg.maxauthsize = crypto_aead_alg_maxauthsize(alg);
0988 
0989     inst->alg.init = crypto_rfc4106_init_tfm;
0990     inst->alg.exit = crypto_rfc4106_exit_tfm;
0991 
0992     inst->alg.setkey = crypto_rfc4106_setkey;
0993     inst->alg.setauthsize = crypto_rfc4106_setauthsize;
0994     inst->alg.encrypt = crypto_rfc4106_encrypt;
0995     inst->alg.decrypt = crypto_rfc4106_decrypt;
0996 
0997     inst->free = crypto_rfc4106_free;
0998 
0999     err = aead_register_instance(tmpl, inst);
1000     if (err)
1001         goto out_drop_alg;
1002 
1003 out:
1004     return err;
1005 
1006 out_drop_alg:
1007     crypto_drop_aead(spawn);
1008 out_free_inst:
1009     kfree(inst);
1010     goto out;
1011 }
1012 
1013 static struct crypto_template crypto_rfc4106_tmpl = {
1014     .name = "rfc4106",
1015     .create = crypto_rfc4106_create,
1016     .module = THIS_MODULE,
1017 };
1018 
1019 static int crypto_rfc4543_setkey(struct crypto_aead *parent, const u8 *key,
1020                  unsigned int keylen)
1021 {
1022     struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(parent);
1023     struct crypto_aead *child = ctx->child;
1024     int err;
1025 
1026     if (keylen < 4)
1027         return -EINVAL;
1028 
1029     keylen -= 4;
1030     memcpy(ctx->nonce, key + keylen, 4);
1031 
1032     crypto_aead_clear_flags(child, CRYPTO_TFM_REQ_MASK);
1033     crypto_aead_set_flags(child, crypto_aead_get_flags(parent) &
1034                      CRYPTO_TFM_REQ_MASK);
1035     err = crypto_aead_setkey(child, key, keylen);
1036     crypto_aead_set_flags(parent, crypto_aead_get_flags(child) &
1037                       CRYPTO_TFM_RES_MASK);
1038 
1039     return err;
1040 }
1041 
1042 static int crypto_rfc4543_setauthsize(struct crypto_aead *parent,
1043                       unsigned int authsize)
1044 {
1045     struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(parent);
1046 
1047     if (authsize != 16)
1048         return -EINVAL;
1049 
1050     return crypto_aead_setauthsize(ctx->child, authsize);
1051 }
1052 
1053 static int crypto_rfc4543_crypt(struct aead_request *req, bool enc)
1054 {
1055     struct crypto_aead *aead = crypto_aead_reqtfm(req);
1056     struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(aead);
1057     struct crypto_rfc4543_req_ctx *rctx = aead_request_ctx(req);
1058     struct aead_request *subreq = &rctx->subreq;
1059     unsigned int authsize = crypto_aead_authsize(aead);
1060     u8 *iv = PTR_ALIGN((u8 *)(rctx + 1) + crypto_aead_reqsize(ctx->child),
1061                crypto_aead_alignmask(ctx->child) + 1);
1062     int err;
1063 
1064     if (req->src != req->dst) {
1065         err = crypto_rfc4543_copy_src_to_dst(req, enc);
1066         if (err)
1067             return err;
1068     }
1069 
1070     memcpy(iv, ctx->nonce, 4);
1071     memcpy(iv + 4, req->iv, 8);
1072 
1073     aead_request_set_tfm(subreq, ctx->child);
1074     aead_request_set_callback(subreq, req->base.flags,
1075                   req->base.complete, req->base.data);
1076     aead_request_set_crypt(subreq, req->src, req->dst,
1077                    enc ? 0 : authsize, iv);
1078     aead_request_set_ad(subreq, req->assoclen + req->cryptlen -
1079                     subreq->cryptlen);
1080 
1081     return enc ? crypto_aead_encrypt(subreq) : crypto_aead_decrypt(subreq);
1082 }
1083 
1084 static int crypto_rfc4543_copy_src_to_dst(struct aead_request *req, bool enc)
1085 {
1086     struct crypto_aead *aead = crypto_aead_reqtfm(req);
1087     struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(aead);
1088     unsigned int authsize = crypto_aead_authsize(aead);
1089     unsigned int nbytes = req->assoclen + req->cryptlen -
1090                   (enc ? 0 : authsize);
1091     SKCIPHER_REQUEST_ON_STACK(nreq, ctx->null);
1092 
1093     skcipher_request_set_tfm(nreq, ctx->null);
1094     skcipher_request_set_callback(nreq, req->base.flags, NULL, NULL);
1095     skcipher_request_set_crypt(nreq, req->src, req->dst, nbytes, NULL);
1096 
1097     return crypto_skcipher_encrypt(nreq);
1098 }
1099 
1100 static int crypto_rfc4543_encrypt(struct aead_request *req)
1101 {
1102     return crypto_rfc4543_crypt(req, true);
1103 }
1104 
1105 static int crypto_rfc4543_decrypt(struct aead_request *req)
1106 {
1107     return crypto_rfc4543_crypt(req, false);
1108 }
1109 
1110 static int crypto_rfc4543_init_tfm(struct crypto_aead *tfm)
1111 {
1112     struct aead_instance *inst = aead_alg_instance(tfm);
1113     struct crypto_rfc4543_instance_ctx *ictx = aead_instance_ctx(inst);
1114     struct crypto_aead_spawn *spawn = &ictx->aead;
1115     struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(tfm);
1116     struct crypto_aead *aead;
1117     struct crypto_skcipher *null;
1118     unsigned long align;
1119     int err = 0;
1120 
1121     aead = crypto_spawn_aead(spawn);
1122     if (IS_ERR(aead))
1123         return PTR_ERR(aead);
1124 
1125     null = crypto_get_default_null_skcipher2();
1126     err = PTR_ERR(null);
1127     if (IS_ERR(null))
1128         goto err_free_aead;
1129 
1130     ctx->child = aead;
1131     ctx->null = null;
1132 
1133     align = crypto_aead_alignmask(aead);
1134     align &= ~(crypto_tfm_ctx_alignment() - 1);
1135     crypto_aead_set_reqsize(
1136         tfm,
1137         sizeof(struct crypto_rfc4543_req_ctx) +
1138         ALIGN(crypto_aead_reqsize(aead), crypto_tfm_ctx_alignment()) +
1139         align + 12);
1140 
1141     return 0;
1142 
1143 err_free_aead:
1144     crypto_free_aead(aead);
1145     return err;
1146 }
1147 
1148 static void crypto_rfc4543_exit_tfm(struct crypto_aead *tfm)
1149 {
1150     struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(tfm);
1151 
1152     crypto_free_aead(ctx->child);
1153     crypto_put_default_null_skcipher2();
1154 }
1155 
1156 static void crypto_rfc4543_free(struct aead_instance *inst)
1157 {
1158     struct crypto_rfc4543_instance_ctx *ctx = aead_instance_ctx(inst);
1159 
1160     crypto_drop_aead(&ctx->aead);
1161 
1162     kfree(inst);
1163 }
1164 
1165 static int crypto_rfc4543_create(struct crypto_template *tmpl,
1166                 struct rtattr **tb)
1167 {
1168     struct crypto_attr_type *algt;
1169     struct aead_instance *inst;
1170     struct crypto_aead_spawn *spawn;
1171     struct aead_alg *alg;
1172     struct crypto_rfc4543_instance_ctx *ctx;
1173     const char *ccm_name;
1174     int err;
1175 
1176     algt = crypto_get_attr_type(tb);
1177     if (IS_ERR(algt))
1178         return PTR_ERR(algt);
1179 
1180     if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
1181         return -EINVAL;
1182 
1183     ccm_name = crypto_attr_alg_name(tb[1]);
1184     if (IS_ERR(ccm_name))
1185         return PTR_ERR(ccm_name);
1186 
1187     inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
1188     if (!inst)
1189         return -ENOMEM;
1190 
1191     ctx = aead_instance_ctx(inst);
1192     spawn = &ctx->aead;
1193     crypto_set_aead_spawn(spawn, aead_crypto_instance(inst));
1194     err = crypto_grab_aead(spawn, ccm_name, 0,
1195                    crypto_requires_sync(algt->type, algt->mask));
1196     if (err)
1197         goto out_free_inst;
1198 
1199     alg = crypto_spawn_aead_alg(spawn);
1200 
1201     err = -EINVAL;
1202 
1203     /* Underlying IV size must be 12. */
1204     if (crypto_aead_alg_ivsize(alg) != 12)
1205         goto out_drop_alg;
1206 
1207     /* Not a stream cipher? */
1208     if (alg->base.cra_blocksize != 1)
1209         goto out_drop_alg;
1210 
1211     err = -ENAMETOOLONG;
1212     if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
1213              "rfc4543(%s)", alg->base.cra_name) >=
1214         CRYPTO_MAX_ALG_NAME ||
1215         snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
1216              "rfc4543(%s)", alg->base.cra_driver_name) >=
1217         CRYPTO_MAX_ALG_NAME)
1218         goto out_drop_alg;
1219 
1220     inst->alg.base.cra_flags = alg->base.cra_flags & CRYPTO_ALG_ASYNC;
1221     inst->alg.base.cra_priority = alg->base.cra_priority;
1222     inst->alg.base.cra_blocksize = 1;
1223     inst->alg.base.cra_alignmask = alg->base.cra_alignmask;
1224 
1225     inst->alg.base.cra_ctxsize = sizeof(struct crypto_rfc4543_ctx);
1226 
1227     inst->alg.ivsize = 8;
1228     inst->alg.chunksize = crypto_aead_alg_chunksize(alg);
1229     inst->alg.maxauthsize = crypto_aead_alg_maxauthsize(alg);
1230 
1231     inst->alg.init = crypto_rfc4543_init_tfm;
1232     inst->alg.exit = crypto_rfc4543_exit_tfm;
1233 
1234     inst->alg.setkey = crypto_rfc4543_setkey;
1235     inst->alg.setauthsize = crypto_rfc4543_setauthsize;
1236     inst->alg.encrypt = crypto_rfc4543_encrypt;
1237     inst->alg.decrypt = crypto_rfc4543_decrypt;
1238 
1239     inst->free = crypto_rfc4543_free,
1240 
1241     err = aead_register_instance(tmpl, inst);
1242     if (err)
1243         goto out_drop_alg;
1244 
1245 out:
1246     return err;
1247 
1248 out_drop_alg:
1249     crypto_drop_aead(spawn);
1250 out_free_inst:
1251     kfree(inst);
1252     goto out;
1253 }
1254 
1255 static struct crypto_template crypto_rfc4543_tmpl = {
1256     .name = "rfc4543",
1257     .create = crypto_rfc4543_create,
1258     .module = THIS_MODULE,
1259 };
1260 
1261 static int __init crypto_gcm_module_init(void)
1262 {
1263     int err;
1264 
1265     gcm_zeroes = kzalloc(sizeof(*gcm_zeroes), GFP_KERNEL);
1266     if (!gcm_zeroes)
1267         return -ENOMEM;
1268 
1269     sg_init_one(&gcm_zeroes->sg, gcm_zeroes->buf, sizeof(gcm_zeroes->buf));
1270 
1271     err = crypto_register_template(&crypto_gcm_base_tmpl);
1272     if (err)
1273         goto out;
1274 
1275     err = crypto_register_template(&crypto_gcm_tmpl);
1276     if (err)
1277         goto out_undo_base;
1278 
1279     err = crypto_register_template(&crypto_rfc4106_tmpl);
1280     if (err)
1281         goto out_undo_gcm;
1282 
1283     err = crypto_register_template(&crypto_rfc4543_tmpl);
1284     if (err)
1285         goto out_undo_rfc4106;
1286 
1287     return 0;
1288 
1289 out_undo_rfc4106:
1290     crypto_unregister_template(&crypto_rfc4106_tmpl);
1291 out_undo_gcm:
1292     crypto_unregister_template(&crypto_gcm_tmpl);
1293 out_undo_base:
1294     crypto_unregister_template(&crypto_gcm_base_tmpl);
1295 out:
1296     kfree(gcm_zeroes);
1297     return err;
1298 }
1299 
1300 static void __exit crypto_gcm_module_exit(void)
1301 {
1302     kfree(gcm_zeroes);
1303     crypto_unregister_template(&crypto_rfc4543_tmpl);
1304     crypto_unregister_template(&crypto_rfc4106_tmpl);
1305     crypto_unregister_template(&crypto_gcm_tmpl);
1306     crypto_unregister_template(&crypto_gcm_base_tmpl);
1307 }
1308 
1309 module_init(crypto_gcm_module_init);
1310 module_exit(crypto_gcm_module_exit);
1311 
1312 MODULE_LICENSE("GPL");
1313 MODULE_DESCRIPTION("Galois/Counter Mode");
1314 MODULE_AUTHOR("Mikko Herranen <mh1@iki.fi>");
1315 MODULE_ALIAS_CRYPTO("gcm_base");
1316 MODULE_ALIAS_CRYPTO("rfc4106");
1317 MODULE_ALIAS_CRYPTO("rfc4543");
1318 MODULE_ALIAS_CRYPTO("gcm");