keystone-sa-hlp.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /*
  2. * Keystone crypto accelerator driver
  3. *
  4. * Copyright (C) 2015,2016 Texas Instruments Incorporated - http://www.ti.com
  5. *
  6. * Authors: Sandeep Nair
  7. * Vitaly Andrianov
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * version 2 as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. */
  18. #ifndef _KEYSTONE_SA_HLP_
  19. #define _KEYSTONE_SA_HLP_
  20. #include <linux/interrupt.h>
  21. #include <linux/soc/ti/knav_dma.h>
  22. #include <linux/regmap.h>
  23. #include <linux/skbuff.h>
  24. #include <asm/aes_glue.h>
  25. #include <crypto/aes.h>
  26. /* Make 32-bit word from 4 bytes */
  27. #define SA_MK_U32(b0, b1, b2, b3) (((b0) << 24) | ((b1) << 16) | \
  28. ((b2) << 8) | (b3))
  29. #define AES_XCBC_DIGEST_SIZE 16
  30. /* Values for NULL algorithms */
  31. #define NULL_KEY_SIZE 0
  32. #define NULL_BLOCK_SIZE 1
  33. #define NULL_DIGEST_SIZE 0
  34. #define NULL_IV_SIZE 0
  35. /* Number of 32 bit words in EPIB */
  36. #define SA_DMA_NUM_EPIB_WORDS 4
  37. /* Number of 32 bit words in PS data */
  38. #define SA_DMA_NUM_PS_WORDS 16
  39. /*
  40. * Maximum number of simultaeneous security contexts
  41. * supported by the driver
  42. */
  43. #define SA_MAX_NUM_CTX 512
  44. /*
  45. * Encoding used to identify the typo of crypto operation
  46. * performed on the packet when the packet is returned
  47. * by SA
  48. */
  49. #define SA_REQ_SUBTYPE_ENC 0x0001
  50. #define SA_REQ_SUBTYPE_DEC 0x0002
  51. #define SA_REQ_SUBTYPE_SHIFT 16
  52. #define SA_REQ_SUBTYPE_MASK 0xffff
  53. /*
  54. * Maximum size of authentication tag
  55. * NOTE: update this macro as we start supporting
  56. * algorithms with bigger digest size
  57. */
  58. #define SA_MAX_AUTH_TAG_SZ SHA1_DIGEST_SIZE
  59. #define SA_RX_BUF0_SIZE 1500
  60. #define SA_PID_OFS 0
  61. #define SA_CMD_STATUS_OFS 0x8
  62. #define SA_PA_FLOWID_OFS 0x10
  63. #define SA_CDMA_FLOWID_OFS 0x14
  64. #define SA_PA_ENG_ID_OFS 0x18
  65. #define SA_CDMA_ENG_ID_OFS 0x1C
  66. /* Driver statistics */
  67. struct sa_drv_stats {
  68. /* Number of data pkts dropped while submitting to CP_ACE */
  69. atomic_t tx_dropped;
  70. /* Number of tear-down pkts dropped while submitting to CP_ACE */
  71. atomic_t sc_tear_dropped;
  72. /* Number of crypto requests sent to CP_ACE */
  73. atomic_t tx_pkts;
  74. /* Number of crypto request completions received from CP_ACE */
  75. atomic_t rx_pkts;
  76. };
  77. /* Crypto driver instance data */
  78. struct keystone_crypto_data {
  79. struct platform_device *pdev;
  80. struct clk *clk;
  81. struct tasklet_struct rx_task;
  82. struct tasklet_struct tx_task;
  83. struct dma_pool *sc_pool;
  84. struct kmem_cache *dma_req_ctx_cache;
  85. struct regmap *sa_regmap;
  86. void *rx_chan;
  87. void *rx_fdq[KNAV_DMA_FDQ_PER_CHAN];
  88. void *rx_compl_q;
  89. void *tx_chan;
  90. void *tx_submit_q;
  91. void *tx_compl_q;
  92. u32 tx_submit_qid;
  93. u32 tx_compl_qid;
  94. u32 rx_compl_qid;
  95. const char *rx_chan_name;
  96. const char *tx_chan_name;
  97. u32 tx_queue_depth;
  98. u32 rx_queue_depths[KNAV_DMA_FDQ_PER_CHAN];
  99. u32 rx_pool_size;
  100. u32 rx_pool_region_id;
  101. void *rx_pool;
  102. u32 tx_pool_size;
  103. u32 tx_pool_region_id;
  104. void *tx_pool;
  105. spinlock_t scid_lock; /* lock for SC-ID allocation */
  106. struct kobject stats_kobj;
  107. int stats_fl;
  108. /* Security context data */
  109. u16 sc_id_start;
  110. u16 sc_id_end;
  111. u16 sc_id;
  112. /* Bitmap to keep track of Security context ID's */
  113. unsigned long ctx_bm[DIV_ROUND_UP(SA_MAX_NUM_CTX,
  114. BITS_PER_LONG)];
  115. /* Driver stats */
  116. struct sa_drv_stats stats;
  117. atomic_t rx_dma_page_cnt; /* N buf from 2nd pool available */
  118. atomic_t tx_dma_desc_cnt; /* Tx DMA desc-s available */
  119. };
  120. /* Packet structure used in Rx */
  121. #define SA_SGLIST_SIZE MAX_SKB_FRAGS
  122. struct sa_packet {
  123. struct scatterlist sg[SA_SGLIST_SIZE];
  124. int sg_ents;
  125. struct keystone_crypto_data *priv;
  126. struct dma_chan *chan;
  127. struct dma_async_tx_descriptor *desc;
  128. u32 epib[SA_DMA_NUM_EPIB_WORDS];
  129. u32 psdata[SA_DMA_NUM_PS_WORDS];
  130. struct completion complete;
  131. void *data;
  132. };
  133. /* Command label updation info */
  134. struct sa_cmdl_param_info {
  135. u16 index;
  136. u16 offset;
  137. u16 size;
  138. };
  139. /* Maximum length of Auxiliary data in 32bit words */
  140. #define SA_MAX_AUX_DATA_WORDS 8
  141. struct sa_cmdl_upd_info {
  142. u16 flags;
  143. u16 submode;
  144. struct sa_cmdl_param_info enc_size;
  145. struct sa_cmdl_param_info enc_size2;
  146. struct sa_cmdl_param_info enc_offset;
  147. struct sa_cmdl_param_info enc_iv;
  148. struct sa_cmdl_param_info enc_iv2;
  149. struct sa_cmdl_param_info aad;
  150. struct sa_cmdl_param_info payload;
  151. struct sa_cmdl_param_info auth_size;
  152. struct sa_cmdl_param_info auth_size2;
  153. struct sa_cmdl_param_info auth_offset;
  154. struct sa_cmdl_param_info auth_iv;
  155. struct sa_cmdl_param_info aux_key_info;
  156. u32 aux_key[SA_MAX_AUX_DATA_WORDS];
  157. };
  158. enum sa_submode {
  159. SA_MODE_GEN = 0,
  160. SA_MODE_CCM,
  161. SA_MODE_GCM,
  162. SA_MODE_GMAC
  163. };
  164. /*
  165. * Number of 32bit words appended after the command label
  166. * in PSDATA to identify the crypto request context.
  167. * word-0: Request type
  168. * word-1: pointer to request
  169. */
  170. #define SA_PSDATA_CTX_WORDS 4
  171. /* Maximum size of Command label in 32 words */
  172. #define SA_MAX_CMDL_WORDS (SA_DMA_NUM_PS_WORDS - SA_PSDATA_CTX_WORDS)
  173. struct sa_ctx_info {
  174. u8 *sc;
  175. dma_addr_t sc_phys;
  176. u16 sc_id;
  177. u16 cmdl_size;
  178. u32 cmdl[SA_MAX_CMDL_WORDS];
  179. struct sa_cmdl_upd_info cmdl_upd_info;
  180. /* Store Auxiliary data such as K2/K3 subkeys in AES-XCBC */
  181. u32 epib[SA_DMA_NUM_EPIB_WORDS];
  182. u32 rx_flow;
  183. u32 rx_compl_qid;
  184. };
  185. struct sa_tfm_ctx {
  186. struct keystone_crypto_data *dev_data;
  187. struct sa_ctx_info enc;
  188. struct sa_ctx_info dec;
  189. struct sa_ctx_info auth;
  190. };
  191. /* Tx DMA callback param */
  192. struct sa_dma_req_ctx {
  193. struct keystone_crypto_data *dev_data;
  194. u32 cmdl[SA_MAX_CMDL_WORDS + SA_PSDATA_CTX_WORDS];
  195. struct scatterlist *src;
  196. unsigned int src_nents;
  197. struct dma_chan *tx_chan;
  198. bool pkt;
  199. };
  200. /* Encryption algorithms */
  201. enum sa_alg_id {
  202. SA_EALG_ID_NONE = 0, /* No encryption */
  203. SA_AALG_ID_NONE = SA_EALG_ID_NONE, /* No Authentication */
  204. SA_EALG_ID_NULL, /* NULL encryption */
  205. SA_EALG_ID_AES_CTR, /* AES Counter mode */
  206. SA_EALG_ID_AES_F8, /* AES F8 mode */
  207. SA_EALG_ID_AES_CBC, /* AES CBC mode */
  208. SA_EALG_ID_DES_CBC, /* DES CBC mode */
  209. SA_EALG_ID_3DES_CBC, /* 3DES CBC mode */
  210. SA_EALG_ID_CCM, /* Counter with CBC-MAC mode */
  211. SA_EALG_ID_GCM, /* Galois Counter mode */
  212. SA_AALG_ID_NULL, /* NULL Authentication */
  213. SA_AALG_ID_MD5, /* MD5 mode */
  214. SA_AALG_ID_SHA1, /* SHA1 mode */
  215. SA_AALG_ID_SHA2_224, /* 224-bit SHA2 mode */
  216. SA_AALG_ID_SHA2_256, /* 256-bit SHA2 mode */
  217. SA_AALG_ID_HMAC_MD5, /* HMAC with MD5 mode */
  218. SA_AALG_ID_HMAC_SHA1, /* HMAC with SHA1 mode */
  219. SA_AALG_ID_HMAC_SHA2_224, /* HMAC with 224-bit SHA2 mode */
  220. SA_AALG_ID_HMAC_SHA2_256, /* HMAC with 256-bit SHA2 mode */
  221. SA_AALG_ID_GMAC, /* Galois Message Auth. Code mode */
  222. SA_AALG_ID_CMAC, /* Cipher-based Mes. Auth. Code mode */
  223. SA_AALG_ID_CBC_MAC, /* Cipher Block Chaining */
  224. SA_AALG_ID_AES_XCBC, /* AES Extended Cipher Block Chaining */
  225. SA_ALG_ID_LAST
  226. };
  227. /*
  228. * Mode control engine algorithms used to index the
  229. * mode control instruction tables
  230. */
  231. enum sa_eng_algo_id {
  232. SA_ENG_ALGO_ECB = 0,
  233. SA_ENG_ALGO_CBC,
  234. SA_ENG_ALGO_CFB,
  235. SA_ENG_ALGO_OFB,
  236. SA_ENG_ALGO_CTR,
  237. SA_ENG_ALGO_F8,
  238. SA_ENG_ALGO_F8F9,
  239. SA_ENG_ALGO_GCM,
  240. SA_ENG_ALGO_GMAC,
  241. SA_ENG_ALGO_CCM,
  242. SA_ENG_ALGO_CMAC,
  243. SA_ENG_ALGO_CBCMAC,
  244. SA_NUM_ENG_ALGOS
  245. };
  246. /* 3DES only supports ECB, CBC, CFB and OFB. */
  247. #define SA_3DES_FIRST_ALGO SA_ENG_ALGO_ECB
  248. #define SA_3DES_LAST_ALGO SA_ENG_ALGO_OFB
  249. #define SA_3DES_NUM_ALGOS (SA_3DES_LAST_ALGO - SA_3DES_FIRST_ALGO + 1)
  250. #define NKEY_SZ 3
  251. #define MCI_SZ 27
  252. struct sa_eng_info {
  253. u8 eng_id;
  254. u16 sc_size;
  255. };
  256. void sa_set_sc_auth(u16 alg_id, const u8 *key, u16 key_sz, u8 *sc_buf);
  257. #define DMA_HAS_PSINFO BIT(31)
  258. #define DMA_HAS_EPIB BIT(30)
  259. void sa_register_algos(const struct device *dev);
  260. void sa_unregister_algos(const struct device *dev);
  261. void sa_tx_completion_process(struct keystone_crypto_data *dev_data);
  262. void sa_rx_completion_process(struct keystone_crypto_data *dev_data);
  263. int sa_set_sc_enc(u16 alg_id, const u8 *key, u16 key_sz,
  264. u16 aad_len, u8 enc, u8 *sc_buf);
  265. void sa_swiz_128(u8 *in, u8 *out, u16 len);
  266. void sa_conv_calg_to_salg(const char *cra_name, int *ealg_id, int *aalg_id);
  267. struct sa_eng_info *sa_get_engine_info(int alg_id);
  268. int sa_get_hash_size(u16 aalg_id);
  269. /*
  270. * Derive sub-key k1, k2 and k3 used in the AES XCBC MAC mode
  271. * detailed in RFC 3566
  272. */
  273. static inline int sa_aes_xcbc_subkey(u8 *sub_key1, u8 *sub_key2,
  274. u8 *sub_key3, const u8 *key,
  275. u16 key_sz)
  276. {
  277. struct AES_KEY enc_key;
  278. if (private_AES_set_encrypt_key(key, (key_sz * 8), &enc_key)) {
  279. pr_err("%s: failed to set enc key\n", __func__);
  280. return -EINVAL;
  281. }
  282. if (sub_key1) {
  283. memset(sub_key1, 0x01, AES_BLOCK_SIZE);
  284. AES_encrypt(sub_key1, sub_key1, &enc_key);
  285. }
  286. if (sub_key2) {
  287. memset(sub_key2, 0x02, AES_BLOCK_SIZE);
  288. AES_encrypt(sub_key2, sub_key2, &enc_key);
  289. }
  290. if (sub_key3) {
  291. memset(sub_key3, 0x03, AES_BLOCK_SIZE);
  292. AES_encrypt(sub_key3, sub_key3, &enc_key);
  293. }
  294. return 0;
  295. }
  296. struct sa_eng_mci_tbl {
  297. uint8_t aes_enc[SA_NUM_ENG_ALGOS][NKEY_SZ][MCI_SZ];
  298. uint8_t aes_dec[SA_NUM_ENG_ALGOS][NKEY_SZ][MCI_SZ];
  299. uint8_t _3des_enc[SA_3DES_NUM_ALGOS][MCI_SZ];
  300. uint8_t _3des_dec[SA_3DES_NUM_ALGOS][MCI_SZ];
  301. };
  302. extern struct sa_eng_mci_tbl sa_mci_tbl;
  303. extern struct device *sa_ks2_dev;
  304. #endif /* _KEYSTONE_SA_HLP_ */