coolkey: Rewrite coolkey_rsa_op() for better readability

This commit is contained in:
Jakub Jelen 2020-06-15 14:27:33 +02:00 committed by Frank Morgner
parent 0cda376dba
commit 43379b3b22
1 changed files with 38 additions and 40 deletions

View File

@ -1675,55 +1675,43 @@ typedef struct coolkey_compute_ecc_params {
u8 buf[MAX_COMPUTE_BUF];
} coolkey_compute_ecc_params_t;
static int coolkey_rsa_op(sc_card_t *card,
const u8 * data, size_t datalen,
u8 * out, size_t max_out_len)
static int coolkey_rsa_op(sc_card_t *card, const u8 * data, size_t datalen,
u8 * out, size_t max_out_len)
{
int r;
const u8 *crypt_in;
u8 **crypt_out_p;
size_t crypt_in_len, *crypt_out_len_p;
coolkey_private_data_t * priv = COOLKEY_DATA(card);
u8 **crypt_out_p = NULL;
size_t crypt_out_len_p;
coolkey_private_data_t *priv = COOLKEY_DATA(card);
coolkey_compute_crypt_params_t params;
u8 key_number;
size_t params_len;
size_t buf_len;
u8 buf[MAX_COMPUTE_BUF+2];
u8 *buf_out;
u8 buf[MAX_COMPUTE_BUF + 2];
SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
sc_log(card->ctx,
"datalen=%"SC_FORMAT_LEN_SIZE_T"u outlen=%"SC_FORMAT_LEN_SIZE_T"u\n",
datalen, max_out_len);
crypt_in = data;
crypt_in_len = datalen;
buf_out = &buf[0];
crypt_out_p = &buf_out;
buf_len = sizeof(buf);
crypt_out_len_p = &buf_len;
key_number = priv->key_id;
params.init.mode = COOLKEY_CRYPT_MODE_RSA_NO_PAD;
params.init.location = COOLKEY_CRYPT_LOCATION_APDU;
params.init.direction = COOLKEY_CRYPT_DIRECTION_ENCRYPT; /* for no pad, direction is irrelevant */
sc_log(card->ctx, "datalen=%"SC_FORMAT_LEN_SIZE_T"u outlen=%"SC_FORMAT_LEN_SIZE_T"u\n",
datalen, max_out_len);
if (priv->key_id > 0xff) {
r = SC_ERROR_NO_DEFAULT_KEY;
goto done;
}
key_number = priv->key_id;
params_len = sizeof(params.init) + crypt_in_len;
memset(&params, 0, sizeof(params));
params.init.mode = COOLKEY_CRYPT_MODE_RSA_NO_PAD;
params.init.direction = COOLKEY_CRYPT_DIRECTION_ENCRYPT; /* for no pad, direction is irrelevant */
/* send the data to the card if necessary */
if (crypt_in_len > MAX_COMPUTE_BUF) {
if (datalen > MAX_COMPUTE_BUF) {
/* We need to write data to special object on the card as it does not safely fit APDU */
u8 len_buf[2];
params.init.location = COOLKEY_CRYPT_LOCATION_DL_OBJECT;
params_len = sizeof(params.init);
crypt_in = NULL;
crypt_in_len = 0;
*crypt_out_p = NULL;
*crypt_out_len_p = 0;
crypt_out_len_p = 0;
ushort2bebytes(len_buf, datalen);
@ -1733,26 +1721,36 @@ static int coolkey_rsa_op(sc_card_t *card,
goto done;
}
r = coolkey_write_object(card, COOLKEY_DL_OBJECT_ID, 2, data, datalen, priv->nonce,
sizeof(priv->nonce));
r = coolkey_write_object(card, COOLKEY_DL_OBJECT_ID, 2, data, datalen, priv->nonce, sizeof(priv->nonce));
if (r < 0) {
goto done;
}
ushort2bebytes(params.init.buf_len, 0);
} else {
/* The data fits in APDU. Copy it to the params object */
u8 *buf_out;
size_t buf_len;
}
ushort2bebytes(params.init.buf_len, crypt_in_len);
if (crypt_in_len) {
memcpy(params.buf, crypt_in, crypt_in_len);
}
params.init.location = COOLKEY_CRYPT_LOCATION_APDU;
params_len = sizeof(params.init) + datalen;
buf_out = &buf[0];
crypt_out_p = &buf_out;
buf_len = sizeof(buf);
crypt_out_len_p = buf_len;
ushort2bebytes(params.init.buf_len, datalen);
memcpy(params.buf, data, datalen);
}
r = coolkey_apdu_io(card, COOLKEY_CLASS, COOLKEY_INS_COMPUTE_CRYPT,
key_number, COOLKEY_CRYPT_ONE_STEP, (u8 *)&params, params_len,
crypt_out_p, crypt_out_len_p, priv->nonce, sizeof(priv->nonce));
crypt_out_p, &crypt_out_len_p, priv->nonce, sizeof(priv->nonce));
if (r < 0) {
goto done;
}
if (datalen > MAX_COMPUTE_BUF) {
u8 len_buf[2];
size_t out_length;
@ -1779,7 +1777,7 @@ static int coolkey_rsa_op(sc_card_t *card,
goto done;
}
out_length = MIN(out_length, max_out_len);
memcpy(out, buf+2, out_length);
memcpy(out, buf + 2, out_length);
r = out_length;
}