File: verifier.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 (143 lines) | stat: -rw-r--r-- 4,047 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
// Copyright 2024 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 ecdsa

import (
	"bytes"
	"crypto/ecdsa"
	"crypto/elliptic"
	"crypto/sha256"
	"crypto/sha512"
	"fmt"
	"hash"
	"math/big"

	"github.com/tink-crypto/tink-go/v2/internal/internalapi"
	internalecdsa "github.com/tink-crypto/tink-go/v2/internal/signature/ecdsa"
	"github.com/tink-crypto/tink-go/v2/key"
	"github.com/tink-crypto/tink-go/v2/tink"
)

// verifier implements the [tink.Verifier] interface for ECDSA (RFC6979).
//
// It accepts signature in both ASN.1 and IEEE_P1363 encoding.
type verifier struct {
	key        *ecdsa.PublicKey
	prefix     []byte
	parameters *Parameters
	hashFunc   func() hash.Hash
}

var _ tink.Verifier = (*verifier)(nil)

func curveFromTinkECDSACurveType(curveType CurveType) (elliptic.Curve, error) {
	switch curveType {
	case NistP256:
		return elliptic.P256(), nil
	case NistP384:
		return elliptic.P384(), nil
	case NistP521:
		return elliptic.P521(), nil
	default:
		// Should never happen.
		return nil, fmt.Errorf("unsupported curve: %v", curveType)
	}
}

func hashFunctionFromEnum(hash HashType) (func() hash.Hash, error) {
	switch hash {
	case SHA256:
		return sha256.New, nil
	case SHA384:
		return sha512.New384, nil
	case SHA512:
		return sha512.New, nil
	default:
		return nil, fmt.Errorf("invalid hash type: %s", hash)
	}
}

// NewVerifier creates a new ECDSA Verifier.
//
// This is an internal API.
func NewVerifier(publicKey *PublicKey, _ internalapi.Token) (tink.Verifier, error) {
	hashFunc, err := hashFunctionFromEnum(publicKey.parameters.HashType())
	if err != nil {
		return nil, err
	}
	curve, err := curveFromTinkECDSACurveType(publicKey.parameters.CurveType())
	if err != nil {
		return nil, err
	}
	publicPoint := publicKey.PublicPoint()
	xy := publicPoint[1:]
	ecdsaPublicKey := &ecdsa.PublicKey{
		Curve: curve,
		X:     new(big.Int).SetBytes(xy[:len(xy)/2]),
		Y:     new(big.Int).SetBytes(xy[len(xy)/2:]),
	}
	return &verifier{
		key:        ecdsaPublicKey,
		prefix:     publicKey.OutputPrefix(),
		parameters: publicKey.parameters,
		hashFunc:   hashFunc,
	}, nil
}

// Verify verifies whether the given signature is valid for the given data.
//
// The signature is expected to be of the form: prefix || signature, where
// prefix is the key's output prefix and can be empty, and signature is the
// signature in the encoding specified by the key's parameters.
func (e *verifier) Verify(signatureBytes, data []byte) error {
	if !bytes.HasPrefix(signatureBytes, e.prefix) {
		return fmt.Errorf("ecdsa_verifier: invalid signature prefix")
	}
	rawSignature := signatureBytes[len(e.prefix):]
	h := e.hashFunc()
	h.Write(data)
	if e.parameters.Variant() == VariantLegacy {
		h.Write([]byte{0})
	}
	hashed := h.Sum(nil)
	var asn1Signature []byte
	switch encoding := e.parameters.SignatureEncoding(); encoding {
	case DER:
		asn1Signature = rawSignature
	case IEEEP1363:
		decodedSig, err := internalecdsa.IEEEP1363Decode(rawSignature)
		if err != nil {
			return err
		}
		asn1Signature, err = internalecdsa.ASN1Encode(decodedSig)
		if err != nil {
			return err
		}
	default:
		return fmt.Errorf("ecdsa_verifier: unsupported encoding: %s", encoding)
	}
	if ok := ecdsa.VerifyASN1(e.key, hashed, asn1Signature); !ok {
		return fmt.Errorf("ecdsa_verifier: invalid signature")
	}
	return nil
}

func verifierConstructor(key key.Key) (any, error) {
	that, ok := key.(*PublicKey)
	if !ok {
		return nil, fmt.Errorf("key is not a *ecdsa.PublicKey")
	}
	return NewVerifier(that, internalapi.Token{})
}