keembay-ocs-ecc.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Intel Keem Bay OCS ECC Crypto Driver.
  4. *
  5. * Copyright (C) 2019-2021 Intel Corporation
  6. */
  7. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  8. #include <linux/clk.h>
  9. #include <linux/completion.h>
  10. #include <linux/crypto.h>
  11. #include <linux/delay.h>
  12. #include <linux/fips.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/io.h>
  15. #include <linux/iopoll.h>
  16. #include <linux/irq.h>
  17. #include <linux/module.h>
  18. #include <linux/of.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/scatterlist.h>
  21. #include <linux/slab.h>
  22. #include <linux/types.h>
  23. #include <crypto/ecc_curve.h>
  24. #include <crypto/ecdh.h>
  25. #include <crypto/engine.h>
  26. #include <crypto/kpp.h>
  27. #include <crypto/rng.h>
  28. #include <crypto/internal/ecc.h>
  29. #include <crypto/internal/kpp.h>
  30. #define DRV_NAME "keembay-ocs-ecc"
  31. #define KMB_OCS_ECC_PRIORITY 350
  32. #define HW_OFFS_OCS_ECC_COMMAND 0x00000000
  33. #define HW_OFFS_OCS_ECC_STATUS 0x00000004
  34. #define HW_OFFS_OCS_ECC_DATA_IN 0x00000080
  35. #define HW_OFFS_OCS_ECC_CX_DATA_OUT 0x00000100
  36. #define HW_OFFS_OCS_ECC_CY_DATA_OUT 0x00000180
  37. #define HW_OFFS_OCS_ECC_ISR 0x00000400
  38. #define HW_OFFS_OCS_ECC_IER 0x00000404
  39. #define HW_OCS_ECC_ISR_INT_STATUS_DONE BIT(0)
  40. #define HW_OCS_ECC_COMMAND_INS_BP BIT(0)
  41. #define HW_OCS_ECC_COMMAND_START_VAL BIT(0)
  42. #define OCS_ECC_OP_SIZE_384 BIT(8)
  43. #define OCS_ECC_OP_SIZE_256 0
  44. /* ECC Instruction : for ECC_COMMAND */
  45. #define OCS_ECC_INST_WRITE_AX (0x1 << HW_OCS_ECC_COMMAND_INS_BP)
  46. #define OCS_ECC_INST_WRITE_AY (0x2 << HW_OCS_ECC_COMMAND_INS_BP)
  47. #define OCS_ECC_INST_WRITE_BX_D (0x3 << HW_OCS_ECC_COMMAND_INS_BP)
  48. #define OCS_ECC_INST_WRITE_BY_L (0x4 << HW_OCS_ECC_COMMAND_INS_BP)
  49. #define OCS_ECC_INST_WRITE_P (0x5 << HW_OCS_ECC_COMMAND_INS_BP)
  50. #define OCS_ECC_INST_WRITE_A (0x6 << HW_OCS_ECC_COMMAND_INS_BP)
  51. #define OCS_ECC_INST_CALC_D_IDX_A (0x8 << HW_OCS_ECC_COMMAND_INS_BP)
  52. #define OCS_ECC_INST_CALC_A_POW_B_MODP (0xB << HW_OCS_ECC_COMMAND_INS_BP)
  53. #define OCS_ECC_INST_CALC_A_MUL_B_MODP (0xC << HW_OCS_ECC_COMMAND_INS_BP)
  54. #define OCS_ECC_INST_CALC_A_ADD_B_MODP (0xD << HW_OCS_ECC_COMMAND_INS_BP)
  55. #define ECC_ENABLE_INTR 1
  56. #define POLL_USEC 100
  57. #define TIMEOUT_USEC 10000
  58. #define KMB_ECC_VLI_MAX_DIGITS ECC_CURVE_NIST_P384_DIGITS
  59. #define KMB_ECC_VLI_MAX_BYTES (KMB_ECC_VLI_MAX_DIGITS \
  60. << ECC_DIGITS_TO_BYTES_SHIFT)
  61. #define POW_CUBE 3
  62. /**
  63. * struct ocs_ecc_dev - ECC device context
  64. * @list: List of device contexts
  65. * @dev: OCS ECC device
  66. * @base_reg: IO base address of OCS ECC
  67. * @engine: Crypto engine for the device
  68. * @irq_done: IRQ done completion.
  69. * @irq: IRQ number
  70. */
  71. struct ocs_ecc_dev {
  72. struct list_head list;
  73. struct device *dev;
  74. void __iomem *base_reg;
  75. struct crypto_engine *engine;
  76. struct completion irq_done;
  77. int irq;
  78. };
  79. /**
  80. * struct ocs_ecc_ctx - Transformation context.
  81. * @engine_ctx: Crypto engine ctx.
  82. * @ecc_dev: The ECC driver associated with this context.
  83. * @curve: The elliptic curve used by this transformation.
  84. * @private_key: The private key.
  85. */
  86. struct ocs_ecc_ctx {
  87. struct crypto_engine_ctx engine_ctx;
  88. struct ocs_ecc_dev *ecc_dev;
  89. const struct ecc_curve *curve;
  90. u64 private_key[KMB_ECC_VLI_MAX_DIGITS];
  91. };
  92. /* Driver data. */
  93. struct ocs_ecc_drv {
  94. struct list_head dev_list;
  95. spinlock_t lock; /* Protects dev_list. */
  96. };
  97. /* Global variable holding the list of OCS ECC devices (only one expected). */
  98. static struct ocs_ecc_drv ocs_ecc = {
  99. .dev_list = LIST_HEAD_INIT(ocs_ecc.dev_list),
  100. .lock = __SPIN_LOCK_UNLOCKED(ocs_ecc.lock),
  101. };
  102. /* Get OCS ECC tfm context from kpp_request. */
  103. static inline struct ocs_ecc_ctx *kmb_ocs_ecc_tctx(struct kpp_request *req)
  104. {
  105. return kpp_tfm_ctx(crypto_kpp_reqtfm(req));
  106. }
  107. /* Converts number of digits to number of bytes. */
  108. static inline unsigned int digits_to_bytes(unsigned int n)
  109. {
  110. return n << ECC_DIGITS_TO_BYTES_SHIFT;
  111. }
  112. /*
  113. * Wait for ECC idle i.e when an operation (other than write operations)
  114. * is done.
  115. */
  116. static inline int ocs_ecc_wait_idle(struct ocs_ecc_dev *dev)
  117. {
  118. u32 value;
  119. return readl_poll_timeout((dev->base_reg + HW_OFFS_OCS_ECC_STATUS),
  120. value,
  121. !(value & HW_OCS_ECC_ISR_INT_STATUS_DONE),
  122. POLL_USEC, TIMEOUT_USEC);
  123. }
  124. static void ocs_ecc_cmd_start(struct ocs_ecc_dev *ecc_dev, u32 op_size)
  125. {
  126. iowrite32(op_size | HW_OCS_ECC_COMMAND_START_VAL,
  127. ecc_dev->base_reg + HW_OFFS_OCS_ECC_COMMAND);
  128. }
  129. /* Direct write of u32 buffer to ECC engine with associated instruction. */
  130. static void ocs_ecc_write_cmd_and_data(struct ocs_ecc_dev *dev,
  131. u32 op_size,
  132. u32 inst,
  133. const void *data_in,
  134. size_t data_size)
  135. {
  136. iowrite32(op_size | inst, dev->base_reg + HW_OFFS_OCS_ECC_COMMAND);
  137. /* MMIO Write src uint32 to dst. */
  138. memcpy_toio(dev->base_reg + HW_OFFS_OCS_ECC_DATA_IN, data_in,
  139. data_size);
  140. }
  141. /* Start OCS ECC operation and wait for its completion. */
  142. static int ocs_ecc_trigger_op(struct ocs_ecc_dev *ecc_dev, u32 op_size,
  143. u32 inst)
  144. {
  145. reinit_completion(&ecc_dev->irq_done);
  146. iowrite32(ECC_ENABLE_INTR, ecc_dev->base_reg + HW_OFFS_OCS_ECC_IER);
  147. iowrite32(op_size | inst, ecc_dev->base_reg + HW_OFFS_OCS_ECC_COMMAND);
  148. return wait_for_completion_interruptible(&ecc_dev->irq_done);
  149. }
  150. /**
  151. * ocs_ecc_read_cx_out() - Read the CX data output buffer.
  152. * @dev: The OCS ECC device to read from.
  153. * @cx_out: The buffer where to store the CX value. Must be at least
  154. * @byte_count byte long.
  155. * @byte_count: The amount of data to read.
  156. */
  157. static inline void ocs_ecc_read_cx_out(struct ocs_ecc_dev *dev, void *cx_out,
  158. size_t byte_count)
  159. {
  160. memcpy_fromio(cx_out, dev->base_reg + HW_OFFS_OCS_ECC_CX_DATA_OUT,
  161. byte_count);
  162. }
  163. /**
  164. * ocs_ecc_read_cy_out() - Read the CX data output buffer.
  165. * @dev: The OCS ECC device to read from.
  166. * @cy_out: The buffer where to store the CY value. Must be at least
  167. * @byte_count byte long.
  168. * @byte_count: The amount of data to read.
  169. */
  170. static inline void ocs_ecc_read_cy_out(struct ocs_ecc_dev *dev, void *cy_out,
  171. size_t byte_count)
  172. {
  173. memcpy_fromio(cy_out, dev->base_reg + HW_OFFS_OCS_ECC_CY_DATA_OUT,
  174. byte_count);
  175. }
  176. static struct ocs_ecc_dev *kmb_ocs_ecc_find_dev(struct ocs_ecc_ctx *tctx)
  177. {
  178. if (tctx->ecc_dev)
  179. return tctx->ecc_dev;
  180. spin_lock(&ocs_ecc.lock);
  181. /* Only a single OCS device available. */
  182. tctx->ecc_dev = list_first_entry(&ocs_ecc.dev_list, struct ocs_ecc_dev,
  183. list);
  184. spin_unlock(&ocs_ecc.lock);
  185. return tctx->ecc_dev;
  186. }
  187. /* Do point multiplication using OCS ECC HW. */
  188. static int kmb_ecc_point_mult(struct ocs_ecc_dev *ecc_dev,
  189. struct ecc_point *result,
  190. const struct ecc_point *point,
  191. u64 *scalar,
  192. const struct ecc_curve *curve)
  193. {
  194. u8 sca[KMB_ECC_VLI_MAX_BYTES]; /* Use the maximum data size. */
  195. u32 op_size = (curve->g.ndigits > ECC_CURVE_NIST_P256_DIGITS) ?
  196. OCS_ECC_OP_SIZE_384 : OCS_ECC_OP_SIZE_256;
  197. size_t nbytes = digits_to_bytes(curve->g.ndigits);
  198. int rc = 0;
  199. /* Generate random nbytes for Simple and Differential SCA protection. */
  200. rc = crypto_get_default_rng();
  201. if (rc)
  202. return rc;
  203. rc = crypto_rng_get_bytes(crypto_default_rng, sca, nbytes);
  204. crypto_put_default_rng();
  205. if (rc)
  206. return rc;
  207. /* Wait engine to be idle before starting new operation. */
  208. rc = ocs_ecc_wait_idle(ecc_dev);
  209. if (rc)
  210. return rc;
  211. /* Send ecc_start pulse as well as indicating operation size. */
  212. ocs_ecc_cmd_start(ecc_dev, op_size);
  213. /* Write ax param; Base point (Gx). */
  214. ocs_ecc_write_cmd_and_data(ecc_dev, op_size, OCS_ECC_INST_WRITE_AX,
  215. point->x, nbytes);
  216. /* Write ay param; Base point (Gy). */
  217. ocs_ecc_write_cmd_and_data(ecc_dev, op_size, OCS_ECC_INST_WRITE_AY,
  218. point->y, nbytes);
  219. /*
  220. * Write the private key into DATA_IN reg.
  221. *
  222. * Since DATA_IN register is used to write different values during the
  223. * computation private Key value is overwritten with
  224. * side-channel-resistance value.
  225. */
  226. ocs_ecc_write_cmd_and_data(ecc_dev, op_size, OCS_ECC_INST_WRITE_BX_D,
  227. scalar, nbytes);
  228. /* Write operand by/l. */
  229. ocs_ecc_write_cmd_and_data(ecc_dev, op_size, OCS_ECC_INST_WRITE_BY_L,
  230. sca, nbytes);
  231. memzero_explicit(sca, sizeof(sca));
  232. /* Write p = curve prime(GF modulus). */
  233. ocs_ecc_write_cmd_and_data(ecc_dev, op_size, OCS_ECC_INST_WRITE_P,
  234. curve->p, nbytes);
  235. /* Write a = curve coefficient. */
  236. ocs_ecc_write_cmd_and_data(ecc_dev, op_size, OCS_ECC_INST_WRITE_A,
  237. curve->a, nbytes);
  238. /* Make hardware perform the multiplication. */
  239. rc = ocs_ecc_trigger_op(ecc_dev, op_size, OCS_ECC_INST_CALC_D_IDX_A);
  240. if (rc)
  241. return rc;
  242. /* Read result. */
  243. ocs_ecc_read_cx_out(ecc_dev, result->x, nbytes);
  244. ocs_ecc_read_cy_out(ecc_dev, result->y, nbytes);
  245. return 0;
  246. }
  247. /**
  248. * kmb_ecc_do_scalar_op() - Perform Scalar operation using OCS ECC HW.
  249. * @ecc_dev: The OCS ECC device to use.
  250. * @scalar_out: Where to store the output scalar.
  251. * @scalar_a: Input scalar operand 'a'.
  252. * @scalar_b: Input scalar operand 'b'
  253. * @curve: The curve on which the operation is performed.
  254. * @ndigits: The size of the operands (in digits).
  255. * @inst: The operation to perform (as an OCS ECC instruction).
  256. *
  257. * Return: 0 on success, negative error code otherwise.
  258. */
  259. static int kmb_ecc_do_scalar_op(struct ocs_ecc_dev *ecc_dev, u64 *scalar_out,
  260. const u64 *scalar_a, const u64 *scalar_b,
  261. const struct ecc_curve *curve,
  262. unsigned int ndigits, const u32 inst)
  263. {
  264. u32 op_size = (ndigits > ECC_CURVE_NIST_P256_DIGITS) ?
  265. OCS_ECC_OP_SIZE_384 : OCS_ECC_OP_SIZE_256;
  266. size_t nbytes = digits_to_bytes(ndigits);
  267. int rc;
  268. /* Wait engine to be idle before starting new operation. */
  269. rc = ocs_ecc_wait_idle(ecc_dev);
  270. if (rc)
  271. return rc;
  272. /* Send ecc_start pulse as well as indicating operation size. */
  273. ocs_ecc_cmd_start(ecc_dev, op_size);
  274. /* Write ax param (Base point (Gx).*/
  275. ocs_ecc_write_cmd_and_data(ecc_dev, op_size, OCS_ECC_INST_WRITE_AX,
  276. scalar_a, nbytes);
  277. /* Write ay param Base point (Gy).*/
  278. ocs_ecc_write_cmd_and_data(ecc_dev, op_size, OCS_ECC_INST_WRITE_AY,
  279. scalar_b, nbytes);
  280. /* Write p = curve prime(GF modulus).*/
  281. ocs_ecc_write_cmd_and_data(ecc_dev, op_size, OCS_ECC_INST_WRITE_P,
  282. curve->p, nbytes);
  283. /* Give instruction A.B or A+B to ECC engine. */
  284. rc = ocs_ecc_trigger_op(ecc_dev, op_size, inst);
  285. if (rc)
  286. return rc;
  287. ocs_ecc_read_cx_out(ecc_dev, scalar_out, nbytes);
  288. if (vli_is_zero(scalar_out, ndigits))
  289. return -EINVAL;
  290. return 0;
  291. }
  292. /* SP800-56A section 5.6.2.3.4 partial verification: ephemeral keys only */
  293. static int kmb_ocs_ecc_is_pubkey_valid_partial(struct ocs_ecc_dev *ecc_dev,
  294. const struct ecc_curve *curve,
  295. struct ecc_point *pk)
  296. {
  297. u64 xxx[KMB_ECC_VLI_MAX_DIGITS] = { 0 };
  298. u64 yy[KMB_ECC_VLI_MAX_DIGITS] = { 0 };
  299. u64 w[KMB_ECC_VLI_MAX_DIGITS] = { 0 };
  300. int rc;
  301. if (WARN_ON(pk->ndigits != curve->g.ndigits))
  302. return -EINVAL;
  303. /* Check 1: Verify key is not the zero point. */
  304. if (ecc_point_is_zero(pk))
  305. return -EINVAL;
  306. /* Check 2: Verify key is in the range [0, p-1]. */
  307. if (vli_cmp(curve->p, pk->x, pk->ndigits) != 1)
  308. return -EINVAL;
  309. if (vli_cmp(curve->p, pk->y, pk->ndigits) != 1)
  310. return -EINVAL;
  311. /* Check 3: Verify that y^2 == (x^3 + a·x + b) mod p */
  312. /* y^2 */
  313. /* Compute y^2 -> store in yy */
  314. rc = kmb_ecc_do_scalar_op(ecc_dev, yy, pk->y, pk->y, curve, pk->ndigits,
  315. OCS_ECC_INST_CALC_A_MUL_B_MODP);
  316. if (rc)
  317. goto exit;
  318. /* x^3 */
  319. /* Assigning w = 3, used for calculating x^3. */
  320. w[0] = POW_CUBE;
  321. /* Load the next stage.*/
  322. rc = kmb_ecc_do_scalar_op(ecc_dev, xxx, pk->x, w, curve, pk->ndigits,
  323. OCS_ECC_INST_CALC_A_POW_B_MODP);
  324. if (rc)
  325. goto exit;
  326. /* Do a*x -> store in w. */
  327. rc = kmb_ecc_do_scalar_op(ecc_dev, w, curve->a, pk->x, curve,
  328. pk->ndigits,
  329. OCS_ECC_INST_CALC_A_MUL_B_MODP);
  330. if (rc)
  331. goto exit;
  332. /* Do ax + b == w + b; store in w. */
  333. rc = kmb_ecc_do_scalar_op(ecc_dev, w, w, curve->b, curve,
  334. pk->ndigits,
  335. OCS_ECC_INST_CALC_A_ADD_B_MODP);
  336. if (rc)
  337. goto exit;
  338. /* x^3 + ax + b == x^3 + w -> store in w. */
  339. rc = kmb_ecc_do_scalar_op(ecc_dev, w, xxx, w, curve, pk->ndigits,
  340. OCS_ECC_INST_CALC_A_ADD_B_MODP);
  341. if (rc)
  342. goto exit;
  343. /* Compare y^2 == x^3 + a·x + b. */
  344. rc = vli_cmp(yy, w, pk->ndigits);
  345. if (rc)
  346. rc = -EINVAL;
  347. exit:
  348. memzero_explicit(xxx, sizeof(xxx));
  349. memzero_explicit(yy, sizeof(yy));
  350. memzero_explicit(w, sizeof(w));
  351. return rc;
  352. }
  353. /* SP800-56A section 5.6.2.3.3 full verification */
  354. static int kmb_ocs_ecc_is_pubkey_valid_full(struct ocs_ecc_dev *ecc_dev,
  355. const struct ecc_curve *curve,
  356. struct ecc_point *pk)
  357. {
  358. struct ecc_point *nQ;
  359. int rc;
  360. /* Checks 1 through 3 */
  361. rc = kmb_ocs_ecc_is_pubkey_valid_partial(ecc_dev, curve, pk);
  362. if (rc)
  363. return rc;
  364. /* Check 4: Verify that nQ is the zero point. */
  365. nQ = ecc_alloc_point(pk->ndigits);
  366. if (!nQ)
  367. return -ENOMEM;
  368. rc = kmb_ecc_point_mult(ecc_dev, nQ, pk, curve->n, curve);
  369. if (rc)
  370. goto exit;
  371. if (!ecc_point_is_zero(nQ))
  372. rc = -EINVAL;
  373. exit:
  374. ecc_free_point(nQ);
  375. return rc;
  376. }
  377. static int kmb_ecc_is_key_valid(const struct ecc_curve *curve,
  378. const u64 *private_key, size_t private_key_len)
  379. {
  380. size_t ndigits = curve->g.ndigits;
  381. u64 one[KMB_ECC_VLI_MAX_DIGITS] = {1};
  382. u64 res[KMB_ECC_VLI_MAX_DIGITS];
  383. if (private_key_len != digits_to_bytes(ndigits))
  384. return -EINVAL;
  385. if (!private_key)
  386. return -EINVAL;
  387. /* Make sure the private key is in the range [2, n-3]. */
  388. if (vli_cmp(one, private_key, ndigits) != -1)
  389. return -EINVAL;
  390. vli_sub(res, curve->n, one, ndigits);
  391. vli_sub(res, res, one, ndigits);
  392. if (vli_cmp(res, private_key, ndigits) != 1)
  393. return -EINVAL;
  394. return 0;
  395. }
  396. /*
  397. * ECC private keys are generated using the method of extra random bits,
  398. * equivalent to that described in FIPS 186-4, Appendix B.4.1.
  399. *
  400. * d = (c mod(n–1)) + 1 where c is a string of random bits, 64 bits longer
  401. * than requested
  402. * 0 <= c mod(n-1) <= n-2 and implies that
  403. * 1 <= d <= n-1
  404. *
  405. * This method generates a private key uniformly distributed in the range
  406. * [1, n-1].
  407. */
  408. static int kmb_ecc_gen_privkey(const struct ecc_curve *curve, u64 *privkey)
  409. {
  410. size_t nbytes = digits_to_bytes(curve->g.ndigits);
  411. u64 priv[KMB_ECC_VLI_MAX_DIGITS];
  412. size_t nbits;
  413. int rc;
  414. nbits = vli_num_bits(curve->n, curve->g.ndigits);
  415. /* Check that N is included in Table 1 of FIPS 186-4, section 6.1.1 */
  416. if (nbits < 160 || curve->g.ndigits > ARRAY_SIZE(priv))
  417. return -EINVAL;
  418. /*
  419. * FIPS 186-4 recommends that the private key should be obtained from a
  420. * RBG with a security strength equal to or greater than the security
  421. * strength associated with N.
  422. *
  423. * The maximum security strength identified by NIST SP800-57pt1r4 for
  424. * ECC is 256 (N >= 512).
  425. *
  426. * This condition is met by the default RNG because it selects a favored
  427. * DRBG with a security strength of 256.
  428. */
  429. if (crypto_get_default_rng())
  430. return -EFAULT;
  431. rc = crypto_rng_get_bytes(crypto_default_rng, (u8 *)priv, nbytes);
  432. crypto_put_default_rng();
  433. if (rc)
  434. goto cleanup;
  435. rc = kmb_ecc_is_key_valid(curve, priv, nbytes);
  436. if (rc)
  437. goto cleanup;
  438. ecc_swap_digits(priv, privkey, curve->g.ndigits);
  439. cleanup:
  440. memzero_explicit(&priv, sizeof(priv));
  441. return rc;
  442. }
  443. static int kmb_ocs_ecdh_set_secret(struct crypto_kpp *tfm, const void *buf,
  444. unsigned int len)
  445. {
  446. struct ocs_ecc_ctx *tctx = kpp_tfm_ctx(tfm);
  447. struct ecdh params;
  448. int rc = 0;
  449. rc = crypto_ecdh_decode_key(buf, len, &params);
  450. if (rc)
  451. goto cleanup;
  452. /* Ensure key size is not bigger then expected. */
  453. if (params.key_size > digits_to_bytes(tctx->curve->g.ndigits)) {
  454. rc = -EINVAL;
  455. goto cleanup;
  456. }
  457. /* Auto-generate private key is not provided. */
  458. if (!params.key || !params.key_size) {
  459. rc = kmb_ecc_gen_privkey(tctx->curve, tctx->private_key);
  460. goto cleanup;
  461. }
  462. rc = kmb_ecc_is_key_valid(tctx->curve, (const u64 *)params.key,
  463. params.key_size);
  464. if (rc)
  465. goto cleanup;
  466. ecc_swap_digits((const u64 *)params.key, tctx->private_key,
  467. tctx->curve->g.ndigits);
  468. cleanup:
  469. memzero_explicit(&params, sizeof(params));
  470. if (rc)
  471. tctx->curve = NULL;
  472. return rc;
  473. }
  474. /* Compute shared secret. */
  475. static int kmb_ecc_do_shared_secret(struct ocs_ecc_ctx *tctx,
  476. struct kpp_request *req)
  477. {
  478. struct ocs_ecc_dev *ecc_dev = tctx->ecc_dev;
  479. const struct ecc_curve *curve = tctx->curve;
  480. u64 shared_secret[KMB_ECC_VLI_MAX_DIGITS];
  481. u64 pubk_buf[KMB_ECC_VLI_MAX_DIGITS * 2];
  482. size_t copied, nbytes, pubk_len;
  483. struct ecc_point *pk, *result;
  484. int rc;
  485. nbytes = digits_to_bytes(curve->g.ndigits);
  486. /* Public key is a point, thus it has two coordinates */
  487. pubk_len = 2 * nbytes;
  488. /* Copy public key from SG list to pubk_buf. */
  489. copied = sg_copy_to_buffer(req->src,
  490. sg_nents_for_len(req->src, pubk_len),
  491. pubk_buf, pubk_len);
  492. if (copied != pubk_len)
  493. return -EINVAL;
  494. /* Allocate and initialize public key point. */
  495. pk = ecc_alloc_point(curve->g.ndigits);
  496. if (!pk)
  497. return -ENOMEM;
  498. ecc_swap_digits(pubk_buf, pk->x, curve->g.ndigits);
  499. ecc_swap_digits(&pubk_buf[curve->g.ndigits], pk->y, curve->g.ndigits);
  500. /*
  501. * Check the public key for following
  502. * Check 1: Verify key is not the zero point.
  503. * Check 2: Verify key is in the range [1, p-1].
  504. * Check 3: Verify that y^2 == (x^3 + a·x + b) mod p
  505. */
  506. rc = kmb_ocs_ecc_is_pubkey_valid_partial(ecc_dev, curve, pk);
  507. if (rc)
  508. goto exit_free_pk;
  509. /* Allocate point for storing computed shared secret. */
  510. result = ecc_alloc_point(pk->ndigits);
  511. if (!result) {
  512. rc = -ENOMEM;
  513. goto exit_free_pk;
  514. }
  515. /* Calculate the shared secret.*/
  516. rc = kmb_ecc_point_mult(ecc_dev, result, pk, tctx->private_key, curve);
  517. if (rc)
  518. goto exit_free_result;
  519. if (ecc_point_is_zero(result)) {
  520. rc = -EFAULT;
  521. goto exit_free_result;
  522. }
  523. /* Copy shared secret from point to buffer. */
  524. ecc_swap_digits(result->x, shared_secret, result->ndigits);
  525. /* Request might ask for less bytes than what we have. */
  526. nbytes = min_t(size_t, nbytes, req->dst_len);
  527. copied = sg_copy_from_buffer(req->dst,
  528. sg_nents_for_len(req->dst, nbytes),
  529. shared_secret, nbytes);
  530. if (copied != nbytes)
  531. rc = -EINVAL;
  532. memzero_explicit(shared_secret, sizeof(shared_secret));
  533. exit_free_result:
  534. ecc_free_point(result);
  535. exit_free_pk:
  536. ecc_free_point(pk);
  537. return rc;
  538. }
  539. /* Compute public key. */
  540. static int kmb_ecc_do_public_key(struct ocs_ecc_ctx *tctx,
  541. struct kpp_request *req)
  542. {
  543. const struct ecc_curve *curve = tctx->curve;
  544. u64 pubk_buf[KMB_ECC_VLI_MAX_DIGITS * 2];
  545. struct ecc_point *pk;
  546. size_t pubk_len;
  547. size_t copied;
  548. int rc;
  549. /* Public key is a point, so it has double the digits. */
  550. pubk_len = 2 * digits_to_bytes(curve->g.ndigits);
  551. pk = ecc_alloc_point(curve->g.ndigits);
  552. if (!pk)
  553. return -ENOMEM;
  554. /* Public Key(pk) = priv * G. */
  555. rc = kmb_ecc_point_mult(tctx->ecc_dev, pk, &curve->g, tctx->private_key,
  556. curve);
  557. if (rc)
  558. goto exit;
  559. /* SP800-56A rev 3 5.6.2.1.3 key check */
  560. if (kmb_ocs_ecc_is_pubkey_valid_full(tctx->ecc_dev, curve, pk)) {
  561. rc = -EAGAIN;
  562. goto exit;
  563. }
  564. /* Copy public key from point to buffer. */
  565. ecc_swap_digits(pk->x, pubk_buf, pk->ndigits);
  566. ecc_swap_digits(pk->y, &pubk_buf[pk->ndigits], pk->ndigits);
  567. /* Copy public key to req->dst. */
  568. copied = sg_copy_from_buffer(req->dst,
  569. sg_nents_for_len(req->dst, pubk_len),
  570. pubk_buf, pubk_len);
  571. if (copied != pubk_len)
  572. rc = -EINVAL;
  573. exit:
  574. ecc_free_point(pk);
  575. return rc;
  576. }
  577. static int kmb_ocs_ecc_do_one_request(struct crypto_engine *engine,
  578. void *areq)
  579. {
  580. struct kpp_request *req = container_of(areq, struct kpp_request, base);
  581. struct ocs_ecc_ctx *tctx = kmb_ocs_ecc_tctx(req);
  582. struct ocs_ecc_dev *ecc_dev = tctx->ecc_dev;
  583. int rc;
  584. if (req->src)
  585. rc = kmb_ecc_do_shared_secret(tctx, req);
  586. else
  587. rc = kmb_ecc_do_public_key(tctx, req);
  588. crypto_finalize_kpp_request(ecc_dev->engine, req, rc);
  589. return 0;
  590. }
  591. static int kmb_ocs_ecdh_generate_public_key(struct kpp_request *req)
  592. {
  593. struct ocs_ecc_ctx *tctx = kmb_ocs_ecc_tctx(req);
  594. const struct ecc_curve *curve = tctx->curve;
  595. /* Ensure kmb_ocs_ecdh_set_secret() has been successfully called. */
  596. if (!tctx->curve)
  597. return -EINVAL;
  598. /* Ensure dst is present. */
  599. if (!req->dst)
  600. return -EINVAL;
  601. /* Check the request dst is big enough to hold the public key. */
  602. if (req->dst_len < (2 * digits_to_bytes(curve->g.ndigits)))
  603. return -EINVAL;
  604. /* 'src' is not supposed to be present when generate pubk is called. */
  605. if (req->src)
  606. return -EINVAL;
  607. return crypto_transfer_kpp_request_to_engine(tctx->ecc_dev->engine,
  608. req);
  609. }
  610. static int kmb_ocs_ecdh_compute_shared_secret(struct kpp_request *req)
  611. {
  612. struct ocs_ecc_ctx *tctx = kmb_ocs_ecc_tctx(req);
  613. const struct ecc_curve *curve = tctx->curve;
  614. /* Ensure kmb_ocs_ecdh_set_secret() has been successfully called. */
  615. if (!tctx->curve)
  616. return -EINVAL;
  617. /* Ensure dst is present. */
  618. if (!req->dst)
  619. return -EINVAL;
  620. /* Ensure src is present. */
  621. if (!req->src)
  622. return -EINVAL;
  623. /*
  624. * req->src is expected to the (other-side) public key, so its length
  625. * must be 2 * coordinate size (in bytes).
  626. */
  627. if (req->src_len != 2 * digits_to_bytes(curve->g.ndigits))
  628. return -EINVAL;
  629. return crypto_transfer_kpp_request_to_engine(tctx->ecc_dev->engine,
  630. req);
  631. }
  632. static int kmb_ecc_tctx_init(struct ocs_ecc_ctx *tctx, unsigned int curve_id)
  633. {
  634. memset(tctx, 0, sizeof(*tctx));
  635. tctx->ecc_dev = kmb_ocs_ecc_find_dev(tctx);
  636. if (IS_ERR(tctx->ecc_dev)) {
  637. pr_err("Failed to find the device : %ld\n",
  638. PTR_ERR(tctx->ecc_dev));
  639. return PTR_ERR(tctx->ecc_dev);
  640. }
  641. tctx->curve = ecc_get_curve(curve_id);
  642. if (!tctx->curve)
  643. return -EOPNOTSUPP;
  644. tctx->engine_ctx.op.prepare_request = NULL;
  645. tctx->engine_ctx.op.do_one_request = kmb_ocs_ecc_do_one_request;
  646. tctx->engine_ctx.op.unprepare_request = NULL;
  647. return 0;
  648. }
  649. static int kmb_ocs_ecdh_nist_p256_init_tfm(struct crypto_kpp *tfm)
  650. {
  651. struct ocs_ecc_ctx *tctx = kpp_tfm_ctx(tfm);
  652. return kmb_ecc_tctx_init(tctx, ECC_CURVE_NIST_P256);
  653. }
  654. static int kmb_ocs_ecdh_nist_p384_init_tfm(struct crypto_kpp *tfm)
  655. {
  656. struct ocs_ecc_ctx *tctx = kpp_tfm_ctx(tfm);
  657. return kmb_ecc_tctx_init(tctx, ECC_CURVE_NIST_P384);
  658. }
  659. static void kmb_ocs_ecdh_exit_tfm(struct crypto_kpp *tfm)
  660. {
  661. struct ocs_ecc_ctx *tctx = kpp_tfm_ctx(tfm);
  662. memzero_explicit(tctx->private_key, sizeof(*tctx->private_key));
  663. }
  664. static unsigned int kmb_ocs_ecdh_max_size(struct crypto_kpp *tfm)
  665. {
  666. struct ocs_ecc_ctx *tctx = kpp_tfm_ctx(tfm);
  667. /* Public key is made of two coordinates, so double the digits. */
  668. return digits_to_bytes(tctx->curve->g.ndigits) * 2;
  669. }
  670. static struct kpp_alg ocs_ecdh_p256 = {
  671. .set_secret = kmb_ocs_ecdh_set_secret,
  672. .generate_public_key = kmb_ocs_ecdh_generate_public_key,
  673. .compute_shared_secret = kmb_ocs_ecdh_compute_shared_secret,
  674. .init = kmb_ocs_ecdh_nist_p256_init_tfm,
  675. .exit = kmb_ocs_ecdh_exit_tfm,
  676. .max_size = kmb_ocs_ecdh_max_size,
  677. .base = {
  678. .cra_name = "ecdh-nist-p256",
  679. .cra_driver_name = "ecdh-nist-p256-keembay-ocs",
  680. .cra_priority = KMB_OCS_ECC_PRIORITY,
  681. .cra_module = THIS_MODULE,
  682. .cra_ctxsize = sizeof(struct ocs_ecc_ctx),
  683. },
  684. };
  685. static struct kpp_alg ocs_ecdh_p384 = {
  686. .set_secret = kmb_ocs_ecdh_set_secret,
  687. .generate_public_key = kmb_ocs_ecdh_generate_public_key,
  688. .compute_shared_secret = kmb_ocs_ecdh_compute_shared_secret,
  689. .init = kmb_ocs_ecdh_nist_p384_init_tfm,
  690. .exit = kmb_ocs_ecdh_exit_tfm,
  691. .max_size = kmb_ocs_ecdh_max_size,
  692. .base = {
  693. .cra_name = "ecdh-nist-p384",
  694. .cra_driver_name = "ecdh-nist-p384-keembay-ocs",
  695. .cra_priority = KMB_OCS_ECC_PRIORITY,
  696. .cra_module = THIS_MODULE,
  697. .cra_ctxsize = sizeof(struct ocs_ecc_ctx),
  698. },
  699. };
  700. static irqreturn_t ocs_ecc_irq_handler(int irq, void *dev_id)
  701. {
  702. struct ocs_ecc_dev *ecc_dev = dev_id;
  703. u32 status;
  704. /*
  705. * Read the status register and write it back to clear the
  706. * DONE_INT_STATUS bit.
  707. */
  708. status = ioread32(ecc_dev->base_reg + HW_OFFS_OCS_ECC_ISR);
  709. iowrite32(status, ecc_dev->base_reg + HW_OFFS_OCS_ECC_ISR);
  710. if (!(status & HW_OCS_ECC_ISR_INT_STATUS_DONE))
  711. return IRQ_NONE;
  712. complete(&ecc_dev->irq_done);
  713. return IRQ_HANDLED;
  714. }
  715. static int kmb_ocs_ecc_probe(struct platform_device *pdev)
  716. {
  717. struct device *dev = &pdev->dev;
  718. struct ocs_ecc_dev *ecc_dev;
  719. int rc;
  720. ecc_dev = devm_kzalloc(dev, sizeof(*ecc_dev), GFP_KERNEL);
  721. if (!ecc_dev)
  722. return -ENOMEM;
  723. ecc_dev->dev = dev;
  724. platform_set_drvdata(pdev, ecc_dev);
  725. INIT_LIST_HEAD(&ecc_dev->list);
  726. init_completion(&ecc_dev->irq_done);
  727. /* Get base register address. */
  728. ecc_dev->base_reg = devm_platform_ioremap_resource(pdev, 0);
  729. if (IS_ERR(ecc_dev->base_reg)) {
  730. dev_err(dev, "Failed to get base address\n");
  731. rc = PTR_ERR(ecc_dev->base_reg);
  732. goto list_del;
  733. }
  734. /* Get and request IRQ */
  735. ecc_dev->irq = platform_get_irq(pdev, 0);
  736. if (ecc_dev->irq < 0) {
  737. rc = ecc_dev->irq;
  738. goto list_del;
  739. }
  740. rc = devm_request_threaded_irq(dev, ecc_dev->irq, ocs_ecc_irq_handler,
  741. NULL, 0, "keembay-ocs-ecc", ecc_dev);
  742. if (rc < 0) {
  743. dev_err(dev, "Could not request IRQ\n");
  744. goto list_del;
  745. }
  746. /* Add device to the list of OCS ECC devices. */
  747. spin_lock(&ocs_ecc.lock);
  748. list_add_tail(&ecc_dev->list, &ocs_ecc.dev_list);
  749. spin_unlock(&ocs_ecc.lock);
  750. /* Initialize crypto engine. */
  751. ecc_dev->engine = crypto_engine_alloc_init(dev, 1);
  752. if (!ecc_dev->engine) {
  753. dev_err(dev, "Could not allocate crypto engine\n");
  754. rc = -ENOMEM;
  755. goto list_del;
  756. }
  757. rc = crypto_engine_start(ecc_dev->engine);
  758. if (rc) {
  759. dev_err(dev, "Could not start crypto engine\n");
  760. goto cleanup;
  761. }
  762. /* Register the KPP algo. */
  763. rc = crypto_register_kpp(&ocs_ecdh_p256);
  764. if (rc) {
  765. dev_err(dev,
  766. "Could not register OCS algorithms with Crypto API\n");
  767. goto cleanup;
  768. }
  769. rc = crypto_register_kpp(&ocs_ecdh_p384);
  770. if (rc) {
  771. dev_err(dev,
  772. "Could not register OCS algorithms with Crypto API\n");
  773. goto ocs_ecdh_p384_error;
  774. }
  775. return 0;
  776. ocs_ecdh_p384_error:
  777. crypto_unregister_kpp(&ocs_ecdh_p256);
  778. cleanup:
  779. crypto_engine_exit(ecc_dev->engine);
  780. list_del:
  781. spin_lock(&ocs_ecc.lock);
  782. list_del(&ecc_dev->list);
  783. spin_unlock(&ocs_ecc.lock);
  784. return rc;
  785. }
  786. static int kmb_ocs_ecc_remove(struct platform_device *pdev)
  787. {
  788. struct ocs_ecc_dev *ecc_dev;
  789. ecc_dev = platform_get_drvdata(pdev);
  790. crypto_unregister_kpp(&ocs_ecdh_p384);
  791. crypto_unregister_kpp(&ocs_ecdh_p256);
  792. spin_lock(&ocs_ecc.lock);
  793. list_del(&ecc_dev->list);
  794. spin_unlock(&ocs_ecc.lock);
  795. crypto_engine_exit(ecc_dev->engine);
  796. return 0;
  797. }
  798. /* Device tree driver match. */
  799. static const struct of_device_id kmb_ocs_ecc_of_match[] = {
  800. {
  801. .compatible = "intel,keembay-ocs-ecc",
  802. },
  803. {}
  804. };
  805. /* The OCS driver is a platform device. */
  806. static struct platform_driver kmb_ocs_ecc_driver = {
  807. .probe = kmb_ocs_ecc_probe,
  808. .remove = kmb_ocs_ecc_remove,
  809. .driver = {
  810. .name = DRV_NAME,
  811. .of_match_table = kmb_ocs_ecc_of_match,
  812. },
  813. };
  814. module_platform_driver(kmb_ocs_ecc_driver);
  815. MODULE_LICENSE("GPL");
  816. MODULE_DESCRIPTION("Intel Keem Bay OCS ECC Driver");
  817. MODULE_ALIAS_CRYPTO("ecdh-nist-p256");
  818. MODULE_ALIAS_CRYPTO("ecdh-nist-p384");
  819. MODULE_ALIAS_CRYPTO("ecdh-nist-p256-keembay-ocs");
  820. MODULE_ALIAS_CRYPTO("ecdh-nist-p384-keembay-ocs");