Skip to content

Commit

Permalink
Merge branch 'issue/499' of github.com:developerfred/mailchain into i…
Browse files Browse the repository at this point in the history
…ssue/499
  • Loading branch information
developerfred committed Jan 5, 2020
2 parents 20febd0 + 9d49515 commit aa8a3fd
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 32 deletions.
16 changes: 7 additions & 9 deletions crypto/cipher/aes256cbc/decrypter.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"github.com/mailchain/mailchain/crypto"
mc "github.com/mailchain/mailchain/crypto/cipher"
"github.com/mailchain/mailchain/crypto/secp256k1"
"github.com/pkg/errors"
)

// NewDecrypter create a new decrypter attaching the private key to it
Expand All @@ -40,7 +39,7 @@ type Decrypter struct {
func (d Decrypter) Decrypt(data mc.EncryptedContent) (mc.PlainContent, error) {
encryptedData, err := bytesDecode(data)
if err != nil {
return nil, mc.ErrDecrypt()
return nil, mc.ErrDecrypt
}

return decryptEncryptedData(d.privateKey, encryptedData)
Expand All @@ -49,12 +48,12 @@ func (d Decrypter) Decrypt(data mc.EncryptedContent) (mc.PlainContent, error) {
func decryptEncryptedData(privKey crypto.PrivateKey, data *encryptedData) ([]byte, error) {
tmpEphemeralPublicKey, err := secp256k1.PublicKeyFromBytes(data.EphemeralPublicKey)
if err != nil {
return nil, errors.WithMessage(err, "could not convert ephemeralPublicKey")
return nil, mc.ErrDecrypt
}

ephemeralPublicKey, err := tmpEphemeralPublicKey.(*secp256k1.PublicKey).ECIES()
if err != nil {
return nil, errors.WithMessage(err, "could not convert to ecies")
return nil, mc.ErrDecrypt
}

recipientPrivKey, err := asPrivateECIES(privKey)
Expand All @@ -64,19 +63,18 @@ func decryptEncryptedData(privKey crypto.PrivateKey, data *encryptedData) ([]byt

sharedSecret, err := deriveSharedSecret(ephemeralPublicKey, recipientPrivKey)
if err != nil {
return nil, errors.WithMessage(err, "could not derive shared secret")
return nil, mc.ErrDecrypt
}

macKey, encryptionKey := generateMacKeyAndEncryptionKey(sharedSecret)
mac, err := generateMac(macKey, data.InitializationVector, *ephemeralPublicKey, data.Ciphertext)

if err != nil {
return nil, errors.WithMessage(err, "generateMac failed")
return nil, mc.ErrDecrypt
}

if subtle.ConstantTimeCompare(data.MessageAuthenticationCode, mac) != 1 {
return nil, errors.Errorf("invalid mac")
}
return nil, mc.ErrDecrypt

return decryptCBC(encryptionKey, data.InitializationVector, data.Ciphertext)
}
Expand All @@ -93,7 +91,7 @@ func decryptCBC(key, iv, ciphertext []byte) ([]byte, error) {

plaintext, err = padding.NewPkcs7Padding(block.BlockSize()).Unpad(plaintext)
if err != nil {
return nil, errors.WithMessage(err, "could not pad")
return nil, mc.ErrDecrypt
}

ret := make([]byte, len(plaintext))
Expand Down
17 changes: 8 additions & 9 deletions crypto/cipher/aes256cbc/encrypter.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"github.com/ethereum/go-ethereum/crypto/ecies"
"github.com/mailchain/mailchain/crypto"
mc "github.com/mailchain/mailchain/crypto/cipher"
"github.com/pkg/errors"
)

// NewEncrypter create a new encrypter with crypto rand for reader
Expand All @@ -44,22 +43,22 @@ type Encrypter struct {
func (e Encrypter) Encrypt(message mc.PlainContent) (mc.EncryptedContent, error) {
epk, err := asPublicECIES(e.publicKey)
if err != nil {
return nil, mc.ErrEncrypt()
return nil, mc.ErrEncrypt
}

ephemeral, err := ecies.GenerateKey(e.rand, ecies.DefaultCurve, nil)
if err != nil {
return nil, errors.WithMessage(err, "could not generate ephemeral key")
return nil, mc.ErrEncrypt
}

iv, err := e.generateIV()
if err != nil {
return nil, errors.WithMessage(err, "could not generate iv")
return nil, mc.ErrEncrypt
}

encryptedData, err := encrypt(ephemeral, epk, message, iv)
if err != nil {
return nil, errors.WithMessage(err, "could not encrypt data")
return nil, mc.ErrEncrypt
}

return bytesEncode(encryptedData)
Expand All @@ -74,12 +73,12 @@ func encrypt(ephemeralPrivateKey *ecies.PrivateKey, pub *ecies.PublicKey, input,
macKey, encryptionKey := generateMacKeyAndEncryptionKey(sharedSecret)
ciphertext, err := encryptCBC(input, iv, encryptionKey)
if err != nil {
return nil, errors.WithMessage(err, "encryptCBC failed")
return nil, mc.ErrEncrypt
}

mac, err := generateMac(macKey, iv, ephemeralPublicKey, ciphertext)
if err != nil {
return nil, errors.WithMessage(err, "generateMac failed")
return nil, mc.ErrEncrypt
}

return &encryptedData{
Expand All @@ -97,11 +96,11 @@ func encryptCBC(data, iv, key []byte) ([]byte, error) {
}
data, err = padding.NewPkcs7Padding(block.BlockSize()).Pad(data)
if err != nil {
return nil, errors.WithMessage(err, "could not pad")
return nil, mc.ErrEncrypt
}

if len(iv) != block.BlockSize() {
return nil, errors.Errorf("cipher.NewCBCEncrypter: IV length must equal block size")
return nil, mc.ErrEncrypt
}

ciphertext := make([]byte, len(data))
Expand Down
23 changes: 9 additions & 14 deletions crypto/cipher/errors.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
package cipher

import (
"github.com/pkg/errors"
)

// ErrEncrypt returns the error message if encryption failed
//
func ErrEncrypt() error {
return errors.New("cipher: encryption failed")
}
import "errors"

// ErrDecrypt returns the error message if decryption failed
//
func ErrDecrypt() error {
return errors.New("cipher: decryption failed")
}
var (
// ErrEncrypt returns the error message if encryption failed
//
ErrEncrypt = errors.New("cipher: encryption failed") //nolint:gochecknoglobals
// ErrDecrypt returns the error message if decryption failed
//
ErrDecrypt = errors.New("cipher: decryption failed") //nolint:gochecknoglobals
)

0 comments on commit aa8a3fd

Please sign in to comment.