aboutsummaryrefslogtreecommitdiffstats
path: root/crypto/crypto.go
blob: 55d6f47e7e032b7d90989163b5c03266c699903d (plain) (blame)
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
package crypto

import (
    "crypto/aes"
    "crypto/cipher"
    "crypto/ecdsa"
    "crypto/elliptic"
    "crypto/rand"
    "crypto/sha256"
    "errors"

    "code.google.com/p/go-uuid/uuid"
    "code.google.com/p/go.crypto/pbkdf2"
    "code.google.com/p/go.crypto/ripemd160"
    "encoding/hex"
    "encoding/json"
    "github.com/ethereum/go-ethereum/crypto/secp256k1"
    "github.com/ethereum/go-ethereum/crypto/sha3"
    "github.com/ethereum/go-ethereum/ethutil"
    "github.com/obscuren/ecies"
)

func init() {
    // specify the params for the s256 curve
    ecies.AddParamsForCurve(S256(), ecies.ECIES_AES128_SHA256)
}

func Sha3(data []byte) []byte {
    d := sha3.NewKeccak256()
    d.Write(data)

    return d.Sum(nil)
}

// Creates an ethereum address given the bytes and the nonce
func CreateAddress(b []byte, nonce uint64) []byte {
    return Sha3(ethutil.NewValue([]interface{}{b, nonce}).Encode())[12:]
}

func Sha256(data []byte) []byte {
    hash := sha256.Sum256(data)

    return hash[:]
}

func Ripemd160(data []byte) []byte {
    ripemd := ripemd160.New()
    ripemd.Write(data)

    return ripemd.Sum(nil)
}

func Ecrecover(data []byte) []byte {
    var in = struct {
        hash []byte
        sig  []byte
    }{data[:32], data[32:]}

    r, _ := secp256k1.RecoverPubkey(in.hash, in.sig)

    return r
}

// New methods using proper ecdsa keys from the stdlib
func ToECDSA(prv []byte) *ecdsa.PrivateKey {
    if len(prv) == 0 {
        return nil
    }

    priv := new(ecdsa.PrivateKey)
    priv.PublicKey.Curve = S256()
    priv.D = ethutil.BigD(prv)
    priv.PublicKey.X, priv.PublicKey.Y = S256().ScalarBaseMult(prv)
    return priv
}

func FromECDSA(prv *ecdsa.PrivateKey) []byte {
    if prv == nil {
        return nil
    }
    return prv.D.Bytes()
}

func ToECDSAPub(pub []byte) *ecdsa.PublicKey {
    if len(pub) == 0 {
        return nil
    }
    x, y := elliptic.Unmarshal(S256(), pub)
    return &ecdsa.PublicKey{S256(), x, y}
}

func FromECDSAPub(pub *ecdsa.PublicKey) []byte {
    if pub == nil {
        return nil
    }
    return elliptic.Marshal(S256(), pub.X, pub.Y)
}

func GenerateKey() (*ecdsa.PrivateKey, error) {
    return ecdsa.GenerateKey(S256(), rand.Reader)
}

func SigToPub(hash, sig []byte) *ecdsa.PublicKey {
    s := Ecrecover(append(hash, sig...))
    x, y := elliptic.Unmarshal(S256(), s)

    return &ecdsa.PublicKey{S256(), x, y}
}

func Sign(hash []byte, prv *ecdsa.PrivateKey) (sig []byte, err error) {
    sig, err = secp256k1.Sign(hash, prv.D.Bytes())
    return
}

func Encrypt(pub *ecdsa.PublicKey, message []byte) ([]byte, error) {
    return ecies.Encrypt(rand.Reader, ecies.ImportECDSAPublic(pub), message, nil, nil)
}

func Decrypt(prv *ecdsa.PrivateKey, ct []byte) ([]byte, error) {
    key := ecies.ImportECDSA(prv)
    return key.Decrypt(rand.Reader, ct, nil, nil)
}

// creates a Key and stores that in the given KeyStore by decrypting a presale key JSON
func ImportPreSaleKey(keyStore KeyStore2, keyJSON []byte, password string) (*Key, error) {
    key, err := decryptPreSaleKey(keyJSON, password)
    if err != nil {
        return nil, err
    }
    id := uuid.NewRandom()
    key.Id = &id
    err = keyStore.StoreKey(key, password)
    return key, err
}

func decryptPreSaleKey(fileContent []byte, password string) (key *Key, err error) {
    preSaleKeyStruct := struct {
        EncSeed string
        EthAddr string
        Email   string
        BtcAddr string
    }{}
    err = json.Unmarshal(fileContent, &preSaleKeyStruct)
    if err != nil {
        return nil, err
    }
    encSeedBytes, err := hex.DecodeString(preSaleKeyStruct.EncSeed)
    iv := encSeedBytes[:16]
    cipherText := encSeedBytes[16:]
    /*
        See https://github.com/ethereum/pyethsaletool

        pyethsaletool generates the encryption key from password by
        2000 rounds of PBKDF2 with HMAC-SHA-256 using password as salt (:().
        16 byte key length within PBKDF2 and resulting key is used as AES key
    */
    passBytes := []byte(password)
    derivedKey := pbkdf2.Key(passBytes, passBytes, 2000, 16, sha256.New)
    plainText, err := aesCBCDecrypt(derivedKey, cipherText, iv)
    ethPriv := Sha3(plainText)
    ecKey := ToECDSA(ethPriv)
    key = &Key{
        Id:         nil,
        PrivateKey: ecKey,
    }
    derivedAddr := ethutil.Bytes2Hex(key.Address())
    expectedAddr := preSaleKeyStruct.EthAddr
    if derivedAddr != expectedAddr {
        err = errors.New("decrypted addr not equal to expected addr")
    }
    return key, err
}

func aesCBCDecrypt(key []byte, cipherText []byte, iv []byte) (plainText []byte, err error) {
    aesBlock, err := aes.NewCipher(key)
    if err != nil {
        return plainText, err
    }
    decrypter := cipher.NewCBCDecrypter(aesBlock, iv)
    paddedPlainText := make([]byte, len(cipherText))
    decrypter.CryptBlocks(paddedPlainText, cipherText)
    plainText = PKCS7Unpad(paddedPlainText)
    if plainText == nil {
        err = errors.New("Decryption failed: PKCS7Unpad failed after decryption")
    }
    return plainText, err
}

// From https://leanpub.com/gocrypto/read#leanpub-auto-block-cipher-modes
func PKCS7Pad(in []byte) []byte {
    padding := 16 - (len(in) % 16)
    if padding == 0 {
        padding = 16
    }
    for i := 0; i < padding; i++ {
        in = append(in, byte(padding))
    }
    return in
}

func PKCS7Unpad(in []byte) []byte {
    if len(in) == 0 {
        return nil
    }

    padding := in[len(in)-1]
    if int(padding) > len(in) || padding > aes.BlockSize {
        return nil
    } else if padding == 0 {
        return nil
    }

    for i := len(in) - 1; i > len(in)-int(padding)-1; i-- {
        if in[i] != padding {
            return nil
        }
    }
    return in[:len(in)-int(padding)]
}