-
Notifications
You must be signed in to change notification settings - Fork 99
/
encryption_test.go
144 lines (100 loc) · 4.63 KB
/
encryption_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
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
package lksdk
import (
"crypto/aes"
"testing"
"github.com/stretchr/testify/require"
)
var (
opusEncryptedFrame = []byte{120, 145, 24, 159, 76, 65, 130, 48, 144, 249, 17, 112, 134, 78, 250, 129, 171, 194, 16, 173, 73, 196, 5, 152, 69, 225, 28, 210, 196, 241, 226, 139, 231, 172, 51, 38, 139, 179, 245, 182, 170, 8, 122, 117, 98, 144, 123, 95, 73, 89, 119, 39, 205, 20, 191, 55, 121, 59, 239, 192, 85, 224, 228, 143, 10, 113, 195, 223, 118, 42, 2, 32, 22, 17, 77, 227, 109, 160, 245, 202, 189, 63, 162, 164, 5, 241, 24, 151, 45, 42, 165, 131, 171, 243, 141, 53, 35, 131, 141, 52, 253, 188, 12, 0}
opusDecryptedFrame = []byte{120, 11, 109, 82, 113, 132, 189, 156, 220, 173, 30, 109, 87, 54, 173, 99, 26, 126, 166, 37, 127, 234, 110, 211, 230, 152, 181, 235, 197, 19, 140, 230, 179, 35, 131, 132, 29, 192, 97, 247, 108, 53, 183, 214, 77, 181, 173, 206, 175, 7, 228, 145, 93, 155, 155, 142, 14, 27, 111, 64, 96, 196, 229, 189, 142, 59, 149, 169, 99, 225, 216, 85, 186, 182}
opusSilenceFrame = []byte{
0xf8, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
}
sifTrailer = []byte{50, 86, 10, 220, 108, 185, 57, 211}
testPassphrase = "12345"
keyIncorrectLength = []byte{1, 2, 3}
)
func TestDeriveKeyFromString(t *testing.T) {
password := "12345"
key, err := DeriveKeyFromString(password)
expectedKey := []byte{15, 94, 198, 66, 93, 211, 116, 46, 55, 97, 232, 121, 189, 233, 224, 22}
require.Nil(t, err)
require.Equal(t, key, expectedKey)
}
func TestDeriveKeyFromBytes(t *testing.T) {
inputSecret := []byte{34, 21, 187, 202, 134, 204, 168, 62, 5, 105, 40, 244, 88}
expectedKey := []byte{129, 224, 93, 62, 17, 203, 99, 136, 101, 35, 149, 128, 189, 152, 251, 76}
key, err := DeriveKeyFromBytes(inputSecret)
require.Nil(t, err)
require.Equal(t, expectedKey, key)
}
func TestDecryptAudioSample(t *testing.T) {
key, err := DeriveKeyFromString(testPassphrase)
require.Nil(t, err)
_, err = DecryptGCMAudioSample(opusEncryptedFrame, keyIncorrectLength, sifTrailer)
require.ErrorIs(t, err, ErrIncorrectKeyLength)
decryptedFrame, err := DecryptGCMAudioSample(nil, key, sifTrailer)
require.Nil(t, err)
require.Nil(t, decryptedFrame)
decryptedFrame, err = DecryptGCMAudioSample(opusEncryptedFrame, key, sifTrailer)
require.Nil(t, err)
require.Equal(t, opusDecryptedFrame, decryptedFrame)
var sifFrame []byte
sifFrame = append(sifFrame, opusSilenceFrame...)
sifFrame = append(sifFrame, sifTrailer...)
decryptedFrame, err = DecryptGCMAudioSample(sifFrame, key, sifTrailer)
require.Nil(t, err)
require.Nil(t, decryptedFrame)
}
func TestEncryptAudioSample(t *testing.T) {
key, err := DeriveKeyFromString(testPassphrase)
require.Nil(t, err)
_, err = EncryptGCMAudioSample(opusDecryptedFrame, keyIncorrectLength, 0)
require.ErrorIs(t, err, ErrIncorrectKeyLength)
encryptedFrame, err := EncryptGCMAudioSample(nil, key, 0)
require.Nil(t, err)
require.Nil(t, encryptedFrame)
encryptedFrame, err = EncryptGCMAudioSample(opusDecryptedFrame, key, 0)
require.Nil(t, err)
// IV is generated randomly so to verify we decrypt and make sure that we got the expected plain text frame
decryptedFrame, err := DecryptGCMAudioSample(encryptedFrame, key, sifTrailer)
require.Nil(t, err)
require.Equal(t, opusDecryptedFrame, decryptedFrame)
}
func BenchmarkDecryptAudioNewCipher(b *testing.B) {
key, _ := DeriveKeyFromString(testPassphrase)
for i := 0; i < b.N; i++ {
cipherBlock, _ := aes.NewCipher(key)
DecryptGCMAudioSampleCustomCipher(opusEncryptedFrame, sifTrailer, cipherBlock)
}
}
func BenchmarkDecryptAudioCachedCipher(b *testing.B) {
key, _ := DeriveKeyFromString(testPassphrase)
cipherBlock, _ := aes.NewCipher(key)
for i := 0; i < b.N; i++ {
DecryptGCMAudioSampleCustomCipher(opusEncryptedFrame, sifTrailer, cipherBlock)
}
}
func BenchmarkEncryptAudioCachedCipher(b *testing.B) {
key, _ := DeriveKeyFromString(testPassphrase)
cipherBlock, _ := aes.NewCipher(key)
for i := 0; i < b.N; i++ {
EncryptGCMAudioSampleCustomCipher(opusDecryptedFrame, 0, cipherBlock)
}
}
func BenchmarkEncryptAudioNewCipher(b *testing.B) {
key, _ := DeriveKeyFromString(testPassphrase)
for i := 0; i < b.N; i++ {
cipherBlock, _ := aes.NewCipher(key)
EncryptGCMAudioSampleCustomCipher(opusDecryptedFrame, 0, cipherBlock)
}
}