Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check staticcall Result From SHA-256 Precompile #457

Merged
merged 1 commit into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions modules/passkey/contracts/libraries/WebAuthn.sol
Original file line number Diff line number Diff line change
Expand Up @@ -339,11 +339,17 @@ library WebAuthn {
function _sha256(bytes memory input) private view returns (bytes32 digest) {
// solhint-disable-next-line no-inline-assembly
assembly ("memory-safe") {
// The SHA-256 precompile is at address 0x0002. Note that we don't check the whether or
// not the precompile reverted or if the return data size is 32 bytes, which is a
// reasonable assumption for the precompile, as it is specified to always return the
// SHA-256 of its input bytes.
pop(staticcall(gas(), 0x0002, add(input, 0x20), mload(input), 0, 32))
// The SHA-256 precompile is at address 0x0002. Note that checking the result of the
// call is important to prevent callers from setting gas to specific values that would
// cause the call to the precompile to revert, but the function to continue executing
// and have digest be whatever was in the scratch space at the time of the call.
// However, we do not check if the return data size is 32 bytes, which is a reasonable
// assumption for the precompile, as it is specified to always return the SHA-256 of
// its input bytes on success. Note that this is similar to the code generated by the
// Solidity compiler for the `sha256` built-in.
if iszero(staticcall(gas(), 0x0002, add(input, 0x20), mload(input), 0, 32)) {
revert(0, 0)
}
digest := mload(0)
}
}
Expand Down
15 changes: 14 additions & 1 deletion modules/passkey/test/libraries/WebAuthn.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ describe('WebAuthn Library', () => {
})
})

describe('signingMessage', function () {
describe('encodeSigningMessage', function () {
it('Should correctly compute a signing message', async () => {
const { webAuthnLib } = await setupTests()

Expand All @@ -150,6 +150,19 @@ describe('WebAuthn Library', () => {

expect(await webAuthnLib.encodeSigningMessage(challenge, authenticatorData, `"origin":"http://safe.global"`)).to.equal(message)
})

it('Should revert if SHA-256 precompile reverts', async () => {
const { webAuthnLib } = await setupTests()

// This test is a bit tricky - the SHA-256 precompile can be made to revert by calling it
// with insufficient gas. Here we check that the revert is propagated by the
// `encodeSigningMessage` function. If the revert were not propagated, since the input is
// large enough, the function would be able to finish executing and return bogus data. Finding
// a large enough client data and exact gas limits to make this happen is a bit annoying, so
// lets hope for no gas schedule changes :fingers_crossed:.
const longClientDataFields = `"long":"${'a'.repeat(100000)}"`
await expect(webAuthnLib.encodeSigningMessage(ethers.ZeroHash, '0x', longClientDataFields, { gasLimit: 1701001 })).to.be.reverted
})
})

describe('verifySignature', function () {
Expand Down
Loading