# A Lightweight Secure and Energy-Efficient Fog-Based Routing Protocol for Constraint Sensors Network
## 3 Proposed SEFR Protocol
### Methodology of SEFR
The source nodes flood a fixed amount of beacon messages to neighbors periodically. Suppose \(Pk_{r}\) is the amount of received packets and \(Pk_{d}\) is the number of drop packets then the estimated packet queuing \(E(PQ)\) can be computed using Equation (4). \[\max[E(PQ)]=\ \ \frac{Pk_{r}}{Pk_{r}+Pk_{d}}*R_{T} \tag{4}\]  
where \(R_{T}\) denotes the data transfer rate of the network. The value \(E(PQ)\) is normalized in the range of 0 to 1. The highest value of \(E(PQ)\) gives the strong probability of data reception rate over the link from the source cluster head node to a neighbor's cluster head. Accordingly, such a node has a high chance for the selection of cluster head as a forwarder rather than low \(E(PQ)\) valued node. Finally, based on the neighbor information, the SEFR protocol computes the weighted value \(W(i)\) as given in Equation (5). \[W(i)=\ \max\ (\alpha*E\ +\ \beta\ *\ \left(\frac{1}{D_{i,t}}\right)\ +\ \gamma\ *\ E(PQ)) \tag{5}\]  
\(\alpha\), \(\beta\), and \(\gamma\) are weighting constants and each value contributes to an equal percentage for energy, distance to fog node, and packet queuing parameters. Thus, their summation \(\alpha+\beta+\gamma\) is set to 1. In SEFR protocol, the highest weighted neighbor cluster head indicates the optimal choice for selecting it as a next-hop. When clusters' data is received by the fog node and it falls closer to the cloud server then the direct transmission is generated. Otherwise, the SEFR protocol adopts the same criteria to identify the appropriate subsequent fog node for data routing, until the clusters' data is received on the cloud server. After the accomplishment of energy-efficient and reliable routing, the next phase of the SEFR protocol is data security. It presents a lightweight security scheme for authentication, confidentiality, and integrity of data transmission from the cluster heads to fog nodes and from fog nodes to a cloud server, which is computationally efficient and low on memory. In this scheme, the fog nodes generate the secret keys for each cluster head using the Pseudo-Random Number Generator (PRNG) of the Blum Blum Shub algorithm [34]. The proposed security scheme exploits a bitwise operation exclusive-OR (XOR), which integrates both data bits and random secret keys. The random keys for each cluster head \(n_{i}\) are generated using the Blum Blum Shub algorithm as given by Equation (6). \[K_{n+1}=K_{n}^{2}mod\ W \tag{6}\]  
In Equation (6), \(K_{n}\) is the generated secret random value for cluster head \(n_{i}\), \(W=pq\) is the product of two large prime numbers \(p\) and \(q\), \(K_{0}\) is the seed integer value that is co-prime to \(W\). Afterward when the cluster head \(n_{i}\) send data \(m_{i}\) to the cluster head \(n_{j}\), it is encrypted using Equation (7). \[E_{j}(m_{i})=m_{i}\oplus K_{i} \tag{7}\]  
Here, \(\oplus\) denotes the XOR operation of data \(m_{i}\) from the cluster head \(n_{i}\) towards cluster head \(n_{j}\). The encrypted data \(E_{j}(m_{i})\) is further transmitted towards the fog node, which can decrypt it by taking the XOR with the key \(K_{i}\) as given in Equation (8). \[D_{j}(m_{i})=E_{j}(m_{i})\oplus K_{i} \tag{8}\]  
The SEFR protocol makes use of a public key-based cryptographic scheme for data protection and security between the fog node and the cloud server. In this scheme, the fog node and the cloud server use pairs of public \(K_{p}\) and private keys \(K_{u}\) for the encryption and decryption of data transmissions. The keys are generated using an RSA algorithm due to its strength and simplicity [35]. It first selects two large primes P1 and P2, computes their product n, then computes the \(\lambda(\text{n})\) (Carmichael totient of the prime product n). Similarly, any coprime e to \(\lambda(\text{n})\) is selected with 1 < e < \(\lambda(\text{n})\) and \(\gcd(e,\lambda(n))\), thus making a public key of (e, n) and a private key pair (d, n), where d is the modular inverse of e and it is determined using \(d\equiv e^{-1}(mod\,\lambda(\text{n}))\).