xfrm_algo.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  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 aead_list[] = {
  30. {
  31. .name = "rfc4106(gcm(aes))",
  32. .uinfo = {
  33. .aead = {
  34. .icv_truncbits = 64,
  35. }
  36. },
  37. .desc = {
  38. .sadb_alg_id = SADB_X_EALG_AES_GCM_ICV8,
  39. .sadb_alg_ivlen = 8,
  40. .sadb_alg_minbits = 128,
  41. .sadb_alg_maxbits = 256
  42. }
  43. },
  44. {
  45. .name = "rfc4106(gcm(aes))",
  46. .uinfo = {
  47. .aead = {
  48. .icv_truncbits = 96,
  49. }
  50. },
  51. .desc = {
  52. .sadb_alg_id = SADB_X_EALG_AES_GCM_ICV12,
  53. .sadb_alg_ivlen = 8,
  54. .sadb_alg_minbits = 128,
  55. .sadb_alg_maxbits = 256
  56. }
  57. },
  58. {
  59. .name = "rfc4106(gcm(aes))",
  60. .uinfo = {
  61. .aead = {
  62. .icv_truncbits = 128,
  63. }
  64. },
  65. .desc = {
  66. .sadb_alg_id = SADB_X_EALG_AES_GCM_ICV16,
  67. .sadb_alg_ivlen = 8,
  68. .sadb_alg_minbits = 128,
  69. .sadb_alg_maxbits = 256
  70. }
  71. },
  72. {
  73. .name = "rfc4309(ccm(aes))",
  74. .uinfo = {
  75. .aead = {
  76. .icv_truncbits = 64,
  77. }
  78. },
  79. .desc = {
  80. .sadb_alg_id = SADB_X_EALG_AES_CCM_ICV8,
  81. .sadb_alg_ivlen = 8,
  82. .sadb_alg_minbits = 128,
  83. .sadb_alg_maxbits = 256
  84. }
  85. },
  86. {
  87. .name = "rfc4309(ccm(aes))",
  88. .uinfo = {
  89. .aead = {
  90. .icv_truncbits = 96,
  91. }
  92. },
  93. .desc = {
  94. .sadb_alg_id = SADB_X_EALG_AES_CCM_ICV12,
  95. .sadb_alg_ivlen = 8,
  96. .sadb_alg_minbits = 128,
  97. .sadb_alg_maxbits = 256
  98. }
  99. },
  100. {
  101. .name = "rfc4309(ccm(aes))",
  102. .uinfo = {
  103. .aead = {
  104. .icv_truncbits = 128,
  105. }
  106. },
  107. .desc = {
  108. .sadb_alg_id = SADB_X_EALG_AES_CCM_ICV16,
  109. .sadb_alg_ivlen = 8,
  110. .sadb_alg_minbits = 128,
  111. .sadb_alg_maxbits = 256
  112. }
  113. },
  114. };
  115. static struct xfrm_algo_desc aalg_list[] = {
  116. {
  117. .name = "digest_null",
  118. .uinfo = {
  119. .auth = {
  120. .icv_truncbits = 0,
  121. .icv_fullbits = 0,
  122. }
  123. },
  124. .desc = {
  125. .sadb_alg_id = SADB_X_AALG_NULL,
  126. .sadb_alg_ivlen = 0,
  127. .sadb_alg_minbits = 0,
  128. .sadb_alg_maxbits = 0
  129. }
  130. },
  131. {
  132. .name = "hmac(md5)",
  133. .compat = "md5",
  134. .uinfo = {
  135. .auth = {
  136. .icv_truncbits = 96,
  137. .icv_fullbits = 128,
  138. }
  139. },
  140. .desc = {
  141. .sadb_alg_id = SADB_AALG_MD5HMAC,
  142. .sadb_alg_ivlen = 0,
  143. .sadb_alg_minbits = 128,
  144. .sadb_alg_maxbits = 128
  145. }
  146. },
  147. {
  148. .name = "hmac(sha1)",
  149. .compat = "sha1",
  150. .uinfo = {
  151. .auth = {
  152. .icv_truncbits = 96,
  153. .icv_fullbits = 160,
  154. }
  155. },
  156. .desc = {
  157. .sadb_alg_id = SADB_AALG_SHA1HMAC,
  158. .sadb_alg_ivlen = 0,
  159. .sadb_alg_minbits = 160,
  160. .sadb_alg_maxbits = 160
  161. }
  162. },
  163. {
  164. .name = "hmac(sha256)",
  165. .compat = "sha256",
  166. .uinfo = {
  167. .auth = {
  168. .icv_truncbits = 96,
  169. .icv_fullbits = 256,
  170. }
  171. },
  172. .desc = {
  173. .sadb_alg_id = SADB_X_AALG_SHA2_256HMAC,
  174. .sadb_alg_ivlen = 0,
  175. .sadb_alg_minbits = 256,
  176. .sadb_alg_maxbits = 256
  177. }
  178. },
  179. {
  180. .name = "hmac(sha384)",
  181. .uinfo = {
  182. .auth = {
  183. .icv_truncbits = 192,
  184. .icv_fullbits = 384,
  185. }
  186. },
  187. .desc = {
  188. .sadb_alg_id = SADB_X_AALG_SHA2_384HMAC,
  189. .sadb_alg_ivlen = 0,
  190. .sadb_alg_minbits = 384,
  191. .sadb_alg_maxbits = 384
  192. }
  193. },
  194. {
  195. .name = "hmac(sha512)",
  196. .uinfo = {
  197. .auth = {
  198. .icv_truncbits = 256,
  199. .icv_fullbits = 512,
  200. }
  201. },
  202. .desc = {
  203. .sadb_alg_id = SADB_X_AALG_SHA2_512HMAC,
  204. .sadb_alg_ivlen = 0,
  205. .sadb_alg_minbits = 512,
  206. .sadb_alg_maxbits = 512
  207. }
  208. },
  209. {
  210. .name = "hmac(rmd160)",
  211. .compat = "rmd160",
  212. .uinfo = {
  213. .auth = {
  214. .icv_truncbits = 96,
  215. .icv_fullbits = 160,
  216. }
  217. },
  218. .desc = {
  219. .sadb_alg_id = SADB_X_AALG_RIPEMD160HMAC,
  220. .sadb_alg_ivlen = 0,
  221. .sadb_alg_minbits = 160,
  222. .sadb_alg_maxbits = 160
  223. }
  224. },
  225. {
  226. .name = "xcbc(aes)",
  227. .uinfo = {
  228. .auth = {
  229. .icv_truncbits = 96,
  230. .icv_fullbits = 128,
  231. }
  232. },
  233. .desc = {
  234. .sadb_alg_id = SADB_X_AALG_AES_XCBC_MAC,
  235. .sadb_alg_ivlen = 0,
  236. .sadb_alg_minbits = 128,
  237. .sadb_alg_maxbits = 128
  238. }
  239. },
  240. };
  241. static struct xfrm_algo_desc ealg_list[] = {
  242. {
  243. .name = "ecb(cipher_null)",
  244. .compat = "cipher_null",
  245. .uinfo = {
  246. .encr = {
  247. .blockbits = 8,
  248. .defkeybits = 0,
  249. }
  250. },
  251. .desc = {
  252. .sadb_alg_id = SADB_EALG_NULL,
  253. .sadb_alg_ivlen = 0,
  254. .sadb_alg_minbits = 0,
  255. .sadb_alg_maxbits = 0
  256. }
  257. },
  258. {
  259. .name = "cbc(des)",
  260. .compat = "des",
  261. .uinfo = {
  262. .encr = {
  263. .blockbits = 64,
  264. .defkeybits = 64,
  265. }
  266. },
  267. .desc = {
  268. .sadb_alg_id = SADB_EALG_DESCBC,
  269. .sadb_alg_ivlen = 8,
  270. .sadb_alg_minbits = 64,
  271. .sadb_alg_maxbits = 64
  272. }
  273. },
  274. {
  275. .name = "cbc(des3_ede)",
  276. .compat = "des3_ede",
  277. .uinfo = {
  278. .encr = {
  279. .blockbits = 64,
  280. .defkeybits = 192,
  281. }
  282. },
  283. .desc = {
  284. .sadb_alg_id = SADB_EALG_3DESCBC,
  285. .sadb_alg_ivlen = 8,
  286. .sadb_alg_minbits = 192,
  287. .sadb_alg_maxbits = 192
  288. }
  289. },
  290. {
  291. .name = "cbc(cast5)",
  292. .compat = "cast5",
  293. .uinfo = {
  294. .encr = {
  295. .blockbits = 64,
  296. .defkeybits = 128,
  297. }
  298. },
  299. .desc = {
  300. .sadb_alg_id = SADB_X_EALG_CASTCBC,
  301. .sadb_alg_ivlen = 8,
  302. .sadb_alg_minbits = 40,
  303. .sadb_alg_maxbits = 128
  304. }
  305. },
  306. {
  307. .name = "cbc(blowfish)",
  308. .compat = "blowfish",
  309. .uinfo = {
  310. .encr = {
  311. .blockbits = 64,
  312. .defkeybits = 128,
  313. }
  314. },
  315. .desc = {
  316. .sadb_alg_id = SADB_X_EALG_BLOWFISHCBC,
  317. .sadb_alg_ivlen = 8,
  318. .sadb_alg_minbits = 40,
  319. .sadb_alg_maxbits = 448
  320. }
  321. },
  322. {
  323. .name = "cbc(aes)",
  324. .compat = "aes",
  325. .uinfo = {
  326. .encr = {
  327. .blockbits = 128,
  328. .defkeybits = 128,
  329. }
  330. },
  331. .desc = {
  332. .sadb_alg_id = SADB_X_EALG_AESCBC,
  333. .sadb_alg_ivlen = 8,
  334. .sadb_alg_minbits = 128,
  335. .sadb_alg_maxbits = 256
  336. }
  337. },
  338. {
  339. .name = "cbc(serpent)",
  340. .compat = "serpent",
  341. .uinfo = {
  342. .encr = {
  343. .blockbits = 128,
  344. .defkeybits = 128,
  345. }
  346. },
  347. .desc = {
  348. .sadb_alg_id = SADB_X_EALG_SERPENTCBC,
  349. .sadb_alg_ivlen = 8,
  350. .sadb_alg_minbits = 128,
  351. .sadb_alg_maxbits = 256,
  352. }
  353. },
  354. {
  355. .name = "cbc(camellia)",
  356. .compat = "camellia",
  357. .uinfo = {
  358. .encr = {
  359. .blockbits = 128,
  360. .defkeybits = 128,
  361. }
  362. },
  363. .desc = {
  364. .sadb_alg_id = SADB_X_EALG_CAMELLIACBC,
  365. .sadb_alg_ivlen = 8,
  366. .sadb_alg_minbits = 128,
  367. .sadb_alg_maxbits = 256
  368. }
  369. },
  370. {
  371. .name = "cbc(twofish)",
  372. .compat = "twofish",
  373. .uinfo = {
  374. .encr = {
  375. .blockbits = 128,
  376. .defkeybits = 128,
  377. }
  378. },
  379. .desc = {
  380. .sadb_alg_id = SADB_X_EALG_TWOFISHCBC,
  381. .sadb_alg_ivlen = 8,
  382. .sadb_alg_minbits = 128,
  383. .sadb_alg_maxbits = 256
  384. }
  385. },
  386. {
  387. .name = "rfc3686(ctr(aes))",
  388. .uinfo = {
  389. .encr = {
  390. .blockbits = 128,
  391. .defkeybits = 160, /* 128-bit key + 32-bit nonce */
  392. }
  393. },
  394. .desc = {
  395. .sadb_alg_id = SADB_X_EALG_AESCTR,
  396. .sadb_alg_ivlen = 8,
  397. .sadb_alg_minbits = 128,
  398. .sadb_alg_maxbits = 256
  399. }
  400. },
  401. };
  402. static struct xfrm_algo_desc calg_list[] = {
  403. {
  404. .name = "deflate",
  405. .uinfo = {
  406. .comp = {
  407. .threshold = 90,
  408. }
  409. },
  410. .desc = { .sadb_alg_id = SADB_X_CALG_DEFLATE }
  411. },
  412. {
  413. .name = "lzs",
  414. .uinfo = {
  415. .comp = {
  416. .threshold = 90,
  417. }
  418. },
  419. .desc = { .sadb_alg_id = SADB_X_CALG_LZS }
  420. },
  421. {
  422. .name = "lzjh",
  423. .uinfo = {
  424. .comp = {
  425. .threshold = 50,
  426. }
  427. },
  428. .desc = { .sadb_alg_id = SADB_X_CALG_LZJH }
  429. },
  430. };
  431. static inline int aead_entries(void)
  432. {
  433. return ARRAY_SIZE(aead_list);
  434. }
  435. static inline int aalg_entries(void)
  436. {
  437. return ARRAY_SIZE(aalg_list);
  438. }
  439. static inline int ealg_entries(void)
  440. {
  441. return ARRAY_SIZE(ealg_list);
  442. }
  443. static inline int calg_entries(void)
  444. {
  445. return ARRAY_SIZE(calg_list);
  446. }
  447. struct xfrm_algo_list {
  448. struct xfrm_algo_desc *algs;
  449. int entries;
  450. u32 type;
  451. u32 mask;
  452. };
  453. static const struct xfrm_algo_list xfrm_aead_list = {
  454. .algs = aead_list,
  455. .entries = ARRAY_SIZE(aead_list),
  456. .type = CRYPTO_ALG_TYPE_AEAD,
  457. .mask = CRYPTO_ALG_TYPE_MASK,
  458. };
  459. static const struct xfrm_algo_list xfrm_aalg_list = {
  460. .algs = aalg_list,
  461. .entries = ARRAY_SIZE(aalg_list),
  462. .type = CRYPTO_ALG_TYPE_HASH,
  463. .mask = CRYPTO_ALG_TYPE_HASH_MASK,
  464. };
  465. static const struct xfrm_algo_list xfrm_ealg_list = {
  466. .algs = ealg_list,
  467. .entries = ARRAY_SIZE(ealg_list),
  468. .type = CRYPTO_ALG_TYPE_BLKCIPHER,
  469. .mask = CRYPTO_ALG_TYPE_BLKCIPHER_MASK,
  470. };
  471. static const struct xfrm_algo_list xfrm_calg_list = {
  472. .algs = calg_list,
  473. .entries = ARRAY_SIZE(calg_list),
  474. .type = CRYPTO_ALG_TYPE_COMPRESS,
  475. .mask = CRYPTO_ALG_TYPE_MASK,
  476. };
  477. static struct xfrm_algo_desc *xfrm_find_algo(
  478. const struct xfrm_algo_list *algo_list,
  479. int match(const struct xfrm_algo_desc *entry, const void *data),
  480. const void *data, int probe)
  481. {
  482. struct xfrm_algo_desc *list = algo_list->algs;
  483. int i, status;
  484. for (i = 0; i < algo_list->entries; i++) {
  485. if (!match(list + i, data))
  486. continue;
  487. if (list[i].available)
  488. return &list[i];
  489. if (!probe)
  490. break;
  491. status = crypto_has_alg(list[i].name, algo_list->type,
  492. algo_list->mask);
  493. if (!status)
  494. break;
  495. list[i].available = status;
  496. return &list[i];
  497. }
  498. return NULL;
  499. }
  500. static int xfrm_alg_id_match(const struct xfrm_algo_desc *entry,
  501. const void *data)
  502. {
  503. return entry->desc.sadb_alg_id == (unsigned long)data;
  504. }
  505. struct xfrm_algo_desc *xfrm_aalg_get_byid(int alg_id)
  506. {
  507. return xfrm_find_algo(&xfrm_aalg_list, xfrm_alg_id_match,
  508. (void *)(unsigned long)alg_id, 1);
  509. }
  510. EXPORT_SYMBOL_GPL(xfrm_aalg_get_byid);
  511. struct xfrm_algo_desc *xfrm_ealg_get_byid(int alg_id)
  512. {
  513. return xfrm_find_algo(&xfrm_ealg_list, xfrm_alg_id_match,
  514. (void *)(unsigned long)alg_id, 1);
  515. }
  516. EXPORT_SYMBOL_GPL(xfrm_ealg_get_byid);
  517. struct xfrm_algo_desc *xfrm_calg_get_byid(int alg_id)
  518. {
  519. return xfrm_find_algo(&xfrm_calg_list, xfrm_alg_id_match,
  520. (void *)(unsigned long)alg_id, 1);
  521. }
  522. EXPORT_SYMBOL_GPL(xfrm_calg_get_byid);
  523. static int xfrm_alg_name_match(const struct xfrm_algo_desc *entry,
  524. const void *data)
  525. {
  526. const char *name = data;
  527. return name && (!strcmp(name, entry->name) ||
  528. (entry->compat && !strcmp(name, entry->compat)));
  529. }
  530. struct xfrm_algo_desc *xfrm_aalg_get_byname(char *name, int probe)
  531. {
  532. return xfrm_find_algo(&xfrm_aalg_list, xfrm_alg_name_match, name,
  533. probe);
  534. }
  535. EXPORT_SYMBOL_GPL(xfrm_aalg_get_byname);
  536. struct xfrm_algo_desc *xfrm_ealg_get_byname(char *name, int probe)
  537. {
  538. return xfrm_find_algo(&xfrm_ealg_list, xfrm_alg_name_match, name,
  539. probe);
  540. }
  541. EXPORT_SYMBOL_GPL(xfrm_ealg_get_byname);
  542. struct xfrm_algo_desc *xfrm_calg_get_byname(char *name, int probe)
  543. {
  544. return xfrm_find_algo(&xfrm_calg_list, xfrm_alg_name_match, name,
  545. probe);
  546. }
  547. EXPORT_SYMBOL_GPL(xfrm_calg_get_byname);
  548. struct xfrm_aead_name {
  549. const char *name;
  550. int icvbits;
  551. };
  552. static int xfrm_aead_name_match(const struct xfrm_algo_desc *entry,
  553. const void *data)
  554. {
  555. const struct xfrm_aead_name *aead = data;
  556. const char *name = aead->name;
  557. return aead->icvbits == entry->uinfo.aead.icv_truncbits && name &&
  558. !strcmp(name, entry->name);
  559. }
  560. struct xfrm_algo_desc *xfrm_aead_get_byname(char *name, int icv_len, int probe)
  561. {
  562. struct xfrm_aead_name data = {
  563. .name = name,
  564. .icvbits = icv_len,
  565. };
  566. return xfrm_find_algo(&xfrm_aead_list, xfrm_aead_name_match, &data,
  567. probe);
  568. }
  569. EXPORT_SYMBOL_GPL(xfrm_aead_get_byname);
  570. struct xfrm_algo_desc *xfrm_aalg_get_byidx(unsigned int idx)
  571. {
  572. if (idx >= aalg_entries())
  573. return NULL;
  574. return &aalg_list[idx];
  575. }
  576. EXPORT_SYMBOL_GPL(xfrm_aalg_get_byidx);
  577. struct xfrm_algo_desc *xfrm_ealg_get_byidx(unsigned int idx)
  578. {
  579. if (idx >= ealg_entries())
  580. return NULL;
  581. return &ealg_list[idx];
  582. }
  583. EXPORT_SYMBOL_GPL(xfrm_ealg_get_byidx);
  584. /*
  585. * Probe for the availability of crypto algorithms, and set the available
  586. * flag for any algorithms found on the system. This is typically called by
  587. * pfkey during userspace SA add, update or register.
  588. */
  589. void xfrm_probe_algs(void)
  590. {
  591. int i, status;
  592. BUG_ON(in_softirq());
  593. for (i = 0; i < aalg_entries(); i++) {
  594. status = crypto_has_hash(aalg_list[i].name, 0,
  595. CRYPTO_ALG_ASYNC);
  596. if (aalg_list[i].available != status)
  597. aalg_list[i].available = status;
  598. }
  599. for (i = 0; i < ealg_entries(); i++) {
  600. status = crypto_has_blkcipher(ealg_list[i].name, 0,
  601. CRYPTO_ALG_ASYNC);
  602. if (ealg_list[i].available != status)
  603. ealg_list[i].available = status;
  604. }
  605. for (i = 0; i < calg_entries(); i++) {
  606. status = crypto_has_comp(calg_list[i].name, 0,
  607. CRYPTO_ALG_ASYNC);
  608. if (calg_list[i].available != status)
  609. calg_list[i].available = status;
  610. }
  611. }
  612. EXPORT_SYMBOL_GPL(xfrm_probe_algs);
  613. int xfrm_count_auth_supported(void)
  614. {
  615. int i, n;
  616. for (i = 0, n = 0; i < aalg_entries(); i++)
  617. if (aalg_list[i].available)
  618. n++;
  619. return n;
  620. }
  621. EXPORT_SYMBOL_GPL(xfrm_count_auth_supported);
  622. int xfrm_count_enc_supported(void)
  623. {
  624. int i, n;
  625. for (i = 0, n = 0; i < ealg_entries(); i++)
  626. if (ealg_list[i].available)
  627. n++;
  628. return n;
  629. }
  630. EXPORT_SYMBOL_GPL(xfrm_count_enc_supported);
  631. #if defined(CONFIG_INET_ESP) || defined(CONFIG_INET_ESP_MODULE) || defined(CONFIG_INET6_ESP) || defined(CONFIG_INET6_ESP_MODULE)
  632. void *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
  633. {
  634. if (tail != skb) {
  635. skb->data_len += len;
  636. skb->len += len;
  637. }
  638. return skb_put(tail, len);
  639. }
  640. EXPORT_SYMBOL_GPL(pskb_put);
  641. #endif