forked from umbracle/ethgo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add keystore v4 * Remove keystore fixtures
- Loading branch information
Showing
7 changed files
with
333 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package keystore | ||
|
||
import ( | ||
"crypto/aes" | ||
"crypto/cipher" | ||
"crypto/rand" | ||
"crypto/sha256" | ||
"encoding/hex" | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
|
||
"golang.org/x/crypto/pbkdf2" | ||
"golang.org/x/crypto/scrypt" | ||
) | ||
|
||
func getRand(size int) []byte { | ||
buf := make([]byte, size) | ||
rand.Read(buf) | ||
return buf | ||
} | ||
|
||
type hexString []byte | ||
|
||
func (h hexString) MarshalJSON() ([]byte, error) { | ||
str := "\"" + hex.EncodeToString(h) + "\"" | ||
return []byte(str), nil | ||
} | ||
|
||
func (h *hexString) UnmarshalJSON(data []byte) error { | ||
raw := string(data) | ||
raw = strings.Trim(raw, "\"") | ||
|
||
data, err := hex.DecodeString(raw) | ||
if err != nil { | ||
return err | ||
} | ||
*h = data | ||
return nil | ||
} | ||
|
||
func aesCTR(key, cipherText, iv []byte) ([]byte, error) { | ||
block, err := aes.NewCipher(key) | ||
if err != nil { | ||
return nil, err | ||
} | ||
stream := cipher.NewCTR(block, iv) | ||
|
||
dst := make([]byte, len(cipherText)) | ||
stream.XORKeyStream(dst, cipherText) | ||
|
||
return dst, nil | ||
} | ||
|
||
type pbkdf2Params struct { | ||
Dklen int | ||
Salt hexString | ||
C int | ||
Prf string | ||
} | ||
|
||
func (p *pbkdf2Params) Key(password []byte) []byte { | ||
return pbkdf2.Key(password, p.Salt, p.C, p.Dklen, sha256.New) | ||
} | ||
|
||
type scryptParams struct { | ||
Dklen int | ||
Salt hexString | ||
N int | ||
P int | ||
R int | ||
} | ||
|
||
func (s *scryptParams) Key(password []byte) ([]byte, error) { | ||
return scrypt.Key(password, s.Salt, s.N, s.R, s.P, s.Dklen) | ||
} | ||
|
||
func applyKdf(fn string, password, paramsRaw []byte) ([]byte, error) { | ||
var key []byte | ||
|
||
if fn == "pbkdf2" { | ||
var params pbkdf2Params | ||
if err := json.Unmarshal(paramsRaw, ¶ms); err != nil { | ||
return nil, err | ||
} | ||
if params.Prf != "hmac-sha256" { | ||
return nil, fmt.Errorf("not found") | ||
} | ||
key = params.Key(password) | ||
} else if fn == "scrypt" { | ||
var params scryptParams | ||
err := json.Unmarshal(paramsRaw, ¶ms) | ||
if err != nil { | ||
return nil, err | ||
} | ||
key, err = params.Key(password) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} else { | ||
return nil, fmt.Errorf("kdf '%s' not supported", fn) | ||
} | ||
return key, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.