Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PKCS#11 Ed25519 #5559

Merged
merged 1 commit into from
Oct 6, 2022
Merged

PKCS#11 Ed25519 #5559

merged 1 commit into from
Oct 6, 2022

Conversation

varder
Copy link

@varder varder commented Sep 29, 2022

ta: pkcs11: Add Ed25519 support
Add functionality to generate, import keys, sign/verify for
Ed25519, Ed25519ctx and Ed25519ph.

Copy link
Contributor

@etienne-lms etienne-lms left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the port to pkcs11 TA.
I've pointed the coding style issues, mostly indentation. See also few other comments.
That said, the overall looks good but i'll need another pass.

@@ -1169,6 +1169,8 @@ enum pkcs11_key_type {
PKCS11_CKK_DSA = 0x001,
PKCS11_CKK_DH = 0x002,
PKCS11_CKK_EC = 0x003,
PKCS11_CKK_EC_EDWARDS = 0x040,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation + move below line 1180 following macro values ordering.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@@ -1250,6 +1252,8 @@ enum pkcs11_mechanism_id {
PKCS11_CKM_ECDSA_SHA384 = 0x01045,
PKCS11_CKM_ECDSA_SHA512 = 0x01046,
PKCS11_CKM_ECDH1_DERIVE = 0x01050,
PKCS11_CKM_EC_EDWARDS_KEY_PAIR_GEN = 0x01055,
PKCS11_CKM_EDDSA = 0x01057,
Copy link
Contributor

@etienne-lms etienne-lms Sep 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PKCS11_CKM_EDDSA and PKCS11_CKM_ECDSA can be easy to confuse.
Would you be ok the rename it? Suggestion PKCS11_CKM_EC_EDDSA.
(and for consistency s/PKCS11_CKK_EDDSA/PKCS11_CKK_EC_EDDSA/g)

(indentation)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discard this comment. This naming is driven by the PKCS#11 specs.
Sorry for the noise.

case PKCS11_CKM_EC_EDWARDS_KEY_PAIR_GEN:
if ((get_class(temp) != PKCS11_CKO_PUBLIC_KEY &&
get_class(temp) != PKCS11_CKO_PRIVATE_KEY) ||
get_key_type(temp) != PKCS11_CKK_EC_EDWARDS) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation:

		if ((get_class(temp) != PKCS11_CKO_PUBLIC_KEY &&
		     get_class(temp) != PKCS11_CKO_PRIVATE_KEY) ||
		    get_key_type(temp) != PKCS11_CKK_EC_EDWARDS) {
			rc = PKCS11_CKR_TEMPLATE_INCONSISTENT;
			goto out;
		}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

if (key_type != PKCS11_CKK_EC_EDWARDS) {
EMSG("Invalid key %s for mechanism %s",
id2str_type(key_type, key_class),
id2str_proc(proc_id));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

 			EMSG("Invalid key %s for mechanism %s",
-				id2str_type(key_type, key_class),
-				id2str_proc(proc_id));
+			     id2str_type(key_type, key_class),
+			     id2str_proc(proc_id));

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

if (key_class != PKCS11_CKO_PUBLIC_KEY &&
key_class != PKCS11_CKO_PRIVATE_KEY) {
EMSG("Invalid key class for mechanism %s",
id2str_proc(proc_id));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

		EMSG("Invalid key class for mechanism %s",
		     id2str_proc(proc_id));

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

return rc;

rc = tee2pkcs_add_attribute(pub_head, PKCS11_CKA_EC_POINT,
tee_obj, TEE_ATTR_ED25519_PUBLIC_VALUE);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indent for the 3 above

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

if (rc)
return rc;

out:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-	out:
+out:

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed


proc->extra_ctx = TEE_Malloc(sizeof(struct eddsa_processing_ctx) +
ctx_len,
TEE_USER_MEM_HINT_NO_FILL_ZERO);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

 	proc->extra_ctx = TEE_Malloc(sizeof(struct eddsa_processing_ctx) +
-								 ctx_len,
-								 TEE_USER_MEM_HINT_NO_FILL_ZERO);
+				      ctx_len, TEE_USER_MEM_HINT_NO_FILL_ZERO);

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed indentation,
Is it Ok?
sizeof(*ctx) + ctx_len,

@@ -390,9 +395,15 @@ void pkcs11_mechanism_supported_key_sizes(uint32_t proc_id,
case PKCS11_CKM_ECDSA_SHA384:
case PKCS11_CKM_ECDSA_SHA512:
case PKCS11_CKM_ECDH1_DERIVE:
case PKCS11_CKFM_GENERATE_KEY_PAIR:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

discard this change

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, thanks for pointing out

struct pkcs11_attribute_head *proc_params)
{
enum pkcs11_rc rc = PKCS11_CKR_GENERAL_ERROR;
uint32_t flag = 0, ctx_len = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 variable definition per line

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Copy link
Contributor

@etienne-lms etienne-lms left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

few remaining comments on the pkcs11 TA part.

@@ -1250,6 +1252,8 @@ enum pkcs11_mechanism_id {
PKCS11_CKM_ECDSA_SHA384 = 0x01045,
PKCS11_CKM_ECDSA_SHA512 = 0x01046,
PKCS11_CKM_ECDH1_DERIVE = 0x01050,
PKCS11_CKM_EC_EDWARDS_KEY_PAIR_GEN = 0x01055,
PKCS11_CKM_EDDSA = 0x01057,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discard this comment. This naming is driven by the PKCS#11 specs.
Sorry for the noise.

return PKCS11_CKR_ARGUMENTS_BAD;

proc->extra_ctx = TEE_Malloc(sizeof(*ctx) + ctx_len,
TEE_USER_MEM_HINT_NO_FILL_ZERO);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation

out_buf, &out_size);
tee_attrs,
tee_attrs_count,
hash_buf, hash_size,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

discard this change. The original code was good:

			res = TEE_AsymmetricSignDigest(proc->tee_op_handle,
						       tee_attrs,
						       tee_attrs_count,
						       in_buf, in_size,
						       out_buf, &out_size);

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@jforissier
Copy link
Contributor

@varder when you address Etienne's comments please also rebase onto master which now has the "proper" Ed25519 implementation, thanks!

tee_attrs,
tee_attrs_count,
in_buf, in_size,
out_buf, &out_size);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

preserve original indentation :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

rc = tee2pkcs_add_attribute(priv_head, PKCS11_CKA_VALUE,
tee_obj, TEE_ATTR_ED25519_PRIVATE_VALUE);
if (rc)
return rc;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

goto out;
ditto lines below

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@@ -393,6 +400,11 @@ void pkcs11_mechanism_supported_key_sizes(uint32_t proc_id,
*min_key_size = 160; /* in bits */
*max_key_size = 521; /* in bits */
break;
case PKCS11_CKM_EC_EDWARDS_KEY_PAIR_GEN:
case PKCS11_CKM_EDDSA:
*min_key_size = 256; /* in bits */
Copy link
Contributor

@etienne-lms etienne-lms Oct 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spec mentions 255 while key effective bit size is 256.
Maybe a word here? /* in bits, v3.1 falsly states 255 */

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to rfc8032
https://www.rfc-editor.org/rfc/rfc8032#section-5.1.5
Key Generation
The private key is 32 octets (256 bits, corresponding to b) of
cryptographically secure random data.

I guess it is a typo in Specs,


static const uint8_t ed25519ph_oid_der[] = {
0x13, 0x0C, 'e', 'd', 'w', 'a', 'r', 'd', 's',
'2', '5', '5', '1', '9',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing 'p', 'h' ?

@@ -41,6 +42,24 @@ static const uint8_t secp521r1_name_der[] = {
0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x23,
};

static const uint8_t ed25519_name_der[] = {
0x06, 0x03, 0x2B, 0x65, 0x70,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems 0x06, 0x03, 0x2B, 0x65, 0x70 is the encoding of EdDSA algo while the curve OID's DER encoding is 06 09 2B 06 01 04 01 DA 47 0F 01.
See script ta/pkcs11/scripts/dump_ec_curve_params.sh used to get IDs used by openssl, maybe it can help.

Copy link
Author

@varder varder Oct 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the existing algos the struct values
struct supported_ecc_curve
can be generated using dump_ec_curve_params.sh
with the commands:
openssl ecparam -name ${EC_CURVE} -param_enc explicit
But it looks like for Ed25519 with Openssl impossible to get the parameters.
I would like to generate it automatically, but i could't find the way.

I have looked at some existing implementation,
and it looks like, 1.3.101.112 is used in this context
https://github.com/opendnssec/SoftHSMv2/blob/develop/src/lib/crypto/BotanEDPrivateKey.cpp#L54
https://github.com/OpenSC/OpenSC/blob/master/src/libopensc/pkcs15-algo.c#L451

@varder varder force-pushed the pkcs11_ed25519 branch 3 times, most recently from 824cea6 to c2cbbbe Compare October 3, 2022 18:50
@varder
Copy link
Author

varder commented Oct 4, 2022

@etienne-lms
Can I squash the fixups?

Copy link
Contributor

@etienne-lms etienne-lms left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed-by: Etienne Carriere <[email protected]> (minor inline comment style issue)

@@ -244,6 +245,21 @@ static const uint8_t secp521r1_oid_der[] = {
0x91, 0x38, 0x64, 0x09, 0x02, 0x01, 0x01,
};

/* Edwards curves may be specified in two flavours:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/*
 * Edwards curves may be specified in two flavours: 

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@etienne-lms
Copy link
Contributor

@etienne-lms
Can I squash the fixups?

Ok

Add functionality to generate, import keys, sign/verify for
ED25519, ED25519ctx and ED25519ph.

The values for the object identifies originates from:
https://www.rfc-editor.org/rfc/rfc8420.html
A.1. ASN.1 Object for Ed25519

The PKCS#11 Specification:
https://docs.oasis-open.org/pkcs11/pkcs11-spec/v3.1/cs01/
pkcs11-spec-v3.1-cs01.pdf

Signed-off-by: Valerii Chubar <[email protected]>
Reviewed-by: Etienne Carriere <[email protected]>
@jforissier jforissier merged commit 03e0743 into OP-TEE:master Oct 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants