|
| 1 | +// Copy from https://github.com/WireGuard/wgctrl-go/blob/a9ab2273dd1075ea74b88c76f8757f8b4003fcbf/wgtypes/types.go#L71-L155 |
| 2 | + |
| 3 | +package generater |
| 4 | + |
| 5 | +import ( |
| 6 | + "crypto/rand" |
| 7 | + "encoding/base64" |
| 8 | + "fmt" |
| 9 | + |
| 10 | + "golang.org/x/crypto/curve25519" |
| 11 | +) |
| 12 | + |
| 13 | +// KeyLen is the expected key length for a WireGuard key. |
| 14 | +const KeyLen = 32 // wgh.KeyLen |
| 15 | + |
| 16 | +// A Key is a public, private, or pre-shared secret key. The Key constructor |
| 17 | +// functions in this package can be used to create Keys suitable for each of |
| 18 | +// these applications. |
| 19 | +type Key [KeyLen]byte |
| 20 | + |
| 21 | +// GenerateKey generates a Key suitable for use as a pre-shared secret key from |
| 22 | +// a cryptographically safe source. |
| 23 | +// |
| 24 | +// The output Key should not be used as a private key; use GeneratePrivateKey |
| 25 | +// instead. |
| 26 | +func GenerateKey() (Key, error) { |
| 27 | + b := make([]byte, KeyLen) |
| 28 | + if _, err := rand.Read(b); err != nil { |
| 29 | + return Key{}, fmt.Errorf("wgtypes: failed to read random bytes: %v", err) |
| 30 | + } |
| 31 | + |
| 32 | + return NewKey(b) |
| 33 | +} |
| 34 | + |
| 35 | +// GeneratePrivateKey generates a Key suitable for use as a private key from a |
| 36 | +// cryptographically safe source. |
| 37 | +func GeneratePrivateKey() (Key, error) { |
| 38 | + key, err := GenerateKey() |
| 39 | + if err != nil { |
| 40 | + return Key{}, err |
| 41 | + } |
| 42 | + |
| 43 | + // Modify random bytes using algorithm described at: |
| 44 | + // https://cr.yp.to/ecdh.html. |
| 45 | + key[0] &= 248 |
| 46 | + key[31] &= 127 |
| 47 | + key[31] |= 64 |
| 48 | + |
| 49 | + return key, nil |
| 50 | +} |
| 51 | + |
| 52 | +// NewKey creates a Key from an existing byte slice. The byte slice must be |
| 53 | +// exactly 32 bytes in length. |
| 54 | +func NewKey(b []byte) (Key, error) { |
| 55 | + if len(b) != KeyLen { |
| 56 | + return Key{}, fmt.Errorf("wgtypes: incorrect key size: %d", len(b)) |
| 57 | + } |
| 58 | + |
| 59 | + var k Key |
| 60 | + copy(k[:], b) |
| 61 | + |
| 62 | + return k, nil |
| 63 | +} |
| 64 | + |
| 65 | +// ParseKey parses a Key from a base64-encoded string, as produced by the |
| 66 | +// Key.String method. |
| 67 | +func ParseKey(s string) (Key, error) { |
| 68 | + b, err := base64.StdEncoding.DecodeString(s) |
| 69 | + if err != nil { |
| 70 | + return Key{}, fmt.Errorf("wgtypes: failed to parse base64-encoded key: %v", err) |
| 71 | + } |
| 72 | + |
| 73 | + return NewKey(b) |
| 74 | +} |
| 75 | + |
| 76 | +// PublicKey computes a public key from the private key k. |
| 77 | +// |
| 78 | +// PublicKey should only be called when k is a private key. |
| 79 | +func (k Key) PublicKey() Key { |
| 80 | + var ( |
| 81 | + pub [KeyLen]byte |
| 82 | + priv = [KeyLen]byte(k) |
| 83 | + ) |
| 84 | + |
| 85 | + // ScalarBaseMult uses the correct base value per https://cr.yp.to/ecdh.html, |
| 86 | + // so no need to specify it. |
| 87 | + curve25519.ScalarBaseMult(&pub, &priv) |
| 88 | + |
| 89 | + return Key(pub) |
| 90 | +} |
| 91 | + |
| 92 | +// String returns the base64-encoded string representation of a Key. |
| 93 | +// |
| 94 | +// ParseKey can be used to produce a new Key from this string. |
| 95 | +func (k Key) String() string { |
| 96 | + return base64.StdEncoding.EncodeToString(k[:]) |
| 97 | +} |
0 commit comments