Skip to content

Add support for X448 and Ed448 - #5419

Closed
mattcaswell wants to merge 18 commits into
openssl:masterfrom
mattcaswell:curve448-evp
Closed

mattcaswell wants to merge 18 commits into
openssl:masterfrom
mattcaswell:curve448-evp

Conversation

@mattcaswell

Copy link
Copy Markdown
Member

This adds EVP layer support for X448 and Ed448. TLS support will come later.

Checklist
  • documentation is added or updated
  • tests are added or updated

Comment thread crypto/ec/ecx_meth.c Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I wonder if making pkey.ptr a union of the possible keytype pointers makes the code cleaner?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, good idea. Actually union members for RSA, DSA and EC keys already exist there, so this makes a lot of sense. Done.

Comment thread crypto/ec/ecx_meth.c Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

can you make those hex values?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I could do, but they appear as decimal in the RFC so I think this is probably easier to understand? Looking at this again though made me realise we need to do something similar for X448 (done).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

okey then.

Comment thread crypto/ec/ecx_meth.c Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think you should remove this blank line; the two statements are closely related.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This got moved around a bit anyway as part of the pkey.ptr change.

Comment thread crypto/ec/ecx_meth.c Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

shouldn't we compare key type first?
And I think the "==0" is more clear than "!"

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We are guaranteed to have keys of the same type here. That check is done for us in EVP_PKEY_cmp. I changed it to "== 0"

Comment thread crypto/ec/ecx_meth.c Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Remove this blank line, same reason as above.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

As above, this got moved around a bit anyway as part of the pkey.ptr change.

Comment thread crypto/ec/ecx_meth.c Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think you should put the comment before the two assignments. Or put a blank line before the comment.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done.

Comment thread crypto/ec/ecx_meth.c Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Explicit NULL test.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed.

Comment thread crypto/evp/pmeth_gn.c Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Put a comma or colon after the word Note

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done

Comment thread doc/man3/EVP_PKEY_CTX_ctrl.pod Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

"When cmd is EVP_PKEY..." Not the semi-math expression.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maybe 448/25519 should be in a separate file?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Hmmm...not sure about that. It seems to make sense to keep it together with all of the other EVP_PKEY tests?

@mattcaswell

Copy link
Copy Markdown
Member Author

Updated commits pushed addressing comments so far.

@mattcaswell

Copy link
Copy Markdown
Member Author

@richsalz - do you have any more comments on this PR? Ideally I'd like to get this into next week's alpha.

Comment thread crypto/evp/pmeth_gn.c Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

keylen is already an int

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Good catch. Fixed.

Comment thread crypto/evp/pmeth_gn.c Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It's currently not clear to me why this is called. If we pass it a key, why do we generate a key? Shouldn't the operation be either KEY_OP_PUBLIC or KEY_OP_PRIVATE?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

So it seems you turned the KEY_OP_KEYGEN parameter in something that doesn't generate a key anymore if a public or private key is already set and acts like the function was called with KEY_OP_PUBLIC or KEY_OP_PRIVATE instead. This seems to be rather unexpected behavior change of EVP_PKEY_keygen() to me and might break existing applications.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

A couple of important points here: Firstly I'm not breaking anything or changing any behaviour here. This is existing behaviour. This is exactly the way the existing "new mac key" stuff does things, and that is implemented across multiple algorithms (including in 1.1.0) so we can't really change this. All I have done here is created a synonym for creating "new private keys" because all the algorithms we have done this for so far are MACs - this is the first one where that isn't the case: but there's nothing special or specific about MACs - its just about setting raw private key data. I have extended the concept a little to bring in public keys too, but the underlying concept is the same.

Secondly, key generation typically consists of two steps: setting up the parameters for the key, and setting the private/public key data. Usually the key data is randomly generated but in the old (existing) "new mac key" way of doing things key generation effectively reduces down to setting up the parameters, and then setting the key data to what has been supplied. I'm just reusing that approach here. In a sense "keygen" is a bit of a misnomer - think of it more as "preparing a key for use".

