sa2ul.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * K3 SA2UL crypto accelerator driver
  4. *
  5. * Copyright (C) 2018-2020 Texas Instruments Incorporated - http://www.ti.com
  6. *
  7. * Authors: Keerthy
  8. * Vitaly Andrianov
  9. * Tero Kristo
  10. */
  11. #ifndef _K3_SA2UL_
  12. #define _K3_SA2UL_
  13. #include <linux/interrupt.h>
  14. #include <linux/skbuff.h>
  15. #include <linux/hw_random.h>
  16. #include <crypto/aes.h>
  17. #define SA_ENGINE_ENABLE_CONTROL 0x1000
  18. struct sa_tfm_ctx;
  19. /*
  20. * SA_ENGINE_ENABLE_CONTROL register bits
  21. */
  22. #define SA_EEC_ENCSS_EN 0x00000001
  23. #define SA_EEC_AUTHSS_EN 0x00000002
  24. #define SA_EEC_TRNG_EN 0x00000008
  25. #define SA_EEC_PKA_EN 0x00000010
  26. #define SA_EEC_CTXCACH_EN 0x00000080
  27. #define SA_EEC_CPPI_PORT_IN_EN 0x00000200
  28. #define SA_EEC_CPPI_PORT_OUT_EN 0x00000800
  29. /*
  30. * Encoding used to identify the typo of crypto operation
  31. * performed on the packet when the packet is returned
  32. * by SA
  33. */
  34. #define SA_REQ_SUBTYPE_ENC 0x0001
  35. #define SA_REQ_SUBTYPE_DEC 0x0002
  36. #define SA_REQ_SUBTYPE_SHIFT 16
  37. #define SA_REQ_SUBTYPE_MASK 0xffff
  38. /* Number of 32 bit words in EPIB */
  39. #define SA_DMA_NUM_EPIB_WORDS 4
  40. /* Number of 32 bit words in PS data */
  41. #define SA_DMA_NUM_PS_WORDS 16
  42. #define NKEY_SZ 3
  43. #define MCI_SZ 27
  44. /*
  45. * Maximum number of simultaeneous security contexts
  46. * supported by the driver
  47. */
  48. #define SA_MAX_NUM_CTX 512
  49. /*
  50. * Assumption: CTX size is multiple of 32
  51. */
  52. #define SA_CTX_SIZE_TO_DMA_SIZE(ctx_sz) \
  53. ((ctx_sz) ? ((ctx_sz) / 32 - 1) : 0)
  54. #define SA_CTX_ENC_KEY_OFFSET 32
  55. #define SA_CTX_ENC_AUX1_OFFSET 64
  56. #define SA_CTX_ENC_AUX2_OFFSET 96
  57. #define SA_CTX_ENC_AUX3_OFFSET 112
  58. #define SA_CTX_ENC_AUX4_OFFSET 128
  59. /* Next Engine Select code in CP_ACE */
  60. #define SA_ENG_ID_EM1 2 /* Enc/Dec engine with AES/DEC core */
  61. #define SA_ENG_ID_EM2 3 /* Encryption/Decryption enginefor pass 2 */
  62. #define SA_ENG_ID_AM1 4 /* Auth. engine with SHA1/MD5/SHA2 core */
  63. #define SA_ENG_ID_AM2 5 /* Authentication engine for pass 2 */
  64. #define SA_ENG_ID_OUTPORT2 20 /* Egress module 2 */
  65. /*
  66. * Command Label Definitions
  67. */
  68. #define SA_CMDL_OFFSET_NESC 0 /* Next Engine Select Code */
  69. #define SA_CMDL_OFFSET_LABEL_LEN 1 /* Engine Command Label Length */
  70. /* 16-bit Length of Data to be processed */
  71. #define SA_CMDL_OFFSET_DATA_LEN 2
  72. #define SA_CMDL_OFFSET_DATA_OFFSET 4 /* Stat Data Offset */
  73. #define SA_CMDL_OFFSET_OPTION_CTRL1 5 /* Option Control Byte 1 */
  74. #define SA_CMDL_OFFSET_OPTION_CTRL2 6 /* Option Control Byte 2 */
  75. #define SA_CMDL_OFFSET_OPTION_CTRL3 7 /* Option Control Byte 3 */
  76. #define SA_CMDL_OFFSET_OPTION_BYTE 8
  77. #define SA_CMDL_HEADER_SIZE_BYTES 8
  78. #define SA_CMDL_OPTION_BYTES_MAX_SIZE 72
  79. #define SA_CMDL_MAX_SIZE_BYTES (SA_CMDL_HEADER_SIZE_BYTES + \
  80. SA_CMDL_OPTION_BYTES_MAX_SIZE)
  81. /* SWINFO word-0 flags */
  82. #define SA_SW_INFO_FLAG_EVICT 0x0001
  83. #define SA_SW_INFO_FLAG_TEAR 0x0002
  84. #define SA_SW_INFO_FLAG_NOPD 0x0004
  85. /*
  86. * This type represents the various packet types to be processed
  87. * by the PHP engine in SA.
  88. * It is used to identify the corresponding PHP processing function.
  89. */
  90. #define SA_CTX_PE_PKT_TYPE_3GPP_AIR 0 /* 3GPP Air Cipher */
  91. #define SA_CTX_PE_PKT_TYPE_SRTP 1 /* SRTP */
  92. #define SA_CTX_PE_PKT_TYPE_IPSEC_AH 2 /* IPSec Authentication Header */
  93. /* IPSec Encapsulating Security Payload */
  94. #define SA_CTX_PE_PKT_TYPE_IPSEC_ESP 3
  95. /* Indicates that it is in data mode, It may not be used by PHP */
  96. #define SA_CTX_PE_PKT_TYPE_NONE 4
  97. #define SA_CTX_ENC_TYPE1_SZ 64 /* Encryption SC with Key only */
  98. #define SA_CTX_ENC_TYPE2_SZ 96 /* Encryption SC with Key and Aux1 */
  99. #define SA_CTX_AUTH_TYPE1_SZ 64 /* Auth SC with Key only */
  100. #define SA_CTX_AUTH_TYPE2_SZ 96 /* Auth SC with Key and Aux1 */
  101. /* Size of security context for PHP engine */
  102. #define SA_CTX_PHP_PE_CTX_SZ 64
  103. #define SA_CTX_MAX_SZ (64 + SA_CTX_ENC_TYPE2_SZ + SA_CTX_AUTH_TYPE2_SZ)
  104. /*
  105. * Encoding of F/E control in SCCTL
  106. * Bit 0-1: Fetch PHP Bytes
  107. * Bit 2-3: Fetch Encryption/Air Ciphering Bytes
  108. * Bit 4-5: Fetch Authentication Bytes or Encr pass 2
  109. * Bit 6-7: Evict PHP Bytes
  110. *
  111. * where 00 = 0 bytes
  112. * 01 = 64 bytes
  113. * 10 = 96 bytes
  114. * 11 = 128 bytes
  115. */
  116. #define SA_CTX_DMA_SIZE_0 0
  117. #define SA_CTX_DMA_SIZE_64 1
  118. #define SA_CTX_DMA_SIZE_96 2
  119. #define SA_CTX_DMA_SIZE_128 3
  120. /*
  121. * Byte offset of the owner word in SCCTL
  122. * in the security context
  123. */
  124. #define SA_CTX_SCCTL_OWNER_OFFSET 0
  125. #define SA_CTX_ENC_KEY_OFFSET 32
  126. #define SA_CTX_ENC_AUX1_OFFSET 64
  127. #define SA_CTX_ENC_AUX2_OFFSET 96
  128. #define SA_CTX_ENC_AUX3_OFFSET 112
  129. #define SA_CTX_ENC_AUX4_OFFSET 128
  130. #define SA_SCCTL_FE_AUTH_ENC 0x65
  131. #define SA_SCCTL_FE_ENC 0x8D
  132. #define SA_ALIGN_MASK (sizeof(u32) - 1)
  133. #define SA_ALIGNED __aligned(32)
  134. #define SA_AUTH_SW_CTRL_MD5 1
  135. #define SA_AUTH_SW_CTRL_SHA1 2
  136. #define SA_AUTH_SW_CTRL_SHA224 3
  137. #define SA_AUTH_SW_CTRL_SHA256 4
  138. #define SA_AUTH_SW_CTRL_SHA384 5
  139. #define SA_AUTH_SW_CTRL_SHA512 6
  140. /* SA2UL can only handle maximum data size of 64KB */
  141. #define SA_MAX_DATA_SZ U16_MAX
  142. /*
  143. * SA2UL can provide unpredictable results with packet sizes that fall
  144. * the following range, so avoid using it.
  145. */
  146. #define SA_UNSAFE_DATA_SZ_MIN 240
  147. #define SA_UNSAFE_DATA_SZ_MAX 256
  148. /**
  149. * struct sa_crypto_data - Crypto driver instance data
  150. * @base: Base address of the register space
  151. * @pdev: Platform device pointer
  152. * @sc_pool: security context pool
  153. * @dev: Device pointer
  154. * @scid_lock: secure context ID lock
  155. * @sc_id_start: starting index for SC ID
  156. * @sc_id_end: Ending index for SC ID
  157. * @sc_id: Security Context ID
  158. * @ctx_bm: Bitmap to keep track of Security context ID's
  159. * @ctx: SA tfm context pointer
  160. * @dma_rx1: Pointer to DMA rx channel for sizes < 256 Bytes
  161. * @dma_rx2: Pointer to DMA rx channel for sizes > 256 Bytes
  162. * @dma_tx: Pointer to DMA TX channel
  163. */
  164. struct sa_crypto_data {
  165. void __iomem *base;
  166. struct platform_device *pdev;
  167. struct dma_pool *sc_pool;
  168. struct device *dev;
  169. spinlock_t scid_lock; /* lock for SC-ID allocation */
  170. /* Security context data */
  171. u16 sc_id_start;
  172. u16 sc_id_end;
  173. u16 sc_id;
  174. unsigned long ctx_bm[DIV_ROUND_UP(SA_MAX_NUM_CTX,
  175. BITS_PER_LONG)];
  176. struct sa_tfm_ctx *ctx;
  177. struct dma_chan *dma_rx1;
  178. struct dma_chan *dma_rx2;
  179. struct dma_chan *dma_tx;
  180. };
  181. /**
  182. * struct sa_cmdl_param_info: Command label parameters info
  183. * @index: Index of the parameter in the command label format
  184. * @offset: the offset of the parameter
  185. * @size: Size of the parameter
  186. */
  187. struct sa_cmdl_param_info {
  188. u16 index;
  189. u16 offset;
  190. u16 size;
  191. };
  192. /* Maximum length of Auxiliary data in 32bit words */
  193. #define SA_MAX_AUX_DATA_WORDS 8
  194. /**
  195. * struct sa_cmdl_upd_info: Command label updation info
  196. * @flags: flags in command label
  197. * @submode: Encryption submodes
  198. * @enc_size: Size of first pass encryption size
  199. * @enc_size2: Size of second pass encryption size
  200. * @enc_offset: Encryption payload offset in the packet
  201. * @enc_iv: Encryption initialization vector for pass2
  202. * @enc_iv2: Encryption initialization vector for pass2
  203. * @aad: Associated data
  204. * @payload: Payload info
  205. * @auth_size: Authentication size for pass 1
  206. * @auth_size2: Authentication size for pass 2
  207. * @auth_offset: Authentication payload offset
  208. * @auth_iv: Authentication initialization vector
  209. * @aux_key_info: Authentication aux key information
  210. * @aux_key: Aux key for authentication
  211. */
  212. struct sa_cmdl_upd_info {
  213. u16 flags;
  214. u16 submode;
  215. struct sa_cmdl_param_info enc_size;
  216. struct sa_cmdl_param_info enc_size2;
  217. struct sa_cmdl_param_info enc_offset;
  218. struct sa_cmdl_param_info enc_iv;
  219. struct sa_cmdl_param_info enc_iv2;
  220. struct sa_cmdl_param_info aad;
  221. struct sa_cmdl_param_info payload;
  222. struct sa_cmdl_param_info auth_size;
  223. struct sa_cmdl_param_info auth_size2;
  224. struct sa_cmdl_param_info auth_offset;
  225. struct sa_cmdl_param_info auth_iv;
  226. struct sa_cmdl_param_info aux_key_info;
  227. u32 aux_key[SA_MAX_AUX_DATA_WORDS];
  228. };
  229. /*
  230. * Number of 32bit words appended after the command label
  231. * in PSDATA to identify the crypto request context.
  232. * word-0: Request type
  233. * word-1: pointer to request
  234. */
  235. #define SA_PSDATA_CTX_WORDS 4
  236. /* Maximum size of Command label in 32 words */
  237. #define SA_MAX_CMDL_WORDS (SA_DMA_NUM_PS_WORDS - SA_PSDATA_CTX_WORDS)
  238. /**
  239. * struct sa_ctx_info: SA context information
  240. * @sc: Pointer to security context
  241. * @sc_phys: Security context physical address that is passed on to SA2UL
  242. * @sc_id: Security context ID
  243. * @cmdl_size: Command label size
  244. * @cmdl: Command label for a particular iteration
  245. * @cmdl_upd_info: structure holding command label updation info
  246. * @epib: Extended protocol information block words
  247. */
  248. struct sa_ctx_info {
  249. u8 *sc;
  250. dma_addr_t sc_phys;
  251. u16 sc_id;
  252. u16 cmdl_size;
  253. u32 cmdl[SA_MAX_CMDL_WORDS];
  254. struct sa_cmdl_upd_info cmdl_upd_info;
  255. /* Store Auxiliary data such as K2/K3 subkeys in AES-XCBC */
  256. u32 epib[SA_DMA_NUM_EPIB_WORDS];
  257. };
  258. /**
  259. * struct sa_tfm_ctx: TFM context structure
  260. * @dev_data: struct sa_crypto_data pointer
  261. * @enc: struct sa_ctx_info for encryption
  262. * @dec: struct sa_ctx_info for decryption
  263. * @keylen: encrption/decryption keylength
  264. * @iv_idx: Initialization vector index
  265. * @key: encryption key
  266. * @fallback: SW fallback algorithm
  267. */
  268. struct sa_tfm_ctx {
  269. struct sa_crypto_data *dev_data;
  270. struct sa_ctx_info enc;
  271. struct sa_ctx_info dec;
  272. struct sa_ctx_info auth;
  273. int keylen;
  274. int iv_idx;
  275. u32 key[AES_KEYSIZE_256 / sizeof(u32)];
  276. u8 authkey[SHA512_BLOCK_SIZE];
  277. struct crypto_shash *shash;
  278. /* for fallback */
  279. union {
  280. struct crypto_sync_skcipher *skcipher;
  281. struct crypto_ahash *ahash;
  282. struct crypto_aead *aead;
  283. } fallback;
  284. };
  285. /**
  286. * struct sa_sha_req_ctx: Structure used for sha request
  287. * @dev_data: struct sa_crypto_data pointer
  288. * @cmdl: Complete command label with psdata and epib included
  289. * @fallback_req: SW fallback request container
  290. */
  291. struct sa_sha_req_ctx {
  292. struct sa_crypto_data *dev_data;
  293. u32 cmdl[SA_MAX_CMDL_WORDS + SA_PSDATA_CTX_WORDS];
  294. struct ahash_request fallback_req;
  295. };
  296. enum sa_submode {
  297. SA_MODE_GEN = 0,
  298. SA_MODE_CCM,
  299. SA_MODE_GCM,
  300. SA_MODE_GMAC
  301. };
  302. /* Encryption algorithms */
  303. enum sa_ealg_id {
  304. SA_EALG_ID_NONE = 0, /* No encryption */
  305. SA_EALG_ID_NULL, /* NULL encryption */
  306. SA_EALG_ID_AES_CTR, /* AES Counter mode */
  307. SA_EALG_ID_AES_F8, /* AES F8 mode */
  308. SA_EALG_ID_AES_CBC, /* AES CBC mode */
  309. SA_EALG_ID_DES_CBC, /* DES CBC mode */
  310. SA_EALG_ID_3DES_CBC, /* 3DES CBC mode */
  311. SA_EALG_ID_CCM, /* Counter with CBC-MAC mode */
  312. SA_EALG_ID_GCM, /* Galois Counter mode */
  313. SA_EALG_ID_AES_ECB,
  314. SA_EALG_ID_LAST
  315. };
  316. /* Authentication algorithms */
  317. enum sa_aalg_id {
  318. SA_AALG_ID_NONE = 0, /* No Authentication */
  319. SA_AALG_ID_NULL = SA_EALG_ID_LAST, /* NULL Authentication */
  320. SA_AALG_ID_MD5, /* MD5 mode */
  321. SA_AALG_ID_SHA1, /* SHA1 mode */
  322. SA_AALG_ID_SHA2_224, /* 224-bit SHA2 mode */
  323. SA_AALG_ID_SHA2_256, /* 256-bit SHA2 mode */
  324. SA_AALG_ID_SHA2_512, /* 512-bit SHA2 mode */
  325. SA_AALG_ID_HMAC_MD5, /* HMAC with MD5 mode */
  326. SA_AALG_ID_HMAC_SHA1, /* HMAC with SHA1 mode */
  327. SA_AALG_ID_HMAC_SHA2_224, /* HMAC with 224-bit SHA2 mode */
  328. SA_AALG_ID_HMAC_SHA2_256, /* HMAC with 256-bit SHA2 mode */
  329. SA_AALG_ID_GMAC, /* Galois Message Auth. Code mode */
  330. SA_AALG_ID_CMAC, /* Cipher-based Mes. Auth. Code mode */
  331. SA_AALG_ID_CBC_MAC, /* Cipher Block Chaining */
  332. SA_AALG_ID_AES_XCBC /* AES Extended Cipher Block Chaining */
  333. };
  334. /*
  335. * Mode control engine algorithms used to index the
  336. * mode control instruction tables
  337. */
  338. enum sa_eng_algo_id {
  339. SA_ENG_ALGO_ECB = 0,
  340. SA_ENG_ALGO_CBC,
  341. SA_ENG_ALGO_CFB,
  342. SA_ENG_ALGO_OFB,
  343. SA_ENG_ALGO_CTR,
  344. SA_ENG_ALGO_F8,
  345. SA_ENG_ALGO_F8F9,
  346. SA_ENG_ALGO_GCM,
  347. SA_ENG_ALGO_GMAC,
  348. SA_ENG_ALGO_CCM,
  349. SA_ENG_ALGO_CMAC,
  350. SA_ENG_ALGO_CBCMAC,
  351. SA_NUM_ENG_ALGOS
  352. };
  353. /**
  354. * struct sa_eng_info: Security accelerator engine info
  355. * @eng_id: Engine ID
  356. * @sc_size: security context size
  357. */
  358. struct sa_eng_info {
  359. u8 eng_id;
  360. u16 sc_size;
  361. };
  362. #endif /* _K3_SA2UL_ */