Skip to content

Commit

Permalink
feat: implement sr25519 keys (#360)
Browse files Browse the repository at this point in the history
* [WIP] #327 - start struct folders

* add new key sr25519

* [WIP] - private.go sr25519

* [WIP - #327] public key add

* [WIP] #327 add test rules

* add files on go.mod and fixed ristreto to ristretto

* [WIP] - create sr25519 module

* [WIP] public.go done[x] need test file

* [WIP]#327 - Public and Keys test files

* [WIP]test keys file

* #327 add function PrivateKeyfromBytes

* add news rules on go.mod

* Review this go.mod and go.sum

* add strobe/debug on .gitignore

* fixed tests

* fixed package ristretto 255

* fixed ristretto on private key

* fixed ristretto module

*  #327 merlin fixed

* change merlin version

*  update go mod

* fixed go get -v ./..

* fixed versions on go.md

* fixed version to 0.1.3

	modified:   go.mod
	modified:   ../../go.mod
	go.sum

* fixed sr25519 package

	modified:   crypto/sr25519/go.mod

* 	modified:   ../../.golangci.yml
	modified:   ../../cmd/mailchain/commands/account.go
	modified:   ../multikey/private_test.go
	deleted:    go.mod

	modified:   .golangci.yml
	modified:   cmd/mailchain/commands/account.go
	modified:   crypto/multikey/private_test.go
	deleted:    crypto/sr25519/go.mod
	modified:   crypto/sr25519/public.go

* #327 add sr25519 on nacl files

* - #327 Create go mod for sr25519

- add news rules on .gitignore
- fixed problens on ristretto package

	modified:   .gitignore

	modified:   crypto/sr25519/keys_test.go
	modified:   crypto/sr25519/public.go
	modified:   crypto/sr25519/sr25519test/keys.go

	new file:   go.mod

	modified:   crypto/cipher/nacl/nacl_test.go
	new file:   go.mod

	new file:   go.mod
	new file:   go.sum

	new file:   go.mod

	modified:   .gitignore
	modified:   crypto/cipher/nacl/nacl_test.go
	modified:   crypto/sr25519/keys_test.go
	modified:   crypto/sr25519/private.go
	modified:   crypto/sr25519/public.go
	modified:   crypto/sr25519/sr25519test/keys.go
	new file:   go.mod
	new file:   go.sum

*  #327 - update sr25519

 Changes to be committed:
	modified:   private.go
	modified:   public.go
	modified:   sr25519test/keys.go
	modified:   keys_test.go
	modified:   ../../go.mod

*  #327 - update sr25519

 Changes to be committed:
	modified:   private.go
	modified:   public.go
	modified:   sr25519test/keys.go
	modified:   keys_test.go
	modified:   ../../go.mod

* sr25519: cipher ncl #327 [x]

* add new function for converte string in byte array

* sr25519: [x] KeyPair
- [x] Charlotte and Sofie Tests
- [x] PrivateKeyToBytes

* add keys of thes sr25519

* [WIP] #327 - start struct folders

* add new key sr25519

* [WIP] - private.go sr25519

* [WIP - #327] public key add

* [WIP] #327 add test rules

* add files on go.mod and fixed ristreto to ristretto

* [WIP] - create sr25519 module

* [WIP] public.go done[x] need test file

* [WIP]#327 - Public and Keys test files

* [WIP]test keys file

* #327 add function PrivateKeyfromBytes

* add news rules on go.mod

* Review this go.mod and go.sum

* add strobe/debug on .gitignore

* fixed tests

* fixed package ristretto 255

* fixed ristretto on private key

* fixed ristretto module

*  #327 merlin fixed

* change merlin version

*  update go mod

* fixed go get -v ./..

* fixed versions on go.md

* fixed version to 0.1.3

	modified:   go.mod
	modified:   ../../go.mod
	go.sum

* fixed sr25519 package

	modified:   crypto/sr25519/go.mod

* #327 add sr25519 on nacl files

* sr25519: cipher ncl #327 [x]

* add new function for converte string in byte array

* Review this go.mod and go.sum

* 	modified:   ../../.golangci.yml
	modified:   ../../cmd/mailchain/commands/account.go
	modified:   ../multikey/private_test.go
	deleted:    go.mod

	modified:   .golangci.yml
	modified:   cmd/mailchain/commands/account.go
	modified:   crypto/multikey/private_test.go
	deleted:    crypto/sr25519/go.mod
	modified:   crypto/sr25519/public.go

* - #327 Create go mod for sr25519

- add news rules on .gitignore
- fixed problens on ristretto package

	modified:   .gitignore

	modified:   crypto/sr25519/keys_test.go
	modified:   crypto/sr25519/public.go
	modified:   crypto/sr25519/sr25519test/keys.go

	new file:   go.mod

	modified:   crypto/cipher/nacl/nacl_test.go
	new file:   go.mod

	new file:   go.mod
	new file:   go.sum

	new file:   go.mod

	modified:   .gitignore
	modified:   crypto/cipher/nacl/nacl_test.go
	modified:   crypto/sr25519/keys_test.go
	modified:   crypto/sr25519/private.go
	modified:   crypto/sr25519/public.go
	modified:   crypto/sr25519/sr25519test/keys.go
	new file:   go.mod
	new file:   go.sum

*  #327 - update sr25519

 Changes to be committed:
	modified:   private.go
	modified:   public.go
	modified:   sr25519test/keys.go
	modified:   keys_test.go
	modified:   ../../go.mod

* sr25519: cipher ncl #327 [x]

* add keys of thes sr25519

* Delete go.sum

* [WIP] sr25519

* suggest changes

* update keys on sr25519

* - [x] TestPrivateKey
- [x] TestPublicKey

* sr25519

* sr25519

* add sr25519 on multikey

* gofmt on sr25519

*  sr25519test/keys.go

* change msg err

* add charlotte sr25519 on multikey

*  add encodingtest.MustDecodeHex

* add encodingtest.MustDecodeHex on ed25519

* add encodingtest.MustDecodeHex on ed25519

	new file:   crypto/sr25519/end_end_test.go

* [WIP] TestPrivateKey_PublicKey same key

*  TestPrivateKey_public fail

*  PublicKey()

*  Publickey is working

*  sr25519: PrivateKey to PulicKey() [x]

* remove keypair.go

* update: address_test add #sr25519

* remove encoding and decoding

*  *[x] Add test case for sr25519 in TestEncrypter_Encrypt crypto/cipher/nacl/encrypt_test.go

* [x] add end_end_test
[x] sign/verify tests
[x] encrypt decrypt tests with nacl

* [ ] Nacl Error

* feat: create key exchange for sr25519 package #505

* native sr25519

* create key exchange for sr25519

* fix linting

* fix linting

* fix linting

* more tests

Co-authored-by: Rob De Feo <robertodefeo@hotmail.com>
  • Loading branch information
developerfred and robdefeo committed Jan 24, 2020
1 parent 4d32c53 commit b4be010
Show file tree
Hide file tree
Showing 34 changed files with 1,821 additions and 22 deletions.
3 changes: 3 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ run:
skip-dirs:
- crypto/secp256k1/secp256k1test
- crypto/ed25519/ed25519test
- crypto/sr25518/sr25519test
- cmd/mailchain/internal/prompts/promptstest



# which files to skip: they will be analyzed, but issues from them
# won't be reported. Default value is empty list, but there is
Expand Down
2 changes: 1 addition & 1 deletion cmd/mailchain/commands/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func accountAddCmd(ks keystore.Store, passphrasePrompt, privateKeyPrompt prompts
}

cmd.Flags().StringP("protocol", "P", "", "Select the protocol [ethereum]")
cmd.Flags().StringP("key-type", "", "", "Select the key type [secp256k1, ed25519]")
cmd.Flags().StringP("key-type", "", "", "Select the key type [secp256k1, ed25519, sr25519]")
cmd.Flags().StringP("private-key", "K", "", "Private key to store")
cmd.Flags().String("passphrase", "", "Passphrase to encrypt/decrypt key with")

Expand Down
17 changes: 17 additions & 0 deletions crypto/cipher/aes256cbc/conversions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/mailchain/mailchain/crypto/ed25519/ed25519test"
"github.com/mailchain/mailchain/crypto/secp256k1"
"github.com/mailchain/mailchain/crypto/secp256k1/secp256k1test"
"github.com/mailchain/mailchain/crypto/sr25519/sr25519test"
)

func Test_asPrivateECIES(t *testing.T) {
Expand Down Expand Up @@ -62,6 +63,14 @@ func Test_asPrivateECIES(t *testing.T) {
true,
true,
},
{
"err-unsupported-sr25519",
args{
sr25519test.SofiaPrivateKey,
},
true,
true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -117,6 +126,14 @@ func Test_asPublicECIES(t *testing.T) {
true,
true,
},
{
"err-invalid-sr25519",
args{
sr25519test.CharlottePublicKey,
},
true,
true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
44 changes: 41 additions & 3 deletions crypto/cipher/ecdh/ecdh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import (
"github.com/mailchain/mailchain/crypto/ed25519/ed25519test"
"github.com/mailchain/mailchain/crypto/secp256k1"
"github.com/mailchain/mailchain/crypto/secp256k1/secp256k1test"
"github.com/mailchain/mailchain/crypto/sr25519"
"github.com/mailchain/mailchain/crypto/sr25519/sr25519test"
"github.com/stretchr/testify/assert"
)

func Test_SharedSecretEndToEnd(t *testing.T) {
assert := assert.New(t)
type args struct {
keyExchange cipher.KeyExchange
RecipientPrivateKey crypto.PrivateKey
Expand All @@ -33,7 +34,7 @@ func Test_SharedSecretEndToEnd(t *testing.T) {
func() crypto.PrivateKey {
pk, err := secp256k1.GenerateKey(rand.Reader)
if err != nil {
assert.FailNow("secp256k1.GenerateKey error = %v", err)
assert.FailNow(t, "secp256k1.GenerateKey error = %v", err)
}
return pk
}(),
Expand Down Expand Up @@ -69,7 +70,7 @@ func Test_SharedSecretEndToEnd(t *testing.T) {
func() crypto.PrivateKey {
pk, err := ed25519.GenerateKey(rand.Reader)
if err != nil {
assert.FailNow("ed25519.GenerateKey error = %v", err)
assert.FailNow(t, "ed25519.GenerateKey error = %v", err)
}
return pk
}(),
Expand All @@ -95,9 +96,46 @@ func Test_SharedSecretEndToEnd(t *testing.T) {
ed25519test.CharlottePrivateKey,
},
},
{
"sr25519-random",
args{
func() cipher.KeyExchange {
kx, _ := NewSR25519(rand.Reader)
return kx
}(),
func() crypto.PrivateKey {
pk, err := sr25519.GenerateKey(rand.Reader)
if err != nil {
assert.FailNow(t, "sr25519.GenerateKey error = %v", err)
}
return pk
}(),
},
},
{
"sr25519-sofia",
args{
func() cipher.KeyExchange {
kx, _ := NewSR25519(rand.Reader)
return kx
}(),
sr25519test.SofiaPrivateKey,
},
},
{
"sr25519-charlotte",
args{
func() cipher.KeyExchange {
kx, _ := NewSR25519(rand.Reader)
return kx
}(),
sr25519test.CharlottePrivateKey,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert := assert.New(t)
ephemeralPrivKey, err := tt.args.keyExchange.EphemeralKey()
if err != nil {
assert.Fail("EphemeralKey() error = %v", err)
Expand Down
69 changes: 69 additions & 0 deletions crypto/cipher/ecdh/sr25519.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package ecdh

import (
"bytes"
"errors"
"io"

"github.com/mailchain/mailchain/crypto"
"github.com/mailchain/mailchain/crypto/sr25519"
)

type SR25519 struct {
rand io.Reader
}

func NewSR25519(rand io.Reader) (*SR25519, error) {
if rand == nil {
return nil, errors.New("rand must not be nil")
}

return &SR25519{rand: rand}, nil
}

func (kx SR25519) EphemeralKey() (crypto.PrivateKey, error) {
return sr25519.GenerateKey(kx.rand)
}

func (kx SR25519) SharedSecret(ephemeralKey crypto.PrivateKey, recipientKey crypto.PublicKey) ([]byte, error) {
ephemeralPrivateKey, err := kx.privateKey(ephemeralKey)
if err != nil {
return nil, ErrSharedSecretGenerate
}

recipientPublicKey, err := kx.publicKey(recipientKey)
if err != nil {
return nil, ErrSharedSecretGenerate
}

ephemeralPublicKey, _ := kx.publicKey(ephemeralKey.PublicKey())

if bytes.Equal(ephemeralPublicKey.Bytes(), recipientPublicKey.Bytes()) {
return nil, ErrSharedSecretGenerate
}

sharedSecret, err := sr25519.ExchangeKeys(ephemeralPrivateKey, recipientPublicKey)
if err != nil {
return nil, ErrSharedSecretGenerate
}

return sharedSecret[:], nil
}

func (kx SR25519) publicKey(pubKey crypto.PublicKey) (*sr25519.PublicKey, error) {
switch pk := pubKey.(type) {
case *sr25519.PublicKey:
return pk, nil
default:
return nil, ErrSharedSecretGenerate
}
}

func (kx SR25519) privateKey(privKey crypto.PrivateKey) (*sr25519.PrivateKey, error) {
switch pk := privKey.(type) {
case *sr25519.PrivateKey:
return pk, nil
default:
return nil, ErrSharedSecretGenerate
}
}
Loading

0 comments on commit b4be010

Please sign in to comment.