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