File: box_test.go

package info (click to toggle)
golang-go.crypto 1%3A0.0~git20201221.eec23a3-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, bullseye-backports
  • size: 4,804 kB
  • sloc: asm: 8,295; ansic: 258; makefile: 5
file content (181 lines) | stat: -rw-r--r-- 5,301 bytes parent folder | download | duplicates (7)
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// 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 box

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

	"golang.org/x/crypto/curve25519"
)

func TestSealOpen(t *testing.T) {
	publicKey1, privateKey1, _ := GenerateKey(rand.Reader)
	publicKey2, privateKey2, _ := GenerateKey(rand.Reader)

	if *privateKey1 == *privateKey2 {
		t.Fatalf("private keys are equal!")
	}
	if *publicKey1 == *publicKey2 {
		t.Fatalf("public keys are equal!")
	}
	message := []byte("test message")
	var nonce [24]byte

	box := Seal(nil, message, &nonce, publicKey1, privateKey2)
	opened, ok := Open(nil, box, &nonce, publicKey2, privateKey1)
	if !ok {
		t.Fatalf("failed to open box")
	}

	if !bytes.Equal(opened, message) {
		t.Fatalf("got %x, want %x", opened, message)
	}

	for i := range box {
		box[i] ^= 0x40
		_, ok := Open(nil, box, &nonce, publicKey2, privateKey1)
		if ok {
			t.Fatalf("opened box with byte %d corrupted", i)
		}
		box[i] ^= 0x40
	}
}

func TestBox(t *testing.T) {
	var privateKey1, privateKey2 [32]byte
	for i := range privateKey1[:] {
		privateKey1[i] = 1
	}
	for i := range privateKey2[:] {
		privateKey2[i] = 2
	}

	var publicKey1 [32]byte
	curve25519.ScalarBaseMult(&publicKey1, &privateKey1)
	var message [64]byte
	for i := range message[:] {
		message[i] = 3
	}

	var nonce [24]byte
	for i := range nonce[:] {
		nonce[i] = 4
	}

	box := Seal(nil, message[:], &nonce, &publicKey1, &privateKey2)

	// expected was generated using the C implementation of NaCl.
	expected, _ := hex.DecodeString("78ea30b19d2341ebbdba54180f821eec265cf86312549bea8a37652a8bb94f07b78a73ed1708085e6ddd0e943bbdeb8755079a37eb31d86163ce241164a47629c0539f330b4914cd135b3855bc2a2dfc")

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

func TestSealOpenAnonymous(t *testing.T) {
	publicKey, privateKey, _ := GenerateKey(rand.Reader)
	message := []byte("test message")

	box, err := SealAnonymous(nil, message, publicKey, nil)
	if err != nil {
		t.Fatalf("Unexpected error sealing %v", err)
	}
	opened, ok := OpenAnonymous(nil, box, publicKey, privateKey)
	if !ok {
		t.Fatalf("failed to open box")
	}

	if !bytes.Equal(opened, message) {
		t.Fatalf("got %x, want %x", opened, message)
	}

	for i := range box {
		box[i] ^= 0x40
		_, ok := OpenAnonymous(nil, box, publicKey, privateKey)
		if ok {
			t.Fatalf("opened box with byte %d corrupted", i)
		}
		box[i] ^= 0x40
	}

	// allocates new slice if out isn't long enough
	out := []byte("hello")
	orig := append([]byte(nil), out...)
	box, err = SealAnonymous(out, message, publicKey, nil)
	if err != nil {
		t.Fatalf("Unexpected error sealing %v", err)
	}
	if !bytes.Equal(out, orig) {
		t.Fatal("expected out to be unchanged")
	}
	if !bytes.HasPrefix(box, orig) {
		t.Fatal("expected out to be coppied to returned slice")
	}
	_, ok = OpenAnonymous(nil, box[len(out):], publicKey, privateKey)
	if !ok {
		t.Fatalf("failed to open box")
	}

	// uses provided slice if it's long enough
	out = append(make([]byte, 0, 1000), []byte("hello")...)
	orig = append([]byte(nil), out...)
	box, err = SealAnonymous(out, message, publicKey, nil)
	if err != nil {
		t.Fatalf("Unexpected error sealing %v", err)
	}
	if !bytes.Equal(out, orig) {
		t.Fatal("expected out to be unchanged")
	}
	if &out[0] != &box[0] {
		t.Fatal("expected box to point to out")
	}
	_, ok = OpenAnonymous(nil, box[len(out):], publicKey, privateKey)
	if !ok {
		t.Fatalf("failed to open box")
	}
}

func TestSealedBox(t *testing.T) {
	var privateKey [32]byte
	for i := range privateKey[:] {
		privateKey[i] = 1
	}

	var publicKey [32]byte
	curve25519.ScalarBaseMult(&publicKey, &privateKey)
	var message [64]byte
	for i := range message[:] {
		message[i] = 3
	}

	fakeRand := bytes.NewReader([]byte{5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5})
	box, err := SealAnonymous(nil, message[:], &publicKey, fakeRand)
	if err != nil {
		t.Fatalf("Unexpected error sealing %v", err)
	}

	// expected was generated using the C implementation of libsodium with a
	// random implementation that always returns 5.
	// https://gist.github.com/mastahyeti/942ec3f175448d68fed25018adbce5a7
	expected, _ := hex.DecodeString("50a61409b1ddd0325e9b16b700e719e9772c07000b1bd7786e907c653d20495d2af1697137a53b1b1dfc9befc49b6eeb38f86be720e155eb2be61976d2efb34d67ecd44a6ad634625eb9c288bfc883431a84ab0f5557dfe673aa6f74c19f033e648a947358cfcc606397fa1747d5219a")

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

	// box was generated using the C implementation of libsodium.
	// https://gist.github.com/mastahyeti/942ec3f175448d68fed25018adbce5a7
	box, _ = hex.DecodeString("3462e0640728247a6f581e3812850d6edc3dcad1ea5d8184c072f62fb65cb357e27ffa8b76f41656bc66a0882c4d359568410665746d27462a700f01e314f382edd7aae9064879b0f8ba7b88866f88f5e4fbd7649c850541877f9f33ebd25d46d9cbcce09b69a9ba07f0eb1d105d4264")
	result, ok := OpenAnonymous(nil, box, &publicKey, &privateKey)
	if !ok {
		t.Fatalf("failed to open box")
	}
	if !bytes.Equal(result, message[:]) {
		t.Fatalf("message didn't match, got\n%x\n, expected\n%x", result, message[:])
	}
}