-
Notifications
You must be signed in to change notification settings - Fork 6
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
Add enclave encrypt package and use go 1.21 #42
Changes from 6 commits
7388aa2
bbd3a10
2696d8c
18cbf61
0f31ad7
e589efc
7d81613
83c7615
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 |
---|---|---|
|
@@ -14,9 +14,9 @@ jobs: | |
- uses: actions/checkout@v2 | ||
|
||
- name: Set up Go | ||
uses: actions/setup-go@v2 | ||
uses: actions/setup-go@93397bea11091df50f3d7e59dc26a7711a8bcfbe # v4.1.0 | ||
with: | ||
go-version: 1.19 | ||
go-version: '1.21' | ||
|
||
- name: Get | ||
run: go get -v | ||
|
@@ -25,7 +25,7 @@ jobs: | |
uses: golangci/[email protected] | ||
with: | ||
args: ./... | ||
version: v1.53.2 | ||
version: v1.55.2 | ||
|
||
- name: Build | ||
run: go build -v ./... | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
module github.com/tkhq/go-sdk | ||
|
||
go 1.20 | ||
go 1.21 | ||
|
||
require ( | ||
github.com/go-openapi/errors v0.20.4 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
go 1.21.0 | ||
|
||
use ( | ||
. | ||
./pkg/enclave_encrypt | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
github.com/bwesterb/go-ristretto v1.2.3/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0= | ||
github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk= | ||
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= | ||
go.opentelemetry.io/otel/sdk v1.14.0/go.mod h1:bwIC5TjrNG6QDCHNWvW4HLHtUQ4I+VQDsnjhvyZCALM= | ||
golang.org/x/mod v0.9.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= | ||
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= | ||
golang.org/x/term v0.16.0/go.mod h1:yn7UURbUtPyrVJPGPq404EukNFxcm/foM+bV/bfcDsY= | ||
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.PHONY: test | ||
test: | ||
go test ./... |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
package enclave_encrypt | ||
|
||
import ( | ||
"crypto/ecdsa" | ||
"crypto/elliptic" | ||
"crypto/sha256" | ||
"errors" | ||
"fmt" | ||
"reflect" | ||
|
||
"github.com/btcsuite/btcutil/base58" | ||
"github.com/cloudflare/circl/kem" | ||
) | ||
|
||
// An instance of the client side for enclave encrypt protocol. This should only be used for either | ||
// a SINGLE send or a single receive. | ||
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 could "enforce" this by setting a private boolean value when it has been "used", but that doesn't necessarily prevent misuse 🤷 |
||
type EnclaveEncryptClient struct { | ||
enclaveAuthKey *ecdsa.PublicKey | ||
targetPrivate kem.PrivateKey | ||
} | ||
|
||
// Create a client from the quorum public key. | ||
func NewEnclaveEncryptClient(enclaveAuthKey *ecdsa.PublicKey) (*EnclaveEncryptClient, error) { | ||
_, targetPrivate, err := KemId.Scheme().GenerateKeyPair() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &EnclaveEncryptClient{ | ||
enclaveAuthKey, | ||
targetPrivate, | ||
}, nil | ||
} | ||
|
||
// Create a client from the quorum public key and target key pair. | ||
func NewEnclaveEncryptClientFromTargetKey(enclaveAuthKey *ecdsa.PublicKey, targetPrivateKey *kem.PrivateKey) (*EnclaveEncryptClient, error) { | ||
return &EnclaveEncryptClient{ | ||
enclaveAuthKey, | ||
*targetPrivateKey, | ||
}, nil | ||
} | ||
|
||
// Encrypt some plaintext to the given server target key. | ||
func (c *EnclaveEncryptClient) Encrypt(plaintext Bytes, msg ServerTargetMsg) (*ClientSendMsg, error) { | ||
if !P256Verify(c.enclaveAuthKey, *msg.TargetPublic, *msg.TargetPublicSignature) { | ||
return nil, errors.New("invalid enclave auth key signature") | ||
} | ||
targetPublic, err := KemId.Scheme().UnmarshalBinaryPublicKey((*msg.TargetPublic)[:]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
ciphertext, encappedPublic, err := encrypt( | ||
&targetPublic, | ||
plaintext, | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
enc := Bytes(encappedPublic) | ||
ciph := Bytes(ciphertext) | ||
return &ClientSendMsg{ | ||
EncappedPublic: &enc, | ||
Ciphertext: &ciph, | ||
}, nil | ||
} | ||
|
||
// Decrypt a message from the server. | ||
func (c *EnclaveEncryptClient) Decrypt(msg ServerSendMsg) (plaintext []byte, err error) { | ||
if !P256Verify(c.enclaveAuthKey, *msg.EncappedPublic, *msg.EncappedPublicSignature) { | ||
return nil, errors.New("invalid enclave auth key signature") | ||
} | ||
|
||
return decrypt( | ||
*msg.EncappedPublic, | ||
c.targetPrivate, | ||
*msg.Ciphertext, | ||
) | ||
} | ||
|
||
// Get this clients target public key. | ||
func (c *EnclaveEncryptClient) TargetPublic() ([]byte, error) { | ||
return c.targetPrivate.Public().MarshalBinary() | ||
} | ||
|
||
// Decrypt a message from the server. | ||
func (c *EnclaveEncryptClient) AuthDecrypt(payload string) (plaintext []byte, err error) { | ||
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. What's the difference between
|
||
payloadBytes := base58.Decode(payload) | ||
err = ValidateChecksum(payloadBytes) | ||
if err != nil { | ||
return nil, err | ||
} | ||
// Trim the checksum | ||
payloadBytes = payloadBytes[:len(payloadBytes)-4] | ||
|
||
if len(payloadBytes) < 33 { | ||
return nil, errors.New("payload is less then 33 bytes, the length of the expected public key") | ||
} | ||
compressedKey := payloadBytes[0:33] | ||
ciphertext := payloadBytes[33:] | ||
|
||
x, y := elliptic.UnmarshalCompressed(elliptic.P256(), compressedKey) | ||
|
||
// FIXME: `elliptic.Unmarshal` is deprecated, but scm does not know how to replace it. | ||
// nolint:staticcheck | ||
encappedPublic := elliptic.Marshal(elliptic.P256(), x, y) | ||
|
||
return decrypt( | ||
encappedPublic, | ||
c.targetPrivate, | ||
ciphertext, | ||
) | ||
} | ||
|
||
// Validates that a payload has a valid checksum in the last four bytes. | ||
func ValidateChecksum(payload []byte) error { | ||
if len(payload) < 5 { | ||
return fmt.Errorf("payload length is < 5 (length: %d)", len(payload)) | ||
} | ||
expected := checksum(payload[:len(payload)-4]) | ||
if !reflect.DeepEqual(expected[:], payload[len(payload)-4:]) { | ||
return fmt.Errorf("checksum mismatch for payload %02x: %v (computed) != %v (last four bytes)", payload, expected, payload[len(payload)-4:]) | ||
} | ||
return nil | ||
} | ||
|
||
// Takes a payload and return a checksum (4 bytes) | ||
// The double-hash operation is dictated by the base58check standard | ||
// See https://en.bitcoin.it/wiki/Base58Check_encoding#Creating_a_Base58Check_string | ||
func checksum(payload []byte) (checkSum [4]byte) { | ||
h := sha256.Sum256(payload) | ||
h2 := sha256.Sum256(h[:]) | ||
copy(checkSum[:], h2[:4]) | ||
return checkSum | ||
} |
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.
omg we've needed this so bad for e2e tests