chcr_crypto.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. // 511 - 16(For IV)
  42. #define T6_MAX_AAD_SIZE 495
  43. /* Define following if h/w is not dropping the AAD and IV data before
  44. * giving the processed data
  45. */
  46. #define CHCR_CRA_PRIORITY 500
  47. #define CHCR_AEAD_PRIORITY 6000
  48. #define CHCR_AES_MAX_KEY_LEN (2 * (AES_MAX_KEY_SIZE)) /* consider xts */
  49. #define CHCR_MAX_CRYPTO_IV_LEN 16 /* AES IV len */
  50. #define CHCR_MAX_AUTHENC_AES_KEY_LEN 32 /* max aes key length*/
  51. #define CHCR_MAX_AUTHENC_SHA_KEY_LEN 128 /* max sha key length*/
  52. #define CHCR_GIVENCRYPT_OP 2
  53. /* CPL/SCMD parameters */
  54. #define CHCR_ENCRYPT_OP 0
  55. #define CHCR_DECRYPT_OP 1
  56. #define CHCR_SCMD_SEQ_NO_CTRL_32BIT 1
  57. #define CHCR_SCMD_SEQ_NO_CTRL_48BIT 2
  58. #define CHCR_SCMD_SEQ_NO_CTRL_64BIT 3
  59. #define CHCR_SCMD_PROTO_VERSION_GENERIC 4
  60. #define CHCR_SCMD_AUTH_CTRL_AUTH_CIPHER 0
  61. #define CHCR_SCMD_AUTH_CTRL_CIPHER_AUTH 1
  62. #define CHCR_SCMD_CIPHER_MODE_NOP 0
  63. #define CHCR_SCMD_CIPHER_MODE_AES_CBC 1
  64. #define CHCR_SCMD_CIPHER_MODE_AES_GCM 2
  65. #define CHCR_SCMD_CIPHER_MODE_AES_CTR 3
  66. #define CHCR_SCMD_CIPHER_MODE_GENERIC_AES 4
  67. #define CHCR_SCMD_CIPHER_MODE_AES_XTS 6
  68. #define CHCR_SCMD_CIPHER_MODE_AES_CCM 7
  69. #define CHCR_SCMD_AUTH_MODE_NOP 0
  70. #define CHCR_SCMD_AUTH_MODE_SHA1 1
  71. #define CHCR_SCMD_AUTH_MODE_SHA224 2
  72. #define CHCR_SCMD_AUTH_MODE_SHA256 3
  73. #define CHCR_SCMD_AUTH_MODE_GHASH 4
  74. #define CHCR_SCMD_AUTH_MODE_SHA512_224 5
  75. #define CHCR_SCMD_AUTH_MODE_SHA512_256 6
  76. #define CHCR_SCMD_AUTH_MODE_SHA512_384 7
  77. #define CHCR_SCMD_AUTH_MODE_SHA512_512 8
  78. #define CHCR_SCMD_AUTH_MODE_CBCMAC 9
  79. #define CHCR_SCMD_AUTH_MODE_CMAC 10
  80. #define CHCR_SCMD_HMAC_CTRL_NOP 0
  81. #define CHCR_SCMD_HMAC_CTRL_NO_TRUNC 1
  82. #define CHCR_SCMD_HMAC_CTRL_TRUNC_RFC4366 2
  83. #define CHCR_SCMD_HMAC_CTRL_IPSEC_96BIT 3
  84. #define CHCR_SCMD_HMAC_CTRL_PL1 4
  85. #define CHCR_SCMD_HMAC_CTRL_PL2 5
  86. #define CHCR_SCMD_HMAC_CTRL_PL3 6
  87. #define CHCR_SCMD_HMAC_CTRL_DIV2 7
  88. #define VERIFY_HW 0
  89. #define VERIFY_SW 1
  90. #define CHCR_SCMD_IVGEN_CTRL_HW 0
  91. #define CHCR_SCMD_IVGEN_CTRL_SW 1
  92. /* This are not really mac key size. They are intermediate values
  93. * of sha engine and its size
  94. */
  95. #define CHCR_KEYCTX_MAC_KEY_SIZE_128 0
  96. #define CHCR_KEYCTX_MAC_KEY_SIZE_160 1
  97. #define CHCR_KEYCTX_MAC_KEY_SIZE_192 2
  98. #define CHCR_KEYCTX_MAC_KEY_SIZE_256 3
  99. #define CHCR_KEYCTX_MAC_KEY_SIZE_512 4
  100. #define CHCR_KEYCTX_CIPHER_KEY_SIZE_128 0
  101. #define CHCR_KEYCTX_CIPHER_KEY_SIZE_192 1
  102. #define CHCR_KEYCTX_CIPHER_KEY_SIZE_256 2
  103. #define CHCR_KEYCTX_NO_KEY 15
  104. #define CHCR_CPL_FW4_PLD_IV_OFFSET (5 * 64) /* bytes. flt #5 and #6 */
  105. #define CHCR_CPL_FW4_PLD_HASH_RESULT_OFFSET (7 * 64) /* bytes. flt #7 */
  106. #define CHCR_CPL_FW4_PLD_DATA_SIZE (4 * 64) /* bytes. flt #4 to #7 */
  107. #define KEY_CONTEXT_HDR_SALT_AND_PAD 16
  108. #define flits_to_bytes(x) (x * 8)
  109. #define IV_NOP 0
  110. #define IV_IMMEDIATE 1
  111. #define IV_DSGL 2
  112. #define AEAD_H_SIZE 16
  113. #define CRYPTO_ALG_SUB_TYPE_MASK 0x0f000000
  114. #define CRYPTO_ALG_SUB_TYPE_HASH_HMAC 0x01000000
  115. #define CRYPTO_ALG_SUB_TYPE_AEAD_RFC4106 0x02000000
  116. #define CRYPTO_ALG_SUB_TYPE_AEAD_GCM 0x03000000
  117. #define CRYPTO_ALG_SUB_TYPE_CBC_SHA 0x04000000
  118. #define CRYPTO_ALG_SUB_TYPE_AEAD_CCM 0x05000000
  119. #define CRYPTO_ALG_SUB_TYPE_AEAD_RFC4309 0x06000000
  120. #define CRYPTO_ALG_SUB_TYPE_CBC_NULL 0x07000000
  121. #define CRYPTO_ALG_SUB_TYPE_CTR 0x08000000
  122. #define CRYPTO_ALG_SUB_TYPE_CTR_RFC3686 0x09000000
  123. #define CRYPTO_ALG_SUB_TYPE_XTS 0x0a000000
  124. #define CRYPTO_ALG_SUB_TYPE_CBC 0x0b000000
  125. #define CRYPTO_ALG_SUB_TYPE_CTR_SHA 0x0c000000
  126. #define CRYPTO_ALG_SUB_TYPE_CTR_NULL 0x0d000000
  127. #define CRYPTO_ALG_TYPE_HMAC (CRYPTO_ALG_TYPE_AHASH |\
  128. CRYPTO_ALG_SUB_TYPE_HASH_HMAC)
  129. #define MAX_SCRATCH_PAD_SIZE 32
  130. #define CHCR_HASH_MAX_BLOCK_SIZE_64 64
  131. #define CHCR_HASH_MAX_BLOCK_SIZE_128 128
  132. #define CHCR_SRC_SG_SIZE (0x10000 - sizeof(int))
  133. #define CHCR_DST_SG_SIZE 2048
  134. static inline struct chcr_context *a_ctx(struct crypto_aead *tfm)
  135. {
  136. return crypto_aead_ctx(tfm);
  137. }
  138. static inline struct chcr_context *c_ctx(struct crypto_skcipher *tfm)
  139. {
  140. return crypto_skcipher_ctx(tfm);
  141. }
  142. static inline struct chcr_context *h_ctx(struct crypto_ahash *tfm)
  143. {
  144. return crypto_tfm_ctx(crypto_ahash_tfm(tfm));
  145. }
  146. struct ablk_ctx {
  147. struct crypto_skcipher *sw_cipher;
  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. u16 imm;
  162. u16 verify;
  163. u16 txqidx;
  164. u16 rxqidx;
  165. u8 iv[CHCR_MAX_CRYPTO_IV_LEN + MAX_SCRATCH_PAD_SIZE];
  166. u8 *scratch_pad;
  167. };
  168. struct ulptx_walk {
  169. struct ulptx_sgl *sgl;
  170. unsigned int nents;
  171. unsigned int pair_idx;
  172. unsigned int last_sg_len;
  173. struct scatterlist *last_sg;
  174. struct ulptx_sge_pair *pair;
  175. };
  176. struct dsgl_walk {
  177. unsigned int nents;
  178. unsigned int last_sg_len;
  179. struct scatterlist *last_sg;
  180. struct cpl_rx_phys_dsgl *dsgl;
  181. struct phys_sge_pairs *to;
  182. };
  183. struct chcr_gcm_ctx {
  184. u8 ghash_h[AEAD_H_SIZE];
  185. };
  186. struct chcr_authenc_ctx {
  187. u8 dec_rrkey[AES_MAX_KEY_SIZE];
  188. u8 h_iopad[2 * CHCR_HASH_MAX_DIGEST_SIZE];
  189. unsigned char auth_mode;
  190. };
  191. struct __aead_ctx {
  192. union {
  193. DECLARE_FLEX_ARRAY(struct chcr_gcm_ctx, gcm);
  194. DECLARE_FLEX_ARRAY(struct chcr_authenc_ctx, authenc);
  195. };
  196. };
  197. struct chcr_aead_ctx {
  198. __be32 key_ctx_hdr;
  199. unsigned int enckey_len;
  200. struct crypto_aead *sw_cipher;
  201. u8 salt[MAX_SALT];
  202. u8 key[CHCR_AES_MAX_KEY_LEN];
  203. u8 nonce[4];
  204. u16 hmac_ctrl;
  205. u16 mayverify;
  206. struct __aead_ctx ctx[];
  207. };
  208. struct hmac_ctx {
  209. struct crypto_shash *base_hash;
  210. u8 ipad[CHCR_HASH_MAX_BLOCK_SIZE_128];
  211. u8 opad[CHCR_HASH_MAX_BLOCK_SIZE_128];
  212. };
  213. struct __crypto_ctx {
  214. union {
  215. DECLARE_FLEX_ARRAY(struct hmac_ctx, hmacctx);
  216. DECLARE_FLEX_ARRAY(struct ablk_ctx, ablkctx);
  217. DECLARE_FLEX_ARRAY(struct chcr_aead_ctx, aeadctx);
  218. };
  219. };
  220. struct chcr_context {
  221. struct chcr_dev *dev;
  222. unsigned char rxq_perchan;
  223. unsigned char txq_perchan;
  224. unsigned int ntxq;
  225. unsigned int nrxq;
  226. struct completion cbc_aes_aio_done;
  227. struct __crypto_ctx crypto_ctx[];
  228. };
  229. struct chcr_hctx_per_wr {
  230. struct scatterlist *srcsg;
  231. struct sk_buff *skb;
  232. dma_addr_t dma_addr;
  233. u32 dma_len;
  234. unsigned int src_ofst;
  235. unsigned int processed;
  236. u32 result;
  237. u8 is_sg_map;
  238. u8 imm;
  239. /*Final callback called. Driver cannot rely on nbytes to decide
  240. * final call
  241. */
  242. u8 isfinal;
  243. };
  244. struct chcr_ahash_req_ctx {
  245. struct chcr_hctx_per_wr hctx_wr;
  246. u8 *reqbfr;
  247. u8 *skbfr;
  248. /* SKB which is being sent to the hardware for processing */
  249. u64 data_len; /* Data len till time */
  250. u16 txqidx;
  251. u16 rxqidx;
  252. u8 reqlen;
  253. u8 partial_hash[CHCR_HASH_MAX_DIGEST_SIZE];
  254. u8 bfr1[CHCR_HASH_MAX_BLOCK_SIZE_128];
  255. u8 bfr2[CHCR_HASH_MAX_BLOCK_SIZE_128];
  256. };
  257. struct chcr_skcipher_req_ctx {
  258. struct sk_buff *skb;
  259. struct scatterlist *dstsg;
  260. unsigned int processed;
  261. unsigned int last_req_len;
  262. unsigned int partial_req;
  263. struct scatterlist *srcsg;
  264. unsigned int src_ofst;
  265. unsigned int dst_ofst;
  266. unsigned int op;
  267. u16 imm;
  268. u8 iv[CHCR_MAX_CRYPTO_IV_LEN];
  269. u8 init_iv[CHCR_MAX_CRYPTO_IV_LEN];
  270. u16 txqidx;
  271. u16 rxqidx;
  272. struct skcipher_request fallback_req; // keep at the end
  273. };
  274. struct chcr_alg_template {
  275. u32 type;
  276. u32 is_registered;
  277. union {
  278. struct skcipher_alg skcipher;
  279. struct ahash_alg hash;
  280. struct aead_alg aead;
  281. } alg;
  282. };
  283. typedef struct sk_buff *(*create_wr_t)(struct aead_request *req,
  284. unsigned short qid,
  285. int size);
  286. void chcr_verify_tag(struct aead_request *req, u8 *input, int *err);
  287. int chcr_aead_dma_map(struct device *dev, struct aead_request *req,
  288. unsigned short op_type);
  289. void chcr_aead_dma_unmap(struct device *dev, struct aead_request *req,
  290. unsigned short op_type);
  291. void chcr_add_aead_dst_ent(struct aead_request *req,
  292. struct cpl_rx_phys_dsgl *phys_cpl,
  293. unsigned short qid);
  294. void chcr_add_aead_src_ent(struct aead_request *req, struct ulptx_sgl *ulptx);
  295. void chcr_add_cipher_src_ent(struct skcipher_request *req,
  296. void *ulptx,
  297. struct cipher_wr_param *wrparam);
  298. int chcr_cipher_dma_map(struct device *dev, struct skcipher_request *req);
  299. void chcr_cipher_dma_unmap(struct device *dev, struct skcipher_request *req);
  300. void chcr_add_cipher_dst_ent(struct skcipher_request *req,
  301. struct cpl_rx_phys_dsgl *phys_cpl,
  302. struct cipher_wr_param *wrparam,
  303. unsigned short qid);
  304. int sg_nents_len_skip(struct scatterlist *sg, u64 len, u64 skip);
  305. void chcr_add_hash_src_ent(struct ahash_request *req, struct ulptx_sgl *ulptx,
  306. struct hash_wr_param *param);
  307. int chcr_hash_dma_map(struct device *dev, struct ahash_request *req);
  308. void chcr_hash_dma_unmap(struct device *dev, struct ahash_request *req);
  309. void chcr_aead_common_exit(struct aead_request *req);
  310. #endif /* __CHCR_CRYPTO_H__ */