File: key.go

package info (click to toggle)
golang-github-tink-crypto-tink-go 2.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 14,996 kB
  • sloc: sh: 876; makefile: 6
file content (401 lines) | stat: -rw-r--r-- 12,900 bytes parent folder | download | duplicates (4)
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
// Copyright 2025 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 slhdsa

import (
	"bytes"
	"fmt"

	"github.com/tink-crypto/tink-go/v2/insecuresecretdataaccess"
	"github.com/tink-crypto/tink-go/v2/internal/outputprefix"
	"github.com/tink-crypto/tink-go/v2/internal/signature/slhdsa"
	"github.com/tink-crypto/tink-go/v2/key"
	"github.com/tink-crypto/tink-go/v2/secretdata"
)

// Variant is the prefix variant of a SLH-DSA key.
//
// It describes the format of the signature. For SLH-DSA, there are two options:
//
//   - TINK: prepends '0x01<big endian key id>' to the signature.
//   - NO_PREFIX: adds no prefix to the signature.
type Variant int

const (
	// VariantUnknown is the default value of Variant.
	VariantUnknown Variant = iota
	// VariantTink prefixes '0x01<big endian key id>' to the signature.
	VariantTink
	// VariantNoPrefix does not prefix the signature with the key id.
	VariantNoPrefix
)

func (variant Variant) String() string {
	switch variant {
	case VariantTink:
		return "TINK"
	case VariantNoPrefix:
		return "NO_PREFIX"
	default:
		return "UNKNOWN"
	}
}

// HashType is the hash type of the SLH-DSA key.
type HashType int

const (
	// UnknownHashType is the default value of HashType.
	UnknownHashType HashType = iota
	// SHA2 hashing.
	SHA2
	// SHAKE hashing.
	SHAKE
)

// SignatureType is the signature type of the SLH-DSA key.
type SignatureType int

const (
	// UnknownSignatureType is the default value of SignatureType.
	UnknownSignatureType SignatureType = iota
	// FastSigning selects fast signing.
	FastSigning
	// SmallSignature selects small signatures.
	SmallSignature
)

type parameterSet struct {
	hashType HashType
	keySize  int
	sigType  SignatureType
}

// Parameters represents the parameters of a SLH-DSA key.
//
// Currently, only the following parameters are supported:
//
//	SLH-DSA-SHA2-128s: {hashType: SHA2, keySize: 64, sigType: SmallSignature},
//	SLH-DSA-SHAKE-256f: {hashType: SHAKE, keySize: 128, sigType: FastSigning}.
type Parameters struct {
	paramSet parameterSet
	variant  Variant
}

func slhDSASHA2128s() parameterSet {
	return parameterSet{
		hashType: SHA2,
		keySize:  64,
		sigType:  SmallSignature,
	}
}

func slhDSASHAKE256f() parameterSet {
	return parameterSet{
		hashType: SHAKE,
		keySize:  128,
		sigType:  FastSigning,
	}
}

func checkSupportedParameters(paramSet parameterSet) error {
	switch paramSet {
	case slhDSASHA2128s(), slhDSASHAKE256f():
		return nil
	default:
		return fmt.Errorf("unsupported parameters: %v", paramSet)
	}
}

var _ key.Parameters = (*Parameters)(nil)

// NewParameters creates a new Parameters.
func NewParameters(hashType HashType, keySize int, sigType SignatureType, variant Variant) (*Parameters, error) {
	paramSet := parameterSet{hashType: hashType, keySize: keySize, sigType: sigType}
	if err := checkSupportedParameters(paramSet); err != nil {
		return nil, fmt.Errorf("slhdsa.NewParameters: %w", err)
	}
	if variant == VariantUnknown {
		return nil, fmt.Errorf("slhdsa.NewParameters: variant must not be %v", VariantUnknown)
	}
	return &Parameters{
		paramSet: paramSet,
		variant:  variant,
	}, nil
}

// HashType returns the hash type.
func (p *Parameters) HashType() HashType { return p.paramSet.hashType }

// KeySize returns the key size in bytes.
func (p *Parameters) KeySize() int { return p.paramSet.keySize }

// SignatureType returns the signature type.
func (p *Parameters) SignatureType() SignatureType { return p.paramSet.sigType }

// Variant returns the prefix variant of the parameters.
func (p *Parameters) Variant() Variant { return p.variant }

// HasIDRequirement returns true if the key has an ID requirement.
func (p *Parameters) HasIDRequirement() bool { return p.variant != VariantNoPrefix }

// Equal returns true if this parameters object is equal to other.
func (p *Parameters) Equal(other key.Parameters) bool {
	then, ok := other.(*Parameters)
	return ok && p.paramSet == then.paramSet && p.variant == then.variant
}

// PublicKey represents a SLH-DSA public key.
type PublicKey struct {
	keyBytes      []byte
	idRequirement uint32
	params        *Parameters
	outputPrefix  []byte
}

var _ key.Key = (*PublicKey)(nil)

