# A Blockchain-Based Secure Inter-Hospital EMR Sharing System
## 3 The Proposed Scheme
### Initial Treatment Authentication and Communication Phase  
When the patient carries his/her mobile device to hospital A to seek medical services, hospital A and the patient must authenticate one another's identity first. After mutual authentication between the patient and hospital A, the doctor of hospital A diagnoses the patient. After diagnosis, the doctor of hospital A stores the medical records of the patient in the server of hospital A, and the doctor of hospital A will also inform the patient about the results. The patient keeps the diagnostic results and medical index in his/her mobile device. The initial treatment authentication and communication phase of the proposed scheme is shown in Figure 5.  
* The patient chooses a random number, \(a\), calculated by \[T_{PAT}=aP\] (14) and then sends \((ID_{PAT},R_{PAT},T_{PAT})\) to hospital A.
* Hospital A chooses a random number, \(b\), calculated by \[T_{HPA}=bP,\] (15) \[PK_{PAT}=R_{PAT}+H_{1}(ID_{PAT},R_{PAT})PK,\] (16) \[K_{AP1}=S_{HPA}T_{PAT}+bPK_{PAT},\] (17) \[K_{AP2}=bT_{PAT},\] (18) and the session key \[SEK_{AP}=H_{2}(K_{AP1},K_{AP2}).\] (19) Hospital A then calculates \[CHK_{PA}=H_{3}(SEK_{AP},T_{PAT}),\] (20) and sends \((ID_{HPA},R_{HPA},T_{HPA},CHK_{PA})\) to the patient.
* The patient calculates \[PK_{HPA}=R_{HPA}+H_{1}(ID_{HPA},R_{HPA})PK,\] (21) \[K_{PA1}=S_{PAT}T_{HPA}+aPK_{HPA},\] (22) \[K_{PA2}=aT_{HPA},\] (23) and the session key \[SEK_{AP}=H_{2}(K_{PA1},K_{PA2}).\] (24)The patient then verifies  
\[CHK_{PA}\overset{?}{=}H_{3}(SEK_{AP},T_{PAT}) \tag{25}\]  
to check the legality of hospital A. If the verification passes, the patient calls the smart contract hachk as follows:  
\begin{tabular}{|p{113.8 pt}||p{113.8 pt}||p{113.8 pt}|} \hline function hospital A check smart contract hachk & \multirow{2}{*}{\(\left\|\begin{array}{c}\text{return hospital A id. exist;}\\ \text{string hospital A id,}\\ \text{string hospital A detail) }\{\}\end{array} } & \multirow{2}{*}{\(\left\|\begin{array}{c}\text{return hospital A id. exist;}\\ \text{return hospital A detail. exist;}\\ \text{return hospital A detail. exist;}\\ \text{}\end{array}\right.\)} \\ \hline \end{tabular}  
The patient then calculates  
\[c_{PAT}=E_{SEK_{AP}}(\text{message}) \tag{26}\]  
\[CHK_{AP}=H_{3}(SEK_{AP},T_{HPA}) \tag{27}\]  
and sends \((ID_{PAT},c_{PAT},CHK_{AP})\) to hospital A.
* Hospital A verifies \[CHK_{AP}\overset{?}{=}H_{3}(SEK_{AP},T_{HPA}),\] (28)  
to check the legality of the patient. If the verification passes, the session key \(SEK_{AP}\) between the patient and hospital A is established successfully. Hospital A calls the smart contract ptchk as follows:  
\begin{tabular}{|p{113.8 pt}||p{113.8 pt}|} \hline return patient id. exist; & \multirow{2}{*}{\(\left\|\begin{array}{c}\text{function patient check smart contract ptchk(}\\ \text{string patient id,}\\ \text{string patient id,}\\ \text{string patient detail,}\\ \text{mapping medical record. exist;}\\ \text{mapping medical record) }\{\}\end{array}\right.\)} \\ \hline \end{tabular}  
Hospital A then decrypts the received message  
\[\text{message}=D_{SEK_{AP}}(c_{PAT}), \tag{29}\]  
to obtain the information about the patient's symptoms. After the diagnosis of the patient, hospital A stores the medical records of the patient in the server and generates the encrypted basic inspection report and certificate \(\text{Cert}_{HPA}\)  
\[c_{HPA}=E_{SEK_{AP}}(EMR,\text{Cert}_{HPA}), \tag{30}\]  
\[Sig_{HPA}=S_{SK_{HPA}}(EMR,\text{Cert}_{HPA}), \tag{31}\]  
If the identity \(ID_{PAT}\) is valid, the hospital A calls the smart contract mrins and ptins as follows:and sends \((ID_{HPA},c_{HPA},Sig_{HPA})\) to the patient.
* The patient decrypts the received message, \[(EMR,Cert_{HPA})=D_{SEK_{AP}}(c_{HPA}),\] (32) verifies the signature, \[(EMR,Cert_{HPA})\stackrel{{?}}{{=}}V_{PK_{HPA}}(Sig_{HPA}).\] (33) and receives the encrypted basic inspection report from hospital A.  
Figure 5: The initial treatment authentication and communication phase of the proposed scheme.