65
Filippo Valsorda Exploiting ECDSA Failures in the Bitcoin Blockchain HITB2014KUL

Exploiting ECDSA Failures in the Bitcoin Blockchain - Filip… · A signed statement, published to the world and recorded in the blockchain A transaction “This money I can spend,

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Exploiting ECDSA Failures in the Bitcoin Blockchain - Filip… · A signed statement, published to the world and recorded in the blockchain A transaction “This money I can spend,

Filippo Valsorda

Exploiting ECDSA Failures in the

Bitcoin Blockchain

HITB2014KUL

Page 2: Exploiting ECDSA Failures in the Bitcoin Blockchain - Filip… · A signed statement, published to the world and recorded in the blockchain A transaction “This money I can spend,

CloudFlare security team @FiloSottile

I mess with cryptography. And open source.

!

filippo.io

Filippo Valsorda

Page 3: Exploiting ECDSA Failures in the Bitcoin Blockchain - Filip… · A signed statement, published to the world and recorded in the blockchain A transaction “This money I can spend,

But you probably know me for this

Page 4: Exploiting ECDSA Failures in the Bitcoin Blockchain - Filip… · A signed statement, published to the world and recorded in the blockchain A transaction “This money I can spend,

https://filippo.io/heartbleed

Page 5: Exploiting ECDSA Failures in the Bitcoin Blockchain - Filip… · A signed statement, published to the world and recorded in the blockchain A transaction “This money I can spend,

Bitcoin

Page 6: Exploiting ECDSA Failures in the Bitcoin Blockchain - Filip… · A signed statement, published to the world and recorded in the blockchain A transaction “This money I can spend,

Public key + Private key

A wallet

The address: hash ( public key )1DY5YvRxSwomrK7nELDZzAidQQ6ktjRR9A

Page 7: Exploiting ECDSA Failures in the Bitcoin Blockchain - Filip… · A signed statement, published to the world and recorded in the blockchain A transaction “This money I can spend,

A signed statement, published to the world

and recorded in the blockchain

A transaction

“This money I can spend, can now be spent by Y”

Page 8: Exploiting ECDSA Failures in the Bitcoin Blockchain - Filip… · A signed statement, published to the world and recorded in the blockchain A transaction “This money I can spend,

A: This money I can spend, can now be spent by X…: This money I can spend, can now be spent by ……: This money I can spend, can now be spent by …

…: This money I can spend, can now be spent by …X: This money I can spend, can now be spent by Y

…: This money I can spend, can now be spent by ……: This money I can spend, can now be spent by …

Y has this money to spend

Page 9: Exploiting ECDSA Failures in the Bitcoin Blockchain - Filip… · A signed statement, published to the world and recorded in the blockchain A transaction “This money I can spend,

A: This money I can spend, can now be spent by X

Signed with A’s private key

Hash of X’s public key

Page 10: Exploiting ECDSA Failures in the Bitcoin Blockchain - Filip… · A signed statement, published to the world and recorded in the blockchain A transaction “This money I can spend,

OP_DUP OP_HASH160 <pubKeyHash> OP_EQUALVERIFY OP_CHECKSIG

<sig> <pubKey>

Actually

Page 11: Exploiting ECDSA Failures in the Bitcoin Blockchain - Filip… · A signed statement, published to the world and recorded in the blockchain A transaction “This money I can spend,
Page 12: Exploiting ECDSA Failures in the Bitcoin Blockchain - Filip… · A signed statement, published to the world and recorded in the blockchain A transaction “This money I can spend,

ECDSA

Page 13: Exploiting ECDSA Failures in the Bitcoin Blockchain - Filip… · A signed statement, published to the world and recorded in the blockchain A transaction “This money I can spend,

A EC based signature scheme

As seen in TLS, DNSSEc, the PS3…

Elliptic Curve Digital Signature Algorithm

Page 14: Exploiting ECDSA Failures in the Bitcoin Blockchain - Filip… · A signed statement, published to the world and recorded in the blockchain A transaction “This money I can spend,

Global: point G on a curve Private key: a random number d

Public key: d X G

A summary

Page 15: Exploiting ECDSA Failures in the Bitcoin Blockchain - Filip… · A signed statement, published to the world and recorded in the blockchain A transaction “This money I can spend,

