# DiffieHellman doesn't generate keys after setting a key

Node.js crypto generateKeys() silently reuses a previously set DH private key instead of regenerating, causing nonce reuse in Diffie-Hellman.

- Payout: $1,800
- Severity: medium
- Program: Internet Bug Bounty
- Found by: bensmyth
- Disclosed: 2023-06-30
- CVE: CVE-2023-30590
- Write-up quality: 6/10
- Original: https://hackerone.com/reports/2038484
- Library page: https://bugbountylibrary.com/r/h1%3A2038484
- Tags: area:oss domain:oss impact:integrity kind:bounty platform:hackerone sev:medium tech:nodejs technique:source-review vuln:crypto

**Target:** node:crypto DiffieHellman (OpenSSL DH wrapper)
**Root cause:** OpenSSL's DH_generate_key only generates a new private key when dh->priv_key is NULL; Node's generateKeys() calls it without resetting the existing key, so a previously set private key persists.
**Steps:**
1. `const dh = crypto.createDiffieHellman(1024);`
2. `dh.setPrivateKey(Buffer.from("02",'hex'));`
3. `dh.generateKeys();`
4. `dh.getPrivateKey().toString('hex')` still returns `02` instead of a fresh random key
**How found:** Developer set a private key, called generateKeys(), and observed the key was unchanged; traced to OpenSSL dh_key.c where priv_key is only allocated when NULL.
**Insight:** When wrapping a crypto library, check whether its 'generate' functions are no-ops when state is pre-populated; test set-then-generate sequences for silent nonce/key reuse.

## Proof of concept (copied exactly from the original report)

**Repro: setPrivateKey then generateKeys keeps old key**

```
const dh = crypto.createDiffieHellman(1024);`
  2. Set private key: 
//set private key to 2
dh.setPrivateKey(Buffer.from("02", 'hex'));        
//outputs 02 (as expected)
console.log(dh.getPrivateKey().toString('hex'));
```

