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