Embedded System design with implementation of a

3 downloads 0 Views 8MB Size Report
Mar 7, 2018 - Its battery has an autonomy of 4400mAh to power a USB output ...... xABD388F0 , 0 x6A51A0D2 , 0 xD8542F68 , 0 x960FA728 , 0 xAB5133A3 ,.
Embedded System design with implementation of a cryptographic algorithm for the development of Nadjibi’s Pay As You Go platform Audrey Jean-Martial KAKPOHOUE ([email protected]) African Institute for Mathematical Sciences (AIMS) Senegal Supervised by: Mr Julien POTRON and Dr Amadou Lamine TOURE Nadjibi, AIMS, Senegal

March 7, 2018 Submitted in Partial Fulfillment of a Masters at AIMS

Acknowledgements First of all, I would like to thank the Almighty GOD, who have been with me till the achievement of this project. I would like to express full gratitude to Mr. Julien POTRON, for giving me the opportunity to do my internship at Nadji.bi. I would like to thank Dr Amadou Lamine TOURE, I am grateful indebted to his analysis,guidance and advices during this research. Many thanks to Mr. Ignace MINLEND for his insightful comments and suggestions. I am deeply gratfully to Dr. Th´eophile HOUNGAN and Dr. Medesu SOGBOHOSSOU at University of Abomey-Calavi for their recommendations and valuable words of advice. Special thanks to my AIMS-Senegal classmates. My heartfelt appreciation goes to my entire family and friends for their words of encouragement. My deep gratitude to Dr. Aissa WADE and all AIMS Staff, for their support.

i

Abstract About 30% of african population have access to electricity, according to the International Energy Agency in 2014. The majority of people turn to alternatives like kerosene, candles which are risky and costly. Few companies have developed solar kits solutions which are better solutions. However, the cost of accessibility to these kits is still a boundary to mostly poor families. To facilitate the acquisition of its kits, Nadjibi found that it is advantageous to develop its own platform Pay As You Go (PAYG). It is in this context that we worked on an Embedded System that will be integrated into the solar kits. This system makes it possible to compel the customer to series of payment to refund the price of the solar product. In order for the Embedded System to perform the functions assigned to it, we chose the hardware components and wrote the program for the software. Also, we implemented Blowfish, a symmetric cryptography algorithm to ensure the secure transmission of information. Keywords: Solar Energy, Solar Kit, Embedded System, Cryptography.

R´ esum´ e (French Language) En 2014, environ 30% de la population africaine a acc`es `a l’´electricit´e, selon l’Agence Internationale de l’Energie. La majeur partie de la population se tourne alors vers des solutions comme le k´eros`ene, des bougies qui pr´esentent des risques et sont coˆ uteuses. Certaines entreprises ont d´evelopp´e des kits solaires qui sont de meilleures solutions. Cependant, le coˆ ut d’achat des kits est toujours une limite pour leur adoption par la plupart des familles pauvres. Afin de faciliter l’acquisition de ses kits, Nadjibi a constat´e qu’il est avantageux de d´evelopper sa propre plateforme Pay as you go (PAYG). C’est dans ce contexte que nous avons travaill´e sur un Syst`eme Embarqu´e qui sera int´egr´e dans les kits solaires. Ce syst`eme permet de contraindre le client `ades s´eries de payment pour rembourser le prix du kit. Pour que le Syst`eme Embarqu´e puisse ex´ecuter les fonctions qui lui sont d´efinies, nous avons choisi les composants du hardware et ´ecrit le programme du software. Nous avons ´egalement impl´ement´e l’algorithme de cryptographie sym´etrique Blowfish pour assurer la transmission s´ecuris´ee de l’information. Mots cl´ es : Energie solaire, Kit solaire, Syst`eme Embarqu´e, Cryptographie.

Declaration I, the undersigned, hereby declare that the work contained in this essay is my original work, and that any work done by others or by myself previously has been acknowledged and referenced accordingly.

Audrey Jean-Martial KAKPOHOUE, March 7, 2018 ii

Contents List of Figures

v

List of Tables

vi

List of Abbreviations

vii

Introduction

1

1 PAYG in the solar kits market

2

1.1

Solar devices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

2

1.2

Definition of PAYG . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

3

1.3

Benefits of PAYG . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

4

1.4

Business Models of PAYG . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

4

1.5

Technologies in solar kits for the PAYG system

. . . . . . . . . . . . . . . . . . . . . .

5

1.6

Payment systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

5

1.7

Current Market Stage of PAYG . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

6

1.8

Securing a PAYG system . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

6

1.9

Operating of Nadjibi’s Pay As You Go concept . . . . . . . . . . . . . . . . . . . . . . .

7

2 Cryptography

9

2.1

Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

9

2.2

Objectives of Cryptography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

9

2.3

Kerckhoff’s Principle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

2.4

Type of Cryptographic System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

2.5

Comparison of Symmetric and Asymmetric Cryptographic Systems . . . . . . . . . . . . 11

2.6

Symmetric cryptography algorithms

2.7

Comparison of symmetric cryptography algorithms . . . . . . . . . . . . . . . . . . . . . 13

2.8

Blowfish algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

. . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

3 Design of the Embedded System

18

3.1

Definition and applications of Embedded Systems . . . . . . . . . . . . . . . . . . . . . 18

3.2

Functions and constitutions of our Embedded System . . . . . . . . . . . . . . . . . . . 18

iii

3.3

Embedded System components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

3.4

Communication modes of the Embedded System components . . . . . . . . . . . . . . . 21

3.5

Choice elements of the system . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22

3.6

Programming microcontroller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

3.7

Experiments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31

Conclusion and Prospects

35

A Text Program (Code acquisition by Remote Control, Decryption algorithm and Communication with RTC) 36 References

49

List of Figures 1.1

Solar Kit Lion v.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

2

1.2

Solar Kit Zebu v.1 F. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

3

1.3

Pay As you Go Process. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

8

2.1

Symmetric Cryptography Process. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

2.2

Asymmetric Cryptography Process. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

2.3

Blowfish encryption and decryption algorithm. . . . . . . . . . . . . . . . . . . . . . . . 16

2.4

F-function of Blowfish. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

3.1

Overview of the Embedded System. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

3.2

NEC Protocol Transmission. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22

3.3

Electronic schematic of the Embedded System. . . . . . . . . . . . . . . . . . . . . . . 23

3.4

Flowchart of the program in the microcontroller. . . . . . . . . . . . . . . . . . . . . . . 26

3.5

Flowchart program for getting button pressed on the remote control. . . . . . . . . . . . 28

3.6

Flowchart Blowfish decryption algorithm. . . . . . . . . . . . . . . . . . . . . . . . . . . 29

3.7

Simulation of the switching block for a control voltage of 5V. . . . . . . . . . . . . . . . 30

3.8

simulation of the switching block for a control voltage of 0V. . . . . . . . . . . . . . . . 30

3.9

Microcontroller PIC16F1825. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31

3.10 Remote and innfrared receiver. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 3.11 RTC DS1307. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 3.12 LED. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 3.13 Seven-segment display. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 3.14 Breadboard. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 3.15 Pickit3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 3.16 Electronic assembly. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 3.17 Display of the pressed digit. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 3.18 Ignition of the LED indicator. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34

v

List of Tables 1.1

Companies providing solar systems using the PAYG concept . . . . . . . . . . . . . . . .

6

1.2

The building blocks for a PAYG system

7

2.1

Difference between symmetric and asymmetric cryptography . . . . . . . . . . . . . . . 11

2.2

Comparison popular symmetric cryptographic algorithms . . . . . . . . . . . . . . . . . 14

. . . . . . . . . . . . . . . . . . . . . . . . . .

vi

List of Abbreviations PAYG: Pay As You Go DESCO: Distributed Energy Service Companies OEC: Off-grid Energy Companies B2B: Business to Business RSA: Rivest Shamir Adleman DES: Data Encryption Standard RC5: Rivest’s Cipher 5 IDEA: International Data Encryption Algorithm AES: Advanced Encryption Standard EAGLE: Easily Applicable Graphics Layout Editor PCB: Printed Circuit Board EEPROM: Electrically-Erasable Programmable Read-Only Memory RTC: Real Time Clock PIC: Programmable Interface Controller MOSFET: Metal Oxide Semiconductor Field Effect Transistor RAM: Random Access Memory ROM: Read Only Memory XOR: exclusive-or-operation SPI: Serial Peripheral Interface I2C: Inter-Integrated Circuit

vii

Introduction Access to electrical energy is an essential component of individual development through the improvement of living conditions (education, security, development of economic activity, modernization of communication tools). Unfortunately, in Africa very few people benefit from the advantages of electricity. According to the International Energy Agency, in 2014, in sub-Saharan Africa, only 290 million over 915 million people have access to electricity and the total number without access is rising [1]. Without access to electricity, people turn to costly and unhealthy energy alternatives which present risks of fire. These energy alternatives are kerosene and candles for lighting, disposable lead-acid batteries for torches or diesel to run generators. To solve this problem of access to electricity, companies have developed solar solutions. These solutions are portable kits consisting of PV (PhotoVoltaic) modules, batteries, a charge controller, wiring and sockets for applications such as lighting, telephone charging, radio, television and fan. The kits are adapted to regions not connected to the electricity grid and have the advantages over energy alternatives of being: renewable energy (solar), secure and safe for health. However, the cost of these kits is an obstacle to their accessibility and mass adoption by the population in need. This is justified by the low purchasing power of this population which is unable to buy them by a cash payment. It is therefore necessary, to set up a payment mechanism, that takes into account the financial resources of this population in order to facilitate the acquisition of the kits. The Pay As You Go (PAYG) is a payment mechanism that allows the customer to amortize the purchase cost of the kit by reimbursing it, according to its resources, progressively and remotely. This allows the distributor to massively distribute the kits, regardless of their cost and to recover the fees remotely. With the Pay As You Go, the population without access to electricity can spend approximatively in the solar kits, the money spent in energy alternatives, for the frequent purchase of fuel, batteries for torches or candle. Several companies have developed platforms for the PAYG concept. Solar energy companies use these platforms to equip their solar kits with the PAYG solution. In doing so, solar companies are increasing the cost of kits as they have to pay for the use of the platforms. Also, since they do not manage the platform themselves, they can not easily and quickly change it to meet the needs of their customers. Nadjibi is a solar energy company that develops and manufactures solar kits. In order to facilitate the acquisition of its kits, Nadjibi wants to integrate the PAYG solution by bulding its own platform. As a manufacturer, Nadjibi is looking to reduce the cost of its kits and to be able to easily and quickly modify the platform PAYG to adapt it to the needs of the customers. A PAYG platform uses a system to guarantee the payment of fees by the customer. This system is to lock the use of the kit when the payment is not made and to unlock when it is done. In this work, we present the design of an electronic system that will compel the customer to repay. The system contains a software part where the operation has been programmed and a physical part where the components we have chosen react to perform the functions. This thesis is organized as follows: At first, we describe the concept behind a PAYG system. Then we will review how to secure such a system with a cryptographic algorithm. After these background aspects, we will describe the implementation of both the Embedded System and the cryptography. Finally, we will close with discussion about applicabilities and possible extension.

1

1. PAYG in the solar kits market This chapter talks about PAYG and solar kits. We will first introduce the solar kits, then we will talk about the PAYG systems used on these kits and we will finish with the company’s PAYG approach.

1.1

Solar devices

The solar products most often concerned by the PAYG system can be classified into three categories: solar lanterns, pico solar and solar home system. In the following lines we present these different types of solar products. 1.1.1 Solar lanterns [2]. They include a fluorescent or LED light, a rechargeable battery and a solar panel. These are portable devices, easy to carry and designed for lighting needs. Panels used in solar lanterns typically have a power of about 3 Watt-peak (Wp) and serve to charge the batteries by converting solar energy into electrical energy. Some solar lanterns include a small built-in solar panel, others are designed to be plugged into a roof-mounted. The batteries used are often sized to provide at least 4 to 6 hours of lighting per day. A majority of solar devices on the market today use lithium-based batteries (lithium-ion, lithium ferrous phosphate). These batteries can be rapidly charged, perform well in extreme conditions, have a high energy density, ability to store more energy per unit weight/size and a longer life than most other types of batteries. Solar lanterns can be an excellent replacement for kerosene lanterns. The company Nadjibi manufactures solar lanterns named Lion v.1. The Lion v.1 has a solar panel with a power of 3.6 Wp, designed to be plugged into a roof-mounted. Its battery has an autonomy of 4400mAh to power a USB output for mobile phone charging and leds for a brightness of 120 lumens. (See Figure 1.1 for a schematic)

