Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * This file is part of the Chelsio T6 Crypto driver for Linux.
0003  *
0004  * Copyright (c) 2003-2016 Chelsio Communications, Inc. All rights reserved.
0005  *
0006  * This software is available to you under a choice of one of two
0007  * licenses.  You may choose to be licensed under the terms of the GNU
0008  * General Public License (GPL) Version 2, available from the file
0009  * COPYING in the main directory of this source tree, or the
0010  * OpenIB.org BSD license below:
0011  *
0012  *     Redistribution and use in source and binary forms, with or
0013  *     without modification, are permitted provided that the following
0014  *     conditions are met:
0015  *
0016  *      - Redistributions of source code must retain the above
0017  *        copyright notice, this list of conditions and the following
0018  *        disclaimer.
0019  *
0020  *      - Redistributions in binary form must reproduce the above
0021  *        copyright notice, this list of conditions and the following
0022  *        disclaimer in the documentation and/or other materials
0023  *        provided with the distribution.
0024  *
0025  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
0026  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
0027  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
0028  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
0029  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
0030  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
0031  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
0032  * SOFTWARE.
0033  *
0034  */
0035 
0036 #ifndef __CHCR_CRYPTO_H__
0037 #define __CHCR_CRYPTO_H__
0038 
0039 #define GHASH_BLOCK_SIZE    16
0040 #define GHASH_DIGEST_SIZE   16
0041 
0042 #define CCM_B0_SIZE             16
0043 #define CCM_AAD_FIELD_SIZE      2
0044 // 511 - 16(For IV)
0045 #define T6_MAX_AAD_SIZE 495
0046 
0047 
0048 /* Define following if h/w is not dropping the AAD and IV data before
0049  * giving the processed data
0050  */
0051 
0052 #define CHCR_CRA_PRIORITY 500
0053 #define CHCR_AEAD_PRIORITY 6000
0054 #define CHCR_AES_MAX_KEY_LEN  (2 * (AES_MAX_KEY_SIZE)) /* consider xts */
0055 #define CHCR_MAX_CRYPTO_IV_LEN 16 /* AES IV len */
0056 
0057 #define CHCR_MAX_AUTHENC_AES_KEY_LEN 32 /* max aes key length*/
0058 #define CHCR_MAX_AUTHENC_SHA_KEY_LEN 128 /* max sha key length*/
0059 
0060 #define CHCR_GIVENCRYPT_OP 2
0061 /* CPL/SCMD parameters */
0062 
0063 #define CHCR_ENCRYPT_OP 0
0064 #define CHCR_DECRYPT_OP 1
0065 
0066 #define CHCR_SCMD_SEQ_NO_CTRL_32BIT     1
0067 #define CHCR_SCMD_SEQ_NO_CTRL_48BIT     2
0068 #define CHCR_SCMD_SEQ_NO_CTRL_64BIT     3
0069 
0070 #define CHCR_SCMD_PROTO_VERSION_GENERIC 4
0071 
0072 #define CHCR_SCMD_AUTH_CTRL_AUTH_CIPHER 0
0073 #define CHCR_SCMD_AUTH_CTRL_CIPHER_AUTH 1
0074 
0075 #define CHCR_SCMD_CIPHER_MODE_NOP               0
0076 #define CHCR_SCMD_CIPHER_MODE_AES_CBC           1
0077 #define CHCR_SCMD_CIPHER_MODE_AES_GCM           2
0078 #define CHCR_SCMD_CIPHER_MODE_AES_CTR           3
0079 #define CHCR_SCMD_CIPHER_MODE_GENERIC_AES       4
0080 #define CHCR_SCMD_CIPHER_MODE_AES_XTS           6
0081 #define CHCR_SCMD_CIPHER_MODE_AES_CCM           7
0082 
0083 #define CHCR_SCMD_AUTH_MODE_NOP             0
0084 #define CHCR_SCMD_AUTH_MODE_SHA1            1
0085 #define CHCR_SCMD_AUTH_MODE_SHA224          2
0086 #define CHCR_SCMD_AUTH_MODE_SHA256          3
0087 #define CHCR_SCMD_AUTH_MODE_GHASH           4
0088 #define CHCR_SCMD_AUTH_MODE_SHA512_224      5
0089 #define CHCR_SCMD_AUTH_MODE_SHA512_256      6
0090 #define CHCR_SCMD_AUTH_MODE_SHA512_384      7
0091 #define CHCR_SCMD_AUTH_MODE_SHA512_512      8
0092 #define CHCR_SCMD_AUTH_MODE_CBCMAC          9
0093 #define CHCR_SCMD_AUTH_MODE_CMAC            10
0094 
0095 #define CHCR_SCMD_HMAC_CTRL_NOP             0
0096 #define CHCR_SCMD_HMAC_CTRL_NO_TRUNC        1
0097 #define CHCR_SCMD_HMAC_CTRL_TRUNC_RFC4366   2
0098 #define CHCR_SCMD_HMAC_CTRL_IPSEC_96BIT     3
0099 #define CHCR_SCMD_HMAC_CTRL_PL1         4
0100 #define CHCR_SCMD_HMAC_CTRL_PL2         5
0101 #define CHCR_SCMD_HMAC_CTRL_PL3         6
0102 #define CHCR_SCMD_HMAC_CTRL_DIV2        7
0103 #define VERIFY_HW 0
0104 #define VERIFY_SW 1
0105 
0106 #define CHCR_SCMD_IVGEN_CTRL_HW             0
0107 #define CHCR_SCMD_IVGEN_CTRL_SW             1
0108 /* This are not really mac key size. They are intermediate values
0109  * of sha engine and its size
0110  */
0111 #define CHCR_KEYCTX_MAC_KEY_SIZE_128        0
0112 #define CHCR_KEYCTX_MAC_KEY_SIZE_160        1
0113 #define CHCR_KEYCTX_MAC_KEY_SIZE_192        2
0114 #define CHCR_KEYCTX_MAC_KEY_SIZE_256        3
0115 #define CHCR_KEYCTX_MAC_KEY_SIZE_512        4
0116 #define CHCR_KEYCTX_CIPHER_KEY_SIZE_128     0
0117 #define CHCR_KEYCTX_CIPHER_KEY_SIZE_192     1
0118 #define CHCR_KEYCTX_CIPHER_KEY_SIZE_256     2
0119 #define CHCR_KEYCTX_NO_KEY                  15
0120 
0121 #define CHCR_CPL_FW4_PLD_IV_OFFSET          (5 * 64) /* bytes. flt #5 and #6 */
0122 #define CHCR_CPL_FW4_PLD_HASH_RESULT_OFFSET (7 * 64) /* bytes. flt #7 */
0123 #define CHCR_CPL_FW4_PLD_DATA_SIZE          (4 * 64) /* bytes. flt #4 to #7 */
0124 
0125 #define KEY_CONTEXT_HDR_SALT_AND_PAD        16
0126 #define flits_to_bytes(x)  (x * 8)
0127 
0128 #define IV_NOP                  0
0129 #define IV_IMMEDIATE            1
0130 #define IV_DSGL         2
0131 
0132 #define AEAD_H_SIZE             16
0133 
0134 #define CRYPTO_ALG_SUB_TYPE_MASK            0x0f000000
0135 #define CRYPTO_ALG_SUB_TYPE_HASH_HMAC       0x01000000
0136 #define CRYPTO_ALG_SUB_TYPE_AEAD_RFC4106    0x02000000
0137 #define CRYPTO_ALG_SUB_TYPE_AEAD_GCM        0x03000000
0138 #define CRYPTO_ALG_SUB_TYPE_CBC_SHA     0x04000000
0139 #define CRYPTO_ALG_SUB_TYPE_AEAD_CCM        0x05000000
0140 #define CRYPTO_ALG_SUB_TYPE_AEAD_RFC4309    0x06000000
0141 #define CRYPTO_ALG_SUB_TYPE_CBC_NULL        0x07000000
0142 #define CRYPTO_ALG_SUB_TYPE_CTR             0x08000000
0143 #define CRYPTO_ALG_SUB_TYPE_CTR_RFC3686     0x09000000
0144 #define CRYPTO_ALG_SUB_TYPE_XTS         0x0a000000
0145 #define CRYPTO_ALG_SUB_TYPE_CBC         0x0b000000
0146 #define CRYPTO_ALG_SUB_TYPE_CTR_SHA     0x0c000000
0147 #define CRYPTO_ALG_SUB_TYPE_CTR_NULL   0x0d000000
0148 #define CRYPTO_ALG_TYPE_HMAC (CRYPTO_ALG_TYPE_AHASH |\
0149                   CRYPTO_ALG_SUB_TYPE_HASH_HMAC)
0150 
0151 #define MAX_SCRATCH_PAD_SIZE    32
0152 
0153 #define CHCR_HASH_MAX_BLOCK_SIZE_64  64
0154 #define CHCR_HASH_MAX_BLOCK_SIZE_128 128
0155 #define CHCR_SRC_SG_SIZE (0x10000 - sizeof(int))
0156 #define CHCR_DST_SG_SIZE 2048
0157 
0158 static inline struct chcr_context *a_ctx(struct crypto_aead *tfm)
0159 {
0160     return crypto_aead_ctx(tfm);
0161 }
0162 
0163 static inline struct chcr_context *c_ctx(struct crypto_skcipher *tfm)
0164 {
0165     return crypto_skcipher_ctx(tfm);
0166 }
0167 
0168 static inline struct chcr_context *h_ctx(struct crypto_ahash *tfm)
0169 {
0170     return crypto_tfm_ctx(crypto_ahash_tfm(tfm));
0171 }
0172 
0173 struct ablk_ctx {
0174     struct crypto_skcipher *sw_cipher;
0175     __be32 key_ctx_hdr;
0176     unsigned int enckey_len;
0177     unsigned char ciph_mode;
0178     u8 key[CHCR_AES_MAX_KEY_LEN];
0179     u8 nonce[4];
0180     u8 rrkey[AES_MAX_KEY_SIZE];
0181 };
0182 struct chcr_aead_reqctx {
0183     struct  sk_buff *skb;
0184     dma_addr_t iv_dma;
0185     dma_addr_t b0_dma;
0186     unsigned int b0_len;
0187     unsigned int op;
0188     u16 imm;
0189     u16 verify;
0190     u16 txqidx;
0191     u16 rxqidx;
0192     u8 iv[CHCR_MAX_CRYPTO_IV_LEN + MAX_SCRATCH_PAD_SIZE];
0193     u8 *scratch_pad;
0194 };
0195 
0196 struct ulptx_walk {
0197     struct ulptx_sgl *sgl;
0198     unsigned int nents;
0199     unsigned int pair_idx;
0200     unsigned int last_sg_len;
0201     struct scatterlist *last_sg;
0202     struct ulptx_sge_pair *pair;
0203 
0204 };
0205 
0206 struct dsgl_walk {
0207     unsigned int nents;
0208     unsigned int last_sg_len;
0209     struct scatterlist *last_sg;
0210     struct cpl_rx_phys_dsgl *dsgl;
0211     struct phys_sge_pairs *to;
0212 };
0213 
0214 struct chcr_gcm_ctx {
0215     u8 ghash_h[AEAD_H_SIZE];
0216 };
0217 
0218 struct chcr_authenc_ctx {
0219     u8 dec_rrkey[AES_MAX_KEY_SIZE];
0220     u8 h_iopad[2 * CHCR_HASH_MAX_DIGEST_SIZE];
0221     unsigned char auth_mode;
0222 };
0223 
0224 struct __aead_ctx {
0225     union {
0226         DECLARE_FLEX_ARRAY(struct chcr_gcm_ctx, gcm);
0227         DECLARE_FLEX_ARRAY(struct chcr_authenc_ctx, authenc);
0228     };
0229 };
0230 
0231 struct chcr_aead_ctx {
0232     __be32 key_ctx_hdr;
0233     unsigned int enckey_len;
0234     struct crypto_aead *sw_cipher;
0235     u8 salt[MAX_SALT];
0236     u8 key[CHCR_AES_MAX_KEY_LEN];
0237     u8 nonce[4];
0238     u16 hmac_ctrl;
0239     u16 mayverify;
0240     struct  __aead_ctx ctx[];
0241 };
0242 
0243 struct hmac_ctx {
0244     struct crypto_shash *base_hash;
0245     u8 ipad[CHCR_HASH_MAX_BLOCK_SIZE_128];
0246     u8 opad[CHCR_HASH_MAX_BLOCK_SIZE_128];
0247 };
0248 
0249 struct __crypto_ctx {
0250     union {
0251         DECLARE_FLEX_ARRAY(struct hmac_ctx, hmacctx);
0252         DECLARE_FLEX_ARRAY(struct ablk_ctx, ablkctx);
0253         DECLARE_FLEX_ARRAY(struct chcr_aead_ctx, aeadctx);
0254     };
0255 };
0256 
0257 struct chcr_context {
0258     struct chcr_dev *dev;
0259     unsigned char rxq_perchan;
0260     unsigned char txq_perchan;
0261     unsigned int  ntxq;
0262     unsigned int  nrxq;
0263     struct completion cbc_aes_aio_done;
0264     struct __crypto_ctx crypto_ctx[];
0265 };
0266 
0267 struct chcr_hctx_per_wr {
0268     struct scatterlist *srcsg;
0269     struct sk_buff *skb;
0270     dma_addr_t dma_addr;
0271     u32 dma_len;
0272     unsigned int src_ofst;
0273     unsigned int processed;
0274     u32 result;
0275     u8 is_sg_map;
0276     u8 imm;
0277     /*Final callback called. Driver cannot rely on nbytes to decide
0278      * final call
0279      */
0280     u8 isfinal;
0281 };
0282 
0283 struct chcr_ahash_req_ctx {
0284     struct chcr_hctx_per_wr hctx_wr;
0285     u8 *reqbfr;
0286     u8 *skbfr;
0287     /* SKB which is being sent to the hardware for processing */
0288     u64 data_len;  /* Data len till time */
0289     u16 txqidx;
0290     u16 rxqidx;
0291     u8 reqlen;
0292     u8 partial_hash[CHCR_HASH_MAX_DIGEST_SIZE];
0293     u8 bfr1[CHCR_HASH_MAX_BLOCK_SIZE_128];
0294     u8 bfr2[CHCR_HASH_MAX_BLOCK_SIZE_128];
0295 };
0296 
0297 struct chcr_skcipher_req_ctx {
0298     struct sk_buff *skb;
0299     struct scatterlist *dstsg;
0300     unsigned int processed;
0301     unsigned int last_req_len;
0302     unsigned int partial_req;
0303     struct scatterlist *srcsg;
0304     unsigned int src_ofst;
0305     unsigned int dst_ofst;
0306     unsigned int op;
0307     u16 imm;
0308     u8 iv[CHCR_MAX_CRYPTO_IV_LEN];
0309     u8 init_iv[CHCR_MAX_CRYPTO_IV_LEN];
0310     u16 txqidx;
0311     u16 rxqidx;
0312     struct skcipher_request fallback_req;   // keep at the end
0313 };
0314 
0315 struct chcr_alg_template {
0316     u32 type;
0317     u32 is_registered;
0318     union {
0319         struct skcipher_alg skcipher;
0320         struct ahash_alg hash;
0321         struct aead_alg aead;
0322     } alg;
0323 };
0324 
0325 typedef struct sk_buff *(*create_wr_t)(struct aead_request *req,
0326                        unsigned short qid,
0327                        int size);
0328 
0329 void chcr_verify_tag(struct aead_request *req, u8 *input, int *err);
0330 int chcr_aead_dma_map(struct device *dev, struct aead_request *req,
0331               unsigned short op_type);
0332 void chcr_aead_dma_unmap(struct device *dev, struct aead_request *req,
0333              unsigned short op_type);
0334 void chcr_add_aead_dst_ent(struct aead_request *req,
0335                struct cpl_rx_phys_dsgl *phys_cpl,
0336                unsigned short qid);
0337 void chcr_add_aead_src_ent(struct aead_request *req, struct ulptx_sgl *ulptx);
0338 void chcr_add_cipher_src_ent(struct skcipher_request *req,
0339                  void *ulptx,
0340                  struct  cipher_wr_param *wrparam);
0341 int chcr_cipher_dma_map(struct device *dev, struct skcipher_request *req);
0342 void chcr_cipher_dma_unmap(struct device *dev, struct skcipher_request *req);
0343 void chcr_add_cipher_dst_ent(struct skcipher_request *req,
0344                  struct cpl_rx_phys_dsgl *phys_cpl,
0345                  struct  cipher_wr_param *wrparam,
0346                  unsigned short qid);
0347 int sg_nents_len_skip(struct scatterlist *sg, u64 len, u64 skip);
0348 void chcr_add_hash_src_ent(struct ahash_request *req, struct ulptx_sgl *ulptx,
0349                struct hash_wr_param *param);
0350 int chcr_hash_dma_map(struct device *dev, struct ahash_request *req);
0351 void chcr_hash_dma_unmap(struct device *dev, struct ahash_request *req);
0352 void chcr_aead_common_exit(struct aead_request *req);
0353 #endif /* __CHCR_CRYPTO_H__ */