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>
17 #if defined(CONFIG_INET_AH) || defined(CONFIG_INET_AH_MODULE) || defined(CONFIG_INET6_AH) || defined(CONFIG_INET6_AH_MODULE)
20 #if defined(CONFIG_INET_ESP) || defined(CONFIG_INET_ESP_MODULE) || defined(CONFIG_INET6_ESP) || defined(CONFIG_INET6_ESP_MODULE)
23 #include <asm/scatterlist.h>
26 * Algorithms supported by IPsec. These entries contain properties which
27 * are used in key negotiation and xfrm processing, and are used to verify
28 * that instantiated crypto transforms have correct parameters for IPsec
31 static struct xfrm_algo_desc aalg_list[] = {
33 .name = "hmac(digest_null)",
34 .compat = "digest_null",
44 .sadb_alg_id = SADB_X_AALG_NULL,
46 .sadb_alg_minbits = 0,
62 .sadb_alg_id = SADB_AALG_MD5HMAC,
64 .sadb_alg_minbits = 128,
65 .sadb_alg_maxbits = 128
80 .sadb_alg_id = SADB_AALG_SHA1HMAC,
82 .sadb_alg_minbits = 160,
83 .sadb_alg_maxbits = 160
87 .name = "hmac(sha256)",
98 .sadb_alg_id = SADB_X_AALG_SHA2_256HMAC,
100 .sadb_alg_minbits = 256,
101 .sadb_alg_maxbits = 256
105 .name = "hmac(ripemd160)",
106 .compat = "ripemd160",
116 .sadb_alg_id = SADB_X_AALG_RIPEMD160HMAC,
118 .sadb_alg_minbits = 160,
119 .sadb_alg_maxbits = 160
133 .sadb_alg_id = SADB_X_AALG_AES_XCBC_MAC,
135 .sadb_alg_minbits = 128,
136 .sadb_alg_maxbits = 128
141 static struct xfrm_algo_desc ealg_list[] = {
143 .name = "ecb(cipher_null)",
144 .compat = "cipher_null",
154 .sadb_alg_id = SADB_EALG_NULL,
156 .sadb_alg_minbits = 0,
157 .sadb_alg_maxbits = 0
172 .sadb_alg_id = SADB_EALG_DESCBC,
174 .sadb_alg_minbits = 64,
175 .sadb_alg_maxbits = 64
179 .name = "cbc(des3_ede)",
180 .compat = "des3_ede",
190 .sadb_alg_id = SADB_EALG_3DESCBC,
192 .sadb_alg_minbits = 192,
193 .sadb_alg_maxbits = 192
197 .name = "cbc(cast128)",
208 .sadb_alg_id = SADB_X_EALG_CASTCBC,
210 .sadb_alg_minbits = 40,
211 .sadb_alg_maxbits = 128
215 .name = "cbc(blowfish)",
216 .compat = "blowfish",
226 .sadb_alg_id = SADB_X_EALG_BLOWFISHCBC,
228 .sadb_alg_minbits = 40,
229 .sadb_alg_maxbits = 448
244 .sadb_alg_id = SADB_X_EALG_AESCBC,
246 .sadb_alg_minbits = 128,
247 .sadb_alg_maxbits = 256
251 .name = "cbc(serpent)",
262 .sadb_alg_id = SADB_X_EALG_SERPENTCBC,
264 .sadb_alg_minbits = 128,
265 .sadb_alg_maxbits = 256,
269 .name = "cbc(camellia)",
279 .sadb_alg_id = SADB_X_EALG_CAMELLIACBC,
281 .sadb_alg_minbits = 128,
282 .sadb_alg_maxbits = 256
286 .name = "cbc(twofish)",
297 .sadb_alg_id = SADB_X_EALG_TWOFISHCBC,
299 .sadb_alg_minbits = 128,
300 .sadb_alg_maxbits = 256
305 static struct xfrm_algo_desc calg_list[] = {
313 .desc = { .sadb_alg_id = SADB_X_CALG_DEFLATE }
322 .desc = { .sadb_alg_id = SADB_X_CALG_LZS }
331 .desc = { .sadb_alg_id = SADB_X_CALG_LZJH }
335 static inline int aalg_entries(void)
337 return ARRAY_SIZE(aalg_list);
340 static inline int ealg_entries(void)
342 return ARRAY_SIZE(ealg_list);
345 static inline int calg_entries(void)
347 return ARRAY_SIZE(calg_list);
350 struct xfrm_algo_list {
351 struct xfrm_algo_desc *algs;
357 static const struct xfrm_algo_list xfrm_aalg_list = {
359 .entries = ARRAY_SIZE(aalg_list),
360 .type = CRYPTO_ALG_TYPE_HASH,
361 .mask = CRYPTO_ALG_TYPE_HASH_MASK | CRYPTO_ALG_ASYNC,
364 static const struct xfrm_algo_list xfrm_ealg_list = {
366 .entries = ARRAY_SIZE(ealg_list),
367 .type = CRYPTO_ALG_TYPE_BLKCIPHER,
368 .mask = CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC,
371 static const struct xfrm_algo_list xfrm_calg_list = {
373 .entries = ARRAY_SIZE(calg_list),
374 .type = CRYPTO_ALG_TYPE_COMPRESS,
375 .mask = CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC,
378 static struct xfrm_algo_desc *xfrm_find_algo(
379 const struct xfrm_algo_list *algo_list,
380 int match(const struct xfrm_algo_desc *entry, const void *data),
381 const void *data, int probe)
383 struct xfrm_algo_desc *list = algo_list->algs;
386 for (i = 0; i < algo_list->entries; i++) {
387 if (!match(list + i, data))
390 if (list[i].available)
396 status = crypto_has_alg(list[i].name, algo_list->type,
401 list[i].available = status;
407 static int xfrm_alg_id_match(const struct xfrm_algo_desc *entry,
410 return entry->desc.sadb_alg_id == (unsigned long)data;
413 struct xfrm_algo_desc *xfrm_aalg_get_byid(int alg_id)
415 return xfrm_find_algo(&xfrm_aalg_list, xfrm_alg_id_match,
416 (void *)(unsigned long)alg_id, 1);
418 EXPORT_SYMBOL_GPL(xfrm_aalg_get_byid);
420 struct xfrm_algo_desc *xfrm_ealg_get_byid(int alg_id)
422 return xfrm_find_algo(&xfrm_ealg_list, xfrm_alg_id_match,
423 (void *)(unsigned long)alg_id, 1);
425 EXPORT_SYMBOL_GPL(xfrm_ealg_get_byid);
427 struct xfrm_algo_desc *xfrm_calg_get_byid(int alg_id)
429 return xfrm_find_algo(&xfrm_calg_list, xfrm_alg_id_match,
430 (void *)(unsigned long)alg_id, 1);
432 EXPORT_SYMBOL_GPL(xfrm_calg_get_byid);
434 static int xfrm_alg_name_match(const struct xfrm_algo_desc *entry,
437 const char *name = data;
439 return name && (!strcmp(name, entry->name) ||
440 (entry->compat && !strcmp(name, entry->compat)));
443 struct xfrm_algo_desc *xfrm_aalg_get_byname(char *name, int probe)
445 return xfrm_find_algo(&xfrm_aalg_list, xfrm_alg_name_match, name,
448 EXPORT_SYMBOL_GPL(xfrm_aalg_get_byname);
450 struct xfrm_algo_desc *xfrm_ealg_get_byname(char *name, int probe)
452 return xfrm_find_algo(&xfrm_ealg_list, xfrm_alg_name_match, name,
455 EXPORT_SYMBOL_GPL(xfrm_ealg_get_byname);
457 struct xfrm_algo_desc *xfrm_calg_get_byname(char *name, int probe)
459 return xfrm_find_algo(&xfrm_calg_list, xfrm_alg_name_match, name,
462 EXPORT_SYMBOL_GPL(xfrm_calg_get_byname);
464 struct xfrm_algo_desc *xfrm_aalg_get_byidx(unsigned int idx)
466 if (idx >= aalg_entries())
469 return &aalg_list[idx];
471 EXPORT_SYMBOL_GPL(xfrm_aalg_get_byidx);
473 struct xfrm_algo_desc *xfrm_ealg_get_byidx(unsigned int idx)
475 if (idx >= ealg_entries())
478 return &ealg_list[idx];
480 EXPORT_SYMBOL_GPL(xfrm_ealg_get_byidx);
483 * Probe for the availability of crypto algorithms, and set the available
484 * flag for any algorithms found on the system. This is typically called by
485 * pfkey during userspace SA add, update or register.
487 void xfrm_probe_algs(void)
492 BUG_ON(in_softirq());
494 for (i = 0; i < aalg_entries(); i++) {
495 status = crypto_has_hash(aalg_list[i].name, 0,
497 if (aalg_list[i].available != status)
498 aalg_list[i].available = status;
501 for (i = 0; i < ealg_entries(); i++) {
502 status = crypto_has_blkcipher(ealg_list[i].name, 0,
504 if (ealg_list[i].available != status)
505 ealg_list[i].available = status;
508 for (i = 0; i < calg_entries(); i++) {
509 status = crypto_has_comp(calg_list[i].name, 0,
511 if (calg_list[i].available != status)
512 calg_list[i].available = status;
516 EXPORT_SYMBOL_GPL(xfrm_probe_algs);
518 int xfrm_count_auth_supported(void)
522 for (i = 0, n = 0; i < aalg_entries(); i++)
523 if (aalg_list[i].available)
527 EXPORT_SYMBOL_GPL(xfrm_count_auth_supported);
529 int xfrm_count_enc_supported(void)
533 for (i = 0, n = 0; i < ealg_entries(); i++)
534 if (ealg_list[i].available)
538 EXPORT_SYMBOL_GPL(xfrm_count_enc_supported);
540 /* Move to common area: it is shared with AH. */
542 int skb_icv_walk(const struct sk_buff *skb, struct hash_desc *desc,
543 int offset, int len, icv_update_fn_t icv_update)
545 int start = skb_headlen(skb);
546 int i, copy = start - offset;
548 struct scatterlist sg;
550 /* Checksum header. */
555 sg.page = virt_to_page(skb->data + offset);
556 sg.offset = (unsigned long)(skb->data + offset) % PAGE_SIZE;
559 err = icv_update(desc, &sg, copy);
563 if ((len -= copy) == 0)
568 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
571 BUG_TRAP(start <= offset + len);
573 end = start + skb_shinfo(skb)->frags[i].size;
574 if ((copy = end - offset) > 0) {
575 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
580 sg.page = frag->page;
581 sg.offset = frag->page_offset + offset-start;
584 err = icv_update(desc, &sg, copy);
595 if (skb_shinfo(skb)->frag_list) {
596 struct sk_buff *list = skb_shinfo(skb)->frag_list;
598 for (; list; list = list->next) {
601 BUG_TRAP(start <= offset + len);
603 end = start + list->len;
604 if ((copy = end - offset) > 0) {
607 err = skb_icv_walk(list, desc, offset-start,
611 if ((len -= copy) == 0)
621 EXPORT_SYMBOL_GPL(skb_icv_walk);
623 #if defined(CONFIG_INET_ESP) || defined(CONFIG_INET_ESP_MODULE) || defined(CONFIG_INET6_ESP) || defined(CONFIG_INET6_ESP_MODULE)
625 void *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
628 skb->data_len += len;
631 return skb_put(tail, len);
633 EXPORT_SYMBOL_GPL(pskb_put);