Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-only */
0002 /*
0003  * Copyright 2016 Broadcom
0004  */
0005 
0006 /*
0007  * This file contains the definition of SPU messages. There are currently two
0008  * SPU message formats: SPU-M and SPU2. The hardware uses different values to
0009  * identify the same things in SPU-M vs SPU2. So this file defines values that
0010  * are hardware independent. Software can use these values for any version of
0011  * SPU hardware. These values are used in APIs in spu.c. Functions internal to
0012  * spu.c and spu2.c convert these to hardware-specific values.
0013  */
0014 
0015 #ifndef _SPU_H
0016 #define _SPU_H
0017 
0018 #include <linux/types.h>
0019 #include <linux/scatterlist.h>
0020 #include <crypto/sha1.h>
0021 #include <crypto/sha2.h>
0022 
0023 enum spu_cipher_alg {
0024     CIPHER_ALG_NONE = 0x0,
0025     CIPHER_ALG_RC4 = 0x1,
0026     CIPHER_ALG_DES = 0x2,
0027     CIPHER_ALG_3DES = 0x3,
0028     CIPHER_ALG_AES = 0x4,
0029     CIPHER_ALG_LAST = 0x5
0030 };
0031 
0032 enum spu_cipher_mode {
0033     CIPHER_MODE_NONE = 0x0,
0034     CIPHER_MODE_ECB = 0x0,
0035     CIPHER_MODE_CBC = 0x1,
0036     CIPHER_MODE_OFB = 0x2,
0037     CIPHER_MODE_CFB = 0x3,
0038     CIPHER_MODE_CTR = 0x4,
0039     CIPHER_MODE_CCM = 0x5,
0040     CIPHER_MODE_GCM = 0x6,
0041     CIPHER_MODE_XTS = 0x7,
0042     CIPHER_MODE_LAST = 0x8
0043 };
0044 
0045 enum spu_cipher_type {
0046     CIPHER_TYPE_NONE = 0x0,
0047     CIPHER_TYPE_DES = 0x0,
0048     CIPHER_TYPE_3DES = 0x0,
0049     CIPHER_TYPE_INIT = 0x0, /* used for ARC4 */
0050     CIPHER_TYPE_AES128 = 0x0,
0051     CIPHER_TYPE_AES192 = 0x1,
0052     CIPHER_TYPE_UPDT = 0x1, /* used for ARC4 */
0053     CIPHER_TYPE_AES256 = 0x2,
0054 };
0055 
0056 enum hash_alg {
0057     HASH_ALG_NONE = 0x0,
0058     HASH_ALG_MD5 = 0x1,
0059     HASH_ALG_SHA1 = 0x2,
0060     HASH_ALG_SHA224 = 0x3,
0061     HASH_ALG_SHA256 = 0x4,
0062     HASH_ALG_AES = 0x5,
0063     HASH_ALG_SHA384 = 0x6,
0064     HASH_ALG_SHA512 = 0x7,
0065     /* Keep SHA3 algorithms at the end always */
0066     HASH_ALG_SHA3_224 = 0x8,
0067     HASH_ALG_SHA3_256 = 0x9,
0068     HASH_ALG_SHA3_384 = 0xa,
0069     HASH_ALG_SHA3_512 = 0xb,
0070     HASH_ALG_LAST
0071 };
0072 
0073 enum hash_mode {
0074     HASH_MODE_NONE = 0x0,
0075     HASH_MODE_HASH = 0x0,
0076     HASH_MODE_XCBC = 0x0,
0077     HASH_MODE_CMAC = 0x1,
0078     HASH_MODE_CTXT = 0x1,
0079     HASH_MODE_HMAC = 0x2,
0080     HASH_MODE_RABIN = 0x4,
0081     HASH_MODE_FHMAC = 0x6,
0082     HASH_MODE_CCM = 0x5,
0083     HASH_MODE_GCM = 0x6,
0084 };
0085 
0086 enum hash_type {
0087     HASH_TYPE_NONE = 0x0,
0088     HASH_TYPE_FULL = 0x0,
0089     HASH_TYPE_INIT = 0x1,
0090     HASH_TYPE_UPDT = 0x2,
0091     HASH_TYPE_FIN = 0x3,
0092     HASH_TYPE_AES128 = 0x0,
0093     HASH_TYPE_AES192 = 0x1,
0094     HASH_TYPE_AES256 = 0x2
0095 };
0096 
0097 enum aead_type {
0098     AES_CCM,
0099     AES_GCM,
0100     AUTHENC,
0101     AEAD_TYPE_LAST
0102 };
0103 
0104 extern char *hash_alg_name[HASH_ALG_LAST];
0105 extern char *aead_alg_name[AEAD_TYPE_LAST];
0106 
0107 struct spu_request_opts {
0108     bool is_inbound;
0109     bool auth_first;
0110     bool is_aead;
0111     bool is_esp;
0112     bool bd_suppress;
0113     bool is_rfc4543;
0114 };
0115 
0116 struct spu_cipher_parms {
0117     enum spu_cipher_alg  alg;
0118     enum spu_cipher_mode mode;
0119     enum spu_cipher_type type;
0120     u8                  *key_buf;
0121     u16                  key_len;
0122     /* iv_buf and iv_len include salt, if applicable */
0123     u8                  *iv_buf;
0124     u16                  iv_len;
0125 };
0126 
0127 struct spu_hash_parms {
0128     enum hash_alg  alg;
0129     enum hash_mode mode;
0130     enum hash_type type;
0131     u8             digestsize;
0132     u8            *key_buf;
0133     u16            key_len;
0134     u16            prebuf_len;
0135     /* length of hash pad. signed, needs to handle roll-overs */
0136     int            pad_len;
0137 };
0138 
0139 struct spu_aead_parms {
0140     u32 assoc_size;
0141     u16 iv_len;      /* length of IV field between assoc data and data */
0142     u8  aad_pad_len; /* For AES GCM/CCM, length of padding after AAD */
0143     u8  data_pad_len;/* For AES GCM/CCM, length of padding after data */
0144     bool return_iv;  /* True if SPU should return an IV */
0145     u32 ret_iv_len;  /* Length in bytes of returned IV */
0146     u32 ret_iv_off;  /* Offset into full IV if partial IV returned */
0147 };
0148 
0149 /************** SPU sizes ***************/
0150 
0151 #define SPU_RX_STATUS_LEN  4
0152 
0153 /* Max length of padding for 4-byte alignment of STATUS field */
0154 #define SPU_STAT_PAD_MAX  4
0155 
0156 /* Max length of pad fragment. 4 is for 4-byte alignment of STATUS field */
0157 #define SPU_PAD_LEN_MAX (SPU_GCM_CCM_ALIGN + MAX_HASH_BLOCK_SIZE + \
0158              SPU_STAT_PAD_MAX)
0159 
0160 /* GCM and CCM require 16-byte alignment */
0161 #define SPU_GCM_CCM_ALIGN 16
0162 
0163 /* Length up SUPDT field in SPU response message for RC4 */
0164 #define SPU_SUPDT_LEN 260
0165 
0166 /* SPU status error codes. These used as common error codes across all
0167  * SPU variants.
0168  */
0169 #define SPU_INVALID_ICV  1
0170 
0171 /* Indicates no limit to the length of the payload in a SPU message */
0172 #define SPU_MAX_PAYLOAD_INF  0xFFFFFFFF
0173 
0174 /* Size of XTS tweak ("i" parameter), in bytes */
0175 #define SPU_XTS_TWEAK_SIZE 16
0176 
0177 /* CCM B_0 field definitions, common for SPU-M and SPU2 */
0178 #define CCM_B0_ADATA        0x40
0179 #define CCM_B0_ADATA_SHIFT     6
0180 #define CCM_B0_M_PRIME      0x38
0181 #define CCM_B0_M_PRIME_SHIFT       3
0182 #define CCM_B0_L_PRIME      0x07
0183 #define CCM_B0_L_PRIME_SHIFT       0
0184 #define CCM_ESP_L_VALUE        4
0185 
0186 /**
0187  * spu_req_incl_icv() - Return true if SPU request message should include the
0188  * ICV as a separate buffer.
0189  * @cipher_mode:  the cipher mode being requested
0190  * @is_encrypt:   true if encrypting. false if decrypting.
0191  *
0192  * Return:  true if ICV to be included as separate buffer
0193  */
0194 static __always_inline  bool spu_req_incl_icv(enum spu_cipher_mode cipher_mode,
0195                           bool is_encrypt)
0196 {
0197     if ((cipher_mode == CIPHER_MODE_GCM) && !is_encrypt)
0198         return true;
0199     if ((cipher_mode == CIPHER_MODE_CCM) && !is_encrypt)
0200         return true;
0201 
0202     return false;
0203 }
0204 
0205 static __always_inline u32 spu_real_db_size(u32 assoc_size,
0206                         u32 aead_iv_buf_len,
0207                         u32 prebuf_len,
0208                         u32 data_size,
0209                         u32 aad_pad_len,
0210                         u32 gcm_pad_len,
0211                         u32 hash_pad_len)
0212 {
0213     return assoc_size + aead_iv_buf_len + prebuf_len + data_size +
0214         aad_pad_len + gcm_pad_len + hash_pad_len;
0215 }
0216 
0217 /************** SPU Functions Prototypes **************/
0218 
0219 void spum_dump_msg_hdr(u8 *buf, unsigned int buf_len);
0220 
0221 u32 spum_ns2_ctx_max_payload(enum spu_cipher_alg cipher_alg,
0222                  enum spu_cipher_mode cipher_mode,
0223                  unsigned int blocksize);
0224 u32 spum_nsp_ctx_max_payload(enum spu_cipher_alg cipher_alg,
0225                  enum spu_cipher_mode cipher_mode,
0226                  unsigned int blocksize);
0227 u32 spum_payload_length(u8 *spu_hdr);
0228 u16 spum_response_hdr_len(u16 auth_key_len, u16 enc_key_len, bool is_hash);
0229 u16 spum_hash_pad_len(enum hash_alg hash_alg, enum hash_mode hash_mode,
0230               u32 chunksize, u16 hash_block_size);
0231 u32 spum_gcm_ccm_pad_len(enum spu_cipher_mode cipher_mode,
0232              unsigned int data_size);
0233 u32 spum_assoc_resp_len(enum spu_cipher_mode cipher_mode,
0234             unsigned int assoc_len, unsigned int iv_len,
0235             bool is_encrypt);
0236 u8 spum_aead_ivlen(enum spu_cipher_mode cipher_mode, u16 iv_len);
0237 bool spu_req_incl_icv(enum spu_cipher_mode cipher_mode, bool is_encrypt);
0238 enum hash_type spum_hash_type(u32 src_sent);
0239 u32 spum_digest_size(u32 alg_digest_size, enum hash_alg alg,
0240              enum hash_type htype);
0241 
0242 u32 spum_create_request(u8 *spu_hdr,
0243             struct spu_request_opts *req_opts,
0244             struct spu_cipher_parms *cipher_parms,
0245             struct spu_hash_parms *hash_parms,
0246             struct spu_aead_parms *aead_parms,
0247             unsigned int data_size);
0248 
0249 u16 spum_cipher_req_init(u8 *spu_hdr, struct spu_cipher_parms *cipher_parms);
0250 
0251 void spum_cipher_req_finish(u8 *spu_hdr,
0252                 u16 spu_req_hdr_len,
0253                 unsigned int is_inbound,
0254                 struct spu_cipher_parms *cipher_parms,
0255                 unsigned int data_size);
0256 
0257 void spum_request_pad(u8 *pad_start,
0258               u32 gcm_padding,
0259               u32 hash_pad_len,
0260               enum hash_alg auth_alg,
0261               enum hash_mode auth_mode,
0262               unsigned int total_sent, u32 status_padding);
0263 
0264 u8 spum_xts_tweak_in_payload(void);
0265 u8 spum_tx_status_len(void);
0266 u8 spum_rx_status_len(void);
0267 int spum_status_process(u8 *statp);
0268 
0269 void spum_ccm_update_iv(unsigned int digestsize,
0270             struct spu_cipher_parms *cipher_parms,
0271             unsigned int assoclen,
0272             unsigned int chunksize,
0273             bool is_encrypt,
0274             bool is_esp);
0275 u32 spum_wordalign_padlen(u32 data_size);
0276 #endif