xref: /aosp_15_r20/external/tink/go/hybrid/internal/hpke/chacha20poly1305_aead_test.go (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1// Copyright 2022 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
17package hpke
18
19import (
20	"bytes"
21	"fmt"
22	"testing"
23)
24
25func TestChaCha20Poly1305AEADSealOpen(t *testing.T) {
26	i := 0
27	vecs := aeadRFCVectors(t)
28	for k, v := range vecs {
29		if k.aeadID != chaCha20Poly1305 {
30			continue
31		}
32
33		i++
34		t.Run(fmt.Sprintf("%d", k.id), func(t *testing.T) {
35			aead, err := newAEAD(k.aeadID)
36			if err != nil {
37				t.Fatalf("newAEAD(%d) err = %v, want nil", k.aeadID, err)
38			}
39
40			ciphertext, err := aead.seal(v.key, v.nonce, v.plaintext, v.associatedData)
41			if err != nil {
42				t.Fatalf("seal err = %v, want nil", err)
43			}
44			if !bytes.Equal(ciphertext, v.ciphertext) {
45				t.Errorf("seal = %x, want %x", ciphertext, v.ciphertext)
46			}
47
48			plaintext, err := aead.open(v.key, v.nonce, v.ciphertext, v.associatedData)
49			if err != nil {
50				t.Fatalf("open err = %v, want nil", err)
51			}
52			if !bytes.Equal(plaintext, v.plaintext) {
53				t.Errorf("open = %x, want %x", plaintext, v.plaintext)
54			}
55		})
56	}
57	if i < 2 {
58		t.Errorf("number of vectors tested = %d, want > %d", i, 2)
59	}
60}
61