]> Cypherpunks.ru repositories - nncp.git/blob - src/cypherpunks.ru/nncp/cmd/nncp-cfgenc/main.go
Encrypted configuration file
[nncp.git] / src / cypherpunks.ru / nncp / cmd / nncp-cfgenc / main.go
1 /*
2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
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 // NNCP configuration file encrypter/decrypter.
20 package main
21
22 import (
23         "bytes"
24         "errors"
25         "flag"
26         "fmt"
27         "io/ioutil"
28         "log"
29         "os"
30
31         "cypherpunks.ru/nncp"
32         "github.com/davecgh/go-xdr/xdr2"
33         "golang.org/x/crypto/blake2b"
34         "golang.org/x/crypto/ssh/terminal"
35 )
36
37 func usage() {
38         fmt.Fprintf(os.Stderr, nncp.UsageHeader())
39         fmt.Fprintln(os.Stderr, "nncp-cfgenc -- encrypt/decrypt configuration file\n")
40         fmt.Fprintf(os.Stderr, "Usage: %s [options] cfg.yaml > cfg.yaml.eblob\n", os.Args[0])
41         fmt.Fprintf(os.Stderr, "       %s [options] -d cfg.yaml.eblob > cfg.yaml\n", os.Args[0])
42         fmt.Fprintf(os.Stderr, "       %s [options] -dump cfg.yaml.eblob\n", os.Args[0])
43         fmt.Fprintln(os.Stderr, "Options:")
44         flag.PrintDefaults()
45 }
46
47 func main() {
48         var (
49                 decrypt  = flag.Bool("d", false, "Decrypt the file")
50                 dump     = flag.Bool("dump", false, "Print human-readable eblob information")
51                 sOpt     = flag.Int("s", nncp.DefaultS, "Balloon space cost, in 32 bytes chunks")
52                 tOpt     = flag.Int("t", nncp.DefaultT, "Balloon time cost, number of rounds")
53                 pOpt     = flag.Int("p", nncp.DefaultP, "Balloon number of parallel jobs")
54                 version  = flag.Bool("version", false, "Print version information")
55                 warranty = flag.Bool("warranty", false, "Print warranty information")
56         )
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                 if eblob.Magic != nncp.MagicNNCPBv1 {
83                         log.Fatalln(errors.New("Unknown eblob type"))
84                 }
85                 fmt.Println("Strengthening function: Balloon with BLAKE2b-256")
86                 fmt.Printf("Memory space cost: %d bytes\n", eblob.SCost*blake2b.Size256)
87                 fmt.Printf("Number of rounds: %d\n", eblob.TCost)
88                 fmt.Printf("Number of parallel jobs: %d\n", eblob.PCost)
89                 fmt.Printf("Blob size: %d\n", len(eblob.Blob))
90                 os.Exit(0)
91         }
92         if *decrypt {
93                 os.Stderr.WriteString("Passphrase:")
94                 password, err := terminal.ReadPassword(0)
95                 if err != nil {
96                         log.Fatalln(err)
97                 }
98                 os.Stderr.WriteString("\n")
99                 cfgRaw, err := nncp.DeEBlob(data, password)
100                 if err != nil {
101                         log.Fatalln(err)
102                 }
103                 os.Stdout.Write(cfgRaw)
104         } else {
105                 os.Stderr.WriteString("Passphrase:")
106                 password1, err := terminal.ReadPassword(0)
107                 if err != nil {
108                         log.Fatalln(err)
109                 }
110                 os.Stderr.WriteString("\n")
111                 os.Stderr.WriteString("Repeat passphrase:")
112                 password2, err := terminal.ReadPassword(0)
113                 if err != nil {
114                         log.Fatalln(err)
115                 }
116                 os.Stderr.WriteString("\n")
117                 if bytes.Compare(password1, password2) != 0 {
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         }
126 }