-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
147 lines (125 loc) · 3.17 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"bytes"
"crypto/rand"
"encoding/hex"
"flag"
"fmt"
"golang.org/x/crypto/hkdf"
"golang.org/x/crypto/pbkdf2"
"hash"
"io"
"log"
"os"
"github.com/pedroalbanese/xoodoo/xoodyak"
)
var (
aad = flag.String("a", "", "Additional Associated data.")
dec = flag.Bool("d", false, "Decrypt instead Encrypt.")
file = flag.String("f", "", "Target file. ('-' for STDIN)")
iter = flag.Int("i", 1024, "Iterations. (for PBKDF2)")
key = flag.String("k", "", "128-bit key to Encrypt/Decrypt.")
pbkdf = flag.String("p", "", "Password-based key derivation function.")
random = flag.Bool("r", false, "Generate random 128-bit cryptographic key.")
kdf = flag.Bool("h", false, "HMAC-based key derivation function.")
salt = flag.String("s", "", "Salt. (for PBKDF2)")
)
func main() {
flag.Parse()
if len(os.Args) < 2 {
fmt.Fprintln(os.Stderr, "Xoodyak Encryption Tool - ALBANESE Research Lab (c) 2023")
fmt.Fprintln(os.Stderr, "Lightweight AEAD Cryptographic Scheme written in Pure Go\n")
fmt.Fprintln(os.Stderr, "Usage of "+os.Args[0]+":")
fmt.Fprintln(os.Stderr, os.Args[0]+" [-d] -p \"pass\" [-i N] [-s \"salt\"] -f <file.ext>")
flag.PrintDefaults()
os.Exit(1)
}
if *random == true {
var key []byte
var err error
key = make([]byte, 16)
_, err = io.ReadFull(rand.Reader, key)
if err != nil {
log.Fatal(err)
}
fmt.Println(hex.EncodeToString(key))
os.Exit(0)
}
if *kdf {
hash, err := Hkdf([]byte(*key), []byte(*salt), []byte(*aad))
if err != nil {
log.Fatal(err)
}
fmt.Printf("%x\n", hash[:128/8])
os.Exit(0)
}
var keyHex string
var keyRaw []byte
if *pbkdf != "" {
keyRaw = pbkdf2.Key([]byte(*pbkdf), []byte(*salt), *iter, 16, xoodyak.NewXoodyakHash)
keyHex = hex.EncodeToString(keyRaw)
} else {
keyHex = *key
}
var key []byte
var err error
if keyHex == "" {
key = make([]byte, 16)
_, err = io.ReadFull(rand.Reader, key)
if err != nil {
log.Fatal(err)
}
fmt.Fprintln(os.Stderr, "Key=", hex.EncodeToString(key))
} else {
key, err = hex.DecodeString(keyHex)
if err != nil {
log.Fatal(err)
}
if len(key) != 16 {
log.Fatal(err)
}
}
buf := bytes.NewBuffer(nil)
var data io.Reader
if *file == "-" {
data = os.Stdin
} else {
data, _ = os.Open(*file)
}
io.Copy(buf, data)
msg := buf.Bytes()
aead, err := xoodyak.NewXoodyakAEAD(key)
if err != nil {
panic(err)
}
if *dec == false {
nonce := make([]byte, aead.NonceSize(), aead.NonceSize()+len(msg)+aead.Overhead())
if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
log.Fatal(err)
}
out := aead.Seal(nonce, nonce, msg, []byte(*aad))
fmt.Printf("%s", out)
os.Exit(0)
}
if *dec == true {
nonce, msg := msg[:aead.NonceSize()], msg[aead.NonceSize():]
out, err := aead.Open(nil, nonce, msg, []byte(*aad))
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s", out)
os.Exit(0)
}
}
func Hkdf(master, salt, info []byte) ([128]byte, error) {
var myHash func() hash.Hash
myHash = func() hash.Hash {
return xoodyak.NewXoodyakHash()
}
hkdf := hkdf.New(myHash, master, salt, []byte(*aad))
key := make([]byte, 128/8)
_, err := io.ReadFull(hkdf, key)
var result [128]byte
copy(result[:], key)
return result, err
}