Figure 1.1: Solar Kit Lion v.1. 1.1.2 Pico Solar Systems [2]. Solar lanterns are designed as a single portable light and do not offer the ability to light multiple 2

Section 1.2. Definition of PAYG

Page 3

rooms/areas simultaneously, and they are often unable to charge phones. However solar pico systems offer the ability to simultaneously light 2 to 6 rooms. They often include the ability to charge multiple mobile phones at the same time and can also power small DC appliances, such as a radio. These products come with an external solar panel of 4 to 25 Wp in size, and they often use lithium-based battery technologies. Solar pico systems power output of 1 to 10 W. The flagship product of Nadjibi is a pico solar named Zebu v.1 F. It can power 3 LED bulbs with a brightness of 90 lumens each. The Zebu v.1 F. also has a USB port for mobile phone charging. Its panel has a power of 6 Wp and the battery an autonomy of 8800mAh. (See Figure 1.2)

Figure 1.2: Solar Kit Zebu v.1 F. 1.1.3 Large Solar Home Systems [2]. Larger solar home systems can be thought of as products with a solar panel above 30 Wp in size and often use deep-cycle, lead-based batteries that are more dependable and less expensive (cost-per-watt) than lithium-based alternatives used in solar lanterns and pico systems. These products offer energy consumers the ability to use up to 10 lights (LED or compact fluorescent) simultaneously and to use a wide range of 12V DC appliances, such as powering televisions and direct-to-home satellite systems, fans, refrigerators, computers. Higher-end solar home systems can be sold with an inverter that converts the DC output of the solar panel into alternating current (AC), allowing consumers the ability to use many off-the-shelf 220-240V AC appliances. Sale of kits is facilitated by PAYG.

1.2

Definition of PAYG

The PAYG is a pioneering, game-changing digital credit system used by the solar companies to remove the initial financial barrier to solar energy access by allowing consumers after the purchase of the solar system to repay the total price through a series of payments (weekly, or monthly...) of small amount rather than paying upfront for the entire solar system. PAYG has its origin in Sub-Saharan Africa. First PAYG approaches are tested in East Africa [3, 4]. This system has an impact on access to electricity, through the distribution of solar devices and improves the profit of companies.

Section 1.3. Benefits of PAYG

1.3

Page 4

Benefits of PAYG

From the consumer’s perspective, PAYG is a game-changer and far more than a financing solution added to a solar kit. It is obvious that reducing the upfront cost and shifting it towards instalments allows customers to gain access to systems that they could otherwise not afford. PAYG lowers the threshold for poor households, allowing them to benefit from cheaper and more useful energy in the house. But that is not the only advantage for the customer. PAYG has a positive impact on family finances, prevents health dangers associated with kerosene lamps, and makes the environment feel more secure. PAYG has advantages for both the customer and the distributor. It allows the distributor to: - reach a larger number of customers; - facilitate the acquisition of its kits by customers; - deploy costly solutions to the market that can be acquired even by low-income people; - collect money remotely without having to move. Since the inception of PAYG, companies adopted different business models of the system.

1.4

Business Models of PAYG

Based on available data in 2014, there are at least 25 companies actively deploying a PAYG solar solution across Africa, Asia, and Latin America [2]. The companies active in this business are often called Distributed Energy Service Companies (DESCOs) or Off-grid Energy Companies (OECs). Companies involved in PAYG are grouped into three categories [5]: 1. distributed energy service companies (DESCO): they provide a given level of energy service in exchange for ongoing payments. 2. microloan providers: they offer rent-to-own models. 3. business-to-business (B2B) intermediaries: supplying hardware and software support for PAYG. Considering DESCOs and microcredit providers we can distinguish two business models. Firstly, there are companies that do not sell the system itself, but only the power generated by the systems (energy-as-a-service), and furthermore ensure the correct operation and maintenance of the systems. This results usually in one-time installation costs as well as weekly or monthly payments. On the other hand, companies selling the systems using PAYG concepts with rent to own models (ownership models). Again, a purchase price usually has to be paid in the beginning and a customizable monthly/weekly/daily fee is applied if the user wants to use the system. In the ownership model, the legal ownership of the asset typically lies with the PAYG company until the customer has completed the leasing term (repayment of the total price of the solar system price) and ”earns” the right to own the system. In both models, the system is blocked automatically if the daily, weekly or monthly fee is not provided and cannot be used again until credit has been purchased. Companies, use different technologies in their solar kits for the PAYG system.

Section 1.5. Technologies in solar kits for the PAYG system

1.5

Page 5

Technologies in solar kits for the PAYG system

There are three kinds of technology used by the companies in their solar devices in order to perform the PAYG system. We have [6]: solar kits on-connected, solar kits with a periodic connection with smartphones and solar kits off-connected. 1. Solar kits on-connected, include a component embedded allowing bidirectional communication with central servers. These kits support remote lock and unlock capability, but can be used also for operations and performance data transfer. 2. Some solar kits utilize a periodic connection with smartphones. Through this connection a verification of payment is done in order to unlock. In this case, unlocking is accomplished by connecting the kit to a dealer’s smartphone, where a proprietary application accounted for customer credit, supplied an unlock code to the solar device, and facilitated a temporary bidirectional data transfer. All performance data can be stored on the device until an internet connection is established, at this point it is downloaded from the device and sent to the central office via cellular network. 3. Solar kits off-connected, include an onboard microcontroller which has lock and unlock capabilities. These kits don’t have data transfer. In this case, the ”unblock kit” information can be transmitted to the microcontroller by: code or audio signal. Whatever the technology used, the payments can be made through mobile money or scratchcard.

1.6

Payment systems

In PAYG, the payment systems are: scratchcards and mobile payment services. 1.6.1 Payment by scratchcards. A scratchcard is a card where one or more areas contain concealed information which can be revealed by scratching off an opaque covering. It is used in prepaid services. In the PAYG system using this card, the customer buys a scratchcard and sends the hidden code to the company as proof of payment. With the information received, the company ensures the effectiveness of the payment and sends the instructions for the unlocking of the kit (code, audio signal, remote unlocking). 1.6.2 Payment via mobile money [7]. Mobile money, in the strict sense of the term, refers to financial services with or without an account banking, by mobile phone, offered by mobile operators. There are three major mobile money services: ”mobile banking”, ”mobile payments” and ”mobile transfers”. • Mobile banking often confused with mobile money is only one type of that. It allows customers of a financial institution to access their accounts and to perform transfers and payments. This service is therefore only available to people who possess a formal bank account. • Mobile payment is a service allowing people without a bank account, to purchase or sell goods and services at a merchant shop/store (or remotely) using their mobile wallet through their mobile phone, instead of cash. Unbanked mobile phone users can also pay utility bills via their mobile wallet (an electronic account linked to the SIM card for unbanked people).

Section 1.7. Current Market Stage of PAYG

Page 6

• Mobile transfer is a service that allows people, without a bank account, to send or receive small amounts of money to/from any other mobile phone user across the country, from urban to remote rural areas, and across international borders. On the market, many PAYG approaches rely on mobile money systems for payment processing [6].

1.7

Current Market Stage of PAYG

In recent years, many PAYG companies appeared in many countries. The following table presents the PAYG system of some solar companies. Table 1.1: Companies providing solar systems using the PAYG concept Geographical area

Africa

Asia

1.8

Company

Model

Technology

Payment system

M-Kopa Mobisol Azuri technologies

Rent-to-own Rent-to-own

Mobile money Mobile money

Off Grid Electric Angaza Design

Energy as a service Rent-to-own

Simpa Networks

Rent-to-own

On-connected On-connected Off-connected (Activation: unlock code) Off-connected (Activation: unlock code) Off-connected (Activation: audio signal) Off-connected (Activation: unlock code)

Rent-to-own

Scratchcards

Mobile money

Mobile money

Scratchcards

Securing a PAYG system

The possibility that solar kits with PAYG technologies will be hacked is an important risk for companies, particularly, those without a deep local presence. Thus far, on the PAYG market there have been scattered but significant anecdotes of devices being hacked [6]. PAYG companies are protective about the exact technologies they have, to ensure their systems can only be used if activated by themself. From hardware to software, several tools are used to protect the PAYG platform from hacking. Some of security measures are: - The use of anti-theft screws on the kits to prevent access to their interior; - Encapsulate the electronic modules on the PCB (Printed Circuit Board) to prevent the disruption of their operation;

Section 1.9. Operating of Nadjibi’s Pay As You Go concept

Page 7

- The use of cryptography for data protection and information transfer; - Connection of the kit with a network for remote control / monitoring (use of on-connected technology); - Mobile money account protection by password. In an off-connected system with activation by unlock code, it is possible to resort to cryptography. The use of cryptography is for secure transmission of the unlock code in the form of an encrypted message. Note that any security technology can be hacked with sufficient effort. System vulnerability vary from one company to another because they do not use the same technologies or payment services.

1.9

Operating of Nadjibi’s Pay As You Go concept

Security is important in a PAYG system in which there must be a business model, a payment method, and a technology with an activation mechanism to lock and unlock the kit. A judicious choice of these elements is important for the construction of PAYG. Table. 1.2 presents their different components. Table 1.2: The building blocks for a PAYG system Model

Payment

Technology

Activation

· Rent to own · Energy as a service

· Mobile money · Scratchcards

· On-connected · Off-connected · Periodic connection

· Remotely · Unlock code · Audio signal

For his PAYG system, Nadjibi wants to use mobile banking for the payment service. Indeed the payment by mobile money like advantages on the scratchcards, is faster to deploy because of the already existing infrastructures (operators of mobile network, inclusion of financial services in the operations) and is easily accessible to many people because of the boom of the mobile phone. The business approach that the company wants to adopt is the rent to own allowing the customer to become the owner of the kit. Regarding the hardware technology of the kits, the company opts for the off-connected with the activation mode by unlock code. Off-connected technology can be deployed in areas where customers do not have a network blanket. Off-connected PAYG technology development cycle is often shorter and lower cost than on-connected. For the prepayment using mobile money platforms, customer reimburses part of the kit’s cost to Nadjibi and immediately after receiving the payment, Nadjibi uses the cellular network to send a code to the customer. This code is used to unlock the kit for a period proportional to the amount of repayment. The product technology validates the code and unlocks the kit for a given period. When the period expires, the technology locks the kit, unlocking it again when the customer inserts the code received after a repayment. After a full refund of the kit’s cost, it is unlocked automatically and for an unlimited time, then the customer becomes the owner of the kit and has no longer to pay any fees.

Section 1.9. Operating of Nadjibi’s Pay As You Go concept

Page 8

Through this process (described in Figure. 1.3), the manufacturer automates the payment method, remotely recovers costs without moving, and computerizes data collection (Payment data: number and frequency of payments, average payment size, frequency and timing of prepayments and delays/defaults). This data can be used for: targeted marketing for upgrades, historical data for lenders, credit decisions for insurances or credit decisions to extend consumer debt or refinance assets.

Figure 1.3: Pay As you Go Process. In the realization of the PAYG platform of Nadjibi, we used cryptography in our work.

2. Cryptography In this chapter, we have done a brief review of literature on cryptography. Later, we chose a cryptography algorithm and presented how we applied it.

2.1

Definitions

