atmel-ecc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Microchip / Atmel ECC (I2C) driver.
  4. *
  5. * Copyright (c) 2017, Microchip Technology Inc.
  6. * Author: Tudor Ambarus <[email protected]>
  7. */
  8. #include <linux/delay.h>
  9. #include <linux/device.h>
  10. #include <linux/err.h>
  11. #include <linux/errno.h>
  12. #include <linux/i2c.h>
  13. #include <linux/init.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/of_device.h>
  17. #include <linux/scatterlist.h>
  18. #include <linux/slab.h>
  19. #include <linux/workqueue.h>
  20. #include <crypto/internal/kpp.h>
  21. #include <crypto/ecdh.h>
  22. #include <crypto/kpp.h>
  23. #include "atmel-i2c.h"
  24. static struct atmel_ecc_driver_data driver_data;
  25. /**
  26. * struct atmel_ecdh_ctx - transformation context
  27. * @client : pointer to i2c client device
  28. * @fallback : used for unsupported curves or when user wants to use its own
  29. * private key.
  30. * @public_key : generated when calling set_secret(). It's the responsibility
  31. * of the user to not call set_secret() while
  32. * generate_public_key() or compute_shared_secret() are in flight.
  33. * @curve_id : elliptic curve id
  34. * @do_fallback: true when the device doesn't support the curve or when the user
  35. * wants to use its own private key.
  36. */
  37. struct atmel_ecdh_ctx {
  38. struct i2c_client *client;
  39. struct crypto_kpp *fallback;
  40. const u8 *public_key;
  41. unsigned int curve_id;
  42. bool do_fallback;
  43. };
  44. static void atmel_ecdh_done(struct atmel_i2c_work_data *work_data, void *areq,
  45. int status)
  46. {
  47. struct kpp_request *req = areq;
  48. struct atmel_i2c_cmd *cmd = &work_data->cmd;
  49. size_t copied, n_sz;
  50. if (status)
  51. goto free_work_data;
  52. /* might want less than we've got */
  53. n_sz = min_t(size_t, ATMEL_ECC_NIST_P256_N_SIZE, req->dst_len);
  54. /* copy the shared secret */
  55. copied = sg_copy_from_buffer(req->dst, sg_nents_for_len(req->dst, n_sz),
  56. &cmd->data[RSP_DATA_IDX], n_sz);
  57. if (copied != n_sz)
  58. status = -EINVAL;
  59. /* fall through */
  60. free_work_data:
  61. kfree_sensitive(work_data);
  62. kpp_request_complete(req, status);
  63. }
  64. /*
  65. * A random private key is generated and stored in the device. The device
  66. * returns the pair public key.
  67. */
  68. static int atmel_ecdh_set_secret(struct crypto_kpp *tfm, const void *buf,
  69. unsigned int len)
  70. {
  71. struct atmel_ecdh_ctx *ctx = kpp_tfm_ctx(tfm);
  72. struct atmel_i2c_cmd *cmd;
  73. void *public_key;
  74. struct ecdh params;
  75. int ret = -ENOMEM;
  76. /* free the old public key, if any */
  77. kfree(ctx->public_key);
  78. /* make sure you don't free the old public key twice */
  79. ctx->public_key = NULL;
  80. if (crypto_ecdh_decode_key(buf, len, &params) < 0) {
  81. dev_err(&ctx->client->dev, "crypto_ecdh_decode_key failed\n");
  82. return -EINVAL;
  83. }
  84. if (params.key_size) {
  85. /* fallback to ecdh software implementation */
  86. ctx->do_fallback = true;
  87. return crypto_kpp_set_secret(ctx->fallback, buf, len);
  88. }
  89. cmd = kmalloc(sizeof(*cmd), GFP_KERNEL);
  90. if (!cmd)
  91. return -ENOMEM;
  92. /*
  93. * The device only supports NIST P256 ECC keys. The public key size will
  94. * always be the same. Use a macro for the key size to avoid unnecessary
  95. * computations.
  96. */
  97. public_key = kmalloc(ATMEL_ECC_PUBKEY_SIZE, GFP_KERNEL);
  98. if (!public_key)
  99. goto free_cmd;
  100. ctx->do_fallback = false;
  101. atmel_i2c_init_genkey_cmd(cmd, DATA_SLOT_2);
  102. ret = atmel_i2c_send_receive(ctx->client, cmd);
  103. if (ret)
  104. goto free_public_key;
  105. /* save the public key */
  106. memcpy(public_key, &cmd->data[RSP_DATA_IDX], ATMEL_ECC_PUBKEY_SIZE);
  107. ctx->public_key = public_key;
  108. kfree(cmd);
  109. return 0;
  110. free_public_key:
  111. kfree(public_key);
  112. free_cmd:
  113. kfree(cmd);
  114. return ret;
  115. }
  116. static int atmel_ecdh_generate_public_key(struct kpp_request *req)
  117. {
  118. struct crypto_kpp *tfm = crypto_kpp_reqtfm(req);
  119. struct atmel_ecdh_ctx *ctx = kpp_tfm_ctx(tfm);
  120. size_t copied, nbytes;
  121. int ret = 0;
  122. if (ctx->do_fallback) {
  123. kpp_request_set_tfm(req, ctx->fallback);
  124. return crypto_kpp_generate_public_key(req);
  125. }
  126. if (!ctx->public_key)
  127. return -EINVAL;
  128. /* might want less than we've got */
  129. nbytes = min_t(size_t, ATMEL_ECC_PUBKEY_SIZE, req->dst_len);
  130. /* public key was saved at private key generation */
  131. copied = sg_copy_from_buffer(req->dst,
  132. sg_nents_for_len(req->dst, nbytes),
  133. ctx->public_key, nbytes);
  134. if (copied != nbytes)
  135. ret = -EINVAL;
  136. return ret;
  137. }
  138. static int atmel_ecdh_compute_shared_secret(struct kpp_request *req)
  139. {
  140. struct crypto_kpp *tfm = crypto_kpp_reqtfm(req);
  141. struct atmel_ecdh_ctx *ctx = kpp_tfm_ctx(tfm);
  142. struct atmel_i2c_work_data *work_data;
  143. gfp_t gfp;
  144. int ret;
  145. if (ctx->do_fallback) {
  146. kpp_request_set_tfm(req, ctx->fallback);
  147. return crypto_kpp_compute_shared_secret(req);
  148. }
  149. /* must have exactly two points to be on the curve */
  150. if (req->src_len != ATMEL_ECC_PUBKEY_SIZE)
  151. return -EINVAL;
  152. gfp = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ? GFP_KERNEL :
  153. GFP_ATOMIC;
  154. work_data = kmalloc(sizeof(*work_data), gfp);
  155. if (!work_data)
  156. return -ENOMEM;
  157. work_data->ctx = ctx;
  158. work_data->client = ctx->client;
  159. ret = atmel_i2c_init_ecdh_cmd(&work_data->cmd, req->src);
  160. if (ret)
  161. goto free_work_data;
  162. atmel_i2c_enqueue(work_data, atmel_ecdh_done, req);
  163. return -EINPROGRESS;
  164. free_work_data:
  165. kfree(work_data);
  166. return ret;
  167. }
  168. static struct i2c_client *atmel_ecc_i2c_client_alloc(void)
  169. {
  170. struct atmel_i2c_client_priv *i2c_priv, *min_i2c_priv = NULL;
  171. struct i2c_client *client = ERR_PTR(-ENODEV);
  172. int min_tfm_cnt = INT_MAX;
  173. int tfm_cnt;
  174. spin_lock(&driver_data.i2c_list_lock);
  175. if (list_empty(&driver_data.i2c_client_list)) {
  176. spin_unlock(&driver_data.i2c_list_lock);
  177. return ERR_PTR(-ENODEV);
  178. }
  179. list_for_each_entry(i2c_priv, &driver_data.i2c_client_list,
  180. i2c_client_list_node) {
  181. tfm_cnt = atomic_read(&i2c_priv->tfm_count);
  182. if (tfm_cnt < min_tfm_cnt) {
  183. min_tfm_cnt = tfm_cnt;
  184. min_i2c_priv = i2c_priv;
  185. }
  186. if (!min_tfm_cnt)
  187. break;
  188. }
  189. if (min_i2c_priv) {
  190. atomic_inc(&min_i2c_priv->tfm_count);
  191. client = min_i2c_priv->client;
  192. }
  193. spin_unlock(&driver_data.i2c_list_lock);
  194. return client;
  195. }
  196. static void atmel_ecc_i2c_client_free(struct i2c_client *client)
  197. {
  198. struct atmel_i2c_client_priv *i2c_priv = i2c_get_clientdata(client);
  199. atomic_dec(&i2c_priv->tfm_count);
  200. }
  201. static int atmel_ecdh_init_tfm(struct crypto_kpp *tfm)
  202. {
  203. const char *alg = kpp_alg_name(tfm);
  204. struct crypto_kpp *fallback;
  205. struct atmel_ecdh_ctx *ctx = kpp_tfm_ctx(tfm);
  206. ctx->curve_id = ECC_CURVE_NIST_P256;
  207. ctx->client = atmel_ecc_i2c_client_alloc();
  208. if (IS_ERR(ctx->client)) {
  209. pr_err("tfm - i2c_client binding failed\n");
  210. return PTR_ERR(ctx->client);
  211. }
  212. fallback = crypto_alloc_kpp(alg, 0, CRYPTO_ALG_NEED_FALLBACK);
  213. if (IS_ERR(fallback)) {
  214. dev_err(&ctx->client->dev, "Failed to allocate transformation for '%s': %ld\n",
  215. alg, PTR_ERR(fallback));
  216. return PTR_ERR(fallback);
  217. }
  218. crypto_kpp_set_flags(fallback, crypto_kpp_get_flags(tfm));
  219. ctx->fallback = fallback;
  220. return 0;
  221. }
  222. static void atmel_ecdh_exit_tfm(struct crypto_kpp *tfm)
  223. {
  224. struct atmel_ecdh_ctx *ctx = kpp_tfm_ctx(tfm);
  225. kfree(ctx->public_key);
  226. crypto_free_kpp(ctx->fallback);
  227. atmel_ecc_i2c_client_free(ctx->client);
  228. }
  229. static unsigned int atmel_ecdh_max_size(struct crypto_kpp *tfm)
  230. {
  231. struct atmel_ecdh_ctx *ctx = kpp_tfm_ctx(tfm);
  232. if (ctx->fallback)
  233. return crypto_kpp_maxsize(ctx->fallback);
  234. /*
  235. * The device only supports NIST P256 ECC keys. The public key size will
  236. * always be the same. Use a macro for the key size to avoid unnecessary
  237. * computations.
  238. */
  239. return ATMEL_ECC_PUBKEY_SIZE;
  240. }
  241. static struct kpp_alg atmel_ecdh_nist_p256 = {
  242. .set_secret = atmel_ecdh_set_secret,
  243. .generate_public_key = atmel_ecdh_generate_public_key,
  244. .compute_shared_secret = atmel_ecdh_compute_shared_secret,
  245. .init = atmel_ecdh_init_tfm,
  246. .exit = atmel_ecdh_exit_tfm,
  247. .max_size = atmel_ecdh_max_size,
  248. .base = {
  249. .cra_flags = CRYPTO_ALG_NEED_FALLBACK,
  250. .cra_name = "ecdh-nist-p256",
  251. .cra_driver_name = "atmel-ecdh",
  252. .cra_priority = ATMEL_ECC_PRIORITY,
  253. .cra_module = THIS_MODULE,
  254. .cra_ctxsize = sizeof(struct atmel_ecdh_ctx),
  255. },
  256. };
  257. static int atmel_ecc_probe(struct i2c_client *client,
  258. const struct i2c_device_id *id)
  259. {
  260. struct atmel_i2c_client_priv *i2c_priv;
  261. int ret;
  262. ret = atmel_i2c_probe(client, id);
  263. if (ret)
  264. return ret;
  265. i2c_priv = i2c_get_clientdata(client);
  266. spin_lock(&driver_data.i2c_list_lock);
  267. list_add_tail(&i2c_priv->i2c_client_list_node,
  268. &driver_data.i2c_client_list);
  269. spin_unlock(&driver_data.i2c_list_lock);
  270. ret = crypto_register_kpp(&atmel_ecdh_nist_p256);
  271. if (ret) {
  272. spin_lock(&driver_data.i2c_list_lock);
  273. list_del(&i2c_priv->i2c_client_list_node);
  274. spin_unlock(&driver_data.i2c_list_lock);
  275. dev_err(&client->dev, "%s alg registration failed\n",
  276. atmel_ecdh_nist_p256.base.cra_driver_name);
  277. } else {
  278. dev_info(&client->dev, "atmel ecc algorithms registered in /proc/crypto\n");
  279. }
  280. return ret;
  281. }
  282. static void atmel_ecc_remove(struct i2c_client *client)
  283. {
  284. struct atmel_i2c_client_priv *i2c_priv = i2c_get_clientdata(client);
  285. /* Return EBUSY if i2c client already allocated. */
  286. if (atomic_read(&i2c_priv->tfm_count)) {
  287. /*
  288. * After we return here, the memory backing the device is freed.
  289. * That happens no matter what the return value of this function
  290. * is because in the Linux device model there is no error
  291. * handling for unbinding a driver.
  292. * If there is still some action pending, it probably involves
  293. * accessing the freed memory.
  294. */
  295. dev_emerg(&client->dev, "Device is busy, expect memory corruption.\n");
  296. return;
  297. }
  298. crypto_unregister_kpp(&atmel_ecdh_nist_p256);
  299. spin_lock(&driver_data.i2c_list_lock);
  300. list_del(&i2c_priv->i2c_client_list_node);
  301. spin_unlock(&driver_data.i2c_list_lock);
  302. }
  303. #ifdef CONFIG_OF
  304. static const struct of_device_id atmel_ecc_dt_ids[] = {
  305. {
  306. .compatible = "atmel,atecc508a",
  307. }, {
  308. /* sentinel */
  309. }
  310. };
  311. MODULE_DEVICE_TABLE(of, atmel_ecc_dt_ids);
  312. #endif
  313. static const struct i2c_device_id atmel_ecc_id[] = {
  314. { "atecc508a", 0 },
  315. { }
  316. };
  317. MODULE_DEVICE_TABLE(i2c, atmel_ecc_id);
  318. static struct i2c_driver atmel_ecc_driver = {
  319. .driver = {
  320. .name = "atmel-ecc",
  321. .of_match_table = of_match_ptr(atmel_ecc_dt_ids),
  322. },
  323. .probe = atmel_ecc_probe,
  324. .remove = atmel_ecc_remove,
  325. .id_table = atmel_ecc_id,
  326. };
  327. static int __init atmel_ecc_init(void)
  328. {
  329. spin_lock_init(&driver_data.i2c_list_lock);
  330. INIT_LIST_HEAD(&driver_data.i2c_client_list);
  331. return i2c_add_driver(&atmel_ecc_driver);
  332. }
  333. static void __exit atmel_ecc_exit(void)
  334. {
  335. atmel_i2c_flush_queue();
  336. i2c_del_driver(&atmel_ecc_driver);
  337. }
  338. module_init(atmel_ecc_init);
  339. module_exit(atmel_ecc_exit);
  340. MODULE_AUTHOR("Tudor Ambarus <[email protected]>");
  341. MODULE_DESCRIPTION("Microchip / Atmel ECC (I2C) driver");
  342. MODULE_LICENSE("GPL v2");