|
| 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 | +} |
0 commit comments