A Framework of Deploying Blockchain in Wireless Sensor Networks

The most critical needs for wireless sensor networks (WSNs) are security, privacy, dependability


INTRODUCTION
The application of wireless sensor networks is becoming more and more popular and important in people's lives. However, they still have a lot of remaining problems related to energy [1][2][3], communications [4,5], data collection and processing [6,7], and especially security issues [8,9]. Security issues in the network play a role in determining the success or failure of the application. In the wireless sensor network model, the nodes can be deployed in many different ways, which can be random or according to a precomputed model. Regardless of how they are deployed, sensor nodes always interact with their surroundings intimately. In addition, these sensor nodes operate completely freely, meaning that they are not controlled or monitored by any monitoring centers. Therefore, they are very vulnerable to hackers. There are many ways for hackers to attack the network such as attack from outside, or attack from inside the network [10][11][12]. In addition, just a small mistake in the network deployment can also create an opportunity for hackers to attack the network [13,14]. Besides, it is difficult to implement complex algorithms in sensor networks due to limitations of the network such as computational capacity and small capacity. Meanwhile, synchronizing the entire system is very difficult because different network architectures are deployed for different applications. When designing wireless sensor networks, we all have common security requirements as outlined in the paper [15][16][17].
Blockchain is one of the most modern security and data storage technologies available today. Blockchain is a distributed database that stores all the transactions that have been made in all participating devices in the networks [18,19]. Blockchain keeps track of transactions and communications as blocks in an electronic ledger. Additionally, blockchain is maintained by 1

EAI Endorsed Transactions on Industrial Networks and Intelligent Systems
Research Article EAI Endorsed Transactions on Industrial Networks and Intelligent Systems 06 2022 -08 2022 | Volume 9 | Issue 32 | e3 all users of the network, and messages are broadcast to all users for authentication. Multiple messages are gathered together to form a block and added to the blockchain if they pass the authentication test. A transaction is considered successful when it is agreed upon by all network nodes participating in the network. All transactions that have been done, they will be stored in the blockchain and the information of any transaction cannot be modified. This is one of the most advantageous features of blockchain technology to ensure data security. In paper [20] presents the use of blockchain technology of Bitcoin. In paper [21,22] the author proposes a data storage architecture for wireless sensor networks using blockchain technology. This architecture has two functions, the first is to store the data of the nodes, the second is to control the data access. In addition, in paper [23] the author proposed a reliable routing method and improved routing performance by combining the deep blockchain and Markov decision process.
Thanks to the integration of blockchain technology, the security and data storage capacity of not only mobile ad-hoc networks, flying ad-hoc networks, but also wireless sensor networks are much improved. Other sensing operations may be made more safe, autonomous, adaptable, and potentially lucrative by combining blockchain technology with other distributed systems, such as robotic swarm systems [24,25]. In [26] the authors use a small number of nodes in the network to authenticate routing by other nodes based on the dynamic mechanism of blockchain technology in mobile ad-hoc networks [27]. In [28] the authors propose a security structure using blockchain technology for unmanned aerial vehicle (UAV) networks [29]. A large number of transactions between sensor nodes can be processed quickly, thanks to the distributed model of blockchain technology. Therefore, the cost of operating a centralized data center will be significantly reduced. Simultaneously, computing and storage needs are shared to all devices in the network. In paper [30,31] the authors proposed a wireless sensor network structure using blockchain technology. With the integration of blockchain technology, the centralized server-client model is eliminated. Because we can interact peer-to-peer, distributing data automatically between sensor devices.
The article proposes to use blockchain technology for security in wireless sensor networks. Initially implemented the SHA-256 hash algorithm. This is one of the algorithms with complexity and high security.
The rest of this paper is organized as follows. Section 2 presents the system model. The blockchain structure is shown in Section 3. Section 4 details the hash algorithm. Section 5 outlines the structure of a sensor network based on blockchain technology. The simulation results are presented in Section 6. Section 7 provides conclusions and future work .

System model
The authentication processes and security mechanisms in the network are proposed in this paper. The sensor network topology consists of sensor nodes (SNs), cluster heads (CHs), base stations (BSs) and end-users as shown in Figure 1.

Plain text
Cipher text Private key Public key  Sensor nodes have common characteristics of computing power and storage space. Data will be sent from sensor nodes to female cluster heads, which have the higher computing power and storage space. The private blockchain is implemented on cluster heads. The public blockchain is implemented on base stations.
All nodes in the network are initialized by the base station. The registration process is fully implemented in the steps of initialization, registration, and authentication. The public keys and private keys of the base station, cluster heads, and sensor nodes are generated by the base station. These keys are generated to verify the integrity of the data. Each node has a unique address. Smart contracts are deployed on public blockchains. The cluster heads use these smart contracts to perform the registration process. Whether the clusters already exist or not is verified by these smart contracts. The MAC address of the cluster heads will be checked for correctness and validity. After successful authentication, the ID (identity) of the cluster heads will be recorded in the public blockchain. An error message is sent back if the other verification failed. Meanwhile, the registration process of sensor nodes will be done on the private blockchain. After successful registration, sensor nodes will be allowed to take part in the network. The registration process of sensor nodes and clusters heads is done in the same way. After deploying the sensor nodes will connect to their cluster heads.
There are two common forms of attack on wireless sensor networks, which are external attacks and internal attacks. Thanks to the registration and authentication of nodes, external attacks are reduced because hackers are not allowed to enter the network. 2 EAI Endorsed Transactions on Industrial Networks and Intelligent Systems 06 2022 -08 2022 | Volume 9 | Issue 32 | e3

