Blame view

crypto/authencesn.c 15.1 KB
2874c5fd2   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-or-later
a5079d084   Steffen Klassert   crypto: authences...
2
3
4
5
6
7
  /*
   * authencesn.c - AEAD wrapper for IPsec with extended sequence numbers,
   *                 derived from authenc.c
   *
   * Copyright (C) 2010 secunet Security Networks AG
   * Copyright (C) 2010 Steffen Klassert <steffen.klassert@secunet.com>
104880a6b   Herbert Xu   crypto: authences...
8
   * Copyright (c) 2015 Herbert Xu <herbert@gondor.apana.org.au>
a5079d084   Steffen Klassert   crypto: authences...
9
   */
7fb2a4bdc   Herbert Xu   crypto: authences...
10
  #include <crypto/internal/aead.h>
a5079d084   Steffen Klassert   crypto: authences...
11
12
13
  #include <crypto/internal/hash.h>
  #include <crypto/internal/skcipher.h>
  #include <crypto/authenc.h>
104880a6b   Herbert Xu   crypto: authences...
14
  #include <crypto/null.h>
a5079d084   Steffen Klassert   crypto: authences...
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  #include <crypto/scatterwalk.h>
  #include <linux/err.h>
  #include <linux/init.h>
  #include <linux/kernel.h>
  #include <linux/module.h>
  #include <linux/rtnetlink.h>
  #include <linux/slab.h>
  #include <linux/spinlock.h>
  
  struct authenc_esn_instance_ctx {
  	struct crypto_ahash_spawn auth;
  	struct crypto_skcipher_spawn enc;
  };
  
  struct crypto_authenc_esn_ctx {
  	unsigned int reqoff;
  	struct crypto_ahash *auth;
e75445a84   Herbert Xu   crypto: authences...
32
  	struct crypto_skcipher *enc;
8d6053984   Kees Cook   crypto: null - Re...
33
  	struct crypto_sync_skcipher *null;
a5079d084   Steffen Klassert   crypto: authences...
34
35
36
  };
  
  struct authenc_esn_request_ctx {
104880a6b   Herbert Xu   crypto: authences...
37
38
  	struct scatterlist src[2];
  	struct scatterlist dst[2];
a5079d084   Steffen Klassert   crypto: authences...
39
40
41
42
43
44
45
46
  	char tail[];
  };
  
  static void authenc_esn_request_complete(struct aead_request *req, int err)
  {
  	if (err != -EINPROGRESS)
  		aead_request_complete(req, err);
  }
104880a6b   Herbert Xu   crypto: authences...
47
48
49
50
51
52
53
54
  static int crypto_authenc_esn_setauthsize(struct crypto_aead *authenc_esn,
  					  unsigned int authsize)
  {
  	if (authsize > 0 && authsize < 4)
  		return -EINVAL;
  
  	return 0;
  }
a5079d084   Steffen Klassert   crypto: authences...
55
56
57
  static int crypto_authenc_esn_setkey(struct crypto_aead *authenc_esn, const u8 *key,
  				     unsigned int keylen)
  {
a5079d084   Steffen Klassert   crypto: authences...
58
59
  	struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(authenc_esn);
  	struct crypto_ahash *auth = ctx->auth;
e75445a84   Herbert Xu   crypto: authences...
60
  	struct crypto_skcipher *enc = ctx->enc;
fddc2c43c   Mathias Krause   crypto: authences...
61
  	struct crypto_authenc_keys keys;
a5079d084   Steffen Klassert   crypto: authences...
62
  	int err = -EINVAL;
fddc2c43c   Mathias Krause   crypto: authences...
63
  	if (crypto_authenc_extractkeys(&keys, key, keylen) != 0)
a5079d084   Steffen Klassert   crypto: authences...
64
  		goto badkey;
a5079d084   Steffen Klassert   crypto: authences...
65
66
67
68
  
  	crypto_ahash_clear_flags(auth, CRYPTO_TFM_REQ_MASK);
  	crypto_ahash_set_flags(auth, crypto_aead_get_flags(authenc_esn) &
  				     CRYPTO_TFM_REQ_MASK);
fddc2c43c   Mathias Krause   crypto: authences...
69
  	err = crypto_ahash_setkey(auth, keys.authkey, keys.authkeylen);
a5079d084   Steffen Klassert   crypto: authences...
70
71
72
73
74
  	crypto_aead_set_flags(authenc_esn, crypto_ahash_get_flags(auth) &
  					   CRYPTO_TFM_RES_MASK);
  
  	if (err)
  		goto out;
e75445a84   Herbert Xu   crypto: authences...
75
76
  	crypto_skcipher_clear_flags(enc, CRYPTO_TFM_REQ_MASK);
  	crypto_skcipher_set_flags(enc, crypto_aead_get_flags(authenc_esn) &
a5079d084   Steffen Klassert   crypto: authences...
77
  					 CRYPTO_TFM_REQ_MASK);
e75445a84   Herbert Xu   crypto: authences...
78
79
  	err = crypto_skcipher_setkey(enc, keys.enckey, keys.enckeylen);
  	crypto_aead_set_flags(authenc_esn, crypto_skcipher_get_flags(enc) &
a5079d084   Steffen Klassert   crypto: authences...
80
81
82
  					   CRYPTO_TFM_RES_MASK);
  
  out:
31545df39   Tudor-Dan Ambarus   crypto: authences...
83
  	memzero_explicit(&keys, sizeof(keys));
a5079d084   Steffen Klassert   crypto: authences...
84
85
86
87
88
89
  	return err;
  
  badkey:
  	crypto_aead_set_flags(authenc_esn, CRYPTO_TFM_RES_BAD_KEY_LEN);
  	goto out;
  }
