2024-04-25 22:07:42 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/zip"
|
|
|
|
"bytes"
|
|
|
|
"crypto/cipher"
|
|
|
|
"crypto/hmac"
|
|
|
|
"crypto/rand"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"hash"
|
|
|
|
"io"
|
|
|
|
"math"
|
|
|
|
"os"
|
2024-04-26 18:14:05 +00:00
|
|
|
"os/signal"
|
2024-04-25 22:07:42 +00:00
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2024-04-27 17:10:26 +00:00
|
|
|
"time"
|
2024-04-25 22:07:42 +00:00
|
|
|
|
|
|
|
"github.com/HACKERALERT/infectious"
|
|
|
|
"github.com/HACKERALERT/serpent"
|
|
|
|
"github.com/schollz/progressbar/v3"
|
|
|
|
"golang.org/x/crypto/argon2"
|
|
|
|
"golang.org/x/crypto/blake2b"
|
|
|
|
"golang.org/x/crypto/chacha20"
|
|
|
|
"golang.org/x/crypto/hkdf"
|
|
|
|
"golang.org/x/crypto/sha3"
|
|
|
|
"golang.org/x/term"
|
|
|
|
)
|
|
|
|
|
2024-04-27 17:10:26 +00:00
|
|
|
var f *bool
|
|
|
|
var k *bool
|
|
|
|
var p *bool
|
|
|
|
var r *bool
|
|
|
|
var mode string
|
2024-04-25 22:07:42 +00:00
|
|
|
|
2024-04-27 17:10:26 +00:00
|
|
|
func parse() int {
|
2024-04-25 22:07:42 +00:00
|
|
|
flag.Usage = func() {
|
2024-04-27 17:10:26 +00:00
|
|
|
fmt.Println("Usage: picocrypt <item1> [<item2> ...]")
|
|
|
|
fmt.Println("Items: can be files, folders, or globs")
|
|
|
|
fmt.Println("Flags:")
|
|
|
|
flag.PrintDefaults()
|
|
|
|
os.Exit(1)
|
2024-04-25 22:07:42 +00:00
|
|
|
}
|
2024-04-27 17:10:26 +00:00
|
|
|
f = flag.Bool("f", false, "(decryption) attempt to fix corruption")
|
2024-04-27 18:05:54 +00:00
|
|
|
k = flag.Bool("k", false, "(decryption) keep output even if corrupted")
|
2024-04-27 17:10:26 +00:00
|
|
|
p = flag.Bool("p", false, "(encryption) use paranoid mode")
|
|
|
|
r = flag.Bool("r", false, "(encryption) encode with Reed-Solomon")
|
2024-04-25 22:07:42 +00:00
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
if flag.NArg() == 0 {
|
|
|
|
flag.Usage()
|
|
|
|
}
|
2024-04-27 17:10:26 +00:00
|
|
|
for _, v := range flag.Args() {
|
|
|
|
if strings.HasPrefix(v, "-") {
|
|
|
|
fmt.Println("Flags must be provided before arguments!")
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-25 22:07:42 +00:00
|
|
|
if flag.NArg() == 1 {
|
|
|
|
if strings.HasSuffix(flag.Arg(0), ".pcv") {
|
|
|
|
mode = "decrypt"
|
|
|
|
} else {
|
|
|
|
mode = "encrypt"
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
mode = "encrypt"
|
|
|
|
for _, v := range flag.Args() {
|
|
|
|
if strings.HasSuffix(v, ".pcv") {
|
2024-04-27 17:10:26 +00:00
|
|
|
fmt.Println("Multiple items must not contain volumes.")
|
2024-04-25 22:07:42 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-27 17:10:26 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
var password []byte
|
|
|
|
var confirmp []byte
|
|
|
|
var err error
|
|
|
|
|
|
|
|
func auth() int {
|
2024-04-25 22:07:42 +00:00
|
|
|
if mode == "encrypt" {
|
|
|
|
fmt.Print("Password: ")
|
|
|
|
password, err = term.ReadPassword(int(os.Stdin.Fd()))
|
|
|
|
if err != nil {
|
2024-04-27 17:10:26 +00:00
|
|
|
fmt.Println("Error reading password!")
|
2024-04-25 22:07:42 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
fmt.Print(strings.Repeat("*", len(password)), " | Confirm: ")
|
2024-04-27 17:10:26 +00:00
|
|
|
confirmp, err = term.ReadPassword(int(os.Stdin.Fd()))
|
2024-04-25 22:07:42 +00:00
|
|
|
if err != nil {
|
2024-04-27 17:10:26 +00:00
|
|
|
fmt.Println("Error reading password!")
|
2024-04-25 22:07:42 +00:00
|
|
|
return 1
|
|
|
|
}
|
2024-04-27 17:10:26 +00:00
|
|
|
fmt.Println(strings.Repeat("*", len(confirmp)))
|
|
|
|
if !bytes.Equal(password, confirmp) {
|
|
|
|
fmt.Println("Passwords don't match!")
|
2024-04-25 22:07:42 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fmt.Print("Password: ")
|
|
|
|
password, err = term.ReadPassword(int(os.Stdin.Fd()))
|
|
|
|
if err != nil {
|
2024-04-27 17:10:26 +00:00
|
|
|
fmt.Println("Error reading password!")
|
2024-04-25 22:07:42 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
fmt.Println(strings.Repeat("*", len(password)))
|
|
|
|
}
|
|
|
|
|
2024-04-27 17:10:26 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
var pin string
|
|
|
|
var pout string
|
|
|
|
var pzip string
|
|
|
|
var file *os.File
|
|
|
|
var writer *zip.Writer
|
|
|
|
var files []string
|
|
|
|
var interrupted bool
|
|
|
|
|
|
|
|
func prepare() int {
|
2024-04-25 22:07:42 +00:00
|
|
|
if mode == "decrypt" {
|
2024-04-27 17:10:26 +00:00
|
|
|
pin = flag.Arg(0)
|
|
|
|
pout = strings.TrimSuffix(pin, ".pcv")
|
2024-04-25 22:07:42 +00:00
|
|
|
} else {
|
|
|
|
stat, err := os.Stat(flag.Arg(0))
|
|
|
|
if flag.NArg() == 1 && err == nil && !stat.IsDir() {
|
2024-04-27 17:10:26 +00:00
|
|
|
pin = flag.Arg(0)
|
|
|
|
pout = pin + ".pcv"
|
2024-04-25 22:07:42 +00:00
|
|
|
} else {
|
|
|
|
items := []string{}
|
|
|
|
for _, v := range flag.Args() {
|
|
|
|
if strings.Contains(v, "../") || strings.HasPrefix(v, "/") {
|
|
|
|
fmt.Println("Cannot encrypt outside of current directory.")
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
matches, err := filepath.Glob(v)
|
|
|
|
if err != nil {
|
2024-04-27 17:10:26 +00:00
|
|
|
fmt.Println("Invalid glob pattern:", v)
|
2024-04-25 22:07:42 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
items = append(items, matches...)
|
|
|
|
}
|
|
|
|
for _, v := range items {
|
|
|
|
stat, err := os.Stat(v)
|
|
|
|
if err != nil {
|
2024-04-27 17:10:26 +00:00
|
|
|
fmt.Println("Cannot access input:", v)
|
2024-04-25 22:07:42 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
if !stat.IsDir() {
|
|
|
|
files = append(files, v)
|
|
|
|
} else {
|
|
|
|
filepath.Walk(v, func(path string, _ os.FileInfo, _ error) error {
|
|
|
|
stat, err := os.Stat(path)
|
|
|
|
if err == nil && !stat.IsDir() {
|
|
|
|
files = append(files, path)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(files) == 0 {
|
2024-04-27 17:10:26 +00:00
|
|
|
fmt.Println("Nothing to encrypt!")
|
2024-04-25 22:07:42 +00:00
|
|
|
return 1
|
|
|
|
}
|
2024-04-27 17:10:26 +00:00
|
|
|
}
|
|
|
|
}
|
2024-04-25 22:07:42 +00:00
|
|
|
|
2024-04-27 17:10:26 +00:00
|
|
|
return 0
|
|
|
|
}
|
2024-04-26 18:14:05 +00:00
|
|
|
|
2024-04-27 17:10:26 +00:00
|
|
|
func compress() int {
|
|
|
|
if files == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
dir, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Cannot get current working directory!")
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
dir = filepath.ToSlash(dir)
|
|
|
|
file, err = os.CreateTemp("", "picocrypt-cli-v2-*.tmp")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Cannot create temporary file!")
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
pzip = file.Name()
|
|
|
|
writer = zip.NewWriter(file)
|
|
|
|
|
|
|
|
for i, path := range files {
|
|
|
|
stat, err := os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
header, err := zip.FileInfoHeader(stat)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
abs, err := filepath.Abs(path)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
abs = filepath.ToSlash(abs)
|
|
|
|
header.Name = strings.TrimPrefix(abs, dir)
|
|
|
|
header.Name = strings.TrimPrefix(header.Name, "/")
|
|
|
|
header.Method = zip.Deflate
|
|
|
|
entry, err := writer.CreateHeader(header)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fin, err := os.Open(path)
|
|
|
|
if err != nil {
|
2024-04-25 22:07:42 +00:00
|
|
|
writer.Close()
|
|
|
|
file.Close()
|
2024-04-27 17:10:26 +00:00
|
|
|
fmt.Println("Read access to input denied:", path)
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
bar := progressbar.NewOptions(
|
|
|
|
int(stat.Size()),
|
|
|
|
progressbar.OptionClearOnFinish(),
|
|
|
|
progressbar.OptionFullWidth(),
|
|
|
|
progressbar.OptionShowBytes(true),
|
|
|
|
progressbar.OptionUseIECUnits(true),
|
|
|
|
progressbar.OptionSetDescription(
|
|
|
|
fmt.Sprintf("Compressing [%d/%d]:", i+1, len(files)),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
_, err = io.Copy(io.MultiWriter(entry, bar), fin)
|
|
|
|
fin.Close()
|
|
|
|
if err != nil {
|
|
|
|
if interrupted {
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
} else {
|
|
|
|
writer.Close()
|
|
|
|
file.Close()
|
|
|
|
fmt.Println("Insufficient disk space!")
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
writer.Close()
|
|
|
|
file.Close()
|
|
|
|
pin = file.Name()
|
|
|
|
pout = "encrypted-" + strconv.Itoa(int(time.Now().Unix())) + ".zip.pcv"
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
var fin *os.File
|
|
|
|
var fout *os.File
|
|
|
|
var padded bool
|
|
|
|
var salt []byte
|
|
|
|
var hkdfSalt []byte
|
|
|
|
var serpentIV []byte
|
|
|
|
var nonce []byte
|
|
|
|
var keyHash []byte
|
|
|
|
var keyHashRef []byte
|
|
|
|
var authTag []byte
|
|
|
|
var key []byte
|
|
|
|
var mac hash.Hash
|
|
|
|
var MiB = 1 << 20
|
|
|
|
var GiB = 1 << 30
|
|
|
|
var rs5, _ = infectious.NewFEC(5, 15)
|
|
|
|
var rs16, _ = infectious.NewFEC(16, 48)
|
|
|
|
var rs24, _ = infectious.NewFEC(24, 72)
|
|
|
|
var rs32, _ = infectious.NewFEC(32, 96)
|
|
|
|
var rs64, _ = infectious.NewFEC(64, 192)
|
|
|
|
var rs128, _ = infectious.NewFEC(128, 136)
|
|
|
|
|
|
|
|
func rsEncode(rs *infectious.FEC, data []byte) []byte {
|
|
|
|
res := make([]byte, rs.Total())
|
|
|
|
rs.Encode(data, func(s infectious.Share) {
|
|
|
|
res[s.Number] = s.Data[0]
|
|
|
|
})
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
func rsDecode(rs *infectious.FEC, data []byte, fast bool) ([]byte, error) {
|
|
|
|
if rs.Total() == 136 && fast {
|
|
|
|
return data[:128], nil
|
|
|
|
}
|
|
|
|
tmp := make([]infectious.Share, rs.Total())
|
|
|
|
for i := 0; i < rs.Total(); i++ {
|
|
|
|
tmp[i].Number = i
|
|
|
|
tmp[i].Data = append(tmp[i].Data, data[i])
|
|
|
|
}
|
|
|
|
res, err := rs.Decode(nil, tmp)
|
|
|
|
if err != nil {
|
|
|
|
if rs.Total() == 136 {
|
|
|
|
return data[:128], err
|
2024-04-25 22:07:42 +00:00
|
|
|
}
|
2024-04-27 17:10:26 +00:00
|
|
|
return data[:rs.Total()/3], err
|
2024-04-25 22:07:42 +00:00
|
|
|
}
|
2024-04-27 17:10:26 +00:00
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func pad(data []byte) []byte {
|
|
|
|
padLen := 128 - len(data)%128
|
|
|
|
padding := bytes.Repeat([]byte{byte(padLen)}, padLen)
|
|
|
|
return append(data, padding...)
|
|
|
|
}
|
2024-04-25 22:07:42 +00:00
|
|
|
|
2024-04-27 17:10:26 +00:00
|
|
|
func unpad(data []byte) []byte {
|
|
|
|
padLen := int(data[127])
|
|
|
|
return data[:128-padLen]
|
|
|
|
}
|
2024-04-25 22:07:42 +00:00
|
|
|
|
2024-04-27 17:10:26 +00:00
|
|
|
func work() int {
|
|
|
|
fin, err = os.Open(pin)
|
2024-04-25 22:07:42 +00:00
|
|
|
if err != nil {
|
2024-04-27 17:10:26 +00:00
|
|
|
fmt.Println("Error accessing input file:", pin)
|
2024-04-25 22:07:42 +00:00
|
|
|
return 1
|
|
|
|
}
|
2024-04-27 17:10:26 +00:00
|
|
|
_, err = os.Stat(pout)
|
2024-04-25 22:07:42 +00:00
|
|
|
if err == nil {
|
2024-04-27 17:10:26 +00:00
|
|
|
fmt.Println("Output file already exists!")
|
2024-04-25 22:07:42 +00:00
|
|
|
return 1
|
|
|
|
}
|
2024-04-27 17:10:26 +00:00
|
|
|
fout, err = os.Create(pout)
|
2024-04-25 22:07:42 +00:00
|
|
|
if err != nil {
|
2024-04-27 17:10:26 +00:00
|
|
|
fmt.Println("Error creating output file:", pout)
|
2024-04-25 22:07:42 +00:00
|
|
|
return 1
|
|
|
|
}
|
2024-04-27 17:10:26 +00:00
|
|
|
stat, err := os.Stat(pin)
|
2024-04-25 22:07:42 +00:00
|
|
|
if err != nil {
|
2024-04-27 17:10:26 +00:00
|
|
|
fmt.Println("Error accessing input file:", pin)
|
2024-04-25 22:07:42 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
total := stat.Size()
|
|
|
|
if mode == "decrypt" {
|
|
|
|
total -= 789
|
|
|
|
}
|
2024-04-27 17:10:26 +00:00
|
|
|
|
2024-04-25 22:07:42 +00:00
|
|
|
if mode == "encrypt" {
|
2024-04-27 17:10:26 +00:00
|
|
|
errs := make([]error, 10)
|
2024-04-25 22:07:42 +00:00
|
|
|
salt = make([]byte, 16)
|
|
|
|
hkdfSalt = make([]byte, 32)
|
|
|
|
serpentIV = make([]byte, 16)
|
|
|
|
nonce = make([]byte, 24)
|
|
|
|
_, errs[0] = fout.Write(rsEncode(rs5, []byte("v1.34")))
|
2024-04-27 17:10:26 +00:00
|
|
|
_, errs[1] = fout.Write(rsEncode(rs5, []byte("00000")))
|
2024-04-25 22:07:42 +00:00
|
|
|
flags := make([]byte, 5)
|
2024-04-27 17:10:26 +00:00
|
|
|
if *p {
|
2024-04-25 22:07:42 +00:00
|
|
|
flags[0] = 1
|
|
|
|
}
|
2024-04-27 17:10:26 +00:00
|
|
|
if *r {
|
2024-04-25 22:07:42 +00:00
|
|
|
flags[3] = 1
|
|
|
|
}
|
|
|
|
if total%int64(MiB) >= int64(MiB)-128 {
|
|
|
|
flags[4] = 1
|
|
|
|
}
|
2024-04-27 17:10:26 +00:00
|
|
|
_, errs[2] = fout.Write(rsEncode(rs5, flags))
|
2024-04-25 22:07:42 +00:00
|
|
|
rand.Read(salt)
|
|
|
|
rand.Read(hkdfSalt)
|
|
|
|
rand.Read(serpentIV)
|
|
|
|
rand.Read(nonce)
|
2024-04-27 17:10:26 +00:00
|
|
|
_, errs[3] = fout.Write(rsEncode(rs16, salt))
|
|
|
|
_, errs[4] = fout.Write(rsEncode(rs32, hkdfSalt))
|
|
|
|
_, errs[5] = fout.Write(rsEncode(rs16, serpentIV))
|
|
|
|
_, errs[6] = fout.Write(rsEncode(rs24, nonce))
|
|
|
|
_, errs[7] = fout.Write(make([]byte, 192))
|
|
|
|
_, errs[8] = fout.Write(make([]byte, 96))
|
|
|
|
_, errs[9] = fout.Write(make([]byte, 192))
|
2024-04-25 22:07:42 +00:00
|
|
|
for _, err := range errs {
|
|
|
|
if err != nil {
|
|
|
|
fin.Close()
|
|
|
|
fout.Close()
|
2024-04-27 17:10:26 +00:00
|
|
|
fmt.Println("Insufficient disk space!")
|
2024-04-25 22:07:42 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2024-04-27 17:10:26 +00:00
|
|
|
errs := make([]error, 9)
|
2024-04-25 22:07:42 +00:00
|
|
|
version := make([]byte, 15)
|
|
|
|
fin.Read(version)
|
2024-04-27 17:10:26 +00:00
|
|
|
_, errs[0] = rsDecode(rs5, version, !(*f))
|
2024-04-25 22:07:42 +00:00
|
|
|
tmp := make([]byte, 15)
|
|
|
|
fin.Read(tmp)
|
2024-04-27 17:10:26 +00:00
|
|
|
tmp, errs[1] = rsDecode(rs5, tmp, !(*f))
|
|
|
|
comments, _ := strconv.Atoi(string(tmp))
|
|
|
|
fin.Read(make([]byte, comments*3))
|
|
|
|
total -= int64(comments) * 3
|
2024-04-25 22:07:42 +00:00
|
|
|
flags := make([]byte, 15)
|
|
|
|
fin.Read(flags)
|
2024-04-27 17:10:26 +00:00
|
|
|
flags, errs[2] = rsDecode(rs5, flags, !(*f))
|
|
|
|
*p = flags[0] == 1
|
|
|
|
*r = flags[3] == 1
|
2024-04-25 22:07:42 +00:00
|
|
|
padded = flags[4] == 1
|
|
|
|
if flags[1] == 1 {
|
|
|
|
fin.Close()
|
|
|
|
fout.Close()
|
2024-04-27 17:10:26 +00:00
|
|
|
fmt.Println("Keyfiles are not supported!")
|
2024-04-25 22:07:42 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
salt = make([]byte, 48)
|
|
|
|
fin.Read(salt)
|
2024-04-27 17:10:26 +00:00
|
|
|
salt, errs[3] = rsDecode(rs16, salt, !(*f))
|
2024-04-25 22:07:42 +00:00
|
|
|
hkdfSalt = make([]byte, 96)
|
|
|
|
fin.Read(hkdfSalt)
|
2024-04-27 17:10:26 +00:00
|
|
|
hkdfSalt, errs[4] = rsDecode(rs32, hkdfSalt, !(*f))
|
2024-04-25 22:07:42 +00:00
|
|
|
serpentIV = make([]byte, 48)
|
|
|
|
fin.Read(serpentIV)
|
2024-04-27 17:10:26 +00:00
|
|
|
serpentIV, errs[5] = rsDecode(rs16, serpentIV, !(*f))
|
2024-04-25 22:07:42 +00:00
|
|
|
nonce = make([]byte, 72)
|
|
|
|
fin.Read(nonce)
|
2024-04-27 17:10:26 +00:00
|
|
|
nonce, errs[6] = rsDecode(rs24, nonce, !(*f))
|
2024-04-25 22:07:42 +00:00
|
|
|
keyHashRef = make([]byte, 192)
|
|
|
|
fin.Read(keyHashRef)
|
2024-04-27 17:10:26 +00:00
|
|
|
keyHashRef, errs[7] = rsDecode(rs64, keyHashRef, !(*f))
|
|
|
|
fin.Read(make([]byte, 96))
|
2024-04-25 22:07:42 +00:00
|
|
|
authTag = make([]byte, 192)
|
|
|
|
fin.Read(authTag)
|
2024-04-27 17:10:26 +00:00
|
|
|
authTag, errs[8] = rsDecode(rs64, authTag, !(*f))
|
2024-04-25 22:07:42 +00:00
|
|
|
for _, err := range errs {
|
|
|
|
if err != nil {
|
|
|
|
fin.Close()
|
|
|
|
fout.Close()
|
2024-04-27 17:10:26 +00:00
|
|
|
fmt.Println("The volume header is irrecoverably damaged!")
|
2024-04-25 22:07:42 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-27 17:10:26 +00:00
|
|
|
if *p {
|
|
|
|
key = argon2.IDKey(password, salt, 8, 1<<20, 8, 32)
|
2024-04-25 22:07:42 +00:00
|
|
|
} else {
|
2024-04-27 17:10:26 +00:00
|
|
|
key = argon2.IDKey(password, salt, 4, 1<<20, 4, 32)
|
2024-04-25 22:07:42 +00:00
|
|
|
}
|
|
|
|
tmp := sha3.New512()
|
|
|
|
tmp.Write(key)
|
|
|
|
keyHash = tmp.Sum(nil)
|
|
|
|
if mode == "decrypt" {
|
2024-04-27 17:10:26 +00:00
|
|
|
if !bytes.Equal(keyHash, keyHashRef) {
|
2024-04-25 22:07:42 +00:00
|
|
|
fin.Close()
|
|
|
|
fout.Close()
|
2024-04-27 17:10:26 +00:00
|
|
|
fmt.Println("Incorrect password!")
|
2024-04-25 22:07:42 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
done, counter := 0, 0
|
|
|
|
chacha, _ := chacha20.NewUnauthenticatedCipher(key, nonce)
|
|
|
|
subkey := make([]byte, 32)
|
|
|
|
hkdf := hkdf.New(sha3.New256, key, hkdfSalt, nil)
|
|
|
|
hkdf.Read(subkey)
|
2024-04-27 17:10:26 +00:00
|
|
|
if *p {
|
2024-04-25 22:07:42 +00:00
|
|
|
mac = hmac.New(sha3.New512, subkey)
|
|
|
|
} else {
|
|
|
|
mac, _ = blake2b.New512(subkey)
|
|
|
|
}
|
|
|
|
serpentKey := make([]byte, 32)
|
|
|
|
hkdf.Read(serpentKey)
|
|
|
|
s, _ := serpent.NewCipher(serpentKey)
|
|
|
|
serpent := cipher.NewCTR(s, serpentIV)
|
|
|
|
|
|
|
|
bar := progressbar.NewOptions(
|
|
|
|
int(total),
|
|
|
|
progressbar.OptionClearOnFinish(),
|
|
|
|
progressbar.OptionFullWidth(),
|
|
|
|
progressbar.OptionShowBytes(true),
|
|
|
|
progressbar.OptionUseIECUnits(true),
|
|
|
|
progressbar.OptionSetDescription(
|
|
|
|
(func() string {
|
|
|
|
if mode == "encrypt" {
|
|
|
|
return "Encrypting:"
|
|
|
|
}
|
|
|
|
return "Decrypting:"
|
|
|
|
})(),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
for {
|
|
|
|
var src []byte
|
2024-04-27 17:10:26 +00:00
|
|
|
if mode == "decrypt" && *r {
|
2024-04-25 22:07:42 +00:00
|
|
|
src = make([]byte, MiB/128*136)
|
|
|
|
} else {
|
|
|
|
src = make([]byte, MiB)
|
|
|
|
}
|
|
|
|
size, err := fin.Read(src)
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
src = src[:size]
|
|
|
|
dst := make([]byte, len(src))
|
2024-04-27 17:10:26 +00:00
|
|
|
bar.Write(src)
|
2024-04-25 22:07:42 +00:00
|
|
|
|
|
|
|
if mode == "encrypt" {
|
2024-04-27 17:10:26 +00:00
|
|
|
if *p {
|
2024-04-25 22:07:42 +00:00
|
|
|
serpent.XORKeyStream(dst, src)
|
|
|
|
copy(src, dst)
|
|
|
|
}
|
|
|
|
chacha.XORKeyStream(dst, src)
|
|
|
|
mac.Write(dst)
|
2024-04-27 17:10:26 +00:00
|
|
|
if *r {
|
2024-04-25 22:07:42 +00:00
|
|
|
copy(src, dst)
|
|
|
|
dst = nil
|
|
|
|
if len(src) == MiB {
|
|
|
|
for i := 0; i < MiB; i += 128 {
|
|
|
|
dst = append(dst, rsEncode(rs128, src[i:i+128])...)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
chunks := math.Floor(float64(len(src)) / 128)
|
|
|
|
for i := 0; float64(i) < chunks; i++ {
|
|
|
|
dst = append(dst, rsEncode(rs128, src[i*128:(i+1)*128])...)
|
|
|
|
}
|
|
|
|
dst = append(dst, rsEncode(rs128, pad(src[int(chunks*128):]))...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2024-04-27 17:10:26 +00:00
|
|
|
if *r {
|
2024-04-25 22:07:42 +00:00
|
|
|
copy(dst, src)
|
|
|
|
src = nil
|
|
|
|
if len(dst) == MiB/128*136 {
|
|
|
|
for i := 0; i < MiB/128*136; i += 136 {
|
2024-04-27 17:10:26 +00:00
|
|
|
tmp, err := rsDecode(rs128, dst[i:i+136], !(*f))
|
2024-04-25 22:07:42 +00:00
|
|
|
if err != nil {
|
|
|
|
fin.Close()
|
|
|
|
fout.Close()
|
2024-04-26 18:14:05 +00:00
|
|
|
fmt.Println("\nThe input file is irrecoverably damaged.")
|
2024-04-25 22:07:42 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
if i == MiB/128*136-136 && done+MiB/128*136 >= int(total) && padded {
|
|
|
|
tmp = unpad(tmp)
|
|
|
|
}
|
|
|
|
src = append(src, tmp...)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
chunks := len(dst)/136 - 1
|
|
|
|
for i := 0; i < chunks; i++ {
|
2024-04-27 17:10:26 +00:00
|
|
|
tmp, err := rsDecode(rs128, dst[i*136:(i+1)*136], !(*f))
|
2024-04-25 22:07:42 +00:00
|
|
|
if err != nil {
|
|
|
|
fin.Close()
|
|
|
|
fout.Close()
|
2024-04-26 18:14:05 +00:00
|
|
|
fmt.Println("\nThe input file is irrecoverably damaged.")
|
2024-04-25 22:07:42 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
src = append(src, tmp...)
|
|
|
|
}
|
2024-04-27 17:10:26 +00:00
|
|
|
tmp, err := rsDecode(rs128, dst[int(chunks)*136:], !(*f))
|
2024-04-25 22:07:42 +00:00
|
|
|
if err != nil {
|
|
|
|
fin.Close()
|
|
|
|
fout.Close()
|
2024-04-26 18:14:05 +00:00
|
|
|
fmt.Println("\nThe input file is irrecoverably damaged.")
|
2024-04-25 22:07:42 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
src = append(src, unpad(tmp)...)
|
|
|
|
}
|
|
|
|
dst = make([]byte, len(src))
|
|
|
|
}
|
|
|
|
mac.Write(src)
|
|
|
|
chacha.XORKeyStream(dst, src)
|
2024-04-27 17:10:26 +00:00
|
|
|
if *p {
|
2024-04-25 22:07:42 +00:00
|
|
|
copy(src, dst)
|
|
|
|
serpent.XORKeyStream(dst, src)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = fout.Write(dst)
|
|
|
|
if err != nil {
|
2024-04-27 17:10:26 +00:00
|
|
|
if interrupted {
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
} else {
|
|
|
|
fin.Close()
|
|
|
|
fout.Close()
|
|
|
|
fmt.Println("\nInsufficient disk space!")
|
|
|
|
return 1
|
2024-04-26 18:14:05 +00:00
|
|
|
}
|
2024-04-25 22:07:42 +00:00
|
|
|
}
|
2024-04-27 17:10:26 +00:00
|
|
|
if mode == "decrypt" && *r {
|
2024-04-25 22:07:42 +00:00
|
|
|
done += MiB / 128 * 136
|
|
|
|
} else {
|
|
|
|
done += MiB
|
|
|
|
}
|
|
|
|
|
|
|
|
if counter >= 60*GiB {
|
|
|
|
nonce = make([]byte, 24)
|
|
|
|
hkdf.Read(nonce)
|
|
|
|
chacha, _ = chacha20.NewUnauthenticatedCipher(key, nonce)
|
|
|
|
serpentIV = make([]byte, 16)
|
|
|
|
hkdf.Read(serpentIV)
|
|
|
|
serpent = cipher.NewCTR(s, serpentIV)
|
|
|
|
counter = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if mode == "encrypt" {
|
2024-04-27 17:10:26 +00:00
|
|
|
fout.Seek(309, 0)
|
2024-04-25 22:07:42 +00:00
|
|
|
fout.Write(rsEncode(rs64, keyHash))
|
|
|
|
fout.Write(rsEncode(rs32, make([]byte, 32)))
|
|
|
|
fout.Write(rsEncode(rs64, mac.Sum(nil)))
|
|
|
|
} else {
|
2024-04-27 17:10:26 +00:00
|
|
|
if !bytes.Equal(mac.Sum(nil), authTag) {
|
2024-04-25 22:07:42 +00:00
|
|
|
fin.Close()
|
|
|
|
fout.Close()
|
2024-04-27 17:10:26 +00:00
|
|
|
if *k {
|
|
|
|
fmt.Println("\nThe modified output has been kept.")
|
|
|
|
return 0
|
|
|
|
} else {
|
|
|
|
fmt.Println("\nThe input volume is damaged or modified!")
|
|
|
|
if *r {
|
|
|
|
fmt.Println("Fortunately, this volume is encoded with Reed-Solomon.")
|
|
|
|
fmt.Println("Try again using the '-f' flag to repair the corruption.")
|
|
|
|
}
|
|
|
|
return 1
|
2024-04-26 02:07:05 +00:00
|
|
|
}
|
2024-04-25 22:07:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fin.Close()
|
|
|
|
fout.Close()
|
2024-04-27 18:05:54 +00:00
|
|
|
fmt.Println("Completed -> ", fout.Name())
|
2024-04-25 22:07:42 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2024-04-27 17:10:26 +00:00
|
|
|
if parse() == 1 {
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
if auth() == 1 {
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
if prepare() == 1 {
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
c := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(c, os.Interrupt)
|
|
|
|
go func() {
|
|
|
|
<-c
|
|
|
|
interrupted = true
|
|
|
|
if fin != nil {
|
|
|
|
fin.Close()
|
|
|
|
}
|
|
|
|
if fout != nil {
|
|
|
|
fmt.Print("\nSystem interrupt detected, cleaning up incomplete output: ")
|
|
|
|
fout.Close()
|
|
|
|
if err := os.Remove(fout.Name()); err == nil {
|
|
|
|
fmt.Print("Success.")
|
|
|
|
} else {
|
|
|
|
fmt.Print("Failure.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if pzip != "" {
|
|
|
|
fmt.Print("\nSystem interrupt detected, cleaning up temporary files: ")
|
|
|
|
writer.Close()
|
|
|
|
file.Close()
|
|
|
|
if err := os.Remove(pzip); err == nil {
|
|
|
|
fmt.Print("Success.")
|
|
|
|
} else {
|
|
|
|
fmt.Print("Failure.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
os.Exit(1)
|
|
|
|
}()
|
|
|
|
|
|
|
|
if compress() == 1 {
|
|
|
|
os.Remove(pzip)
|
|
|
|
os.Exit(1)
|
|
|
|
} else {
|
|
|
|
defer os.Remove(pzip)
|
|
|
|
}
|
|
|
|
if work() == 1 {
|
|
|
|
if pzip != "" {
|
|
|
|
os.Remove(pzip)
|
|
|
|
}
|
|
|
|
if fout != nil {
|
|
|
|
os.Remove(fout.Name())
|
2024-04-26 18:14:05 +00:00
|
|
|
}
|
2024-04-27 17:10:26 +00:00
|
|
|
os.Exit(1)
|
2024-04-26 18:14:05 +00:00
|
|
|
}
|
2024-04-25 22:07:42 +00:00
|
|
|
}
|