-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexamples_test.go
More file actions
212 lines (163 loc) · 6.09 KB
/
examples_test.go
File metadata and controls
212 lines (163 loc) · 6.09 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package thyrse_test
import (
"crypto/ecdh"
"encoding/hex"
"fmt"
"github.com/codahale/thyrse"
)
func Example() {
protocol := thyrse.New("com.example.kat")
protocol.Mix("first", []byte("one"))
protocol.Mix("second", []byte("two"))
third := protocol.Derive("third", nil, 8)
fmt.Printf("Derive('third', 8) = %x\n", third)
plaintext := []byte("this is an example")
ciphertext := protocol.Mask("fourth", nil, plaintext)
fmt.Printf("Mask('fourth', '%s') = %x\n", plaintext, ciphertext)
ciphertext = protocol.Seal("fifth", nil, []byte("this is an example"))
fmt.Printf("Seal('fifth', '%s') = %x\n", plaintext, ciphertext)
protocol.Ratchet("sixth")
sixth := protocol.Derive("seventh", nil, 8)
fmt.Printf("Derive('seventh', 8) = %x\n", sixth)
// Output:
// Derive('third', 8) = e9af288f79a69df3
// Mask('fourth', 'this is an example') = 2d31bdcc6edd9241983aa0823ee147fd9afb
// Seal('fifth', 'this is an example') = eb77f8247978608e5fa7bd862cb769ab4bc00935f3e129d8ce06d6d3aa065e5d2eafa5931adff73e5d2b3185bb95a4cc3e7a
// Derive('seventh', 8) = 33b887899926c00f
}
func ExampleProtocol_mac() {
mac := func(key, message []byte) []byte {
// Initialize a protocol with a domain string.
mac := thyrse.New("com.example.mac")
// Mix the key into the protocol.
mac.Mix("key", key)
// Mix the message into the protocol.
mac.Mix("message", message)
// Derive 16 bytes of output.
// Note: The output length is encoded into the derivation, so changing the length will change the output.
tag := mac.Derive("tag", nil, 16)
return tag
}
key := []byte("my-secret-key")
message := []byte("hello world")
tag := mac(key, message)
fmt.Printf("tag = %x\n", tag)
// Output:
// tag = 6a8aae111bf9cb08238be181ec73fbf9
}
func ExampleProtocol_stream() {
encrypt := func(key, nonce, plaintext []byte) []byte {
// Initialize a protocol with a domain string.
stream := thyrse.New("com.example.stream")
// Mix the key and nonce into the protocol.
stream.Mix("key", key)
stream.Mix("nonce", nonce)
// Encrypt the plaintext without any authenticity.
return stream.Mask("message", nil, plaintext)
}
decrypt := func(key, nonce, ciphertext []byte) []byte {
// Initialize a protocol with a domain string.
stream := thyrse.New("com.example.stream")
// Mix the key and nonce into the protocol.
stream.Mix("key", key)
stream.Mix("nonce", nonce)
// Decrypt the ciphertext.
return stream.Unmask("message", nil, ciphertext)
}
key := []byte("my-secret-key")
nonce := []byte("actually random")
plaintext := []byte("hello world")
ciphertext := encrypt(key, nonce, plaintext)
fmt.Printf("ciphertext = %x\n", ciphertext)
plaintext = decrypt(key, nonce, ciphertext)
fmt.Printf("plaintext = %s\n", plaintext)
// Output:
// ciphertext = 1b71afc466b2fbba28a96e
// plaintext = hello world
}
func ExampleProtocol_aead() {
encrypt := func(key, nonce, ad, plaintext []byte) []byte {
// Initialize a protocol with a domain string.
aead := thyrse.New("com.example.aead")
// Mix the key and nonce into the protocol.
aead.Mix("key", key)
aead.Mix("nonce", nonce)
// Mix the authenticated data into the protocol.
aead.Mix("ad", ad)
// Seal the plaintext.
return aead.Seal("message", nil, plaintext)
}
decrypt := func(key, nonce, ad, ciphertext []byte) ([]byte, error) {
// Initialize a protocol with a domain string.
aead := thyrse.New("com.example.aead")
// Mix the key and nonce into the protocol.
aead.Mix("key", key)
aead.Mix("nonce", nonce)
// Mix the authenticated data into the protocol.
aead.Mix("ad", ad)
// Open the ciphertext.
return aead.Open("message", nil, ciphertext)
}
key := []byte("my-secret-key")
nonce := []byte("actually random")
ad := []byte("some authenticated data")
plaintext := []byte("hello world")
ciphertext := encrypt(key, nonce, ad, plaintext)
fmt.Printf("ciphertext = %x\n", ciphertext)
plaintext, err := decrypt(key, nonce, ad, ciphertext)
if err != nil {
panic(err)
}
fmt.Printf("plaintext = %s\n", plaintext)
// Output:
// ciphertext = 196b81b2a6f611503a1c2efdfc53247ce8a02ef3decd26be324467a3bba7d1aab8cce56c6c5844bff7d921
// plaintext = hello world
}
func ExampleProtocol_hpke() {
encrypt := func(receiver *ecdh.PublicKey, plaintext []byte) []byte {
// This should be randomly generated, but it would make the test always fail.
ephemeralPrivBuf, _ := hex.DecodeString("a0b9a9ea71d45df9a8c7cf7da798c4394342993b21f24c7bb3612e573e8a58df")
ephemeral, _ := ecdh.X25519().NewPrivateKey(ephemeralPrivBuf)
// Initialize a protocol with a domain string.
hpke := thyrse.New("com.example.hpke")
// Mix the receiver's public key and the ephemeral public key into the protocol.
hpke.Mix("receiver", receiver.Bytes())
hpke.Mix("ephemeral", ephemeral.PublicKey().Bytes())
// Mix the ECDH shared secret into the protocol.
ss, err := ephemeral.ECDH(receiver)
if err != nil {
panic(err)
}
hpke.Mix("ecdh", ss)
// Seal the plaintext and append it to the ephemeral public key.
return hpke.Seal("message", ephemeral.PublicKey().Bytes(), plaintext)
}
decrypt := func(receiver *ecdh.PrivateKey, ciphertext []byte) ([]byte, error) {
ephemeral, err := ecdh.X25519().NewPublicKey(ciphertext[:32])
if err != nil {
panic(err)
}
hpke := thyrse.New("com.example.hpke")
hpke.Mix("receiver", receiver.PublicKey().Bytes())
hpke.Mix("ephemeral", ephemeral.Bytes())
ss, err := receiver.ECDH(ephemeral)
if err != nil {
panic(err)
}
hpke.Mix("ecdh", ss)
return hpke.Open("message", nil, ciphertext[32:])
}
receiverPrivBuf, _ := hex.DecodeString("c3a9b89b9a9a15da3c7a7e8ce9c96a828744abf52c0239f4180b0948fa3b1c74")
receiver, _ := ecdh.X25519().NewPrivateKey(receiverPrivBuf)
message := []byte("hello world")
ciphertext := encrypt(receiver.PublicKey(), message)
fmt.Printf("ciphertext = %x\n", ciphertext)
plaintext, err := decrypt(receiver, ciphertext)
if err != nil {
panic(err)
}
fmt.Printf("plaintext = %s\n", plaintext)
// Output:
// ciphertext = 672e904ba78b50b56f896d4b9c2f8018aecfd34038523a6faa4e82e37be4281f9027ff4a302ec34e467273773f52597ffc7384becf557a3fe32466c85a3762a6296199d6e5bf71cfd5c575
// plaintext = hello world
}