## 3 The Proposed Fog-Based MQTT Architecture with an Authentication Scheme
### Authentication System
#### 3.2.2 Lightweight Authentication Scheme
At the end, the AM computes the message digest of the master identity of client H (MID\({}_{\text{C}}\)), then sends AID\({}_{\text{C}}\), SK\({}_{\text{AM}\_{C}}\), and H (MID\({}_{\text{C}}\)) to C and SK\({}_{\text{AM}\_{B}}\) to B. \[\text{AID}_{\text{C}}=\text{H (ID${}_{\text{C}}$ \oplus$ SN)}\] (1) \[\text{MID}_{\text{C}}=\text{AID${}_{\text{C}}$ \oplus$ MK1}\] (2) \[\text{SK}_{\text{AM}\_{C}}=\text{MID${}_{\text{C}}$ \oplus$ MK2}\] (3) \[\text{Hash1}=\text{H (MID${}_{\text{C}}$)}\] (4) \[\text{AID${}_{\text{B}}$}=\text{H (ID${}_{\text{B}}$ \oplus$ SN)}\] (5) \[\text{MID}_{\text{B}}=\text{AID${}_{\text{B}}$ \oplus$ MK1}\] (6) \[\text{SK}_{\text{AM}\_{B}}=\text{MID${}_{\text{B}}$ \oplus$ MK2}\] (7)
* Authentication Phase To begin the authentication process, C generates a random number (R1), then computes and sends the ciphers C1, C2, and C3 as well as AID\({}_{\text{C}}\) and Hash2 to B to establish the connection. The ciphers and Hash2 are calculated according to the following equations: \[\text{C1}=\text{SK}_{\text{AM}\_{C}}\text{\ \ }\text{\ }\textAfter the successful matching of Hash2 and Hash2\({}^{\prime}\), C needs to prove the authenticity of the AM. Therefore, the AM generates nonce (Na), then computes and sends the cipher Verifier and Hash3 to C according to the following equations:  
\[\text{R2}=\text{H}\left(\text{R1}\ \mid\ \mid\ \text{H}\left(\text{MID}_{\text{C}} \right)\right) \tag{19}\]  
\[\text{Verifier}=\text{R2}\oplus\text{Na} \tag{20}\]  
\[\text{AID}_{\text{C}}=\text{H (ID${}_{\text{C}}$ \oplus$ SN)}\]0  
C, in turn, calculates Hash3\({}^{\prime}\), compares it with the received Hash3, and extracts Na using the equations presented below. If both hashes (Hash3 and Hash3\({}^{\prime}\)) are matched, the AM is verified by C.  
\[\text{AID}_{\text{C}}=\text{H (ID${}_{\text{C}}$ \oplus$ SN)}\]1  
\[\text{AID}_{\text{C}}=\text{H (ID${}_{\text{C}}$ \oplus$ SN)}\]2  
\[\text{AID}_{\text{C}}=\text{H (ID${}_{\text{C}}$ \oplus$ SN)}\]3  
To start the mutual authentication between C and B, the AM computes Hash4 using the secret key of B that was previously created in the registration phase (SKAM_B) and the following ciphers. Then, it sends C5, C7, and Hash4 to B.  
\[\text{AID}_{\text{C}}=\text{H (ID${}_{\text{C}}$ \oplus$ SN)}\]4  
\[\text{AID}_{\text{C}}=\text{H (ID${}_{\text{C}}$ \oplus$ SN)}\]5  
\[\text{AID}_{\text{C}}=\text{H (ID${}_{\text{C}}$ \oplus$ SN)}\]6  
\[\text{AID}_{\text{C}}=\text{H (ID${}_{\text{C}}$ \oplus$ SN)}\]7  
\[\text{AID}_{\text{C}}=\text{H (ID${}_{\text{C}}$ \oplus$ SN)}\]8  
At the same time, [PERSON] computes Hash5 using ID\({}_{\text{C}}\), ID\({}_{\text{B}}\), and Na and sends it to B.  
\[\text{AID}_{\text{C}}=\text{H (ID${}_{\text{C}}$ \oplus$ SN)}\]9  
Upon receiving C5, C7, and Hash4, [PERSON] computes Hash4' and compares it with the received Hash4. If both hashes are identical, the AM is authenticated by [PERSON] After that, [PERSON] computes Hash5' according to the following equations and compares it with the Hash5 received from [PERSON]. \[\text{MID}_{\text{C}}=\text{AID${}_{\text{C}}$ \oplus$ MK1}\]0  
\[\text{MID}_{\text{C}}=\text{AID${}_{\text{C}}$ \oplus$ MK1}\]1  
\[\text{MID}_{\text{C}}=\text{AID${}_{\text{C}}$ \oplus$ MK1}\]2  
If both hashes (Hash5 and Hash5') are matched, B authenticates C. In the next step, B generates a random number (R3) to compute the cipher C8 and Hash6, as shown in the following equations, and sends them to C.  
\[\text{MID}_{\text{C}}=\text{AID${}_{\text{C}}$ \oplus$ MK1}\]3  
\[\text{MID}_{\text{C}}=\text{AID${}_{\text{C}}$ \oplus$ MK1}\]4  
\[\text{MID}_{\text{C}}=\text{AID${}_{\text{C}}$ \oplus$ MK1}\]5  
\[\text{MID}_{\text{C}}=\text{AID${}_{\text{C}}$ \oplus$ MK1}\]6  
In turn, C computes Hash6' using the following equations and compares it with the received Hash6. \[\text{MID}_{\text{C}}=\text{AID${}_{\text{C}}$ \oplus$ MK1}\]7  
\[\text{MID}_{\text{C}}=\text{AID${}_{\text{C}}$ \oplus$ MK1}\]8Hash6 = H (C8 | | C9 | | R3)  
If Hash6 matches Hash6, C authenticates B, and thus, mutual authentication is achieved successfully.