SignerRSA.sol 2.3 KB

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