-
Notifications
You must be signed in to change notification settings - Fork 0
/
ivattack.go
88 lines (75 loc) · 1.69 KB
/
ivattack.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
package set4
import (
"errors"
"github.com/dbalan/cryptopals/common"
"github.com/dbalan/cryptopals/set2"
"strings"
)
var (
HighAsciiErr = errors.New("high ascii errors")
)
func keyAsIVOracle(in string, key []byte) ([]byte, error) {
in = strings.Replace(in, ";", "%3B", -1)
in = strings.Replace(in, "=", "%3D", -1)
pt := []byte("comment1=cooking%20MCs;userdata=" + in + ";comment2=%20like%20a%20pound%20of%20bacon")
lpt := len(pt)
if (lpt % 16) != 0 {
newlen := lpt + 16 - (lpt % 16)
padded, err := set2.PKCS7Padding(pt, newlen)
if err != nil {
return nil, err
}
pt = padded
}
enc, err := set2.EncAES128CBC(pt, key, key)
if err != nil {
return nil, err
}
return enc, nil
}
func modifyCT(ct []byte) []byte {
blocks := common.Blocks(ct, 16)
if len(blocks) < 3 {
panic("not enough!")
}
newct := blocks[0]
newct = append(newct, common.Repeat(16, byte(0))...)
newct = append(newct, blocks[0]...)
return newct
}
func userDecrypt(ct []byte, key []byte) ([]byte, error) {
dec, _ := set2.DecAES128CBC(ct, key, key)
for _, d := range dec {
if int(d) > 0x7f {
return dec, HighAsciiErr
}
}
return nil, nil
}
func AttackKeyAsIV() error {
key, err := common.RandBytes(16)
if err != nil {
return err
}
ct, err := keyAsIVOracle("something", key)
if err != nil {
return err
}
// attacker
newct := modifyCT(ct)
// user
dec, err := userDecrypt(newct, key)
if err == nil || err != HighAsciiErr {
return errors.New("we havve a problem")
}
// attacker again
blocks := common.Blocks(dec, 16)
recoverd, err := common.XORBlk(blocks[0], blocks[2])
if err != nil {
return err
}
if !common.EqualBlocks(recoverd, key) {
return errors.New("wrong key")
}
return nil
}