-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcrypto.go
200 lines (159 loc) · 4.03 KB
/
crypto.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// SPDX-FileCopyrightText: 2023 Steffen Vogel <[email protected]>
// SPDX-License-Identifier: Apache-2.0
package rosenpass
import (
"crypto/cipher"
"crypto/rand"
"fmt"
"github.com/cloudflare/circl/kem"
"github.com/cloudflare/circl/kem/kyber/kyber512"
"github.com/cloudflare/circl/kem/mceliece/mceliece460896"
"golang.org/x/crypto/blake2b"
"golang.org/x/crypto/chacha20poly1305"
)
var (
kemStatic kem.Scheme = mceliece460896.Scheme()
kemEphemeral kem.Scheme = kyber512.Scheme()
)
// GenerateKeyPair generates a new Classic McEliece key pair.
func GenerateKeyPair() (PublicKey, SecretKey, error) { //nolint:revive
return generateStaticKeyPair()
}
// GenerateKeyPair generates a new Classic McEliece key pair in its old (round 2) format.
func GenerateRound2KeyPair() (PublicKey, SecretKey, error) { //nolint:revive
spk, ssk, err := generateStaticKeyPair()
if err != nil {
return nil, nil, err
}
// Convert a secret key from its round 3 to round 2 format
if len(ssk) == sskSize {
g := ssk[40:232]
a := ssk[232:13032]
s := ssk[13032:13608]
sskNew := []byte{}
sskNew = append(sskNew, s...)
sskNew = append(sskNew, g...)
sskNew = append(sskNew, a...)
return spk, sskNew, nil
}
return spk, ssk, nil
}
// Generates a new pre-shared key.
func GeneratePresharedKey() (Key, error) { //nolint:revive
k, err := generateKey(pskSize)
if err != nil {
return key{}, err
}
return key(k), nil
}
func blake2(k key, d []byte) key {
h, _ := blake2b.New256(k[:])
h.Write(d)
return key(h.Sum(nil))
}
func hmac(k key, d []byte) key {
var iKey, oKey key
for i := range iKey {
iKey[i] = k[i] ^ 0x36
oKey[i] = k[i] ^ 0x5c
}
outer := blake2(iKey, d)
return blake2(oKey, outer[:])
}
// A keyed hmac function with one 32-byte input, one variable-size input, and one 32-byte output.
// As keyed hmac function we use the HMAC construction with BLAKE2s as the inner hmac function.
func (k key) hash(data ...[]byte) key {
for _, d := range data {
k = hmac(k, d)
}
return k
}
func (k key) mix(data ...[]byte) key {
for _, d := range data {
k = k.hash(khMix[:], d)
}
return k
}
func newAEAD(k key) (cipher.AEAD, error) {
return chacha20poly1305.New(k[:])
}
func newXAEAD(k key) (cipher.AEAD, error) {
return chacha20poly1305.NewX(k[:])
}
func generateSessionID() (sid, error) {
s, err := generateKey(sidSize)
if err != nil {
return sid{}, err
}
return sid(s), nil
}
func generateNonce() (nonceX, error) {
n, err := generateKey(nonceSizeX)
if err != nil {
return nonceX{}, err
}
return nonceX(n), nil
}
func generateBiscuitKey() (key, error) {
n, err := generateKey(keySize)
if err != nil {
return key{}, err
}
return key(n), nil
}
func generateKey(l int) ([]byte, error) {
p := make([]byte, l)
if n, err := rand.Read(p); err != nil {
return nil, err
} else if n != l {
return nil, fmt.Errorf("partial read")
}
return p, nil
}
func generateStaticKeyPair() (spk, ssk, error) {
pk, sk, err := generateKeyPair(kemStatic)
if err != nil {
return nil, nil, err
}
return spk(pk), ssk(sk), nil
}
func generateEphemeralKeyPair() (epk, esk, error) {
pk, sk, err := generateKeyPair(kemEphemeral)
if err != nil {
return nil, nil, err
}
return epk(pk), esk(sk), nil
}
func generateKeyPair(typ kem.Scheme) ([]byte, []byte, error) {
pk, sk, err := typ.GenerateKeyPair()
if err != nil {
return nil, nil, err
}
pk2, _ := pk.MarshalBinary()
sk2, _ := sk.MarshalBinary()
return pk2, sk2, nil
}
func newKEM(typ kem.Scheme, key []byte) (*keyEncapsulation, error) {
return &keyEncapsulation{
key: key,
scheme: typ,
}, nil
}
type keyEncapsulation struct {
scheme kem.Scheme
key []byte
}
func (ke *keyEncapsulation) EncapSecret(pk []byte) (ct []byte, ss []byte, err error) {
cpk, err := ke.scheme.UnmarshalBinaryPublicKey(pk)
if err != nil {
return nil, nil, err
}
return ke.scheme.Encapsulate(cpk)
}
func (ke *keyEncapsulation) DecapSecret(ct []byte) (ss []byte, err error) {
csk, err := ke.scheme.UnmarshalBinaryPrivateKey(ke.key)
if err != nil {
return nil, err
}
return ke.scheme.Decapsulate(csk, ct)
}