Immunefi Profile picture
13 Jan, 19 tweets, 5 min read
This is part 3 of the thread about signatures in Ethereum.

In this thread, we will learn:

- Replay attacks
- Signature Malleability
- EIP-2
- Nonces

Ok, let's get started! 🏃‍♂️

With signatures in Ethereum, there may be an issue if a valid signature might be used several times in other places where it’s not intended to be used.

This types of security issues are called Replay Attacks. ⏮🤺

More about them 👇
Imagine a scenario where we have a function that transfers funds, but only when a valid signature is provided.

At first glance, the code looks good.

But the devil is in the details 😈 Image
So, what happens in the code?

We check the address of the ECDSA signature by providing `v,r,s` values.

We compare the returned address with the owner address, and if it’s an owner, we proceed with the transfer of funds.

What are we missing? 🤔
The problem is in the signed message.

The message only contains the receiver address and the amount to be unlocked.

There is nothing in the message that could prevent the same signatures from being used multiple times.😨

How can we prevent that?
To prevent the signature replay attack, we can store the signature we used in the `executed` mapping.

This way, whenever someone would want to replay our signature, it would fail, as we can check if this signature was already used by simply checking the mapping.

Code below: Image
There are still issues with the code above ☝️

It does not follow the recommended best practice for signature verification, especially the `S` value.

This can lead to signature malleability.

What does that mean?
Within Solidity, an ECDSA signature is represented by its *r, s* and *v* values.

The relationship between the public key, the message hash, *r*, *s*, and *v* can be checked to ensure that only the person who knows the corresponding *private* key calculates *r*, *s*, and *v*...
...However, due to the symmetric structure of elliptic curves, for every set of *r*, *s*, and *v*, there is another set of *r*, *s*, and *v* that *also* has the same precise mathematical relationship.

This results in TWO valid signatures 🤯
Fortunately, it’s easy to detect the duplicate signature 😎

Not going much into mathematical details, we need to choose the proper "half" of the eliptic curve.

Value of *s* is the one that helps us determine which half we need.
As seen below, an Elliptic Curve is symmetric on the X-axis, meaning two points can exist with the same X value.

We can carefully adjust *s* to produce a valid signature for the same *r* on the other side of the X-axis. Image
The meaning behind all of this is we can invert one valid signature to get another valid signature which will still be valid and basically replay a signature!

There is a way to prevent that, and the first major hard-fork of Ethereum introduced a solution to this:

EIP-2
EIP-2 introduced limits on the *s* values to prevent signature malleability by only considering lower levels of *s* as valid.

By restricting valid range, EIP-2 effectively removes half the points from the group, ensuring there is at most one valid point at each **x** coordinate.
EIP-2 was introduced into EVM, it didn’t affect the precompiled contract `ecrecover`

So, whenever we’re using plain `ecrecover`, we’re still prone to signature malleability!

Don’t worry, because OpenZeppelin created an appropriate library ECDSA.sol that solves this issue.
The trick is simple: we restrict the s value to be in the lower-end. Image
Another way to combat signature malleability *and* replay is the usage of an application-level nonce.

“Nonce” is cryptographer short-hand for “number used once”.

We can use a nonce for every signature and store the next nonce inside the contract. Image
This covers the most *common* issues with signatures.
We still need to cover EIP-712 but that's in another 🧶

And if you prefer reading a blog post instead of a 🧵, check out awesome article at our Medium:

medium.com/immunefi/intro…
If you enjoyed this thread, please like and retweet to share the knowledge at #immunefischool and other Web3 communities.

The more you know, the safer the ecosystem!⛑

See ya in the next one!👋

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Immunefi

Immunefi Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @immunefi

10 Jan
This is part 2 of a thread on signatures in Ethereum.

In this thread we will learn:

- What is a digital signature in the context of Ethereum?
- ECDSA
- Meta-transactions
- ERC20-Permit

Ok, let's get started! 🏃‍♂️

A digital signature can be created to sign any message.

For Ethereum transactions, the details of the transaction itself are used as the message.

The mathematics of cryptography provides a way for the message (i.e. the transaction details) to be combined with the private key👇
☝️that in itself creates a code that can only be produced with knowledge of the private key.

That code is called the digital signature.

The above explanation was taken from the wonderful Mastering Ethereum book, which you can find free on GitHub.

github.com/ethereumbook/e…
Read 13 tweets
6 Jan
This is part 1 out of a 2 part thread series dedicated to Signatures in Ethereum.

It was inevitable that sooner or later, we would need to talk about this.

But bear with us, as we're going to make this as digestible as possible!

First stop, Public Key Cryptography 🔐👩‍🏫

👇
There are two main purposes of cryptography

- prove knowledge of a secret without revealing the secret
- prove authenticity of data (digital signature)

Cryptography is used extensively within Ethereum, and one place that users meet with cryptography is via Ethereum accounts.👩‍💻
Proof of ownership of Externally Owned Accounts (EOAs) is established through private keys and digital signatures.

The private keys are used almost everywhere within Ethereum during user interactions, and the Ethereum address of an EOA is derived from the private key.
Read 11 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(