## 2 Materials and Methods
### System Architecture
#### 2.2.4 Security and Encryption  
Controlling relays via SMS messages poses a security risk, as anyone with access to the mobile number can potentially turn on or off the relay. To address this issue, we propose a secure relay control system, using an OTP algorithm and AES encryption. To make sure that only authorized users may turn on or off the relay, the system utilizes a one-time password, the so-called OTP technique. The OTP is more secure against possible attacks, since it is created or stored locally on the microcontroller and is not reliant on the current time or sent via SMS. To increase security even further, the OTP is encrypted using the AES encryption, using a shared key. SMS messages delivered to the GSM shield, which talks with the microcontroller, trigger the relay depending on the OTP and shared key.  
A microcontroller board and a GSM shield are used in the system, to connect to the mobile network. By sending an SMS message to the GSM shield, the relay, which is attached to the relay (actuator) pin, may be switched on or off. The SMS message is made up of a frame that includes the relay status, shared key, and OTP (on or off).  
A random number generator is used locally, to create the OTP, that is stored on the chip and available only for the sender. The OTP is more resistant to possible assaults, since it is a 24-bit value that is maintained in memory, and is not reliant on the current time. The OTP is delivered to the microcontroller, together with the shared key, when a user sends an SMS to activate or deactivate the relay.  
With AES encryption, the shared key is a 256-bit key. The OTP and relay status, as shown in Figure 12, are encrypted using the shared key, which is only known to the sender and recipient of the SMS message. The microcontroller receives the encrypted frame and uses the shared key to decode it, before verifying the OTP. Depending on the relay status in the frame and if the OTP is valid, the Arduino switches the relay on or off.  
The full frame would consist of:  
* 8 bits for the state (0 for off, 00001111 for on);
* 24 bits for the OTP;
* 256 bits for the shared key using AES encryption. The complete frame size would be:  
8 bits + 24 bits + 256 bits = 288 bits  
Or, in bytes:  
(8 bits + 24 bits + 256 bits)/8 = 36 bytes  
Here is an example of AES encryption of a 288-bit frame message, with the following structure:  
18-bit command 124-bit OTP 256-bit AES encrypted payload |  
Figure 12: Improving relay control security using AES encryption.  
With the 8-bit command, you can control whether the relay is on or off (command = 0x0F or 0x00). The one-time password (OTP) is a 24-bit number created by the sender and stored on the microcontroller. The payload is encrypted using the advanced encryption standard (AES) method, with a shared secret key, utilizing the remaining bits (256 bits).  
To encrypt the payload, we concatenate the command, secret key, and OTP into a single string as the initial stage. For example, if the command is 0x0F, the OTP is 0x123456, and the shared secret key is \"my5 ecr3 key\", the resulting string would then be \"my5 ecr3 key0F123456\". As the second step, the shared secret key is then used to create an AES key, using a key derivation algorithm (KDF). For instance, we may generate a 256-bit AES key using the PBKDF2 algorithm. As a final step, the final 256-bit encrypted payload is created by encrypting the concatenated string, using the AES algorithm and the generated/stored key. As an example of an SMS, the following values would be used to build a 288-bit frame, that would be used to send an SMS message to the relay to activate it:! 0x0F! 0x12 ab5c! 0d6e615e72c50b8a156789a637a114c5f8d14 drg58127a524e16621d10d3f3f9e!  
The OTP would be extracted from the frame when the message was received by the recipient, who would then compare it to the OTP that was kept locally. If the OTP is legitimate, it would use the shared secret key to decode the payload and carry out the relay-activation command.