atmel-ecc.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  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/bitrev.h>
  9. #include <linux/crc16.h>
  10. #include <linux/delay.h>
  11. #include <linux/device.h>
  12. #include <linux/err.h>
  13. #include <linux/errno.h>
  14. #include <linux/i2c.h>
  15. #include <linux/init.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/of_device.h>
  19. #include <linux/scatterlist.h>
  20. #include <linux/slab.h>
  21. #include <linux/workqueue.h>
  22. #include <crypto/internal/kpp.h>
  23. #include <crypto/ecdh.h>
  24. #include <crypto/kpp.h>
  25. #include "atmel-ecc.h"
  26. /* Used for binding tfm objects to i2c clients. */
  27. struct atmel_ecc_driver_data {
  28. struct list_head i2c_client_list;
  29. spinlock_t i2c_list_lock;
  30. } ____cacheline_aligned;
  31. static struct atmel_ecc_driver_data driver_data;
  32. /**
  33. * atmel_ecc_i2c_client_priv - i2c_client private data
  34. * @client : pointer to i2c client device
  35. * @i2c_client_list_node: part of i2c_client_list
  36. * @lock : lock for sending i2c commands
  37. * @wake_token : wake token array of zeros
  38. * @wake_token_sz : size in bytes of the wake_token
  39. * @tfm_count : number of active crypto transformations on i2c client
  40. *
  41. * Reads and writes from/to the i2c client are sequential. The first byte
  42. * transmitted to the device is treated as the byte size. Any attempt to send
  43. * more than this number of bytes will cause the device to not ACK those bytes.
  44. * After the host writes a single command byte to the input buffer, reads are
  45. * prohibited until after the device completes command execution. Use a mutex
  46. * when sending i2c commands.
  47. */
  48. struct atmel_ecc_i2c_client_priv {
  49. struct i2c_client *client;
  50. struct list_head i2c_client_list_node;
  51. struct mutex lock;
  52. u8 wake_token[WAKE_TOKEN_MAX_SIZE];
  53. size_t wake_token_sz;
  54. atomic_t tfm_count ____cacheline_aligned;
  55. };
  56. /**
  57. * atmel_ecdh_ctx - transformation context
  58. * @client : pointer to i2c client device
  59. * @fallback : used for unsupported curves or when user wants to use its own
  60. * private key.
  61. * @public_key : generated when calling set_secret(). It's the responsibility
  62. * of the user to not call set_secret() while
  63. * generate_public_key() or compute_shared_secret() are in flight.
  64. * @curve_id : elliptic curve id
  65. * @n_sz : size in bytes of the n prime
  66. * @do_fallback: true when the device doesn't support the curve or when the user
  67. * wants to use its own private key.
  68. */
  69. struct atmel_ecdh_ctx {
  70. struct i2c_client *client;
  71. struct crypto_kpp *fallback;
  72. const u8 *public_key;
  73. unsigned int curve_id;
  74. size_t n_sz;
  75. bool do_fallback;
  76. };
  77. /**
  78. * atmel_ecc_work_data - data structure representing the work
  79. * @ctx : transformation context.
  80. * @cbk : pointer to a callback function to be invoked upon completion of this
  81. * request. This has the form:
  82. * callback(struct atmel_ecc_work_data *work_data, void *areq, u8 status)
  83. * where:
  84. * @work_data: data structure representing the work
  85. * @areq : optional pointer to an argument passed with the original
  86. * request.
  87. * @status : status returned from the i2c client device or i2c error.
  88. * @areq: optional pointer to a user argument for use at callback time.
  89. * @work: describes the task to be executed.
  90. * @cmd : structure used for communicating with the device.
  91. */
  92. struct atmel_ecc_work_data {
  93. struct atmel_ecdh_ctx *ctx;
  94. void (*cbk)(struct atmel_ecc_work_data *work_data, void *areq,
  95. int status);
  96. void *areq;
  97. struct work_struct work;
  98. struct atmel_ecc_cmd cmd;
  99. };
  100. static u16 atmel_ecc_crc16(u16 crc, const u8 *buffer, size_t len)
  101. {
  102. return cpu_to_le16(bitrev16(crc16(crc, buffer, len)));
  103. }
  104. /**
  105. * atmel_ecc_checksum() - Generate 16-bit CRC as required by ATMEL ECC.
  106. * CRC16 verification of the count, opcode, param1, param2 and data bytes.
  107. * The checksum is saved in little-endian format in the least significant
  108. * two bytes of the command. CRC polynomial is 0x8005 and the initial register
  109. * value should be zero.
  110. *
  111. * @cmd : structure used for communicating with the device.
  112. */
  113. static void atmel_ecc_checksum(struct atmel_ecc_cmd *cmd)
  114. {
  115. u8 *data = &cmd->count;
  116. size_t len = cmd->count - CRC_SIZE;
  117. u16 *crc16 = (u16 *)(data + len);
  118. *crc16 = atmel_ecc_crc16(0, data, len);
  119. }
  120. static void atmel_ecc_init_read_cmd(struct atmel_ecc_cmd *cmd)
  121. {
  122. cmd->word_addr = COMMAND;
  123. cmd->opcode = OPCODE_READ;
  124. /*
  125. * Read the word from Configuration zone that contains the lock bytes
  126. * (UserExtra, Selector, LockValue, LockConfig).
  127. */
  128. cmd->param1 = CONFIG_ZONE;
  129. cmd->param2 = DEVICE_LOCK_ADDR;
  130. cmd->count = READ_COUNT;
  131. atmel_ecc_checksum(cmd);
  132. cmd->msecs = MAX_EXEC_TIME_READ;
  133. cmd->rxsize = READ_RSP_SIZE;
  134. }
  135. static void atmel_ecc_init_genkey_cmd(struct atmel_ecc_cmd *cmd, u16 keyid)
  136. {
  137. cmd->word_addr = COMMAND;
  138. cmd->count = GENKEY_COUNT;
  139. cmd->opcode = OPCODE_GENKEY;
  140. cmd->param1 = GENKEY_MODE_PRIVATE;
  141. /* a random private key will be generated and stored in slot keyID */
  142. cmd->param2 = cpu_to_le16(keyid);
  143. atmel_ecc_checksum(cmd);
  144. cmd->msecs = MAX_EXEC_TIME_GENKEY;
  145. cmd->rxsize = GENKEY_RSP_SIZE;
  146. }
  147. static int atmel_ecc_init_ecdh_cmd(struct atmel_ecc_cmd *cmd,
  148. struct scatterlist *pubkey)
  149. {
  150. size_t copied;
  151. cmd->word_addr = COMMAND;
  152. cmd->count = ECDH_COUNT;
  153. cmd->opcode = OPCODE_ECDH;
  154. cmd->param1 = ECDH_PREFIX_MODE;
  155. /* private key slot */
  156. cmd->param2 = cpu_to_le16(DATA_SLOT_2);
  157. /*
  158. * The device only supports NIST P256 ECC keys. The public key size will
  159. * always be the same. Use a macro for the key size to avoid unnecessary
  160. * computations.
  161. */
  162. copied = sg_copy_to_buffer(pubkey,
  163. sg_nents_for_len(pubkey,
  164. ATMEL_ECC_PUBKEY_SIZE),
  165. cmd->data, ATMEL_ECC_PUBKEY_SIZE);
  166. if (copied != ATMEL_ECC_PUBKEY_SIZE)
  167. return -EINVAL;
  168. atmel_ecc_checksum(cmd);
  169. cmd->msecs = MAX_EXEC_TIME_ECDH;
  170. cmd->rxsize = ECDH_RSP_SIZE;
  171. return 0;
  172. }
  173. /*
  174. * After wake and after execution of a command, there will be error, status, or
  175. * result bytes in the device's output register that can be retrieved by the
  176. * system. When the length of that group is four bytes, the codes returned are
  177. * detailed in error_list.
  178. */
  179. static int atmel_ecc_status(struct device *dev, u8 *status)
  180. {
  181. size_t err_list_len = ARRAY_SIZE(error_list);
  182. int i;
  183. u8 err_id = status[1];
  184. if (*status != STATUS_SIZE)
  185. return 0;
  186. if (err_id == STATUS_WAKE_SUCCESSFUL || err_id == STATUS_NOERR)
  187. return 0;
  188. for (i = 0; i < err_list_len; i++)
  189. if (error_list[i].value == err_id)
  190. break;
  191. /* if err_id is not in the error_list then ignore it */
  192. if (i != err_list_len) {
  193. dev_err(dev, "%02x: %s:\n", err_id, error_list[i].error_text);
  194. return err_id;
  195. }
  196. return 0;
  197. }
  198. static int atmel_ecc_wakeup(struct i2c_client *client)
  199. {
  200. struct atmel_ecc_i2c_client_priv *i2c_priv = i2c_get_clientdata(client);
  201. u8 status[STATUS_RSP_SIZE];
  202. int ret;
  203. /*
  204. * The device ignores any levels or transitions on the SCL pin when the
  205. * device is idle, asleep or during waking up. Don't check for error
  206. * when waking up the device.
  207. */
  208. i2c_master_send(client, i2c_priv->wake_token, i2c_priv->wake_token_sz);
  209. /*
  210. * Wait to wake the device. Typical execution times for ecdh and genkey
  211. * are around tens of milliseconds. Delta is chosen to 50 microseconds.
  212. */
  213. usleep_range(TWHI_MIN, TWHI_MAX);
  214. ret = i2c_master_recv(client, status, STATUS_SIZE);
  215. if (ret < 0)
  216. return ret;
  217. return atmel_ecc_status(&client->dev, status);
  218. }
  219. static int atmel_ecc_sleep(struct i2c_client *client)
  220. {
  221. u8 sleep = SLEEP_TOKEN;
  222. return i2c_master_send(client, &sleep, 1);
  223. }
  224. static void atmel_ecdh_done(struct atmel_ecc_work_data *work_data, void *areq,
  225. int status)
  226. {
  227. struct kpp_request *req = areq;
  228. struct atmel_ecdh_ctx *ctx = work_data->ctx;
  229. struct atmel_ecc_cmd *cmd = &work_data->cmd;
  230. size_t copied, n_sz;
  231. if (status)
  232. goto free_work_data;
  233. /* might want less than we've got */
  234. n_sz = min_t(size_t, ctx->n_sz, req->dst_len);
  235. /* copy the shared secret */
  236. copied = sg_copy_from_buffer(req->dst, sg_nents_for_len(req->dst, n_sz),
  237. &cmd->data[RSP_DATA_IDX], n_sz);
  238. if (copied != n_sz)
  239. status = -EINVAL;
  240. /* fall through */
  241. free_work_data:
  242. kzfree(work_data);
  243. kpp_request_complete(req, status);
  244. }
  245. /*
  246. * atmel_ecc_send_receive() - send a command to the device and receive its
  247. * response.
  248. * @client: i2c client device
  249. * @cmd : structure used to communicate with the device
  250. *
  251. * After the device receives a Wake token, a watchdog counter starts within the
  252. * device. After the watchdog timer expires, the device enters sleep mode
  253. * regardless of whether some I/O transmission or command execution is in
  254. * progress. If a command is attempted when insufficient time remains prior to
  255. * watchdog timer execution, the device will return the watchdog timeout error
  256. * code without attempting to execute the command. There is no way to reset the
  257. * counter other than to put the device into sleep or idle mode and then
  258. * wake it up again.
  259. */
  260. static int atmel_ecc_send_receive(struct i2c_client *client,
  261. struct atmel_ecc_cmd *cmd)
  262. {
  263. struct atmel_ecc_i2c_client_priv *i2c_priv = i2c_get_clientdata(client);
  264. int ret;
  265. mutex_lock(&i2c_priv->lock);
  266. ret = atmel_ecc_wakeup(client);
  267. if (ret)
  268. goto err;
  269. /* send the command */
  270. ret = i2c_master_send(client, (u8 *)cmd, cmd->count + WORD_ADDR_SIZE);
  271. if (ret < 0)
  272. goto err;
  273. /* delay the appropriate amount of time for command to execute */
  274. msleep(cmd->msecs);
  275. /* receive the response */
  276. ret = i2c_master_recv(client, cmd->data, cmd->rxsize);
  277. if (ret < 0)
  278. goto err;
  279. /* put the device into low-power mode */
  280. ret = atmel_ecc_sleep(client);
  281. if (ret < 0)
  282. goto err;
  283. mutex_unlock(&i2c_priv->lock);
  284. return atmel_ecc_status(&client->dev, cmd->data);
  285. err:
  286. mutex_unlock(&i2c_priv->lock);
  287. return ret;
  288. }
  289. static void atmel_ecc_work_handler(struct work_struct *work)
  290. {
  291. struct atmel_ecc_work_data *work_data =
  292. container_of(work, struct atmel_ecc_work_data, work);
  293. struct atmel_ecc_cmd *cmd = &work_data->cmd;
  294. struct i2c_client *client = work_data->ctx->client;
  295. int status;
  296. status = atmel_ecc_send_receive(client, cmd);
  297. work_data->cbk(work_data, work_data->areq, status);
  298. }
  299. static void atmel_ecc_enqueue(struct atmel_ecc_work_data *work_data,
  300. void (*cbk)(struct atmel_ecc_work_data *work_data,
  301. void *areq, int status),
  302. void *areq)
  303. {
  304. work_data->cbk = (void *)cbk;
  305. work_data->areq = areq;
  306. INIT_WORK(&work_data->work, atmel_ecc_work_handler);
  307. schedule_work(&work_data->work);
  308. }
  309. static unsigned int atmel_ecdh_supported_curve(unsigned int curve_id)
  310. {
  311. if (curve_id == ECC_CURVE_NIST_P256)
  312. return ATMEL_ECC_NIST_P256_N_SIZE;
  313. return 0;
  314. }
  315. /*
  316. * A random private key is generated and stored in the device. The device
  317. * returns the pair public key.
  318. */
  319. static int atmel_ecdh_set_secret(struct crypto_kpp *tfm, const void *buf,
  320. unsigned int len)
  321. {
  322. struct atmel_ecdh_ctx *ctx = kpp_tfm_ctx(tfm);
  323. struct atmel_ecc_cmd *cmd;
  324. void *public_key;
  325. struct ecdh params;
  326. int ret = -ENOMEM;
  327. /* free the old public key, if any */
  328. kfree(ctx->public_key);
  329. /* make sure you don't free the old public key twice */
  330. ctx->public_key = NULL;
  331. if (crypto_ecdh_decode_key(buf, len, &params) < 0) {
  332. dev_err(&ctx->client->dev, "crypto_ecdh_decode_key failed\n");
  333. return -EINVAL;
  334. }
  335. ctx->n_sz = atmel_ecdh_supported_curve(params.curve_id);
  336. if (!ctx->n_sz || params.key_size) {
  337. /* fallback to ecdh software implementation */
  338. ctx->do_fallback = true;
  339. return crypto_kpp_set_secret(ctx->fallback, buf, len);
  340. }
  341. cmd = kmalloc(sizeof(*cmd), GFP_KERNEL);
  342. if (!cmd)
  343. return -ENOMEM;
  344. /*
  345. * The device only supports NIST P256 ECC keys. The public key size will
  346. * always be the same. Use a macro for the key size to avoid unnecessary
  347. * computations.
  348. */
  349. public_key = kmalloc(ATMEL_ECC_PUBKEY_SIZE, GFP_KERNEL);
  350. if (!public_key)
  351. goto free_cmd;
  352. ctx->do_fallback = false;
  353. ctx->curve_id = params.curve_id;
  354. atmel_ecc_init_genkey_cmd(cmd, DATA_SLOT_2);
  355. ret = atmel_ecc_send_receive(ctx->client, cmd);
  356. if (ret)
  357. goto free_public_key;
  358. /* save the public key */
  359. memcpy(public_key, &cmd->data[RSP_DATA_IDX], ATMEL_ECC_PUBKEY_SIZE);
  360. ctx->public_key = public_key;
  361. kfree(cmd);
  362. return 0;
  363. free_public_key:
  364. kfree(public_key);
  365. free_cmd:
  366. kfree(cmd);
  367. return ret;
  368. }
  369. static int atmel_ecdh_generate_public_key(struct kpp_request *req)
  370. {
  371. struct crypto_kpp *tfm = crypto_kpp_reqtfm(req);
  372. struct atmel_ecdh_ctx *ctx = kpp_tfm_ctx(tfm);
  373. size_t copied, nbytes;
  374. int ret = 0;
  375. if (ctx->do_fallback) {
  376. kpp_request_set_tfm(req, ctx->fallback);
  377. return crypto_kpp_generate_public_key(req);
  378. }
  379. /* might want less than we've got */
  380. nbytes = min_t(size_t, ATMEL_ECC_PUBKEY_SIZE, req->dst_len);
  381. /* public key was saved at private key generation */
  382. copied = sg_copy_from_buffer(req->dst,
  383. sg_nents_for_len(req->dst, nbytes),
  384. ctx->public_key, nbytes);
  385. if (copied != nbytes)
  386. ret = -EINVAL;
  387. return ret;
  388. }
  389. static int atmel_ecdh_compute_shared_secret(struct kpp_request *req)
  390. {
  391. struct crypto_kpp *tfm = crypto_kpp_reqtfm(req);
  392. struct atmel_ecdh_ctx *ctx = kpp_tfm_ctx(tfm);
  393. struct atmel_ecc_work_data *work_data;
  394. gfp_t gfp;
  395. int ret;
  396. if (ctx->do_fallback) {
  397. kpp_request_set_tfm(req, ctx->fallback);
  398. return crypto_kpp_compute_shared_secret(req);
  399. }
  400. /* must have exactly two points to be on the curve */
  401. if (req->src_len != ATMEL_ECC_PUBKEY_SIZE)
  402. return -EINVAL;
  403. gfp = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ? GFP_KERNEL :
  404. GFP_ATOMIC;
  405. work_data = kmalloc(sizeof(*work_data), gfp);
  406. if (!work_data)
  407. return -ENOMEM;
  408. work_data->ctx = ctx;
  409. ret = atmel_ecc_init_ecdh_cmd(&work_data->cmd, req->src);
  410. if (ret)
  411. goto free_work_data;
  412. atmel_ecc_enqueue(work_data, atmel_ecdh_done, req);
  413. return -EINPROGRESS;
  414. free_work_data:
  415. kfree(work_data);
  416. return ret;
  417. }
  418. static struct i2c_client *atmel_ecc_i2c_client_alloc(void)
  419. {
  420. struct atmel_ecc_i2c_client_priv *i2c_priv, *min_i2c_priv = NULL;
  421. struct i2c_client *client = ERR_PTR(-ENODEV);
  422. int min_tfm_cnt = INT_MAX;
  423. int tfm_cnt;
  424. spin_lock(&driver_data.i2c_list_lock);
  425. if (list_empty(&driver_data.i2c_client_list)) {
  426. spin_unlock(&driver_data.i2c_list_lock);
  427. return ERR_PTR(-ENODEV);
  428. }
  429. list_for_each_entry(i2c_priv, &driver_data.i2c_client_list,
  430. i2c_client_list_node) {
  431. tfm_cnt = atomic_read(&i2c_priv->tfm_count);
  432. if (tfm_cnt < min_tfm_cnt) {
  433. min_tfm_cnt = tfm_cnt;
  434. min_i2c_priv = i2c_priv;
  435. }
  436. if (!min_tfm_cnt)
  437. break;
  438. }
  439. if (min_i2c_priv) {
  440. atomic_inc(&min_i2c_priv->tfm_count);
  441. client = min_i2c_priv->client;
  442. }
  443. spin_unlock(&driver_data.i2c_list_lock);
  444. return client;
  445. }
  446. static void atmel_ecc_i2c_client_free(struct i2c_client *client)
  447. {
  448. struct atmel_ecc_i2c_client_priv *i2c_priv = i2c_get_clientdata(client);
  449. atomic_dec(&i2c_priv->tfm_count);
  450. }
  451. static int atmel_ecdh_init_tfm(struct crypto_kpp *tfm)
  452. {
  453. const char *alg = kpp_alg_name(tfm);
  454. struct crypto_kpp *fallback;
  455. struct atmel_ecdh_ctx *ctx = kpp_tfm_ctx(tfm);
  456. ctx->client = atmel_ecc_i2c_client_alloc();
  457. if (IS_ERR(ctx->client)) {
  458. pr_err("tfm - i2c_client binding failed\n");
  459. return PTR_ERR(ctx->client);
  460. }
  461. fallback = crypto_alloc_kpp(alg, 0, CRYPTO_ALG_NEED_FALLBACK);
  462. if (IS_ERR(fallback)) {
  463. dev_err(&ctx->client->dev, "Failed to allocate transformation for '%s': %ld\n",
  464. alg, PTR_ERR(fallback));
  465. return PTR_ERR(fallback);
  466. }
  467. crypto_kpp_set_flags(fallback, crypto_kpp_get_flags(tfm));
  468. ctx->fallback = fallback;
  469. return 0;
  470. }
  471. static void atmel_ecdh_exit_tfm(struct crypto_kpp *tfm)
  472. {
  473. struct atmel_ecdh_ctx *ctx = kpp_tfm_ctx(tfm);
  474. kfree(ctx->public_key);
  475. crypto_free_kpp(ctx->fallback);
  476. atmel_ecc_i2c_client_free(ctx->client);
  477. }
  478. static unsigned int atmel_ecdh_max_size(struct crypto_kpp *tfm)
  479. {
  480. struct atmel_ecdh_ctx *ctx = kpp_tfm_ctx(tfm);
  481. if (ctx->fallback)
  482. return crypto_kpp_maxsize(ctx->fallback);
  483. /*
  484. * The device only supports NIST P256 ECC keys. The public key size will
  485. * always be the same. Use a macro for the key size to avoid unnecessary
  486. * computations.
  487. */
  488. return ATMEL_ECC_PUBKEY_SIZE;
  489. }
  490. static struct kpp_alg atmel_ecdh = {
  491. .set_secret = atmel_ecdh_set_secret,
  492. .generate_public_key = atmel_ecdh_generate_public_key,
  493. .compute_shared_secret = atmel_ecdh_compute_shared_secret,
  494. .init = atmel_ecdh_init_tfm,
  495. .exit = atmel_ecdh_exit_tfm,
  496. .max_size = atmel_ecdh_max_size,
  497. .base = {
  498. .cra_flags = CRYPTO_ALG_NEED_FALLBACK,
  499. .cra_name = "ecdh",
  500. .cra_driver_name = "atmel-ecdh",
  501. .cra_priority = ATMEL_ECC_PRIORITY,
  502. .cra_module = THIS_MODULE,
  503. .cra_ctxsize = sizeof(struct atmel_ecdh_ctx),
  504. },
  505. };
  506. static inline size_t atmel_ecc_wake_token_sz(u32 bus_clk_rate)
  507. {
  508. u32 no_of_bits = DIV_ROUND_UP(TWLO_USEC * bus_clk_rate, USEC_PER_SEC);
  509. /* return the size of the wake_token in bytes */
  510. return DIV_ROUND_UP(no_of_bits, 8);
  511. }
  512. static int device_sanity_check(struct i2c_client *client)
  513. {
  514. struct atmel_ecc_cmd *cmd;
  515. int ret;
  516. cmd = kmalloc(sizeof(*cmd), GFP_KERNEL);
  517. if (!cmd)
  518. return -ENOMEM;
  519. atmel_ecc_init_read_cmd(cmd);
  520. ret = atmel_ecc_send_receive(client, cmd);
  521. if (ret)
  522. goto free_cmd;
  523. /*
  524. * It is vital that the Configuration, Data and OTP zones be locked
  525. * prior to release into the field of the system containing the device.
  526. * Failure to lock these zones may permit modification of any secret
  527. * keys and may lead to other security problems.
  528. */
  529. if (cmd->data[LOCK_CONFIG_IDX] || cmd->data[LOCK_VALUE_IDX]) {
  530. dev_err(&client->dev, "Configuration or Data and OTP zones are unlocked!\n");
  531. ret = -ENOTSUPP;
  532. }
  533. /* fall through */
  534. free_cmd:
  535. kfree(cmd);
  536. return ret;
  537. }
  538. static int atmel_ecc_probe(struct i2c_client *client,
  539. const struct i2c_device_id *id)
  540. {
  541. struct atmel_ecc_i2c_client_priv *i2c_priv;
  542. struct device *dev = &client->dev;
  543. int ret;
  544. u32 bus_clk_rate;
  545. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  546. dev_err(dev, "I2C_FUNC_I2C not supported\n");
  547. return -ENODEV;
  548. }
  549. ret = of_property_read_u32(client->adapter->dev.of_node,
  550. "clock-frequency", &bus_clk_rate);
  551. if (ret) {
  552. dev_err(dev, "of: failed to read clock-frequency property\n");
  553. return ret;
  554. }
  555. if (bus_clk_rate > 1000000L) {
  556. dev_err(dev, "%d exceeds maximum supported clock frequency (1MHz)\n",
  557. bus_clk_rate);
  558. return -EINVAL;
  559. }
  560. i2c_priv = devm_kmalloc(dev, sizeof(*i2c_priv), GFP_KERNEL);
  561. if (!i2c_priv)
  562. return -ENOMEM;
  563. i2c_priv->client = client;
  564. mutex_init(&i2c_priv->lock);
  565. /*
  566. * WAKE_TOKEN_MAX_SIZE was calculated for the maximum bus_clk_rate -
  567. * 1MHz. The previous bus_clk_rate check ensures us that wake_token_sz
  568. * will always be smaller than or equal to WAKE_TOKEN_MAX_SIZE.
  569. */
  570. i2c_priv->wake_token_sz = atmel_ecc_wake_token_sz(bus_clk_rate);
  571. memset(i2c_priv->wake_token, 0, sizeof(i2c_priv->wake_token));
  572. atomic_set(&i2c_priv->tfm_count, 0);
  573. i2c_set_clientdata(client, i2c_priv);
  574. ret = device_sanity_check(client);
  575. if (ret)
  576. return ret;
  577. spin_lock(&driver_data.i2c_list_lock);
  578. list_add_tail(&i2c_priv->i2c_client_list_node,
  579. &driver_data.i2c_client_list);
  580. spin_unlock(&driver_data.i2c_list_lock);
  581. ret = crypto_register_kpp(&atmel_ecdh);
  582. if (ret) {
  583. spin_lock(&driver_data.i2c_list_lock);
  584. list_del(&i2c_priv->i2c_client_list_node);
  585. spin_unlock(&driver_data.i2c_list_lock);
  586. dev_err(dev, "%s alg registration failed\n",
  587. atmel_ecdh.base.cra_driver_name);
  588. } else {
  589. dev_info(dev, "atmel ecc algorithms registered in /proc/crypto\n");
  590. }
  591. return ret;
  592. }
  593. static int atmel_ecc_remove(struct i2c_client *client)
  594. {
  595. struct atmel_ecc_i2c_client_priv *i2c_priv = i2c_get_clientdata(client);
  596. /* Return EBUSY if i2c client already allocated. */
  597. if (atomic_read(&i2c_priv->tfm_count)) {
  598. dev_err(&client->dev, "Device is busy\n");
  599. return -EBUSY;
  600. }
  601. crypto_unregister_kpp(&atmel_ecdh);
  602. spin_lock(&driver_data.i2c_list_lock);
  603. list_del(&i2c_priv->i2c_client_list_node);
  604. spin_unlock(&driver_data.i2c_list_lock);
  605. return 0;
  606. }
  607. #ifdef CONFIG_OF
  608. static const struct of_device_id atmel_ecc_dt_ids[] = {
  609. {
  610. .compatible = "atmel,atecc508a",
  611. }, {
  612. /* sentinel */
  613. }
  614. };
  615. MODULE_DEVICE_TABLE(of, atmel_ecc_dt_ids);
  616. #endif
  617. static const struct i2c_device_id atmel_ecc_id[] = {
  618. { "atecc508a", 0 },
  619. { }
  620. };
  621. MODULE_DEVICE_TABLE(i2c, atmel_ecc_id);
  622. static struct i2c_driver atmel_ecc_driver = {
  623. .driver = {
  624. .name = "atmel-ecc",
  625. .of_match_table = of_match_ptr(atmel_ecc_dt_ids),
  626. },
  627. .probe = atmel_ecc_probe,
  628. .remove = atmel_ecc_remove,
  629. .id_table = atmel_ecc_id,
  630. };
  631. static int __init atmel_ecc_init(void)
  632. {
  633. spin_lock_init(&driver_data.i2c_list_lock);
  634. INIT_LIST_HEAD(&driver_data.i2c_client_list);
  635. return i2c_add_driver(&atmel_ecc_driver);
  636. }
  637. static void __exit atmel_ecc_exit(void)
  638. {
  639. flush_scheduled_work();
  640. i2c_del_driver(&atmel_ecc_driver);
  641. }
  642. module_init(atmel_ecc_init);
  643. module_exit(atmel_ecc_exit);
  644. MODULE_AUTHOR("Tudor Ambarus <tudor.ambarus@microchip.com>");
  645. MODULE_DESCRIPTION("Microchip / Atmel ECC (I2C) driver");
  646. MODULE_LICENSE("GPL v2");