Skip to content

Commit

Permalink
Fixed opening of graves with Argon2
Browse files Browse the repository at this point in the history
  • Loading branch information
betapictoris committed Aug 21, 2023
1 parent febb522 commit 0d7029d
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func main() {

// And encrypt it
log.Debug("Encrypting...")
encryptFile(app_path + "/morgue/" + cCtx.Args().First() + ".tar.gz", key)
err = encryptFile(app_path + "/morgue/" + cCtx.Args().First() + ".tar.gz", key)
if err != nil {
log.Fatal("Failed to encrypt archive.", "err", err)
}
Expand Down Expand Up @@ -293,14 +293,18 @@ func main() {
* Returns a key and error.
*/
func checkKey(grave, passphrase string) (string, error) {
// Get all keys from the keys file.
log.Info("Checking key...")

// Get all keys from the keys file. :
log.Debug("Reading keys file...")
keys, err := ioutil.ReadFile(app_path + "/keys")
if err != nil {
return "", err
}

var encoded_hash string

log.Debug("Finding key...")
// Loop through all keys -- this isn't perfect, but it works.
for _, i := range strings.Split(string(keys), "\n") {
e := strings.Split(i, " ")
Expand All @@ -313,12 +317,15 @@ func checkKey(grave, passphrase string) (string, error) {
}
}

// Try to parse the encoded hash...
log.Debug("Getting values...")
vals := strings.Split(encoded_hash, "$")
if len(vals) != 6 {
return "", errors.New("The hash is not in the correct format.")
}

var version int
log.Debug("Checking version...")
_, err = fmt.Sscanf(vals[2], "v=%d", &version)
if err != nil {
return "", err
Expand All @@ -327,28 +334,33 @@ func checkKey(grave, passphrase string) (string, error) {
return "", errors.New("The argon2 version is incompatible.")
}

var memory uint32
var iterations uint32
var threads uint8
_, err = fmt.Sscanf(vals[3], "m=%d,t=%d,p=%d", memory, iterations, threads)
var memory int
var iterations int
var threads int
log.Debug("Finding memory, iterations, and threads...")
_, err = fmt.Sscanf(vals[3], "m=%d,t=%d,p=%d", &memory, &iterations, &threads)
if err != nil {
return "", err
}

log.Debug("Finding salt...")
salt, err := base64.RawStdEncoding.Strict().DecodeString(vals[4])
if err != nil {
return "", err
}
//saltLength := uint32(len(salt))

log.Debug("Finding hash...")
hash, err := base64.RawStdEncoding.Strict().DecodeString(vals[5])
if err != nil {
return "", err
}
keyLength := uint32(len(hash))

newHash := argon2.IDKey([]byte(passphrase), salt, iterations, memory, threads, keyLength)


log.Debug("Rehashing with the same parameters...")
newHash := argon2.IDKey([]byte(passphrase), salt, uint32(iterations), uint32(memory), uint8(threads), keyLength)

log.Debug("Checking...")
if subtle.ConstantTimeCompare(hash, newHash) == 1 {
return string(newHash), nil
}
Expand Down Expand Up @@ -404,7 +416,7 @@ func createKey(grave, passphrase string) (string, error) {
}

log.Debug("Done!")
return string(key), nil
return string(argon), nil
}

/*
Expand Down Expand Up @@ -741,6 +753,10 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
if err != nil {
log.Fatal("Failed to preform key action.", "is new key", grave_is_new, "err", err)
}

if key == "" {
log.Fatal("Couldn't validate key.")
}

return m, tea.Quit
case tea.KeyCtrlC, tea.KeyEsc:
Expand Down

0 comments on commit 0d7029d

Please sign in to comment.