xref: /aosp_15_r20/external/tink/go/hybrid/ecies_aead_hkdf_hybrid_encrypt_test.go (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1// Copyright 2019 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 hybrid
18
19import (
20	"bytes"
21	"testing"
22
23	"github.com/google/tink/go/aead"
24	"github.com/google/tink/go/daead"
25	"github.com/google/tink/go/hybrid/subtle"
26	"github.com/google/tink/go/subtle/random"
27	tinkpb "github.com/google/tink/go/proto/tink_go_proto"
28)
29
30func basicMultipleEncrypts(t *testing.T, c string, k *tinkpb.KeyTemplate) {
31	t.Helper()
32	curve, err := subtle.GetCurve(c)
33	if err != nil {
34		t.Fatalf("error getting %s curve: %s ", c, err)
35	}
36	pvt, err := subtle.GenerateECDHKeyPair(curve)
37	if err != nil {
38		t.Fatalf("error generating ECDH key pair: %s", err)
39	}
40	salt := []byte("some salt")
41	pt := random.GetRandomBytes(20)
42	context := []byte("context info")
43	rDem, err := newRegisterECIESAEADHKDFDemHelper(k)
44	if err != nil {
45		t.Fatalf("error generating a DEM helper :%s", err)
46	}
47	e, err := subtle.NewECIESAEADHKDFHybridEncrypt(&pvt.PublicKey, salt, "SHA256", "UNCOMPRESSED", rDem)
48	if err != nil {
49		t.Fatalf("error generating an encryption construct :%s", err)
50	}
51	d, err := subtle.NewECIESAEADHKDFHybridDecrypt(pvt, salt, "SHA256", "UNCOMPRESSED", rDem)
52	if err != nil {
53		t.Fatalf("error generating an decryption construct :%s", err)
54	}
55	cl := [][]byte{}
56	for i := 0; i < 8; i++ {
57		ct, err := e.Encrypt(pt, context)
58		if err != nil {
59			t.Fatalf("encryption error :%s", err)
60		}
61		for _, c := range cl {
62			if bytes.Equal(ct, c) {
63				t.Fatalf("encryption is not randomized")
64			}
65		}
66		cl = append(cl, ct)
67		dt, err := d.Decrypt(ct, context)
68		if err != nil {
69			t.Fatalf("decryption error :%s", err)
70		}
71		if !bytes.Equal(dt, pt) {
72			t.Fatalf("decryption not inverse of encryption")
73		}
74	}
75	if len(cl) != 8 {
76		t.Errorf("randomized encryption check failed")
77	}
78}
79
80func TestECAESCTRHMACSHA256Encrypt(t *testing.T) {
81	basicMultipleEncrypts(t, "NIST_P256", aead.AES256CTRHMACSHA256KeyTemplate())
82	basicMultipleEncrypts(t, "NIST_P384", aead.AES256CTRHMACSHA256KeyTemplate())
83	basicMultipleEncrypts(t, "NIST_P521", aead.AES256CTRHMACSHA256KeyTemplate())
84	basicMultipleEncrypts(t, "NIST_P224", aead.AES256CTRHMACSHA256KeyTemplate())
85
86	basicMultipleEncrypts(t, "NIST_P256", aead.AES128CTRHMACSHA256KeyTemplate())
87	basicMultipleEncrypts(t, "NIST_P384", aead.AES128CTRHMACSHA256KeyTemplate())
88	basicMultipleEncrypts(t, "NIST_P521", aead.AES128CTRHMACSHA256KeyTemplate())
89	basicMultipleEncrypts(t, "NIST_P224", aead.AES128CTRHMACSHA256KeyTemplate())
90}
91
92func TestECAES256GCMEncrypt(t *testing.T) {
93	basicMultipleEncrypts(t, "NIST_P256", aead.AES256GCMKeyTemplate())
94	basicMultipleEncrypts(t, "NIST_P384", aead.AES256GCMKeyTemplate())
95	basicMultipleEncrypts(t, "NIST_P521", aead.AES256GCMKeyTemplate())
96	basicMultipleEncrypts(t, "NIST_P224", aead.AES256GCMKeyTemplate())
97
98	basicMultipleEncrypts(t, "NIST_P256", aead.AES128GCMKeyTemplate())
99	basicMultipleEncrypts(t, "NIST_P384", aead.AES128GCMKeyTemplate())
100	basicMultipleEncrypts(t, "NIST_P521", aead.AES128GCMKeyTemplate())
101	basicMultipleEncrypts(t, "NIST_P224", aead.AES128GCMKeyTemplate())
102}
103
104func TestECAESSIVEncrypt(t *testing.T) {
105	basicMultipleEncrypts(t, "NIST_P256", daead.AESSIVKeyTemplate())
106	basicMultipleEncrypts(t, "NIST_P384", daead.AESSIVKeyTemplate())
107	basicMultipleEncrypts(t, "NIST_P521", daead.AESSIVKeyTemplate())
108	basicMultipleEncrypts(t, "NIST_P224", daead.AESSIVKeyTemplate())
109}
110