aboutsummaryrefslogtreecommitdiff
path: root/pkgs/yokey/crypto.go
blob: df6e4ec7dd61e8fbe291d9168256e2fdc5079675 (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
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
package main

import (
	"crypto"
	"crypto/ed25519"
	"crypto/rand"
	"crypto/sha256"
	"encoding/base64"
	"encoding/pem"
	"io"
	"strings"

	"golang.org/x/crypto/curve25519"
	"golang.org/x/crypto/hkdf"
	"golang.org/x/crypto/ssh"
	"pault.ag/go/sshsig"
)

const sshsigContext = "sshsig-key-v1"

func deriveKey(master []byte, info string, length int) []byte {
	h := hkdf.New(sha256.New, master, []byte("fixed-salt"), []byte(info))
	out := make([]byte, length)
	if _, err := io.ReadFull(h, out); err != nil {
		panic(err)
	}
	return out
}

func buildInfo(baseInfo, context string) string {
	trimmed := strings.TrimSpace(context)
	if trimmed == "" {
		return baseInfo
	}

	return baseInfo + ":" + trimmed
}

func deriveEd25519(master []byte, context string) (string, []byte) {
	seed := deriveKey(master, buildInfo("ed25519-key-v1", context), 32)
	priv := ed25519.NewKeyFromSeed(seed)
	pub := priv.Public().(ed25519.PublicKey)

	sshPub, err := ssh.NewPublicKey(pub)
	if err != nil {
		panic(err)
	}

	privBlock, err := ssh.MarshalPrivateKey(priv, "")
	if err != nil {
		panic(err)
	}

	return string(ssh.MarshalAuthorizedKey(sshPub)), pem.EncodeToMemory(privBlock)
}

func deriveWireGuard(master []byte, context string) (string, string) {
	priv := deriveKey(master, buildInfo("wireguard-key-v1", context), 32)
	pub, _ := curve25519.X25519(priv, curve25519.Basepoint)

	return base64.StdEncoding.EncodeToString(pub), base64.StdEncoding.EncodeToString(priv)
}

const passwordCharset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@$%^&*"
const passwordLength = 14

func derivePassword(master []byte, seed string) string {
	return derivePasswordWithContext(master, seed, "")
}

func derivePasswordWithContext(master []byte, seed, context string) string {
	info := buildInfo("password-v1:"+seed, context)
	stream := hkdf.New(sha256.New, master, []byte("my-fixed-salt"), []byte(info))
	out := make([]byte, 0, passwordLength)
	buf := make([]byte, 64)
	maxMultiple := byte(256 / len(passwordCharset) * len(passwordCharset))

	for len(out) < passwordLength {
		if _, err := io.ReadFull(stream, buf); err != nil {
			panic(err)
		}

		for _, b := range buf {
			if b >= maxMultiple {
				continue
			}

			out = append(out, passwordCharset[int(b)%len(passwordCharset)])
			if len(out) == passwordLength {
				break
			}
		}
	}

	return string(out)
}

func deriveSSHSigSigner(master []byte) ssh.Signer {
	_, privPem := deriveEd25519(master, sshsigContext)
	rawPriv, err := ssh.ParseRawPrivateKey(privPem)
	if err != nil {
		panic(err)
	}

	signer, err := ssh.NewSignerFromKey(rawPriv)
	if err != nil {
		panic(err)
	}

	return signer
}

func signSSHSig(master []byte, namespace string, message []byte) []byte {
	signer := deriveSSHSigSigner(master)
	h := crypto.SHA512.New()
	if _, err := h.Write(message); err != nil {
		panic(err)
	}

	sig, err := sshsig.Sign(rand.Reader, signer, []byte(namespace), sshsig.HashAlgoSHA512, h.Sum(nil))
	if err != nil {
		panic(err)
	}

	return sig
}

func verifySSHSig(master []byte, namespace string, message, signature []byte) error {
	signer := deriveSSHSigSigner(master)
	pub := signer.PublicKey()

	parsedSig, err := sshsig.ParseSignature(signature)
	if err != nil {
		return err
	}

	h := crypto.SHA512.New()
	if _, err := h.Write(message); err != nil {
		return err
	}

	return sshsig.Verify(pub, []byte(namespace), sshsig.HashAlgoSHA512, h.Sum(nil), parsedSig)
}

func generateMasterKeyBytes() []byte {
	key := make([]byte, 32)
	rand.Read(key)

	return key
}