forked from mirrors/linux
		
	crypto: ccp - Fix base RSA function for version 5 CCPs
Version 5 devices have requirements for buffer lengths, as well as parameter format (e.g. bits vs. bytes). Fix the base CCP driver code to meet requirements all supported versions. Signed-off-by: Gary R Hook <gary.hook@amd.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
This commit is contained in:
		
							parent
							
								
									68cc652f83
								
							
						
					
					
						commit
						6ba46c7d4d
					
				
					 2 changed files with 54 additions and 34 deletions
				
			
		| 
						 | 
					@ -469,7 +469,7 @@ static int ccp5_perform_rsa(struct ccp_op *op)
 | 
				
			||||||
	CCP5_CMD_PROT(&desc) = 0;
 | 
						CCP5_CMD_PROT(&desc) = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	function.raw = 0;
 | 
						function.raw = 0;
 | 
				
			||||||
	CCP_RSA_SIZE(&function) = op->u.rsa.mod_size >> 3;
 | 
						CCP_RSA_SIZE(&function) = (op->u.rsa.mod_size + 7) >> 3;
 | 
				
			||||||
	CCP5_CMD_FUNCTION(&desc) = function.raw;
 | 
						CCP5_CMD_FUNCTION(&desc) = function.raw;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	CCP5_CMD_LEN(&desc) = op->u.rsa.input_len;
 | 
						CCP5_CMD_LEN(&desc) = op->u.rsa.input_len;
 | 
				
			||||||
| 
						 | 
					@ -484,10 +484,10 @@ static int ccp5_perform_rsa(struct ccp_op *op)
 | 
				
			||||||
	CCP5_CMD_DST_HI(&desc) = ccp_addr_hi(&op->dst.u.dma);
 | 
						CCP5_CMD_DST_HI(&desc) = ccp_addr_hi(&op->dst.u.dma);
 | 
				
			||||||
	CCP5_CMD_DST_MEM(&desc) = CCP_MEMTYPE_SYSTEM;
 | 
						CCP5_CMD_DST_MEM(&desc) = CCP_MEMTYPE_SYSTEM;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/* Exponent is in LSB memory */
 | 
						/* Key (Exponent) is in external memory */
 | 
				
			||||||
	CCP5_CMD_KEY_LO(&desc) = op->sb_key * LSB_ITEM_SIZE;
 | 
						CCP5_CMD_KEY_LO(&desc) = ccp_addr_lo(&op->exp.u.dma);
 | 
				
			||||||
	CCP5_CMD_KEY_HI(&desc) = 0;
 | 
						CCP5_CMD_KEY_HI(&desc) = ccp_addr_hi(&op->exp.u.dma);
 | 
				
			||||||
	CCP5_CMD_KEY_MEM(&desc) = CCP_MEMTYPE_SB;
 | 
						CCP5_CMD_KEY_MEM(&desc) = CCP_MEMTYPE_SYSTEM;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return ccp5_do_cmd(&desc, op->cmd_q);
 | 
						return ccp5_do_cmd(&desc, op->cmd_q);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1731,8 +1731,7 @@ static int ccp_run_sha_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
 | 
				
			||||||
static int ccp_run_rsa_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
 | 
					static int ccp_run_rsa_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	struct ccp_rsa_engine *rsa = &cmd->u.rsa;
 | 
						struct ccp_rsa_engine *rsa = &cmd->u.rsa;
 | 
				
			||||||
	struct ccp_dm_workarea exp, src;
 | 
						struct ccp_dm_workarea exp, src, dst;
 | 
				
			||||||
	struct ccp_data dst;
 | 
					 | 
				
			||||||
	struct ccp_op op;
 | 
						struct ccp_op op;
 | 
				
			||||||
	unsigned int sb_count, i_len, o_len;
 | 
						unsigned int sb_count, i_len, o_len;
 | 
				
			||||||
	int ret;
 | 
						int ret;
 | 
				
			||||||
