-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth_kdf.go
180 lines (144 loc) · 4.19 KB
/
auth_kdf.go
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// SPDX-FileCopyrightText: 2023-2024 Steffen Vogel <[email protected]>
// SPDX-License-Identifier: Apache-2.0
package openpgp
import (
"crypto/sha256"
"crypto/sha512"
"errors"
"fmt"
"hash"
iso "cunicu.li/go-iso7816"
"cunicu.li/go-openpgp-card/internal/s2k"
)
var (
errIterationsTooSmall = errors.New("iterations too small")
errUnsupportedKDFAlg = fmt.Errorf("%w algorithm", ErrUnsupported)
errUnsupportedKDFHashAlg = fmt.Errorf("%w hash algorithm", ErrUnsupported)
errUnsupportedPasswordLengthsTooShortForKDF = fmt.Errorf("%w: password lengths are too small for KDF", ErrUnsupported)
errMissingKDFSalt = errors.New("missing salt")
)
func (c *Card) GetKDF() (k *KDF, err error) {
resp, err := c.getData(tagKDF)
if err != nil {
return nil, err
}
k = &KDF{}
if err := k.Decode(resp); err != nil {
return nil, err
}
c.kdf = k
return k, nil
}
// SetupKDF initialize the KDF data object and updates passwords to work with it.
//
// Resetting code must be set again. User/PW1 and Admin/PW3 are unchanged.
//
// Access condition: Admin/PW3 (User/PW1 and AdminPW3 must be passed as arguments)
// See: OpenPGP Smart Card Application - Section 4.3.2 Key derived format
func (c *Card) SetupKDF(alg AlgKDF, iterations int, pw1, pw3 string) (err error) {
// Check if KDF is supported
if c.Capabilities.Flags&CapKDF == 0 {
return fmt.Errorf("key derived passwords are %w", ErrUnsupported)
}
if min(c.PasswordStatus.LengthPW1, c.PasswordStatus.LengthRC, c.PasswordStatus.LengthPW3) < 64 {
return errUnsupportedPasswordLengthsTooShortForKDF
}
if alg == AlgKDFIterSaltedS2K && iterations < 1000 {
return errIterationsTooSmall
}
// Prepare new KDF parameters
// Put KDF parameters to card
kdf := &KDF{
Algorithm: alg,
}
switch alg {
case AlgKDFNone:
case AlgKDFIterSaltedS2K:
kdf.HashAlgorithm = AlgHashSHA512
kdf.Iterations = iterations
if _, err := c.Rand.Read(kdf.SaltPW1[:]); err != nil {
return fmt.Errorf("failed to generate random salt: %w", err)
}
if _, err := c.Rand.Read(kdf.SaltRC[:]); err != nil {
return fmt.Errorf("failed to generate random salt: %w", err)
}
if _, err := c.Rand.Read(kdf.SaltPW3[:]); err != nil {
return fmt.Errorf("failed to generate random salt: %w", err)
}
default:
return ErrUnsupported
}
if kdf.InitialHashPW1, err = kdf.DerivePassword(PW1, DefaultPW1); err != nil {
return fmt.Errorf("failed to derive password: %w", err)
}
if kdf.InitialHashPW3, err = kdf.DerivePassword(PW3, DefaultPW3); err != nil {
return fmt.Errorf("failed to derive password: %w", err)
}
// Verify for KDF-DO update
if err := c.VerifyPassword(PW3, pw3); err != nil {
return err
}
// Update passwords according to new KDF
pws := map[byte]string{
PW1: pw1,
PW3: pw3,
}
for pwType, pwCurrent := range pws {
pwOld, err := c.kdf.DerivePassword(pwType, pwCurrent)
if err != nil {
return fmt.Errorf("failed to derive password: %w", err)
}
pwNew, err := kdf.DerivePassword(pwType, pwCurrent)
if err != nil {
return fmt.Errorf("failed to derive password: %w", err)
}
data := []byte{}
data = append(data, pwOld...)
data = append(data, pwNew...)
if _, err = send(c.tx, iso.InsChangeReferenceData, 0x00, pwType, data); err != nil {
return fmt.Errorf("failed to change password: %w", err)
}
}
// Update KDF-DO
b, err := kdf.Encode()
if err != nil {
return err
}
if err := c.putData(tagKDF, b); err != nil {
return err
}
c.kdf = kdf
return nil
}
func (k *KDF) DerivePassword(pwType byte, pw string) ([]byte, error) {
switch k.Algorithm {
case AlgKDFNone:
return []byte(pw), nil
case AlgKDFIterSaltedS2K:
var hash hash.Hash
switch k.HashAlgorithm {
case AlgHashSHA256:
hash = sha256.New()
case AlgHashSHA512:
hash = sha512.New()
default:
return nil, errUnsupportedKDFHashAlg
}
var salt [8]byte
switch pwType {
case PW1:
salt = k.SaltPW1
case RC:
salt = k.SaltRC
case PW3:
salt = k.SaltPW3
default:
return nil, errMissingKDFSalt
}
out := make([]byte, hash.Size())
s2k.Iterated(out, hash, []byte(pw), salt, k.Iterations)
return out, nil
default:
return nil, errUnsupportedKDFAlg
}
}