This repository has been archived by the owner on Mar 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
crypto.go
252 lines (215 loc) · 6.12 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package main
import (
"crypto/aes"
"crypto/cipher"
"crypto/md5"
"crypto/rand"
"crypto/rsa"
"crypto/sha1"
"crypto/x509"
"encoding/base64"
"encoding/json"
"encoding/pem"
"fmt"
"io"
"io/ioutil"
"log"
"math/big"
"reflect"
"strings"
"code.google.com/p/go.crypto/ssh"
)
type Salter interface {
GenerateSalt() (string, error)
}
type RandomSaltGenerator struct {
}
type StaticSaltGenerator struct {
salt string
}
func (ssg *StaticSaltGenerator) GenerateSalt() (string, error) {
return ssg.salt, nil
}
func (sg *RandomSaltGenerator) GenerateSalt() (string, error) {
const SALT_LENGTH = 8
b := make([]byte, SALT_LENGTH)
_, err := rand.Read(b)
encoder := base64.StdEncoding
encoded := make([]byte, encoder.EncodedLen(len(b)))
encoder.Encode(encoded, b)
if err != nil {
return "", err
}
return string(encoded), nil
}
func sshPubkeyToRsaPubkey(pubkey ssh.PublicKey) rsa.PublicKey {
s := reflect.ValueOf(pubkey).Elem()
rsaKey := rsa.PublicKey{
N: s.Field(0).Interface().(*big.Int),
E: s.Field(1).Interface().(int),
}
return rsaKey
}
func rsaPubkeyToSSHPubkey(rsakey rsa.PublicKey) (sshkey ssh.PublicKey, err error) {
sshkey, err = ssh.NewPublicKey(&rsakey)
if err != nil {
return nil, err
}
return sshkey, nil
}
type AESEncryption struct {
EncodedKey string
Ciphertext string
}
func encodeAES(key []byte, plaintext string) (ciphertext string, err error) {
cipherBlock, err := aes.NewCipher(key)
if err != nil {
return "", err
}
// We need an unique IV to go at the front of the ciphertext
out := make([]byte, aes.BlockSize+len(plaintext))
iv := out[:aes.BlockSize]
_, err = io.ReadFull(rand.Reader, iv)
if err != nil {
return "", err
}
stream := cipher.NewCFBEncrypter(cipherBlock, iv)
stream.XORKeyStream(out[aes.BlockSize:], []byte(plaintext))
encoded := base64.StdEncoding.EncodeToString(out)
return encoded, nil
}
// takes a base64-encoded AES-encrypted ciphertext
func decodeAES(key []byte, ciphertext string) (string, error) {
encrypted, err := base64.StdEncoding.DecodeString(ciphertext)
if err != nil {
return "", err
}
decrypter, err := aes.NewCipher(key)
if err != nil {
return "", err
}
iv := encrypted[:aes.BlockSize]
msg := encrypted[aes.BlockSize:]
aesDecrypter := cipher.NewCFBDecrypter(decrypter, iv)
aesDecrypter.XORKeyStream(msg, msg)
return string(msg), nil
}
// returns a base64 encoded ciphertext.
// OAEP can only encrypt plaintexts that are smaller than the key length; for
// a 1024-bit key, about 117 bytes. So instead, this function:
// * generates a random 32-byte symmetric key (randKey)
// * encrypts the plaintext with AES256 using that random symmetric key -> cipherText
// * encrypts the random symmetric key with the ssh PublicKey -> cipherKey
// * returns the base64-encoded marshalled JSON for the ciphertext and key
func CredulousEncode(plaintext string, pubkey ssh.PublicKey) (ciphertext string, err error) {
rsaKey := sshPubkeyToRsaPubkey(pubkey)
randKey := make([]byte, 32)
_, err = rand.Read(randKey)
if err != nil {
return "", err
}
encoded, err := encodeAES(randKey, plaintext)
if err != nil {
return "", err
}
out, err := rsa.EncryptOAEP(sha1.New(), rand.Reader, &rsaKey, []byte(randKey), []byte("Credulous"))
if err != nil {
return "", err
}
cipherKey := base64.StdEncoding.EncodeToString(out)
cipherStruct := AESEncryption{
EncodedKey: cipherKey,
Ciphertext: encoded,
}
tmp, err := json.Marshal(cipherStruct)
if err != nil {
return "", err
}
ciphertext = base64.StdEncoding.EncodeToString(tmp)
return ciphertext, nil
}
func CredulousDecodeAES(ciphertext string, privkey *rsa.PrivateKey) (plaintext string, err error) {
in, err := base64.StdEncoding.DecodeString(ciphertext)
if err != nil {
return "", err
}
// pull apart the layers of base64-encoded JSON
var encrypted AESEncryption
err = json.Unmarshal(in, &encrypted)
if err != nil {
return "", err
}
encryptedKey, err := base64.StdEncoding.DecodeString(encrypted.EncodedKey)
if err != nil {
return "", err
}
// decrypt the AES key using the ssh private key
aesKey, err := rsa.DecryptOAEP(sha1.New(), rand.Reader, privkey, encryptedKey, []byte("Credulous"))
if err != nil {
return "", err
}
plaintext, err = decodeAES(aesKey, encrypted.Ciphertext)
return plaintext, nil
}
func CredulousDecodePureRSA(ciphertext string, privkey *rsa.PrivateKey) (plaintext string, err error) {
in, err := base64.StdEncoding.DecodeString(ciphertext)
if err != nil {
return "", err
}
out, err := rsa.DecryptOAEP(sha1.New(), rand.Reader, privkey, in, []byte("Credulous"))
if err != nil {
return "", err
}
plaintext = string(out)
return plaintext, nil
}
func CredulousDecodeWithSalt(ciphertext string, salt string, privkey *rsa.PrivateKey) (plaintext string, err error) {
in, err := base64.StdEncoding.DecodeString(ciphertext)
if err != nil {
return "", err
}
out, err := rsa.DecryptOAEP(sha1.New(), rand.Reader, privkey, in, []byte("Credulous"))
if err != nil {
return "", err
}
plaintext = strings.Replace(string(out), salt, "", 1)
return plaintext, nil
}
func loadPrivateKey(filename string) (privateKey *rsa.PrivateKey, err error) {
var tmp []byte
if tmp, err = ioutil.ReadFile(filename); err != nil {
return &rsa.PrivateKey{}, err
}
pemblock, _ := pem.Decode([]byte(tmp))
if x509.IsEncryptedPEMBlock(pemblock) {
if tmp, err = decryptPEM(pemblock, filename); err != nil {
return &rsa.PrivateKey{}, err
}
} else {
log.Print("WARNING: Your private SSH key has no passphrase!")
}
key, err := ssh.ParseRawPrivateKey(tmp)
if err != nil {
return &rsa.PrivateKey{}, err
}
privateKey = key.(*rsa.PrivateKey)
return privateKey, nil
}
func SSHFingerprint(pubkey ssh.PublicKey) (fingerprint string) {
binary := pubkey.Marshal()
hash := md5.Sum(binary)
// now add the colons
fingerprint = fmt.Sprintf("%02x", (hash[0]))
for i := 1; i < len(hash); i += 1 {
fingerprint += ":" + fmt.Sprintf("%02x", (hash[i]))
}
return fingerprint
}
func SSHPrivateFingerprint(privkey rsa.PrivateKey) (fingerprint string, err error) {
sshPubkey, err := rsaPubkeyToSSHPubkey(privkey.PublicKey)
if err != nil {
return "", err
}
fingerprint = SSHFingerprint(sshPubkey)
return fingerprint, nil
}