algapi.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. /*
  2. * Cryptographic API for algorithms (i.e., low-level API).
  3. *
  4. * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.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. */
  12. #include <linux/err.h>
  13. #include <linux/errno.h>
  14. #include <linux/init.h>
  15. #include <linux/kernel.h>
  16. #include <linux/list.h>
  17. #include <linux/module.h>
  18. #include <linux/rtnetlink.h>
  19. #include <linux/string.h>
  20. #include "internal.h"
  21. static LIST_HEAD(crypto_template_list);
  22. void crypto_larval_error(const char *name, u32 type, u32 mask)
  23. {
  24. struct crypto_alg *alg;
  25. down_read(&crypto_alg_sem);
  26. alg = __crypto_alg_lookup(name, type, mask);
  27. up_read(&crypto_alg_sem);
  28. if (alg) {
  29. if (crypto_is_larval(alg)) {
  30. struct crypto_larval *larval = (void *)alg;
  31. complete_all(&larval->completion);
  32. }
  33. crypto_mod_put(alg);
  34. }
  35. }
  36. EXPORT_SYMBOL_GPL(crypto_larval_error);
  37. static inline int crypto_set_driver_name(struct crypto_alg *alg)
  38. {
  39. static const char suffix[] = "-generic";
  40. char *driver_name = alg->cra_driver_name;
  41. int len;
  42. if (*driver_name)
  43. return 0;
  44. len = strlcpy(driver_name, alg->cra_name, CRYPTO_MAX_ALG_NAME);
  45. if (len + sizeof(suffix) > CRYPTO_MAX_ALG_NAME)
  46. return -ENAMETOOLONG;
  47. memcpy(driver_name + len, suffix, sizeof(suffix));
  48. return 0;
  49. }
  50. static int crypto_check_alg(struct crypto_alg *alg)
  51. {
  52. if (alg->cra_alignmask & (alg->cra_alignmask + 1))
  53. return -EINVAL;
  54. if (alg->cra_blocksize > PAGE_SIZE / 8)
  55. return -EINVAL;
  56. if (alg->cra_priority < 0)
  57. return -EINVAL;
  58. return crypto_set_driver_name(alg);
  59. }
  60. static void crypto_destroy_instance(struct crypto_alg *alg)
  61. {
  62. struct crypto_instance *inst = (void *)alg;
  63. struct crypto_template *tmpl = inst->tmpl;
  64. tmpl->free(inst);
  65. crypto_tmpl_put(tmpl);
  66. }
  67. static void crypto_remove_spawn(struct crypto_spawn *spawn,
  68. struct list_head *list,
  69. struct list_head *secondary_spawns)
  70. {
  71. struct crypto_instance *inst = spawn->inst;
  72. struct crypto_template *tmpl = inst->tmpl;
  73. list_del_init(&spawn->list);
  74. spawn->alg = NULL;
  75. if (crypto_is_dead(&inst->alg))
  76. return;
  77. inst->alg.cra_flags |= CRYPTO_ALG_DEAD;
  78. if (hlist_unhashed(&inst->list))
  79. return;
  80. if (!tmpl || !crypto_tmpl_get(tmpl))
  81. return;
  82. crypto_notify(CRYPTO_MSG_ALG_UNREGISTER, &inst->alg);
  83. list_move(&inst->alg.cra_list, list);
  84. hlist_del(&inst->list);
  85. inst->alg.cra_destroy = crypto_destroy_instance;
  86. list_splice(&inst->alg.cra_users, secondary_spawns);
  87. }
  88. static void crypto_remove_spawns(struct list_head *spawns,
  89. struct list_head *list, u32 new_type)
  90. {
  91. struct crypto_spawn *spawn, *n;
  92. LIST_HEAD(secondary_spawns);
  93. list_for_each_entry_safe(spawn, n, spawns, list) {
  94. if ((spawn->alg->cra_flags ^ new_type) & spawn->mask)
  95. continue;
  96. crypto_remove_spawn(spawn, list, &secondary_spawns);
  97. }
  98. while (!list_empty(&secondary_spawns)) {
  99. list_for_each_entry_safe(spawn, n, &secondary_spawns, list)
  100. crypto_remove_spawn(spawn, list, &secondary_spawns);
  101. }
  102. }
  103. static int __crypto_register_alg(struct crypto_alg *alg,
  104. struct list_head *list)
  105. {
  106. struct crypto_alg *q;
  107. int ret = -EAGAIN;
  108. if (crypto_is_dead(alg))
  109. goto out;
  110. INIT_LIST_HEAD(&alg->cra_users);
  111. ret = -EEXIST;
  112. atomic_set(&alg->cra_refcnt, 1);
  113. list_for_each_entry(q, &crypto_alg_list, cra_list) {
  114. if (q == alg)
  115. goto out;
  116. if (crypto_is_moribund(q))
  117. continue;
  118. if (crypto_is_larval(q)) {
  119. struct crypto_larval *larval = (void *)q;
  120. /*
  121. * Check to see if either our generic name or
  122. * specific name can satisfy the name requested
  123. * by the larval entry q.
  124. */
  125. if (strcmp(alg->cra_name, q->cra_name) &&
  126. strcmp(alg->cra_driver_name, q->cra_name))
  127. continue;
  128. if (larval->adult)
  129. continue;
  130. if ((q->cra_flags ^ alg->cra_flags) & larval->mask)
  131. continue;
  132. if (!crypto_mod_get(alg))
  133. continue;
  134. larval->adult = alg;
  135. complete_all(&larval->completion);
  136. continue;
  137. }
  138. if (strcmp(alg->cra_name, q->cra_name))
  139. continue;
  140. if (strcmp(alg->cra_driver_name, q->cra_driver_name) &&
  141. q->cra_priority > alg->cra_priority)
  142. continue;
  143. crypto_remove_spawns(&q->cra_users, list, alg->cra_flags);
  144. }
  145. list_add(&alg->cra_list, &crypto_alg_list);
  146. crypto_notify(CRYPTO_MSG_ALG_REGISTER, alg);
  147. ret = 0;
  148. out:
  149. return ret;
  150. }
  151. static void crypto_remove_final(struct list_head *list)
  152. {
  153. struct crypto_alg *alg;
  154. struct crypto_alg *n;
  155. list_for_each_entry_safe(alg, n, list, cra_list) {
  156. list_del_init(&alg->cra_list);
  157. crypto_alg_put(alg);
  158. }
  159. }
  160. int crypto_register_alg(struct crypto_alg *alg)
  161. {
  162. LIST_HEAD(list);
  163. int err;
  164. err = crypto_check_alg(alg);
  165. if (err)
  166. return err;
  167. down_write(&crypto_alg_sem);
  168. err = __crypto_register_alg(alg, &list);
  169. up_write(&crypto_alg_sem);
  170. crypto_remove_final(&list);
  171. return err;
  172. }
  173. EXPORT_SYMBOL_GPL(crypto_register_alg);
  174. static int crypto_remove_alg(struct crypto_alg *alg, struct list_head *list)
  175. {
  176. if (unlikely(list_empty(&alg->cra_list)))
  177. return -ENOENT;
  178. alg->cra_flags |= CRYPTO_ALG_DEAD;
  179. crypto_notify(CRYPTO_MSG_ALG_UNREGISTER, alg);
  180. list_del_init(&alg->cra_list);
  181. crypto_remove_spawns(&alg->cra_users, list, alg->cra_flags);
  182. return 0;
  183. }
  184. int crypto_unregister_alg(struct crypto_alg *alg)
  185. {
  186. int ret;
  187. LIST_HEAD(list);
  188. down_write(&crypto_alg_sem);
  189. ret = crypto_remove_alg(alg, &list);
  190. up_write(&crypto_alg_sem);
  191. if (ret)
  192. return ret;
  193. BUG_ON(atomic_read(&alg->cra_refcnt) != 1);
  194. if (alg->cra_destroy)
  195. alg->cra_destroy(alg);
  196. crypto_remove_final(&list);
  197. return 0;
  198. }
  199. EXPORT_SYMBOL_GPL(crypto_unregister_alg);
  200. int crypto_register_template(struct crypto_template *tmpl)
  201. {
  202. struct crypto_template *q;
  203. int err = -EEXIST;
  204. down_write(&crypto_alg_sem);
  205. list_for_each_entry(q, &crypto_template_list, list) {
  206. if (q == tmpl)
  207. goto out;
  208. }
  209. list_add(&tmpl->list, &crypto_template_list);
  210. crypto_notify(CRYPTO_MSG_TMPL_REGISTER, tmpl);
  211. err = 0;
  212. out:
  213. up_write(&crypto_alg_sem);
  214. return err;
  215. }
  216. EXPORT_SYMBOL_GPL(crypto_register_template);
  217. void crypto_unregister_template(struct crypto_template *tmpl)
  218. {
  219. struct crypto_instance *inst;
  220. struct hlist_node *p, *n;
  221. struct hlist_head *list;
  222. LIST_HEAD(users);
  223. down_write(&crypto_alg_sem);
  224. BUG_ON(list_empty(&tmpl->list));
  225. list_del_init(&tmpl->list);
  226. list = &tmpl->instances;
  227. hlist_for_each_entry(inst, p, list, list) {
  228. int err = crypto_remove_alg(&inst->alg, &users);
  229. BUG_ON(err);
  230. }
  231. crypto_notify(CRYPTO_MSG_TMPL_UNREGISTER, tmpl);
  232. up_write(&crypto_alg_sem);
  233. hlist_for_each_entry_safe(inst, p, n, list, list) {
  234. BUG_ON(atomic_read(&inst->alg.cra_refcnt) != 1);
  235. tmpl->free(inst);
  236. }
  237. crypto_remove_final(&users);
  238. }
  239. EXPORT_SYMBOL_GPL(crypto_unregister_template);
  240. static struct crypto_template *__crypto_lookup_template(const char *name)
  241. {
  242. struct crypto_template *q, *tmpl = NULL;
  243. down_read(&crypto_alg_sem);
  244. list_for_each_entry(q, &crypto_template_list, list) {
  245. if (strcmp(q->name, name))
  246. continue;
  247. if (unlikely(!crypto_tmpl_get(q)))
  248. continue;
  249. tmpl = q;
  250. break;
  251. }
  252. up_read(&crypto_alg_sem);
  253. return tmpl;
  254. }
  255. struct crypto_template *crypto_lookup_template(const char *name)
  256. {
  257. return try_then_request_module(__crypto_lookup_template(name), name);
  258. }
  259. EXPORT_SYMBOL_GPL(crypto_lookup_template);
  260. int crypto_register_instance(struct crypto_template *tmpl,
  261. struct crypto_instance *inst)
  262. {
  263. LIST_HEAD(list);
  264. int err = -EINVAL;
  265. err = crypto_check_alg(&inst->alg);
  266. if (err)
  267. goto err;
  268. inst->alg.cra_module = tmpl->module;
  269. down_write(&crypto_alg_sem);
  270. err = __crypto_register_alg(&inst->alg, &list);
  271. if (err)
  272. goto unlock;
  273. hlist_add_head(&inst->list, &tmpl->instances);
  274. inst->tmpl = tmpl;
  275. unlock:
  276. up_write(&crypto_alg_sem);
  277. crypto_remove_final(&list);
  278. err:
  279. return err;
  280. }
  281. EXPORT_SYMBOL_GPL(crypto_register_instance);
  282. int crypto_init_spawn(struct crypto_spawn *spawn, struct crypto_alg *alg,
  283. struct crypto_instance *inst, u32 mask)
  284. {
  285. int err = -EAGAIN;
  286. spawn->inst = inst;
  287. spawn->mask = mask;
  288. down_write(&crypto_alg_sem);
  289. if (!crypto_is_moribund(alg)) {
  290. list_add(&spawn->list, &alg->cra_users);
  291. spawn->alg = alg;
  292. err = 0;
  293. }
  294. up_write(&crypto_alg_sem);
  295. return err;
  296. }
  297. EXPORT_SYMBOL_GPL(crypto_init_spawn);
  298. void crypto_drop_spawn(struct crypto_spawn *spawn)
  299. {
  300. down_write(&crypto_alg_sem);
  301. list_del(&spawn->list);
  302. up_write(&crypto_alg_sem);
  303. }
  304. EXPORT_SYMBOL_GPL(crypto_drop_spawn);
  305. struct crypto_tfm *crypto_spawn_tfm(struct crypto_spawn *spawn, u32 type,
  306. u32 mask)
  307. {
  308. struct crypto_alg *alg;
  309. struct crypto_alg *alg2;
  310. struct crypto_tfm *tfm;
  311. down_read(&crypto_alg_sem);
  312. alg = spawn->alg;
  313. alg2 = alg;
  314. if (alg2)
  315. alg2 = crypto_mod_get(alg2);
  316. up_read(&crypto_alg_sem);
  317. if (!alg2) {
  318. if (alg)
  319. crypto_shoot_alg(alg);
  320. return ERR_PTR(-EAGAIN);
  321. }
  322. tfm = ERR_PTR(-EINVAL);
  323. if (unlikely((alg->cra_flags ^ type) & mask))
  324. goto out_put_alg;
  325. tfm = __crypto_alloc_tfm(alg, type, mask);
  326. if (IS_ERR(tfm))
  327. goto out_put_alg;
  328. return tfm;
  329. out_put_alg:
  330. crypto_mod_put(alg);
  331. return tfm;
  332. }
  333. EXPORT_SYMBOL_GPL(crypto_spawn_tfm);
  334. int crypto_register_notifier(struct notifier_block *nb)
  335. {
  336. return blocking_notifier_chain_register(&crypto_chain, nb);
  337. }
  338. EXPORT_SYMBOL_GPL(crypto_register_notifier);
  339. int crypto_unregister_notifier(struct notifier_block *nb)
  340. {
  341. return blocking_notifier_chain_unregister(&crypto_chain, nb);
  342. }
  343. EXPORT_SYMBOL_GPL(crypto_unregister_notifier);
  344. struct crypto_attr_type *crypto_get_attr_type(struct rtattr **tb)
  345. {
  346. struct rtattr *rta = tb[0];
  347. struct crypto_attr_type *algt;
  348. if (!rta)
  349. return ERR_PTR(-ENOENT);
  350. if (RTA_PAYLOAD(rta) < sizeof(*algt))
  351. return ERR_PTR(-EINVAL);
  352. if (rta->rta_type != CRYPTOA_TYPE)
  353. return ERR_PTR(-EINVAL);
  354. algt = RTA_DATA(rta);
  355. return algt;
  356. }
  357. EXPORT_SYMBOL_GPL(crypto_get_attr_type);
  358. int crypto_check_attr_type(struct rtattr **tb, u32 type)
  359. {
  360. struct crypto_attr_type *algt;
  361. algt = crypto_get_attr_type(tb);
  362. if (IS_ERR(algt))
  363. return PTR_ERR(algt);
  364. if ((algt->type ^ type) & algt->mask)
  365. return -EINVAL;
  366. return 0;
  367. }
  368. EXPORT_SYMBOL_GPL(crypto_check_attr_type);
  369. const char *crypto_attr_alg_name(struct rtattr *rta)
  370. {
  371. struct crypto_attr_alg *alga;
  372. if (!rta)
  373. return ERR_PTR(-ENOENT);
  374. if (RTA_PAYLOAD(rta) < sizeof(*alga))
  375. return ERR_PTR(-EINVAL);
  376. if (rta->rta_type != CRYPTOA_ALG)
  377. return ERR_PTR(-EINVAL);
  378. alga = RTA_DATA(rta);
  379. alga->name[CRYPTO_MAX_ALG_NAME - 1] = 0;
  380. return alga->name;
  381. }
  382. EXPORT_SYMBOL_GPL(crypto_attr_alg_name);
  383. struct crypto_alg *crypto_attr_alg(struct rtattr *rta, u32 type, u32 mask)
  384. {
  385. const char *name;
  386. int err;
  387. name = crypto_attr_alg_name(rta);
  388. err = PTR_ERR(name);
  389. if (IS_ERR(name))
  390. return ERR_PTR(err);
  391. return crypto_alg_mod_lookup(name, type, mask);
  392. }
  393. EXPORT_SYMBOL_GPL(crypto_attr_alg);
  394. int crypto_attr_u32(struct rtattr *rta, u32 *num)
  395. {
  396. struct crypto_attr_u32 *nu32;
  397. if (!rta)
  398. return -ENOENT;
  399. if (RTA_PAYLOAD(rta) < sizeof(*nu32))
  400. return -EINVAL;
  401. if (rta->rta_type != CRYPTOA_U32)
  402. return -EINVAL;
  403. nu32 = RTA_DATA(rta);
  404. *num = nu32->num;
  405. return 0;
  406. }
  407. EXPORT_SYMBOL_GPL(crypto_attr_u32);
  408. struct crypto_instance *crypto_alloc_instance(const char *name,
  409. struct crypto_alg *alg)
  410. {
  411. struct crypto_instance *inst;
  412. struct crypto_spawn *spawn;
  413. int err;
  414. inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
  415. if (!inst)
  416. return ERR_PTR(-ENOMEM);
  417. err = -ENAMETOOLONG;
  418. if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME, "%s(%s)", name,
  419. alg->cra_name) >= CRYPTO_MAX_ALG_NAME)
  420. goto err_free_inst;
  421. if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s(%s)",
  422. name, alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
  423. goto err_free_inst;
  424. spawn = crypto_instance_ctx(inst);
  425. err = crypto_init_spawn(spawn, alg, inst,
  426. CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC);
  427. if (err)
  428. goto err_free_inst;
  429. return inst;
  430. err_free_inst:
  431. kfree(inst);
  432. return ERR_PTR(err);
  433. }
  434. EXPORT_SYMBOL_GPL(crypto_alloc_instance);
  435. void crypto_init_queue(struct crypto_queue *queue, unsigned int max_qlen)
  436. {
  437. INIT_LIST_HEAD(&queue->list);
  438. queue->backlog = &queue->list;
  439. queue->qlen = 0;
  440. queue->max_qlen = max_qlen;
  441. }
  442. EXPORT_SYMBOL_GPL(crypto_init_queue);
  443. int crypto_enqueue_request(struct crypto_queue *queue,
  444. struct crypto_async_request *request)
  445. {
  446. int err = -EINPROGRESS;
  447. if (unlikely(queue->qlen >= queue->max_qlen)) {
  448. err = -EBUSY;
  449. if (!(request->flags & CRYPTO_TFM_REQ_MAY_BACKLOG))
  450. goto out;
  451. if (queue->backlog == &queue->list)
  452. queue->backlog = &request->list;
  453. }
  454. queue->qlen++;
  455. list_add_tail(&request->list, &queue->list);
  456. out:
  457. return err;
  458. }
  459. EXPORT_SYMBOL_GPL(crypto_enqueue_request);
  460. struct crypto_async_request *crypto_dequeue_request(struct crypto_queue *queue)
  461. {
  462. struct list_head *request;
  463. if (unlikely(!queue->qlen))
  464. return NULL;
  465. queue->qlen--;
  466. if (queue->backlog != &queue->list)
  467. queue->backlog = queue->backlog->next;
  468. request = queue->list.next;
  469. list_del(request);
  470. return list_entry(request, struct crypto_async_request, list);
  471. }
  472. EXPORT_SYMBOL_GPL(crypto_dequeue_request);
  473. int crypto_tfm_in_queue(struct crypto_queue *queue, struct crypto_tfm *tfm)
  474. {
  475. struct crypto_async_request *req;
  476. list_for_each_entry(req, &queue->list, list) {
  477. if (req->tfm == tfm)
  478. return 1;
  479. }
  480. return 0;
  481. }
  482. EXPORT_SYMBOL_GPL(crypto_tfm_in_queue);
  483. static inline void crypto_inc_byte(u8 *a, unsigned int size)
  484. {
  485. u8 *b = (a + size);
  486. u8 c;
  487. for (; size; size--) {
  488. c = *--b + 1;
  489. *b = c;
  490. if (c)
  491. break;
  492. }
  493. }
  494. void crypto_inc(u8 *a, unsigned int size)
  495. {
  496. __be32 *b = (__be32 *)(a + size);
  497. u32 c;
  498. for (; size >= 4; size -= 4) {
  499. c = be32_to_cpu(*--b) + 1;
  500. *b = cpu_to_be32(c);
  501. if (c)
  502. return;
  503. }
  504. crypto_inc_byte(a, size);
  505. }
  506. EXPORT_SYMBOL_GPL(crypto_inc);
  507. static inline void crypto_xor_byte(u8 *a, const u8 *b, unsigned int size)
  508. {
  509. for (; size; size--)
  510. *a++ ^= *b++;
  511. }
  512. void crypto_xor(u8 *dst, const u8 *src, unsigned int size)
  513. {
  514. u32 *a = (u32 *)dst;
  515. u32 *b = (u32 *)src;
  516. for (; size >= 4; size -= 4)
  517. *a++ ^= *b++;
  518. crypto_xor_byte((u8 *)a, (u8 *)b, size);
  519. }
  520. EXPORT_SYMBOL_GPL(crypto_xor);
  521. static int __init crypto_algapi_init(void)
  522. {
  523. crypto_init_proc();
  524. return 0;
  525. }
  526. static void __exit crypto_algapi_exit(void)
  527. {
  528. crypto_exit_proc();
  529. }
  530. module_init(crypto_algapi_init);
  531. module_exit(crypto_algapi_exit);
  532. MODULE_LICENSE("GPL");
  533. MODULE_DESCRIPTION("Cryptographic algorithms API");