func calculateOutputPrefix(variant Variant, keyID uint32) ([]byte, error) {
	switch variant {
	case VariantTink:
		return outputprefix.Tink(keyID), nil
	case VariantNoPrefix:
		return nil, nil
	default:
		return nil, fmt.Errorf("invalid output prefix variant: %v", variant)
	}
}

func publicKeyLengthForParams(paramSet parameterSet) (int, error) {
	switch paramSet {
	case slhDSASHA2128s():
		return slhdsa.SLH_DSA_SHA2_128s.PublicKeyLength(), nil
	case slhDSASHAKE256f():
		return slhdsa.SLH_DSA_SHAKE_256f.PublicKeyLength(), nil
	default:
		return 0, fmt.Errorf("invalid parameters: %v", paramSet)
	}
}

func privateKeyLengthForParams(paramSet parameterSet) (int, error) {
	switch paramSet {
	case slhDSASHA2128s():
		return slhdsa.SLH_DSA_SHA2_128s.SecretKeyLength(), nil
	case slhDSASHAKE256f():
		return slhdsa.SLH_DSA_SHAKE_256f.SecretKeyLength(), nil
	default:
		return 0, fmt.Errorf("invalid parameters: %v", paramSet)
	}
}

// checkPublicKeyLengthForParameters assumes that params are not nil.
func checkPublicKeyLengthForParameters(length int, params *Parameters) error {
	expLength, err := publicKeyLengthForParams(params.paramSet)
	if err != nil {
		return err
	}
	if length != expLength {
		return fmt.Errorf("invalid public key length: %v", length)
	}
	return nil
}

// checkPrivateKeyLengthForParameters assumes that params are not nil.
func checkPrivateKeyLengthForParameters(length int, params *Parameters) error {
	expLength, err := privateKeyLengthForParams(params.paramSet)
	if err != nil {
		return err
	}
	if length != expLength {
		return fmt.Errorf("invalid private key length: %v", length)
	}
	return nil
}

// NewPublicKey creates a new SLH-DSA public key.
//
// idRequirement is the ID of the key in the keyset. It must be zero if params
// doesn't have an ID requirement.
func NewPublicKey(keyBytes []byte, idRequirement uint32, params *Parameters) (*PublicKey, error) {
	if !params.HasIDRequirement() && idRequirement != 0 {
		return nil, fmt.Errorf("slhdsa.NewPublicKey: idRequirement must be zero if params doesn't have an ID requirement")
	}
	if err := checkPublicKeyLengthForParameters(len(keyBytes), params); err != nil {
		return nil, fmt.Errorf("slhdsa.NewPublicKey: %w", err)
	}
	outputPrefix, err := calculateOutputPrefix(params.variant, idRequirement)
	if err != nil {
		return nil, fmt.Errorf("slhdsa.NewPublicKey: %w", err)
	}
	return &PublicKey{
		keyBytes:      bytes.Clone(keyBytes),
		idRequirement: idRequirement,
		params:        params,
		outputPrefix:  outputPrefix,
	}, nil
}

// KeyBytes returns the public key bytes.
func (k *PublicKey) KeyBytes() []byte { return bytes.Clone(k.keyBytes) }

// OutputPrefix returns the output prefix of this key.
func (k *PublicKey) OutputPrefix() []byte { return bytes.Clone(k.outputPrefix) }

// Parameters returns the parameters of the key.
func (k *PublicKey) Parameters() key.Parameters { return k.params }

// IDRequirement returns the ID requirement of the key, and whether it is
// required.
func (k *PublicKey) IDRequirement() (uint32, bool) {
	return k.idRequirement, k.params.HasIDRequirement()
}

// Equal returns true if this key is equal to other.
func (k *PublicKey) Equal(other key.Key) bool {
	if k == other {
		return true
	}
	that, ok := other.(*PublicKey)
	return ok && k.params.Equal(that.Parameters()) &&
		bytes.Equal(k.keyBytes, that.keyBytes) &&
		k.idRequirement == that.idRequirement
}

// PrivateKey represents a SLH-DSA private key.
type PrivateKey struct {
	publicKey *PublicKey
	// keyBytes is the seed used to generate the private key.
	keyBytes secretdata.Bytes
	// expandedKeyBytes is the expanded private key.
	// We cache the expanded private key as an optimization. Decoding it is
	// faster than recomputing it from the seed every time we create a signer.
	expandedKeyBytes secretdata.Bytes
}

var _ key.Key = (*PrivateKey)(nil)

// publicKeyForParameters assumes that len(privateKeyBytes) is correct and that params are not nil.
func publicKeyForParameters(privateKeyBytes secretdata.Bytes, params *Parameters) ([]byte, error) {
	var sk *slhdsa.SecretKey
	var err error
	switch params.paramSet {
	case slhDSASHA2128s():
		sk, err = slhdsa.SLH_DSA_SHA2_128s.DecodeSecretKey(privateKeyBytes.Data(insecuresecretdataaccess.Token{}))
	case slhDSASHAKE256f():
		sk, err = slhdsa.SLH_DSA_SHAKE_256f.DecodeSecretKey(privateKeyBytes.Data(insecuresecretdataaccess.Token{}))
	default:
		return nil, fmt.Errorf("invalid parameters: %v", params)
	}
	if err != nil {
		return nil, fmt.Errorf("invalid private key bytes: %w", err)
	}
	return sk.PublicKey().Encode(), nil
}

