Skip to content

ext/gmp: Add gmp_powm_sec()#22852

Open
LamentXU123 wants to merge 1 commit into
php:masterfrom
LamentXU123:gmp-sec
Open

ext/gmp: Add gmp_powm_sec()#22852
LamentXU123 wants to merge 1 commit into
php:masterfrom
LamentXU123:gmp-sec

Conversation

@LamentXU123

@LamentXU123 LamentXU123 commented Jul 21, 2026

Copy link
Copy Markdown
Member

I am currently migrating a cryptographic lib (RSA signature & decryption) from python to PHP, relying on ext-gmp. Now, I want to compute modular exponentiation with secret private exponents. For RSA decryption and signing I need to compute base^d mod N where d is a secret private exponent.

Ah, I need to consider security issue because clearly gmp_powm (implemented with mpz_powm in libgmp) leaks exponent bits via timing and cache side channels, which creates a security vulnerability.

Now, we've got mpz_powm_sec in gmp >= 5.0 which perfectly solve my issue. And, is used by gmpy2 (a very popular python lib for gmp, and is the lib my original python app relies on). In gmpy2, we have powmod_sec and powmod. It is also supported in .Net apps (and more...). However in PHP we only have gmp_powm.

...And I need to implement the securer version by myself!

So let's expose the gmp API mpz_powm_sec to userland. It is really useful. Any pow calculation which requires cryptographic security should use this instead of mpz_powm

And as a direct exposure to the GMP internal API, this doesn't requires a RFC.

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

The three guards match mpz_powm_sec()'s preconditions exactly (exp > 0, odd non-zero modulus), so nothing reaches GMP's DIVIDE_BY_ZERO. Clean.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants