Blockchain getting started tutorial ethereum source code analysis between p2p-rlpx nodes encryption chain 1

Source: Internet
Author: User
Rlpx encryption (rlpx encryption)
The discover Node Discovery Protocol introduced earlier is basically transmitted in plaintext because the data carried is not very important.
Each node opens two ports, UDP port, and TCP port, to carry service data. The UDP port is the same as the TCP port. In this way, if a port is discovered through UDP, TCP can be used to connect to the corresponding port.
The rlpx Protocol defines the encryption process of TCP links.
Rlpx is used (perfect forward secrecy. The two parties of the link generate a random private key and obtain the Public Key through the random private key. Then, both parties exchange their respective public keys, so that both parties can generate the same shared-secret through their random private key and the other's public key ). In subsequent communications, this shared key is used as the key of the symmetric encryption algorithm. In this case. If a private key is disclosed one day, it only affects the security of the leaked message and is secure for the previous communication (because the communication key is randomly generated, it disappears after use ).
Forward Security (reference from Wikipedia)
Forward security or forward security (forward secrecy, Abbreviation: FS), also known as perfect forward security [1] (English: Perfect forward secrecy, Abbreviation: PFS) is the Security Attribute of the communication protocol in cryptography. It indicates that the leakage of a long-term CMK will not cause session key leakage in the past. [2] forward security can protect past communications from the threat of future exposure of passwords or keys. [3] if the system has forward security, it can ensure that, in the event of accidental disclosure of passwords or keys, communication that has been conducted in the past is still secure and will not be affected, this is true even if the system is active.
Difi-Herman Key Exchange
Difi-Hellman Key Exchange (D-H) is a security protocol. It allows both parties to create a key through unsafe channels without any prior information from the other party. This key can be used as a symmetric key for subsequent communication to encrypt the communication content. The concept of public key exchange was first defined by Ralph C. proposed by Merkle, and this key exchange method was first published in 1976 by Bailey Whitfield Diffie and Martin Edward Hellman. Martin Hermann once advocated this key exchange method, which should be called difi-Hermann-Merkle Key Exchange (English: Diffie-Hellman-Merkle Key Exchange ).
Keywords:
Difi-Herman Key Negotiation
Defi-Herman key Creation
Exponential Key Exchange
Defi-Herman Protocol
Although difi-Herman's key exchange is an anonymous (unauthenticated) Key Exchange Protocol, it is the basis of many authentication protocols, it is also used to provide complete forward security in the transient mode of the Transport Layer Security protocol.
Description
By exchanging information through a public channel, defi-Herman can create a shared secret that can be used for secure communication over a public channel ).
P2P/rlpx. Go source code
This file implements the rlpx link protocol.
The procedure of link contact is as follows:
Doenchandshake () uses this method to exchange keys and create encrypted channels. If the connection fails, the link is closed.
Doprotohandshake () is used to negotiate protocol features, such as the Protocol version of both parties and whether snappy encryption is supported.
After the two links are processed, they are created. Because TCP is a stream protocol. All rlpx Protocols define the frame splitting method. All data can be understood as one rlpxframe. Rlpx reads and writes are all processed through the rlpxframerw object.
Doenchandshake
The initiator of the link is called initiator. The passive receiver of the link is converted into a receiver. The process is different in these two modes. After the handshake is completed. An SEC is generated, which can be understood as a symmetric encryption key. Then a newrlpxframerw frame reader is created. Creates an encrypted channel.

func (t *rlpx) doEncHandshake(prv *ecdsa.PrivateKey, dial *discover.Node) (discover.NodeID, error) {    var (        sec secrets        err error    )    if dial == nil {        sec, err = receiverEncHandshake(t.fd, prv, nil)    } else {        sec, err = initiatorEncHandshake(t.fd, prv, dial.ID, nil)    }    if err != nil {        return discover.NodeID{}, err    }    t.wmu.Lock()    t.rw = newRLPXFrameRW(t.fd, sec)    t.wmu.Unlock()    return sec.RemoteID, nil}

Initiatorenchandshake first looks at the operations of the initiator of the link. First, the authmsg is created through makeauthmsg. Then, it is sent to the peer through the network. Then read the response from the peer through readhandshakemsg. Finally, secrets is called to create a shared secret.

// initiatorEncHandshake negotiates a session token on conn.// it should be called on the dialing side of the connection.//// prv is the local client‘s private key.func initiatorEncHandshake(conn io.ReadWriter, prv *ecdsa.PrivateKey, remoteID discover.NodeID, token []byte) (s secrets, err error) {    h := &encHandshake{initiator: true, remoteID: remoteID}    authMsg, err := h.makeAuthMsg(prv, token)    if err != nil {        return s, err    }    authPacket, err := sealEIP8(authMsg, h)    if err != nil {        return s, err    }    if _, err = conn.Write(authPacket); err != nil {        return s, err    }    authRespMsg := new(authRespV4)    authRespPacket, err := readHandshakeMsg(authRespMsg, encAuthRespLen, prv, conn)    if err != nil {        return s, err    }    if err := h.handleAuthResp(authRespMsg); err != nil {        return s, err    }    return h.secrets(authPacket, authRespPacket)}

Makeauthmsg. This method creates the handshake message of initiator. First, the peer public key can be obtained through the peer ID. Therefore, the peer Public Key is known to the initiator. However, the peer public key should be unknown to the connected person.

// Makeauthmsg creates the initiator handshake message. func (H * enchandshake) makeauthmsg (PRV * ECDSA. privatekey, Token [] Byte) (* authmsgv4, error) {rpub, err: = H. remoteid. pubkey () If Err! = Nil {return nil, FMT. errorf ("Bad remoteid: % v", err)} H. remotepub = ecies. importecdsapublic (rpub) // generate random initiator nonce. // generate a random initial value to avoid replay? Or to avoid guessing the key through multiple connections? H. initnonce = make ([] Byte, shalen) If _, err: = Rand. Read (H. initnonce); Err! = Nil {return nil, err} // generate random keypair to for ecdh. // generate a random private key H. randomprivkey, err = ecies. generatekey (Rand. reader, crypto. s256 (), nil) If Err! = Nil {return nil, err}

Blockchain getting started tutorial ethereum source code analysis between p2p-rlpx nodes encryption chain 1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.