2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2022 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
46 func blake256() hash.Hash {
47 h, err := blake2b.New256(nil)
54 // Create an encrypted blob. sCost -- memory space requirements, number
55 // of hash-output sized (32 bytes) blocks. tCost -- time requirements,
56 // number of rounds. pCost -- number of parallel jobs.
57 func NewEBlob(sCost, tCost, pCost int, password, data []byte) ([]byte, error) {
60 if _, err = rand.Read(salt[:]); err != nil {
64 Magic: MagicNNCPBv3.B,
71 var eblobBuf bytes.Buffer
72 if _, err = xdr.Marshal(&eblobBuf, &eblob); err != nil {
75 key := balloon.H(blake256, password, salt[:], sCost, tCost, pCost)
76 aead, err := chacha20poly1305.New(key)
80 buf := make([]byte, 0, len(data)+aead.Overhead())
81 buf = aead.Seal(buf, make([]byte, aead.NonceSize()), data, eblobBuf.Bytes())
84 if _, err = xdr.Marshal(&eblobBuf, &eblob); err != nil {
87 return eblobBuf.Bytes(), nil
90 func DeEBlob(eblobRaw, password []byte) ([]byte, error) {
93 if _, err = xdr.Unmarshal(bytes.NewReader(eblobRaw), &eblob); err != nil {
98 err = MagicNNCPBv1.TooOld()
100 err = MagicNNCPBv1.TooOld()
116 aead, err := chacha20poly1305.New(key)
121 ciphertext := eblob.Blob
123 var eblobBuf bytes.Buffer
124 if _, err = xdr.Marshal(&eblobBuf, &eblob); err != nil {
127 data, err := aead.Open(
129 make([]byte, aead.NonceSize()),