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

Use hash size as KSF output size, expose KSF parameters & salt in configuration #73

Open
wants to merge 1 commit into
base: main
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (c *Client) GetConf() *internal.Configuration {
// buildPRK derives the randomized password from the OPRF output.
func (c *Client) buildPRK(evaluation *group.Element) []byte {
output := c.OPRF.Finalize(evaluation)
stretched := c.conf.KSF.Harden(output, nil, c.conf.OPRF.Group().ElementLength())
stretched := c.conf.KSF.Harden(output, c.conf.KSFSalt, c.conf.Hash.Size())

return c.conf.KDF.Extract(nil, encoding.Concat(output, stretched))
}
Expand Down
16 changes: 9 additions & 7 deletions examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func isSameConf(a, b *opaque.Configuration) bool {
a.KDF != b.KDF ||
a.MAC != b.MAC ||
a.Hash != b.Hash ||
a.KSF != b.KSF ||
!reflect.DeepEqual(a.KSF, b.KSF) ||
a.AKE != b.AKE {
return false
}
Expand All @@ -48,11 +48,13 @@ func Example_configuration() {
defaultConf := opaque.DefaultConfiguration()

customConf := &opaque.Configuration{
OPRF: opaque.RistrettoSha512,
KDF: crypto.SHA512,
MAC: crypto.SHA512,
Hash: crypto.SHA512,
KSF: ksf.Argon2id,
OPRF: opaque.RistrettoSha512,
KDF: crypto.SHA512,
MAC: crypto.SHA512,
Hash: crypto.SHA512,
KSF: opaque.KSFConfiguration{
Identifier: ksf.Argon2id,
},
Comment on lines +51 to +57
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
OPRF: opaque.RistrettoSha512,
KDF: crypto.SHA512,
MAC: crypto.SHA512,
Hash: crypto.SHA512,
KSF: opaque.KSFConfiguration{
Identifier: ksf.Argon2id,
},
OPRF: opaque.RistrettoSha512,
KDF: crypto.SHA512,
MAC: crypto.SHA512,
Hash: crypto.SHA512,
KSF: opaque.KSFConfiguration{
Identifier: ksf.Argon2id,
},

AKE: opaque.RistrettoSha512,
Context: nil,
}
Expand All @@ -79,7 +81,7 @@ func Example_configuration() {

fmt.Println("OPAQUE configuration is easy!")

// Output: Encoded Configuration: 0107070701010000
// Output: Encoded Configuration: 010707070101000000000000
// OPAQUE configuration is easy!
}

Expand Down
1 change: 1 addition & 0 deletions internal/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type Configuration struct {
MAC *Mac
Hash *Hash
KSF *KSF
KSFSalt []byte
OPRF oprf.Identifier
Context []byte
NonceLen int
Expand Down
3 changes: 3 additions & 0 deletions internal/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ type KSF struct {
type ksfInterface interface {
// Harden uses default parameters for the key derivation function over the input password and salt.
Harden(password, salt []byte, length int) []byte
Parameterize(parameters ...int)
}

// IdentityKSF represents a KSF with no operations.
Expand All @@ -117,3 +118,5 @@ type IdentityKSF struct{}
func (i IdentityKSF) Harden(password, _ []byte, _ int) []byte {
return password
}

func (i IdentityKSF) Parameterize(parameters ...int) {}
97 changes: 73 additions & 24 deletions opaque.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (g Group) Group() group.Group {
return group.Group(g)
}

const confLength = 6
const confIdsLength = 6

var (
errInvalidOPRFid = errors.New("invalid OPRF group id")
Expand All @@ -78,26 +78,34 @@ var (
errInvalidAKEid = errors.New("invalid AKE group id")
)

type KSFConfiguration struct {
Identifier ksf.Identifier `json:"identifier"`
Parameters []int `json:"parameters"`
Salt []byte `json:"salt"`
}

Comment on lines +81 to +86
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
type KSFConfiguration struct {
Identifier ksf.Identifier `json:"identifier"`
Parameters []int `json:"parameters"`
Salt []byte `json:"salt"`
}
type KSFConfiguration struct {
Parameters []int `json:"parameters"`
Salt []byte `json:"salt"`
Identifier ksf.Identifier `json:"identifier"`
}

aligns better in memory

// Configuration represents an OPAQUE configuration. Note that OprfGroup and AKEGroup are recommended to be the same,
// as well as KDF, MAC, Hash should be the same.
type Configuration struct {
Context []byte
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
Context []byte

KDF crypto.Hash `json:"kdf"`
MAC crypto.Hash `json:"mac"`
Hash crypto.Hash `json:"hash"`
OPRF Group `json:"oprf"`
KSF ksf.Identifier `json:"ksf"`
AKE Group `json:"group"`
KDF crypto.Hash `json:"kdf"`
MAC crypto.Hash `json:"mac"`
Hash crypto.Hash `json:"hash"`
OPRF Group `json:"oprf"`
KSF KSFConfiguration `json:"ksf"`
AKE Group `json:"group"`
Comment on lines +91 to +96
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
KDF crypto.Hash `json:"kdf"`
MAC crypto.Hash `json:"mac"`
Hash crypto.Hash `json:"hash"`
OPRF Group `json:"oprf"`
KSF KSFConfiguration `json:"ksf"`
AKE Group `json:"group"`
KSF KSFConfiguration `json:"ksf"`
Context []byte
KDF crypto.Hash `json:"kdf"`
MAC crypto.Hash `json:"mac"`
Hash crypto.Hash `json:"hash"`
OPRF Group `json:"oprf"`
KSF KSFConfiguration `json:"ksf"`
AKE Group `json:"group"`

memory alignement

}

// DefaultConfiguration returns a default configuration with strong parameters.
func DefaultConfiguration() *Configuration {
return &Configuration{
OPRF: RistrettoSha512,
KDF: crypto.SHA512,
MAC: crypto.SHA512,
Hash: crypto.SHA512,
KSF: ksf.Argon2id,
OPRF: RistrettoSha512,
KDF: crypto.SHA512,
MAC: crypto.SHA512,
Hash: crypto.SHA512,
KSF: KSFConfiguration{
Identifier: ksf.Argon2id,
},
AKE: RistrettoSha512,
Context: nil,
}
Expand Down Expand Up @@ -145,7 +153,7 @@ func (c *Configuration) verify() error {
return errInvalidHASHid
}

if c.KSF != 0 && !c.KSF.Available() {
if c.KSF.Identifier != 0 && !c.KSF.Identifier.Available() {
return errInvalidKSFid
}

Expand All @@ -166,13 +174,18 @@ func (c *Configuration) toInternal() (*internal.Configuration, error) {
KDF: internal.NewKDF(c.KDF),
MAC: mac,
Hash: internal.NewHash(c.Hash),
KSF: internal.NewKSF(c.KSF),
KSF: internal.NewKSF(c.KSF.Identifier),
KSFSalt: c.KSF.Salt,
NonceLen: internal.NonceLength,
EnvelopeSize: internal.NonceLength + mac.Size(),
Group: g,
Context: c.Context,
}

if c.KSF.Parameters != nil {
ip.KSF.Parameterize(c.KSF.Parameters...)
}

return ip, nil
}

Expand All @@ -189,16 +202,23 @@ func (c *Configuration) Deserializer() (*Deserializer, error) {

// Serialize returns the byte encoding of the Configuration structure.
func (c *Configuration) Serialize() []byte {
b := []byte{
ids := []byte{
byte(c.OPRF),
byte(c.KDF),
byte(c.MAC),
byte(c.Hash),
byte(c.KSF),
byte(c.KSF.Identifier),
byte(c.AKE),
}

return encoding.Concat(b, encoding.EncodeVector(c.Context))
var ksfEncodedParams []byte
for _, param := range c.KSF.Parameters {
ksfEncodedParams = append(ksfEncodedParams, encoding.I2OSP(param, 4)...)
}
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
}
}

ksfEncodedParams = encoding.EncodeVector(ksfEncodedParams)
ksfEncodedSalt := encoding.EncodeVector(c.KSF.Salt)

return encoding.Concatenate(ids, encoding.EncodeVector(c.Context), ksfEncodedParams, ksfEncodedSalt)
}

// GetFakeRecord creates a fake Client record to be used when no existing client record exists,
Expand Down Expand Up @@ -228,21 +248,50 @@ func (c *Configuration) GetFakeRecord(credentialIdentifier []byte) (*ClientRecor

// DeserializeConfiguration decodes the input and returns a Parameter structure.
func DeserializeConfiguration(encoded []byte) (*Configuration, error) {
if len(encoded) < confLength+2 { // corresponds to the configuration length + 2-byte encoding of empty context
if len(encoded) < confIdsLength+2 { // corresponds to the configuration length + 2-byte encoding of empty context
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
if len(encoded) < confIdsLength+2 { // corresponds to the configuration length + 2-byte encoding of empty context
if len(encoded) < confIdsLength+6 { // corresponds to the configuration length + 3*2-byte encoding of empty context and KSF parameters

return nil, internal.ErrConfigurationInvalidLength
}

ctx, _, err := encoding.DecodeVector(encoded[confLength:])
ctx, _, err := encoding.DecodeVector(encoded[confIdsLength:])
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
ctx, _, err := encoding.DecodeVector(encoded[confIdsLength:])
ctx, offset, err := encoding.DecodeVector(encoded[confIdsLength:])

use the returned offset

if err != nil {
return nil, fmt.Errorf("decoding the configuration context: %w", err)
}

var ksfParams []int
ksfParamOffset := confIdsLength + 2 + len(ctx)

if len(encoded) >= ksfParamOffset+2 {
ksfEncodedParams, _, err := encoding.DecodeVector(encoded[ksfParamOffset:])
if err != nil {
return nil, fmt.Errorf("decoding the ksf configuration parameters: %w", err)
}
for i := 0; i < len(ksfEncodedParams); i += 4 {
ksfParams = append(ksfParams, encoding.OS2IP(ksfEncodedParams[i:i+4]))
}
}

var ksfSalt []byte
ksfSaltOffset := ksfParamOffset + 2 + (len(ksfParams) * 4)
if len(encoded) >= ksfSaltOffset+2 {
ksfSalt, _, err = encoding.DecodeVector(encoded[ksfSaltOffset:])
if err != nil {
return nil, fmt.Errorf("decoding the ksf salt: %w", err)
}
if len(ksfSalt) == 0 {
ksfSalt = nil
}
}
Comment on lines +260 to +283
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
var ksfParams []int
ksfParamOffset := confIdsLength + 2 + len(ctx)
if len(encoded) >= ksfParamOffset+2 {
ksfEncodedParams, _, err := encoding.DecodeVector(encoded[ksfParamOffset:])
if err != nil {
return nil, fmt.Errorf("decoding the ksf configuration parameters: %w", err)
}
for i := 0; i < len(ksfEncodedParams); i += 4 {
ksfParams = append(ksfParams, encoding.OS2IP(ksfEncodedParams[i:i+4]))
}
}
var ksfSalt []byte
ksfSaltOffset := ksfParamOffset + 2 + (len(ksfParams) * 4)
if len(encoded) >= ksfSaltOffset+2 {
ksfSalt, _, err = encoding.DecodeVector(encoded[ksfSaltOffset:])
if err != nil {
return nil, fmt.Errorf("decoding the ksf salt: %w", err)
}
if len(ksfSalt) == 0 {
ksfSalt = nil
}
}
offset += confIdsLength
ksfEncodedParams, offsetKSF, err := encoding.DecodeVector(encoded[offset:])
if err != nil {
return nil, fmt.Errorf("decoding the ksf configuration parameters: %w", err)
}
offset += offsetKSF
var ksfParams []int
for i := 0; i < len(ksfEncodedParams); i += 4 {
ksfParams = append(ksfParams, encoding.OS2IP(ksfEncodedParams[i:i+4]))
}
ksfSalt, _, err := encoding.DecodeVector(encoded[offset:])
if err != nil {
return nil, fmt.Errorf("decoding the ksf salt: %w", err)
}
if len(ksfSalt) == 0 {
ksfSalt = nil
}

we can use the offset argument returned by DecodeVector


c := &Configuration{
OPRF: Group(encoded[0]),
KDF: crypto.Hash(encoded[1]),
MAC: crypto.Hash(encoded[2]),
Hash: crypto.Hash(encoded[3]),
KSF: ksf.Identifier(encoded[4]),
OPRF: Group(encoded[0]),
KDF: crypto.Hash(encoded[1]),
MAC: crypto.Hash(encoded[2]),
Hash: crypto.Hash(encoded[3]),
KSF: KSFConfiguration{
Identifier: ksf.Identifier(encoded[4]),
Parameters: ksfParams,
Salt: ksfSalt,
},
AKE: Group(encoded[5]),
Context: ctx,
}
Expand Down
12 changes: 7 additions & 5 deletions tests/deserializer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@ func TestDeserializer(t *testing.T) {

// Test for an invalid configuration.
conf := &opaque.Configuration{
OPRF: 0,
KDF: 0,
MAC: 0,
Hash: 0,
KSF: 0,
OPRF: 0,
KDF: 0,
MAC: 0,
Hash: 0,
KSF: opaque.KSFConfiguration{
Identifier: 0,
},
AKE: 0,
Context: nil,
}
Expand Down
8 changes: 5 additions & 3 deletions tests/fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func fuzzTestConfigurationError(t *testing.T, c *opaque.Configuration, err error
{errors.New("invalid KDF id"), c.KDF, hash.Hash(c.KDF).Available()},
{errors.New("invalid MAC id"), c.MAC, hash.Hash(c.MAC).Available()},
{errors.New("invalid Hash id"), c.Hash, hash.Hash(c.Hash).Available()},
{errors.New("invalid KSF id"), c.KSF, c.KSF == 0 && c.KSF.Available()},
{errors.New("invalid KSF id"), c.KSF.Identifier, c.KSF.Identifier == 0 && c.KSF.Identifier.Available()},
{errors.New("invalid OPRF group id"), c.OPRF, c.OPRF.Available() && c.OPRF.OPRF().Available()},
{errors.New("invalid AKE group id"), c.AKE, c.AKE.Available() && c.AKE.Group().Available()},
}
Expand Down Expand Up @@ -202,8 +202,10 @@ func inputToConfig(context []byte, kdf, mac, h uint, o []byte, ksfID, ake byte)
MAC: crypto.Hash(mac),
Hash: crypto.Hash(h),
OPRF: oprfToGroup(oprf.Identifier(o)),
KSF: ksf.Identifier(ksfID),
AKE: opaque.Group(ake),
KSF: opaque.KSFConfiguration{
Identifier: ksf.Identifier(ksfID),
},
AKE: opaque.Group(ake),
}
}

Expand Down
18 changes: 12 additions & 6 deletions tests/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ var configurationTable = []configuration{
KDF: crypto.SHA256,
MAC: crypto.SHA256,
Hash: crypto.SHA256,
KSF: ksf.Scrypt,
AKE: opaque.P256Sha256,
KSF: opaque.KSFConfiguration{
Identifier: ksf.Scrypt,
},
AKE: opaque.P256Sha256,
},
curve: elliptic.P256(),
},
Expand All @@ -66,8 +68,10 @@ var configurationTable = []configuration{
KDF: crypto.SHA512,
MAC: crypto.SHA512,
Hash: crypto.SHA512,
KSF: ksf.Scrypt,
AKE: opaque.P384Sha512,
KSF: opaque.KSFConfiguration{
Identifier: ksf.Scrypt,
},
AKE: opaque.P384Sha512,
},
curve: elliptic.P384(),
},
Expand All @@ -78,8 +82,10 @@ var configurationTable = []configuration{
KDF: crypto.SHA512,
MAC: crypto.SHA512,
Hash: crypto.SHA512,
KSF: ksf.Scrypt,
AKE: opaque.P521Sha512,
KSF: opaque.KSFConfiguration{
Identifier: ksf.Scrypt,
},
AKE: opaque.P521Sha512,
},
curve: elliptic.P521(),
},
Expand Down
Loading