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 AesCcm struct {
ccm atomic .Value
clientCertificateType clientcertificate .Type
id ID
psk bool
keyExchangeAlgorithm KeyExchangeAlgorithm
cryptoCCMTagLen ciphersuite .CCMTagLen
ecc bool
}
func (c *AesCcm ) CertificateType () clientcertificate .Type {
return c .clientCertificateType
}
func (c *AesCcm ) ID () ID {
return c .id
}
func (c *AesCcm ) String () string {
return c .id .String ()
}
func (c *AesCcm ) ECC () bool {
return c .ecc
}
func (c *AesCcm ) KeyExchangeAlgorithm () KeyExchangeAlgorithm {
return c .keyExchangeAlgorithm
}
func (c *AesCcm ) HashFunc () func () hash .Hash {
return sha256 .New
}
func (c *AesCcm ) AuthenticationType () AuthenticationType {
if c .psk {
return AuthenticationTypePreSharedKey
}
return AuthenticationTypeCertificate
}
func (c *AesCcm ) IsInitialized () bool {
return c .ccm .Load () != nil
}
func (c *AesCcm ) Init (masterSecret , clientRandom , serverRandom []byte , isClient bool , prfKeyLen int ) error {
const (
prfMacLen = 0
prfIvLen = 4
)
keys , err := prf .GenerateEncryptionKeys (
masterSecret , clientRandom , serverRandom , prfMacLen , prfKeyLen , prfIvLen , c .HashFunc (),
)
if err != nil {
return err
}
var ccm *ciphersuite .CCM
if isClient {
ccm , err = ciphersuite .NewCCM (
c .cryptoCCMTagLen , keys .ClientWriteKey , keys .ClientWriteIV , keys .ServerWriteKey , keys .ServerWriteIV ,
)
} else {
ccm , err = ciphersuite .NewCCM (
c .cryptoCCMTagLen , keys .ServerWriteKey , keys .ServerWriteIV , keys .ClientWriteKey , keys .ClientWriteIV ,
)
}
c .ccm .Store (ccm )
return err
}
func (c *AesCcm ) Encrypt (pkt *recordlayer .RecordLayer , raw []byte ) ([]byte , error ) {
cipherSuite , ok := c .ccm .Load ().(*ciphersuite .CCM )
if !ok {
return nil , fmt .Errorf ("%w, unable to encrypt" , errCipherSuiteNotInit )
}
return cipherSuite .Encrypt (pkt , raw )
}
func (c *AesCcm ) Decrypt (h recordlayer .Header , raw []byte ) ([]byte , error ) {
cipherSuite , ok := c .ccm .Load ().(*ciphersuite .CCM )
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 .