| 
						 | 
					@ -1743,30 +1742,40 @@ static int ccp_run_rsa_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
 | 
				
			||||||
	if (!rsa->exp || !rsa->mod || !rsa->src || !rsa->dst)
 | 
						if (!rsa->exp || !rsa->mod || !rsa->src || !rsa->dst)
 | 
				
			||||||
		return -EINVAL;
 | 
							return -EINVAL;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						memset(&op, 0, sizeof(op));
 | 
				
			||||||
 | 
						op.cmd_q = cmd_q;
 | 
				
			||||||
 | 
						op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/* The RSA modulus must precede the message being acted upon, so
 | 
						/* The RSA modulus must precede the message being acted upon, so
 | 
				
			||||||
	 * it must be copied to a DMA area where the message and the
 | 
						 * it must be copied to a DMA area where the message and the
 | 
				
			||||||
	 * modulus can be concatenated.  Therefore the input buffer
 | 
						 * modulus can be concatenated.  Therefore the input buffer
 | 
				
			||||||
	 * length required is twice the output buffer length (which
 | 
						 * length required is twice the output buffer length (which
 | 
				
			||||||
	 * must be a multiple of 256-bits).
 | 
						 * must be a multiple of 256-bits).  Compute o_len, i_len in bytes.
 | 
				
			||||||
 | 
						 * Buffer sizes must be a multiple of 32 bytes; rounding up may be
 | 
				
			||||||
 | 
						 * required.
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	o_len = ((rsa->key_size + 255) / 256) * 32;
 | 
						o_len = 32 * ((rsa->key_size + 255) / 256);
 | 
				
			||||||
	i_len = o_len * 2;
 | 
						i_len = o_len * 2;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (cmd_q->ccp->vdata->version < CCP_VERSION(5, 0)) {
 | 
				
			||||||
 | 
							/* sb_count is the number of storage block slots required
 | 
				
			||||||
 | 
							 * for the modulus.
 | 
				
			||||||
 | 
							 */
 | 
				
			||||||
		sb_count = o_len / CCP_SB_BYTES;
 | 
							sb_count = o_len / CCP_SB_BYTES;
 | 
				
			||||||
 | 
							op.sb_key = cmd_q->ccp->vdata->perform->sballoc(cmd_q,
 | 
				
			||||||
	memset(&op, 0, sizeof(op));
 | 
													sb_count);
 | 
				
			||||||
	op.cmd_q = cmd_q;
 | 
					 | 
				
			||||||
	op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
 | 
					 | 
				
			||||||
	op.sb_key = cmd_q->ccp->vdata->perform->sballoc(cmd_q, sb_count);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		if (!op.sb_key)
 | 
							if (!op.sb_key)
 | 
				
			||||||
			return -EIO;
 | 
								return -EIO;
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							/* A version 5 device allows a modulus size that will not fit
 | 
				
			||||||
 | 
							 * in the LSB, so the command will transfer it from memory.
 | 
				
			||||||
 | 
							 * Set the sb key to the default, even though it's not used.
 | 
				
			||||||
 | 
							 */
 | 
				
			||||||
 | 
							op.sb_key = cmd_q->sb_key;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/* The RSA exponent may span multiple (32-byte) SB entries and must
 | 
						/* The RSA exponent must be in little endian format. Reverse its
 | 
				
			||||||
	 * be in little endian format. Reverse copy each 32-byte chunk
 | 
						 * byte order.
 | 
				
			||||||
	 * of the exponent (En chunk to E0 chunk, E(n-1) chunk to E1 chunk)
 | 
					 | 
				
			||||||
	 * and each byte within that chunk and do not perform any byte swap
 | 
					 | 
				
			||||||
	 * operations on the passthru operation.
 | 
					 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	ret = ccp_init_dm_workarea(&exp, cmd_q, o_len, DMA_TO_DEVICE);
 | 
						ret = ccp_init_dm_workarea(&exp, cmd_q, o_len, DMA_TO_DEVICE);
 | 
				
			||||||
	if (ret)
 | 
						if (ret)
 | 
				
			||||||
| 
						 | 
					@ -1775,12 +1784,23 @@ static int ccp_run_rsa_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
 | 
				
			||||||
	ret = ccp_reverse_set_dm_area(&exp, 0, rsa->exp, 0, rsa->exp_len);
 | 
						ret = ccp_reverse_set_dm_area(&exp, 0, rsa->exp, 0, rsa->exp_len);
 | 
				
			||||||
	if (ret)
 | 
						if (ret)
 | 
				
			||||||
		goto e_exp;
 | 
							goto e_exp;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (cmd_q->ccp->vdata->version < CCP_VERSION(5, 0)) {
 | 
				
			||||||
 | 
							/* Copy the exponent to the local storage block, using
 | 
				
			||||||
 | 
							 * as many 32-byte blocks as were allocated above. It's
 | 
				
			||||||
 | 
							 * already little endian, so no further change is required.
 | 
				
			||||||
 | 
							 */
 | 
				
			||||||
		ret = ccp_copy_to_sb(cmd_q, &exp, op.jobid, op.sb_key,
 | 
							ret = ccp_copy_to_sb(cmd_q, &exp, op.jobid, op.sb_key,
 | 
				
			||||||
				     CCP_PASSTHRU_BYTESWAP_NOOP);
 | 
									     CCP_PASSTHRU_BYTESWAP_NOOP);
 | 
				
			||||||
		if (ret) {
 | 
							if (ret) {
 | 
				
			||||||
			cmd->engine_error = cmd_q->cmd_error;
 | 
								cmd->engine_error = cmd_q->cmd_error;
 | 
				
			||||||
			goto e_exp;
 | 
								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
 | 
						/* Concatenate the modulus and the message. Both the modulus and
 | 
				
			||||||
	 * the operands must be in little endian format.  Since the input
 | 
						 * the operands must be in little endian format.  Since the input
 | 
				
			||||||
| 
						 | 
					@ -1798,8 +1818,7 @@ static int ccp_run_rsa_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
 | 
				
			||||||
		goto e_src;
 | 
							goto e_src;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/* Prepare the output area for the operation */
 | 
						/* Prepare the output area for the operation */
 | 
				
			||||||
	ret = ccp_init_data(&dst, cmd_q, rsa->dst, rsa->mod_len,
 | 
						ret = ccp_init_dm_workarea(&dst, cmd_q, o_len, DMA_FROM_DEVICE);
 | 
				
			||||||
			    o_len, DMA_FROM_DEVICE);
 | 
					 | 
				
			||||||
	if (ret)
 | 
						if (ret)
 | 
				
			||||||
		goto e_src;
 | 
							goto e_src;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1807,7 +1826,7 @@ static int ccp_run_rsa_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
 | 
				
			||||||
	op.src.u.dma.address = src.dma.address;
 | 
						op.src.u.dma.address = src.dma.address;
 | 
				
			||||||
	op.src.u.dma.offset = 0;
 | 
						op.src.u.dma.offset = 0;
 | 
				
			||||||
	op.src.u.dma.length = i_len;
 | 
						op.src.u.dma.length = i_len;
 | 
				
			||||||
	op.dst.u.dma.address = dst.dm_wa.dma.address;
 | 
						op.dst.u.dma.address = dst.dma.address;
 | 
				
			||||||
	op.dst.u.dma.offset = 0;
 | 
						op.dst.u.dma.offset = 0;
 | 
				
			||||||
	op.dst.u.dma.length = o_len;
 | 
						op.dst.u.dma.length = o_len;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1820,10 +1839,10 @@ static int ccp_run_rsa_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
 | 
				
			||||||
		goto e_dst;
 | 
							goto e_dst;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	ccp_reverse_get_dm_area(&dst.dm_wa, 0, rsa->dst, 0, rsa->mod_len);
 | 
						ccp_reverse_get_dm_area(&dst, 0, rsa->dst, 0, rsa->mod_len);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
e_dst:
 | 
					e_dst:
 | 
				
			||||||
	ccp_free_data(&dst, cmd_q);
 | 
						ccp_dm_free(&dst);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
e_src:
 | 
					e_src:
 | 
				
			||||||
	ccp_dm_free(&src);
 | 
						ccp_dm_free(&src);
 | 
				
			||||||
| 
						 | 
					@ -1832,6 +1851,7 @@ static int ccp_run_rsa_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
 | 
				
			||||||
	ccp_dm_free(&exp);
 | 
						ccp_dm_free(&exp);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
e_sb:
 | 
					e_sb:
 | 
				
			||||||
 | 
						if (cmd_q->ccp->vdata->version < CCP_VERSION(5, 0))
 | 
				
			||||||
		cmd_q->ccp->vdata->perform->sbfree(cmd_q, op.sb_key, sb_count);
 | 
							cmd_q->ccp->vdata->perform->sbfree(cmd_q, op.sb_key, sb_count);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return ret;
 | 
						return ret;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in a new issue