Repository navigation
Add support for X448 and Ed448 - #5419
mattcaswell wants to merge 18 commits into
Conversation
There was a problem hiding this comment.
I wonder if making pkey.ptr a union of the possible keytype pointers makes the code cleaner?
There was a problem hiding this comment.
Yes, good idea. Actually union members for RSA, DSA and EC keys already exist there, so this makes a lot of sense. Done.
There was a problem hiding this comment.
can you make those hex values?
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
I think you should remove this blank line; the two statements are closely related.
There was a problem hiding this comment.
This got moved around a bit anyway as part of the pkey.ptr change.
There was a problem hiding this comment.
shouldn't we compare key type first?
And I think the "==0" is more clear than "!"
There was a problem hiding this comment.
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"
There was a problem hiding this comment.
Remove this blank line, same reason as above.
There was a problem hiding this comment.
As above, this got moved around a bit anyway as part of the pkey.ptr change.
There was a problem hiding this comment.
I think you should put the comment before the two assignments. Or put a blank line before the comment.
There was a problem hiding this comment.
Put a comma or colon after the word Note
There was a problem hiding this comment.
"When cmd is EVP_PKEY..." Not the semi-math expression.
There was a problem hiding this comment.
Maybe 448/25519 should be in a separate file?
There was a problem hiding this comment.
Hmmm...not sure about that. It seems to make sense to keep it together with all of the other EVP_PKEY tests?
|
Updated commits pushed addressing comments so far. |
|
@richsalz - do you have any more comments on this PR? Ideally I'd like to get this into next week's alpha. |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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".
There was a problem hiding this comment.
Could you please expand that? What compatibility reasons?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Any reason why you prefer a define instead of a static (ossl_inline) function?
There was a problem hiding this comment.
No, not really. Probably an inline function would be more readable, so I made that change.
There was a problem hiding this comment.
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.)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
The RETURNS sections needs to be updated.
There was a problem hiding this comment.
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.
Improves readability.
EVP_PKEY_new_mac_key() can take a key length of -1 for HMAC.
06f9703 to
8f7966c
Compare
|
New commits pushed addressing the latest comments. I also rebased due to conflicts with master. |
|
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... |
|
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. |
kaduk
left a comment
There was a problem hiding this comment.
Just a quick skim; not a "real" review.
| if (pk->pkey.x448 == NULL) | ||
| return; | ||
| *priv = pk->pkey.x448->privkey; | ||
| } else { |
There was a problem hiding this comment.
I'm not sure how comfortable I am with an unqualified 'else' here (and similarly, later).
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)...
|
I will try to review it today or tomorrow.
|
|
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. |
|
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; |
There was a problem hiding this comment.
Please consider making an ECX_KEY where you allocate both members instead.
There was a problem hiding this comment.
Good idea. I have no idea why I didn't do this to start with. It simplifies the code considerably.
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: This is the spec that we were already using for X25519/Ed25519, and I've just extended this to include X448/Ed448. |
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). |
|
I'm closing this in favour of #5481 |
This adds EVP layer support for X448 and Ed448. TLS support will come later.
Checklist