accounts: make KDFParams a struct

This commit is contained in:
htkao 2019-05-20 21:44:20 -07:00
parent 97d3615612
commit 0bea9a6447
2 changed files with 32 additions and 30 deletions

View file

@ -78,12 +78,22 @@ type encryptedKeyJSONV1 struct {
Version string `json:"version"` Version string `json:"version"`
} }
type kdfparamsJSON struct {
Salt string `json:"salt"`
Prf string `json:"prf"`
DKLen int `json:"dklen"`
C int `json:"c"`
N int `json:"n"`
P int `json:"p"`
R int `json:"r"`
}
type CryptoJSON struct { type CryptoJSON struct {
Cipher string `json:"cipher"` Cipher string `json:"cipher"`
CipherText string `json:"ciphertext"` CipherText string `json:"ciphertext"`
CipherParams cipherparamsJSON `json:"cipherparams"` CipherParams cipherparamsJSON `json:"cipherparams"`
KDF string `json:"kdf"` KDF string `json:"kdf"`
KDFParams map[string]interface{} `json:"kdfparams"` KDFParams kdfparamsJSON `json:"kdfparams"`
MAC string `json:"mac"` MAC string `json:"mac"`
} }

View file

@ -159,12 +159,14 @@ func EncryptDataV3(data, auth []byte, scryptN, scryptP int) (CryptoJSON, error)
} }
mac := crypto.Keccak256(derivedKey[16:32], cipherText) mac := crypto.Keccak256(derivedKey[16:32], cipherText)
scryptParamsJSON := make(map[string]interface{}, 5) scryptParamsJSON := kdfparamsJSON{
scryptParamsJSON["n"] = scryptN N: scryptN,
scryptParamsJSON["r"] = scryptR R: scryptR,
scryptParamsJSON["p"] = scryptP P: scryptP,
scryptParamsJSON["dklen"] = scryptDKLen DKLen: scryptDKLen,
scryptParamsJSON["salt"] = hex.EncodeToString(salt) Salt: hex.EncodeToString(salt),
}
cipherParamsJSON := cipherparamsJSON{ cipherParamsJSON := cipherparamsJSON{
IV: hex.EncodeToString(iv), IV: hex.EncodeToString(iv),
} }
@ -319,38 +321,28 @@ func decryptKeyV1(keyProtected *encryptedKeyJSONV1, auth string) (keyBytes []byt
func getKDFKey(cryptoJSON CryptoJSON, auth string) ([]byte, error) { func getKDFKey(cryptoJSON CryptoJSON, auth string) ([]byte, error) {
authArray := []byte(auth) authArray := []byte(auth)
salt, err := hex.DecodeString(cryptoJSON.KDFParams["salt"].(string)) salt, err := hex.DecodeString(cryptoJSON.KDFParams.Salt)
if err != nil { if err != nil {
return nil, err return nil, err
} }
dkLen := ensureInt(cryptoJSON.KDFParams["dklen"])
if cryptoJSON.KDF == keyHeaderKDF { if cryptoJSON.KDF == keyHeaderKDF {
n := ensureInt(cryptoJSON.KDFParams["n"]) return scrypt.Key(
r := ensureInt(cryptoJSON.KDFParams["r"]) authArray,
p := ensureInt(cryptoJSON.KDFParams["p"]) salt,
return scrypt.Key(authArray, salt, n, r, p, dkLen) cryptoJSON.KDFParams.N,
cryptoJSON.KDFParams.R,
cryptoJSON.KDFParams.P,
cryptoJSON.KDFParams.DKLen,
)
} else if cryptoJSON.KDF == "pbkdf2" { } else if cryptoJSON.KDF == "pbkdf2" {
c := ensureInt(cryptoJSON.KDFParams["c"]) if cryptoJSON.KDFParams.Prf != "hmac-sha256" {
prf := cryptoJSON.KDFParams["prf"].(string) return nil, fmt.Errorf("unsupported PBKDF2 PRF: %s", cryptoJSON.KDFParams.Prf)
if prf != "hmac-sha256" {
return nil, fmt.Errorf("Unsupported PBKDF2 PRF: %s", prf)
} }
key := pbkdf2.Key(authArray, salt, c, dkLen, sha256.New) key := pbkdf2.Key(authArray, salt, cryptoJSON.KDFParams.C, cryptoJSON.KDFParams.DKLen, sha256.New)
return key, nil return key, nil
} }
return nil, fmt.Errorf("Unsupported KDF: %s", cryptoJSON.KDF) return nil, fmt.Errorf("unsupported KDF: %s", cryptoJSON.KDF)
}
// TODO: can we do without this when unmarshalling dynamic JSON?
// why do integers in KDF params end up as float64 and not int after
// unmarshal?
func ensureInt(x interface{}) int {
res, ok := x.(int)
if !ok {
res = int(x.(float64))
}
return res
} }