Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 1 | /* |
| 2 | * GCM: Galois/Counter Mode. |
| 3 | * |
| 4 | * Copyright (c) 2007 Nokia Siemens Networks - Mikko Herranen <mh1@iki.fi> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU General Public License version 2 as published |
| 8 | * by the Free Software Foundation. |
| 9 | */ |
| 10 | |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 11 | #include <crypto/gf128mul.h> |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 12 | #include <crypto/internal/aead.h> |
Herbert Xu | 1472e5e | 2007-12-07 19:26:11 +0800 | [diff] [blame] | 13 | #include <crypto/internal/skcipher.h> |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 14 | #include <crypto/internal/hash.h> |
Herbert Xu | 17db854 | 2015-05-21 15:11:11 +0800 | [diff] [blame] | 15 | #include <crypto/null.h> |
Herbert Xu | 42c271c | 2007-12-07 18:52:49 +0800 | [diff] [blame] | 16 | #include <crypto/scatterwalk.h> |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 17 | #include <crypto/hash.h> |
| 18 | #include "internal.h" |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 19 | #include <linux/completion.h> |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 20 | #include <linux/err.h> |
| 21 | #include <linux/init.h> |
| 22 | #include <linux/kernel.h> |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/slab.h> |
| 25 | |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 26 | struct gcm_instance_ctx { |
Herbert Xu | 1472e5e | 2007-12-07 19:26:11 +0800 | [diff] [blame] | 27 | struct crypto_skcipher_spawn ctr; |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 28 | struct crypto_ahash_spawn ghash; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 29 | }; |
| 30 | |
| 31 | struct crypto_gcm_ctx { |
| 32 | struct crypto_ablkcipher *ctr; |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 33 | struct crypto_ahash *ghash; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 34 | }; |
| 35 | |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 36 | struct crypto_rfc4106_ctx { |
| 37 | struct crypto_aead *child; |
| 38 | u8 nonce[4]; |
| 39 | }; |
| 40 | |
Jussi Kivilinna | 9489667d | 2013-04-07 16:43:41 +0300 | [diff] [blame] | 41 | struct crypto_rfc4543_instance_ctx { |
| 42 | struct crypto_aead_spawn aead; |
Jussi Kivilinna | 9489667d | 2013-04-07 16:43:41 +0300 | [diff] [blame] | 43 | }; |
| 44 | |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 45 | struct crypto_rfc4543_ctx { |
| 46 | struct crypto_aead *child; |
Jussi Kivilinna | 9489667d | 2013-04-07 16:43:41 +0300 | [diff] [blame] | 47 | struct crypto_blkcipher *null; |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 48 | u8 nonce[4]; |
| 49 | }; |
| 50 | |
| 51 | struct crypto_rfc4543_req_ctx { |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 52 | struct aead_request subreq; |
| 53 | }; |
| 54 | |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 55 | struct crypto_gcm_ghash_ctx { |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 56 | unsigned int cryptlen; |
| 57 | struct scatterlist *src; |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 58 | int (*complete)(struct aead_request *req, u32 flags); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | struct crypto_gcm_req_priv_ctx { |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 62 | u8 iv[16]; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 63 | u8 auth_tag[16]; |
Herbert Xu | 6160b28 | 2007-12-04 19:17:50 +1100 | [diff] [blame] | 64 | u8 iauth_tag[16]; |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 65 | struct scatterlist src[3]; |
| 66 | struct scatterlist dst[3]; |
| 67 | struct scatterlist sg; |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 68 | struct crypto_gcm_ghash_ctx ghash_ctx; |
| 69 | union { |
| 70 | struct ahash_request ahreq; |
| 71 | struct ablkcipher_request abreq; |
| 72 | } u; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 73 | }; |
| 74 | |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 75 | struct crypto_gcm_setkey_result { |
| 76 | int err; |
| 77 | struct completion completion; |
| 78 | }; |
| 79 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 80 | static struct { |
| 81 | u8 buf[16]; |
| 82 | struct scatterlist sg; |
| 83 | } *gcm_zeroes; |
| 84 | |
| 85 | static int crypto_rfc4543_copy_src_to_dst(struct aead_request *req, bool enc); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 86 | |
Herbert Xu | 2589469 | 2007-12-12 19:16:38 +0800 | [diff] [blame] | 87 | static inline struct crypto_gcm_req_priv_ctx *crypto_gcm_reqctx( |
| 88 | struct aead_request *req) |
| 89 | { |
| 90 | unsigned long align = crypto_aead_alignmask(crypto_aead_reqtfm(req)); |
| 91 | |
| 92 | return (void *)PTR_ALIGN((u8 *)aead_request_ctx(req), align + 1); |
| 93 | } |
| 94 | |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 95 | static void crypto_gcm_setkey_done(struct crypto_async_request *req, int err) |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 96 | { |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 97 | struct crypto_gcm_setkey_result *result = req->data; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 98 | |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 99 | if (err == -EINPROGRESS) |
| 100 | return; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 101 | |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 102 | result->err = err; |
| 103 | complete(&result->completion); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | static int crypto_gcm_setkey(struct crypto_aead *aead, const u8 *key, |
| 107 | unsigned int keylen) |
| 108 | { |
| 109 | struct crypto_gcm_ctx *ctx = crypto_aead_ctx(aead); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 110 | struct crypto_ahash *ghash = ctx->ghash; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 111 | struct crypto_ablkcipher *ctr = ctx->ctr; |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 112 | struct { |
| 113 | be128 hash; |
| 114 | u8 iv[8]; |
| 115 | |
| 116 | struct crypto_gcm_setkey_result result; |
| 117 | |
| 118 | struct scatterlist sg[1]; |
| 119 | struct ablkcipher_request req; |
| 120 | } *data; |
| 121 | int err; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 122 | |
| 123 | crypto_ablkcipher_clear_flags(ctr, CRYPTO_TFM_REQ_MASK); |
| 124 | crypto_ablkcipher_set_flags(ctr, crypto_aead_get_flags(aead) & |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 125 | CRYPTO_TFM_REQ_MASK); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 126 | err = crypto_ablkcipher_setkey(ctr, key, keylen); |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 127 | crypto_aead_set_flags(aead, crypto_ablkcipher_get_flags(ctr) & |
| 128 | CRYPTO_TFM_RES_MASK); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 129 | if (err) |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 130 | return err; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 131 | |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 132 | data = kzalloc(sizeof(*data) + crypto_ablkcipher_reqsize(ctr), |
| 133 | GFP_KERNEL); |
| 134 | if (!data) |
| 135 | return -ENOMEM; |
| 136 | |
| 137 | init_completion(&data->result.completion); |
| 138 | sg_init_one(data->sg, &data->hash, sizeof(data->hash)); |
| 139 | ablkcipher_request_set_tfm(&data->req, ctr); |
| 140 | ablkcipher_request_set_callback(&data->req, CRYPTO_TFM_REQ_MAY_SLEEP | |
| 141 | CRYPTO_TFM_REQ_MAY_BACKLOG, |
| 142 | crypto_gcm_setkey_done, |
| 143 | &data->result); |
| 144 | ablkcipher_request_set_crypt(&data->req, data->sg, data->sg, |
| 145 | sizeof(data->hash), data->iv); |
| 146 | |
| 147 | err = crypto_ablkcipher_encrypt(&data->req); |
| 148 | if (err == -EINPROGRESS || err == -EBUSY) { |
| 149 | err = wait_for_completion_interruptible( |
| 150 | &data->result.completion); |
| 151 | if (!err) |
| 152 | err = data->result.err; |
| 153 | } |
| 154 | |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 155 | if (err) |
| 156 | goto out; |
| 157 | |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 158 | crypto_ahash_clear_flags(ghash, CRYPTO_TFM_REQ_MASK); |
| 159 | crypto_ahash_set_flags(ghash, crypto_aead_get_flags(aead) & |
| 160 | CRYPTO_TFM_REQ_MASK); |
| 161 | err = crypto_ahash_setkey(ghash, (u8 *)&data->hash, sizeof(be128)); |
| 162 | crypto_aead_set_flags(aead, crypto_ahash_get_flags(ghash) & |
| 163 | CRYPTO_TFM_RES_MASK); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 164 | |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 165 | out: |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 166 | kzfree(data); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 167 | return err; |
| 168 | } |
| 169 | |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 170 | static int crypto_gcm_setauthsize(struct crypto_aead *tfm, |
| 171 | unsigned int authsize) |
| 172 | { |
| 173 | switch (authsize) { |
| 174 | case 4: |
| 175 | case 8: |
| 176 | case 12: |
| 177 | case 13: |
| 178 | case 14: |
| 179 | case 15: |
| 180 | case 16: |
| 181 | break; |
| 182 | default: |
| 183 | return -EINVAL; |
| 184 | } |
| 185 | |
| 186 | return 0; |
| 187 | } |
| 188 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 189 | static void crypto_gcm_init_common(struct aead_request *req) |
| 190 | { |
| 191 | struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req); |
| 192 | __be32 counter = cpu_to_be32(1); |
| 193 | struct scatterlist *sg; |
| 194 | |
| 195 | memset(pctx->auth_tag, 0, sizeof(pctx->auth_tag)); |
| 196 | memcpy(pctx->iv, req->iv, 12); |
| 197 | memcpy(pctx->iv + 12, &counter, 4); |
| 198 | |
| 199 | sg_init_table(pctx->src, 3); |
| 200 | sg_set_buf(pctx->src, pctx->auth_tag, sizeof(pctx->auth_tag)); |
| 201 | sg = scatterwalk_ffwd(pctx->src + 1, req->src, req->assoclen); |
| 202 | if (sg != pctx->src + 1) |
Dan Williams | c56f6d1 | 2015-08-07 18:15:13 +0200 | [diff] [blame^] | 203 | sg_chain(pctx->src, 2, sg); |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 204 | |
| 205 | if (req->src != req->dst) { |
| 206 | sg_init_table(pctx->dst, 3); |
| 207 | sg_set_buf(pctx->dst, pctx->auth_tag, sizeof(pctx->auth_tag)); |
| 208 | sg = scatterwalk_ffwd(pctx->dst + 1, req->dst, req->assoclen); |
| 209 | if (sg != pctx->dst + 1) |
Dan Williams | c56f6d1 | 2015-08-07 18:15:13 +0200 | [diff] [blame^] | 210 | sg_chain(pctx->dst, 2, sg); |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | |
| 214 | static void crypto_gcm_init_crypt(struct aead_request *req, |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 215 | unsigned int cryptlen) |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 216 | { |
| 217 | struct crypto_aead *aead = crypto_aead_reqtfm(req); |
| 218 | struct crypto_gcm_ctx *ctx = crypto_aead_ctx(aead); |
Herbert Xu | 2589469 | 2007-12-12 19:16:38 +0800 | [diff] [blame] | 219 | struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req); |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 220 | struct ablkcipher_request *ablk_req = &pctx->u.abreq; |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 221 | struct scatterlist *dst; |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 222 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 223 | dst = req->src == req->dst ? pctx->src : pctx->dst; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 224 | |
| 225 | ablkcipher_request_set_tfm(ablk_req, ctx->ctr); |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 226 | ablkcipher_request_set_crypt(ablk_req, pctx->src, dst, |
| 227 | cryptlen + sizeof(pctx->auth_tag), |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 228 | pctx->iv); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 229 | } |
| 230 | |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 231 | static inline unsigned int gcm_remain(unsigned int len) |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 232 | { |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 233 | len &= 0xfU; |
| 234 | return len ? 16 - len : 0; |
| 235 | } |
| 236 | |
| 237 | static void gcm_hash_len_done(struct crypto_async_request *areq, int err); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 238 | |
| 239 | static int gcm_hash_update(struct aead_request *req, |
Mark Rustad | 3e3dc25 | 2014-07-25 02:53:38 -0700 | [diff] [blame] | 240 | crypto_completion_t compl, |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 241 | struct scatterlist *src, |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 242 | unsigned int len, u32 flags) |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 243 | { |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 244 | struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 245 | struct ahash_request *ahreq = &pctx->u.ahreq; |
| 246 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 247 | ahash_request_set_callback(ahreq, flags, compl, req); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 248 | ahash_request_set_crypt(ahreq, src, NULL, len); |
| 249 | |
| 250 | return crypto_ahash_update(ahreq); |
| 251 | } |
| 252 | |
| 253 | static int gcm_hash_remain(struct aead_request *req, |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 254 | unsigned int remain, |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 255 | crypto_completion_t compl, u32 flags) |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 256 | { |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 257 | return gcm_hash_update(req, compl, &gcm_zeroes->sg, remain, flags); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 258 | } |
| 259 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 260 | static int gcm_hash_len(struct aead_request *req, u32 flags) |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 261 | { |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 262 | struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 263 | struct ahash_request *ahreq = &pctx->u.ahreq; |
| 264 | struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx; |
| 265 | u128 lengths; |
| 266 | |
| 267 | lengths.a = cpu_to_be64(req->assoclen * 8); |
| 268 | lengths.b = cpu_to_be64(gctx->cryptlen * 8); |
| 269 | memcpy(pctx->iauth_tag, &lengths, 16); |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 270 | sg_init_one(&pctx->sg, pctx->iauth_tag, 16); |
| 271 | ahash_request_set_callback(ahreq, flags, gcm_hash_len_done, req); |
| 272 | ahash_request_set_crypt(ahreq, &pctx->sg, |
| 273 | pctx->iauth_tag, sizeof(lengths)); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 274 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 275 | return crypto_ahash_finup(ahreq); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 276 | } |
| 277 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 278 | static int gcm_hash_len_continue(struct aead_request *req, u32 flags) |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 279 | { |
Herbert Xu | 2589469 | 2007-12-12 19:16:38 +0800 | [diff] [blame] | 280 | struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 281 | struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 282 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 283 | return gctx->complete(req, flags); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 284 | } |
| 285 | |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 286 | static void gcm_hash_len_done(struct crypto_async_request *areq, int err) |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 287 | { |
| 288 | struct aead_request *req = areq->data; |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 289 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 290 | if (err) |
| 291 | goto out; |
| 292 | |
| 293 | err = gcm_hash_len_continue(req, 0); |
| 294 | if (err == -EINPROGRESS) |
| 295 | return; |
| 296 | |
| 297 | out: |
| 298 | aead_request_complete(req, err); |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 299 | } |
| 300 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 301 | static int gcm_hash_crypt_remain_continue(struct aead_request *req, u32 flags) |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 302 | { |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 303 | return gcm_hash_len(req, flags) ?: |
| 304 | gcm_hash_len_continue(req, flags); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 305 | } |
| 306 | |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 307 | static void gcm_hash_crypt_remain_done(struct crypto_async_request *areq, |
| 308 | int err) |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 309 | { |
| 310 | struct aead_request *req = areq->data; |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 311 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 312 | if (err) |
| 313 | goto out; |
| 314 | |
| 315 | err = gcm_hash_crypt_remain_continue(req, 0); |
| 316 | if (err == -EINPROGRESS) |
| 317 | return; |
| 318 | |
| 319 | out: |
| 320 | aead_request_complete(req, err); |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 321 | } |
| 322 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 323 | static int gcm_hash_crypt_continue(struct aead_request *req, u32 flags) |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 324 | { |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 325 | struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req); |
| 326 | struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx; |
| 327 | unsigned int remain; |
| 328 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 329 | remain = gcm_remain(gctx->cryptlen); |
| 330 | if (remain) |
| 331 | return gcm_hash_remain(req, remain, |
| 332 | gcm_hash_crypt_remain_done, flags) ?: |
| 333 | gcm_hash_crypt_remain_continue(req, flags); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 334 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 335 | return gcm_hash_crypt_remain_continue(req, flags); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 336 | } |
| 337 | |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 338 | static void gcm_hash_crypt_done(struct crypto_async_request *areq, int err) |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 339 | { |
| 340 | struct aead_request *req = areq->data; |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 341 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 342 | if (err) |
| 343 | goto out; |
| 344 | |
| 345 | err = gcm_hash_crypt_continue(req, 0); |
| 346 | if (err == -EINPROGRESS) |
| 347 | return; |
| 348 | |
| 349 | out: |
| 350 | aead_request_complete(req, err); |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 351 | } |
| 352 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 353 | static int gcm_hash_assoc_remain_continue(struct aead_request *req, u32 flags) |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 354 | { |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 355 | struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req); |
| 356 | struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx; |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 357 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 358 | if (gctx->cryptlen) |
| 359 | return gcm_hash_update(req, gcm_hash_crypt_done, |
| 360 | gctx->src, gctx->cryptlen, flags) ?: |
| 361 | gcm_hash_crypt_continue(req, flags); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 362 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 363 | return gcm_hash_crypt_remain_continue(req, flags); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 364 | } |
| 365 | |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 366 | static void gcm_hash_assoc_remain_done(struct crypto_async_request *areq, |
| 367 | int err) |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 368 | { |
| 369 | struct aead_request *req = areq->data; |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 370 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 371 | if (err) |
| 372 | goto out; |
| 373 | |
| 374 | err = gcm_hash_assoc_remain_continue(req, 0); |
| 375 | if (err == -EINPROGRESS) |
| 376 | return; |
| 377 | |
| 378 | out: |
| 379 | aead_request_complete(req, err); |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 380 | } |
| 381 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 382 | static int gcm_hash_assoc_continue(struct aead_request *req, u32 flags) |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 383 | { |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 384 | unsigned int remain; |
| 385 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 386 | remain = gcm_remain(req->assoclen); |
| 387 | if (remain) |
| 388 | return gcm_hash_remain(req, remain, |
| 389 | gcm_hash_assoc_remain_done, flags) ?: |
| 390 | gcm_hash_assoc_remain_continue(req, flags); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 391 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 392 | return gcm_hash_assoc_remain_continue(req, flags); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 393 | } |
| 394 | |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 395 | static void gcm_hash_assoc_done(struct crypto_async_request *areq, int err) |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 396 | { |
| 397 | struct aead_request *req = areq->data; |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 398 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 399 | if (err) |
| 400 | goto out; |
| 401 | |
| 402 | err = gcm_hash_assoc_continue(req, 0); |
| 403 | if (err == -EINPROGRESS) |
| 404 | return; |
| 405 | |
| 406 | out: |
| 407 | aead_request_complete(req, err); |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 408 | } |
| 409 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 410 | static int gcm_hash_init_continue(struct aead_request *req, u32 flags) |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 411 | { |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 412 | if (req->assoclen) |
| 413 | return gcm_hash_update(req, gcm_hash_assoc_done, |
| 414 | req->src, req->assoclen, flags) ?: |
| 415 | gcm_hash_assoc_continue(req, flags); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 416 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 417 | return gcm_hash_assoc_remain_continue(req, flags); |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | static void gcm_hash_init_done(struct crypto_async_request *areq, int err) |
| 421 | { |
| 422 | struct aead_request *req = areq->data; |
| 423 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 424 | if (err) |
| 425 | goto out; |
| 426 | |
| 427 | err = gcm_hash_init_continue(req, 0); |
| 428 | if (err == -EINPROGRESS) |
| 429 | return; |
| 430 | |
| 431 | out: |
| 432 | aead_request_complete(req, err); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 433 | } |
| 434 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 435 | static int gcm_hash(struct aead_request *req, u32 flags) |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 436 | { |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 437 | struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 438 | struct ahash_request *ahreq = &pctx->u.ahreq; |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 439 | struct crypto_gcm_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req)); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 440 | |
| 441 | ahash_request_set_tfm(ahreq, ctx->ghash); |
| 442 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 443 | ahash_request_set_callback(ahreq, flags, gcm_hash_init_done, req); |
| 444 | return crypto_ahash_init(ahreq) ?: |
| 445 | gcm_hash_init_continue(req, flags); |
Herbert Xu | 6160b28 | 2007-12-04 19:17:50 +1100 | [diff] [blame] | 446 | } |
| 447 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 448 | static int gcm_enc_copy_hash(struct aead_request *req, u32 flags) |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 449 | { |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 450 | struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 451 | struct crypto_aead *aead = crypto_aead_reqtfm(req); |
| 452 | u8 *auth_tag = pctx->auth_tag; |
| 453 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 454 | crypto_xor(auth_tag, pctx->iauth_tag, 16); |
| 455 | scatterwalk_map_and_copy(auth_tag, req->dst, |
| 456 | req->assoclen + req->cryptlen, |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 457 | crypto_aead_authsize(aead), 1); |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 458 | return 0; |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 459 | } |
| 460 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 461 | static int gcm_encrypt_continue(struct aead_request *req, u32 flags) |
Herbert Xu | 6160b28 | 2007-12-04 19:17:50 +1100 | [diff] [blame] | 462 | { |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 463 | struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req); |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 464 | struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx; |
Herbert Xu | 6160b28 | 2007-12-04 19:17:50 +1100 | [diff] [blame] | 465 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 466 | gctx->src = sg_next(req->src == req->dst ? pctx->src : pctx->dst); |
| 467 | gctx->cryptlen = req->cryptlen; |
| 468 | gctx->complete = gcm_enc_copy_hash; |
Herbert Xu | 6160b28 | 2007-12-04 19:17:50 +1100 | [diff] [blame] | 469 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 470 | return gcm_hash(req, flags); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 471 | } |
| 472 | |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 473 | static void gcm_encrypt_done(struct crypto_async_request *areq, int err) |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 474 | { |
| 475 | struct aead_request *req = areq->data; |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 476 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 477 | if (err) |
| 478 | goto out; |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 479 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 480 | err = gcm_encrypt_continue(req, 0); |
| 481 | if (err == -EINPROGRESS) |
| 482 | return; |
| 483 | |
| 484 | out: |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 485 | aead_request_complete(req, err); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 486 | } |
| 487 | |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 488 | static int crypto_gcm_encrypt(struct aead_request *req) |
| 489 | { |
Herbert Xu | 2589469 | 2007-12-12 19:16:38 +0800 | [diff] [blame] | 490 | struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 491 | struct ablkcipher_request *abreq = &pctx->u.abreq; |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 492 | u32 flags = aead_request_flags(req); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 493 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 494 | crypto_gcm_init_common(req); |
| 495 | crypto_gcm_init_crypt(req, req->cryptlen); |
| 496 | ablkcipher_request_set_callback(abreq, flags, gcm_encrypt_done, req); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 497 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 498 | return crypto_ablkcipher_encrypt(abreq) ?: |
| 499 | gcm_encrypt_continue(req, flags); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 500 | } |
| 501 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 502 | static int crypto_gcm_verify(struct aead_request *req) |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 503 | { |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 504 | struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req); |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 505 | struct crypto_aead *aead = crypto_aead_reqtfm(req); |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 506 | u8 *auth_tag = pctx->auth_tag; |
| 507 | u8 *iauth_tag = pctx->iauth_tag; |
| 508 | unsigned int authsize = crypto_aead_authsize(aead); |
| 509 | unsigned int cryptlen = req->cryptlen - authsize; |
| 510 | |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 511 | crypto_xor(auth_tag, iauth_tag, 16); |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 512 | scatterwalk_map_and_copy(iauth_tag, req->src, |
| 513 | req->assoclen + cryptlen, authsize, 0); |
James Yonan | 6bf37e5 | 2013-09-26 02:20:39 -0600 | [diff] [blame] | 514 | return crypto_memneq(iauth_tag, auth_tag, authsize) ? -EBADMSG : 0; |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 515 | } |
| 516 | |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 517 | static void gcm_decrypt_done(struct crypto_async_request *areq, int err) |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 518 | { |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 519 | struct aead_request *req = areq->data; |
| 520 | |
| 521 | if (!err) |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 522 | err = crypto_gcm_verify(req); |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 523 | |
| 524 | aead_request_complete(req, err); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 525 | } |
| 526 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 527 | static int gcm_dec_hash_continue(struct aead_request *req, u32 flags) |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 528 | { |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 529 | struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req); |
| 530 | struct ablkcipher_request *abreq = &pctx->u.abreq; |
| 531 | struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx; |
| 532 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 533 | crypto_gcm_init_crypt(req, gctx->cryptlen); |
| 534 | ablkcipher_request_set_callback(abreq, flags, gcm_decrypt_done, req); |
| 535 | return crypto_ablkcipher_decrypt(abreq) ?: crypto_gcm_verify(req); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 536 | } |
| 537 | |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 538 | static int crypto_gcm_decrypt(struct aead_request *req) |
| 539 | { |
Herbert Xu | 6160b28 | 2007-12-04 19:17:50 +1100 | [diff] [blame] | 540 | struct crypto_aead *aead = crypto_aead_reqtfm(req); |
Herbert Xu | 2589469 | 2007-12-12 19:16:38 +0800 | [diff] [blame] | 541 | struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 542 | struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx; |
Herbert Xu | 6160b28 | 2007-12-04 19:17:50 +1100 | [diff] [blame] | 543 | unsigned int authsize = crypto_aead_authsize(aead); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 544 | unsigned int cryptlen = req->cryptlen; |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 545 | u32 flags = aead_request_flags(req); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 546 | |
Herbert Xu | 6160b28 | 2007-12-04 19:17:50 +1100 | [diff] [blame] | 547 | cryptlen -= authsize; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 548 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 549 | crypto_gcm_init_common(req); |
| 550 | |
| 551 | gctx->src = sg_next(pctx->src); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 552 | gctx->cryptlen = cryptlen; |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 553 | gctx->complete = gcm_dec_hash_continue; |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 554 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 555 | return gcm_hash(req, flags); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 556 | } |
| 557 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 558 | static int crypto_gcm_init_tfm(struct crypto_aead *tfm) |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 559 | { |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 560 | struct aead_instance *inst = aead_alg_instance(tfm); |
| 561 | struct gcm_instance_ctx *ictx = aead_instance_ctx(inst); |
| 562 | struct crypto_gcm_ctx *ctx = crypto_aead_ctx(tfm); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 563 | struct crypto_ablkcipher *ctr; |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 564 | struct crypto_ahash *ghash; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 565 | unsigned long align; |
| 566 | int err; |
| 567 | |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 568 | ghash = crypto_spawn_ahash(&ictx->ghash); |
| 569 | if (IS_ERR(ghash)) |
| 570 | return PTR_ERR(ghash); |
| 571 | |
Herbert Xu | 1472e5e | 2007-12-07 19:26:11 +0800 | [diff] [blame] | 572 | ctr = crypto_spawn_skcipher(&ictx->ctr); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 573 | err = PTR_ERR(ctr); |
| 574 | if (IS_ERR(ctr)) |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 575 | goto err_free_hash; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 576 | |
| 577 | ctx->ctr = ctr; |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 578 | ctx->ghash = ghash; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 579 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 580 | align = crypto_aead_alignmask(tfm); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 581 | align &= ~(crypto_tfm_ctx_alignment() - 1); |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 582 | crypto_aead_set_reqsize(tfm, |
Herbert Xu | 5d72336 | 2015-05-11 17:47:59 +0800 | [diff] [blame] | 583 | align + offsetof(struct crypto_gcm_req_priv_ctx, u) + |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 584 | max(sizeof(struct ablkcipher_request) + |
| 585 | crypto_ablkcipher_reqsize(ctr), |
| 586 | sizeof(struct ahash_request) + |
Herbert Xu | 5d72336 | 2015-05-11 17:47:59 +0800 | [diff] [blame] | 587 | crypto_ahash_reqsize(ghash))); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 588 | |
| 589 | return 0; |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 590 | |
| 591 | err_free_hash: |
| 592 | crypto_free_ahash(ghash); |
| 593 | return err; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 594 | } |
| 595 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 596 | static void crypto_gcm_exit_tfm(struct crypto_aead *tfm) |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 597 | { |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 598 | struct crypto_gcm_ctx *ctx = crypto_aead_ctx(tfm); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 599 | |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 600 | crypto_free_ahash(ctx->ghash); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 601 | crypto_free_ablkcipher(ctx->ctr); |
| 602 | } |
| 603 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 604 | static int crypto_gcm_create_common(struct crypto_template *tmpl, |
| 605 | struct rtattr **tb, |
| 606 | const char *full_name, |
| 607 | const char *ctr_name, |
| 608 | const char *ghash_name) |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 609 | { |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 610 | struct crypto_attr_type *algt; |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 611 | struct aead_instance *inst; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 612 | struct crypto_alg *ctr; |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 613 | struct crypto_alg *ghash_alg; |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 614 | struct hash_alg_common *ghash; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 615 | struct gcm_instance_ctx *ctx; |
| 616 | int err; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 617 | |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 618 | algt = crypto_get_attr_type(tb); |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 619 | if (IS_ERR(algt)) |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 620 | return PTR_ERR(algt); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 621 | |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 622 | if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask) |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 623 | return -EINVAL; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 624 | |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 625 | ghash_alg = crypto_find_alg(ghash_name, &crypto_ahash_type, |
| 626 | CRYPTO_ALG_TYPE_HASH, |
| 627 | CRYPTO_ALG_TYPE_AHASH_MASK); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 628 | if (IS_ERR(ghash_alg)) |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 629 | return PTR_ERR(ghash_alg); |
| 630 | |
| 631 | ghash = __crypto_hash_alg_common(ghash_alg); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 632 | |
| 633 | err = -ENOMEM; |
Herbert Xu | 1472e5e | 2007-12-07 19:26:11 +0800 | [diff] [blame] | 634 | inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL); |
| 635 | if (!inst) |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 636 | goto out_put_ghash; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 637 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 638 | ctx = aead_instance_ctx(inst); |
| 639 | err = crypto_init_ahash_spawn(&ctx->ghash, ghash, |
| 640 | aead_crypto_instance(inst)); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 641 | if (err) |
| 642 | goto err_free_inst; |
| 643 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 644 | err = -EINVAL; |
| 645 | if (ghash->digestsize != 16) |
| 646 | goto err_drop_ghash; |
| 647 | |
| 648 | crypto_set_skcipher_spawn(&ctx->ctr, aead_crypto_instance(inst)); |
Herbert Xu | 1472e5e | 2007-12-07 19:26:11 +0800 | [diff] [blame] | 649 | err = crypto_grab_skcipher(&ctx->ctr, ctr_name, 0, |
| 650 | crypto_requires_sync(algt->type, |
| 651 | algt->mask)); |
| 652 | if (err) |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 653 | goto err_drop_ghash; |
Herbert Xu | 1472e5e | 2007-12-07 19:26:11 +0800 | [diff] [blame] | 654 | |
| 655 | ctr = crypto_skcipher_spawn_alg(&ctx->ctr); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 656 | |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 657 | /* We only support 16-byte blocks. */ |
Herbert Xu | 1472e5e | 2007-12-07 19:26:11 +0800 | [diff] [blame] | 658 | if (ctr->cra_ablkcipher.ivsize != 16) |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 659 | goto out_put_ctr; |
| 660 | |
| 661 | /* Not a stream cipher? */ |
| 662 | err = -EINVAL; |
| 663 | if (ctr->cra_blocksize != 1) |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 664 | goto out_put_ctr; |
| 665 | |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 666 | err = -ENAMETOOLONG; |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 667 | if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME, |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 668 | "gcm_base(%s,%s)", ctr->cra_driver_name, |
| 669 | ghash_alg->cra_driver_name) >= |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 670 | CRYPTO_MAX_ALG_NAME) |
Herbert Xu | 1472e5e | 2007-12-07 19:26:11 +0800 | [diff] [blame] | 671 | goto out_put_ctr; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 672 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 673 | memcpy(inst->alg.base.cra_name, full_name, CRYPTO_MAX_ALG_NAME); |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 674 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 675 | inst->alg.base.cra_flags = (ghash->base.cra_flags | ctr->cra_flags) & |
| 676 | CRYPTO_ALG_ASYNC; |
| 677 | inst->alg.base.cra_priority = (ghash->base.cra_priority + |
| 678 | ctr->cra_priority) / 2; |
| 679 | inst->alg.base.cra_blocksize = 1; |
| 680 | inst->alg.base.cra_alignmask = ghash->base.cra_alignmask | |
| 681 | ctr->cra_alignmask; |
| 682 | inst->alg.base.cra_ctxsize = sizeof(struct crypto_gcm_ctx); |
| 683 | inst->alg.ivsize = 12; |
| 684 | inst->alg.maxauthsize = 16; |
| 685 | inst->alg.init = crypto_gcm_init_tfm; |
| 686 | inst->alg.exit = crypto_gcm_exit_tfm; |
| 687 | inst->alg.setkey = crypto_gcm_setkey; |
| 688 | inst->alg.setauthsize = crypto_gcm_setauthsize; |
| 689 | inst->alg.encrypt = crypto_gcm_encrypt; |
| 690 | inst->alg.decrypt = crypto_gcm_decrypt; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 691 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 692 | err = aead_register_instance(tmpl, inst); |
| 693 | if (err) |
| 694 | goto out_put_ctr; |
| 695 | |
| 696 | out_put_ghash: |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 697 | crypto_mod_put(ghash_alg); |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 698 | return err; |
Herbert Xu | 1472e5e | 2007-12-07 19:26:11 +0800 | [diff] [blame] | 699 | |
| 700 | out_put_ctr: |
| 701 | crypto_drop_skcipher(&ctx->ctr); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 702 | err_drop_ghash: |
| 703 | crypto_drop_ahash(&ctx->ghash); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 704 | err_free_inst: |
| 705 | kfree(inst); |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 706 | goto out_put_ghash; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 707 | } |
| 708 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 709 | static int crypto_gcm_create(struct crypto_template *tmpl, struct rtattr **tb) |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 710 | { |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 711 | const char *cipher_name; |
| 712 | char ctr_name[CRYPTO_MAX_ALG_NAME]; |
| 713 | char full_name[CRYPTO_MAX_ALG_NAME]; |
| 714 | |
| 715 | cipher_name = crypto_attr_alg_name(tb[1]); |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 716 | if (IS_ERR(cipher_name)) |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 717 | return PTR_ERR(cipher_name); |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 718 | |
| 719 | if (snprintf(ctr_name, CRYPTO_MAX_ALG_NAME, "ctr(%s)", cipher_name) >= |
| 720 | CRYPTO_MAX_ALG_NAME) |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 721 | return -ENAMETOOLONG; |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 722 | |
| 723 | if (snprintf(full_name, CRYPTO_MAX_ALG_NAME, "gcm(%s)", cipher_name) >= |
| 724 | CRYPTO_MAX_ALG_NAME) |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 725 | return -ENAMETOOLONG; |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 726 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 727 | return crypto_gcm_create_common(tmpl, tb, full_name, |
| 728 | ctr_name, "ghash"); |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 729 | } |
| 730 | |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 731 | static void crypto_gcm_free(struct crypto_instance *inst) |
| 732 | { |
| 733 | struct gcm_instance_ctx *ctx = crypto_instance_ctx(inst); |
| 734 | |
Herbert Xu | 1472e5e | 2007-12-07 19:26:11 +0800 | [diff] [blame] | 735 | crypto_drop_skcipher(&ctx->ctr); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 736 | crypto_drop_ahash(&ctx->ghash); |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 737 | kfree(aead_instance(inst)); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | static struct crypto_template crypto_gcm_tmpl = { |
| 741 | .name = "gcm", |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 742 | .create = crypto_gcm_create, |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 743 | .free = crypto_gcm_free, |
| 744 | .module = THIS_MODULE, |
| 745 | }; |
| 746 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 747 | static int crypto_gcm_base_create(struct crypto_template *tmpl, |
| 748 | struct rtattr **tb) |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 749 | { |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 750 | const char *ctr_name; |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 751 | const char *ghash_name; |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 752 | char full_name[CRYPTO_MAX_ALG_NAME]; |
| 753 | |
| 754 | ctr_name = crypto_attr_alg_name(tb[1]); |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 755 | if (IS_ERR(ctr_name)) |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 756 | return PTR_ERR(ctr_name); |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 757 | |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 758 | ghash_name = crypto_attr_alg_name(tb[2]); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 759 | if (IS_ERR(ghash_name)) |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 760 | return PTR_ERR(ghash_name); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 761 | |
| 762 | if (snprintf(full_name, CRYPTO_MAX_ALG_NAME, "gcm_base(%s,%s)", |
| 763 | ctr_name, ghash_name) >= CRYPTO_MAX_ALG_NAME) |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 764 | return -ENAMETOOLONG; |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 765 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 766 | return crypto_gcm_create_common(tmpl, tb, full_name, |
| 767 | ctr_name, ghash_name); |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | static struct crypto_template crypto_gcm_base_tmpl = { |
| 771 | .name = "gcm_base", |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 772 | .create = crypto_gcm_base_create, |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 773 | .free = crypto_gcm_free, |
| 774 | .module = THIS_MODULE, |
| 775 | }; |
| 776 | |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 777 | static int crypto_rfc4106_setkey(struct crypto_aead *parent, const u8 *key, |
| 778 | unsigned int keylen) |
| 779 | { |
| 780 | struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(parent); |
| 781 | struct crypto_aead *child = ctx->child; |
| 782 | int err; |
| 783 | |
| 784 | if (keylen < 4) |
| 785 | return -EINVAL; |
| 786 | |
| 787 | keylen -= 4; |
| 788 | memcpy(ctx->nonce, key + keylen, 4); |
| 789 | |
| 790 | crypto_aead_clear_flags(child, CRYPTO_TFM_REQ_MASK); |
| 791 | crypto_aead_set_flags(child, crypto_aead_get_flags(parent) & |
| 792 | CRYPTO_TFM_REQ_MASK); |
| 793 | err = crypto_aead_setkey(child, key, keylen); |
| 794 | crypto_aead_set_flags(parent, crypto_aead_get_flags(child) & |
| 795 | CRYPTO_TFM_RES_MASK); |
| 796 | |
| 797 | return err; |
| 798 | } |
| 799 | |
| 800 | static int crypto_rfc4106_setauthsize(struct crypto_aead *parent, |
| 801 | unsigned int authsize) |
| 802 | { |
| 803 | struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(parent); |
| 804 | |
| 805 | switch (authsize) { |
| 806 | case 8: |
| 807 | case 12: |
| 808 | case 16: |
| 809 | break; |
| 810 | default: |
| 811 | return -EINVAL; |
| 812 | } |
| 813 | |
| 814 | return crypto_aead_setauthsize(ctx->child, authsize); |
| 815 | } |
| 816 | |
| 817 | static struct aead_request *crypto_rfc4106_crypt(struct aead_request *req) |
| 818 | { |
| 819 | struct aead_request *subreq = aead_request_ctx(req); |
| 820 | struct crypto_aead *aead = crypto_aead_reqtfm(req); |
| 821 | struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(aead); |
| 822 | struct crypto_aead *child = ctx->child; |
| 823 | u8 *iv = PTR_ALIGN((u8 *)(subreq + 1) + crypto_aead_reqsize(child), |
| 824 | crypto_aead_alignmask(child) + 1); |
| 825 | |
| 826 | memcpy(iv, ctx->nonce, 4); |
| 827 | memcpy(iv + 4, req->iv, 8); |
| 828 | |
| 829 | aead_request_set_tfm(subreq, child); |
| 830 | aead_request_set_callback(subreq, req->base.flags, req->base.complete, |
| 831 | req->base.data); |
| 832 | aead_request_set_crypt(subreq, req->src, req->dst, req->cryptlen, iv); |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 833 | aead_request_set_ad(subreq, req->assoclen); |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 834 | |
| 835 | return subreq; |
| 836 | } |
| 837 | |
| 838 | static int crypto_rfc4106_encrypt(struct aead_request *req) |
| 839 | { |
| 840 | req = crypto_rfc4106_crypt(req); |
| 841 | |
| 842 | return crypto_aead_encrypt(req); |
| 843 | } |
| 844 | |
| 845 | static int crypto_rfc4106_decrypt(struct aead_request *req) |
| 846 | { |
| 847 | req = crypto_rfc4106_crypt(req); |
| 848 | |
| 849 | return crypto_aead_decrypt(req); |
| 850 | } |
| 851 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 852 | static int crypto_rfc4106_init_tfm(struct crypto_aead *tfm) |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 853 | { |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 854 | struct aead_instance *inst = aead_alg_instance(tfm); |
| 855 | struct crypto_aead_spawn *spawn = aead_instance_ctx(inst); |
| 856 | struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(tfm); |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 857 | struct crypto_aead *aead; |
| 858 | unsigned long align; |
| 859 | |
| 860 | aead = crypto_spawn_aead(spawn); |
| 861 | if (IS_ERR(aead)) |
| 862 | return PTR_ERR(aead); |
| 863 | |
| 864 | ctx->child = aead; |
| 865 | |
| 866 | align = crypto_aead_alignmask(aead); |
| 867 | align &= ~(crypto_tfm_ctx_alignment() - 1); |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 868 | crypto_aead_set_reqsize( |
| 869 | tfm, |
Herbert Xu | 5d72336 | 2015-05-11 17:47:59 +0800 | [diff] [blame] | 870 | sizeof(struct aead_request) + |
| 871 | ALIGN(crypto_aead_reqsize(aead), crypto_tfm_ctx_alignment()) + |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 872 | align + 12); |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 873 | |
| 874 | return 0; |
| 875 | } |
| 876 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 877 | static void crypto_rfc4106_exit_tfm(struct crypto_aead *tfm) |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 878 | { |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 879 | struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(tfm); |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 880 | |
| 881 | crypto_free_aead(ctx->child); |
| 882 | } |
| 883 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 884 | static int crypto_rfc4106_create(struct crypto_template *tmpl, |
| 885 | struct rtattr **tb) |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 886 | { |
| 887 | struct crypto_attr_type *algt; |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 888 | struct aead_instance *inst; |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 889 | struct crypto_aead_spawn *spawn; |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 890 | struct aead_alg *alg; |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 891 | const char *ccm_name; |
| 892 | int err; |
| 893 | |
| 894 | algt = crypto_get_attr_type(tb); |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 895 | if (IS_ERR(algt)) |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 896 | return PTR_ERR(algt); |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 897 | |
| 898 | if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask) |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 899 | return -EINVAL; |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 900 | |
| 901 | ccm_name = crypto_attr_alg_name(tb[1]); |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 902 | if (IS_ERR(ccm_name)) |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 903 | return PTR_ERR(ccm_name); |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 904 | |
| 905 | inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL); |
| 906 | if (!inst) |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 907 | return -ENOMEM; |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 908 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 909 | spawn = aead_instance_ctx(inst); |
| 910 | crypto_set_aead_spawn(spawn, aead_crypto_instance(inst)); |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 911 | err = crypto_grab_aead(spawn, ccm_name, 0, |
| 912 | crypto_requires_sync(algt->type, algt->mask)); |
| 913 | if (err) |
| 914 | goto out_free_inst; |
| 915 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 916 | alg = crypto_spawn_aead_alg(spawn); |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 917 | |
| 918 | err = -EINVAL; |
| 919 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 920 | /* Underlying IV size must be 12. */ |
| 921 | if (crypto_aead_alg_ivsize(alg) != 12) |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 922 | goto out_drop_alg; |
| 923 | |
| 924 | /* Not a stream cipher? */ |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 925 | if (alg->base.cra_blocksize != 1) |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 926 | goto out_drop_alg; |
| 927 | |
| 928 | err = -ENAMETOOLONG; |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 929 | if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME, |
| 930 | "rfc4106(%s)", alg->base.cra_name) >= |
| 931 | CRYPTO_MAX_ALG_NAME || |
| 932 | snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME, |
| 933 | "rfc4106(%s)", alg->base.cra_driver_name) >= |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 934 | CRYPTO_MAX_ALG_NAME) |
| 935 | goto out_drop_alg; |
| 936 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 937 | inst->alg.base.cra_flags |= alg->base.cra_flags & CRYPTO_ALG_ASYNC; |
| 938 | inst->alg.base.cra_priority = alg->base.cra_priority; |
| 939 | inst->alg.base.cra_blocksize = 1; |
| 940 | inst->alg.base.cra_alignmask = alg->base.cra_alignmask; |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 941 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 942 | inst->alg.base.cra_ctxsize = sizeof(struct crypto_rfc4106_ctx); |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 943 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 944 | inst->alg.ivsize = 8; |
| 945 | inst->alg.maxauthsize = crypto_aead_alg_maxauthsize(alg); |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 946 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 947 | inst->alg.init = crypto_rfc4106_init_tfm; |
| 948 | inst->alg.exit = crypto_rfc4106_exit_tfm; |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 949 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 950 | inst->alg.setkey = crypto_rfc4106_setkey; |
| 951 | inst->alg.setauthsize = crypto_rfc4106_setauthsize; |
| 952 | inst->alg.encrypt = crypto_rfc4106_encrypt; |
| 953 | inst->alg.decrypt = crypto_rfc4106_decrypt; |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 954 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 955 | err = aead_register_instance(tmpl, inst); |
| 956 | if (err) |
| 957 | goto out_drop_alg; |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 958 | |
| 959 | out: |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 960 | return err; |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 961 | |
| 962 | out_drop_alg: |
| 963 | crypto_drop_aead(spawn); |
| 964 | out_free_inst: |
| 965 | kfree(inst); |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 966 | goto out; |
| 967 | } |
| 968 | |
| 969 | static void crypto_rfc4106_free(struct crypto_instance *inst) |
| 970 | { |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 971 | crypto_drop_aead(crypto_instance_ctx(inst)); |
| 972 | kfree(aead_instance(inst)); |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 973 | } |
| 974 | |
| 975 | static struct crypto_template crypto_rfc4106_tmpl = { |
| 976 | .name = "rfc4106", |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 977 | .create = crypto_rfc4106_create, |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 978 | .free = crypto_rfc4106_free, |
| 979 | .module = THIS_MODULE, |
| 980 | }; |
| 981 | |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 982 | static int crypto_rfc4543_setkey(struct crypto_aead *parent, const u8 *key, |
| 983 | unsigned int keylen) |
| 984 | { |
| 985 | struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(parent); |
| 986 | struct crypto_aead *child = ctx->child; |
| 987 | int err; |
| 988 | |
| 989 | if (keylen < 4) |
| 990 | return -EINVAL; |
| 991 | |
| 992 | keylen -= 4; |
| 993 | memcpy(ctx->nonce, key + keylen, 4); |
| 994 | |
| 995 | crypto_aead_clear_flags(child, CRYPTO_TFM_REQ_MASK); |
| 996 | crypto_aead_set_flags(child, crypto_aead_get_flags(parent) & |
| 997 | CRYPTO_TFM_REQ_MASK); |
| 998 | err = crypto_aead_setkey(child, key, keylen); |
| 999 | crypto_aead_set_flags(parent, crypto_aead_get_flags(child) & |
| 1000 | CRYPTO_TFM_RES_MASK); |
| 1001 | |
| 1002 | return err; |
| 1003 | } |
| 1004 | |
| 1005 | static int crypto_rfc4543_setauthsize(struct crypto_aead *parent, |
| 1006 | unsigned int authsize) |
| 1007 | { |
| 1008 | struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(parent); |
| 1009 | |
| 1010 | if (authsize != 16) |
| 1011 | return -EINVAL; |
| 1012 | |
| 1013 | return crypto_aead_setauthsize(ctx->child, authsize); |
| 1014 | } |
| 1015 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 1016 | static int crypto_rfc4543_crypt(struct aead_request *req, bool enc) |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1017 | { |
| 1018 | struct crypto_aead *aead = crypto_aead_reqtfm(req); |
| 1019 | struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(aead); |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 1020 | struct crypto_rfc4543_req_ctx *rctx = aead_request_ctx(req); |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1021 | struct aead_request *subreq = &rctx->subreq; |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1022 | unsigned int authsize = crypto_aead_authsize(aead); |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1023 | u8 *iv = PTR_ALIGN((u8 *)(rctx + 1) + crypto_aead_reqsize(ctx->child), |
| 1024 | crypto_aead_alignmask(ctx->child) + 1); |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 1025 | int err; |
| 1026 | |
| 1027 | if (req->src != req->dst) { |
| 1028 | err = crypto_rfc4543_copy_src_to_dst(req, enc); |
| 1029 | if (err) |
| 1030 | return err; |
| 1031 | } |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1032 | |
| 1033 | memcpy(iv, ctx->nonce, 4); |
| 1034 | memcpy(iv + 4, req->iv, 8); |
| 1035 | |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1036 | aead_request_set_tfm(subreq, ctx->child); |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 1037 | aead_request_set_callback(subreq, req->base.flags, |
| 1038 | req->base.complete, req->base.data); |
| 1039 | aead_request_set_crypt(subreq, req->src, req->dst, |
| 1040 | enc ? 0 : authsize, iv); |
| 1041 | aead_request_set_ad(subreq, req->assoclen + req->cryptlen - |
| 1042 | subreq->cryptlen); |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1043 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 1044 | return enc ? crypto_aead_encrypt(subreq) : crypto_aead_decrypt(subreq); |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1045 | } |
| 1046 | |
Jussi Kivilinna | 9489667d | 2013-04-07 16:43:41 +0300 | [diff] [blame] | 1047 | static int crypto_rfc4543_copy_src_to_dst(struct aead_request *req, bool enc) |
| 1048 | { |
| 1049 | struct crypto_aead *aead = crypto_aead_reqtfm(req); |
| 1050 | struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(aead); |
| 1051 | unsigned int authsize = crypto_aead_authsize(aead); |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 1052 | unsigned int nbytes = req->assoclen + req->cryptlen - |
| 1053 | (enc ? 0 : authsize); |
Jussi Kivilinna | 9489667d | 2013-04-07 16:43:41 +0300 | [diff] [blame] | 1054 | struct blkcipher_desc desc = { |
| 1055 | .tfm = ctx->null, |
| 1056 | }; |
| 1057 | |
| 1058 | return crypto_blkcipher_encrypt(&desc, req->dst, req->src, nbytes); |
| 1059 | } |
| 1060 | |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1061 | static int crypto_rfc4543_encrypt(struct aead_request *req) |
| 1062 | { |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 1063 | return crypto_rfc4543_crypt(req, true); |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1064 | } |
| 1065 | |
| 1066 | static int crypto_rfc4543_decrypt(struct aead_request *req) |
| 1067 | { |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 1068 | return crypto_rfc4543_crypt(req, false); |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1069 | } |
| 1070 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 1071 | static int crypto_rfc4543_init_tfm(struct crypto_aead *tfm) |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1072 | { |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 1073 | struct aead_instance *inst = aead_alg_instance(tfm); |
| 1074 | struct crypto_rfc4543_instance_ctx *ictx = aead_instance_ctx(inst); |
Jussi Kivilinna | 9489667d | 2013-04-07 16:43:41 +0300 | [diff] [blame] | 1075 | struct crypto_aead_spawn *spawn = &ictx->aead; |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 1076 | struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(tfm); |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1077 | struct crypto_aead *aead; |
Jussi Kivilinna | 9489667d | 2013-04-07 16:43:41 +0300 | [diff] [blame] | 1078 | struct crypto_blkcipher *null; |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1079 | unsigned long align; |
Jussi Kivilinna | 9489667d | 2013-04-07 16:43:41 +0300 | [diff] [blame] | 1080 | int err = 0; |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1081 | |
| 1082 | aead = crypto_spawn_aead(spawn); |
| 1083 | if (IS_ERR(aead)) |
| 1084 | return PTR_ERR(aead); |
| 1085 | |
Herbert Xu | 17db854 | 2015-05-21 15:11:11 +0800 | [diff] [blame] | 1086 | null = crypto_get_default_null_skcipher(); |
Jussi Kivilinna | 9489667d | 2013-04-07 16:43:41 +0300 | [diff] [blame] | 1087 | err = PTR_ERR(null); |
| 1088 | if (IS_ERR(null)) |
| 1089 | goto err_free_aead; |
| 1090 | |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1091 | ctx->child = aead; |
Jussi Kivilinna | 9489667d | 2013-04-07 16:43:41 +0300 | [diff] [blame] | 1092 | ctx->null = null; |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1093 | |
| 1094 | align = crypto_aead_alignmask(aead); |
| 1095 | align &= ~(crypto_tfm_ctx_alignment() - 1); |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 1096 | crypto_aead_set_reqsize( |
| 1097 | tfm, |
Herbert Xu | 5d72336 | 2015-05-11 17:47:59 +0800 | [diff] [blame] | 1098 | sizeof(struct crypto_rfc4543_req_ctx) + |
| 1099 | ALIGN(crypto_aead_reqsize(aead), crypto_tfm_ctx_alignment()) + |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 1100 | align + 12); |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1101 | |
| 1102 | return 0; |
Jussi Kivilinna | 9489667d | 2013-04-07 16:43:41 +0300 | [diff] [blame] | 1103 | |
| 1104 | err_free_aead: |
| 1105 | crypto_free_aead(aead); |
| 1106 | return err; |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1107 | } |
| 1108 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 1109 | static void crypto_rfc4543_exit_tfm(struct crypto_aead *tfm) |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1110 | { |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 1111 | struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(tfm); |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1112 | |
| 1113 | crypto_free_aead(ctx->child); |
Herbert Xu | 17db854 | 2015-05-21 15:11:11 +0800 | [diff] [blame] | 1114 | crypto_put_default_null_skcipher(); |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1115 | } |
| 1116 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 1117 | static int crypto_rfc4543_create(struct crypto_template *tmpl, |
| 1118 | struct rtattr **tb) |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1119 | { |
| 1120 | struct crypto_attr_type *algt; |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 1121 | struct aead_instance *inst; |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1122 | struct crypto_aead_spawn *spawn; |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 1123 | struct aead_alg *alg; |
Jussi Kivilinna | 9489667d | 2013-04-07 16:43:41 +0300 | [diff] [blame] | 1124 | struct crypto_rfc4543_instance_ctx *ctx; |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1125 | const char *ccm_name; |
| 1126 | int err; |
| 1127 | |
| 1128 | algt = crypto_get_attr_type(tb); |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1129 | if (IS_ERR(algt)) |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 1130 | return PTR_ERR(algt); |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1131 | |
| 1132 | if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask) |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 1133 | return -EINVAL; |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1134 | |
| 1135 | ccm_name = crypto_attr_alg_name(tb[1]); |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1136 | if (IS_ERR(ccm_name)) |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 1137 | return PTR_ERR(ccm_name); |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1138 | |
Jussi Kivilinna | 9489667d | 2013-04-07 16:43:41 +0300 | [diff] [blame] | 1139 | inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL); |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1140 | if (!inst) |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 1141 | return -ENOMEM; |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1142 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 1143 | ctx = aead_instance_ctx(inst); |
Jussi Kivilinna | 9489667d | 2013-04-07 16:43:41 +0300 | [diff] [blame] | 1144 | spawn = &ctx->aead; |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 1145 | crypto_set_aead_spawn(spawn, aead_crypto_instance(inst)); |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1146 | err = crypto_grab_aead(spawn, ccm_name, 0, |
| 1147 | crypto_requires_sync(algt->type, algt->mask)); |
| 1148 | if (err) |
| 1149 | goto out_free_inst; |
| 1150 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 1151 | alg = crypto_spawn_aead_alg(spawn); |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1152 | |
| 1153 | err = -EINVAL; |
| 1154 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 1155 | /* Underlying IV size must be 12. */ |
| 1156 | if (crypto_aead_alg_ivsize(alg) != 12) |
Herbert Xu | 17db854 | 2015-05-21 15:11:11 +0800 | [diff] [blame] | 1157 | goto out_drop_alg; |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1158 | |
| 1159 | /* Not a stream cipher? */ |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 1160 | if (alg->base.cra_blocksize != 1) |
Herbert Xu | 17db854 | 2015-05-21 15:11:11 +0800 | [diff] [blame] | 1161 | goto out_drop_alg; |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1162 | |
| 1163 | err = -ENAMETOOLONG; |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 1164 | if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME, |
| 1165 | "rfc4543(%s)", alg->base.cra_name) >= |
| 1166 | CRYPTO_MAX_ALG_NAME || |
| 1167 | snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME, |
| 1168 | "rfc4543(%s)", alg->base.cra_driver_name) >= |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1169 | CRYPTO_MAX_ALG_NAME) |
Herbert Xu | 17db854 | 2015-05-21 15:11:11 +0800 | [diff] [blame] | 1170 | goto out_drop_alg; |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1171 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 1172 | inst->alg.base.cra_flags = alg->base.cra_flags & CRYPTO_ALG_ASYNC; |
| 1173 | inst->alg.base.cra_priority = alg->base.cra_priority; |
| 1174 | inst->alg.base.cra_blocksize = 1; |
| 1175 | inst->alg.base.cra_alignmask = alg->base.cra_alignmask; |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1176 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 1177 | inst->alg.base.cra_ctxsize = sizeof(struct crypto_rfc4543_ctx); |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1178 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 1179 | inst->alg.ivsize = 8; |
| 1180 | inst->alg.maxauthsize = crypto_aead_alg_maxauthsize(alg); |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1181 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 1182 | inst->alg.init = crypto_rfc4543_init_tfm; |
| 1183 | inst->alg.exit = crypto_rfc4543_exit_tfm; |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1184 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 1185 | inst->alg.setkey = crypto_rfc4543_setkey; |
| 1186 | inst->alg.setauthsize = crypto_rfc4543_setauthsize; |
| 1187 | inst->alg.encrypt = crypto_rfc4543_encrypt; |
| 1188 | inst->alg.decrypt = crypto_rfc4543_decrypt; |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1189 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 1190 | err = aead_register_instance(tmpl, inst); |
| 1191 | if (err) |
| 1192 | goto out_drop_alg; |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1193 | |
| 1194 | out: |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 1195 | return err; |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1196 | |
| 1197 | out_drop_alg: |
| 1198 | crypto_drop_aead(spawn); |
| 1199 | out_free_inst: |
| 1200 | kfree(inst); |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1201 | goto out; |
| 1202 | } |
| 1203 | |
| 1204 | static void crypto_rfc4543_free(struct crypto_instance *inst) |
| 1205 | { |
Jussi Kivilinna | 9489667d | 2013-04-07 16:43:41 +0300 | [diff] [blame] | 1206 | struct crypto_rfc4543_instance_ctx *ctx = crypto_instance_ctx(inst); |
| 1207 | |
| 1208 | crypto_drop_aead(&ctx->aead); |
Jussi Kivilinna | 9489667d | 2013-04-07 16:43:41 +0300 | [diff] [blame] | 1209 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 1210 | kfree(aead_instance(inst)); |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1211 | } |
| 1212 | |
| 1213 | static struct crypto_template crypto_rfc4543_tmpl = { |
| 1214 | .name = "rfc4543", |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 1215 | .create = crypto_rfc4543_create, |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1216 | .free = crypto_rfc4543_free, |
| 1217 | .module = THIS_MODULE, |
| 1218 | }; |
| 1219 | |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 1220 | static int __init crypto_gcm_module_init(void) |
| 1221 | { |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 1222 | int err; |
| 1223 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 1224 | gcm_zeroes = kzalloc(sizeof(*gcm_zeroes), GFP_KERNEL); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 1225 | if (!gcm_zeroes) |
| 1226 | return -ENOMEM; |
| 1227 | |
Herbert Xu | adcbc68 | 2015-06-16 13:54:18 +0800 | [diff] [blame] | 1228 | sg_init_one(&gcm_zeroes->sg, gcm_zeroes->buf, sizeof(gcm_zeroes->buf)); |
| 1229 | |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 1230 | err = crypto_register_template(&crypto_gcm_base_tmpl); |
| 1231 | if (err) |
| 1232 | goto out; |
| 1233 | |
| 1234 | err = crypto_register_template(&crypto_gcm_tmpl); |
| 1235 | if (err) |
| 1236 | goto out_undo_base; |
| 1237 | |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 1238 | err = crypto_register_template(&crypto_rfc4106_tmpl); |
| 1239 | if (err) |
| 1240 | goto out_undo_gcm; |
| 1241 | |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1242 | err = crypto_register_template(&crypto_rfc4543_tmpl); |
| 1243 | if (err) |
| 1244 | goto out_undo_rfc4106; |
| 1245 | |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 1246 | return 0; |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 1247 | |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1248 | out_undo_rfc4106: |
| 1249 | crypto_unregister_template(&crypto_rfc4106_tmpl); |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 1250 | out_undo_gcm: |
| 1251 | crypto_unregister_template(&crypto_gcm_tmpl); |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 1252 | out_undo_base: |
| 1253 | crypto_unregister_template(&crypto_gcm_base_tmpl); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 1254 | out: |
| 1255 | kfree(gcm_zeroes); |
| 1256 | return err; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 1257 | } |
| 1258 | |
| 1259 | static void __exit crypto_gcm_module_exit(void) |
| 1260 | { |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 1261 | kfree(gcm_zeroes); |
Tobias Brunner | 73c89c1 | 2010-01-17 21:52:11 +1100 | [diff] [blame] | 1262 | crypto_unregister_template(&crypto_rfc4543_tmpl); |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 1263 | crypto_unregister_template(&crypto_rfc4106_tmpl); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 1264 | crypto_unregister_template(&crypto_gcm_tmpl); |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 1265 | crypto_unregister_template(&crypto_gcm_base_tmpl); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 1266 | } |
| 1267 | |
| 1268 | module_init(crypto_gcm_module_init); |
| 1269 | module_exit(crypto_gcm_module_exit); |
| 1270 | |
| 1271 | MODULE_LICENSE("GPL"); |
| 1272 | MODULE_DESCRIPTION("Galois/Counter Mode"); |
| 1273 | MODULE_AUTHOR("Mikko Herranen <mh1@iki.fi>"); |
Kees Cook | 5d26a10 | 2014-11-20 17:05:53 -0800 | [diff] [blame] | 1274 | MODULE_ALIAS_CRYPTO("gcm_base"); |
| 1275 | MODULE_ALIAS_CRYPTO("rfc4106"); |
| 1276 | MODULE_ALIAS_CRYPTO("rfc4543"); |
Kees Cook | 4943ba1 | 2014-11-24 16:32:38 -0800 | [diff] [blame] | 1277 | MODULE_ALIAS_CRYPTO("gcm"); |