Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tkn20,kyber,x25519,x448: plug constant-time leaks #411

Merged
merged 2 commits into from
Mar 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
tkn20,kyber,x25519,x448: plug constant-time leaks
In particular leaking z in kyber could be quite damaging:
https://groups.google.com/a/list.nist.gov/g/pqc-forum/c/SJ31w0QSmIM/m/XgdBgh3wAwAJ

The changes to x25519 and x448 are unlikely to be needed, but it's more
idiomatic at least.
  • Loading branch information
tmthrgd committed Mar 6, 2023
commit 49af35bf5e8476a717557fcd296a7d8a5e379a74
5 changes: 3 additions & 2 deletions abe/cpabe/tkn20/internal/tkn/tk.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
package tkn

import (
"bytes"
"crypto/subtle"
"encoding/binary"
"fmt"
"io"
Expand Down Expand Up @@ -181,7 +181,8 @@ func (s *SecretParams) UnmarshalBinary(data []byte) error {

func (s *SecretParams) Equal(s2 *SecretParams) bool {
return s.a.Equal(s2.a) && s.wtA.Equal(s2.wtA) && s.bstar.Equal(s2.bstar) &&
s.bstar12.Equal(s2.bstar12) && s.k.Equal(s2.k) && bytes.Equal(s.prfKey, s2.prfKey)
s.bstar12.Equal(s2.bstar12) && s.k.Equal(s2.k) &&
subtle.ConstantTimeCompare(s.prfKey, s2.prfKey) == 1
}

type AttributesKey struct {
Expand Down
6 changes: 3 additions & 3 deletions dh/x25519/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ func (k *Key) clamp(in *Key) *Key {
// isValidPubKey verifies if the public key is not a low-order point.
func (k *Key) isValidPubKey() bool {
fp.Modp((*fp.Elt)(k))
isLowOrder := false
var isLowOrder int
for _, P := range lowOrderPoints {
isLowOrder = isLowOrder || subtle.ConstantTimeCompare(P[:], k[:]) != 0
isLowOrder |= subtle.ConstantTimeCompare(P[:], k[:])
}
return !isLowOrder
return isLowOrder == 0
}

// KeyGen obtains a public key given a secret key.
Expand Down
6 changes: 3 additions & 3 deletions dh/x448/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ func (k *Key) clamp(in *Key) *Key {
// isValidPubKey verifies if the public key is not a low-order point.
func (k *Key) isValidPubKey() bool {
fp.Modp((*fp.Elt)(k))
isLowOrder := false
var isLowOrder int
for _, P := range lowOrderPoints {
isLowOrder = isLowOrder || subtle.ConstantTimeCompare(P[:], k[:]) != 0
isLowOrder |= subtle.ConstantTimeCompare(P[:], k[:])
}
return !isLowOrder
return isLowOrder == 0
}

// KeyGen obtains a public key given a secret key.
Expand Down
2 changes: 1 addition & 1 deletion kem/kyber/kyber1024/kyber.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion kem/kyber/kyber512/kyber.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion kem/kyber/kyber768/kyber.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion kem/kyber/templates/pkg.templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion pke/kyber/kyber1024/internal/cpapke.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion pke/kyber/kyber512/internal/cpapke.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package internal

import (
"crypto/subtle"

"github.com/cloudflare/circl/internal/sha3"
"github.com/cloudflare/circl/pke/kyber/internal/common"
)
Expand Down Expand Up @@ -170,5 +172,5 @@ func (sk *PrivateKey) Equal(other *PrivateKey) bool {
ret |= sk.sh[i][j] ^ other.sh[i][j]
}
}
return ret == 0
return subtle.ConstantTimeEq(int32(ret), 0) == 1
}
4 changes: 3 additions & 1 deletion pke/kyber/kyber768/internal/cpapke.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.