## 2 Materials and Methods
### Proposed Scheme
#### 2.5.4 Authentication Phase  
When the HGWN receives the login message, it checks to see if the \(SN_{ID}\) is stored in the HGWN database. If \(SN_{ID}\) is in the database, Case 1 will be down. Otherwise, it performs Case 2. Figures 9 and 10 depict distinct procedures individually for the two cases. **Case 1:**  
* The HGWN verifies the \(TS_{1}\) by selecting a new timestamp \(TS_{2}\) to check the freshness \(|TS_{2}-TS_{1}|\leq\triangle T\), where \(\triangle T\) is the current timestamp. It calculates \(SK_{i}^{*}=h(HGW_{ID}\parallel PID_{i}\parallel HGW_{MSK})\) based on the stored information in its database. After that, it decrypts \(CT_{i}=D_{SK_{i}}[HGW_{ID}^{*},SN_{ID}^{*},W_{i}^{*},TS_{1}^{*}]\), where \(D_{SK_{i}}\) depicts the decryption of a symmetric key using the key \(SK_{i}\). After retrieving the information, HGWN verifies the timestamp \(|TS_{1}^{*}-TS_{1}^{*}|\leq\triangle T\), where \(TS_{1}^{*}\) is the message receiving time. If it holds, HGWN checks \(HGW_{ID}^{*}\
eq HGW_{ID}\), and \(SN_{ID}^{*}\
eq SN_{ID}\), and if these parameters are valid, it computes \(W_{i}^{(**)}=h(PID_{i}\parallel TID_{i}\parallel N_{i}^{*})\) based on the stored \(PID_{i}\), and \(TID_{i}\), then checks \(W_{i}^{(**)}\
eq W_{i}^{*}\), if it does not hold, it terminates the session. Otherwise, it selects a random nonce \(N_{i}\) to compute a shared secret key with the sensor node \(SK_{j}=h(HGW_{ID}\parallel SN_{ID}\parallel HGW_{MSK})=h(SN_{ID}\parallel SN _{MSK})\), \(P_{i}=h(PID_{i}\parallel N_{j}\parallel N_{i}^{*}\parallel TS_{2})\), and \(CT_{j}=E_{SK_{j}}[HGW_{ID},SN_{ID},PID_{i},N_{i}^{*},N_{j},P_{i},TS_{2}]\), and sends an authentication message \(M_{2}=SN_{ID},CT_{j}\) to the sensor node via a public channel. * The sensor node \(SN_{i}\) receives the message and decrypts \(CT_{j}=D_{A_{i}}[HGW_{ID},SN_{ID},\)\(PID_{i},N_{i}^{*},N_{j},P_{i},TS_{2}]\) using the stored key \(A_{j}=h(SN_{ID}\parallel SN_{MSK})\) stored in the memory to obtain the information. Later, \(SNi\) checks the freshness of the timestamp \(|TS_{2}^{{}^{\prime}}-TS_{2}^{*}|\leq\), where \(TS_{2}^{*}\) is the message \(M_{2}\) received time, if not fresh, terminates the session; otherwise, it computes \(P_{i}^{*}=h(PID_{i}\parallel N_{j}^{\prime}\parallel N_{i}^{\prime}\parallel TS _{2}^{\prime})\) and checks\(P_{i}^{*}\
eq P_{i}^{\prime}\), if it does not hold, \(SN_{i}\) terminates the session. After that, \(SN_{i}\) calculates \(Z_{i}=h(PID_{i}\oplus N_{i}^{*}\oplus N_{j})\), \(SK_{U\to SN}=h(HGW_{ID}\parallel SN_{ID}\parallel PID_{i}\parallel N_{j})\), where \(SK_{URISM}\) is a shared key between user and sensor node, and \(B_{i}=h(SK_{U\to SN}\parallel TS_{3})\). Then, \(TS_{2}\) sends a replay authentication message \(M_{3}=SN_{ID},B_{i},Z_{i},TS_{3}\) to the user via an open channel. * Upon receiving \(M_{3}\), it checks the freshness of the timestamp \(|TS_{3}-TS_{2}^{*}|\leq\triangle T\), if not fresh, it terminates the session; otherwise, it computes \(N_{j}^{\prime}=Z_{i}\oplus h(PID_{i}^{*}\oplus N_{i})\) using the previously computed \(PID_{i}^{*}=h(U_{ID}\parallel R^{*})\), \(SK_{U\to SN}^{*}=h(HGW_{ID}\parallel SN_{ID}\parallel PID_{i}^{*}\parallel N _{j})\), and \(B_{i}^{*}=h(SK_{U\to SN}^{*}\parallel TS_{3})\). Finally, \(U_{i}\) verifies \(B_{i}^{*}\
eq B_{i}\), if it holds, it ensures that \(U_{i}\) and \(SN_{i}\) share the same session key and store it for the future communication. **Case 2:**  
* The FGWN calculates \(SK_{if}=A_{if}^{*}\oplus h(FGW_{ID}\parallel SN_{ID}\parallel FGW_{MSK})=h(SN_{ ID}\parallel SN_{MSK})\), then it extracts \(PID_{i}\) corresponding to \(TID_{i}\) and generates a nonce number \(N_{f}\), and computes \(V_{i}=h(TID_{i}\parallel N_{f}\parallel TS_{4})\), \(CT_{f}=E_{SK_{if}}[HGW_{ID},FGW_{ID},\)\(SN_{PID},PID_{i},N_{f},V_{i},TS_{4}]\), then it sends \(M_{5}=SN_{ID},CT_{f}\) to the sensor node \(SN_{i}\).