I've been trying to implement Diffie Hellman key exchange so far from my own understandings (I know I could just stick with SSL/TLS, but I want to understand it in depth more), so I created this basic server<->client key exchange:
Generator and modulus are constants, thus same all the time Generator is 3 Modulus is a 2048bit prime number Step 1: 1. Client connects to a server, asks for key exchange 2. Server generates random 2048bit exponent 3. Server rises generator to this exponent 4. Server does modulo on result with 2048bit modulus 5. Server sends result of modulo operation unencrypted to client Step 2: 1. Client receives result from server unencrypted in form of hex number 2. Client generates random 2048bit exponent 3. Client rises generator to this exponent 5. Client sends this number to server unencrypted back 6. Client rises server's number to client's exponent and does modulo op. 7. Client remembers result as secret key (2048bit) 8. Server receives this number, and rises it to it's own exponent and does modulo op. 9. Server remembers result as secret key (2048bit) Step3: 1. Both client and server do SHA256 on secret key and get 256bit integer 2. This 256bit key is used as private key for further AES/CBC/PKCS5 communication
So my questions are:
- Can exponent be random number just like it is now or it should be a prime too?
- Is using dynamic modulus and generator better idea?
- Is there anything to improve this key exchange or are there any flaws?
- Is using SHA256 a proper way to generate 256bit key for AES/CBC?
Edit: purpose of this key exchange is for JSON messaging between server and client and end-to-end encryption in VoIP between clients (it's all for self-educational purpose, I know I could stick with WebRTC/DTLS or any already existing protocol!)