Commit 83d650ab authored by Gary R Hook's avatar Gary R Hook Committed by Herbert Xu

crypto: ccp - Simplify some buffer management routines

The reverse-get/set functions can be simplified by
eliminating unused code.
Signed-off-by: default avatarGary R Hook <gary.hook@amd.com>
Signed-off-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>
parent 4cdf101e
...@@ -184,62 +184,46 @@ static void ccp_get_dm_area(struct ccp_dm_workarea *wa, unsigned int wa_offset, ...@@ -184,62 +184,46 @@ static void ccp_get_dm_area(struct ccp_dm_workarea *wa, unsigned int wa_offset,
} }
static int ccp_reverse_set_dm_area(struct ccp_dm_workarea *wa, static int ccp_reverse_set_dm_area(struct ccp_dm_workarea *wa,
unsigned int wa_offset,
struct scatterlist *sg, struct scatterlist *sg,
unsigned int len, unsigned int se_len, unsigned int sg_offset,
bool sign_extend) unsigned int len)
{ {
unsigned int nbytes, sg_offset, dm_offset, sb_len, i; u8 *p, *q;
u8 buffer[CCP_REVERSE_BUF_SIZE];
ccp_set_dm_area(wa, wa_offset, sg, sg_offset, len);
if (WARN_ON(se_len > sizeof(buffer)))
return -EINVAL; p = wa->address + wa_offset;
q = p + len - 1;
sg_offset = len; while (p < q) {
dm_offset = 0; *p = *p ^ *q;
nbytes = len; *q = *p ^ *q;
while (nbytes) { *p = *p ^ *q;
sb_len = min_t(unsigned int, nbytes, se_len); p++;
sg_offset -= sb_len; q--;
scatterwalk_map_and_copy(buffer, sg, sg_offset, sb_len, 0);
for (i = 0; i < sb_len; i++)
wa->address[dm_offset + i] = buffer[sb_len - i - 1];
dm_offset += sb_len;
nbytes -= sb_len;
if ((sb_len != se_len) && sign_extend) {
/* Must sign-extend to nearest sign-extend length */
if (wa->address[dm_offset - 1] & 0x80)
memset(wa->address + dm_offset, 0xff,
se_len - sb_len);
}
} }
return 0; return 0;
} }
static void ccp_reverse_get_dm_area(struct ccp_dm_workarea *wa, static void ccp_reverse_get_dm_area(struct ccp_dm_workarea *wa,
unsigned int wa_offset,
struct scatterlist *sg, struct scatterlist *sg,
unsigned int sg_offset,
unsigned int len) unsigned int len)
{ {
unsigned int nbytes, sg_offset, dm_offset, sb_len, i; u8 *p, *q;
u8 buffer[CCP_REVERSE_BUF_SIZE];
p = wa->address + wa_offset;
sg_offset = 0; q = p + len - 1;
dm_offset = len; while (p < q) {
nbytes = len; *p = *p ^ *q;
while (nbytes) { *q = *p ^ *q;
sb_len = min_t(unsigned int, nbytes, sizeof(buffer)); *p = *p ^ *q;
dm_offset -= sb_len; p++;
q--;
for (i = 0; i < sb_len; i++)
buffer[sb_len - i - 1] = wa->address[dm_offset + i];
scatterwalk_map_and_copy(buffer, sg, sg_offset, sb_len, 1);
sg_offset += sb_len;
nbytes -= sb_len;
} }
ccp_get_dm_area(wa, wa_offset, sg, sg_offset, len);
} }
static void ccp_free_data(struct ccp_data *data, struct ccp_cmd_queue *cmd_q) static void ccp_free_data(struct ccp_data *data, struct ccp_cmd_queue *cmd_q)
...@@ -1269,8 +1253,7 @@ static int ccp_run_rsa_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd) ...@@ -1269,8 +1253,7 @@ static int ccp_run_rsa_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
if (ret) if (ret)
goto e_sb; goto e_sb;
ret = ccp_reverse_set_dm_area(&exp, rsa->exp, rsa->exp_len, ret = ccp_reverse_set_dm_area(&exp, 0, rsa->exp, 0, rsa->exp_len);
CCP_SB_BYTES, false);
if (ret) if (ret)
goto e_exp; goto e_exp;
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,
...@@ -1288,16 +1271,12 @@ static int ccp_run_rsa_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd) ...@@ -1288,16 +1271,12 @@ static int ccp_run_rsa_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
if (ret) if (ret)
goto e_exp; goto e_exp;
ret = ccp_reverse_set_dm_area(&src, rsa->mod, rsa->mod_len, ret = ccp_reverse_set_dm_area(&src, 0, rsa->mod, 0, rsa->mod_len);
CCP_SB_BYTES, false);
if (ret) if (ret)
goto e_src; goto e_src;
src.address += o_len; /* Adjust the address for the copy operation */ ret = ccp_reverse_set_dm_area(&src, o_len, rsa->src, 0, rsa->src_len);
ret = ccp_reverse_set_dm_area(&src, rsa->src, rsa->src_len,
CCP_SB_BYTES, false);
if (ret) if (ret)
goto e_src; goto e_src;
src.address -= o_len; /* Reset the address to original value */
/* 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_data(&dst, cmd_q, rsa->dst, rsa->mod_len,
...@@ -1322,7 +1301,7 @@ static int ccp_run_rsa_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd) ...@@ -1322,7 +1301,7 @@ 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, rsa->dst, rsa->mod_len); ccp_reverse_get_dm_area(&dst.dm_wa, 0, rsa->dst, 0, rsa->mod_len);
e_dst: e_dst:
ccp_free_data(&dst, cmd_q); ccp_free_data(&dst, cmd_q);
...@@ -1574,25 +1553,22 @@ static int ccp_run_ecc_mm_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd) ...@@ -1574,25 +1553,22 @@ static int ccp_run_ecc_mm_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
save = src.address; save = src.address;
/* Copy the ECC modulus */ /* Copy the ECC modulus */
ret = ccp_reverse_set_dm_area(&src, ecc->mod, ecc->mod_len, ret = ccp_reverse_set_dm_area(&src, 0, ecc->mod, 0, ecc->mod_len);
CCP_ECC_OPERAND_SIZE, false);
if (ret) if (ret)
goto e_src; goto e_src;
src.address += CCP_ECC_OPERAND_SIZE; src.address += CCP_ECC_OPERAND_SIZE;
/* Copy the first operand */ /* Copy the first operand */
ret = ccp_reverse_set_dm_area(&src, ecc->u.mm.operand_1, ret = ccp_reverse_set_dm_area(&src, 0, ecc->u.mm.operand_1, 0,
ecc->u.mm.operand_1_len, ecc->u.mm.operand_1_len);
CCP_ECC_OPERAND_SIZE, false);
if (ret) if (ret)
goto e_src; goto e_src;
src.address += CCP_ECC_OPERAND_SIZE; src.address += CCP_ECC_OPERAND_SIZE;
if (ecc->function != CCP_ECC_FUNCTION_MINV_384BIT) { if (ecc->function != CCP_ECC_FUNCTION_MINV_384BIT) {
/* Copy the second operand */ /* Copy the second operand */
ret = ccp_reverse_set_dm_area(&src, ecc->u.mm.operand_2, ret = ccp_reverse_set_dm_area(&src, 0, ecc->u.mm.operand_2, 0,
ecc->u.mm.operand_2_len, ecc->u.mm.operand_2_len);
CCP_ECC_OPERAND_SIZE, false);
if (ret) if (ret)
goto e_src; goto e_src;
src.address += CCP_ECC_OPERAND_SIZE; src.address += CCP_ECC_OPERAND_SIZE;
...@@ -1631,7 +1607,8 @@ static int ccp_run_ecc_mm_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd) ...@@ -1631,7 +1607,8 @@ static int ccp_run_ecc_mm_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
} }
/* Save the ECC result */ /* Save the ECC result */
ccp_reverse_get_dm_area(&dst, ecc->u.mm.result, CCP_ECC_MODULUS_BYTES); ccp_reverse_get_dm_area(&dst, 0, ecc->u.mm.result, 0,
CCP_ECC_MODULUS_BYTES);
e_dst: e_dst:
ccp_dm_free(&dst); ccp_dm_free(&dst);
...@@ -1699,22 +1676,19 @@ static int ccp_run_ecc_pm_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd) ...@@ -1699,22 +1676,19 @@ static int ccp_run_ecc_pm_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
save = src.address; save = src.address;
/* Copy the ECC modulus */ /* Copy the ECC modulus */
ret = ccp_reverse_set_dm_area(&src, ecc->mod, ecc->mod_len, ret = ccp_reverse_set_dm_area(&src, 0, ecc->mod, 0, ecc->mod_len);
CCP_ECC_OPERAND_SIZE, false);
if (ret) if (ret)
goto e_src; goto e_src;
src.address += CCP_ECC_OPERAND_SIZE; src.address += CCP_ECC_OPERAND_SIZE;
/* Copy the first point X and Y coordinate */ /* Copy the first point X and Y coordinate */
ret = ccp_reverse_set_dm_area(&src, ecc->u.pm.point_1.x, ret = ccp_reverse_set_dm_area(&src, 0, ecc->u.pm.point_1.x, 0,
ecc->u.pm.point_1.x_len, ecc->u.pm.point_1.x_len);
CCP_ECC_OPERAND_SIZE, false);
if (ret) if (ret)
goto e_src; goto e_src;
src.address += CCP_ECC_OPERAND_SIZE; src.address += CCP_ECC_OPERAND_SIZE;
ret = ccp_reverse_set_dm_area(&src, ecc->u.pm.point_1.y, ret = ccp_reverse_set_dm_area(&src, 0, ecc->u.pm.point_1.y, 0,
ecc->u.pm.point_1.y_len, ecc->u.pm.point_1.y_len);
CCP_ECC_OPERAND_SIZE, false);
if (ret) if (ret)
goto e_src; goto e_src;
src.address += CCP_ECC_OPERAND_SIZE; src.address += CCP_ECC_OPERAND_SIZE;
...@@ -1725,15 +1699,13 @@ static int ccp_run_ecc_pm_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd) ...@@ -1725,15 +1699,13 @@ static int ccp_run_ecc_pm_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
if (ecc->function == CCP_ECC_FUNCTION_PADD_384BIT) { if (ecc->function == CCP_ECC_FUNCTION_PADD_384BIT) {
/* Copy the second point X and Y coordinate */ /* Copy the second point X and Y coordinate */
ret = ccp_reverse_set_dm_area(&src, ecc->u.pm.point_2.x, ret = ccp_reverse_set_dm_area(&src, 0, ecc->u.pm.point_2.x, 0,
ecc->u.pm.point_2.x_len, ecc->u.pm.point_2.x_len);
CCP_ECC_OPERAND_SIZE, false);
if (ret) if (ret)
goto e_src; goto e_src;
src.address += CCP_ECC_OPERAND_SIZE; src.address += CCP_ECC_OPERAND_SIZE;
ret = ccp_reverse_set_dm_area(&src, ecc->u.pm.point_2.y, ret = ccp_reverse_set_dm_area(&src, 0, ecc->u.pm.point_2.y, 0,
ecc->u.pm.point_2.y_len, ecc->u.pm.point_2.y_len);
CCP_ECC_OPERAND_SIZE, false);
if (ret) if (ret)
goto e_src; goto e_src;
src.address += CCP_ECC_OPERAND_SIZE; src.address += CCP_ECC_OPERAND_SIZE;
...@@ -1743,19 +1715,17 @@ static int ccp_run_ecc_pm_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd) ...@@ -1743,19 +1715,17 @@ static int ccp_run_ecc_pm_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
src.address += CCP_ECC_OPERAND_SIZE; src.address += CCP_ECC_OPERAND_SIZE;
} else { } else {
/* Copy the Domain "a" parameter */ /* Copy the Domain "a" parameter */
ret = ccp_reverse_set_dm_area(&src, ecc->u.pm.domain_a, ret = ccp_reverse_set_dm_area(&src, 0, ecc->u.pm.domain_a, 0,
ecc->u.pm.domain_a_len, ecc->u.pm.domain_a_len);
CCP_ECC_OPERAND_SIZE, false);
if (ret) if (ret)
goto e_src; goto e_src;
src.address += CCP_ECC_OPERAND_SIZE; src.address += CCP_ECC_OPERAND_SIZE;
if (ecc->function == CCP_ECC_FUNCTION_PMUL_384BIT) { if (ecc->function == CCP_ECC_FUNCTION_PMUL_384BIT) {
/* Copy the scalar value */ /* Copy the scalar value */
ret = ccp_reverse_set_dm_area(&src, ecc->u.pm.scalar, ret = ccp_reverse_set_dm_area(&src, 0,
ecc->u.pm.scalar_len, ecc->u.pm.scalar, 0,
CCP_ECC_OPERAND_SIZE, ecc->u.pm.scalar_len);
false);
if (ret) if (ret)
goto e_src; goto e_src;
src.address += CCP_ECC_OPERAND_SIZE; src.address += CCP_ECC_OPERAND_SIZE;
...@@ -1800,10 +1770,10 @@ static int ccp_run_ecc_pm_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd) ...@@ -1800,10 +1770,10 @@ static int ccp_run_ecc_pm_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
save = dst.address; save = dst.address;
/* Save the ECC result X and Y coordinates */ /* Save the ECC result X and Y coordinates */
ccp_reverse_get_dm_area(&dst, ecc->u.pm.result.x, ccp_reverse_get_dm_area(&dst, 0, ecc->u.pm.result.x, 0,
CCP_ECC_MODULUS_BYTES); CCP_ECC_MODULUS_BYTES);
dst.address += CCP_ECC_OUTPUT_SIZE; dst.address += CCP_ECC_OUTPUT_SIZE;
ccp_reverse_get_dm_area(&dst, ecc->u.pm.result.y, ccp_reverse_get_dm_area(&dst, 0, ecc->u.pm.result.y, 0,
CCP_ECC_MODULUS_BYTES); CCP_ECC_MODULUS_BYTES);
dst.address += CCP_ECC_OUTPUT_SIZE; dst.address += CCP_ECC_OUTPUT_SIZE;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment