Skip to content

ext/gmp: Add gmp_powm_sec() - #22852

Merged
LamentXU123 merged 3 commits into
php:masterfrom
LamentXU123:gmp-sec
Jul 28, 2026
Merged

ext/gmp: Add gmp_powm_sec()#22852
LamentXU123 merged 3 commits 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.

@TimWolla
TimWolla removed their request for review July 23, 2026 09:22

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

Minor nit but LGTM

Comment thread ext/gmp/gmp.c Outdated
@LamentXU123

Copy link
Copy Markdown
Member Author

The CI failure is unrelated.

Comment thread ext/gmp/tests/gmp_powm_sec.phpt Outdated
@adapik

adapik commented Jul 24, 2026

Copy link
Copy Markdown

I've faced the same problem in my cryptographic library. I would appreciate it if this PR could be approved.

@LamentXU123
LamentXU123 removed the request for review from kocsismate July 25, 2026 11:55
Comment thread ext/gmp/gmp.c Outdated

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

If CI is green.

@LamentXU123
LamentXU123 merged commit 081106e into php:master Jul 28, 2026
16 of 18 checks passed
@LamentXU123

Copy link
Copy Markdown
Member Author

Thanks :)

@LamentXU123
LamentXU123 deleted the gmp-sec branch July 28, 2026 13:12
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.

5 participants