package ciphersuite
import (
"crypto/sha1"
"crypto/sha256"
"fmt"
"hash"
"sync/atomic"
"github.com/pion/dtls/v2/pkg/crypto/ciphersuite"
"github.com/pion/dtls/v2/pkg/crypto/clientcertificate"
"github.com/pion/dtls/v2/pkg/crypto/prf"
"github.com/pion/dtls/v2/pkg/protocol/recordlayer"
)
type TLSEcdheEcdsaWithAes256CbcSha struct {
cbc atomic .Value
}
func (c *TLSEcdheEcdsaWithAes256CbcSha ) CertificateType () clientcertificate .Type {
return clientcertificate .ECDSASign
}
func (c *TLSEcdheEcdsaWithAes256CbcSha ) KeyExchangeAlgorithm () KeyExchangeAlgorithm {
return KeyExchangeAlgorithmEcdhe
}
func (c *TLSEcdheEcdsaWithAes256CbcSha ) ECC () bool {
return true
}
func (c *TLSEcdheEcdsaWithAes256CbcSha ) ID () ID {
return TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA
}
func (c *TLSEcdheEcdsaWithAes256CbcSha ) String () string {
return "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA"
}
func (c *TLSEcdheEcdsaWithAes256CbcSha ) HashFunc () func () hash .Hash {
return sha256 .New
}
func (c *TLSEcdheEcdsaWithAes256CbcSha ) AuthenticationType () AuthenticationType {
return AuthenticationTypeCertificate
}
func (c *TLSEcdheEcdsaWithAes256CbcSha ) IsInitialized () bool {
return c .cbc .Load () != nil
}
func (c *TLSEcdheEcdsaWithAes256CbcSha ) Init (masterSecret , clientRandom , serverRandom []byte , isClient bool ) error {
const (
prfMacLen = 20
prfKeyLen = 32
prfIvLen = 16
)
keys , err := prf .GenerateEncryptionKeys (masterSecret , clientRandom , serverRandom , prfMacLen , prfKeyLen , prfIvLen , c .HashFunc ())
if err != nil {
return err
}
var cbc *ciphersuite .CBC
if isClient {
cbc , err = ciphersuite .NewCBC (
keys .ClientWriteKey , keys .ClientWriteIV , keys .ClientMACKey ,
keys .ServerWriteKey , keys .ServerWriteIV , keys .ServerMACKey ,
sha1 .New ,
)
} else {
cbc , err = ciphersuite .NewCBC (
keys .ServerWriteKey , keys .ServerWriteIV , keys .ServerMACKey ,
keys .ClientWriteKey , keys .ClientWriteIV , keys .ClientMACKey ,
sha1 .New ,
)
}
c .cbc .Store (cbc )
return err
}
func (c *TLSEcdheEcdsaWithAes256CbcSha ) Encrypt (pkt *recordlayer .RecordLayer , raw []byte ) ([]byte , error ) {
cipherSuite , ok := c .cbc .Load ().(*ciphersuite .CBC )
if !ok {
return nil , fmt .Errorf ("%w, unable to encrypt" , errCipherSuiteNotInit )
}
return cipherSuite .Encrypt (pkt , raw )
}
func (c *TLSEcdheEcdsaWithAes256CbcSha ) Decrypt (raw []byte ) ([]byte , error ) {
cipherSuite , ok := c .cbc .Load ().(*ciphersuite .CBC )
if !ok {
return nil , fmt .Errorf ("%w, unable to decrypt" , errCipherSuiteNotInit )
}
return cipherSuite .Decrypt (raw )
}
The pages are generated with Golds v0.8.2 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .