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 | 1472e5e | 2007-12-07 19:26:11 +0800 | [diff] [blame^] | 12 | #include <crypto/internal/skcipher.h> |
Herbert Xu | 42c271c | 2007-12-07 18:52:49 +0800 | [diff] [blame] | 13 | #include <crypto/scatterwalk.h> |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 14 | #include <linux/completion.h> |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 15 | #include <linux/err.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/kernel.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/slab.h> |
| 20 | |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 21 | struct gcm_instance_ctx { |
Herbert Xu | 1472e5e | 2007-12-07 19:26:11 +0800 | [diff] [blame^] | 22 | struct crypto_skcipher_spawn ctr; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 23 | }; |
| 24 | |
| 25 | struct crypto_gcm_ctx { |
| 26 | struct crypto_ablkcipher *ctr; |
| 27 | struct gf128mul_4k *gf128; |
| 28 | }; |
| 29 | |
| 30 | struct crypto_gcm_ghash_ctx { |
| 31 | u32 bytes; |
| 32 | u32 flags; |
| 33 | struct gf128mul_4k *gf128; |
| 34 | u8 buffer[16]; |
| 35 | }; |
| 36 | |
| 37 | struct crypto_gcm_req_priv_ctx { |
| 38 | u8 auth_tag[16]; |
Herbert Xu | 6160b28 | 2007-12-04 19:17:50 +1100 | [diff] [blame] | 39 | u8 iauth_tag[16]; |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 40 | struct scatterlist src[2]; |
| 41 | struct scatterlist dst[2]; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 42 | struct crypto_gcm_ghash_ctx ghash; |
Herbert Xu | 7f68137 | 2007-12-06 14:59:53 +0800 | [diff] [blame] | 43 | struct ablkcipher_request abreq; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 44 | }; |
| 45 | |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 46 | struct crypto_gcm_setkey_result { |
| 47 | int err; |
| 48 | struct completion completion; |
| 49 | }; |
| 50 | |
Herbert Xu | 2589469 | 2007-12-12 19:16:38 +0800 | [diff] [blame] | 51 | static inline struct crypto_gcm_req_priv_ctx *crypto_gcm_reqctx( |
| 52 | struct aead_request *req) |
| 53 | { |
| 54 | unsigned long align = crypto_aead_alignmask(crypto_aead_reqtfm(req)); |
| 55 | |
| 56 | return (void *)PTR_ALIGN((u8 *)aead_request_ctx(req), align + 1); |
| 57 | } |
| 58 | |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 59 | static void crypto_gcm_ghash_init(struct crypto_gcm_ghash_ctx *ctx, u32 flags, |
| 60 | struct gf128mul_4k *gf128) |
| 61 | { |
| 62 | ctx->bytes = 0; |
| 63 | ctx->flags = flags; |
| 64 | ctx->gf128 = gf128; |
| 65 | memset(ctx->buffer, 0, 16); |
| 66 | } |
| 67 | |
| 68 | static void crypto_gcm_ghash_update(struct crypto_gcm_ghash_ctx *ctx, |
| 69 | const u8 *src, unsigned int srclen) |
| 70 | { |
| 71 | u8 *dst = ctx->buffer; |
| 72 | |
| 73 | if (ctx->bytes) { |
| 74 | int n = min(srclen, ctx->bytes); |
| 75 | u8 *pos = dst + (16 - ctx->bytes); |
| 76 | |
| 77 | ctx->bytes -= n; |
| 78 | srclen -= n; |
| 79 | |
| 80 | while (n--) |
| 81 | *pos++ ^= *src++; |
| 82 | |
| 83 | if (!ctx->bytes) |
| 84 | gf128mul_4k_lle((be128 *)dst, ctx->gf128); |
| 85 | } |
| 86 | |
| 87 | while (srclen >= 16) { |
| 88 | crypto_xor(dst, src, 16); |
| 89 | gf128mul_4k_lle((be128 *)dst, ctx->gf128); |
| 90 | src += 16; |
| 91 | srclen -= 16; |
| 92 | } |
| 93 | |
| 94 | if (srclen) { |
| 95 | ctx->bytes = 16 - srclen; |
| 96 | while (srclen--) |
| 97 | *dst++ ^= *src++; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | static void crypto_gcm_ghash_update_sg(struct crypto_gcm_ghash_ctx *ctx, |
| 102 | struct scatterlist *sg, int len) |
| 103 | { |
| 104 | struct scatter_walk walk; |
| 105 | u8 *src; |
| 106 | int n; |
| 107 | |
Herbert Xu | 6160b28 | 2007-12-04 19:17:50 +1100 | [diff] [blame] | 108 | if (!len) |
| 109 | return; |
| 110 | |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 111 | scatterwalk_start(&walk, sg); |
| 112 | |
| 113 | while (len) { |
| 114 | n = scatterwalk_clamp(&walk, len); |
| 115 | |
| 116 | if (!n) { |
Herbert Xu | b2ab4a5 | 2007-12-05 20:59:25 +1100 | [diff] [blame] | 117 | scatterwalk_start(&walk, scatterwalk_sg_next(walk.sg)); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 118 | n = scatterwalk_clamp(&walk, len); |
| 119 | } |
| 120 | |
| 121 | src = scatterwalk_map(&walk, 0); |
| 122 | |
| 123 | crypto_gcm_ghash_update(ctx, src, n); |
| 124 | len -= n; |
| 125 | |
| 126 | scatterwalk_unmap(src, 0); |
| 127 | scatterwalk_advance(&walk, n); |
| 128 | scatterwalk_done(&walk, 0, len); |
| 129 | if (len) |
| 130 | crypto_yield(ctx->flags); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | static void crypto_gcm_ghash_flush(struct crypto_gcm_ghash_ctx *ctx) |
| 135 | { |
| 136 | u8 *dst = ctx->buffer; |
| 137 | |
| 138 | if (ctx->bytes) { |
| 139 | u8 *tmp = dst + (16 - ctx->bytes); |
| 140 | |
| 141 | while (ctx->bytes--) |
| 142 | *tmp++ ^= 0; |
| 143 | |
| 144 | gf128mul_4k_lle((be128 *)dst, ctx->gf128); |
| 145 | } |
| 146 | |
| 147 | ctx->bytes = 0; |
| 148 | } |
| 149 | |
| 150 | static void crypto_gcm_ghash_final_xor(struct crypto_gcm_ghash_ctx *ctx, |
| 151 | unsigned int authlen, |
| 152 | unsigned int cryptlen, u8 *dst) |
| 153 | { |
| 154 | u8 *buf = ctx->buffer; |
| 155 | u128 lengths; |
| 156 | |
| 157 | lengths.a = cpu_to_be64(authlen * 8); |
| 158 | lengths.b = cpu_to_be64(cryptlen * 8); |
| 159 | |
| 160 | crypto_gcm_ghash_flush(ctx); |
| 161 | crypto_xor(buf, (u8 *)&lengths, 16); |
| 162 | gf128mul_4k_lle((be128 *)buf, ctx->gf128); |
| 163 | crypto_xor(dst, buf, 16); |
| 164 | } |
| 165 | |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 166 | 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] | 167 | { |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 168 | struct crypto_gcm_setkey_result *result = req->data; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 169 | |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 170 | if (err == -EINPROGRESS) |
| 171 | return; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 172 | |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 173 | result->err = err; |
| 174 | complete(&result->completion); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | static int crypto_gcm_setkey(struct crypto_aead *aead, const u8 *key, |
| 178 | unsigned int keylen) |
| 179 | { |
| 180 | struct crypto_gcm_ctx *ctx = crypto_aead_ctx(aead); |
| 181 | struct crypto_ablkcipher *ctr = ctx->ctr; |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 182 | struct { |
| 183 | be128 hash; |
| 184 | u8 iv[8]; |
| 185 | |
| 186 | struct crypto_gcm_setkey_result result; |
| 187 | |
| 188 | struct scatterlist sg[1]; |
| 189 | struct ablkcipher_request req; |
| 190 | } *data; |
| 191 | int err; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 192 | |
| 193 | crypto_ablkcipher_clear_flags(ctr, CRYPTO_TFM_REQ_MASK); |
| 194 | crypto_ablkcipher_set_flags(ctr, crypto_aead_get_flags(aead) & |
| 195 | CRYPTO_TFM_REQ_MASK); |
| 196 | |
| 197 | err = crypto_ablkcipher_setkey(ctr, key, keylen); |
| 198 | if (err) |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 199 | return err; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 200 | |
| 201 | crypto_aead_set_flags(aead, crypto_ablkcipher_get_flags(ctr) & |
| 202 | CRYPTO_TFM_RES_MASK); |
| 203 | |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 204 | data = kzalloc(sizeof(*data) + crypto_ablkcipher_reqsize(ctr), |
| 205 | GFP_KERNEL); |
| 206 | if (!data) |
| 207 | return -ENOMEM; |
| 208 | |
| 209 | init_completion(&data->result.completion); |
| 210 | sg_init_one(data->sg, &data->hash, sizeof(data->hash)); |
| 211 | ablkcipher_request_set_tfm(&data->req, ctr); |
| 212 | ablkcipher_request_set_callback(&data->req, CRYPTO_TFM_REQ_MAY_SLEEP | |
| 213 | CRYPTO_TFM_REQ_MAY_BACKLOG, |
| 214 | crypto_gcm_setkey_done, |
| 215 | &data->result); |
| 216 | ablkcipher_request_set_crypt(&data->req, data->sg, data->sg, |
| 217 | sizeof(data->hash), data->iv); |
| 218 | |
| 219 | err = crypto_ablkcipher_encrypt(&data->req); |
| 220 | if (err == -EINPROGRESS || err == -EBUSY) { |
| 221 | err = wait_for_completion_interruptible( |
| 222 | &data->result.completion); |
| 223 | if (!err) |
| 224 | err = data->result.err; |
| 225 | } |
| 226 | |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 227 | if (err) |
| 228 | goto out; |
| 229 | |
| 230 | if (ctx->gf128 != NULL) |
| 231 | gf128mul_free_4k(ctx->gf128); |
| 232 | |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 233 | ctx->gf128 = gf128mul_init_4k_lle(&data->hash); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 234 | |
| 235 | if (ctx->gf128 == NULL) |
| 236 | err = -ENOMEM; |
| 237 | |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 238 | out: |
| 239 | kfree(data); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 240 | return err; |
| 241 | } |
| 242 | |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 243 | static void crypto_gcm_init_crypt(struct ablkcipher_request *ablk_req, |
| 244 | struct aead_request *req, |
| 245 | unsigned int cryptlen) |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 246 | { |
| 247 | struct crypto_aead *aead = crypto_aead_reqtfm(req); |
| 248 | struct crypto_gcm_ctx *ctx = crypto_aead_ctx(aead); |
Herbert Xu | 2589469 | 2007-12-12 19:16:38 +0800 | [diff] [blame] | 249 | struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 250 | u32 flags = req->base.tfm->crt_flags; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 251 | struct crypto_gcm_ghash_ctx *ghash = &pctx->ghash; |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 252 | struct scatterlist *dst; |
| 253 | __be32 counter = cpu_to_be32(1); |
| 254 | |
| 255 | memset(pctx->auth_tag, 0, sizeof(pctx->auth_tag)); |
| 256 | memcpy(req->iv + 12, &counter, 4); |
| 257 | |
| 258 | sg_init_table(pctx->src, 2); |
| 259 | sg_set_buf(pctx->src, pctx->auth_tag, sizeof(pctx->auth_tag)); |
| 260 | scatterwalk_sg_chain(pctx->src, 2, req->src); |
| 261 | |
| 262 | dst = pctx->src; |
| 263 | if (req->src != req->dst) { |
| 264 | sg_init_table(pctx->dst, 2); |
| 265 | sg_set_buf(pctx->dst, pctx->auth_tag, sizeof(pctx->auth_tag)); |
| 266 | scatterwalk_sg_chain(pctx->dst, 2, req->dst); |
| 267 | dst = pctx->dst; |
| 268 | } |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 269 | |
| 270 | ablkcipher_request_set_tfm(ablk_req, ctx->ctr); |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 271 | ablkcipher_request_set_crypt(ablk_req, pctx->src, dst, |
| 272 | cryptlen + sizeof(pctx->auth_tag), |
| 273 | req->iv); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 274 | |
| 275 | crypto_gcm_ghash_init(ghash, flags, ctx->gf128); |
| 276 | |
Herbert Xu | 6160b28 | 2007-12-04 19:17:50 +1100 | [diff] [blame] | 277 | crypto_gcm_ghash_update_sg(ghash, req->assoc, req->assoclen); |
| 278 | crypto_gcm_ghash_flush(ghash); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 279 | } |
| 280 | |
Herbert Xu | 6160b28 | 2007-12-04 19:17:50 +1100 | [diff] [blame] | 281 | static int crypto_gcm_hash(struct aead_request *req) |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 282 | { |
Herbert Xu | 6160b28 | 2007-12-04 19:17:50 +1100 | [diff] [blame] | 283 | struct crypto_aead *aead = crypto_aead_reqtfm(req); |
Herbert Xu | 2589469 | 2007-12-12 19:16:38 +0800 | [diff] [blame] | 284 | struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 285 | u8 *auth_tag = pctx->auth_tag; |
| 286 | struct crypto_gcm_ghash_ctx *ghash = &pctx->ghash; |
| 287 | |
| 288 | crypto_gcm_ghash_update_sg(ghash, req->dst, req->cryptlen); |
| 289 | crypto_gcm_ghash_final_xor(ghash, req->assoclen, req->cryptlen, |
| 290 | auth_tag); |
| 291 | |
Herbert Xu | 6160b28 | 2007-12-04 19:17:50 +1100 | [diff] [blame] | 292 | scatterwalk_map_and_copy(auth_tag, req->dst, req->cryptlen, |
| 293 | crypto_aead_authsize(aead), 1); |
| 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | static void crypto_gcm_encrypt_done(struct crypto_async_request *areq, int err) |
| 298 | { |
| 299 | struct aead_request *req = areq->data; |
| 300 | |
| 301 | if (!err) |
| 302 | err = crypto_gcm_hash(req); |
| 303 | |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 304 | aead_request_complete(req, err); |
| 305 | } |
| 306 | |
| 307 | static int crypto_gcm_encrypt(struct aead_request *req) |
| 308 | { |
Herbert Xu | 2589469 | 2007-12-12 19:16:38 +0800 | [diff] [blame] | 309 | struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req); |
Herbert Xu | 7f68137 | 2007-12-06 14:59:53 +0800 | [diff] [blame] | 310 | struct ablkcipher_request *abreq = &pctx->abreq; |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 311 | int err; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 312 | |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 313 | crypto_gcm_init_crypt(abreq, req, req->cryptlen); |
| 314 | ablkcipher_request_set_callback(abreq, aead_request_flags(req), |
| 315 | crypto_gcm_encrypt_done, req); |
| 316 | |
| 317 | err = crypto_ablkcipher_encrypt(abreq); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 318 | if (err) |
| 319 | return err; |
| 320 | |
Herbert Xu | 6160b28 | 2007-12-04 19:17:50 +1100 | [diff] [blame] | 321 | return crypto_gcm_hash(req); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 322 | } |
| 323 | |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 324 | static int crypto_gcm_verify(struct aead_request *req) |
| 325 | { |
| 326 | struct crypto_aead *aead = crypto_aead_reqtfm(req); |
| 327 | struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req); |
| 328 | struct crypto_gcm_ghash_ctx *ghash = &pctx->ghash; |
| 329 | u8 *auth_tag = pctx->auth_tag; |
| 330 | u8 *iauth_tag = pctx->iauth_tag; |
| 331 | unsigned int authsize = crypto_aead_authsize(aead); |
| 332 | unsigned int cryptlen = req->cryptlen - authsize; |
| 333 | |
| 334 | crypto_gcm_ghash_final_xor(ghash, req->assoclen, cryptlen, auth_tag); |
| 335 | |
| 336 | authsize = crypto_aead_authsize(aead); |
| 337 | scatterwalk_map_and_copy(iauth_tag, req->src, cryptlen, authsize, 0); |
| 338 | return memcmp(iauth_tag, auth_tag, authsize) ? -EBADMSG : 0; |
| 339 | } |
| 340 | |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 341 | static void crypto_gcm_decrypt_done(struct crypto_async_request *areq, int err) |
| 342 | { |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 343 | struct aead_request *req = areq->data; |
| 344 | |
| 345 | if (!err) |
| 346 | err = crypto_gcm_verify(req); |
| 347 | |
| 348 | aead_request_complete(req, err); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | static int crypto_gcm_decrypt(struct aead_request *req) |
| 352 | { |
Herbert Xu | 6160b28 | 2007-12-04 19:17:50 +1100 | [diff] [blame] | 353 | struct crypto_aead *aead = crypto_aead_reqtfm(req); |
Herbert Xu | 2589469 | 2007-12-12 19:16:38 +0800 | [diff] [blame] | 354 | struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req); |
Herbert Xu | 7f68137 | 2007-12-06 14:59:53 +0800 | [diff] [blame] | 355 | struct ablkcipher_request *abreq = &pctx->abreq; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 356 | struct crypto_gcm_ghash_ctx *ghash = &pctx->ghash; |
Herbert Xu | 6160b28 | 2007-12-04 19:17:50 +1100 | [diff] [blame] | 357 | unsigned int cryptlen = req->cryptlen; |
| 358 | unsigned int authsize = crypto_aead_authsize(aead); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 359 | int err; |
| 360 | |
Herbert Xu | 6160b28 | 2007-12-04 19:17:50 +1100 | [diff] [blame] | 361 | if (cryptlen < authsize) |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 362 | return -EINVAL; |
Herbert Xu | 6160b28 | 2007-12-04 19:17:50 +1100 | [diff] [blame] | 363 | cryptlen -= authsize; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 364 | |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 365 | crypto_gcm_init_crypt(abreq, req, cryptlen); |
| 366 | ablkcipher_request_set_callback(abreq, aead_request_flags(req), |
| 367 | crypto_gcm_decrypt_done, req); |
| 368 | |
| 369 | crypto_gcm_ghash_update_sg(ghash, req->src, cryptlen); |
| 370 | |
| 371 | err = crypto_ablkcipher_decrypt(abreq); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 372 | if (err) |
| 373 | return err; |
| 374 | |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 375 | return crypto_gcm_verify(req); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | static int crypto_gcm_init_tfm(struct crypto_tfm *tfm) |
| 379 | { |
| 380 | struct crypto_instance *inst = (void *)tfm->__crt_alg; |
| 381 | struct gcm_instance_ctx *ictx = crypto_instance_ctx(inst); |
| 382 | struct crypto_gcm_ctx *ctx = crypto_tfm_ctx(tfm); |
| 383 | struct crypto_ablkcipher *ctr; |
| 384 | unsigned long align; |
| 385 | int err; |
| 386 | |
Herbert Xu | 1472e5e | 2007-12-07 19:26:11 +0800 | [diff] [blame^] | 387 | ctr = crypto_spawn_skcipher(&ictx->ctr); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 388 | err = PTR_ERR(ctr); |
| 389 | if (IS_ERR(ctr)) |
| 390 | return err; |
| 391 | |
| 392 | ctx->ctr = ctr; |
| 393 | ctx->gf128 = NULL; |
| 394 | |
Herbert Xu | 2589469 | 2007-12-12 19:16:38 +0800 | [diff] [blame] | 395 | align = crypto_tfm_alg_alignmask(tfm); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 396 | align &= ~(crypto_tfm_ctx_alignment() - 1); |
Herbert Xu | 7f68137 | 2007-12-06 14:59:53 +0800 | [diff] [blame] | 397 | tfm->crt_aead.reqsize = align + |
| 398 | sizeof(struct crypto_gcm_req_priv_ctx) + |
| 399 | crypto_ablkcipher_reqsize(ctr); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 400 | |
| 401 | return 0; |
| 402 | } |
| 403 | |
| 404 | static void crypto_gcm_exit_tfm(struct crypto_tfm *tfm) |
| 405 | { |
| 406 | struct crypto_gcm_ctx *ctx = crypto_tfm_ctx(tfm); |
| 407 | |
| 408 | if (ctx->gf128 != NULL) |
| 409 | gf128mul_free_4k(ctx->gf128); |
| 410 | |
| 411 | crypto_free_ablkcipher(ctx->ctr); |
| 412 | } |
| 413 | |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 414 | static struct crypto_instance *crypto_gcm_alloc_common(struct rtattr **tb, |
| 415 | const char *full_name, |
| 416 | const char *ctr_name) |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 417 | { |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 418 | struct crypto_attr_type *algt; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 419 | struct crypto_instance *inst; |
| 420 | struct crypto_alg *ctr; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 421 | struct gcm_instance_ctx *ctx; |
| 422 | int err; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 423 | |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 424 | algt = crypto_get_attr_type(tb); |
| 425 | err = PTR_ERR(algt); |
| 426 | if (IS_ERR(algt)) |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 427 | return ERR_PTR(err); |
| 428 | |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 429 | if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask) |
| 430 | return ERR_PTR(-EINVAL); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 431 | |
Herbert Xu | 1472e5e | 2007-12-07 19:26:11 +0800 | [diff] [blame^] | 432 | inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL); |
| 433 | if (!inst) |
| 434 | return ERR_PTR(-ENOMEM); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 435 | |
Herbert Xu | 1472e5e | 2007-12-07 19:26:11 +0800 | [diff] [blame^] | 436 | ctx = crypto_instance_ctx(inst); |
| 437 | crypto_set_skcipher_spawn(&ctx->ctr, inst); |
| 438 | err = crypto_grab_skcipher(&ctx->ctr, ctr_name, 0, |
| 439 | crypto_requires_sync(algt->type, |
| 440 | algt->mask)); |
| 441 | if (err) |
| 442 | goto err_free_inst; |
| 443 | |
| 444 | ctr = crypto_skcipher_spawn_alg(&ctx->ctr); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 445 | |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 446 | /* We only support 16-byte blocks. */ |
Herbert Xu | 1472e5e | 2007-12-07 19:26:11 +0800 | [diff] [blame^] | 447 | if (ctr->cra_ablkcipher.ivsize != 16) |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 448 | goto out_put_ctr; |
| 449 | |
| 450 | /* Not a stream cipher? */ |
| 451 | err = -EINVAL; |
| 452 | if (ctr->cra_blocksize != 1) |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 453 | goto out_put_ctr; |
| 454 | |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 455 | err = -ENAMETOOLONG; |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 456 | if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME, |
| 457 | "gcm_base(%s)", ctr->cra_driver_name) >= |
| 458 | CRYPTO_MAX_ALG_NAME) |
Herbert Xu | 1472e5e | 2007-12-07 19:26:11 +0800 | [diff] [blame^] | 459 | goto out_put_ctr; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 460 | |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 461 | memcpy(inst->alg.cra_name, full_name, CRYPTO_MAX_ALG_NAME); |
| 462 | |
Herbert Xu | 1472e5e | 2007-12-07 19:26:11 +0800 | [diff] [blame^] | 463 | inst->alg.cra_flags = CRYPTO_ALG_TYPE_AEAD; |
| 464 | inst->alg.cra_flags |= ctr->cra_flags & CRYPTO_ALG_ASYNC; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 465 | inst->alg.cra_priority = ctr->cra_priority; |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 466 | inst->alg.cra_blocksize = 1; |
Herbert Xu | 2589469 | 2007-12-12 19:16:38 +0800 | [diff] [blame] | 467 | inst->alg.cra_alignmask = ctr->cra_alignmask | (__alignof__(u64) - 1); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 468 | inst->alg.cra_type = &crypto_aead_type; |
Herbert Xu | 84c9115 | 2007-12-17 21:42:08 +0800 | [diff] [blame] | 469 | inst->alg.cra_aead.ivsize = 16; |
Herbert Xu | 7ba683a | 2007-12-02 18:49:21 +1100 | [diff] [blame] | 470 | inst->alg.cra_aead.maxauthsize = 16; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 471 | inst->alg.cra_ctxsize = sizeof(struct crypto_gcm_ctx); |
| 472 | inst->alg.cra_init = crypto_gcm_init_tfm; |
| 473 | inst->alg.cra_exit = crypto_gcm_exit_tfm; |
| 474 | inst->alg.cra_aead.setkey = crypto_gcm_setkey; |
| 475 | inst->alg.cra_aead.encrypt = crypto_gcm_encrypt; |
| 476 | inst->alg.cra_aead.decrypt = crypto_gcm_decrypt; |
| 477 | |
| 478 | out: |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 479 | return inst; |
Herbert Xu | 1472e5e | 2007-12-07 19:26:11 +0800 | [diff] [blame^] | 480 | |
| 481 | out_put_ctr: |
| 482 | crypto_drop_skcipher(&ctx->ctr); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 483 | err_free_inst: |
| 484 | kfree(inst); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 485 | inst = ERR_PTR(err); |
| 486 | goto out; |
| 487 | } |
| 488 | |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 489 | static struct crypto_instance *crypto_gcm_alloc(struct rtattr **tb) |
| 490 | { |
| 491 | int err; |
| 492 | const char *cipher_name; |
| 493 | char ctr_name[CRYPTO_MAX_ALG_NAME]; |
| 494 | char full_name[CRYPTO_MAX_ALG_NAME]; |
| 495 | |
| 496 | cipher_name = crypto_attr_alg_name(tb[1]); |
| 497 | err = PTR_ERR(cipher_name); |
| 498 | if (IS_ERR(cipher_name)) |
| 499 | return ERR_PTR(err); |
| 500 | |
| 501 | if (snprintf(ctr_name, CRYPTO_MAX_ALG_NAME, "ctr(%s)", cipher_name) >= |
| 502 | CRYPTO_MAX_ALG_NAME) |
| 503 | return ERR_PTR(-ENAMETOOLONG); |
| 504 | |
| 505 | if (snprintf(full_name, CRYPTO_MAX_ALG_NAME, "gcm(%s)", cipher_name) >= |
| 506 | CRYPTO_MAX_ALG_NAME) |
| 507 | return ERR_PTR(-ENAMETOOLONG); |
| 508 | |
| 509 | return crypto_gcm_alloc_common(tb, full_name, ctr_name); |
| 510 | } |
| 511 | |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 512 | static void crypto_gcm_free(struct crypto_instance *inst) |
| 513 | { |
| 514 | struct gcm_instance_ctx *ctx = crypto_instance_ctx(inst); |
| 515 | |
Herbert Xu | 1472e5e | 2007-12-07 19:26:11 +0800 | [diff] [blame^] | 516 | crypto_drop_skcipher(&ctx->ctr); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 517 | kfree(inst); |
| 518 | } |
| 519 | |
| 520 | static struct crypto_template crypto_gcm_tmpl = { |
| 521 | .name = "gcm", |
| 522 | .alloc = crypto_gcm_alloc, |
| 523 | .free = crypto_gcm_free, |
| 524 | .module = THIS_MODULE, |
| 525 | }; |
| 526 | |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 527 | static struct crypto_instance *crypto_gcm_base_alloc(struct rtattr **tb) |
| 528 | { |
| 529 | int err; |
| 530 | const char *ctr_name; |
| 531 | char full_name[CRYPTO_MAX_ALG_NAME]; |
| 532 | |
| 533 | ctr_name = crypto_attr_alg_name(tb[1]); |
| 534 | err = PTR_ERR(ctr_name); |
| 535 | if (IS_ERR(ctr_name)) |
| 536 | return ERR_PTR(err); |
| 537 | |
| 538 | if (snprintf(full_name, CRYPTO_MAX_ALG_NAME, "gcm_base(%s)", |
| 539 | ctr_name) >= CRYPTO_MAX_ALG_NAME) |
| 540 | return ERR_PTR(-ENAMETOOLONG); |
| 541 | |
| 542 | return crypto_gcm_alloc_common(tb, full_name, ctr_name); |
| 543 | } |
| 544 | |
| 545 | static struct crypto_template crypto_gcm_base_tmpl = { |
| 546 | .name = "gcm_base", |
| 547 | .alloc = crypto_gcm_base_alloc, |
| 548 | .free = crypto_gcm_free, |
| 549 | .module = THIS_MODULE, |
| 550 | }; |
| 551 | |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 552 | static int __init crypto_gcm_module_init(void) |
| 553 | { |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 554 | int err; |
| 555 | |
| 556 | err = crypto_register_template(&crypto_gcm_base_tmpl); |
| 557 | if (err) |
| 558 | goto out; |
| 559 | |
| 560 | err = crypto_register_template(&crypto_gcm_tmpl); |
| 561 | if (err) |
| 562 | goto out_undo_base; |
| 563 | |
| 564 | out: |
| 565 | return err; |
| 566 | |
| 567 | out_undo_base: |
| 568 | crypto_unregister_template(&crypto_gcm_base_tmpl); |
| 569 | goto out; |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | static void __exit crypto_gcm_module_exit(void) |
| 573 | { |
| 574 | crypto_unregister_template(&crypto_gcm_tmpl); |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 575 | crypto_unregister_template(&crypto_gcm_base_tmpl); |
Mikko Herranen | 28db8e3 | 2007-11-26 22:24:11 +0800 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | module_init(crypto_gcm_module_init); |
| 579 | module_exit(crypto_gcm_module_exit); |
| 580 | |
| 581 | MODULE_LICENSE("GPL"); |
| 582 | MODULE_DESCRIPTION("Galois/Counter Mode"); |
| 583 | MODULE_AUTHOR("Mikko Herranen <mh1@iki.fi>"); |
Herbert Xu | d00aa19 | 2007-12-07 20:31:10 +0800 | [diff] [blame] | 584 | MODULE_ALIAS("gcm_base"); |