package ciphersuite
import (
"crypto/sha256"
"fmt"
"hash"
"sync/atomic"
"github.com/pion/dtls/v3/pkg/crypto/ciphersuite"
"github.com/pion/dtls/v3/pkg/crypto/clientcertificate"
"github.com/pion/dtls/v3/pkg/crypto/prf"
"github.com/pion/dtls/v3/pkg/protocol/recordlayer"
)
type TLSPskWithAes128CbcSha256 struct {
cbc atomic .Value
}
func (c *TLSPskWithAes128CbcSha256 ) CertificateType () clientcertificate .Type {
return clientcertificate .Type (0 )
}
func (c *TLSPskWithAes128CbcSha256 ) KeyExchangeAlgorithm () KeyExchangeAlgorithm {
return KeyExchangeAlgorithmPsk
}
func (c *TLSPskWithAes128CbcSha256 ) ECC () bool {
return false
}
func (c *TLSPskWithAes128CbcSha256 ) ID () ID {
return TLS_PSK_WITH_AES_128_CBC_SHA256
}
func (c *TLSPskWithAes128CbcSha256 ) String () string {
return "TLS_PSK_WITH_AES_128_CBC_SHA256"
}
func (c *TLSPskWithAes128CbcSha256 ) HashFunc () func () hash .Hash {
return sha256 .New
}
func (c *TLSPskWithAes128CbcSha256 ) AuthenticationType () AuthenticationType {
return AuthenticationTypePreSharedKey
}
func (c *TLSPskWithAes128CbcSha256 ) IsInitialized () bool {
return c .cbc .Load () != nil
}
func (c *TLSPskWithAes128CbcSha256 ) Init (masterSecret , clientRandom , serverRandom []byte , isClient bool ) error {
const (
prfMacLen = 32
prfKeyLen = 16
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 ,
c .HashFunc (),
)
} else {
cbc , err = ciphersuite .NewCBC (
keys .ServerWriteKey , keys .ServerWriteIV , keys .ServerMACKey ,
keys .ClientWriteKey , keys .ClientWriteIV , keys .ClientMACKey ,
c .HashFunc (),
)
}
c .cbc .Store (cbc )
return err
}
func (c *TLSPskWithAes128CbcSha256 ) 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 *TLSPskWithAes128CbcSha256 ) Decrypt (h recordlayer .Header , 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 (h , 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 .