Blame view

crypto/authencesn.c 15.2 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
88
89
90
91
92
93
  					   CRYPTO_TFM_RES_MASK);
  
  out:
  	return err;
  
  badkey:
  	crypto_aead_set_flags(authenc_esn, CRYPTO_TFM_RES_BAD_KEY_LEN);
  	goto out;
  }
104880a6b   Herbert Xu   crypto: authences...
94
95
  static int crypto_authenc_esn_genicv_tail(struct aead_request *req,
  					  unsigned int flags)
a5079d084   Steffen Klassert   crypto: authences...
96
  {
a5079d084   Steffen Klassert   crypto: authences...
97
98
99
  	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...
100
101
102
103
104
105
106
107
  	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...
108

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

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

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

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

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

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

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

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

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

104880a6b   Herbert Xu   crypto: authences...
168
  	authenc_esn_request_complete(areq, err);
a5079d084   Steffen Klassert   crypto: authences...
169
  }
104880a6b   Herbert Xu   crypto: authences...
170
  static int crypto_authenc_esn_copy(struct aead_request *req, unsigned int len)
a5079d084   Steffen Klassert   crypto: authences...
171
172
173
  {
  	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...
174
  	SKCIPHER_REQUEST_ON_STACK(skreq, ctx->null);
a5079d084   Steffen Klassert   crypto: authences...
175

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

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

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

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

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

104880a6b   Herbert Xu   crypto: authences...
236
237
238
239
  	/* 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...
240

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

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

e75445a84   Herbert Xu   crypto: authences...
247
248
249
250
  	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...
251

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

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

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

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

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

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

104880a6b   Herbert Xu   crypto: authences...
293
294
295
296
  	/* 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...
297

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

104880a6b   Herbert Xu   crypto: authences...
301
302
303
304
  	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...
305

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

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

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

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

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

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

104880a6b   Herbert Xu   crypto: authences...
448
449
450
451
  	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...
452

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

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