-
Notifications
You must be signed in to change notification settings - Fork 15
/
kem.go
260 lines (248 loc) · 10.6 KB
/
kem.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/* SPDX-FileCopyrightText: © 2020-2024 Nadim Kobeissi <[email protected]>
* SPDX-License-Identifier: MIT */
// Package kyberk2so provides a clean implementation of the Kyber IND-CCA2-secure
// key encapsulation mechanism (KEM), whose security is based on the hardness of
// solving the learning-with-errors (LWE) problem over module lattices.
package kyberk2so
import (
"crypto/rand"
"crypto/subtle"
"golang.org/x/crypto/sha3"
)
// KemKeypair512 returns a Kyber-512 private key
// and a corresponding Kyber-512 public key.
// An accompanying error is returned if no sufficient
// randomness could be obtained from the system.
func KemKeypair512() ([Kyber512SKBytes]byte, [Kyber512PKBytes]byte, error) {
const paramsK = 2
var privateKeyFixedLength [Kyber512SKBytes]byte
var publicKeyFixedLength [Kyber512PKBytes]byte
indcpaPrivateKey, indcpaPublicKey, err := indcpaKeypair(paramsK)
if err != nil {
return privateKeyFixedLength, publicKeyFixedLength, err
}
pkh := sha3.Sum256(indcpaPublicKey)
rnd := make([]byte, paramsSymBytes)
_, err = rand.Read(rnd)
if err != nil {
return privateKeyFixedLength, publicKeyFixedLength, err
}
privateKey := append(indcpaPrivateKey, indcpaPublicKey...)
privateKey = append(privateKey, pkh[:]...)
privateKey = append(privateKey, rnd...)
copy(privateKeyFixedLength[:], privateKey)
copy(publicKeyFixedLength[:], indcpaPublicKey)
return privateKeyFixedLength, publicKeyFixedLength, nil
}
// KemKeypair768 returns a Kyber-768 private key
// and a corresponding Kyber-768 public key.
// An accompanying error is returned if no sufficient
// randomness could be obtained from the system.
func KemKeypair768() ([Kyber768SKBytes]byte, [Kyber768PKBytes]byte, error) {
const paramsK int = 3
var privateKeyFixedLength [Kyber768SKBytes]byte
var publicKeyFixedLength [Kyber768PKBytes]byte
indcpaPrivateKey, indcpaPublicKey, err := indcpaKeypair(paramsK)
if err != nil {
return privateKeyFixedLength, publicKeyFixedLength, err
}
pkh := sha3.Sum256(indcpaPublicKey)
rnd := make([]byte, paramsSymBytes)
_, err = rand.Read(rnd)
if err != nil {
return privateKeyFixedLength, publicKeyFixedLength, err
}
privateKey := append(indcpaPrivateKey, indcpaPublicKey...)
privateKey = append(privateKey, pkh[:]...)
privateKey = append(privateKey, rnd...)
copy(privateKeyFixedLength[:], privateKey)
copy(publicKeyFixedLength[:], indcpaPublicKey)
return privateKeyFixedLength, publicKeyFixedLength, nil
}
// KemKeypair1024 returns a Kyber-1024 private key
// and a corresponding Kyber-1024 public key.
// An accompanying error is returned if no sufficient
// randomness could be obtained from the system.
func KemKeypair1024() ([Kyber1024SKBytes]byte, [Kyber1024PKBytes]byte, error) {
const paramsK int = 4
var privateKeyFixedLength [Kyber1024SKBytes]byte
var publicKeyFixedLength [Kyber1024PKBytes]byte
indcpaPrivateKey, indcpaPublicKey, err := indcpaKeypair(paramsK)
if err != nil {
return privateKeyFixedLength, publicKeyFixedLength, err
}
pkh := sha3.Sum256(indcpaPublicKey)
rnd := make([]byte, paramsSymBytes)
_, err = rand.Read(rnd)
if err != nil {
return privateKeyFixedLength, publicKeyFixedLength, err
}
privateKey := append(indcpaPrivateKey, indcpaPublicKey...)
privateKey = append(privateKey, pkh[:]...)
privateKey = append(privateKey, rnd...)
copy(privateKeyFixedLength[:], privateKey)
copy(publicKeyFixedLength[:], indcpaPublicKey)
return privateKeyFixedLength, publicKeyFixedLength, nil
}
// KemEncrypt512 takes a public key (from KemKeypair512) as input
// and returns a ciphertext and a 32-byte shared secret.
// An accompanying error is returned if no sufficient
// randomness could be obtained from the system.
func KemEncrypt512(publicKey [Kyber512PKBytes]byte) (
[Kyber512CTBytes]byte, [KyberSSBytes]byte, error,
) {
const paramsK int = 2
var ciphertextFixedLength [Kyber512CTBytes]byte
var sharedSecretFixedLength [KyberSSBytes]byte
sharedSecret := make([]byte, paramsSymBytes)
buf := make([]byte, 2*paramsSymBytes)
_, err := rand.Read(buf[:paramsSymBytes])
if err != nil {
return ciphertextFixedLength, sharedSecretFixedLength, err
}
buf1 := sha3.Sum256(buf[:paramsSymBytes])
buf2 := sha3.Sum256(publicKey[:])
kr := sha3.Sum512(append(buf1[:], buf2[:]...))
ciphertext, err := indcpaEncrypt(buf1[:], publicKey[:], kr[paramsSymBytes:], paramsK)
krc := sha3.Sum256(ciphertext)
sha3.ShakeSum256(sharedSecret, append(kr[:paramsSymBytes], krc[:]...))
copy(ciphertextFixedLength[:], ciphertext)
copy(sharedSecretFixedLength[:], sharedSecret)
return ciphertextFixedLength, sharedSecretFixedLength, err
}
// KemEncrypt768 takes a public key (from KemKeypair768) as input and
// returns a ciphertext and a 32-byte shared secret.
// An accompanying error is returned if no sufficient
// randomness could be obtained from the system.
func KemEncrypt768(publicKey [Kyber768PKBytes]byte) (
[Kyber768CTBytes]byte, [KyberSSBytes]byte, error,
) {
const paramsK int = 3
var ciphertextFixedLength [Kyber768CTBytes]byte
var sharedSecretFixedLength [KyberSSBytes]byte
sharedSecret := make([]byte, paramsSymBytes)
buf := make([]byte, 2*paramsSymBytes)
_, err := rand.Read(buf[:paramsSymBytes])
if err != nil {
return ciphertextFixedLength, sharedSecretFixedLength, err
}
buf1 := sha3.Sum256(buf[:paramsSymBytes])
buf2 := sha3.Sum256(publicKey[:])
kr := sha3.Sum512(append(buf1[:], buf2[:]...))
ciphertext, err := indcpaEncrypt(buf1[:], publicKey[:], kr[paramsSymBytes:], paramsK)
krc := sha3.Sum256(ciphertext)
sha3.ShakeSum256(sharedSecret, append(kr[:paramsSymBytes], krc[:]...))
copy(ciphertextFixedLength[:], ciphertext)
copy(sharedSecretFixedLength[:], sharedSecret)
return ciphertextFixedLength, sharedSecretFixedLength, err
}
// KemEncrypt1024 takes a public key (from KemKeypair1024) as input
// and returns a ciphertext and a 32-byte shared secret.
// An accompanying error is returned if no sufficient
// randomness could be obtained from the system.
func KemEncrypt1024(publicKey [Kyber1024PKBytes]byte) (
[Kyber1024CTBytes]byte, [KyberSSBytes]byte, error,
) {
const paramsK int = 4
var ciphertextFixedLength [Kyber1024CTBytes]byte
var sharedSecretFixedLength [KyberSSBytes]byte
sharedSecret := make([]byte, paramsSymBytes)
buf := make([]byte, 2*paramsSymBytes)
_, err := rand.Read(buf[:paramsSymBytes])
if err != nil {
return ciphertextFixedLength, sharedSecretFixedLength, err
}
buf1 := sha3.Sum256(buf[:paramsSymBytes])
buf2 := sha3.Sum256(publicKey[:])
kr := sha3.Sum512(append(buf1[:], buf2[:]...))
ciphertext, err := indcpaEncrypt(buf1[:], publicKey[:], kr[paramsSymBytes:], paramsK)
krc := sha3.Sum256(ciphertext)
sha3.ShakeSum256(sharedSecret, append(kr[:paramsSymBytes], krc[:]...))
copy(ciphertextFixedLength[:], ciphertext)
copy(sharedSecretFixedLength[:], sharedSecret)
return ciphertextFixedLength, sharedSecretFixedLength, err
}
// KemDecrypt512 takes a ciphertext (from KeyEncrypt512),
// a private key (from KemKeypair512) and returns a 32-byte shared secret.
// An accompanying error is returned if no sufficient
// randomness could be obtained from the system.
func KemDecrypt512(
ciphertext [Kyber512CTBytes]byte,
privateKey [Kyber512SKBytes]byte,
) ([KyberSSBytes]byte, error) {
const paramsK int = 2
var sharedSecretFixedLength [KyberSSBytes]byte
sharedSecret := make([]byte, KyberSSBytes)
indcpaPrivateKey := privateKey[:paramsIndcpaSecretKeyBytesK512]
pki := paramsIndcpaSecretKeyBytesK512 + paramsIndcpaPublicKeyBytesK512
publicKey := privateKey[paramsIndcpaSecretKeyBytesK512:pki]
buf := indcpaDecrypt(ciphertext[:], indcpaPrivateKey, paramsK)
ski := Kyber512SKBytes - 2*paramsSymBytes
kr := sha3.Sum512(append(buf, privateKey[ski:ski+paramsSymBytes]...))
cmp, err := indcpaEncrypt(buf, publicKey, kr[paramsSymBytes:], paramsK)
fail := byte(subtle.ConstantTimeCompare(ciphertext[:], cmp) - 1)
krh := sha3.Sum256(ciphertext[:])
for i := 0; i < paramsSymBytes; i++ {
skx := privateKey[:Kyber512SKBytes-paramsSymBytes+i]
kr[i] = kr[i] ^ (fail & (kr[i] ^ skx[i]))
}
sha3.ShakeSum256(sharedSecret, append(kr[:paramsSymBytes], krh[:]...))
copy(sharedSecretFixedLength[:], sharedSecret)
return sharedSecretFixedLength, err
}
// KemDecrypt768 takes a ciphertext (from KeyEncrypt768),
// a private key (from KemKeypair768) and returns a 32-byte shared secret.
// An accompanying error is returned if no sufficient
// randomness could be obtained from the system.
func KemDecrypt768(
ciphertext [Kyber768CTBytes]byte,
privateKey [Kyber768SKBytes]byte,
) ([KyberSSBytes]byte, error) {
const paramsK int = 3
var sharedSecretFixedLength [KyberSSBytes]byte
sharedSecret := make([]byte, KyberSSBytes)
indcpaPrivateKey := privateKey[:paramsIndcpaSecretKeyBytesK768]
pki := paramsIndcpaSecretKeyBytesK768 + paramsIndcpaPublicKeyBytesK768
publicKey := privateKey[paramsIndcpaSecretKeyBytesK768:pki]
buf := indcpaDecrypt(ciphertext[:], indcpaPrivateKey, paramsK)
ski := Kyber768SKBytes - 2*paramsSymBytes
kr := sha3.Sum512(append(buf, privateKey[ski:ski+paramsSymBytes]...))
cmp, err := indcpaEncrypt(buf, publicKey, kr[paramsSymBytes:], paramsK)
fail := byte(subtle.ConstantTimeCompare(ciphertext[:], cmp) - 1)
krh := sha3.Sum256(ciphertext[:])
for i := 0; i < paramsSymBytes; i++ {
skx := privateKey[:Kyber768SKBytes-paramsSymBytes+i]
kr[i] = kr[i] ^ (fail & (kr[i] ^ skx[i]))
}
sha3.ShakeSum256(sharedSecret, append(kr[:paramsSymBytes], krh[:]...))
copy(sharedSecretFixedLength[:], sharedSecret)
return sharedSecretFixedLength, err
}
// KemDecrypt1024 takes a ciphertext (from KeyEncrypt1024),
// a private key (from KemKeypair1024) and returns a 32-byte shared secret.
// An accompanying error is returned if no sufficient
// randomness could be obtained from the system.
func KemDecrypt1024(
ciphertext [Kyber1024CTBytes]byte,
privateKey [Kyber1024SKBytes]byte,
) ([KyberSSBytes]byte, error) {
const paramsK int = 4
var sharedSecretFixedLength [KyberSSBytes]byte
sharedSecret := make([]byte, KyberSSBytes)
indcpaPrivateKey := privateKey[:paramsIndcpaSecretKeyBytesK1024]
pki := paramsIndcpaSecretKeyBytesK1024 + paramsIndcpaPublicKeyBytesK1024
publicKey := privateKey[paramsIndcpaSecretKeyBytesK1024:pki]
buf := indcpaDecrypt(ciphertext[:], indcpaPrivateKey, paramsK)
ski := Kyber1024SKBytes - 2*paramsSymBytes
kr := sha3.Sum512(append(buf, privateKey[ski:ski+paramsSymBytes]...))
cmp, err := indcpaEncrypt(buf, publicKey, kr[paramsSymBytes:], paramsK)
fail := byte(subtle.ConstantTimeCompare(ciphertext[:], cmp) - 1)
krh := sha3.Sum256(ciphertext[:])
for i := 0; i < paramsSymBytes; i++ {
skx := privateKey[:Kyber1024SKBytes-paramsSymBytes+i]
kr[i] = kr[i] ^ (fail & (kr[i] ^ skx[i]))
}
sha3.ShakeSum256(sharedSecret, append(kr[:paramsSymBytes], krh[:]...))
copy(sharedSecretFixedLength[:], sharedSecret)
return sharedSecretFixedLength, err
}