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