xfrm_algo.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. /*
  2. * xfrm algorithm interface
  3. *
  4. * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
  5. *
  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.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/kernel.h>
  13. #include <linux/pfkeyv2.h>
  14. #include <linux/crypto.h>
  15. #include <linux/scatterlist.h>
  16. #include <net/xfrm.h>
  17. #if defined(CONFIG_INET_AH) || defined(CONFIG_INET_AH_MODULE) || defined(CONFIG_INET6_AH) || defined(CONFIG_INET6_AH_MODULE)
  18. #include <net/ah.h>
  19. #endif
  20. #if defined(CONFIG_INET_ESP) || defined(CONFIG_INET_ESP_MODULE) || defined(CONFIG_INET6_ESP) || defined(CONFIG_INET6_ESP_MODULE)
  21. #include <net/esp.h>
  22. #endif
  23. /*
  24. * Algorithms supported by IPsec. These entries contain properties which
  25. * are used in key negotiation and xfrm processing, and are used to verify
  26. * that instantiated crypto transforms have correct parameters for IPsec
  27. * purposes.
  28. */
  29. static struct xfrm_algo_desc aalg_list[] = {
  30. {
  31. .name = "hmac(digest_null)",
  32. .compat = "digest_null",
  33. .uinfo = {
  34. .auth = {
  35. .icv_truncbits = 0,
  36. .icv_fullbits = 0,
  37. }
  38. },
  39. .desc = {
  40. .sadb_alg_id = SADB_X_AALG_NULL,
  41. .sadb_alg_ivlen = 0,
  42. .sadb_alg_minbits = 0,
  43. .sadb_alg_maxbits = 0
  44. }
  45. },
  46. {
  47. .name = "hmac(md5)",
  48. .compat = "md5",
  49. .uinfo = {
  50. .auth = {
  51. .icv_truncbits = 96,
  52. .icv_fullbits = 128,
  53. }
  54. },
  55. .desc = {
  56. .sadb_alg_id = SADB_AALG_MD5HMAC,
  57. .sadb_alg_ivlen = 0,
  58. .sadb_alg_minbits = 128,
  59. .sadb_alg_maxbits = 128
  60. }
  61. },
  62. {
  63. .name = "hmac(sha1)",
  64. .compat = "sha1",
  65. .uinfo = {
  66. .auth = {
  67. .icv_truncbits = 96,
  68. .icv_fullbits = 160,
  69. }
  70. },
  71. .desc = {
  72. .sadb_alg_id = SADB_AALG_SHA1HMAC,
  73. .sadb_alg_ivlen = 0,
  74. .sadb_alg_minbits = 160,
  75. .sadb_alg_maxbits = 160
  76. }
  77. },
  78. {
  79. .name = "hmac(sha256)",
  80. .compat = "sha256",
  81. .uinfo = {
  82. .auth = {
  83. .icv_truncbits = 96,
  84. .icv_fullbits = 256,
  85. }
  86. },
  87. .desc = {
  88. .sadb_alg_id = SADB_X_AALG_SHA2_256HMAC,
  89. .sadb_alg_ivlen = 0,
  90. .sadb_alg_minbits = 256,
  91. .sadb_alg_maxbits = 256
  92. }
  93. },
  94. {
  95. .name = "hmac(ripemd160)",
  96. .compat = "ripemd160",
  97. .uinfo = {
  98. .auth = {
  99. .icv_truncbits = 96,
  100. .icv_fullbits = 160,
  101. }
  102. },
  103. .desc = {
  104. .sadb_alg_id = SADB_X_AALG_RIPEMD160HMAC,
  105. .sadb_alg_ivlen = 0,
  106. .sadb_alg_minbits = 160,
  107. .sadb_alg_maxbits = 160
  108. }
  109. },
  110. {
  111. .name = "xcbc(aes)",
  112. .uinfo = {
  113. .auth = {
  114. .icv_truncbits = 96,
  115. .icv_fullbits = 128,
  116. }
  117. },
  118. .desc = {
  119. .sadb_alg_id = SADB_X_AALG_AES_XCBC_MAC,
  120. .sadb_alg_ivlen = 0,
  121. .sadb_alg_minbits = 128,
  122. .sadb_alg_maxbits = 128
  123. }
  124. },
  125. };
  126. static struct xfrm_algo_desc ealg_list[] = {
  127. {
  128. .name = "ecb(cipher_null)",
  129. .compat = "cipher_null",
  130. .uinfo = {
  131. .encr = {
  132. .blockbits = 8,
  133. .defkeybits = 0,
  134. }
  135. },
  136. .desc = {
  137. .sadb_alg_id = SADB_EALG_NULL,
  138. .sadb_alg_ivlen = 0,
  139. .sadb_alg_minbits = 0,
  140. .sadb_alg_maxbits = 0
  141. }
  142. },
  143. {
  144. .name = "cbc(des)",
  145. .compat = "des",
  146. .uinfo = {
  147. .encr = {
  148. .blockbits = 64,
  149. .defkeybits = 64,
  150. }
  151. },
  152. .desc = {
  153. .sadb_alg_id = SADB_EALG_DESCBC,
  154. .sadb_alg_ivlen = 8,
  155. .sadb_alg_minbits = 64,
  156. .sadb_alg_maxbits = 64
  157. }
  158. },
  159. {
  160. .name = "cbc(des3_ede)",
  161. .compat = "des3_ede",
  162. .uinfo = {
  163. .encr = {
  164. .blockbits = 64,
  165. .defkeybits = 192,
  166. }
  167. },
  168. .desc = {
  169. .sadb_alg_id = SADB_EALG_3DESCBC,
  170. .sadb_alg_ivlen = 8,
  171. .sadb_alg_minbits = 192,
  172. .sadb_alg_maxbits = 192
  173. }
  174. },
  175. {
  176. .name = "cbc(cast128)",
  177. .compat = "cast128",
  178. .uinfo = {
  179. .encr = {
  180. .blockbits = 64,
  181. .defkeybits = 128,
  182. }
  183. },
  184. .desc = {
  185. .sadb_alg_id = SADB_X_EALG_CASTCBC,
  186. .sadb_alg_ivlen = 8,
  187. .sadb_alg_minbits = 40,
  188. .sadb_alg_maxbits = 128
  189. }
  190. },
  191. {
  192. .name = "cbc(blowfish)",
  193. .compat = "blowfish",
  194. .uinfo = {
  195. .encr = {
  196. .blockbits = 64,
  197. .defkeybits = 128,
  198. }
  199. },
  200. .desc = {
  201. .sadb_alg_id = SADB_X_EALG_BLOWFISHCBC,
  202. .sadb_alg_ivlen = 8,
  203. .sadb_alg_minbits = 40,
  204. .sadb_alg_maxbits = 448
  205. }
  206. },
  207. {
  208. .name = "cbc(aes)",
  209. .compat = "aes",
  210. .uinfo = {
  211. .encr = {
  212. .blockbits = 128,
  213. .defkeybits = 128,
  214. }
  215. },
  216. .desc = {
  217. .sadb_alg_id = SADB_X_EALG_AESCBC,
  218. .sadb_alg_ivlen = 8,
  219. .sadb_alg_minbits = 128,
  220. .sadb_alg_maxbits = 256
  221. }
  222. },
  223. {
  224. .name = "cbc(serpent)",
  225. .compat = "serpent",
  226. .uinfo = {
  227. .encr = {
  228. .blockbits = 128,
  229. .defkeybits = 128,
  230. }
  231. },
  232. .desc = {
  233. .sadb_alg_id = SADB_X_EALG_SERPENTCBC,
  234. .sadb_alg_ivlen = 8,
  235. .sadb_alg_minbits = 128,
  236. .sadb_alg_maxbits = 256,
  237. }
  238. },
  239. {
  240. .name = "cbc(camellia)",
  241. .uinfo = {
  242. .encr = {
  243. .blockbits = 128,
  244. .defkeybits = 128,
  245. }
  246. },
  247. .desc = {
  248. .sadb_alg_id = SADB_X_EALG_CAMELLIACBC,
  249. .sadb_alg_ivlen = 8,
  250. .sadb_alg_minbits = 128,
  251. .sadb_alg_maxbits = 256
  252. }
  253. },
  254. {
  255. .name = "cbc(twofish)",
  256. .compat = "twofish",
  257. .uinfo = {
  258. .encr = {
  259. .blockbits = 128,
  260. .defkeybits = 128,
  261. }
  262. },
  263. .desc = {
  264. .sadb_alg_id = SADB_X_EALG_TWOFISHCBC,
  265. .sadb_alg_ivlen = 8,
  266. .sadb_alg_minbits = 128,
  267. .sadb_alg_maxbits = 256
  268. }
  269. },
  270. };
  271. static struct xfrm_algo_desc calg_list[] = {
  272. {
  273. .name = "deflate",
  274. .uinfo = {
  275. .comp = {
  276. .threshold = 90,
  277. }
  278. },
  279. .desc = { .sadb_alg_id = SADB_X_CALG_DEFLATE }
  280. },
  281. {
  282. .name = "lzs",
  283. .uinfo = {
  284. .comp = {
  285. .threshold = 90,
  286. }
  287. },
  288. .desc = { .sadb_alg_id = SADB_X_CALG_LZS }
  289. },
  290. {
  291. .name = "lzjh",
  292. .uinfo = {
  293. .comp = {
  294. .threshold = 50,
  295. }
  296. },
  297. .desc = { .sadb_alg_id = SADB_X_CALG_LZJH }
  298. },
  299. };
  300. static inline int aalg_entries(void)
  301. {
  302. return ARRAY_SIZE(aalg_list);
  303. }
  304. static inline int ealg_entries(void)
  305. {
  306. return ARRAY_SIZE(ealg_list);
  307. }
  308. static inline int calg_entries(void)
  309. {
  310. return ARRAY_SIZE(calg_list);
  311. }
  312. struct xfrm_algo_list {
  313. struct xfrm_algo_desc *algs;
  314. int entries;
  315. u32 type;
  316. u32 mask;
  317. };
  318. static const struct xfrm_algo_list xfrm_aalg_list = {
  319. .algs = aalg_list,
  320. .entries = ARRAY_SIZE(aalg_list),
  321. .type = CRYPTO_ALG_TYPE_HASH,
  322. .mask = CRYPTO_ALG_TYPE_HASH_MASK | CRYPTO_ALG_ASYNC,
  323. };
  324. static const struct xfrm_algo_list xfrm_ealg_list = {
  325. .algs = ealg_list,
  326. .entries = ARRAY_SIZE(ealg_list),
  327. .type = CRYPTO_ALG_TYPE_BLKCIPHER,
  328. .mask = CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC,
  329. };
  330. static const struct xfrm_algo_list xfrm_calg_list = {
  331. .algs = calg_list,
  332. .entries = ARRAY_SIZE(calg_list),
  333. .type = CRYPTO_ALG_TYPE_COMPRESS,
  334. .mask = CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC,
  335. };
  336. static struct xfrm_algo_desc *xfrm_find_algo(
  337. const struct xfrm_algo_list *algo_list,
  338. int match(const struct xfrm_algo_desc *entry, const void *data),
  339. const void *data, int probe)
  340. {
  341. struct xfrm_algo_desc *list = algo_list->algs;
  342. int i, status;
  343. for (i = 0; i < algo_list->entries; i++) {
  344. if (!match(list + i, data))
  345. continue;
  346. if (list[i].available)
  347. return &list[i];
  348. if (!probe)
  349. break;
  350. status = crypto_has_alg(list[i].name, algo_list->type,
  351. algo_list->mask);
  352. if (!status)
  353. break;
  354. list[i].available = status;
  355. return &list[i];
  356. }
  357. return NULL;
  358. }
  359. static int xfrm_alg_id_match(const struct xfrm_algo_desc *entry,
  360. const void *data)
  361. {
  362. return entry->desc.sadb_alg_id == (unsigned long)data;
  363. }
  364. struct xfrm_algo_desc *xfrm_aalg_get_byid(int alg_id)
  365. {
  366. return xfrm_find_algo(&xfrm_aalg_list, xfrm_alg_id_match,
  367. (void *)(unsigned long)alg_id, 1);
  368. }
  369. EXPORT_SYMBOL_GPL(xfrm_aalg_get_byid);
  370. struct xfrm_algo_desc *xfrm_ealg_get_byid(int alg_id)
  371. {
  372. return xfrm_find_algo(&xfrm_ealg_list, xfrm_alg_id_match,
  373. (void *)(unsigned long)alg_id, 1);
  374. }
  375. EXPORT_SYMBOL_GPL(xfrm_ealg_get_byid);
  376. struct xfrm_algo_desc *xfrm_calg_get_byid(int alg_id)
  377. {
  378. return xfrm_find_algo(&xfrm_calg_list, xfrm_alg_id_match,
  379. (void *)(unsigned long)alg_id, 1);
  380. }
  381. EXPORT_SYMBOL_GPL(xfrm_calg_get_byid);
  382. static int xfrm_alg_name_match(const struct xfrm_algo_desc *entry,
  383. const void *data)
  384. {
  385. const char *name = data;
  386. return name && (!strcmp(name, entry->name) ||
  387. (entry->compat && !strcmp(name, entry->compat)));
  388. }
  389. struct xfrm_algo_desc *xfrm_aalg_get_byname(char *name, int probe)
  390. {
  391. return xfrm_find_algo(&xfrm_aalg_list, xfrm_alg_name_match, name,
  392. probe);
  393. }
  394. EXPORT_SYMBOL_GPL(xfrm_aalg_get_byname);
  395. struct xfrm_algo_desc *xfrm_ealg_get_byname(char *name, int probe)
  396. {
  397. return xfrm_find_algo(&xfrm_ealg_list, xfrm_alg_name_match, name,
  398. probe);
  399. }
  400. EXPORT_SYMBOL_GPL(xfrm_ealg_get_byname);
  401. struct xfrm_algo_desc *xfrm_calg_get_byname(char *name, int probe)
  402. {
  403. return xfrm_find_algo(&xfrm_calg_list, xfrm_alg_name_match, name,
  404. probe);
  405. }
  406. EXPORT_SYMBOL_GPL(xfrm_calg_get_byname);
  407. struct xfrm_algo_desc *xfrm_aalg_get_byidx(unsigned int idx)
  408. {
  409. if (idx >= aalg_entries())
  410. return NULL;
  411. return &aalg_list[idx];
  412. }
  413. EXPORT_SYMBOL_GPL(xfrm_aalg_get_byidx);
  414. struct xfrm_algo_desc *xfrm_ealg_get_byidx(unsigned int idx)
  415. {
  416. if (idx >= ealg_entries())
  417. return NULL;
  418. return &ealg_list[idx];
  419. }
  420. EXPORT_SYMBOL_GPL(xfrm_ealg_get_byidx);
  421. /*
  422. * Probe for the availability of crypto algorithms, and set the available
  423. * flag for any algorithms found on the system. This is typically called by
  424. * pfkey during userspace SA add, update or register.
  425. */
  426. void xfrm_probe_algs(void)
  427. {
  428. #ifdef CONFIG_CRYPTO
  429. int i, status;
  430. BUG_ON(in_softirq());
  431. for (i = 0; i < aalg_entries(); i++) {
  432. status = crypto_has_hash(aalg_list[i].name, 0,
  433. CRYPTO_ALG_ASYNC);
  434. if (aalg_list[i].available != status)
  435. aalg_list[i].available = status;
  436. }
  437. for (i = 0; i < ealg_entries(); i++) {
  438. status = crypto_has_blkcipher(ealg_list[i].name, 0,
  439. CRYPTO_ALG_ASYNC);
  440. if (ealg_list[i].available != status)
  441. ealg_list[i].available = status;
  442. }
  443. for (i = 0; i < calg_entries(); i++) {
  444. status = crypto_has_comp(calg_list[i].name, 0,
  445. CRYPTO_ALG_ASYNC);
  446. if (calg_list[i].available != status)
  447. calg_list[i].available = status;
  448. }
  449. #endif
  450. }
  451. EXPORT_SYMBOL_GPL(xfrm_probe_algs);
  452. int xfrm_count_auth_supported(void)
  453. {
  454. int i, n;
  455. for (i = 0, n = 0; i < aalg_entries(); i++)
  456. if (aalg_list[i].available)
  457. n++;
  458. return n;
  459. }
  460. EXPORT_SYMBOL_GPL(xfrm_count_auth_supported);
  461. int xfrm_count_enc_supported(void)
  462. {
  463. int i, n;
  464. for (i = 0, n = 0; i < ealg_entries(); i++)
  465. if (ealg_list[i].available)
  466. n++;
  467. return n;
  468. }
  469. EXPORT_SYMBOL_GPL(xfrm_count_enc_supported);
  470. /* Move to common area: it is shared with AH. */
  471. int skb_icv_walk(const struct sk_buff *skb, struct hash_desc *desc,
  472. int offset, int len, icv_update_fn_t icv_update)
  473. {
  474. int start = skb_headlen(skb);
  475. int i, copy = start - offset;
  476. int err;
  477. struct scatterlist sg;
  478. /* Checksum header. */
  479. if (copy > 0) {
  480. if (copy > len)
  481. copy = len;
  482. sg_init_one(&sg, skb->data + offset, copy);
  483. err = icv_update(desc, &sg, copy);
  484. if (unlikely(err))
  485. return err;
  486. if ((len -= copy) == 0)
  487. return 0;
  488. offset += copy;
  489. }
  490. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  491. int end;
  492. BUG_TRAP(start <= offset + len);
  493. end = start + skb_shinfo(skb)->frags[i].size;
  494. if ((copy = end - offset) > 0) {
  495. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  496. if (copy > len)
  497. copy = len;
  498. sg_init_table(&sg, 1);
  499. sg_set_page(&sg, frag->page, copy,
  500. frag->page_offset + offset-start);
  501. err = icv_update(desc, &sg, copy);
  502. if (unlikely(err))
  503. return err;
  504. if (!(len -= copy))
  505. return 0;
  506. offset += copy;
  507. }
  508. start = end;
  509. }
  510. if (skb_shinfo(skb)->frag_list) {
  511. struct sk_buff *list = skb_shinfo(skb)->frag_list;
  512. for (; list; list = list->next) {
  513. int end;
  514. BUG_TRAP(start <= offset + len);
  515. end = start + list->len;
  516. if ((copy = end - offset) > 0) {
  517. if (copy > len)
  518. copy = len;
  519. err = skb_icv_walk(list, desc, offset-start,
  520. copy, icv_update);
  521. if (unlikely(err))
  522. return err;
  523. if ((len -= copy) == 0)
  524. return 0;
  525. offset += copy;
  526. }
  527. start = end;
  528. }
  529. }
  530. BUG_ON(len);
  531. return 0;
  532. }
  533. EXPORT_SYMBOL_GPL(skb_icv_walk);
  534. #if defined(CONFIG_INET_ESP) || defined(CONFIG_INET_ESP_MODULE) || defined(CONFIG_INET6_ESP) || defined(CONFIG_INET6_ESP_MODULE)
  535. void *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
  536. {
  537. if (tail != skb) {
  538. skb->data_len += len;
  539. skb->len += len;
  540. }
  541. return skb_put(tail, len);
  542. }
  543. EXPORT_SYMBOL_GPL(pskb_put);
  544. #endif