// NewPrivateKey creates a new SLH-DSA private key from privateKeyBytes, with
// idRequirement and params.
func NewPrivateKey(privateKeyBytes secretdata.Bytes, idRequirement uint32, params *Parameters) (*PrivateKey, error) {
	if params == nil {
		return nil, fmt.Errorf("slhdsa.NewPrivateKey: params must not be nil")
	}
	if err := checkPrivateKeyLengthForParameters(privateKeyBytes.Len(), params); err != nil {
		return nil, fmt.Errorf("slhdsa.NewPrivateKey: %w", err)
	}
	pubKeyBytes, err := publicKeyForParameters(privateKeyBytes, params)
	if err != nil {
		return nil, fmt.Errorf("slhdsa.NewPrivateKey: %w", err)
	}
	pubKey, err := NewPublicKey(pubKeyBytes, idRequirement, params)
	if err != nil {
		return nil, fmt.Errorf("slhdsa.NewPrivateKey: %w", err)
	}
	return &PrivateKey{
		publicKey: pubKey,
		keyBytes:  privateKeyBytes,
	}, nil
}

// NewPrivateKeyWithPublicKey creates a new SLH-DSA private key from
// privateKeyBytes and a [PublicKey].
func NewPrivateKeyWithPublicKey(privateKeyBytes secretdata.Bytes, pubKey *PublicKey) (*PrivateKey, error) {
	if pubKey == nil {
		return nil, fmt.Errorf("slhdsa.NewPrivateKeyWithPublicKey: pubKey must not be nil")
	}
	if pubKey.params == nil {
		return nil, fmt.Errorf("slhdsa.NewPrivateKeyWithPublicKey: pubKey.params must not be nil")
	}
	if err := checkPrivateKeyLengthForParameters(privateKeyBytes.Len(), pubKey.params); err != nil {
		return nil, fmt.Errorf("slhdsa.NewPrivateKeyWithPublicKey: %w", err)
	}
	// Make sure the public key is correct.
	pubKeyBytes, err := publicKeyForParameters(privateKeyBytes, pubKey.params)
	if err != nil {
		return nil, fmt.Errorf("slhdsa.NewPrivateKey: %w", err)
	}
	if !bytes.Equal(pubKeyBytes, pubKey.KeyBytes()) {
		return nil, fmt.Errorf("slhdsa.NewPrivateKeyWithPublicKey: public key does not match private key")
	}
	return &PrivateKey{
		publicKey: pubKey,
		keyBytes:  privateKeyBytes,
	}, nil
}

// PrivateKeyBytes returns the private key seed.
func (k *PrivateKey) PrivateKeyBytes() secretdata.Bytes { return k.keyBytes }

// PublicKey returns the public key of the key.
//
// This implements the privateKey interface defined in handle.go.
func (k *PrivateKey) PublicKey() (key.Key, error) { return k.publicKey, nil }

// Parameters returns the parameters of the key.
func (k *PrivateKey) Parameters() key.Parameters { return k.publicKey.params }

// IDRequirement returns the ID requirement of the key, and whether it is
// required.
func (k *PrivateKey) IDRequirement() (uint32, bool) { return k.publicKey.IDRequirement() }

// OutputPrefix returns the output prefix of this key.
func (k *PrivateKey) OutputPrefix() []byte { return bytes.Clone(k.publicKey.outputPrefix) }

// Equal returns true if this key is equal to other.
func (k *PrivateKey) Equal(other key.Key) bool {
	if k == other {
		return true
	}
	that, ok := other.(*PrivateKey)
	return ok && k.publicKey.Equal(that.publicKey) &&
		k.keyBytes.Equal(that.keyBytes) &&
		k.expandedKeyBytes.Equal(that.expandedKeyBytes)
}

func createPrivateKey(p key.Parameters, idRequirement uint32) (key.Key, error) {
	slhDSAParams, ok := p.(*Parameters)
	if !ok {
		return nil, fmt.Errorf("invalid parameters type: %T", p)
	}
	// Make sure the parameters are not "empty".
	var sk *slhdsa.SecretKey
	switch slhDSAParams.paramSet {
	case slhDSASHA2128s():
		sk, _ = slhdsa.SLH_DSA_SHA2_128s.KeyGen()
	case slhDSASHAKE256f():
		sk, _ = slhdsa.SLH_DSA_SHAKE_256f.KeyGen()
	default:
		return nil, fmt.Errorf("invalid parameters: %v", slhDSAParams)
	}
	return NewPrivateKey(secretdata.NewBytesFromData(sk.Encode(), insecuresecretdataaccess.Token{}), idRequirement, slhDSAParams)
}