SignerRSA.sol 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.20;
  3. import {AbstractSigner} from "./AbstractSigner.sol";
  4. import {RSA} from "../RSA.sol";
  5. /**
  6. * @dev Implementation of {AbstractSigner} using xref:api:utils/cryptography#RSA[RSA] signatures.
  7. *
  8. * For {Account} usage, a {_setSigner} function is provided to set the {signer} public key.
  9. * Doing so is easier for a factory, who is likely to use initializable clones of this contract.
  10. *
  11. * Example of usage:
  12. *
  13. * ```solidity
  14. * contract MyAccountRSA is Account, SignerRSA, Initializable {
  15. * function initialize(bytes memory e, bytes memory n) public initializer {
  16. * _setSigner(e, n);
  17. * }
  18. * }
  19. * ```
  20. *
  21. * IMPORTANT: Failing to call {_setSigner} either during construction (if used standalone)
  22. * or during initialization (if used as a clone) may leave the signer either front-runnable or unusable.
  23. */
  24. abstract contract SignerRSA is AbstractSigner {
  25. bytes private _e;
  26. bytes private _n;
  27. /**
  28. * @dev Sets the signer with a RSA public key. This function should be called during construction
  29. * or through an initializer.
  30. */
  31. function _setSigner(bytes memory e, bytes memory n) internal {
  32. _e = e;
  33. _n = n;
  34. }
  35. /// @dev Return the signer's RSA public key.
  36. function signer() public view virtual returns (bytes memory e, bytes memory n) {
  37. return (_e, _n);
  38. }
  39. /**
  40. * @dev See {AbstractSigner-_rawSignatureValidation}. Verifies a PKCSv1.5 signature by calling
  41. * xref:api:utils/cryptography.adoc#RSA-pkcs1Sha256-bytes-bytes-bytes-bytes-[RSA.pkcs1Sha256].
  42. *
  43. * IMPORTANT: Following the RSASSA-PKCS1-V1_5-VERIFY procedure outlined in RFC8017 (section 8.2.2), the
  44. * provided `hash` is used as the `M` (message) and rehashed using SHA256 according to EMSA-PKCS1-v1_5
  45. * encoding as per section 9.2 (step 1) of the RFC.
  46. */
  47. function _rawSignatureValidation(
  48. bytes32 hash,
  49. bytes calldata signature
  50. ) internal view virtual override returns (bool) {
  51. (bytes memory e, bytes memory n) = signer();
  52. return RSA.pkcs1Sha256(abi.encodePacked(hash), signature, e, n);
  53. }
  54. }