Skip to content

Commit cef76e6

Browse files
committed
cmd/setec: stop depending on tink-go's testutil
Updates tink-crypto/tink-go#31 Signed-off-by: Brad Fitzpatrick <brad@danga.com>
1 parent 743dfa5 commit cef76e6

File tree

2 files changed

+74
-2
lines changed

2 files changed

+74
-2
lines changed

cmd/setec/dummy-tink.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Copyright 2018 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
////////////////////////////////////////////////////////////////////////////////
16+
17+
// This file is a copy of tink-go's https://github.com/tink-crypto/tink-go/blob/v2.1.0/testutil/testutil.go#L90-L130
18+
// with its type name unexported.
19+
//
20+
// We do this to avoid https://github.com/tink-crypto/tink-go/issues/31 happening
21+
// in tink-go's testutil init function, which breaks our assumptions in CI
22+
// that things don't hit the network.
23+
24+
package main
25+
26+
import (
27+
"bytes"
28+
"encoding/gob"
29+
"errors"
30+
"fmt"
31+
)
32+
33+
// dummyAEAD is a dummy implementation of AEAD interface. It "encrypts" data
34+
// with a simple serialization capturing the dummy name, plaintext, and
35+
// associated data, and "decrypts" it by reversing this and checking that the
36+
// name and associated data match.
37+
type dummyAEAD struct {
38+
Name string
39+
}
40+
41+
type dummyAEADData struct {
42+
Name string
43+
Plaintext []byte
44+
AssociatedData []byte
45+
}
46+
47+
// Encrypt encrypts the plaintext.
48+
func (a *dummyAEAD) Encrypt(plaintext []byte, associatedData []byte) ([]byte, error) {
49+
buf := new(bytes.Buffer)
50+
encoder := gob.NewEncoder(buf)
51+
err := encoder.Encode(dummyAEADData{
52+
Name: a.Name,
53+
Plaintext: plaintext,
54+
AssociatedData: associatedData,
55+
})
56+
if err != nil {
57+
return nil, fmt.Errorf("dummy aead encrypt: %v", err)
58+
}
59+
return buf.Bytes(), nil
60+
}
61+
62+
// Decrypt decrypts the ciphertext.
63+
func (a *dummyAEAD) Decrypt(ciphertext []byte, associatedData []byte) ([]byte, error) {
64+
data := dummyAEADData{}
65+
decoder := gob.NewDecoder(bytes.NewBuffer(ciphertext))
66+
if err := decoder.Decode(&data); err != nil {
67+
return nil, fmt.Errorf("dummy aead decrypt: invalid data: %v", err)
68+
}
69+
if data.Name != a.Name || !bytes.Equal(data.AssociatedData, associatedData) {
70+
return nil, errors.New("dummy aead encrypt: name/associated data mismatch")
71+
}
72+
return data.Plaintext, nil
73+
}

cmd/setec/setec.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import (
3232
"github.com/tailscale/setec/server"
3333
"github.com/tailscale/setec/types/api"
3434
"github.com/tink-crypto/tink-go-awskms/v2/integration/awskms"
35-
"github.com/tink-crypto/tink-go/v2/testutil"
3635
"github.com/tink-crypto/tink-go/v2/tink"
3736
"golang.org/x/term"
3837
"tailscale.com/tsnet"
@@ -189,7 +188,7 @@ func runServer(env *command.Env) error {
189188
serverArgs.Hostname = "setec-dev"
190189
}
191190
if serverArgs.KMSKeyName == "" {
192-
kek = &testutil.DummyAEAD{
191+
kek = &dummyAEAD{
193192
Name: "SetecDevOnlyDummyEncryption",
194193
}
195194
}

0 commit comments

Comments
 (0)