ERC-5630 Wallet Key Encryption/Decryption Standard (New approach for encryption / decryption)
Draft · Accounts & wallets · created 2022-09-07
Defines two RPC methods, public key retrieval (eth_getEncryptionPublicKey) and key agreement (eth_performECDH), so that an Ethereum wallet can use the same secp256k1 key it uses for signing to encrypt and decrypt messages.
Why it is needed
Ethereum account keys are originally meant only for signing. So even if you want to send an encrypted message to an account address, it is hard to obtain the public key needed for encryption unless you already have a communication channel with the recipient. Also, in a common design where a decentralized app (dApp) generates a new secret value for the user, the user has to separately store and back up that secret, which can lead to lost funds. Meanwhile, a previous proposal used the same secret key on two different curves together, a signing curve (secp256k1) and an encryption curve (ec25519), and hardcoded a specific encryption scheme into the wallet. There is no security proof for this combination of two curves. This standard solves these problems by performing signing and encryption on the same curve and having the wallet handle only the single most sensitive core operation.
What to implement
function encryptTo(bytes memory plaintext, bytes32 randomness) public view returns (bytes memory ciphertext);: A function that tells how to create a ciphertext addressed to the contract itself. It returns a ciphertext created from the given plaintext and randomness.
In depth
eth_getEncryptionPublicKey behavior
params is a single [account], where account is a 20 byte hexadecimal Ethereum account with a 0x prefix. The client looks up the secret signing key sk corresponding to account and returns an error if it does not exist. If it exists, it computes the secp256k1 public key corresponding to sk and returns it as a compressed 0x prefixed hexadecimal string per SEC 1 Section 2.3.3. Since the key used for signing is also used to compute the encryption public key, no separate encryption only key is created.
eth_performECDH behavior
params is [account, ephemeralKey]. The wallet looks up the secret key sk of account and returns an error if it does not exist. ephemeralKey is deserialized into an elliptic curve point per SEC 1 Section 2.3.3, where the compressed format is required, and it throws an error if deserialization fails. It then computes the elliptic curve Diffie-Hellman secret per SEC 1 Section 3.3.1, and returns the resulting field element as a 0x prefixed 32 byte hexadecimal string per SEC 1 Section 2.3.5. This ECDH is the only sensitive operation the wallet performs, and all remaining ECIES steps are performed by the app outside the wallet.
Point serialization and validation rules
All binary data is serialized as a 0x prefixed hexadecimal string, and public keys are represented in compressed form. When deserializing a point, the input byte string MUST be exactly 33 bytes long, the first byte MUST be 0x02 or 0x03, and the integer represented by the remaining 32 bytes MUST lie in the range {0, ..., p-1} and form a quadratic residue in X^3 + 7 (mod p). Because a compressed point either resolves to a point on the curve or fails to resolve at all, a point off the curve cannot sneak past validation. The original text explains that this property, combined with the fact that twist attacks apply only to classic ECDH and not to ECIES using an ephemeral key, means that such an attack does not apply to this standard.
Recommended ECIES variant
Anyone implementing ECIES outside the wallet SHOULD use the following choices unless there is a specific reason not to: ANSI-X9.63-KDF using a SHA-512 hash for the KDF, HMAC-SHA-256-256 with a 32 octet (256 bit) key for the HMAC, and AES-256 in CBC mode for the symmetric cipher. The ciphertext uses a concatenated binary serialization of its elements, and elliptic curve points are compressed. Having the wallet handle only ECDH offers two benefits. The app can choose a different ECIES variant without changing the wallet (flexibility), and only small, roughly 32 byte messages need to pass between the client and the wallet, which reduces the burden when plaintext or ciphertext is large or when the two are separated over the internet (bandwidth).
Encrypting to a contract: encryptTo
Considering account abstraction (EIP-4337) and the rise of smart contract wallets, this also defines a way for a contract to advertise how to create a ciphertext addressed to itself. It corresponds to the encryption counterpart of EIP-1271, the signature verification standard. A contract returns a ciphertext from a given plaintext and randomness via the function encryptTo(bytes memory plaintext, bytes32 randomness). Each contract MAY implement encryptTo however it wants, but SHOULD use the recommended ECIES variant above unless there is a specific reason not to.
The rationale for reusing the key on the same curve
The earlier proposal used the same secret key across two different curves, secp256k1 for signing and ec25519 for encryption, a combination with no security proof. This standard reuses the same key for both signing and encryption on a single curve, secp256k1. This setup was shown to be secure in the generic group model by Degabriele, Lehmann, Paterson, Smart, and Strefler's 2011 study on the security of combined encryption and signing in EMV, and the same combination (using ECDSA and ECIES on the same curve) is in actual operation in EMV payments. Also, if an account has signed even one transaction, its secp256k1 public key can be recovered from that signature, so a ciphertext can be created for that account without a prior channel.
Compatibility, test vectors, and proposal status
eth_performECDH is a new method, so there are no compatibility concerns. This standard overrides the behavior of eth_getEncryptionPublicKey from the earlier proposal, but since the encryption public key only needs to be fetched fresh at the time of encryption, this is unlikely to be an issue, and it does not affect decrypting ciphertext created the old way with eth_decrypt. The original text states that the earlier proposal was never standardized and that it is not aware of any non-deprecated implementation in current production code. The original text includes test vectors linking a secret signing key, its corresponding public key, an example ciphertext, an ephemeral public key, and the ECDH result. This proposal is in Draft status, so its content may still change.