-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
358 lines (347 loc) · 10.3 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package main
import (
"encoding/base64"
"fmt"
"os"
"strings"
dilithium "github.com/kudelskisecurity/crystals-go/crystals-dilithium"
kyber "github.com/kudelskisecurity/crystals-go/crystals-kyber"
"github.com/mariiatuzovska/logger"
"github.com/urfave/cli"
)
var (
ServiceName = "crystals-kyber"
Version = "1.0"
log logger.LoggerService
)
const fatalExitCode = 1
func main() {
app := cli.NewApp()
app.Name = serviceNameVithVersion()
app.Usage = "command line client"
app.Description = "crystals-kyber encryption/decryption service"
app.Version = Version
app.Authors = []cli.Author{{Name: "Tuzovska Mariia", Email: "[email protected]"}}
app.Commands = []cli.Command{
{
Name: "kyber",
Aliases: []string{"k"},
Usage: "Kyber algorithms kyber512/kyber768/kyber1024",
Subcommands: []cli.Command{
{
Name: "key-gen",
Aliases: []string{"k"},
Action: func(c *cli.Context) error {
setLog(c)
log.Info("Generating [kyber] public and secret keys has been started")
k := getKyberAlg(c)
seed := getSeed(c)
pk, sk := k.PKEKeyGen(seed)
log.Info("Kyber keys have been generated")
base64PK, base64SK := base64.StdEncoding.EncodeToString(pk), base64.StdEncoding.EncodeToString(sk)
fmt.Println("Public key:", base64PK, "\n")
fmt.Println("Secret key:", base64SK, "\n")
return nil
},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "alg",
Usage: "Algorithm name (kyber512/kyber768/kyber1024)",
},
&cli.StringFlag{
Name: "seed",
Usage: "32 bytes encoded to base64 string seed",
},
&cli.StringFlag{
Name: "log-level",
Usage: "Log level (DEBUG/INFO/WARNING/ERROR/FATAL)",
Value: "ERROR",
},
},
},
{
Name: "encrypt",
Aliases: []string{"e", "enc"},
Action: func(c *cli.Context) error {
setLog(c)
log.Info("Encryption [kyber] has been started")
k := getKyberAlg(c)
pk, err := base64.StdEncoding.DecodeString(c.String("public-key"))
if err != nil {
log.Fatal("Unknown flag value: public-key is malformed", fatalExitCode)
}
seed := getSeed(c)
encrypted := k.Encrypt(pk, []byte(c.String("message")), seed)
base64EncryptedMSg := base64.StdEncoding.EncodeToString(encrypted)
fmt.Println("Ciphertext:", base64EncryptedMSg)
log.Info("Message has been encrypted")
return nil
},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "alg",
Usage: "Algorithm name (kyber512/kyber768/kyber1024)",
Value: "kyber768",
},
&cli.StringFlag{
Name: "seed",
Usage: "32 bytes encoded to base64 string seed",
},
&cli.StringFlag{
Name: "public-key",
Usage: "Base64 encoded public key",
Required: true,
},
&cli.StringFlag{
Name: "message",
Usage: "Message must be more then 256 bytes",
Required: true,
},
&cli.StringFlag{
Name: "log-level",
Usage: "Log level (DEBUG/INFO/WARNING/ERROR/FATAL)",
Value: "ERROR",
},
},
},
{
Name: "decrypt",
Aliases: []string{"d", "dec"},
Action: func(c *cli.Context) error {
setLog(c)
log.Info("Decryption [kyber] has been started")
k := getKyberAlg(c)
pk, err := base64.StdEncoding.DecodeString(c.String("secret-key"))
if err != nil {
log.Fatal("Unknown flag value: secret-key is malformed", fatalExitCode)
}
ciphertext, err := base64.StdEncoding.DecodeString(c.String("ciphertext"))
if err != nil {
log.Fatal("Unknown flag value: ciphertext is malformed", fatalExitCode)
}
decrypted := k.Decrypt(pk, ciphertext)
fmt.Println("Plaintext:", string(decrypted))
log.Info("Message has been decrypted")
return nil
},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "alg",
Usage: "Algorithm name (kyber512/kyber768/kyber1024)",
Value: "kyber768",
},
&cli.StringFlag{
Name: "log-level",
Usage: "Log level (DEBUG/INFO/WARNING/ERROR/FATAL)",
Value: "ERROR",
},
&cli.StringFlag{
Name: "secret-key",
Usage: "Base64 encoded secret key",
Required: true,
},
&cli.StringFlag{
Name: "ciphertext",
Usage: "Encrypted message",
Required: true,
},
},
},
},
},
{
Name: "dilithium",
Aliases: []string{"d"},
Usage: "Dilithium algorithms kyber512/kyber768/kyber1024",
Subcommands: []cli.Command{
{
Name: "key-gen",
Aliases: []string{"k"},
Action: func(c *cli.Context) error {
setLog(c)
log.Info("Generating [dilithium] public and secret keys has been started")
d := getDilithiumAlg(c)
seed := getSeed(c)
pk, sk := d.KeyGen(seed)
base64PK, base64SK := base64.StdEncoding.EncodeToString(pk), base64.StdEncoding.EncodeToString(sk)
fmt.Println("Public key:", base64PK, "\n")
fmt.Println("Secret key:", base64SK, "\n")
log.Info("Dilithium keys have been generated")
return nil
},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "alg",
Usage: "Algorithm name (dilithium2/dilithium3/dilithium5)",
Value: "dilithium3",
},
&cli.StringFlag{
Name: "seed",
Usage: "32 bytes encoded to base64 string seed",
},
&cli.StringFlag{
Name: "log-level",
Usage: "Log level (DEBUG/INFO/WARNING/ERROR/FATAL)",
Value: "ERROR",
},
},
},
{
Name: "sign",
Aliases: []string{"s"},
Action: func(c *cli.Context) error {
setLog(c)
log.Info("Signing [dilithium] message has been started")
d := getDilithiumAlg(c)
sk, err := base64.StdEncoding.DecodeString(c.String("secret-key"))
if err != nil {
log.Fatal("Unknown flag value: public-key is malformed", fatalExitCode)
}
log.Info("Secret key has been read")
signature := d.Sign(sk, []byte(c.String("message")))
base64OfSignature := base64.StdEncoding.EncodeToString(signature)
fmt.Println("Signature:", base64OfSignature, "\n")
log.Info("Signature has been created")
return nil
},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "alg",
Usage: "Algorithm name (kyber512/kyber768/kyber1024)",
Value: "kyber768",
},
&cli.StringFlag{
Name: "log-level",
Usage: "Log level (DEBUG/INFO/WARNING/ERROR/FATAL)",
Value: "ERROR",
},
&cli.StringFlag{
Name: "secret-key",
Usage: "Base64 encoded secret key",
Required: true,
},
&cli.StringFlag{
Name: "message",
Required: true,
},
},
},
{
Name: "verify",
Aliases: []string{"v"},
Action: func(c *cli.Context) error {
setLog(c)
log.Info("Verifying [dilithium] message has been started")
d := getDilithiumAlg(c)
pk, err := base64.StdEncoding.DecodeString(c.String("public-key"))
if err != nil {
log.Fatal("Unknown flag value: public-key is malformed", fatalExitCode)
}
log.Info("Public key has been read")
sgn, err := base64.StdEncoding.DecodeString(c.String("signature"))
if err != nil {
log.Fatal("Unknown flag value: signature is malformed", fatalExitCode)
}
log.Info("Signature has been read")
ok := d.Verify(pk, []byte(c.String("message")), sgn)
if ok {
fmt.Println("Signature verified: ok")
} else {
fmt.Println("Signature verified: signature is not valid")
}
log.Info("Signature has been verified")
return nil
},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "alg",
Usage: "Algorithm name (kyber512/kyber768/kyber1024)",
Value: "kyber768",
},
&cli.StringFlag{
Name: "log-level",
Usage: "Log level (DEBUG/INFO/WARNING/ERROR/FATAL)",
Value: "ERROR",
},
&cli.StringFlag{
Name: "public-key",
Usage: "Base64 encoded public key",
Required: true,
},
&cli.StringFlag{
Name: "message",
Required: true,
},
&cli.StringFlag{
Name: "signature",
Required: true,
},
},
},
},
},
}
app.Run(os.Args)
}
func serviceNameVithVersion() string {
return ServiceName + "-" + Version
}
func setLog(c *cli.Context) {
log = logger.NewLoggerService().SetServiceName(serviceNameVithVersion())
switch strings.ToUpper(c.String("log-level")) {
case "DEBUG":
log.SetLevel(logger.DebugLevel)
log.Debug("DEBUG log level has been set")
case "INFO":
log.SetLevel(logger.InfoLevel)
log.Info("INFO log level has been set")
case "WARNING":
log.SetLevel(logger.WarningLevel)
case "ERROR":
log.SetLevel(logger.ErrorLevel)
case "FATAL":
log.SetLevel(logger.FatalLevel)
default:
log.SetLevel(logger.ErrorLevel)
log.Errorf("Unknown flag value: log-level=%s. log-level should be from list: DEBUG/INFO/WARNING/ERROR/FATAL."+
" Starting with ERROR log level", c.String("log-level"))
}
}
func getKyberAlg(c *cli.Context) *kyber.Kyber {
switch strings.ToLower(c.String("alg")) {
case "kyber512":
return kyber.NewKyber512()
case "kyber768":
return kyber.NewKyber768()
case "kyber1024":
return kyber.NewKyber1024()
}
log.Fatalf("Unknown flag value: alg=%s. alg should be from list: kyber512/kyber768/kyber1024", fatalExitCode, c.String("alg"))
return nil
}
func getDilithiumAlg(c *cli.Context) *dilithium.Dilithium {
switch strings.ToLower(c.String("alg")) {
case "dilithium2":
return dilithium.NewDilithium2()
case "dilithium3":
return dilithium.NewDilithium3()
case "dilithium5":
return dilithium.NewDilithium5()
}
log.Fatalf("Unknown flag value: alg=%s. alg should be from list: dilithium2/dilithium3/dilithium5", fatalExitCode, c.String("alg"))
return nil
}
func getSeed(c *cli.Context) []byte {
if str := c.String("seed"); str != "" {
seed, err := base64.StdEncoding.DecodeString(str)
if err != nil {
log.Fatalf("Cannot decode base64 string: seed=%s. error: %v", fatalExitCode, str, err)
}
if len(seed) < 32 {
log.Fatalf("Cannot decode base64 string: seed=%s. error: seed must be 32 bytes encoded to base64 string", fatalExitCode, str)
}
log.Info("Seed has been set")
return seed
}
return nil
}