Alert Source Discuss
⚠️ Draft Standards Track: ERC

ERC-7627: Secure Messaging Protocol

A solution implementing end-to-end encryption for sending messages between users.

Authors Chen Liaoyuan (@chenly) <cly@kip.pro>
Created 2024-02-19
Discussion Link https://ethereum-magicians.org/t/erc-7627-secure-messaging-protocol/18761

Abstract

This proposal implements the capability to securely exchange encrypted messages on-chain. Users can register their public keys and encryption algorithms by registration and subsequently send encrypted messages to other users using their addresses. The interface also includes enumerations for public key algorithms and a structure for user information to support various encryption algorithms and user information management.

Motivation

With the emergence of Layer 2 chains featuring sub-second block times and the introduction of account abstraction, the use of end-to-end encrypted communication has facilitated the proliferation of real-time communication and online chat dApps. Leveraging asymmetric encryption now enables the establishment of decentralized, end-to-end interoperable messaging protocols as a standard.

Specification

Objectives

  • Provide a standardized interface for implementing messaging systems in smart contracts, including user registration and message sending functionalities.
  • Enhance flexibility and scalability for messaging systems by defining enumerations for public key algorithms and a structure for user information.
  • Define events for tracking message sending to enhance the observability and auditability of the contract.
  • Using a custom sessionId allows messages to be organized into a conversation.
  • Encrypt message content using the recipient’s public key during message transmission.

Interface

The key words “MUST”, “MUST NOT”, “REQUIRED”, “SHALL”, “SHALL NOT”, “SHOULD”, “SHOULD NOT”, “RECOMMENDED”, “NOT RECOMMENDED”, “MAY”, and “OPTIONAL” in this document are to be interpreted as described in RFC 2119 and RFC 8174.

Implementers of this standard MUST have all of the following functions:

pragma solidity ^0.8.0;

interface IERC7627 {

    enum PublicKeyAlgorithm { RSA, ECDSA, ED25519, DSA, DH, ECDH, X25519 }
    
    // Events

    /**
     * @dev Event emitted when a message is sent.
     * @param from The address of the sender.
     * @param to The address of the recipient.
     * @param sessionId The session ID of the message.
     * @param encryptedMessage The encrypted message.
     */
    event MessageSent(address indexed from, address indexed to, bytes32 sessionId, bytes encryptedMessage);

    /**
     * @dev Event emitted when a user updates their public key.
     * @param user The address of the user.
     * @param newPublicKey The new public key of the user.
     * @param algorithm The algorithm used for the public key.
     */
    event PublicKeyUpdated(address indexed user, bytes newPublicKey, PublicKeyAlgorithm algorithm);

    // Functions

    /**
     * @dev Function to update a user's public key.
     * @param _publicKey The public key of the user.
     * @param _algorithm The algorithm used for the public key.
     */
    function updatePublicKey(bytes calldata _publicKey, PublicKeyAlgorithm _algorithm) external;

    /**
     * @dev Function to send an encrypted message from one user to another.
     * @param _to The address of the recipient.
     * @param _sessionId The session ID of the message.
     * @param _encryptedMessage The encrypted message.
     */
    function sendMessage(address _to, bytes32 _sessionId, bytes calldata _encryptedMessage) external;

    /**
     * @dev Function to retrieve a user's public key and algorithm.
     * @param _user The address of the user.
     * @return publicKey The public key of the user.
     * @return algorithm The algorithm used for the public key.
     */
    function getUserPublicKey(address _user) external view returns (bytes memory publicKey, PublicKeyAlgorithm algorithm);
}

Rationale

Traditional messaging lacks security and transparency for blockchain communication. The choice of asymmetric encryption ensures the confidentiality and integrity of messages, which is why we opted for this encryption method. Providing a unified interface enables easy integration of encrypted communication into smart contracts, thereby fostering innovation. Encrypted messaging guarantees adherence to best practices in data security. Due to security reasons, public keys need to be regularly updated, hence we have added a feature that allows users to autonomously update their public keys. The interface supports various encryption methods, enhancing adaptability. Event tracking enhances the observability and auditability of the contract, aiding compliance efforts. Standardization promotes interoperability, facilitating seamless communication across platforms.

Reference Implementation

pragma solidity ^0.8.0;

contract ERC7627 {

    enum PublicKeyAlgorithm { RSA, ECDSA, ED25519, DSA, DH, ECDH, X25519 }

    struct UserInfo {
        bytes publicKey;
        PublicKeyAlgorithm algorithm;
    }

    mapping(address => UserInfo) public pk;

    event MessageSent(address indexed from, address indexed to, bytes32 sessionId, bytes encryptedMessage);
    event PublicKeyUpdated(address indexed user, bytes newPublicKey, PublicKeyAlgorithm algorithm);

    // Function to register a user with their public key
    function updatePublicKey(bytes calldata _publicKey, PublicKeyAlgorithm _algorithm) public {
        pk[msg.sender].publicKey = _publicKey;
        pk[msg.sender].algorithm = _algorithm;
        emit PublicKeyUpdated(msg.sender, _publicKey, _algorithm);
    }

    // Function to send an encrypted message from one user to another
    function sendMessage(address _to, bytes32 _sessionId, bytes calldata _encryptedMessage) public {
        emit MessageSent(msg.sender, _to, _sessionId, _encryptedMessage);
    }

    // Function to retrieve a user's public key
    function getUserPublicKey(address _user) public view returns (bytes memory, PublicKeyAlgorithm) {
        UserInfo memory userInfo = pk[_user];
        return (userInfo.publicKey, userInfo.algorithm);
    }
}

Security Considerations

Utilization of Latest Secure Encryption Algorithms

When selecting encryption algorithms, it is essential to stay informed about the latest security news and recommendations. Avoid using asymmetric encryption algorithms with known vulnerabilities or those not recommended to ensure the confidentiality and integrity of messages. Regularly update encryption algorithms to address evolving security threats.

Strict Encryption Using Public Keys for Message Content

To maintain message confidentiality, the content of sent messages must be strictly encrypted using the recipient’s public key. Any plaintext information transmitted could lead to information leakage and security risks. Encrypt message content at all times during transmission and storage to prevent unauthorized access to sensitive information.

Key Management and Protection

Robust key management and protection measures are necessary for both user public and private keys. Ensure secure storage and transmission of keys to prevent leakage and tampering. Employ multi-factor authentication and key rotation strategies to enhance key security and regularly assess key management processes to mitigate potential security risks.

Auditing and Monitoring

Implement auditing and monitoring mechanisms to track message sending and receiving, as well as key usage. Promptly identify anomalous activities and potential security threats and take appropriate response measures. Record critical operations and events for security incident investigation and traceability purposes.

Copyright and related rights waived via CC0

Citation

Please cite this document as:

Chen Liaoyuan (@chenly) <cly@kip.pro>, "ERC-7627: Secure Messaging Protocol [DRAFT]," Ethereum Improvement Proposals, no. 7627, February 2024. [Online serial]. Available: https://eips.ethereum.org/EIPS/eip-7627.