1/* SPDX-License-Identifier: GPL-2.0-only */2/*3* Copyright 2016 Broadcom4*/56#ifndef _CIPHER_H7#define _CIPHER_H89#include <linux/atomic.h>10#include <linux/mailbox/brcm-message.h>11#include <linux/mailbox_client.h>12#include <crypto/aes.h>13#include <crypto/internal/hash.h>14#include <crypto/internal/skcipher.h>15#include <crypto/aead.h>16#include <crypto/arc4.h>17#include <crypto/gcm.h>18#include <crypto/sha1.h>19#include <crypto/sha2.h>20#include <crypto/sha3.h>2122#include "spu.h"23#include "spum.h"24#include "spu2.h"2526/* Driver supports up to MAX_SPUS SPU blocks */27#define MAX_SPUS 162829#define ARC4_STATE_SIZE 43031#define CCM_AES_IV_SIZE 1632#define CCM_ESP_IV_SIZE 833#define RFC4543_ICV_SIZE 163435#define MAX_KEY_SIZE ARC4_MAX_KEY_SIZE36#define MAX_IV_SIZE AES_BLOCK_SIZE37#define MAX_DIGEST_SIZE SHA3_512_DIGEST_SIZE38#define MAX_ASSOC_SIZE 5123940/* size of salt value for AES-GCM-ESP and AES-CCM-ESP */41#define GCM_ESP_SALT_SIZE 442#define CCM_ESP_SALT_SIZE 343#define MAX_SALT_SIZE GCM_ESP_SALT_SIZE44#define GCM_ESP_SALT_OFFSET 045#define CCM_ESP_SALT_OFFSET 14647#define GCM_ESP_DIGESTSIZE 164849#define MAX_HASH_BLOCK_SIZE SHA512_BLOCK_SIZE5051/*52* Maximum number of bytes from a non-final hash request that can be deferred53* until more data is available. With new crypto API framework, this54* can be no more than one block of data.55*/56#define HASH_CARRY_MAX MAX_HASH_BLOCK_SIZE5758/* Force at least 4-byte alignment of all SPU message fields */59#define SPU_MSG_ALIGN 46061/* Number of times to resend mailbox message if mb queue is full */62#define SPU_MB_RETRY_MAX 10006364/* op_counts[] indexes */65enum op_type {66SPU_OP_CIPHER,67SPU_OP_HASH,68SPU_OP_HMAC,69SPU_OP_AEAD,70SPU_OP_NUM71};7273enum spu_spu_type {74SPU_TYPE_SPUM,75SPU_TYPE_SPU2,76};7778/*79* SPUM_NS2 and SPUM_NSP are the SPU-M block on Northstar 2 and Northstar Plus,80* respectively.81*/82enum spu_spu_subtype {83SPU_SUBTYPE_SPUM_NS2,84SPU_SUBTYPE_SPUM_NSP,85SPU_SUBTYPE_SPU2_V1,86SPU_SUBTYPE_SPU2_V287};8889struct spu_type_subtype {90enum spu_spu_type type;91enum spu_spu_subtype subtype;92};9394struct cipher_op {95enum spu_cipher_alg alg;96enum spu_cipher_mode mode;97};9899struct auth_op {100enum hash_alg alg;101enum hash_mode mode;102};103104struct iproc_alg_s {105u32 type;106union {107struct skcipher_alg skcipher;108struct ahash_alg hash;109struct aead_alg aead;110} alg;111struct cipher_op cipher_info;112struct auth_op auth_info;113bool auth_first;114bool registered;115};116117/*118* Buffers for a SPU request/reply message pair. All part of one structure to119* allow a single alloc per request.120*/121struct spu_msg_buf {122/* Request message fragments */123124/*125* SPU request message header. For SPU-M, holds MH, EMH, SCTX, BDESC,126* and BD header. For SPU2, holds FMD, OMD.127*/128u8 bcm_spu_req_hdr[ALIGN(SPU2_HEADER_ALLOC_LEN, SPU_MSG_ALIGN)];129130/* IV or counter. Size to include salt. Also used for XTS tweek. */131u8 iv_ctr[ALIGN(2 * AES_BLOCK_SIZE, SPU_MSG_ALIGN)];132133/* Hash digest. request and response. */134u8 digest[ALIGN(MAX_DIGEST_SIZE, SPU_MSG_ALIGN)];135136/* SPU request message padding */137u8 spu_req_pad[ALIGN(SPU_PAD_LEN_MAX, SPU_MSG_ALIGN)];138139/* SPU-M request message STATUS field */140u8 tx_stat[ALIGN(SPU_TX_STATUS_LEN, SPU_MSG_ALIGN)];141142/* Response message fragments */143144/* SPU response message header */145u8 spu_resp_hdr[ALIGN(SPU2_HEADER_ALLOC_LEN, SPU_MSG_ALIGN)];146147/* SPU response message STATUS field padding */148u8 rx_stat_pad[ALIGN(SPU_STAT_PAD_MAX, SPU_MSG_ALIGN)];149150/* SPU response message STATUS field */151u8 rx_stat[ALIGN(SPU_RX_STATUS_LEN, SPU_MSG_ALIGN)];152153union {154/* Buffers only used for skcipher */155struct {156/*157* Field used for either SUPDT when RC4 is used158* -OR- tweak value when XTS/AES is used159*/160u8 supdt_tweak[ALIGN(SPU_SUPDT_LEN, SPU_MSG_ALIGN)];161} c;162163/* Buffers only used for aead */164struct {165/* SPU response pad for GCM data */166u8 gcmpad[ALIGN(AES_BLOCK_SIZE, SPU_MSG_ALIGN)];167168/* SPU request msg padding for GCM AAD */169u8 req_aad_pad[ALIGN(SPU_PAD_LEN_MAX, SPU_MSG_ALIGN)];170171/* SPU response data to be discarded */172u8 resp_aad[ALIGN(MAX_ASSOC_SIZE + MAX_IV_SIZE,173SPU_MSG_ALIGN)];174} a;175};176};177178struct iproc_ctx_s {179u8 enckey[MAX_KEY_SIZE + ARC4_STATE_SIZE];180unsigned int enckeylen;181182u8 authkey[MAX_KEY_SIZE + ARC4_STATE_SIZE];183unsigned int authkeylen;184185u8 salt[MAX_SALT_SIZE];186unsigned int salt_len;187unsigned int salt_offset;188u8 iv[MAX_IV_SIZE];189190unsigned int digestsize;191192struct iproc_alg_s *alg;193bool is_esp;194195struct cipher_op cipher;196enum spu_cipher_type cipher_type;197198struct auth_op auth;199bool auth_first;200201/*202* The maximum length in bytes of the payload in a SPU message for this203* context. For SPU-M, the payload is the combination of AAD and data.204* For SPU2, the payload is just data. A value of SPU_MAX_PAYLOAD_INF205* indicates that there is no limit to the length of the SPU message206* payload.207*/208unsigned int max_payload;209210struct crypto_aead *fallback_cipher;211212/* auth_type is determined during processing of request */213214u8 ipad[MAX_HASH_BLOCK_SIZE];215u8 opad[MAX_HASH_BLOCK_SIZE];216217/*218* Buffer to hold SPU message header template. Template is created at219* setkey time for skcipher requests, since most of the fields in the220* header are known at that time. At request time, just fill in a few221* missing pieces related to length of data in the request and IVs, etc.222*/223u8 bcm_spu_req_hdr[ALIGN(SPU2_HEADER_ALLOC_LEN, SPU_MSG_ALIGN)];224225/* Length of SPU request header */226u16 spu_req_hdr_len;227228/* Expected length of SPU response header */229u16 spu_resp_hdr_len;230231/*232* shash descriptor - needed to perform incremental hashing in233* software, when hw doesn't support it.234*/235struct shash_desc *shash;236237bool is_rfc4543; /* RFC 4543 style of GMAC */238};239240/* state from iproc_reqctx_s necessary for hash state export/import */241struct spu_hash_export_s {242unsigned int total_todo;243unsigned int total_sent;244u8 hash_carry[HASH_CARRY_MAX];245unsigned int hash_carry_len;246u8 incr_hash[MAX_DIGEST_SIZE];247bool is_sw_hmac;248};249250struct iproc_reqctx_s {251/* general context */252struct crypto_async_request *parent;253254/* only valid after enqueue() */255struct iproc_ctx_s *ctx;256257u8 chan_idx; /* Mailbox channel to be used to submit this request */258259/* total todo, rx'd, and sent for this request */260unsigned int total_todo;261unsigned int total_received; /* only valid for skcipher */262unsigned int total_sent;263264/*265* num bytes sent to hw from the src sg in this request. This can differ266* from total_sent for incremental hashing. total_sent includes previous267* init() and update() data. src_sent does not.268*/269unsigned int src_sent;270271/*272* For AEAD requests, start of associated data. This will typically273* point to the beginning of the src scatterlist from the request,274* since assoc data is at the beginning of the src scatterlist rather275* than in its own sg.276*/277struct scatterlist *assoc;278279/*280* scatterlist entry and offset to start of data for next chunk. Crypto281* API src scatterlist for AEAD starts with AAD, if present. For first282* chunk, src_sg is sg entry at beginning of input data (after AAD).283* src_skip begins at the offset in that sg entry where data begins.284*/285struct scatterlist *src_sg;286int src_nents; /* Number of src entries with data */287u32 src_skip; /* bytes of current sg entry already used */288289/*290* Same for destination. For AEAD, if there is AAD, output data must291* be written at offset following AAD.292*/293struct scatterlist *dst_sg;294int dst_nents; /* Number of dst entries with data */295u32 dst_skip; /* bytes of current sg entry already written */296297/* Mailbox message used to send this request to PDC driver */298struct brcm_message mb_mssg;299300bool bd_suppress; /* suppress BD field in SPU response? */301302/* cipher context */303bool is_encrypt;304305/*306* CBC mode: IV. CTR mode: counter. Else empty. Used as a DMA307* buffer for AEAD requests. So allocate as DMAable memory. If IV308* concatenated with salt, includes the salt.309*/310u8 *iv_ctr;311/* Length of IV or counter, in bytes */312unsigned int iv_ctr_len;313314/*315* Hash requests can be of any size, whether initial, update, or final.316* A non-final request must be submitted to the SPU as an integral317* number of blocks. This may leave data at the end of the request318* that is not a full block. Since the request is non-final, it cannot319* be padded. So, we write the remainder to this hash_carry buffer and320* hold it until the next request arrives. The carry data is then321* submitted at the beginning of the data in the next SPU msg.322* hash_carry_len is the number of bytes currently in hash_carry. These323* fields are only used for ahash requests.324*/325u8 hash_carry[HASH_CARRY_MAX];326unsigned int hash_carry_len;327unsigned int is_final; /* is this the final for the hash op? */328329/*330* Digest from incremental hash is saved here to include in next hash331* operation. Cannot be stored in req->result for truncated hashes,332* since result may be sized for final digest. Cannot be saved in333* msg_buf because that gets deleted between incremental hash ops334* and is not saved as part of export().335*/336u8 incr_hash[MAX_DIGEST_SIZE];337338/* hmac context */339bool is_sw_hmac;340341gfp_t gfp;342343/* Buffers used to build SPU request and response messages */344struct spu_msg_buf msg_buf;345346struct aead_request req;347};348349/*350* Structure encapsulates a set of function pointers specific to the type of351* SPU hardware running. These functions handling creation and parsing of352* SPU request messages and SPU response messages. Includes hardware-specific353* values read from device tree.354*/355struct spu_hw {356void (*spu_dump_msg_hdr)(u8 *buf, unsigned int buf_len);357u32 (*spu_ctx_max_payload)(enum spu_cipher_alg cipher_alg,358enum spu_cipher_mode cipher_mode,359unsigned int blocksize);360u32 (*spu_payload_length)(u8 *spu_hdr);361u16 (*spu_response_hdr_len)(u16 auth_key_len, u16 enc_key_len,362bool is_hash);363u16 (*spu_hash_pad_len)(enum hash_alg hash_alg,364enum hash_mode hash_mode, u32 chunksize,365u16 hash_block_size);366u32 (*spu_gcm_ccm_pad_len)(enum spu_cipher_mode cipher_mode,367unsigned int data_size);368u32 (*spu_assoc_resp_len)(enum spu_cipher_mode cipher_mode,369unsigned int assoc_len,370unsigned int iv_len, bool is_encrypt);371u8 (*spu_aead_ivlen)(enum spu_cipher_mode cipher_mode,372u16 iv_len);373enum hash_type (*spu_hash_type)(u32 src_sent);374u32 (*spu_digest_size)(u32 digest_size, enum hash_alg alg,375enum hash_type);376u32 (*spu_create_request)(u8 *spu_hdr,377struct spu_request_opts *req_opts,378struct spu_cipher_parms *cipher_parms,379struct spu_hash_parms *hash_parms,380struct spu_aead_parms *aead_parms,381unsigned int data_size);382u16 (*spu_cipher_req_init)(u8 *spu_hdr,383struct spu_cipher_parms *cipher_parms);384void (*spu_cipher_req_finish)(u8 *spu_hdr,385u16 spu_req_hdr_len,386unsigned int is_inbound,387struct spu_cipher_parms *cipher_parms,388unsigned int data_size);389void (*spu_request_pad)(u8 *pad_start, u32 gcm_padding,390u32 hash_pad_len, enum hash_alg auth_alg,391enum hash_mode auth_mode,392unsigned int total_sent, u32 status_padding);393u8 (*spu_xts_tweak_in_payload)(void);394u8 (*spu_tx_status_len)(void);395u8 (*spu_rx_status_len)(void);396int (*spu_status_process)(u8 *statp);397void (*spu_ccm_update_iv)(unsigned int digestsize,398struct spu_cipher_parms *cipher_parms,399unsigned int assoclen, unsigned int chunksize,400bool is_encrypt, bool is_esp);401u32 (*spu_wordalign_padlen)(u32 data_size);402403/* The base virtual address of the SPU hw registers */404void __iomem *reg_vbase[MAX_SPUS];405406/* Version of the SPU hardware */407enum spu_spu_type spu_type;408409/* Sub-version of the SPU hardware */410enum spu_spu_subtype spu_subtype;411412/* The number of SPUs on this platform */413u32 num_spu;414415/* The number of SPU channels on this platform */416u32 num_chan;417};418419struct bcm_device_private {420struct platform_device *pdev;421422struct spu_hw spu;423424atomic_t session_count; /* number of streams active */425atomic_t stream_count; /* monotonic counter for streamID's */426427/* Length of BCM header. Set to 0 when hw does not expect BCM HEADER. */428u8 bcm_hdr_len;429430/* The index of the channel to use for the next crypto request */431atomic_t next_chan;432433struct dentry *debugfs_dir;434struct dentry *debugfs_stats;435436/* Number of request bytes processed and result bytes returned */437atomic64_t bytes_in;438atomic64_t bytes_out;439440/* Number of operations of each type */441atomic_t op_counts[SPU_OP_NUM];442443atomic_t cipher_cnt[CIPHER_ALG_LAST][CIPHER_MODE_LAST];444atomic_t hash_cnt[HASH_ALG_LAST];445atomic_t hmac_cnt[HASH_ALG_LAST];446atomic_t aead_cnt[AEAD_TYPE_LAST];447448/* Number of calls to setkey() for each operation type */449atomic_t setkey_cnt[SPU_OP_NUM];450451/* Number of times request was resubmitted because mb was full */452atomic_t mb_no_spc;453454/* Number of mailbox send failures */455atomic_t mb_send_fail;456457/* Number of ICV check failures for AEAD messages */458atomic_t bad_icv;459460struct mbox_client mcl;461462/* Array of mailbox channel pointers, one for each channel */463struct mbox_chan **mbox;464};465466extern struct bcm_device_private iproc_priv;467468#endif469470471