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 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
|
// Package frodo640shake implements the variant FrodoKEM-640 with SHAKE.
package frodo640shake
import (
"bytes"
cryptoRand "crypto/rand"
"crypto/subtle"
"io"
"github.com/cloudflare/circl/internal/sha3"
"github.com/cloudflare/circl/kem"
)
const (
paramN = 640
// Denoted by 'mbar' in the FrodoKEM spec.
paramNbar = 8
logQ = 15
logQMask = ((1 << logQ) - 1)
seedASize = 16
pkHashSize = 16
// Denoted by 'B' in the FrodoKEM spec.
extractedBits = 2
messageSize = 16
matrixBpPackedSize = (logQ * (paramN * paramNbar)) / 8
)
const (
// Size of seed for NewKeyFromSeed.
// = len(s) + len(seedSE) + len(z).
KeySeedSize = SharedKeySize + SharedKeySize + 16
// Size of seed for EncapsulateTo.
EncapsulationSeedSize = 16
// Size of the established shared key.
SharedKeySize = 16
// Size of the encapsulated shared key.
CiphertextSize = 9720
// Size of a packed public key.
PublicKeySize = 9616
// Size of a packed private key.
PrivateKeySize = 19888
)
// Multi-dimensional arrays are stored in 1-dimensional arrays in
// row-major order.
type (
nByNU16 [paramN * paramN]uint16
nByNbarU16 [paramN * paramNbar]uint16
nbarByNU16 [paramNbar * paramN]uint16
nbarByNbarU16 [paramNbar * paramNbar]uint16
)
// Type of a FrodoKEM-640-SHAKE public key
type PublicKey struct {
seedA [seedASize]byte
matrixB nByNbarU16
}
// Type of a FrodoKEM-640-SHAKE private key
type PrivateKey struct {
hashInputIfDecapsFail [SharedKeySize]byte
pk *PublicKey
// matrixS stores transpose(S)
matrixS nByNbarU16
// H(packed(pk))
hpk [pkHashSize]byte
}
// NewKeyFromSeed derives a public/private keypair deterministically
// from the given seed.
//
// Panics if seed is not of length KeySeedSize.
func newKeyFromSeed(seed []byte) (*PublicKey, *PrivateKey) {
if len(seed) != KeySeedSize {
panic("seed must be of length KeySeedSize")
}
var sk PrivateKey
var pk PublicKey
var E nByNbarU16
var byteSE [2 * (len(sk.matrixS) + len(E))]byte
var A nByNU16
// Generate the secret value s, and the seed for S, E, and A. Add seedA to the public key
shake128 := sha3.NewShake128()
_, _ = shake128.Write(seed[2*SharedKeySize:])
_, _ = shake128.Read(pk.seedA[:])
shake128.Reset()
_, _ = shake128.Write([]byte{0x5F})
_, _ = shake128.Write(seed[SharedKeySize : 2*SharedKeySize])
_, _ = shake128.Read(byteSE[:])
i := 0
for i < len(sk.matrixS) {
sk.matrixS[i] = uint16(byteSE[i*2]) | (uint16(byteSE[(i*2)+1]) << 8)
i++
}
sample(sk.matrixS[:])
for j := range E {
E[j] = uint16(byteSE[i*2]) | (uint16(byteSE[(i*2)+1]) << 8)
i++
}
sample(E[:])
expandSeedIntoA(&A, &pk.seedA, &shake128)
mulAddASPlusE(&pk.matrixB, &A, &sk.matrixS, &E)
// Populate the private key
copy(sk.hashInputIfDecapsFail[:], seed[0:SharedKeySize])
sk.pk = &pk
// Add H(pk) to the private key
shake128.Reset()
var ppk [PublicKeySize]byte
pk.Pack(ppk[:])
_, _ = shake128.Write(ppk[:])
_, _ = shake128.Read(sk.hpk[:])
return &pk, &sk
}
// GenerateKeyPair generates public and private keys using entropy from rand.
// If rand is nil, crypto/rand.Reader will be used.
func generateKeyPair(rand io.Reader) (*PublicKey, *PrivateKey, error) {
var seed [KeySeedSize]byte
if rand == nil {
rand = cryptoRand.Reader
}
_, err := io.ReadFull(rand, seed[:])
if err != nil {
return nil, nil, err
}
pk, sk := newKeyFromSeed(seed[:])
return pk, sk, err
}
// EncapsulateTo generates a shared key and a ciphertext containing said key
// from the public key and the randomness from seed and writes the shared key
// to ss and ciphertext to ct.
//
// Panics if ss, ct, or seed are not of length SharedKeySize, CiphertextSize
// and EncapsulationSeedSize respectively.
//
// seed may be nil, in which case crypto/rand.Reader is used to generate one.
func (pk *PublicKey) EncapsulateTo(ct []byte, ss []byte, seed []byte) {
if seed == nil {
seed = make([]byte, EncapsulationSeedSize)
if _, err := cryptoRand.Read(seed[:]); err != nil {
panic(err)
}
}
if len(seed) != EncapsulationSeedSize {
panic("seed must be of length EncapsulationSeedSize")
}
if len(ct) != CiphertextSize {
panic("ct must be of length CiphertextSize")
}
if len(ss) != SharedKeySize {
panic("ss must be of length SharedKeySize")
}
var G2out [2 * SharedKeySize]byte
var SpEpEpp [(paramN * paramNbar) + (paramN * paramNbar) + (paramNbar * paramNbar)]uint16
var byteSpEpEpp [2 * len(SpEpEpp)]byte
Sp := SpEpEpp[:paramN*paramNbar]
Ep := SpEpEpp[paramN*paramNbar : 2*paramN*paramNbar]
Epp := SpEpEpp[2*paramN*paramNbar:]
var Bp nbarByNU16
var V nbarByNbarU16
var C nbarByNbarU16
var A nByNU16
var hpk [pkHashSize]byte
var mu [messageSize]byte
copy(mu[:], seed[:messageSize])
// compute hpk = G_1(packed(pk))
shake128 := sha3.NewShake128()
var ppk [PublicKeySize]byte
pk.Pack(ppk[:])
_, _ = shake128.Write(ppk[:])
_, _ = shake128.Read(hpk[:])
// compute (seedSE || k) = G_2(hpk || mu)
shake128.Reset()
_, _ = shake128.Write(hpk[:])
_, _ = shake128.Write(mu[:])
_, _ = shake128.Read(G2out[:])
// Generate Sp, Ep, Epp, and A, and compute:
// Bp = Sp*A + Ep
// V = Sp*B + Epp
shake128.Reset()
_, _ = shake128.Write([]byte{0x96})
_, _ = shake128.Write(G2out[:SharedKeySize])
_, _ = shake128.Read(byteSpEpEpp[:])
for i := range SpEpEpp {
SpEpEpp[i] = uint16(byteSpEpEpp[i*2]) | (uint16(byteSpEpEpp[(i*2)+1]) << 8)
}
sample(SpEpEpp[:])
expandSeedIntoA(&A, &pk.seedA, &shake128)
mulAddSAPlusE(&Bp, Sp, &A, Ep)
mulAddSBPlusE(&V, Sp, &pk.matrixB, Epp)
// Encode mu, and compute C = V + enc(mu) (mod q)
encodeMessage(&C, &mu)
add(&C, &V, &C)
// Prepare the ciphertext
pack(ct[:matrixBpPackedSize], Bp[:])
pack(ct[matrixBpPackedSize:], C[:])
// Compute ss = F(ct||k)
shake128.Reset()
_, _ = shake128.Write(ct[:])
_, _ = shake128.Write(G2out[SharedKeySize:])
_, _ = shake128.Read(ss[:])
}
// DecapsulateTo computes the shared key that is encapsulated in ct
// from the private key.
//
// Panics if ct or ss are not of length CiphertextSize and SharedKeySize
// respectively.
func (sk *PrivateKey) DecapsulateTo(ss, ct []byte) {
if len(ct) != CiphertextSize {
panic("ct must be of length CiphertextSize")
}
if len(ss) != SharedKeySize {
panic("ss must be of length SharedKeySize")
}
var Bp nbarByNU16
var C nbarByNbarU16
var W nbarByNbarU16
var CC nbarByNbarU16
var BBp nbarByNU16
var SpEpEpp [(paramN * paramNbar) + (paramN * paramNbar) + (paramNbar * paramNbar)]uint16
var byteSpEpEpp [2 * len(SpEpEpp)]byte
Sp := SpEpEpp[:paramN*paramNbar]
Ep := SpEpEpp[paramN*paramNbar : 2*paramN*paramNbar]
Epp := SpEpEpp[2*paramN*paramNbar:]
var A nByNU16
var muprime [messageSize]byte
var G2out [2 * SharedKeySize]byte
kprime := G2out[SharedKeySize:]
// Compute W = C - Bp*S (mod q), and decode the randomness mu
unpack(Bp[:], ct[0:matrixBpPackedSize])
unpack(C[:], ct[matrixBpPackedSize:])
mulBS(&W, &Bp, &sk.matrixS)
sub(&W, &C, &W)
decodeMessage(&muprime, &W)
// Generate (seedSE' || k') = G_2(hpk || mu')
shake128 := sha3.NewShake128()
_, _ = shake128.Write(sk.hpk[:])
_, _ = shake128.Write(muprime[:])
_, _ = shake128.Read(G2out[:])
// Generate Sp, Ep, Epp, A, and compute BBp = Sp*A + Ep.
shake128.Reset()
_, _ = shake128.Write([]byte{0x96})
_, _ = shake128.Write(G2out[:SharedKeySize])
_, _ = shake128.Read(byteSpEpEpp[:])
for i := range SpEpEpp {
SpEpEpp[i] = uint16(byteSpEpEpp[i*2]) | (uint16(byteSpEpEpp[(i*2)+1]) << 8)
}
sample(SpEpEpp[:])
expandSeedIntoA(&A, &sk.pk.seedA, &shake128)
mulAddSAPlusE(&BBp, Sp[:], &A, Ep[:])
// Reduce BBp modulo q
for i := range BBp {
BBp[i] = BBp[i] & logQMask
}
// compute W = Sp*B + Epp
mulAddSBPlusE(&W, Sp, &sk.pk.matrixB, Epp)
// Encode mu, and compute CC = W + enc(mu') (mod q)
encodeMessage(&CC, &muprime)
add(&CC, &W, &CC)
// Prepare input to F
// If (Bp == BBp & C == CC) then ss = F(ct || k'), else ss = F(ct || s)
// Needs to avoid branching on secret data as per:
// Qian Guo, Thomas Johansson, Alexander Nilsson. A key-recovery timing attack on post-quantum
// primitives using the Fujisaki-Okamoto transformation and its application on FrodoKEM. In CRYPTO 2020.
selector := ctCompareU16(Bp[:], BBp[:]) | ctCompareU16(C[:], CC[:])
// If (selector == 0) then load k' to do ss = F(ct || k'), else if (selector == 1) load s to do ss = F(ct || s)
subtle.ConstantTimeCopy(selector, kprime[:], sk.hashInputIfDecapsFail[:])
shake128.Reset()
_, _ = shake128.Write(ct[:])
_, _ = shake128.Write(kprime[:])
_, _ = shake128.Read(ss[:])
}
// Packs sk to buf.
//
// Panics if buf is not of size PrivateKeySize.
func (sk *PrivateKey) Pack(buf []byte) {
if len(buf) != PrivateKeySize {
panic("buf must be of length PrivateKeySize")
}
copy(buf[:SharedKeySize], sk.hashInputIfDecapsFail[:])
buf = buf[SharedKeySize:]
sk.pk.Pack(buf[:PublicKeySize])
buf = buf[PublicKeySize:]
j := 0
for i := range sk.matrixS {
buf[j] = byte(sk.matrixS[i])
buf[j+1] = byte(sk.matrixS[i] >> 8)
j += 2
}
buf = buf[j:]
copy(buf[:], sk.hpk[:])
}
// Unpacks sk from buf.
//
// Panics if buf is not of size PrivateKeySize.
func (sk *PrivateKey) Unpack(buf []byte) {
if len(buf) != PrivateKeySize {
panic("buf must be of length PrivateKeySize")
}
copy(sk.hashInputIfDecapsFail[:], buf[:SharedKeySize])
buf = buf[SharedKeySize:]
sk.pk = new(PublicKey)
sk.pk.Unpack(buf[:PublicKeySize])
buf = buf[PublicKeySize:]
for i := range sk.matrixS {
sk.matrixS[i] = uint16(buf[i*2]) | (uint16(buf[(i*2)+1]) << 8)
}
buf = buf[len(sk.matrixS)*2:]
copy(sk.hpk[:], buf[:])
}
// Packs pk to buf.
//
// Panics if buf is not of size PublicKeySize.
func (pk *PublicKey) Pack(buf []byte) {
if len(buf) != PublicKeySize {
panic("buf must be of length PublicKeySize")
}
copy(buf[:seedASize], pk.seedA[:])
pack(buf[seedASize:], pk.matrixB[:])
}
// TODO: Unpacks pk from buf.
//
// Panics if buf is not of size PublicKeySize.
func (pk *PublicKey) Unpack(buf []byte) {
if len(buf) != PublicKeySize {
panic("buf must be of length PublicKeySize")
}
copy(pk.seedA[:], buf[:seedASize])
unpack(pk.matrixB[:], buf[seedASize:])
}
// Boilerplate down below for the KEM scheme API.
type scheme struct{}
var sch kem.Scheme = &scheme{}
// Scheme returns a KEM interface.
func Scheme() kem.Scheme { return sch }
func (scheme) Name() string { return "FrodoKEM-640-SHAKE" }
func (*scheme) PublicKeySize() int { return PublicKeySize }
func (*scheme) PrivateKeySize() int { return PrivateKeySize }
func (*scheme) SeedSize() int { return KeySeedSize }
func (*scheme) SharedKeySize() int { return SharedKeySize }
func (*scheme) CiphertextSize() int { return CiphertextSize }
func (*scheme) EncapsulationSeedSize() int { return EncapsulationSeedSize }
func (sk *PrivateKey) Scheme() kem.Scheme { return sch }
func (pk *PublicKey) Scheme() kem.Scheme { return sch }
func (sk *PrivateKey) MarshalBinary() ([]byte, error) {
var ret [PrivateKeySize]byte
sk.Pack(ret[:])
return ret[:], nil
}
func (sk *PrivateKey) Equal(other kem.PrivateKey) bool {
oth, ok := other.(*PrivateKey)
if !ok {
return false
}
if sk.pk == nil && oth.pk == nil {
return true
}
if sk.pk == nil || oth.pk == nil {
return false
}
return ctCompareU16(sk.matrixS[:], oth.matrixS[:]) == 0 &&
subtle.ConstantTimeCompare(sk.hashInputIfDecapsFail[:], oth.hashInputIfDecapsFail[:]) == 1 &&
sk.pk.Equal(oth.pk) &&
bytes.Equal(sk.hpk[:], oth.hpk[:])
}
func (pk *PublicKey) Equal(other kem.PublicKey) bool {
oth, ok := other.(*PublicKey)
if !ok {
return false
}
if pk == nil && oth == nil {
return true
}
if pk == nil || oth == nil {
return false
}
for i := range pk.matrixB {
if (pk.matrixB[i] & logQMask) != (oth.matrixB[i] & logQMask) {
return false
}
}
return bytes.Equal(pk.seedA[:], oth.seedA[:])
}
func (sk *PrivateKey) Public() kem.PublicKey {
return sk.pk
}
func (pk *PublicKey) MarshalBinary() ([]byte, error) {
var ret [PublicKeySize]byte
pk.Pack(ret[:])
return ret[:], nil
}
func (*scheme) GenerateKeyPair() (kem.PublicKey, kem.PrivateKey, error) {
return generateKeyPair(cryptoRand.Reader)
}
func (*scheme) DeriveKeyPair(seed []byte) (kem.PublicKey, kem.PrivateKey) {
if len(seed) != KeySeedSize {
panic(kem.ErrSeedSize)
}
return newKeyFromSeed(seed[:])
}
func (*scheme) Encapsulate(pk kem.PublicKey) (ct, ss []byte, err error) {
ct = make([]byte, CiphertextSize)
ss = make([]byte, SharedKeySize)
pub, ok := pk.(*PublicKey)
if !ok {
return nil, nil, kem.ErrTypeMismatch
}
pub.EncapsulateTo(ct, ss, nil)
return
}
func (*scheme) EncapsulateDeterministically(
pk kem.PublicKey, seed []byte,
) (ct, ss []byte, err error) {
if len(seed) != EncapsulationSeedSize {
return nil, nil, kem.ErrSeedSize
}
ct = make([]byte, CiphertextSize)
ss = make([]byte, SharedKeySize)
pub, ok := pk.(*PublicKey)
if !ok {
return nil, nil, kem.ErrTypeMismatch
}
pub.EncapsulateTo(ct, ss, seed)
return
}
func (*scheme) Decapsulate(sk kem.PrivateKey, ct []byte) ([]byte, error) {
if len(ct) != CiphertextSize {
return nil, kem.ErrCiphertextSize
}
priv, ok := sk.(*PrivateKey)
if !ok {
return nil, kem.ErrTypeMismatch
}
ss := make([]byte, SharedKeySize)
priv.DecapsulateTo(ss, ct)
return ss, nil
}
func (*scheme) UnmarshalBinaryPublicKey(buf []byte) (kem.PublicKey, error) {
if len(buf) != PublicKeySize {
return nil, kem.ErrPubKeySize
}
var ret PublicKey
ret.Unpack(buf)
return &ret, nil
}
func (*scheme) UnmarshalBinaryPrivateKey(buf []byte) (kem.PrivateKey, error) {
if len(buf) != PrivateKeySize {
return nil, kem.ErrPrivKeySize
}
var ret PrivateKey
ret.Unpack(buf)
return &ret, nil
}
|