// Package rfc8009 provides encryption and checksum methods as specified in RFC 8009
package rfc8009 import ( ) // EncryptData encrypts the data provided using methods specific to the etype provided as defined in RFC 8009. func (, []byte, etype.EType) ([]byte, []byte, error) { := .GetKeyByteSize() if .GetETypeID() == etypeID.AES256_CTS_HMAC_SHA384_192 { = 32 } if len() != { return []byte{}, []byte{}, fmt.Errorf("incorrect keysize: expected: %v actual: %v", .GetKeyByteSize(), len()) } := make([]byte, aes.BlockSize) return aescts.Encrypt(, , ) } // EncryptMessage encrypts the message provided using the methods specific to the etype provided as defined in RFC 8009. // The encrypted data is concatenated with its integrity hash to create an encrypted message. func (, []byte, uint32, etype.EType) ([]byte, []byte, error) { := .GetKeyByteSize() if .GetETypeID() == etypeID.AES256_CTS_HMAC_SHA384_192 { = 32 } if len() != { return []byte{}, []byte{}, fmt.Errorf("incorrect keysize: expected: %v actual: %v", , len()) } if len() != .GetKeyByteSize() { } //confounder := make([]byte, .GetConfounderByteSize()) , := rand.Read() if != nil { return []byte{}, []byte{}, fmt.Errorf("could not generate random confounder: %v", ) } := append(, ...) // Derive key for encryption from usage var []byte if != 0 { , = .DeriveKey(, common.GetUsageKe()) if != nil { return []byte{}, []byte{}, fmt.Errorf("error deriving key for encryption: %v", ) } } // Encrypt the data , , := .EncryptData(, ) if != nil { return , , fmt.Errorf("error encrypting data: %v", ) } := make([]byte, .GetConfounderByteSize()) , := GetIntegityHash(, , , , ) if != nil { return , , fmt.Errorf("error encrypting data: %v", ) } = append(, ...) return , , nil } // DecryptData decrypts the data provided using the methods specific to the etype provided as defined in RFC 8009. func (, []byte, etype.EType) ([]byte, error) { := .GetKeyByteSize() if .GetETypeID() == etypeID.AES256_CTS_HMAC_SHA384_192 { = 32 } if len() != { return []byte{}, fmt.Errorf("incorrect keysize: expected: %v actual: %v", , len()) } := make([]byte, aes.BlockSize) return aescts.Decrypt(, , ) } // DecryptMessage decrypts the message provided using the methods specific to the etype provided as defined in RFC 8009. // The integrity of the message is also verified. func (, []byte, uint32, etype.EType) ([]byte, error) { //Derive the key , := .DeriveKey(, common.GetUsageKe()) if != nil { return nil, fmt.Errorf("error deriving key: %v", ) } // Strip off the checksum from the end , := .DecryptData(, [:len()-.GetHMACBitLength()/8]) if != nil { return nil, } //Verify checksum if !.VerifyIntegrity(, , , ) { return nil, errors.New("integrity verification failed") } //Remove the confounder bytes return [.GetConfounderByteSize():], nil } // GetIntegityHash returns a keyed integrity hash of the bytes provided as defined in RFC 8009 func (, , []byte, uint32, etype.EType) ([]byte, error) { // Generate and append integrity hash // Rather than calculating the hash over the confounder and plaintext // it is calculated over the iv concatenated with the AES cipher output. := append(, ...) return common.GetIntegrityHash(, , , ) } // VerifyIntegrity verifies the integrity of cipertext bytes ct. func (, []byte, uint32, etype.EType) bool { := make([]byte, .GetHMACBitLength()/8) copy(, [len()-.GetHMACBitLength()/8:]) := make([]byte, .GetConfounderByteSize()) := append(, [:len()-(.GetHMACBitLength()/8)]...) , := common.GetIntegrityHash(, , , ) return hmac.Equal(, ) }