Repository navigation
Generalize PKEY KDF controls and setters - #4197
johndoe31415 wants to merge 4 commits into
Conversation
Instead of repeating similar code for each new KDF (e.g., EVP_PKEY_CTX_set1_hkdf_salt, EVP_PKEY_CTX_set1_scrypt_salt, ...), change all of those to the more generic EVP_PKEY_CTX_set_pbe_xyz variants. For TLS1-PRF and HKDF, introduce backwards-compatible enums and setters. Since scrypt is fairly new (and there's no official release with scrypt PKEY KDF yet), do not introduce backwards-compatible setters for it. For HKDF, add a test that uses only generic setter macros. As for the CTRL values, use the nomenclature EVP_PKEY_CTRL_KDF_* suggested by @levitte to avoid a conflict with the already present EVP_PKEY_CTRL_MD.
levitte
left a comment
There was a problem hiding this comment.
Additionally, I would like to see crypto/kdf/tls1_prf.c and crypto/kdf/hkdf.c updated to use the generic macros. The compatibility macros would end up only being retained for humans who use them directly.
|
|
||
| /* Aliases for backwards-compatibility */ | ||
| # define EVP_PKEY_CTRL_TLS_MD EVP_PKEY_CTRL_KDF_MD | ||
| # define EVP_PKEY_CTRL_HKDF_MD EVP_PKEY_CTRL_KDF_MD |
There was a problem hiding this comment.
You need to make sure that pkey_hkdf_ctrl in crypto/kdf/hkdf.c also understands (EVP_PKEY_ALG_CTRL + 3).
You could do that by defining EVP_PKEY_CTRL_HKDF_MD to be that value, and in that case, you'll need this patch:
diff --git a/crypto/kdf/hkdf.c b/crypto/kdf/hkdf.c
index 63c3523c27..058d498879 100644
--- a/crypto/kdf/hkdf.c
+++ b/crypto/kdf/hkdf.c
@@ -72,6 +72,7 @@ static int pkey_hkdf_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
switch (type) {
case EVP_PKEY_CTRL_HKDF_MD:
+ case EVP_PKEY_CTRL_KDF_MD:
if (p2 == NULL)
return 0;
OR you could define another macro, like this:
# define EVP_PKEY_CTRL_OLD_HKDF_MD (EVP_PKEY_ALG_CTRL + 3)and the following diff:
diff --git a/crypto/kdf/hkdf.c b/crypto/kdf/hkdf.c
index 63c3523c27..b6d4bfef57 100644
--- a/crypto/kdf/hkdf.c
+++ b/crypto/kdf/hkdf.c
@@ -71,7 +71,8 @@ static int pkey_hkdf_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
HKDF_PKEY_CTX *kctx = ctx->data;
switch (type) {
- case EVP_PKEY_CTRL_HKDF_MD:
+ case EVP_PKEY_CTRL_OLD_HKDF_MD:
+ case EVP_PKEY_CTRL_KDF_MD:
if (p2 == NULL)
return 0;
Either way, something like that is needed, or we have a backward compatibility problem.
There was a problem hiding this comment.
Since all other macros retain the same numbers, they are no worry.
| # define EVP_PKEY_CTRL_HKDF_MODE (EVP_PKEY_ALG_CTRL + 7) | ||
| # define EVP_PKEY_CTRL_PASS (EVP_PKEY_ALG_CTRL + 8) | ||
| # define EVP_PKEY_CTRL_SCRYPT_SALT (EVP_PKEY_ALG_CTRL + 9) | ||
| # define EVP_PKEY_CTRL_SCRYPT_N (EVP_PKEY_ALG_CTRL + 10) |
There was a problem hiding this comment.
Note that OpenSSL 1.1.0x hasn't defined anything above EVP_PKEY_ALG_CTRL + 6, so it's perfectly safe to renumber the SCRYPT numbers to be EVP_PKEY_ALG_CTRL + 8 and up.
Internally, use the generic CTRL_KDF_* values in the code for TLS1-PRF and HKDF instead of relying on the backwards-compatible defines. Add the now-missing (EVP_PKEY_ALG_CTRL + 3) value to retain ABI compatibility and make both CTRL_MD values valid for setting the message digest of HKDF. Renumber scrypt defines to have conscutive control numbers.
| if (strcmp(type, "md") == 0) | ||
| return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_DERIVE, | ||
| EVP_PKEY_CTRL_HKDF_MD, value); | ||
| EVP_PKEY_CTRL_PBE_MD, value); |
There was a problem hiding this comment.
It is :-/
Typical "oh, andj ust this one more occurrence, then commit" syndrome. Sorry about that.
There was a problem hiding this comment.
Heh, like you're alone doing that kind of mistake. It happens to all of us ;-)
| return 1; | ||
|
|
||
| case EVP_PKEY_CTRL_HKDF_MODE: | ||
| case EVP_PKEY_CTRL_KDF_MODE: |
There was a problem hiding this comment.
Is this a general KDF operation or something unique to HKDF? I suspect the latter. If so I think we should retain HKDF/hkdf in the ctrl name and the associated macro.
There was a problem hiding this comment.
Is it inconceivable that some other KDF might have a mode setting as well? If so, I don't see why we would need to make the tag specific to HKDF.
There was a problem hiding this comment.
Some other KDF might have the concept of a "mode" but the meaning is likely to be completely different and it might not take an int parameter. For HKDF there are the three permitted values EVP_PKEY_HKDEF_MODE_EXTRACT_AND_EXPAND, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY and EVP_PKEY_HKDEF_MODE_EXPAND_ONLY that is likely to be completely different if the KDF is not HKDF.
There was a problem hiding this comment.
Hmm, ok, with such specific values, I can see why you'd want this tag to be HKDF specific. Works for me.
There was a problem hiding this comment.
Thinking about it are those three typos? Should they be EVP_PKEY_HKDF_MODE_EXTRACT_AND_EXPAND etc?
There was a problem hiding this comment.
That's not what I meant... I only said that I understand your reasoning for wanting EVP_PKEY_CTRL_HKDF_MODE rather than EVP_PKEY_CTRL_KDF_MODE
There was a problem hiding this comment.
Should've been clearer, I meant that as a separate observation.
There was a problem hiding this comment.
Oh, ok! I thought they were only typos in your comment... maybe I should have known better ;-)
Now that I grepped, it does seem like they are typos in the core, yes
| EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_DERIVE, \ | ||
| EVP_PKEY_CTRL_HKDF_MODE, mode, NULL) | ||
| EVP_PKEY_CTRL_KDF_MODE, mode, NULL) | ||
|
|
There was a problem hiding this comment.
I'm not sure about the renaming of some of these macros. As I said I consider "pbe" to be a subset of "kdf" so a macro that may be valid for several KDF algorithms (which may also include PBE) would have "kdf" in its name. Something specific to PBE would have pbe in it. That would change EVP_PKEY_CTX_set1_pbe_salt to EVP_PKEY_CTX_set1_kdf_salt for example (salts can be set in PBE and none-PBE KDFs), to me it just wouldn't look right using the macro EVP_PKEY_CTX_set1_pbe_salt to set the salt for a KDF which is not a PBE algorithm.
There was a problem hiding this comment.
There is also inconsistency between the ctrl name and the macro. I'd name these so that anything that is likely to be used by multiple EVP_PKEY_METHODs (some of which may not be KDFs) would just be of the form EVP_PKEY_CTRL_FOO with the macro EVP_PKEY_CTX_set_foo(). If it could be used by multiple KDFs (which might include PBEs) then EVP_PKEY_CTRL_KDF_FOO with macro EVP_PKEY_CTX_set_kdf_foo(), If it is a PBE only then EVP_PKEY_CTRL_PBE_FOO and EVP_PKEY_CTX_set_pbe_foo(). Finally anything algorithm specific for algorithm "alg" becomes EVP_PKEY_CTRL_ALG_FOO and EVP_PKEY_CTX_set_alg_foo().
There was a problem hiding this comment.
So we are waiting for the submitter to update these names?
I will provisionally update labels accordingly.
|
Is this still relevant in light of #6674 ? |
Instead of repeating similar code for each new KDF (e.g.,
EVP_PKEY_CTX_set1_hkdf_salt, EVP_PKEY_CTX_set1_scrypt_salt, ...), change
all of those to the more generic EVP_PKEY_CTX_set_pbe_xyz variants. For
TLS1-PRF and HKDF, introduce backwards-compatible enums and setters.
Since scrypt is fairly new (and there's no official release with scrypt
PKEY KDF yet), do not introduce backwards-compatible setters for it. For
HKDF, add a test that uses only generic setter macros. As for the CTRL
values, use the nomenclature EVP_PKEY_CTRL_KDF_* suggested by @levitte
to avoid a conflict with the already present EVP_PKEY_CTRL_MD.
Since this PR touches the KDF setters in a non-trivial way, I think it'd be great if we could merge #4196 first so I'd then update this PR in order to test both the backwards-compatible TLS1-PRF setters as well as the generic ones which are newly introduced (just as with HKDF).
Checklist