// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package tls

import (
	
	
	
	
	
	
	
)

// This file contains the functions necessary to compute the TLS 1.3 key
// schedule. See RFC 8446, Section 7.

// nextTrafficSecret generates the next traffic secret, given the current one,
// according to RFC 8446, Section 7.2.
func ( *cipherSuiteTLS13) ( []byte) []byte {
	return tls13.ExpandLabel(.hash.New, , "traffic upd", nil, .hash.Size())
}

// trafficKey generates traffic keys according to RFC 8446, Section 7.3.
func ( *cipherSuiteTLS13) ( []byte) (,  []byte) {
	 = tls13.ExpandLabel(.hash.New, , "key", nil, .keyLen)
	 = tls13.ExpandLabel(.hash.New, , "iv", nil, aeadNonceLength)
	return
}

// finishedHash generates the Finished verify_data or PskBinderEntry according
// to RFC 8446, Section 4.4.4. See sections 4.4 and 4.2.11.2 for the baseKey
// selection.
func ( *cipherSuiteTLS13) ( []byte,  hash.Hash) []byte {
	 := tls13.ExpandLabel(.hash.New, , "finished", nil, .hash.Size())
	 := hmac.New(.hash.New, )
	.Write(.Sum(nil))
	return .Sum(nil)
}

// exportKeyingMaterial implements RFC5705 exporters for TLS 1.3 according to
// RFC 8446, Section 7.5.
func ( *cipherSuiteTLS13) ( *tls13.MasterSecret,  hash.Hash) func(string, []byte, int) ([]byte, error) {
	 := .ExporterMasterSecret()
	return func( string,  []byte,  int) ([]byte, error) {
		return .Exporter(, , ), nil
	}
}

type keySharePrivateKeys struct {
	curveID CurveID
	ecdhe   *ecdh.PrivateKey
	mlkem   *mlkem.DecapsulationKey768
}

const x25519PublicKeySize = 32

// generateECDHEKey returns a PrivateKey that implements Diffie-Hellman
// according to RFC 8446, Section 4.2.8.2.
func generateECDHEKey( io.Reader,  CurveID) (*ecdh.PrivateKey, error) {
	,  := curveForCurveID()
	if ! {
		return nil, errors.New("tls: internal error: unsupported curve")
	}

	return .GenerateKey()
}

func curveForCurveID( CurveID) (ecdh.Curve, bool) {
	switch  {
	case X25519:
		return ecdh.X25519(), true
	case CurveP256:
		return ecdh.P256(), true
	case CurveP384:
		return ecdh.P384(), true
	case CurveP521:
		return ecdh.P521(), true
	default:
		return nil, false
	}
}

func curveIDForCurve( ecdh.Curve) (CurveID, bool) {
	switch  {
	case ecdh.X25519():
		return X25519, true
	case ecdh.P256():
		return CurveP256, true
	case ecdh.P384():
		return CurveP384, true
	case ecdh.P521():
		return CurveP521, true
	default:
		return 0, false
	}
}