forked from scorum/bitshares-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
memo.go
182 lines (143 loc) · 4.13 KB
/
memo.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
package types
//go:generate ffjson $GOFILE
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/sha256"
"crypto/sha512"
"encoding/hex"
"strconv"
"github.com/youthonline/bitshares-go/util"
//"github.com/denkhaus/bitshares/util"
"github.com/juju/errors"
)
type UInt64 uint64
type Buffer []byte
type Memo struct {
From PublicKey `json:"from"`
To PublicKey `json:"to"`
Nonce UInt64 `json:"nonce"`
Message Buffer `json:"message"`
}
func (p Memo) Marshal(enc *util.TypeEncoder) error {
if err := enc.Encode(p.From); err != nil {
return errors.Annotate(err, "encode from")
}
if err := enc.Encode(p.To); err != nil {
return errors.Annotate(err, "encode to")
}
if err := enc.Encode(p.Nonce); err != nil {
return errors.Annotate(err, "encode nonce")
}
if err := enc.Encode(p.Message); err != nil {
return errors.Annotate(err, "encode Message")
}
return nil
}
//Encrypt calculates a shared secret by the senders private key
//and the recipients public key, then encrypts the given memo message.
func (p *Memo) Encrypt(priv *PrivateKey, msg string) error {
sec, err := priv.SharedSecret(&p.To, 16, 16)
if err != nil {
return errors.Annotate(err, "SharedSecret")
}
iv, blk, err := p.cypherBlock(sec)
if err != nil {
return errors.Annotate(err, "cypherBlock")
}
buf := []byte(msg)
digest := sha256.Sum256(buf)
mode := cipher.NewCBCEncrypter(blk, iv)
// checksum + msg
raw := digest[:4]
raw = append(raw, buf...)
if len(raw)%16 != 0 {
raw = pad(raw, 16)
}
dst := make([]byte, len(raw))
mode.CryptBlocks(dst, raw)
p.Message = dst
return nil
}
//Decrypt calculates a shared secret by the receivers private key
//and the senders public key, then decrypts the given memo message.
func (p Memo) Decrypt(priv *PrivateKey) (string, error) {
var counterPartyPubKey PublicKey
myPubKey := priv.PublicKey()
if myPubKey.Equal(&p.To) {
counterPartyPubKey = p.From
} else if myPubKey.Equal(&p.From) {
counterPartyPubKey = p.To
} else {
return "", errors.New("Invalid counterparty public key, cannot decrypt")
}
sec, err := priv.SharedSecret(&counterPartyPubKey, 16, 16)
if err != nil {
return "", errors.Annotate(err, "SharedSecret")
}
iv, blk, err := p.cypherBlock(sec)
if err != nil {
return "", errors.Annotate(err, "cypherBlock")
}
mode := cipher.NewCBCDecrypter(blk, iv)
dst := make([]byte, len(p.Message))
mode.CryptBlocks(dst, p.Message)
//verify checksum
chk1 := dst[:4]
msg := unpad(dst[4:])
dig := sha256.Sum256(msg)
chk2 := dig[:4]
if bytes.Compare(chk1, chk2) != 0 {
return "", errors.New("ErrInvalidChecksum")
}
return string(msg), nil
}
// func (p Memo) cypherBlock(sec []byte) ([]byte, cipher.Block, error) {
// //ss := sha512.Sum512(sec)
// var seed []byte
// seed = append(seed, []byte(strconv.FormatUint(uint64(p.Nonce), 10))...)
// seed = append(seed, []byte(hex.EncodeToString(sec[:]))...) //[]byte(hex.EncodeToString(ss[:]))...)
// sd := sha512.Sum512(seed)
// hash := hex.EncodeToString(sd[:])
// iv, err := hex.DecodeString(string(hash[64:96]))
// if err != nil {
// return nil, nil, errors.Annotate(err, "DecodeString [iv]")
// }
// key, err := hex.DecodeString(string(hash[:64]))
// if err != nil {
// return nil, nil, errors.Annotate(err, "DecodeString [key]")
// }
// blk, err := aes.NewCipher(key)
// if err != nil {
// return nil, nil, errors.Annotate(err, "NewCipher")
// }
// return iv, blk, nil
// }
func (p Memo) cypherBlock(sec []byte) ([]byte, cipher.Block, error) {
ss := sha512.Sum512(sec)
var seed []byte
seed = append(seed, []byte(strconv.FormatUint(uint64(p.Nonce), 10))...)
seed = append(seed, []byte(hex.EncodeToString(ss[:]))...)
sd := sha512.Sum512(seed)
blk, err := aes.NewCipher(sd[0:32])
if err != nil {
return nil, nil, errors.Annotate(err, "NewCipher")
}
return sd[32:48], blk, nil
}
func unpad(buf []byte) []byte {
b := buf[len(buf)-1:][0]
cnt := int(b)
l := len(buf) - cnt
a := bytes.Repeat([]byte{b}, cnt)
if bytes.Compare(a, buf[l:]) == 0 {
return buf[:l]
}
return buf
}
func pad(buf []byte, length int) []byte {
cnt := length - len(buf)%length
buf = append(buf, bytes.Repeat([]byte{byte(cnt)}, cnt)...)
return buf
}