File: ed25519_signer_verifier_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 (271 lines) | stat: -rw-r--r-- 8,758 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// Copyright 2020 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 subtle_test

import (
	"bytes"
	"crypto/ed25519"
	"crypto/rand"
	"encoding/hex"
	"fmt"
	"slices"
	"testing"

	"github.com/google/go-cmp/cmp"
	subtleSignature "github.com/tink-crypto/tink-go/v2/signature/subtle"
	"github.com/tink-crypto/tink-go/v2/subtle/random"
	"github.com/tink-crypto/tink-go/v2/testutil"
	"github.com/tink-crypto/tink-go/v2/tink"
)

func TestED25519SignVerifyCorrectness(t *testing.T) {
	// Taken from https://datatracker.ietf.org/doc/html/rfc8032#section-7.1 - TEST 3.
	message := []byte{0xaf, 0x82}
	privKeyHex := "c5aa8df43f9f837bedb7442f31dcb7b166d38535076f094b85ce3a2e0b4458f7"
	privKeySeed, err := hex.DecodeString(privKeyHex)
	if err != nil {
		t.Fatalf("hex.DecodeString(%q) err = %v, want nil", privKeyHex, err)
	}
	privateKey := ed25519.NewKeyFromSeed(privKeySeed)
	pubKeyHex := "fc51cd8e6218a1a38da47ed00230f0580816ed13ba3303ac5deb911548908025"
	pubKeyBytes, err := hex.DecodeString(pubKeyHex)
	if err != nil {
		t.Fatalf("hex.DecodeString(%q) err = %v, want nil", pubKeyHex, err)
	}
	publicKey := ed25519.PublicKey(pubKeyBytes)
	signatureHex := "6291d657deec24024827e69c3abe01a30ce548a284743a445e3680d7db5ac3ac18ff9b538d16f290ae67f760984dc6594a7c15e9716ed28dc027beceea1ec40a"
	wantSignature, err := hex.DecodeString(signatureHex)
	if err != nil {
		t.Fatalf("hex.DecodeString(%q) err = %v, want nil", signatureHex, err)
	}

	signer, err := subtleSignature.NewED25519SignerFromPrivateKey(&privateKey)
	if err != nil {
		t.Fatalf("unexpected error when creating ED25519 Signer: %s", err)
	}
	verifier, err := subtleSignature.NewED25519VerifierFromPublicKey(&publicKey)
	if err != nil {
		t.Fatalf("unexpected error when creating ED25519 Verifier: %s", err)
	}

	gotSignature, err := signer.Sign(message)
	if err != nil {
		t.Fatalf("signer.Sign(%x) err = %v, want nil", message, err)
	}
	if diff := cmp.Diff(gotSignature, wantSignature); diff != "" {
		t.Errorf("signer.Sign() returned unexpected diff (-want +got):\n%s", diff)
	}

	if err := verifier.Verify(wantSignature, message); err != nil {
		t.Errorf("verifier.Verify(%x, %x) err = %v, want nil", wantSignature, message, err)
	}
}

func TestED25519VerifyFails(t *testing.T) {
	publicKey, privateKey, err := ed25519.GenerateKey(rand.Reader)
	if err != nil {
		t.Fatalf("key generation error: %s", err)
	}
	signer, err := subtleSignature.NewED25519SignerFromPrivateKey(&privateKey)
	if err != nil {
		t.Fatalf("unexpected error when creating ED25519 Signer: %s", err)
	}
	verifier, err := subtleSignature.NewED25519VerifierFromPublicKey(&publicKey)
	if err != nil {
		t.Fatalf("unexpected error when creating ED25519 Verifier: %s", err)
	}
	data := random.GetRandomBytes(20)
	signatureBytes, err := signer.Sign(data)
	if err != nil {
		t.Fatalf("signer.Sign(%x) err = %v, want nil", data, err)
	}

	// Modify the signature.
	for i := 0; i < len(signatureBytes); i++ {
		modifiedRawSignature := slices.Clone(signatureBytes)
		for j := 0; j < 8; j++ {
			modifiedRawSignature[i] = byte(modifiedRawSignature[i] ^ (1 << uint32(j)))
			if err := verifier.Verify(modifiedRawSignature, data); err == nil {
				t.Errorf("verifier.Verify(%x, data) err = nil, want error", modifiedRawSignature)
			}
		}
	}
	// Modify the message.
	for i := 0; i < len(data); i++ {
		modifiedData := slices.Clone(data)
		for j := 0; j < 8; j++ {
			modifiedData[i] = byte(modifiedData[i] ^ (1 << uint32(j)))
			if err := verifier.Verify(signatureBytes, modifiedData); err == nil {
				t.Errorf("verifier.Verify(signature, %x) err = nil, want error", modifiedData)
			}
		}
	}
}