Cryptography: It refers to the design of mechanisms based on mathematical algorithms that provide fundamental information security services. The cryptographic process results in the ciphertext for transmission or storage. [8] Cryptanalysis: The art and science of breaking the ciphertext is known as cryptanalysis. It involves the study of cryptographic mechanism with the intention to break them. Cryptanalysis is also used during the design of the new cryptographic techniques to test their security strengths. [8] Cryptology: Science encompassing cryptography and cryptanalysis. Cryptographic system or cryptosystem: A cryptosystem is an implementation of cryptographic techniques and their accompanying infrastructure to provide information security services. A cryptosystem is also referred to as a cipher system. Cryptography concerns with the design of cryptosystems, while cryptanalysis studies the breaking of cryptosystems. Plaintext: It is the data to be protected. [8] Ciphertext or Cryptogram: It is the scrambled version of the plaintext produced by the encryption algorithm using a specific encryption key. [8] Encryption Algorithm: It is a mathematical process that produces a ciphertext for any given plaintext and encryption key. It is a cryptographic algorithm that takes plaintext and an encryption key as input and produces a ciphertext. [8] Decryption Algorithm: It is a mathematical process, that produces a unique plaintext for any given ciphertext and decryption key. It is a cryptographic algorithm that takes a ciphertext and a decryption key as input, and outputs a plaintext. The decryption algorithm essentially reverses the encryption algorithm and is thus closely related to it. [8] Encryption Key: It is a value that is known to the sender. The sender puts the encryption key into the encryption algorithm along with the plaintext in order to compute the ciphertext. [8] Decryption Key: It is a value that is known to the receiver. The decryption key is related to the encryption key, but is not always identical to it. The receiver inputs the decryption key into the decryption algorithm along with the ciphertext in order to compute the plaintext. [8]

2.2

Objectives of Cryptography

Cryptography is used to provide the following information security services: Confidentiality: Nobody can read the message not including the future receiver.[9] 9

Section 2.3. Kerckhoff’s Principle

Page 10

Authentication: This process is proving a one’s identity. After receiving the information, the system verifies the identity of the sender to determine whether the information comes from an authorized person or an unauthorized person.[9] Integrity: Only the authorized party is modifying the transmitted information or message. Nobody can change the given message.[9] Non-repudation: This is a mechanism to prove that the sender really sent this message.[9] Access Control: Only the authorized parties are capable to contact the given information.[9]

2.3

Kerckhoff’s Principle

This principle expresses that a cryptosystem should be secured even if everything about the system, except the key, is public knowledge. Opposed to this principle, the Security through Obscurity which is based on the non-disclosure of information about the structure, operation and implementation of the cryptosystem, to ensure its security.

2.4

Type of Cryptographic System

There are two types of cryptographic system: Symmetric Cryptography: The first cryptosystems were symmetrical type i.e. the keys for encrypting and decrypting are the same. In this type of cryptography, the plaintext and a key are used as inputs to the encryption algorithm to produce the ciphertext. Using the same key and the ciphertext as inputs to the decryption algorithm, the plaintext is obtained (see Figure 2.1). Some examples of this type are: Data encryption Standard (DES), Triple-DES, Advanced Encryption Standard (AES).

Figure 2.1: Symmetric Cryptography Process.

Section 2.5. Comparison of Symmetric and Asymmetric Cryptographic Systems

Page 11

Asymmetric Cryptography: Also known as public key cryptography, asymmetric cryptography is created because in a symmetric cryptosystem, it is difficult to share keys. This system uses different keys to encrypt and decrypt. The encryption of the plaintext is made with a key different from that used for the decryption of the ciphetext (see Figure 2.2). RSA and Elgamal encryption are some examples of this type of cryptography.

Figure 2.2: Asymmetric Cryptography Process.

2.5

Comparison of Symmetric and Asymmetric Cryptographic Systems

In Table. 2.1, we present a comparison of the two types of cryptographic system [10]. Table 2.1: Difference between symmetric and asymmetric cryptography Characteristic

Symmetric Cryptography

Key used for encryption/decryption

Same key is used

Speed of encryption/decryption Size of resulting encrypted text

Known keys Usage

Asymmetric Cryptography

Very fast

One key is used for encryption and another for decryption Slower

Usually same as or less than the original plaintext size Both parties should know the key in symmetric key encryption Confidentiality

More than the original plaintext size One of the keys is known by the two parties in public key encryption Confidentiality, Digital signature

Section 2.6. Symmetric cryptography algorithms

Page 12

We have chosen to implement a symmetric algorithm because of the following advantages that it offers: • The encryption and decryption times are shorter than those of the asymmetric algorithms. • For the same level of security, the key size of the symmetric algorithms is less than the key size of the asymmetric algorithms.[11] These two points make the symmetric algorithms more convenient, especially for a cryptographic algorithm implementation in Embedded Systems.

2.6

Symmetric cryptography algorithms

The popular symmetric cryptographic algorithms include: Advanced Encryption Standard (AES), Blowfish, RC5, Data Encryption Standard (DES), Triple-DES (3DES) and International Data Encryption Algorithm (IDEA) [12]. The best-known symmetric algorithms are mainly block cipher algorithms. The term block cipher means that the input data of the algorithm has a fixed length. Block cipher is opposed for stream cipher where the algorithm acts on each input bit one after the other. Many of these symmetric key algorithms use Feistel networks. A cryptographic system based on Feistel cipher structure uses the same algorithm for both encryption and decryption. Feistel networks are a specific construction for designing symmetric encryption schemes. They were designed by Horst Feistel. In the Feistel network structure, the input is split up into two blocks, the left half and the right half. The structure is composed of several processing rounds consisting of substitution step followed by permutation step. Substitution involves replacing symbols or groups of symbols with other symbols or groups of symbols in order to create confusion. Confusion means that each binary digit (bit) of the ciphertext should depend on several parts of the key, obscuring the connections between the both. Permutation is a set of transpositions. Transpositions consist of mixing the symbols or groups of symbols of a clear message according to predefined rules to create a diffusion. Diffusion means that if we change a single bit of the plaintext, then (statistically) half of the bits in the ciphertext should change, and similarly, if we change one bit of the ciphertext, then approximately one half of the plaintext bits should change. In the following lines, we briefly present the best-known symmetric algorithms. 2.6.1 Data Encryption Standard (DES). DES is a symmetric key algorithm which was developed in 1977 at IBM labs by Horst Fiestel. It uses block size of 64 bits, key size of 56 bits and 16 rounds of transposition and substitution. The DES was once a predominant symmetric-key algorithm for the encryption of electronic data. But now it is an outdated symmetric key data encryption method. 2.6.2 Triple-DES (3DES). Triple-DES is also proposed by IBM in 1978 as a substitute to DES. It uses three keys of size 64 bits each. The algorithm is the same as DES, only that the process is repeated three times. In the

Section 2.7. Comparison of symmetric cryptography algorithms

Page 13

procedure for encryption, the plaintext is encrypted with the first key then decrypted with the second key, and finally encrypted again with the third key. The procedure for decrypting the ciphertext is the same as the procedure for encryption, except it is a reverse process. 2.6.3 RC5 algorithm. The RC5 encryption algorithm is developed by Ronald Rivest in 1994. RC5 has variable block size, key size and number of rounds. The block size can be 32, 64 or 128 bits, the size of the key is between 0 to 2040 bits and the number of rounds is between 0 to 255. The original suggested choice of parameters were a block size of 64 bits, a 128-bit key and 12 rounds. The general structure of the algorithm is a Feistel network. 2.6.4 International Data Encryption Algorithm (IDEA). IDEA is an encryption algorithm developed at ETH in Zurich, Switzerland in 1990. It uses a block cipher with a 128-bit key. Its sizes of ciphertext and plaintext are the same and are 64 bits. It was designed to be an alternative to DES. 2.6.5 Blowfish algorithm. Created in 1993, Blowfish is a block cipher algorithm based on DES, but with longer keys and more hazards when encrypting. It has a 64-bit block size and a variable key length from 32 bits to 448 bits. It is based on 16 rounds Fiestel cipher network. 2.6.6 Advanced Encryption Standard (AES). Rijndael algorithm designed by Vincent Rijmen and Joan Daemen was selected in october 2000 as AES. For AES the length of the data blocks is fixed to 128 bits and the length of the cipher key can vary between 128, 192, or 256 bits. AES does not use a Feistel structure.

2.7

Comparison of symmetric cryptography algorithms

In order to fix our choice on a symmetric cryptography algorithm, we made a comparison between the different symmetric cryptography algorithms. Table. 2.2 presents a comparison of them [13, 14].

Section 2.8. Blowfish algorithm

Page 14

Table 2.2: Comparison popular symmetric cryptographic algorithms Parameters

DES

3DES

RC5

IDEA

Blowfish

AES

Key size

64 bits

112 or 118 bits

128 bits

32-448 bits

128,192 256 bits

Block size

64 bits

64 bits

64 bits

64 bits

128 bits

Brute force attack Adequate security Very slow

Differential and Related-key attacks Adequate security Very slow

0-2040 bits (128 suggested) 34 , 64 128 bits (64 suggested) Co-relation and Timing attacks Secure

Linear attack

No attack

Secure

Slow

Fast

Highly secure Very fast

Side channel attacks Excellent security Faster

Attacks Level of security Encryption speed

By comparing the various symmetric cryptographic algorithms, we note that the blowfish algorithm has notable advantages. It uses a key length from 32 bits to 448 bits and has a 64 bits block size divided into two 32 bits halves.It is very fast in encryption, provides excellent security and no effective cryptanalysis of it has been found to date [15]. Blowfish algorithm is unpatented and license-free, and is available free for all uses [16]. Considering these points we have chosen to implement in the Embedded System, the symmetric cryptographic algorithm, Blowfish.

2.8

Blowfish algorithm

Blowfish is a symmetric block cipher algorithm, designed in 1993 by Bruce Schneier. It is based on Feistel Network and contains 16 rounds. It encrypts block data of 64 bits and uses key size of 32-448 bits to generate two sub-key arrays. In this algorithm, two main functions are performed : Key expansion and Data encryption. 2.8.1 Key expansion. The key expansion consists of converting the secret key of the Blowfish algorithm (key length from 32 bits to 448 bits) into two sub-key arrays : P-array and S-boxes. The both sub-keys are generated earlier to any data encryption or decryption and totaling 4168 bytes. The P-array consists of 18, 32-bit sub-keys: P1 , P2 , · · · , P18 .

Section 2.8. Blowfish algorithm

Page 15

The S-boxes are four in number with each containing 256 elements of 32 bits: F irst S − box : S1,0 ; S1,1 ; · · · ; S1,255 Second S − box : S2,0 ; S2,1 ; · · · ; S2,255 T hird S − box : S3,0 ; S3,1 ; · · · ; S3,255 F ourth S − box : S4,0 ; S4,1 ; · · · ; S4,255 The use of P-array and S-boxes are presented in the sections 2.8.2 and 2.8.3. Their generation process works as follows [17]: 1. Initialize first the P-array and then the four S-boxes, in order, with the hexadecimal digits of pi (less the initial 3): P1 = 0X243F 6A88, P2 = 0X85A308D3 , · · ·. 2. XOR P1 with the first 32 bits of the key, XOR P2 with the second 32-bits of the key, and so on for all bits of the key. Repeatedly cycle through the key bits until the entire P-array has been XORed with key bits. 3. Encrypt the all-zero string with the Blowfish algorithm, using the sub-keys described in steps (1) and (2). 4. Replace P1 and P2 with the output of step (3). 5. Encrypt the output of step (3) using the Blowfish algorithm with the modified sub-keys. 6. Replace P3 and P4 with the output of step (5). 7. Continue the process, replacing all entries of the P-array, and then all four S-boxes in order with the output of the continuously changing Blowfish algorithm. In total, 521 iterations are required to generate all required sub-keys. Applications can store the sub-keys rather than execute this derivation process multiple times. 2.8.2 Data encryption. [18, 16] Encryption begins with a 64 bits block element of plaintext that will be morphed into a 64 bits ciphertext. • The 64 bits segment is immediately split into two equally sized segments (into two 32 bits halves) that will be used as the base of the Blowfish algorithm: Left block (L) and Right block (R). • The exclusive-or-operation (XOR) is performed between the first 32 bits block segment (L) and the first P-array. • The resulting 32 bits data is passed to the F function (details about the F function in section 2.8.3) which permutes the data and provides a 32 bits block segment. • This permuted block segment is XORed with the second 32 bits segment (R) created by the 64 bit plaintext split. • After the XOR operation is complete the 32 bits segments L and R are swapped. These operations are repeated 15 times using the other P-arrays (P2 to P15 ). The algorithm comprises a total of 16 iterations.

