diff --git a/Projs/PD2/internal/utils/cryptoUtils/cryptoUtils.go b/Projs/PD2/internal/utils/cryptoUtils/cryptoUtils.go index 6e4d4bb..e41fb80 100644 --- a/Projs/PD2/internal/utils/cryptoUtils/cryptoUtils.go +++ b/Projs/PD2/internal/utils/cryptoUtils/cryptoUtils.go @@ -201,9 +201,8 @@ func (k *KeyStore) GetClientTLSConfig() *tls.Config { return tlsConfig } -func (k KeyStore) EncryptMessageContent(receiverCert *x509.Certificate, content []byte) ([]byte, error) { +func (k KeyStore) EncryptMessageContent(receiverCert *x509.Certificate, recieverId string, reciever, content []byte) ([]byte, error) { // Digital envolope - // Create a random symmetric key dataKey := make([]byte, 32) if _, err := rand.Read(dataKey); err != nil { @@ -221,7 +220,7 @@ func (k KeyStore) EncryptMessageContent(receiverCert *x509.Certificate, content } // sign the message and append the signature - hashedContent := sha256.Sum256(content) + hashedContent := sha256.Sum256(append(content, []byte(recieverId)...)) signature, err := rsa.SignPKCS1v15(nil, k.privKey, crypto.SHA256, hashedContent[:]) if err != nil { return nil, err @@ -237,7 +236,7 @@ func (k KeyStore) EncryptMessageContent(receiverCert *x509.Certificate, content return pair(encryptedDataKey, ciphertext), nil } -func (k KeyStore) DecryptMessageContent(senderCert *x509.Certificate, cipherContent []byte) ([]byte, error) { +func (k KeyStore) DecryptMessageContent(senderCert *x509.Certificate, recieverId string, cipherContent []byte) ([]byte, error) { encryptedDataKey, encryptedMsg := unPair(cipherContent) dataKey, err := rsa.DecryptOAEP(sha256.New(), rand.Reader, k.GetPrivKey(), encryptedDataKey, nil) if err != nil { @@ -256,7 +255,7 @@ func (k KeyStore) DecryptMessageContent(senderCert *x509.Certificate, cipherCont } // check signature with sender public key signature, content := unPair(contentAndSig) - hashedContent := sha256.Sum256(content) + hashedContent := sha256.Sum256(append(content, []byte(recieverId)...)) senderKey := senderCert.PublicKey.(*rsa.PublicKey) if err := rsa.VerifyPKCS1v15(senderKey, crypto.SHA256, hashedContent[:], signature); err != nil { return nil, err