104880a6b   Herbert Xu   crypto: authences...
90
91
  static int crypto_authenc_esn_genicv_tail(struct aead_request *req,
  					  unsigned int flags)
a5079d084   Steffen Klassert   crypto: authences...
92
  {
a5079d084   Steffen Klassert   crypto: authences...
93
94
95
  	struct crypto_aead *authenc_esn = crypto_aead_reqtfm(req);
  	struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(authenc_esn);
  	struct authenc_esn_request_ctx *areq_ctx = aead_request_ctx(req);
104880a6b   Herbert Xu   crypto: authences...
96
97
98
99
100
101
102
103
  	struct crypto_ahash *auth = ctx->auth;
  	u8 *hash = PTR_ALIGN((u8 *)areq_ctx->tail,
  			     crypto_ahash_alignmask(auth) + 1);
  	unsigned int authsize = crypto_aead_authsize(authenc_esn);
  	unsigned int assoclen = req->assoclen;
  	unsigned int cryptlen = req->cryptlen;
  	struct scatterlist *dst = req->dst;
  	u32 tmp[2];
a5079d084   Steffen Klassert   crypto: authences...
104

104880a6b   Herbert Xu   crypto: authences...
105
106
107
108
  	/* Move high-order bits of sequence number back. */
  	scatterwalk_map_and_copy(tmp, dst, 4, 4, 0);
  	scatterwalk_map_and_copy(tmp + 1, dst, assoclen + cryptlen, 4, 0);
  	scatterwalk_map_and_copy(tmp, dst, 0, 8, 1);
a5079d084   Steffen Klassert   crypto: authences...
109

104880a6b   Herbert Xu   crypto: authences...
110
111
  	scatterwalk_map_and_copy(hash, dst, assoclen + cryptlen, authsize, 1);
  	return 0;
a5079d084   Steffen Klassert   crypto: authences...
112
  }
a5079d084   Steffen Klassert   crypto: authences...
113
114
115
116
  static void authenc_esn_geniv_ahash_done(struct crypto_async_request *areq,
  					 int err)
  {
  	struct aead_request *req = areq->data;
a5079d084   Steffen Klassert   crypto: authences...
117

104880a6b   Herbert Xu   crypto: authences...
118
  	err = err ?: crypto_authenc_esn_genicv_tail(req, 0);
a5079d084   Steffen Klassert   crypto: authences...
119
120
  	aead_request_complete(req, err);
  }
104880a6b   Herbert Xu   crypto: authences...
121
122
  static int crypto_authenc_esn_genicv(struct aead_request *req,
  				     unsigned int flags)
