-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrsa.go
434 lines (372 loc) · 11.3 KB
/
rsa.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
package crypto
import (
"bytes"
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"errors"
"os"
"path/filepath"
"strings"
)
const (
DefaultDotKeys = ".keys"
DefaultIDRsa = "id_rsa"
DefaultIDRsaPub = "id_rsa.pub"
DefaultKeySize = 1024
)
// Config stores key pair file path and key size.
type Config struct {
DotKeys string // DotKeys represents the parent dir of IDRsa and IDRsaPub
IDRsa string // IDRsa represents private key file
IDRsaPub string // IDRsaPub represents public key file
KeySize int // KeySize represents the bit size of key pair
}
// NewDefaultConfig return Config with default params.
func NewDefaultConfig() *Config {
return &Config{
DotKeys: DefaultDotKeys,
IDRsa: filepath.Join(DefaultDotKeys, DefaultIDRsa),
IDRsaPub: filepath.Join(DefaultDotKeys, DefaultIDRsaPub),
KeySize: DefaultKeySize,
}
}
// NewConfig return a Config object with user defined params.
func NewConfig(dotKeys, idRsa, idRsaPub string, keySize int) *Config {
return &Config{
DotKeys: dotKeys,
IDRsa: filepath.Join(dotKeys, idRsa),
IDRsaPub: filepath.Join(dotKeys, idRsaPub),
KeySize: keySize,
}
}
// GenerateKeyPair generates a pair of rsa key pair with the given key size defined in Config.
func (c *Config) GenerateKeyPair() (*rsa.PrivateKey, error) {
// Generate key pair
privateKey, err := generateKeyPair(c.KeySize)
if err != nil {
return nil, err
}
return privateKey, nil
}
// KeyPair stores key pair object
type KeyPair struct {
publicKey *rsa.PublicKey
privateKey *rsa.PrivateKey
}
// SaveKeyPair save key pair to local path defined in Config.
func (c *Config) SaveKeyPair(keyPair *rsa.PrivateKey) error {
// Make parent dir
if err := os.MkdirAll(c.DotKeys, os.ModePerm); err != nil {
return err
}
// Save private key
if err := saveIDRsa(c.IDRsa, keyPair); err != nil {
return err
}
// Save public key
if err := saveIDRsaPub(c.IDRsaPub, keyPair); err != nil {
return err
}
return nil
}
// LoadPublicKey return a KeyPair object contains public key from Config.IDRsaPub
func (c *Config) LoadPublicKey() (*KeyPair, error) {
publicKey, err := getIDRsaPub(c.IDRsaPub)
if err != nil {
return nil, err
}
return &KeyPair{publicKey: publicKey}, nil
}
// LoadPrivateKey return a KeyPair object contains private key from Config.IDRsa
func (c *Config) LoadPrivateKey() (*KeyPair, error) {
privateKey, err := getIDRsa(c.IDRsa)
if err != nil {
return nil, err
}
return &KeyPair{privateKey: privateKey}, nil
}
// EncryptPKCS1v15 encrypts the given message with RSA and the padding scheme from PKCS #1 v1.5
func (p *KeyPair) EncryptPKCS1v15(plainText string) (string, error) {
cipherText, err := encryptPKCS1v15(plainText, p.publicKey)
if err != nil {
return "", err
}
return cipherText, nil
}
// DecryptPKCS1v15 decrypts a plaintext using RSA and the padding scheme from PKCS #1 v1.5.
func (p *KeyPair) DecryptPKCS1v15(cipherText string) (string, error) {
plainText, err := decryptPKCS1v15(cipherText, p.privateKey)
if err != nil {
return "", err
}
return plainText, nil
}
// EncryptOAEP encrypts the given message with RSA-OAEP.
func (p *KeyPair) EncryptOAEP(plainText string) (string, error) {
cipherText, err := encryptOAEP(plainText, p.publicKey)
if err != nil {
return "", err
}
return cipherText, nil
}
// DecryptOAEP decrypts ciphertext using RSA-OAEP.
func (p *KeyPair) DecryptOAEP(cipherText string) (string, error) {
plainText, err := decryptOAEP(cipherText, p.privateKey)
if err != nil {
return "", err
}
return plainText, nil
}
// SignPKCS1v15 calculates the signature of hashed using RSASSA-PKCS1-V1_5-SIGN from RSA PKCS #1 v1.5.
func (p *KeyPair) SignPKCS1v15(payload string) (string, error) {
signature, err := signPKCS1v15(payload, p.privateKey)
if err != nil {
return "", err
}
return signature, nil
}
// VerifyPKCS1v15 verifies an RSA PKCS #1 v1.5 signature.
func (p *KeyPair) VerifyPKCS1v15(payload, signature64 string) error {
return verifyPKCS1v15(payload, signature64, p.publicKey)
}
// SignPSS calculates the signature of digest using PSS.
func (p *KeyPair) SignPSS(payload string) (string, error) {
signature, err := signPSS(payload, p.privateKey)
if err != nil {
return "", err
}
return signature, nil
}
// VerifyPSS verifies a PSS signature.
func (p *KeyPair) VerifyPSS(payload, signature64 string) error {
return verifyPSS(payload, signature64, p.publicKey)
}
// generateKeyPair generates an RSA keypair of the given bit size.
func generateKeyPair(keySize int) (*rsa.PrivateKey, error) {
// Generate key pair
keyPair, err := rsa.GenerateKey(rand.Reader, keySize)
if err != nil {
return nil, err
}
// Validate key
err = keyPair.Validate()
if err != nil {
return nil, err
}
return keyPair, nil
}
// saveIDRsa save private key to filename.
func saveIDRsa(fileName string, keyPair *rsa.PrivateKey) error {
// Private key stream
privateKeyBlock := &pem.Block{
Type: "PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(keyPair),
}
// Create file
f, err := os.Create(fileName)
if err != nil {
return err
}
return pem.Encode(f, privateKeyBlock)
}
// saveIDRsaPub save public key to filename.
func saveIDRsaPub(fileName string, keyPair *rsa.PrivateKey) error {
// Public key stream
pubKeyBytes, err := x509.MarshalPKIXPublicKey(&keyPair.PublicKey)
if err != nil {
return err
}
publicKeyBlock := &pem.Block{
Type: "PUBLIC KEY",
Bytes: pubKeyBytes,
}
// Create file
f, err := os.Create(fileName)
if err != nil {
return err
}
return pem.Encode(f, publicKeyBlock)
}
// getIDRsaPub get public key from filename.
func getIDRsaPub(filename string) (*rsa.PublicKey, error) {
keyData, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
keyBlock, _ := pem.Decode(keyData)
if keyBlock == nil {
return nil, errors.New("ERROR: fail get public key, invalid key")
}
publicKey, err := x509.ParsePKIXPublicKey(keyBlock.Bytes)
if err != nil {
return nil, err
}
switch key := publicKey.(type) {
case *rsa.PublicKey:
return key, nil
default:
return nil, nil
}
}
// getIDRsa get private key from filename.
func getIDRsa(filename string) (*rsa.PrivateKey, error) {
keyData, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
keyBlock, _ := pem.Decode(keyData)
if keyBlock == nil {
return nil, errors.New("ERROR: fail get rsa private key, invalid key")
}
privateKey, err := x509.ParsePKCS1PrivateKey(keyBlock.Bytes)
if err != nil {
return nil, err
}
return privateKey, nil
}
// encryptPKCS1v15 the given message with RSA and the padding scheme from PKCS #1 v1.5.
func encryptPKCS1v15(plainText string, key *rsa.PublicKey) (string, error) {
partLen := key.Size() - 11
chunks := split([]byte(plainText), partLen)
buffer := bytes.NewBufferString("")
for _, chunk := range chunks {
encrypted, err := rsa.EncryptPKCS1v15(rand.Reader, key, chunk)
if err != nil {
return "", err
}
buffer.Write(encrypted)
}
return base64.RawURLEncoding.EncodeToString(buffer.Bytes()), nil
}
// decryptPKCS1v15 the cipher text using RSA and the padding scheme from PKCS #1 v1.5.
func decryptPKCS1v15(cipherText string, key *rsa.PrivateKey) (string, error) {
partLen := key.Size()
raw, err := base64.RawURLEncoding.DecodeString(cipherText)
chunks := split(raw, partLen)
buffer := bytes.NewBufferString("")
for _, chunk := range chunks {
decrypted, err := rsa.DecryptPKCS1v15(rand.Reader, key, chunk)
if err != nil {
return "", err
}
buffer.Write(decrypted)
}
return buffer.String(), err
}
// encryptOAEP encrypts the given message with RSA-OAEP.
func encryptOAEP(plainText string, key *rsa.PublicKey) (string, error) {
// Params
rnd := rand.Reader
hash := sha256.New()
maxSize := key.Size() - 2*hash.Size() - 2 // from rsa.go
chunks := split([]byte(plainText), maxSize)
buffer := bytes.NewBufferString("")
for _, chunk := range chunks {
// Encrypt with OAEP
encrypted, err := rsa.EncryptOAEP(hash, rnd, key, chunk, nil)
if err != nil {
return "", err
}
buffer.Write(encrypted)
}
return base64.RawURLEncoding.EncodeToString(buffer.Bytes()), nil
}
// decryptOAEP decrypts cipher text using RSA-OAEP.
func decryptOAEP(cipherText string, key *rsa.PrivateKey) (string, error) {
// Params
rnd := rand.Reader
hash := sha256.New()
partLen := key.Size()
raw, err := base64.RawURLEncoding.DecodeString(cipherText)
chunks := split(raw, partLen)
buffer := bytes.NewBufferString("")
for _, chunk := range chunks {
// Decrypt with OAEP
decrypted, err := rsa.DecryptOAEP(hash, rnd, key, chunk, nil)
if err != nil {
return "", err
}
buffer.Write(decrypted)
}
return buffer.String(), err
}
// signPKCS1v15 calculates the signature of hashed using RSASSA-PKCS1-V1_5-SIGN from RSA PKCS #1 v1.5.
func signPKCS1v15(payload string, key *rsa.PrivateKey) (string, error) {
// Remove unwanted characters and get sha256 hash of the payload
replacer := strings.NewReplacer("\n", "", "\r", "", " ", "")
msg := strings.TrimSpace(strings.ToLower(replacer.Replace(payload)))
hashed := sha256.Sum256([]byte(msg))
// Sign the hashed payload
signature, err := rsa.SignPKCS1v15(rand.Reader, key, crypto.SHA256, hashed[:])
if err != nil {
return "", err
}
// Return base64 encoded string
return base64.StdEncoding.EncodeToString(signature), nil
}
// verifyPKCS1v15 verifies an RSA PKCS #1 v1.5 signature.
func verifyPKCS1v15(payload string, signature64 string, key *rsa.PublicKey) error {
// Decode base64 encoded signature
signature, err := base64.StdEncoding.DecodeString(signature64)
if err != nil {
return err
}
// Remove unwanted characters and get sha256 hash of the payload
replacer := strings.NewReplacer("\n", "", "\r", "", " ", "")
msg := strings.TrimSpace(strings.ToLower(replacer.Replace(payload)))
hashed := sha256.Sum256([]byte(msg))
return rsa.VerifyPKCS1v15(key, crypto.SHA256, hashed[:], signature)
}
// signPSS calculates the signature of digest using PSS.
func signPSS(payload string, key *rsa.PrivateKey) (string, error) {
// Remove unwanted characters and get sha256 hash of the payload
replacer := strings.NewReplacer("\n", "", "\r", "", " ", "")
msg := strings.TrimSpace(strings.ToLower(replacer.Replace(payload)))
// Prepare params
hash := crypto.SHA256
h := hash.New()
h.Write([]byte(msg))
hashed := h.Sum(nil)
// Sign the hashed payload
signature, err := rsa.SignPSS(rand.Reader, key, hash, hashed, nil)
if err != nil {
return "", err
}
// Return base64 encoded string
return base64.StdEncoding.EncodeToString(signature), nil
}
// verifyPSS verifies a PSS signature.
func verifyPSS(payload, signature64 string, key *rsa.PublicKey) error {
// Decode base64 encoded signature
signature, err := base64.StdEncoding.DecodeString(signature64)
if err != nil {
return err
}
// Remove unwanted characters and get sha256 hash of the payload
replacer := strings.NewReplacer("\n", "", "\r", "", " ", "")
msg := strings.TrimSpace(strings.ToLower(replacer.Replace(payload)))
// Prepare params
hash := crypto.SHA256
h := hash.New()
h.Write([]byte(msg))
hashed := h.Sum(nil)
return rsa.VerifyPSS(key, hash, hashed, signature, nil)
}
// split divides buf into limit size.
func split(buf []byte, limit int) [][]byte {
var chunk []byte
chunks := make([][]byte, 0, len(buf)/limit+1)
for len(buf) >= limit {
chunk, buf = buf[:limit], buf[limit:]
chunks = append(chunks, chunk)
}
if len(buf) > 0 {
chunks = append(chunks, buf[:])
}
return chunks
}