SignerRSA.sol 2.2 KB

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