github.com/noisysockets/noisysockets@v0.21.2-0.20240515114641-7f467e651c90/types/noise_types.go (about) 1 // SPDX-License-Identifier: MPL-2.0 2 /* 3 * Copyright (C) 2024 The Noisy Sockets Authors. 4 * 5 * This Source Code Form is subject to the terms of the Mozilla Public 6 * License, v. 2.0. If a copy of the MPL was not distributed with this 7 * file, You can obtain one at http://mozilla.org/MPL/2.0/. 8 * 9 * Portions of this file are based on code originally from wireguard-go, 10 * 11 * Copyright (C) 2017-2023 WireGuard LLC. All Rights Reserved. 12 * 13 * Permission is hereby granted, free of charge, to any person obtaining a copy of 14 * this software and associated documentation files (the "Software"), to deal in 15 * the Software without restriction, including without limitation the rights to 16 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 17 * of the Software, and to permit persons to whom the Software is furnished to do 18 * so, subject to the following conditions: 19 * 20 * The above copyright notice and this permission notice shall be included in all 21 * copies or substantial portions of the Software. 22 * 23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 24 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 25 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 26 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 27 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 28 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 29 * SOFTWARE. 30 */ 31 32 package types 33 34 import ( 35 "crypto/rand" 36 "crypto/subtle" 37 "encoding/base64" 38 39 "golang.org/x/crypto/curve25519" 40 ) 41 42 const ( 43 NoisePublicKeySize = 32 44 NoisePrivateKeySize = 32 45 NoisePresharedKeySize = 32 46 ) 47 48 type ( 49 NoisePublicKey [NoisePublicKeySize]byte 50 NoisePrivateKey [NoisePrivateKeySize]byte 51 NoisePresharedKey [NoisePresharedKeySize]byte 52 NoiseNonce uint64 // padded to 12-bytes 53 ) 54 55 func NewPrivateKey() (sk NoisePrivateKey, err error) { 56 _, err = rand.Read(sk[:]) 57 sk.clamp() 58 return 59 } 60 61 func (sk *NoisePrivateKey) UnmarshalText(text []byte) error { 62 b, err := base64.StdEncoding.DecodeString(string(text)) 63 if err != nil { 64 return err 65 } 66 copy(sk[:], b) 67 return nil 68 } 69 70 func (sk *NoisePrivateKey) clamp() { 71 sk[0] &= 248 72 sk[31] = (sk[31] & 127) | 64 73 } 74 75 func (sk NoisePrivateKey) Public() (pk NoisePublicKey) { 76 apk := (*[NoisePublicKeySize]byte)(&pk) 77 ask := (*[NoisePrivateKeySize]byte)(&sk) 78 curve25519.ScalarBaseMult(apk, ask) 79 return 80 } 81 82 func (sk NoisePrivateKey) Equals(tar NoisePrivateKey) bool { 83 return subtle.ConstantTimeCompare(sk[:], tar[:]) == 1 84 } 85 86 func (sk NoisePrivateKey) String() string { 87 return base64.StdEncoding.EncodeToString(sk[:]) 88 } 89 90 func (pk *NoisePublicKey) UnmarshalText(text []byte) error { 91 b, err := base64.StdEncoding.DecodeString(string(text)) 92 if err != nil { 93 return err 94 } 95 96 copy(pk[:], b) 97 return nil 98 } 99 100 func (pk NoisePublicKey) Equals(tar NoisePublicKey) bool { 101 return subtle.ConstantTimeCompare(pk[:], tar[:]) == 1 102 } 103 104 func (pk NoisePublicKey) String() string { 105 return base64.StdEncoding.EncodeToString(pk[:]) 106 } 107 108 func (pk NoisePublicKey) DisplayString() string { 109 base64Key := base64.StdEncoding.EncodeToString(pk[:]) 110 return base64Key[0:4] + "…" + base64Key[39:43] 111 }