e = hash(message) k = a random number

(x, y) = k X G r = x

Signature

Sig: [r,(e+r*d)/k]

Page 16: Exploiting ECDSA Failures in the Bitcoin Blockchain - Filip… · A signed statement, published to the world and recorded in the blockchain A transaction “This money I can spend,

Unless…

Seems fine, right?

What happens if that k is not random?

Page 17: Exploiting ECDSA Failures in the Bitcoin Blockchain - Filip… · A signed statement, published to the world and recorded in the blockchain A transaction “This money I can spend,

k1 = k2 (x, y) = k X G r = x

r1 = r2

If you reuse k

Sig1: [r,(e1+r*d)/k] Sig2: [r,(e2+r*d)/k]

Page 18: Exploiting ECDSA Failures in the Bitcoin Blockchain - Filip… · A signed statement, published to the world and recorded in the blockchain A transaction “This money I can spend,

If you reuse k

Sig1: [ r ,(e1+r*d)/k] Sig2: [ r ,(e2+r*d)/k]

k1 = k2 (x, y) = k X G r = x

r1 = r2

Page 19: Exploiting ECDSA Failures in the Bitcoin Blockchain - Filip… · A signed statement, published to the world and recorded in the blockchain A transaction “This money I can spend,

If you reuse k

Sig1: [r, (e1+r*d)/k ] Sig2: [r, (e2+r*d)/k ]

k1 = k2 (x, y) = k X G r = x

r1 = r2

Page 20: Exploiting ECDSA Failures in the Bitcoin Blockchain - Filip… · A signed statement, published to the world and recorded in the blockchain A transaction “This money I can spend,

k = (e1 - e2)/ (e1+r*d)/k - (e2+r*d)/k]

If you reuse k

d = [(e1+r*d)/k]*k-e1 r

Page 21: Exploiting ECDSA Failures in the Bitcoin Blockchain - Filip… · A signed statement, published to the world and recorded in the blockchain A transaction “This money I can spend,

Boom.

Page 22: Exploiting ECDSA Failures in the Bitcoin Blockchain - Filip… · A signed statement, published to the world and recorded in the blockchain A transaction “This money I can spend,

Text Text Text Text Text Text Text Text Text

Imperialviolet

Accent Accent Accent

Page 23: Exploiting ECDSA Failures in the Bitcoin Blockchain - Filip… · A signed statement, published to the world and recorded in the blockchain A transaction “This money I can spend,

Text Text Text Text Text Text Text Text Text

Sony’s ECDSA code

Mittwoch, 29. Dezember 2010

Page 24: Exploiting ECDSA Failures in the Bitcoin Blockchain - Filip… · A signed statement, published to the world and recorded in the blockchain A transaction “This money I can spend,
Page 25: Exploiting ECDSA Failures in the Bitcoin Blockchain - Filip… · A signed statement, published to the world and recorded in the blockchain A transaction “This money I can spend,

the

blockchain

Page 26: Exploiting ECDSA Failures in the Bitcoin Blockchain - Filip… · A signed statement, published to the world and recorded in the blockchain A transaction “This money I can spend,

To spend money: the public key of the address;

a signature w/ that key

Reminder

when money is moved a signature is published

Page 27: Exploiting ECDSA Failures in the Bitcoin Blockchain - Filip… · A signed statement, published to the world and recorded in the blockchain A transaction “This money I can spend,

for block in chain: for tx in block: for input in tx: ...

An easy search

A input is money being spent in the tx

Page 28: Exploiting ECDSA Failures in the Bitcoin Blockchain - Filip… · A signed statement, published to the world and recorded in the blockchain A transaction “This money I can spend,

Extract r from the signature; take note of where we found

it in a lookup table; check if we found it before.

An easy search

Page 29: Exploiting ECDSA Failures in the Bitcoin Blockchain - Filip… · A signed statement, published to the world and recorded in the blockchain A transaction “This money I can spend,

Done!If anyone reuses k,

we will find two equal r.

Page 30: Exploiting ECDSA Failures in the Bitcoin Blockchain - Filip… · A signed statement, published to the world and recorded in the blockchain A transaction “This money I can spend,

Well… No. I mean, yes, but there are

100M inputs in the blockchain.

