-
Notifications
You must be signed in to change notification settings - Fork 6
/
cipher.go
71 lines (61 loc) · 2.21 KB
/
cipher.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
61
62
63
64
65
66
67
68
69
70
71
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package dtls
import "fmt"
type CipherSuite uint16
const (
CipherSuite_TLS_PSK_WITH_AES_128_CCM_8 CipherSuite = 0xC0A8
CipherSuite_TLS_PSK_WITH_AES_128_CBC_SHA256 CipherSuite = 0x00AE
CipherSuite_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 CipherSuite = 0xC0AE
CipherSuite_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 CipherSuite = 0xC023
)
func (cs CipherSuite) NeedPsk() bool {
switch cs {
case CipherSuite_TLS_PSK_WITH_AES_128_CCM_8, CipherSuite_TLS_PSK_WITH_AES_128_CBC_SHA256:
return true
}
return false
}
func (cs CipherSuite) NeedCert() bool {
switch cs {
case CipherSuite_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8, CipherSuite_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256:
return true
}
return false
}
func (cs CipherSuite) String() string {
return cipherSuiteToString(cs)
}
type Cipher interface {
GetPrfSize() int
GenerateKeyBlock(masterSecret []byte, rawKeyBlock []byte) *KeyBlock
Encrypt(s *session, rec *record, key []byte, iv []byte, mac []byte) ([]byte, error)
Decrypt(s *session, rec *record, key []byte, iv []byte, mac []byte) ([]byte, error)
}
func getCipher(peer *Peer, cipherSuite CipherSuite) Cipher {
switch cipherSuite {
case CipherSuite_TLS_PSK_WITH_AES_128_CCM_8:
return CipherCcm{peer: peer}
case CipherSuite_TLS_PSK_WITH_AES_128_CBC_SHA256:
return CipherCBC{peer: peer}
case CipherSuite_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8:
return CipherCcm{peer: peer}
case CipherSuite_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256:
return CipherCBC{peer: peer}
}
return nil
}
func cipherSuiteToString(c CipherSuite) string {
switch c {
case CipherSuite_TLS_PSK_WITH_AES_128_CCM_8:
return "TLS_PSK_WITH_AES_128_CCM_8(0xC0A8)"
case CipherSuite_TLS_PSK_WITH_AES_128_CBC_SHA256:
return "TLS_PSK_WITH_AES_128_CBC_SHA256(0x00AE)"
case CipherSuite_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8:
return "TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8(0xC0AE)"
case CipherSuite_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256:
return "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256(0xC023)"
}
return fmt.Sprintf("Unknown(0x%X)", uint16(c))
}