Skip to content

Commit

Permalink
added separate encryption manager for Ipfs keys & fixed linting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
dev1644 committed Jul 13, 2020
1 parent 1630647 commit ac65df7
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 69 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: go
go:
- "1.11"
- "1.14"
services:
- docker

Expand Down
108 changes: 52 additions & 56 deletions encrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ var (
GCM Protocol = "AES256-GCM"
// CFB allows for usage of AES256-CFB encryption/decryption
CFB Protocol = "AES256-CFB"
// RSA IPFS (RSA) keys for encryption/decryption
RSA Protocol = "RSA"
)

// EncryptManager handles file encryption and decryption
Expand All @@ -49,6 +47,12 @@ type EncryptManager struct {
protocol Protocol
}

// EncryptManagerIpfs handles data encryption and decryption for Ipfs keys
// Currently it supports only RSA keys
type EncryptManagerIpfs struct {
passphrase []byte
}

// RsaKeyPair is an rsa key pair
type RsaKeyPair struct {
privateKey rsa.PrivateKey
Expand All @@ -70,6 +74,14 @@ func NewEncryptManager(passphrase string, protocol Protocol) *EncryptManager {
}
}

// NewEncryptManagerIpfs creates a new EncryptManager for Ipfs keys
// Default is RSA
func NewEncryptManagerIpfs(passphrase string) *EncryptManagerIpfs {
return &EncryptManagerIpfs{
passphrase: []byte(passphrase),
}
}

// WithGCM is used setup, and return EncryptManager for use with AES256-GCM
// the params are expected to be unencrypted, and in hex encoded string format
func (e *EncryptManager) WithGCM(params *GCMDecryptParams) *EncryptManager {
Expand Down Expand Up @@ -104,13 +116,6 @@ func (e *EncryptManager) Encrypt(r io.Reader) ([]byte, error) {
}
out = encryptedData

case RSA:
encryptedData, err := e.encryptRSA(r)
if err != nil {
return nil, err
}
out = encryptedData

default:
return nil, fmt.Errorf("no protocol specified")
}
Expand Down Expand Up @@ -189,38 +194,6 @@ func (e *EncryptManager) encryptCFB(r io.Reader) ([]byte, error) {
return encrypted, nil
}

//encryptRSA encrypts given io.Reader using RSA-PCKS
// the resultant encrypted bytes is returned
func (e *EncryptManager) encryptRSA(r io.Reader) ([]byte, error) {
if r == nil {
return nil, errors.New("invalid content provided")
}

b, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}

rsaKeyPair, err := e.unmarshallRsaKey()

if err != nil {
return nil, err
}

if rsaKeyPair.pubkey.Size() < len(b) {
return nil, fmt.Errorf("Can't encrypt file larger than RSA pub key size")
}

ciphertext, err := rsa.EncryptPKCS1v15(rand.Reader, &rsaKeyPair.pubkey, b)

if err != nil {
fmt.Fprintf(os.Stderr, "Error from encryption: %s\n", err)
return nil, err
}

return ciphertext, nil
}

