You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Cloak/internal/client/auth.go

39 lines
1.2 KiB
Go

6 years ago
package client
import (
"crypto/rand"
"encoding/binary"
"github.com/cbeuw/Cloak/internal/ecdh"
"github.com/cbeuw/Cloak/internal/util"
5 years ago
"sync/atomic"
6 years ago
)
const (
UNORDERED_FLAG = 0x01 // 0000 0001
)
func makeHiddenData(sta *State) (random, TLSsessionID, keyShare, sharedSecret []byte) {
5 years ago
// random is marshalled ephemeral pub key 32 bytes
// TLSsessionID || keyShare is [encrypted UID 16 bytes, proxy method 12 bytes, encryption method 1 byte, timestamp 8 bytes, sessionID 4 bytes] [1 byte flag] [6 bytes reserved] [16 bytes authentication tag]
5 years ago
ephPv, ephPub, _ := ecdh.GenerateKey(rand.Reader)
random = ecdh.Marshal(ephPub)
plaintext := make([]byte, 48)
copy(plaintext, sta.UID)
copy(plaintext[16:28], sta.ProxyMethod)
plaintext[28] = sta.EncryptionMethod
binary.BigEndian.PutUint64(plaintext[29:37], uint64(sta.Now().Unix()))
binary.BigEndian.PutUint32(plaintext[37:41], atomic.LoadUint32(&sta.SessionID))
if sta.Unordered {
plaintext[41] |= UNORDERED_FLAG
}
5 years ago
sharedSecret = ecdh.GenerateSharedSecret(ephPv, sta.staticPub)
nonce := random[0:12]
ciphertext, _ := util.AESGCMEncrypt(nonce, sharedSecret, plaintext)
TLSsessionID = ciphertext[0:32]
keyShare = ciphertext[32:64]
return
6 years ago
}