Blame view

crypto/authencesn.c 15.3 KB
a5079d084   Steffen Klassert   crypto: authences...
1
2
3
4
5
6
  /*
   * 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...
7
   * Copyright (c) 2015 Herbert Xu <herbert@gondor.apana.org.au>
a5079d084   Steffen Klassert   crypto: authences...
8
9
10
11
12
13
14
   *
   * This program is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License as published by the Free
   * Software Foundation; either version 2 of the License, or (at your option)
   * any later version.
   *
   */
7fb2a4bdc   Herbert Xu   crypto: authences...
15
  #include <crypto/internal/aead.h>
a5079d084   Steffen Klassert   crypto: authences...
16
17
18
  #include <crypto/internal/hash.h>
  #include <crypto/internal/skcipher.h>
  #include <crypto/authenc.h>
104880a6b   Herbert Xu   crypto: authences...
19
  #include <crypto/null.h>
a5079d084   Steffen Klassert   crypto: authences...
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
  #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...
37
38
  	struct crypto_skcipher *enc;
  	struct crypto_skcipher *null;
a5079d084   Steffen Klassert   crypto: authences...
39
40
41
  };
  
  struct authenc_esn_request_ctx {
104880a6b   Herbert Xu   crypto: authences...
42
43
  	struct scatterlist src[2];
  	struct scatterlist dst[2];
a5079d084   Steffen Klassert   crypto: authences...
44
45
46
47
48
49
50
51
  	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...
52
53
54
55
56
57
58
59
  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...
60
61
62
  static int crypto_authenc_esn_setkey(struct crypto_aead *authenc_esn, const u8 *key,
  				     unsigned int keylen)
  {
a5079d084   Steffen Klassert   crypto: authences...
63
64
  	struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(authenc_esn);
  	struct crypto_ahash *auth = ctx->auth;
e75445a84   Herbert Xu   crypto: authences...
65
  	struct crypto_skcipher *enc = ctx->enc;
fddc2c43c   Mathias Krause   crypto: authences...
66
  	struct crypto_authenc_keys keys;
a5079d084   Steffen Klassert   crypto: authences...
67
  	int err = -EINVAL;
fddc2c43c   Mathias Krause   crypto: authences...
68
  	if (crypto_authenc_extractkeys(&keys, key, keylen) != 0)
a5079d084   Steffen Klassert   crypto: authences...
69
  		goto badkey;
a5079d084   Steffen Klassert   crypto: authences...
70
71
72
73
  
  	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...
74
  	err = crypto_ahash_setkey(auth, keys.authkey, keys.authkeylen);
a5079d084   Steffen Klassert   crypto: authences...
75
76
77
78
79
  	crypto_aead_set_flags(authenc_esn, crypto_ahash_get_flags(auth) &
  					   CRYPTO_TFM_RES_MASK);
  
  	if (err)
  		goto out;
e75445a84   Herbert Xu   crypto: authences...
80
81
  	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...
82
  					 CRYPTO_TFM_REQ_MASK);
e75445a84   Herbert Xu   crypto: authences...
83
84
  	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...
85
86
87
  					   CRYPTO_TFM_RES_MASK);
  
  out:
a07fc8dd2   Tudor-Dan Ambarus   crypto: authences...
88
  	memzero_explicit(&keys, sizeof(keys));
a5079d084   Steffen Klassert   crypto: authences...
89
90
91
92
93
94
  	return err;
  
  badkey:
  	crypto_aead_set_flags(authenc_esn, CRYPTO_TFM_RES_BAD_KEY_LEN);
  	goto out;
  }
104880a6b   Herbert Xu   crypto: authences...
95
96
  static int crypto_authenc_esn_genicv_tail(struct aead_request *req,
  					  unsigned int flags)