Done!

Out of memory! :(

Page 31: Exploiting ECDSA Failures in the Bitcoin Blockchain - Filip… · A signed statement, published to the world and recorded in the blockchain A transaction “This money I can spend,

First pass: filter the possible r. Add to a Bloom filter, if present add to a set.

!

Second pass: if r present in the set, export sig and pubkey.

A smarter search

Page 32: Exploiting ECDSA Failures in the Bitcoin Blockchain - Filip… · A signed statement, published to the world and recorded in the blockchain A transaction “This money I can spend,

A smarter search

r = 42 r = 42

Bloom filter+ Blockchain

Set

Page 33: Exploiting ECDSA Failures in the Bitcoin Blockchain - Filip… · A signed statement, published to the world and recorded in the blockchain A transaction “This money I can spend,

A smarter searchBloom filter

? Blockchain

42

r = 42 r = 42

+

Set

Page 34: Exploiting ECDSA Failures in the Bitcoin Blockchain - Filip… · A signed statement, published to the world and recorded in the blockchain A transaction “This money I can spend,

A smarter search

? ✓

Final list Sig, Pubkey, Tx…

r = 42 r = 42

42

Set

1936Blockchain

Page 35: Exploiting ECDSA Failures in the Bitcoin Blockchain - Filip… · A signed statement, published to the world and recorded in the blockchain A transaction “This money I can spend,

Group the list by (r, pubkey) and recover d

from pairs of signatures!

Finally

Page 36: Exploiting ECDSA Failures in the Bitcoin Blockchain - Filip… · A signed statement, published to the world and recorded in the blockchain A transaction “This money I can spend,

A ready to use toolBlockchainr

github.com/filosottile/blockchainr

Page 37: Exploiting ECDSA Failures in the Bitcoin Blockchain - Filip… · A signed statement, published to the world and recorded in the blockchain A transaction “This money I can spend,

Results

Page 38: Exploiting ECDSA Failures in the Bitcoin Blockchain - Filip… · A signed statement, published to the world and recorded in the blockchain A transaction “This money I can spend,

https://filippo.io/hitb

If you want to follow from home

Page 39: Exploiting ECDSA Failures in the Bitcoin Blockchain - Filip… · A signed statement, published to the world and recorded in the blockchain A transaction “This money I can spend,

Does this happen?

Page 40: Exploiting ECDSA Failures in the Bitcoin Blockchain - Filip… · A signed statement, published to the world and recorded in the blockchain A transaction “This money I can spend,

Yes.

Does this happen?

Page 41: Exploiting ECDSA Failures in the Bitcoin Blockchain - Filip… · A signed statement, published to the world and recorded in the blockchain A transaction “This money I can spend,

Vertical: address Color: r

Page 42: Exploiting ECDSA Failures in the Bitcoin Blockchain - Filip… · A signed statement, published to the world and recorded in the blockchain A transaction “This money I can spend,

weird Multisignature transactions

Page 43: Exploiting ECDSA Failures in the Bitcoin Blockchain - Filip… · A signed statement, published to the world and recorded in the blockchain A transaction “This money I can spend,

1KtjBE8yDxoqNTSyLG2re4qtKK19KpvVLT 1BkE8ttBRUKVNTj3Lx1EPsw7vVbhuLZhBt

Page 44: Exploiting ECDSA Failures in the Bitcoin Blockchain - Filip… · A signed statement, published to the world and recorded in the blockchain A transaction “This money I can spend,

Vertical: address Color: r

Page 45: Exploiting ECDSA Failures in the Bitcoin Blockchain - Filip… · A signed statement, published to the world and recorded in the blockchain A transaction “This money I can spend,

“gomez” 1GozmcsMBC7bnMVUQLTKEw5vBxbSeG4erW / 1HKywxiL4JziqXrzLKhmB6a74ma6kxbSDj

Page 46: Exploiting ECDSA Failures in the Bitcoin Blockchain - Filip… · A signed statement, published to the world and recorded in the blockchain A transaction “This money I can spend,

Repeated r in the same transaction

Page 47: Exploiting ECDSA Failures in the Bitcoin Blockchain - Filip… · A signed statement, published to the world and recorded in the blockchain A transaction “This money I can spend,

https://bitcointalk.org/index.php?topic=271486

“Bad signatures leading to 55.82152538 BTC theft (so far)”

Page 48: Exploiting ECDSA Failures in the Bitcoin Blockchain - Filip… · A signed statement, published to the world and recorded in the blockchain A transaction “This money I can spend,

https://bitcointalk.org/index.php?topic=277595

Blockchain.info security [FUNDS STOLEN]

Page 49: Exploiting ECDSA Failures in the Bitcoin Blockchain - Filip… · A signed statement, published to the world and recorded in the blockchain A transaction “This money I can spend,

Text Text Text Text Text Text Text Text Text

TEXT TEXT TEXT TEXT

Accent Accent Accent

Page 50: Exploiting ECDSA Failures in the Bitcoin Blockchain - Filip… · A signed statement, published to the world and recorded in the blockchain A transaction “This money I can spend,

Nick sullivan “exploiting randomness” demo

Page 51: Exploiting ECDSA Failures in the Bitcoin Blockchain - Filip… · A signed statement, published to the world and recorded in the blockchain A transaction “This money I can spend,
Page 52: Exploiting ECDSA Failures in the Bitcoin Blockchain - Filip… · A signed statement, published to the world and recorded in the blockchain A transaction “This money I can spend,
Page 53: Exploiting ECDSA Failures in the Bitcoin Blockchain - Filip… · A signed statement, published to the world and recorded in the blockchain A transaction “This money I can spend,

The fix

Page 54: Exploiting ECDSA Failures in the Bitcoin Blockchain - Filip… · A signed statement, published to the world and recorded in the blockchain A transaction “This money I can spend,

k must be secret and unique

What’s needed

Not necessarily random

Page 55: Exploiting ECDSA Failures in the Bitcoin Blockchain - Filip… · A signed statement, published to the world and recorded in the blockchain A transaction “This money I can spend,

Generate k deterministically, as a function of private key

and message.

RFC 6979

k = HMAC_DRBG ( d, H (m) )

Page 56: Exploiting ECDSA Failures in the Bitcoin Blockchain - Filip… · A signed statement, published to the world and recorded in the blockchain A transaction “This money I can spend,

Bitcoin core

unsafe: openssl patch by AGL waiting on master

Page 57: Exploiting ECDSA Failures in the Bitcoin Blockchain - Filip… · A signed statement, published to the world and recorded in the blockchain A transaction “This money I can spend,
Page 58: Exploiting ECDSA Failures in the Bitcoin Blockchain - Filip… · A signed statement, published to the world and recorded in the blockchain A transaction “This money I can spend,

electrum

safe since v1.9 correct use of python-ecdsa

Page 59: Exploiting ECDSA Failures in the Bitcoin Blockchain - Filip… · A signed statement, published to the world and recorded in the blockchain A transaction “This money I can spend,

Multibit / bitcoinj

safe correct use of bouncycastle

Page 60: Exploiting ECDSA Failures in the Bitcoin Blockchain - Filip… · A signed statement, published to the world and recorded in the blockchain A transaction “This money I can spend,

Blockchain.info

Unsafe relies on the browser RNG (if any!)

Page 61: Exploiting ECDSA Failures in the Bitcoin Blockchain - Filip… · A signed statement, published to the world and recorded in the blockchain A transaction “This money I can spend,
Page 62: Exploiting ECDSA Failures in the Bitcoin Blockchain - Filip… · A signed statement, published to the world and recorded in the blockchain A transaction “This money I can spend,

bitrated / bitcoinjs-lib

Safe Hashes privkey, message and random

Page 63: Exploiting ECDSA Failures in the Bitcoin Blockchain - Filip… · A signed statement, published to the world and recorded in the blockchain A transaction “This money I can spend,

Armory

unsafe (? - 90%) crypto++ seems to use a random value

Page 64: Exploiting ECDSA Failures in the Bitcoin Blockchain - Filip… · A signed statement, published to the world and recorded in the blockchain A transaction “This money I can spend,

Trezor

Safe Implements RFC 6979

Page 65: Exploiting ECDSA Failures in the Bitcoin Blockchain - Filip… · A signed statement, published to the world and recorded in the blockchain A transaction “This money I can spend,

Q&A

@filosottile filippo.io/hitb-slides