Section 2.8. Blowfish algorithm

Page 16

• After the sixteenth iteration, L and R are swapped again to undo the last swap. R is XORed with the seventeenth P-array and L with the eighteenth. • Finally, L and R are recombined to get the ciphertext. Algorithm 1 Blowfish Encryption for i f rom 1 to 16 do L = L XOR P i R = F (L) XOR R Swap L and R end for Swap L and R (U ndo the last swap.) R = R XOR P 17 L = L XOR P 18 Recombine L and R Decryption is exactly the same as encryption, except that P1 , P2 , · · · , P18 are used in the reverse order. The figure. 2.3 shows how blowfish algorithm works.

Figure 2.3: Blowfish encryption and decryption algorithm. 2.8.3 Function F of Blowfish. The function splits the 32-bit input (left block L) into four 8 bits quarters (a, b, c, d), and uses the quarters as input to the S-boxes. The outputs are added and XORed to produce the final 32-bit output.

Section 2.8. Blowfish algorithm

Page 17

All addition is modulo 232 . Figure. 2.4 shows Blowfish’s F-function. The Function F looks like this: F (L) = ((S1,a + S2,b mod 232 ) XOR S3,c ) + S4,d mod 232

(2.8.1)

Figure 2.4: F-function of Blowfish. 2.8.4 Application of the blowfish algorithm. The code sent to the client is constituted in encrypted form of: • a number for padding on the first 40 bits, • a password on the next 16 bits, • an address (details in section 3.6) on the last 8 bits. After the encryption, the left and right blocks encrypted are converted into the decimal base and then associated to constitute the cyphertext (code) of 64 bits (20 digits). Each kit has its own encryption key. Thus the unlock code is specific to each kit. The Blowfish decryption algorithm is inserted into an Embedded System that we built.

3. Design of the Embedded System This chapter talks about design of the Embedded System. In the following lines, we present the choice of the components of the Embedded System, the programming of the system and the tests for the verification of the operation.

3.1

Definition and applications of Embedded Systems

An Embedded System is a hardware system in which a software is integrated and designed to perform a specific task. It can be an independent system or a part of a large system. The hardware can be defined as the physical manifestation of a system. A software program is commonly defined as a set of instructions that allow for a certain type of system operation. The software program for Embedded Systems is usually called firmware. Embedded Systems examples can be seen everywhere (at our homes, offices...). They can be found in real life appliances. As examples of devices containing Embedded Systems we have: printer, digital camera, home security system, washing machine, calculator. Embedded Systems are used by automobile manufacturers for different functionalities in the cars like ignition, security and audio systems.

3.2

Functions and constitutions of our Embedded System

For the development of a PAYG concept for solar kits, the design of an Embedded System is required. This Embedded System will be integrated on the electronic cards (PCB) of the solar kits and will have like functions: - Acquire a code; - Validate or reject the code; - In case of valid code, unlock the kit for use during a specified period (kit’s period of availability); - Lock the kit after this time; - Completely unlock the kit after a certain number of valid code. - Lock the kit after a certain number of rejected code to require a physical intervention of the company so that the kit is again usable. To fulfill these functions, the components of the hardware system are: - Microcontroller responsible for coordinating the various operations; - A component to communicate the code to the Embedded System. The infrared remote control is the one chosen for its convenience; - Real-time clock for the management of the kit’s period of availability;

18

Section 3.3. Embedded System components

Page 19

- External EEPROM (Electrically-Erasable Programmable Read-Only Memory) memory to save data (passwords which are identical for all kits, the state of the system, the date of unlocking of the kit, the number of fake codes entered and the number of correct codes entered); - A switching block to lock and unlock the kit. Figure. 3.1 gives a rough picture of the Embedded System.

Figure 3.1: Overview of the Embedded System. On the software side of the Embedded System, the firmware is inserted into the microcontroller and allows the various tasks to be performed. A cryptographic algorithm in the firmware is used to decrypt the code acquired.

3.3

Embedded System components

3.3.1 Microcontroller. Embedded System hardware is built with a microprocessor or microcontroller. The microprocessor is an electronic chip which performs the arithmetic and logic operations and controls the general operation of the system. A microcontroller (MCU) is very similar to a microprocessor, except that it includes in addition to an integrated microprocessor, memories (RAM, ROM) and other devices on the same chip. The essential elements of a microcontroller are: • Microprocessor also known as the CPU (Central Processing Unit) which runs the program and the other elements. • ROM (Read Only Memory) : a memory whose content is retained even in the event of power failure. It contains the program that will be executed by the microprocessor. • RAM (Random Access Memory) a memory in which the unit can read and write at any time. It is used in the calculation phases of the program, to store intermediate results, to store the variables of an application. The contents of this memory are not retained in the event of a power failure. • The input / output interfaces that allow the microcontroller to communicate with its environment.

Section 3.3. Embedded System components

Page 20

3.3.2 Infrared Remote Control. In electronics, a remote control is a component used to operate a wireless device remotely. The communication between the remote control and the device can be by infrared, radio waves or Bluetooth. Infrared remote controls are the most used today. Infrared remote controls use infrared (IR) light emitted from an IR LED to convey information. This information is sent according to a given protocol and is in the form of digitally encoded pulses. The protocol helps the receiver distinguish the desired signals from other sources of infrared noise. The receiver uses a photodiode to convert the IR light into the electrical current from which the information is extracted. 3.3.3 Real Time Clock (RTC). A real time clock is a clock allowing a very precise time count for an electronic system, with a view to managing the events according to the time. It provides seven kinds of time value : - seconds, - minutes, - hours, - date of the month, - month, - day of the week - and year with leap year compensation. These values are transferred via the I2C (Inter-Integrated Circuit) serial communication. 3.3.4 Switching block. In electronics, the switching blocks are used to cut, re-establish or orientate the electrical current. These switching blocks are made of transistors. The operation of these transistors is similar to that of the interrupters. By making the analogy, the open and closed state of the interrupter is called respectively blocked state and saturated state for the transistor. These states of the transistor are controllable by electrical signals (current or voltage). 3.3.5 Electrically-Erasable Programmable Read-Only Memory (EEPROM). EEPROM is a type of non-volatile memory used in electronic devices to store data. Non-volatile memory, unlike volatile memory, is characterized by its ability to retain the data even after being turned off. EEPROMs are erasable, also reprogrammable and this several times. Their reprogramming and erasing are done by applying special programming signals and are limited in number, reaching a million operations in modern EEPROMs. EEPROMs communicate with other components via I2C or SPI (Serial Peripheral Interface) serial communications.

Section 3.4. Communication modes of the Embedded System components

3.4

Page 21

Communication modes of the Embedded System components

The communication between the microcontroller and the other components of the system is done through various existing techniques. In the following lines, we present these techniques. 3.4.1 Serial Peripheral Interface (SPI). The SPI is a serial communication developed by Motorola that operates in full duplex mode (data flows simultaneously in both directions). Serial communications refer to any digital signal where data is transferred one bit at a time. In SPI, devices communicate in a master / slave environment where the master device initiates the communication and generates the clock signal (signal that governs digital electronics and allows the organization of the execution of instructions). A slave device is controlled through a Chip Select known as Slave Select. The SPI bus specifies four signal connections [19]: • Serial clock (called SCK), the line of the clock signal that the transmission uses. • Serial data output from master (called MOSI, SIMO, SDO, SO or DO), the line for transmission from the master to the slaves and is uni-directional. • Serial data output from slave (called MISO, SOMI, SDI, SI or DI), the line for transmission from a slave to the master and is also uni-directional. • Slave Select (called SS or CS), this line connects a single slave device to a master. 3.4.2 Inter-Integrated Circuit (I2C). The I2C is a type of serial communications that allows the transmission of information between various connected circuits in half duplex mode (data can be transmitted in both directions, but not at the same time). The electrical characteristics and the communication protocol have been deposited by Philips. It is a master / slave configuration. The transfer of information between the electronic components is done with only 2 wires, one for data and one for the clock [19]: • Serial Data (SDA), that allows bidirectional exchanges between master and slave. • Serial Clock (SCL), the line of the clock signal. The master circuit is the one which requests information transfer and generates the clock signal for the transfer. Thus, an addressed circuit is considered as a slave. In the Embedded System, the microcontroller is the master and the RTC is the slave. The library I2C Remappable in the MikroC software allows to program the microcontrollers for communication via the I2C connection with other electronic components. 3.4.3 NEC Infrared Transmission Protocol. Infrared remote controllers and receivers follow standard protocols for sending and receiving the data. NEC protocol is widely used in protocols. Message’s bits are transmitted as follows: • Logical 0 00 : a 562.5µs pulse burst followed by a 562.5µs space, with a total transmit time of 1.125ms • Logical 0 10 : a 562.5µs pulse burst followed by a 1.6875ms space, with a total transmit time of 2.25ms

Section 3.5. Choice elements of the system

Page 22

When a key is pressed on the remote controller, the message transmitted consists of the following, in order: • A 9ms leading pulse burst. • A 4.5ms space. • The 8-bit address for the receiving device. • The 8-bit logical inverse of the address. • The 8-bit command. • The 8-bit logical inverse of the command. • A final 562.5µs pulse burst to signify the end of message transmission. The fully transmitting message lasts 67.5ms (discounting the final 562.5µs pulse burst that means the end of message). NEC protocol encodes the buttons using a 32-bit frame format seperated into two blocks of 16 bits that lasts 27ms each. This comes from each of the 16-bit blocks ultimately containing eight 0s (9ms) and eight 1s (18ms). The two 16-bit blocks are: • The address (address + inverse) • The command (command + inverse) Figure. 3.2 illustrates the format of an NEC IR transmission frame, for an address of 0x8D and a command of 0xB1.

Figure 3.2: NEC Protocol Transmission.

3.5

Choice elements of the system

The Embedded System consists of 5 blocks: microcontroller, infrared remote control, Real-Time Clock, EEPROM and the switching block. Figure. 3.3 shows the electronic schematic of the Embedded System. This schematic is realized with EAGLE (Easily Applicable Graphics Layout Editor) PCB design software.

Section 3.5. Choice elements of the system

Figure 3.3: Electronic schematic of the Embedded System.

Page 23

Section 3.5. Choice elements of the system

Page 24

The choice of components constituting these blocks is presented in the following lines. 3.5.1 Infrared Remote Control [20]. The utility of the remote control is to allow the user to communicate the unlock code to the system. For that, the remote control we have to use must have the buttons for the ten digits and an additional button for the validation of the entered code. Our choice fell on the Keyes remote. This remote is small, has digits buttons and an OK button that can be used to validate entered code. It uses the NEC protocol for sending information. The features of the remote control are the following : • Infrared sensor : AX-1838HS • Operating frequency : 38kHz • Operating voltage : 5V • Remote control dimensions : 8.5 x 4 x 0.65 mm • Transmission distance max : 8 m • Effective angle : 60 degrees 3.5.2 Real Time Clock (RTC). The need for a real-time clock in the system is for time management during which the kit remains unlocked for use. One of the most used RTCs is the DS1307. We have also selected for the RTC of the system the DS1307. It is low cost, gives time values for a period up to 2100 and can run for years on a very small coin cell. Its features are [21]: • Stand-alone digital clock, provide date and time • Communication with other interfaces via an I2C serial link • Operating voltage 5V • Temperature Range (C) -40 to 80 • Battery Backup with Automatic Power-Fail Detect and Switch Circuitry 3.5.3 Electrically-Erasable Programmable Read-Only Memory (EEPROM). In the system we are developing, some information will be saved in an EEPROM. Our criterion of choice is an EEPROM with a large size of locations in memory to save a large number allowing then multiple possibilities of passwords and storage dates. The ones we found on the market are 16-bit saving numbers up to 65535. The chosen EEPROM is the 93LC46B developed by Microchip Technology Inc. Its features are [22]: • Memory size : 1024 bits organized as 64 words of 16 bits each • Operating Voltage Range (V) : 2.5 to 5.5 • Mode of communication: Serial Peripheral Interface (SPI) • 1 000 000 Erase/Write Cycles

