-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
provider record encryption #3
base: noot/prefix-lookup
Are you sure you want to change the base?
Changes from all commits
242a3b5
677a86b
0c57991
3596989
284f2b6
c35602b
06b08b7
853acdf
fa069ea
639d763
c1a6dc8
8264e56
704129f
3d9734f
98c0812
40deff8
657001d
147e2d9
b1c4871
eabcfd0
5485040
0a934e8
e5ec16b
c7922af
7a4c6c9
526fae6
2c251cf
2d001b9
74b7c51
dc84e3e
6a5b7c9
e8eca13
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package dht | ||
|
||
import ( | ||
"crypto/aes" | ||
"crypto/cipher" | ||
"crypto/rand" | ||
"crypto/sha256" | ||
"errors" | ||
|
||
"github.com/multiformats/go-multihash" | ||
) | ||
|
||
const ( | ||
keySize = 32 | ||
encryptedPeerIDLength = 66 | ||
) | ||
|
||
var errInvalidKeySize = errors.New("key size must be 32 bytes") | ||
|
||
func encryptAES(plaintext, key []byte) ([]byte, error) { | ||
aesgcm, err := newAESGCM(key) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
nonce := make([]byte, aesgcm.NonceSize()) | ||
_, err = rand.Read(nonce) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
ct := aesgcm.Seal(nil, nonce, plaintext, nil) | ||
return append(nonce, ct...), nil | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We probably need to append encryption algorithm here too so that the encryption function rotation doesn't require coordinated client update. I.e. for example There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good point, will add! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is going to add 6 bytes to every encrypted value. Can we use a varint as code for AESGSM, then update the spec to document it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @masih do you have a pointer to the AESGCM varint? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe we will have to define this mask ourselves and then document in the spec what that number means There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right. We'd need to define those in the spec ourselves. This can also be a new multicodec. I recommend picking a varint that's not reserved already to have the option of adding it to the multicodec table later on. |
||
} | ||
|
||
func decryptAES(nonceAndCT, key []byte) ([]byte, error) { | ||
aesgcm, err := newAESGCM(key) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
nonce := nonceAndCT[:aesgcm.NonceSize()] | ||
ciphertext := nonceAndCT[aesgcm.NonceSize():] | ||
plaintext, err := aesgcm.Open(nil, nonce, ciphertext, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return plaintext, nil | ||
} | ||
|
||
func newAESGCM(key []byte) (cipher.AEAD, error) { | ||
if len(key) != keySize { | ||
return nil, errInvalidKeySize | ||
} | ||
|
||
block, err := aes.NewCipher(key[:]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
aesgcm, err := cipher.NewGCM(block) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return aesgcm, nil | ||
} | ||
|
||
func multihashToKey(mh multihash.Multihash) []byte { | ||
const prefix = "AESGCM" | ||
h := sha256.Sum256(append([]byte(prefix), mh...)) | ||
return h[:] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package dht | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
var testKey = []byte("AES256Key-32Characters1234567890") | ||
|
||
func TestAES(t *testing.T) { | ||
plaintext := []byte("nootwashere") | ||
ciphertext, err := encryptAES(plaintext, testKey) | ||
require.NoError(t, err) | ||
plaintextRes, err := decryptAES(ciphertext, testKey) | ||
require.NoError(t, err) | ||
require.Equal(t, plaintext, plaintextRes) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,5 +10,4 @@ build: | |
protoc --proto_path=$(GOPATH)/src:. --gogofast_out=. $< | ||
|
||
clean: | ||
rm -f *.pb.go | ||
rm -f *.go | ||
rm -f *.pb.go |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be possible to get these constants from somewhere where they are already defined instead of defining them again? This would help with code maintainability :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unfortunately the Go AES package doesn't define these :/ I'll try looking somewhere for them!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should be able to get Nonce size from
cipher.AEAD
interface.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated