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

Use abi.encodePacked in _sign() function similar to as done by Sapphire #77

Open
hats-bug-reporter bot opened this issue Jul 9, 2024 · 1 comment
Labels
Invalid - lead auditor invalid This doesn't seem right

Comments

@hats-bug-reporter
Copy link

Github username: --
Twitter username: --
Submission hash (on-chain): 0xeb5f742f50e72bd0b9a2ab9f05147ae2a027e65f13e3cceb384b38a88d86c2ef
Severity: low

Description:
Description\

AbstractTxSerializer.sol, _sign() has used bytes.concat to concatenate the input bytes32 _sigHash:

    function _sign(
        bytes32 _inputId,
        bytes32 _sigHash
    ) internal view returns (bytes memory sig) {
        (, bytes memory privKey) = _getKeyPair(_inputId);

        sig = Sapphire.sign(
            Sapphire.SigningAlg.Secp256k1PrehashedSha256,
            privKey,
@>            bytes.concat(_sigHash),
            ""
        );
    }

Affected code:

The issue is that, for such _sign implementation, use of bytes.concat() is not recommended as bytes.concat() per solidity documentation is used in case:

the bytes.concat function can concatenate an arbitrary number of bytes or bytes1 ... bytes32 values. The function returns a single bytes memory array that contains the contents of the arguments without padding.

_sigHash is a single input in bytes32 form and its not arbitrary number of bytes to use the bytes.concate().

Further, Sapphire has used abi.encodePacked() instead of bytes.concat() wherever Sapphire.sign() function used.

For example in Sapphire's EthereumUtils.sol , see below how bytes32 digest has used abi.encodePacked to concatenate the bytes.

    function sign(
        address pubkeyAddr,
        bytes32 secretKey,
        bytes32 digest
    ) internal view returns (SignatureRSV memory rsv) {
        bytes memory signature = Sapphire.sign(
            Sapphire.SigningAlg.Secp256k1PrehashedKeccak256,
            abi.encodePacked(secretKey),
@>          abi.encodePacked(digest),
            ""
        );

        rsv = splitDERSignature(signature);

        recoverV(pubkeyAddr, digest, rsv);
    }

Therefore, its recommended to use abi.encodePacked().

Recommendation to fix
Consider below changes in AbstractTxSerializer.sol:

    function _sign(
        bytes32 _inputId,
        bytes32 _sigHash
    ) internal view returns (bytes memory sig) {
        (, bytes memory privKey) = _getKeyPair(_inputId);

        sig = Sapphire.sign(
            Sapphire.SigningAlg.Secp256k1PrehashedSha256,
            privKey,
-           bytes.concat(_sigHash),
+          abi.encodePacked(_sigHash),
            ""
        );
    }
@hats-bug-reporter hats-bug-reporter bot added the bug Something isn't working label Jul 9, 2024
@party-for-illuminati
Copy link
Collaborator

Informational

@party-for-illuminati party-for-illuminati added invalid This doesn't seem right and removed bug Something isn't working labels Jul 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Invalid - lead auditor invalid This doesn't seem right
Projects
None yet
Development

No branches or pull requests

2 participants