Section 3.5. Choice elements of the system

Page 25

• Data Retention > 200 Years • Temperature Range (C) : -40 to 125 3.5.4 Switching block. The most widely used transistors in circuits for switching are bipolar transistors and MOSFET transistors. The bipolar transistors are controllable by the current and the MOSFET transistors by the voltage. The choice of MOSFET transistors is much more justified by their switching speed which is higher than bipolar transistors. These transistors have also the advantage of consuming a very low energy. Our switching block includes two MOSFET transistors. The choice of transistors is justified by the current they can drive and the voltage they can support. The selected transistors are: • MOSFET canal N, 2N7000. Drain Source Voltage 60V; Drain Current 200mA. • MOSFET canal P, IRLML5203. Drain Source Voltage -30V; Drain Current -3A. 3.5.5 Microcontroller. Many families of microcontrollers are available on the market. ATMEL-AVR, Microchip-PIC are some examples. The choice of a microcontroller depends on the application to be made. For our Embedded System, the microcontroller must: • communicate with the RTC through an I2C serial link; • communicate with the EEPROM memory by SPI; • have a digital input to receive the code sent by the remote control; • have a digital output to block and unblock the kit; • have a sufficient ROM for program and decryption key storage; • possess sufficient RAM to perform operations including decryption calculation. We opted for a PIC (Programmable Interface Controller) microcontroller manufactured by Microchip Technology Inc. PIC microcontrollers are widely used in the industry. They are easily accessible and those at lower cost. We chose the PIC16F18857 microcontroller with the following characteristics [23]: • Program memory : 56KB • RAM Bytes : 4096 • Data EEPROM Bytes : 256 bits organized as 32 words of 8 bits each • Temperature Range (C) : -40 to 125 • Operating Voltage Range (V) : 2.3 to 5.5 • Pin Count : 28 • Digital Communication Peripherals : 1-UART, 2-SPI, 2-I2C

Section 3.6. Programming microcontroller

3.6

Page 26

Programming microcontroller

In this section, we present the programming of the microcontroller. The C language is the language we used to program the microcontroller. The programming was done with the complete C compiler Mikroc developed by MikroElektronika. The programming is sequential which means that the microcontroller executes the instructions one by one. We organized the program using the instruction switch statement with 6 cases. Figure. 3.4 shows the flowchart of the program.

Figure 3.4: Flowchart of the program in the microcontroller. The functions of the cases are: Case 1: This state corresponds to the step of acquisition by the system of the unlock code. The microcontroller with the signals sent by the remote control detects the numbers corresponding to the buttons pressed to form the code. At this point, the kit is blocked. Case 2: It is reached after the complete entry of the code notified by the OK button. The microcontroller decrypts the code and separates the password and the address (memory location) of the message. The value stored at the address is read in the EEPROM and compared to the password. If there is an equality then the code is valid and the system enters in the case 4 otherwise the code is rejected and the system enters in the case 3. In the case the code is validated, the data read from the address is erased so that a valid code can not be used more than once. Case 3: This case is the step that follows the rejection of the code. The number of false codes is incremented and stored in memory. If the total number of false codes exceeds the set value, the kit is blocked to request an intervention from the company. This blocking consists of a retention

Section 3.6. Programming microcontroller

Page 27

of the system in an infinite loop in case 3 where the kit is still in the locked state. On the other hand, if the total number of false codes is lower than the set value, the system returns to case 1 for the acquisition of a new code. Case 4: Once in this case, the entered code is already validated and the kit will be unlocked in the following case. The microcontroller obtains from the RTC the current date and calculates the date on which the kit will be blocked again. The calculated date is saved in the EEPROM. The remaining fee for the kit is calculated and saved too. If the remaining fees to be paid are zero, the system goes to case 6. Otherwise, the system goes the case 5. Case 5: At this point, the kit is unlocked. The microcontroller compares the current date provided by the RTC with the end date of the kit’s availability period. At the end date, the system returns to case 1. Case 6: This step is reached after the total refund of fees. The kit is unlocked and stays like that indefinitely. Date calculations is facilated at the level of the microcontroller by the use of Julian days. Julian Day invented by Joseph Juste Scaliger is a dating system independent of complex calendar cycles (unequal duration of months, intercalary months, extra days, leap years) and consisting of counting the number of days since a date fixed. The Julian day (jd) is computed from Gregorian day, month and year (d, m, y) as follows [24]:

jd = ( 1461 * ( y + 4800 + ( m - 14 ) / 12 ) ) / 4 + ( 367 * ( m - 2 - 12 * ( ( m - 14 ) / 12 ) ) ) / 12 - ( 3 * ( ( y + 4900 + ( m - 14 ) / 12 ) / 100 ) ) / 4 + d - 32075 (3.6.1) 3.6.1 Interfacing microcontroller and Remote Control. To receive the information sent by the remote control, the microcontroller must be able to check the NEC protocol and identify the signal corresponding to each button on the remote control. In the electronic schematic of the Embedded System (figure. 3.3), the infrared receiver allows to obtain the signal sent by the remote control. This receiver transmits the signal to the microcontroller. This transmission takes place via a connection of the output of the infrared receiver with a digital input of the microcontroller. The microcontroller extracts from the signal the information on the button pressed. This is done through a program that checks the NEC protocol, take the command and the inverse of the command to identify the button pressed. The flowchart of the program is shown in figure. 3.5.

Section 3.6. Programming microcontroller

Page 28

Figure 3.5: Flowchart program for getting button pressed on the remote control. 3.6.2 Implementation of Blowfish algorithm in the microcontroller. The code received by the microcontroller using the remote control is a cyphertext and is decrypted with the Blowfish cryptography algorithm. The flowchart of the Blowfish algorithm is presented in figure. 3.6. The algorithm comprises only the decryption part. In order for the execution of the bowfish algorithm by the microcontroller to be carried out quickly, the step of the key expansion is excluded from the implementation. The subkeys must therefore be generated before the implementation and insert into the decryption algorithm at the level of the Embedded System. This algorithm is translated into C language for its implementation in the microcontroller.

Section 3.6. Programming microcontroller

Page 29

Figure 3.6: Flowchart Blowfish decryption algorithm. 3.6.3 Interfacing microcontroller and EEPROM. The communication between the microcontroller and the EEPROM is via a Serial Peripheral Interface (SPI). The microcontroller is the master and the EEPROM is the slave. The communication protocol is as follows : • Initialization of communication. • Sending the operation code (writing or reading ...) and the address. • Transmission of data. • Stop communication. 3.6.4 Interfacing microcontroller and RTC. The RTC communicates the date information to the microcontroller through a serial communication I2C. The microcontroller is the master and the RTC is the slave. The I2C library in the mikroC software is used to program the communication protocol which is as follows: • Initialization of I2C communication at the frequency of 100Khz. • Launch of the I2C communication.

Section 3.6. Programming microcontroller

Page 30

• Send a request to provide the address to which we want to take the information (day of the month, month or year). • Write the address (sending the address) to which you want to read the time value. • Restart the I2C communication. • Send a request to read the data at the address provided. • Reading the data at the address. • Stop communication. 3.6.5 Interfacing microcontroller and Switching block. It is through the switching block that the microcontroller is able to lock and unlock the kit. This block is constituted by two MOSFET transistors and is controlled in voltage. In the switching block, the presence of a voltage of 5V unlocks and the absence of voltage (0V) locks the kit (see figure . 3.7 and figure 3.8 simulation of the block with the MULTISIM software). The digital outputs of the microcontroller have voltage levels of 5V and 0V. Thus, the control of the switching block will be controlled by a digital output of the microcontroller.

Figure 3.7: Simulation of the switching block for a control voltage of 5V.

Figure 3.8: simulation of the switching block for a control voltage of 0V.

Section 3.7. Experiments

3.7

Page 31

Experiments

We have made some experiments to test some features of the system. So we checked : • acquisition of the digits of a code by the system with the remote control; • decryption of code by the system with the Blowfish algorithm; • communication between the system and the RTC through an I2C serial communication. The main materials used for the tests are: • Two microcontrollers PIC16F1825. One displays the digit of the pressed button on a seven segment display and the second contains the test program for the blowfish algorithm and obtaining time values from the RTC (see Figure 3.9).

Figure 3.9: Microcontroller PIC16F1825. • Keyes remote control and the infrared receiver (see Figure 3.10).

Figure 3.10: Remote and innfrared receiver. • a real Time Clock DS1307 (see Figure 3.11).

Figure 3.11: RTC DS1307.

Section 3.7. Experiments

Page 32

• a LED as an indicator (see Figure 3.12).

Figure 3.12: LED. • a seven-segment display to show the pressed buttons (digits) (see Figure 3.13).

Figure 3.13: Seven-segment display. • a breadboard for the connections between components (see Figure 3.14 ).

Figure 3.14: Breadboard. • a pickit3 for uploading the firmware into microcontrollers (see Figure 3.15).

Figure 3.15: Pickit3. Figure 3.16 shows the electronic assembly.

Section 3.7. Experiments

Page 33

Figure 3.16: Electronic assembly. The results of the different experiments are presented in the following lines. 3.7.1 Digit acquisition test. After pressing a button on the remote control, the system displays the pressed digit using the seven-segment display. Figure 3.17 shows the results after pressing the button 1.

Figure 3.17: Display of the pressed digit. 3.7.2 Decryption with blowfish algorithm and communication with RTC. With the pi digits as subkeys, we encrypt Lef t part = 22 90 64 92 24 and Right part = 00 16 77 69 60 and obtain as ciphertext Lef t part = 31 29 36 96 05 and Right part = 22 44 72 38 15. The cyphertext is then entered into our system which decrypts it and compares it to the plaintext. In the case of a match (correct plaintext) the LED lights up and the RTC allows to keep the LED on during 2 minutes. In the opposite case (wrong plaintext) nothing happens. Figure 3.18 shows test results after entering

Section 3.7. Experiments

Page 34

the ciphertext. The LED lights up indicating that the system got the correct plaintext.

Figure 3.18: Ignition of the LED indicator.

Conclusion and Prospects This work is part of the development of a PAYG platform for the Nadjibi solar kits. PAYG is a mechanism that allows a progressive and remote payment of the purchase cost of kits. The purpose of our work in the project of a PAYG system was the design of an Embedded System and the implementation of a cryptographic algorithm in it. The Embedded System has the role of unlocking the solar kit after a payment and block it after a while. The utility of cryptography is to secure the transmission of unlock codes to customers. In our work, after a comparison between the two types of cryptographic system, we chose symmetric cryptography for its speed in encryption and decryption and also for the size of its keys which are smaller. Another comparison between the symmetric algorithms made it possible to choose Blowfish algorithm because of its structure, speed and security. This algorithm has been translated into C language for its implementation in the hardware of the Embedded System. In the rest of our work, we have also chosen the different hardware components. The program enabling the system to perform its functions has also been written.This program manages among other things the communication between the hardware components. For future studies, we propose tests with side channel attacks in order to find the weaknesses of the implementation and corrected them. Also, we suggest, since the Embedded System is off-network (not connected to a network for remote communication), the adding of devices to connect it to a network (GSM, Internet). Thus, the Embedded System will be hybrid i.e, it can operate both off-network and on-network. While on-network, the Embedded System will allow to monitor and control the kit remotely, but also to use it for collecting data related to use or other parameters.

35

