-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiv_test.go
77 lines (70 loc) · 2.55 KB
/
iv_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
package rubberhose_test
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/binary"
"fmt"
"strings"
"testing"
rubberhose "github.com/Cookie04DE/RubberHose"
"github.com/stretchr/testify/require"
)
type ivTestCase struct {
bytes []byte
number int64
expectedResult []byte
}
var ivTestCases = []ivTestCase{
{bytes: []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, number: 10, expectedResult: []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10}},
{bytes: []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, number: 10, expectedResult: []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11}},
{bytes: []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0}, number: 10, expectedResult: []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 10}},
{bytes: []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255}, number: 1, expectedResult: []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0}},
{bytes: []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255}, number: 2, expectedResult: []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1}},
{bytes: []byte{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, number: 1, expectedResult: []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
}
func TestIncrementIV(t *testing.T) {
for i, testCase := range ivTestCases {
rubberhose.IncrementIV(testCase.bytes, testCase.number)
require.Equal(t, testCase.expectedResult, testCase.bytes, fmt.Sprintf("Test %d", i+1))
}
}
func TestIncrementedIV(t *testing.T) {
key := make([]byte, 32)
_, err := rand.Read(key)
require.NoError(t, err)
iv := make([]byte, 16)
_, err = rand.Read(iv)
require.NoError(t, err)
b, err := aes.NewCipher(key)
require.NoError(t, err)
ctr := cipher.NewCTR(b, iv)
cipherText := make([]byte, 32)
repeatedAs := strings.Repeat("a", 16)
repeatedBs := strings.Repeat("b", 16)
ctr.XORKeyStream(cipherText, []byte(repeatedAs+repeatedBs))
ctr = cipher.NewCTR(b, iv)
plainA := make([]byte, 16)
ctr.XORKeyStream(plainA, cipherText[:16])
require.Equal(t, repeatedAs, string(plainA))
rubberhose.IncrementIV(iv, 1)
ctr = cipher.NewCTR(b, iv)
plainB := make([]byte, 16)
ctr.XORKeyStream(plainB, cipherText[16:])
require.Equal(t, repeatedBs, string(plainB))
}
func BenchmarkIncrementIV(b *testing.B) {
iv := make([]byte, 16)
_, err := rand.Read(iv)
if err != nil {
b.Fatal(err)
}
numberBytes := make([]byte, 8)
_, err = rand.Read(iv)
if err != nil {
b.Fatal(err)
}
for n := 0; n < b.N; n++ {
rubberhose.IncrementIV(iv, int64(binary.LittleEndian.Uint64(numberBytes)))
}
}