Skip to content

Commit

Permalink
tkn20,kyber,x25519,x448: plug constant-time leaks
Browse files Browse the repository at this point in the history
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 authored and armfazh committed Mar 14, 2023
1 parent a5c5796 commit 588a0e8
Show file tree
Hide file tree
Showing 10 changed files with 22 additions and 15 deletions.
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.

0 comments on commit 588a0e8

Please sign in to comment.