-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsa.go
172 lines (155 loc) · 4.42 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
// Package secrets provides RSA codec to work with Calyptia Cloud pipeline secrets.
package secrets
import (
"crypto/rand"
"crypto/rsa"
"crypto/sha512"
"crypto/x509"
"encoding/pem"
"fmt"
"hash"
)
const (
DefaultEncryptionBits = 2048
blockTYpeRSAPublicKey = "RSA PUBLIC KEY"
blockTypeRSAPrivateKey = "RSA PRIVATE KEY"
)
type RSA struct {
Bits int
}
func NewRSAHandler(bits int) *RSA {
if bits == 0 {
bits = DefaultEncryptionBits
}
return &RSA{Bits: bits}
}
func (r *RSA) EncryptWithPublicKey(msg []byte, key []byte) ([]byte, error) {
publicKey, err := BytesToPublicKey(key)
if err != nil {
return nil, err
}
h := sha512.New()
ciphertext, err := Encrypt(h, publicKey, msg)
if err != nil {
return nil, err
}
return ciphertext, nil
}
// Encrypt encrypts the given message using RSA-OAEP with the given public key.
//
// The message is split into chunks of size k - 2*hLen - 2, where k is the size of the public key in bytes and hLen is the length of the hash function used.
// Each chunk is encrypted separately and the resulting ciphertexts are concatenated.
//
// At the end it returns the ciphertext encrypted.
func Encrypt(hash hash.Hash, publicKey *rsa.PublicKey, msg []byte) ([]byte, error) {
k := publicKey.Size()
limit := k - 2*hash.Size() - 2
chunks := splitByteBySizeLimit(msg, limit)
var cipherText []byte
for _, chunk := range chunks {
ciphertextChunk, err := rsa.EncryptOAEP(hash, rand.Reader, publicKey, chunk, nil)
if err != nil {
return nil, err
}
cipherText = append(cipherText, ciphertextChunk...)
}
return cipherText, nil
}
// splitByteBySizeLimit splits the given byte array into chunks of size limit.
// If the given byte array is smaller than the limit, it returns a slice with the given byte array.
func splitByteBySizeLimit(buf []byte, sizeLimit int) [][]byte {
if len(buf) <= sizeLimit {
return [][]byte{buf}
}
var chunk []byte
chunks := make([][]byte, 0, len(buf)/sizeLimit+1)
for len(buf) >= sizeLimit {
chunk, buf = buf[:sizeLimit], buf[sizeLimit:]
chunks = append(chunks, chunk)
}
if len(buf) > 0 {
chunks = append(chunks, buf)
}
return chunks
}
// Decrypt decrypts the given ciphertext using RSA-OAEP with the given private key.
//
// The ciphertext is split into chunks of size k, where k is the size of the private key in bytes.
// Each chunk is decrypted separately and the resulting plaintexts are concatenated.
//
// At the end it returns the plaintext decrypted.
func Decrypt(hash hash.Hash, privateKey *rsa.PrivateKey, msg []byte) ([]byte, error) {
limit := privateKey.Size()
chunks := splitByteBySizeLimit(msg, limit)
var plaintext []byte
for _, chunk := range chunks {
plaintextChunk, err := rsa.DecryptOAEP(hash, rand.Reader, privateKey, chunk, nil)
if err != nil {
return nil, err
}
plaintext = append(plaintext, plaintextChunk...)
}
return plaintext, nil
}
func (r *RSA) DecryptWithPrivateKey(msg []byte, key []byte) ([]byte, error) {
privateKey, err := BytesToPrivateKey(key)
if err != nil {
return nil, err
}
h := sha512.New()
plaintext, err := Decrypt(h, privateKey, msg)
if err != nil {
return nil, err
}
return plaintext, nil
}
func (r *RSA) GenerateKeyPair() ([]byte, []byte, error) {
privateKey, err := rsa.GenerateKey(rand.Reader, r.Bits)
if err != nil {
return nil, nil, err
}
publicKeyBytes, err := PublicKeyToBytes(&privateKey.PublicKey)
if err != nil {
return nil, nil, err
}
return PrivateKeyToBytes(privateKey), publicKeyBytes, nil
}
func PrivateKeyToBytes(priv *rsa.PrivateKey) []byte {
return pem.EncodeToMemory(
&pem.Block{
Type: blockTypeRSAPrivateKey,
Bytes: x509.MarshalPKCS1PrivateKey(priv),
},
)
}
func PublicKeyToBytes(pub *rsa.PublicKey) ([]byte, error) {
pubASN1, err := x509.MarshalPKIXPublicKey(pub)
if err != nil {
return nil, err
}
pubBytes := pem.EncodeToMemory(&pem.Block{
Type: blockTYpeRSAPublicKey,
Bytes: pubASN1,
})
return pubBytes, nil
}
func BytesToPrivateKey(privateKey []byte) (*rsa.PrivateKey, error) {
block, _ := pem.Decode(privateKey)
key, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return nil, err
}
return key, nil
}
func BytesToPublicKey(publicKey []byte) (*rsa.PublicKey, error) {
block, _ := pem.Decode(publicKey)
publicKeyInterface, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return nil, err
}
key, ok := publicKeyInterface.(*rsa.PublicKey)
if !ok {
return nil, fmt.Errorf("cannot decode bytes to public key")
}
return key, nil
}