ccp.h 15 KB

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