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

tss/rsa: Rewrite serialization functions for KeyShare and SignShare #454

Open
wants to merge 1 commit into
base: tssRSAverif
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 35 additions & 0 deletions internal/conv/conv.go
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"math/big"
"strings"

"golang.org/x/crypto/cryptobyte"
)

// BytesLe2Hex returns an hexadecimal string of a number stored in a
Expand Down Expand Up @@ -138,3 +140,36 @@ func BigInt2Uint64Le(z []uint64, x *big.Int) {
z[i] = 0
}
}

// MarshalBinary encodes a value into a byte array in a format readable by UnmarshalBinary.
func MarshalBinary(v cryptobyte.MarshalingValue) ([]byte, error) {
var b cryptobyte.Builder
b.AddValue(v)
return b.Bytes()
}

// A UnmarshalingValue decodes itself from a cryptobyte.String and advances the pointer.
// Returns true indicating the reading was successful.
type UnmarshalingValue interface {
ReadValue(*cryptobyte.String) bool
}

// UnmarshalBinary recovers a value from a byte array.
// Returns an error if the recovered value is invalid.
// Any panic raised when calling to ReadValue is recovered and an error is returned instead.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this helper function generically implements UnmarshalBinary if there is an implementation of ReadValue (which internally uses the cryptobyte package).

func UnmarshalBinary(v UnmarshalingValue, data []byte) (err error) {
defer func() {
r := recover()
if r != nil {
err = fmt.Errorf("%T failed to unmarshal: %v", v, r)
}
}()

r := cryptobyte.String(data)
ok := v.ReadValue(&r)
if !ok {
return fmt.Errorf("cannot read %T from input string", v)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if there are trailing bytes in data?

Copy link
Contributor Author

@armfazh armfazh Jan 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it consumes only the data needed by the implementor, so this allows to do reading continuations in composed data structures. User can call Empty() to check for trailing data.


return nil
}
25 changes: 25 additions & 0 deletions internal/test/test.go
@@ -1,6 +1,8 @@
package test

import (
"bytes"
"encoding"
"errors"
"fmt"
"strings"
Expand Down Expand Up @@ -58,3 +60,26 @@ func CheckPanic(f func()) error {
f()
return hasPanicked
}

func CheckMarshal(
t *testing.T,
x, y interface {
encoding.BinaryMarshaler
encoding.BinaryUnmarshaler
},
) {
t.Helper()

want, err := x.MarshalBinary()
CheckNoErr(t, err, fmt.Sprintf("cannot marshal %T = %v", x, x))

err = y.UnmarshalBinary(want)
CheckNoErr(t, err, fmt.Sprintf("cannot unmarshal %T from %x", y, want))

got, err := y.MarshalBinary()
CheckNoErr(t, err, fmt.Sprintf("cannot marshal %T = %v", y, y))

if !bytes.Equal(got, want) {
ReportError(t, got, want, x, y)
}
}