Skip to content

bls12381: Detects invalid prefix in G1 and G2 serialized elements #500

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

Merged
merged 2 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
56 changes: 37 additions & 19 deletions ecc/bls12381/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,41 @@
// is the lexicographically largest of the two associated with the encoded
// x-coordinate.
//
// |----------------------------------------------------|
// | Serialization Format |
// |-----|-------|-------|---------------|--------------|
// | MSB | MSB-1 | MSB-2 | Description | Encoding |
// |-----|-------|-------|---------------|--------------|
// | 0 | X | X | Uncompressed | e || x || y |
// | 1 | X | X | Compressed | e || x |
// |-----|-------|-------|---------------|--------------|
// | X | 0 | X | Non-Infinity | e || x || y |
// | X | 1 | X | Infinity | e || 0 || 0 |
// |-----|-------|-------|---------------|--------------|
// | | | | Compressed, | |
// | 1 | 0 | 1 | Non-Infinity, | e || x |
// | | | | Big y-coord | |
// |-----|-------|-------|---------------|--------------|
// | | | | Compressed, | |
// | 1 | 0 | 0 | Non-Infinity, | e || x |
// | | | | Small y-coord | |
// |----------------------------------------------------|
// |------------------------------------------------------|
// | Serialization Format |
// |-----|-------|-------|-----------------|--------------|
// | MSB | MSB-1 | MSB-2 | Description | Encoding |
// |-----|-------|-------|-----------------|--------------|
// | | | | Non-compressed, | |
// | 0 | 0 | 0 | Non-Infinity, | e || x || y |
// | | | | Zero. | |
// |-----|-------|-------|-----------------|--------------|
// | | | | Non-compressed, | |
// | 0 | 0 | 1 | Non-Infinity, | Invalid |
// | | | | One. | |
// |-----|-------|-------|-----------------|--------------|
// | | | | Non-compressed, | |
// | 0 | 1 | 0 | Infinity, | e || 0 || 0 |
// | | | | Zero. | |
// |-----|-------|-------|-----------------|--------------|
// | | | | Non-compressed, | |
// | 0 | 1 | 1 | Infinity, | Invalid |
// | | | | One. | |
// |-----|-------|-------|-----------------|--------------|
// | | | | Compressed, | |
// | 1 | 0 | 0 | Non-Infinity, | e || x |
// | | | | Small y-coord | |
// |-----|-------|-------|-----------------|--------------|
// | | | | Compressed, | |
// | 1 | 0 | 1 | Non-Infinity, | e || x |
// | | | | Big y-coord | |
// |-----|-------|-------|-----------------|--------------|
// | | | | Compressed, | |
// | 1 | 1 | 0 | Infinity, | e || 0 |
// | | | | Zero. | |
// |-----|-------|-------|-----------------|--------------|
// | | | | Compressed, | |
// | 1 | 1 | 1 | Infinity, | Invalid |
// | | | | One. | |
// |------------------------------------------------------|
package bls12381
8 changes: 7 additions & 1 deletion ecc/bls12381/g1.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ func (g *G1) SetBytes(b []byte) error {
return errInputLength
}

// Check for invalid prefixes
switch b[0] & 0xE0 {
case 0x20, 0x60, 0xE0:
return errEncoding
}

isCompressed := int((b[0] >> 7) & 0x1)
isInfinity := int((b[0] >> 6) & 0x1)
isBigYCoord := int((b[0] >> 5) & 0x1)
Expand All @@ -44,7 +50,7 @@ func (g *G1) SetBytes(b []byte) error {
l = G1SizeCompressed
}
zeros := make([]byte, l-1)
if (b[0]&0x1F) != 0 || subtle.ConstantTimeCompare(b[1:], zeros) != 1 {
if (b[0]&0x1F) != 0 || subtle.ConstantTimeCompare(b[1:l], zeros) != 1 {
return errEncoding
}
g.SetIdentity()
Expand Down
7 changes: 7 additions & 0 deletions ecc/bls12381/g1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,13 @@ func TestG1Serial(t *testing.T) {
want = *randomG1(t)
}
})
t.Run("badPrefix", func(t *testing.T) {
q := new(G1)
b := make([]byte, G1Size)
for _, b[0] = range []byte{0x20, 0x60, 0xE0} {
test.CheckIsErr(t, q.SetBytes(b), mustErr)
}
})
t.Run("badLength", func(t *testing.T) {
q := new(G1)
p := randomG1(t)
Expand Down
8 changes: 7 additions & 1 deletion ecc/bls12381/g2.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ func (g *G2) SetBytes(b []byte) error {
return errInputLength
}

// Check for invalid prefixes
switch b[0] & 0xE0 {
case 0x20, 0x60, 0xE0:
return errEncoding
}

isCompressed := int((b[0] >> 7) & 0x1)
isInfinity := int((b[0] >> 6) & 0x1)
isBigYCoord := int((b[0] >> 5) & 0x1)
Expand All @@ -42,7 +48,7 @@ func (g *G2) SetBytes(b []byte) error {
l = G2SizeCompressed
}
zeros := make([]byte, l-1)
if (b[0]&0x1F) != 0 || subtle.ConstantTimeCompare(b[1:], zeros) != 1 {
if (b[0]&0x1F) != 0 || subtle.ConstantTimeCompare(b[1:l], zeros) != 1 {
return errEncoding
}
g.SetIdentity()
Expand Down
7 changes: 7 additions & 0 deletions ecc/bls12381/g2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ func TestG2Serial(t *testing.T) {
want = *randomG2(t)
}
})
t.Run("badPrefix", func(t *testing.T) {
q := new(G2)
b := make([]byte, G2Size)
for _, b[0] = range []byte{0x20, 0x60, 0xE0} {
test.CheckIsErr(t, q.SetBytes(b), mustErr)
}
})
t.Run("badLength", func(t *testing.T) {
q := new(G2)
p := randomG2(t)
Expand Down
Loading