// 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 ( ) const ( // 8 bytes of 0xff. // https://datatracker.ietf.org/doc/html/rfc9146#name-record-payload-protection seqNumPlaceholder = 0xffffffffffffffff ) var ( //nolint:goerr113 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")} ) 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 //nolint:gosec //G115 binary.BigEndian.PutUint16([len()-2:], uint16()) return [:] } // generateAEADAdditionalDataCID generates additional data for AEAD ciphers // according to https://datatracker.ietf.org/doc/html/rfc9146#name-aead-ciphers func generateAEADAdditionalDataCID( *recordlayer.Header, int) []byte { var cryptobyte.Builder .AddUint64(seqNumPlaceholder) .AddUint8(uint8(protocol.ContentTypeConnectionID)) .AddUint8(uint8(len(.ConnectionID))) //nolint:gosec //G115 .AddUint8(uint8(protocol.ContentTypeConnectionID)) .AddUint8(.Version.Major) .AddUint8(.Version.Minor) .AddUint16(.Epoch) util.AddUint48(&, .SequenceNumber) .AddBytes(.ConnectionID) .AddUint16(uint16()) //nolint:gosec //G115 return .BytesOrPanic() } // 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() //nolint:gosec //G115 // if len(payload) >= (paddingLen - 1) then the MSB of t is zero = byte(int32(^) >> 31) //nolint:gosec //G115 // 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() //nolint:gosec //G115 // if i <= paddingLen then the MSB of t is zero := byte(int32(^) >> 31) //nolint:gosec //G115 := [len()-1-] &^= & ^ & } // We AND together the bits of good and replicate the result across // all the bits. &= << 4 &= << 2 &= << 1 = uint8(int8() >> 7) //nolint:gosec //G115 = int() + 1 return , }