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
|
package main
import (
"crypto/rand"
"encoding/hex"
"io"
"net/http"
"net/http/httptest"
"testing"
"testing/quick"
"git.tjp.lol/authentic_kate"
"github.com/jamesruan/sodium"
)
type ByteSerDes struct{}
func (ByteSerDes) Serialize(w io.Writer, data []byte) error {
_, err := w.Write(data)
return err
}
func (ByteSerDes) Deserialize(r io.Reader, data *[]byte) error {
buf, err := io.ReadAll(r)
if err != nil {
return err
}
*data = buf
return nil
}
func kateSeal(key [32]byte, plaintext []byte) []byte {
keyHex := hex.EncodeToString(key[:])
auth := kate.New(keyHex, kate.AuthConfig[[]byte]{
SerDes: ByteSerDes{},
CookieName: "test",
})
w := httptest.NewRecorder()
if err := auth.Set(w, plaintext); err != nil {
panic(err)
}
cookies := w.Result().Cookies()
if len(cookies) == 0 {
panic("No cookie set")
}
encryptedBytes, err := hex.DecodeString(cookies[0].Value)
if err != nil {
panic(err)
}
return encryptedBytes
}
func libsodiumSeal(key [32]byte, plaintext []byte) []byte {
var nonce [24]byte
if _, err := rand.Read(nonce[:]); err != nil {
panic(err)
}
ciphertextAndMac := sodium.Bytes(plaintext).SecretBox(
sodium.SecretBoxNonce{Bytes: nonce[:]},
sodium.SecretBoxKey{Bytes: key[:]},
)
result := make([]byte, 24+len(ciphertextAndMac))
copy(result[:24], nonce[:])
copy(result[24:], ciphertextAndMac)
return result
}
func kateOpen(key [32]byte, box []byte) ([]byte, bool) {
keyHex := hex.EncodeToString(key[:])
auth := kate.New(keyHex, kate.AuthConfig[[]byte]{
SerDes: ByteSerDes{},
CookieName: "test",
})
cookieValue := hex.EncodeToString(box)
req := httptest.NewRequest("GET", "/", nil)
req.AddCookie(&http.Cookie{Name: "test", Value: cookieValue})
var decryptedData []byte
var success bool
handler := auth.Optional(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
data, ok := auth.Get(r.Context())
if ok {
decryptedData = data
success = true
}
}))
testW := httptest.NewRecorder()
handler.ServeHTTP(testW, req)
return decryptedData, success
}
func libsodiumOpen(key [32]byte, box []byte) ([]byte, bool) {
if len(box) < 24 {
return nil, false
}
nonce := box[:24]
ciphertextAndMac := box[24:]
decrypted, err := sodium.Bytes(ciphertextAndMac).SecretBoxOpen(
sodium.SecretBoxNonce{Bytes: nonce},
sodium.SecretBoxKey{Bytes: key[:]},
)
if err != nil {
return nil, false
}
return decrypted, true
}
func TestCrossLibraryCompatibility(t *testing.T) {
t.Run("KateEncrypt_LibsodiumDecrypt", func(t *testing.T) {
f := func(data []byte) bool {
if len(data) == 0 {
return true
}
var key [32]byte
if _, err := rand.Read(key[:]); err != nil {
return false
}
kateBox := kateSeal(key, data)
decrypted, ok := libsodiumOpen(key, kateBox)
return ok && string(decrypted) == string(data)
}
if err := quick.Check(f, nil); err != nil {
t.Errorf("Property test failed: %v", err)
}
})
t.Run("LibsodiumEncrypt_KateDecrypt", func(t *testing.T) {
f := func(data []byte) bool {
if len(data) == 0 {
return true
}
var key [32]byte
if _, err := rand.Read(key[:]); err != nil {
return false
}
libsodiumBox := libsodiumSeal(key, data)
decrypted, ok := kateOpen(key, libsodiumBox)
return ok && string(decrypted) == string(data)
}
if err := quick.Check(f, nil); err != nil {
t.Errorf("Property test failed: %v", err)
}
})
}
|