Skip to content

fix(cipher/diffiehellman): use valid random private keys in key-exchange test#804

Open
rajpreetcodes wants to merge 1 commit into
TheAlgorithms:masterfrom
rajpreetcodes:fix/diffiehellman-rsa-key-too-small
Open

fix(cipher/diffiehellman): use valid random private keys in key-exchange test#804
rajpreetcodes wants to merge 1 commit into
TheAlgorithms:masterfrom
rajpreetcodes:fix/diffiehellman-rsa-key-too-small

Conversation

@rajpreetcodes
Copy link
Copy Markdown

Description of Change

The Diffie-Hellman key-exchange test produced its "random" private keys by calling rsa.GenerateKey(rand.Reader, 31) and reading .D.Int64().

Go 1.24 introduced a minimum RSA key size of 1024 bits, so rsa.GenerateKey with a 31-bit size now returns the error rsa: key too small and a nil key. The test ignored that error (alicePrvKey, _ := ...) and then dereferenced the nil key (alicePrvKey.D.Int64()), producing a nil-pointer panic:

--- FAIL: TestDiffieHellmanKeyExchange/Test_2:_Key_Exchange
panic: runtime error: invalid memory address or nil pointer dereference
    .../diffiehellmankeyexchange_test.go:28

Because the CI workflow uses setup-go with go-version: '^1.18' (which resolves to the latest Go release), this breaks the build on Go ≥ 1.24.

This change generates the private keys directly with crypto/rand in the valid Diffie-Hellman range [1, primeNumber-1], removing the RSA dependency. It also makes the test more meaningful: the old .D.Int64() value could be negative, which sent modularExponentiation down a degenerate path where the equality assertion held even for invalid inputs.

Verification:

  • go test ./cipher/diffiehellman/ passes (ran with -count=20, not flaky)
  • go test ./... is green
  • go vet ./... clean
  • gofmt clean

Checklist

  • Added description of change
  • Added file name matches File name guidelines
  • Added tests and example, test must pass
  • Added documentation so that the program is self-explanatory and educational - GoDoc guidelines
  • Relevant documentation/comments is changed or added
  • PR title follows semantic commit guidelines
  • Search previous suggestions before making a new one, as yours may be a duplicate.
  • I acknowledge that all my contributions will be made under the project's license.

Notes: Fixes a nil-pointer panic in the Diffie-Hellman key-exchange test on Go ≥ 1.24 (rsa: key too small).

…nge test

The key-exchange test generated "random" private keys via
rsa.GenerateKey(rand.Reader, 31) and read .D.Int64(). Go 1.24 added a
minimum RSA key size of 1024 bits, so GenerateKey now returns
"rsa: key too small" and a nil key. The test ignored that error and
dereferenced the nil key, causing a nil-pointer panic on Go >= 1.24
(the version CI's "^1.18" spec resolves to).

Generate the private keys directly with crypto/rand in the valid
Diffie-Hellman range [1, primeNumber-1] instead. This removes the
broken RSA dependency and also makes the test meaningful: the previous
.D.Int64() could be negative, which drove modularExponentiation down a
degenerate path so the assertion held even for invalid inputs.
@codacy-production
Copy link
Copy Markdown

codacy-production Bot commented May 25, 2026

Not up to standards ⛔

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant