Blame view

crypto/authencesn.c 14.2 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)
674f368a9   Eric Biggers   crypto: remove CR...
64
  		goto out;
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
  	if (err)
  		goto out;
e75445a84   Herbert Xu   crypto: authences...
72
73
  	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...
74
  					 CRYPTO_TFM_REQ_MASK);
e75445a84   Herbert Xu   crypto: authences...
75
  	err = crypto_skcipher_setkey(enc, keys.enckey, keys.enckeylen);
a5079d084   Steffen Klassert   crypto: authences...
76
  out:
31545df39   Tudor-Dan Ambarus   crypto: authences...
77
  	memzero_explicit(&keys, sizeof(keys));
a5079d084   Steffen Klassert   crypto: authences...
78
  	return err;
a5079d084   Steffen Klassert   crypto: authences...
79
  }
104880a6b   Herbert Xu   crypto: authences...
80
81
  static int crypto_authenc_esn_genicv_tail(struct aead_request *req,
  					  unsigned int flags)
a5079d084   Steffen Klassert   crypto: authences...
82
  {
a5079d084   Steffen Klassert   crypto: authences...
83
84
85
  	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...
86
87
88
89
90
91
92
93
  	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...
94

104880a6b   Herbert Xu   crypto: authences...
95
96
97
98
  	/* 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...
99

104880a6b   Herbert Xu   crypto: authences...
100
101
  	scatterwalk_map_and_copy(hash, dst, assoclen + cryptlen, authsize, 1);
  	return 0;
a5079d084   Steffen Klassert   crypto: authences...
102
  }
a5079d084   Steffen Klassert   crypto: authences...
103
104
105
106
  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...
107

104880a6b   Herbert Xu   crypto: authences...
108
  	err = err ?: crypto_authenc_esn_genicv_tail(req, 0);
a5079d084   Steffen Klassert   crypto: authences...
109
110
  	aead_request_complete(req, err);
  }
104880a6b   Herbert Xu   crypto: authences...
111
112
  static int crypto_authenc_esn_genicv(struct aead_request *req,
  				     unsigned int flags)
a5079d084   Steffen Klassert   crypto: authences...
113
  {
a5079d084   Steffen Klassert   crypto: authences...
114
  	struct crypto_aead *authenc_esn = crypto_aead_reqtfm(req);
a5079d084   Steffen Klassert   crypto: authences...
115
  	struct authenc_esn_request_ctx *areq_ctx = aead_request_ctx(req);
a5079d084   Steffen Klassert   crypto: authences...
116
  	struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(authenc_esn);
104880a6b   Herbert Xu   crypto: authences...
117
118
119
  	struct crypto_ahash *auth = ctx->auth;
  	u8 *hash = PTR_ALIGN((u8 *)areq_ctx->tail,
  			     crypto_ahash_alignmask(auth) + 1);
a5079d084   Steffen Klassert   crypto: authences...
120
  	struct ahash_request *ahreq = (void *)(areq_ctx->tail + ctx->reqoff);
104880a6b   Herbert Xu   crypto: authences...
121
122
  	unsigned int authsize = crypto_aead_authsize(authenc_esn);
  	unsigned int assoclen = req->assoclen;
a5079d084   Steffen Klassert   crypto: authences...
123
  	unsigned int cryptlen = req->cryptlen;
104880a6b   Herbert Xu   crypto: authences...
124
125
  	struct scatterlist *dst = req->dst;
  	u32 tmp[2];
a5079d084   Steffen Klassert   crypto: authences...
126

104880a6b   Herbert Xu   crypto: authences...
127
128
  	if (!authsize)
  		return 0;
a5079d084   Steffen Klassert   crypto: authences...
129

104880a6b   Herbert Xu   crypto: authences...
130
131
132
133
  	/* 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...
134

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

104880a6b   Herbert Xu   crypto: authences...
138
139
140
141
  	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...
142

104880a6b   Herbert Xu   crypto: authences...
143
144
  	return crypto_ahash_digest(ahreq) ?:
  	       crypto_authenc_esn_genicv_tail(req, aead_request_flags(req));
a5079d084   Steffen Klassert   crypto: authences...
145
  }
104880a6b   Herbert Xu   crypto: authences...
146
147
  static void crypto_authenc_esn_encrypt_done(struct crypto_async_request *req,
  					    int err)
a5079d084   Steffen Klassert   crypto: authences...
148
  {
104880a6b   Herbert Xu   crypto: authences...
149
  	struct aead_request *areq = req->data;
a5079d084   Steffen Klassert   crypto: authences...
150

104880a6b   Herbert Xu   crypto: authences...
151
152
  	if (!err)
  		err = crypto_authenc_esn_genicv(areq, 0);
a5079d084   Steffen Klassert   crypto: authences...
153

104880a6b   Herbert Xu   crypto: authences...
154
  	authenc_esn_request_complete(areq, err);
a5079d084   Steffen Klassert   crypto: authences...
155
  }
104880a6b   Herbert Xu   crypto: authences...
156
  static int crypto_authenc_esn_copy(struct aead_request *req, unsigned int len)
a5079d084   Steffen Klassert   crypto: authences...
157
158
159
  {
  	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...
160
  	SYNC_SKCIPHER_REQUEST_ON_STACK(skreq, ctx->null);
a5079d084   Steffen Klassert   crypto: authences...
161

8d6053984   Kees Cook   crypto: null - Re...
162
  	skcipher_request_set_sync_tfm(skreq, ctx->null);
e75445a84   Herbert Xu   crypto: authences...
163
164
165
166
167
  	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...
168
  }
104880a6b   Herbert Xu   crypto: authences...
169
  static int crypto_authenc_esn_encrypt(struct aead_request *req)
a5079d084   Steffen Klassert   crypto: authences...
170
171
172
  {
  	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...
173
  	struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(authenc_esn);
e75445a84   Herbert Xu   crypto: authences...
174
175
176
  	struct skcipher_request *skreq = (void *)(areq_ctx->tail +
  						  ctx->reqoff);
  	struct crypto_skcipher *enc = ctx->enc;
104880a6b   Herbert Xu   crypto: authences...
177
  	unsigned int assoclen = req->assoclen;
a5079d084   Steffen Klassert   crypto: authences...
178
  	unsigned int cryptlen = req->cryptlen;
104880a6b   Herbert Xu   crypto: authences...
179
180
  	struct scatterlist *src, *dst;
  	int err;
a5079d084   Steffen Klassert   crypto: authences...
181

104880a6b   Herbert Xu   crypto: authences...
182
183
184
  	sg_init_table(areq_ctx->src, 2);
  	src = scatterwalk_ffwd(areq_ctx->src, req->src, assoclen);
  	dst = src;
a5079d084   Steffen Klassert   crypto: authences...
185

104880a6b   Herbert Xu   crypto: authences...
186
187
188
189
  	if (req->src != req->dst) {
  		err = crypto_authenc_esn_copy(req, assoclen);
  		if (err)
  			return err;
a5079d084   Steffen Klassert   crypto: authences...
190

104880a6b   Herbert Xu   crypto: authences...
191
192
  		sg_init_table(areq_ctx->dst, 2);
  		dst = scatterwalk_ffwd(areq_ctx->dst, req->dst, assoclen);
a5079d084   Steffen Klassert   crypto: authences...
193
  	}
e75445a84   Herbert Xu   crypto: authences...
194
195
196
197
  	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...
198

e75445a84   Herbert Xu   crypto: authences...
199
  	err = crypto_skcipher_encrypt(skreq);
a5079d084   Steffen Klassert   crypto: authences...
200
201
  	if (err)
  		return err;
104880a6b   Herbert Xu   crypto: authences...
202
  	return crypto_authenc_esn_genicv(req, aead_request_flags(req));
a5079d084   Steffen Klassert   crypto: authences...
203
  }
104880a6b   Herbert Xu   crypto: authences...
204
205
  static int crypto_authenc_esn_decrypt_tail(struct aead_request *req,
  					   unsigned int flags)
a5079d084   Steffen Klassert   crypto: authences...
206
  {
104880a6b   Herbert Xu   crypto: authences...
207
208
209
210
  	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...
211
212
  	struct skcipher_request *skreq = (void *)(areq_ctx->tail +
  						  ctx->reqoff);
104880a6b   Herbert Xu   crypto: authences...
213
214
215
216
217
218
219
220
  	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...
221

41cdf7a45   Herbert Xu   crypto: authences...
222
223
  	if (!authsize)
  		goto decrypt;
104880a6b   Herbert Xu   crypto: authences...
224
225
226
227
  	/* 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...
228

104880a6b   Herbert Xu   crypto: authences...
229
230
  	if (crypto_memneq(ihash, ohash, authsize))
  		return -EBADMSG;
a5079d084   Steffen Klassert   crypto: authences...
231

41cdf7a45   Herbert Xu   crypto: authences...
232
  decrypt:
104880a6b   Herbert Xu   crypto: authences...
233
234
  	sg_init_table(areq_ctx->dst, 2);
  	dst = scatterwalk_ffwd(areq_ctx->dst, dst, assoclen);
a5079d084   Steffen Klassert   crypto: authences...
235

e75445a84   Herbert Xu   crypto: authences...
236
237
238
239
  	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...
240

e75445a84   Herbert Xu   crypto: authences...
241
  	return crypto_skcipher_decrypt(skreq);
a5079d084   Steffen Klassert   crypto: authences...
242
  }
104880a6b   Herbert Xu   crypto: authences...
243
244
  static void authenc_esn_verify_ahash_done(struct crypto_async_request *areq,
  					  int err)
a5079d084   Steffen Klassert   crypto: authences...
245
  {
104880a6b   Herbert Xu   crypto: authences...
246
  	struct aead_request *req = areq->data;
a5079d084   Steffen Klassert   crypto: authences...
247

104880a6b   Herbert Xu   crypto: authences...
248
  	err = err ?: crypto_authenc_esn_decrypt_tail(req, 0);
a77733636   Harsh Jain   crypto: authences...
249
  	authenc_esn_request_complete(req, err);
a5079d084   Steffen Klassert   crypto: authences...
250
  }
104880a6b   Herbert Xu   crypto: authences...
251
  static int crypto_authenc_esn_decrypt(struct aead_request *req)
a5079d084   Steffen Klassert   crypto: authences...
252
253
254
  {
  	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...
255
256
257
258
259
260
261
262
263
264
265
266
  	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...
267

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

104880a6b   Herbert Xu   crypto: authences...
270
271
272
273
274
  	if (req->src != dst) {
  		err = crypto_authenc_esn_copy(req, assoclen + cryptlen);
  		if (err)
  			return err;
  	}
a5079d084   Steffen Klassert   crypto: authences...
275

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

104880a6b   Herbert Xu   crypto: authences...
279
280
  	if (!authsize)
  		goto tail;
a5079d084   Steffen Klassert   crypto: authences...
281

104880a6b   Herbert Xu   crypto: authences...
282
283
284
285
  	/* 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...
286

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

104880a6b   Herbert Xu   crypto: authences...
290
291
292
293
  	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...
294

104880a6b   Herbert Xu   crypto: authences...
295
  	err = crypto_ahash_digest(ahreq);
a5079d084   Steffen Klassert   crypto: authences...
296
297
  	if (err)
  		return err;
104880a6b   Herbert Xu   crypto: authences...
298
299
  tail:
  	return crypto_authenc_esn_decrypt_tail(req, aead_request_flags(req));
a5079d084   Steffen Klassert   crypto: authences...
300
  }
104880a6b   Herbert Xu   crypto: authences...
301
  static int crypto_authenc_esn_init_tfm(struct crypto_aead *tfm)
a5079d084   Steffen Klassert   crypto: authences...
302
  {
104880a6b   Herbert Xu   crypto: authences...
303
304
305
  	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...
306
  	struct crypto_ahash *auth;
e75445a84   Herbert Xu   crypto: authences...
307
  	struct crypto_skcipher *enc;
8d6053984   Kees Cook   crypto: null - Re...
308
  	struct crypto_sync_skcipher *null;
a5079d084   Steffen Klassert   crypto: authences...
309
310
311
312
313
  	int err;
  
  	auth = crypto_spawn_ahash(&ictx->auth);
  	if (IS_ERR(auth))
  		return PTR_ERR(auth);
60425a8ba   Eric Biggers   crypto: skcipher ...
314
  	enc = crypto_spawn_skcipher(&ictx->enc);
a5079d084   Steffen Klassert   crypto: authences...
315
316
317
  	err = PTR_ERR(enc);
  	if (IS_ERR(enc))
  		goto err_free_ahash;
3a2d4fb51   Eric Biggers   crypto: null - Ge...
318
  	null = crypto_get_default_null_skcipher();
104880a6b   Herbert Xu   crypto: authences...
319
320
321
  	err = PTR_ERR(null);
  	if (IS_ERR(null))
  		goto err_free_skcipher;
a5079d084   Steffen Klassert   crypto: authences...
322
323
  	ctx->auth = auth;
  	ctx->enc = enc;
104880a6b   Herbert Xu   crypto: authences...
324
  	ctx->null = null;
a5079d084   Steffen Klassert   crypto: authences...
325

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

104880a6b   Herbert Xu   crypto: authences...
329
330
  	crypto_aead_set_reqsize(
  		tfm,
6650d09b3   Herbert Xu   crypto: authences...
331
332
333
  		sizeof(struct authenc_esn_request_ctx) +
  		ctx->reqoff +
  		max_t(unsigned int,
e75445a84   Herbert Xu   crypto: authences...
334
335
336
337
  		      crypto_ahash_reqsize(auth) +
  		      sizeof(struct ahash_request),
  		      sizeof(struct skcipher_request) +
  		      crypto_skcipher_reqsize(enc)));
a5079d084   Steffen Klassert   crypto: authences...
338
339
  
  	return 0;
104880a6b   Herbert Xu   crypto: authences...
340
  err_free_skcipher:
e75445a84   Herbert Xu   crypto: authences...
341
  	crypto_free_skcipher(enc);
a5079d084   Steffen Klassert   crypto: authences...
342
343
344
345
  err_free_ahash:
  	crypto_free_ahash(auth);
  	return err;
  }
104880a6b   Herbert Xu   crypto: authences...
346
  static void crypto_authenc_esn_exit_tfm(struct crypto_aead *tfm)
a5079d084   Steffen Klassert   crypto: authences...
347
  {
104880a6b   Herbert Xu   crypto: authences...
348
  	struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(tfm);
a5079d084   Steffen Klassert   crypto: authences...
349
350
  
  	crypto_free_ahash(ctx->auth);
e75445a84   Herbert Xu   crypto: authences...
351
  	crypto_free_skcipher(ctx->enc);
3a2d4fb51   Eric Biggers   crypto: null - Ge...
352
  	crypto_put_default_null_skcipher();
104880a6b   Herbert Xu   crypto: authences...
353
354
355
356
357
358
359
360
361
  }
  
  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...
362
  }
104880a6b   Herbert Xu   crypto: authences...
363
364
  static int crypto_authenc_esn_create(struct crypto_template *tmpl,
  				     struct rtattr **tb)
a5079d084   Steffen Klassert   crypto: authences...
365
  {
b9f76dddb   Eric Biggers   crypto: skcipher ...
366
  	u32 mask;
104880a6b   Herbert Xu   crypto: authences...
367
  	struct aead_instance *inst;
370738824   Eric Biggers   crypto: authences...
368
  	struct authenc_esn_instance_ctx *ctx;
a5079d084   Steffen Klassert   crypto: authences...
369
370
  	struct hash_alg_common *auth;
  	struct crypto_alg *auth_base;
e75445a84   Herbert Xu   crypto: authences...
371
  	struct skcipher_alg *enc;
a5079d084   Steffen Klassert   crypto: authences...
372
  	int err;
7bcb2c99f   Eric Biggers   crypto: algapi - ...
373
374
375
  	err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_AEAD, &mask);
  	if (err)
  		return err;
b9f76dddb   Eric Biggers   crypto: skcipher ...
376

a5079d084   Steffen Klassert   crypto: authences...
377
  	inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
a5079d084   Steffen Klassert   crypto: authences...
378
  	if (!inst)
370738824   Eric Biggers   crypto: authences...
379
  		return -ENOMEM;
104880a6b   Herbert Xu   crypto: authences...
380
  	ctx = aead_instance_ctx(inst);
a5079d084   Steffen Klassert   crypto: authences...
381

370738824   Eric Biggers   crypto: authences...
382
383
  	err = crypto_grab_ahash(&ctx->auth, aead_crypto_instance(inst),
  				crypto_attr_alg_name(tb[1]), 0, mask);
a5079d084   Steffen Klassert   crypto: authences...
384
385
  	if (err)
  		goto err_free_inst;
370738824   Eric Biggers   crypto: authences...
386
387
  	auth = crypto_spawn_ahash_alg(&ctx->auth);
  	auth_base = &auth->base;
a5079d084   Steffen Klassert   crypto: authences...
388

b9f76dddb   Eric Biggers   crypto: skcipher ...
389
  	err = crypto_grab_skcipher(&ctx->enc, aead_crypto_instance(inst),
370738824   Eric Biggers   crypto: authences...
390
  				   crypto_attr_alg_name(tb[2]), 0, mask);
a5079d084   Steffen Klassert   crypto: authences...
391
  	if (err)
370738824   Eric Biggers   crypto: authences...
392
  		goto err_free_inst;
e75445a84   Herbert Xu   crypto: authences...
393
  	enc = crypto_spawn_skcipher_alg(&ctx->enc);
a5079d084   Steffen Klassert   crypto: authences...
394
395
  
  	err = -ENAMETOOLONG;
104880a6b   Herbert Xu   crypto: authences...
396
397
  	if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
  		     "authencesn(%s,%s)", auth_base->cra_name,
e75445a84   Herbert Xu   crypto: authences...
398
  		     enc->base.cra_name) >= CRYPTO_MAX_ALG_NAME)
370738824   Eric Biggers   crypto: authences...
399
  		goto err_free_inst;
a5079d084   Steffen Klassert   crypto: authences...
400

104880a6b   Herbert Xu   crypto: authences...
401
  	if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
a5079d084   Steffen Klassert   crypto: authences...
402
  		     "authencesn(%s,%s)", auth_base->cra_driver_name,
e75445a84   Herbert Xu   crypto: authences...
403
  		     enc->base.cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
370738824   Eric Biggers   crypto: authences...
404
  		goto err_free_inst;
a5079d084   Steffen Klassert   crypto: authences...
405

e75445a84   Herbert Xu   crypto: authences...
406
  	inst->alg.base.cra_priority = enc->base.cra_priority * 10 +
104880a6b   Herbert Xu   crypto: authences...
407
  				      auth_base->cra_priority;
e75445a84   Herbert Xu   crypto: authences...
408
  	inst->alg.base.cra_blocksize = enc->base.cra_blocksize;
104880a6b   Herbert Xu   crypto: authences...
409
  	inst->alg.base.cra_alignmask = auth_base->cra_alignmask |
e75445a84   Herbert Xu   crypto: authences...
410
  				       enc->base.cra_alignmask;
104880a6b   Herbert Xu   crypto: authences...
411
  	inst->alg.base.cra_ctxsize = sizeof(struct crypto_authenc_esn_ctx);
e75445a84   Herbert Xu   crypto: authences...
412
413
  	inst->alg.ivsize = crypto_skcipher_alg_ivsize(enc);
  	inst->alg.chunksize = crypto_skcipher_alg_chunksize(enc);
104880a6b   Herbert Xu   crypto: authences...
414
  	inst->alg.maxauthsize = auth->digestsize;
a5079d084   Steffen Klassert   crypto: authences...
415

104880a6b   Herbert Xu   crypto: authences...
416
417
  	inst->alg.init = crypto_authenc_esn_init_tfm;
  	inst->alg.exit = crypto_authenc_esn_exit_tfm;
a5079d084   Steffen Klassert   crypto: authences...
418

104880a6b   Herbert Xu   crypto: authences...
419
420
421
422
  	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...
423

d1dc4df1f   Eric Biggers   crypto: authences...
424
  	inst->free = crypto_authenc_esn_free;
a5079d084   Steffen Klassert   crypto: authences...
425

104880a6b   Herbert Xu   crypto: authences...
426
  	err = aead_register_instance(tmpl, inst);
370738824   Eric Biggers   crypto: authences...
427
  	if (err) {
a5079d084   Steffen Klassert   crypto: authences...
428
  err_free_inst:
370738824   Eric Biggers   crypto: authences...
429
430
431
  		crypto_authenc_esn_free(inst);
  	}
  	return err;
a5079d084   Steffen Klassert   crypto: authences...
432
  }
a5079d084   Steffen Klassert   crypto: authences...
433
434
  static struct crypto_template crypto_authenc_esn_tmpl = {
  	.name = "authencesn",
104880a6b   Herbert Xu   crypto: authences...
435
  	.create = crypto_authenc_esn_create,
a5079d084   Steffen Klassert   crypto: authences...
436
437
438
439
440
441
442
443
444
445
446
447
  	.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...
448
  subsys_initcall(crypto_authenc_esn_module_init);
a5079d084   Steffen Klassert   crypto: authences...
449
450
451
452
453
  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...
454
  MODULE_ALIAS_CRYPTO("authencesn");