Diff markup
1 /* 1 /*
2 * CCM: Counter with CBC-MAC 2 * CCM: Counter with CBC-MAC
3 * 3 *
4 * (C) Copyright IBM Corp. 2007 - Joy Latten < 4 * (C) Copyright IBM Corp. 2007 - Joy Latten <latten@us.ibm.com>
5 * 5 *
6 * This program is free software; you can redi 6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public L 7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of th 8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version. 9 * any later version.
10 * 10 *
11 */ 11 */
12 12
13 #include <crypto/internal/aead.h> 13 #include <crypto/internal/aead.h>
14 #include <crypto/internal/skcipher.h> 14 #include <crypto/internal/skcipher.h>
15 #include <crypto/scatterwalk.h> 15 #include <crypto/scatterwalk.h>
16 #include <linux/err.h> 16 #include <linux/err.h>
17 #include <linux/init.h> 17 #include <linux/init.h>
18 #include <linux/kernel.h> 18 #include <linux/kernel.h>
19 #include <linux/module.h> 19 #include <linux/module.h>
20 #include <linux/slab.h> 20 #include <linux/slab.h>
21 21
22 #include "internal.h" 22 #include "internal.h"
23 23
24 struct ccm_instance_ctx { 24 struct ccm_instance_ctx {
25 struct crypto_skcipher_spawn ctr; 25 struct crypto_skcipher_spawn ctr;
26 struct crypto_spawn cipher; 26 struct crypto_spawn cipher;
27 }; 27 };
28 28
29 struct crypto_ccm_ctx { 29 struct crypto_ccm_ctx {
30 struct crypto_cipher *cipher; 30 struct crypto_cipher *cipher;
31 struct crypto_ablkcipher *ctr; 31 struct crypto_ablkcipher *ctr;
32 }; 32 };
33 33
34 struct crypto_rfc4309_ctx { 34 struct crypto_rfc4309_ctx {
35 struct crypto_aead *child; 35 struct crypto_aead *child;
36 u8 nonce[3]; 36 u8 nonce[3];
37 }; 37 };
38 38
39 struct crypto_ccm_req_priv_ctx { 39 struct crypto_ccm_req_priv_ctx {
40 u8 odata[16]; 40 u8 odata[16];
41 u8 idata[16]; 41 u8 idata[16];
42 u8 auth_tag[16]; 42 u8 auth_tag[16];
43 u32 ilen; 43 u32 ilen;
44 u32 flags; 44 u32 flags;
45 struct scatterlist src[2]; 45 struct scatterlist src[2];
46 struct scatterlist dst[2]; 46 struct scatterlist dst[2];
47 struct ablkcipher_request abreq; 47 struct ablkcipher_request abreq;
48 }; 48 };
49 49
50 static inline struct crypto_ccm_req_priv_ctx * 50 static inline struct crypto_ccm_req_priv_ctx *crypto_ccm_reqctx(
51 struct aead_request *req) 51 struct aead_request *req)
52 { 52 {
53 unsigned long align = crypto_aead_alig 53 unsigned long align = crypto_aead_alignmask(crypto_aead_reqtfm(req));
54 54
55 return (void *)PTR_ALIGN((u8 *)aead_re 55 return (void *)PTR_ALIGN((u8 *)aead_request_ctx(req), align + 1);
56 } 56 }
57 57
58 static int set_msg_len(u8 *block, unsigned int 58 static int set_msg_len(u8 *block, unsigned int msglen, int csize)
59 { 59 {
60 __be32 data; 60 __be32 data;
61 61
62 memset(block, 0, csize); 62 memset(block, 0, csize);
63 block += csize; 63 block += csize;
64 64
65 if (csize >= 4) 65 if (csize >= 4)
66 csize = 4; 66 csize = 4;
67 else if (msglen > (1 << (8 * csize))) 67 else if (msglen > (1 << (8 * csize)))
68 return -EOVERFLOW; 68 return -EOVERFLOW;
69 69
70 data = cpu_to_be32(msglen); 70 data = cpu_to_be32(msglen);
71 memcpy(block - csize, (u8 *)&data + 4 71 memcpy(block - csize, (u8 *)&data + 4 - csize, csize);
72 72
73 return 0; 73 return 0;
74 } 74 }
75 75
76 static int crypto_ccm_setkey(struct crypto_aea 76 static int crypto_ccm_setkey(struct crypto_aead *aead, const u8 *key,
77 unsigned int keyl 77 unsigned int keylen)
78 { 78 {
79 struct crypto_ccm_ctx *ctx = crypto_ae 79 struct crypto_ccm_ctx *ctx = crypto_aead_ctx(aead);
80 struct crypto_ablkcipher *ctr = ctx->c 80 struct crypto_ablkcipher *ctr = ctx->ctr;
81 struct crypto_cipher *tfm = ctx->ciphe 81 struct crypto_cipher *tfm = ctx->cipher;
82 int err = 0; 82 int err = 0;
83 83
84 crypto_ablkcipher_clear_flags(ctr, CRY 84 crypto_ablkcipher_clear_flags(ctr, CRYPTO_TFM_REQ_MASK);
85 crypto_ablkcipher_set_flags(ctr, crypt 85 crypto_ablkcipher_set_flags(ctr, crypto_aead_get_flags(aead) &
86 CRYPTO_TFM 86 CRYPTO_TFM_REQ_MASK);
87 err = crypto_ablkcipher_setkey(ctr, ke 87 err = crypto_ablkcipher_setkey(ctr, key, keylen);
88 crypto_aead_set_flags(aead, crypto_abl 88 crypto_aead_set_flags(aead, crypto_ablkcipher_get_flags(ctr) &
89 CRYPTO_TFM_RES_M 89 CRYPTO_TFM_RES_MASK);
90 if (err) 90 if (err)
91 goto out; 91 goto out;
92 92
93 crypto_cipher_clear_flags(tfm, CRYPTO_ 93 crypto_cipher_clear_flags(tfm, CRYPTO_TFM_REQ_MASK);
94 crypto_cipher_set_flags(tfm, crypto_ae 94 crypto_cipher_set_flags(tfm, crypto_aead_get_flags(aead) &
95 CRYPTO_TFM 95 CRYPTO_TFM_REQ_MASK);
96 err = crypto_cipher_setkey(tfm, key, k 96 err = crypto_cipher_setkey(tfm, key, keylen);
97 crypto_aead_set_flags(aead, crypto_cip 97 crypto_aead_set_flags(aead, crypto_cipher_get_flags(tfm) &
98 CRYPTO_TFM_RES_M 98 CRYPTO_TFM_RES_MASK);
99 99
100 out: 100 out:
101 return err; 101 return err;
102 } 102 }
103 103
104 static int crypto_ccm_setauthsize(struct crypt 104 static int crypto_ccm_setauthsize(struct crypto_aead *tfm,
105 unsigned int 105 unsigned int authsize)
106 { 106 {
107 switch (authsize) { 107 switch (authsize) {
108 case 4: 108 case 4:
109 case 6: 109 case 6:
110 case 8: 110 case 8:
111 case 10: 111 case 10:
112 case 12: 112 case 12:
113 case 14: 113 case 14:
114 case 16: 114 case 16:
115 break; 115 break;
116 default: 116 default:
117 return -EINVAL; 117 return -EINVAL;
118 } 118 }
119 119
120 return 0; 120 return 0;
121 } 121 }
122 122
123 static int format_input(u8 *info, struct aead_ 123 static int format_input(u8 *info, struct aead_request *req,
124 unsigned int cryptlen) 124 unsigned int cryptlen)
125 { 125 {
126 struct crypto_aead *aead = crypto_aead 126 struct crypto_aead *aead = crypto_aead_reqtfm(req);
127 unsigned int lp = req->iv[0]; 127 unsigned int lp = req->iv[0];
128 unsigned int l = lp + 1; 128 unsigned int l = lp + 1;
129 unsigned int m; 129 unsigned int m;
130 130
131 m = crypto_aead_authsize(aead); 131 m = crypto_aead_authsize(aead);
132 132
133 memcpy(info, req->iv, 16); 133 memcpy(info, req->iv, 16);
134 134
135 /* format control info per RFC 3610 an 135 /* format control info per RFC 3610 and
136 * NIST Special Publication 800-38C 136 * NIST Special Publication 800-38C
137 */ 137 */
138 *info |= (8 * ((m - 2) / 2)); 138 *info |= (8 * ((m - 2) / 2));
139 if (req->assoclen) 139 if (req->assoclen)
140 *info |= 64; 140 *info |= 64;
141 141
142 return set_msg_len(info + 16 - l, cryp 142 return set_msg_len(info + 16 - l, cryptlen, l);
143 } 143 }
144 144
145 static int format_adata(u8 *adata, unsigned in 145 static int format_adata(u8 *adata, unsigned int a)
146 { 146 {
147 int len = 0; 147 int len = 0;
148 148
149 /* add control info for associated dat 149 /* add control info for associated data
150 * RFC 3610 and NIST Special Publicati 150 * RFC 3610 and NIST Special Publication 800-38C
151 */ 151 */
152 if (a < 65280) { 152 if (a < 65280) {
153 *(__be16 *)adata = cpu_to_be16 153 *(__be16 *)adata = cpu_to_be16(a);
154 len = 2; 154 len = 2;
155 } else { 155 } else {
156 *(__be16 *)adata = cpu_to_be16 156 *(__be16 *)adata = cpu_to_be16(0xfffe);
157 *(__be32 *)&adata[2] = cpu_to_ 157 *(__be32 *)&adata[2] = cpu_to_be32(a);
158 len = 6; 158 len = 6;
159 } 159 }
160 160
161 return len; 161 return len;
162 } 162 }
163 163
164 static void compute_mac(struct crypto_cipher * 164 static void compute_mac(struct crypto_cipher *tfm, u8 *data, int n,
165 struct crypto_ccm_req_p 165 struct crypto_ccm_req_priv_ctx *pctx)
166 { 166 {
167 unsigned int bs = 16; 167 unsigned int bs = 16;
168 u8 *odata = pctx->odata; 168 u8 *odata = pctx->odata;
169 u8 *idata = pctx->idata; 169 u8 *idata = pctx->idata;
170 int datalen, getlen; 170 int datalen, getlen;
171 171
172 datalen = n; 172 datalen = n;
173 173
174 /* first time in here, block may be pa 174 /* first time in here, block may be partially filled. */
175 getlen = bs - pctx->ilen; 175 getlen = bs - pctx->ilen;
176 if (datalen >= getlen) { 176 if (datalen >= getlen) {
177 memcpy(idata + pctx->ilen, dat 177 memcpy(idata + pctx->ilen, data, getlen);
178 crypto_xor(odata, idata, bs); 178 crypto_xor(odata, idata, bs);
179 crypto_cipher_encrypt_one(tfm, 179 crypto_cipher_encrypt_one(tfm, odata, odata);
180 datalen -= getlen; 180 datalen -= getlen;
181 data += getlen; 181 data += getlen;
182 pctx->ilen = 0; 182 pctx->ilen = 0;
183 } 183 }
184 184
185 /* now encrypt rest of data */ 185 /* now encrypt rest of data */
186 while (datalen >= bs) { 186 while (datalen >= bs) {
187 crypto_xor(odata, data, bs); 187 crypto_xor(odata, data, bs);
188 crypto_cipher_encrypt_one(tfm, 188 crypto_cipher_encrypt_one(tfm, odata, odata);
189 189
190 datalen -= bs; 190 datalen -= bs;
191 data += bs; 191 data += bs;
192 } 192 }
193 193
194 /* check and see if there's leftover d 194 /* check and see if there's leftover data that wasn't
195 * enough to fill a block. 195 * enough to fill a block.
196 */ 196 */
197 if (datalen) { 197 if (datalen) {
198 memcpy(idata + pctx->ilen, dat 198 memcpy(idata + pctx->ilen, data, datalen);
199 pctx->ilen += datalen; 199 pctx->ilen += datalen;
200 } 200 }
201 } 201 }
202 202
203 static void get_data_to_compute(struct crypto_ 203 static void get_data_to_compute(struct crypto_cipher *tfm,
204 struct crypto_c 204 struct crypto_ccm_req_priv_ctx *pctx,
205 struct scatterl 205 struct scatterlist *sg, unsigned int len)
206 { 206 {
207 struct scatter_walk walk; 207 struct scatter_walk walk;
208 u8 *data_src; 208 u8 *data_src;
209 int n; 209 int n;
210 210
211 scatterwalk_start(&walk, sg); 211 scatterwalk_start(&walk, sg);
212 212
213 while (len) { 213 while (len) {
214 n = scatterwalk_clamp(&walk, l 214 n = scatterwalk_clamp(&walk, len);
215 if (!n) { 215 if (!n) {
216 scatterwalk_start(&wal 216 scatterwalk_start(&walk, sg_next(walk.sg));
217 n = scatterwalk_clamp( 217 n = scatterwalk_clamp(&walk, len);
218 } 218 }
219 data_src = scatterwalk_map(&wa 219 data_src = scatterwalk_map(&walk, 0);
220 220
221 compute_mac(tfm, data_src, n, 221 compute_mac(tfm, data_src, n, pctx);
222 len -= n; 222 len -= n;
223 223
224 scatterwalk_unmap(data_src, 0) 224 scatterwalk_unmap(data_src, 0);
225 scatterwalk_advance(&walk, n); 225 scatterwalk_advance(&walk, n);
226 scatterwalk_done(&walk, 0, len 226 scatterwalk_done(&walk, 0, len);
227 if (len) 227 if (len)
228 crypto_yield(pctx->fla 228 crypto_yield(pctx->flags);
229 } 229 }
230 230
231 /* any leftover needs padding and then 231 /* any leftover needs padding and then encrypted */
232 if (pctx->ilen) { 232 if (pctx->ilen) {
233 int padlen; 233 int padlen;
234 u8 *odata = pctx->odata; 234 u8 *odata = pctx->odata;
235 u8 *idata = pctx->idata; 235 u8 *idata = pctx->idata;
236 236
237 padlen = 16 - pctx->ilen; 237 padlen = 16 - pctx->ilen;
238 memset(idata + pctx->ilen, 0, 238 memset(idata + pctx->ilen, 0, padlen);
239 crypto_xor(odata, idata, 16); 239 crypto_xor(odata, idata, 16);
240 crypto_cipher_encrypt_one(tfm, 240 crypto_cipher_encrypt_one(tfm, odata, odata);
241 pctx->ilen = 0; 241 pctx->ilen = 0;
242 } 242 }
243 } 243 }
244 244
245 static int crypto_ccm_auth(struct aead_request 245 static int crypto_ccm_auth(struct aead_request *req, struct scatterlist *plain,
246 unsigned int cryptl 246 unsigned int cryptlen)
247 { 247 {
248 struct crypto_aead *aead = crypto_aead 248 struct crypto_aead *aead = crypto_aead_reqtfm(req);
249 struct crypto_ccm_ctx *ctx = crypto_ae 249 struct crypto_ccm_ctx *ctx = crypto_aead_ctx(aead);
250 struct crypto_ccm_req_priv_ctx *pctx = 250 struct crypto_ccm_req_priv_ctx *pctx = crypto_ccm_reqctx(req);
251 struct crypto_cipher *cipher = ctx->ci 251 struct crypto_cipher *cipher = ctx->cipher;
252 unsigned int assoclen = req->assoclen; 252 unsigned int assoclen = req->assoclen;
253 u8 *odata = pctx->odata; 253 u8 *odata = pctx->odata;
254 u8 *idata = pctx->idata; 254 u8 *idata = pctx->idata;
255 int err; 255 int err;
256 256
257 /* format control data for input */ 257 /* format control data for input */
258 err = format_input(odata, req, cryptle 258 err = format_input(odata, req, cryptlen);
259 if (err) 259 if (err)
260 goto out; 260 goto out;
261 261
262 /* encrypt first block to use as start 262 /* encrypt first block to use as start in computing mac */
263 crypto_cipher_encrypt_one(cipher, odat 263 crypto_cipher_encrypt_one(cipher, odata, odata);
264 264
265 /* format associated data and compute 265 /* format associated data and compute into mac */
266 if (assoclen) { 266 if (assoclen) {
267 pctx->ilen = format_adata(idat 267 pctx->ilen = format_adata(idata, assoclen);
268 get_data_to_compute(cipher, pc 268 get_data_to_compute(cipher, pctx, req->assoc, req->assoclen);
>> 269 } else {
>> 270 pctx->ilen = 0;
269 } 271 }
270 272
271 /* compute plaintext into mac */ 273 /* compute plaintext into mac */
272 get_data_to_compute(cipher, pctx, plai 274 get_data_to_compute(cipher, pctx, plain, cryptlen);
273 275
274 out: 276 out:
275 return err; 277 return err;
276 } 278 }
277 279
278 static void crypto_ccm_encrypt_done(struct cry 280 static void crypto_ccm_encrypt_done(struct crypto_async_request *areq, int err)
279 { 281 {
280 struct aead_request *req = areq->data; 282 struct aead_request *req = areq->data;
281 struct crypto_aead *aead = crypto_aead 283 struct crypto_aead *aead = crypto_aead_reqtfm(req);
282 struct crypto_ccm_req_priv_ctx *pctx = 284 struct crypto_ccm_req_priv_ctx *pctx = crypto_ccm_reqctx(req);
283 u8 *odata = pctx->odata; 285 u8 *odata = pctx->odata;
284 286
285 if (!err) 287 if (!err)
286 scatterwalk_map_and_copy(odata 288 scatterwalk_map_and_copy(odata, req->dst, req->cryptlen,
287 crypt 289 crypto_aead_authsize(aead), 1);
288 aead_request_complete(req, err); 290 aead_request_complete(req, err);
289 } 291 }
290 292
291 static inline int crypto_ccm_check_iv(const u8 293 static inline int crypto_ccm_check_iv(const u8 *iv)
292 { 294 {
293 /* 2 <= L <= 8, so 1 <= L' <= 7. */ 295 /* 2 <= L <= 8, so 1 <= L' <= 7. */
294 if (1 > iv[0] || iv[0] > 7) 296 if (1 > iv[0] || iv[0] > 7)
295 return -EINVAL; 297 return -EINVAL;
296 298
297 return 0; 299 return 0;
298 } 300 }
299 301
300 static int crypto_ccm_encrypt(struct aead_requ 302 static int crypto_ccm_encrypt(struct aead_request *req)
301 { 303 {
302 struct crypto_aead *aead = crypto_aead 304 struct crypto_aead *aead = crypto_aead_reqtfm(req);
303 struct crypto_ccm_ctx *ctx = crypto_ae 305 struct crypto_ccm_ctx *ctx = crypto_aead_ctx(aead);
304 struct crypto_ccm_req_priv_ctx *pctx = 306 struct crypto_ccm_req_priv_ctx *pctx = crypto_ccm_reqctx(req);
305 struct ablkcipher_request *abreq = &pc 307 struct ablkcipher_request *abreq = &pctx->abreq;
306 struct scatterlist *dst; 308 struct scatterlist *dst;
307 unsigned int cryptlen = req->cryptlen; 309 unsigned int cryptlen = req->cryptlen;
308 u8 *odata = pctx->odata; 310 u8 *odata = pctx->odata;
309 u8 *iv = req->iv; 311 u8 *iv = req->iv;
310 int err; 312 int err;
311 313
312 err = crypto_ccm_check_iv(iv); 314 err = crypto_ccm_check_iv(iv);
313 if (err) 315 if (err)
314 return err; 316 return err;
315 317
316 pctx->flags = aead_request_flags(req); 318 pctx->flags = aead_request_flags(req);
317 319
318 err = crypto_ccm_auth(req, req->src, c 320 err = crypto_ccm_auth(req, req->src, cryptlen);
319 if (err) 321 if (err)
320 return err; 322 return err;
321 323
322 /* Note: rfc 3610 and NIST 800-38C re 324 /* Note: rfc 3610 and NIST 800-38C require counter of
323 * zero to encrypt auth tag. 325 * zero to encrypt auth tag.
324 */ 326 */
325 memset(iv + 15 - iv[0], 0, iv[0] + 1); 327 memset(iv + 15 - iv[0], 0, iv[0] + 1);
326 328
327 sg_init_table(pctx->src, 2); 329 sg_init_table(pctx->src, 2);
328 sg_set_buf(pctx->src, odata, 16); 330 sg_set_buf(pctx->src, odata, 16);
329 scatterwalk_sg_chain(pctx->src, 2, req 331 scatterwalk_sg_chain(pctx->src, 2, req->src);
330 332
331 dst = pctx->src; 333 dst = pctx->src;
332 if (req->src != req->dst) { 334 if (req->src != req->dst) {
333 sg_init_table(pctx->dst, 2); 335 sg_init_table(pctx->dst, 2);
334 sg_set_buf(pctx->dst, odata, 1 336 sg_set_buf(pctx->dst, odata, 16);
335 scatterwalk_sg_chain(pctx->dst 337 scatterwalk_sg_chain(pctx->dst, 2, req->dst);
336 dst = pctx->dst; 338 dst = pctx->dst;
337 } 339 }
338 340
339 ablkcipher_request_set_tfm(abreq, ctx- 341 ablkcipher_request_set_tfm(abreq, ctx->ctr);
340 ablkcipher_request_set_callback(abreq, 342 ablkcipher_request_set_callback(abreq, pctx->flags,
341 crypto 343 crypto_ccm_encrypt_done, req);
342 ablkcipher_request_set_crypt(abreq, pc 344 ablkcipher_request_set_crypt(abreq, pctx->src, dst, cryptlen + 16, iv);
343 err = crypto_ablkcipher_encrypt(abreq) 345 err = crypto_ablkcipher_encrypt(abreq);
344 if (err) 346 if (err)
345 return err; 347 return err;
346 348
347 /* copy authtag to end of dst */ 349 /* copy authtag to end of dst */
348 scatterwalk_map_and_copy(odata, req->d 350 scatterwalk_map_and_copy(odata, req->dst, cryptlen,
349 crypto_aead_a 351 crypto_aead_authsize(aead), 1);
350 return err; 352 return err;
351 } 353 }
352 354
353 static void crypto_ccm_decrypt_done(struct cry 355 static void crypto_ccm_decrypt_done(struct crypto_async_request *areq,
354 int err) 356 int err)
355 { 357 {
356 struct aead_request *req = areq->data; 358 struct aead_request *req = areq->data;
357 struct crypto_ccm_req_priv_ctx *pctx = 359 struct crypto_ccm_req_priv_ctx *pctx = crypto_ccm_reqctx(req);
358 struct crypto_aead *aead = crypto_aead 360 struct crypto_aead *aead = crypto_aead_reqtfm(req);
359 unsigned int authsize = crypto_aead_au 361 unsigned int authsize = crypto_aead_authsize(aead);
360 unsigned int cryptlen = req->cryptlen 362 unsigned int cryptlen = req->cryptlen - authsize;
361 363
362 if (!err) { 364 if (!err) {
363 err = crypto_ccm_auth(req, req 365 err = crypto_ccm_auth(req, req->dst, cryptlen);
364 if (!err && memcmp(pctx->auth_ 366 if (!err && memcmp(pctx->auth_tag, pctx->odata, authsize))
365 err = -EBADMSG; 367 err = -EBADMSG;
366 } 368 }
367 aead_request_complete(req, err); 369 aead_request_complete(req, err);
368 } 370 }
369 371
370 static int crypto_ccm_decrypt(struct aead_requ 372 static int crypto_ccm_decrypt(struct aead_request *req)
371 { 373 {
372 struct crypto_aead *aead = crypto_aead 374 struct crypto_aead *aead = crypto_aead_reqtfm(req);
373 struct crypto_ccm_ctx *ctx = crypto_ae 375 struct crypto_ccm_ctx *ctx = crypto_aead_ctx(aead);
374 struct crypto_ccm_req_priv_ctx *pctx = 376 struct crypto_ccm_req_priv_ctx *pctx = crypto_ccm_reqctx(req);
375 struct ablkcipher_request *abreq = &pc 377 struct ablkcipher_request *abreq = &pctx->abreq;
376 struct scatterlist *dst; 378 struct scatterlist *dst;
377 unsigned int authsize = crypto_aead_au 379 unsigned int authsize = crypto_aead_authsize(aead);
378 unsigned int cryptlen = req->cryptlen; 380 unsigned int cryptlen = req->cryptlen;
379 u8 *authtag = pctx->auth_tag; 381 u8 *authtag = pctx->auth_tag;
380 u8 *odata = pctx->odata; 382 u8 *odata = pctx->odata;
381 u8 *iv = req->iv; 383 u8 *iv = req->iv;
382 int err; 384 int err;
383 385
384 if (cryptlen < authsize) 386 if (cryptlen < authsize)
385 return -EINVAL; 387 return -EINVAL;
386 cryptlen -= authsize; 388 cryptlen -= authsize;
387 389
388 err = crypto_ccm_check_iv(iv); 390 err = crypto_ccm_check_iv(iv);
389 if (err) 391 if (err)
390 return err; 392 return err;
391 393
392 pctx->flags = aead_request_flags(req); 394 pctx->flags = aead_request_flags(req);
393 395
394 scatterwalk_map_and_copy(authtag, req- 396 scatterwalk_map_and_copy(authtag, req->src, cryptlen, authsize, 0);
395 397
396 memset(iv + 15 - iv[0], 0, iv[0] + 1); 398 memset(iv + 15 - iv[0], 0, iv[0] + 1);
397 399
398 sg_init_table(pctx->src, 2); 400 sg_init_table(pctx->src, 2);
399 sg_set_buf(pctx->src, authtag, 16); 401 sg_set_buf(pctx->src, authtag, 16);
400 scatterwalk_sg_chain(pctx->src, 2, req 402 scatterwalk_sg_chain(pctx->src, 2, req->src);
401 403
402 dst = pctx->src; 404 dst = pctx->src;
403 if (req->src != req->dst) { 405 if (req->src != req->dst) {
404 sg_init_table(pctx->dst, 2); 406 sg_init_table(pctx->dst, 2);
405 sg_set_buf(pctx->dst, authtag, 407 sg_set_buf(pctx->dst, authtag, 16);
406 scatterwalk_sg_chain(pctx->dst 408 scatterwalk_sg_chain(pctx->dst, 2, req->dst);
407 dst = pctx->dst; 409 dst = pctx->dst;
408 } 410 }
409 411
410 ablkcipher_request_set_tfm(abreq, ctx- 412 ablkcipher_request_set_tfm(abreq, ctx->ctr);
411 ablkcipher_request_set_callback(abreq, 413 ablkcipher_request_set_callback(abreq, pctx->flags,
412 crypto 414 crypto_ccm_decrypt_done, req);
413 ablkcipher_request_set_crypt(abreq, pc 415 ablkcipher_request_set_crypt(abreq, pctx->src, dst, cryptlen + 16, iv);
414 err = crypto_ablkcipher_decrypt(abreq) 416 err = crypto_ablkcipher_decrypt(abreq);
415 if (err) 417 if (err)
416 return err; 418 return err;
417 419
418 err = crypto_ccm_auth(req, req->dst, c 420 err = crypto_ccm_auth(req, req->dst, cryptlen);
419 if (err) 421 if (err)
420 return err; 422 return err;
421 423
422 /* verify */ 424 /* verify */
423 if (memcmp(authtag, odata, authsize)) 425 if (memcmp(authtag, odata, authsize))
424 return -EBADMSG; 426 return -EBADMSG;
425 427
426 return err; 428 return err;
427 } 429 }
428 430
429 static int crypto_ccm_init_tfm(struct crypto_t 431 static int crypto_ccm_init_tfm(struct crypto_tfm *tfm)
430 { 432 {
431 struct crypto_instance *inst = (void * 433 struct crypto_instance *inst = (void *)tfm->__crt_alg;
432 struct ccm_instance_ctx *ictx = crypto 434 struct ccm_instance_ctx *ictx = crypto_instance_ctx(inst);
433 struct crypto_ccm_ctx *ctx = crypto_tf 435 struct crypto_ccm_ctx *ctx = crypto_tfm_ctx(tfm);
434 struct crypto_cipher *cipher; 436 struct crypto_cipher *cipher;
435 struct crypto_ablkcipher *ctr; 437 struct crypto_ablkcipher *ctr;
436 unsigned long align; 438 unsigned long align;
437 int err; 439 int err;
438 440
439 cipher = crypto_spawn_cipher(&ictx->ci 441 cipher = crypto_spawn_cipher(&ictx->cipher);
440 if (IS_ERR(cipher)) 442 if (IS_ERR(cipher))
441 return PTR_ERR(cipher); 443 return PTR_ERR(cipher);
442 444
443 ctr = crypto_spawn_skcipher(&ictx->ctr 445 ctr = crypto_spawn_skcipher(&ictx->ctr);
444 err = PTR_ERR(ctr); 446 err = PTR_ERR(ctr);
445 if (IS_ERR(ctr)) 447 if (IS_ERR(ctr))
446 goto err_free_cipher; 448 goto err_free_cipher;
447 449
448 ctx->cipher = cipher; 450 ctx->cipher = cipher;
449 ctx->ctr = ctr; 451 ctx->ctr = ctr;
450 452
451 align = crypto_tfm_alg_alignmask(tfm); 453 align = crypto_tfm_alg_alignmask(tfm);
452 align &= ~(crypto_tfm_ctx_alignment() 454 align &= ~(crypto_tfm_ctx_alignment() - 1);
453 tfm->crt_aead.reqsize = align + 455 tfm->crt_aead.reqsize = align +
454 sizeof(struct 456 sizeof(struct crypto_ccm_req_priv_ctx) +
455 crypto_ablkcip 457 crypto_ablkcipher_reqsize(ctr);
456 458
457 return 0; 459 return 0;
458 460
459 err_free_cipher: 461 err_free_cipher:
460 crypto_free_cipher(cipher); 462 crypto_free_cipher(cipher);
461 return err; 463 return err;
462 } 464 }
463 465
464 static void crypto_ccm_exit_tfm(struct crypto_ 466 static void crypto_ccm_exit_tfm(struct crypto_tfm *tfm)
465 { 467 {
466 struct crypto_ccm_ctx *ctx = crypto_tf 468 struct crypto_ccm_ctx *ctx = crypto_tfm_ctx(tfm);
467 469
468 crypto_free_cipher(ctx->cipher); 470 crypto_free_cipher(ctx->cipher);
469 crypto_free_ablkcipher(ctx->ctr); 471 crypto_free_ablkcipher(ctx->ctr);
470 } 472 }
471 473
472 static struct crypto_instance *crypto_ccm_allo 474 static struct crypto_instance *crypto_ccm_alloc_common(struct rtattr **tb,
473 475 const char *full_name,
474 476 const char *ctr_name,
475 477 const char *cipher_name)
476 { 478 {
477 struct crypto_attr_type *algt; 479 struct crypto_attr_type *algt;
478 struct crypto_instance *inst; 480 struct crypto_instance *inst;
479 struct crypto_alg *ctr; 481 struct crypto_alg *ctr;
480 struct crypto_alg *cipher; 482 struct crypto_alg *cipher;
481 struct ccm_instance_ctx *ictx; 483 struct ccm_instance_ctx *ictx;
482 int err; 484 int err;
483 485
484 algt = crypto_get_attr_type(tb); 486 algt = crypto_get_attr_type(tb);
485 err = PTR_ERR(algt); 487 err = PTR_ERR(algt);
486 if (IS_ERR(algt)) 488 if (IS_ERR(algt))
487 return ERR_PTR(err); 489 return ERR_PTR(err);
488 490
489 if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD 491 if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
490 return ERR_PTR(-EINVAL); 492 return ERR_PTR(-EINVAL);
491 493
492 cipher = crypto_alg_mod_lookup(cipher_ 494 cipher = crypto_alg_mod_lookup(cipher_name, CRYPTO_ALG_TYPE_CIPHER,
493 CRYPTO_ 495 CRYPTO_ALG_TYPE_MASK);
494 err = PTR_ERR(cipher); 496 err = PTR_ERR(cipher);
495 if (IS_ERR(cipher)) 497 if (IS_ERR(cipher))
496 return ERR_PTR(err); 498 return ERR_PTR(err);
497 499
498 err = -EINVAL; 500 err = -EINVAL;
499 if (cipher->cra_blocksize != 16) 501 if (cipher->cra_blocksize != 16)
500 goto out_put_cipher; 502 goto out_put_cipher;
501 503
502 inst = kzalloc(sizeof(*inst) + sizeof( 504 inst = kzalloc(sizeof(*inst) + sizeof(*ictx), GFP_KERNEL);
503 err = -ENOMEM; 505 err = -ENOMEM;
504 if (!inst) 506 if (!inst)
505 goto out_put_cipher; 507 goto out_put_cipher;
506 508
507 ictx = crypto_instance_ctx(inst); 509 ictx = crypto_instance_ctx(inst);
508 510
509 err = crypto_init_spawn(&ictx->cipher, 511 err = crypto_init_spawn(&ictx->cipher, cipher, inst,
510 CRYPTO_ALG_TYP 512 CRYPTO_ALG_TYPE_MASK);
511 if (err) 513 if (err)
512 goto err_free_inst; 514 goto err_free_inst;
513 515
514 crypto_set_skcipher_spawn(&ictx->ctr, 516 crypto_set_skcipher_spawn(&ictx->ctr, inst);
515 err = crypto_grab_skcipher(&ictx->ctr, 517 err = crypto_grab_skcipher(&ictx->ctr, ctr_name, 0,
516 crypto_requ 518 crypto_requires_sync(algt->type,
517 519 algt->mask));
518 if (err) 520 if (err)
519 goto err_drop_cipher; 521 goto err_drop_cipher;
520 522
521 ctr = crypto_skcipher_spawn_alg(&ictx- 523 ctr = crypto_skcipher_spawn_alg(&ictx->ctr);
522 524
523 /* Not a stream cipher? */ 525 /* Not a stream cipher? */
524 err = -EINVAL; 526 err = -EINVAL;
525 if (ctr->cra_blocksize != 1) 527 if (ctr->cra_blocksize != 1)
526 goto err_drop_ctr; 528 goto err_drop_ctr;
527 529
528 /* We want the real thing! */ 530 /* We want the real thing! */
529 if (ctr->cra_ablkcipher.ivsize != 16) 531 if (ctr->cra_ablkcipher.ivsize != 16)
530 goto err_drop_ctr; 532 goto err_drop_ctr;
531 533
532 err = -ENAMETOOLONG; 534 err = -ENAMETOOLONG;
533 if (snprintf(inst->alg.cra_driver_name 535 if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
534 "ccm_base(%s,%s)", ctr->c 536 "ccm_base(%s,%s)", ctr->cra_driver_name,
535 cipher->cra_driver_name) 537 cipher->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
536 goto err_drop_ctr; 538 goto err_drop_ctr;
537 539
538 memcpy(inst->alg.cra_name, full_name, 540 memcpy(inst->alg.cra_name, full_name, CRYPTO_MAX_ALG_NAME);
539 541
540 inst->alg.cra_flags = CRYPTO_ALG_TYPE_ 542 inst->alg.cra_flags = CRYPTO_ALG_TYPE_AEAD;
541 inst->alg.cra_flags |= ctr->cra_flags 543 inst->alg.cra_flags |= ctr->cra_flags & CRYPTO_ALG_ASYNC;
542 inst->alg.cra_priority = cipher->cra_p 544 inst->alg.cra_priority = cipher->cra_priority + ctr->cra_priority;
543 inst->alg.cra_blocksize = 1; 545 inst->alg.cra_blocksize = 1;
544 inst->alg.cra_alignmask = cipher->cra_ 546 inst->alg.cra_alignmask = cipher->cra_alignmask | ctr->cra_alignmask |
545 (__alignof__ 547 (__alignof__(u32) - 1);
546 inst->alg.cra_type = &crypto_aead_type 548 inst->alg.cra_type = &crypto_aead_type;
547 inst->alg.cra_aead.ivsize = 16; 549 inst->alg.cra_aead.ivsize = 16;
548 inst->alg.cra_aead.maxauthsize = 16; 550 inst->alg.cra_aead.maxauthsize = 16;
549 inst->alg.cra_ctxsize = sizeof(struct 551 inst->alg.cra_ctxsize = sizeof(struct crypto_ccm_ctx);
550 inst->alg.cra_init = crypto_ccm_init_t 552 inst->alg.cra_init = crypto_ccm_init_tfm;
551 inst->alg.cra_exit = crypto_ccm_exit_t 553 inst->alg.cra_exit = crypto_ccm_exit_tfm;
552 inst->alg.cra_aead.setkey = crypto_ccm 554 inst->alg.cra_aead.setkey = crypto_ccm_setkey;
553 inst->alg.cra_aead.setauthsize = crypt 555 inst->alg.cra_aead.setauthsize = crypto_ccm_setauthsize;
554 inst->alg.cra_aead.encrypt = crypto_cc 556 inst->alg.cra_aead.encrypt = crypto_ccm_encrypt;
555 inst->alg.cra_aead.decrypt = crypto_cc 557 inst->alg.cra_aead.decrypt = crypto_ccm_decrypt;
556 558
557 out: 559 out:
558 crypto_mod_put(cipher); 560 crypto_mod_put(cipher);
559 return inst; 561 return inst;
560 562
561 err_drop_ctr: 563 err_drop_ctr:
562 crypto_drop_skcipher(&ictx->ctr); 564 crypto_drop_skcipher(&ictx->ctr);
563 err_drop_cipher: 565 err_drop_cipher:
564 crypto_drop_spawn(&ictx->cipher); 566 crypto_drop_spawn(&ictx->cipher);
565 err_free_inst: 567 err_free_inst:
566 kfree(inst); 568 kfree(inst);
567 out_put_cipher: 569 out_put_cipher:
568 inst = ERR_PTR(err); 570 inst = ERR_PTR(err);
569 goto out; 571 goto out;
570 } 572 }
571 573
572 static struct crypto_instance *crypto_ccm_allo 574 static struct crypto_instance *crypto_ccm_alloc(struct rtattr **tb)
573 { 575 {
574 int err; 576 int err;
575 const char *cipher_name; 577 const char *cipher_name;
576 char ctr_name[CRYPTO_MAX_ALG_NAME]; 578 char ctr_name[CRYPTO_MAX_ALG_NAME];
577 char full_name[CRYPTO_MAX_ALG_NAME]; 579 char full_name[CRYPTO_MAX_ALG_NAME];
578 580
579 cipher_name = crypto_attr_alg_name(tb[ 581 cipher_name = crypto_attr_alg_name(tb[1]);
580 err = PTR_ERR(cipher_name); 582 err = PTR_ERR(cipher_name);
581 if (IS_ERR(cipher_name)) 583 if (IS_ERR(cipher_name))
582 return ERR_PTR(err); 584 return ERR_PTR(err);
583 585
584 if (snprintf(ctr_name, CRYPTO_MAX_ALG_ 586 if (snprintf(ctr_name, CRYPTO_MAX_ALG_NAME, "ctr(%s)",
585 cipher_name) >= CRYPTO_MA 587 cipher_name) >= CRYPTO_MAX_ALG_NAME)
586 return ERR_PTR(-ENAMETOOLONG); 588 return ERR_PTR(-ENAMETOOLONG);
587 589
588 if (snprintf(full_name, CRYPTO_MAX_ALG 590 if (snprintf(full_name, CRYPTO_MAX_ALG_NAME, "ccm(%s)", cipher_name) >=
589 CRYPTO_MAX_ALG_NAME) 591 CRYPTO_MAX_ALG_NAME)
590 return ERR_PTR(-ENAMETOOLONG); 592 return ERR_PTR(-ENAMETOOLONG);
591 593
592 return crypto_ccm_alloc_common(tb, ful 594 return crypto_ccm_alloc_common(tb, full_name, ctr_name, cipher_name);
593 } 595 }
594 596
595 static void crypto_ccm_free(struct crypto_inst 597 static void crypto_ccm_free(struct crypto_instance *inst)
596 { 598 {
597 struct ccm_instance_ctx *ctx = crypto_ 599 struct ccm_instance_ctx *ctx = crypto_instance_ctx(inst);
598 600
599 crypto_drop_spawn(&ctx->cipher); 601 crypto_drop_spawn(&ctx->cipher);
600 crypto_drop_skcipher(&ctx->ctr); 602 crypto_drop_skcipher(&ctx->ctr);
601 kfree(inst); 603 kfree(inst);
602 } 604 }
603 605
604 static struct crypto_template crypto_ccm_tmpl 606 static struct crypto_template crypto_ccm_tmpl = {
605 .name = "ccm", 607 .name = "ccm",
606 .alloc = crypto_ccm_alloc, 608 .alloc = crypto_ccm_alloc,
607 .free = crypto_ccm_free, 609 .free = crypto_ccm_free,
608 .module = THIS_MODULE, 610 .module = THIS_MODULE,
609 }; 611 };
610 612
611 static struct crypto_instance *crypto_ccm_base 613 static struct crypto_instance *crypto_ccm_base_alloc(struct rtattr **tb)
612 { 614 {
613 int err; 615 int err;
614 const char *ctr_name; 616 const char *ctr_name;
615 const char *cipher_name; 617 const char *cipher_name;
616 char full_name[CRYPTO_MAX_ALG_NAME]; 618 char full_name[CRYPTO_MAX_ALG_NAME];
617 619
618 ctr_name = crypto_attr_alg_name(tb[1]) 620 ctr_name = crypto_attr_alg_name(tb[1]);
619 err = PTR_ERR(ctr_name); 621 err = PTR_ERR(ctr_name);
620 if (IS_ERR(ctr_name)) 622 if (IS_ERR(ctr_name))
621 return ERR_PTR(err); 623 return ERR_PTR(err);
622 624
623 cipher_name = crypto_attr_alg_name(tb[ 625 cipher_name = crypto_attr_alg_name(tb[2]);
624 err = PTR_ERR(cipher_name); 626 err = PTR_ERR(cipher_name);
625 if (IS_ERR(cipher_name)) 627 if (IS_ERR(cipher_name))
626 return ERR_PTR(err); 628 return ERR_PTR(err);
627 629
628 if (snprintf(full_name, CRYPTO_MAX_ALG 630 if (snprintf(full_name, CRYPTO_MAX_ALG_NAME, "ccm_base(%s,%s)",
629 ctr_name, cipher_name) >= 631 ctr_name, cipher_name) >= CRYPTO_MAX_ALG_NAME)
630 return ERR_PTR(-ENAMETOOLONG); 632 return ERR_PTR(-ENAMETOOLONG);
631 633
632 return crypto_ccm_alloc_common(tb, ful 634 return crypto_ccm_alloc_common(tb, full_name, ctr_name, cipher_name);
633 } 635 }
634 636
635 static struct crypto_template crypto_ccm_base_ 637 static struct crypto_template crypto_ccm_base_tmpl = {
636 .name = "ccm_base", 638 .name = "ccm_base",
637 .alloc = crypto_ccm_base_alloc, 639 .alloc = crypto_ccm_base_alloc,
638 .free = crypto_ccm_free, 640 .free = crypto_ccm_free,
639 .module = THIS_MODULE, 641 .module = THIS_MODULE,
640 }; 642 };
641 643
642 static int crypto_rfc4309_setkey(struct crypto 644 static int crypto_rfc4309_setkey(struct crypto_aead *parent, const u8 *key,
643 unsigned int 645 unsigned int keylen)
644 { 646 {
645 struct crypto_rfc4309_ctx *ctx = crypt 647 struct crypto_rfc4309_ctx *ctx = crypto_aead_ctx(parent);
646 struct crypto_aead *child = ctx->child 648 struct crypto_aead *child = ctx->child;
647 int err; 649 int err;
648 650
649 if (keylen < 3) 651 if (keylen < 3)
650 return -EINVAL; 652 return -EINVAL;
651 653
652 keylen -= 3; 654 keylen -= 3;
653 memcpy(ctx->nonce, key + keylen, 3); 655 memcpy(ctx->nonce, key + keylen, 3);
654 656
655 crypto_aead_clear_flags(child, CRYPTO_ 657 crypto_aead_clear_flags(child, CRYPTO_TFM_REQ_MASK);
656 crypto_aead_set_flags(child, crypto_ae 658 crypto_aead_set_flags(child, crypto_aead_get_flags(parent) &
657 CRYPTO_TF 659 CRYPTO_TFM_REQ_MASK);
658 err = crypto_aead_setkey(child, key, k 660 err = crypto_aead_setkey(child, key, keylen);
659 crypto_aead_set_flags(parent, crypto_a 661 crypto_aead_set_flags(parent, crypto_aead_get_flags(child) &
660 CRYPTO_T 662 CRYPTO_TFM_RES_MASK);
661 663
662 return err; 664 return err;
663 } 665 }
664 666
665 static int crypto_rfc4309_setauthsize(struct c 667 static int crypto_rfc4309_setauthsize(struct crypto_aead *parent,
666 unsigned 668 unsigned int authsize)
667 { 669 {
668 struct crypto_rfc4309_ctx *ctx = crypt 670 struct crypto_rfc4309_ctx *ctx = crypto_aead_ctx(parent);
669 671
670 switch (authsize) { 672 switch (authsize) {
671 case 8: 673 case 8:
672 case 12: 674 case 12:
673 case 16: 675 case 16:
674 break; 676 break;
675 default: 677 default:
676 return -EINVAL; 678 return -EINVAL;
677 } 679 }
678 680
679 return crypto_aead_setauthsize(ctx->ch 681 return crypto_aead_setauthsize(ctx->child, authsize);
680 } 682 }
681 683
682 static struct aead_request *crypto_rfc4309_cry 684 static struct aead_request *crypto_rfc4309_crypt(struct aead_request *req)
683 { 685 {
684 struct aead_request *subreq = aead_req 686 struct aead_request *subreq = aead_request_ctx(req);
685 struct crypto_aead *aead = crypto_aead 687 struct crypto_aead *aead = crypto_aead_reqtfm(req);
686 struct crypto_rfc4309_ctx *ctx = crypt 688 struct crypto_rfc4309_ctx *ctx = crypto_aead_ctx(aead);
687 struct crypto_aead *child = ctx->child 689 struct crypto_aead *child = ctx->child;
688 u8 *iv = PTR_ALIGN((u8 *)(subreq + 1) 690 u8 *iv = PTR_ALIGN((u8 *)(subreq + 1) + crypto_aead_reqsize(child),
689 crypto_aead_alignma 691 crypto_aead_alignmask(child) + 1);
690 692
691 /* L' */ 693 /* L' */
692 iv[0] = 3; 694 iv[0] = 3;
693 695
694 memcpy(iv + 1, ctx->nonce, 3); 696 memcpy(iv + 1, ctx->nonce, 3);
695 memcpy(iv + 4, req->iv, 8); 697 memcpy(iv + 4, req->iv, 8);
696 698
697 aead_request_set_tfm(subreq, child); 699 aead_request_set_tfm(subreq, child);
698 aead_request_set_callback(subreq, req- 700 aead_request_set_callback(subreq, req->base.flags, req->base.complete,
699 req->base.da 701 req->base.data);
700 aead_request_set_crypt(subreq, req->sr 702 aead_request_set_crypt(subreq, req->src, req->dst, req->cryptlen, iv);
701 aead_request_set_assoc(subreq, req->as 703 aead_request_set_assoc(subreq, req->assoc, req->assoclen);
702 704
703 return subreq; 705 return subreq;
704 } 706 }
705 707
706 static int crypto_rfc4309_encrypt(struct aead_ 708 static int crypto_rfc4309_encrypt(struct aead_request *req)
707 { 709 {
708 req = crypto_rfc4309_crypt(req); 710 req = crypto_rfc4309_crypt(req);
709 711
710 return crypto_aead_encrypt(req); 712 return crypto_aead_encrypt(req);
711 } 713 }
712 714
713 static int crypto_rfc4309_decrypt(struct aead_ 715 static int crypto_rfc4309_decrypt(struct aead_request *req)
714 { 716 {
715 req = crypto_rfc4309_crypt(req); 717 req = crypto_rfc4309_crypt(req);
716 718
717 return crypto_aead_decrypt(req); 719 return crypto_aead_decrypt(req);
718 } 720 }
719 721
720 static int crypto_rfc4309_init_tfm(struct cryp 722 static int crypto_rfc4309_init_tfm(struct crypto_tfm *tfm)
721 { 723 {
722 struct crypto_instance *inst = (void * 724 struct crypto_instance *inst = (void *)tfm->__crt_alg;
723 struct crypto_aead_spawn *spawn = cryp 725 struct crypto_aead_spawn *spawn = crypto_instance_ctx(inst);
724 struct crypto_rfc4309_ctx *ctx = crypt 726 struct crypto_rfc4309_ctx *ctx = crypto_tfm_ctx(tfm);
725 struct crypto_aead *aead; 727 struct crypto_aead *aead;
726 unsigned long align; 728 unsigned long align;
727 729
728 aead = crypto_spawn_aead(spawn); 730 aead = crypto_spawn_aead(spawn);
729 if (IS_ERR(aead)) 731 if (IS_ERR(aead))
730 return PTR_ERR(aead); 732 return PTR_ERR(aead);
731 733
732 ctx->child = aead; 734 ctx->child = aead;
733 735
734 align = crypto_aead_alignmask(aead); 736 align = crypto_aead_alignmask(aead);
735 align &= ~(crypto_tfm_ctx_alignment() 737 align &= ~(crypto_tfm_ctx_alignment() - 1);
736 tfm->crt_aead.reqsize = sizeof(struct 738 tfm->crt_aead.reqsize = sizeof(struct aead_request) +
737 ALIGN(crypto_a 739 ALIGN(crypto_aead_reqsize(aead),
738 crypto_t 740 crypto_tfm_ctx_alignment()) +
739 align + 16; 741 align + 16;
740 742
741 return 0; 743 return 0;
742 } 744 }
743 745
744 static void crypto_rfc4309_exit_tfm(struct cry 746 static void crypto_rfc4309_exit_tfm(struct crypto_tfm *tfm)
745 { 747 {
746 struct crypto_rfc4309_ctx *ctx = crypt 748 struct crypto_rfc4309_ctx *ctx = crypto_tfm_ctx(tfm);
747 749
748 crypto_free_aead(ctx->child); 750 crypto_free_aead(ctx->child);
749 } 751 }
750 752
751 static struct crypto_instance *crypto_rfc4309_ 753 static struct crypto_instance *crypto_rfc4309_alloc(struct rtattr **tb)
752 { 754 {
753 struct crypto_attr_type *algt; 755 struct crypto_attr_type *algt;
754 struct crypto_instance *inst; 756 struct crypto_instance *inst;
755 struct crypto_aead_spawn *spawn; 757 struct crypto_aead_spawn *spawn;
756 struct crypto_alg *alg; 758 struct crypto_alg *alg;
757 const char *ccm_name; 759 const char *ccm_name;
758 int err; 760 int err;
759 761
760 algt = crypto_get_attr_type(tb); 762 algt = crypto_get_attr_type(tb);
761 err = PTR_ERR(algt); 763 err = PTR_ERR(algt);
762 if (IS_ERR(algt)) 764 if (IS_ERR(algt))
763 return ERR_PTR(err); 765 return ERR_PTR(err);
764 766
765 if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD 767 if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
766 return ERR_PTR(-EINVAL); 768 return ERR_PTR(-EINVAL);
767 769
768 ccm_name = crypto_attr_alg_name(tb[1]) 770 ccm_name = crypto_attr_alg_name(tb[1]);
769 err = PTR_ERR(ccm_name); 771 err = PTR_ERR(ccm_name);
770 if (IS_ERR(ccm_name)) 772 if (IS_ERR(ccm_name))
771 return ERR_PTR(err); 773 return ERR_PTR(err);
772 774
773 inst = kzalloc(sizeof(*inst) + sizeof( 775 inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
774 if (!inst) 776 if (!inst)
775 return ERR_PTR(-ENOMEM); 777 return ERR_PTR(-ENOMEM);
776 778
777 spawn = crypto_instance_ctx(inst); 779 spawn = crypto_instance_ctx(inst);
778 crypto_set_aead_spawn(spawn, inst); 780 crypto_set_aead_spawn(spawn, inst);
779 err = crypto_grab_aead(spawn, ccm_name 781 err = crypto_grab_aead(spawn, ccm_name, 0,
780 crypto_requires 782 crypto_requires_sync(algt->type, algt->mask));
781 if (err) 783 if (err)
782 goto out_free_inst; 784 goto out_free_inst;
783 785
784 alg = crypto_aead_spawn_alg(spawn); 786 alg = crypto_aead_spawn_alg(spawn);
785 787
786 err = -EINVAL; 788 err = -EINVAL;
787 789
788 /* We only support 16-byte blocks. */ 790 /* We only support 16-byte blocks. */
789 if (alg->cra_aead.ivsize != 16) 791 if (alg->cra_aead.ivsize != 16)
790 goto out_drop_alg; 792 goto out_drop_alg;
791 793
792 /* Not a stream cipher? */ 794 /* Not a stream cipher? */
793 if (alg->cra_blocksize != 1) 795 if (alg->cra_blocksize != 1)
794 goto out_drop_alg; 796 goto out_drop_alg;
795 797
796 err = -ENAMETOOLONG; 798 err = -ENAMETOOLONG;
797 if (snprintf(inst->alg.cra_name, CRYPT 799 if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME,
798 "rfc4309(%s)", alg->cra_n 800 "rfc4309(%s)", alg->cra_name) >= CRYPTO_MAX_ALG_NAME ||
799 snprintf(inst->alg.cra_driver_name 801 snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
800 "rfc4309(%s)", alg->cra_d 802 "rfc4309(%s)", alg->cra_driver_name) >=
801 CRYPTO_MAX_ALG_NAME) 803 CRYPTO_MAX_ALG_NAME)
802 goto out_drop_alg; 804 goto out_drop_alg;
803 805
804 inst->alg.cra_flags = CRYPTO_ALG_TYPE_ 806 inst->alg.cra_flags = CRYPTO_ALG_TYPE_AEAD;
805 inst->alg.cra_flags |= alg->cra_flags 807 inst->alg.cra_flags |= alg->cra_flags & CRYPTO_ALG_ASYNC;
806 inst->alg.cra_priority = alg->cra_prio 808 inst->alg.cra_priority = alg->cra_priority;
807 inst->alg.cra_blocksize = 1; 809 inst->alg.cra_blocksize = 1;
808 inst->alg.cra_alignmask = alg->cra_ali 810 inst->alg.cra_alignmask = alg->cra_alignmask;
809 inst->alg.cra_type = &crypto_nivaead_t 811 inst->alg.cra_type = &crypto_nivaead_type;
810 812
811 inst->alg.cra_aead.ivsize = 8; 813 inst->alg.cra_aead.ivsize = 8;
812 inst->alg.cra_aead.maxauthsize = 16; 814 inst->alg.cra_aead.maxauthsize = 16;
813 815
814 inst->alg.cra_ctxsize = sizeof(struct 816 inst->alg.cra_ctxsize = sizeof(struct crypto_rfc4309_ctx);
815 817
816 inst->alg.cra_init = crypto_rfc4309_in 818 inst->alg.cra_init = crypto_rfc4309_init_tfm;
817 inst->alg.cra_exit = crypto_rfc4309_ex 819 inst->alg.cra_exit = crypto_rfc4309_exit_tfm;
818 820
819 inst->alg.cra_aead.setkey = crypto_rfc 821 inst->alg.cra_aead.setkey = crypto_rfc4309_setkey;
820 inst->alg.cra_aead.setauthsize = crypt 822 inst->alg.cra_aead.setauthsize = crypto_rfc4309_setauthsize;
821 inst->alg.cra_aead.encrypt = crypto_rf 823 inst->alg.cra_aead.encrypt = crypto_rfc4309_encrypt;
822 inst->alg.cra_aead.decrypt = crypto_rf 824 inst->alg.cra_aead.decrypt = crypto_rfc4309_decrypt;
823 825
824 inst->alg.cra_aead.geniv = "seqiv"; 826 inst->alg.cra_aead.geniv = "seqiv";
825 827
826 out: 828 out:
827 return inst; 829 return inst;
828 830
829 out_drop_alg: 831 out_drop_alg:
830 crypto_drop_aead(spawn); 832 crypto_drop_aead(spawn);
831 out_free_inst: 833 out_free_inst:
832 kfree(inst); 834 kfree(inst);
833 inst = ERR_PTR(err); 835 inst = ERR_PTR(err);
834 goto out; 836 goto out;
835 } 837 }
836 838
837 static void crypto_rfc4309_free(struct crypto_ 839 static void crypto_rfc4309_free(struct crypto_instance *inst)
838 { 840 {
839 crypto_drop_spawn(crypto_instance_ctx( 841 crypto_drop_spawn(crypto_instance_ctx(inst));
840 kfree(inst); 842 kfree(inst);
841 } 843 }
842 844
843 static struct crypto_template crypto_rfc4309_t 845 static struct crypto_template crypto_rfc4309_tmpl = {
844 .name = "rfc4309", 846 .name = "rfc4309",
845 .alloc = crypto_rfc4309_alloc, 847 .alloc = crypto_rfc4309_alloc,
846 .free = crypto_rfc4309_free, 848 .free = crypto_rfc4309_free,
847 .module = THIS_MODULE, 849 .module = THIS_MODULE,
848 }; 850 };
849 851
850 static int __init crypto_ccm_module_init(void) 852 static int __init crypto_ccm_module_init(void)
851 { 853 {
852 int err; 854 int err;
853 855
854 err = crypto_register_template(&crypto 856 err = crypto_register_template(&crypto_ccm_base_tmpl);
855 if (err) 857 if (err)
856 goto out; 858 goto out;
857 859
858 err = crypto_register_template(&crypto 860 err = crypto_register_template(&crypto_ccm_tmpl);
859 if (err) 861 if (err)
860 goto out_undo_base; 862 goto out_undo_base;
861 863
862 err = crypto_register_template(&crypto 864 err = crypto_register_template(&crypto_rfc4309_tmpl);
863 if (err) 865 if (err)
864 goto out_undo_ccm; 866 goto out_undo_ccm;
865 867
866 out: 868 out:
867 return err; 869 return err;
868 870
869 out_undo_ccm: 871 out_undo_ccm:
870 crypto_unregister_template(&crypto_ccm 872 crypto_unregister_template(&crypto_ccm_tmpl);
871 out_undo_base: 873 out_undo_base:
872 crypto_unregister_template(&crypto_ccm 874 crypto_unregister_template(&crypto_ccm_base_tmpl);
873 goto out; 875 goto out;
874 } 876 }
875 877
876 static void __exit crypto_ccm_module_exit(void 878 static void __exit crypto_ccm_module_exit(void)
877 { 879 {
878 crypto_unregister_template(&crypto_rfc 880 crypto_unregister_template(&crypto_rfc4309_tmpl);
879 crypto_unregister_template(&crypto_ccm 881 crypto_unregister_template(&crypto_ccm_tmpl);
880 crypto_unregister_template(&crypto_ccm 882 crypto_unregister_template(&crypto_ccm_base_tmpl);
881 } 883 }
882 884
883 module_init(crypto_ccm_module_init); 885 module_init(crypto_ccm_module_init);
884 module_exit(crypto_ccm_module_exit); 886 module_exit(crypto_ccm_module_exit);
885 887
886 MODULE_LICENSE("GPL"); 888 MODULE_LICENSE("GPL");
887 MODULE_DESCRIPTION("Counter with CBC MAC"); 889 MODULE_DESCRIPTION("Counter with CBC MAC");
888 MODULE_ALIAS("ccm_base"); 890 MODULE_ALIAS("ccm_base");
889 MODULE_ALIAS("rfc4309"); 891 MODULE_ALIAS("rfc4309");
890 892
|
This page was automatically generated by the
LXR engine.
|