ccp.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. /*
  2. * AMD Cryptographic Coprocessor (CCP) driver
  3. *
  4. * Copyright (C) 2013 Advanced Micro Devices, Inc.
  5. *
  6. * Author: Tom Lendacky <thomas.lendacky@amd.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #ifndef __CPP_H__
  13. #define __CPP_H__
  14. #include <linux/scatterlist.h>
  15. #include <linux/workqueue.h>
  16. #include <linux/list.h>
  17. #include <crypto/aes.h>
  18. #include <crypto/sha.h>
  19. struct ccp_device;
  20. struct ccp_cmd;
  21. #if defined(CONFIG_CRYPTO_DEV_CCP_DD) || \
  22. defined(CONFIG_CRYPTO_DEV_CCP_DD_MODULE)
  23. /**
  24. * ccp_present - check if a CCP device is present
  25. *
  26. * Returns zero if a CCP device is present, -ENODEV otherwise.
  27. */
  28. int ccp_present(void);
  29. /**
  30. * ccp_enqueue_cmd - queue an operation for processing by the CCP
  31. *
  32. * @cmd: ccp_cmd struct to be processed
  33. *
  34. * Refer to the ccp_cmd struct below for required fields.
  35. *
  36. * Queue a cmd to be processed by the CCP. If queueing the cmd
  37. * would exceed the defined length of the cmd queue the cmd will
  38. * only be queued if the CCP_CMD_MAY_BACKLOG flag is set and will
  39. * result in a return code of -EBUSY.
  40. *
  41. * The callback routine specified in the ccp_cmd struct will be
  42. * called to notify the caller of completion (if the cmd was not
  43. * backlogged) or advancement out of the backlog. If the cmd has
  44. * advanced out of the backlog the "err" value of the callback
  45. * will be -EINPROGRESS. Any other "err" value during callback is
  46. * the result of the operation.
  47. *
  48. * The cmd has been successfully queued if:
  49. * the return code is -EINPROGRESS or
  50. * the return code is -EBUSY and CCP_CMD_MAY_BACKLOG flag is set
  51. */
  52. int ccp_enqueue_cmd(struct ccp_cmd *cmd);
  53. #else /* CONFIG_CRYPTO_DEV_CCP_DD is not enabled */
  54. static inline int ccp_present(void)
  55. {
  56. return -ENODEV;
  57. }
  58. static inline int ccp_enqueue_cmd(struct ccp_cmd *cmd)
  59. {
  60. return -ENODEV;
  61. }
  62. #endif /* CONFIG_CRYPTO_DEV_CCP_DD */
  63. /***** AES engine *****/
  64. /**
  65. * ccp_aes_type - AES key size
  66. *
  67. * @CCP_AES_TYPE_128: 128-bit key
  68. * @CCP_AES_TYPE_192: 192-bit key
  69. * @CCP_AES_TYPE_256: 256-bit key
  70. */
  71. enum ccp_aes_type {
  72. CCP_AES_TYPE_128 = 0,
  73. CCP_AES_TYPE_192,
  74. CCP_AES_TYPE_256,
  75. CCP_AES_TYPE__LAST,
  76. };
  77. /**
  78. * ccp_aes_mode - AES operation mode
  79. *
  80. * @CCP_AES_MODE_ECB: ECB mode
  81. * @CCP_AES_MODE_CBC: CBC mode
  82. * @CCP_AES_MODE_OFB: OFB mode
  83. * @CCP_AES_MODE_CFB: CFB mode
  84. * @CCP_AES_MODE_CTR: CTR mode
  85. * @CCP_AES_MODE_CMAC: CMAC mode
  86. */
  87. enum ccp_aes_mode {
  88. CCP_AES_MODE_ECB = 0,
  89. CCP_AES_MODE_CBC,
  90. CCP_AES_MODE_OFB,
  91. CCP_AES_MODE_CFB,
  92. CCP_AES_MODE_CTR,
  93. CCP_AES_MODE_CMAC,
  94. CCP_AES_MODE__LAST,
  95. };
  96. /**
  97. * ccp_aes_mode - AES operation mode
  98. *
  99. * @CCP_AES_ACTION_DECRYPT: AES decrypt operation
  100. * @CCP_AES_ACTION_ENCRYPT: AES encrypt operation
  101. */
  102. enum ccp_aes_action {
  103. CCP_AES_ACTION_DECRYPT = 0,
  104. CCP_AES_ACTION_ENCRYPT,
  105. CCP_AES_ACTION__LAST,
  106. };
  107. /**
  108. * struct ccp_aes_engine - CCP AES operation
  109. * @type: AES operation key size
  110. * @mode: AES operation mode
  111. * @action: AES operation (decrypt/encrypt)
  112. * @key: key to be used for this AES operation
  113. * @key_len: length in bytes of key
  114. * @iv: IV to be used for this AES operation
  115. * @iv_len: length in bytes of iv
  116. * @src: data to be used for this operation
  117. * @dst: data produced by this operation
  118. * @src_len: length in bytes of data used for this operation
  119. * @cmac_final: indicates final operation when running in CMAC mode
  120. * @cmac_key: K1/K2 key used in final CMAC operation
  121. * @cmac_key_len: length in bytes of cmac_key
  122. *
  123. * Variables required to be set when calling ccp_enqueue_cmd():
  124. * - type, mode, action, key, key_len, src, dst, src_len
  125. * - iv, iv_len for any mode other than ECB
  126. * - cmac_final for CMAC mode
  127. * - cmac_key, cmac_key_len for CMAC mode if cmac_final is non-zero
  128. *
  129. * The iv variable is used as both input and output. On completion of the
  130. * AES operation the new IV overwrites the old IV.
  131. */
  132. struct ccp_aes_engine {
  133. enum ccp_aes_type type;
  134. enum ccp_aes_mode mode;
  135. enum ccp_aes_action action;
  136. struct scatterlist *key;
  137. u32 key_len; /* In bytes */
  138. struct scatterlist *iv;
  139. u32 iv_len; /* In bytes */
  140. struct scatterlist *src, *dst;
  141. u64 src_len; /* In bytes */
  142. u32 cmac_final; /* Indicates final cmac cmd */
  143. struct scatterlist *cmac_key; /* K1/K2 cmac key required for
  144. * final cmac cmd */
  145. u32 cmac_key_len; /* In bytes */
  146. };
  147. /***** XTS-AES engine *****/
  148. /**
  149. * ccp_xts_aes_unit_size - XTS unit size
  150. *
  151. * @CCP_XTS_AES_UNIT_SIZE_16: Unit size of 16 bytes
  152. * @CCP_XTS_AES_UNIT_SIZE_512: Unit size of 512 bytes
  153. * @CCP_XTS_AES_UNIT_SIZE_1024: Unit size of 1024 bytes
  154. * @CCP_XTS_AES_UNIT_SIZE_2048: Unit size of 2048 bytes
  155. * @CCP_XTS_AES_UNIT_SIZE_4096: Unit size of 4096 bytes
  156. */
  157. enum ccp_xts_aes_unit_size {
  158. CCP_XTS_AES_UNIT_SIZE_16 = 0,
  159. CCP_XTS_AES_UNIT_SIZE_512,
  160. CCP_XTS_AES_UNIT_SIZE_1024,
  161. CCP_XTS_AES_UNIT_SIZE_2048,
  162. CCP_XTS_AES_UNIT_SIZE_4096,
  163. CCP_XTS_AES_UNIT_SIZE__LAST,
  164. };
  165. /**
  166. * struct ccp_xts_aes_engine - CCP XTS AES operation
  167. * @action: AES operation (decrypt/encrypt)
  168. * @unit_size: unit size of the XTS operation
  169. * @key: key to be used for this XTS AES operation
  170. * @key_len: length in bytes of key
  171. * @iv: IV to be used for this XTS AES operation
  172. * @iv_len: length in bytes of iv
  173. * @src: data to be used for this operation
  174. * @dst: data produced by this operation
  175. * @src_len: length in bytes of data used for this operation
  176. * @final: indicates final XTS operation
  177. *
  178. * Variables required to be set when calling ccp_enqueue_cmd():
  179. * - action, unit_size, key, key_len, iv, iv_len, src, dst, src_len, final
  180. *
  181. * The iv variable is used as both input and output. On completion of the
  182. * AES operation the new IV overwrites the old IV.
  183. */
  184. struct ccp_xts_aes_engine {
  185. enum ccp_aes_action action;
  186. enum ccp_xts_aes_unit_size unit_size;
  187. struct scatterlist *key;
  188. u32 key_len; /* In bytes */
  189. struct scatterlist *iv;
  190. u32 iv_len; /* In bytes */
  191. struct scatterlist *src, *dst;
  192. u64 src_len; /* In bytes */
  193. u32 final;
  194. };
  195. /***** SHA engine *****/
  196. #define CCP_SHA_BLOCKSIZE SHA256_BLOCK_SIZE
  197. #define CCP_SHA_CTXSIZE SHA256_DIGEST_SIZE
  198. /**
  199. * ccp_sha_type - type of SHA operation
  200. *
  201. * @CCP_SHA_TYPE_1: SHA-1 operation
  202. * @CCP_SHA_TYPE_224: SHA-224 operation
  203. * @CCP_SHA_TYPE_256: SHA-256 operation
  204. */
  205. enum ccp_sha_type {
  206. CCP_SHA_TYPE_1 = 1,
  207. CCP_SHA_TYPE_224,
  208. CCP_SHA_TYPE_256,
  209. CCP_SHA_TYPE__LAST,
  210. };
  211. /**
  212. * struct ccp_sha_engine - CCP SHA operation
  213. * @type: Type of SHA operation
  214. * @ctx: current hash value
  215. * @ctx_len: length in bytes of hash value
  216. * @src: data to be used for this operation
  217. * @src_len: length in bytes of data used for this operation
  218. * @opad: data to be used for final HMAC operation
  219. * @opad_len: length in bytes of data used for final HMAC operation
  220. * @first: indicates first SHA operation
  221. * @final: indicates final SHA operation
  222. * @msg_bits: total length of the message in bits used in final SHA operation
  223. *
  224. * Variables required to be set when calling ccp_enqueue_cmd():
  225. * - type, ctx, ctx_len, src, src_len, final
  226. * - msg_bits if final is non-zero
  227. *
  228. * The ctx variable is used as both input and output. On completion of the
  229. * SHA operation the new hash value overwrites the old hash value.
  230. */
  231. struct ccp_sha_engine {
  232. enum ccp_sha_type type;
  233. struct scatterlist *ctx;
  234. u32 ctx_len; /* In bytes */
  235. struct scatterlist *src;
  236. u64 src_len; /* In bytes */
  237. struct scatterlist *opad;
  238. u32 opad_len; /* In bytes */
  239. u32 first; /* Indicates first sha cmd */
  240. u32 final; /* Indicates final sha cmd */
  241. u64 msg_bits; /* Message length in bits required for
  242. * final sha cmd */
  243. };
  244. /***** RSA engine *****/
  245. /**
  246. * struct ccp_rsa_engine - CCP RSA operation
  247. * @key_size: length in bits of RSA key
  248. * @exp: RSA exponent
  249. * @exp_len: length in bytes of exponent
  250. * @mod: RSA modulus
  251. * @mod_len: length in bytes of modulus
  252. * @src: data to be used for this operation
  253. * @dst: data produced by this operation
  254. * @src_len: length in bytes of data used for this operation
  255. *
  256. * Variables required to be set when calling ccp_enqueue_cmd():
  257. * - key_size, exp, exp_len, mod, mod_len, src, dst, src_len
  258. */
  259. struct ccp_rsa_engine {
  260. u32 key_size; /* In bits */
  261. struct scatterlist *exp;
  262. u32 exp_len; /* In bytes */
  263. struct scatterlist *mod;
  264. u32 mod_len; /* In bytes */
  265. struct scatterlist *src, *dst;
  266. u32 src_len; /* In bytes */
  267. };
  268. /***** Passthru engine *****/
  269. /**
  270. * ccp_passthru_bitwise - type of bitwise passthru operation
  271. *
  272. * @CCP_PASSTHRU_BITWISE_NOOP: no bitwise operation performed
  273. * @CCP_PASSTHRU_BITWISE_AND: perform bitwise AND of src with mask
  274. * @CCP_PASSTHRU_BITWISE_OR: perform bitwise OR of src with mask
  275. * @CCP_PASSTHRU_BITWISE_XOR: perform bitwise XOR of src with mask
  276. * @CCP_PASSTHRU_BITWISE_MASK: overwrite with mask
  277. */
  278. enum ccp_passthru_bitwise {
  279. CCP_PASSTHRU_BITWISE_NOOP = 0,
  280. CCP_PASSTHRU_BITWISE_AND,
  281. CCP_PASSTHRU_BITWISE_OR,
  282. CCP_PASSTHRU_BITWISE_XOR,
  283. CCP_PASSTHRU_BITWISE_MASK,
  284. CCP_PASSTHRU_BITWISE__LAST,
  285. };
  286. /**
  287. * ccp_passthru_byteswap - type of byteswap passthru operation
  288. *
  289. * @CCP_PASSTHRU_BYTESWAP_NOOP: no byte swapping performed
  290. * @CCP_PASSTHRU_BYTESWAP_32BIT: swap bytes within 32-bit words
  291. * @CCP_PASSTHRU_BYTESWAP_256BIT: swap bytes within 256-bit words
  292. */
  293. enum ccp_passthru_byteswap {
  294. CCP_PASSTHRU_BYTESWAP_NOOP = 0,
  295. CCP_PASSTHRU_BYTESWAP_32BIT,
  296. CCP_PASSTHRU_BYTESWAP_256BIT,
  297. CCP_PASSTHRU_BYTESWAP__LAST,
  298. };
  299. /**
  300. * struct ccp_passthru_engine - CCP pass-through operation
  301. * @bit_mod: bitwise operation to perform
  302. * @byte_swap: byteswap operation to perform
  303. * @mask: mask to be applied to data
  304. * @mask_len: length in bytes of mask
  305. * @src: data to be used for this operation
  306. * @dst: data produced by this operation
  307. * @src_len: length in bytes of data used for this operation
  308. * @final: indicate final pass-through operation
  309. *
  310. * Variables required to be set when calling ccp_enqueue_cmd():
  311. * - bit_mod, byte_swap, src, dst, src_len
  312. * - mask, mask_len if bit_mod is not CCP_PASSTHRU_BITWISE_NOOP
  313. */
  314. struct ccp_passthru_engine {
  315. enum ccp_passthru_bitwise bit_mod;
  316. enum ccp_passthru_byteswap byte_swap;
  317. struct scatterlist *mask;
  318. u32 mask_len; /* In bytes */
  319. struct scatterlist *src, *dst;
  320. u64 src_len; /* In bytes */
  321. u32 final;
  322. };
  323. /***** ECC engine *****/
  324. #define CCP_ECC_MODULUS_BYTES 48 /* 384-bits */
  325. #define CCP_ECC_MAX_OPERANDS 6
  326. #define CCP_ECC_MAX_OUTPUTS 3
  327. /**
  328. * ccp_ecc_function - type of ECC function
  329. *
  330. * @CCP_ECC_FUNCTION_MMUL_384BIT: 384-bit modular multiplication
  331. * @CCP_ECC_FUNCTION_MADD_384BIT: 384-bit modular addition
  332. * @CCP_ECC_FUNCTION_MINV_384BIT: 384-bit multiplicative inverse
  333. * @CCP_ECC_FUNCTION_PADD_384BIT: 384-bit point addition
  334. * @CCP_ECC_FUNCTION_PMUL_384BIT: 384-bit point multiplication
  335. * @CCP_ECC_FUNCTION_PDBL_384BIT: 384-bit point doubling
  336. */
  337. enum ccp_ecc_function {
  338. CCP_ECC_FUNCTION_MMUL_384BIT = 0,
  339. CCP_ECC_FUNCTION_MADD_384BIT,
  340. CCP_ECC_FUNCTION_MINV_384BIT,
  341. CCP_ECC_FUNCTION_PADD_384BIT,
  342. CCP_ECC_FUNCTION_PMUL_384BIT,
  343. CCP_ECC_FUNCTION_PDBL_384BIT,
  344. };
  345. /**
  346. * struct ccp_ecc_modular_math - CCP ECC modular math parameters
  347. * @operand_1: first operand for the modular math operation
  348. * @operand_1_len: length of the first operand
  349. * @operand_2: second operand for the modular math operation
  350. * (not used for CCP_ECC_FUNCTION_MINV_384BIT)
  351. * @operand_2_len: length of the second operand
  352. * (not used for CCP_ECC_FUNCTION_MINV_384BIT)
  353. * @result: result of the modular math operation
  354. * @result_len: length of the supplied result buffer
  355. */
  356. struct ccp_ecc_modular_math {
  357. struct scatterlist *operand_1;
  358. unsigned int operand_1_len; /* In bytes */
  359. struct scatterlist *operand_2;
  360. unsigned int operand_2_len; /* In bytes */
  361. struct scatterlist *result;
  362. unsigned int result_len; /* In bytes */
  363. };
  364. /**
  365. * struct ccp_ecc_point - CCP ECC point definition
  366. * @x: the x coordinate of the ECC point
  367. * @x_len: the length of the x coordinate
  368. * @y: the y coordinate of the ECC point
  369. * @y_len: the length of the y coordinate
  370. */
  371. struct ccp_ecc_point {
  372. struct scatterlist *x;
  373. unsigned int x_len; /* In bytes */
  374. struct scatterlist *y;
  375. unsigned int y_len; /* In bytes */
  376. };
  377. /**
  378. * struct ccp_ecc_point_math - CCP ECC point math parameters
  379. * @point_1: the first point of the ECC point math operation
  380. * @point_2: the second point of the ECC point math operation
  381. * (only used for CCP_ECC_FUNCTION_PADD_384BIT)
  382. * @domain_a: the a parameter of the ECC curve
  383. * @domain_a_len: the length of the a parameter
  384. * @scalar: the scalar parameter for the point match operation
  385. * (only used for CCP_ECC_FUNCTION_PMUL_384BIT)
  386. * @scalar_len: the length of the scalar parameter
  387. * (only used for CCP_ECC_FUNCTION_PMUL_384BIT)
  388. * @result: the point resulting from the point math operation
  389. */
  390. struct ccp_ecc_point_math {
  391. struct ccp_ecc_point point_1;
  392. struct ccp_ecc_point point_2;
  393. struct scatterlist *domain_a;
  394. unsigned int domain_a_len; /* In bytes */
  395. struct scatterlist *scalar;
  396. unsigned int scalar_len; /* In bytes */
  397. struct ccp_ecc_point result;
  398. };
  399. /**
  400. * struct ccp_ecc_engine - CCP ECC operation
  401. * @function: ECC function to perform
  402. * @mod: ECC modulus
  403. * @mod_len: length in bytes of modulus
  404. * @mm: module math parameters
  405. * @pm: point math parameters
  406. * @ecc_result: result of the ECC operation
  407. *
  408. * Variables required to be set when calling ccp_enqueue_cmd():
  409. * - function, mod, mod_len
  410. * - operand, operand_len, operand_count, output, output_len, output_count
  411. * - ecc_result
  412. */
  413. struct ccp_ecc_engine {
  414. enum ccp_ecc_function function;
  415. struct scatterlist *mod;
  416. u32 mod_len; /* In bytes */
  417. union {
  418. struct ccp_ecc_modular_math mm;
  419. struct ccp_ecc_point_math pm;
  420. } u;
  421. u16 ecc_result;
  422. };
  423. /**
  424. * ccp_engine - CCP operation identifiers
  425. *
  426. * @CCP_ENGINE_AES: AES operation
  427. * @CCP_ENGINE_XTS_AES: 128-bit XTS AES operation
  428. * @CCP_ENGINE_RSVD1: unused
  429. * @CCP_ENGINE_SHA: SHA operation
  430. * @CCP_ENGINE_RSA: RSA operation
  431. * @CCP_ENGINE_PASSTHRU: pass-through operation
  432. * @CCP_ENGINE_ZLIB_DECOMPRESS: unused
  433. * @CCP_ENGINE_ECC: ECC operation
  434. */
  435. enum ccp_engine {
  436. CCP_ENGINE_AES = 0,
  437. CCP_ENGINE_XTS_AES_128,
  438. CCP_ENGINE_RSVD1,
  439. CCP_ENGINE_SHA,
  440. CCP_ENGINE_RSA,
  441. CCP_ENGINE_PASSTHRU,
  442. CCP_ENGINE_ZLIB_DECOMPRESS,
  443. CCP_ENGINE_ECC,
  444. CCP_ENGINE__LAST,
  445. };
  446. /* Flag values for flags member of ccp_cmd */
  447. #define CCP_CMD_MAY_BACKLOG 0x00000001
  448. /**
  449. * struct ccp_cmd - CPP operation request
  450. * @entry: list element (ccp driver use only)
  451. * @work: work element used for callbacks (ccp driver use only)
  452. * @ccp: CCP device to be run on (ccp driver use only)
  453. * @ret: operation return code (ccp driver use only)
  454. * @flags: cmd processing flags
  455. * @engine: CCP operation to perform
  456. * @engine_error: CCP engine return code
  457. * @u: engine specific structures, refer to specific engine struct below
  458. * @callback: operation completion callback function
  459. * @data: parameter value to be supplied to the callback function
  460. *
  461. * Variables required to be set when calling ccp_enqueue_cmd():
  462. * - engine, callback
  463. * - See the operation structures below for what is required for each
  464. * operation.
  465. */
  466. struct ccp_cmd {
  467. /* The list_head, work_struct, ccp and ret variables are for use
  468. * by the CCP driver only.
  469. */
  470. struct list_head entry;
  471. struct work_struct work;
  472. struct ccp_device *ccp;
  473. int ret;
  474. u32 flags;
  475. enum ccp_engine engine;
  476. u32 engine_error;
  477. union {
  478. struct ccp_aes_engine aes;
  479. struct ccp_xts_aes_engine xts;
  480. struct ccp_sha_engine sha;
  481. struct ccp_rsa_engine rsa;
  482. struct ccp_passthru_engine passthru;
  483. struct ccp_ecc_engine ecc;
  484. } u;
  485. /* Completion callback support */
  486. void (*callback)(void *data, int err);
  487. void *data;
  488. };
  489. #endif