-
-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
43 changed files
with
565 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package nacl | ||
|
||
import ( | ||
"github.com/mailchain/mailchain/crypto" | ||
"github.com/mailchain/mailchain/crypto/cipher" | ||
) | ||
|
||
// NewDecrypter create a new decrypter attaching the private key to it | ||
func NewDecrypter(privateKey crypto.PrivateKey) Decrypter { | ||
return Decrypter{privateKey: privateKey} | ||
} | ||
|
||
// Decrypter will decrypt data using AES256CBC method | ||
type Decrypter struct { | ||
privateKey crypto.PrivateKey | ||
} | ||
|
||
// Decrypt data using recipient private key with AES in CBC mode. | ||
func (d Decrypter) Decrypt(data cipher.EncryptedContent) (cipher.PlainContent, error) { | ||
return easyOpen(data, d.privateKey.Bytes()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package nacl | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/mailchain/mailchain/crypto" | ||
"github.com/mailchain/mailchain/crypto/cipher" | ||
"github.com/mailchain/mailchain/crypto/ed25519/ed25519test" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestNewDecrypter(t *testing.T) { | ||
type args struct { | ||
privateKey crypto.PrivateKey | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want Decrypter | ||
}{ | ||
{ | ||
"success", | ||
args{ | ||
ed25519test.CharlottePrivateKey, | ||
}, | ||
Decrypter{ | ||
privateKey: ed25519test.CharlottePrivateKey, | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := NewDecrypter(tt.args.privateKey); !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("NewDecrypter() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestDecrypter_Decrypt(t *testing.T) { | ||
assert := assert.New(t) | ||
type fields struct { | ||
privateKey crypto.PrivateKey | ||
} | ||
type args struct { | ||
data cipher.EncryptedContent | ||
} | ||
tests := []struct { | ||
name string | ||
fields fields | ||
args args | ||
want cipher.PlainContent | ||
wantErr bool | ||
}{ | ||
{ | ||
"success-charlotte", | ||
fields{ | ||
ed25519test.CharlottePrivateKey, | ||
}, | ||
args{ | ||
cipher.EncryptedContent{0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x5b, 0x19, 0x83, 0xe5, 0x6e, 0x7f, 0xed, 0xfe, 0xbb, 0xd0, 0x70, 0x34, 0xce, 0x25, 0x49, 0x76, 0xa3, 0x50, 0x78, 0x91, 0x18, 0xe6, 0xe3}, | ||
}, | ||
cipher.PlainContent{0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65}, | ||
false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
d := Decrypter{ | ||
privateKey: tt.fields.privateKey, | ||
} | ||
got, err := d.Decrypt(tt.args.data) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("Decrypter.Decrypt() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if !assert.Equal(tt.want, got) { | ||
t.Errorf("Decrypter.Decrypt() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package nacl | ||
|
||
import ( | ||
"crypto/rand" | ||
"io" | ||
|
||
"github.com/mailchain/mailchain/crypto" | ||
"github.com/mailchain/mailchain/crypto/cipher" | ||
"github.com/mailchain/mailchain/crypto/ed25519" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
// NewEncrypter create a new encrypter with crypto rand for reader | ||
func NewEncrypter() Encrypter { | ||
return Encrypter{rand: rand.Reader} | ||
} | ||
|
||
// Encrypter will encrypt data using AES256CBC method | ||
type Encrypter struct { | ||
rand io.Reader | ||
} | ||
|
||
func (e Encrypter) Encrypt(recipientPublicKey crypto.PublicKey, message cipher.PlainContent) (cipher.EncryptedContent, error) { | ||
if err := validatePublicKeyType(recipientPublicKey); err != nil { | ||
return nil, err | ||
} | ||
|
||
return easySeal(message, recipientPublicKey.Bytes(), e.rand) | ||
} | ||
|
||
func validatePublicKeyType(recipientPublicKey crypto.PublicKey) error { | ||
switch recipientPublicKey.(type) { | ||
case ed25519.PublicKey: | ||
return nil | ||
default: | ||
return errors.Errorf("invalid public key type for nacl encryption") | ||
} | ||
} |
Oops, something went wrong.