[NET]: Forget the zero_it argument of sk_alloc()
[linux-2.6/x86.git] / net / xfrm / xfrm_algo.c
blob0426388d351de58bf116ae526b791c8e42c22764
1 /*
2 * xfrm algorithm interface
4 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version.
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/pfkeyv2.h>
15 #include <linux/crypto.h>
16 #include <linux/scatterlist.h>
17 #include <net/xfrm.h>
18 #if defined(CONFIG_INET_AH) || defined(CONFIG_INET_AH_MODULE) || defined(CONFIG_INET6_AH) || defined(CONFIG_INET6_AH_MODULE)
19 #include <net/ah.h>
20 #endif
21 #if defined(CONFIG_INET_ESP) || defined(CONFIG_INET_ESP_MODULE) || defined(CONFIG_INET6_ESP) || defined(CONFIG_INET6_ESP_MODULE)
22 #include <net/esp.h>
23 #endif
24 #include <asm/scatterlist.h>
27 * Algorithms supported by IPsec. These entries contain properties which
28 * are used in key negotiation and xfrm processing, and are used to verify
29 * that instantiated crypto transforms have correct parameters for IPsec
30 * purposes.
32 static struct xfrm_algo_desc aalg_list[] = {
34 .name = "hmac(digest_null)",
35 .compat = "digest_null",
37 .uinfo = {
38 .auth = {
39 .icv_truncbits = 0,
40 .icv_fullbits = 0,
44 .desc = {
45 .sadb_alg_id = SADB_X_AALG_NULL,
46 .sadb_alg_ivlen = 0,
47 .sadb_alg_minbits = 0,
48 .sadb_alg_maxbits = 0
52 .name = "hmac(md5)",
53 .compat = "md5",
55 .uinfo = {
56 .auth = {
57 .icv_truncbits = 96,
58 .icv_fullbits = 128,
62 .desc = {
63 .sadb_alg_id = SADB_AALG_MD5HMAC,
64 .sadb_alg_ivlen = 0,
65 .sadb_alg_minbits = 128,
66 .sadb_alg_maxbits = 128
70 .name = "hmac(sha1)",
71 .compat = "sha1",
73 .uinfo = {
74 .auth = {
75 .icv_truncbits = 96,
76 .icv_fullbits = 160,
80 .desc = {
81 .sadb_alg_id = SADB_AALG_SHA1HMAC,
82 .sadb_alg_ivlen = 0,
83 .sadb_alg_minbits = 160,
84 .sadb_alg_maxbits = 160
88 .name = "hmac(sha256)",
89 .compat = "sha256",
91 .uinfo = {
92 .auth = {
93 .icv_truncbits = 96,
94 .icv_fullbits = 256,
98 .desc = {
99 .sadb_alg_id = SADB_X_AALG_SHA2_256HMAC,
100 .sadb_alg_ivlen = 0,
101 .sadb_alg_minbits = 256,
102 .sadb_alg_maxbits = 256
106 .name = "hmac(ripemd160)",
107 .compat = "ripemd160",
109 .uinfo = {
110 .auth = {
111 .icv_truncbits = 96,
112 .icv_fullbits = 160,
116 .desc = {
117 .sadb_alg_id = SADB_X_AALG_RIPEMD160HMAC,
118 .sadb_alg_ivlen = 0,
119 .sadb_alg_minbits = 160,
120 .sadb_alg_maxbits = 160
124 .name = "xcbc(aes)",
126 .uinfo = {
127 .auth = {
128 .icv_truncbits = 96,
129 .icv_fullbits = 128,
133 .desc = {
134 .sadb_alg_id = SADB_X_AALG_AES_XCBC_MAC,
135 .sadb_alg_ivlen = 0,
136 .sadb_alg_minbits = 128,
137 .sadb_alg_maxbits = 128
142 static struct xfrm_algo_desc ealg_list[] = {
144 .name = "ecb(cipher_null)",
145 .compat = "cipher_null",
147 .uinfo = {
148 .encr = {
149 .blockbits = 8,
150 .defkeybits = 0,
154 .desc = {
155 .sadb_alg_id = SADB_EALG_NULL,
156 .sadb_alg_ivlen = 0,
157 .sadb_alg_minbits = 0,
158 .sadb_alg_maxbits = 0
162 .name = "cbc(des)",
163 .compat = "des",
165 .uinfo = {
166 .encr = {
167 .blockbits = 64,
168 .defkeybits = 64,
172 .desc = {
173 .sadb_alg_id = SADB_EALG_DESCBC,
174 .sadb_alg_ivlen = 8,
175 .sadb_alg_minbits = 64,
176 .sadb_alg_maxbits = 64
180 .name = "cbc(des3_ede)",
181 .compat = "des3_ede",
183 .uinfo = {
184 .encr = {
185 .blockbits = 64,
186 .defkeybits = 192,
190 .desc = {
191 .sadb_alg_id = SADB_EALG_3DESCBC,
192 .sadb_alg_ivlen = 8,
193 .sadb_alg_minbits = 192,
194 .sadb_alg_maxbits = 192
198 .name = "cbc(cast128)",
199 .compat = "cast128",
201 .uinfo = {
202 .encr = {
203 .blockbits = 64,
204 .defkeybits = 128,
208 .desc = {
209 .sadb_alg_id = SADB_X_EALG_CASTCBC,
210 .sadb_alg_ivlen = 8,
211 .sadb_alg_minbits = 40,
212 .sadb_alg_maxbits = 128
216 .name = "cbc(blowfish)",
217 .compat = "blowfish",
219 .uinfo = {
220 .encr = {
221 .blockbits = 64,
222 .defkeybits = 128,
226 .desc = {
227 .sadb_alg_id = SADB_X_EALG_BLOWFISHCBC,
228 .sadb_alg_ivlen = 8,
229 .sadb_alg_minbits = 40,
230 .sadb_alg_maxbits = 448
234 .name = "cbc(aes)",
235 .compat = "aes",
237 .uinfo = {
238 .encr = {
239 .blockbits = 128,
240 .defkeybits = 128,
244 .desc = {
245 .sadb_alg_id = SADB_X_EALG_AESCBC,
246 .sadb_alg_ivlen = 8,
247 .sadb_alg_minbits = 128,
248 .sadb_alg_maxbits = 256
252 .name = "cbc(serpent)",
253 .compat = "serpent",
255 .uinfo = {
256 .encr = {
257 .blockbits = 128,
258 .defkeybits = 128,
262 .desc = {
263 .sadb_alg_id = SADB_X_EALG_SERPENTCBC,
264 .sadb_alg_ivlen = 8,
265 .sadb_alg_minbits = 128,
266 .sadb_alg_maxbits = 256,
270 .name = "cbc(camellia)",
272 .uinfo = {
273 .encr = {
274 .blockbits = 128,
275 .defkeybits = 128,
279 .desc = {
280 .sadb_alg_id = SADB_X_EALG_CAMELLIACBC,
281 .sadb_alg_ivlen = 8,
282 .sadb_alg_minbits = 128,
283 .sadb_alg_maxbits = 256
287 .name = "cbc(twofish)",
288 .compat = "twofish",
290 .uinfo = {
291 .encr = {
292 .blockbits = 128,
293 .defkeybits = 128,
297 .desc = {
298 .sadb_alg_id = SADB_X_EALG_TWOFISHCBC,
299 .sadb_alg_ivlen = 8,
300 .sadb_alg_minbits = 128,
301 .sadb_alg_maxbits = 256
306 static struct xfrm_algo_desc calg_list[] = {
308 .name = "deflate",
309 .uinfo = {
310 .comp = {
311 .threshold = 90,
314 .desc = { .sadb_alg_id = SADB_X_CALG_DEFLATE }
317 .name = "lzs",
318 .uinfo = {
319 .comp = {
320 .threshold = 90,
323 .desc = { .sadb_alg_id = SADB_X_CALG_LZS }
326 .name = "lzjh",
327 .uinfo = {
328 .comp = {
329 .threshold = 50,
332 .desc = { .sadb_alg_id = SADB_X_CALG_LZJH }
336 static inline int aalg_entries(void)
338 return ARRAY_SIZE(aalg_list);
341 static inline int ealg_entries(void)
343 return ARRAY_SIZE(ealg_list);
346 static inline int calg_entries(void)
348 return ARRAY_SIZE(calg_list);
351 struct xfrm_algo_list {
352 struct xfrm_algo_desc *algs;
353 int entries;
354 u32 type;
355 u32 mask;
358 static const struct xfrm_algo_list xfrm_aalg_list = {
359 .algs = aalg_list,
360 .entries = ARRAY_SIZE(aalg_list),
361 .type = CRYPTO_ALG_TYPE_HASH,
362 .mask = CRYPTO_ALG_TYPE_HASH_MASK | CRYPTO_ALG_ASYNC,
365 static const struct xfrm_algo_list xfrm_ealg_list = {
366 .algs = ealg_list,
367 .entries = ARRAY_SIZE(ealg_list),
368 .type = CRYPTO_ALG_TYPE_BLKCIPHER,
369 .mask = CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC,
372 static const struct xfrm_algo_list xfrm_calg_list = {
373 .algs = calg_list,
374 .entries = ARRAY_SIZE(calg_list),
375 .type = CRYPTO_ALG_TYPE_COMPRESS,
376 .mask = CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC,
379 static struct xfrm_algo_desc *xfrm_find_algo(
380 const struct xfrm_algo_list *algo_list,
381 int match(const struct xfrm_algo_desc *entry, const void *data),
382 const void *data, int probe)
384 struct xfrm_algo_desc *list = algo_list->algs;
385 int i, status;
387 for (i = 0; i < algo_list->entries; i++) {
388 if (!match(list + i, data))
389 continue;
391 if (list[i].available)
392 return &list[i];
394 if (!probe)
395 break;
397 status = crypto_has_alg(list[i].name, algo_list->type,
398 algo_list->mask);
399 if (!status)
400 break;
402 list[i].available = status;
403 return &list[i];
405 return NULL;
408 static int xfrm_alg_id_match(const struct xfrm_algo_desc *entry,
409 const void *data)
411 return entry->desc.sadb_alg_id == (unsigned long)data;
414 struct xfrm_algo_desc *xfrm_aalg_get_byid(int alg_id)
416 return xfrm_find_algo(&xfrm_aalg_list, xfrm_alg_id_match,
417 (void *)(unsigned long)alg_id, 1);
419 EXPORT_SYMBOL_GPL(xfrm_aalg_get_byid);
421 struct xfrm_algo_desc *xfrm_ealg_get_byid(int alg_id)
423 return xfrm_find_algo(&xfrm_ealg_list, xfrm_alg_id_match,
424 (void *)(unsigned long)alg_id, 1);
426 EXPORT_SYMBOL_GPL(xfrm_ealg_get_byid);
428 struct xfrm_algo_desc *xfrm_calg_get_byid(int alg_id)
430 return xfrm_find_algo(&xfrm_calg_list, xfrm_alg_id_match,
431 (void *)(unsigned long)alg_id, 1);
433 EXPORT_SYMBOL_GPL(xfrm_calg_get_byid);
435 static int xfrm_alg_name_match(const struct xfrm_algo_desc *entry,
436 const void *data)
438 const char *name = data;
440 return name && (!strcmp(name, entry->name) ||
441 (entry->compat && !strcmp(name, entry->compat)));
444 struct xfrm_algo_desc *xfrm_aalg_get_byname(char *name, int probe)
446 return xfrm_find_algo(&xfrm_aalg_list, xfrm_alg_name_match, name,
447 probe);
449 EXPORT_SYMBOL_GPL(xfrm_aalg_get_byname);
451 struct xfrm_algo_desc *xfrm_ealg_get_byname(char *name, int probe)
453 return xfrm_find_algo(&xfrm_ealg_list, xfrm_alg_name_match, name,
454 probe);
456 EXPORT_SYMBOL_GPL(xfrm_ealg_get_byname);
458 struct xfrm_algo_desc *xfrm_calg_get_byname(char *name, int probe)
460 return xfrm_find_algo(&xfrm_calg_list, xfrm_alg_name_match, name,
461 probe);
463 EXPORT_SYMBOL_GPL(xfrm_calg_get_byname);
465 struct xfrm_algo_desc *xfrm_aalg_get_byidx(unsigned int idx)
467 if (idx >= aalg_entries())
468 return NULL;
470 return &aalg_list[idx];
472 EXPORT_SYMBOL_GPL(xfrm_aalg_get_byidx);
474 struct xfrm_algo_desc *xfrm_ealg_get_byidx(unsigned int idx)
476 if (idx >= ealg_entries())
477 return NULL;
479 return &ealg_list[idx];
481 EXPORT_SYMBOL_GPL(xfrm_ealg_get_byidx);
484 * Probe for the availability of crypto algorithms, and set the available
485 * flag for any algorithms found on the system. This is typically called by
486 * pfkey during userspace SA add, update or register.
488 void xfrm_probe_algs(void)
490 #ifdef CONFIG_CRYPTO
491 int i, status;
493 BUG_ON(in_softirq());
495 for (i = 0; i < aalg_entries(); i++) {
496 status = crypto_has_hash(aalg_list[i].name, 0,
497 CRYPTO_ALG_ASYNC);
498 if (aalg_list[i].available != status)
499 aalg_list[i].available = status;
502 for (i = 0; i < ealg_entries(); i++) {
503 status = crypto_has_blkcipher(ealg_list[i].name, 0,
504 CRYPTO_ALG_ASYNC);
505 if (ealg_list[i].available != status)
506 ealg_list[i].available = status;
509 for (i = 0; i < calg_entries(); i++) {
510 status = crypto_has_comp(calg_list[i].name, 0,
511 CRYPTO_ALG_ASYNC);
512 if (calg_list[i].available != status)
513 calg_list[i].available = status;
515 #endif
517 EXPORT_SYMBOL_GPL(xfrm_probe_algs);
519 int xfrm_count_auth_supported(void)
521 int i, n;
523 for (i = 0, n = 0; i < aalg_entries(); i++)
524 if (aalg_list[i].available)
525 n++;
526 return n;
528 EXPORT_SYMBOL_GPL(xfrm_count_auth_supported);
530 int xfrm_count_enc_supported(void)
532 int i, n;
534 for (i = 0, n = 0; i < ealg_entries(); i++)
535 if (ealg_list[i].available)
536 n++;
537 return n;
539 EXPORT_SYMBOL_GPL(xfrm_count_enc_supported);
541 /* Move to common area: it is shared with AH. */
543 int skb_icv_walk(const struct sk_buff *skb, struct hash_desc *desc,
544 int offset, int len, icv_update_fn_t icv_update)
546 int start = skb_headlen(skb);
547 int i, copy = start - offset;
548 int err;
549 struct scatterlist sg;
551 /* Checksum header. */
552 if (copy > 0) {
553 if (copy > len)
554 copy = len;
556 sg_init_one(&sg, skb->data + offset, copy);
558 err = icv_update(desc, &sg, copy);
559 if (unlikely(err))
560 return err;
562 if ((len -= copy) == 0)
563 return 0;
564 offset += copy;
567 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
568 int end;
570 BUG_TRAP(start <= offset + len);
572 end = start + skb_shinfo(skb)->frags[i].size;
573 if ((copy = end - offset) > 0) {
574 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
576 if (copy > len)
577 copy = len;
579 sg_init_table(&sg, 1);
580 sg_set_page(&sg, frag->page, copy,
581 frag->page_offset + offset-start);
583 err = icv_update(desc, &sg, copy);
584 if (unlikely(err))
585 return err;
587 if (!(len -= copy))
588 return 0;
589 offset += copy;
591 start = end;
594 if (skb_shinfo(skb)->frag_list) {
595 struct sk_buff *list = skb_shinfo(skb)->frag_list;
597 for (; list; list = list->next) {
598 int end;
600 BUG_TRAP(start <= offset + len);
602 end = start + list->len;
603 if ((copy = end - offset) > 0) {
604 if (copy > len)
605 copy = len;
606 err = skb_icv_walk(list, desc, offset-start,
607 copy, icv_update);
608 if (unlikely(err))
609 return err;
610 if ((len -= copy) == 0)
611 return 0;
612 offset += copy;
614 start = end;
617 BUG_ON(len);
618 return 0;
620 EXPORT_SYMBOL_GPL(skb_icv_walk);
622 #if defined(CONFIG_INET_ESP) || defined(CONFIG_INET_ESP_MODULE) || defined(CONFIG_INET6_ESP) || defined(CONFIG_INET6_ESP_MODULE)
624 void *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
626 if (tail != skb) {
627 skb->data_len += len;
628 skb->len += len;
630 return skb_put(tail, len);
632 EXPORT_SYMBOL_GPL(pskb_put);
633 #endif