Skip to content

Add RAND_DRBG_bytes - #4752

Closed
kroeckx wants to merge 3 commits into
openssl:masterfrom
kroeckx:rand_drbg_bytes
Closed

kroeckx wants to merge 3 commits into
openssl:masterfrom
kroeckx:rand_drbg_bytes

Conversation

@kroeckx

@kroeckx kroeckx commented Nov 17, 2017

Copy link
Copy Markdown
Member

No description provided.

@mspncp mspncp left a comment

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.

Looks fine to me on first view in principle, except for one issue: I don't think it's a good idea to allow a NULL drbg pointer and fallback to using RAND_bytes(). See my annotations.

Comment thread crypto/rand/drbg_lib.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.

I don't like this automagical fallback to RAND_bytes(). The default RAND_METHOD (RAND_OpenSSL()) is implemented on top of the RAND_DRBG api, so RAND_DRBG's implementation should not refer back to RAND_METHODs.

Also, no other RAND_DRBG_xxx() api call accepts a NULL pointer as drbg. It's the caller's duty to choose properly between RAND_bytes() and RAND_DRBG_bytes(), like in ssl_randbytes.

Comment thread ssl/ssl_lib.c Outdated

@mspncp mspncp Nov 17, 2017 •

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.

See my previous comment:

    if (s->drbg != NULL) {
        ...
        return RAND_DRBG_bytes(s->drbg, rnd, size);
    }
    if (size > INT_MAX)
        return 0;
    return RAND_bytes(rnd, (int)size);

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.

The first version of the code snippet was buggy, so I edited it.

@mspncp

mspncp commented Nov 17, 2017 •

Copy link
Copy Markdown
Contributor

But I do like that you add additional data automatically.

@kroeckx

kroeckx commented Nov 17, 2017

Copy link
Copy Markdown
Member Author

I've pushed a new version

Comment thread crypto/rand/drbg_lib.c Outdated

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I'm wondering if OPENSSL_secure_free() would be acceptable for this case.

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.

Does it make a great difference in performance? If not, I would leave it as it is.

Also, if performance is an issue, then I would be more worried about the performance impact of calling rand_drbg_get_additional_data() on every call.

@kroeckx
kroeckx force-pushed the rand_drbg_bytes branch 3 times, most recently from d9a5dfd to 8262261 Compare November 19, 2017 15:18
@kroeckx

kroeckx commented Nov 19, 2017

Copy link
Copy Markdown
Member Author

I've pushed a new version

@kroeckx kroeckx changed the title WIP: Add RAND_DRBG_bytes Add RAND_DRBG_bytes Nov 19, 2017
@mspncp

mspncp commented Nov 19, 2017

Copy link
Copy Markdown
Contributor

What has changed w.r.t the version from two days ago?

@kroeckx

kroeckx commented Nov 19, 2017

Copy link
Copy Markdown
Member Author

Only the rand_drbg_get_additional_data() function, it should do something useful on at least unix and windows.

@kroeckx

kroeckx commented Nov 19, 2017

Copy link
Copy Markdown
Member Author

The travis failure with --strict-warnings is something I'm trying to deal with in #4757

Comment thread crypto/rand/rand_lib.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.

Since we're generating new bytes, RDRAND might be appropriate even though it is feeding into the DRBG.

The other quick data source that comes to mind are the clock_gettime(2) timers:

#if _POSIX_C_SOURCE >= 199309L
    struct timespec ts;
    clockid_t cid;

# ifdef CLOCK_BOOTTIME
    cid = CLOCK_BOOTTIME;
# elif defined CLOCK_MONOTONIC
    cid = CLOCK_MONOTONIC;
# else
    cid = CLOCK_REALTIME;
# endif

    if (clock_gettime(cid, &ts) == 0)
       ...
#endif

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I've actually had clock_gettime(CLOCK_MONOTONIC) in a previous version of the patch but I've removed it in favor of just reading the TSC ourselves.

But clock_gettime(CLOCK_REALTIME) might be a useful replacement for gettimeofday(), it has a resolution ns instead of μs.

@kroeckx

kroeckx commented Nov 21, 2017

Copy link
Copy Markdown
Member Author

I've pushed a new commit

@mspncp mspncp left a comment •

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.

Since my change requests were addressed, here is my formal approval. It goes with the caveat that I did not look too deep into rand_drbg_get_additional_data(), because I don't have the expertise to weigh entropy vs. performance.

@kroeckx kroeckx added the approval: review pending This pull request needs review by a committer label Nov 25, 2017
@paulidale

Copy link
Copy Markdown
Contributor

I'll try to have a decent look at this tomorrow.

@paulidale paulidale left a comment

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.

One typo in a comment which is insignificant.

Comment thread crypto/rand/drbg_lib.c

/*
* Generates |outlen| random bytes and stores them in |out|. It will
* using the given |drbg| to generate the bytes.

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.

use instead of using.

@mattcaswell mattcaswell 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 Dec 8, 2017
@mattcaswell

Copy link
Copy Markdown
Member

Ping @kroeckx. Looks like this has the necessary approvals to be pushed?

@mattcaswell mattcaswell added this to the Post 1.1.1 milestone Jan 24, 2018
@paulidale

Copy link
Copy Markdown
Contributor

Conflicts?

@mspncp

mspncp commented Jan 28, 2018

Copy link
Copy Markdown
Contributor

@kroeckx could you please rebase to resolve the conflicts?

@mattcaswell this PR is ready except for the conflicts. Is it ok to move it from the 'Post 1.1.1' to the '1.1.1.' milestone?

@mattcaswell

Copy link
Copy Markdown
Member

this PR is ready except for the conflicts. Is it ok to move it from the 'Post 1.1.1' to the '1.1.1.' milestone?

@mspncp by all means merge it if it is ready. The milestone should not be seen as a blocker for that. The milestone is more about stuff which must be merged before 1.1.1.

levitte pushed a commit that referenced this pull request Jan 29, 2018
Reviewed-by: Paul Dale <[email protected]>
Reviewed-by: Matthias St. Pierre <[email protected]>
(Merged from #4752)
@mspncp

mspncp commented Jan 29, 2018

Copy link
Copy Markdown
Contributor

Squashed and rebased, resolving the trivial conflicts. Closing, thanks!

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.

4 participants