atmel-ecc.c 21 KB

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