a5079d084   Steffen Klassert   crypto: authences...
97
  {
a5079d084   Steffen Klassert   crypto: authences...
98
99
100
  	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...
101
102
103
104
105
106
107
108
  	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...
109

104880a6b   Herbert Xu   crypto: authences...
110
111
112
113
  	/* 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...
114

104880a6b   Herbert Xu   crypto: authences...
115
116
  	scatterwalk_map_and_copy(hash, dst, assoclen + cryptlen, authsize, 1);
  	return 0;
a5079d084   Steffen Klassert   crypto: authences...
117
  }
a5079d084   Steffen Klassert   crypto: authences...
118
119
120
121
  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...
122

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

104880a6b   Herbert Xu   crypto: authences...
142
143
  	if (!authsize)
  		return 0;
a5079d084   Steffen Klassert   crypto: authences...
144

104880a6b   Herbert Xu   crypto: authences...
145
146
147
148
  	/* 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...
149

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

104880a6b   Herbert Xu   crypto: authences...
153
154
155
156
  	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...
157

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

104880a6b   Herbert Xu   crypto: authences...
166
167
  	if (!err)
  		err = crypto_authenc_esn_genicv(areq, 0);
a5079d084   Steffen Klassert   crypto: authences...
168

104880a6b   Herbert Xu   crypto: authences...
169
  	authenc_esn_request_complete(areq, err);
a5079d084   Steffen Klassert   crypto: authences...
170
  }
104880a6b   Herbert Xu   crypto: authences...
171
  static int crypto_authenc_esn_copy(struct aead_request *req, unsigned int len)
a5079d084   Steffen Klassert   crypto: authences...
172
173
174
  {
  	struct crypto_aead *authenc_esn = crypto_aead_reqtfm(req);
  	struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(authenc_esn);
e75445a84   Herbert Xu   crypto: authences...
175
  	SKCIPHER_REQUEST_ON_STACK(skreq, ctx->null);
a5079d084   Steffen Klassert   crypto: authences...
176

e75445a84   Herbert Xu   crypto: authences...
177
178
179
180
181
182
  	skcipher_request_set_tfm(skreq, ctx->null);
  	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...
183
  }
104880a6b   Herbert Xu   crypto: authences...
184
  static int crypto_authenc_esn_encrypt(struct aead_request *req)
a5079d084   Steffen Klassert   crypto: authences...
185
186
187
  {
  	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...
188
  	struct crypto_authenc_esn_ctx *ctx = crypto_aead_ctx(authenc_esn);
e75445a84   Herbert Xu   crypto: authences...
189
190
191
  	struct skcipher_request *skreq = (void *)(areq_ctx->tail +
  						  ctx->reqoff);
  	struct crypto_skcipher *enc = ctx->enc;
104880a6b   Herbert Xu   crypto: authences...
192
  	unsigned int assoclen = req->assoclen;
a5079d084   Steffen Klassert   crypto: authences...
193
  	unsigned int cryptlen = req->cryptlen;
104880a6b   Herbert Xu   crypto: authences...
194
195
  	struct scatterlist *src, *dst;
  	int err;
a5079d084   Steffen Klassert   crypto: authences...
196

104880a6b   Herbert Xu   crypto: authences...
197
198
199
  	sg_init_table(areq_ctx->src, 2);
  	src = scatterwalk_ffwd(areq_ctx->src, req->src, assoclen);
  	dst = src;
a5079d084   Steffen Klassert   crypto: authences...
200

104880a6b   Herbert Xu   crypto: authences...
201
202
203
204
  	if (req->src != req->dst) {
  		err = crypto_authenc_esn_copy(req, assoclen);
  		if (err)
  			return err;
a5079d084   Steffen Klassert   crypto: authences...
205

104880a6b   Herbert Xu   crypto: authences...
206
207
  		sg_init_table(areq_ctx->dst, 2);
  		dst = scatterwalk_ffwd(areq_ctx->dst, req->dst, assoclen);
a5079d084   Steffen Klassert   crypto: authences...
208
  	}
e75445a84   Herbert Xu   crypto: authences...
209
210
211
212
  	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...
213

e75445a84   Herbert Xu   crypto: authences...
214
  	err = crypto_skcipher_encrypt(skreq);
a5079d084   Steffen Klassert   crypto: authences...
215
216
  	if (err)
  		return err;
104880a6b   Herbert Xu   crypto: authences...
217
  	return crypto_authenc_esn_genicv(req, aead_request_flags(req));
a5079d084   Steffen Klassert   crypto: authences...
218
  }
104880a6b   Herbert Xu   crypto: authences...
219
220
  static int crypto_authenc_esn_decrypt_tail(struct aead_request *req,
  					   unsigned int flags)
a5079d084   Steffen Klassert   crypto: authences...
221
  {
104880a6b   Herbert Xu   crypto: authences...
222
223
224
225
  	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...
226
227
  	struct skcipher_request *skreq = (void *)(areq_ctx->tail +
  						  ctx->reqoff);
104880a6b   Herbert Xu   crypto: authences...
228
229
230
231
232
233
234
235
  	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...
236

41cdf7a45   Herbert Xu   crypto: authences...
237
238
  	if (!authsize)
  		goto decrypt;
104880a6b   Herbert Xu   crypto: authences...
239
240
241
242
  	/* 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...
243

104880a6b   Herbert Xu   crypto: authences...
244
245
  	if (crypto_memneq(ihash, ohash, authsize))
  		return -EBADMSG;
a5079d084   Steffen Klassert   crypto: authences...
246

41cdf7a45   Herbert Xu   crypto: authences...
247
  decrypt:
104880a6b   Herbert Xu   crypto: authences...
248
249
  	sg_init_table(areq_ctx->dst, 2);
  	dst = scatterwalk_ffwd(areq_ctx->dst, dst, assoclen);
a5079d084   Steffen Klassert   crypto: authences...
250

e75445a84   Herbert Xu   crypto: authences...
251
252
253
254
  	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...
255

e75445a84   Herbert Xu   crypto: authences...
256
  	return crypto_skcipher_decrypt(skreq);
a5079d084   Steffen Klassert   crypto: authences...
257
  }
104880a6b   Herbert Xu   crypto: authences...
258
259
  static void authenc_esn_verify_ahash_done(struct crypto_async_request *areq,
  					  int err)
a5079d084   Steffen Klassert   crypto: authences...
260
  {
104880a6b   Herbert Xu   crypto: authences...
261
  	struct aead_request *req = areq->data;
a5079d084   Steffen Klassert   crypto: authences...
262

104880a6b   Herbert Xu   crypto: authences...
263
264
  	err = err ?: crypto_authenc_esn_decrypt_tail(req, 0);
  	aead_request_complete(req, err);
a5079d084   Steffen Klassert   crypto: authences...
265
  }
104880a6b   Herbert Xu   crypto: authences...
266
  static int crypto_authenc_esn_decrypt(struct aead_request *req)
a5079d084   Steffen Klassert   crypto: authences...
267
268
269
  {
  	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...
270
271
272
273
274
275
276
277
278
279
280
281
  	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...
282

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

104880a6b   Herbert Xu   crypto: authences...
285
286
287
288
289
  	if (req->src != dst) {
  		err = crypto_authenc_esn_copy(req, assoclen + cryptlen);
  		if (err)
  			return err;
  	}
a5079d084   Steffen Klassert   crypto: authences...
290

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

104880a6b   Herbert Xu   crypto: authences...
294
295
  	if (!authsize)
  		goto tail;
a5079d084   Steffen Klassert   crypto: authences...
296

104880a6b   Herbert Xu   crypto: authences...
297
298
299
300
  	/* 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...
301

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

104880a6b   Herbert Xu   crypto: authences...
305
306
307
308
  	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...
309

104880a6b   Herbert Xu   crypto: authences...
310
  	err = crypto_ahash_digest(ahreq);
a5079d084   Steffen Klassert   crypto: authences...
311
312
  	if (err)
  		return err;
104880a6b   Herbert Xu   crypto: authences...
313
314
  tail:
  	return crypto_authenc_esn_decrypt_tail(req, aead_request_flags(req));
a5079d084   Steffen Klassert   crypto: authences...
315
  }
104880a6b   Herbert Xu   crypto: authences...
316
  static int crypto_authenc_esn_init_tfm(struct crypto_aead *tfm)
a5079d084   Steffen Klassert   crypto: authences...
317
  {
104880a6b   Herbert Xu   crypto: authences...
318
319
320
  	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...
321
  	struct crypto_ahash *auth;
e75445a84   Herbert Xu   crypto: authences...
322
323
  	struct crypto_skcipher *enc;
  	struct crypto_skcipher *null;
a5079d084   Steffen Klassert   crypto: authences...
324
325
326
327
328
  	int err;
  
  	auth = crypto_spawn_ahash(&ictx->auth);
  	if (IS_ERR(auth))
  		return PTR_ERR(auth);
60425a8ba   Eric Biggers   crypto: skcipher ...
329
  	enc = crypto_spawn_skcipher(&ictx->enc);
a5079d084   Steffen Klassert   crypto: authences...
330
331
332
  	err = PTR_ERR(enc);
  	if (IS_ERR(enc))
  		goto err_free_ahash;
e75445a84   Herbert Xu   crypto: authences...
333
  	null = crypto_get_default_null_skcipher2();
104880a6b   Herbert Xu   crypto: authences...
334
335
336
  	err = PTR_ERR(null);
  	if (IS_ERR(null))
  		goto err_free_skcipher;
a5079d084   Steffen Klassert   crypto: authences...
337
338
  	ctx->auth = auth;
  	ctx->enc = enc;
104880a6b   Herbert Xu   crypto: authences...
339
  	ctx->null = null;
a5079d084   Steffen Klassert   crypto: authences...
340

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

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

5e4b8c1fc   Herbert Xu   crypto: aead - Re...
394
  	if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
104880a6b   Herbert Xu   crypto: authences...
395
  		return -EINVAL;
a5079d084   Steffen Klassert   crypto: authences...
396
397
  
  	auth = ahash_attr_alg(tb[1], CRYPTO_ALG_TYPE_HASH,
927ef32dc   Herbert Xu   crypto: authenc -...
398
399
  			      CRYPTO_ALG_TYPE_AHASH_MASK |
  			      crypto_requires_sync(algt->type, algt->mask));
a5079d084   Steffen Klassert   crypto: authences...
400
  	if (IS_ERR(auth))
104880a6b   Herbert Xu   crypto: authences...
401
  		return PTR_ERR(auth);
a5079d084   Steffen Klassert   crypto: authences...
402
403
404
405
406
407
408
409
410
411
412
413
  
  	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...
414
  	ctx = aead_instance_ctx(inst);
a5079d084   Steffen Klassert   crypto: authences...
415

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

104880a6b   Herbert Xu   crypto: authences...
449
450
  	inst->alg.init = crypto_authenc_esn_init_tfm;
  	inst->alg.exit = crypto_authenc_esn_exit_tfm;
a5079d084   Steffen Klassert   crypto: authences...
451

104880a6b   Herbert Xu   crypto: authences...
452
453
454
455
  	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...
456

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

104880a6b   Herbert Xu   crypto: authences...
459
460
461
  	err = aead_register_instance(tmpl, inst);
  	if (err)
  		goto err_drop_enc;
a5079d084   Steffen Klassert   crypto: authences...
462
463
464
  
  out:
  	crypto_mod_put(auth_base);
104880a6b   Herbert Xu   crypto: authences...
465
  	return err;
a5079d084   Steffen Klassert   crypto: authences...
466
467
468
469
470
471
472
473
  
  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...
474
475
  	goto out;
  }
a5079d084   Steffen Klassert   crypto: authences...
476
477
  static struct crypto_template crypto_authenc_esn_tmpl = {
  	.name = "authencesn",
104880a6b   Herbert Xu   crypto: authences...
478
  	.create = crypto_authenc_esn_create,
a5079d084   Steffen Klassert   crypto: authences...
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
  	.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);
  }
  
  module_init(crypto_authenc_esn_module_init);
  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...
498
  MODULE_ALIAS_CRYPTO("authencesn");