atmel-ecc.c 10 KB

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