diff --git a/auth/fmt.go b/auth/fmt.go index 4580725d39c1f020f9ca34561cc367bbdb13caf8..06cf9cbc38007a5e7e7b067250d1fa6a00c16480 100644 --- a/auth/fmt.go +++ b/auth/fmt.go @@ -9,7 +9,7 @@ package auth import ( "github.com/cloudflare/circl/dh/sidh" - sidhinterface "gitlab.com/elixxir/client/interfaces/sidh" + util "gitlab.com/elixxir/client/storage/utility" "github.com/pkg/errors" jww "github.com/spf13/jwalterweatherman" "gitlab.com/elixxir/crypto/cyclic" @@ -22,16 +22,17 @@ const saltSize = 32 type baseFormat struct { data []byte pubkey []byte - sidHpubkey []byte + sidHpubkey []byte salt []byte ecrPayload []byte } func newBaseFormat(payloadSize, pubkeySize, sidHPubkeySize int ) baseFormat { - total := pubkeySize + sidHPubkeySize + saltSize + // NOTE: sidhPubKey needs an extra byte to hold the variant setting + total := pubkeySize + sidHPubkeySize + 1 + saltSize if payloadSize < total { jww.FATAL.Panicf("Size of baseFormat is too small (%d), must be big " + - "enough to contain public key (%d) sidHPublicKey (%d) and salt (%d) " + + "enough to contain public key (%d) sidHPublicKey (%d + 1) and salt (%d) " + "which totals to %d", payloadSize, pubkeySize, sidHPubkeySize, saltSize, total) } @@ -48,7 +49,7 @@ func buildBaseFormat(data []byte, pubkeySize, sidHPubkeySize int) baseFormat { } f.pubkey = f.data[:pubkeySize] - f.sidHpubkey = f.data[pubkeySize: pubkeySize + sidHPubkeySize] + f.sidHpubkey = f.data[pubkeySize: pubkeySize + sidHPubkeySize + 1] f.salt = f.data[pubkeySize + sidHPubkeySize: pubkeySize+sidHPubkeySize+saltSize] f.ecrPayload = f.data[pubkeySize+sidHPubkeySize+saltSize:] return f @@ -76,13 +77,14 @@ func (f baseFormat) SetPubKey(pubKey *cyclic.Int) { } func (f baseFormat) SetSidHPubKey(pubKey *sidh.PublicKey) { - pubKey.Export(f.sidHpubkey) + f.sidHpubkey[0] = byte(pubKey.Variant()) + pubKey.Export(f.sidHpubkey[1:]) } func (f baseFormat) GetSidhPubKey() (*sidh.PublicKey, error) { - pubKey := sidh.NewPublicKey(sidhinterface.KeyId, - sidh.KeyVariantSidhA) - err := pubKey.Import(f.sidHpubkey) + variant := sidh.KeyVariant(f.sidHpubkey[0]) + pubKey := util.NewSIDHPublicKey(variant) + err := pubKey.Import(f.sidHpubkey[1:]) return pubKey, err } diff --git a/auth/fmt_test.go b/auth/fmt_test.go index 8ffab3b86a43380ffcffa35c1503a6e0b997df54..6c3966ef4463893770d6d042fd11101784a60d13 100644 --- a/auth/fmt_test.go +++ b/auth/fmt_test.go @@ -20,7 +20,7 @@ import ( func TestNewBaseFormat(t *testing.T) { // Construct message pubKeySize := 256 - payloadSize := saltSize + pubKeySize + sidhinterface.PubKeyByteSize + payloadSize := saltSize + pubKeySize + sidhinterface.PubKeyByteSize + 1 baseMsg := newBaseFormat(payloadSize, pubKeySize, sidhinterface.PubKeyByteSize) @@ -65,7 +65,7 @@ func TestNewBaseFormat(t *testing.T) { func TestBaseFormat_SetGetPubKey(t *testing.T) { // Construct message pubKeySize := 256 - payloadSize := saltSize + pubKeySize + sidhinterface.PubKeyByteSize + payloadSize := saltSize + pubKeySize + sidhinterface.PubKeyByteSize + 1 baseMsg := newBaseFormat(payloadSize, pubKeySize, sidhinterface.PubKeyByteSize) @@ -94,7 +94,7 @@ func TestBaseFormat_SetGetPubKey(t *testing.T) { func TestBaseFormat_SetGetSalt(t *testing.T) { // Construct message pubKeySize := 256 - payloadSize := saltSize + pubKeySize + sidhinterface.PubKeyByteSize + payloadSize := saltSize + pubKeySize + sidhinterface.PubKeyByteSize + 1 baseMsg := newBaseFormat(payloadSize, pubKeySize, sidhinterface.PubKeyByteSize)