// RetrieveGCMDecryptionParameters is used to retrieve GCM cipher and nonce
// before returning, the cipher and nonce data are formatted, and encrypted
func (e *EncryptManager) RetrieveGCMDecryptionParameters() ([]byte, error) {
Expand All @@ -238,17 +211,12 @@ func (e *EncryptManager) Decrypt(r io.Reader) ([]byte, error) {
switch e.protocol {
case CFB:
return e.decryptCFB(r)
case GCM:
return e.decryptGCM(r)
case GCM:
if e.gcmDecryptParams == nil {
return nil, errors.New("no gcm decryption parameters given")
}
return e.decryptGCM(r)

case RSA:
return e.decryptRSA(r)

default:
return nil, fmt.Errorf("invalid invocation, must be one of\nAES256-GCM: EncryptManager::WithGCM::Decrypt\nAES256-CFB: EncryptManager::WithCFB:Decrypt")
}
Expand Down Expand Up @@ -319,25 +287,55 @@ func (e *EncryptManager) decryptCFB(r io.Reader) ([]byte, error) {
return decrypted, nil
}

//decryptRSA decrypts given io.Reader using RSA-PCKS
// the resultant decrypted bytes is returned
func (e *EncryptManager) decryptRSA(r io.Reader) ([]byte, error) {

// Encrypt encrypts given io.Reader using RSA-PCKS
// the resultant encrypted bytes is returned
func (e *EncryptManagerIpfs) Encrypt(r io.Reader) ([]byte, error) {
if r == nil {
return nil, errors.New("invalid content provided")
}

// read raw contents
b, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}

// unmarshalling RSA key pair
rsaKeyPair, err := e.unmarshallRsaKey()
if err != nil {
return nil, err
}

// encrypt contents
ciphertext, err := rsa.EncryptPKCS1v15(rand.Reader, &rsaKeyPair.pubkey, b)
if err != nil {
fmt.Fprintf(os.Stderr, "Error from encryption: %s\n", err)
return nil, err
}

return ciphertext, nil
}

// Decrypt decrypts given io.Reader using RSA-PCKS
// the resultant decrypted bytes is returned
func (e *EncryptManagerIpfs) Decrypt(r io.Reader) ([]byte, error) {
if r == nil {
return nil, errors.New("invalid content provided")
}

// read raw contents
b, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}

// unmarshalling RSA key pair
rsaKeyPair, err := e.unmarshallRsaKey()
if err != nil {
return nil, err
}

// decrypt contents
decrypted, err := rsa.DecryptPKCS1v15(rand.Reader, &rsaKeyPair.privateKey, b)
if err != nil {
fmt.Fprintf(os.Stderr, "Error from decryption: %s\n", err)
Expand All @@ -347,25 +345,23 @@ func (e *EncryptManager) decryptRSA(r io.Reader) ([]byte, error) {
return decrypted, nil
}

func (e *EncryptManager) unmarshallRsaKey() (*RsaKeyPair, error) {
func (e *EncryptManagerIpfs) unmarshallRsaKey() (*RsaKeyPair, error) {

// unmarshalling private key
decoded, err := base64.StdEncoding.DecodeString(string(e.passphrase))

sk, err := ic.UnmarshalPrivateKey(decoded)
if err != nil {
return nil, fmt.Errorf("Invalid paraphrase is provided - Error %s", err)
}

// parsing private key
rawPrivateKey, _ := sk.Raw()

privk, err := x509.ParsePKCS1PrivateKey(rawPrivateKey)

if err != nil {
return nil, err
}

pubk := privk.PublicKey

rsaKeyPair := &RsaKeyPair{privateKey: *privk, pubkey: pubk}

return rsaKeyPair, nil
}
15 changes: 4 additions & 11 deletions encrypt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,14 +188,8 @@ func Test_EncryptManager_AES256_CFB(t *testing.T) {
}
}

func Test_EncryptManager(t *testing.T) {

// open a sample file
original, err := ioutil.ReadFile("sample_data")
if err != nil {
t.Errorf("setup failed: %s", err)
return
}
func Test_EncryptManagerIpfs(t *testing.T) {
original := []byte("Test data to be encrypted")

type fields struct {
passphrase string
Expand All @@ -220,7 +214,7 @@ func Test_EncryptManager(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := NewEncryptManager(tt.fields.passphrase, RSA)
e := NewEncryptManagerIpfs(tt.fields.passphrase)

// encrypt
dataToDecrypt, err := e.Encrypt(tt.args.r)
Expand All @@ -229,8 +223,7 @@ func Test_EncryptManager(t *testing.T) {
return
}

// if expecting encryption error
// we need to fake some data to decrypt
// if expecting encryption error skip rest of the computation
if tt.wantErr {
return
}
Expand Down
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -29,39 +29,50 @@ github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5y
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/ipfs/go-cid v0.0.5 h1:o0Ix8e/ql7Zb5UVUJEUfjsWCIY8t48++9lR8qi6oiJU=
github.com/ipfs/go-cid v0.0.5/go.mod h1:plgt+Y5MnOey4vO4UlUazGqdbEXuFYitED67FexhXog=
github.com/jbenet/go-cienv v0.1.0/go.mod h1:TqNnHUmJgXau0nCzC7kXWeotg3J9W34CUv5Djy1+FlA=
github.com/jbenet/goprocess v0.1.4 h1:DRGOFReOMqqDNXwW70QkacFW0YN9QnwLV0Vqk+3oU0o=
github.com/jbenet/goprocess v0.1.4/go.mod h1:5yspPrukOVuOLORacaBi858NqyClJPQxYZlqdZVfqY4=
github.com/jessevdk/go-flags v0.0.0-20141203071132-1679536dcc89/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/jrick/logrotate v1.0.0/go.mod h1:LNinyqDIJnpAur+b8yyulnQw/wDuN1+BYKlTRt3OuAQ=
github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/kkdai/bstream v0.0.0-20161212061736-f391b8402d23/go.mod h1:J+Gs4SYgM6CZQHDETBtE9HaSEkGmuNXF86RwHhHUvq4=
github.com/libp2p/go-buffer-pool v0.0.1/go.mod h1:xtyIz9PMobb13WaxR6Zo1Pd1zXJKYg0a8KiIvDp3TzQ=
github.com/libp2p/go-buffer-pool v0.0.2 h1:QNK2iAFa8gjAe1SPz6mHSMuCcjs+X1wlHzeOSqcmlfs=
github.com/libp2p/go-buffer-pool v0.0.2/go.mod h1:MvaB6xw5vOrDl8rYZGLFdKAuk/hRoRZd1Vi32+RXyFM=
github.com/libp2p/go-flow-metrics v0.0.3/go.mod h1:HeoSNUrOJVK1jEpDqVEiUOIXqhbnS27omG0uWU5slZs=
github.com/libp2p/go-libp2p-core v0.6.0 h1:u03qofNYTBN+yVg08PuAKylZogVf0xcTEeM8skGf+ak=
github.com/libp2p/go-libp2p-core v0.6.0/go.mod h1:txwbVEhHEXikXn9gfC7/UDDw7rkxuX0bJvM49Ykaswo=
github.com/libp2p/go-msgio v0.0.4/go.mod h1:63lBBgOTDKQL6EWazRMCwXsEeEeK9O2Cd+0+6OOuipQ=
github.com/libp2p/go-openssl v0.0.5 h1:pQkejVhF0xp08D4CQUcw8t+BFJeXowja6RVcb5p++EA=
github.com/libp2p/go-openssl v0.0.5/go.mod h1:unDrJpgy3oFr+rqXsarWifmJuNnJR4chtO1HmaZjggc=
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1 h1:lYpkrQH5ajf0OXOcUbGjvZxxijuBwbbmlSxLiuofa+g=
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1/go.mod h1:pD8RvIylQ358TN4wwqatJ8rNavkEINozVn9DtGI3dfQ=
github.com/minio/sha256-simd v0.1.1-0.20190913151208-6de447530771/go.mod h1:B5e1o+1/KgNmWrSQK08Y6Z1Vb5pwIktudl0J58iy0KM=
github.com/minio/sha256-simd v0.1.1 h1:5QHSlgo3nt5yKOJrC7W8w7X+NFl8cMPZm96iu8kKUJU=
github.com/minio/sha256-simd v0.1.1/go.mod h1:B5e1o+1/KgNmWrSQK08Y6Z1Vb5pwIktudl0J58iy0KM=
github.com/mr-tron/base58 v1.1.0/go.mod h1:xcD2VGqlgYjBdcBLw+TuYLr8afG+Hj8g2eTVqeSzSU8=
github.com/mr-tron/base58 v1.1.3 h1:v+sk57XuaCKGXpWtVBX8YJzO7hMGx4Aajh4TQbdEFdc=
github.com/mr-tron/base58 v1.1.3/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc=
github.com/multiformats/go-base32 v0.0.3 h1:tw5+NhuwaOjJCC5Pp82QuXbrmLzWg7uxlMFp8Nq/kkI=
github.com/multiformats/go-base32 v0.0.3/go.mod h1:pLiuGC8y0QR3Ue4Zug5UzK9LjgbkL8NSQj0zQ5Nz/AA=
github.com/multiformats/go-multiaddr v0.2.2 h1:XZLDTszBIJe6m0zF6ITBrEcZR73OPUhCBBS9rYAuUzI=
github.com/multiformats/go-multiaddr v0.2.2/go.mod h1:NtfXiOtHvghW9KojvtySjH5y0u0xW5UouOmQQrn6a3Y=
github.com/multiformats/go-multibase v0.0.1 h1:PN9/v21eLywrFWdFNsFKaU04kLJzuYzmrJR+ubhT9qA=
github.com/multiformats/go-multibase v0.0.1/go.mod h1:bja2MqRZ3ggyXtZSEDKpl0uO/gviWFaSteVbWT51qgs=
github.com/multiformats/go-multihash v0.0.13 h1:06x+mk/zj1FoMsgNejLpy6QTvJqlSt/BhLEy87zidlc=
github.com/multiformats/go-multihash v0.0.13/go.mod h1:VdAWLKTwram9oKAatUcLxBNUjdtcVwxObEQBtRfuyjc=
github.com/multiformats/go-varint v0.0.5 h1:XVZwSo04Cs3j/jS0uAEPpT3JY6DzMcVLLoWOSnCxOjg=
github.com/multiformats/go-varint v0.0.5/go.mod h1:3Ls8CIEsrijN6+B7PbrXRPxHRPuXSrVKRY101jdMZYE=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/spacemonkeygo/spacelog v0.0.0-20180420211403-2296661a0572 h1:RC6RW7j+1+HkWaX/Yh71Ee5ZHaHYt7ZP4sQgUrm6cDU=
github.com/spacemonkeygo/spacelog v0.0.0-20180420211403-2296661a0572/go.mod h1:w0SWMsp6j9O/dk4/ZpIhL+3CkG8ofA2vuv7k+ltqUMc=
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
Expand Down
1 change: 0 additions & 1 deletion sample_data

This file was deleted.

0 comments on commit ac65df7

Please sign in to comment.