Skip to content

Generalize PKEY KDF controls and setters - #4197

Closed
johndoe31415 wants to merge 4 commits into
openssl:masterfrom
johndoe31415:generic-kdf-ctrls
Closed

johndoe31415 wants to merge 4 commits into
openssl:masterfrom
johndoe31415:generic-kdf-ctrls

Conversation

@johndoe31415

@johndoe31415 johndoe31415 commented Aug 19, 2017 •

Copy link
Copy Markdown
Contributor

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
  • documentation is added or updated
  • tests are added or updated

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.
@johndoe31415 johndoe31415 mentioned this pull request Aug 19, 2017
2 tasks done

@levitte levitte left a comment

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.

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.

Comment thread include/openssl/kdf.h

/* 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

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.

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.

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.

Since all other macros retain the same numbers, they are no worry.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hi @levitte, thanks for the comprehensive review; I've just pushed ecd0eb6 which should address all of your issues. I've opted for option #2 (it makes it very clear to the user that they're not supposed to use it anymore).

Comment thread include/openssl/kdf.h Outdated
# 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)

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.

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.
Comment thread crypto/kdf/hkdf.c Outdated
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);

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.

This looks like a typo...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It is :-/

Typical "oh, andj ust this one more occurrence, then commit" syndrome. Sorry about that.

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.

Heh, like you're alone doing that kind of mistake. It happens to all of us ;-)

@levitte levitte left a comment

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.

Looks good to me

@levitte levitte added the approval: review pending This pull request needs review by a committer label Aug 19, 2017
Comment thread crypto/kdf/hkdf.c
return 1;

case EVP_PKEY_CTRL_HKDF_MODE:
case EVP_PKEY_CTRL_KDF_MODE:

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.

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.

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.

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.

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.

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.

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.

Hmm, ok, with such specific values, I can see why you'd want this tag to be HKDF specific. Works for me.

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.

Thinking about it are those three typos? Should they be EVP_PKEY_HKDF_MODE_EXTRACT_AND_EXPAND etc?

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.

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

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.

Should've been clearer, I meant that as a separate observation.

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.

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

Comment thread include/openssl/kdf.h
EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_DERIVE, \
EVP_PKEY_CTRL_HKDF_MODE, mode, NULL)
EVP_PKEY_CTRL_KDF_MODE, mode, NULL)

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 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.

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.

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().

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.

I agree.

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.

So we are waiting for the submitter to update these names?
I will provisionally update labels accordingly.

@kaduk kaduk removed the approval: review pending This pull request needs review by a committer label Sep 6, 2017
@mattcaswell mattcaswell added this to the Post 1.1.1 milestone Jan 24, 2018
@richsalz richsalz added the approval: review pending This pull request needs review by a committer label Apr 18, 2018
@paulidale

Copy link
Copy Markdown
Contributor

Is this still relevant in light of #6674 ?

@levitte

levitte commented Feb 13, 2019

Copy link
Copy Markdown
Member

Is this still relevant in light of #6674 ?

I do believe the concerns raised here were answered in #6674, so closing this. If there are new concerns with the resulting API, please raise a new issue

@levitte levitte closed this Feb 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approval: review pending This pull request needs review by a committer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants