2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2020 Sergey Matveev <stargrave@stargrave.org>
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.
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.
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/>.
25 xdr "github.com/davecgh/go-xdr/xdr2"
26 "go.cypherpunks.ru/balloon"
27 "golang.org/x/crypto/blake2b"
28 "golang.org/x/crypto/chacha20poly1305"
32 DefaultS = 1 << 20 / 32
38 MagicNNCPBv3 [8]byte = [8]byte{'N', 'N', 'C', 'P', 'B', 0, 0, 3}
50 func blake256() hash.Hash {
51 h, err := blake2b.New256(nil)
58 // Create an encrypted blob. sCost -- memory space requirements, number
59 // of hash-output sized (32 bytes) blocks. tCost -- time requirements,
60 // number of rounds. pCost -- number of parallel jobs.
61 func NewEBlob(sCost, tCost, pCost int, password, data []byte) ([]byte, error) {
64 if _, err = rand.Read(salt[:]); err != nil {
75 var eblobBuf bytes.Buffer
76 if _, err = xdr.Marshal(&eblobBuf, &eblob); err != nil {
79 key := balloon.H(blake256, password, salt[:], sCost, tCost, pCost)
80 aead, err := chacha20poly1305.New(key)
84 buf := make([]byte, 0, len(data)+aead.Overhead())
85 buf = aead.Seal(buf, make([]byte, aead.NonceSize()), data, eblobBuf.Bytes())
88 if _, err = xdr.Marshal(&eblobBuf, &eblob); err != nil {
91 return eblobBuf.Bytes(), nil
94 func DeEBlob(eblobRaw, password []byte) ([]byte, error) {
97 if _, err = xdr.Unmarshal(bytes.NewReader(eblobRaw), &eblob); err != nil {
100 if eblob.Magic != MagicNNCPBv3 {
111 aead, err := chacha20poly1305.New(key)
116 ciphertext := eblob.Blob
118 var eblobBuf bytes.Buffer
119 if _, err = xdr.Marshal(&eblobBuf, &eblob); err != nil {
122 data, err := aead.Open(
124 make([]byte, aead.NonceSize()),