github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/services/wireguard/key/key.go (about) 1 /* 2 * Copyright (C) 2018 The "MysteriumNetwork/node" Authors. 3 * 4 * This program is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 package key 19 20 import ( 21 "crypto/rand" 22 "encoding/base64" 23 24 "github.com/pkg/errors" 25 "golang.org/x/crypto/curve25519" 26 ) 27 28 const keyLength = 32 29 30 // PrivateKeyToPublicKey generates wireguard public key from private key 31 func PrivateKeyToPublicKey(key string) (string, error) { 32 k, err := base64.StdEncoding.DecodeString(key) 33 if err != nil { 34 return "", err 35 } 36 37 var pub [keyLength]byte 38 var priv [keyLength]byte 39 copy(priv[:], k[:keyLength]) 40 curve25519.ScalarBaseMult(&pub, &priv) 41 42 return base64.StdEncoding.EncodeToString(pub[:]), nil 43 } 44 45 // GeneratePrivateKey generates a private key 46 func GeneratePrivateKey() (string, error) { 47 randomBytes := make([]byte, keyLength) 48 if _, err := rand.Read(randomBytes); err != nil { 49 return "", errors.Wrapf(err, "failed to generate random bytes for private key") 50 } 51 52 // https://cr.yp.to/ecdh.html 53 randomBytes[0] &= 248 54 randomBytes[31] &= 127 55 randomBytes[31] |= 64 56 57 return base64.StdEncoding.EncodeToString(randomBytes), nil 58 }