aboutsummaryrefslogtreecommitdiff
path: root/keys.go
blob: 0248a4bbeb309a56ac6533e9b9f04a4fd93b2269 (plain)
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
package main

import (
	"bytes"
	"crypto/sha512"

	"dim13.org/signify/bhash"

	"golang.org/x/crypto/ed25519"
)

var (
	PKAlg  = [2]byte{'E', 'd'}
	KDFAlg = [2]byte{'B', 'K'}
)

type Sig struct {
	PKAlg  [2]byte
	KeyNum [8]byte
	Sig    [ed25519.SignatureSize]byte
}

type PubKey struct {
	PKAlg  [2]byte
	KeyNum [8]byte
	PubKey [ed25519.PublicKeySize]byte
}

type EncKey struct {
	PKAlg     [2]byte
	KDFAlg    [2]byte
	KDFRounds uint32
	Salt      [16]byte
	Checksum  [8]byte
	KeyNum    [8]byte
	SecKey    [ed25519.PrivateKeySize]byte
}

func xor(text, key []byte) {
	if len(text) != len(key) {
		return
	}
	for i := 0; i < len(text); i++ {
		text[i] ^= key[i]
	}
}

func (v *EncKey) IsValid(pass []byte) bool {
	if v.KDFRounds > 0 {
		key := bhash.Pbkdf(pass, v.Salt[:], int(v.KDFRounds), len(v.SecKey))
		for x := range key {
			v.SecKey[x] ^= key[x]
		}
	}
	sum := sha512.Sum512(v.SecKey[:])
	return bytes.Equal(sum[:len(v.Checksum)], v.Checksum[:])
}