-
Notifications
You must be signed in to change notification settings - Fork 8
/
ecies.go
136 lines (116 loc) · 2.81 KB
/
ecies.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
package go_eccrypto
import (
"crypto/hmac"
"crypto/rand"
"crypto/sha256"
"crypto/sha512"
"encoding/hex"
"errors"
)
type EciesMetadata struct {
Iv string `json:"iv"`
EphemPublicKey string `json:"ephemPublicKey"`
Mac string `json:"mac"`
Mode string `json:"mode"`
}
const (
B = iota
KB int64 = 1 << (10 * iota)
MB
GB
TB
)
const (
ENCRYPTION_MODE_1 = "AES256"
)
func Encrypt(pubkeyHex string, plainbytes []byte) (string, *EciesMetadata, error) {
// make sure size of input bytes lt 2g
if plainbytes == nil || int64(len(plainbytes)) >= 2*GB {
return "", nil, errors.New("file exceeds the maximum file-size(lt 2G)")
}
// derive Share Secret
priv, err := GenerateKey()
if err != nil {
return "", nil, err
}
ephemPublicKey := priv.PublicKey.Bytes(false)
pk, err := NewPublicKeyFromHex(pubkeyHex)
if err != nil {
return "", nil, err
}
shareSecret, err := priv.ECDH(pk)
// generate iv
iv, err := randBytes(16)
if err != nil {
return "", nil, err
}
sha512hash := sha512.Sum512(shareSecret[1:])
encryptionKey := sha512hash[0:32]
macKey := sha512hash[32:]
ciphertext, err := aesCBCEnc(encryptionKey, plainbytes, iv)
if err != nil {
return "", nil, err
}
dataToMac := make([]byte, 0)
dataToMac = append(dataToMac, iv...)
dataToMac = append(dataToMac, ephemPublicKey...)
dataToMac = append(dataToMac, ciphertext...)
mac := getHmacCode(macKey, dataToMac)
metadata := &EciesMetadata{
Iv: hex.EncodeToString(iv),
EphemPublicKey: hex.EncodeToString(ephemPublicKey),
Mac: mac,
Mode: ENCRYPTION_MODE_1,
}
return hex.EncodeToString(ciphertext), metadata, err
}
func randBytes(bits int) ([]byte, error) {
key := make([]byte, bits)
_, err := rand.Read(key)
if err != nil {
return nil, err
}
return key, nil
}
func Decrypt(privateKeyHex string, cipherText string, t *EciesMetadata) (string, error) {
switch t.Mode {
case ENCRYPTION_MODE_1:
privKey, err := NewPrivateKeyFromHex(privateKeyHex)
if err != nil {
return "", err
}
epkHex := t.EphemPublicKey
epk, err := NewPublicKeyFromHex(epkHex)
if err != nil {
return "", err
}
// derive Share Secret
ecdh, err := privKey.ECDH(epk)
if err != nil {
return "", err
}
cipherBytes, err := hex.DecodeString(cipherText)
if err != nil {
return "", err
}
ivBytes, err := hex.DecodeString(t.Iv)
if err != nil {
return "", err
}
ecdhHash := sha512.Sum512(ecdh[1:])
encryptionKey := ecdhHash[:32]
plaintext, e := aesCBCDec(encryptionKey, cipherBytes, ivBytes)
if e != nil {
return "", err
}
return string(plaintext), nil
default:
return "", errors.New("encryption mode not support")
}
}
func getHmacCode(key []byte, data []byte) string {
h := hmac.New(sha256.New, key)
h.Write(data)
s := hex.EncodeToString(h.Sum(nil))
return s
}