Appendix A. Text Program (Code acquisition by Remote Control, Decryption algorithm and Communication with RTC) // I n p u t i n f r a r e d s i g n a l s b i t INPUT a t R A 2 b i t ; // I n t e r r u p t e u r PAYG t o s w i t c h on o r o f f t h e k i t s b i t INTERRUPTEUR a t R A 0 b i t ; // Data r i g h t 32 b i t s t o d e c r y p t unsigned long i n t D r i g h t = 0 x00000000 ; // Data l e f t 32 b i t s t o d e c r y p t unsigned long i n t D l e f t = 0 x00000000 ; // V a r i a b l e w h i c h k e e p s t h e s i g n a l r e c e i v e from t h e i r r e m o t e unsigned i n t s i g n a l = 0 ; // The s t a t e o f p r o c e s s i n g unsigned i n t c a s ; // V a r i a b l e u s e d t o s t a y u n t i l g e t t i n g unsigned i n t s t a y = 1 ; // No o f d i g i t o f t h e code from l e f t t o r i g h t unsigned i n t k = 1 ; // V a r i a b l e f o r t h e m i n u t e unsigned i n t m i n u t e = 0 ; // S u b k e y s . D i g i t s o f P i w i t h o u t m o d i f i c a t i o n . const unsigned long i n t p a r r a y [ 1 8 ] = { 0 x243F6A88 , 0 x85A308D3 , 0 x13198A2E , 0 x03707344 , 0 xA4093822 , 0 x299F31D0 , 0 x082EFA98 , 0xEC4E6C89 , 0 x452821E6 , 0 x38D01377 , 0 xBE5466CF , 0 x34E90C6C , 0 xC0AC29B7 , 0 xC97C50DD , 0x3F84D5B5 , 0 xB5470917 , 0 x9216D5D9 , 0 x8979FB1B }; // S−b l o c k s . D i g i t s o f P i w i t h o u t m o d i f i c a t i o n const unsigned long i n t s b l o c k 0 [ 2 5 6 ] = { 0xD1310BA6 , 0 x98DFB5AC , 0 x2FFD72DB , 0 xD01ADFB7 , 0 xB8E1AFED , 0 x6A267E96 , 0 xBA7C9045 , 0 xF12C7F99 , 0 x24A19947 , 0 xB3916CF7 , 0 x0801F2E2 , 0 x858EFC16 , 0 x636920D8 , 0 x71574E69 , 0xA458FEA3 , 0 xF4933D7E , 0 x0D95748F , 0 x728EB658 , 0 x718BCD58 , 0 x82154AEE , 0 x7B54A41D , 0xC25A59B5 , 0 x9C30D539 , 0 x2AF26013 , 0 xC5D1B023 , 0 x286085F0 , 0 xCA417918 , 0 xB8DB38EF , 0x8E79DCB0 , 0 x603A180E , 0 x6C9E0E8B , 0 xB01E8A3E , 0 xD71577C1 , 0 xBD314B27 , 0 x78AF2FDA , 0 x55605C60 , 0 xE65525F3 , 0 xAA55AB94 , 0 x57489862 , 0 x63E81440 , 0 x55CA396A , 0 x2AAB10B6 , 0xB4CC5C34 , 0 x1141E8CE , 0 xA15486AF , 0 x7C72E993 , 0 xB3EE1411 , 0 x636FBC2A , 0 x2BA9C55D , 0 x741831F6 , 0 xCE5C3E16 , 0 x9B87931E , 0 xAFD6BA33 , 0 x6C24CF5C , 0 x7A325381 , 0 x28958677 , 0 x3B8F4898 , 0 x6B4BB9AF , 0 xC4BFE81B , 0 x66282193 , 0 x61D809CC , 0 xFB21A991 , 0 x487CAC60 , 0x5DEC8032 , 0 xEF845D5D , 0 xE98575B1 , 0 xDC262302 , 0 xEB651B88 , 0 x23893E81 , 0 xD396ACC5 , 0x0F6D6FF3 , 0 x83F44239 , 0 x2E0B4482 , 0 xA4842004 , 0 x69C8F04A , 0 x9E1F9B5E , 0 x21C66842 , 0xF6E96C9A , 0 x670C9C61 , 0 xABD388F0 , 0 x6A51A0D2 , 0 xD8542F68 , 0 x960FA728 , 0 xAB5133A3 , 36

