wa->sg_used += nbytes;
wa->bytes_left -= nbytes; if (wa->sg_used == sg_dma_len(wa->dma_sg)) { /* Advance to the next DMA scatterlist entry */
wa->dma_sg = sg_next(wa->dma_sg);
/* In the case that the DMA mapped scatterlist has entries *thathavebeenmerged,thenon-DMAmappedscatterlist *mustbeadvancedmultipletimesforeachmergedentry. *Thisensuresthatthecurrentnon-DMAmappedentry *correspondstothecurrentDMAmappedentry.
*/ do {
sg_combined_len += wa->sg->length;
wa->sg = sg_next(wa->sg);
} while (wa->sg_used > sg_combined_len);
wa->sg_used = 0;
}
}
staticvoid ccp_dm_free(struct ccp_dm_workarea *wa)
{ if (wa->length <= CCP_DMAPOOL_MAX_SIZE) { if (wa->address)
dma_pool_free(wa->dma_pool, wa->address,
wa->dma.address);
} else { if (wa->dma.address)
dma_unmap_single(wa->dev, wa->dma.address, wa->length,
wa->dma.dir);
kfree(wa->address);
}
/* The CCP can only DMA from/to one address each per operation. This *requiresthatwefindthesmallestDMAareabetweenthesource *anddestination.Theresultinglenvalueswillalwaysbe<=UINT_MAX *becausethedmalengthisanunsignedint.
*/
sg_src_len = sg_dma_len(src->sg_wa.dma_sg) - src->sg_wa.sg_used;
sg_src_len = min_t(u64, src->sg_wa.bytes_left, sg_src_len);
/* The data operation length will be at least block_size in length *orthesmallerofavailablesgroomremainingforthesourceor *thedestination
*/
op_len = max(op_len, block_size);
/* Unless we have to buffer data, there's no reason to wait */
op->soc = 0;
if (sg_src_len < block_size) { /* Not enough data in the sg element, so it *needstobebufferedintoablocksizechunk
*/ int cp_len = ccp_fill_queue_buf(src);
op->soc = 1;
op->src.u.dma.address = src->dm_wa.dma.address;
op->src.u.dma.offset = 0;
op->src.u.dma.length = (blocksize_op) ? block_size : cp_len;
} else { /* Enough data in the sg element, but we need to *adjustforanypreviouslycopieddata
*/
op->src.u.dma.address = sg_dma_address(src->sg_wa.dma_sg);
op->src.u.dma.offset = src->sg_wa.sg_used;
op->src.u.dma.length = op_len & ~(block_size - 1);
if (dst) { if (sg_dst_len < block_size) { /* Not enough room in the sg element or we're on the *lastpieceofdata(whenusingpadding),sothe *outputneedstobebufferedintoablocksizechunk
*/
op->soc = 1;
op->dst.u.dma.address = dst->dm_wa.dma.address;
op->dst.u.dma.offset = 0;
op->dst.u.dma.length = op->src.u.dma.length;
} else { /* Enough room in the sg element, but we need to *adjustforanypreviouslyusedarea
*/
op->dst.u.dma.address = sg_dma_address(dst->sg_wa.dma_sg);
op->dst.u.dma.offset = dst->sg_wa.sg_used;
op->dst.u.dma.length = op->src.u.dma.length;
}
}
}
/* All supported key sizes fit in a single (32-byte) SB entry *andmustbeinlittleendianformat.Usethe256-bitbyte *swappassthruoptiontoconvertfrombigendiantolittle *endian.
*/
ret = ccp_init_dm_workarea(&key, cmd_q,
CCP_AES_KEY_SB_COUNT * CCP_SB_BYTES,
DMA_TO_DEVICE); if (ret) return ret;
dm_offset = CCP_SB_BYTES - aes->key_len;
ret = ccp_set_dm_area(&key, dm_offset, aes->key, 0, aes->key_len); if (ret) goto e_key;
ret = ccp_copy_to_sb(cmd_q, &key, op.jobid, op.sb_key,
CCP_PASSTHRU_BYTESWAP_256BIT); if (ret) {
cmd->engine_error = cmd_q->cmd_error; goto e_key;
}
/* The AES context fits in a single (32-byte) SB entry and *mustbeinlittleendianformat.Usethe256-bitbyteswap *passthruoptiontoconvertfrombigendiantolittleendian.
*/
ret = ccp_init_dm_workarea(&ctx, cmd_q,
CCP_AES_CTX_SB_COUNT * CCP_SB_BYTES,
DMA_BIDIRECTIONAL); if (ret) goto e_key;
dm_offset = CCP_SB_BYTES - AES_BLOCK_SIZE;
ret = ccp_set_dm_area(&ctx, dm_offset, aes->iv, 0, aes->iv_len); if (ret) goto e_ctx;
ret = ccp_copy_to_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
CCP_PASSTHRU_BYTESWAP_256BIT); if (ret) {
cmd->engine_error = cmd_q->cmd_error; goto e_ctx;
}
/* Send data to the CCP AES engine */
ret = ccp_init_data(&src, cmd_q, aes->src, aes->src_len,
AES_BLOCK_SIZE, DMA_TO_DEVICE); if (ret) goto e_ctx;
while (src.sg_wa.bytes_left) {
ccp_prepare_data(&src, NULL, &op, AES_BLOCK_SIZE, true); if (aes->cmac_final && !src.sg_wa.bytes_left) {
op.eom = 1;
/* Push the K1/K2 key to the CCP now */
ret = ccp_copy_from_sb(cmd_q, &ctx, op.jobid,
op.sb_ctx,
CCP_PASSTHRU_BYTESWAP_256BIT); if (ret) {
cmd->engine_error = cmd_q->cmd_error; goto e_src;
}
ret = ccp_set_dm_area(&ctx, 0, aes->cmac_key, 0,
aes->cmac_key_len); if (ret) goto e_src;
ret = ccp_copy_to_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
CCP_PASSTHRU_BYTESWAP_256BIT); if (ret) {
cmd->engine_error = cmd_q->cmd_error; goto e_src;
}
}
ret = cmd_q->ccp->vdata->perform->aes(&op); if (ret) {
cmd->engine_error = cmd_q->cmd_error; goto e_src;
}
ccp_process_data(&src, NULL, &op);
}
/* Retrieve the AES context - convert from LE to BE using *32-byte(256-bit)byteswapping
*/
ret = ccp_copy_from_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
CCP_PASSTHRU_BYTESWAP_256BIT); if (ret) {
cmd->engine_error = cmd_q->cmd_error; goto e_src;
}
/* ...but we only need AES_BLOCK_SIZE bytes */
dm_offset = CCP_SB_BYTES - AES_BLOCK_SIZE;
ccp_get_dm_area(&ctx, dm_offset, aes->iv, 0, aes->iv_len);
/* Copy the key to the LSB */
ret = ccp_init_dm_workarea(&wa->key, cmd_q,
CCP_AES_CTX_SB_COUNT * CCP_SB_BYTES,
DMA_TO_DEVICE); if (ret) return ret;
dm_offset = CCP_SB_BYTES - aes->key_len;
ret = ccp_set_dm_area(&wa->key, dm_offset, aes->key, 0, aes->key_len); if (ret) goto e_key;
ret = ccp_copy_to_sb(cmd_q, &wa->key, wa->op.jobid, wa->op.sb_key,
CCP_PASSTHRU_BYTESWAP_256BIT); if (ret) {
cmd->engine_error = cmd_q->cmd_error; goto e_key;
}
/* Copy the context (IV) to the LSB. *ThereisanassumptionherethattheIVis96bitsinlength,plus *anonceof32bits.IfnoIVispresent,useazeroedbuffer.
*/
ret = ccp_init_dm_workarea(&wa->ctx, cmd_q,
CCP_AES_CTX_SB_COUNT * CCP_SB_BYTES,
DMA_BIDIRECTIONAL); if (ret) goto e_key;
dm_offset = CCP_AES_CTX_SB_COUNT * CCP_SB_BYTES - aes->iv_len;
ret = ccp_set_dm_area(&wa->ctx, dm_offset, aes->iv, 0, aes->iv_len); if (ret) goto e_ctx;
ret = ccp_copy_to_sb(cmd_q, &wa->ctx, wa->op.jobid, wa->op.sb_ctx,
CCP_PASSTHRU_BYTESWAP_256BIT); if (ret) {
cmd->engine_error = cmd_q->cmd_error; goto e_ctx;
}
wa->op.init = 1; if (aes->aad_len > 0) { /* Step 1: Run a GHASH over the Additional Authenticated Data */
ret = ccp_init_data(&wa->aad, cmd_q, p_aad, aes->aad_len,
AES_BLOCK_SIZE,
DMA_TO_DEVICE); if (ret) goto e_ctx;
/* Step 3: Update the IV portion of the context with the original IV */
ret = ccp_copy_from_sb(cmd_q, &wa->ctx, wa->op.jobid, wa->op.sb_ctx,
CCP_PASSTHRU_BYTESWAP_256BIT); if (ret) {
cmd->engine_error = cmd_q->cmd_error; goto e_dst;
}
ret = ccp_set_dm_area(&wa->ctx, dm_offset, aes->iv, 0, aes->iv_len); if (ret) goto e_dst;
ret = ccp_copy_to_sb(cmd_q, &wa->ctx, wa->op.jobid, wa->op.sb_ctx,
CCP_PASSTHRU_BYTESWAP_256BIT); if (ret) {
cmd->engine_error = cmd_q->cmd_error; goto e_dst;
}
/* Step 4: Concatenate the lengths of the AAD and source, and *hashthat16bytebuffer.
*/
ret = ccp_init_dm_workarea(&wa->final, cmd_q, AES_BLOCK_SIZE,
DMA_BIDIRECTIONAL); if (ret) goto e_dst;
final = (__be64 *)wa->final.address;
final[0] = cpu_to_be64(aes->aad_len * 8);
final[1] = cpu_to_be64(ilen * 8);
if (aes->action == CCP_AES_ACTION_ENCRYPT) { /* Put the ciphered tag after the ciphertext. */
ccp_get_dm_area(&wa->final, 0, p_tag, 0, authsize);
} else { /* Does this ciphered tag match the input? */
ret = ccp_init_dm_workarea(&wa->tag, cmd_q, authsize,
DMA_BIDIRECTIONAL); if (ret) goto e_final_wa;
ret = ccp_set_dm_area(&wa->tag, 0, p_tag, 0, authsize); if (ret) {
ccp_dm_free(&wa->tag); goto e_final_wa;
}
/* All supported key sizes fit in a single (32-byte) SB entry *andmustbeinlittleendianformat.Usethe256-bitbyte *swappassthruoptiontoconvertfrombigendiantolittle *endian.
*/
ret = ccp_init_dm_workarea(&key, cmd_q,
CCP_AES_KEY_SB_COUNT * CCP_SB_BYTES,
DMA_TO_DEVICE); if (ret) return ret;
dm_offset = CCP_SB_BYTES - aes->key_len;
ret = ccp_set_dm_area(&key, dm_offset, aes->key, 0, aes->key_len); if (ret) goto e_key;
ret = ccp_copy_to_sb(cmd_q, &key, op.jobid, op.sb_key,
CCP_PASSTHRU_BYTESWAP_256BIT); if (ret) {
cmd->engine_error = cmd_q->cmd_error; goto e_key;
}
/* The AES context fits in a single (32-byte) SB entry and *mustbeinlittleendianformat.Usethe256-bitbyteswap *passthruoptiontoconvertfrombigendiantolittleendian.
*/
ret = ccp_init_dm_workarea(&ctx, cmd_q,
CCP_AES_CTX_SB_COUNT * CCP_SB_BYTES,
DMA_BIDIRECTIONAL); if (ret) goto e_key;
if (aes->mode != CCP_AES_MODE_ECB) { /* Load the AES context - convert to LE */
dm_offset = CCP_SB_BYTES - AES_BLOCK_SIZE;
ret = ccp_set_dm_area(&ctx, dm_offset, aes->iv, 0, aes->iv_len); if (ret) goto e_ctx;
ret = ccp_copy_to_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
CCP_PASSTHRU_BYTESWAP_256BIT); if (ret) {
cmd->engine_error = cmd_q->cmd_error; goto e_ctx;
}
} switch (aes->mode) { case CCP_AES_MODE_CFB: /* CFB128 only */ case CCP_AES_MODE_CTR:
op.u.aes.size = AES_BLOCK_SIZE * BITS_PER_BYTE - 1; break; default:
op.u.aes.size = 0;
}
/* Prepare the input and output data workareas. For in-place *operationsweneedtosetthedmadirectiontoBIDIRECTIONAL *andcopythesrcworkareatothedstworkarea.
*/ if (sg_virt(aes->src) == sg_virt(aes->dst))
in_place = true;
ret = ccp_init_data(&src, cmd_q, aes->src, aes->src_len,
AES_BLOCK_SIZE,
in_place ? DMA_BIDIRECTIONAL : DMA_TO_DEVICE); if (ret) goto e_ctx;
if (in_place) {
dst = src;
} else {
ret = ccp_init_data(&dst, cmd_q, aes->dst, aes->src_len,
AES_BLOCK_SIZE, DMA_FROM_DEVICE); if (ret) goto e_src;
}
/* Send data to the CCP AES engine */ while (src.sg_wa.bytes_left) {
ccp_prepare_data(&src, &dst, &op, AES_BLOCK_SIZE, true); if (!src.sg_wa.bytes_left) {
op.eom = 1;
/* Since we don't retrieve the AES context in ECB *modewehavetowaitfortheoperationtocomplete *onthelastpieceofdata
*/ if (aes->mode == CCP_AES_MODE_ECB)
op.soc = 1;
}
ret = cmd_q->ccp->vdata->perform->aes(&op); if (ret) {
cmd->engine_error = cmd_q->cmd_error; goto e_dst;
}
ccp_process_data(&src, &dst, &op);
}
if (aes->mode != CCP_AES_MODE_ECB) { /* Retrieve the AES context - convert from LE to BE using *32-byte(256-bit)byteswapping
*/
ret = ccp_copy_from_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
CCP_PASSTHRU_BYTESWAP_256BIT); if (ret) {
cmd->engine_error = cmd_q->cmd_error; goto e_dst;
}
/* ...but we only need AES_BLOCK_SIZE bytes */
dm_offset = CCP_SB_BYTES - AES_BLOCK_SIZE;
ccp_get_dm_area(&ctx, dm_offset, aes->iv, 0, aes->iv_len);
}
/* A version 3 device only supports 128-bit keys, which fits into a *singleSBentry.Aversion5deviceusesa512-bitvector,sotwo *SBentries.
*/ if (cmd_q->ccp->vdata->version == CCP_VERSION(3, 0))
sb_count = CCP_XTS_AES_KEY_SB_COUNT; else
sb_count = CCP5_XTS_AES_KEY_SB_COUNT;
ret = ccp_init_dm_workarea(&key, cmd_q,
sb_count * CCP_SB_BYTES,
DMA_TO_DEVICE); if (ret) return ret;
if (cmd_q->ccp->vdata->version == CCP_VERSION(3, 0)) { /* All supported key sizes must be in little endian format. *Usethe256-bitbyteswappassthruoptiontoconvertfrom *bigendiantolittleendian.
*/
dm_offset = CCP_SB_BYTES - AES_KEYSIZE_128;
ret = ccp_set_dm_area(&key, dm_offset, xts->key, 0, xts->key_len); if (ret) goto e_key;
ret = ccp_set_dm_area(&key, 0, xts->key, xts->key_len, xts->key_len); if (ret) goto e_key;
} else { /* Version 5 CCPs use a 512-bit space for the key: each portion *occupies256bits,oroneentireslot,andiszero-padded.
*/ unsignedint pad;
dm_offset = CCP_SB_BYTES;
pad = dm_offset - xts->key_len;
ret = ccp_set_dm_area(&key, pad, xts->key, 0, xts->key_len); if (ret) goto e_key;
ret = ccp_set_dm_area(&key, dm_offset + pad, xts->key,
xts->key_len, xts->key_len); if (ret) goto e_key;
}
ret = ccp_copy_to_sb(cmd_q, &key, op.jobid, op.sb_key,
CCP_PASSTHRU_BYTESWAP_256BIT); if (ret) {
cmd->engine_error = cmd_q->cmd_error; goto e_key;
}
/* The AES context fits in a single (32-byte) SB entry and *forXTSisalreadyinlittleendianformatsonobyteswapping *isneeded.
*/
ret = ccp_init_dm_workarea(&ctx, cmd_q,
CCP_XTS_AES_CTX_SB_COUNT * CCP_SB_BYTES,
DMA_BIDIRECTIONAL); if (ret) goto e_key;
ret = ccp_set_dm_area(&ctx, 0, xts->iv, 0, xts->iv_len); if (ret) goto e_ctx;
ret = ccp_copy_to_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
CCP_PASSTHRU_BYTESWAP_NOOP); if (ret) {
cmd->engine_error = cmd_q->cmd_error; goto e_ctx;
}
/* Prepare the input and output data workareas. For in-place *operationsweneedtosetthedmadirectiontoBIDIRECTIONAL *andcopythesrcworkareatothedstworkarea.
*/ if (sg_virt(xts->src) == sg_virt(xts->dst))
in_place = true;
ret = ccp_init_data(&src, cmd_q, xts->src, xts->src_len,
unit_size,
in_place ? DMA_BIDIRECTIONAL : DMA_TO_DEVICE); if (ret) goto e_ctx;
if (in_place) {
dst = src;
} else {
ret = ccp_init_data(&dst, cmd_q, xts->dst, xts->src_len,
unit_size, DMA_FROM_DEVICE); if (ret) goto e_src;
}
/* Send data to the CCP AES engine */ while (src.sg_wa.bytes_left) {
ccp_prepare_data(&src, &dst, &op, unit_size, true); if (!src.sg_wa.bytes_left)
op.eom = 1;
ret = cmd_q->ccp->vdata->perform->xts_aes(&op); if (ret) {
cmd->engine_error = cmd_q->cmd_error; goto e_dst;
}
ccp_process_data(&src, &dst, &op);
}
/* Retrieve the AES context - convert from LE to BE using *32-byte(256-bit)byteswapping
*/
ret = ccp_copy_from_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
CCP_PASSTHRU_BYTESWAP_256BIT); if (ret) {
cmd->engine_error = cmd_q->cmd_error; goto e_dst;
}
/* ...but we only need AES_BLOCK_SIZE bytes */
dm_offset = CCP_SB_BYTES - AES_BLOCK_SIZE;
ccp_get_dm_area(&ctx, dm_offset, xts->iv, 0, xts->iv_len);
len_singlekey = des3->key_len / 3;
ret = ccp_set_dm_area(&key, dm_offset + 2 * len_singlekey,
des3->key, 0, len_singlekey); if (ret) goto e_key;
ret = ccp_set_dm_area(&key, dm_offset + len_singlekey,
des3->key, len_singlekey, len_singlekey); if (ret) goto e_key;
ret = ccp_set_dm_area(&key, dm_offset,
des3->key, 2 * len_singlekey, len_singlekey); if (ret) goto e_key;
/* Copy the key to the SB */
ret = ccp_copy_to_sb(cmd_q, &key, op.jobid, op.sb_key,
CCP_PASSTHRU_BYTESWAP_256BIT); if (ret) {
cmd->engine_error = cmd_q->cmd_error; goto e_key;
}
ret = ccp_init_dm_workarea(&ctx, cmd_q,
CCP_DES3_CTX_SB_COUNT * CCP_SB_BYTES,
DMA_BIDIRECTIONAL); if (ret) goto e_key;
/* Load the context into the LSB */
dm_offset = CCP_SB_BYTES - des3->iv_len;
ret = ccp_set_dm_area(&ctx, dm_offset, des3->iv, 0,
des3->iv_len); if (ret) goto e_ctx;
ret = ccp_copy_to_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
CCP_PASSTHRU_BYTESWAP_256BIT); if (ret) {
cmd->engine_error = cmd_q->cmd_error; goto e_ctx;
}
}
ret = ccp_init_data(&src, cmd_q, des3->src, des3->src_len,
DES3_EDE_BLOCK_SIZE,
in_place ? DMA_BIDIRECTIONAL : DMA_TO_DEVICE); if (ret) goto e_ctx;
if (in_place)
dst = src; else {
ret = ccp_init_data(&dst, cmd_q, des3->dst, des3->src_len,
DES3_EDE_BLOCK_SIZE, DMA_FROM_DEVICE); if (ret) goto e_src;
}
/* Send data to the CCP DES3 engine */ while (src.sg_wa.bytes_left) {
ccp_prepare_data(&src, &dst, &op, DES3_EDE_BLOCK_SIZE, true); if (!src.sg_wa.bytes_left) {
op.eom = 1;
/* Since we don't retrieve the context in ECB mode *wehavetowaitfortheoperationtocomplete *onthelastpieceofdata
*/
op.soc = 0;
}
ret = cmd_q->ccp->vdata->perform->des3(&op); if (ret) {
cmd->engine_error = cmd_q->cmd_error; goto e_dst;
}
ccp_process_data(&src, &dst, &op);
}
if (des3->mode != CCP_DES3_MODE_ECB) { /* Retrieve the context and make BE */
ret = ccp_copy_from_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
CCP_PASSTHRU_BYTESWAP_256BIT); if (ret) {
cmd->engine_error = cmd_q->cmd_error; goto e_dst;
}
/* ...but we only need the last DES3_EDE_BLOCK_SIZE bytes */
ccp_get_dm_area(&ctx, dm_offset, des3->iv, 0,
DES3_EDE_BLOCK_SIZE);
}
e_dst: if (!in_place)
ccp_free_data(&dst, cmd_q);
e_src:
ccp_free_data(&src, cmd_q);
e_ctx: if (des3->mode != CCP_DES3_MODE_ECB)
ccp_dm_free(&ctx);
e_key:
ccp_dm_free(&key);
return ret;
}
static noinline_for_stack int
ccp_run_sha_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
{ struct ccp_sha_engine *sha = &cmd->u.sha; struct ccp_dm_workarea ctx; struct ccp_data src; struct ccp_op op; unsignedint ioffset, ooffset; unsignedint digest_size; int sb_count; constvoid *init;
u64 block_size; int ctx_size; int ret;
switch (sha->type) { case CCP_SHA_TYPE_1: if (sha->ctx_len < SHA1_DIGEST_SIZE) return -EINVAL;
block_size = SHA1_BLOCK_SIZE; break; case CCP_SHA_TYPE_224: if (sha->ctx_len < SHA224_DIGEST_SIZE) return -EINVAL;
block_size = SHA224_BLOCK_SIZE; break; case CCP_SHA_TYPE_256: if (sha->ctx_len < SHA256_DIGEST_SIZE) return -EINVAL;
block_size = SHA256_BLOCK_SIZE; break; case CCP_SHA_TYPE_384: if (cmd_q->ccp->vdata->version < CCP_VERSION(4, 0)
|| sha->ctx_len < SHA384_DIGEST_SIZE) return -EINVAL;
block_size = SHA384_BLOCK_SIZE; break; case CCP_SHA_TYPE_512: if (cmd_q->ccp->vdata->version < CCP_VERSION(4, 0)
|| sha->ctx_len < SHA512_DIGEST_SIZE) return -EINVAL;
block_size = SHA512_BLOCK_SIZE; break; default: return -EINVAL;
}
if (!sha->ctx) return -EINVAL;
if (!sha->final && (sha->src_len & (block_size - 1))) return -EINVAL;
/* The version 3 device can't handle zero-length input */ if (cmd_q->ccp->vdata->version == CCP_VERSION(3, 0)) {
if (!sha->src_len) { unsignedint digest_len; const u8 *sha_zero;
/* Not final, just return */ if (!sha->final) return0;
/* CCP can't do a zero length sha operation so the *callermustbufferthedata.
*/ if (sha->msg_bits) return -EINVAL;
/* For SHA1/224/256 the context fits in a single (32-byte) SB entry; *SHA384/512require2adjacentSBslots,withtherighthalfinthe *firstslot,andthelefthalfinthesecond.Eachportionmustthen *beinlittleendianformat:usethe256-bitbyteswapoption.
*/
ret = ccp_init_dm_workarea(&ctx, cmd_q, sb_count * CCP_SB_BYTES,
DMA_BIDIRECTIONAL); if (ret) return ret; if (sha->first) { switch (sha->type) { case CCP_SHA_TYPE_1: case CCP_SHA_TYPE_224: case CCP_SHA_TYPE_256:
memcpy(ctx.address + ioffset, init, ctx_size); break; case CCP_SHA_TYPE_384: case CCP_SHA_TYPE_512:
memcpy(ctx.address + ctx_size / 2, init,
ctx_size / 2);
memcpy(ctx.address, init + ctx_size / 2,
ctx_size / 2); break; default:
ret = -EINVAL; goto e_ctx;
}
} else { /* Restore the context */
ret = ccp_set_dm_area(&ctx, 0, sha->ctx, 0,
sb_count * CCP_SB_BYTES); if (ret) goto e_ctx;
}
ret = ccp_copy_to_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
CCP_PASSTHRU_BYTESWAP_256BIT); if (ret) {
cmd->engine_error = cmd_q->cmd_error; goto e_ctx;
}
if (sha->src) { /* Send data to the CCP SHA engine; block_size is set above */
ret = ccp_init_data(&src, cmd_q, sha->src, sha->src_len,
block_size, DMA_TO_DEVICE); if (ret) goto e_ctx;
while (src.sg_wa.bytes_left) {
ccp_prepare_data(&src, NULL, &op, block_size, false); if (sha->final && !src.sg_wa.bytes_left)
op.eom = 1;
ret = cmd_q->ccp->vdata->perform->sha(&op); if (ret) {
cmd->engine_error = cmd_q->cmd_error; goto e_data;
}
/* Retrieve the SHA context - convert from LE to BE using *32-byte(256-bit)byteswappingtoBE
*/
ret = ccp_copy_from_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
CCP_PASSTHRU_BYTESWAP_256BIT); if (ret) {
cmd->engine_error = cmd_q->cmd_error; goto e_data;
}
if (sha->final) { /* Finishing up, so get the digest */ switch (sha->type) { case CCP_SHA_TYPE_1: case CCP_SHA_TYPE_224: case CCP_SHA_TYPE_256:
ccp_get_dm_area(&ctx, ooffset,
sha->ctx, 0,
digest_size); break; case CCP_SHA_TYPE_384: case CCP_SHA_TYPE_512:
ccp_get_dm_area(&ctx, 0,
sha->ctx, LSB_ITEM_SIZE - ooffset,
LSB_ITEM_SIZE);
ccp_get_dm_area(&ctx, LSB_ITEM_SIZE + ooffset,
sha->ctx, 0,
LSB_ITEM_SIZE - ooffset); break; default:
ret = -EINVAL; goto e_data;
}
} else { /* Stash the context */
ccp_get_dm_area(&ctx, 0, sha->ctx, 0,
sb_count * CCP_SB_BYTES);
}
/* The RSA modulus must precede the message being acted upon, so *itmustbecopiedtoaDMAareawherethemessageandthe *moduluscanbeconcatenated.Thereforetheinputbuffer *lengthrequiredistwicetheoutputbufferlength(which *mustbeamultipleof256-bits).Computeo_len,i_leninbytes. *Buffersizesmustbeamultipleof32bytes;roundingupmaybe *required.
*/
o_len = 32 * ((rsa->key_size + 255) / 256);
i_len = o_len * 2;
sb_count = 0; if (cmd_q->ccp->vdata->version < CCP_VERSION(5, 0)) { /* sb_count is the number of storage block slots required *forthemodulus.
*/
sb_count = o_len / CCP_SB_BYTES;
op.sb_key = cmd_q->ccp->vdata->perform->sballoc(cmd_q,
sb_count); if (!op.sb_key) return -EIO;
} else { /* A version 5 device allows a modulus size that will not fit *intheLSB,sothecommandwilltransferitfrommemory. *Setthesbkeytothedefault,eventhoughit'snotused.
*/
op.sb_key = cmd_q->sb_key;
}
/* The RSA exponent must be in little endian format. Reverse its *byteorder.
*/
ret = ccp_init_dm_workarea(&exp, cmd_q, o_len, DMA_TO_DEVICE); if (ret) goto e_sb;
ret = ccp_reverse_set_dm_area(&exp, 0, rsa->exp, 0, rsa->exp_len); if (ret) goto e_exp;
if (cmd_q->ccp->vdata->version < CCP_VERSION(5, 0)) { /* Copy the exponent to the local storage block, using *asmany32-byteblocksaswereallocatedabove.It's *alreadylittleendian,sonofurtherchangeisrequired.
*/
ret = ccp_copy_to_sb(cmd_q, &exp, op.jobid, op.sb_key,
CCP_PASSTHRU_BYTESWAP_NOOP); if (ret) {
cmd->engine_error = cmd_q->cmd_error; goto e_exp;
}
} else { /* The exponent can be retrieved from memory via DMA. */
op.exp.u.dma.address = exp.dma.address;
op.exp.u.dma.offset = 0;
}
/* Concatenate the modulus and the message. Both the modulus and *theoperandsmustbeinlittleendianformat.Sincetheinput *isinbigendianformatitmustbeconverted.
*/
ret = ccp_init_dm_workarea(&src, cmd_q, i_len, DMA_TO_DEVICE); if (ret) goto e_exp;
ret = ccp_reverse_set_dm_area(&src, 0, rsa->mod, 0, rsa->mod_len); if (ret) goto e_src;
ret = ccp_reverse_set_dm_area(&src, o_len, rsa->src, 0, rsa->src_len); if (ret) goto e_src;
/* Prepare the output area for the operation */
ret = ccp_init_dm_workarea(&dst, cmd_q, o_len, DMA_FROM_DEVICE); if (ret) goto e_src;
if (pt->bit_mod != CCP_PASSTHRU_BITWISE_NOOP) { /* Load the mask */
op.sb_key = cmd_q->sb_key;
ret = ccp_init_dm_workarea(&mask, cmd_q,
CCP_PASSTHRU_SB_COUNT *
CCP_SB_BYTES,
DMA_TO_DEVICE); if (ret) return ret;
ret = ccp_set_dm_area(&mask, 0, pt->mask, 0, pt->mask_len); if (ret) goto e_mask;
ret = ccp_copy_to_sb(cmd_q, &mask, op.jobid, op.sb_key,
CCP_PASSTHRU_BYTESWAP_NOOP); if (ret) {
cmd->engine_error = cmd_q->cmd_error; goto e_mask;
}
}
/* Prepare the input and output data workareas. For in-place *operationsweneedtosetthedmadirectiontoBIDIRECTIONAL *andcopythesrcworkareatothedstworkarea.
*/ if (sg_virt(pt->src) == sg_virt(pt->dst))
in_place = true;
ret = ccp_init_data(&src, cmd_q, pt->src, pt->src_len,
CCP_PASSTHRU_MASKSIZE,
in_place ? DMA_BIDIRECTIONAL : DMA_TO_DEVICE); if (ret) goto e_mask;
if (in_place) {
dst = src;
} else {
ret = ccp_init_data(&dst, cmd_q, pt->dst, pt->src_len,
CCP_PASSTHRU_MASKSIZE, DMA_FROM_DEVICE); if (ret) goto e_src;
}
/* Send data to the CCP Passthru engine *BecausetheCCPengineworksonasinglesourceanddestination *dmaaddressatatime,eachentryinthesourcescatterlist *(afterthedma_map_sgcall)mustbelessthanorequaltothe *(remaining)lengthinthedestinationscatterlistentryandthe *lengthmustbeamultipleofCCP_PASSTHRU_BLOCKSIZE
*/
dst.sg_wa.sg_used = 0; for (i = 1; i <= src.sg_wa.dma_count; i++) { if (!dst.sg_wa.sg ||
(sg_dma_len(dst.sg_wa.sg) < sg_dma_len(src.sg_wa.sg))) {
ret = -EINVAL; goto e_dst;
}
if (i == src.sg_wa.dma_count) {
op.eom = 1;
op.soc = 1;
}
/* Concatenate the modulus and the operands. Both the modulus and *theoperandsmustbeinlittleendianformat.Sincetheinput *isinbigendianformatitmustbeconvertedandplacedina *fixedlengthbuffer.
*/
ret = ccp_init_dm_workarea(&src, cmd_q, CCP_ECC_SRC_BUF_SIZE,
DMA_TO_DEVICE); if (ret) return ret;
/* Save the workarea address since it is updated in order to perform *theconcatenation
*/
save = src.address;
/* Copy the ECC modulus */
ret = ccp_reverse_set_dm_area(&src, 0, ecc->mod, 0, ecc->mod_len); if (ret) goto e_src;
src.address += CCP_ECC_OPERAND_SIZE;
/* Copy the first operand */
ret = ccp_reverse_set_dm_area(&src, 0, ecc->u.mm.operand_1, 0,
ecc->u.mm.operand_1_len); if (ret) goto e_src;
src.address += CCP_ECC_OPERAND_SIZE;
if (ecc->function != CCP_ECC_FUNCTION_MINV_384BIT) { /* Copy the second operand */
ret = ccp_reverse_set_dm_area(&src, 0, ecc->u.mm.operand_2, 0,
ecc->u.mm.operand_2_len); if (ret) goto e_src;
src.address += CCP_ECC_OPERAND_SIZE;
}
/* Restore the workarea address */
src.address = save;
/* Prepare the output area for the operation */
ret = ccp_init_dm_workarea(&dst, cmd_q, CCP_ECC_DST_BUF_SIZE,
DMA_FROM_DEVICE); if (ret) goto e_src;
/* Concatenate the modulus and the operands. Both the modulus and *theoperandsmustbeinlittleendianformat.Sincetheinput *isinbigendianformatitmustbeconvertedandplacedina *fixedlengthbuffer.
*/
ret = ccp_init_dm_workarea(&src, cmd_q, CCP_ECC_SRC_BUF_SIZE,
DMA_TO_DEVICE); if (ret) return ret;
/* Save the workarea address since it is updated in order to perform *theconcatenation
*/
save = src.address;
/* Copy the ECC modulus */
ret = ccp_reverse_set_dm_area(&src, 0, ecc->mod, 0, ecc->mod_len); if (ret) goto e_src;
src.address += CCP_ECC_OPERAND_SIZE;
/* Copy the first point X and Y coordinate */
ret = ccp_reverse_set_dm_area(&src, 0, ecc->u.pm.point_1.x, 0,
ecc->u.pm.point_1.x_len); if (ret) goto e_src;
src.address += CCP_ECC_OPERAND_SIZE;
ret = ccp_reverse_set_dm_area(&src, 0, ecc->u.pm.point_1.y, 0,
ecc->u.pm.point_1.y_len); if (ret) goto e_src;
src.address += CCP_ECC_OPERAND_SIZE;
/* Set the first point Z coordinate to 1 */
*src.address = 0x01;
src.address += CCP_ECC_OPERAND_SIZE;
if (ecc->function == CCP_ECC_FUNCTION_PADD_384BIT) { /* Copy the second point X and Y coordinate */
ret = ccp_reverse_set_dm_area(&src, 0, ecc->u.pm.point_2.x, 0,
ecc->u.pm.point_2.x_len); if (ret) goto e_src;
src.address += CCP_ECC_OPERAND_SIZE;
ret = ccp_reverse_set_dm_area(&src, 0, ecc->u.pm.point_2.y, 0,
ecc->u.pm.point_2.y_len); if (ret) goto e_src;
src.address += CCP_ECC_OPERAND_SIZE;
/* Set the second point Z coordinate to 1 */
*src.address = 0x01;
src.address += CCP_ECC_OPERAND_SIZE;
} else { /* Copy the Domain "a" parameter */
ret = ccp_reverse_set_dm_area(&src, 0, ecc->u.pm.domain_a, 0,
ecc->u.pm.domain_a_len); if (ret) goto e_src;
src.address += CCP_ECC_OPERAND_SIZE;
if (ecc->function == CCP_ECC_FUNCTION_PMUL_384BIT) { /* Copy the scalar value */
ret = ccp_reverse_set_dm_area(&src, 0,
ecc->u.pm.scalar, 0,
ecc->u.pm.scalar_len); if (ret) goto e_src;
src.address += CCP_ECC_OPERAND_SIZE;
}
}
/* Restore the workarea address */
src.address = save;
/* Prepare the output area for the operation */
ret = ccp_init_dm_workarea(&dst, cmd_q, CCP_ECC_DST_BUF_SIZE,
DMA_FROM_DEVICE); if (ret) goto e_src;
ret = cmd_q->ccp->vdata->perform->ecc(&op); if (ret) {
cmd->engine_error = cmd_q->cmd_error; goto e_dst;
}
ecc->ecc_result = le16_to_cpup(
(const __le16 *)(dst.address + CCP_ECC_RESULT_OFFSET)); if (!(ecc->ecc_result & CCP_ECC_RESULT_SUCCESS)) {
ret = -EIO; goto e_dst;
}
/* Save the workarea address since it is updated as we walk through *tocopythepointmathresult
*/
save = dst.address;
/* Save the ECC result X and Y coordinates */
ccp_reverse_get_dm_area(&dst, 0, ecc->u.pm.result.x, 0,
CCP_ECC_MODULUS_BYTES);
dst.address += CCP_ECC_OUTPUT_SIZE;
ccp_reverse_get_dm_area(&dst, 0, ecc->u.pm.result.y, 0,
CCP_ECC_MODULUS_BYTES);
/* Restore the workarea address */
dst.address = save;
if (!ecc->mod ||
(ecc->mod_len > CCP_ECC_MODULUS_BYTES)) return -EINVAL;
switch (ecc->function) { case CCP_ECC_FUNCTION_MMUL_384BIT: case CCP_ECC_FUNCTION_MADD_384BIT: case CCP_ECC_FUNCTION_MINV_384BIT: return ccp_run_ecc_mm_cmd(cmd_q, cmd);
case CCP_ECC_FUNCTION_PADD_384BIT: case CCP_ECC_FUNCTION_PMUL_384BIT: case CCP_ECC_FUNCTION_PDBL_384BIT: return ccp_run_ecc_pm_cmd(cmd_q, cmd);
default: return -EINVAL;
}
}
int ccp_run_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
{ int ret;
switch (cmd->engine) { case CCP_ENGINE_AES: switch (cmd->u.aes.mode) { case CCP_AES_MODE_CMAC:
ret = ccp_run_aes_cmac_cmd(cmd_q, cmd); break; case CCP_AES_MODE_GCM:
ret = ccp_run_aes_gcm_cmd(cmd_q, cmd); break; default:
ret = ccp_run_aes_cmd(cmd_q, cmd); break;
} break; case CCP_ENGINE_XTS_AES_128:
ret = ccp_run_xts_aes_cmd(cmd_q, cmd); break; case CCP_ENGINE_DES3:
ret = ccp_run_des3_cmd(cmd_q, cmd); break; case CCP_ENGINE_SHA:
ret = ccp_run_sha_cmd(cmd_q, cmd); break; case CCP_ENGINE_RSA:
ret = ccp_run_rsa_cmd(cmd_q, cmd); break; case CCP_ENGINE_PASSTHRU: if (cmd->flags & CCP_CMD_PASSTHRU_NO_DMA_MAP)
ret = ccp_run_passthru_nomap_cmd(cmd_q, cmd); else
ret = ccp_run_passthru_cmd(cmd_q, cmd); break; case CCP_ENGINE_ECC:
ret = ccp_run_ecc_cmd(cmd_q, cmd); break; default:
ret = -EINVAL;
}
return ret;
}
Messung V0.5 in Prozent
¤ Diese beiden folgenden Angebotsgruppen bietet das Unternehmen0.56Angebot
(Wie Sie bei der Firma Beratungs- und Dienstleistungen beauftragen können 2026-09-28)
¤
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.