a5079d084   Steffen Klassert   crypto: authences...
123
  {
a5079d084   Steffen Klassert   crypto: authences...
124
  	struct crypto_aead *authenc_esn = crypto_aead_reqtfm(req);
a5079d084   Steffen Klassert   crypto: authences...
125
  	struct authenc_esn_request_ctx *areq_ctx = aead_request_ctx(req);
a5079d084   Steffen Klassert   crypto: authences...
126
  	struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(authenc_esn);
104880a6b   Herbert Xu   crypto: authences...
127
128
129
  	struct crypto_ahash *auth = ctx->auth;
  	u8 *hash = PTR_ALIGN((u8 *)areq_ctx->tail,
  			     crypto_ahash_alignmask(auth) + 1);
a5079d084   Steffen Klassert   crypto: authences...
130
  	struct ahash_request *ahreq = (void *)(areq_ctx->tail + ctx->reqoff);
104880a6b   Herbert Xu   crypto: authences...
131
132
  	unsigned int authsize = crypto_aead_authsize(authenc_esn);
  	unsigned int assoclen = req->assoclen;
a5079d084   Steffen Klassert   crypto: authences...
133
  	unsigned int cryptlen = req->cryptlen;
104880a6b   Herbert Xu   crypto: authences...
134
135
  	struct scatterlist *dst = req->dst;
  	u32 tmp[2];
a5079d084   Steffen Klassert   crypto: authences...
136

104880a6b   Herbert Xu   crypto: authences...
137
138
  	if (!authsize)
  		return 0;
a5079d084   Steffen Klassert   crypto: authences...
139

104880a6b   Herbert Xu   crypto: authences...
140
141
142
143
  	/* Move high-order bits of sequence number to the end. */
  	scatterwalk_map_and_copy(tmp, dst, 0, 8, 0);
  	scatterwalk_map_and_copy(tmp, dst, 4, 4, 1);
  	scatterwalk_map_and_copy(tmp + 1, dst, assoclen + cryptlen, 4, 1);
a5079d084   Steffen Klassert   crypto: authences...
144

104880a6b   Herbert Xu   crypto: authences...
145
146
  	sg_init_table(areq_ctx->dst, 2);
  	dst = scatterwalk_ffwd(areq_ctx->dst, dst, 4);
a5079d084   Steffen Klassert   crypto: authences...
147

104880a6b   Herbert Xu   crypto: authences...
148
149
150
151
  	ahash_request_set_tfm(ahreq, auth);
  	ahash_request_set_crypt(ahreq, dst, hash, assoclen + cryptlen);
  	ahash_request_set_callback(ahreq, flags,
  				   authenc_esn_geniv_ahash_done, req);
a5079d084   Steffen Klassert   crypto: authences...
152

104880a6b   Herbert Xu   crypto: authences...
153
154
  	return crypto_ahash_digest(ahreq) ?:
  	       crypto_authenc_esn_genicv_tail(req, aead_request_flags(req));
a5079d084   Steffen Klassert   crypto: authences...
155
  }
104880a6b   Herbert Xu   crypto: authences...
156
157
  static void crypto_authenc_esn_encrypt_done(struct crypto_async_request *req,
  					    int err)
a5079d084   Steffen Klassert   crypto: authences...
158
  {
104880a6b   Herbert Xu   crypto: authences...
159
  	struct aead_request *areq = req->data;
a5079d084   Steffen Klassert   crypto: authences...
160

104880a6b   Herbert Xu   crypto: authences...
161
162
  	if (!err)
  		err = crypto_authenc_esn_genicv(areq, 0);
a5079d084   Steffen Klassert   crypto: authences...
163

104880a6b   Herbert Xu   crypto: authences...
164
  	authenc_esn_request_complete(areq, err);
a5079d084   Steffen Klassert   crypto: authences...
165
  }
104880a6b   Herbert Xu   crypto: authences...
166
  static int crypto_authenc_esn_copy(struct aead_request *req, unsigned int len)
a5079d084   Steffen Klassert   crypto: authences...
167
168
169
  {
  	struct crypto_aead *authenc_esn = crypto_aead_reqtfm(req);
  	struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(authenc_esn);
8d6053984   Kees Cook   crypto: null - Re...
170
  	SYNC_SKCIPHER_REQUEST_ON_STACK(skreq, ctx->null);
a5079d084   Steffen Klassert   crypto: authences...
171

8d6053984   Kees Cook   crypto: null - Re...
172
  	skcipher_request_set_sync_tfm(skreq, ctx->null);
e75445a84   Herbert Xu   crypto: authences...
173
174
175
176
177
  	skcipher_request_set_callback(skreq, aead_request_flags(req),
  				      NULL, NULL);
  	skcipher_request_set_crypt(skreq, req->src, req->dst, len, NULL);
  
  	return crypto_skcipher_encrypt(skreq);
a5079d084   Steffen Klassert   crypto: authences...
178
  }
104880a6b   Herbert Xu   crypto: authences...
179
  static int crypto_authenc_esn_encrypt(struct aead_request *req)
a5079d084   Steffen Klassert   crypto: authences...
180
181
182
  {
  	struct crypto_aead *authenc_esn = crypto_aead_reqtfm(req);
  	struct authenc_esn_request_ctx *areq_ctx = aead_request_ctx(req);
104880a6b   Herbert Xu   crypto: authences...
183
  	struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(authenc_esn);
e75445a84   Herbert Xu   crypto: authences...
184
185
186
  	struct skcipher_request *skreq = (void *)(areq_ctx->tail +
  						  ctx->reqoff);
  	struct crypto_skcipher *enc = ctx->enc;
104880a6b   Herbert Xu   crypto: authences...
187
  	unsigned int assoclen = req->assoclen;
a5079d084   Steffen Klassert   crypto: authences...
188
  	unsigned int cryptlen = req->cryptlen;
104880a6b   Herbert Xu   crypto: authences...
189
190
  	struct scatterlist *src, *dst;
  	int err;
a5079d084   Steffen Klassert   crypto: authences...
191

104880a6b   Herbert Xu   crypto: authences...
192
193
194
  	sg_init_table(areq_ctx->src, 2);
  	src = scatterwalk_ffwd(areq_ctx->src, req->src, assoclen);
  	dst = src;
a5079d084   Steffen Klassert   crypto: authences...
195

104880a6b   Herbert Xu   crypto: authences...
196
197
198
199
  	if (req->src != req->dst) {
  		err = crypto_authenc_esn_copy(req, assoclen);
  		if (err)
  			return err;
a5079d084   Steffen Klassert   crypto: authences...
200

104880a6b   Herbert Xu   crypto: authences...
201
202
  		sg_init_table(areq_ctx->dst, 2);
  		dst = scatterwalk_ffwd(areq_ctx->dst, req->dst, assoclen);
a5079d084   Steffen Klassert   crypto: authences...
203
  	}
e75445a84   Herbert Xu   crypto: authences...
204
205
206
207
  	skcipher_request_set_tfm(skreq, enc);
  	skcipher_request_set_callback(skreq, aead_request_flags(req),
  				      crypto_authenc_esn_encrypt_done, req);
  	skcipher_request_set_crypt(skreq, src, dst, cryptlen, req->iv);
a5079d084   Steffen Klassert   crypto: authences...
208

e75445a84   Herbert Xu   crypto: authences...
209
  	err = crypto_skcipher_encrypt(skreq);
a5079d084   Steffen Klassert   crypto: authences...
210
211
  	if (err)
  		return err;
104880a6b   Herbert Xu   crypto: authences...
212
  	return crypto_authenc_esn_genicv(req, aead_request_flags(req));
a5079d084   Steffen Klassert   crypto: authences...
213
  }
104880a6b   Herbert Xu   crypto: authences...
214
215
  static int crypto_authenc_esn_decrypt_tail(struct aead_request *req,
  					   unsigned int flags)
a5079d084   Steffen Klassert   crypto: authences...
216
  {
104880a6b   Herbert Xu   crypto: authences...
217
218
219
220
  	struct crypto_aead *authenc_esn = crypto_aead_reqtfm(req);
  	unsigned int authsize = crypto_aead_authsize(authenc_esn);
  	struct authenc_esn_request_ctx *areq_ctx = aead_request_ctx(req);
  	struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(authenc_esn);
e75445a84   Herbert Xu   crypto: authences...
221
222
  	struct skcipher_request *skreq = (void *)(areq_ctx->tail +
  						  ctx->reqoff);
104880a6b   Herbert Xu   crypto: authences...
223
224
225
226
227
228
229
230
  	struct crypto_ahash *auth = ctx->auth;
  	u8 *ohash = PTR_ALIGN((u8 *)areq_ctx->tail,
  			      crypto_ahash_alignmask(auth) + 1);
  	unsigned int cryptlen = req->cryptlen - authsize;
  	unsigned int assoclen = req->assoclen;
  	struct scatterlist *dst = req->dst;
  	u8 *ihash = ohash + crypto_ahash_digestsize(auth);
  	u32 tmp[2];
a5079d084   Steffen Klassert   crypto: authences...
231

41cdf7a45   Herbert Xu   crypto: authences...
232
233
  	if (!authsize)
  		goto decrypt;
104880a6b   Herbert Xu   crypto: authences...
234
235
236
237
  	/* Move high-order bits of sequence number back. */
  	scatterwalk_map_and_copy(tmp, dst, 4, 4, 0);
  	scatterwalk_map_and_copy(tmp + 1, dst, assoclen + cryptlen, 4, 0);
  	scatterwalk_map_and_copy(tmp, dst, 0, 8, 1);
a5079d084   Steffen Klassert   crypto: authences...
238

104880a6b   Herbert Xu   crypto: authences...
239
240
  	if (crypto_memneq(ihash, ohash, authsize))
  		return -EBADMSG;
a5079d084   Steffen Klassert   crypto: authences...
241

41cdf7a45   Herbert Xu   crypto: authences...
242
  decrypt:
104880a6b   Herbert Xu   crypto: authences...
243
244
  	sg_init_table(areq_ctx->dst, 2);
  	dst = scatterwalk_ffwd(areq_ctx->dst, dst, assoclen);
a5079d084   Steffen Klassert   crypto: authences...
245

e75445a84   Herbert Xu   crypto: authences...
246
247
248
249
  	skcipher_request_set_tfm(skreq, ctx->enc);
  	skcipher_request_set_callback(skreq, flags,
  				      req->base.complete, req->base.data);
  	skcipher_request_set_crypt(skreq, dst, dst, cryptlen, req->iv);
a5079d084   Steffen Klassert   crypto: authences...
250

e75445a84   Herbert Xu   crypto: authences...
251
  	return crypto_skcipher_decrypt(skreq);
a5079d084   Steffen Klassert   crypto: authences...
252
  }
104880a6b   Herbert Xu   crypto: authences...
253
254
  static void authenc_esn_verify_ahash_done(struct crypto_async_request *areq,
  					  int err)
a5079d084   Steffen Klassert   crypto: authences...
255
  {
104880a6b   Herbert Xu   crypto: authences...
256
  	struct aead_request *req = areq->data;
a5079d084   Steffen Klassert   crypto: authences...
257

104880a6b   Herbert Xu   crypto: authences...
258
  	err = err ?: crypto_authenc_esn_decrypt_tail(req, 0);
a77733636   Harsh Jain   crypto: authences...
259
  	authenc_esn_request_complete(req, err);
a5079d084   Steffen Klassert   crypto: authences...
260
  }
104880a6b   Herbert Xu   crypto: authences...
261
  static int crypto_authenc_esn_decrypt(struct aead_request *req)
a5079d084   Steffen Klassert   crypto: authences...
262
263
264
  {
  	struct crypto_aead *authenc_esn = crypto_aead_reqtfm(req);
  	struct authenc_esn_request_ctx *areq_ctx = aead_request_ctx(req);
104880a6b   Herbert Xu   crypto: authences...
265
266
267
268
269
270
271
272
273
274
275
276
  	struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(authenc_esn);
  	struct ahash_request *ahreq = (void *)(areq_ctx->tail + ctx->reqoff);
  	unsigned int authsize = crypto_aead_authsize(authenc_esn);
  	struct crypto_ahash *auth = ctx->auth;
  	u8 *ohash = PTR_ALIGN((u8 *)areq_ctx->tail,
  			      crypto_ahash_alignmask(auth) + 1);
  	unsigned int assoclen = req->assoclen;
  	unsigned int cryptlen = req->cryptlen;
  	u8 *ihash = ohash + crypto_ahash_digestsize(auth);
  	struct scatterlist *dst = req->dst;
  	u32 tmp[2];
  	int err;
a5079d084   Steffen Klassert   crypto: authences...
277

104880a6b   Herbert Xu   crypto: authences...
278
  	cryptlen -= authsize;
a5079d084   Steffen Klassert   crypto: authences...
279

104880a6b   Herbert Xu   crypto: authences...
280
281
282
283
284
  	if (req->src != dst) {
  		err = crypto_authenc_esn_copy(req, assoclen + cryptlen);
  		if (err)
  			return err;
  	}
a5079d084   Steffen Klassert   crypto: authences...
285

104880a6b   Herbert Xu   crypto: authences...
286
287
  	scatterwalk_map_and_copy(ihash, req->src, assoclen + cryptlen,
  				 authsize, 0);
a5079d084   Steffen Klassert   crypto: authences...
288

104880a6b   Herbert Xu   crypto: authences...
289
290
  	if (!authsize)
  		goto tail;
a5079d084   Steffen Klassert   crypto: authences...
291

104880a6b   Herbert Xu   crypto: authences...
292
293
294
295
  	/* Move high-order bits of sequence number to the end. */
  	scatterwalk_map_and_copy(tmp, dst, 0, 8, 0);
  	scatterwalk_map_and_copy(tmp, dst, 4, 4, 1);
  	scatterwalk_map_and_copy(tmp + 1, dst, assoclen + cryptlen, 4, 1);
a5079d084   Steffen Klassert   crypto: authences...
296

104880a6b   Herbert Xu   crypto: authences...
297
298
  	sg_init_table(areq_ctx->dst, 2);
  	dst = scatterwalk_ffwd(areq_ctx->dst, dst, 4);
a5079d084   Steffen Klassert   crypto: authences...
299

104880a6b   Herbert Xu   crypto: authences...
300
301
302
303
  	ahash_request_set_tfm(ahreq, auth);
  	ahash_request_set_crypt(ahreq, dst, ohash, assoclen + cryptlen);
  	ahash_request_set_callback(ahreq, aead_request_flags(req),
  				   authenc_esn_verify_ahash_done, req);
a5079d084   Steffen Klassert   crypto: authences...
304

104880a6b   Herbert Xu   crypto: authences...
305
  	err = crypto_ahash_digest(ahreq);
a5079d084   Steffen Klassert   crypto: authences...
306
307
  	if (err)
  		return err;
104880a6b   Herbert Xu   crypto: authences...
308
309
  tail:
  	return crypto_authenc_esn_decrypt_tail(req, aead_request_flags(req));
a5079d084   Steffen Klassert   crypto: authences...
310
  }
