]> Cypherpunks.ru repositories - nncp.git/blob - src/cypherpunks.ru/nncp/eblob.go
816388dfbb98878655153dbe6060c934461c743c
[nncp.git] / src / cypherpunks.ru / nncp / eblob.go
1 /*
2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2019 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         "bytes"
23         "crypto/rand"
24         "hash"
25
26         "cypherpunks.ru/balloon"
27         "github.com/davecgh/go-xdr/xdr2"
28         "golang.org/x/crypto/blake2b"
29         "golang.org/x/crypto/chacha20poly1305"
30 )
31
32 const (
33         DefaultS = 1 << 20 / 32
34         DefaultT = 1 << 4
35         DefaultP = 2
36 )
37
38 var (
39         MagicNNCPBv3 [8]byte = [8]byte{'N', 'N', 'C', 'P', 'B', 0, 0, 3}
40 )
41
42 type EBlob struct {
43         Magic [8]byte
44         SCost uint32
45         TCost uint32
46         PCost uint32
47         Salt  *[32]byte
48         Blob  []byte
49 }
50
51 func blake256() hash.Hash {
52         h, err := blake2b.New256(nil)
53         if err != nil {
54                 panic(err)
55         }
56         return h
57 }
58
59 // Create an encrypted blob. sCost -- memory space requirements, number
60 // of hash-output sized (32 bytes) blocks. tCost -- time requirements,
61 // number of rounds. pCost -- number of parallel jobs.
62 func NewEBlob(sCost, tCost, pCost int, password, data []byte) ([]byte, error) {
63         salt := new([32]byte)
64         var err error
65         if _, err = rand.Read(salt[:]); err != nil {
66                 return nil, err
67         }
68         eblob := EBlob{
69                 Magic: MagicNNCPBv3,
70                 SCost: uint32(sCost),
71                 TCost: uint32(tCost),
72                 PCost: uint32(pCost),
73                 Salt:  salt,
74                 Blob:  nil,
75         }
76         var eblobBuf bytes.Buffer
77         if _, err = xdr.Marshal(&eblobBuf, &eblob); err != nil {
78                 return nil, err
79         }
80         key := balloon.H(blake256, password, salt[:], sCost, tCost, pCost)
81         aead, err := chacha20poly1305.New(key)
82         if err != nil {
83                 return nil, err
84         }
85         buf := make([]byte, 0, len(data)+aead.Overhead())
86         buf = aead.Seal(buf, make([]byte, aead.NonceSize()), data, eblobBuf.Bytes())
87         eblob.Blob = buf
88         eblobBuf.Reset()
89         if _, err = xdr.Marshal(&eblobBuf, &eblob); err != nil {
90                 return nil, err
91         }
92         return eblobBuf.Bytes(), nil
93 }
94
95 func DeEBlob(eblobRaw, password []byte) ([]byte, error) {
96         var eblob EBlob
97         var err error
98         if _, err = xdr.Unmarshal(bytes.NewReader(eblobRaw), &eblob); err != nil {
99                 return nil, err
100         }
101         if eblob.Magic != MagicNNCPBv3 {
102                 return nil, BadMagic
103         }
104         key := balloon.H(
105                 blake256,
106                 password,
107                 eblob.Salt[:],
108                 int(eblob.SCost),
109                 int(eblob.TCost),
110                 int(eblob.PCost),
111         )
112         aead, err := chacha20poly1305.New(key)
113         if err != nil {
114                 return nil, err
115         }
116
117         ciphertext := eblob.Blob
118         eblob.Blob = nil
119         var eblobBuf bytes.Buffer
120         if _, err = xdr.Marshal(&eblobBuf, &eblob); err != nil {
121                 return nil, err
122         }
123         data, err := aead.Open(
124                 ciphertext[:0],
125                 make([]byte, aead.NonceSize()),
126                 ciphertext,
127                 eblobBuf.Bytes(),
128         )
129         if err != nil {
130                 return nil, err
131         }
132         return data, nil
133 }