# A Secure Protocol against Selfish and Pollution Attacker Misbehavior in Clustered WSNs
## 4 Design and Implementation
### DoS and Pollution Attacks Prevention
#### 4.2.1 Encoding and Parts Creation at Source Node  
The encoding process is conducted by each source node \(N_{i}\) in the network. Each node is pre-equipped with its specific unique matrix. \(A_{i}=[a_{k,q}]\) of large size (\(l\times L\)) denotes the pre-loaded matrix, where \(a_{k}^{q}\) are \(A_{i}\)'s integer coefficients, \(k\in\llbracket 1,l\rrbracket\) and \(q\in\llbracket 1,L\rrbracket\), chosen so that each combination of \(l\) columns forms an invertible matrix of size (\(l\times l\)).  
When measuring events, a node \(N_{i}\) registers the measurements in its memory. After \(t\) periods, it has collected \(t\) parts of these measurements in a vector of size \(t\), \(X_{i}=(x_{i,1},x_{i,2},\ldots,x_{i,t})\), forming a data message of size \(t\). Then, \(N_{i}\) randomly chooses a number \(p_{i}\) such that \(l\leq p_{i}\leq l\times L\) and pinpoints the coefficient of \(A_{i}\) having the position \(p_{i}\). Next, it extracts a sub-matrix \(A_{i}^{\prime}=[a_{k,q}]\) of size \(t\times p\) starting at the indicated \(p_{i}\) position in the matrix \(A_{i}\), where \(k\in\llbracket 1,t\rrbracket\) and \(q\in\llbracket 1,p\rrbracket\). Figure 4 shows the process of extraction of the sub-matrix \(A_{i}^{\prime}\) used to generate the coded packets.  
Next, \(N_{i}\) calculates \(p\) different parts from the \(t\) parts of the vector \(X_{i}\). These parts are the elements of the vector \(M_{i}=(m_{i,1},m_{i,2},\ldots,m_{i,p})\) of size \(p\) composed as follows: \(M_{i}=A_{i}^{\prime}\times X_{i}\): More specifically,  
\[S_{1}=\left\{\begin{array}{rl}m_{i,1}=&a_{1,1}x_{i,1}+a_{1,2}x_{i,2}+\ldots+ a_{1,t}x_{i,t}\\ m_{i,2}=&a_{2,1}x_{i,1}+a_{2,2}x_{i,2}+\ldots+a_{2,t}x_{i,t}\\ \vdots=&\vdots\\ m_{i,p}=&a_{p,1}x_{i,1}+a_{p,2}x_{i,2}+\ldots+a_{p,t}x_{i,t}\end{array}\right. \tag{4}\]  
The value \(p_{i}\), required to decode data at the \(BS\), is attached to each data packet to be sent through a distinct \(CH\).  
After encoding the original data parts, \(N_{i}\) transmits these parts along the \(p\) distinct \(CH\)s, chosen at the beginning of the protocol. More specifically, \(N_{i}\) sends the packets (\(Id_{i}\), \(m_{i,p}\), \(p_{i}\)), where \(j\in\llbracket 1,p\rrbracket\), through the nearest \(p\) different \(CH\)s saved in its routing table, where \(Id_{i}\) is the node \(N_{i}\) identity and \(p_{i}\) is the random number chosen for conducting the network coding phase of \(N_{i}\). Next, each \(CH\) forwards the received packets from multiple source nodes to the \(BS\).  
Each node \(N_{i}\) has \(p\) distinct paths to the \(BS\) through \(p\) different \(CH\)s. The scheme tolerates \((p-t-1)\) of them being compromised. In other words, the coalition of misbehaving \(CH\)s involved in the attack here is assumed to be of a size of maximum \(K\) with \(K=p-t-1\) to ensure complete reception of all source nodes messages. In fact, the source node will have at least \(t+1\) parts received in the \(BS\), to be sure it can rebuild the original measures. Beyond the threshold \(K\), the performance of the protocol in term of availability and integrity decreases.