# PiPatient Privacy Copyright Cloud Management
## 3 The Proposed Mechanism
### Algorithms for Key Agreement, Data Encryption/Decryption, and Updating
receive from Patient Pi) { do nothing } // Waiting for the ack from Patient Pi
2.\(R_{Pi}{}^{new}=M_{1}\oplus\)H(\(T_{CA}\)) // Deciphers random number
3.\(ID_{Pi}=NID_{Pi}\oplus R_{Pi}{}^{new}\) // Retrieves patient's ID
4. if{ exist (\(ID_{Pi}\))} exit() // Checks if\(ID_{Pi}\) is illegal or not, if not exist session
5.\(ID_{Dj}=NID_{Dj}\oplus R_{Pi}{}^{new}\) // Retrieves doctor's ID
6.\(M_{2}=R_{Pi}{}^{new}\oplus\)H(\(ID_{Dj}\oplus T_{CA}\)) // Builds exchange message \(M_{2}\)
7.\(M_{3}=R_{Pi}{}^{new}\oplus\)H(\(ID_{Dj}\oplus T_{CA}\)) // Builds exchange message \(M_{3}\)
8.Sends { \(\{\) \(NID_{Pi}\), \(NID_{Dj}\), \(M_{2}\)}, EPRS // Sends communication message to EPRS
9.Sends { \(\{\) \(NID_{Pi}\), \(M_{3}\) }, \(D_{j}\) } // Sends communication message to \(D_{j}\)
10.while{! (receive (\(M_{5}\)))} { do nothing } // Waiting for the response from \(D_{j}\)
11.Sends { \(\{\) \(M_{5}\), whis(\(NID_{Pi}\)) } // Forwards communication message to \(P_{i}\)
```

**Algorithm 3**Procedure of doctor \(\mathrm{D}_{j}\)

```
1.while(! receive from Patient Pi) { do nothing } // Waiting for the ack from Patient Pi
2.\(R_{Pi}{}^{new}=M_{1}\oplus\)H(\(T_{CA}\)) // Deciphers random number
3.\(ID_{Pi}=NID_{Dj}\oplus R_{Pi}{}^{new}\) // Retrieves patient's ID
4. if{ exist (\(ID_{Pi}\))} exit() // Checks if\(ID_{Pi}\) is illegal or not, if not exist session
5.\(ID_{Dj}=NID_{Dj}\oplus R_{Pi}{}^{new}\) // Retrieves doctor's ID
6.\(M_{2}=R_{Pi}{}^{new}\oplus\)H(\(ID_{Dj}\oplus T_{CA}\)) // Builds exchange message \(M_{2}\)
7.\(M_{3}=R_{Pi}{}^{new}\oplus\)H(\(ID_{Dj}\oplus T_{CA}\)) // Builds exchange message \(M_{3}\)
8.Sends { \(\{\) \(NID_{Pi}\), \(NID_{Dj}\), \(M_{2}\)}, EPRS // Sends communication message to EPRS
9.Sends { \(\{\) \(NID_{Pi}\), \(M_{3}\) }, \(D_{j}\) } // Sends communication message to \(D_{j}\)
10.while(! (receive (\(M_{5}\)))) { do nothing } // Waiting for the response from \(D_{j}\)
11.Sends { \(\{\) \(M_{5}\), whis(\(NID_{Pi}\)) } // Forwards communication message to \(P_{i}\)
```

**Algorithm 3**Procedure of doctor \(\mathrm{D}_{j}\)

```
1.while(! receive from Patient Pi) { do nothing } // Waiting for the ack from Patient Pi
2.\(R_{Pi}{}^{new}=M_{1}\oplus\)H(\(T_{CA}\)) // Deciphers random number
3.\(ID_{Pi}=NID_{Dj}\oplus R_{Pi}{}^{new}\) // Retrieves patient's ID
4. if{ exist (\(ID_{Pi}\))} exit() // Checks if\(ID_{Pi}\) is illegal or not, if not exist session
5.\(ID_{Dj}=NID_{Dj}\oplus R_{Pi}{}^{new}\) // Retrieves doctor's ID
6.\(M_{2}=R_{Pi}{}^{new}\oplus\)H(\(ID_{Dj}\oplus T_{CA}\)) // Builds exchange message \(M_{2}\)
7.\(M_{3}=R_{Pi}{}^{new}\
```
1.while(! (receive (M\({}_{2}\))) { do nothing } // Waiting for the ack {\(NID_{Pi}\), \(NID_{Dj}\), \(M_{2}\)} from CA
2.\(R_{Pi}{}^{new}=M_{2}\oplus\)EC // Deciphers random number \(R_{Pi}{}^{new}\)
3.\(ID_{Pi}=NID_{Pi}\oplus R_{Pi}{}^{new}\) // Retrieves patient's ID
4. if (! exist (\(ID_{Pi}\))) exit() // Checks if \(ID_{Pi}\) is illegal or not, if not exist session
5.\(R_{EPRS}=Random()\) // Generates a new random number
6.\(M_{4}=R_{Pi}{}^{new}\oplus R_{EPRS}\) // Builds exchange message \(M_{5}\)
7.Sends { { [ { [ { { { { { { { { { { { { { { }_{}_{}_{j}_{j}}}}}}}}}}}} ] } } // Forwards communication message to \(D_{j}\)
8.while(! (receive (M\({}_{5}\))) { do nothing } // Waiting for the ack [M\({}_{5}\)] from D)
9.\(SK=\)h(\(R_{Pi}{}^{new}\oplus R_{EPRS}\oplus\)(\(M_{5}\oplus R_{EPRS}\))) // Computes session key h(\(R_{Pi}{}^{new}\oplus R_{EPRS}\oplus R_{D}\)) // The following statements for privacy data processing in secure tunnel
10.\(M_{6}=\){\(E_{SK}\)(E\({}_{K}\)(\(R_{i}\))) // EPRS encrypts the privacy record of the \(ID_{pi}\)
11.Sends { { [ { {{\{{ {\{{M_{6}}}}}}}}}, { {P } } }] } // Sends encrypted privacy of \(P_{i}\) to \(P_{i}\)
12.while(! (receive from \(P_{i}\)) { do nothing } // Waiting for the response from \(P_{i}\)
13.\(M_{10}=\)D\({}_{SK}\)(\(M_{9}\)) // Decipher \(M_{9}\) using session key \(SK\)
14.Updates (\(M_{10}\), \(ID_{Pi}\)) // EPRS updates \(ID_{Pi}\)'s electronic patient record
```  
**Algorithm 4**Procedure of doctor EPRS