// Package rfc3962 provides encryption and checksum methods as specified in RFC 3962
package rfc3962 import ( ) // EncryptData encrypts the data provided using methods specific to the etype provided as defined in RFC 3962. func (, []byte, etype.EType) ([]byte, []byte, error) { if len() != .GetKeyByteSize() { return []byte{}, []byte{}, fmt.Errorf("incorrect keysize: expected: %v actual: %v", .GetKeyByteSize(), len()) } := make([]byte, .GetCypherBlockBitLength()/8) return aescts.Encrypt(, , ) } // EncryptMessage encrypts the message provided using the methods specific to the etype provided as defined in RFC 3962. // The encrypted data is concatenated with its integrity hash to create an encrypted message. func (, []byte, uint32, etype.EType) ([]byte, []byte, error) { if len() != .GetKeyByteSize() { return []byte{}, []byte{}, fmt.Errorf("incorrect keysize: expected: %v actual: %v", .GetKeyByteSize(), len()) } //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", ) } // Generate and append integrity hash , := common.GetIntegrityHash(, , , ) 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 3962. func (, []byte, etype.EType) ([]byte, error) { if len() != .GetKeyByteSize() { return []byte{}, fmt.Errorf("incorrect keysize: expected: %v actual: %v", .GetKeyByteSize(), len()) } := make([]byte, .GetCypherBlockBitLength()/8) return aescts.Decrypt(, , ) } // DecryptMessage decrypts the message provided using the methods specific to the etype provided as defined in RFC 3962. // 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 }