]> Cypherpunks.ru repositories - nncp.git/blob - src/cypherpunks.ru/nncp/node.go
Initial
[nncp.git] / src / cypherpunks.ru / nncp / node.go
1 /*
2 NNCP -- Node-to-Node CoPy
3 Copyright (C) 2016-2017 Sergey Matveev <stargrave@stargrave.org>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 package nncp
20
21 import (
22         "crypto/rand"
23         "errors"
24
25         "github.com/flynn/noise"
26         "golang.org/x/crypto/blake2b"
27         "golang.org/x/crypto/ed25519"
28         "golang.org/x/crypto/nacl/box"
29 )
30
31 type NodeId [blake2b.Size256]byte
32
33 func (id NodeId) String() string {
34         return ToBase32(id[:])
35 }
36
37 type Node struct {
38         Name     string
39         Id       *NodeId
40         ExchPub  *[32]byte
41         SignPub  ed25519.PublicKey
42         NoisePub *[32]byte
43         Incoming *string
44         Freq     *string
45         Via      []*NodeId
46 }
47
48 type NodeOur struct {
49         Id       *NodeId
50         ExchPub  *[32]byte
51         ExchPrv  *[32]byte
52         SignPub  ed25519.PublicKey
53         SignPrv  ed25519.PrivateKey
54         NoisePub *[32]byte
55         NoisePrv *[32]byte
56 }
57
58 func NewNodeGenerate() (*NodeOur, error) {
59         exchPub, exchPrv, err := box.GenerateKey(rand.Reader)
60         if err != nil {
61                 return nil, err
62         }
63         signPub, signPrv, err := ed25519.GenerateKey(rand.Reader)
64         if err != nil {
65                 return nil, err
66         }
67         noiseKey := noise.DH25519.GenerateKeypair(rand.Reader)
68         noisePub := new([32]byte)
69         noisePrv := new([32]byte)
70         copy(noisePrv[:], noiseKey.Private)
71         copy(noisePub[:], noiseKey.Public)
72
73         id := NodeId(blake2b.Sum256([]byte(signPub)))
74         node := NodeOur{
75                 Id:       &id,
76                 ExchPub:  exchPub,
77                 ExchPrv:  exchPrv,
78                 SignPub:  signPub,
79                 SignPrv:  signPrv,
80                 NoisePub: noisePub,
81                 NoisePrv: noisePrv,
82         }
83         return &node, nil
84 }
85
86 func (nodeOur *NodeOur) Their() *Node {
87         return &Node{
88                 Name:    "self",
89                 Id:      nodeOur.Id,
90                 ExchPub: nodeOur.ExchPub,
91                 SignPub: nodeOur.SignPub,
92         }
93 }
94
95 func NodeIdFromString(raw string) (*NodeId, error) {
96         decoded, err := FromBase32(raw)
97         if err != nil {
98                 return nil, err
99         }
100         if len(decoded) != blake2b.Size256 {
101                 return nil, errors.New("Invalid node id size")
102         }
103         buf := new([blake2b.Size256]byte)
104         copy(buf[:], decoded)
105         nodeId := NodeId(*buf)
106         return &nodeId, nil
107 }