104880a6b   Herbert Xu   crypto: authences...
311
  static int crypto_authenc_esn_init_tfm(struct crypto_aead *tfm)
a5079d084   Steffen Klassert   crypto: authences...
312
  {
104880a6b   Herbert Xu   crypto: authences...
313
314
315
  	struct aead_instance *inst = aead_alg_instance(tfm);
  	struct authenc_esn_instance_ctx *ictx = aead_instance_ctx(inst);
  	struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(tfm);
a5079d084   Steffen Klassert   crypto: authences...
316
  	struct crypto_ahash *auth;
e75445a84   Herbert Xu   crypto: authences...
317
  	struct crypto_skcipher *enc;
8d6053984   Kees Cook   crypto: null - Re...
318
  	struct crypto_sync_skcipher *null;
a5079d084   Steffen Klassert   crypto: authences...
319
320
321
322
323
  	int err;
  
  	auth = crypto_spawn_ahash(&ictx->auth);
  	if (IS_ERR(auth))
  		return PTR_ERR(auth);
60425a8ba   Eric Biggers   crypto: skcipher ...
324
  	enc = crypto_spawn_skcipher(&ictx->enc);
a5079d084   Steffen Klassert   crypto: authences...
325
326
327
  	err = PTR_ERR(enc);
  	if (IS_ERR(enc))
  		goto err_free_ahash;
3a2d4fb51   Eric Biggers   crypto: null - Ge...
328
  	null = crypto_get_default_null_skcipher();
104880a6b   Herbert Xu   crypto: authences...
329
330
331
  	err = PTR_ERR(null);
  	if (IS_ERR(null))
  		goto err_free_skcipher;
a5079d084   Steffen Klassert   crypto: authences...
332
333
  	ctx->auth = auth;
  	ctx->enc = enc;
104880a6b   Herbert Xu   crypto: authences...
334
  	ctx->null = null;
a5079d084   Steffen Klassert   crypto: authences...
335

104880a6b   Herbert Xu   crypto: authences...
336
337
  	ctx->reqoff = ALIGN(2 * crypto_ahash_digestsize(auth),
  			    crypto_ahash_alignmask(auth) + 1);
a5079d084   Steffen Klassert   crypto: authences...
338

104880a6b   Herbert Xu   crypto: authences...
339
340
  	crypto_aead_set_reqsize(
  		tfm,
6650d09b3   Herbert Xu   crypto: authences...
341
342
343
  		sizeof(struct authenc_esn_request_ctx) +
  		ctx->reqoff +
  		max_t(unsigned int,
e75445a84   Herbert Xu   crypto: authences...
344
345
346
347
  		      crypto_ahash_reqsize(auth) +
  		      sizeof(struct ahash_request),
  		      sizeof(struct skcipher_request) +
  		      crypto_skcipher_reqsize(enc)));
a5079d084   Steffen Klassert   crypto: authences...
348
349
  
  	return 0;
104880a6b   Herbert Xu   crypto: authences...
350
  err_free_skcipher:
e75445a84   Herbert Xu   crypto: authences...
351
  	crypto_free_skcipher(enc);
a5079d084   Steffen Klassert   crypto: authences...
352
353
354
355
  err_free_ahash:
  	crypto_free_ahash(auth);
  	return err;
  }
104880a6b   Herbert Xu   crypto: authences...
356
  static void crypto_authenc_esn_exit_tfm(struct crypto_aead *tfm)
a5079d084   Steffen Klassert   crypto: authences...
357
  {
104880a6b   Herbert Xu   crypto: authences...
358
  	struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(tfm);
a5079d084   Steffen Klassert   crypto: authences...
359
360
  
  	crypto_free_ahash(ctx->auth);
e75445a84   Herbert Xu   crypto: authences...
361
  	crypto_free_skcipher(ctx->enc);
3a2d4fb51   Eric Biggers   crypto: null - Ge...
362
  	crypto_put_default_null_skcipher();
104880a6b   Herbert Xu   crypto: authences...
363
364
365
366
367
368
369
370
371
  }
  
  static void crypto_authenc_esn_free(struct aead_instance *inst)
  {
  	struct authenc_esn_instance_ctx *ctx = aead_instance_ctx(inst);
  
  	crypto_drop_skcipher(&ctx->enc);
  	crypto_drop_ahash(&ctx->auth);
  	kfree(inst);
a5079d084   Steffen Klassert   crypto: authences...
372
  }
