Cloak/internal/client/auth.go

50 lines
1.8 KiB
Go
Raw Normal View History

2018-10-07 17:09:45 +00:00
package client
import (
"encoding/binary"
"github.com/cbeuw/Cloak/internal/ecdh"
"github.com/cbeuw/Cloak/internal/util"
"io"
2020-04-06 12:07:16 +00:00
"time"
2018-10-07 17:09:45 +00:00
)
2019-08-12 21:43:16 +00:00
const (
UNORDERED_FLAG = 0x01 // 0000 0001
)
type authenticationPayload struct {
randPubKey [32]byte
ciphertextWithTag [64]byte
}
// makeAuthenticationPayload generates the ephemeral key pair, calculates the shared secret, and then compose and
// encrypt the authenticationPayload
func makeAuthenticationPayload(authInfo authInfo, randReader io.Reader, time time.Time) (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 |
+----------+----------------+---------------------+-------------+--------------+--------+------------+
*/
ephPv, ephPub, _ := ecdh.GenerateKey(randReader)
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
binary.BigEndian.PutUint64(plaintext[29:37], uint64(time.Unix()))
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)
copy(ret.ciphertextWithTag[:], ciphertextWithTag[:])
2019-08-02 00:01:19 +00:00
return
2018-10-07 17:09:45 +00:00
}