aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/golang.org/x/crypto/nacl/secretbox/secretbox_test.go
blob: 3c70b0f4b3d230118c5778cae349544e9ff0e334 (plain)
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
// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package secretbox

import (
	"bytes"
	"crypto/rand"
	"encoding/hex"
	"testing"
)

func TestSealOpen(t *testing.T) {
	var key [32]byte
	var nonce [24]byte

	rand.Reader.Read(key[:])
	rand.Reader.Read(nonce[:])

	var box, opened []byte

	for msgLen := 0; msgLen < 128; msgLen += 17 {
		message := make([]byte, msgLen)
		rand.Reader.Read(message)

		box = Seal(box[:0], message, &nonce, &key)
		var ok bool
		opened, ok = Open(opened[:0], box, &nonce, &key)
		if !ok {
			t.Errorf("%d: failed to open box", msgLen)
			continue
		}

		if !bytes.Equal(opened, message) {
			t.Errorf("%d: got %x, expected %x", msgLen, opened, message)
			continue
		}
	}

	for i := range box {
		box[i] ^= 0x20
		_, ok := Open(opened[:0], box, &nonce, &key)
		if ok {
			t.Errorf("box was opened after corrupting byte %d", i)
		}
		box[i] ^= 0x20
	}
}

func TestSecretBox(t *testing.T) {
	var key [32]byte
	var nonce [24]byte
	var message [64]byte

	for i := range key[:] {
		key[i] = 1
	}
	for i := range nonce[:] {
		nonce[i] = 2
	}
	for i := range message[:] {
		message[i] = 3
	}

	box := Seal(nil, message[:], &nonce, &key)
	// expected was generated using the C implementation of NaCl.
	expected, _ := hex.DecodeString("8442bc313f4626f1359e3b50122b6ce6fe66ddfe7d39d14e637eb4fd5b45beadab55198df6ab5368439792a23c87db70acb6156dc5ef957ac04f6276cf6093b84be77ff0849cc33e34b7254d5a8f65ad")

	if !bytes.Equal(box, expected) {
		t.Fatalf("box didn't match, got\n%x\n, expected\n%x", box, expected)
	}
}

func TestAppend(t *testing.T) {
	var key [32]byte
	var nonce [24]byte
	var message [8]byte

	out := make([]byte, 4)
	box := Seal(out, message[:], &nonce, &key)
	if !bytes.Equal(box[:4], out[:4]) {
		t.Fatalf("Seal didn't correctly append")
	}

	out = make([]byte, 4, 100)
	box = Seal(out, message[:], &nonce, &key)
	if !bytes.Equal(box[:4], out[:4]) {
		t.Fatalf("Seal didn't correctly append with sufficient capacity.")
	}
}

func benchmarkSealSize(b *testing.B, size int) {
	message := make([]byte, size)
	out := make([]byte, size+Overhead)
	var nonce [24]byte
	var key [32]byte

	b.SetBytes(int64(size))
	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		out = Seal(out[:0], message, &nonce, &key)
	}
}

func BenchmarkSeal8Bytes(b *testing.B) {
	benchmarkSealSize(b, 8)
}

func BenchmarkSeal100Bytes(b *testing.B) {
	benchmarkSealSize(b, 100)
}

func BenchmarkSeal1K(b *testing.B) {
	benchmarkSealSize(b, 1024)
}

func BenchmarkSeal8K(b *testing.B) {
	benchmarkSealSize(b, 8192)
}

func benchmarkOpenSize(b *testing.B, size int) {
	msg := make([]byte, size)
	result := make([]byte, size)
	var nonce [24]byte
	var key [32]byte
	box := Seal(nil, msg, &nonce, &key)

	b.SetBytes(int64(size))
	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		if _, ok := Open(result[:0], box, &nonce, &key); !ok {
			panic("Open failed")
		}
	}
}

func BenchmarkOpen8Bytes(b *testing.B) {
	benchmarkOpenSize(b, 8)
}

func BenchmarkOpen100Bytes(b *testing.B) {
	benchmarkOpenSize(b, 100)
}

func BenchmarkOpen1K(b *testing.B) {
	benchmarkOpenSize(b, 1024)
}

func BenchmarkOpen8K(b *testing.B) {
	benchmarkOpenSize(b, 8192)
}