// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT

// Package ciphersuite provides the crypto operations needed for a DTLS CipherSuite
package ciphersuite import ( ) var ( errNotEnoughRoomForNonce = &protocol.InternalError{Err: errors.New("buffer not long enough to contain nonce")} //nolint:goerr113 errDecryptPacket = &protocol.TemporaryError{Err: errors.New("failed to decrypt packet")} //nolint:goerr113 errInvalidMAC = &protocol.TemporaryError{Err: errors.New("invalid mac")} //nolint:goerr113 errFailedToCast = &protocol.FatalError{Err: errors.New("failed to cast")} //nolint:goerr113 ) func generateAEADAdditionalData( *recordlayer.Header, int) []byte { var [13]byte // SequenceNumber MUST be set first // we only want uint48, clobbering an extra 2 (using uint64, Golang doesn't have uint48) binary.BigEndian.PutUint64([:], .SequenceNumber) binary.BigEndian.PutUint16([:], .Epoch) [8] = byte(.ContentType) [9] = .Version.Major [10] = .Version.Minor binary.BigEndian.PutUint16([len()-2:], uint16()) return [:] } // examinePadding returns, in constant time, the length of the padding to remove // from the end of payload. It also returns a byte which is equal to 255 if the // padding was valid and 0 otherwise. See RFC 2246, Section 6.2.3.2. // // https://github.com/golang/go/blob/039c2081d1178f90a8fa2f4e6958693129f8de33/src/crypto/tls/conn.go#L245 func examinePadding( []byte) ( int, byte) { if len() < 1 { return 0, 0 } := [len()-1] := uint(len()-1) - uint() // if len(payload) >= (paddingLen - 1) then the MSB of t is zero = byte(int32(^) >> 31) // The maximum possible padding length plus the actual length field := 256 // The length of the padded data is public, so we can use an if here if > len() { = len() } for := 0; < ; ++ { := uint() - uint() // if i <= paddingLen then the MSB of t is zero := byte(int32(^) >> 31) := [len()-1-] &^= & ^ & } // We AND together the bits of good and replicate the result across // all the bits. &= << 4 &= << 2 &= << 1 = uint8(int8() >> 7) = int() + 1 return , }