File: ecies_aead_hkdf_hybrid_encrypt_test.go

package info (click to toggle)
golang-github-tink-crypto-tink-go 2.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 12,952 kB
  • sloc: sh: 864; makefile: 6
file content (108 lines) | stat: -rw-r--r-- 3,941 bytes parent folder | download
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
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package ecies_test

import (
	"bytes"
	"testing"

	"github.com/tink-crypto/tink-go/v2/aead"
	"github.com/tink-crypto/tink-go/v2/daead"
	"github.com/tink-crypto/tink-go/v2/hybrid/internal/ecies"
	"github.com/tink-crypto/tink-go/v2/hybrid/subtle"
	"github.com/tink-crypto/tink-go/v2/subtle/random"
	tinkpb "github.com/tink-crypto/tink-go/v2/proto/tink_go_proto"
)

func basicMultipleEncrypts(t *testing.T, c string, k *tinkpb.KeyTemplate) {
	t.Helper()
	curve, err := subtle.GetCurve(c)
	if err != nil {
		t.Fatalf("error getting %s curve: %s ", c, err)
	}
	pvt, err := subtle.GenerateECDHKeyPair(curve)
	if err != nil {
		t.Fatalf("error generating ECDH key pair: %s", err)
	}
	salt := []byte("some salt")
	pt := random.GetRandomBytes(20)
	context := []byte("context info")
	rDem, err := ecies.NewDEMHelper(k)
	if err != nil {
		t.Fatalf("error generating a DEM helper :%s", err)
	}
	e, err := subtle.NewECIESAEADHKDFHybridEncrypt(&pvt.PublicKey, salt, "SHA256", "UNCOMPRESSED", rDem)
	if err != nil {
		t.Fatalf("error generating an encryption construct :%s", err)
	}
	d, err := subtle.NewECIESAEADHKDFHybridDecrypt(pvt, salt, "SHA256", "UNCOMPRESSED", rDem)
	if err != nil {
		t.Fatalf("error generating an decryption construct :%s", err)
	}
	cl := [][]byte{}
	for i := 0; i < 8; i++ {
		ct, err := e.Encrypt(pt, context)
		if err != nil {
			t.Fatalf("encryption error :%s", err)
		}
		for _, c := range cl {
			if bytes.Equal(ct, c) {
				t.Fatalf("encryption is not randomized")
			}
		}
		cl = append(cl, ct)
		dt, err := d.Decrypt(ct, context)
		if err != nil {
			t.Fatalf("decryption error :%s", err)
		}
		if !bytes.Equal(dt, pt) {
			t.Fatalf("decryption not inverse of encryption")
		}
	}
	if len(cl) != 8 {
		t.Errorf("randomized encryption check failed")
	}
}

func TestECAESCTRHMACSHA256Encrypt(t *testing.T) {
	basicMultipleEncrypts(t, "NIST_P256", aead.AES256CTRHMACSHA256KeyTemplate())
	basicMultipleEncrypts(t, "NIST_P384", aead.AES256CTRHMACSHA256KeyTemplate())
	basicMultipleEncrypts(t, "NIST_P521", aead.AES256CTRHMACSHA256KeyTemplate())
	basicMultipleEncrypts(t, "NIST_P224", aead.AES256CTRHMACSHA256KeyTemplate())

	basicMultipleEncrypts(t, "NIST_P256", aead.AES128CTRHMACSHA256KeyTemplate())
	basicMultipleEncrypts(t, "NIST_P384", aead.AES128CTRHMACSHA256KeyTemplate())
	basicMultipleEncrypts(t, "NIST_P521", aead.AES128CTRHMACSHA256KeyTemplate())
	basicMultipleEncrypts(t, "NIST_P224", aead.AES128CTRHMACSHA256KeyTemplate())
}

func TestECAES256GCMEncrypt(t *testing.T) {
	basicMultipleEncrypts(t, "NIST_P256", aead.AES256GCMKeyTemplate())
	basicMultipleEncrypts(t, "NIST_P384", aead.AES256GCMKeyTemplate())
	basicMultipleEncrypts(t, "NIST_P521", aead.AES256GCMKeyTemplate())
	basicMultipleEncrypts(t, "NIST_P224", aead.AES256GCMKeyTemplate())

	basicMultipleEncrypts(t, "NIST_P256", aead.AES128GCMKeyTemplate())
	basicMultipleEncrypts(t, "NIST_P384", aead.AES128GCMKeyTemplate())
	basicMultipleEncrypts(t, "NIST_P521", aead.AES128GCMKeyTemplate())
	basicMultipleEncrypts(t, "NIST_P224", aead.AES128GCMKeyTemplate())
}

func TestECAESSIVEncrypt(t *testing.T) {
	basicMultipleEncrypts(t, "NIST_P256", daead.AESSIVKeyTemplate())
	basicMultipleEncrypts(t, "NIST_P384", daead.AESSIVKeyTemplate())
	basicMultipleEncrypts(t, "NIST_P521", daead.AESSIVKeyTemplate())
	basicMultipleEncrypts(t, "NIST_P224", daead.AESSIVKeyTemplate())
}