forked from pydio/cells
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypto.go
233 lines (199 loc) · 5.13 KB
/
crypto.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
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
/*
* Copyright (c) 2018. Abstrium SAS <team (at) pydio.com>
* This file is part of Pydio Cells.
*
* Pydio Cells is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio Cells is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio Cells. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <https://pydio.com>.
*/
// Package crypto provides tools for data encryption and certificates management
package crypto
import (
"bytes"
"crypto"
"crypto/aes"
"crypto/cipher"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/md5"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"encoding/base64"
"fmt"
"io"
"math/big"
"strings"
"golang.org/x/crypto/pbkdf2"
)
var SALT = []byte{224, 32, 00, 33, 78, 3, 25, 56, 54, 5, 54, 9, 79, 76, 189, 8}
var keyPairAlgs = map[string]elliptic.Curve{
"p224": elliptic.P224(),
"p256": elliptic.P256(),
"p384": elliptic.P384(),
"p521": elliptic.P521(),
"": elliptic.P521(),
}
func RandomBytes(size int) ([]byte, error) {
key := make([]byte, size)
_, err := rand.Read(key)
return key, err
}
func KeyFromPassword(password []byte, l int) []byte {
if len(password) < l {
password, _ = pkcs7Pad(password, l)
}
return pbkdf2.Key(password, SALT, 50000, l, sha256.New)
}
func Seal(key []byte, data []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
nonce := make([]byte, 12)
if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
return nil, err
}
aesgcm, err := cipher.NewGCM(block)
if err != nil {
return nil, err
}
cipherData := aesgcm.Seal(nil, nonce, data, nil)
return append(nonce, cipherData...), nil
}
func SealWithNonce(key []byte, nonce []byte, data []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
aesgcm, err := cipher.NewGCM(block)
if err != nil {
return nil, err
}
return aesgcm.Seal(nil, nonce, data, nil), nil
}
func Open(key []byte, nonce []byte, cipherData []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
aesgcm, err := cipher.NewGCM(block)
if err != nil {
return nil, err
}
return aesgcm.Open(nil, nonce, cipherData, nil)
}
func NewEcdsaPrivateKey(alg string) (*ecdsa.PrivateKey, error) {
curve, ok := keyPairAlgs[alg]
if !ok {
curve = elliptic.P521()
}
return ecdsa.GenerateKey(curve, rand.Reader)
}
func EncodePrivate(password []byte, key crypto.PrivateKey) ([]byte, error) {
var (
bytes []byte
err error
)
switch k := key.(type) {
case *rsa.PrivateKey:
bytes = x509.MarshalPKCS1PrivateKey(k)
case *ecdsa.PrivateKey:
bytes, err = x509.MarshalECPrivateKey(k)
default:
return nil, nil
}
if err != nil {
return nil, err
}
pk := KeyFromPassword(password, 32)
return Seal(pk, bytes)
}
func ParsePrivate(password []byte, bytes []byte) (crypto.PrivateKey, error) {
var err error
bytes, err = Open(password, bytes[:12], bytes[12:])
if err != nil {
return nil, err
}
return x509.ParsePKCS8PrivateKey(bytes)
}
func GetSignature(key *ecdsa.PrivateKey, data []byte) (string, error) {
h := sha256.New()
if _, err := h.Write(data); err != nil {
return "", err
}
hashed := h.Sum(nil)
r, s, err := ecdsa.Sign(rand.Reader, key, hashed)
if err != nil {
return "", err
}
enc := base64.StdEncoding
return enc.EncodeToString(r.Bytes()) + ":" + enc.EncodeToString(s.Bytes()), nil
}
func VerifySignature(data []byte, key *ecdsa.PublicKey, signature string) bool {
h := sha256.New()
if _, err := h.Write(data); err != nil {
return false
}
parts := strings.Split(signature, ":")
if len(parts) != 2 {
return false
}
enc := base64.StdEncoding
r := &big.Int{}
rb, err := enc.DecodeString(parts[0])
if err != nil {
return false
}
r.SetBytes(rb)
s := &big.Int{}
sb, err := enc.DecodeString(parts[1])
if err != nil {
return false
}
s.SetBytes(sb)
hashed := h.Sum(nil)
return ecdsa.Verify(key, hashed, r, s)
}
func pkcs7Pad(data []byte, blocklen int) ([]byte, error) {
padlen := 1
for ((len(data) + padlen) % blocklen) != 0 {
padlen = padlen + 1
}
pad := bytes.Repeat([]byte{byte(padlen)}, padlen)
return append(data, pad...), nil
}
func pkcs7Unpad(data []byte, blocklen int) ([]byte, error) {
if len(data)%blocklen != 0 || len(data) == 0 {
return nil, fmt.Errorf("invalid data len %d", len(data))
}
padlen := int(data[len(data)-1])
if padlen > blocklen || padlen == 0 {
return nil, fmt.Errorf("invalid padding")
}
// check padding
pad := data[len(data)-padlen:]
for i := 0; i < padlen; i++ {
if pad[i] != byte(padlen) {
return nil, fmt.Errorf("invalid padding")
}
}
return data[:len(data)-padlen], nil
}
func Md5(data []byte) []byte {
h := md5.New()
h.Write(data)
return h.Sum(nil)
}