Blockchain Structure
The term "Blockchain" refers to a technology that includes: a growing list of data structures, called blocks, connected and secured by cryptography. Blockchain technologies allow the secure transmission of data based on an extremely complex encryption system. It is similar to a company's ledger, where money is closely monitored and every peer-to-peer record is recorded. Each block contains information about the time of creation and is associated with the previous block, accompanied by a time code and transaction data. Once the data is accepted by the network, there is no way to change it. Blockchain is designed to combat fraud and alteration of data.
Each transaction is stored in a block, which is linked together into a chain. Each block contains the current block header, previous block address, transaction execution time, random number (nonce), and value of the current block. The data quantity and data details obtained are mainly contained in the block body. The data in the blockchain is permanently recorded and can be accessed at any time after that. Thanks to the use of digital signatures of the Merkle tree, it is guaranteed that the data obtained is never confused and duplicated. In blocks, the Mekle-root value is unique because the received data is processed by the Merkle-tree hash function. Figure 2 shows the structure of the blockchain.  Public blockchain: In a public blockchain, all the records are visible to the public, and everyone is allowed to take part in the consensus process. Public blockchain have the highest immutability as compared to the other two types since the number of participants is very high. However, public blockchain have lower efficiency as compared to private and consortium blockchain.

Figure 3. Types of keys in blockchain
Private blockchain: In a private blockchain, only those nodes which come from one specific organization are allowed to join the network and the consensus process, it means it has a permission consensus process. It is also regarded as a centralized network since it is fully under the control of one organization. Such networks have high efficiency but can be tampered with relatively easily as compared to public blockchain because of the lesser number of participants.
Consortium blockchain: A consortium blockchain also has a permission consensus process, but unlike a private blockchain, only a few selected organizations can participate in it. Therefore, it is a partially decentralized system. It also has high efficiency but can be tampered with relatively easily as compared to public blockchain.

Information in each block
The data structure within a block of blockchain is shown in Figure 4. The data in each block depends on the type of blockchain, for example, bitcoin's blockchain contains information about transactions such as the sender, receiver information, and the number of bitcoins traded. The health insurance blockchain will store information about the insured, health history of that person.
Each block has a hash code that identifies a block and the data in it. This code is unique, it is similar to 3 EAI Endorsed Transactions on Industrial Networks and Intelligent Systems 06 2022 -08 2022 | Volume 9 | Issue 32 | e3 a fingerprint. Any change in the block changes the hash code.
The hash of the previous block (which is the code of the previous block) will form the chain. Any change to one block will cause the next blocks to be inconsistent.

Mechanism of action
Blockchain technology is a ledger that records and stores information on transactions. Blockchain, it can be said that this is a database organized into a chain of information blocks, allowing it to grow and expand over time, whenever new data is available, a new block is added. When each block is loaded into the ledger, it is linked to the previous block using their function hashes. This forms a completely trackable and nontamper-proof record in the blockchain.  We look at the Figure 5. The first block is the starting block, when the information on the second block is changed, the third block and the following blocks will no longer match, or in other words, the links by the matching hash code will be wrong. The second block has information changed, the hash of the block will change according to (second block changed hash from 6BQ1 to H62Y). So the link between second block and third block by hash code 6BQ1 is incompatible. With this design, blockchain helps prevent data changes. In principle, once data has been written to the blockchain, there is no way to change that data. Blockchain has used a consensus algorithm, in which there are two commonly implemented consensus algorithms: Proof of Work Algorithm (PoW) and Proof of Stake Algorithm (PoS). Proof of Work (PoW): PoW's mechanism is to slow down the process of forming new blocks. In the case of Bitcoin, for example, it takes about 10 minutes to compute the proof of work, after which a new block is added. With this PoW mechanism, tampering seems impossible, because when changing the data of one block, the tamper interferer will have to recalculate all the proof of work of the next blocks. Each block to recalculate takes at least 10 minutes, with hundreds or thousands of blocks, it will take a lot of time.
Proof of Stake (PoS): in contrast to PoW, the PoS algorithm is another way to verify transactions. With PoS, new block creators are selected at random, based on the value of their stakes. This person is responsible for validating the new blocks. To become a validator, this person has to deposit a certain amount (which is a stake, and this will be lost if this person validates a fraudulent transaction) and the validator can only deposit a certain amount of money exploit. Upon successful confirmation of a block, validators will receive a reward of the associated fees of the respective transactions in that block. If this person does not want to continue as a validator, after a certain period of time to authenticate this person does not make any fake claims, their shares and earnings will be refunded. As such, it will take time and money to perform a validation of a fake block.

The cryptography hash function SHA-256
In the SHA-2 family of hash functions, we have four standard lengths of 224, 256, 384 and 521 bits. That is, we have SHA-224, SHA-256, SHA-384, and SHA-512. A detailed description of the SHA-256 algorithm is presented in paper [32] , The value of the hash is calculated as follows: where H l is a 256-bit string of values; CF(M l , H l ) is a compression function. CF(M l , H l ) has two parts the message expansion and the state update transformation [33].  where is the representation for XOR, ≫ is representing the left rotation, ≫ represents left shift. Figure 6 shows the change of steps i. The functions T i , Ch(x, y, z),Maj(x, y, z), 0 and 1 are given by the following formula: When using the SHA-256 algorithm to generate secure keys for transactions in the blockchain, we have the results as shown in Figure 7. In the Figure 7, we see that every time a transaction is made, the system will generate and save all the information related to the transaction. Every transaction we create fields "data", "index", "timestamp", "previoushash", "currenthash", "nounce". These fields have codes that follow the working principle of blockchain technology as outlined and analyzed above.

Blockchain-Based WSNs Structure
Thanks to the feature of not being able to record data, data transmission on the blockchain is highly secure. This is one of the strengths of WSNs when using this security technology. Sensor devices such as sensor 1, 2, 3, 4, 5, 6 are connected to a block in the chain as shown in Figure 8. Besides, in addition to collecting and storing data of its sensor devices , then these blocks also collect data from the sensors of other blocks. As such, all blocks hold their own sensor data and the sensor data of other blocks as well. There is no central block. The blocks in the chain are linked together as shown in Figure 9. Fields like previous hash, current hash are all contained in a block. The codes generated by hash functions are very long strings of words, but for simplicity we assume the form of a code snippet consisting of numbers and letters as shown in Figure 9.   Figure 10 shows a block in the chain that contains an incorrect code in the previous hash field, namely that the code has changed from "24chyl" to "23th01" in the 3rd block. When this condition occurs will result in an error link. The system will close the link and close the data transmission on that blockchain immediately. The system will then take some time to wait for the chaining connection to complete with the aim to ensure that the data in each cluster is fully uploaded.

Simulation Results
In this paper we use SHA-256. SHA-256 is the latest hashing algorithm of the secure hashing algorithm (SHA) family. The SHA256 algorithm will generate a 256-bit hash value for every message regardless of the message length. The generated 256-bit hash 5 EAI Endorsed Transactions on Industrial Networks and Intelligent Systems 06 2022 -08 2022 | Volume 9 | Issue 32 | e3  We assume 4 transactions, then each transaction will correspond to a block as shown in Figure 11. Each block in this study contains the block number, sensor data, and a hash value of the previous block, as shown in Figure 11. Also, each block is the basic unit of the blockchain. The message content of each block includes block number, nonce value, data sensor, previous hash, current hash. When the first block of chain is generated, the hash value of the previous block is set to zero as shown in Figure 11. When the operator presses the mine button, the system starts the mining process. The hash value of this block is the hash value generated by our system. When a new block is added, it stores the code in the current hash field of the previous block in the previous hash field of the following block. Therefore, the blocks in the chain will be linked together as shown in Figure 11. When a block in the chain is changed, all the blocks in the chain will not be data homogenous and give an error as shown in Figure 12. We change the data of sensor 6 from 24.5 0 C to 25.5 0 C in the 2 nd block, we see all the following blocks will give an error because when reusing the data the system will save the   Figure 12. Result when have change data modification and generate a different code. If we agree to this data change, we will have to update all the hashes in the blocks to get a homogenous data chain as shown in Figure 13. In addition, the sensor network data is distributed across many peer devices as shown in the Figure 14. When the data is repaired, we must correct it on all devices if we want to have a consensus block in the chain. Therefore, data tampering is not possible because it takes too much time.
As shown in the Figure 14, the same blocks in different peers will store the same information. It means that data is stored in many peer devices in the network. When there is a forgery or change of data it causes inconsistency in a chain and inconsistency among peers.

Conclusion and future work
Integrating blockchain technology to solve the security problem of applications has many benefits. It also can prevent the risk of data tampering due to the use of a decentralized consensus mechanism. We provide the model the security system to support WSNs. In addition, scenarios are provided including simulation results to process data for security purposes. The simulation results show some promising points. The data of a sensor node can be stored in many devices in the network and has transaction time. Hence, it becomes much more difficult to change or tamper with the data.
In the design of a wireless sensor network that integrates blockchain technology, we would to pay attention to the cause of increased data transmission latency when integrating this security technology. Because the validation process is conducted over many steps. Therefore, we need to increase processing speed, reduce packet sizes, and also deploy new algorithms in the transaction confirmation process.