SignerRSA.sol 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. constructor(bytes memory e, bytes memory n) {
  28. _setSigner(e, n);
  29. }
  30. /**
  31. * @dev Sets the signer with a RSA public key. This function should be called during construction
  32. * or through an initializer.
  33. */
  34. function _setSigner(bytes memory e, bytes memory n) internal {
  35. _e = e;
  36. _n = n;
  37. }
  38. /// @dev Return the signer's RSA public key.
  39. function signer() public view virtual returns (bytes memory e, bytes memory n) {
  40. return (_e, _n);
  41. }
  42. /**
  43. * @dev See {AbstractSigner-_rawSignatureValidation}. Verifies a PKCSv1.5 signature by calling
  44. * xref:api:utils/cryptography.adoc#RSA-pkcs1Sha256-bytes-bytes-bytes-bytes-[RSA.pkcs1Sha256].
  45. *
  46. * IMPORTANT: Following the RSASSA-PKCS1-V1_5-VERIFY procedure outlined in RFC8017 (section 8.2.2), the
  47. * provided `hash` is used as the `M` (message) and rehashed using SHA256 according to EMSA-PKCS1-v1_5
  48. * encoding as per section 9.2 (step 1) of the RFC.
  49. */
  50. function _rawSignatureValidation(
  51. bytes32 hash,
  52. bytes calldata signature
  53. ) internal view virtual override returns (bool) {
  54. (bytes memory e, bytes memory n) = signer();
  55. return RSA.pkcs1Sha256(abi.encodePacked(hash), signature, e, n);
  56. }
  57. }