Comment thread crypto/evp/pmeth_gn.c Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could you please expand that? What compatibility reasons?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Basically the old EVP_PKEY_new_mac_key() has this weird behaviour where, for HMAC only, if you set a keylen of -1 then it assumes that the (binary!) key is NUL terminated! We use that capability in the dgst app. In refreshing my memory on why this was the case I realised I went too far in the dgst app renaming EVP_PKEY_new_mac_key() to EVP_PKEY_new_priv_key(). It should stay as EVP_PKEY_new_mac_key() because of this oddity (the new functions take a size_t for the length so this isn't possible with them). I expanded out the comment to explain this better, and fixed the dgst app.

Comment thread crypto/ec/ecx_meth.c Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Any reason why you prefer a define instead of a static (ossl_inline) function?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

No, not really. Probably an inline function would be more readable, so I made that change.

Comment thread doc/man3/EVP_PKEY_CTX_ctrl.pod Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If you set the private (or public) key you're really not generating a (new) key, so that sentence doesn't make sense to me.

It's also not clear to me why we need this as a public function.

("when during" doesn't make sense.)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I reworded this to make this clearer (and to try and explain what is meant by key generation better). Note: these aren't functions but macros. EVP_PKEY_CTX_set_mac_key() is already a public macro and EVP_PKEY_set_priv_key() is just a synonym for it (because it now covers more than just mac keys). The underlying ctrls have to be public. I could make the EVP_PKEY_set_priv_key() and EVP_PKEY_set_pub_key() macros private, but given that the ctrls are public it seems weird not too.

Comment thread doc/man3/EVP_PKEY_new.pod Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The RETURNS sections needs to be updated.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed. Also fixed HISTORY while I was at it.

ecx_meth.c is specific to the 25519 curve. This commit makes it more generic
so it can handle the 448 curve too.
There's nothing special about being able to set the MAC key. It's just a
private key so the same ctrl can be used for other things other than MAC
algorithms. We also add the ability to set public keys directly.
This enables setting the private and public key directly for X25519,
X448, Ed25519 and Ed448
Previously private and public keys had to be pem encoded to be read by
evp_test. This enables us to embed the raw private/public key values
in the test file. The algorithm has to support EVP_PKEY_new_priv_key()
and EVP_PKEY_new_pub_key() for this to work.
This adds the Ed448 test vectors from RFC8032 and the X448 test vectors
from RFC7748.
EVP_PKEY_new_mac_key() can take a key length of -1 for HMAC.
@mattcaswell

Copy link
Copy Markdown
Member Author

New commits pushed addressing the latest comments. I also rebased due to conflicts with master.

@mattcaswell

Copy link
Copy Markdown
Member Author

In developing the related TLS PR I found a few bugs in this. I've added a couple of commits to address those bugs. I also spotted some documentation which need updating so I've added a commit for that.

Still looking for an approval on this...

@kroeckx

kroeckx commented Feb 27, 2018

Copy link
Copy Markdown
Member

Can you explain why ecx_key_op() also takes a for instance a KEY_OP_PRIVATE, and when that should be used and when KEY_OP_KEYGEN?

@mattcaswell

Copy link
Copy Markdown
Member Author

Can you explain why ecx_key_op() also takes a for instance a KEY_OP_PRIVATE, and when that should be used and when KEY_OP_KEYGEN?

KEY_OP_PRIVATE is used when we are filling in an EVP_PKEY * during ASN.1 decoding. It performs additional checks such as ensuring that there no X509 algorithm parameters associated with the key (which would be an error). Note: all of this is existing functionality which I haven't changed except to extend it to work with more than just 25519.

@richsalz richsalz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm okay with this; not sure if @kroeckx had comments.

@kaduk kaduk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Just a quick skim; not a "real" review.

Comment thread crypto/ec/ecx_meth.c
if (pk->pkey.x448 == NULL)
return;
*priv = pk->pkey.x448->privkey;
} else {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm not sure how comfortable I am with an unqualified 'else' here (and similarly, later).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It really shouldn't be anything other than ED448 here. I could put an assert in if it makes you feel more comfortable? But I'm not sure whether it is worth it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I mean, yes, this is all internal and I trust you do have gotten things right.
And if we tried switching to a switch statement, we'd get our choice of compilers warning us for no 'default' label or for an unreachable default label (if we use an enum and use all values of it)...

@mattcaswell

Copy link
Copy Markdown
Member Author

@kroeckx, do you have any more comments on this, or can I take @richsalz's approval?

@kroeckx

kroeckx commented Feb 27, 2018 via email

Copy link
Copy Markdown
Member

@kroeckx

kroeckx commented Feb 27, 2018

Copy link
Copy Markdown
Member

I have a little bit of a problem with arguments that say this is how it's already the case, I just do the same. I also don't see the old code do that in the ecx_key_op(). It really either copies the private or public key from the parameter it's called with or generated a new random key. In the new code I also really don't see the difference if it gets called with KEY_OP_PUBLIC, palg set to NULL and having the key in p / plen, instead of storing it in some other place and then calling KEY_OP_KEYGEN. It only seems to complicate and duplicate code.

What I do see is that the old EVP_PKEY_new_mac_key() did call EVP_PKEY_CTX_set_mac_key() to store the key at some temporary place and then call EVP_PKEY_keygen() to copy it again, just like you do now, and I'm not sure it's a good example.

So the only thing I can really say is that things are inconsistent. And the only reason I can see why those 2 new functions where added is for the test suite.

@kroeckx

kroeckx commented Feb 28, 2018

Copy link
Copy Markdown
Member

Also, please don't invent a new file format for storing private and public keys.

struct ec_key_st *ec; /* ECC */
CURVE25519_KEY *curve25519;
X448_KEY *x448;
ED448_KEY *ed448;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please consider making an ECX_KEY where you allocate both members instead.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Good idea. I have no idea why I didn't do this to start with. It simplifies the code considerably.

@mattcaswell

Copy link
Copy Markdown
Member Author

Also, please don't invent a new file format for storing private and public keys.

I'm not sure I understand this? I've not invented any file format in this PR. I assume you are referring to the specifics of how X448/Ed448 are encoded/decoded for PKCS8 private keys, or into a SubjectPublicKeyInfo structure for public keys. Both of these things are defined here:

https://tools.ietf.org/html/draft-ietf-curdle-pkix-07

This is the spec that we were already using for X25519/Ed25519, and I've just extended this to include X448/Ed448.

@mattcaswell

Copy link
Copy Markdown
Member Author

I have a little bit of a problem with arguments that say this is how it's already the case, I just do the same. I also don't see the old code do that in the ecx_key_op(). It really either copies the private or public key from the parameter it's called with or generated a new random key. In the new code I also really don't see the difference if it gets called with KEY_OP_PUBLIC, palg set to NULL and having the key in p / plen, instead of storing it in some other place and then calling KEY_OP_KEYGEN. It only seems to complicate and duplicate code.

What I do see is that the old EVP_PKEY_new_mac_key() did call EVP_PKEY_CTX_set_mac_key() to store the key at some temporary place and then call EVP_PKEY_keygen() to copy it again, just like you do now, and I'm not sure it's a good example.

So the only thing I can really say is that things are inconsistent. And the only reason I can see why those 2 new functions where added is for the test suite.

I strongly believe the 2 new functions are required to make the EVP API usable for X25519/Ed25519/X448/Ed448. I made precisely this argument recently in #5453 (which complains about the unusability of the current API). Basically if you want to use those algorithms for anything other than TLS then its incredibly hard without these functions. For example there is currently no way of using an arbitrary X25519 private key without reading it in from a PKCS8 structure. The only way of supplying an externally provided X25519 public key is either reading it from a SubjectPublicKeyInfo or using an undocumented TLS specific function (EVP_PKEY_set1_tls_encodedpoint).

However, it seems we have disagreement on the implementation of these functions. In the interests of moving this PR forward, I propose that I remove them from this PR. They are not needed for TLS usage, so that will not slow down the review of #5470. I will then raise a new PR to introduce these functions (possibly done slightly differently to take account of your comments).

@mattcaswell mattcaswell mentioned this pull request Feb 28, 2018
2 tasks done
@mattcaswell

Copy link
Copy Markdown
Member Author

I'm closing this in favour of #5481

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.

4 participants