-
Notifications
You must be signed in to change notification settings - Fork 34
/
v2.go
173 lines (138 loc) · 4.56 KB
/
v2.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
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
package paseto
import (
"crypto"
"crypto/rand"
"io"
"golang.org/x/crypto/blake2b"
"golang.org/x/crypto/chacha20poly1305"
"golang.org/x/crypto/ed25519"
errors "golang.org/x/xerrors"
)
const (
v2SignSize = ed25519.SignatureSize
// XNonceSize is the size of the XChaCha20 nonce in bytes.
XNonceSize = 24
)
var headerV2 = []byte("v2.local.")
var headerV2Public = []byte("v2.public.")
// NewV2 returns a v2 implementation of PASETO tokens.
func NewV2() *V2 {
return &V2{}
}
// V2 is a v2 implementation of PASETO tokens
type V2 struct {
// this property is used for testing purposes only
nonce []byte
}
// Encrypt implements Protocol.Encrypt
func (p *V2) Encrypt(key []byte, payload, footer interface{}) (string, error) {
payloadBytes, err := infToByteArr(payload)
if err != nil {
return "", errors.Errorf("failed to encode payload to []byte: %w", err)
}
footerBytes, err := infToByteArr(footer)
if err != nil {
return "", errors.Errorf("failed to encode footer to []byte: %w", err)
}
var rndBytes []byte
if p.nonce != nil {
rndBytes = p.nonce
} else {
rndBytes = make([]byte, XNonceSize)
if _, err := io.ReadFull(rand.Reader, rndBytes); err != nil { //nolint:govet
return "", errors.Errorf("failed to read from rand.Reader: %w", err)
}
}
hash, err := blake2b.New(XNonceSize, rndBytes)
if err != nil {
return "", errors.Errorf("failed to create blake2b hash: %w", err)
}
if _, err := hash.Write(payloadBytes); err != nil { //nolint:govet
return "", errors.Errorf("failed to hash payload: %w", err)
}
nonce := hash.Sum(nil)
aead, err := chacha20poly1305.NewX(key)
if err != nil {
return "", errors.Errorf("failed to create chacha20poly1305 cipher: %w", err)
}
encryptedPayload := aead.Seal(payloadBytes[:0], nonce, payloadBytes, preAuthEncode(headerV2, nonce, footerBytes))
return createToken(headerV2, append(nonce, encryptedPayload...), footerBytes), nil
}
// Decrypt implements Protocol.Decrypt
func (*V2) Decrypt(token string, key []byte, payload, footer interface{}) error {
body, footerBytes, err := splitToken([]byte(token), headerV2)
if err != nil {
return errors.Errorf("failed to decode token: %w", err)
}
if len(body) < XNonceSize {
return errors.Errorf("incorrect token size: %w", ErrIncorrectTokenFormat)
}
nonce := body[:XNonceSize]
encryptedPayload := body[XNonceSize:]
aead, err := chacha20poly1305.NewX(key)
if err != nil {
return errors.Errorf("failed to create chacha20poly1305 cipher: %w", err)
}
decryptedPayload, err := aead.Open(encryptedPayload[:0], nonce, encryptedPayload, preAuthEncode(headerV2, nonce, footerBytes))
if err != nil {
return ErrInvalidTokenAuth
}
if payload != nil {
if err := fillValue(decryptedPayload, payload); err != nil {
return errors.Errorf("failed to decode payload: %w", err)
}
}
if footer != nil {
if err := fillValue(footerBytes, footer); err != nil {
return errors.Errorf("failed to decode footer: %w", err)
}
}
return nil
}
// Sign implements Protocol.Sign
func (*V2) Sign(privateKey crypto.PrivateKey, payload, footer interface{}) (string, error) {
key, ok := privateKey.(ed25519.PrivateKey)
if !ok {
return "", ErrIncorrectPrivateKeyType
}
payloadBytes, err := infToByteArr(payload)
if err != nil {
return "", errors.Errorf("failed to encode payload to []byte: %w", err)
}
footerBytes, err := infToByteArr(footer)
if err != nil {
return "", errors.Errorf("failed to encode footer to []byte: %w", err)
}
sig := ed25519.Sign(key, preAuthEncode(headerV2Public, payloadBytes, footerBytes))
return createToken(headerV2Public, append(payloadBytes, sig...), footerBytes), nil
}
// Verify implements Protocol.Verify
func (*V2) Verify(token string, publicKey crypto.PublicKey, payload, footer interface{}) error {
pub, ok := publicKey.(ed25519.PublicKey)
if !ok {
return ErrIncorrectPublicKeyType
}
data, footerBytes, err := splitToken([]byte(token), headerV2Public)
if err != nil {
return errors.Errorf("failed to decode token: %w", err)
}
if len(data) < v2SignSize {
return errors.Errorf("incorrect token size: %w", ErrIncorrectTokenFormat)
}
payloadBytes := data[:len(data)-v2SignSize]
signature := data[len(data)-v2SignSize:]
if !ed25519.Verify(pub, preAuthEncode(headerV2Public, payloadBytes, footerBytes), signature) {
return ErrInvalidSignature
}
if payload != nil {
if err := fillValue(payloadBytes, payload); err != nil {
return errors.Errorf("failed to decode payload: %w", err)
}
}
if footer != nil {
if err := fillValue(footerBytes, footer); err != nil {
return errors.Errorf("failed to decode footer: %w", err)
}
}
return nil
}