Page 37 0x6EEF0B6C , 0 x137A3BE4 , 0 xBA3BF050 , 0 x7EFB2A98 , 0 xA1F1651D , 0 x39AF0176 , 0 x66CA593E , 0 x82430E88 , 0 x8CEE8619 , 0 x456F9FB4 , 0 x7D84A5C3 , 0 x3B8B5EBE , 0 xE06F75D8 , 0 x85C12073 , 0 x401A449F , 0 x56C16AA6 , 0 x4ED3AA62 , 0 x363F7706 , 0 x1BFEDF72 , 0 x429B023D , 0 x37D0D724 , 0xD00A1248 , 0 xDB0FEAD3 , 0 x49F1C09B , 0 x075372C9 , 0 x80991B7B , 0 x25D479D8 , 0 xF6E8DEF7 , 0xE3FE501A , 0 xB6794C3B , 0 x976CE0BD , 0 x04C006BA , 0 xC1A94FB6 , 0 x409F60C4 , 0 x5E5C9EC2 , 0 x196A2463 , 0 x68FB6FAF , 0 x3E6C53B5 , 0 x1339B2EB , 0 x3B52EC6F , 0 x6DFC511F , 0 x9B30952C , 0 xCC814544 , 0 xAF5EBD09 , 0 xBEE3D004 , 0 xDE334AFD , 0 x660F2807 , 0 x192E4BB3 , 0 xC0CBA857 , 0 x45C8740F , 0 xD20B5F39 , 0 xB9D3FBDB , 0 x5579C0BD , 0 x1A60320A , 0 xD6A100C6 , 0 x402C7279 , 0 x679F25FE , 0 xFB1FA3CC , 0 x8EA5E9F8 , 0 xDB3222F8 , 0 x3C7516DF , 0 xFD616B15 , 0 x2F501EC8 , 0xAD0552AB , 0 x323DB5FA , 0 xFD238760 , 0 x53317B48 , 0 x3E00DF82 , 0 x9E5C57BB , 0 xCA6F8CA0 , 0 x1A87562E , 0 xDF1769DB , 0 xD542A8F6 , 0 x287EFFC3 , 0 xAC6732C6 , 0 x8C4F5573 , 0 x695B27B0 , 0xBBCA58C8 , 0 xE1FFA35D , 0 xB8F011A0 , 0 x10FA3D98 , 0 xFD2183B8 , 0 x4AFCB56C , 0 x2DD1D35B , 0 x9A53E479 , 0 xB6F84565 , 0 xD28E49BC , 0 x4BFB9790 , 0 xE1DDF2DA , 0 xA4CB7E33 , 0 x62FB1341 , 0xCEE4C6E8 , 0 xEF20CADA , 0 x36774C01 , 0 xD07E9EFE , 0 x2BF11FB4 , 0 x95DBDA4D , 0 xAE909198 , 0xEAAD8E71 , 0 x6B93D5A0 , 0 xD08ED1D0 , 0 xAFC725E0 , 0 x8E3C5B2F , 0 x8E7594B7 , 0 x8FF6E2FB , 0 xF2122B64 , 0 x8888B812 , 0 x900DF01C , 0 x4FAD5EA0 , 0 x688FC31C , 0 xD1CFF191 , 0 xB3A8C1AD , 0 x2F2F2218 , 0 xBE0E1777 , 0 xEA752DFE , 0 x8B021FA1 , 0 xE5A0CC0F , 0 xB56F74E8 , 0 x18ACF3D6 , 0 xCE89E299 , 0 xB4A84FE0 , 0 xFD13E0B7 , 0 x7CC43B81 , 0 xD2ADA8D9 , 0 x165FA266 , 0 x80957705 , 0 x93CC7314 , 0 x211A1477 , 0 xE6AD2065 , 0 x77B5FA86 , 0 xC75442F5 , 0 xFB9D35CF , 0 xEBCDAF0C , 0x7B3E89A0 , 0 xD6411BD3 , 0 xAE1E7E49 , 0 x00250E2D , 0 x2071B35E , 0 x226800BB , 0 x57B8E0AF , 0 x2464369B , 0 xF009B91E , 0 x5563911D , 0 x59DFA6AA , 0 x78C14389 , 0 xD95A537F , 0 x207D5BA2 , 0x02E5B9C5 , 0 x83260376 , 0 x6295CFA9 , 0 x11C81968 , 0 x4E734A41 , 0 xB3472DCA , 0 x7B14A94A , 0 x1B510052 , 0 x9A532915 , 0 xD60F573F , 0 xBC9BC6E4 , 0 x2B60A476 , 0 x81E67400 , 0 x08BA6FB5 , 0 x571BE91F , 0 xF296EC6B , 0 x2A0DD915 , 0 xB6636521 , 0 xE7B9F9B6 , 0 xFF34052E , 0 xC5855664 , 0x53B02D5D , 0 xA99F8FA1 , 0 x08BA4799 , 0 x6E85076A }; const unsigned long i n t s b l o c k 1 [ 2 5 6 ] = { 0x4B7A70E9 , 0 xB5B32944 , 0 xDB75092E , 0 xC4192623 , 0 xAD6EA6B0 , 0 x49A7DF7D , 0 x9CEE60B8 , 0x8FEDB266 , 0 xECAA8C71 , 0 x699A17FF , 0 x5664526C , 0 xC2B19EE1 , 0 x193602A5 , 0 x75094C29 , 0 xA0591340 , 0 xE4183A3E , 0 x3F54989A , 0 x5B429D65 , 0 x6B8FE4D6 , 0 x99F73FD6 , 0 xA1D29C07 , 0 xEFE830F5 , 0 x4D2D38E6 , 0 xF0255DC1 , 0 x4CDD2086 , 0 x8470EB26 , 0 x6382E9C6 , 0 x021ECC5E , 0 x09686B3F , 0 x3EBAEFC9 , 0 x3C971814 , 0 x6B6A70A1 , 0 x687F3584 , 0 x52A0E286 , 0 xB79C5305 , 0 xAA500737 , 0 x3E07841C , 0 x7FDEAE5C , 0 x8E7D44EC , 0 x5716F2B8 , 0 xB03ADA37 , 0 xF0500C0D , 0 xF01C1F04 , 0 x0200B3FF , 0 xAE0CF51A , 0 x3CB574B2 , 0 x25837A58 , 0 xDC0921BD , 0 xD19113F9 , 0x7CA92FF6 , 0 x94324773 , 0 x22F54701 , 0 x3AE5E581 , 0 x37C2DADC , 0 xC8B57634 , 0 x9AF3DDA7 , 0 xA9446146 , 0 x0FD0030E , 0 xECC8C73E , 0 xA4751E41 , 0 xE238CD99 , 0 x3BEA0E2F , 0 x3280BBA1 , 0 x183EB331 , 0 x4E548B38 , 0 x4F6DB908 , 0 x6F420D03 , 0 xF60A04BF , 0 x2CB81290 , 0 x24977C79 , 0 x5679B072 , 0 xBCAF89AF , 0 xDE9A771F , 0 xD9930810 , 0 xB38BAE12 , 0 xDCCF3F2E , 0 x5512721F , 0 x2E6B7124 , 0 x501ADDE6 , 0 x9F84CD87 , 0 x7A584718 , 0 x7408DA17 , 0 xBC9F9ABC , 0 xE94B7D8C , 0xEC7AEC3A , 0 xDB851DFA , 0 x63094366 , 0 xC464C3D2 , 0 xEF1C1847 , 0 x3215D908 , 0 xDD433B37 , 0x24C2BA16 , 0 x12A14D43 , 0 x2A65C451 , 0 x50940002 , 0 x133AE4DD , 0 x71DFF89E , 0 x10314E55 , 0x81AC77D6 , 0 x5F11199B , 0 x043556F1 , 0 xD7A3C76B , 0 x3C11183B , 0 x5924A509 , 0 xF28FE6ED , 0x97F1FBFA , 0 x9EBABF2C , 0 x1E153C6E , 0 x86E34570 , 0 xEAE96FB1 , 0 x860E5E0A , 0 x5A3E2AB3 , 0 x771FE71C , 0 x4E3D06FA , 0 x2965DCB9 , 0 x99E71D0F , 0 x803E89D6 , 0 x5266C825 , 0 x2E4CC978 , 0x9C10B36A , 0 xC6150EBA , 0 x94E2EA78 , 0 xA5FC3C53 , 0 x1E0A2DF4 , 0 xF2F74EA7 , 0 x361D2B3D , 0 x1939260F , 0 x19C27960 , 0 x5223A708 , 0 xF71312B6 , 0 xEBADFE6E , 0 xEAC31F66 , 0 xE3BC4595 , 0xA67BC883 , 0 xB17F37D1 , 0 x018CFF28 , 0 xC332DDEF , 0 xBE6C5AA5 , 0 x65582185 , 0 x68AB9802 , 0xEECEA50F , 0 xDB2F953B , 0 x2AEF7DAD , 0 x5B6E2F84 , 0 x1521B628 , 0 x29076170 , 0 xECDD4775 ,

Page 38 0 x619F1510 , 0 x13CCA830 , 0 xEB61BD96 , 0 x0334FE1E , 0 xAA0363CF , 0 xB5735C90 , 0 x4C70A239 , 0xD59E9E0B , 0 xCBAADE14 , 0 xEECC86BC , 0 x60622CA7 , 0 x9CAB5CAB , 0 xB2F3846E , 0 x648B1EAF , 0x19BDF0CA , 0 xA02369B9 , 0 x655ABB50 , 0 x40685A32 , 0 x3C2AB4B3 , 0 x319EE9D5 , 0 xC021B8F7 , 0 x9B540B19 , 0 x875FA099 , 0 x95F7997E , 0 x623D7DA8 , 0 xF837889A , 0 x97E32D77 , 0 x11ED935F , 0 x16681281 , 0 x0E358829 , 0 xC7E61FD6 , 0 x96DEDFA1 , 0 x7858BA99 , 0 x57F584A5 , 0 x1B227263 , 0x9B83C3FF , 0 x1AC24696 , 0 xCDB30AEB , 0 x532E3054 , 0 x8FD948E4 , 0 x6DBC3128 , 0 x58EBF2EF , 0x34C6FFEA , 0 xFE28ED61 , 0 xEE7C3C73 , 0 x5D4A14D9 , 0 xE864B7E3 , 0 x42105D14 , 0 x203E13E0 , 0x45EEE2B6 , 0 xA3AAABEA , 0 xDB6C4F15 , 0 xFACB4FD0 , 0 xC742F442 , 0 xEF6ABBB5 , 0 x654F3B1D , 0 x41CD2105 , 0 xD81E799E , 0 x86854DC7 , 0 xE44B476A , 0 x3D816250 , 0 xCF62A1F2 , 0 x5B8D2646 , 0 xFC8883A0 , 0 xC1C7B6A3 , 0 x7F1524C3 , 0 x69CB7492 , 0 x47848A0B , 0 x5692B285 , 0 x095BBF00 , 0xAD19489D , 0 x1462B174 , 0 x23820E00 , 0 x58428D2A , 0 x0C55F5EA , 0 x1DADF43E , 0 x233F7061 , 0 x3372F092 , 0 x8D937E41 , 0 xD65FECF1 , 0 x6C223BDB , 0 x7CDE3759 , 0 xCBEE7460 , 0 x4085F2A7 , 0 xCE77326E , 0 xA6078084 , 0 x19F8509E , 0 xE8EFD855 , 0 x61D99735 , 0 xA969A7AA , 0 xC50C06C2 , 0x5A04ABFC , 0 x800BCADC , 0 x9E447A2E , 0 xC3453484 , 0 xFDD56705 , 0 x0E1E9EC9 , 0 xDB73DBD3 , 0 x105588CD , 0 x675FDA79 , 0 xE3674340 , 0 xC5C43465 , 0 x713E38D8 , 0 x3D28F89E , 0 xF16DFF20 , 0 x153E21E7 , 0 x8FB03D4A , 0 xE6E39F2B , 0 xDB83ADF7 }; const unsigned long i n t s b l o c k 2 [ 2 5 6 ] = { 0xE93D5A68 , 0 x948140F7 , 0 xF64C261C , 0 x94692934 , 0 x411520F7 , 0 x7602D4F7 , 0 xBCF46B2E , 0xD4A20068 , 0 xD4082471 , 0 x3320F46A , 0 x43B7D4B7 , 0 x500061AF , 0 x1E39F62E , 0 x97244546 , 0 x14214F74 , 0 xBF8B8840 , 0 x4D95FC1D , 0 x96B591AF , 0 x70F4DDD3 , 0 x66A02F45 , 0 xBFBC09EC , 0x03BD9785 , 0 x7FAC6DD0 , 0 x31CB8504 , 0 x96EB27B3 , 0 x55FD3941 , 0 xDA2547E6 , 0 xABCA0A9A , 0 x28507825 , 0 x530429F4 , 0 x0A2C86DA , 0 xE9B66DFB , 0 x68DC1462 , 0 xD7486900 , 0 x680EC0A4 , 0x27A18DEE , 0 x4F3FFEA2 , 0 xE887AD8C , 0 xB58CE006 , 0 x7AF4D6B6 , 0 xAACE1E7C , 0 xD3375FEC , 0xCE78A399 , 0 x406B2A42 , 0 x20FE9E35 , 0 xD9F385B9 , 0 xEE39D7AB , 0 x3B124E8B , 0 x1DC9FAF7 , 0x4B6D1856 , 0 x26A36631 , 0 xEAE397B2 , 0 x3A6EFA74 , 0 xDD5B4332 , 0 x6841E7F7 , 0 xCA7820FB , 0xFB0AF54E , 0 xD8FEB397 , 0 x454056AC , 0 xBA489527 , 0 x55533A3A , 0 x20838D87 , 0 xFE6BA9B7 , 0xD096954B , 0 x55A867BC , 0 xA1159A58 , 0 xCCA92963 , 0 x99E1DB33 , 0 xA62A4A56 , 0 x3F3125F9 , 0x5EF47E1C , 0 x9029317C , 0 xFDF8E802 , 0 x04272F70 , 0 x80BB155C , 0 x05282CE3 , 0 x95C11548 , 0xE4C66D22 , 0 x48C1133F , 0 xC70F86DC , 0 x07F9C9EE , 0 x41041F0F , 0 x404779A4 , 0 x5D886E17 , 0 x325F51EB , 0 xD59BC0D1 , 0 xF2BCC18F , 0 x41113564 , 0 x257B7834 , 0 x602A9C60 , 0 xDFF8E8A3 , 0 x1F636C1B , 0 x0E12B4C2 , 0 x02E1329E , 0 xAF664FD1 , 0 xCAD18115 , 0 x6B2395E0 , 0 x333E92E1 , 0 x3B240B62 , 0 xEEBEB922 , 0 x85B2A20E , 0 xE6BA0D99 , 0 xDE720C8C , 0 x2DA2F728 , 0 xD0127845 , 0x95B794FD , 0 x647D0862 , 0 xE7CCF5F0 , 0 x5449A36F , 0 x877D48FA , 0 xC39DFD27 , 0 xF33E8D1E , 0 x0A476341 , 0 x992EFF74 , 0 x3A6F6EAB , 0 xF4F8FD37 , 0 xA812DC60 , 0 xA1EBDDF8 , 0 x991BE14C , 0xDB6E6B0D , 0 xC67B5510 , 0 x6D672C37 , 0 x2765D43B , 0 xDCD0E804 , 0 xF1290DC7 , 0 xCC00FFA3 , 0 xB5390F92 , 0 x690FED0B , 0 x667B9FFB , 0 xCEDB7D9C , 0 xA091CF0B , 0 xD9155EA3 , 0 xBB132F88 , 0x515BAD24 , 0 x7B9479BF , 0 x763BD6EB , 0 x37392EB3 , 0 xCC115979 , 0 x8026E297 , 0 xF42E312D , 0x6842ADA7 , 0 xC66A2B3B , 0 x12754CCC , 0 x782EF11C , 0 x6A124237 , 0 xB79251E7 , 0 x06A1BBE6 , 0x4BFB6350 , 0 x1A6B1018 , 0 x11CAEDFA , 0 x3D25BDD8 , 0 xE2E1C3C9 , 0 x44421659 , 0 x0A121386 , 0xD90CEC6E , 0 xD5ABEA2A , 0 x64AF674E , 0 xDA86A85F , 0 xBEBFE988 , 0 x64E4C3FE , 0 x9DBC8057 , 0 xF0F7C086 , 0 x60787BF8 , 0 x6003604D , 0 xD1FD8346 , 0 xF6381FB0 , 0 x7745AE04 , 0 xD736FCCC , 0 x83426B33 , 0 xF01EAB71 , 0 xB0804187 , 0 x3C005E5F , 0 x77A057BE , 0 xBDE8AE24 , 0 x55464299 , 0 xBF582E61 , 0 x4E58F48F , 0 xF2DDFDA2 , 0 xF474EF38 , 0 x8789BDC2 , 0 x5366F9C3 , 0 xC8B38E74 , 0 xB475F255 , 0 x46FCD9B9 , 0 x7AEB2661 , 0 x8B1DDF84 , 0 x846A0E79 , 0 x915F95E2 , 0 x466E598E , 0 x20B45770 , 0 x8CD55591 , 0 xC902DE4C , 0 xB90BACE1 , 0 xBB8205D0 , 0 x11A86248 , 0 x7574A99E , 0xB77F19B6 , 0 xE0A9DC09 , 0 x662D09A1 , 0 xC4324633 , 0 xE85A1F02 , 0 x09F0BE8C , 0 x4A99A025 , 0x1D6EFE10 , 0 x1AB93D1D , 0 x0BA5A4DF , 0 xA186F20F , 0 x2868F169 , 0 xDCB7DA83 , 0 x573906FE ,

Page 39 0xA1E2CE9B , 0 x4FCD7F52 , 0 x50115E01 , 0 xA70683FA , 0 xA002B5C4 , 0 x0DE6D027 , 0 x9AF88C27 , 0 x773F8641 , 0 xC3604C06 , 0 x61A806B5 , 0 xF0177A28 , 0 xC0F586E0 , 0 x006058AA , 0 x30DC7D62 , 0x11E69ED7 , 0 x2338EA63 , 0 x53C2DD94 , 0 xC2C21634 , 0 xBBCBEE56 , 0 x90BCB6DE , 0 xEBFC7DA1 , 0xCE591D76 , 0 x6F05E409 , 0 x4B7C0188 , 0 x39720A3D , 0 x7C927C24 , 0 x86E3725F , 0 x724D9DB9 , 0x1AC15BB4 , 0 xD39EB8FC , 0 xED545578 , 0 x08FCA5B5 , 0 xD83D7CD3 , 0 x4DAD0FC4 , 0 x1E50EF5E , 0 xB161E6F8 , 0 xA28514D9 , 0 x6C51133C , 0 x6FD5C7E7 , 0 x56E14EC4 , 0 x362ABFCE , 0 xDDC6C837 , 0xD79A3234 , 0 x92638212 , 0 x670EFA8E , 0 x406000E0 }; const unsigned long i n t s b l o c k 3 [ 2 5 6 ] = { 0x3A39CE37 , 0 xD3FAF5CF , 0 xABC27737 , 0 x5AC52D1B , 0 x5CB0679E , 0 x4FA33742 , 0 xD3822740 , 0x99BC9BBE , 0 xD5118E9D , 0 xBF0F7315 , 0 xD62D1C7E , 0 xC700C47B , 0 xB78C1B6B , 0 x21A19045 , 0xB26EB1BE , 0 x6A366EB4 , 0 x5748AB2F , 0 xBC946E79 , 0 xC6A376D2 , 0 x6549C2C8 , 0 x530FF8EE , 0x468DDE7D , 0 xD5730A1D , 0 x4CD04DC6 , 0 x2939BBDB , 0 xA9BA4650 , 0 xAC9526E8 , 0 xBE5EE304 , 0xA1FAD5F0 , 0 x6A2D519A , 0 x63EF8CE2 , 0 x9A86EE22 , 0 xC089C2B8 , 0 x43242EF6 , 0 xA51E03AA , 0x9CF2D0A4 , 0 x83C061BA , 0 x9BE96A4D , 0 x8FE51550 , 0 xBA645BD6 , 0 x2826A2F9 , 0 xA73A3AE1 , 0 x4BA99586 , 0 xEF5562E9 , 0 xC72FEFD3 , 0 xF752F7DA , 0 x3F046F69 , 0 x77FA0A59 , 0 x80E4A915 , 0 x87B08601 , 0 x9B09E6AD , 0 x3B3EE593 , 0 xE990FD5A , 0 x9E34D797 , 0 x2CF0B7D9 , 0 x022B8B51 , 0x96D5AC3A , 0 x017DA67D , 0 xD1CF3ED6 , 0 x7C7D2D28 , 0 x1F9F25CF , 0 xADF2B89B , 0 x5AD6B472 , 0 x5A88F54C , 0 xE029AC71 , 0 xE019A5E6 , 0 x47B0ACFD , 0 xED93FA9B , 0 xE8D3C48D , 0 x283B57CC , 0 xF8D56629 , 0 x79132E28 , 0 x785F0191 , 0 xED756055 , 0 xF7960E44 , 0 xE3D35E8C , 0 x15056DD4 , 0x88F46DBA , 0 x03A16125 , 0 x0564F0BD , 0 xC3EB9E15 , 0 x3C9057A2 , 0 x97271AEC , 0 xA93A072A , 0x1B3F6D9B , 0 x1E6321F5 , 0 xF59C66FB , 0 x26DCF319 , 0 x7533D928 , 0 xB155FDF5 , 0 x03563482 , 0x8ABA3CBB , 0 x28517711 , 0 xC20AD9F8 , 0 xABCC5167 , 0 xCCAD925F , 0 x4DE81751 , 0 x3830DC8E , 0 x379D5862 , 0 x9320F991 , 0 xEA7A90C2 , 0 xFB3E7BCE , 0 x5121CE64 , 0 x774FBE32 , 0 xA8B6E37E , 0 xC3293D46 , 0 x48DE5369 , 0 x6413E680 , 0 xA2AE0810 , 0 xDD6DB224 , 0 x69852DFD , 0 x09072166 , 0xB39A460A , 0 x6445C0DD , 0 x586CDECF , 0 x1C20C8AE , 0 x5BBEF7DD , 0 x1B588D40 , 0 xCCD2017F , 0x6BB4E3BB , 0 xDDA26A7E , 0 x3A59FF45 , 0 x3E350A44 , 0 xBCB4CDD5 , 0 x72EACEA8 , 0 xFA6484BB , 0x8D6612AE , 0 xBF3C6F47 , 0 xD29BE463 , 0 x542F5D9E , 0 xAEC2771B , 0 xF64E6370 , 0 x740E0D8D , 0 xE75B1357 , 0 xF8721671 , 0 xAF537D5D , 0 x4040CB08 , 0 x4EB4E2CC , 0 x34D2466A , 0 x0115AF84 , 0 xE1B00428 , 0 x95983A1D , 0 x06B89FB4 , 0 xCE6EA048 , 0 x6F3F3B82 , 0 x3520AB82 , 0 x011A1D4B , 0 x277227F8 , 0 x611560B1 , 0 xE7933FDC , 0 xBB3A792B , 0 x344525BD , 0 xA08839E1 , 0 x51CE794B , 0 x2F32C9B7 , 0 xA01FBAC9 , 0 xE01CC87E , 0 xBCC7D1F6 , 0 xCF0111C3 , 0 xA1E8AAC7 , 0 x1A908749 , 0xD44FBD9A , 0 xD0DADECB, 0 xD50ADA38 , 0 x0339C32A , 0 xC6913667 , 0 x8DF9317C , 0 xE0B12B4F , 0 xF79E59B7 , 0 x43F5BB3A , 0 xF2D519FF , 0 x27D9459C , 0 xBF97222C , 0 x15E6FC2A , 0 x0F91FC71 , 0 x9B941525 , 0 xFAE59361 , 0 xCEB69CEB , 0 xC2A86459 , 0 x12BAA8D1 , 0 xB6C1075E , 0 xE3056A0C , 0 x10D25065 , 0 xCB03A442 , 0 xE0EC6E0E , 0 x1698DB3B , 0 x4C98A0BE , 0 x3278E964 , 0 x9F1F9532 , 0xE0D392DF , 0 xD3A0342B , 0 x8971F21E , 0 x1B0A7441 , 0 x4BA3348C , 0 xC5BE7120 , 0 xC37632D8 , 0xDF359F8D , 0 x9B992F2E , 0 xE60B6F47 , 0 x0FE3F11D , 0 xE54CDA54 , 0 x1EDAD891 , 0 xCE6279CF , 0xCD3E7E6F , 0 x1618B166 , 0 xFD2C1D05 , 0 x848FD2C5 , 0 xF6FB2299 , 0 xF523F357 , 0 xA6327623 , 0 x93A83531 , 0 x56CCCD02 , 0 xACF08162 , 0 x5A75EBB5 , 0 x6E163697 , 0 x88D273CC , 0 xDE966292 , 0x81B949D0 , 0 x4C50901B , 0 x71C65614 , 0 xE6C6C7BD , 0 x327A140A , 0 x45E1D006 , 0 xC3F27B9A , 0xC9AA53FD , 0 x62A80F00 , 0 xBB25BFE2 , 0 x35BDD2F6 , 0 x71126905 , 0 xB2040222 , 0 xB6CBCF7C , 0xCD769C2B , 0 x53113EC0 , 0 x1640E3D3 , 0 x38ABBD60 , 0 x2547ADF0 , 0 xBA38209C , 0 xF746CE76 , 0x77AFA1C5 , 0 x20756060 , 0 x85CBFE4E , 0 x8AE88DD8 , 0 x7AAAF9B0 , 0 x4CF9AA7E , 0 x1948C25C , 0x02FB8A8C , 0 x01C36AE4 , 0 xD6EBE1F9 , 0 x90D4F869 , 0 xA65CDEA0 , 0 x3F09252D , 0 xC208E69F , 0 xB74E6132 , 0 xCE77E25B , 0 x578FDFE3 , 0 x3AC372E6 };

