aboutsummaryrefslogtreecommitdiffstats
path: root/device/noise-types.go
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2019-03-03 04:04:41 +0100
committerJason A. Donenfeld <Jason@zx2c4.com>2019-03-03 05:00:40 +0100
commit69f0fe67b63d90e523a5a1241fb1b46c2e8dbe03 (patch)
tree1ef86da3242afde462dcadb7241bb09f499d5bd7 /device/noise-types.go
parenttun: windows: expose GUID (diff)
downloadwireguard-go-69f0fe67b63d90e523a5a1241fb1b46c2e8dbe03.tar.xz
wireguard-go-69f0fe67b63d90e523a5a1241fb1b46c2e8dbe03.zip
global: begin modularization
Diffstat (limited to 'device/noise-types.go')
-rw-r--r--device/noise-types.go81
1 files changed, 81 insertions, 0 deletions
diff --git a/device/noise-types.go b/device/noise-types.go
new file mode 100644
index 0000000..82b12c1
--- /dev/null
+++ b/device/noise-types.go
@@ -0,0 +1,81 @@
+/* SPDX-License-Identifier: MIT
+ *
+ * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
+ */
+
+package device
+
+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) (err error) {
+ err = loadExactHex(key[:], src)
+ key.clamp()
+ return
+}
+
+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[:])
+}