104880a6b   Herbert Xu   crypto: authences...
373
374
  static int crypto_authenc_esn_create(struct crypto_template *tmpl,
  				     struct rtattr **tb)
a5079d084   Steffen Klassert   crypto: authences...
375
376
  {
  	struct crypto_attr_type *algt;
104880a6b   Herbert Xu   crypto: authences...
377
  	struct aead_instance *inst;
a5079d084   Steffen Klassert   crypto: authences...
378
379
  	struct hash_alg_common *auth;
  	struct crypto_alg *auth_base;
e75445a84   Herbert Xu   crypto: authences...
380
  	struct skcipher_alg *enc;
a5079d084   Steffen Klassert   crypto: authences...
381
382
383
384
385
  	struct authenc_esn_instance_ctx *ctx;
  	const char *enc_name;
  	int err;
  
  	algt = crypto_get_attr_type(tb);
a5079d084   Steffen Klassert   crypto: authences...
386
  	if (IS_ERR(algt))
104880a6b   Herbert Xu   crypto: authences...
387
  		return PTR_ERR(algt);
a5079d084   Steffen Klassert   crypto: authences...
388

5e4b8c1fc   Herbert Xu   crypto: aead - Re...
389
  	if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
104880a6b   Herbert Xu   crypto: authences...
390
  		return -EINVAL;
a5079d084   Steffen Klassert   crypto: authences...
391
392
  
  	auth = ahash_attr_alg(tb[1], CRYPTO_ALG_TYPE_HASH,
927ef32dc   Herbert Xu   crypto: authenc -...
393
394
  			      CRYPTO_ALG_TYPE_AHASH_MASK |
  			      crypto_requires_sync(algt->type, algt->mask));
a5079d084   Steffen Klassert   crypto: authences...
395
  	if (IS_ERR(auth))
104880a6b   Herbert Xu   crypto: authences...
396
  		return PTR_ERR(auth);
a5079d084   Steffen Klassert   crypto: authences...
397
398
399
400
401
402
403
404
405
406
407
408
  
  	auth_base = &auth->base;
  
  	enc_name = crypto_attr_alg_name(tb[2]);
  	err = PTR_ERR(enc_name);
  	if (IS_ERR(enc_name))
  		goto out_put_auth;
  
  	inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
  	err = -ENOMEM;
  	if (!inst)
  		goto out_put_auth;
104880a6b   Herbert Xu   crypto: authences...
409
  	ctx = aead_instance_ctx(inst);
a5079d084   Steffen Klassert   crypto: authences...
410

104880a6b   Herbert Xu   crypto: authences...
411
412
  	err = crypto_init_ahash_spawn(&ctx->auth, auth,
  				      aead_crypto_instance(inst));
a5079d084   Steffen Klassert   crypto: authences...
413
414
  	if (err)
  		goto err_free_inst;
104880a6b   Herbert Xu   crypto: authences...
415
  	crypto_set_skcipher_spawn(&ctx->enc, aead_crypto_instance(inst));
a35528eca   Eric Biggers   crypto: skcipher ...
416
417
418
  	err = crypto_grab_skcipher(&ctx->enc, enc_name, 0,
  				   crypto_requires_sync(algt->type,
  							algt->mask));
a5079d084   Steffen Klassert   crypto: authences...
419
420
  	if (err)
  		goto err_drop_auth;
e75445a84   Herbert Xu   crypto: authences...
421
  	enc = crypto_spawn_skcipher_alg(&ctx->enc);
a5079d084   Steffen Klassert   crypto: authences...
422
423
  
  	err = -ENAMETOOLONG;
104880a6b   Herbert Xu   crypto: authences...
424
425
  	if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
  		     "authencesn(%s,%s)", auth_base->cra_name,
e75445a84   Herbert Xu   crypto: authences...
426
  		     enc->base.cra_name) >= CRYPTO_MAX_ALG_NAME)
a5079d084   Steffen Klassert   crypto: authences...
427
  		goto err_drop_enc;
104880a6b   Herbert Xu   crypto: authences...
428
  	if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
a5079d084   Steffen Klassert   crypto: authences...
429
  		     "authencesn(%s,%s)", auth_base->cra_driver_name,
e75445a84   Herbert Xu   crypto: authences...
430
  		     enc->base.cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
a5079d084   Steffen Klassert   crypto: authences...
431
  		goto err_drop_enc;
e75445a84   Herbert Xu   crypto: authences...
432
433
434
  	inst->alg.base.cra_flags = (auth_base->cra_flags |
  				    enc->base.cra_flags) & CRYPTO_ALG_ASYNC;
  	inst->alg.base.cra_priority = enc->base.cra_priority * 10 +
104880a6b   Herbert Xu   crypto: authences...
435
  				      auth_base->cra_priority;
e75445a84   Herbert Xu   crypto: authences...
436
  	inst->alg.base.cra_blocksize = enc->base.cra_blocksize;
104880a6b   Herbert Xu   crypto: authences...
437
  	inst->alg.base.cra_alignmask = auth_base->cra_alignmask |
e75445a84   Herbert Xu   crypto: authences...
438
  				       enc->base.cra_alignmask;
104880a6b   Herbert Xu   crypto: authences...
439
  	inst->alg.base.cra_ctxsize = sizeof(struct crypto_authenc_esn_ctx);
e75445a84   Herbert Xu   crypto: authences...
440
441
  	inst->alg.ivsize = crypto_skcipher_alg_ivsize(enc);
  	inst->alg.chunksize = crypto_skcipher_alg_chunksize(enc);
104880a6b   Herbert Xu   crypto: authences...
442
  	inst->alg.maxauthsize = auth->digestsize;
a5079d084   Steffen Klassert   crypto: authences...
443

104880a6b   Herbert Xu   crypto: authences...
444
445
  	inst->alg.init = crypto_authenc_esn_init_tfm;
  	inst->alg.exit = crypto_authenc_esn_exit_tfm;
a5079d084   Steffen Klassert   crypto: authences...
446

104880a6b   Herbert Xu   crypto: authences...
447
448
449
450
  	inst->alg.setkey = crypto_authenc_esn_setkey;
  	inst->alg.setauthsize = crypto_authenc_esn_setauthsize;
  	inst->alg.encrypt = crypto_authenc_esn_encrypt;
  	inst->alg.decrypt = crypto_authenc_esn_decrypt;
a5079d084   Steffen Klassert   crypto: authences...
451

104880a6b   Herbert Xu   crypto: authences...
452
  	inst->free = crypto_authenc_esn_free,
a5079d084   Steffen Klassert   crypto: authences...
453

104880a6b   Herbert Xu   crypto: authences...
454
455
456
  	err = aead_register_instance(tmpl, inst);
  	if (err)
  		goto err_drop_enc;
a5079d084   Steffen Klassert   crypto: authences...
457
458
459
  
  out:
  	crypto_mod_put(auth_base);
104880a6b   Herbert Xu   crypto: authences...
460
  	return err;
a5079d084   Steffen Klassert   crypto: authences...
461
462
463
464
465
466
467
468
  
  err_drop_enc:
  	crypto_drop_skcipher(&ctx->enc);
  err_drop_auth:
  	crypto_drop_ahash(&ctx->auth);
  err_free_inst:
  	kfree(inst);
  out_put_auth:
a5079d084   Steffen Klassert   crypto: authences...
469
470
  	goto out;
  }
a5079d084   Steffen Klassert   crypto: authences...
471
472
  static struct crypto_template crypto_authenc_esn_tmpl = {
  	.name = "authencesn",
104880a6b   Herbert Xu   crypto: authences...
473
  	.create = crypto_authenc_esn_create,
a5079d084   Steffen Klassert   crypto: authences...
474
475
476
477
478
479
480
481
482
483
484
485
  	.module = THIS_MODULE,
  };
  
  static int __init crypto_authenc_esn_module_init(void)
  {
  	return crypto_register_template(&crypto_authenc_esn_tmpl);
  }
  
  static void __exit crypto_authenc_esn_module_exit(void)
  {
  	crypto_unregister_template(&crypto_authenc_esn_tmpl);
  }
c4741b230   Eric Biggers   crypto: run initc...
486
  subsys_initcall(crypto_authenc_esn_module_init);
a5079d084   Steffen Klassert   crypto: authences...
487
488
489
490
491
  module_exit(crypto_authenc_esn_module_exit);
  
  MODULE_LICENSE("GPL");
  MODULE_AUTHOR("Steffen Klassert <steffen.klassert@secunet.com>");
  MODULE_DESCRIPTION("AEAD wrapper for IPsec with extended sequence numbers");
4943ba16b   Kees Cook   crypto: include c...
492
  MODULE_ALIAS_CRYPTO("authencesn");