summaryrefslogtreecommitdiffhomepage
path: root/noise-types.go
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2018-02-07 18:58:38 +0100
committerJason A. Donenfeld <Jason@zx2c4.com>2018-02-07 18:59:41 +0100
commit51a6001bb92c2af95415ad631344be98e54da18c (patch)
treec7d8624c5def9ab47c2e010b88b95eb376159f7f /noise-types.go
parentceccd394200b9152f4df57e5bbe7d7e9002b8704 (diff)
Go treats underscores specially
In case there's ever a platform called helpers or protocol, we don't want to be doing this.
Diffstat (limited to 'noise-types.go')
-rw-r--r--noise-types.go74
1 files changed, 74 insertions, 0 deletions
diff --git a/noise-types.go b/noise-types.go
new file mode 100644
index 0000000..1a944df
--- /dev/null
+++ b/noise-types.go
@@ -0,0 +1,74 @@
+package main
+
+import (
+ "crypto/subtle"
+ "encoding/hex"
+ "errors"
+ "golang.org/x/crypto/chacha20poly1305"
+)
+
+const (
+ NoisePublicKeySize = 32
+ NoisePrivateKeySize = 32
+)
+
+type (
+ NoisePublicKey [NoisePublicKeySize]byte
+ NoisePrivateKey [NoisePrivateKeySize]byte
+ NoiseSymmetricKey [chacha20poly1305.KeySize]byte
+ NoiseNonce uint64 // padded to 12-bytes
+)
+
+func loadExactHex(dst []byte, src string) error {
+ slice, err := hex.DecodeString(src)
+ if err != nil {
+ return err
+ }
+ if len(slice) != len(dst) {
+ return errors.New("Hex string does not fit the slice")
+ }
+ copy(dst, slice)
+ return nil
+}
+
+func (key NoisePrivateKey) IsZero() bool {
+ var zero NoisePrivateKey
+ return key.Equals(zero)
+}
+
+func (key NoisePrivateKey) Equals(tar NoisePrivateKey) bool {
+ return subtle.ConstantTimeCompare(key[:], tar[:]) == 1
+}
+
+func (key *NoisePrivateKey) FromHex(src string) error {
+ return loadExactHex(key[:], src)
+}
+
+func (key NoisePrivateKey) ToHex() string {
+ return hex.EncodeToString(key[:])
+}
+
+func (key *NoisePublicKey) FromHex(src string) error {
+ return loadExactHex(key[:], src)
+}
+
+func (key NoisePublicKey) ToHex() string {
+ return hex.EncodeToString(key[:])
+}
+
+func (key NoisePublicKey) IsZero() bool {
+ var zero NoisePublicKey
+ return key.Equals(zero)
+}
+
+func (key NoisePublicKey) Equals(tar NoisePublicKey) bool {
+ return subtle.ConstantTimeCompare(key[:], tar[:]) == 1
+}
+
+func (key *NoiseSymmetricKey) FromHex(src string) error {
+ return loadExactHex(key[:], src)
+}
+
+func (key NoiseSymmetricKey) ToHex() string {
+ return hex.EncodeToString(key[:])
+}