Skip to content

Commit 809fa4f

Browse files
authored
Add group test for serializations (#30)
* added tests for invalid groups in serializations * remove leftover check Signed-off-by: bytemare <[email protected]>
1 parent d51f93e commit 809fa4f

File tree

7 files changed

+72
-24
lines changed

7 files changed

+72
-24
lines changed

.github/workflows/ci.yml

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
name: OPAQUE
22
on:
3-
push:
4-
branches: [ main ]
53
pull_request:
6-
# The branches below must be a subset of the branches above
7-
branches: [ main ]
4+
branches:
5+
- main
86

97
permissions:
108
contents: read
@@ -55,7 +53,6 @@ jobs:
5553

5654
analyze:
5755
name: Analyze
58-
if: github.event_name == 'push'
5956
runs-on: ubuntu-latest
6057
steps:
6158
- name: Checkout repo
@@ -87,6 +84,8 @@ jobs:
8784
args: >
8885
-Dsonar.projectKey=bytemare_opaque
8986
-Dsonar.organization=bytemare-github
90-
-Dsonar.go.coverage.reportPaths=coverage.out -Dsonar.sources=.
91-
-Dsonar.test.exclusions=tests/** -Dsonar.tests=tests/
87+
-Dsonar.go.coverage.reportPaths=coverage.out
88+
-Dsonar.sources=.
89+
-Dsonar.test.exclusions=examples_test.go,tests/**
90+
-Dsonar.tests=tests/
9291
-Dsonar.verbose=true

.github/workflows/codeql.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
name: "CodeQL"
22

33
on:
4-
push:
5-
branches: [ main ]
64
pull_request:
7-
# The branches below must be a subset of the branches above
8-
branches: [ main ]
5+
branches:
6+
- main
97
schedule:
108
- cron: '31 10 * * 0'
119

.github/workflows/snyk.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
name: Snyk
22

33
on:
4-
push:
5-
branches: [ main ]
64
pull_request:
7-
# The branches below must be a subset of the branches above
85
branches: [ main ]
96
schedule:
107
- cron: '31 10 * * 0'

deserializer.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ package opaque
1010

1111
import (
1212
"errors"
13-
"log"
1413

1514
"github.com/bytemare/crypto/group"
1615

@@ -191,8 +190,6 @@ func (d *Deserializer) KE2(ke2 []byte) (*message.KE2, error) {
191190
return nil, errInvalidServerEPK
192191
}
193192

194-
log.Printf("group %v", d.conf.Group)
195-
196193
return &message.KE2{
197194
G: d.conf.Group,
198195
CredentialResponse: cresp,

internal/encoding/group.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
// Package encoding provides encoding utilities.
1010
package encoding
1111

12-
import "github.com/bytemare/crypto/group"
12+
import (
13+
"github.com/bytemare/crypto/group"
14+
)
1315

1416
const (
1517
ristrettoPointLength = 32
@@ -46,7 +48,10 @@ var PointLength = map[group.Group]int{
4648

4749
// SerializeScalar pads the given scalar if necessary.
4850
func SerializeScalar(s *group.Scalar, g group.Group) []byte {
49-
length := ScalarLength[g]
51+
length, ok := ScalarLength[g]
52+
if !ok {
53+
panic("invalid group identifier")
54+
}
5055

5156
e := s.Bytes()
5257

tests/encoding_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@ package opaque_test
1111
import (
1212
"bytes"
1313
"encoding/hex"
14+
"errors"
1415
"fmt"
1516
"testing"
1617

18+
"github.com/bytemare/crypto/group"
19+
1720
"github.com/bytemare/opaque/internal/encoding"
1821
)
1922

@@ -215,3 +218,57 @@ func expectPanic(expectedError error, f func()) (bool, string) {
215218

216219
return true, ""
217220
}
221+
222+
func TestSerializeScalarGroups(t *testing.T) {
223+
// Valid Configurations
224+
for _, conf := range confs {
225+
g := group.Group(conf.Conf.AKE)
226+
s := g.NewScalar().Random()
227+
if hasPanic, err := hasPanic(func() { _ = encoding.SerializeScalar(s, g) }); hasPanic {
228+
t.Fatalf("unexpected panic for valid group and scalar: %v", err)
229+
}
230+
}
231+
232+
// Invalid Configuration
233+
g := group.Group(0)
234+
expect := errors.New("invalid group identifier")
235+
if hasPanic, err := expectPanic(expect, func() { _ = encoding.SerializeScalar(nil, g) }); !hasPanic {
236+
t.Fatalf("expected panic for invalid group and scalar:\n\twant: %v\n\tgot: %v", expect, err)
237+
}
238+
}
239+
240+
func TestSerializeScalar(t *testing.T) {
241+
length := 16
242+
for _, conf := range confs {
243+
g := group.Group(conf.Conf.AKE)
244+
encoded := [32]byte{}
245+
encoded[length] = 1
246+
s, err := g.NewScalar().Decode(encoded[:])
247+
if err != nil {
248+
t.Fatal(err)
249+
}
250+
251+
ser := encoding.SerializeScalar(s, g)
252+
if len(ser) == length {
253+
t.Fatalf("serialization did not pad correctly. Before %d / After %d", length, len(ser))
254+
}
255+
}
256+
}
257+
258+
func TestSerializePointGroups(t *testing.T) {
259+
// Valid Configurations
260+
for _, conf := range confs {
261+
g := group.Group(conf.Conf.AKE)
262+
p := g.Base()
263+
if hasPanic, err := hasPanic(func() { _ = encoding.SerializePoint(p, g) }); hasPanic {
264+
t.Fatalf("unexpected panic for valid group and point: %v", err)
265+
}
266+
}
267+
268+
// Invalid Configuration
269+
g := group.Group(0)
270+
expect := errors.New("invalid group identifier")
271+
if hasPanic, err := expectPanic(expect, func() { _ = encoding.SerializePoint(nil, g) }); !hasPanic {
272+
t.Fatalf("expected panic for invalid group and point:\n\twant: %v\n\tgot: %v", expect, err)
273+
}
274+
}

tests/oprf_test.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,6 @@ import (
2626
"github.com/bytemare/opaque/internal/tag"
2727
)
2828

29-
func TestOPRFString(t *testing.T) {
30-
p := oprf.RistrettoSha512
31-
fmt.Println(p)
32-
}
33-
3429
type oprfVector struct {
3530
DST string `json:"groupDST"`
3631
Hash string `json:"hash"`

0 commit comments

Comments
 (0)