I have the following setup:
A server and a client will be connected over tcp. The server and client both have access to the preshared secret key. When the client connects to the server, the client and server both need to know the key, so they need to use the same key to authenticate or it will fail.
I came up with the following process and would like to know if there is anything obviously wrong with it that I didn't see. The actual encryption is not relevant in this case. So this is what happens when the client connects:
- Server generates a 16 byte salt (salt1).
- Server calculates hashServer1 = HASH(key + HASH(key + salt1)).
- Server sends salt1 to client.
- Clients calculates using the received salt1 hashClient1 = HASH(key + HASH(key + salt1)).
- Client generates 16 byte salt (salt2).
- Client calculates hashClient2 = HASH(key + HASH(key + salt2)).
- Client sends (hashClient1 + salt2) to server.
- Server checks if received hashClient1 == hashServer1 and if not breaks.
- Server calculates using received salt2 hashServer2 = HASH(key + HASH(key + salt2)).
- Server sends hashServer2 to client.
- Client checks if received hashServer2 == hashClient2 and if not breaks.
If I am not mistaken, this should be pretty secure when using a strong hash algorithm. The key is never send as clear text and due to the salt the data is different every time.
EDIT: Changed the salt used for hashing from saltX to HASH(key + saltX).