func TestED25519SignVerify(t *testing.T) {
	publicKey, privateKey, err := ed25519.GenerateKey(rand.Reader)
	if err != nil {
		t.Fatalf("key generation error: %s", err)
	}
	for _, tc := range []struct {
		name     string
		signer   tink.Signer
		verifier tink.Verifier
	}{
		{
			name: "signer from private key",
			signer: func() tink.Signer {
				signer, err := subtleSignature.NewED25519SignerFromPrivateKey(&privateKey)
				if err != nil {
					t.Fatalf("unexpected error when creating ED25519 Signer: %s", err)
				}
				return signer
			}(),
			verifier: func() tink.Verifier {
				verifier, err := subtleSignature.NewED25519VerifierFromPublicKey(&publicKey)
				if err != nil {
					t.Fatalf("unexpected error when creating ED25519 Verifier: %s", err)
				}
				return verifier
			}(),
		},
		{
			name: "signer from slice",
			signer: func() tink.Signer {
				signer, err := subtleSignature.NewED25519Signer(privateKey[:ed25519.SeedSize])
				if err != nil {
					t.Fatalf("unexpected error when creating ED25519 Signer: %s", err)
				}
				return signer
			}(),
			verifier: func() tink.Verifier {
				verifier, err := subtleSignature.NewED25519VerifierFromPublicKey(&publicKey)
				if err != nil {
					t.Fatalf("unexpected error when creating ED25519 Verifier: %s", err)
				}
				return verifier
			}(),
		},
	} {
		t.Run(tc.name, func(t *testing.T) {
			for i := 0; i < 100; i++ {
				data := random.GetRandomBytes(20)
				signatureBytes, err := tc.signer.Sign(data)
				if err != nil {
					t.Fatalf("signer.Sign(%x) err = %v, want nil", data, err)
				}
				if err := tc.verifier.Verify(signatureBytes, data); err != nil {
					t.Errorf("tc.verifier.Verify(%x, %x) err = %v, want nil", signatureBytes, data, err)
				}
			}
		})
	}
}

func NewED25519SignerInvalidKeyLength(t *testing.T) {
	seedHex := "fc51cd8e6218a1a38da47ed00230f0580816ed13ba3303ac5deb911548908025"
	seedBytes, err := hex.DecodeString(seedHex)
	if err != nil {
		t.Fatalf("hex.DecodeString(%q) err = %v, want nil", seedHex, err)
	}
	for _, tc := range []struct {
		name     string
		keyValue []byte
	}{
		{
			name:     "nil key",
			keyValue: nil,
		},
		{
			name:     "empty key",
			keyValue: []byte{},
		},
		{
			name:     "too small",
			keyValue: seedBytes[:len(seedBytes)-1],
		},
		{
			name:     "too large",
			keyValue: slices.Concat(seedBytes, []byte{0xFF}),
		},
	} {
		t.Run(tc.name, func(t *testing.T) {
			_, err := subtleSignature.NewED25519Signer(tc.keyValue)
			if err == nil {
				t.Errorf("NewED25519Signer(0x%x) err = nil, want error", tc.keyValue)
			}
		})
	}
}

func TestED25519WycheproofCases(t *testing.T) {
	suite := new(ed25519Suite)
	if err := testutil.PopulateSuite(suite, "eddsa_test.json"); err != nil {
		t.Fatalf("failed populating suite: %s", err)
	}
	for _, group := range suite.TestGroups {
		private := ed25519.PrivateKey(group.Key.SK)
		public := ed25519.PrivateKey(group.Key.PK)
		signer, err := subtleSignature.NewED25519Signer(private)
		if err != nil {
			continue
		}
		verifier, err := subtleSignature.NewED25519Verifier(public)
		if err != nil {
			continue
		}
		for _, test := range group.Tests {
			caseName := fmt.Sprintf("Sign-%s-%s:Case-%d", suite.Algorithm, group.Type, test.CaseID)
			t.Run(caseName, func(t *testing.T) {
				got, err := signer.Sign(test.Message)
				switch test.Result {
				case "valid":
					if err != nil {
						t.Fatalf("ED25519Signer.Sign() failed in a valid test case: %s", err)
					}
					if !bytes.Equal(got, test.Signature) {
						// Ed25519 is deterministic.
						// Getting an alternative signature may leak the private key.
						// This is especially the case if an attacker can also learn the valid signature.
						t.Fatalf("ED25519Signer.Sign() = %s, want = %s", hex.EncodeToString(got), hex.EncodeToString(test.Signature))
					}
				case "invalid":
					if err == nil && bytes.Equal(got, test.Signature) {
						t.Fatalf("ED25519Signer.Sign() produced a matching signature in an invalid test case.")
					}
				default:
					t.Fatalf("unrecognized result: %q", test.Result)
				}
			})

			caseName = fmt.Sprintf("Verify-%s-%s:Case-%d", suite.Algorithm, group.Type, test.CaseID)
			t.Run(caseName, func(t *testing.T) {
				err := verifier.Verify(test.Signature, test.Message)
				switch test.Result {
				case "valid":
					if err != nil {
						t.Fatalf("ED25519Verifier.Verify() failed in a valid test case: %s", err)
					}
				case "invalid":
					if err == nil {
						t.Fatal("ED25519Verifier.Verify() succeeded in an invalid test case.")
					}
				default:
					t.Fatalf("unsupported test result: %q", test.Result)
				}
			})
		}
	}
}