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 | |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 40 | struct crypto_gcm_ghash_ctx { |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 41 | unsigned int cryptlen; |
| 42 | struct scatterlist *src; |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 43 | void (*complete)(struct aead_request *req, int err); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | struct crypto_gcm_req_priv_ctx { |
| 47 | u8 auth_tag[16]; |
Herbert Xu | 6160b28 | 2007-12-04 19:17:50 +1100 | [diff] [blame] | 48 | u8 iauth_tag[16]; |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 49 | struct scatterlist src[2]; |
| 50 | struct scatterlist dst[2]; |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 51 | struct crypto_gcm_ghash_ctx ghash_ctx; |
| 52 | union { |
| 53 | struct ahash_request ahreq; |
| 54 | struct ablkcipher_request abreq; |
| 55 | } u; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 56 | }; |
| 57 | |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 58 | struct crypto_gcm_setkey_result { |
| 59 | int err; |
| 60 | struct completion completion; |
| 61 | }; |
| 62 | |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 63 | static void *gcm_zeroes; |
| 64 | |
Herbert Xu | 2589469 | 2007-12-12 19:16:38 +0800 | [diff] [blame] | 65 | static inline struct crypto_gcm_req_priv_ctx *crypto_gcm_reqctx( |
| 66 | struct aead_request *req) |
| 67 | { |
| 68 | unsigned long align = crypto_aead_alignmask(crypto_aead_reqtfm(req)); |
| 69 | |
| 70 | return (void *)PTR_ALIGN((u8 *)aead_request_ctx(req), align + 1); |
| 71 | } |
| 72 | |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 73 | 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] | 74 | { |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 75 | struct crypto_gcm_setkey_result *result = req->data; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 76 | |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 77 | if (err == -EINPROGRESS) |
| 78 | return; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 79 | |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 80 | result->err = err; |
| 81 | complete(&result->completion); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | static int crypto_gcm_setkey(struct crypto_aead *aead, const u8 *key, |
| 85 | unsigned int keylen) |
| 86 | { |
| 87 | struct crypto_gcm_ctx *ctx = crypto_aead_ctx(aead); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 88 | struct crypto_ahash *ghash = ctx->ghash; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 89 | struct crypto_ablkcipher *ctr = ctx->ctr; |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 90 | struct { |
| 91 | be128 hash; |
| 92 | u8 iv[8]; |
| 93 | |
| 94 | struct crypto_gcm_setkey_result result; |
| 95 | |
| 96 | struct scatterlist sg[1]; |
| 97 | struct ablkcipher_request req; |
| 98 | } *data; |
| 99 | int err; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 100 | |
| 101 | crypto_ablkcipher_clear_flags(ctr, CRYPTO_TFM_REQ_MASK); |
| 102 | crypto_ablkcipher_set_flags(ctr, crypto_aead_get_flags(aead) & |
| 103 | CRYPTO_TFM_REQ_MASK); |
| 104 | |
| 105 | err = crypto_ablkcipher_setkey(ctr, key, keylen); |
| 106 | if (err) |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 107 | return err; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 108 | |
| 109 | crypto_aead_set_flags(aead, crypto_ablkcipher_get_flags(ctr) & |
| 110 | CRYPTO_TFM_RES_MASK); |
| 111 | |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 112 | data = kzalloc(sizeof(*data) + crypto_ablkcipher_reqsize(ctr), |
| 113 | GFP_KERNEL); |
| 114 | if (!data) |
| 115 | return -ENOMEM; |
| 116 | |
| 117 | init_completion(&data->result.completion); |
| 118 | sg_init_one(data->sg, &data->hash, sizeof(data->hash)); |
| 119 | ablkcipher_request_set_tfm(&data->req, ctr); |
| 120 | ablkcipher_request_set_callback(&data->req, CRYPTO_TFM_REQ_MAY_SLEEP | |
| 121 | CRYPTO_TFM_REQ_MAY_BACKLOG, |
| 122 | crypto_gcm_setkey_done, |
| 123 | &data->result); |
| 124 | ablkcipher_request_set_crypt(&data->req, data->sg, data->sg, |
| 125 | sizeof(data->hash), data->iv); |
| 126 | |
| 127 | err = crypto_ablkcipher_encrypt(&data->req); |
| 128 | if (err == -EINPROGRESS || err == -EBUSY) { |
| 129 | err = wait_for_completion_interruptible( |
| 130 | &data->result.completion); |
| 131 | if (!err) |
| 132 | err = data->result.err; |
| 133 | } |
| 134 | |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 135 | if (err) |
| 136 | goto out; |
| 137 | |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 138 | crypto_ahash_clear_flags(ghash, CRYPTO_TFM_REQ_MASK); |
| 139 | crypto_ahash_set_flags(ghash, crypto_aead_get_flags(aead) & |
| 140 | CRYPTO_TFM_REQ_MASK); |
| 141 | err = crypto_ahash_setkey(ghash, (u8 *)&data->hash, sizeof(be128)); |
| 142 | crypto_aead_set_flags(aead, crypto_ahash_get_flags(ghash) & |
| 143 | CRYPTO_TFM_RES_MASK); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 144 | |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 145 | out: |
| 146 | kfree(data); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 147 | return err; |
| 148 | } |
| 149 | |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 150 | static int crypto_gcm_setauthsize(struct crypto_aead *tfm, |
| 151 | unsigned int authsize) |
| 152 | { |
| 153 | switch (authsize) { |
| 154 | case 4: |
| 155 | case 8: |
| 156 | case 12: |
| 157 | case 13: |
| 158 | case 14: |
| 159 | case 15: |
| 160 | case 16: |
| 161 | break; |
| 162 | default: |
| 163 | return -EINVAL; |
| 164 | } |
| 165 | |
| 166 | return 0; |
| 167 | } |
| 168 | |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 169 | static void crypto_gcm_init_crypt(struct ablkcipher_request *ablk_req, |
| 170 | struct aead_request *req, |
| 171 | unsigned int cryptlen) |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 172 | { |
| 173 | struct crypto_aead *aead = crypto_aead_reqtfm(req); |
| 174 | struct crypto_gcm_ctx *ctx = crypto_aead_ctx(aead); |
Herbert Xu | 2589469 | 2007-12-12 19:16:38 +0800 | [diff] [blame] | 175 | struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req); |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 176 | struct scatterlist *dst; |
| 177 | __be32 counter = cpu_to_be32(1); |
| 178 | |
| 179 | memset(pctx->auth_tag, 0, sizeof(pctx->auth_tag)); |
| 180 | memcpy(req->iv + 12, &counter, 4); |
| 181 | |
| 182 | sg_init_table(pctx->src, 2); |
| 183 | sg_set_buf(pctx->src, pctx->auth_tag, sizeof(pctx->auth_tag)); |
| 184 | scatterwalk_sg_chain(pctx->src, 2, req->src); |
| 185 | |
| 186 | dst = pctx->src; |
| 187 | if (req->src != req->dst) { |
| 188 | sg_init_table(pctx->dst, 2); |
| 189 | sg_set_buf(pctx->dst, pctx->auth_tag, sizeof(pctx->auth_tag)); |
| 190 | scatterwalk_sg_chain(pctx->dst, 2, req->dst); |
| 191 | dst = pctx->dst; |
| 192 | } |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 193 | |
| 194 | ablkcipher_request_set_tfm(ablk_req, ctx->ctr); |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 195 | ablkcipher_request_set_crypt(ablk_req, pctx->src, dst, |
| 196 | cryptlen + sizeof(pctx->auth_tag), |
| 197 | req->iv); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 198 | } |
| 199 | |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 200 | static inline unsigned int gcm_remain(unsigned int len) |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 201 | { |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 202 | len &= 0xfU; |
| 203 | return len ? 16 - len : 0; |
| 204 | } |
| 205 | |
| 206 | static void gcm_hash_len_done(struct crypto_async_request *areq, int err); |
| 207 | static void gcm_hash_final_done(struct crypto_async_request *areq, int err); |
| 208 | |
| 209 | static int gcm_hash_update(struct aead_request *req, |
| 210 | struct crypto_gcm_req_priv_ctx *pctx, |
| 211 | crypto_completion_t complete, |
| 212 | struct scatterlist *src, |
| 213 | unsigned int len) |
| 214 | { |
| 215 | struct ahash_request *ahreq = &pctx->u.ahreq; |
| 216 | |
| 217 | ahash_request_set_callback(ahreq, aead_request_flags(req), |
| 218 | complete, req); |
| 219 | ahash_request_set_crypt(ahreq, src, NULL, len); |
| 220 | |
| 221 | return crypto_ahash_update(ahreq); |
| 222 | } |
| 223 | |
| 224 | static int gcm_hash_remain(struct aead_request *req, |
| 225 | struct crypto_gcm_req_priv_ctx *pctx, |
| 226 | unsigned int remain, |
| 227 | crypto_completion_t complete) |
| 228 | { |
| 229 | struct ahash_request *ahreq = &pctx->u.ahreq; |
| 230 | |
| 231 | ahash_request_set_callback(ahreq, aead_request_flags(req), |
| 232 | complete, req); |
| 233 | sg_init_one(pctx->src, gcm_zeroes, remain); |
| 234 | ahash_request_set_crypt(ahreq, pctx->src, NULL, remain); |
| 235 | |
| 236 | return crypto_ahash_update(ahreq); |
| 237 | } |
| 238 | |
| 239 | static int gcm_hash_len(struct aead_request *req, |
| 240 | struct crypto_gcm_req_priv_ctx *pctx) |
| 241 | { |
| 242 | struct ahash_request *ahreq = &pctx->u.ahreq; |
| 243 | struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx; |
| 244 | u128 lengths; |
| 245 | |
| 246 | lengths.a = cpu_to_be64(req->assoclen * 8); |
| 247 | lengths.b = cpu_to_be64(gctx->cryptlen * 8); |
| 248 | memcpy(pctx->iauth_tag, &lengths, 16); |
| 249 | sg_init_one(pctx->src, pctx->iauth_tag, 16); |
| 250 | ahash_request_set_callback(ahreq, aead_request_flags(req), |
| 251 | gcm_hash_len_done, req); |
| 252 | ahash_request_set_crypt(ahreq, pctx->src, |
| 253 | NULL, sizeof(lengths)); |
| 254 | |
| 255 | return crypto_ahash_update(ahreq); |
| 256 | } |
| 257 | |
| 258 | static int gcm_hash_final(struct aead_request *req, |
| 259 | struct crypto_gcm_req_priv_ctx *pctx) |
| 260 | { |
| 261 | struct ahash_request *ahreq = &pctx->u.ahreq; |
| 262 | |
| 263 | ahash_request_set_callback(ahreq, aead_request_flags(req), |
| 264 | gcm_hash_final_done, req); |
| 265 | ahash_request_set_crypt(ahreq, NULL, pctx->iauth_tag, 0); |
| 266 | |
| 267 | return crypto_ahash_final(ahreq); |
| 268 | } |
| 269 | |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 270 | static void __gcm_hash_final_done(struct aead_request *req, int err) |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 271 | { |
Herbert Xu | 2589469 | 2007-12-12 19:16:38 +0800 | [diff] [blame] | 272 | struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 273 | struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 274 | |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 275 | if (!err) |
| 276 | crypto_xor(pctx->auth_tag, pctx->iauth_tag, 16); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 277 | |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 278 | gctx->complete(req, err); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 279 | } |
| 280 | |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 281 | 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] | 282 | { |
| 283 | struct aead_request *req = areq->data; |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 284 | |
| 285 | __gcm_hash_final_done(req, err); |
| 286 | } |
| 287 | |
| 288 | static void __gcm_hash_len_done(struct aead_request *req, int err) |
| 289 | { |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 290 | struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req); |
| 291 | |
| 292 | if (!err) { |
| 293 | err = gcm_hash_final(req, pctx); |
| 294 | if (err == -EINPROGRESS || err == -EBUSY) |
| 295 | return; |
| 296 | } |
| 297 | |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 298 | __gcm_hash_final_done(req, err); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 299 | } |
| 300 | |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 301 | 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] | 302 | { |
| 303 | struct aead_request *req = areq->data; |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 304 | |
| 305 | __gcm_hash_len_done(req, err); |
| 306 | } |
| 307 | |
| 308 | static void __gcm_hash_crypt_remain_done(struct aead_request *req, int err) |
| 309 | { |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 310 | struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req); |
| 311 | |
| 312 | if (!err) { |
| 313 | err = gcm_hash_len(req, pctx); |
| 314 | if (err == -EINPROGRESS || err == -EBUSY) |
| 315 | return; |
| 316 | } |
| 317 | |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 318 | __gcm_hash_len_done(req, err); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 319 | } |
| 320 | |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 321 | static void gcm_hash_crypt_remain_done(struct crypto_async_request *areq, |
| 322 | int err) |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 323 | { |
| 324 | struct aead_request *req = areq->data; |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 325 | |
| 326 | __gcm_hash_crypt_remain_done(req, err); |
| 327 | } |
| 328 | |
| 329 | static void __gcm_hash_crypt_done(struct aead_request *req, int err) |
| 330 | { |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 331 | struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req); |
| 332 | struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx; |
| 333 | unsigned int remain; |
| 334 | |
| 335 | if (!err) { |
| 336 | remain = gcm_remain(gctx->cryptlen); |
| 337 | BUG_ON(!remain); |
| 338 | err = gcm_hash_remain(req, pctx, remain, |
| 339 | gcm_hash_crypt_remain_done); |
| 340 | if (err == -EINPROGRESS || err == -EBUSY) |
| 341 | return; |
| 342 | } |
| 343 | |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 344 | __gcm_hash_crypt_remain_done(req, err); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 345 | } |
| 346 | |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 347 | 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] | 348 | { |
| 349 | struct aead_request *req = areq->data; |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 350 | |
| 351 | __gcm_hash_crypt_done(req, err); |
| 352 | } |
| 353 | |
| 354 | static void __gcm_hash_assoc_remain_done(struct aead_request *req, int err) |
| 355 | { |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 356 | struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req); |
| 357 | struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx; |
| 358 | crypto_completion_t complete; |
| 359 | unsigned int remain = 0; |
| 360 | |
| 361 | if (!err && gctx->cryptlen) { |
| 362 | remain = gcm_remain(gctx->cryptlen); |
| 363 | complete = remain ? gcm_hash_crypt_done : |
| 364 | gcm_hash_crypt_remain_done; |
| 365 | err = gcm_hash_update(req, pctx, complete, |
| 366 | gctx->src, gctx->cryptlen); |
| 367 | if (err == -EINPROGRESS || err == -EBUSY) |
| 368 | return; |
| 369 | } |
| 370 | |
| 371 | if (remain) |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 372 | __gcm_hash_crypt_done(req, err); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 373 | else |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 374 | __gcm_hash_crypt_remain_done(req, err); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 375 | } |
| 376 | |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 377 | static void gcm_hash_assoc_remain_done(struct crypto_async_request *areq, |
| 378 | int err) |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 379 | { |
| 380 | struct aead_request *req = areq->data; |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 381 | |
| 382 | __gcm_hash_assoc_remain_done(req, err); |
| 383 | } |
| 384 | |
| 385 | static void __gcm_hash_assoc_done(struct aead_request *req, int err) |
| 386 | { |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 387 | struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req); |
| 388 | unsigned int remain; |
| 389 | |
| 390 | if (!err) { |
| 391 | remain = gcm_remain(req->assoclen); |
| 392 | BUG_ON(!remain); |
| 393 | err = gcm_hash_remain(req, pctx, remain, |
| 394 | gcm_hash_assoc_remain_done); |
| 395 | if (err == -EINPROGRESS || err == -EBUSY) |
| 396 | return; |
| 397 | } |
| 398 | |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 399 | __gcm_hash_assoc_remain_done(req, err); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 400 | } |
| 401 | |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 402 | 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] | 403 | { |
| 404 | struct aead_request *req = areq->data; |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 405 | |
| 406 | __gcm_hash_assoc_done(req, err); |
| 407 | } |
| 408 | |
| 409 | static void __gcm_hash_init_done(struct aead_request *req, int err) |
| 410 | { |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 411 | struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req); |
| 412 | crypto_completion_t complete; |
| 413 | unsigned int remain = 0; |
| 414 | |
| 415 | if (!err && req->assoclen) { |
| 416 | remain = gcm_remain(req->assoclen); |
| 417 | complete = remain ? gcm_hash_assoc_done : |
| 418 | gcm_hash_assoc_remain_done; |
| 419 | err = gcm_hash_update(req, pctx, complete, |
| 420 | req->assoc, req->assoclen); |
| 421 | if (err == -EINPROGRESS || err == -EBUSY) |
| 422 | return; |
| 423 | } |
| 424 | |
| 425 | if (remain) |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 426 | __gcm_hash_assoc_done(req, err); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 427 | else |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 428 | __gcm_hash_assoc_remain_done(req, err); |
| 429 | } |
| 430 | |
| 431 | static void gcm_hash_init_done(struct crypto_async_request *areq, int err) |
| 432 | { |
| 433 | struct aead_request *req = areq->data; |
| 434 | |
| 435 | __gcm_hash_init_done(req, err); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | static int gcm_hash(struct aead_request *req, |
| 439 | struct crypto_gcm_req_priv_ctx *pctx) |
| 440 | { |
| 441 | struct ahash_request *ahreq = &pctx->u.ahreq; |
| 442 | struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx; |
| 443 | struct crypto_gcm_ctx *ctx = crypto_tfm_ctx(req->base.tfm); |
| 444 | unsigned int remain; |
| 445 | crypto_completion_t complete; |
| 446 | int err; |
| 447 | |
| 448 | ahash_request_set_tfm(ahreq, ctx->ghash); |
| 449 | |
| 450 | ahash_request_set_callback(ahreq, aead_request_flags(req), |
| 451 | gcm_hash_init_done, req); |
| 452 | err = crypto_ahash_init(ahreq); |
| 453 | if (err) |
| 454 | return err; |
| 455 | remain = gcm_remain(req->assoclen); |
| 456 | complete = remain ? gcm_hash_assoc_done : gcm_hash_assoc_remain_done; |
| 457 | err = gcm_hash_update(req, pctx, complete, req->assoc, req->assoclen); |
| 458 | if (err) |
| 459 | return err; |
| 460 | if (remain) { |
| 461 | err = gcm_hash_remain(req, pctx, remain, |
| 462 | gcm_hash_assoc_remain_done); |
| 463 | if (err) |
| 464 | return err; |
| 465 | } |
| 466 | remain = gcm_remain(gctx->cryptlen); |
| 467 | complete = remain ? gcm_hash_crypt_done : gcm_hash_crypt_remain_done; |
| 468 | err = gcm_hash_update(req, pctx, complete, gctx->src, gctx->cryptlen); |
| 469 | if (err) |
| 470 | return err; |
| 471 | if (remain) { |
| 472 | err = gcm_hash_remain(req, pctx, remain, |
| 473 | gcm_hash_crypt_remain_done); |
| 474 | if (err) |
| 475 | return err; |
| 476 | } |
| 477 | err = gcm_hash_len(req, pctx); |
| 478 | if (err) |
| 479 | return err; |
| 480 | err = gcm_hash_final(req, pctx); |
| 481 | if (err) |
| 482 | return err; |
| 483 | |
Herbert Xu | 6160b28 | 2007-12-04 19:17:50 +1100 | [diff] [blame] | 484 | return 0; |
| 485 | } |
| 486 | |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 487 | static void gcm_enc_copy_hash(struct aead_request *req, |
| 488 | struct crypto_gcm_req_priv_ctx *pctx) |
| 489 | { |
| 490 | struct crypto_aead *aead = crypto_aead_reqtfm(req); |
| 491 | u8 *auth_tag = pctx->auth_tag; |
| 492 | |
| 493 | scatterwalk_map_and_copy(auth_tag, req->dst, req->cryptlen, |
| 494 | crypto_aead_authsize(aead), 1); |
| 495 | } |
| 496 | |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 497 | static void gcm_enc_hash_done(struct aead_request *req, int err) |
Herbert Xu | 6160b28 | 2007-12-04 19:17:50 +1100 | [diff] [blame] | 498 | { |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 499 | struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req); |
Herbert Xu | 6160b28 | 2007-12-04 19:17:50 +1100 | [diff] [blame] | 500 | |
| 501 | if (!err) |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 502 | gcm_enc_copy_hash(req, pctx); |
Herbert Xu | 6160b28 | 2007-12-04 19:17:50 +1100 | [diff] [blame] | 503 | |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 504 | aead_request_complete(req, err); |
| 505 | } |
| 506 | |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 507 | static void gcm_encrypt_done(struct crypto_async_request *areq, int err) |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 508 | { |
| 509 | struct aead_request *req = areq->data; |
| 510 | struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req); |
| 511 | |
| 512 | if (!err) { |
| 513 | err = gcm_hash(req, pctx); |
| 514 | if (err == -EINPROGRESS || err == -EBUSY) |
| 515 | return; |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 516 | else if (!err) { |
| 517 | crypto_xor(pctx->auth_tag, pctx->iauth_tag, 16); |
| 518 | gcm_enc_copy_hash(req, pctx); |
| 519 | } |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 520 | } |
| 521 | |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 522 | aead_request_complete(req, err); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 523 | } |
| 524 | |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 525 | static int crypto_gcm_encrypt(struct aead_request *req) |
| 526 | { |
Herbert Xu | 2589469 | 2007-12-12 19:16:38 +0800 | [diff] [blame] | 527 | struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 528 | struct ablkcipher_request *abreq = &pctx->u.abreq; |
| 529 | struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx; |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 530 | int err; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 531 | |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 532 | crypto_gcm_init_crypt(abreq, req, req->cryptlen); |
| 533 | ablkcipher_request_set_callback(abreq, aead_request_flags(req), |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 534 | gcm_encrypt_done, req); |
| 535 | |
| 536 | gctx->src = req->dst; |
| 537 | gctx->cryptlen = req->cryptlen; |
| 538 | gctx->complete = gcm_enc_hash_done; |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 539 | |
| 540 | err = crypto_ablkcipher_encrypt(abreq); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 541 | if (err) |
| 542 | return err; |
| 543 | |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 544 | err = gcm_hash(req, pctx); |
| 545 | if (err) |
| 546 | return err; |
| 547 | |
| 548 | crypto_xor(pctx->auth_tag, pctx->iauth_tag, 16); |
| 549 | gcm_enc_copy_hash(req, pctx); |
| 550 | |
| 551 | return 0; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 552 | } |
| 553 | |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 554 | static int crypto_gcm_verify(struct aead_request *req, |
| 555 | struct crypto_gcm_req_priv_ctx *pctx) |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 556 | { |
| 557 | struct crypto_aead *aead = crypto_aead_reqtfm(req); |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 558 | u8 *auth_tag = pctx->auth_tag; |
| 559 | u8 *iauth_tag = pctx->iauth_tag; |
| 560 | unsigned int authsize = crypto_aead_authsize(aead); |
| 561 | unsigned int cryptlen = req->cryptlen - authsize; |
| 562 | |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 563 | crypto_xor(auth_tag, iauth_tag, 16); |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 564 | scatterwalk_map_and_copy(iauth_tag, req->src, cryptlen, authsize, 0); |
| 565 | return memcmp(iauth_tag, auth_tag, authsize) ? -EBADMSG : 0; |
| 566 | } |
| 567 | |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 568 | static void gcm_decrypt_done(struct crypto_async_request *areq, int err) |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 569 | { |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 570 | struct aead_request *req = areq->data; |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 571 | struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req); |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 572 | |
| 573 | if (!err) |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 574 | err = crypto_gcm_verify(req, pctx); |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 575 | |
| 576 | aead_request_complete(req, err); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 577 | } |
| 578 | |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 579 | static void gcm_dec_hash_done(struct aead_request *req, int err) |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 580 | { |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 581 | struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req); |
| 582 | struct ablkcipher_request *abreq = &pctx->u.abreq; |
| 583 | struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx; |
| 584 | |
| 585 | if (!err) { |
| 586 | ablkcipher_request_set_callback(abreq, aead_request_flags(req), |
| 587 | gcm_decrypt_done, req); |
| 588 | crypto_gcm_init_crypt(abreq, req, gctx->cryptlen); |
| 589 | err = crypto_ablkcipher_decrypt(abreq); |
| 590 | if (err == -EINPROGRESS || err == -EBUSY) |
| 591 | return; |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 592 | else if (!err) |
| 593 | err = crypto_gcm_verify(req, pctx); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 594 | } |
| 595 | |
Huang Ying | 62c5593 | 2009-11-16 21:52:22 +0800 | [diff] [blame] | 596 | aead_request_complete(req, err); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 597 | } |
| 598 | |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 599 | static int crypto_gcm_decrypt(struct aead_request *req) |
| 600 | { |
Herbert Xu | 6160b28 | 2007-12-04 19:17:50 +1100 | [diff] [blame] | 601 | struct crypto_aead *aead = crypto_aead_reqtfm(req); |
Herbert Xu | 2589469 | 2007-12-12 19:16:38 +0800 | [diff] [blame] | 602 | struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 603 | struct ablkcipher_request *abreq = &pctx->u.abreq; |
| 604 | struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx; |
Herbert Xu | 6160b28 | 2007-12-04 19:17:50 +1100 | [diff] [blame] | 605 | unsigned int authsize = crypto_aead_authsize(aead); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 606 | unsigned int cryptlen = req->cryptlen; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 607 | int err; |
| 608 | |
Herbert Xu | 6160b28 | 2007-12-04 19:17:50 +1100 | [diff] [blame] | 609 | if (cryptlen < authsize) |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 610 | return -EINVAL; |
Herbert Xu | 6160b28 | 2007-12-04 19:17:50 +1100 | [diff] [blame] | 611 | cryptlen -= authsize; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 612 | |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 613 | gctx->src = req->src; |
| 614 | gctx->cryptlen = cryptlen; |
| 615 | gctx->complete = gcm_dec_hash_done; |
| 616 | |
| 617 | err = gcm_hash(req, pctx); |
| 618 | if (err) |
| 619 | return err; |
| 620 | |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 621 | ablkcipher_request_set_callback(abreq, aead_request_flags(req), |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 622 | gcm_decrypt_done, req); |
| 623 | crypto_gcm_init_crypt(abreq, req, cryptlen); |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 624 | err = crypto_ablkcipher_decrypt(abreq); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 625 | if (err) |
| 626 | return err; |
| 627 | |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 628 | return crypto_gcm_verify(req, pctx); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | static int crypto_gcm_init_tfm(struct crypto_tfm *tfm) |
| 632 | { |
| 633 | struct crypto_instance *inst = (void *)tfm->__crt_alg; |
| 634 | struct gcm_instance_ctx *ictx = crypto_instance_ctx(inst); |
| 635 | struct crypto_gcm_ctx *ctx = crypto_tfm_ctx(tfm); |
| 636 | struct crypto_ablkcipher *ctr; |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 637 | struct crypto_ahash *ghash; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 638 | unsigned long align; |
| 639 | int err; |
| 640 | |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 641 | ghash = crypto_spawn_ahash(&ictx->ghash); |
| 642 | if (IS_ERR(ghash)) |
| 643 | return PTR_ERR(ghash); |
| 644 | |
Herbert Xu | 1472e5e | 2007-12-07 19:26:11 +0800 | [diff] [blame] | 645 | ctr = crypto_spawn_skcipher(&ictx->ctr); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 646 | err = PTR_ERR(ctr); |
| 647 | if (IS_ERR(ctr)) |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 648 | goto err_free_hash; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 649 | |
| 650 | ctx->ctr = ctr; |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 651 | ctx->ghash = ghash; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 652 | |
Herbert Xu | 2589469 | 2007-12-12 19:16:38 +0800 | [diff] [blame] | 653 | align = crypto_tfm_alg_alignmask(tfm); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 654 | align &= ~(crypto_tfm_ctx_alignment() - 1); |
Herbert Xu | 7f68137 | 2007-12-06 14:59:53 +0800 | [diff] [blame] | 655 | tfm->crt_aead.reqsize = align + |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 656 | offsetof(struct crypto_gcm_req_priv_ctx, u) + |
| 657 | max(sizeof(struct ablkcipher_request) + |
| 658 | crypto_ablkcipher_reqsize(ctr), |
| 659 | sizeof(struct ahash_request) + |
| 660 | crypto_ahash_reqsize(ghash)); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 661 | |
| 662 | return 0; |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 663 | |
| 664 | err_free_hash: |
| 665 | crypto_free_ahash(ghash); |
| 666 | return err; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | static void crypto_gcm_exit_tfm(struct crypto_tfm *tfm) |
| 670 | { |
| 671 | struct crypto_gcm_ctx *ctx = crypto_tfm_ctx(tfm); |
| 672 | |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 673 | crypto_free_ahash(ctx->ghash); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 674 | crypto_free_ablkcipher(ctx->ctr); |
| 675 | } |
| 676 | |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 677 | static struct crypto_instance *crypto_gcm_alloc_common(struct rtattr **tb, |
| 678 | const char *full_name, |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 679 | const char *ctr_name, |
| 680 | const char *ghash_name) |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 681 | { |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 682 | struct crypto_attr_type *algt; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 683 | struct crypto_instance *inst; |
| 684 | struct crypto_alg *ctr; |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 685 | struct crypto_alg *ghash_alg; |
| 686 | struct ahash_alg *ghash_ahash_alg; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 687 | struct gcm_instance_ctx *ctx; |
| 688 | int err; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 689 | |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 690 | algt = crypto_get_attr_type(tb); |
| 691 | err = PTR_ERR(algt); |
| 692 | if (IS_ERR(algt)) |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 693 | return ERR_PTR(err); |
| 694 | |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 695 | if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask) |
| 696 | return ERR_PTR(-EINVAL); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 697 | |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 698 | ghash_alg = crypto_find_alg(ghash_name, &crypto_ahash_type, |
| 699 | CRYPTO_ALG_TYPE_HASH, |
| 700 | CRYPTO_ALG_TYPE_AHASH_MASK); |
| 701 | err = PTR_ERR(ghash_alg); |
| 702 | if (IS_ERR(ghash_alg)) |
| 703 | return ERR_PTR(err); |
| 704 | |
| 705 | err = -ENOMEM; |
Herbert Xu | 1472e5e | 2007-12-07 19:26:11 +0800 | [diff] [blame] | 706 | inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL); |
| 707 | if (!inst) |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 708 | goto out_put_ghash; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 709 | |
Herbert Xu | 1472e5e | 2007-12-07 19:26:11 +0800 | [diff] [blame] | 710 | ctx = crypto_instance_ctx(inst); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 711 | ghash_ahash_alg = container_of(ghash_alg, struct ahash_alg, halg.base); |
| 712 | err = crypto_init_ahash_spawn(&ctx->ghash, &ghash_ahash_alg->halg, |
| 713 | inst); |
| 714 | if (err) |
| 715 | goto err_free_inst; |
| 716 | |
Herbert Xu | 1472e5e | 2007-12-07 19:26:11 +0800 | [diff] [blame] | 717 | crypto_set_skcipher_spawn(&ctx->ctr, inst); |
| 718 | err = crypto_grab_skcipher(&ctx->ctr, ctr_name, 0, |
| 719 | crypto_requires_sync(algt->type, |
| 720 | algt->mask)); |
| 721 | if (err) |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 722 | goto err_drop_ghash; |
Herbert Xu | 1472e5e | 2007-12-07 19:26:11 +0800 | [diff] [blame] | 723 | |
| 724 | ctr = crypto_skcipher_spawn_alg(&ctx->ctr); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 725 | |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 726 | /* We only support 16-byte blocks. */ |
Herbert Xu | 1472e5e | 2007-12-07 19:26:11 +0800 | [diff] [blame] | 727 | if (ctr->cra_ablkcipher.ivsize != 16) |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 728 | goto out_put_ctr; |
| 729 | |
| 730 | /* Not a stream cipher? */ |
| 731 | err = -EINVAL; |
| 732 | if (ctr->cra_blocksize != 1) |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 733 | goto out_put_ctr; |
| 734 | |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 735 | err = -ENAMETOOLONG; |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 736 | if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME, |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 737 | "gcm_base(%s,%s)", ctr->cra_driver_name, |
| 738 | ghash_alg->cra_driver_name) >= |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 739 | CRYPTO_MAX_ALG_NAME) |
Herbert Xu | 1472e5e | 2007-12-07 19:26:11 +0800 | [diff] [blame] | 740 | goto out_put_ctr; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 741 | |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 742 | memcpy(inst->alg.cra_name, full_name, CRYPTO_MAX_ALG_NAME); |
| 743 | |
Herbert Xu | 1472e5e | 2007-12-07 19:26:11 +0800 | [diff] [blame] | 744 | inst->alg.cra_flags = CRYPTO_ALG_TYPE_AEAD; |
| 745 | inst->alg.cra_flags |= ctr->cra_flags & CRYPTO_ALG_ASYNC; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 746 | inst->alg.cra_priority = ctr->cra_priority; |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 747 | inst->alg.cra_blocksize = 1; |
Herbert Xu | 2589469 | 2007-12-12 19:16:38 +0800 | [diff] [blame] | 748 | inst->alg.cra_alignmask = ctr->cra_alignmask | (__alignof__(u64) - 1); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 749 | inst->alg.cra_type = &crypto_aead_type; |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 750 | inst->alg.cra_aead.ivsize = 16; |
Herbert Xu | 7ba683a | 2007-12-02 18:49:21 +1100 | [diff] [blame] | 751 | inst->alg.cra_aead.maxauthsize = 16; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 752 | inst->alg.cra_ctxsize = sizeof(struct crypto_gcm_ctx); |
| 753 | inst->alg.cra_init = crypto_gcm_init_tfm; |
| 754 | inst->alg.cra_exit = crypto_gcm_exit_tfm; |
| 755 | inst->alg.cra_aead.setkey = crypto_gcm_setkey; |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 756 | inst->alg.cra_aead.setauthsize = crypto_gcm_setauthsize; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 757 | inst->alg.cra_aead.encrypt = crypto_gcm_encrypt; |
| 758 | inst->alg.cra_aead.decrypt = crypto_gcm_decrypt; |
| 759 | |
| 760 | out: |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 761 | crypto_mod_put(ghash_alg); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 762 | return inst; |
Herbert Xu | 1472e5e | 2007-12-07 19:26:11 +0800 | [diff] [blame] | 763 | |
| 764 | out_put_ctr: |
| 765 | crypto_drop_skcipher(&ctx->ctr); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 766 | err_drop_ghash: |
| 767 | crypto_drop_ahash(&ctx->ghash); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 768 | err_free_inst: |
| 769 | kfree(inst); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 770 | out_put_ghash: |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 771 | inst = ERR_PTR(err); |
| 772 | goto out; |
| 773 | } |
| 774 | |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 775 | static struct crypto_instance *crypto_gcm_alloc(struct rtattr **tb) |
| 776 | { |
| 777 | int err; |
| 778 | const char *cipher_name; |
| 779 | char ctr_name[CRYPTO_MAX_ALG_NAME]; |
| 780 | char full_name[CRYPTO_MAX_ALG_NAME]; |
| 781 | |
| 782 | cipher_name = crypto_attr_alg_name(tb[1]); |
| 783 | err = PTR_ERR(cipher_name); |
| 784 | if (IS_ERR(cipher_name)) |
| 785 | return ERR_PTR(err); |
| 786 | |
| 787 | if (snprintf(ctr_name, CRYPTO_MAX_ALG_NAME, "ctr(%s)", cipher_name) >= |
| 788 | CRYPTO_MAX_ALG_NAME) |
| 789 | return ERR_PTR(-ENAMETOOLONG); |
| 790 | |
| 791 | if (snprintf(full_name, CRYPTO_MAX_ALG_NAME, "gcm(%s)", cipher_name) >= |
| 792 | CRYPTO_MAX_ALG_NAME) |
| 793 | return ERR_PTR(-ENAMETOOLONG); |
| 794 | |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 795 | return crypto_gcm_alloc_common(tb, full_name, ctr_name, "ghash"); |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 796 | } |
| 797 | |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 798 | static void crypto_gcm_free(struct crypto_instance *inst) |
| 799 | { |
| 800 | struct gcm_instance_ctx *ctx = crypto_instance_ctx(inst); |
| 801 | |
Herbert Xu | 1472e5e | 2007-12-07 19:26:11 +0800 | [diff] [blame] | 802 | crypto_drop_skcipher(&ctx->ctr); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 803 | crypto_drop_ahash(&ctx->ghash); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 804 | kfree(inst); |
| 805 | } |
| 806 | |
| 807 | static struct crypto_template crypto_gcm_tmpl = { |
| 808 | .name = "gcm", |
| 809 | .alloc = crypto_gcm_alloc, |
| 810 | .free = crypto_gcm_free, |
| 811 | .module = THIS_MODULE, |
| 812 | }; |
| 813 | |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 814 | static struct crypto_instance *crypto_gcm_base_alloc(struct rtattr **tb) |
| 815 | { |
| 816 | int err; |
| 817 | const char *ctr_name; |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 818 | const char *ghash_name; |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 819 | char full_name[CRYPTO_MAX_ALG_NAME]; |
| 820 | |
| 821 | ctr_name = crypto_attr_alg_name(tb[1]); |
| 822 | err = PTR_ERR(ctr_name); |
| 823 | if (IS_ERR(ctr_name)) |
| 824 | return ERR_PTR(err); |
| 825 | |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 826 | ghash_name = crypto_attr_alg_name(tb[2]); |
| 827 | err = PTR_ERR(ghash_name); |
| 828 | if (IS_ERR(ghash_name)) |
| 829 | return ERR_PTR(err); |
| 830 | |
| 831 | if (snprintf(full_name, CRYPTO_MAX_ALG_NAME, "gcm_base(%s,%s)", |
| 832 | ctr_name, ghash_name) >= CRYPTO_MAX_ALG_NAME) |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 833 | return ERR_PTR(-ENAMETOOLONG); |
| 834 | |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 835 | return crypto_gcm_alloc_common(tb, full_name, ctr_name, ghash_name); |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | static struct crypto_template crypto_gcm_base_tmpl = { |
| 839 | .name = "gcm_base", |
| 840 | .alloc = crypto_gcm_base_alloc, |
| 841 | .free = crypto_gcm_free, |
| 842 | .module = THIS_MODULE, |
| 843 | }; |
| 844 | |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 845 | static int crypto_rfc4106_setkey(struct crypto_aead *parent, const u8 *key, |
| 846 | unsigned int keylen) |
| 847 | { |
| 848 | struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(parent); |
| 849 | struct crypto_aead *child = ctx->child; |
| 850 | int err; |
| 851 | |
| 852 | if (keylen < 4) |
| 853 | return -EINVAL; |
| 854 | |
| 855 | keylen -= 4; |
| 856 | memcpy(ctx->nonce, key + keylen, 4); |
| 857 | |
| 858 | crypto_aead_clear_flags(child, CRYPTO_TFM_REQ_MASK); |
| 859 | crypto_aead_set_flags(child, crypto_aead_get_flags(parent) & |
| 860 | CRYPTO_TFM_REQ_MASK); |
| 861 | err = crypto_aead_setkey(child, key, keylen); |
| 862 | crypto_aead_set_flags(parent, crypto_aead_get_flags(child) & |
| 863 | CRYPTO_TFM_RES_MASK); |
| 864 | |
| 865 | return err; |
| 866 | } |
| 867 | |
| 868 | static int crypto_rfc4106_setauthsize(struct crypto_aead *parent, |
| 869 | unsigned int authsize) |
| 870 | { |
| 871 | struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(parent); |
| 872 | |
| 873 | switch (authsize) { |
| 874 | case 8: |
| 875 | case 12: |
| 876 | case 16: |
| 877 | break; |
| 878 | default: |
| 879 | return -EINVAL; |
| 880 | } |
| 881 | |
| 882 | return crypto_aead_setauthsize(ctx->child, authsize); |
| 883 | } |
| 884 | |
| 885 | static struct aead_request *crypto_rfc4106_crypt(struct aead_request *req) |
| 886 | { |
| 887 | struct aead_request *subreq = aead_request_ctx(req); |
| 888 | struct crypto_aead *aead = crypto_aead_reqtfm(req); |
| 889 | struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(aead); |
| 890 | struct crypto_aead *child = ctx->child; |
| 891 | u8 *iv = PTR_ALIGN((u8 *)(subreq + 1) + crypto_aead_reqsize(child), |
| 892 | crypto_aead_alignmask(child) + 1); |
| 893 | |
| 894 | memcpy(iv, ctx->nonce, 4); |
| 895 | memcpy(iv + 4, req->iv, 8); |
| 896 | |
| 897 | aead_request_set_tfm(subreq, child); |
| 898 | aead_request_set_callback(subreq, req->base.flags, req->base.complete, |
| 899 | req->base.data); |
| 900 | aead_request_set_crypt(subreq, req->src, req->dst, req->cryptlen, iv); |
| 901 | aead_request_set_assoc(subreq, req->assoc, req->assoclen); |
| 902 | |
| 903 | return subreq; |
| 904 | } |
| 905 | |
| 906 | static int crypto_rfc4106_encrypt(struct aead_request *req) |
| 907 | { |
| 908 | req = crypto_rfc4106_crypt(req); |
| 909 | |
| 910 | return crypto_aead_encrypt(req); |
| 911 | } |
| 912 | |
| 913 | static int crypto_rfc4106_decrypt(struct aead_request *req) |
| 914 | { |
| 915 | req = crypto_rfc4106_crypt(req); |
| 916 | |
| 917 | return crypto_aead_decrypt(req); |
| 918 | } |
| 919 | |
| 920 | static int crypto_rfc4106_init_tfm(struct crypto_tfm *tfm) |
| 921 | { |
| 922 | struct crypto_instance *inst = (void *)tfm->__crt_alg; |
| 923 | struct crypto_aead_spawn *spawn = crypto_instance_ctx(inst); |
| 924 | struct crypto_rfc4106_ctx *ctx = crypto_tfm_ctx(tfm); |
| 925 | struct crypto_aead *aead; |
| 926 | unsigned long align; |
| 927 | |
| 928 | aead = crypto_spawn_aead(spawn); |
| 929 | if (IS_ERR(aead)) |
| 930 | return PTR_ERR(aead); |
| 931 | |
| 932 | ctx->child = aead; |
| 933 | |
| 934 | align = crypto_aead_alignmask(aead); |
| 935 | align &= ~(crypto_tfm_ctx_alignment() - 1); |
| 936 | tfm->crt_aead.reqsize = sizeof(struct aead_request) + |
| 937 | ALIGN(crypto_aead_reqsize(aead), |
| 938 | crypto_tfm_ctx_alignment()) + |
| 939 | align + 16; |
| 940 | |
| 941 | return 0; |
| 942 | } |
| 943 | |
| 944 | static void crypto_rfc4106_exit_tfm(struct crypto_tfm *tfm) |
| 945 | { |
| 946 | struct crypto_rfc4106_ctx *ctx = crypto_tfm_ctx(tfm); |
| 947 | |
| 948 | crypto_free_aead(ctx->child); |
| 949 | } |
| 950 | |
| 951 | static struct crypto_instance *crypto_rfc4106_alloc(struct rtattr **tb) |
| 952 | { |
| 953 | struct crypto_attr_type *algt; |
| 954 | struct crypto_instance *inst; |
| 955 | struct crypto_aead_spawn *spawn; |
| 956 | struct crypto_alg *alg; |
| 957 | const char *ccm_name; |
| 958 | int err; |
| 959 | |
| 960 | algt = crypto_get_attr_type(tb); |
| 961 | err = PTR_ERR(algt); |
| 962 | if (IS_ERR(algt)) |
| 963 | return ERR_PTR(err); |
| 964 | |
| 965 | if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask) |
| 966 | return ERR_PTR(-EINVAL); |
| 967 | |
| 968 | ccm_name = crypto_attr_alg_name(tb[1]); |
| 969 | err = PTR_ERR(ccm_name); |
| 970 | if (IS_ERR(ccm_name)) |
| 971 | return ERR_PTR(err); |
| 972 | |
| 973 | inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL); |
| 974 | if (!inst) |
| 975 | return ERR_PTR(-ENOMEM); |
| 976 | |
| 977 | spawn = crypto_instance_ctx(inst); |
| 978 | crypto_set_aead_spawn(spawn, inst); |
| 979 | err = crypto_grab_aead(spawn, ccm_name, 0, |
| 980 | crypto_requires_sync(algt->type, algt->mask)); |
| 981 | if (err) |
| 982 | goto out_free_inst; |
| 983 | |
| 984 | alg = crypto_aead_spawn_alg(spawn); |
| 985 | |
| 986 | err = -EINVAL; |
| 987 | |
| 988 | /* We only support 16-byte blocks. */ |
| 989 | if (alg->cra_aead.ivsize != 16) |
| 990 | goto out_drop_alg; |
| 991 | |
| 992 | /* Not a stream cipher? */ |
| 993 | if (alg->cra_blocksize != 1) |
| 994 | goto out_drop_alg; |
| 995 | |
| 996 | err = -ENAMETOOLONG; |
| 997 | if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME, |
| 998 | "rfc4106(%s)", alg->cra_name) >= CRYPTO_MAX_ALG_NAME || |
| 999 | snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME, |
| 1000 | "rfc4106(%s)", alg->cra_driver_name) >= |
| 1001 | CRYPTO_MAX_ALG_NAME) |
| 1002 | goto out_drop_alg; |
| 1003 | |
| 1004 | inst->alg.cra_flags = CRYPTO_ALG_TYPE_AEAD; |
| 1005 | inst->alg.cra_flags |= alg->cra_flags & CRYPTO_ALG_ASYNC; |
| 1006 | inst->alg.cra_priority = alg->cra_priority; |
| 1007 | inst->alg.cra_blocksize = 1; |
| 1008 | inst->alg.cra_alignmask = alg->cra_alignmask; |
| 1009 | inst->alg.cra_type = &crypto_nivaead_type; |
| 1010 | |
| 1011 | inst->alg.cra_aead.ivsize = 8; |
| 1012 | inst->alg.cra_aead.maxauthsize = 16; |
| 1013 | |
| 1014 | inst->alg.cra_ctxsize = sizeof(struct crypto_rfc4106_ctx); |
| 1015 | |
| 1016 | inst->alg.cra_init = crypto_rfc4106_init_tfm; |
| 1017 | inst->alg.cra_exit = crypto_rfc4106_exit_tfm; |
| 1018 | |
| 1019 | inst->alg.cra_aead.setkey = crypto_rfc4106_setkey; |
| 1020 | inst->alg.cra_aead.setauthsize = crypto_rfc4106_setauthsize; |
| 1021 | inst->alg.cra_aead.encrypt = crypto_rfc4106_encrypt; |
| 1022 | inst->alg.cra_aead.decrypt = crypto_rfc4106_decrypt; |
| 1023 | |
| 1024 | inst->alg.cra_aead.geniv = "seqiv"; |
| 1025 | |
| 1026 | out: |
| 1027 | return inst; |
| 1028 | |
| 1029 | out_drop_alg: |
| 1030 | crypto_drop_aead(spawn); |
| 1031 | out_free_inst: |
| 1032 | kfree(inst); |
| 1033 | inst = ERR_PTR(err); |
| 1034 | goto out; |
| 1035 | } |
| 1036 | |
| 1037 | static void crypto_rfc4106_free(struct crypto_instance *inst) |
| 1038 | { |
| 1039 | crypto_drop_spawn(crypto_instance_ctx(inst)); |
| 1040 | kfree(inst); |
| 1041 | } |
| 1042 | |
| 1043 | static struct crypto_template crypto_rfc4106_tmpl = { |
| 1044 | .name = "rfc4106", |
| 1045 | .alloc = crypto_rfc4106_alloc, |
| 1046 | .free = crypto_rfc4106_free, |
| 1047 | .module = THIS_MODULE, |
| 1048 | }; |
| 1049 | |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 1050 | static int __init crypto_gcm_module_init(void) |
| 1051 | { |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 1052 | int err; |
| 1053 | |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 1054 | gcm_zeroes = kzalloc(16, GFP_KERNEL); |
| 1055 | if (!gcm_zeroes) |
| 1056 | return -ENOMEM; |
| 1057 | |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 1058 | err = crypto_register_template(&crypto_gcm_base_tmpl); |
| 1059 | if (err) |
| 1060 | goto out; |
| 1061 | |
| 1062 | err = crypto_register_template(&crypto_gcm_tmpl); |
| 1063 | if (err) |
| 1064 | goto out_undo_base; |
| 1065 | |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 1066 | err = crypto_register_template(&crypto_rfc4106_tmpl); |
| 1067 | if (err) |
| 1068 | goto out_undo_gcm; |
| 1069 | |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 1070 | return 0; |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 1071 | |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 1072 | out_undo_gcm: |
| 1073 | crypto_unregister_template(&crypto_gcm_tmpl); |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 1074 | out_undo_base: |
| 1075 | crypto_unregister_template(&crypto_gcm_base_tmpl); |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 1076 | out: |
| 1077 | kfree(gcm_zeroes); |
| 1078 | return err; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 1079 | } |
| 1080 | |
| 1081 | static void __exit crypto_gcm_module_exit(void) |
| 1082 | { |
Huang Ying | 9382d97 | 2009-08-06 15:34:26 +1000 | [diff] [blame] | 1083 | kfree(gcm_zeroes); |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 1084 | crypto_unregister_template(&crypto_rfc4106_tmpl); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 1085 | crypto_unregister_template(&crypto_gcm_tmpl); |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 1086 | crypto_unregister_template(&crypto_gcm_base_tmpl); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 1087 | } |
| 1088 | |
| 1089 | module_init(crypto_gcm_module_init); |
| 1090 | module_exit(crypto_gcm_module_exit); |
| 1091 | |
| 1092 | MODULE_LICENSE("GPL"); |
| 1093 | MODULE_DESCRIPTION("Galois/Counter Mode"); |
| 1094 | MODULE_AUTHOR("Mikko Herranen <mh1@iki.fi>"); |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 1095 | MODULE_ALIAS("gcm_base"); |
Herbert Xu | dadbc53 | 2007-12-17 15:33:17 +0800 | [diff] [blame] | 1096 | MODULE_ALIAS("rfc4106"); |