chcr_crypto.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. * This file is part of the Chelsio T6 Crypto driver for Linux.
  3. *
  4. * Copyright (c) 2003-2016 Chelsio Communications, Inc. All rights reserved.
  5. *
  6. * This software is available to you under a choice of one of two
  7. * licenses. You may choose to be licensed under the terms of the GNU
  8. * General Public License (GPL) Version 2, available from the file
  9. * COPYING in the main directory of this source tree, or the
  10. * OpenIB.org BSD license below:
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above
  17. * copyright notice, this list of conditions and the following
  18. * disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials
  23. * provided with the distribution.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  29. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  30. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  31. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. * SOFTWARE.
  33. *
  34. */
  35. #ifndef __CHCR_CRYPTO_H__
  36. #define __CHCR_CRYPTO_H__
  37. #define GHASH_BLOCK_SIZE 16
  38. #define GHASH_DIGEST_SIZE 16
  39. #define CCM_B0_SIZE 16
  40. #define CCM_AAD_FIELD_SIZE 2
  41. #define T6_MAX_AAD_SIZE 511
  42. /* Define following if h/w is not dropping the AAD and IV data before
  43. * giving the processed data
  44. */
  45. #define CHCR_CRA_PRIORITY 500
  46. #define CHCR_AEAD_PRIORITY 6000
  47. #define CHCR_AES_MAX_KEY_LEN (2 * (AES_MAX_KEY_SIZE)) /* consider xts */
  48. #define CHCR_MAX_CRYPTO_IV_LEN 16 /* AES IV len */
  49. #define CHCR_MAX_AUTHENC_AES_KEY_LEN 32 /* max aes key length*/
  50. #define CHCR_MAX_AUTHENC_SHA_KEY_LEN 128 /* max sha key length*/
  51. #define CHCR_GIVENCRYPT_OP 2
  52. /* CPL/SCMD parameters */
  53. #define CHCR_ENCRYPT_OP 0
  54. #define CHCR_DECRYPT_OP 1
  55. #define CHCR_SCMD_SEQ_NO_CTRL_32BIT 1
  56. #define CHCR_SCMD_SEQ_NO_CTRL_48BIT 2
  57. #define CHCR_SCMD_SEQ_NO_CTRL_64BIT 3
  58. #define CHCR_SCMD_PROTO_VERSION_GENERIC 4
  59. #define CHCR_SCMD_AUTH_CTRL_AUTH_CIPHER 0
  60. #define CHCR_SCMD_AUTH_CTRL_CIPHER_AUTH 1
  61. #define CHCR_SCMD_CIPHER_MODE_NOP 0
  62. #define CHCR_SCMD_CIPHER_MODE_AES_CBC 1
  63. #define CHCR_SCMD_CIPHER_MODE_AES_GCM 2
  64. #define CHCR_SCMD_CIPHER_MODE_AES_CTR 3
  65. #define CHCR_SCMD_CIPHER_MODE_GENERIC_AES 4
  66. #define CHCR_SCMD_CIPHER_MODE_AES_XTS 6
  67. #define CHCR_SCMD_CIPHER_MODE_AES_CCM 7
  68. #define CHCR_SCMD_AUTH_MODE_NOP 0
  69. #define CHCR_SCMD_AUTH_MODE_SHA1 1
  70. #define CHCR_SCMD_AUTH_MODE_SHA224 2
  71. #define CHCR_SCMD_AUTH_MODE_SHA256 3
  72. #define CHCR_SCMD_AUTH_MODE_GHASH 4
  73. #define CHCR_SCMD_AUTH_MODE_SHA512_224 5
  74. #define CHCR_SCMD_AUTH_MODE_SHA512_256 6
  75. #define CHCR_SCMD_AUTH_MODE_SHA512_384 7
  76. #define CHCR_SCMD_AUTH_MODE_SHA512_512 8
  77. #define CHCR_SCMD_AUTH_MODE_CBCMAC 9
  78. #define CHCR_SCMD_AUTH_MODE_CMAC 10
  79. #define CHCR_SCMD_HMAC_CTRL_NOP 0
  80. #define CHCR_SCMD_HMAC_CTRL_NO_TRUNC 1
  81. #define CHCR_SCMD_HMAC_CTRL_TRUNC_RFC4366 2
  82. #define CHCR_SCMD_HMAC_CTRL_IPSEC_96BIT 3
  83. #define CHCR_SCMD_HMAC_CTRL_PL1 4
  84. #define CHCR_SCMD_HMAC_CTRL_PL2 5
  85. #define CHCR_SCMD_HMAC_CTRL_PL3 6
  86. #define CHCR_SCMD_HMAC_CTRL_DIV2 7
  87. #define VERIFY_HW 0
  88. #define VERIFY_SW 1
  89. #define CHCR_SCMD_IVGEN_CTRL_HW 0
  90. #define CHCR_SCMD_IVGEN_CTRL_SW 1
  91. /* This are not really mac key size. They are intermediate values
  92. * of sha engine and its size
  93. */
  94. #define CHCR_KEYCTX_MAC_KEY_SIZE_128 0
  95. #define CHCR_KEYCTX_MAC_KEY_SIZE_160 1
  96. #define CHCR_KEYCTX_MAC_KEY_SIZE_192 2
  97. #define CHCR_KEYCTX_MAC_KEY_SIZE_256 3
  98. #define CHCR_KEYCTX_MAC_KEY_SIZE_512 4
  99. #define CHCR_KEYCTX_CIPHER_KEY_SIZE_128 0
  100. #define CHCR_KEYCTX_CIPHER_KEY_SIZE_192 1
  101. #define CHCR_KEYCTX_CIPHER_KEY_SIZE_256 2
  102. #define CHCR_KEYCTX_NO_KEY 15
  103. #define CHCR_CPL_FW4_PLD_IV_OFFSET (5 * 64) /* bytes. flt #5 and #6 */
  104. #define CHCR_CPL_FW4_PLD_HASH_RESULT_OFFSET (7 * 64) /* bytes. flt #7 */
  105. #define CHCR_CPL_FW4_PLD_DATA_SIZE (4 * 64) /* bytes. flt #4 to #7 */
  106. #define KEY_CONTEXT_HDR_SALT_AND_PAD 16
  107. #define flits_to_bytes(x) (x * 8)
  108. #define IV_NOP 0
  109. #define IV_IMMEDIATE 1
  110. #define IV_DSGL 2
  111. #define AEAD_H_SIZE 16
  112. #define CRYPTO_ALG_SUB_TYPE_MASK 0x0f000000
  113. #define CRYPTO_ALG_SUB_TYPE_HASH_HMAC 0x01000000
  114. #define CRYPTO_ALG_SUB_TYPE_AEAD_RFC4106 0x02000000
  115. #define CRYPTO_ALG_SUB_TYPE_AEAD_GCM 0x03000000
  116. #define CRYPTO_ALG_SUB_TYPE_CBC_SHA 0x04000000
  117. #define CRYPTO_ALG_SUB_TYPE_AEAD_CCM 0x05000000
  118. #define CRYPTO_ALG_SUB_TYPE_AEAD_RFC4309 0x06000000
  119. #define CRYPTO_ALG_SUB_TYPE_CBC_NULL 0x07000000
  120. #define CRYPTO_ALG_SUB_TYPE_CTR 0x08000000
  121. #define CRYPTO_ALG_SUB_TYPE_CTR_RFC3686 0x09000000
  122. #define CRYPTO_ALG_SUB_TYPE_XTS 0x0a000000
  123. #define CRYPTO_ALG_SUB_TYPE_CBC 0x0b000000
  124. #define CRYPTO_ALG_SUB_TYPE_CTR_SHA 0x0c000000
  125. #define CRYPTO_ALG_SUB_TYPE_CTR_NULL 0x0d000000
  126. #define CRYPTO_ALG_TYPE_HMAC (CRYPTO_ALG_TYPE_AHASH |\
  127. CRYPTO_ALG_SUB_TYPE_HASH_HMAC)
  128. #define MAX_SCRATCH_PAD_SIZE 32
  129. #define CHCR_HASH_MAX_BLOCK_SIZE_64 64
  130. #define CHCR_HASH_MAX_BLOCK_SIZE_128 128
  131. #define CHCR_SRC_SG_SIZE (0x10000 - sizeof(int))
  132. #define CHCR_DST_SG_SIZE 2048
  133. static inline struct chcr_context *a_ctx(struct crypto_aead *tfm)
  134. {
  135. return crypto_aead_ctx(tfm);
  136. }
  137. static inline struct chcr_context *c_ctx(struct crypto_ablkcipher *tfm)
  138. {
  139. return crypto_ablkcipher_ctx(tfm);
  140. }
  141. static inline struct chcr_context *h_ctx(struct crypto_ahash *tfm)
  142. {
  143. return crypto_tfm_ctx(crypto_ahash_tfm(tfm));
  144. }
  145. struct ablk_ctx {
  146. struct crypto_skcipher *sw_cipher;
  147. struct crypto_cipher *aes_generic;
  148. __be32 key_ctx_hdr;
  149. unsigned int enckey_len;
  150. unsigned char ciph_mode;
  151. u8 key[CHCR_AES_MAX_KEY_LEN];
  152. u8 nonce[4];
  153. u8 rrkey[AES_MAX_KEY_SIZE];
  154. };
  155. struct chcr_aead_reqctx {
  156. struct sk_buff *skb;
  157. dma_addr_t iv_dma;
  158. dma_addr_t b0_dma;
  159. unsigned int b0_len;
  160. unsigned int op;
  161. short int aad_nents;
  162. short int src_nents;
  163. short int dst_nents;
  164. u16 imm;
  165. u16 verify;
  166. u8 iv[CHCR_MAX_CRYPTO_IV_LEN + MAX_SCRATCH_PAD_SIZE];
  167. u8 *scratch_pad;
  168. };
  169. struct ulptx_walk {
  170. struct ulptx_sgl *sgl;
  171. unsigned int nents;
  172. unsigned int pair_idx;
  173. unsigned int last_sg_len;
  174. struct scatterlist *last_sg;
  175. struct ulptx_sge_pair *pair;
  176. };
  177. struct dsgl_walk {
  178. unsigned int nents;
  179. unsigned int last_sg_len;
  180. struct scatterlist *last_sg;
  181. struct cpl_rx_phys_dsgl *dsgl;
  182. struct phys_sge_pairs *to;
  183. };
  184. struct chcr_gcm_ctx {
  185. u8 ghash_h[AEAD_H_SIZE];
  186. };
  187. struct chcr_authenc_ctx {
  188. u8 dec_rrkey[AES_MAX_KEY_SIZE];
  189. u8 h_iopad[2 * CHCR_HASH_MAX_DIGEST_SIZE];
  190. unsigned char auth_mode;
  191. };
  192. struct __aead_ctx {
  193. struct chcr_gcm_ctx gcm[0];
  194. struct chcr_authenc_ctx authenc[0];
  195. };
  196. struct chcr_aead_ctx {
  197. __be32 key_ctx_hdr;
  198. unsigned int enckey_len;
  199. struct crypto_aead *sw_cipher;
  200. u8 salt[MAX_SALT];
  201. u8 key[CHCR_AES_MAX_KEY_LEN];
  202. u8 nonce[4];
  203. u16 hmac_ctrl;
  204. u16 mayverify;
  205. struct __aead_ctx ctx[0];
  206. };
  207. struct hmac_ctx {
  208. struct crypto_shash *base_hash;
  209. u8 ipad[CHCR_HASH_MAX_BLOCK_SIZE_128];
  210. u8 opad[CHCR_HASH_MAX_BLOCK_SIZE_128];
  211. };
  212. struct __crypto_ctx {
  213. struct hmac_ctx hmacctx[0];
  214. struct ablk_ctx ablkctx[0];
  215. struct chcr_aead_ctx aeadctx[0];
  216. };
  217. struct chcr_context {
  218. struct chcr_dev *dev;
  219. unsigned char tx_qidx;
  220. unsigned char rx_qidx;
  221. unsigned char tx_chan_id;
  222. unsigned char pci_chan_id;
  223. struct __crypto_ctx crypto_ctx[0];
  224. };
  225. struct chcr_hctx_per_wr {
  226. struct scatterlist *srcsg;
  227. struct sk_buff *skb;
  228. dma_addr_t dma_addr;
  229. u32 dma_len;
  230. unsigned int src_ofst;
  231. unsigned int processed;
  232. u32 result;
  233. u8 is_sg_map;
  234. u8 imm;
  235. /*Final callback called. Driver cannot rely on nbytes to decide
  236. * final call
  237. */
  238. u8 isfinal;
  239. };
  240. struct chcr_ahash_req_ctx {
  241. struct chcr_hctx_per_wr hctx_wr;
  242. u8 *reqbfr;
  243. u8 *skbfr;
  244. /* SKB which is being sent to the hardware for processing */
  245. u64 data_len; /* Data len till time */
  246. u8 reqlen;
  247. u8 partial_hash[CHCR_HASH_MAX_DIGEST_SIZE];
  248. u8 bfr1[CHCR_HASH_MAX_BLOCK_SIZE_128];
  249. u8 bfr2[CHCR_HASH_MAX_BLOCK_SIZE_128];
  250. };
  251. struct chcr_blkcipher_req_ctx {
  252. struct sk_buff *skb;
  253. struct scatterlist *dstsg;
  254. unsigned int processed;
  255. unsigned int last_req_len;
  256. struct scatterlist *srcsg;
  257. unsigned int src_ofst;
  258. unsigned int dst_ofst;
  259. unsigned int op;
  260. u16 imm;
  261. u8 iv[CHCR_MAX_CRYPTO_IV_LEN];
  262. };
  263. struct chcr_alg_template {
  264. u32 type;
  265. u32 is_registered;
  266. union {
  267. struct crypto_alg crypto;
  268. struct ahash_alg hash;
  269. struct aead_alg aead;
  270. } alg;
  271. };
  272. typedef struct sk_buff *(*create_wr_t)(struct aead_request *req,
  273. unsigned short qid,
  274. int size);
  275. void chcr_verify_tag(struct aead_request *req, u8 *input, int *err);
  276. int chcr_aead_dma_map(struct device *dev, struct aead_request *req,
  277. unsigned short op_type);
  278. void chcr_aead_dma_unmap(struct device *dev, struct aead_request *req,
  279. unsigned short op_type);
  280. void chcr_add_aead_dst_ent(struct aead_request *req,
  281. struct cpl_rx_phys_dsgl *phys_cpl,
  282. unsigned int assoclen,
  283. unsigned short qid);
  284. void chcr_add_aead_src_ent(struct aead_request *req, struct ulptx_sgl *ulptx,
  285. unsigned int assoclen);
  286. void chcr_add_cipher_src_ent(struct ablkcipher_request *req,
  287. void *ulptx,
  288. struct cipher_wr_param *wrparam);
  289. int chcr_cipher_dma_map(struct device *dev, struct ablkcipher_request *req);
  290. void chcr_cipher_dma_unmap(struct device *dev, struct ablkcipher_request *req);
  291. void chcr_add_cipher_dst_ent(struct ablkcipher_request *req,
  292. struct cpl_rx_phys_dsgl *phys_cpl,
  293. struct cipher_wr_param *wrparam,
  294. unsigned short qid);
  295. int sg_nents_len_skip(struct scatterlist *sg, u64 len, u64 skip);
  296. void chcr_add_hash_src_ent(struct ahash_request *req, struct ulptx_sgl *ulptx,
  297. struct hash_wr_param *param);
  298. int chcr_hash_dma_map(struct device *dev, struct ahash_request *req);
  299. void chcr_hash_dma_unmap(struct device *dev, struct ahash_request *req);
  300. void chcr_aead_common_exit(struct aead_request *req);
  301. #endif /* __CHCR_CRYPTO_H__ */