package ciphersuite
import (
"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 TLSEcdheEcdsaWithAes128GcmSha256 struct {
gcm atomic .Value
}
func (c *TLSEcdheEcdsaWithAes128GcmSha256 ) CertificateType () clientcertificate .Type {
return clientcertificate .ECDSASign
}
func (c *TLSEcdheEcdsaWithAes128GcmSha256 ) KeyExchangeAlgorithm () KeyExchangeAlgorithm {
return KeyExchangeAlgorithmEcdhe
}
func (c *TLSEcdheEcdsaWithAes128GcmSha256 ) ECC () bool {
return true
}
func (c *TLSEcdheEcdsaWithAes128GcmSha256 ) ID () ID {
return TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
}
func (c *TLSEcdheEcdsaWithAes128GcmSha256 ) String () string {
return "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256"
}
func (c *TLSEcdheEcdsaWithAes128GcmSha256 ) HashFunc () func () hash .Hash {
return sha256 .New
}
func (c *TLSEcdheEcdsaWithAes128GcmSha256 ) AuthenticationType () AuthenticationType {
return AuthenticationTypeCertificate
}
func (c *TLSEcdheEcdsaWithAes128GcmSha256 ) IsInitialized () bool {
return c .gcm .Load () != nil
}
func (c *TLSEcdheEcdsaWithAes128GcmSha256 ) init (masterSecret , clientRandom , serverRandom []byte , isClient bool , prfMacLen , prfKeyLen , prfIvLen int , hashFunc func () hash .Hash ) error {
keys , err := prf .GenerateEncryptionKeys (masterSecret , clientRandom , serverRandom , prfMacLen , prfKeyLen , prfIvLen , hashFunc )
if err != nil {
return err
}
var gcm *ciphersuite .GCM
if isClient {
gcm , err = ciphersuite .NewGCM (keys .ClientWriteKey , keys .ClientWriteIV , keys .ServerWriteKey , keys .ServerWriteIV )
} else {
gcm , err = ciphersuite .NewGCM (keys .ServerWriteKey , keys .ServerWriteIV , keys .ClientWriteKey , keys .ClientWriteIV )
}
c .gcm .Store (gcm )
return err
}
func (c *TLSEcdheEcdsaWithAes128GcmSha256 ) Init (masterSecret , clientRandom , serverRandom []byte , isClient bool ) error {
const (
prfMacLen = 0
prfKeyLen = 16
prfIvLen = 4
)
return c .init (masterSecret , clientRandom , serverRandom , isClient , prfMacLen , prfKeyLen , prfIvLen , c .HashFunc ())
}
func (c *TLSEcdheEcdsaWithAes128GcmSha256 ) Encrypt (pkt *recordlayer .RecordLayer , raw []byte ) ([]byte , error ) {
cipherSuite , ok := c .gcm .Load ().(*ciphersuite .GCM )
if !ok {
return nil , fmt .Errorf ("%w, unable to encrypt" , errCipherSuiteNotInit )
}
return cipherSuite .Encrypt (pkt , raw )
}
func (c *TLSEcdheEcdsaWithAes128GcmSha256 ) Decrypt (raw []byte ) ([]byte , error ) {
cipherSuite , ok := c .gcm .Load ().(*ciphersuite .GCM )
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 .