2018-10-07 17:09:45 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
2019-01-25 00:24:47 +00:00
|
|
|
"github.com/cbeuw/Cloak/internal/ecdh"
|
|
|
|
"github.com/cbeuw/Cloak/internal/util"
|
2018-10-07 17:09:45 +00:00
|
|
|
)
|
|
|
|
|
2019-08-12 21:43:16 +00:00
|
|
|
const (
|
|
|
|
UNORDERED_FLAG = 0x01 // 0000 0001
|
|
|
|
)
|
|
|
|
|
2020-01-24 16:44:29 +00:00
|
|
|
type authenticationPayload struct {
|
|
|
|
randPubKey [32]byte
|
|
|
|
ciphertextWithTag [64]byte
|
2019-08-16 23:16:31 +00:00
|
|
|
}
|
|
|
|
|
2020-01-24 16:44:29 +00:00
|
|
|
// makeAuthenticationPayload generates the ephemeral key pair, calculates the shared secret, and then compose and
|
2020-01-25 10:19:45 +00:00
|
|
|
// encrypt the authenticationPayload
|
2020-04-10 13:09:48 +00:00
|
|
|
func makeAuthenticationPayload(authInfo AuthInfo) (ret authenticationPayload, sharedSecret [32]byte) {
|
2019-08-20 21:43:04 +00:00
|
|
|
/*
|
|
|
|
Authentication data:
|
|
|
|
+----------+----------------+---------------------+-------------+--------------+--------+------------+
|
|
|
|
| _UID_ | _Proxy Method_ | _Encryption Method_ | _Timestamp_ | _Session Id_ | _Flag_ | _reserved_ |
|
|
|
|
+----------+----------------+---------------------+-------------+--------------+--------+------------+
|
|
|
|
| 16 bytes | 12 bytes | 1 byte | 8 bytes | 4 bytes | 1 byte | 6 bytes |
|
|
|
|
+----------+----------------+---------------------+-------------+--------------+--------+------------+
|
|
|
|
*/
|
2020-04-09 21:11:12 +00:00
|
|
|
ephPv, ephPub, _ := ecdh.GenerateKey(authInfo.WorldState.Rand)
|
2020-01-24 16:44:29 +00:00
|
|
|
copy(ret.randPubKey[:], ecdh.Marshal(ephPub))
|
2019-08-02 00:01:19 +00:00
|
|
|
|
|
|
|
plaintext := make([]byte, 48)
|
2020-04-06 12:07:16 +00:00
|
|
|
copy(plaintext, authInfo.UID)
|
|
|
|
copy(plaintext[16:28], authInfo.ProxyMethod)
|
|
|
|
plaintext[28] = authInfo.EncryptionMethod
|
2020-04-09 21:11:12 +00:00
|
|
|
binary.BigEndian.PutUint64(plaintext[29:37], uint64(authInfo.WorldState.Now().Unix()))
|
2020-04-06 12:07:16 +00:00
|
|
|
binary.BigEndian.PutUint32(plaintext[37:41], authInfo.SessionId)
|
2019-08-02 00:01:19 +00:00
|
|
|
|
2020-04-06 12:07:16 +00:00
|
|
|
if authInfo.Unordered {
|
2019-08-12 21:43:16 +00:00
|
|
|
plaintext[41] |= UNORDERED_FLAG
|
|
|
|
}
|
|
|
|
|
2020-04-07 20:15:28 +00:00
|
|
|
copy(sharedSecret[:], ecdh.GenerateSharedSecret(ephPv, authInfo.ServerPubKey))
|
|
|
|
ciphertextWithTag, _ := util.AESGCMEncrypt(ret.randPubKey[:12], sharedSecret[:], plaintext)
|
2020-01-24 16:44:29 +00:00
|
|
|
copy(ret.ciphertextWithTag[:], ciphertextWithTag[:])
|
2019-08-02 00:01:19 +00:00
|
|
|
return
|
2018-10-07 17:09:45 +00:00
|
|
|
}
|