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