-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrsa.go
190 lines (174 loc) · 4.8 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
package rencrypto
import (
"bytes"
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"encoding/binary"
"fmt"
"hash"
"math/big"
)
type rsaEncrypter struct {
*rsa.PublicKey
}
func NewRSAEncrypter(key interface{}) (Encrypter, error) {
var pubKey *rsa.PublicKey
switch key := key.(type) {
case *rsa.PrivateKey:
pubKey = &key.PublicKey
case rsa.PrivateKey:
pubKey = &key.PublicKey
case *rsa.PublicKey:
pubKey = key
case rsa.PublicKey:
pubKey = &key
case []byte:
publicKey, err := unmarshalRSAPubKey(key)
if err != nil {
return nil, err
}
pubKey = &publicKey
default:
return nil, fmt.Errorf("unknown type: %T cannot convert into an RSA encrypter", key)
}
return &rsaEncrypter{
PublicKey: pubKey,
}, nil
}
func (encrypter *rsaEncrypter) Marshal() ([]byte, error) {
return marshalRSAPubKey(encrypter.PublicKey)
}
func (encrypter *rsaEncrypter) Encrypt(data []byte) ([]byte, error) {
return rsa.EncryptOAEP(sha256.New(), rand.Reader, encrypter.PublicKey, data, nil)
}
func (encrypter *rsaEncrypter) Hash(hash hash.Hash) ([]byte, error) {
data, err := encrypter.Marshal()
if err != nil {
return nil, err
}
return hash.Sum(data)[len(data):], nil
}
func (encrypter *rsaEncrypter) Public() crypto.PublicKey {
return encrypter.PublicKey
}
func marshalRSAPubKey(publicKey *rsa.PublicKey) ([]byte, error) {
buf := new(bytes.Buffer)
if err := binary.Write(buf, binary.BigEndian, int64(publicKey.E)); err != nil {
return []byte{}, err
}
if err := binary.Write(buf, binary.BigEndian, publicKey.N.Bytes()); err != nil {
return []byte{}, err
}
return buf.Bytes(), nil
}
func unmarshalRSAPubKey(pubKeyBytes []byte) (rsa.PublicKey, error) {
var E int64
if err := binary.Read(bytes.NewReader(pubKeyBytes[:8]), binary.BigEndian, &E); err != nil {
return rsa.PublicKey{}, err
}
N := new(big.Int).SetBytes(pubKeyBytes[8:])
return rsa.PublicKey{
N: N,
E: int(E),
}, nil
}
type rsaDecrypter struct {
*rsa.PrivateKey
}
func NewRSADecrypter(key interface{}) (Decrypter, error) {
var privKey *rsa.PrivateKey
switch key := key.(type) {
case *rsa.PrivateKey:
privKey = key
case rsa.PrivateKey:
privKey = &key
case []byte:
privateKey, err := unmarshalRSAPrivKey(key)
if err != nil {
return nil, err
}
privKey = &privateKey
default:
return nil, fmt.Errorf("unknown type: %T cannot convert into an RSA decrypter", key)
}
return &rsaDecrypter{
PrivateKey: privKey,
}, nil
}
func (decrypter *rsaDecrypter) Marshal() ([]byte, error) {
return marshalRSAPrivKey(decrypter.PrivateKey)
}
func (decrypter *rsaDecrypter) Decrypt(data []byte) ([]byte, error) {
return rsa.DecryptOAEP(sha256.New(), rand.Reader, decrypter.PrivateKey, data, nil)
}
func (decrypter *rsaDecrypter) Encrypter() Encrypter {
return &rsaEncrypter{
PublicKey: &decrypter.PublicKey,
}
}
func marshalRSAPrivKey(privateKey *rsa.PrivateKey) ([]byte, error) {
buf := new(bytes.Buffer)
primeCount := int64(len(privateKey.Primes))
if err := binary.Write(buf, binary.BigEndian, primeCount); err != nil {
return nil, err
}
for _, prime := range privateKey.Primes {
primeBytes := prime.Bytes()
primeSize := int64(len(primeBytes))
if err := binary.Write(buf, binary.BigEndian, primeSize); err != nil {
return []byte{}, err
}
if err := binary.Write(buf, binary.BigEndian, primeBytes); err != nil {
return []byte{}, err
}
}
dBytes := privateKey.D.Bytes()
dSize := int64(len(dBytes))
if err := binary.Write(buf, binary.BigEndian, dSize); err != nil {
return []byte{}, err
}
if err := binary.Write(buf, binary.BigEndian, dBytes); err != nil {
return []byte{}, err
}
pubKeyBytes, err := marshalRSAPubKey(&privateKey.PublicKey)
if err != nil {
return []byte{}, err
}
if err := binary.Write(buf, binary.BigEndian, pubKeyBytes); err != nil {
return []byte{}, err
}
return buf.Bytes(), nil
}
func unmarshalRSAPrivKey(privKeyBytes []byte) (rsa.PrivateKey, error) {
var primeCount int64
if err := binary.Read(bytes.NewReader(privKeyBytes[:8]), binary.BigEndian, &primeCount); err != nil {
return rsa.PrivateKey{}, err
}
privKeyBytes = privKeyBytes[8:]
primes := []*big.Int{}
for i := int64(0); i < primeCount; i++ {
var primeSize int64
if err := binary.Read(bytes.NewReader(privKeyBytes[:8]), binary.BigEndian, &primeSize); err != nil {
return rsa.PrivateKey{}, err
}
prime := new(big.Int).SetBytes(privKeyBytes[8 : primeSize+8])
primes = append(primes, prime)
privKeyBytes = privKeyBytes[primeSize+8:]
}
var dSize int64
if err := binary.Read(bytes.NewReader(privKeyBytes[:8]), binary.BigEndian, &dSize); err != nil {
return rsa.PrivateKey{}, err
}
D := new(big.Int).SetBytes(privKeyBytes[8 : dSize+8])
pubKey, err := unmarshalRSAPubKey(privKeyBytes[dSize+8:])
if err != nil {
return rsa.PrivateKey{}, err
}
return rsa.PrivateKey{
PublicKey: pubKey,
D: D,
Primes: primes,
}, nil
}