## 4 EGCIR Protocol
### Data Security with Hierarchical Key Distribution  
This section presents the details of proposed data security with hierarchical key distribution algorithm on the formulated clusters, as discussed in Section 4.3. In the EGCIR protocol, the process of key generation and distribution is performed in the hierarchy. The BS acts as a key distribution center (KDC), which generates and shares the symmetric key among cluster heads for data encryption and authentication. In addition, the cluster heads act as a local BS to ensure secure communications within a cluster. Symmetric keys are used for encoding and decoding sensors' data between the sending and receiving nodes. To initiate the routing session, the source sensor nodes send a request packet \(R_{req}\) towards cluster heads for issuing symmetric keys \(s_{k}\). The routing session aims to identify the next-hop for data forwarding over the wireless channels. Upon receiving the \(R_{req}\), the cluster head transmits the \(s_{k}\) towards sensor nodes for the particular session, also the generated \(s_{k}\) is encrypted with the private key \(PR_{k}\) of the cluster head. Similarly, on receiving the \(s_{k}\) from the cluster head, sensor nodes decrypt it with the public key \(PU_{k}\) of the cluster head, which ensures the authenticity of it. Accordingly, the cluster data \(D(C_{i})\) from all member nodes \(n_{1}\), \(n_{2}\),..., \(n_{n}\) is securely forwarded to the associated cluster head, as given in Equation (3). \[D(C_{i})=n_{1}(d)\oplus s_{k1}+n_{2}(d)\oplus s_{k2}+\ldots+n_{n}(d)\oplus s_{ kn} \tag{3}\]  
In addition--instead of assigning encryption keys to cluster heads before intercluster routing--the proposed security algorithm needs the cluster heads to negotiate with the BS. Both the source and upstream cluster heads \(i\) and \(j\) search the BS in their local tables, if they are found, then the cluster heads \(i\) and \(j\) send \(R_{req}\) to the BS directly. It may be a case that there is no entry of the BS in the local tables of cluster heads, then \(R_{req}\) is sent to the BS through an intermediate node. Upon receiving the request packets, the BS generates and sends back the \(s_{k}\) key to the cluster heads either directly or using the intermediate nodes. Likewise, the BS stores the generated \(s_{k}\) for the particular cluster heads in its generalized table. To secure the symmetric keys against intermediate nodes, the BS extracts the public keys of source and upstream cluster heads from the generalized table. Afterward, the BS encrypts the symmetric keys using public keys of source and upstream cluster heads. Accordingly, the symmetric keys can only be decrypted using the associated private keys and no other intermediate node can misuse the symmetric keys for data transmission. The WSN can perform different functions on open-space and there are many possibilities for malicious nodes to capture the requested packets. Therefore, the EGCIR protocol incorporates nonce in the \(R_{req}\) packet as \(\{(\text{ID},\text{ time})+N_{i}\}\) to produce randomness and avoid the replay attack. The nonce is a cryptographic value and is used only once in the entire communication session. The presence of nonce is a \(R_{req}\) packet, which guarantees that the same packet cannot be regenerated by any malicious node for obtaining the symmetric key. In the EGCIR protocol, the BS performs the role of main server, which is consulted before data routing takes place. Such a mechanism improves the level of security against malicious threats because each time a unique symmetric key is generated by the BS to initiate the intercluster routing. After receiving the \(s_{k}\) from the BS, both cluster heads \(i\) and \(j\) can exchange the secret information to authenticate with each other. Furthermore, the EGCIR protocol makes the symmetric key more secure by encryption and decryption mechanisms based on private-public keys. It exploits the RSA [43; 44], which is a standard security algorithm to generate private-public keys.