]> Cypherpunks.ru repositories - nncp.git/blob - src/cmd/nncp-cfgenc/main.go
465963d7fefc2e2c47ae6eeb88b369fbab22ae6a
[nncp.git] / src / cmd / nncp-cfgenc / main.go
1 /*
2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2021 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, version 3 of the License.
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 // NNCP configuration file encrypter/decrypter.
19 package main
20
21 import (
22         "bytes"
23         "errors"
24         "flag"
25         "fmt"
26         "io/ioutil"
27         "log"
28         "os"
29
30         xdr "github.com/davecgh/go-xdr/xdr2"
31         "go.cypherpunks.ru/nncp/v8"
32         "golang.org/x/crypto/blake2b"
33         "golang.org/x/term"
34 )
35
36 func usage() {
37         fmt.Fprintf(os.Stderr, nncp.UsageHeader())
38         fmt.Fprintf(os.Stderr, "nncp-cfgenc -- encrypt/decrypt configuration file\n\n")
39         fmt.Fprintf(os.Stderr, "Usage: %s [options] cfg.hjson > cfg.hjson.eblob\n", os.Args[0])
40         fmt.Fprintf(os.Stderr, "       %s [options] -d cfg.hjson.eblob > cfg.hjson\n", os.Args[0])
41         fmt.Fprintf(os.Stderr, "       %s [options] -dump cfg.hjson.eblob\n", os.Args[0])
42         fmt.Fprintln(os.Stderr, "Options:")
43         flag.PrintDefaults()
44 }
45
46 func main() {
47         var (
48                 decrypt  = flag.Bool("d", false, "Decrypt the file")
49                 dump     = flag.Bool("dump", false, "Print human-readable eblob information")
50                 sOpt     = flag.Int("s", nncp.DefaultS, "Balloon space cost, in 32 bytes chunks")
51                 tOpt     = flag.Int("t", nncp.DefaultT, "Balloon time cost, number of rounds")
52                 pOpt     = flag.Int("p", nncp.DefaultP, "Balloon number of parallel jobs")
53                 version  = flag.Bool("version", false, "Print version information")
54                 warranty = flag.Bool("warranty", false, "Print warranty information")
55         )
56         log.SetFlags(log.Lshortfile)
57         flag.Usage = usage
58         flag.Parse()
59         if *warranty {
60                 fmt.Println(nncp.Warranty)
61                 return
62         }
63         if *version {
64                 fmt.Println(nncp.VersionGet())
65                 return
66         }
67
68         if flag.NArg() != 1 {
69                 usage()
70                 os.Exit(1)
71         }
72
73         data, err := ioutil.ReadFile(flag.Arg(0))
74         if err != nil {
75                 log.Fatalln("Can not read data:", err)
76         }
77         if *dump {
78                 var eblob nncp.EBlob
79                 if _, err := xdr.Unmarshal(bytes.NewReader(data), &eblob); err != nil {
80                         log.Fatalln(err)
81                 }
82                 switch eblob.Magic {
83                 case nncp.MagicNNCPBv1.B:
84                         log.Fatalln(nncp.MagicNNCPBv1.TooOld())
85                 case nncp.MagicNNCPBv2.B:
86                         log.Fatalln(nncp.MagicNNCPBv2.TooOld())
87                 case nncp.MagicNNCPBv3.B:
88                 default:
89                         log.Fatalln(errors.New("Unknown eblob type"))
90                 }
91                 fmt.Println("Strengthening function: Balloon with BLAKE2b-256")
92                 fmt.Printf("Memory space cost: %d bytes\n", eblob.SCost*blake2b.Size256)
93                 fmt.Printf("Number of rounds: %d\n", eblob.TCost)
94                 fmt.Printf("Number of parallel jobs: %d\n", eblob.PCost)
95                 fmt.Printf("Blob size: %d\n", len(eblob.Blob))
96                 return
97         }
98
99         os.Stderr.WriteString("Passphrase:")
100         password1, err := term.ReadPassword(0)
101         if err != nil {
102                 log.Fatalln(err)
103         }
104         if *decrypt {
105                 cfgRaw, err := nncp.DeEBlob(data, password1)
106                 if err != nil {
107                         log.Fatalln(err)
108                 }
109                 os.Stdout.Write(cfgRaw)
110                 return
111         }
112         os.Stderr.WriteString("\nRepeat passphrase:")
113         password2, err := term.ReadPassword(0)
114         if err != nil {
115                 log.Fatalln(err)
116         }
117         os.Stderr.WriteString("\n")
118         if bytes.Compare(password1, password2) != 0 {
119                 log.Fatalln(errors.New("Passphrases do not match"))
120         }
121         eblob, err := nncp.NewEBlob(*sOpt, *tOpt, *pOpt, password1, data)
122         if err != nil {
123                 log.Fatalln(err)
124         }
125         os.Stdout.Write(eblob)
126 }