forked from pion/dtls
/
cipher_suite_tls_ecdhe_ecdsa_with_aes_128_gcm_sha256.go
60 lines (47 loc) · 1.53 KB
/
cipher_suite_tls_ecdhe_ecdsa_with_aes_128_gcm_sha256.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package dtls
import (
"crypto/sha256"
"errors"
"hash"
)
type cipherSuiteTLSEcdheEcdsaWithAes128GcmSha256 struct {
gcm *cryptoGCM
}
func (c cipherSuiteTLSEcdheEcdsaWithAes128GcmSha256) certificateType() clientCertificateType {
return clientCertificateTypeECDSASign
}
func (c cipherSuiteTLSEcdheEcdsaWithAes128GcmSha256) ID() cipherSuiteID {
return 0xc02b
}
func (c cipherSuiteTLSEcdheEcdsaWithAes128GcmSha256) hashFunc() func() hash.Hash {
return sha256.New
}
func (c *cipherSuiteTLSEcdheEcdsaWithAes128GcmSha256) init(masterSecret, clientRandom, serverRandom []byte, isClient bool) error {
const (
prfMacLen = 0
prfKeyLen = 16
prfIvLen = 4
)
keys, err := prfEncryptionKeys(masterSecret, clientRandom, serverRandom, prfMacLen, prfKeyLen, prfIvLen, c.hashFunc())
if err != nil {
return err
}
if isClient {
c.gcm, err = newCryptoGCM(keys.clientWriteKey, keys.clientWriteIV, keys.serverWriteKey, keys.serverWriteIV)
} else {
c.gcm, err = newCryptoGCM(keys.serverWriteKey, keys.serverWriteIV, keys.clientWriteKey, keys.clientWriteIV)
}
return err
}
func (c *cipherSuiteTLSEcdheEcdsaWithAes128GcmSha256) encrypt(pkt *recordLayer, raw []byte) ([]byte, error) {
if c.gcm == nil {
return nil, errors.New("CipherSuite has not been initalized, unable to encrypt")
}
return c.gcm.encrypt(pkt, raw)
}
func (c *cipherSuiteTLSEcdheEcdsaWithAes128GcmSha256) decrypt(raw []byte) ([]byte, error) {
if c.gcm == nil {
return nil, errors.New("CipherSuite has not been initalized, unable to decrypt ")
}
return c.gcm.decrypt(raw)
}