File: encrypted_test.go

package info (click to toggle)
golang-github-theupdateframework-go-tuf 2.0.2%2B0.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,908 kB
  • sloc: python: 164; makefile: 89; sh: 37
file content (158 lines) | stat: -rw-r--r-- 5,006 bytes parent folder | download
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
package encrypted

import (
	"encoding/json"
	"strings"
	"testing"

	. "gopkg.in/check.v1"
)

var (
	kdfVectors = map[KDFParameterStrength][]byte{
		Legacy:   []byte(`{"kdf":{"name":"scrypt","params":{"N":32768,"r":8,"p":1},"salt":"WO3mVvyTwJ9vwT5/Tk5OW5WPIBUofMjcpEfrLnfY4uA="},"cipher":{"name":"nacl/secretbox","nonce":"tCy7HcTFr4uxv4Nrg/DWmncuZ148U1MX"},"ciphertext":"08n43p5G5yviPEZpO7tPPF4aZQkWiWjkv4taFdhDBA0tamKH4nw="}`),
		Standard: []byte(`{"kdf":{"name":"scrypt","params":{"N":65536,"r":8,"p":1},"salt":"FhzPOt9/bJG4PTq6lQ6ecG6GzaOuOy/ynG5+yRiFlNs="},"cipher":{"name":"nacl/secretbox","nonce":"aw1ng1jHaDz/tQ7V2gR9O2+IGQ8xJEuE"},"ciphertext":"HycvuLZL4sYH0BrYTh4E/H20VtAW6u5zL5Pr+IBjYLYnCPzDkq8="}`),
		OWASP:    []byte(`{"kdf":{"name":"scrypt","params":{"N":131072,"r":8,"p":1},"salt":"m38E3kouJTtiheLQN22NQ8DTito5hrjpUIskqcd375k="},"cipher":{"name":"nacl/secretbox","nonce":"Y6PM13yA+o44pE/W1ZBwczeGnTV/m9Zc"},"ciphertext":"6H8sqj1K6B6yDjtH5AQ6lbFigg/C2yDDJc4rYJ79w9aVPImFIPI="}`),
	}
)

// Hook up gocheck into the "go test" runner.
func Test(t *testing.T) { TestingT(t) }

type EncryptedSuite struct{}

var _ = Suite(&EncryptedSuite{})

var plaintext = []byte("reallyimportant")

func (EncryptedSuite) TestRoundtrip(c *C) {
	passphrase := []byte("supersecret")

	enc, err := Encrypt(plaintext, passphrase)
	c.Assert(err, IsNil)

	// successful decrypt
	dec, err := Decrypt(enc, passphrase)
	c.Assert(err, IsNil)
	c.Assert(dec, DeepEquals, plaintext)

	// wrong passphrase
	passphrase[0] = 0
	dec, err = Decrypt(enc, passphrase)
	c.Assert(err, NotNil)
	c.Assert(dec, IsNil)
}

func (EncryptedSuite) TestTamperedRoundtrip(c *C) {
	passphrase := []byte("supersecret")

	enc, err := Encrypt(plaintext, passphrase)
	c.Assert(err, IsNil)

	data := &data{}
	err = json.Unmarshal(enc, data)
	c.Assert(err, IsNil)

	data.Ciphertext[0] = ^data.Ciphertext[0]

	enc, _ = json.Marshal(data)

	dec, err := Decrypt(enc, passphrase)
	c.Assert(err, NotNil)
	c.Assert(dec, IsNil)
}

func (EncryptedSuite) TestDecrypt(c *C) {
	enc := []byte(`{"kdf":{"name":"scrypt","params":{"N":32768,"r":8,"p":1},"salt":"N9a7x5JFGbrtB2uBR81jPwp0eiLR4A7FV3mjVAQrg1g="},"cipher":{"name":"nacl/secretbox","nonce":"2h8HxMmgRfuYdpswZBQaU3xJ1nkA/5Ik"},"ciphertext":"SEW6sUh0jf2wfdjJGPNS9+bkk2uB+Cxamf32zR8XkQ=="}`)
	passphrase := []byte("supersecret")

	dec, err := Decrypt(enc, passphrase)
	c.Assert(err, IsNil)
	c.Assert(dec, DeepEquals, plaintext)
}

