-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathencrypted_response_test.go
72 lines (56 loc) · 1.88 KB
/
encrypted_response_test.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
package dnscrypt
import (
"bytes"
"math/rand"
"testing"
"github.com/ameshkov/dnscrypt/v2/xsecretbox"
"github.com/stretchr/testify/require"
)
func TestDNSCryptResponseEncryptDecryptXSalsa20Poly1305(t *testing.T) {
testDNSCryptResponseEncryptDecrypt(t, XSalsa20Poly1305)
}
func TestDNSCryptResponseEncryptDecryptXChacha20Poly1305(t *testing.T) {
testDNSCryptResponseEncryptDecrypt(t, XChacha20Poly1305)
}
func testDNSCryptResponseEncryptDecrypt(t *testing.T, esVersion CryptoConstruction) {
// Generate the secret/public pairs
clientSecretKey, clientPublicKey := generateRandomKeyPair()
serverSecretKey, serverPublicKey := generateRandomKeyPair()
// Generate client shared key
clientSharedKey, err := computeSharedKey(esVersion, &clientSecretKey, &serverPublicKey)
require.NoError(t, err)
// Generate server shared key
serverSharedKey, err := computeSharedKey(esVersion, &serverSecretKey, &clientPublicKey)
require.NoError(t, err)
r1 := &EncryptedResponse{
EsVersion: esVersion,
}
// Fill client-nonce
_, _ = rand.Read(r1.Nonce[:nonceSize/12])
// Generate random packet
packet := make([]byte, 100)
_, _ = rand.Read(packet[:])
// Encrypt it
encrypted, err := r1.Encrypt(packet, serverSharedKey)
require.NoError(t, err)
// Now let's try decrypting it
r2 := &EncryptedResponse{
EsVersion: esVersion,
}
// Decrypt it
decrypted, err := r2.Decrypt(encrypted, clientSharedKey)
require.NoError(t, err)
// Check that packet is the same
require.True(t, bytes.Equal(packet, decrypted))
// Now check invalid data (some random stuff)
_, err = r2.Decrypt(packet, clientSharedKey)
require.NotNil(t, err)
// Empty array
_, err = r2.Decrypt([]byte{}, clientSharedKey)
require.NotNil(t, err)
// Minimum valid size
b := make([]byte, len(resolverMagic)+nonceSize+xsecretbox.TagSize+minDNSPacketSize)
_, _ = rand.Read(b)
_, err = r2.Decrypt(b, clientSharedKey)
require.NotNil(t, err)
}