-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrypto.go
More file actions
236 lines (196 loc) · 6.48 KB
/
crypto.go
File metadata and controls
236 lines (196 loc) · 6.48 KB
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
package sctx
import (
"crypto"
"crypto/ecdsa"
"crypto/ed25519"
"crypto/elliptic"
"crypto/rand"
"crypto/sha256"
"encoding/asn1"
"fmt"
"math/big"
)
// CryptoAlgorithm represents the supported cryptographic algorithms.
type CryptoAlgorithm string
const (
// CryptoEd25519 is the default high-performance algorithm (30% faster than ECDSA).
CryptoEd25519 CryptoAlgorithm = "ed25519"
// CryptoECDSAP256 is the FIPS 140-2 compliant algorithm for government/compliance requirements.
CryptoECDSAP256 CryptoAlgorithm = "ecdsa-p256"
// DefaultCryptoAlgorithm prioritizes performance - governments can opt into compliance.
DefaultCryptoAlgorithm = CryptoEd25519
)
// CryptoSigner provides algorithm-agnostic cryptographic operations.
type CryptoSigner interface {
// Sign signs the provided data
Sign(data []byte) ([]byte, error)
// Verify verifies a signature against data and public key
Verify(data []byte, signature []byte, publicKey crypto.PublicKey) bool
// Algorithm returns the algorithm identifier
Algorithm() CryptoAlgorithm
// PublicKey returns the public key
PublicKey() crypto.PublicKey
// KeyType returns a string description for debugging
KeyType() string
}
// NewCryptoSigner creates a new crypto signer for the specified algorithm.
func NewCryptoSigner(algorithm CryptoAlgorithm, privateKey crypto.PrivateKey) (CryptoSigner, error) {
if privateKey == nil {
return nil, fmt.Errorf("private key is required")
}
switch algorithm {
case CryptoEd25519:
ed25519Key, ok := privateKey.(ed25519.PrivateKey)
if !ok {
return nil, fmt.Errorf("private key must be ed25519.PrivateKey for Ed25519 algorithm")
}
return &ed25519Signer{privateKey: ed25519Key}, nil
case CryptoECDSAP256:
ecdsaKey, ok := privateKey.(*ecdsa.PrivateKey)
if !ok {
return nil, fmt.Errorf("private key must be *ecdsa.PrivateKey for ECDSA P-256 algorithm")
}
if ecdsaKey == nil {
return nil, fmt.Errorf("private key is required")
}
if ecdsaKey.Curve != elliptic.P256() {
return nil, fmt.Errorf("ECDSA private key must use P-256 curve for NIST compliance")
}
return &ecdsaP256Signer{privateKey: ecdsaKey}, nil
default:
return nil, fmt.Errorf("unsupported algorithm: %s", algorithm)
}
}
// GenerateKeyPair generates a key pair for the specified algorithm.
func GenerateKeyPair(algorithm CryptoAlgorithm) (crypto.PublicKey, crypto.PrivateKey, error) {
switch algorithm {
case CryptoEd25519:
publicKey, privateKey, err := ed25519.GenerateKey(rand.Reader)
return publicKey, privateKey, err
case CryptoECDSAP256:
privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
return nil, nil, err
}
return &privateKey.PublicKey, privateKey, nil
default:
return nil, nil, fmt.Errorf("unsupported algorithm: %s", algorithm)
}
}
// ed25519Signer implements CryptoSigner for Ed25519.
type ed25519Signer struct {
privateKey ed25519.PrivateKey
}
func (s *ed25519Signer) Sign(data []byte) ([]byte, error) {
// Ed25519 signs the data directly (no hashing required)
signature := ed25519.Sign(s.privateKey, data)
return signature, nil
}
func (*ed25519Signer) Verify(data []byte, signature []byte, publicKey crypto.PublicKey) bool {
ed25519PubKey, ok := publicKey.(ed25519.PublicKey)
if !ok {
return false
}
return ed25519.Verify(ed25519PubKey, data, signature)
}
func (*ed25519Signer) Algorithm() CryptoAlgorithm {
return CryptoEd25519
}
func (s *ed25519Signer) PublicKey() crypto.PublicKey {
return s.privateKey.Public()
}
func (*ed25519Signer) KeyType() string {
return "Ed25519"
}
// ecdsaP256Signer implements CryptoSigner for ECDSA P-256.
type ecdsaP256Signer struct {
privateKey *ecdsa.PrivateKey
}
// ecdsaSignature represents an ECDSA signature for ASN.1 encoding (FIPS compliance).
type ecdsaSignature struct {
R *big.Int
S *big.Int
}
func (s *ecdsaP256Signer) Sign(data []byte) ([]byte, error) {
// ECDSA requires hashing the data first
hash := sha256.Sum256(data)
r, sig, err := ecdsa.Sign(rand.Reader, s.privateKey, hash[:])
if err != nil {
return nil, fmt.Errorf("ECDSA signing failed: %w", err)
}
// Encode signature using ASN.1 DER format (FIPS standard)
signature := ecdsaSignature{R: r, S: sig}
signatureBytes, err := asn1.Marshal(signature)
if err != nil {
return nil, fmt.Errorf("failed to marshal ECDSA signature: %w", err)
}
return signatureBytes, nil
}
func (*ecdsaP256Signer) Verify(data []byte, signature []byte, publicKey crypto.PublicKey) bool {
ecdsaPubKey, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
return false
}
// Decode ASN.1 DER signature
var sig ecdsaSignature
if _, err := asn1.Unmarshal(signature, &sig); err != nil {
return false
}
// Hash the data
hash := sha256.Sum256(data)
// Verify signature
return ecdsa.Verify(ecdsaPubKey, hash[:], sig.R, sig.S)
}
func (*ecdsaP256Signer) Algorithm() CryptoAlgorithm {
return CryptoECDSAP256
}
func (s *ecdsaP256Signer) PublicKey() crypto.PublicKey {
return &s.privateKey.PublicKey
}
func (*ecdsaP256Signer) KeyType() string {
return "ECDSA P-256"
}
// DetectAlgorithmFromPublicKey determines the algorithm from a public key.
func DetectAlgorithmFromPublicKey(publicKey crypto.PublicKey) (CryptoAlgorithm, error) {
if publicKey == nil {
return "", fmt.Errorf("public key is nil")
}
switch key := publicKey.(type) {
case ed25519.PublicKey:
return CryptoEd25519, nil
case *ecdsa.PublicKey:
if key.Curve == elliptic.P256() {
return CryptoECDSAP256, nil
}
return "", fmt.Errorf("unsupported ECDSA curve: %s", key.Curve.Params().Name)
default:
return "", fmt.Errorf("unsupported public key type: %T", publicKey)
}
}
// DetectAlgorithmFromPrivateKey determines the algorithm from a private key.
func DetectAlgorithmFromPrivateKey(privateKey crypto.PrivateKey) (CryptoAlgorithm, error) {
if privateKey == nil {
return "", fmt.Errorf("private key is nil")
}
switch key := privateKey.(type) {
case ed25519.PrivateKey:
return CryptoEd25519, nil
case *ecdsa.PrivateKey:
if key.Curve == elliptic.P256() {
return CryptoECDSAP256, nil
}
return "", fmt.Errorf("unsupported ECDSA curve: %s", key.Curve.Params().Name)
default:
return "", fmt.Errorf("unsupported private key type: %T", privateKey)
}
}
// ValidateAlgorithm checks if the algorithm is supported.
func ValidateAlgorithm(algorithm CryptoAlgorithm) error {
switch algorithm {
case CryptoEd25519, CryptoECDSAP256:
return nil
default:
return fmt.Errorf("unsupported algorithm: %s. Supported algorithms: %s (default, high-performance), %s (FIPS 140-2 compliant)",
algorithm, CryptoEd25519, CryptoECDSAP256)
}
}