/
signer.go
60 lines (50 loc) · 1.28 KB
/
signer.go
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
// Copyright Chainify Group LTD. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package signature
import (
"crypto/ecdsa"
"crypto/elliptic"
"errors"
"fmt"
"hash"
"github.com/chainifynet/aws-encryption-sdk-go/pkg/internal/crypto/hasher"
"github.com/chainifynet/aws-encryption-sdk-go/pkg/internal/utils/rand"
)
var (
ErrSigning = errors.New("sign error")
)
type Signer interface {
hasher.Hasher
Sign() ([]byte, error)
}
type ECCSigner struct {
hasher.Hasher
signLen int
key *ecdsa.PrivateKey
}
var _ Signer = (*ECCSigner)(nil)
type SignerFunc func(hashFn func() hash.Hash, c elliptic.Curve, signLen int, key *ecdsa.PrivateKey) Signer
func NewECCSigner(hashFn func() hash.Hash, c elliptic.Curve, signLen int, key *ecdsa.PrivateKey) Signer {
return &ECCSigner{
Hasher: hasher.NewECCHasher(hashFn, c),
signLen: signLen,
key: key,
}
}
func (s *ECCSigner) Sign() ([]byte, error) {
var signature []byte
for {
// can be replaced with:
// sig, err := cs.key.Sign(rand.Reader, finalHash, nil)
sig, err := ecdsa.SignASN1(rand.Reader, s.key, s.Sum())
if err != nil {
return nil, fmt.Errorf("signASN1: %w", errors.Join(ErrSigning, err))
}
if len(sig) == s.signLen {
signature = sig
break
}
continue
}
return signature, nil
}