int flow_debug_logging;
module_param(flow_debug_logging, int, 0644);
MODULE_PARM_DESC(flow_debug_logging, "Enable Flow Debug Logging");
int packet_debug_logging;
module_param(packet_debug_logging, int, 0644);
MODULE_PARM_DESC(packet_debug_logging, "Enable Packet Debug Logging");
int debug_logging_sleep;
module_param(debug_logging_sleep, int, 0644);
MODULE_PARM_DESC(debug_logging_sleep, "Packet Debug Logging Sleep");
/* * The value of these module parameters is used to set the priority for each * algo type when this driver registers algos with the kernel crypto API. * To use a priority other than the default, set the priority in the insmod or * modprobe. Changing the module priority after init time has no effect. * * The default priorities are chosen to be lower (less preferred) than ARMv8 CE * algos, but more preferred than generic software algos.
*/ staticint cipher_pri = 150;
module_param(cipher_pri, int, 0644);
MODULE_PARM_DESC(cipher_pri, "Priority for cipher algos");
/* A type 3 BCM header, expected to precede the SPU header for SPU-M. * Bits 3 and 4 in the first byte encode the channel number (the dma ringset). * 0x60 - ring 0 * 0x68 - ring 1 * 0x70 - ring 2 * 0x78 - ring 3
*/ staticchar BCMHEADER[] = { 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x28 }; /* * Some SPU hw does not use BCM header on SPU messages. So BCM_HDR_LEN * is set dynamically after reading SPU type from device tree.
*/ #define BCM_HDR_LEN iproc_priv.bcm_hdr_len
/* min and max time to sleep before retrying when mbox queue is full. usec */ #define MBOX_SLEEP_MIN 800 #define MBOX_SLEEP_MAX 1000
/** * select_channel() - Select a SPU channel to handle a crypto request. Selects * channel in round robin order. * * Return: channel index
*/ static u8 select_channel(void)
{
u8 chan_idx = atomic_inc_return(&iproc_priv.next_chan);
return chan_idx % iproc_priv.spu.num_chan;
}
/** * spu_skcipher_rx_sg_create() - Build up the scatterlist of buffers used to * receive a SPU response message for an skcipher request. Includes buffers to * catch SPU message headers and the response data. * @mssg: mailbox message containing the receive sg * @rctx: crypto request context * @rx_frag_num: number of scatterlist elements required to hold the * SPU response message * @chunksize: Number of bytes of response data expected * @stat_pad_len: Number of bytes required to pad the STAT field to * a 4-byte boundary * * The scatterlist that gets allocated here is freed in spu_chunk_cleanup() * when the request completes, whether the request is handled successfully or * there is an error. * * Returns: * 0 if successful * < 0 if an error
*/ staticint
spu_skcipher_rx_sg_create(struct brcm_message *mssg, struct iproc_reqctx_s *rctx,
u8 rx_frag_num, unsignedint chunksize, u32 stat_pad_len)
{ struct spu_hw *spu = &iproc_priv.spu; struct scatterlist *sg; /* used to build sgs in mbox message */ struct iproc_ctx_s *ctx = rctx->ctx;
u32 datalen; /* Number of bytes of response data expected */
mssg->spu.dst = kmalloc_array(rx_frag_num, sizeof(struct scatterlist),
rctx->gfp); if (!mssg->spu.dst) return -ENOMEM;
sg = mssg->spu.dst;
sg_init_table(sg, rx_frag_num); /* Space for SPU message header */
sg_set_buf(sg++, rctx->msg_buf.spu_resp_hdr, ctx->spu_resp_hdr_len);
/* If XTS tweak in payload, add buffer to receive encrypted tweak */ if ((ctx->cipher.mode == CIPHER_MODE_XTS) &&
spu->spu_xts_tweak_in_payload())
sg_set_buf(sg++, rctx->msg_buf.c.supdt_tweak,
SPU_XTS_TWEAK_SIZE);
/* Copy in each dst sg entry from request, up to chunksize */
datalen = spu_msg_sg_add(&sg, &rctx->dst_sg, &rctx->dst_skip,
rctx->dst_nents, chunksize); if (datalen < chunksize) {
pr_err("%s(): failed to copy dst sg to mbox msg. chunksize %u, datalen %u",
__func__, chunksize, datalen); return -EFAULT;
}
if (stat_pad_len)
sg_set_buf(sg++, rctx->msg_buf.rx_stat_pad, stat_pad_len);
/** * spu_skcipher_tx_sg_create() - Build up the scatterlist of buffers used to * send a SPU request message for an skcipher request. Includes SPU message * headers and the request data. * @mssg: mailbox message containing the transmit sg * @rctx: crypto request context * @tx_frag_num: number of scatterlist elements required to construct the * SPU request message * @chunksize: Number of bytes of request data * @pad_len: Number of pad bytes * * The scatterlist that gets allocated here is freed in spu_chunk_cleanup() * when the request completes, whether the request is handled successfully or * there is an error. * * Returns: * 0 if successful * < 0 if an error
*/ staticint
spu_skcipher_tx_sg_create(struct brcm_message *mssg, struct iproc_reqctx_s *rctx,
u8 tx_frag_num, unsignedint chunksize, u32 pad_len)
{ struct spu_hw *spu = &iproc_priv.spu; struct scatterlist *sg; /* used to build sgs in mbox message */ struct iproc_ctx_s *ctx = rctx->ctx;
u32 datalen; /* Number of bytes of response data expected */
u32 stat_len;
mssg->spu.src = kmalloc_array(tx_frag_num, sizeof(struct scatterlist),
rctx->gfp); if (unlikely(!mssg->spu.src)) return -ENOMEM;
/* if XTS tweak in payload, copy from IV (where crypto API puts it) */ if ((ctx->cipher.mode == CIPHER_MODE_XTS) &&
spu->spu_xts_tweak_in_payload())
sg_set_buf(sg++, rctx->msg_buf.iv_ctr, SPU_XTS_TWEAK_SIZE);
/* Copy in each src sg entry from request, up to chunksize */
datalen = spu_msg_sg_add(&sg, &rctx->src_sg, &rctx->src_skip,
rctx->src_nents, chunksize); if (unlikely(datalen < chunksize)) {
pr_err("%s(): failed to copy src sg to mbox msg",
__func__); return -EFAULT;
}
if (pad_len)
sg_set_buf(sg++, rctx->msg_buf.spu_req_pad, pad_len);
staticint mailbox_send_message(struct brcm_message *mssg, u32 flags,
u8 chan_idx)
{ int err; int retry_cnt = 0; struct device *dev = &(iproc_priv.pdev->dev);
err = mbox_send_message(iproc_priv.mbox[chan_idx], mssg); if (flags & CRYPTO_TFM_REQ_MAY_SLEEP) { while ((err == -ENOBUFS) && (retry_cnt < SPU_MB_RETRY_MAX)) { /* * Mailbox queue is full. Since MAY_SLEEP is set, assume * not in atomic context and we can wait and try again.
*/
retry_cnt++;
usleep_range(MBOX_SLEEP_MIN, MBOX_SLEEP_MAX);
err = mbox_send_message(iproc_priv.mbox[chan_idx],
mssg);
atomic_inc(&iproc_priv.mb_no_spc);
}
} if (err < 0) {
atomic_inc(&iproc_priv.mb_send_fail); return err;
}
/* Check error returned by mailbox controller */
err = mssg->error; if (unlikely(err < 0)) {
dev_err(dev, "message error %d", err); /* Signal txdone for mailbox channel */
}
/* Signal txdone for mailbox channel */
mbox_client_txdone(iproc_priv.mbox[chan_idx], err); return err;
}
/** * handle_skcipher_req() - Submit as much of a block cipher request as fits in * a single SPU request message, starting at the current position in the request * data. * @rctx: Crypto request context * * This may be called on the crypto API thread, or, when a request is so large * it must be broken into multiple SPU messages, on the thread used to invoke * the response callback. When requests are broken into multiple SPU * messages, we assume subsequent messages depend on previous results, and * thus always wait for previous results before submitting the next message. * Because requests are submitted in lock step like this, there is no need * to synchronize access to request data structures. * * Return: -EINPROGRESS: request has been accepted and result will be returned * asynchronously * Any other value indicates an error
*/ staticint handle_skcipher_req(struct iproc_reqctx_s *rctx)
{ struct spu_hw *spu = &iproc_priv.spu; struct crypto_async_request *areq = rctx->parent; struct skcipher_request *req =
container_of(areq, struct skcipher_request, base); struct iproc_ctx_s *ctx = rctx->ctx; struct spu_cipher_parms cipher_parms; int err; unsignedint chunksize; /* Num bytes of request to submit */ int remaining; /* Bytes of request still to process */ int chunk_start; /* Beginning of data for current SPU msg */
/* IV or ctr value to use in this SPU msg */
u8 local_iv_ctr[MAX_IV_SIZE];
u32 stat_pad_len; /* num bytes to align status field */
u32 pad_len; /* total length of all padding */ struct brcm_message *mssg; /* mailbox message */
/* number of entries in src and dst sg in mailbox message. */
u8 rx_frag_num = 2; /* response header and STATUS */
u8 tx_frag_num = 1; /* request header */
/* determine the chunk we are breaking off and update the indexes */ if ((ctx->max_payload != SPU_MAX_PAYLOAD_INF) &&
(remaining > ctx->max_payload))
chunksize = ctx->max_payload; else
chunksize = remaining;
/* Count number of sg entries to be included in this request */
rctx->src_nents = spu_sg_count(rctx->src_sg, rctx->src_skip, chunksize);
rctx->dst_nents = spu_sg_count(rctx->dst_sg, rctx->dst_skip, chunksize);
if ((ctx->cipher.mode == CIPHER_MODE_CBC) &&
rctx->is_encrypt && chunk_start) /* * Encrypting non-first first chunk. Copy last block of * previous result to IV for this chunk.
*/
sg_copy_part_to_buf(req->dst, rctx->msg_buf.iv_ctr,
rctx->iv_ctr_len,
chunk_start - rctx->iv_ctr_len);
if (rctx->iv_ctr_len) { /* get our local copy of the iv */
__builtin_memcpy(local_iv_ctr, rctx->msg_buf.iv_ctr,
rctx->iv_ctr_len);
/* generate the next IV if possible */ if ((ctx->cipher.mode == CIPHER_MODE_CBC) &&
!rctx->is_encrypt) { /* * CBC Decrypt: next IV is the last ciphertext block in * this chunk
*/
sg_copy_part_to_buf(req->src, rctx->msg_buf.iv_ctr,
rctx->iv_ctr_len,
rctx->src_sent - rctx->iv_ctr_len);
} elseif (ctx->cipher.mode == CIPHER_MODE_CTR) { /* * The SPU hardware increments the counter once for * each AES block of 16 bytes. So update the counter * for the next chunk, if there is one. Note that for * this chunk, the counter has already been copied to * local_iv_ctr. We can assume a block size of 16, * because we only support CTR mode for AES, not for * any other cipher alg.
*/
add_to_ctr(rctx->msg_buf.iv_ctr, chunksize >> 4);
}
}
if (ctx->max_payload == SPU_MAX_PAYLOAD_INF)
flow_log("max_payload infinite\n"); else
flow_log("max_payload %u\n", ctx->max_payload);
/* Copy SPU header template created at setkey time */
memcpy(rctx->msg_buf.bcm_spu_req_hdr, ctx->bcm_spu_req_hdr, sizeof(rctx->msg_buf.bcm_spu_req_hdr));
/** * handle_skcipher_resp() - Process a block cipher SPU response. Updates the * total received count for the request and updates global stats. * @rctx: Crypto request context
*/ staticvoid handle_skcipher_resp(struct iproc_reqctx_s *rctx)
{ struct spu_hw *spu = &iproc_priv.spu; struct crypto_async_request *areq = rctx->parent; struct skcipher_request *req = skcipher_request_cast(areq); struct iproc_ctx_s *ctx = rctx->ctx;
u32 payload_len;
/* See how much data was returned */
payload_len = spu->spu_payload_length(rctx->msg_buf.spu_resp_hdr);
/* * In XTS mode, the first SPU_XTS_TWEAK_SIZE bytes may be the * encrypted tweak ("i") value; we don't count those.
*/ if ((ctx->cipher.mode == CIPHER_MODE_XTS) &&
spu->spu_xts_tweak_in_payload() &&
(payload_len >= SPU_XTS_TWEAK_SIZE))
payload_len -= SPU_XTS_TWEAK_SIZE;
/** * spu_ahash_rx_sg_create() - Build up the scatterlist of buffers used to * receive a SPU response message for an ahash request. * @mssg: mailbox message containing the receive sg * @rctx: crypto request context * @rx_frag_num: number of scatterlist elements required to hold the * SPU response message * @digestsize: length of hash digest, in bytes * @stat_pad_len: Number of bytes required to pad the STAT field to * a 4-byte boundary * * The scatterlist that gets allocated here is freed in spu_chunk_cleanup() * when the request completes, whether the request is handled successfully or * there is an error. * * Return: * 0 if successful * < 0 if an error
*/ staticint
spu_ahash_rx_sg_create(struct brcm_message *mssg, struct iproc_reqctx_s *rctx,
u8 rx_frag_num, unsignedint digestsize,
u32 stat_pad_len)
{ struct spu_hw *spu = &iproc_priv.spu; struct scatterlist *sg; /* used to build sgs in mbox message */ struct iproc_ctx_s *ctx = rctx->ctx;
mssg->spu.dst = kmalloc_array(rx_frag_num, sizeof(struct scatterlist),
rctx->gfp); if (!mssg->spu.dst) return -ENOMEM;
sg = mssg->spu.dst;
sg_init_table(sg, rx_frag_num); /* Space for SPU message header */
sg_set_buf(sg++, rctx->msg_buf.spu_resp_hdr, ctx->spu_resp_hdr_len);
/* Space for digest */
sg_set_buf(sg++, rctx->msg_buf.digest, digestsize);
if (stat_pad_len)
sg_set_buf(sg++, rctx->msg_buf.rx_stat_pad, stat_pad_len);
/** * spu_ahash_tx_sg_create() - Build up the scatterlist of buffers used to send * a SPU request message for an ahash request. Includes SPU message headers and * the request data. * @mssg: mailbox message containing the transmit sg * @rctx: crypto request context * @tx_frag_num: number of scatterlist elements required to construct the * SPU request message * @spu_hdr_len: length in bytes of SPU message header * @hash_carry_len: Number of bytes of data carried over from previous req * @new_data_len: Number of bytes of new request data * @pad_len: Number of pad bytes * * The scatterlist that gets allocated here is freed in spu_chunk_cleanup() * when the request completes, whether the request is handled successfully or * there is an error. * * Return: * 0 if successful * < 0 if an error
*/ staticint
spu_ahash_tx_sg_create(struct brcm_message *mssg, struct iproc_reqctx_s *rctx,
u8 tx_frag_num,
u32 spu_hdr_len, unsignedint hash_carry_len, unsignedint new_data_len, u32 pad_len)
{ struct spu_hw *spu = &iproc_priv.spu; struct scatterlist *sg; /* used to build sgs in mbox message */
u32 datalen; /* Number of bytes of response data expected */
u32 stat_len;
mssg->spu.src = kmalloc_array(tx_frag_num, sizeof(struct scatterlist),
rctx->gfp); if (!mssg->spu.src) return -ENOMEM;
if (hash_carry_len)
sg_set_buf(sg++, rctx->hash_carry, hash_carry_len);
if (new_data_len) { /* Copy in each src sg entry from request, up to chunksize */
datalen = spu_msg_sg_add(&sg, &rctx->src_sg, &rctx->src_skip,
rctx->src_nents, new_data_len); if (datalen < new_data_len) {
pr_err("%s(): failed to copy src sg to mbox msg",
__func__); return -EFAULT;
}
}
if (pad_len)
sg_set_buf(sg++, rctx->msg_buf.spu_req_pad, pad_len);
/** * handle_ahash_req() - Process an asynchronous hash request from the crypto * API. * @rctx: Crypto request context * * Builds a SPU request message embedded in a mailbox message and submits the * mailbox message on a selected mailbox channel. The SPU request message is * constructed as a scatterlist, including entries from the crypto API's * src scatterlist to avoid copying the data to be hashed. This function is * called either on the thread from the crypto API, or, in the case that the * crypto API request is too large to fit in a single SPU request message, * on the thread that invokes the receive callback with a response message. * Because some operations require the response from one chunk before the next * chunk can be submitted, we always wait for the response for the previous * chunk before submitting the next chunk. Because requests are submitted in * lock step like this, there is no need to synchronize access to request data * structures. * * Return: * -EINPROGRESS: request has been submitted to SPU and response will be * returned asynchronously * -EAGAIN: non-final request included a small amount of data, which for * efficiency we did not submit to the SPU, but instead stored * to be submitted to the SPU with the next part of the request * other: an error code
*/ staticint handle_ahash_req(struct iproc_reqctx_s *rctx)
{ struct spu_hw *spu = &iproc_priv.spu; struct crypto_async_request *areq = rctx->parent; struct ahash_request *req = ahash_request_cast(areq); struct crypto_ahash *ahash = crypto_ahash_reqtfm(req); struct crypto_tfm *tfm = crypto_ahash_tfm(ahash); unsignedint blocksize = crypto_tfm_alg_blocksize(tfm); struct iproc_ctx_s *ctx = rctx->ctx;
/* number of bytes still to be hashed in this req */ unsignedint nbytes_to_hash = 0; int err; unsignedint chunksize = 0; /* length of hash carry + new data */ /* * length of new data, not from hash carry, to be submitted in * this hw request
*/ unsignedint new_data_len;
unsignedint __maybe_unused chunk_start = 0;
u32 db_size; /* Length of data field, incl gcm and hash padding */ int pad_len = 0; /* total pad len, including gcm, hash, stat padding */
u32 data_pad_len = 0; /* length of GCM/CCM padding */
u32 stat_pad_len = 0; /* length of padding to align STATUS word */ struct brcm_message *mssg; /* mailbox message */ struct spu_request_opts req_opts; struct spu_cipher_parms cipher_parms; struct spu_hash_parms hash_parms; struct spu_aead_parms aead_parms; unsignedint local_nbuf;
u32 spu_hdr_len; unsignedint digestsize;
u16 rem = 0;
/* * number of entries in src and dst sg. Always includes SPU msg header. * rx always includes a buffer to catch digest and STATUS.
*/
u8 rx_frag_num = 3;
u8 tx_frag_num = 1;
/* * For hash algorithms below assignment looks bit odd but * it's needed for AES-XCBC and AES-CMAC hash algorithms * to differentiate between 128, 192, 256 bit key values. * Based on the key values, hash algorithm is selected. * For example for 128 bit key, hash algorithm is AES-128.
*/
cipher_parms.type = ctx->cipher_type;
/* * Compute the amount remaining to hash. This may include data * carried over from previous requests.
*/
nbytes_to_hash = rctx->total_todo - rctx->total_sent;
chunksize = nbytes_to_hash; if ((ctx->max_payload != SPU_MAX_PAYLOAD_INF) &&
(chunksize > ctx->max_payload))
chunksize = ctx->max_payload;
/* * If this is not a final request and the request data is not a multiple * of a full block, then simply park the extra data and prefix it to the * data for the next request.
*/ if (!rctx->is_final) {
u8 *dest = rctx->hash_carry + rctx->hash_carry_len;
u16 new_len; /* len of data to add to hash carry */
rem = chunksize % blocksize; /* remainder */ if (rem) { /* chunksize not a multiple of blocksize */
chunksize -= rem; if (chunksize == 0) { /* Don't have a full block to submit to hw */
new_len = rem - rctx->hash_carry_len;
sg_copy_part_to_buf(req->src, dest, new_len,
rctx->src_sent);
rctx->hash_carry_len = rem;
flow_log("Exiting with hash carry len: %u\n",
rctx->hash_carry_len);
packet_dump(" buf: ",
rctx->hash_carry,
rctx->hash_carry_len); return -EAGAIN;
}
}
}
/* if we have hash carry, then prefix it to the data in this request */
local_nbuf = rctx->hash_carry_len;
rctx->hash_carry_len = 0; if (local_nbuf)
tx_frag_num++;
new_data_len = chunksize - local_nbuf;
/* Count number of sg entries to be used in this request */
rctx->src_nents = spu_sg_count(rctx->src_sg, rctx->src_skip,
new_data_len);
/* AES hashing keeps key size in type field, so need to copy it here */ if (hash_parms.alg == HASH_ALG_AES)
hash_parms.type = (enum hash_type)cipher_parms.type; else
hash_parms.type = spu->spu_hash_type(rctx->total_sent);
/* * If a non-first chunk, then include the digest returned from the * previous chunk so that hw can add to it (except for AES types).
*/ if ((hash_parms.type == HASH_TYPE_UPDT) &&
(hash_parms.alg != HASH_ALG_AES)) {
hash_parms.key_buf = rctx->incr_hash;
hash_parms.key_len = digestsize;
}
/** * spu_hmac_outer_hash() - Request synchonous software compute of the outer hash * for an HMAC request. * @req: The HMAC request from the crypto API * @ctx: The session context * * Return: 0 if synchronous hash operation successful * -EINVAL if the hash algo is unrecognized * any other value indicates an error
*/ staticint spu_hmac_outer_hash(struct ahash_request *req, struct iproc_ctx_s *ctx)
{ struct crypto_ahash *ahash = crypto_ahash_reqtfm(req); unsignedint blocksize =
crypto_tfm_alg_blocksize(crypto_ahash_tfm(ahash)); int rc;
/* if this an HMAC then do the outer hash */ if (rctx->is_sw_hmac) {
err = spu_hmac_outer_hash(req, ctx); if (err < 0) return err;
flow_dump(" hmac: ", req->result, ctx->digestsize);
}
/** * handle_ahash_resp() - Process a SPU response message for a hash request. * Checks if the entire crypto API request has been processed, and if so, * invokes post processing on the result. * @rctx: Crypto request context
*/ staticvoid handle_ahash_resp(struct iproc_reqctx_s *rctx)
{ struct iproc_ctx_s *ctx = rctx->ctx; struct crypto_async_request *areq = rctx->parent; struct ahash_request *req = ahash_request_cast(areq); struct crypto_ahash *ahash = crypto_ahash_reqtfm(req); unsignedint blocksize =
crypto_tfm_alg_blocksize(crypto_ahash_tfm(ahash)); /* * Save hash to use as input to next op if incremental. Might be copying * too much, but that's easier than figuring out actual digest size here
*/
memcpy(rctx->incr_hash, rctx->msg_buf.digest, MAX_DIGEST_SIZE);
if (rctx->is_final && (rctx->total_sent == rctx->total_todo))
ahash_req_done(rctx);
}
/** * spu_aead_rx_sg_create() - Build up the scatterlist of buffers used to receive * a SPU response message for an AEAD request. Includes buffers to catch SPU * message headers and the response data. * @mssg: mailbox message containing the receive sg * @req: Crypto API request * @rctx: crypto request context * @rx_frag_num: number of scatterlist elements required to hold the * SPU response message * @assoc_len: Length of associated data included in the crypto request * @ret_iv_len: Length of IV returned in response * @resp_len: Number of bytes of response data expected to be written to * dst buffer from crypto API * @digestsize: Length of hash digest, in bytes * @stat_pad_len: Number of bytes required to pad the STAT field to * a 4-byte boundary * * The scatterlist that gets allocated here is freed in spu_chunk_cleanup() * when the request completes, whether the request is handled successfully or * there is an error. * * Returns: * 0 if successful * < 0 if an error
*/ staticint spu_aead_rx_sg_create(struct brcm_message *mssg, struct aead_request *req, struct iproc_reqctx_s *rctx,
u8 rx_frag_num, unsignedint assoc_len,
u32 ret_iv_len, unsignedint resp_len, unsignedint digestsize, u32 stat_pad_len)
{ struct spu_hw *spu = &iproc_priv.spu; struct scatterlist *sg; /* used to build sgs in mbox message */ struct iproc_ctx_s *ctx = rctx->ctx;
u32 datalen; /* Number of bytes of response data expected */
u32 assoc_buf_len;
u8 data_padlen = 0;
if (ctx->is_rfc4543) { /* RFC4543: only pad after data, not after AAD */
data_padlen = spu->spu_gcm_ccm_pad_len(ctx->cipher.mode,
assoc_len + resp_len);
assoc_buf_len = assoc_len;
} else {
data_padlen = spu->spu_gcm_ccm_pad_len(ctx->cipher.mode,
resp_len);
assoc_buf_len = spu->spu_assoc_resp_len(ctx->cipher.mode,
assoc_len, ret_iv_len,
rctx->is_encrypt);
}
if (ctx->cipher.mode == CIPHER_MODE_CCM) /* ICV (after data) must be in the next 32-bit word for CCM */
data_padlen += spu->spu_wordalign_padlen(assoc_buf_len +
resp_len +
data_padlen);
if (data_padlen) /* have to catch gcm pad in separate buffer */
rx_frag_num++;
mssg->spu.dst = kmalloc_array(rx_frag_num, sizeof(struct scatterlist),
rctx->gfp); if (!mssg->spu.dst) return -ENOMEM;
/* Space for SPU message header */
sg_set_buf(sg++, rctx->msg_buf.spu_resp_hdr, ctx->spu_resp_hdr_len);
if (assoc_buf_len) { /* * Don't write directly to req->dst, because SPU may pad the * assoc data in the response
*/
memset(rctx->msg_buf.a.resp_aad, 0, assoc_buf_len);
sg_set_buf(sg++, rctx->msg_buf.a.resp_aad, assoc_buf_len);
}
if (resp_len) { /* * Copy in each dst sg entry from request, up to chunksize. * dst sg catches just the data. digest caught in separate buf.
*/
datalen = spu_msg_sg_add(&sg, &rctx->dst_sg, &rctx->dst_skip,
rctx->dst_nents, resp_len); if (datalen < (resp_len)) {
pr_err("%s(): failed to copy dst sg to mbox msg. expected len %u, datalen %u",
__func__, resp_len, datalen); return -EFAULT;
}
}
/* If GCM/CCM data is padded, catch padding in separate buffer */ if (data_padlen) {
memset(rctx->msg_buf.a.gcmpad, 0, data_padlen);
sg_set_buf(sg++, rctx->msg_buf.a.gcmpad, data_padlen);
}
/* Always catch ICV in separate buffer */
sg_set_buf(sg++, rctx->msg_buf.digest, digestsize);
/** * spu_aead_tx_sg_create() - Build up the scatterlist of buffers used to send a * SPU request message for an AEAD request. Includes SPU message headers and the * request data. * @mssg: mailbox message containing the transmit sg * @rctx: crypto request context * @tx_frag_num: number of scatterlist elements required to construct the * SPU request message * @spu_hdr_len: length of SPU message header in bytes * @assoc: crypto API associated data scatterlist * @assoc_len: length of associated data * @assoc_nents: number of scatterlist entries containing assoc data * @aead_iv_len: length of AEAD IV, if included * @chunksize: Number of bytes of request data * @aad_pad_len: Number of bytes of padding at end of AAD. For GCM/CCM. * @pad_len: Number of pad bytes * @incl_icv: If true, write separate ICV buffer after data and * any padding * * The scatterlist that gets allocated here is freed in spu_chunk_cleanup() * when the request completes, whether the request is handled successfully or * there is an error. * * Return: * 0 if successful * < 0 if an error
*/ staticint spu_aead_tx_sg_create(struct brcm_message *mssg, struct iproc_reqctx_s *rctx,
u8 tx_frag_num,
u32 spu_hdr_len, struct scatterlist *assoc, unsignedint assoc_len, int assoc_nents, unsignedint aead_iv_len, unsignedint chunksize,
u32 aad_pad_len, u32 pad_len, bool incl_icv)
{ struct spu_hw *spu = &iproc_priv.spu; struct scatterlist *sg; /* used to build sgs in mbox message */ struct scatterlist *assoc_sg = assoc; struct iproc_ctx_s *ctx = rctx->ctx;
u32 datalen; /* Number of bytes of data to write */
u32 written; /* Number of bytes of data written */
u32 assoc_offset = 0;
u32 stat_len;
mssg->spu.src = kmalloc_array(tx_frag_num, sizeof(struct scatterlist),
rctx->gfp); if (!mssg->spu.src) return -ENOMEM;
if (assoc_len) { /* Copy in each associated data sg entry from request */
written = spu_msg_sg_add(&sg, &assoc_sg, &assoc_offset,
assoc_nents, assoc_len); if (written < assoc_len) {
pr_err("%s(): failed to copy assoc sg to mbox msg",
__func__); return -EFAULT;
}
}
if (aead_iv_len)
sg_set_buf(sg++, rctx->msg_buf.iv_ctr, aead_iv_len);
if (aad_pad_len) {
memset(rctx->msg_buf.a.req_aad_pad, 0, aad_pad_len);
sg_set_buf(sg++, rctx->msg_buf.a.req_aad_pad, aad_pad_len);
}
datalen = chunksize; if ((chunksize > ctx->digestsize) && incl_icv)
datalen -= ctx->digestsize; if (datalen) { /* For aead, a single msg should consume the entire src sg */
written = spu_msg_sg_add(&sg, &rctx->src_sg, &rctx->src_skip,
rctx->src_nents, datalen); if (written < datalen) {
pr_err("%s(): failed to copy src sg to mbox msg",
__func__); return -EFAULT;
}
}
if (pad_len) {
memset(rctx->msg_buf.spu_req_pad, 0, pad_len);
sg_set_buf(sg++, rctx->msg_buf.spu_req_pad, pad_len);
}
if (incl_icv)
sg_set_buf(sg++, rctx->msg_buf.digest, ctx->digestsize);
/** * handle_aead_req() - Submit a SPU request message for the next chunk of the * current AEAD request. * @rctx: Crypto request context * * Unlike other operation types, we assume the length of the request fits in * a single SPU request message. aead_enqueue() makes sure this is true. * Comments for other op types regarding threads applies here as well. * * Unlike incremental hash ops, where the spu returns the entire hash for * truncated algs like sha-224, the SPU returns just the truncated hash in * response to aead requests. So digestsize is always ctx->digestsize here. * * Return: -EINPROGRESS: crypto request has been accepted and result will be * returned asynchronously * Any other value indicates an error
*/ staticint handle_aead_req(struct iproc_reqctx_s *rctx)
{ struct spu_hw *spu = &iproc_priv.spu; struct crypto_async_request *areq = rctx->parent; struct aead_request *req = container_of(areq, struct aead_request, base); struct iproc_ctx_s *ctx = rctx->ctx; int err; unsignedint chunksize; unsignedint resp_len;
u32 spu_hdr_len;
u32 db_size;
u32 stat_pad_len;
u32 pad_len; struct brcm_message *mssg; /* mailbox message */ struct spu_request_opts req_opts; struct spu_cipher_parms cipher_parms; struct spu_hash_parms hash_parms; struct spu_aead_parms aead_parms; int assoc_nents = 0; bool incl_icv = false; unsignedint digestsize = ctx->digestsize;
/* number of entries in src and dst sg. Always includes SPU msg header.
*/
u8 rx_frag_num = 2; /* and STATUS */
u8 tx_frag_num = 1;
/* doing the whole thing at once */
chunksize = rctx->total_todo;
aead_parms.assoc_size = req->assoclen; if (ctx->is_esp && !ctx->is_rfc4543) { /* * 8-byte IV is included assoc data in request. SPU2 * expects AAD to include just SPI and seqno. So * subtract off the IV len.
*/
aead_parms.assoc_size -= GCM_RFC4106_IV_SIZE;
/* * Count number of sg entries from the crypto API request that are to * be included in this mailbox message. For dst sg, don't count space * for digest. Digest gets caught in a separate buffer and copied back * to dst sg when processing response.
*/
rctx->src_nents = spu_sg_count(rctx->src_sg, rctx->src_skip, chunksize);
rctx->dst_nents = spu_sg_count(rctx->dst_sg, rctx->dst_skip, chunksize); if (aead_parms.assoc_size)
assoc_nents = spu_sg_count(rctx->assoc, 0,
aead_parms.assoc_size);
if (ctx->auth.alg == HASH_ALG_AES)
hash_parms.type = (enum hash_type)ctx->cipher_type;
/* General case AAD padding (CCM and RFC4543 special cases below) */
aead_parms.aad_pad_len = spu->spu_gcm_ccm_pad_len(ctx->cipher.mode,
aead_parms.assoc_size);
/* General case data padding (CCM decrypt special case below) */
aead_parms.data_pad_len = spu->spu_gcm_ccm_pad_len(ctx->cipher.mode,
chunksize);
if (ctx->cipher.mode == CIPHER_MODE_CCM) { /* * for CCM, AAD len + 2 (rather than AAD len) needs to be * 128-bit aligned
*/
aead_parms.aad_pad_len = spu->spu_gcm_ccm_pad_len(
ctx->cipher.mode,
aead_parms.assoc_size + 2);
/* * And when decrypting CCM, need to pad without including * size of ICV which is tacked on to end of chunk
*/ if (!rctx->is_encrypt)
aead_parms.data_pad_len =
spu->spu_gcm_ccm_pad_len(ctx->cipher.mode,
chunksize - digestsize);
/* CCM also requires software to rewrite portions of IV: */
spu->spu_ccm_update_iv(digestsize, &cipher_parms, req->assoclen,
chunksize, rctx->is_encrypt,
ctx->is_esp);
}
if (ctx->is_rfc4543) { /* * RFC4543: data is included in AAD, so don't pad after AAD * and pad data based on both AAD + data size
*/
aead_parms.aad_pad_len = 0; if (!rctx->is_encrypt)
aead_parms.data_pad_len = spu->spu_gcm_ccm_pad_len(
ctx->cipher.mode,
aead_parms.assoc_size + chunksize -
digestsize); else
aead_parms.data_pad_len = spu->spu_gcm_ccm_pad_len(
ctx->cipher.mode,
aead_parms.assoc_size + chunksize);
req_opts.is_rfc4543 = true;
}
if (spu_req_incl_icv(ctx->cipher.mode, rctx->is_encrypt)) {
incl_icv = true;
tx_frag_num++; /* Copy ICV from end of src scatterlist to digest buf */
sg_copy_part_to_buf(req->src, rctx->msg_buf.digest, digestsize,
req->assoclen + rctx->total_sent -
digestsize);
}
/* Determine total length of padding. Put all padding in one buffer. */
db_size = spu_real_db_size(aead_parms.assoc_size, aead_parms.iv_len, 0,
chunksize, aead_parms.aad_pad_len,
aead_parms.data_pad_len, 0);
/* * Build mailbox message containing SPU request msg and rx buffers * to catch response message
*/
memset(mssg, 0, sizeof(*mssg));
mssg->type = BRCM_MESSAGE_SPU;
mssg->ctx = rctx; /* Will be returned in response */
/* Create rx scatterlist to catch result */
rx_frag_num += rctx->dst_nents;
resp_len = chunksize;
/* * Always catch ICV in separate buffer. Have to for GCM/CCM because of * padding. Have to for SHA-224 and other truncated SHAs because SPU * sends entire digest back.
*/
rx_frag_num++;
if (((ctx->cipher.mode == CIPHER_MODE_GCM) ||
(ctx->cipher.mode == CIPHER_MODE_CCM)) && !rctx->is_encrypt) { /* * Input is ciphertxt plus ICV, but ICV not incl * in output.
*/
resp_len -= ctx->digestsize; if (resp_len == 0) /* no rx frags to catch output data */
rx_frag_num -= rctx->dst_nents;
}
/* See how much data was returned */
payload_len = spu->spu_payload_length(rctx->msg_buf.spu_resp_hdr);
flow_log("payload_len %u\n", payload_len);
/* only count payload */
atomic64_add(payload_len, &iproc_priv.bytes_in);
if (req->assoclen)
packet_dump(" assoc_data ", rctx->msg_buf.a.resp_aad,
req->assoclen);
/* * Copy the ICV back to the destination * buffer. In decrypt case, SPU gives us back the digest, but crypto * API doesn't expect ICV in dst buffer.
*/
result_len = req->cryptlen; if (rctx->is_encrypt) {
icv_offset = req->assoclen + rctx->total_sent;
packet_dump(" ICV: ", rctx->msg_buf.digest, ctx->digestsize);
flow_log("copying ICV to dst sg at offset %u\n", icv_offset);
sg_copy_part_from_buf(req->dst, rctx->msg_buf.digest,
ctx->digestsize, icv_offset);
result_len += ctx->digestsize;
}
/** * spu_chunk_cleanup() - Do cleanup after processing one chunk of a request * @rctx: request context * * Mailbox scatterlists are allocated for each chunk. So free them after * processing each chunk.
*/ staticvoid spu_chunk_cleanup(struct iproc_reqctx_s *rctx)
{ /* mailbox message used to tx request */ struct brcm_message *mssg = &rctx->mb_mssg;
/** * finish_req() - Used to invoke the complete callback from the requester when * a request has been handled asynchronously. * @rctx: Request context * @err: Indicates whether the request was successful or not * * Ensures that cleanup has been done for request
*/ staticvoid finish_req(struct iproc_reqctx_s *rctx, int err)
{ struct crypto_async_request *areq = rctx->parent;
flow_log("%s() err:%d\n\n", __func__, err);
/* No harm done if already called */
spu_chunk_cleanup(rctx);
if (areq)
crypto_request_complete(areq, err);
}
/** * spu_rx_callback() - Callback from mailbox framework with a SPU response. * @cl: mailbox client structure for SPU driver * @msg: mailbox message containing SPU response
*/ staticvoid spu_rx_callback(struct mbox_client *cl, void *msg)
{ struct spu_hw *spu = &iproc_priv.spu; struct brcm_message *mssg = msg; struct iproc_reqctx_s *rctx; int err;
rctx = mssg->ctx; if (unlikely(!rctx)) { /* This is fatal */
pr_err("%s(): no request context", __func__);
err = -EFAULT; goto cb_finish;
}
/* process the SPU status */
err = spu->spu_status_process(rctx->msg_buf.rx_stat); if (err != 0) { if (err == SPU_INVALID_ICV)
atomic_inc(&iproc_priv.bad_icv);
err = -EBADMSG; goto cb_finish;
}
/* Process the SPU response message */ switch (rctx->ctx->alg->type) { case CRYPTO_ALG_TYPE_SKCIPHER:
handle_skcipher_resp(rctx); break; case CRYPTO_ALG_TYPE_AHASH:
handle_ahash_resp(rctx); break; case CRYPTO_ALG_TYPE_AEAD:
handle_aead_resp(rctx); break; default:
err = -EINVAL; goto cb_finish;
}
/* * If this response does not complete the request, then send the next * request chunk.
*/ if (rctx->total_sent < rctx->total_todo) { /* Deallocate anything specific to previous chunk */
spu_chunk_cleanup(rctx);
switch (rctx->ctx->alg->type) { case CRYPTO_ALG_TYPE_SKCIPHER:
err = handle_skcipher_req(rctx); break; case CRYPTO_ALG_TYPE_AHASH:
err = handle_ahash_req(rctx); if (err == -EAGAIN) /* * we saved data in hash carry, but tell crypto * API we successfully completed request.
*/
err = 0; break; case CRYPTO_ALG_TYPE_AEAD:
err = handle_aead_req(rctx); break; default:
err = -EINVAL;
}
if (err == -EINPROGRESS) /* Successfully submitted request for next chunk */ return;
}
cb_finish:
finish_req(rctx, err);
}
/* ==================== Kernel Cryptographic API ==================== */
/** * skcipher_enqueue() - Handle skcipher encrypt or decrypt request. * @req: Crypto API request * @encrypt: true if encrypting; false if decrypting * * Return: -EINPROGRESS if request accepted and result will be returned * asynchronously * < 0 if an error
*/ staticint skcipher_enqueue(struct skcipher_request *req, bool encrypt)
{ struct iproc_reqctx_s *rctx = skcipher_request_ctx(req); struct iproc_ctx_s *ctx =
crypto_skcipher_ctx(crypto_skcipher_reqtfm(req)); int err;
/* Choose a SPU to process this request */
rctx->chan_idx = select_channel();
err = handle_skcipher_req(rctx); if (err != -EINPROGRESS) /* synchronous result */
spu_chunk_cleanup(rctx);
/* SPU needs XTS keys in the reverse order the crypto API presents */ if ((ctx->cipher.alg == CIPHER_ALG_AES) &&
(ctx->cipher.mode == CIPHER_MODE_XTS)) { unsignedint xts_keylen = keylen / 2;
ctx->digestsize = crypto_ahash_digestsize(tfm); /* If we add a hash whose digest is larger, catch it here. */
WARN_ON(ctx->digestsize > MAX_DIGEST_SIZE);
/** * spu_no_incr_hash() - Determine whether incremental hashing is supported. * @ctx: Crypto session context * * SPU-2 does not support incremental hashing (we'll have to revisit and * condition based on chip revision or device tree entry if future versions do * support incremental hash) * * SPU-M also doesn't support incremental hashing of AES-XCBC * * Return: true if incremental hashing is not supported * false otherwise
*/ staticbool spu_no_incr_hash(struct iproc_ctx_s *ctx)
{ struct spu_hw *spu = &iproc_priv.spu;
if (spu->spu_type == SPU_TYPE_SPU2) returntrue;
if ((ctx->auth.alg == HASH_ALG_AES) &&
(ctx->auth.mode == HASH_MODE_XCBC)) returntrue;
/* Otherwise, incremental hashing is supported */ returnfalse;
}
if (spu_no_incr_hash(ctx)) { /* * If we get an incremental hashing request and it's not * supported by the hardware, we need to handle it in software * by calling synchronous hash functions.
*/
alg_name = crypto_ahash_alg_name(tfm);
hash = crypto_alloc_shash(alg_name, 0, 0); if (IS_ERR(hash)) {
ret = PTR_ERR(hash); goto err;
}
/* Set the key using data we already have from setkey */ if (ctx->authkeylen > 0) {
ret = crypto_shash_setkey(hash, ctx->authkey,
ctx->authkeylen); if (ret) goto err_shash;
}
/* Initialize hash w/ this key and other params */
ret = crypto_shash_init(ctx->shash); if (ret) goto err_shash;
} else { /* Otherwise call the internal function which uses SPU hw */
ret = __ahash_init(req);
}
if (spu_no_incr_hash(ctx)) { /* * If we get an incremental hashing request and it's not * supported by the hardware, we need to handle it in software * by calling synchronous hash functions.
*/ if (req->src)
nents = sg_nents(req->src); else return -EINVAL;
/* Copy data from req scatterlist to tmp buffer */
gfp = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC;
tmpbuf = kmalloc(req->nbytes, gfp); if (!tmpbuf) return -ENOMEM;
if (spu_no_incr_hash(ctx)) { /* * If we get an incremental hashing request and it's not * supported by the hardware, we need to handle it in software * by calling synchronous hash functions.
*/
ret = crypto_shash_final(ctx->shash, req->result);
/* Done with hash, can deallocate it now */
crypto_free_shash(ctx->shash->tfm);
kfree(ctx->shash);
} else { /* Otherwise call the internal function which uses SPU hw */
ret = __ahash_final(req);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.