func (EncryptedSuite) TestMarshalUnmarshal(c *C) {
	passphrase := []byte("supersecret")

	wrapped, err := Marshal(plaintext, passphrase)
	c.Assert(err, IsNil)
	c.Assert(wrapped, NotNil)

	var protected []byte
	err = Unmarshal(wrapped, &protected, passphrase)
	c.Assert(err, IsNil)
	c.Assert(protected, DeepEquals, plaintext)
}

func (EncryptedSuite) TestInvalidKDFSettings(c *C) {
	passphrase := []byte("supersecret")

	wrapped, err := MarshalWithCustomKDFParameters(plaintext, passphrase, 0)
	c.Assert(err, IsNil)
	c.Assert(wrapped, NotNil)

	var protected []byte
	err = Unmarshal(wrapped, &protected, passphrase)
	c.Assert(err, IsNil)
	c.Assert(protected, DeepEquals, plaintext)
}

func (EncryptedSuite) TestLegacyKDFSettings(c *C) {
	passphrase := []byte("supersecret")

	wrapped, err := MarshalWithCustomKDFParameters(plaintext, passphrase, Legacy)
	c.Assert(err, IsNil)
	c.Assert(wrapped, NotNil)

	var protected []byte
	err = Unmarshal(wrapped, &protected, passphrase)
	c.Assert(err, IsNil)
	c.Assert(protected, DeepEquals, plaintext)
}

func (EncryptedSuite) TestStandardKDFSettings(c *C) {
	passphrase := []byte("supersecret")

	wrapped, err := MarshalWithCustomKDFParameters(plaintext, passphrase, Standard)
	c.Assert(err, IsNil)
	c.Assert(wrapped, NotNil)

	var protected []byte
	err = Unmarshal(wrapped, &protected, passphrase)
	c.Assert(err, IsNil)
	c.Assert(protected, DeepEquals, plaintext)
}

func (EncryptedSuite) TestOWASPKDFSettings(c *C) {
	passphrase := []byte("supersecret")

	wrapped, err := MarshalWithCustomKDFParameters(plaintext, passphrase, OWASP)
	c.Assert(err, IsNil)
	c.Assert(wrapped, NotNil)

	var protected []byte
	err = Unmarshal(wrapped, &protected, passphrase)
	c.Assert(err, IsNil)
	c.Assert(protected, DeepEquals, plaintext)
}

func (EncryptedSuite) TestKDFSettingVectors(c *C) {
	passphrase := []byte("supersecret")

	for _, v := range kdfVectors {
		var protected []byte
		err := Unmarshal(v, &protected, passphrase)
		c.Assert(err, IsNil)
		c.Assert(protected, DeepEquals, plaintext)
	}
}

func (EncryptedSuite) TestUnsupportedKDFParameters(c *C) {
	enc := []byte(`{"kdf":{"name":"scrypt","params":{"N":99,"r":99,"p":99},"salt":"cZFcQJdwPhPyhU1R4qkl0qVOIjZd4V/7LYYAavq166k="},"cipher":{"name":"nacl/secretbox","nonce":"7vhRS7j0hEPBWV05skAdgLj81AkGeE7U"},"ciphertext":"6WYU/YSXVbYzl/NzaeAzmjLyfFhOOjLc0d8/GFV0aBFdJvyCcXc="}`)
	passphrase := []byte("supersecret")

	dec, err := Decrypt(enc, passphrase)
	c.Assert(err, NotNil)
	c.Assert(dec, IsNil)
	c.Assert(strings.Contains(err.Error(), "unsupported scrypt parameters"), Equals, true)
}