Skip to content

Commit a11ba08

Browse files
committed
New crypto data structs
Signed-off-by: Gustavo Sampaio <[email protected]>
1 parent df97acc commit a11ba08

26 files changed

+198
-280
lines changed

bucket_core.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,21 @@ import (
55
"io"
66
"time"
77

8+
crabfsCrypto "github.com/runletapp/crabfs/crypto"
89
"github.com/runletapp/crabfs/interfaces"
910
)
1011

1112
var _ interfaces.Bucket = &bucketCoreImpl{}
1213

1314
type bucketCoreImpl struct {
14-
privateKey interfaces.PrivKey
15+
privateKey crabfsCrypto.PrivKey
1516
bucket string
1617

1718
fs interfaces.Core
1819
}
1920

2021
// BucketCoreNew creates a new bucket core io
21-
func BucketCoreNew(fs interfaces.Core, privateKey interfaces.PrivKey, bucket string) interfaces.Bucket {
22+
func BucketCoreNew(fs interfaces.Core, privateKey crabfsCrypto.PrivKey, bucket string) interfaces.Bucket {
2223
return &bucketCoreImpl{
2324
privateKey: privateKey,
2425
bucket: bucket,

crabfs.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"io"
66
"time"
77

8+
crabfsCrypto "github.com/runletapp/crabfs/crypto"
89
"github.com/runletapp/crabfs/interfaces"
910
"github.com/runletapp/crabfs/options"
1011

@@ -143,7 +144,7 @@ func (fs *crabFS) Close() error {
143144
return fs.datastore.Close()
144145
}
145146

146-
func (fs *crabFS) Get(ctx context.Context, publicKey interfaces.PubKey, bucket string, filename string) (interfaces.Fetcher, error) {
147+
func (fs *crabFS) Get(ctx context.Context, publicKey crabfsCrypto.PubKey, bucket string, filename string) (interfaces.Fetcher, error) {
147148
locker := fs.gc.Locker()
148149
locker.Lock()
149150

@@ -169,7 +170,7 @@ func (fs *crabFS) Get(ctx context.Context, publicKey interfaces.PubKey, bucket s
169170
return fetcher, nil
170171
}
171172

172-
func (fs *crabFS) Put(ctx context.Context, privateKey interfaces.PrivKey, bucket string, filename string, file io.Reader, mtime time.Time) error {
173+
func (fs *crabFS) Put(ctx context.Context, privateKey crabfsCrypto.PrivKey, bucket string, filename string, file io.Reader, mtime time.Time) error {
173174
locker := fs.gc.Locker()
174175
locker.Lock()
175176
defer locker.Unlock()
@@ -206,7 +207,7 @@ func (fs *crabFS) Put(ctx context.Context, privateKey interfaces.PrivKey, bucket
206207
return fs.host.Publish(ctx, privateKey, bucket, filename, blockMap, mtime, totalSize)
207208
}
208209

209-
func (fs *crabFS) Remove(ctx context.Context, privateKey interfaces.PrivKey, bucket string, filename string) error {
210+
func (fs *crabFS) Remove(ctx context.Context, privateKey crabfsCrypto.PrivKey, bucket string, filename string) error {
210211
if err := fs.gc.Schedule(); err != nil {
211212
return err
212213
}
@@ -233,15 +234,15 @@ func (fs *crabFS) GarbageCollector() interfaces.GarbageCollector {
233234
return fs.gc
234235
}
235236

236-
func (fs *crabFS) WithBucket(privateKey interfaces.PrivKey, bucket string) (interfaces.Bucket, error) {
237+
func (fs *crabFS) WithBucket(privateKey crabfsCrypto.PrivKey, bucket string) (interfaces.Bucket, error) {
237238
if err := fs.PublishPublicKey(privateKey.GetPublic()); err != nil {
238239
return nil, err
239240
}
240241

241242
return BucketCoreNew(fs, privateKey, bucket), nil
242243
}
243244

244-
func (fs *crabFS) PublishPublicKey(publicKey interfaces.PubKey) error {
245+
func (fs *crabFS) PublishPublicKey(publicKey crabfsCrypto.PubKey) error {
245246
locker := fs.gc.Locker()
246247
locker.Lock()
247248
defer locker.Unlock()

crypto/private_key.go

+12-14
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package crypto
22

33
import (
4-
"bytes"
54
"crypto"
65
"crypto/rand"
76
"crypto/rsa"
87
"crypto/sha256"
98
"crypto/x509"
9+
"encoding/hex"
1010
"io"
11-
"io/ioutil"
1211
)
1312

1413
var _ PrivKey = &privKeyImpl{}
@@ -20,14 +19,9 @@ type privKeyImpl struct {
2019
hash []byte
2120
}
2221

23-
// UnmarshallPrivateKey parse a private key from bytes generated with PrivKey.Marshall
24-
func UnmarshallPrivateKey(r io.Reader) (PrivKey, error) {
25-
data, err := ioutil.ReadAll(r)
26-
if err != nil {
27-
return nil, err
28-
}
29-
30-
key, err := x509.ParsePKCS1PrivateKey(data)
22+
// UnmarshalPrivateKey parse a private key from bytes generated with PrivKey.Marshal
23+
func UnmarshalPrivateKey(b []byte) (PrivKey, error) {
24+
key, err := x509.ParsePKCS1PrivateKey(b)
3125
if err != nil {
3226
return nil, err
3327
}
@@ -41,11 +35,11 @@ func privateKeyFromRSA(internalPk *rsa.PrivateKey) (PrivKey, error) {
4135
}
4236

4337
hash := sha256.New()
44-
data, err := pk.Marshall()
38+
data, err := pk.Marshal()
4539
if err != nil {
4640
return nil, err
4741
}
48-
_, err = io.Copy(hash, data)
42+
_, err = hash.Write(data)
4943
if err != nil {
5044
return nil, err
5145
}
@@ -74,10 +68,10 @@ func (pvk *privKeyImpl) GetPublic() PubKey {
7468
return pvk.pub
7569
}
7670

77-
func (pvk *privKeyImpl) Marshall() (io.Reader, error) {
71+
func (pvk *privKeyImpl) Marshal() ([]byte, error) {
7872
data := x509.MarshalPKCS1PrivateKey(pvk.internalPk)
7973

80-
return bytes.NewReader(data), nil
74+
return data, nil
8175
}
8276

8377
func (pvk *privKeyImpl) Decrypt(cipherText []byte, label []byte) ([]byte, error) {
@@ -92,3 +86,7 @@ func (pvk *privKeyImpl) Sign(data []byte) ([]byte, error) {
9286
func (pvk *privKeyImpl) Hash() []byte {
9387
return pvk.hash
9488
}
89+
90+
func (pvk *privKeyImpl) HashString() string {
91+
return hex.EncodeToString(pvk.hash)
92+
}

crypto/private_key_interface.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
package crypto
22

3-
import (
4-
"io"
5-
)
6-
73
// PrivKey private key abstraction
84
type PrivKey interface {
95
GetPublic() PubKey
10-
Marshall() (io.Reader, error)
6+
Marshal() ([]byte, error)
117
Decrypt(cipherText []byte, label []byte) ([]byte, error)
128
Sign(data []byte) ([]byte, error)
139
Hash() []byte
10+
HashString() string
1411
}

crypto/private_key_test.go

+11-15
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package crypto
33
import (
44
"bytes"
55
"crypto/rand"
6-
"io/ioutil"
76
"testing"
87

98
"github.com/golang/mock/gomock"
@@ -24,39 +23,36 @@ func setDownPrivKeyTest(ctrl *gomock.Controller) {
2423
ctrl.Finish()
2524
}
2625

27-
func TestPrivKeyMarshall(t *testing.T) {
26+
func TestPrivKeyMarshal(t *testing.T) {
2827
key, ctrl := setUpPrivKeyTest(t)
2928
defer setDownPrivKeyTest(ctrl)
3029
assert := assert.New(t)
3130

32-
r, err := key.Marshall()
33-
assert.Nil(err)
34-
35-
data, err := ioutil.ReadAll(r)
31+
data, err := key.Marshal()
3632
assert.Nil(err)
3733
assert.True(len(data) > 0)
3834
}
3935

40-
func TestPrivKeyUnMarshall(t *testing.T) {
36+
func TestPrivKeyUnMarshal(t *testing.T) {
4137
key, ctrl := setUpPrivKeyTest(t)
4238
defer setDownPrivKeyTest(ctrl)
4339
assert := assert.New(t)
4440

45-
r, err := key.Marshall()
41+
b, err := key.Marshal()
4642
assert.Nil(err)
4743

48-
key2, err := UnmarshallPrivateKey(r)
44+
key2, err := UnmarshalPrivateKey(b)
4945
assert.Nil(err)
5046

5147
assert.True(bytes.Compare(key.Hash(), key2.Hash()) == 0)
5248
}
5349

54-
func TestPrivKeyUnMarshallInvalid(t *testing.T) {
50+
func TestPrivKeyUnMarshalInvalid(t *testing.T) {
5551
_, ctrl := setUpPrivKeyTest(t)
5652
defer setDownPrivKeyTest(ctrl)
5753
assert := assert.New(t)
5854

59-
_, err := UnmarshallPrivateKey(bytes.NewReader([]byte("abc")))
55+
_, err := UnmarshalPrivateKey([]byte("abc"))
6056
assert.NotNil(err)
6157
}
6258

@@ -90,28 +86,28 @@ func TestPrivKeyEncryptDecryptWrongLabel(t *testing.T) {
9086
assert.NotNil(err)
9187
}
9288

93-
func TestPrivKeySignValidate(t *testing.T) {
89+
func TestPrivKeySignVerify(t *testing.T) {
9490
key, ctrl := setUpPrivKeyTest(t)
9591
defer setDownPrivKeyTest(ctrl)
9692
assert := assert.New(t)
9793

9894
sign, err := key.Sign([]byte("abc"))
9995
assert.Nil(err)
10096

101-
check, err := key.GetPublic().Validate([]byte("abc"), sign)
97+
check, err := key.GetPublic().Verify([]byte("abc"), sign)
10298
assert.Nil(err)
10399
assert.True(check)
104100
}
105101

106-
func TestPrivKeySignValidateInvalid(t *testing.T) {
102+
func TestPrivKeySignVerifyInvalid(t *testing.T) {
107103
key, ctrl := setUpPrivKeyTest(t)
108104
defer setDownPrivKeyTest(ctrl)
109105
assert := assert.New(t)
110106

111107
sign, err := key.Sign([]byte("abc"))
112108
assert.Nil(err)
113109

114-
check, err := key.GetPublic().Validate([]byte("abc2"), sign)
110+
check, err := key.GetPublic().Verify([]byte("abc2"), sign)
115111
assert.NotNil(err)
116112
assert.False(check)
117113
}

crypto/public_key.go

+13-16
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
package crypto
22

33
import (
4-
"bytes"
54
"crypto"
65
"crypto/rand"
76
"crypto/rsa"
87
"crypto/sha256"
98
"crypto/x509"
10-
"io"
11-
"io/ioutil"
9+
"encoding/hex"
1210
)
1311

1412
var _ PubKey = &publicKeyImpl{}
@@ -19,14 +17,9 @@ type publicKeyImpl struct {
1917
hash []byte
2018
}
2119

22-
// UnmarshallPublicKey parse a public key from bytes generated with PubKey.Marshall
23-
func UnmarshallPublicKey(r io.Reader) (PubKey, error) {
24-
data, err := ioutil.ReadAll(r)
25-
if err != nil {
26-
return nil, err
27-
}
28-
29-
key, err := x509.ParsePKCS1PublicKey(data)
20+
// UnmarshalPublicKey parse a public key from bytes generated with PubKey.Marshal
21+
func UnmarshalPublicKey(b []byte) (PubKey, error) {
22+
key, err := x509.ParsePKCS1PublicKey(b)
3023
if err != nil {
3124
return nil, err
3225
}
@@ -40,11 +33,11 @@ func publicKeyNewFromRSA(pub *rsa.PublicKey) (PubKey, error) {
4033
}
4134

4235
hash := sha256.New()
43-
data, err := pk.Marshall()
36+
data, err := pk.Marshal()
4437
if err != nil {
4538
return nil, err
4639
}
47-
_, err = io.Copy(hash, data)
40+
_, err = hash.Write(data)
4841
if err != nil {
4942
return nil, err
5043
}
@@ -54,16 +47,16 @@ func publicKeyNewFromRSA(pub *rsa.PublicKey) (PubKey, error) {
5447
return pk, nil
5548
}
5649

57-
func (puk *publicKeyImpl) Marshall() (io.Reader, error) {
50+
func (puk *publicKeyImpl) Marshal() ([]byte, error) {
5851
data := x509.MarshalPKCS1PublicKey(puk.internalPk)
59-
return bytes.NewReader(data), nil
52+
return data, nil
6053
}
6154

6255
func (puk *publicKeyImpl) Encrypt(data []byte, label []byte) ([]byte, error) {
6356
return rsa.EncryptOAEP(sha256.New(), rand.Reader, puk.internalPk, data, label)
6457
}
6558

66-
func (puk *publicKeyImpl) Validate(data []byte, signature []byte) (bool, error) {
59+
func (puk *publicKeyImpl) Verify(data []byte, signature []byte) (bool, error) {
6760
hashed := sha256.Sum256(data)
6861
err := rsa.VerifyPSS(puk.internalPk, crypto.SHA256, hashed[:], signature, nil)
6962
return err == nil, err
@@ -72,3 +65,7 @@ func (puk *publicKeyImpl) Validate(data []byte, signature []byte) (bool, error)
7265
func (puk *publicKeyImpl) Hash() []byte {
7366
return puk.hash
7467
}
68+
69+
func (puk *publicKeyImpl) HashString() string {
70+
return hex.EncodeToString(puk.hash)
71+
}

crypto/public_key_interface.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package crypto
22

3-
import "io"
4-
53
// PubKey public key abstraction
64
type PubKey interface {
7-
Marshall() (io.Reader, error)
5+
Marshal() ([]byte, error)
86
Encrypt(data []byte, label []byte) ([]byte, error)
9-
Validate(data []byte, signature []byte) (bool, error)
7+
Verify(data []byte, signature []byte) (bool, error)
108
Hash() []byte
9+
HashString() string
1110
}

crypto/public_key_test.go

+7-11
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package crypto
33
import (
44
"bytes"
55
"crypto/rand"
6-
"io/ioutil"
76
"testing"
87

98
"github.com/golang/mock/gomock"
@@ -24,38 +23,35 @@ func setDownPubKeyTest(ctrl *gomock.Controller) {
2423
ctrl.Finish()
2524
}
2625

27-
func TestPubKeyMarshall(t *testing.T) {
26+
func TestPubKeyMarshal(t *testing.T) {
2827
key, ctrl := setUpPubKeyTest(t)
2928
defer setDownPubKeyTest(ctrl)
3029
assert := assert.New(t)
3130

32-
r, err := key.GetPublic().Marshall()
33-
assert.Nil(err)
34-
35-
data, err := ioutil.ReadAll(r)
31+
data, err := key.GetPublic().Marshal()
3632
assert.Nil(err)
3733
assert.True(len(data) > 0)
3834
}
3935

40-
func TestPubKeyUnMarshall(t *testing.T) {
36+
func TestPubKeyUnMarshal(t *testing.T) {
4137
key, ctrl := setUpPubKeyTest(t)
4238
defer setDownPubKeyTest(ctrl)
4339
assert := assert.New(t)
4440

45-
r, err := key.GetPublic().Marshall()
41+
r, err := key.GetPublic().Marshal()
4642
assert.Nil(err)
4743

48-
key2, err := UnmarshallPublicKey(r)
44+
key2, err := UnmarshalPublicKey(r)
4945
assert.Nil(err)
5046

5147
assert.True(bytes.Compare(key.GetPublic().Hash(), key2.Hash()) == 0)
5248
}
5349

54-
func TestPubKeyUnMarshallInvalid(t *testing.T) {
50+
func TestPubKeyUnMarshalInvalid(t *testing.T) {
5551
_, ctrl := setUpPubKeyTest(t)
5652
defer setDownPubKeyTest(ctrl)
5753
assert := assert.New(t)
5854

59-
_, err := UnmarshallPublicKey(bytes.NewReader([]byte("abc")))
55+
_, err := UnmarshalPublicKey([]byte("abc"))
6056
assert.NotNil(err)
6157
}

0 commit comments

Comments
 (0)