Skip to content

Conversation

@sam1013
Copy link
Contributor

@sam1013 sam1013 commented Jan 24, 2018

No description provided.

if (BN_mod_inverse(r1, r2, rsa->e, ctx)) {
/* GCD == 1 since inverse exists */
break;
} else {
Copy link
Contributor

Choose a reason for hiding this comment

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

Style nit -- don't need an else after a break. Then unshift all the lines.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done.

break;
} else {
error = ERR_peek_last_error();
if (ERR_GET_LIB(error) == ERR_LIB_BN &&
Copy link
Contributor

Choose a reason for hiding this comment

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

Style nit. Put the && at the start of the line, and indent that second line an extra tabstop.

@mattcaswell mattcaswell added this to the 1.1.1 milestone Jan 25, 2018
Copy link
Member

@mattcaswell mattcaswell left a comment

Choose a reason for hiding this comment

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

This PR does not apply cleanly to 1.1.0 or 1.0.2. Could you create a PR for 1.1.0 too (and one for 1.0.2 if the 1.1.0 doesn't cherry-pick cleanly)?

if (ERR_GET_LIB(error) == ERR_LIB_BN
&& ERR_GET_REASON(error) == BN_R_NO_INVERSE) {
/* GCD != 1 */
ERR_clear_error();
Copy link
Member

Choose a reason for hiding this comment

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

I wonder whether ERR_set_mark() before the BN_mod_inverse() call, and ERR_pop_to_mark() here might be better?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As far as I understood, set/pop mark cannot be used to check for a specific error. In our case we want to distinguish BN_R_NO_INVERSE from other errors like out-of-memory. Former should cause a retry using a different prime number, staying inside the for-loop. Latter should abort to avoid endless loops.

Copy link
Member

Choose a reason for hiding this comment

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

They are not intended as a replacement for the ERR_peek_last_error() call. You still need to do that to check what the last error code was and therefor look at BN_R_NO_INVERSE. Rather they are a replacement for ERR_clear_error(). That function clears all errors on the error queue. By using ERR_set_mark() and ERR_pop_to_mark() you can clear only those errors added by the BN_mod_inverse() call.

if (!BN_gcd(r1, r2, rsa->e, ctx))
goto err;
if (BN_is_one(r1))
if (BN_mod_inverse(r1, r2, rsa->e, ctx)) {
Copy link
Member

Choose a reason for hiding this comment

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

Since this returns a pointer, can you add != NULL?

Copy link
Contributor Author

@sam1013 sam1013 Jan 25, 2018

Choose a reason for hiding this comment

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

done. The pointer checking is somehow inconsistent if you look at other usages of BN_mod_inverse inside an if condition.

@mattcaswell
Copy link
Member

See #5170 for 1.1.0/1.0.2 version of this.

@sam1013
Copy link
Contributor Author

sam1013 commented Jan 31, 2018

Hope it's fine not to remove the ERR mask in all cases.

@mattcaswell mattcaswell added the approval: review pending This pull request needs review by a committer label Jan 31, 2018
if (BN_copy(rsa->e, e_value) == NULL)
goto err;

BN_set_flags(rsa->e, BN_FLG_CONSTTIME);
Copy link
Contributor

Choose a reason for hiding this comment

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

rsa->e is a public value, so marking it as constant-time is probably undesirable. This means public RSA operations will use the slower exponentiation algorithm that is meant to hide the exponent.

@kroeckx kroeckx 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 Feb 17, 2018
@richsalz richsalz dismissed their stale review February 19, 2018 16:29

My requested changes were made

@mattcaswell
Copy link
Member

I squashed and pushed this.

levitte pushed a commit that referenced this pull request Feb 21, 2018
…nnel attacks on RSA key generation

Reviewed-by: Rich Salz <rsalz@openssl.org>
Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Kurt Roeckx <kurt@roeckx.be>
(Merged from #5161)
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 branch: master Applies to master branch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants