Skip to content

WIP: Add scrypt as EVP_PKEY_METHOD - #4026

Closed
johndoe31415 wants to merge 3 commits into
openssl:masterfrom
johndoe31415:new_kdfs
Closed

johndoe31415 wants to merge 3 commits into
openssl:masterfrom
johndoe31415:new_kdfs

Conversation

@johndoe31415

@johndoe31415 johndoe31415 commented Jul 26, 2017 •

Copy link
Copy Markdown
Contributor

This adds the scrypt KDF as a EVP_PKEY_METHOD. It is onging work in progress; converted to a PR as of the request by @snhenson.

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

@johndoe31415

Copy link
Copy Markdown
Contributor Author

@snhenson Regarding your comment about the uint64_t parsing, is maybe sscanf with the SCNu64 macro defined in <inttypes.h> something we can use? Only came to my mind after-the-fact.

@snhenson

Copy link
Copy Markdown
Contributor

I don't know if there is a portable way to do things. Some of the obscure platforms of interest might not even have sscanf hence my question.

Comment thread include/openssl/kdf.h 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'd suggest macros for these too. So something like EVP_PKEY_CTX_set_scrypt_N etc. as we do with the TLS PRF. The uint64_t cases maybe a new wrapper function that takes a uint64_t parameter and passes it to the underlying ctrl as a pointer?

@richsalz

Copy link
Copy Markdown
Contributor

Just do the "obvious" *10+*p-'0' It's what was done for parse_uint64 in evp_test.c

@johndoe31415

Copy link
Copy Markdown
Contributor Author

@richsalz The parser in evp_test.c isn't entirely correct, however. It will successfully parse 18446744073709551616 as 0 up including 18446744073709551619 (which it parses as 3). For the test I figured this to be okay, but for code that's actively used I thought this might be unwanted. Therefore the own parser.

Comment thread crypto/objects/objects.txt 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.

Typo in comment which I thought I'd fixed. I meant add ": scrypt" at the end. That should keep SN_id_scrypt unchanged (which we should do for compatibility reasons) and add a new LN_id_scrypt.

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.

Hmmm, still not sure about this. Just tried it like this:

1 3 6 1 4 1 11591 4 11          : id-scrypt         : scrypt

But then the NID symbol name changes from NID_id_scrypt to NID_scrypt and I get

crypto/asn1/p5_scrypt.c: In function ‘pkcs5_scrypt_set’:
crypto/asn1/p5_scrypt.c:202:38: error: ‘NID_id_scrypt’ undeclared (first use in this function)
     keyfunc->algorithm = OBJ_nid2obj(NID_id_scrypt);

Is this okay, i.e., should I simply change all occurences of NID_id_scrcypt to NID_scrypt?

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.

Hmm... try this:

!Cname id-scrypt
1 3 6 1 4 1 11591 4 11      : id-scrypt : scrypt

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.

Tried this as well (see latest commit) -- still doesn't work unfortunately.

@johndoe31415

Copy link
Copy Markdown
Contributor Author

Rebased to master, because I'm unsure if the build failure is because of something I accidently messed up in objects.txt.

@snhenson snhenson mentioned this pull request Aug 2, 2017
1 task done
@snhenson

snhenson commented Aug 3, 2017

Copy link
Copy Markdown
Contributor

FYI now #4079 has been implemented using "scrypt" for the name in evp_test and pkeyutl should work.

@johndoe31415

Copy link
Copy Markdown
Contributor Author

Thank you, @snhenson -- I just rebased to be able to use your changes and will try it our right away!

@johndoe31415

Copy link
Copy Markdown
Contributor Author

Worked perfectly. Thank you again, @snhenson!

With the major roadblocks out the way, I figure adding PBKDF2 is quite straightforward. If there aren't any objections, I'd get to it then?

Maybe one more question about a corner case: The current way that the PKEY_METHOD scrypt implementation handles passphrases and salts, it isn't possible to specify a zero-length password or salt. Therefore the testcase for this (which was present in testing the scrypt PBE) is commented out for KDF-scrypt. It's an esoteric corner case, should I change the code so that it's possible to set zero-length salt/passphrase for the scrypt KDF as well?

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 it's important that we find a way to support this. The reason is that in future I'd like to make EVP_PKEY_METHOD the preferred interface for scrypt (and other PBE algorithms) so it must be at least as flexible as the underlying function. I can think of several ways to do that, one is to allocate the fields when set even if they're zero length so the != NULL check passes.

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.

Done for PBKDF2 right off the bat and for scrypt now as well with 305df7b.

@snhenson

snhenson commented Aug 3, 2017

Copy link
Copy Markdown
Contributor

This is looking closer to something which can be included. Couple of additions needed though: this needs to be documented. Also some of the commits can be squashed (and log messages edited) and the whole thing rebased against the current master.

@snhenson

snhenson commented Aug 3, 2017

Copy link
Copy Markdown
Contributor

I think PBKDF2 should be easier to handle: in some ways scrypt is the most difficult case.

@johndoe31415

Copy link
Copy Markdown
Contributor Author

Hi @snhenson, that's great news! I've already rebased to master, will write some documentation as well and then squash the whole scrypt odysee in one commit, the PBKDF2 as another.

The thing I'm not feeling entirely comfortable with right now is the macro setters, which don't have any tests. I'd like to write a small example program that at least tests one vector for each manually. This would ensure that stupid mistakes (copy/paste kind of stuff or some erroneous typo in one of the header files) gets caught right away. I'm writing this code anyways for the "EXAMPLE" section of the manpage, so I figure we could as well include it as a test if that's okay?

