// Package rfc4757 provides encryption and checksum methods as specified in RFC 4757
package rfc4757 import ( ) // EncryptData encrypts the data provided using methods specific to the etype provided as defined in RFC 4757. func (, []byte, etype.EType) ([]byte, error) { if len() != .GetKeyByteSize() { return []byte{}, fmt.Errorf("incorrect keysize: expected: %v actual: %v", .GetKeyByteSize(), len()) } , := rc4.NewCipher() if != nil { return []byte{}, fmt.Errorf("error creating RC4 cipher: %v", ) } := make([]byte, len()) copy(, ) .XORKeyStream(, ) .Reset() return , nil } // DecryptData decrypts the data provided using the methods specific to the etype provided as defined in RFC 4757. func (, []byte, etype.EType) ([]byte, error) { return EncryptData(, , ) } // EncryptMessage encrypts the message provided using the methods specific to the etype provided as defined in RFC 4757. // The encrypted data is concatenated with its RC4 header containing integrity checksum and confounder to create an encrypted message. func (, []byte, uint32, bool, etype.EType) ([]byte, error) { := make([]byte, .GetConfounderByteSize()) // size = 8 , := rand.Read() if != nil { return []byte{}, fmt.Errorf("error generating confounder: %v", ) } := := HMAC(, UsageToMSMsgType()) := append(, ...) := HMAC(, ) := HMAC(, ) , := EncryptData(, , ) if != nil { return []byte{}, fmt.Errorf("error encrypting data: %v", ) } := append(, ...) return , nil } // DecryptMessage decrypts the message provided using the methods specific to the etype provided as defined in RFC 4757. // The integrity of the message is also verified. func (, []byte, uint32, bool, etype.EType) ([]byte, error) { := [:.GetHMACBitLength()/8] := [.GetHMACBitLength()/8:] , , := deriveKeys(, , , ) , := DecryptData(, , ) if != nil { return []byte{}, fmt.Errorf("error decrypting data: %v", ) } if !VerifyIntegrity(, , , ) { return []byte{}, errors.New("integrity checksum incorrect") } return [.GetConfounderByteSize():], nil } // VerifyIntegrity checks the integrity checksum of the data matches that calculated from the decrypted data. func (, , []byte, etype.EType) bool { := HMAC(, ) return hmac.Equal(, [:.GetHMACBitLength()/8]) }