atmel-ecc.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  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,
  172. sg_nents_for_len(pubkey,
  173. ATMEL_ECC_PUBKEY_SIZE),
  174. cmd->data, ATMEL_ECC_PUBKEY_SIZE);
  175. if (copied != ATMEL_ECC_PUBKEY_SIZE)
  176. return -EINVAL;
  177. atmel_ecc_checksum(cmd);
  178. cmd->msecs = MAX_EXEC_TIME_ECDH;
  179. cmd->rxsize = ECDH_RSP_SIZE;
  180. return 0;
  181. }
  182. /*
  183. * After wake and after execution of a command, there will be error, status, or
  184. * result bytes in the device's output register that can be retrieved by the
  185. * system. When the length of that group is four bytes, the codes returned are
  186. * detailed in error_list.
  187. */
  188. static int atmel_ecc_status(struct device *dev, u8 *status)
  189. {
  190. size_t err_list_len = ARRAY_SIZE(error_list);
  191. int i;
  192. u8 err_id = status[1];
  193. if (*status != STATUS_SIZE)
  194. return 0;
  195. if (err_id == STATUS_WAKE_SUCCESSFUL || err_id == STATUS_NOERR)
  196. return 0;
  197. for (i = 0; i < err_list_len; i++)
  198. if (error_list[i].value == err_id)
  199. break;
  200. /* if err_id is not in the error_list then ignore it */
  201. if (i != err_list_len) {
  202. dev_err(dev, "%02x: %s:\n", err_id, error_list[i].error_text);
  203. return err_id;
  204. }
  205. return 0;
  206. }
  207. static int atmel_ecc_wakeup(struct i2c_client *client)
  208. {
  209. struct atmel_ecc_i2c_client_priv *i2c_priv = i2c_get_clientdata(client);
  210. u8 status[STATUS_RSP_SIZE];
  211. int ret;
  212. /*
  213. * The device ignores any levels or transitions on the SCL pin when the
  214. * device is idle, asleep or during waking up. Don't check for error
  215. * when waking up the device.
  216. */
  217. i2c_master_send(client, i2c_priv->wake_token, i2c_priv->wake_token_sz);
  218. /*
  219. * Wait to wake the device. Typical execution times for ecdh and genkey
  220. * are around tens of milliseconds. Delta is chosen to 50 microseconds.
  221. */
  222. usleep_range(TWHI_MIN, TWHI_MAX);
  223. ret = i2c_master_recv(client, status, STATUS_SIZE);
  224. if (ret < 0)
  225. return ret;
  226. return atmel_ecc_status(&client->dev, status);
  227. }
  228. static int atmel_ecc_sleep(struct i2c_client *client)
  229. {
  230. u8 sleep = SLEEP_TOKEN;
  231. return i2c_master_send(client, &sleep, 1);
  232. }
  233. static void atmel_ecdh_done(struct atmel_ecc_work_data *work_data, void *areq,
  234. int status)
  235. {
  236. struct kpp_request *req = areq;
  237. struct atmel_ecdh_ctx *ctx = work_data->ctx;
  238. struct atmel_ecc_cmd *cmd = &work_data->cmd;
  239. size_t copied, n_sz;
  240. if (status)
  241. goto free_work_data;
  242. /* might want less than we've got */
  243. n_sz = min_t(size_t, ctx->n_sz, req->dst_len);
  244. /* copy the shared secret */
  245. copied = sg_copy_from_buffer(req->dst, sg_nents_for_len(req->dst, n_sz),
  246. &cmd->data[RSP_DATA_IDX], n_sz);
  247. if (copied != n_sz)
  248. status = -EINVAL;
  249. /* fall through */
  250. free_work_data:
  251. kzfree(work_data);
  252. kpp_request_complete(req, status);
  253. }
  254. /*
  255. * atmel_ecc_send_receive() - send a command to the device and receive its
  256. * response.
  257. * @client: i2c client device
  258. * @cmd : structure used to communicate with the device
  259. *
  260. * After the device receives a Wake token, a watchdog counter starts within the
  261. * device. After the watchdog timer expires, the device enters sleep mode
  262. * regardless of whether some I/O transmission or command execution is in
  263. * progress. If a command is attempted when insufficient time remains prior to
  264. * watchdog timer execution, the device will return the watchdog timeout error
  265. * code without attempting to execute the command. There is no way to reset the
  266. * counter other than to put the device into sleep or idle mode and then
  267. * wake it up again.
  268. */
  269. static int atmel_ecc_send_receive(struct i2c_client *client,
  270. struct atmel_ecc_cmd *cmd)
  271. {
  272. struct atmel_ecc_i2c_client_priv *i2c_priv = i2c_get_clientdata(client);
  273. int ret;
  274. mutex_lock(&i2c_priv->lock);
  275. ret = atmel_ecc_wakeup(client);
  276. if (ret)
  277. goto err;
  278. /* send the command */
  279. ret = i2c_master_send(client, (u8 *)cmd, cmd->count + WORD_ADDR_SIZE);
  280. if (ret < 0)
  281. goto err;
  282. /* delay the appropriate amount of time for command to execute */
  283. msleep(cmd->msecs);
  284. /* receive the response */
  285. ret = i2c_master_recv(client, cmd->data, cmd->rxsize);
  286. if (ret < 0)
  287. goto err;
  288. /* put the device into low-power mode */
  289. ret = atmel_ecc_sleep(client);
  290. if (ret < 0)
  291. goto err;
  292. mutex_unlock(&i2c_priv->lock);
  293. return atmel_ecc_status(&client->dev, cmd->data);
  294. err:
  295. mutex_unlock(&i2c_priv->lock);
  296. return ret;
  297. }
  298. static void atmel_ecc_work_handler(struct work_struct *work)
  299. {
  300. struct atmel_ecc_work_data *work_data =
  301. container_of(work, struct atmel_ecc_work_data, work);
  302. struct atmel_ecc_cmd *cmd = &work_data->cmd;
  303. struct i2c_client *client = work_data->ctx->client;
  304. int status;
  305. status = atmel_ecc_send_receive(client, cmd);
  306. work_data->cbk(work_data, work_data->areq, status);
  307. }
  308. static void atmel_ecc_enqueue(struct atmel_ecc_work_data *work_data,
  309. void (*cbk)(struct atmel_ecc_work_data *work_data,
  310. void *areq, int status),
  311. void *areq)
  312. {
  313. work_data->cbk = (void *)cbk;
  314. work_data->areq = areq;
  315. INIT_WORK(&work_data->work, atmel_ecc_work_handler);
  316. schedule_work(&work_data->work);
  317. }
  318. static unsigned int atmel_ecdh_supported_curve(unsigned int curve_id)
  319. {
  320. if (curve_id == ECC_CURVE_NIST_P256)
  321. return ATMEL_ECC_NIST_P256_N_SIZE;
  322. return 0;
  323. }
  324. /*
  325. * A random private key is generated and stored in the device. The device
  326. * returns the pair public key.
  327. */
  328. static int atmel_ecdh_set_secret(struct crypto_kpp *tfm, const void *buf,
  329. unsigned int len)
  330. {
  331. struct atmel_ecdh_ctx *ctx = kpp_tfm_ctx(tfm);
  332. struct atmel_ecc_cmd *cmd;
  333. void *public_key;
  334. struct ecdh params;
  335. int ret = -ENOMEM;
  336. /* free the old public key, if any */
  337. kfree(ctx->public_key);
  338. /* make sure you don't free the old public key twice */
  339. ctx->public_key = NULL;
  340. if (crypto_ecdh_decode_key(buf, len, &params) < 0) {
  341. dev_err(&ctx->client->dev, "crypto_ecdh_decode_key failed\n");
  342. return -EINVAL;
  343. }
  344. ctx->n_sz = atmel_ecdh_supported_curve(params.curve_id);
  345. if (!ctx->n_sz || params.key_size) {
  346. /* fallback to ecdh software implementation */
  347. ctx->do_fallback = true;
  348. return crypto_kpp_set_secret(ctx->fallback, buf, len);
  349. }
  350. cmd = kmalloc(sizeof(*cmd), GFP_KERNEL);
  351. if (!cmd)
  352. return -ENOMEM;
  353. /*
  354. * The device only supports NIST P256 ECC keys. The public key size will
  355. * always be the same. Use a macro for the key size to avoid unnecessary
  356. * computations.
  357. */
  358. public_key = kmalloc(ATMEL_ECC_PUBKEY_SIZE, GFP_KERNEL);
  359. if (!public_key)
  360. goto free_cmd;
  361. ctx->do_fallback = false;
  362. ctx->curve_id = params.curve_id;
  363. atmel_ecc_init_genkey_cmd(cmd, DATA_SLOT_2);
  364. ret = atmel_ecc_send_receive(ctx->client, cmd);
  365. if (ret)
  366. goto free_public_key;
  367. /* save the public key */
  368. memcpy(public_key, &cmd->data[RSP_DATA_IDX], ATMEL_ECC_PUBKEY_SIZE);
  369. ctx->public_key = public_key;
  370. kfree(cmd);
  371. return 0;
  372. free_public_key:
  373. kfree(public_key);
  374. free_cmd:
  375. kfree(cmd);
  376. return ret;
  377. }
  378. static int atmel_ecdh_generate_public_key(struct kpp_request *req)
  379. {
  380. struct crypto_kpp *tfm = crypto_kpp_reqtfm(req);
  381. struct atmel_ecdh_ctx *ctx = kpp_tfm_ctx(tfm);
  382. size_t copied, nbytes;
  383. int ret = 0;
  384. if (ctx->do_fallback) {
  385. kpp_request_set_tfm(req, ctx->fallback);
  386. return crypto_kpp_generate_public_key(req);
  387. }
  388. /* might want less than we've got */
  389. nbytes = min_t(size_t, ATMEL_ECC_PUBKEY_SIZE, req->dst_len);
  390. /* public key was saved at private key generation */
  391. copied = sg_copy_from_buffer(req->dst,
  392. sg_nents_for_len(req->dst, nbytes),
  393. ctx->public_key, nbytes);
  394. if (copied != nbytes)
  395. ret = -EINVAL;
  396. return ret;
  397. }
  398. static int atmel_ecdh_compute_shared_secret(struct kpp_request *req)
  399. {
  400. struct crypto_kpp *tfm = crypto_kpp_reqtfm(req);
  401. struct atmel_ecdh_ctx *ctx = kpp_tfm_ctx(tfm);
  402. struct atmel_ecc_work_data *work_data;
  403. gfp_t gfp;
  404. int ret;
  405. if (ctx->do_fallback) {
  406. kpp_request_set_tfm(req, ctx->fallback);
  407. return crypto_kpp_compute_shared_secret(req);
  408. }
  409. /* must have exactly two points to be on the curve */
  410. if (req->src_len != ATMEL_ECC_PUBKEY_SIZE)
  411. return -EINVAL;
  412. gfp = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ? GFP_KERNEL :
  413. GFP_ATOMIC;
  414. work_data = kmalloc(sizeof(*work_data), gfp);
  415. if (!work_data)
  416. return -ENOMEM;
  417. work_data->ctx = ctx;
  418. ret = atmel_ecc_init_ecdh_cmd(&work_data->cmd, req->src);
  419. if (ret)
  420. goto free_work_data;
  421. atmel_ecc_enqueue(work_data, atmel_ecdh_done, req);
  422. return -EINPROGRESS;
  423. free_work_data:
  424. kfree(work_data);
  425. return ret;
  426. }
  427. static struct i2c_client *atmel_ecc_i2c_client_alloc(void)
  428. {
  429. struct atmel_ecc_i2c_client_priv *i2c_priv, *min_i2c_priv = NULL;
  430. struct i2c_client *client = ERR_PTR(-ENODEV);
  431. int min_tfm_cnt = INT_MAX;
  432. int tfm_cnt;
  433. spin_lock(&driver_data.i2c_list_lock);
  434. if (list_empty(&driver_data.i2c_client_list)) {
  435. spin_unlock(&driver_data.i2c_list_lock);
  436. return ERR_PTR(-ENODEV);
  437. }
  438. list_for_each_entry(i2c_priv, &driver_data.i2c_client_list,
  439. i2c_client_list_node) {
  440. tfm_cnt = atomic_read(&i2c_priv->tfm_count);
  441. if (tfm_cnt < min_tfm_cnt) {
  442. min_tfm_cnt = tfm_cnt;
  443. min_i2c_priv = i2c_priv;
  444. }
  445. if (!min_tfm_cnt)
  446. break;
  447. }
  448. if (min_i2c_priv) {
  449. atomic_inc(&min_i2c_priv->tfm_count);
  450. client = min_i2c_priv->client;
  451. }
  452. spin_unlock(&driver_data.i2c_list_lock);
  453. return client;
  454. }
  455. static void atmel_ecc_i2c_client_free(struct i2c_client *client)
  456. {
  457. struct atmel_ecc_i2c_client_priv *i2c_priv = i2c_get_clientdata(client);
  458. atomic_dec(&i2c_priv->tfm_count);
  459. }
  460. static int atmel_ecdh_init_tfm(struct crypto_kpp *tfm)
  461. {
  462. const char *alg = kpp_alg_name(tfm);
  463. struct crypto_kpp *fallback;
  464. struct atmel_ecdh_ctx *ctx = kpp_tfm_ctx(tfm);
  465. ctx->client = atmel_ecc_i2c_client_alloc();
  466. if (IS_ERR(ctx->client)) {
  467. pr_err("tfm - i2c_client binding failed\n");
  468. return PTR_ERR(ctx->client);
  469. }
  470. fallback = crypto_alloc_kpp(alg, 0, CRYPTO_ALG_NEED_FALLBACK);
  471. if (IS_ERR(fallback)) {
  472. dev_err(&ctx->client->dev, "Failed to allocate transformation for '%s': %ld\n",
  473. alg, PTR_ERR(fallback));
  474. return PTR_ERR(fallback);
  475. }
  476. crypto_kpp_set_flags(fallback, crypto_kpp_get_flags(tfm));
  477. ctx->fallback = fallback;
  478. return 0;
  479. }
  480. static void atmel_ecdh_exit_tfm(struct crypto_kpp *tfm)
  481. {
  482. struct atmel_ecdh_ctx *ctx = kpp_tfm_ctx(tfm);
  483. kfree(ctx->public_key);
  484. crypto_free_kpp(ctx->fallback);
  485. atmel_ecc_i2c_client_free(ctx->client);
  486. }
  487. static unsigned int atmel_ecdh_max_size(struct crypto_kpp *tfm)
  488. {
  489. struct atmel_ecdh_ctx *ctx = kpp_tfm_ctx(tfm);
  490. if (ctx->fallback)
  491. return crypto_kpp_maxsize(ctx->fallback);
  492. /*
  493. * The device only supports NIST P256 ECC keys. The public key size will
  494. * always be the same. Use a macro for the key size to avoid unnecessary
  495. * computations.
  496. */
  497. return ATMEL_ECC_PUBKEY_SIZE;
  498. }
  499. static struct kpp_alg atmel_ecdh = {
  500. .set_secret = atmel_ecdh_set_secret,
  501. .generate_public_key = atmel_ecdh_generate_public_key,
  502. .compute_shared_secret = atmel_ecdh_compute_shared_secret,
  503. .init = atmel_ecdh_init_tfm,
  504. .exit = atmel_ecdh_exit_tfm,
  505. .max_size = atmel_ecdh_max_size,
  506. .base = {
  507. .cra_flags = CRYPTO_ALG_NEED_FALLBACK,
  508. .cra_name = "ecdh",
  509. .cra_driver_name = "atmel-ecdh",
  510. .cra_priority = ATMEL_ECC_PRIORITY,
  511. .cra_module = THIS_MODULE,
  512. .cra_ctxsize = sizeof(struct atmel_ecdh_ctx),
  513. },
  514. };
  515. static inline size_t atmel_ecc_wake_token_sz(u32 bus_clk_rate)
  516. {
  517. u32 no_of_bits = DIV_ROUND_UP(TWLO_USEC * bus_clk_rate, USEC_PER_SEC);
  518. /* return the size of the wake_token in bytes */
  519. return DIV_ROUND_UP(no_of_bits, 8);
  520. }
  521. static int device_sanity_check(struct i2c_client *client)
  522. {
  523. struct atmel_ecc_cmd *cmd;
  524. int ret;
  525. cmd = kmalloc(sizeof(*cmd), GFP_KERNEL);
  526. if (!cmd)
  527. return -ENOMEM;
  528. atmel_ecc_init_read_cmd(cmd);
  529. ret = atmel_ecc_send_receive(client, cmd);
  530. if (ret)
  531. goto free_cmd;
  532. /*
  533. * It is vital that the Configuration, Data and OTP zones be locked
  534. * prior to release into the field of the system containing the device.
  535. * Failure to lock these zones may permit modification of any secret
  536. * keys and may lead to other security problems.
  537. */
  538. if (cmd->data[LOCK_CONFIG_IDX] || cmd->data[LOCK_VALUE_IDX]) {
  539. dev_err(&client->dev, "Configuration or Data and OTP zones are unlocked!\n");
  540. ret = -ENOTSUPP;
  541. }
  542. /* fall through */
  543. free_cmd:
  544. kfree(cmd);
  545. return ret;
  546. }
  547. static int atmel_ecc_probe(struct i2c_client *client,
  548. const struct i2c_device_id *id)
  549. {
  550. struct atmel_ecc_i2c_client_priv *i2c_priv;
  551. struct device *dev = &client->dev;
  552. int ret;
  553. u32 bus_clk_rate;
  554. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  555. dev_err(dev, "I2C_FUNC_I2C not supported\n");
  556. return -ENODEV;
  557. }
  558. ret = of_property_read_u32(client->adapter->dev.of_node,
  559. "clock-frequency", &bus_clk_rate);
  560. if (ret) {
  561. dev_err(dev, "of: failed to read clock-frequency property\n");
  562. return ret;
  563. }
  564. if (bus_clk_rate > 1000000L) {
  565. dev_err(dev, "%d exceeds maximum supported clock frequency (1MHz)\n",
  566. bus_clk_rate);
  567. return -EINVAL;
  568. }
  569. i2c_priv = devm_kmalloc(dev, sizeof(*i2c_priv), GFP_KERNEL);
  570. if (!i2c_priv)
  571. return -ENOMEM;
  572. i2c_priv->client = client;
  573. mutex_init(&i2c_priv->lock);
  574. /*
  575. * WAKE_TOKEN_MAX_SIZE was calculated for the maximum bus_clk_rate -
  576. * 1MHz. The previous bus_clk_rate check ensures us that wake_token_sz
  577. * will always be smaller than or equal to WAKE_TOKEN_MAX_SIZE.
  578. */
  579. i2c_priv->wake_token_sz = atmel_ecc_wake_token_sz(bus_clk_rate);
  580. memset(i2c_priv->wake_token, 0, sizeof(i2c_priv->wake_token));
  581. atomic_set(&i2c_priv->tfm_count, 0);
  582. i2c_set_clientdata(client, i2c_priv);
  583. ret = device_sanity_check(client);
  584. if (ret)
  585. return ret;
  586. spin_lock(&driver_data.i2c_list_lock);
  587. list_add_tail(&i2c_priv->i2c_client_list_node,
  588. &driver_data.i2c_client_list);
  589. spin_unlock(&driver_data.i2c_list_lock);
  590. ret = crypto_register_kpp(&atmel_ecdh);
  591. if (ret) {
  592. spin_lock(&driver_data.i2c_list_lock);
  593. list_del(&i2c_priv->i2c_client_list_node);
  594. spin_unlock(&driver_data.i2c_list_lock);
  595. dev_err(dev, "%s alg registration failed\n",
  596. atmel_ecdh.base.cra_driver_name);
  597. } else {
  598. dev_info(dev, "atmel ecc algorithms registered in /proc/crypto\n");
  599. }
  600. return ret;
  601. }
  602. static int atmel_ecc_remove(struct i2c_client *client)
  603. {
  604. struct atmel_ecc_i2c_client_priv *i2c_priv = i2c_get_clientdata(client);
  605. /* Return EBUSY if i2c client already allocated. */
  606. if (atomic_read(&i2c_priv->tfm_count)) {
  607. dev_err(&client->dev, "Device is busy\n");
  608. return -EBUSY;
  609. }
  610. crypto_unregister_kpp(&atmel_ecdh);
  611. spin_lock(&driver_data.i2c_list_lock);
  612. list_del(&i2c_priv->i2c_client_list_node);
  613. spin_unlock(&driver_data.i2c_list_lock);
  614. return 0;
  615. }
  616. #ifdef CONFIG_OF
  617. static const struct of_device_id atmel_ecc_dt_ids[] = {
  618. {
  619. .compatible = "atmel,atecc508a",
  620. }, {
  621. /* sentinel */
  622. }
  623. };
  624. MODULE_DEVICE_TABLE(of, atmel_ecc_dt_ids);
  625. #endif
  626. static const struct i2c_device_id atmel_ecc_id[] = {
  627. { "atecc508a", 0 },
  628. { }
  629. };
  630. MODULE_DEVICE_TABLE(i2c, atmel_ecc_id);
  631. static struct i2c_driver atmel_ecc_driver = {
  632. .driver = {
  633. .name = "atmel-ecc",
  634. .of_match_table = of_match_ptr(atmel_ecc_dt_ids),
  635. },
  636. .probe = atmel_ecc_probe,
  637. .remove = atmel_ecc_remove,
  638. .id_table = atmel_ecc_id,
  639. };
  640. static int __init atmel_ecc_init(void)
  641. {
  642. spin_lock_init(&driver_data.i2c_list_lock);
  643. INIT_LIST_HEAD(&driver_data.i2c_client_list);
  644. return i2c_add_driver(&atmel_ecc_driver);
  645. }
  646. static void __exit atmel_ecc_exit(void)
  647. {
  648. flush_scheduled_work();
  649. i2c_del_driver(&atmel_ecc_driver);
  650. }
  651. module_init(atmel_ecc_init);
  652. module_exit(atmel_ecc_exit);
  653. MODULE_AUTHOR("Tudor Ambarus <tudor.ambarus@microchip.com>");
  654. MODULE_DESCRIPTION("Microchip / Atmel ECC (I2C) driver");
  655. MODULE_LICENSE("GPL v2");