Skip to content

Commit

Permalink
pkcs11-tool: add pure EdDSA support to sign/verify
Browse files Browse the repository at this point in the history
- add matching of ec_curve_info using ec_params value
- distinguish between ed25519 and edd448 using curve size

Related #2952
  • Loading branch information
dlegaultbbry committed Jan 11, 2024
1 parent 773fcc6 commit acca970
Showing 1 changed file with 77 additions and 0 deletions.
77 changes: 77 additions & 0 deletions src/tools/pkcs11-tool.c
Original file line number Diff line number Diff line change
Expand Up @@ -2081,6 +2081,30 @@ static int unlock_pin(CK_SLOT_ID slot, CK_SESSION_HANDLE sess, int login_type)
return 0;
}

/* return matching ec_curve_info or NULL based on ec_params */
static const struct ec_curve_info* match_ec_curve_by_params(const unsigned char *ec_params, CK_ULONG ec_params_size)
{
char ecpbuf[64];
CK_ULONG ecpsize = ec_params_size*2;

if (ec_params_size > (sizeof(ecpbuf)/2)) {
util_fatal("Invalid EC params");
}

sc_bin_to_hex(ec_params, ec_params_size, ecpbuf, sizeof(ecpbuf), 0);

for (size_t i = 0; ec_curve_infos[i].name != NULL; ++i) {

if ((strlen(ec_curve_infos[i].ec_params) == ecpsize) &&
(strcmp(ec_curve_infos[i].ec_params, ecpbuf) == 0))
{
return &ec_curve_infos[i];
}
}

return NULL;
}

/* return digest length in bytes */
static unsigned long hash_length(const int hash) {
unsigned long sLen = 0;
Expand Down Expand Up @@ -2264,6 +2288,9 @@ static void sign_data(CK_SLOT_ID slot, CK_SESSION_HANDLE session,
unsigned char in_buffer[1025], sig_buffer[512];
CK_MECHANISM mech;
CK_RSA_PKCS_PSS_PARAMS pss_params;
CK_EDDSA_PARAMS eddsa_params = {
.phFlag = CK_FALSE,
};
CK_RV rv;
CK_ULONG sig_len;
int fd, r;
Expand All @@ -2278,6 +2305,29 @@ static void sign_data(CK_SLOT_ID slot, CK_SESSION_HANDLE session,
mech.mechanism = opt_mechanism;
hashlen = parse_pss_params(session, key, &mech, &pss_params);

/* support pure EdDSA only */
if (opt_mechanism == CKM_EDDSA) {
const struct ec_curve_info *curve;
unsigned char *ec_params;
CK_ULONG ec_params_size = 0;

ec_params = getEC_PARAMS(session, key, &ec_params_size);
if (ec_params == NULL) {
util_fatal("Key has no EC_PARAMS attribute");
}

curve = match_ec_curve_by_params(ec_params, ec_params_size);
if (curve == NULL) {
util_fatal("Unknown or unsupported EC curve used in key");
}

/* Ed448: need the params defined but default to false */
if (curve->size == 448) {
mech.pParameter = &eddsa_params;
mech.ulParameterLen = (CK_ULONG)sizeof(eddsa_params);
}
}

if (opt_input == NULL)
fd = 0;
else if ((fd = open(opt_input, O_RDONLY|O_BINARY)) < 0)
Expand Down Expand Up @@ -2370,6 +2420,9 @@ static void verify_signature(CK_SLOT_ID slot, CK_SESSION_HANDLE session,
unsigned char in_buffer[1025], sig_buffer[512];
CK_MECHANISM mech;
CK_RSA_PKCS_PSS_PARAMS pss_params;
CK_EDDSA_PARAMS eddsa_params = {
.phFlag = CK_FALSE,
};
CK_RV rv;
CK_ULONG sig_len;
int fd, fd2, r, r2;
Expand All @@ -2394,6 +2447,30 @@ static void verify_signature(CK_SLOT_ID slot, CK_SESSION_HANDLE session,
fprintf(stderr, "Warning, requesting salt length recovery from signature (supported only in in opensc pkcs11 module).\n");
}
}

/* support pure EdDSA only */
if (opt_mechanism == CKM_EDDSA) {
const struct ec_curve_info *curve;
unsigned char *ec_params;
CK_ULONG ec_params_size = 0;

ec_params = getEC_PARAMS(session, key, &ec_params_size);
if (ec_params == NULL) {
util_fatal("Key has no EC_PARAMS attribute");
}

curve = match_ec_curve_by_params(ec_params, ec_params_size);
if (curve == NULL) {
util_fatal("Unknown or unsupported EC curve used in key");
}

/* Ed448: need the params defined but default to false */
if (curve->size == 448) {
mech.pParameter = &eddsa_params;
mech.ulParameterLen = (CK_ULONG)sizeof(eddsa_params);
}
}

/* Open a signature file */
if (opt_signature_file == NULL)
util_fatal("No file with signature provided. Use --signature-file");
Expand Down

0 comments on commit acca970

Please sign in to comment.