Page 40 // F f u n c t i o n i n t h e a l g o r i t h m o f B l o w f i s h unsigned long i n t f f u n c t i o n ( unsigned long i n t e n t e r ) { unsigned long i n t r e s u l t = 0 ; r e s u l t = s b l o c k 0 [ ( e n t e r >> 2 4 ) & 0 x000000FF ] ; r e s u l t = ( r e s u l t + s b l o c k 1 [ ( e n t e r >> 1 6 ) & 0 x000000FF ] ) % 2 ˆ 3 2 ; r e s u l t = r e s u l t ˆ s b l o c k 2 [ ( e n t e r >> 8 ) & 0 x000000FF ] ; r e s u l t = ( r e s u l t + s b l o c k 3 [ e n t e r & 0 x000000FF ] ) % 2 ˆ 3 2 ; return ( r e s u l t ) ; } // F u n c t i o n A l g o r i t h m b l o w f i s h t o d e c r y p t short decrypt () { // V a r i a b l e t o make c ha n ge unsigned long i n t c ha ng e = 0 ; // V a r i a b l e f o r i n t e r a c t i o n int i = 17; // I t e r a c t i o n f o r ( i = 1 7 ; i > 1 ; i −=1) { D left = D left ˆ p array [ i ] ; D right = D right ˆ f function ( D left ); // Swap change = D r i g h t ; D right = D left ; D l e f t = c ha n ge ; } // L a s t Swap change = D r i g h t ; D right = D left ; D l e f t = c ha n ge ; // L a s t I t e r a c t i o n D right = D right ˆ p array [ 1 ] ; D left = D left ˆ p array [ 0 ] ; return 1; } // I2C S e r i a l Communication unsigned s h o r t r e a d d s 1 3 0 7 ( unsigned s h o r t a d d r e s s ) { // D e c l a r a t i o n o f v a r i a b l e unsigned s h o r t r e a d d a t a ; // S t a r t I2C c o m m u n i c a t i o n I2C1 Start (); // R e q u e s t t o w r i t e

Page 41 I2C1 Wr ( 0 xD0 ) ; // The a d d r e s s I2C1 Wr ( a d d r e s s ) ; // R e q u e s t t o r e a d I2C1 Repeated Start ( ) ; I2C1 Wr ( 0 xD1 ) ; r e a d d a t a=I2C1 Rd ( 0 ) ; // Stop I2C c o m m u n i c a t i o n I2C1 Stop ( ) ; // R e t u r n t h e v a l u e r e a d return ( read data ) ; } // r e a d s i g n a l r e m o t e c o n t r o l short nec remote read () { // v a r i a b l e u s e d t o c h e c k t h e t i m e o f t h e NEC p r o t o c o l u s i n g d e l a y unsigned i n t c o u n t = 0 ; s i g n e d i n t i =31; // Check 9ms p u l s e ( r e m o t e c o n t r o l s e n d s l o g i c low ) w h i l e ( INPUT == 0 & c o u n t < 200 ) { c o u n t ++; delay us (50); } // NEC p r o t o c o l c h e c k i n g i f n o t low between 7000ms and 9000ms i f ( c o u n t > 180 | c o u n t < 140 ) { r e t u r n 0 ; } count = 0; // Check 4 . 5 ms s p a c e ( r e m o t e c o n t r o l s e n d s l o g i c h i g h ) w h i l e ( INPUT == 1 & c o u n t < 100 ) { c o u n t ++; delay us (50); } // NEC p r o t o c o l c h e c k i n g i f n o t h i g h between 3500ms i f ( c o u n t > 90 | c o u n t < 70 ) { return 0; } // Read code message (32− b i t ) f o r ( i =31; i >=0; i −−) { count = 0; // Nec p r o t o c o l c h e c k i n g t h e p a r t low o f t h e IR s i g n a l w h i l e ( INPUT == 0 & c o u n t < 14 ) { c o u n t ++; delay us (50); }

and

return 0

4500ms r e t u r n 0

Page 42

// NEC p r o t o c o l c h e c k i n g i f n o t low between 200 u s i f ( c o u n t > 12 | c o u n t < 4 ) { return 0; }

and

600 u s

return 0

count = 0; // Nec p r o t o c o l c h e c k i n g t h e p a r t h i g h o f t h e IR s i g n a l and t h e meaning w h i l e ( INPUT == 1 & c o u n t < 40 ) { c o u n t ++; delay us (50); } // NEC p r o t o c o l c h e c k i n g i f n o t h i g h between 200 u s and 1700 u s r e t u r n 0 i f ( c o u n t > 35 | c o u n t < 4 ) { return 0;} // NEC p r o t o c o l c h e c k i n g if h i g h between 600 u s and 1000 u s r e t u r n 0 i f ( c o u n t < 20 & c o u n t > 12 ) { return 0;} // Nec p r o t o c o l C h e c k i n g t h e a d d r e s s 0 x00FF o f IR S i g n a l from // t h e 31 t h b i t t o 0 b i t // NEC p r o t o c o l c h e c k i n g i f n o t h i g h between 200 u s and 600 u s // f o r 31 t h b i t t o 24 t h r e t u r n 0 i f ( i =24){ i f ( ! ( c o u n t > 4 & c o u n t < 12 ) ) { r e t u r n 0 ; } } // NEC p r o t o c o l c h e c k i n g i f n o t h i g h between 1000 u s and 1700 u s // f o r 23 t h b i t t o 16 t h r e t u r n 0 i f ( i =16){ i f ( ! ( c o u n t > 20 & c o u n t < 35 ) ) { r e t u r n 0 ; } } // G e t t i n g IR s i g n a l from t h e 15 t h b i t t o 0 b i t // NEC p r o t o c o l c h e c k i n g i f h i g h between i f ( ( c o u n t > 2 0 ) & ( c o u n t < 3 5 ) & i