Thanks again so much for your support!

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

Suggest we have a common name for this without reference to the algorithm. For example EVP_PKEY_CTX_set1_pbe_pass() the expectation here is that all PBE KDFs will support it.

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.

Fixed in be01d24384. In your comment you wrote about pbe_pass(), but the highlighted code shows scrypt_salt(). Do you suggest that I rename the _salt() functions as well? For now, the patch only introduces pbe_pass().

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.

No I think the salt can stay as it is. The salt (or at least how it is encoded) can vary from one PBE algorithm to another.

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 don't think it's a good idea to use the above by default. It requires 1GB+ of memory and on some systems that could grind them to a halt or cause "make test" to fail with an out of memory error. It did that on my VM when I was first testing scrypt.

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.

Fixed in ee41de12af.

@snhenson

snhenson commented Aug 3, 2017

Copy link
Copy Markdown
Contributor

Can you exclude the PBKDF2 KDF from this PR? That can be a separate PR once this is merged.

@johndoe31415 johndoe31415 mentioned this pull request Aug 4, 2017
2 tasks done
@johndoe31415
johndoe31415 force-pushed the new_kdfs branch 2 times, most recently from 7df6382 to c883d25 Compare August 4, 2017 17:10
@johndoe31415

Copy link
Copy Markdown
Contributor Author

Rebased to master and squashed everything into three commits (the original contribution, the documentation and the new test file). Removed PBKDF2 entirely from this PR and extracted it into #4087.

@johndoe31415
johndoe31415 force-pushed the new_kdfs branch 2 times, most recently from ce75a4e to 2f49822 Compare August 5, 2017 08:58
Comment thread crypto/kdf/scrypt.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.

Coding style says blank line after declarations.

Comment thread crypto/kdf/scrypt.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.

Add blank line.

Comment thread crypto/kdf/scrypt.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.

Ditto

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'd suggest the documentation follows the style of other algorithms like RSA-PSS: that is located in man7 and named after the algorithm itself. There are some existing KDFs (like the TLS1 PRF) that don't yet follow this but they will be updated at some point.

@johndoe31415

Copy link
Copy Markdown
Contributor Author

Hi @snhenson, thanks for your review. I've addressed all of your comments in c0deff0.

Comment thread crypto/objects/obj_mac.num 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.

Is this left over from a previous version? On my system after making the changes to objects.txt and doing make update I get no changes in obj_mac.num

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.

Well spotted. I've reverted

    crypto/asn1/charmap.h \
    crypto/conf/conf_def.h \
    crypto/objects/obj_dat.h \
    crypto/objects/obj_mac.num \
    crypto/objects/obj_xref.h \
    crypto/objects/obj_xref.txt \
    include/openssl/obj_mac.h

and ran make update. Can confirm that this is a leftover from the previous runs that became obsolete. b7c9b69 fixes this.

@snhenson

snhenson commented Aug 7, 2017

Copy link
Copy Markdown
Contributor

Approved subject to squashing the last two fixes. Many thanks for your contribution!

@snhenson snhenson self-assigned this Aug 7, 2017
@snhenson snhenson added the approval: review pending This pull request needs review by a committer label Aug 7, 2017
Add an interface that allows accessing the scrypt KDF as a PKEY_METHOD.
This fixes openssl#4021 (at least for the scrypt portion of the issue).
Added the pkey_meth_kdf_test tests which test the PKEY_METHOD macros (at
the moment, of HKDF and scrypt).
Added manpage for the new scrypt EVP_PKEY_METHOD KDF interface.
@johndoe31415

johndoe31415 commented Aug 7, 2017 •

Copy link
Copy Markdown
Contributor Author

Hi @snhenson, that's awesome news! I've squashed/fixuped the last hiccups and rebased to master. Thanks again for your patience. Cheers, Johannes

@paulidale paulidale added approval: done This pull request has the required number of approvals and removed approval: review pending This pull request needs review by a committer labels Aug 8, 2017
@snhenson

snhenson commented Aug 8, 2017

Copy link
Copy Markdown
Contributor

Pushed now, thanks everyone.

@snhenson snhenson closed this Aug 8, 2017
levitte pushed a commit that referenced this pull request Aug 8, 2017
Add an interface that allows accessing the scrypt KDF as a PKEY_METHOD.
This fixes #4021 (at least for the scrypt portion of the issue).

Reviewed-by: Paul Dale <[email protected]>
Reviewed-by: Stephen Henson <[email protected]>
(Merged from #4026)
levitte pushed a commit that referenced this pull request Aug 8, 2017
Added the pkey_meth_kdf_test tests which test the PKEY_METHOD macros (at
the moment, of HKDF and scrypt).

Reviewed-by: Paul Dale <[email protected]>
Reviewed-by: Stephen Henson <[email protected]>
(Merged from #4026)
levitte pushed a commit that referenced this pull request Aug 8, 2017
Added manpage for the new scrypt EVP_PKEY_METHOD KDF interface.

Reviewed-by: Paul Dale <[email protected]>
Reviewed-by: Stephen Henson <[email protected]>
(Merged from #4026)
Comment thread crypto/evp/pmeth_lib.c
}

int EVP_PKEY_CTX_ctrl_uint64(EVP_PKEY_CTX *ctx, int keytype, int optype,
int cmd, uint64_t value)

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.

Formatting nit. There is corresponding misalignment in include/openssl/evp.h. It wouldn't be inappropriate to fix it in new request, @johndoe31415.

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.

Ah, dang! As requested, new PR: #4166 -- thanks for the review!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approval: done This pull request has the required number of approvals

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants