2 gohpenc -- Go high-performance encryption utility
3 Copyright (C) 2017-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/>.
18 // Go high-performance encryption utility
34 "golang.org/x/crypto/chacha20poly1305"
35 "golang.org/x/crypto/poly1305"
44 doPSK = flag.Bool("psk", false, "Generate PSK")
45 keyB32 = flag.String("k", "", "Encryption key")
46 decrypt = flag.Bool("d", false, "Decrypt, instead of encrypt")
47 blockSize = flag.Int("b", 1<<10, "Blocksize, in KiB")
48 threads = flag.Int("c", runtime.NumCPU(), "Number of threads")
50 Base32Codec *base32.Encoding = base32.StdEncoding.WithPadding(base32.NoPadding)
72 func NewWorker(key, salt []byte) *Worker {
73 aead, err := chacha20poly1305.NewX(key)
79 nonce: make([]byte, chacha20poly1305.NonceSizeX),
80 input: make([]byte, LenSize+bs+poly1305.TagSize),
81 ready: make(chan struct{}),
82 task: make(chan Task),
83 output: make(chan []byte),
84 written: make(chan struct{}),
91 func (w *Worker) Run() {
97 binary.BigEndian.PutUint64(w.nonce[SaltSize:], task.ctr)
99 output, err = w.aead.Open(
102 w.input[LenSize:LenSize+task.size+poly1305.TagSize],
108 output = output[LenSize:]
110 binary.BigEndian.PutUint32(w.input, uint32(task.size))
111 output = w.aead.Seal(
114 w.input[LenSize:LenSize+task.size],
128 key := make([]byte, chacha20poly1305.KeySize)
129 if _, err := io.ReadFull(rand.Reader, key); err != nil {
132 fmt.Println(Base32Codec.EncodeToString(key))
137 key, err = Base32Codec.DecodeString(*keyB32)
141 if len(key) != chacha20poly1305.KeySize {
142 panic("Invalid key size")
144 salt := make([]byte, SaltSize)
147 if _, err = io.ReadFull(os.Stdin, salt); err != nil {
151 if _, err = io.ReadFull(rand.Reader, salt); err != nil {
154 if _, err = os.Stdout.Write(salt); err != nil {
159 bs = *blockSize * (1 << 10)
161 panic("blocksize exceeds 32-bits")
163 stdin := bufio.NewReaderSize(os.Stdin, LenSize+bs+poly1305.TagSize)
165 workers := make([]*Worker, *threads)
166 for i := 0; i < *threads; i++ {
167 workers[i] = NewWorker(key, salt)
172 w := workers[ctr%uint64(len(workers))]
173 if _, err := os.Stdout.Write(<-w.output); err != nil {
176 w.written <- struct{}{}
184 w := workers[ctr%uint64(len(workers))]
187 _, err = io.ReadFull(stdin, w.input[:LenSize])
194 size = int(binary.BigEndian.Uint32(w.input[:LenSize]))
195 if _, err = io.ReadFull(
197 w.input[LenSize:LenSize+size+poly1305.TagSize],
202 size, err = stdin.Read(w.input[LenSize : LenSize+bs])
211 w.task <- Task{ctr, size}