Repository navigation
Implement PBKDF2 EVP_PKEY_METHOD - #4087
johndoe31415 wants to merge 10 commits into
Conversation
9a592b8 to
e49959e
Compare
|
I believe this PR is now ready for review. It's rebased to master. Some minor fixes are "SquashMe" commits that I'd squash (together with any other review commits) into the two main ones just before it's ready for merging. |
paulidale
left a comment
There was a problem hiding this comment.
All bar one suggestion are tiny nits.
The one that isn't is making this feature build time configurable.
There was a problem hiding this comment.
It should probably be optional -- i.e. configurable. Guarded by OPENSSL_NO_PBKDF2 most likely.
There was a problem hiding this comment.
There isn't currently an option that disables PBKDF2 support: it is always present.
There was a problem hiding this comment.
I'll try to give it a shot and try to introduce "no-pbkdf2", if that's okay for you?
There was a problem hiding this comment.
@snhenson thanks for letting me know that.
Adding one seems reasonable -- more so since it is done :)
There was a problem hiding this comment.
drop the comma after the i.e.
|
A rebase is also required. |
ffed85f to
9f49d3e
Compare
|
Hi @paulidale, thanks for your review! I have fixed your nits and am looking into adding a "no-pbkdf2" configuration now. Rebased & squashed everything else. |
|
Ah, interesting point: no-pbkdf2 implies no-scrypt as well, because scrypt internally uses PBKDF2 for derivation. I'm not sure how to specify that depencency in "Configure" and I have zero perl experience... any pointers on how to proceed? Edit: Currently proceeding by replacing all "ifndef OPENSSL_NO_SCRYPT" by "if !defined(OPENSSL_NO_SCRYPT) && !defined(OPENSSL_NO_PBKDF2)" -- but am not sure if that's the solution we're looking for. |
|
So, I have produced a patch that makes PBKDF2 disableable. However, there's serveral issues remaining. First, I receive a warning during build: I've looked at mkdef.pl and see that PBKDF2 is mentioned nowhere -- but since I have no clue what it does or what implications changing @known_algorithms has, I've not touched it. Secondly, test fail now: Apparently, the EVP test is not designed to skip non-built-in tests (likely we're going to have the same issue with scrypt, I fear). |
|
You should probably put something like this into opensslconf.h.in |
eb43d24 to
41bf282
Compare
|
@richsalz, ah, yes of course! Great suggestion, just commited it. Squashed it as well so that the back-and-forth doesn't show up when viewing the single commit. |
02c7371 to
d3dd23f
Compare
|
Just made another change to the code: Previously, I would have let the PKCS#12 GOST MAC fail with the same error code that it would have failed if PBKDF2 derivation failed. This is nonsense and horrible for the user. Therefore, the new error code PKCS12_R_MISSING_PBKDF2 is introduced in crypto/pkcs12/p12_mutl.c for this case. |
|
No need to put the scrypt/pbkdf2 dependency in opensslconf.h.in -- Configure itself has logic for cascading dependencies. Look for |
|
Oh, and for mkdef.pl, known_algorithms is what you want -- it's just the list of things that can be enabled/disabled by configure and produce OPENSSL_NO_BLAH preprocessor symbols. |
|
@kaduk, thanks a lot for your hints, it worked perfectly. This also took care of the failed 01-symbol-presence test. I'm looking into 30-evp right now to find an elegant way of ignoring KDFs that we will know in advance aren't included in the build (and thus, will fail). |
look at test/evp_test.c and |
|
Hi @kaduk, ah, yes! Had just found it, it really was a easy copy/paste. Tests run fine now! I'll squash the whole no-pbkdf2 thing right away so it's easily reviewable. Thanks to you all again for your support and patience. You're all amazingly helpful, this is great! |
e28b423 to
04c394d
Compare
paulidale
left a comment
There was a problem hiding this comment.
Thanks for going above and beyond making this configurable.
|
Travis is failing. Looks like |
|
Thanks, @mattcaswell -- indeed it needed a make update. Just did it and will squash the patch later. |
There was a problem hiding this comment.
I'm wondering why we've made this disablable at all? In general I think we should be avoiding adding too many disablable options unless there is a good reason for it. In this case it looks like it adds additional complexity without a lot of gain. In any case, if we do decide to retain this, then INSTALL will need an update with details of the new option.
There was a problem hiding this comment.
I wondered too. Why would we need to disable this? It complicates matters and adds additional needs for #ifdefs for it. We didn't have an option to disable it in 1.1.0
There was a problem hiding this comment.
Hmmm, I do think it's a good idea to be able to strip down the code basis to the bare minimum (ultimately maybe one day have the ability to have a OpenSSL version that's stripped down so much it can conveniently run on very small embedded systems, maybe even in PSK-only DTLS or something along the lines). That said, it does increase complexity. Since it's a completely separate commit, it's your judgement if you want to take it mainline or not. I think there's good arguments to be made for either choice, so no hurt feelings :-)
There was a problem hiding this comment.
I suggested this as a possible improvement. I'm not attached to it.
There was a problem hiding this comment.
I think this commit in the PR should be dropped.
There was a problem hiding this comment.
Fixed in 09555b7f.
There was a problem hiding this comment.
This pod file doesn't seem appropriate for section 7 - it looks more like it should be section 3. We are using section 7 for a generic introduction to a topic with references off to section 3 man pages for documentation on specific functions. This page is documenting specific functions. (I think the same comment could be applied to the recently added scrypt.pod file).
There was a problem hiding this comment.
We had a discussion over where general algorithm descriptions should go. Should we hang them off a (possibly obscure) ctrl operation manual page or reference the algorithm name itself? IIRC the conclusion was the algorithm name was better and it should go in section 7.
There was a problem hiding this comment.
I'm struggling to see the difference between the content on this page, and the other pages in section 3. It certainly seems quite different to the rest of the section 7 pages. IMO this should be moved.
There was a problem hiding this comment.
Ah well I'm looking more at similarities between this and the man7 stuff. There we have descriptions of how to use specific algorithms such as Ed25519 and X25519. In the cases of those two though there is no associated ctrl operation so they can't go anywhere else.
In the case of scrypt and PBKDF2 there are some ctrls we can associate with it so they could go in manual pages which reference them.
That would also apply to the RSA-PSS manual page too, should that be moved?
Just trying to see how we can handle this all consistently.
There was a problem hiding this comment.
This all depends on what you actually want to document and how that documentation should be reached. man7 pages should be general descriptions on a topic, so a general description of PBKDF2 in OpenSSL would be fitting for man7. These would typically not have a SYNOPSIS section, or a very short one, mostly referring to the header associated with the stuff described (see for example bio.pod).
However, there's also the documentation of specific functions (or macros as the case may be), and they should rather be in man3, with a collection of the function names in the NAME section and a fleshed out SYNOPSIS section.
With that in mind, I agree with @mattcaswell regarding this page, it looks much more like a man3 page than a man7 one.
There was a problem hiding this comment.
It's possible this page should be split into two. I can absolutely understand that one would wish for man pbkdf2 to give a general description of PBKDF2! I would expect such a page to refer to the functions that can be use to control this aspect, like L<EVP_PKEY_CTX_set1_pbe_pass(3)>...
There was a problem hiding this comment.
I'm fine either way -- but I'm holding off on this until you've come to consensus, if that's okay? In any case if we move the PBKDF2 docs to man3, I'd also apply the same to the scrypt man page in a different PR (that's already on master as man7).
There was a problem hiding this comment.
This should list all the functions described in this page (see other section 3 pages for examples)
There was a problem hiding this comment.
Agree. Also, technically, one would want the documentation for the individual functions to be easily accessible, i.e. man EVP_PKEY_CTX_set1_pbe_pass should give us this page. For that to happen, that function name must be mentioned in the NAME section.
There was a problem hiding this comment.
"memory-hard"? I have no idea what that means...
There was a problem hiding this comment.
"Stronger Key Derivation via Sequential Memory-Hard Functions", Percival 2009:
Definition 1. A memory-hard algorithm on a Random Access Machine is an al-
gorithm which uses S(n) space and T (n) operations, where S(n) ∈ Ω T (n) 1−ǫ .A memory-hard algorithm is thus an algorithm which asymptotically uses almost
as many memory locations as it uses operations 5 ; it can also be thought of as an
algorithm which comes close to using the most memory possible for a given number
of operations, since by treating memory addresses as keys to a hash table it is trivial
to limit a Random Access Machine to an address space proportional to its running
time.
There was a problem hiding this comment.
This all depends on what you actually want to document and how that documentation should be reached. man7 pages should be general descriptions on a topic, so a general description of PBKDF2 in OpenSSL would be fitting for man7. These would typically not have a SYNOPSIS section, or a very short one, mostly referring to the header associated with the stuff described (see for example bio.pod).
However, there's also the documentation of specific functions (or macros as the case may be), and they should rather be in man3, with a collection of the function names in the NAME section and a fleshed out SYNOPSIS section.
With that in mind, I agree with @mattcaswell regarding this page, it looks much more like a man3 page than a man7 one.
There was a problem hiding this comment.
Agree. Also, technically, one would want the documentation for the individual functions to be easily accessible, i.e. man EVP_PKEY_CTX_set1_pbe_pass should give us this page. For that to happen, that function name must be mentioned in the NAME section.
There was a problem hiding this comment.
It's possible this page should be split into two. I can absolutely understand that one would wish for man pbkdf2 to give a general description of PBKDF2! I would expect such a page to refer to the functions that can be use to control this aspect, like L<EVP_PKEY_CTX_set1_pbe_pass(3)>...
There was a problem hiding this comment.
I would rather rename EVP_PKEY_CTRL_SCRYPT_SALT to EVP_PKEY_CTRL_SALT and reuse that.
There was a problem hiding this comment.
For similar reasons, I can see a future where other KDFs also have an iteration count parameter, so it might be appropriate to give a more generic name (i.e. remove _PBKDF2)
There was a problem hiding this comment.
This is addressed by 4ca36747. Thanks for the comment!
There was a problem hiding this comment.
Rename EVP_PKEY_CTX_set1_scrypt_salt to EVP_PKEY_CTX_set1_pbe_salt and have it use EVP_PKEY_CTRL_SALT, then remove this one.
There was a problem hiding this comment.
Hmmm -- but what about HKDF? HKDF already uses EVP_PKEY_CTRL_HKDF_SALT (EVP_PKEY_ALG_CTRL + 4).
In order to be consistent, shouldn't that then also use the generic CTRL_SALT? Because then we have a conflict: currently on master, both EVP_PKEY_CTRL_HKDF_SALT (+4) and EVP_PKEY_CTRL_SCRYPT_SALT (+9) are used. So consolidating the two might break something either way (although scrypt has only been in the code for a very short number of days, so not that dramatic probably). If you have an idea how you'd want this to look, I'd get cracking.
There was a problem hiding this comment.
Ah, hadn't noticed the HKDF salt control. Yes, I agree with you, that one should probably be generalised as well. Doing this should be fairly straight forward, because as you say, SCRYPT and PBKDF2 are new... So what I would suggest is to generalise the control macros that make sense to do this with (if you want to be really radical, you could generalise all the HKDF ones), and then create alias macros where needed... So I could imagine this:
# define EVP_PKEY_CTRL_MD (EVP_PKEY_ALG_CTRL + 3)
# define EVP_PKEY_CTRL_SALT (EVP_PKEY_ALG_CTRL + 4)
# define EVP_PKEY_CTRL_KEY (EVP_PKEY_ALG_CTRL + 5)
# define EVP_PKEY_CTRL_INFO (EVP_PKEY_ALG_CTRL + 6)
# define EVP_PKEY_CTRL_MODE (EVP_PKEY_ALG_CTRL + 7)
/* Backward compatibility macros */
# define EVP_PKEY_CTRL_HKDF_MD EVP_PKEY_CTRL_MD
# define EVP_PKEY_CTRL_HKDF_SALT EVP_PKEY_CTRL_SALT
# define EVP_PKEY_CTRL_HKDF_KEY EVP_PKEY_CTRL_KEY
# define EVP_PKEY_CTRL_HKDF_INFO EVP_PKEY_CTRL_INFO
# define EVP_PKEY_CTRL_HKDF_MODE EVP_PKEY_CTRL_MODEThere was a problem hiding this comment.
... and then, convert the diverse function macros accordingly, i.e. add macros like EVP_PKEY_CTX_set1_pbe_salt and make alias macros out of the old (like EVP_PKEY_CTX_set1_hkdf_salt).
There was a problem hiding this comment.
On the other hand, very KDF specific parameter controls, like EVP_PKEY_CTRL_SCRYPT_N, should probably stay the same. So in the end, it's a matter of judging if a parameter is very specific, or is common for more than one KDF.
There was a problem hiding this comment.
Oh, and adressing this in a separate PR seems like a very wise move. I'll make sure to have a look.
There was a problem hiding this comment.
To me it depends on the meaning you attaching to the ctrl. The existing EVP_PKEY_CTRL_MD is used for signatures and indicates the digest to sign/verify with. In fact the code in EVP_PKEY_CTX_ctrl_str with the "digest" string only works for signatures.
If we want to generalise this to mean "the digest associated with the operation" which isn't just signatures then that at least will need changing.
There was a problem hiding this comment.
I'd originally suggested that we use different ctrl values for "salt" because there is some difference in the way the salt is used and especially its encoding between algorithms. However the meaning of "random data to thwart dictionary attacks" applies to at least the PBE cases so I'm fine with those having the same value.
There was a problem hiding this comment.
At some point we'll need to classify some of these algorithms so they can't be used inappropriately. For example an attempt to use a KDF to verify signatures or a non-PBE KDF where a PBE is required. I'd regarded a PBE as a subset of a KDF which always accepts a password parameter.
In future I'd like to add ASN.1 support to PBEs so they can be handled entirely through EVP_PKEY instead of the various different APIs we currently have. That would tidy up things like PKCS#8 handling quite a bit.
|
Where are we with this one, @openssl? |
|
Oh wow, I completely dropped the ball on this. To be honest I kind of thought that it'd been already merged. Okay with you, @levitte, if I upgrade the suggested changes and rebase against master? |
|
Please do. |
This adds a EVP_PKEY_METHOD for the PBKDF2. Test cases have been copied from the PBE and all pass. Zero-length passwords and salts are supported (although when using a string to set them, only pass/salt work, but not hexpass/hexsalt with an empty string argument).
Added manpage for the PKEY API to PBKDF2 and linked to it from the scrypt man page.
Add the no-pbkdf2 configuration option that entirely disables PBKDF2. Note that this also implicitly disables scrypt and PKCS#12 GOST MAC. For the latter, a new error code is introduced (PKCS12_R_MISSING_PBKDF2). Use Configure-driven (no-pbkdf implies no-scrypt) dependency management, as suggested by @kaduk. Also include PBKDF2 in the mkdef.pl script. Fix EVP KDF and EVP PBE tests for both scrypt and PBKDF2 (skip tests if algorithms not compiled in instead of failing).
This reverts commit bbac07b. In discussion it was agreed that PBKDF2 is so essential that making it disableable only adds complexity that's unnecessary.
After revert of bbac07b, PBKDF2 is now not a disableable symbol anymore.
Moved MAN7 to MAN3 and started some algorithmic description of PBKDF2, as @levitte suggested.
In the discussion, there was some disagreement -- however, merging of key/salt seemed to have consensus. Not changing the others (yet?).
4ca3674 to
90af5ec
Compare
|
Alright, gave it another shot. I left the original structure intact and didn't do any squashing so it's clearly visible what I've changed. New patches start with d7e3230. First, I've reverted the possibility to disable PBKDF2, as requested by @mattcaswell. Then, I've taken the input of @levitte and @snhenson regarding merging the different PKEY #defines -- there was still no definite consensus (at least as I read it), but I believe that everyone wanted to merge salt/key values so that's how I implemented it. Lastly, I've given a rough draft of how a man7 PBKDF2 could look like and moved the current man7 to man3, as suggested by @levitte. If this is the direction you imagine the man7 to look like, please let me know and I'll polish and extended it. Otherwise I'd just drop it. Note that I've added some special handling for the previous scrypt-specific salt #define (scrypt has been in master for about half a year now and I don't think it's appropriate to ignore). Also note that tests 70-test_sslcbcpadding.t and 70-test_sslrecords.t are failing (but are failing on master as well, so I don't think I'm at fault). |
Regarding that, #5679 may be the chosen solution... |
|
This looks oh so much better in my view. @mattcaswell, do you have some thoughts on this? |
|
Is this really a PKEY method? Might a new EVP interface for KDFs make more sense? |
|
Unfortunately, some tests are failing. Please see the CIs... Also, this PR hasn't been marked with a milestone. I'm assuming post-1.1.1 at this point, does @openssl agree? |
|
Hi @levitte, I'll check it out -- did run the local tests but admittedly didn't check the CI. So I'll beef up the man7 in the way that I'd started out? Does this look good to everyone? |
|
@paulidale, re your question, this is no weirder than the other KDFs in |
It worries me a bit that the default configuration fails its tests in Travis ;-)
I'm not sure if we're still at the debate of whether it should be split up into a man3 part and a man7 part or not... The man7 file looked pretty much ok for a first read, but I'll have a second look. However, if it needs flashing out, please carry on! |
|
|
||
| =head1 NAME | ||
|
|
||
| pbkdf2 - PBKDF2 key derivation function |
There was a problem hiding this comment.
Instead of pbkdf2, you must enumerate the names of all the functions described in this document. In other words, something like this:
EVP_PKEY_CTX_set1_pbe_pass, EVP_PKEY_CTX_set1_pbkdf2_salt,
EVP_PKEY_CTX_set_pbkdf2_md, EVP_PKEY_CTX_set_pbkdf2_iter
- PBKDF2 key derivation functions
Also, this file needs to be renamed to one of the function names (please avoid EVP_PKEY_CTX_set1_pbe_pass.pod, though... that one is mentioned in more than one place and will probably show up elsewhere).
There was a problem hiding this comment.
(I just saw docs/man7/scrypt.pod and understand where you got your example from... I'll ahem take a closer look at it, it seems to need a split'n'rewrite)
| L<EVP_PKEY_CTX_new(3)>, | ||
| L<EVP_PKEY_CTX_ctrl_str(3)>, | ||
| L<EVP_PKEY_derive(3)>, | ||
| L<scrypt(7)> |
There was a problem hiding this comment.
Not sure why we'd mention scrypt(7) here...
| =head1 DESCRIPTION | ||
|
|
||
| The EVP_PKEY_PBKDF2 algorithm implements the PBKDF2 password based key | ||
| derivation function, as described in RFC 2898. It is an iterative HMAC-based |
There was a problem hiding this comment.
You could add the reference L<pbkdf2(7)> here too, or possibly instead of "RFC 2898"... you're choice exactly which you do.
There was a problem hiding this comment.
In second thought, since docs/man7/pbkdf2.pod mentions the RFC, I'm not sure it needs to be mentioned here. The CONFORMING TO section below should be enough.
| L<EVP_PKEY_CTX_new(3)>, | ||
| L<EVP_PKEY_CTX_ctrl_str(3)>, | ||
| L<EVP_PKEY_derive(3)> | ||
| L<pbkdf2(7)> |
Agreed. |
|
#6674 obsoletes this one I think. |
Indeed. Closing this. |
This adds a EVP_PKEY_METHOD for the PBKDF2. Test cases have been copied
from the PBE and all pass. Zero-length passwords and salts are
supported (although when using a string to set them, only pass/salt
work, but not hexpass/hexsalt with an empty string argument). Test case
in test/pkey_meth_kdf_test.c currently only commited in code, but unused
(will be later merged/squashed with the similar scrypt PR).
This has been cherry-picked from a commit off #4026 and split into two PRs, as a request from @snhenson.
Checklist