-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsam.go
245 lines (223 loc) · 6.24 KB
/
rsam.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
package rsam
import (
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"errors"
"hash"
"os"
"github.com/gossl/rsam/cry"
)
// Generates a new key pair (private and public)
func GeneratePairKeys(bits int) (*rsa.PrivateKey, *rsa.PublicKey, error) {
privkey, err := rsa.GenerateKey(rand.Reader, bits)
if err != nil {
return nil, nil, err
}
return privkey, &privkey.PublicKey, nil
}
// Converts private key to bytes
func PrivateKeyToBytes(priv *rsa.PrivateKey) []byte {
privBytes := pem.EncodeToMemory(
&pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(priv),
},
)
return privBytes
}
// Converts public key to bytes
func PublicKeyToBytes(pub *rsa.PublicKey) []byte {
pubASN1, err := x509.MarshalPKIXPublicKey(pub)
if err != nil {
return nil
}
pubBytes := pem.EncodeToMemory(&pem.Block{
Type: "RSA PUBLIC KEY",
Bytes: pubASN1,
})
return pubBytes
}
// Converts bytes to private key
func BytesToPrivateKey(priv []byte) (*rsa.PrivateKey, error) {
block, _ := pem.Decode(priv)
enc := x509.IsEncryptedPEMBlock(block)
b := block.Bytes
var err error
if enc {
b, err = x509.DecryptPEMBlock(block, nil)
if err != nil {
return nil, err
}
}
key, err := x509.ParsePKCS1PrivateKey(b)
if err != nil {
return nil, err
}
return key, nil
}
// Converts bytes to public key
func BytesToPublicKey(pub []byte) (*rsa.PublicKey, error) {
block, _ := pem.Decode(pub)
enc := x509.IsEncryptedPEMBlock(block)
b := block.Bytes
var err error
if enc {
b, err = x509.DecryptPEMBlock(block, nil)
if err != nil {
return nil, err
}
}
ifc, err := x509.ParsePKIXPublicKey(b)
if err != nil {
return nil, err
}
key, ok := ifc.(*rsa.PublicKey)
if !ok {
return nil, err
}
return key, nil
}
// Encrypts data with public key
func EncryptWithPublicKey(msg []byte, pub *rsa.PublicKey, hash hash.Hash) ([]byte, error) {
var err error
var ciphertext, c []byte
chunkSize := pub.Size() - 2*hash.Size() - 2
if chunkSize < 1 {
return nil, errors.New("Invalid public key: public key is too short, pub.Size() - 2*hash.Size() - 2 must be greater than 0")
}
// TODO: GO ROUTINE HERE
for i := 0; i < len(msg); i += chunkSize {
if i+chunkSize > len(msg) {
c, err = rsa.EncryptOAEP(hash, rand.Reader, pub, msg[i:], nil)
} else {
c, err = rsa.EncryptOAEP(hash, rand.Reader, pub, msg[i:i+chunkSize], nil)
}
if err != nil {
return nil, err
}
ciphertext = append(ciphertext, c...)
}
return ciphertext, err
}
// Decrypts data with private key
func DecryptWithPrivateKey(ciphertext []byte, priv *rsa.PrivateKey, hash hash.Hash) ([]byte, error) {
var plaintext, p []byte
var err error
chunkSize := priv.Size()
// TODO: GO ROUTINE HERE
for i := 0; i < len(ciphertext); i += chunkSize {
if i+chunkSize > len(ciphertext) {
p, err = rsa.DecryptOAEP(hash, rand.Reader, priv, ciphertext[i:], nil)
} else {
p, err = rsa.DecryptOAEP(hash, rand.Reader, priv, ciphertext[i:i+chunkSize], nil)
}
if err != nil {
return nil, err
}
plaintext = append(plaintext, p...)
}
return plaintext, nil
}
// Encrypts data with public key
func EncryptWithPrivateKey(msg []byte, priv *rsa.PrivateKey, hash hash.Hash) ([]byte, error) {
var err error
var ciphertext, c []byte
chunkSize := priv.Size() - 2*hash.Size() - 2
if chunkSize < 1 {
return nil, errors.New("Invalid private key: private key is too short, pub.Size() - 2*hash.Size() - 2 must be greater than 0")
}
for i := 0; i < len(msg); i += chunkSize {
if i+chunkSize > len(msg) {
c, err = cry.EncryptOAEPM(hash, nil, priv, msg[i:], nil)
} else {
c, err = cry.EncryptOAEPM(hash, nil, priv, msg[i:i+chunkSize], nil)
}
if err != nil {
return nil, err
}
ciphertext = append(ciphertext, c...)
}
return ciphertext, err
}
// Decrypts data with private key
func DecryptWithPublicKey(ciphertext []byte, pub *rsa.PublicKey, hash hash.Hash) ([]byte, error) {
var plaintext, m []byte
chunkSize := pub.Size()
var err error
for i := 0; i < len(ciphertext); i += chunkSize {
if i+chunkSize > len(ciphertext) {
m, err = cry.DecryptOAEPM(hash, nil, pub, ciphertext[i:], nil)
} else {
m, err = cry.DecryptOAEPM(hash, nil, pub, ciphertext[i:i+chunkSize], nil)
}
if err != nil {
return nil, err
}
plaintext = append(plaintext, m...)
}
return plaintext, nil
}
// Decrypts data with private key
func SignWithPrivateKey(msg []byte, priv *rsa.PrivateKey, hashOpt ...crypto.Hash) ([]byte, error) {
hash := crypto.Hash(crypto.SHA256)
if len(hashOpt) > 0 {
hash = crypto.Hash(hashOpt[0])
}
hashed := sha256.Sum256(msg)
signature, err := rsa.SignPSS(rand.Reader, priv, hash, hashed[:], nil)
if err != nil {
return nil, err
}
return signature, nil
}
// Decrypts data with private key
func VerifyWithPublicKey(msg []byte, signature []byte, pub *rsa.PublicKey, hashOpt ...crypto.Hash) error {
hash := crypto.Hash(crypto.SHA256)
if len(hashOpt) > 0 {
hash = crypto.Hash(hashOpt[0])
}
hashed := sha256.Sum256(msg)
return rsa.VerifyPSS(pub, hash, hashed[:], signature, nil)
}
// Decrypts data with private key
func VerifyWithPrivateKey(msg []byte, signature []byte, priv *rsa.PrivateKey) error {
hash := crypto.Hash(crypto.SHA256)
hashed := sha256.Sum256(msg)
return cry.VerifyPSSByPrivate(priv, hash, hashed[:], signature, nil)
}
// Encrypts data with public key
func SignWithPublicKey(msg []byte, pub *rsa.PublicKey) ([]byte, error) {
hash := crypto.Hash(crypto.SHA256)
hashed := sha256.Sum256(msg)
return cry.SignPSSByPublic(nil, pub, hash, hashed[:], nil)
}
// Returns base64 encoded key from file
func B64EncodeKeyFile(path string) (string, error) {
bytes, err := os.ReadFile(path)
if err != nil {
return "", err
}
return B64EncodeKeyString(string(bytes)), nil
}
// Returns decoding of base64 encoded key from file
func B64DecodeKeyFile(path string) (string, error) {
bytes, err := os.ReadFile(path)
if err != nil {
return "", err
}
return B64DecodeKeyString(string(bytes))
}
// Returns base64 encoded public key from string
func B64EncodeKeyString(key string) string {
return base64.StdEncoding.EncodeToString([]byte(key))
}
// Returns base64 encoded public key from string
func B64DecodeKeyString(key string) (string, error) {
decoded, err := base64.StdEncoding.DecodeString(key)
return string(decoded), err
}