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