-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheddsa.go
101 lines (75 loc) · 2.09 KB
/
eddsa.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
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
package jwt
//RFC8032
//EDDSA +1
import (
"crypto/ed25519"
cryptorand "crypto/rand"
"unsafe"
"fmt"
"io"
)
type KeySource interface {
~[]byte | ~string | ~*[privateKeyLen]byte | ~[privateKeyLen]byte
}
const (
privateKeyLen = ed25519.PrivateKeySize // 64
publicKeyLen = ed25519.PublicKeySize //32
signatureSize = ed25519.SignatureSize
seedLen = ed25519.SeedSize //32
)
type _EDDSA struct {
PrivateKey *PrivateKeyEd
PublicKey *PublicKeyEd
}
func (e *_EDDSA) Bytes() []byte { return e.PrivateKey.key[:] }
func (e *_EDDSA) SignSize() int { return privateKeyLen }
func (e *_EDDSA) Algorithm() Algorithm { return EDDSA }
func NewEddsa(private *PrivateKeyEd, public *PublicKeyEd) (*_EDDSA, error) {
switch {
case private == nil:
return nil, ErrNil
default:
return &_EDDSA{PrivateKey: private, PublicKey: public}, nil
}
}
func (e *_EDDSA) Sign(payload []byte) ([64]byte, error) {
if len(payload) == 0 {
return [64]byte{}, ErrPayloadIsEmpty
}
if e.PrivateKey == nil {
return [64]byte{}, fmt.Errorf("private key is not initialized")
}
signature := Sign(e.PrivateKey, payload, domPrefixPure, "")
return signature, nil // return sign
}
func (e *_EDDSA) Verify(payload []byte, sig []byte) bool { // need sign
if payload == nil || len(payload) == 0 {
return false
}
return Verify__(e.PublicKey, payload, sig)
}
//
// func (e *_EDDSA) VerifyToken(token *Token[*_EDDSA]) error {
// switch {
// case !token.isValid():
// return ErrTokenIsINVALID
// case !constTimeEqual(token.header.Algorithm.String(), EDDSA.String()):
// return ErrInvalid
// case !e.Verify(token.BeforeSignature(), token.Signature()):
// return ErrSignatureInvalid
// }
// return nil
// }
func GenerateEDDSARandom(rand io.Reader) (*PrivateKeyEd, *PublicKeyEd, error) {
if rand == nil {
rand = cryptorand.Reader
}
var seed [32]byte
if _, err := io.ReadFull(rand, seed[:]); err != nil {
return nil, nil, err
}
privateKey := NewKeyFromSeed(seed)
var public [32]byte
memcopy_avx2_32(unsafe.Pointer(&public[0]), unsafe.Pointer(&privateKey.key[32]))
return privateKey, NewPublicKey(&public), nil
}