# attacks and defenses on multi party Diffie-Hellman protocol

I tried to create a 4 partied Diffie-Hellman implementation for 4 people using its cyclic group property.

However, I am not sure regarding those 2 issues, and would appreciate if you could elaborate on them:

1. Is it safe against a passive adversary, who just listens to messages, or is it also safe against an adversary who could change messages that are sent between people in the party?
2. How can i change the protocol to be more secure against an adversary that can change not more than one communication between people in the party(for instance the adversary can change a message between $$P_0$$ and $$P_1$$ or $$P_2$$ and $$P_3$$ etc)? This is my idea I am not sure how to implement: by allowing every person $$P_i$$ to send messages to people that their index is one more or one less, meaning $$P_i$$ can send messages to $$P_{i-1}$$ or $$P_{i+1}$$(for instance $$P_3$$ can send a message to $$P_2$$ or $$P_0$$)

This is my 4 people implementation of Diffie-Hellman:

for some generator g: since $${y_A}^{x_B} = (g^{x_A})^{x_B} = (g^{x_B})^{x_A} = {y_B}^{x_A}$$ (for two people), for four people we'll have: $$(((g^{x_A})^{x_B})^{x_C})^{x_D} = ... = (((g^{x_D})^{x_C})^{x_B})^{x_A} = (((g^{x_A})^{x_B})^{x_C})^{x_D}$$ ($$x_i$$ is private), then we can do the following scheme:

For $$P_i$$ where $$i \in {0,1,2,3}$$:

1. $$P_i$$ generates its own private key $$x_i$$
2. $$P_i$$ generates its public key $$y_i=g^{x_i}$$
3. $$P_i$$ sends $$y_i$$ to $$P_{i+1}$$
4. $$P_i$$ now needs to calculate $$z_{(i-1)(i)}=y(i-1)^{x_i}$$
5. $$P_i$$ sends calculated $$z_(i-1)(i)$$ to $$P_{i+1}$$
6. finally, $$P_i$$ calculates using above steps $$k_{(i+1)(i+2)(i+3)(i)} = z_{(i+1)(i+2)(i+3)}^{x_i}$$

so $$k_{(P_i)(P_{i+1})(P_{i+2})(P_{i+3})} = ... = k_{(P_{i+3})(P_{i+2})(P_{i+1})(P_i)}$$

However, not sure how to apply 2. (changing the protocol to be secure against an adversary that can change not more than one communication as mentioned above).

And regarding 1, would really appreciate mathmatical explanation to understand it better.

Thank you very much for helping