-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathenc.go
578 lines (503 loc) · 11.3 KB
/
enc.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
package main
import (
"bytes"
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"strconv"
"strings"
)
import (
"crypto/aes"
"crypto/cipher"
"crypto/des"
"crypto/rand"
// "crypto/rc4"
"encoding/hex"
)
import (
"github.com/as/mute"
"github.com/as/pkcs7"
)
const (
Prefix = "enc: "
Unset = "‡"
)
var (
Role = Prefix[:3]
Opposite = func() string {
if Role == "dec" {
return "enc"
}
return "dec"
}()
NBuffers = 0
Streaming = false
BlockSize = 16
)
var args struct {
h, q bool
r bool
a, s, m string
l int
k, f, e string
i string
p string
}
var f *flag.FlagSet
type Std string
type Mode string
type Keylen string
type algorithm struct {
Std
Mode
Keylen
}
type blockstream struct {
cipher.Stream
}
type ECBEncrypter struct {
cipher.Block
}
type ECBDecrypter struct {
cipher.Block
}
func Blockify(s cipher.Stream) *blockstream {
return &blockstream{s}
}
func (s *blockstream) BlockSize() int {
return BlockSize
}
func (s *blockstream) CryptBlocks(dst, src []byte) {
s.XORKeyStream(dst, src)
}
func NewCBC(block cipher.Block, iv []byte) (cb cipher.BlockMode) {
if Role == "enc" {
return cipher.NewCBCEncrypter(block, iv)
}
return cipher.NewCBCDecrypter(block, iv)
}
func NewECB(block cipher.Block, iv []byte) (cb cipher.BlockMode) {
if Role == "enc" {
return NewECBEncrypter(block, iv)
}
return NewECBDecrypter(block, iv)
}
func NewCFB(block cipher.Block, iv []byte) (cb cipher.BlockMode) {
if Role == "enc" {
return Blockify(cipher.NewCFBEncrypter(block, iv))
}
return Blockify(cipher.NewCFBDecrypter(block, iv))
}
func NewCTR(block cipher.Block, iv []byte) (cb cipher.BlockMode) {
return Blockify(cipher.NewCTR(block, iv))
}
func NewOFB(block cipher.Block, iv []byte) (cb cipher.BlockMode) {
return Blockify(cipher.NewOFB(block, iv))
}
// NewECBEncrypter implements ECB encryption. This is for
// demonstration purposes only. ECB leaks information about
// the plaintext because given enc(M¹, K) → C¹ enc(M², K) → C²
// C¹ = C² if M¹ = e²
func NewECBEncrypter(b cipher.Block, iv []byte) cipher.BlockMode {
return &ECBEncrypter{b}
}
func (e *ECBEncrypter) CryptBlocks(dst, src []byte) {
bs := e.BlockSize()
for len(src) > 0 {
e.Encrypt(dst[:bs], src[:bs])
dst, src = dst[bs:], src[bs:]
}
}
// NewECBDecrypter implements ECB decryption.
func NewECBDecrypter(b cipher.Block, iv []byte) cipher.BlockMode {
return &ECBDecrypter{b}
}
func (e *ECBDecrypter) CryptBlocks(dst, src []byte) {
bs := e.BlockSize()
for len(src) > 0 {
e.Decrypt(dst[:bs], src[:bs])
dst, src = dst[bs:], src[bs:]
}
}
func (s Std) SetCipher(key []byte) (c cipher.Block, err error) {
switch s {
case "":
return aes.NewCipher(key)
case "aes":
return aes.NewCipher(key)
case "des":
return des.NewCipher(key)
case "des3":
return des.NewTripleDESCipher(key)
default:
return nil, fmt.Errorf("cipher not found: %s", s)
}
}
func (m Mode) SetMode(block cipher.Block, iv []byte) interface{} {
switch m {
case "cbc":
return NewCBC(block, iv)
case "ecb":
return NewECB(block, iv)
case "ctr":
Streaming = true
return cipher.NewCTR(block, iv)
case "ofb":
Streaming = true
return cipher.NewOFB(block, iv)
}
return nil
}
func Alg(std, mode string, key []byte) (opmode interface{}, err error) {
a := &algorithm{
Std(std),
Mode(mode),
Keylen(len(key)),
}
if bits := len(key) * 8; bits != args.l {
return nil, fmt.Errorf("bad key length for %s: %d", args.s, bits)
}
block, err := a.SetCipher(key)
if err != nil {
return nil, err
}
BlockSize = block.BlockSize()
iv := mustiv(BlockSize)
return a.SetMode(block, iv), nil
}
func init() {
f = flag.NewFlagSet("main", flag.ContinueOnError)
f.BoolVar(&args.h, "h", false, "")
f.BoolVar(&args.q, "?", false, "")
f.StringVar(&args.a, "a", "aes/cbc/256", "")
f.StringVar(&args.s, "s", "", "")
f.StringVar(&args.m, "m", "", "")
f.IntVar(&args.l, "l", 0, "")
// Allow someone to have an empty key by making the default
// something else.
f.StringVar(&args.k, "k", Unset, "")
f.StringVar(&args.f, "f", Unset, "")
f.StringVar(&args.e, "e", Unset, "")
f.StringVar(&args.p, "p", Unset, "")
f.BoolVar(&args.r, "r", false, "")
f.StringVar(&args.i, "i", "", "")
err := mute.Parse(f, os.Args[1:])
if err != nil {
printerr(err)
os.Exit(1)
}
}
func operation(s string) func(io.Writer, io.Reader, cipher.BlockMode) (int, error) {
switch s {
case "enc":
return encrypt
case "dec":
return decrypt
}
printerr("internal bug: report to author")
os.Exit(1)
return nil
}
func main() {
if args.h || args.q {
usage()
}
var (
err error
w = os.Stdout
r = os.Stdin
)
mustparsealg()
alg, err := Alg(args.s, args.m, mustkey())
if err != nil {
printerr(err)
os.Exit(1)
}
switch t := alg.(type) {
case cipher.BlockMode:
_, err = operation(Role)(w, r, t)
case cipher.Stream:
_, err = stream(w, r, t)
}
if err != nil && err != io.EOF {
printerr(err)
os.Exit(1)
}
}
func randinject(r io.Reader, n int) io.Reader {
rb := make([]byte, BlockSize)
rand.Read(rb)
return io.MultiReader(bytes.NewReader(rb), r)
}
func stream(w io.Writer, r io.Reader, s cipher.Stream) (int64, error) {
if args.r && Role == "enc" {
r = randinject(r, BlockSize)
}
sr := cipher.StreamReader{s, r}
if args.r && Role == "dec" {
if n, err := io.CopyN(ioutil.Discard, sr, int64(BlockSize)); err != nil {
return n, err
}
}
return io.Copy(w, sr)
}
func encrypt(w io.Writer, r io.Reader, alg cipher.BlockMode) (int, error) {
var (
err error
p []byte
n int
b = make([]byte, BlockSize)
)
if args.r {
r = randinject(r, BlockSize)
}
for err == nil {
n, err = r.Read(b)
if n < 1 {
continue
}
p, err = pkcs7.Pad(b[:n], BlockSize)
if len(p) < BlockSize {
continue
}
alg.CryptBlocks(p, p)
n, err = w.Write(p)
}
return n, err
}
func decrypt(w io.Writer, r io.Reader, alg cipher.BlockMode) (int, error) {
var (
err error
p []byte
n int
b = make([]byte, BlockSize*2)
)
for err == nil {
n, err = r.Read(b)
if err != nil {
break
}
if n < BlockSize {
continue
}
alg.CryptBlocks(b[:n], b[:n])
if p, err = pkcs7.Unpad(b[:n], BlockSize); err != nil {
break
}
if args.r {
args.r = false
continue
}
n, err = w.Write(p)
}
return n, err
}
//
// Goroutines
//
type ModReader struct {
mod int
r io.Reader
buf *bytes.Buffer
}
func newModReader(r io.Reader, m int) *ModReader {
return &ModReader{
m,
r,
new(bytes.Buffer),
}
}
func (m ModReader) Read(p []byte) (int, error) {
maxread := int64(len(p))
lr := io.LimitReader(m.r, maxread)
n, _ := m.buf.ReadFrom(lr)
if n == 0 {
return 0, io.EOF
}
align := m.buf.Len()
if int(n) > m.mod {
align -= align % m.mod
}
printerr("modreader: Read: m.mod", m.mod)
printerr("modreader: Read: (align)", align)
return io.ReadFull(m.buf, p[:align])
}
type MinReader struct {
min int
r io.Reader
}
func newMinReader(r io.Reader, m int) *MinReader {
return &MinReader{m, r}
}
func (m MinReader) Read(p []byte) (n int, err error) {
return io.ReadAtLeast(m.r, p, m.min)
}
//
// Helper
//
func println(v ...interface{}) {
fmt.Print(Prefix)
fmt.Println(v...)
}
func printerr(v ...interface{}) {
fmt.Fprint(os.Stderr, Prefix)
fmt.Fprintln(os.Stderr, v...)
}
func dieon(err error) {
if err != nil {
printerr(err)
os.Exit(1)
}
}
func mustkey() (key []byte) {
n := 0
for _, a := range []string{args.k, args.e, args.f} {
if a != Unset {
n++
}
}
if n != 1 {
dieon(fmt.Errorf("key: not provided"))
}
switch {
case n > 1:
dieon(fmt.Errorf("key: too many keys"))
case args.e != Unset:
return musthex(os.Getenv(args.e))
case args.f != Unset:
fkey, err := ioutil.ReadFile(args.f)
dieon(err)
return musthex(string(fkey))
case args.k != Unset:
return musthex(args.k)
}
dieon(fmt.Errorf("key: not provided"))
return nil
}
func mustiv(bs int) (iv []byte) {
iv = musthex(args.i)
if len(iv) == 0 {
iv = make([]byte, bs)
}
if bs != len(iv) {
dieon(fmt.Errorf("iv: bad length: %d: need blocksize: %d", bs, len(iv)))
}
return iv
}
// checkalgs validates the encryption algorithm argument.
// The standard, operating mode, and block length are validated
// and their parametric values are placed inside args.l, args.m,
// and args.s.
func mustparsealg() {
keys := [...]interface{}{&args.s, &args.m, &args.l}
vals := strings.FieldsFunc(args.a, func(r rune) bool {
return r == '/' || r == '\\'
})
nv := len(vals)
if nv != 3 {
dieon(fmt.Errorf("garbled algorithm: %s", args.a))
}
for i, ptr := range keys {
if ptr == nil || i > nv {
break
}
switch t := ptr.(type) {
case *int:
*t = mustatoi(vals[i])
case *string:
*t = vals[i]
}
}
}
func musthex(a string) (h []byte) {
h = []byte(a)
hexlen, err := hex.Decode(h, h)
dieon(err)
return h[:hexlen]
}
func mustatoi(a string) int {
i, err := strconv.Atoi(a)
dieon(err)
return i
}
func usage() {
fmt.Printf(`
NAME
%[1]s - %[1]ssrypt messages
SYNOPSIS
%[1]s [-a alg] [-r | -i iv ] -e keyvar
%[1]s [ options ] -f keyfile
%[1]s [ options ] -k key
DESCRIPTION
DO NOT USE THIS PROGRAM FOR SERIOUS CRYPTOGRAPHIC WORK
%s encrypts plaintext from stdin and outputs resulting
ciphertext to stdout. It uses the encryption alogrithm alg
and padding algorithm PKCS#7.
An encryption algorithm consists of a encryption standard,
block size, and operating mode.
-a alg Use algorithm alg. The default is aes/cbc/256.
Mode defaults to cbc for block ciphers and default
blocksize for std.
The next options provide the encryption key to %s. The key is
hex encoded. Whitespace is truncated, any other runes are
invalid.
-e var Var names the environment variable holding the key.
-k key Key is the key itself
-f file File names the file containing the key on the first line.
Options
7697
for semantic security. Ignored for ebc mode
and stream ciphers.
-r Random block is prepended to the plaintext and encrypted
-i iv Initialization vector (hex encoded)
ALGORITHMS
Only aes, des, and des3 is enabled at this time.
aes Advanced Encryption Standard
des Data Encryption Standard
des3 Triple DES
otp One-time pad
blowfish Blowfish
cast5 CAST5
salsa Salsa
tea Tea
twofish Twofish
xtea Xtea
BLOCK MODES
Only cbc and ecb is enabled at this time. Using ecb provides
you with zero security. Use at your own risk.
cbc Cipher Block Chaining
ecb Elecrtonic Code Book
cbf Cipher Feedback
crt Counter
ofb Output Feedback
EXAMPLE
Encrypt plaintext m with default aes/cbc/256. Prepend one
random block of noise to the plaintext before encrypting to
randomize the block chain.
# Note how kv is used (kv != $kv)
# The env variable's name (not value)
# is seen on the command line.
echo -n we crash at dawn > m
kv=000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f
bvghhhhhh-r -e kv < m
Encrypt m with aes/cbc/128. Key is set accordingly.
kv=00112233445566778899aabbccddeeff
enc -a aes/cbc/128 -e kv < m
BUGS
No GCM
ECB mode produces ciphertext that reveals patterns in
the underlying plaintext:
enc(B¹, K) → C¹
enc(B², K) → C²
C¹ = C² if B¹ = B²
Enc and dec do not authenticate unless a standard operating
mode supports bundled authentication. Use HMAC and friends
to implement authentication alongsize enc and dec.
SEE ALSO
%[2]s
Gen
Hmac
`, Role, Opposite)
os.Exit(0)
}