-
Notifications
You must be signed in to change notification settings - Fork 146
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
base: tssRSAverif
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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. | ||
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) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if there are trailing bytes in There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
||
return nil | ||
} |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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).