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)
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>
18 #if defined(CONFIG_INET_AH) || defined(CONFIG_INET_AH_MODULE) || defined(CONFIG_INET6_AH) || defined(CONFIG_INET6_AH_MODULE)
21 #if defined(CONFIG_INET_ESP) || defined(CONFIG_INET_ESP_MODULE) || defined(CONFIG_INET6_ESP) || defined(CONFIG_INET6_ESP_MODULE)
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
32 static struct xfrm_algo_desc aalg_list[] = {
34 .name = "hmac(digest_null)",
35 .compat = "digest_null",
45 .sadb_alg_id = SADB_X_AALG_NULL,
47 .sadb_alg_minbits = 0,
63 .sadb_alg_id = SADB_AALG_MD5HMAC,
65 .sadb_alg_minbits = 128,
66 .sadb_alg_maxbits = 128
81 .sadb_alg_id = SADB_AALG_SHA1HMAC,
83 .sadb_alg_minbits = 160,
84 .sadb_alg_maxbits = 160
88 .name = "hmac(sha256)",
99 .sadb_alg_id = SADB_X_AALG_SHA2_256HMAC,
101 .sadb_alg_minbits = 256,
102 .sadb_alg_maxbits = 256
106 .name = "hmac(ripemd160)",
107 .compat = "ripemd160",
117 .sadb_alg_id = SADB_X_AALG_RIPEMD160HMAC,
119 .sadb_alg_minbits = 160,
120 .sadb_alg_maxbits = 160
134 .sadb_alg_id = SADB_X_AALG_AES_XCBC_MAC,
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",
155 .sadb_alg_id = SADB_EALG_NULL,
157 .sadb_alg_minbits = 0,
158 .sadb_alg_maxbits = 0
173 .sadb_alg_id = SADB_EALG_DESCBC,
175 .sadb_alg_minbits = 64,
176 .sadb_alg_maxbits = 64
180 .name = "cbc(des3_ede)",
181 .compat = "des3_ede",
191 .sadb_alg_id = SADB_EALG_3DESCBC,
193 .sadb_alg_minbits = 192,
194 .sadb_alg_maxbits = 192
198 .name = "cbc(cast128)",
209 .sadb_alg_id = SADB_X_EALG_CASTCBC,
211 .sadb_alg_minbits = 40,
212 .sadb_alg_maxbits = 128
216 .name = "cbc(blowfish)",
217 .compat = "blowfish",
227 .sadb_alg_id = SADB_X_EALG_BLOWFISHCBC,
229 .sadb_alg_minbits = 40,
230 .sadb_alg_maxbits = 448
245 .sadb_alg_id = SADB_X_EALG_AESCBC,
247 .sadb_alg_minbits = 128,
248 .sadb_alg_maxbits = 256
252 .name = "cbc(serpent)",
263 .sadb_alg_id = SADB_X_EALG_SERPENTCBC,
265 .sadb_alg_minbits = 128,
266 .sadb_alg_maxbits = 256,
270 .name = "cbc(camellia)",
280 .sadb_alg_id = SADB_X_EALG_CAMELLIACBC,
282 .sadb_alg_minbits = 128,
283 .sadb_alg_maxbits = 256
287 .name = "cbc(twofish)",
298 .sadb_alg_id = SADB_X_EALG_TWOFISHCBC,
300 .sadb_alg_minbits = 128,
301 .sadb_alg_maxbits = 256
306 static struct xfrm_algo_desc calg_list[] = {
314 .desc = { .sadb_alg_id = SADB_X_CALG_DEFLATE }
323 .desc = { .sadb_alg_id = SADB_X_CALG_LZS }
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;
358 static const struct xfrm_algo_list xfrm_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 = {
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 = {
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;
387 for (i = 0; i < algo_list->entries; i++) {
388 if (!match(list + i, data))
391 if (list[i].available)
397 status = crypto_has_alg(list[i].name, algo_list->type,
402 list[i].available = status;
408 static int xfrm_alg_id_match(const struct xfrm_algo_desc *entry,
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,
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,
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,
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,
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())
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())
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)
493 BUG_ON(in_softirq());
495 for (i = 0; i < aalg_entries(); i++) {
496 status = crypto_has_hash(aalg_list[i].name, 0,
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,
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,
512 if (calg_list[i].available != status)
513 calg_list[i].available = status;
517 EXPORT_SYMBOL_GPL(xfrm_probe_algs);
519 int xfrm_count_auth_supported(void)
523 for (i = 0, n = 0; i < aalg_entries(); i++)
524 if (aalg_list[i].available)
528 EXPORT_SYMBOL_GPL(xfrm_count_auth_supported);
530 int xfrm_count_enc_supported(void)
534 for (i = 0, n = 0; i < ealg_entries(); i++)
535 if (ealg_list[i].available)
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;
549 struct scatterlist sg;
551 /* Checksum header. */
556 sg_init_one(&sg, skb->data + offset, copy);
558 err = icv_update(desc, &sg, copy);
562 if ((len -= copy) == 0)
567 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
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];
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);
594 if (skb_shinfo(skb)->frag_list) {
595 struct sk_buff *list = skb_shinfo(skb)->frag_list;
597 for (; list; list = list->next) {
600 BUG_TRAP(start <= offset + len);
602 end = start + list->len;
603 if ((copy = end - offset) > 0) {
606 err = skb_icv_walk(list, desc, offset-start,
610 if ((len -= copy) == 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)
627 skb->data_len += len;
630 return skb_put(tail, len);
632 EXPORT_SYMBOL_GPL(pskb_put);