diff --git a/auth/callback.go b/auth/callback.go index 5ebe91152a79c4ed228aff537e58010631319b8a..594c4fa9a37c4e6e20f9fd1514d233a26b0e669f 100644 --- a/auth/callback.go +++ b/auth/callback.go @@ -9,7 +9,6 @@ package auth import ( "github.com/cloudflare/circl/dh/sidh" - sidhinterface "gitlab.com/elixxir/client/interfaces/sidh" "fmt" "github.com/pkg/errors" jww "github.com/spf13/jwalterweatherman" @@ -86,7 +85,7 @@ func (m *Manager) processAuthMessage(msg message.Receive) { func (m *Manager) handleRequest(cmixMsg format.Message, myHistoricalPrivKey *cyclic.Int, grp *cyclic.Group) { //decode the outer format - baseFmt, partnerPubKey, partnerSIDHPubKey, err := handleBaseFormat( + baseFmt, partnerPubKey, err := handleBaseFormat( cmixMsg, grp) if err != nil { jww.WARN.Printf("Failed to handle auth request: %s", err) @@ -119,6 +118,11 @@ func (m *Manager) handleRequest(cmixMsg format.Message, "request's encrypted payload: %s", err) return } + partnerSIDHPubKey, err := ecrFmt.GetSidhPubKey() + if err != nil { + jww.WARN.Printf("Could not unmarshal partner SIDH Pubkey: %s", + err) + } //decode the request format requestFmt, err := newRequestFormat(ecrFmt) @@ -301,7 +305,7 @@ func (m *Manager) handleConfirm(cmixMsg format.Message, sr *auth.SentRequest, } // extract the message - baseFmt, partnerPubKey, partnerSIDHPubKey, err := handleBaseFormat( + baseFmt, partnerPubKey, err := handleBaseFormat( cmixMsg, grp) if err != nil { em := fmt.Sprintf("Failed to handle auth confirm: %s", err) @@ -312,7 +316,6 @@ func (m *Manager) handleConfirm(cmixMsg format.Message, sr *auth.SentRequest, } jww.TRACE.Printf("handleConfirm PARTNERPUBKEY: %v", partnerPubKey.Bytes()) - jww.TRACE.Printf("handleConfirm PARTNERSIDHPUBKEY: %v", partnerSIDHPubKey) jww.TRACE.Printf("handleConfirm SRMYPUBKEY: %v", sr.GetMyPubKey().Bytes()) // decrypt the payload @@ -341,6 +344,20 @@ func (m *Manager) handleConfirm(cmixMsg format.Message, sr *auth.SentRequest, return } + partnerSIDHPubKey, err := ecrFmt.GetSidhPubKey() + if err != nil { + em := fmt.Sprintf("Could not get auth conf SIDH Pubkey: %s", + err) + jww.WARN.Print(em) + events.Report(10, "Auth", "ConfirmError", em) + m.storage.Auth().Done(sr.GetPartner()) + return + } + jww.TRACE.Printf("handleConfirm PARTNERSIDHPUBKEY: %v", + partnerSIDHPubKey) + + + // finalize the confirmation if err := m.doConfirm(sr, grp, partnerPubKey, sr.GetMyPrivKey(), sr.GetPartnerHistoricalPubKey(), @@ -450,26 +467,20 @@ func copySlice(s []byte) []byte { } func handleBaseFormat(cmixMsg format.Message, grp *cyclic.Group) (baseFormat, - *cyclic.Int, *sidh.PublicKey, error) { + *cyclic.Int, error) { baseFmt, err := unmarshalBaseFormat(cmixMsg.GetContents(), - grp.GetP().ByteLen(), sidhinterface.PubKeyByteSize) + grp.GetP().ByteLen()) if err != nil { - return baseFormat{}, nil, nil, errors.WithMessage(err, "Failed to"+ + return baseFormat{}, nil, errors.WithMessage(err, "Failed to"+ " unmarshal auth") } if !grp.BytesInside(baseFmt.pubkey) { - return baseFormat{}, nil, nil, errors.WithMessage(err, "Received "+ + return baseFormat{}, nil, errors.WithMessage(err, "Received "+ "auth confirmation public key is not in the e2e cyclic group") } partnerPubKey := grp.NewIntFromBytes(baseFmt.pubkey) - partnerSIDHPubKey, err := baseFmt.GetSidhPubKey() - if err != nil { - return baseFormat{}, nil, nil, errors.WithMessage(err, - "Failed to unmarshal auth request's sidh Pubkey") - } - - return baseFmt, partnerPubKey, partnerSIDHPubKey, nil + return baseFmt, partnerPubKey, nil } diff --git a/auth/confirm.go b/auth/confirm.go index d21df492a431878b7657664d7ed0cf549ce48d6c..546d1372a9dacd61d5010def3b2fd3a4cbbe6379 100644 --- a/auth/confirm.go +++ b/auth/confirm.go @@ -21,7 +21,6 @@ import ( cAuth "gitlab.com/elixxir/crypto/e2e/auth" "gitlab.com/elixxir/primitives/format" "gitlab.com/xx_network/primitives/id" - sidhinterface "gitlab.com/elixxir/client/interfaces/sidh" "io" util "gitlab.com/elixxir/client/storage/utility" ) @@ -78,12 +77,12 @@ func ConfirmRequestAuth(partner contact.Contact, rng io.Reader, // we build the payload before we save because it is technically fallible // which can get into a bricked state if it fails cmixMsg := format.NewMessage(storage.Cmix().GetGroup().GetP().ByteLen()) - baseFmt := newBaseFormat(cmixMsg.ContentsSize(), grp.GetP().ByteLen(), - sidhinterface.PubKeyByteSize) + baseFmt := newBaseFormat(cmixMsg.ContentsSize(), grp.GetP().ByteLen()) ecrFmt := newEcrFormat(baseFmt.GetEcrPayloadLen()) // setup the encrypted payload ecrFmt.SetOwnership(ownership) + ecrFmt.SetSidHPubKey(newSIDHPubKey) // confirmation has no custom payload //encrypt the payload @@ -97,7 +96,6 @@ func ConfirmRequestAuth(partner contact.Contact, rng io.Reader, //final construction baseFmt.SetEcrPayload(ecrPayload) baseFmt.SetPubKey(newPubKey) - baseFmt.SetSidHPubKey(newSIDHPubKey) cmixMsg.SetKeyFP(fp) cmixMsg.SetMac(mac) diff --git a/auth/fmt.go b/auth/fmt.go index c3fe24df881c9ee5592403b5676bfc2477953558..48a7cf55194168ab8927e22ad9ac3aef1ecb9a80 100644 --- a/auth/fmt.go +++ b/auth/fmt.go @@ -14,35 +14,33 @@ import ( jww "github.com/spf13/jwalterweatherman" "gitlab.com/elixxir/crypto/cyclic" "gitlab.com/xx_network/primitives/id" + sidhinterface "gitlab.com/elixxir/client/interfaces/sidh" ) //Basic Format////////////////////////////////////////////////////////////////// type baseFormat struct { data []byte pubkey []byte - sidHpubkey []byte ecrPayload []byte } -func newBaseFormat(payloadSize, pubkeySize, sidHPubkeySize int ) baseFormat { - // NOTE: sidhPubKey needs an extra byte to hold the variant setting - total := pubkeySize + sidHPubkeySize + 1 +func newBaseFormat(payloadSize, pubkeySize int) baseFormat { + total := pubkeySize + sidhinterface.PubKeyByteSize + 1 if payloadSize < total { jww.FATAL.Panicf("Size of baseFormat is too small (%d), must be big " + - "enough to contain public key (%d) sidHPublicKey (%d + 1) " + - "which totals to %d", payloadSize, pubkeySize, sidHPubkeySize, - total) + "enough to contain public key (%d) and sidh key (%d)" + + "which totals to %d", payloadSize, pubkeySize, + sidhinterface.PubKeyByteSize + 1, total) } jww.INFO.Printf("Empty Space RequestAuth: %d", payloadSize-total) - f := buildBaseFormat(make([]byte, payloadSize), pubkeySize, - sidHPubkeySize) + f := buildBaseFormat(make([]byte, payloadSize), pubkeySize) return f } -func buildBaseFormat(data []byte, pubkeySize, sidHPubkeySize int) baseFormat { +func buildBaseFormat(data []byte, pubkeySize int) baseFormat { f := baseFormat{ data: data, } @@ -51,21 +49,17 @@ func buildBaseFormat(data []byte, pubkeySize, sidHPubkeySize int) baseFormat { end := pubkeySize f.pubkey = f.data[:end] - start = end - end = start + sidHPubkeySize + 1 - f.sidHpubkey = f.data[start:end] - start = end f.ecrPayload = f.data[start:] return f } -func unmarshalBaseFormat(b []byte, pubkeySize, sidHPubkeySize int) (baseFormat, error) { +func unmarshalBaseFormat(b []byte, pubkeySize int) (baseFormat, error) { if len(b) < pubkeySize { return baseFormat{}, errors.New("Received baseFormat too small") } - return buildBaseFormat(b, pubkeySize, sidHPubkeySize), nil + return buildBaseFormat(b, pubkeySize), nil } func (f baseFormat) Marshal() []byte { @@ -81,18 +75,7 @@ func (f baseFormat) SetPubKey(pubKey *cyclic.Int) { copy(f.pubkey, pubKeyBytes) } -func (f baseFormat) SetSidHPubKey(pubKey *sidh.PublicKey) { - f.sidHpubkey[0] = byte(pubKey.Variant()) - pubKey.Export(f.sidHpubkey[1:]) -} - -func (f baseFormat) GetSidhPubKey() (*sidh.PublicKey, error) { - variant := sidh.KeyVariant(f.sidHpubkey[0]) - pubKey := util.NewSIDHPublicKey(variant) - err := pubKey.Import(f.sidHpubkey[1:]) - return pubKey, err -} - +// GetEcrPayload is the data that is encrypted func (f baseFormat) GetEcrPayload() []byte { return f.ecrPayload } @@ -116,11 +99,12 @@ const ownershipSize = 32 type ecrFormat struct { data []byte ownership []byte + sidHpubkey []byte payload []byte } func newEcrFormat(size int) ecrFormat { - if size < ownershipSize { + if size < (ownershipSize + sidhinterface.PubKeyByteSize + 1) { jww.FATAL.Panicf("Size too small to hold") } @@ -135,8 +119,16 @@ func buildEcrFormat(data []byte) ecrFormat { data: data, } - f.ownership = f.data[:ownershipSize] - f.payload = f.data[ownershipSize:] + start := 0 + end := ownershipSize + f.ownership = f.data[start:end] + + start = end + end = start + sidhinterface.PubKeyByteSize + 1 + f.sidHpubkey = f.data[start:end] + + start = end + f.payload = f.data[start:] return f } @@ -164,6 +156,18 @@ func (f ecrFormat) SetOwnership(ownership []byte) { copy(f.ownership, ownership) } +func (f ecrFormat) SetSidHPubKey(pubKey *sidh.PublicKey) { + f.sidHpubkey[0] = byte(pubKey.Variant()) + pubKey.Export(f.sidHpubkey[1:]) +} + +func (f ecrFormat) GetSidhPubKey() (*sidh.PublicKey, error) { + variant := sidh.KeyVariant(f.sidHpubkey[0]) + pubKey := util.NewSIDHPublicKey(variant) + err := pubKey.Import(f.sidHpubkey[1:]) + return pubKey, err +} + func (f ecrFormat) GetPayload() []byte { return f.payload } diff --git a/auth/fmt_test.go b/auth/fmt_test.go index 4d373930327a45c04af04f7e799f5fbac8c2c036..af990942d713f4e7b13659eefe6ae4fd753ee2e9 100644 --- a/auth/fmt_test.go +++ b/auth/fmt_test.go @@ -21,8 +21,7 @@ func TestNewBaseFormat(t *testing.T) { // Construct message pubKeySize := 256 payloadSize := pubKeySize + sidhinterface.PubKeyByteSize + 1 - baseMsg := newBaseFormat(payloadSize, pubKeySize, - sidhinterface.PubKeyByteSize) + baseMsg := newBaseFormat(payloadSize, pubKeySize) // Check that the base format was constructed properly if !bytes.Equal(baseMsg.pubkey, make([]byte, pubKeySize)) { @@ -32,8 +31,7 @@ func TestNewBaseFormat(t *testing.T) { "\n\tReceived: %v", make([]byte, pubKeySize), baseMsg.pubkey) } - expectedEcrPayloadSize := payloadSize - (pubKeySize + - sidhinterface.PubKeyByteSize + 1) + expectedEcrPayloadSize := payloadSize - (pubKeySize) if !bytes.Equal(baseMsg.ecrPayload, make([]byte, expectedEcrPayloadSize)) { t.Errorf("NewBaseFormat error: "+ "Unexpected payload field in base format."+ @@ -49,7 +47,7 @@ func TestNewBaseFormat(t *testing.T) { } }() - newBaseFormat(0, pubKeySize, sidhinterface.PubKeyByteSize) + newBaseFormat(0, pubKeySize) } /* Tests the setter/getter methods for baseFormat */ @@ -59,8 +57,7 @@ func TestBaseFormat_SetGetPubKey(t *testing.T) { // Construct message pubKeySize := 256 payloadSize := pubKeySize + sidhinterface.PubKeyByteSize + 1 - baseMsg := newBaseFormat(payloadSize, pubKeySize, - sidhinterface.PubKeyByteSize) + baseMsg := newBaseFormat(payloadSize, pubKeySize) // Test setter grp := getGroup() @@ -89,12 +86,10 @@ func TestBaseFormat_SetGetEcrPayload(t *testing.T) { // Construct message pubKeySize := 256 payloadSize := (pubKeySize + sidhinterface.PubKeyByteSize) * 2 - baseMsg := newBaseFormat(payloadSize, pubKeySize, - sidhinterface.PubKeyByteSize) + baseMsg := newBaseFormat(payloadSize, pubKeySize) // Test setter - ecrPayloadSize := payloadSize - (pubKeySize + - sidhinterface.PubKeyByteSize + 1) + ecrPayloadSize := payloadSize - (pubKeySize) ecrPayload := newPayload(ecrPayloadSize, "ecrPayload") baseMsg.SetEcrPayload(ecrPayload) if !bytes.Equal(ecrPayload, baseMsg.ecrPayload) { @@ -128,10 +123,8 @@ func TestBaseFormat_MarshalUnmarshal(t *testing.T) { // Construct a fully populated message pubKeySize := 256 payloadSize := (pubKeySize + sidhinterface.PubKeyByteSize) * 2 - baseMsg := newBaseFormat(payloadSize, pubKeySize, - sidhinterface.PubKeyByteSize) - ecrPayloadSize := payloadSize - (pubKeySize + - sidhinterface.PubKeyByteSize + 1) + baseMsg := newBaseFormat(payloadSize, pubKeySize) + ecrPayloadSize := payloadSize - (pubKeySize) ecrPayload := newPayload(ecrPayloadSize, "ecrPayload") baseMsg.SetEcrPayload(ecrPayload) grp := getGroup() @@ -147,8 +140,7 @@ func TestBaseFormat_MarshalUnmarshal(t *testing.T) { } // Test unmarshal - newMsg, err := unmarshalBaseFormat(data, pubKeySize, - sidhinterface.PubKeyByteSize) + newMsg, err := unmarshalBaseFormat(data, pubKeySize) if err != nil { t.Errorf("unmarshalBaseFormat() error: "+ "Could not unmarshal into baseFormat: %v", err) @@ -161,8 +153,7 @@ func TestBaseFormat_MarshalUnmarshal(t *testing.T) { } // Unmarshal error test: Invalid size parameter - _, err = unmarshalBaseFormat(make([]byte, 0), pubKeySize, - sidhinterface.PubKeyByteSize) + _, err = unmarshalBaseFormat(make([]byte, 0), pubKeySize) if err == nil { t.Errorf("unmarshalBaseFormat() error: " + "Should not be able to unmarshal when baseFormat is too small") @@ -173,7 +164,7 @@ func TestBaseFormat_MarshalUnmarshal(t *testing.T) { // Tests newEcrFormat func TestNewEcrFormat(t *testing.T) { // Construct message - payloadSize := ownershipSize * 2 + payloadSize := ownershipSize * 2 + sidhinterface.PubKeyByteSize + 1 ecrMsg := newEcrFormat(payloadSize) // Check that the ecrFormat was constructed properly @@ -184,7 +175,8 @@ func TestNewEcrFormat(t *testing.T) { "\n\tReceived: %v", make([]byte, payloadSize), ecrMsg.ownership) } - if !bytes.Equal(ecrMsg.payload, make([]byte, payloadSize-ownershipSize)) { + if !bytes.Equal(ecrMsg.payload, make([]byte, + payloadSize-ownershipSize-sidhinterface.PubKeyByteSize-1)) { t.Errorf("newEcrFormat error: "+ "Unexpected ownership field in ecrFormat."+ "\n\tExpected: %v"+ @@ -207,7 +199,7 @@ func TestNewEcrFormat(t *testing.T) { // Set/Get ownership tests func TestEcrFormat_SetGetOwnership(t *testing.T) { // Construct message - payloadSize := ownershipSize * 2 + payloadSize := ownershipSize * 2 + sidhinterface.PubKeyByteSize + 1 ecrMsg := newEcrFormat(payloadSize) // Test setter @@ -243,11 +235,13 @@ func TestEcrFormat_SetGetOwnership(t *testing.T) { // Set/Get payload tests func TestEcrFormat_SetGetPayload(t *testing.T) { // Construct message - payloadSize := ownershipSize * 2 + payloadSize := ownershipSize * 2 + sidhinterface.PubKeyByteSize + 1 ecrMsg := newEcrFormat(payloadSize) // Test set - expectedPayload := newPayload(payloadSize-ownershipSize, "ownership") + expectedPayload := newPayload( + payloadSize-ownershipSize-sidhinterface.PubKeyByteSize-1, + "ownership") ecrMsg.SetPayload(expectedPayload) if !bytes.Equal(expectedPayload, ecrMsg.payload) { @@ -279,9 +273,11 @@ func TestEcrFormat_SetGetPayload(t *testing.T) { // Marshal/ unmarshal tests func TestEcrFormat_MarshalUnmarshal(t *testing.T) { // Construct message - payloadSize := ownershipSize * 2 + payloadSize := ownershipSize * 2 + sidhinterface.PubKeyByteSize + 1 ecrMsg := newEcrFormat(payloadSize) - expectedPayload := newPayload(payloadSize-ownershipSize, "ownership") + expectedPayload := newPayload( + payloadSize-ownershipSize - sidhinterface.PubKeyByteSize - 1, + "ownership") ecrMsg.SetPayload(expectedPayload) ownership := newOwnership("owner") ecrMsg.SetOwnership(ownership) @@ -319,7 +315,7 @@ func TestEcrFormat_MarshalUnmarshal(t *testing.T) { // Tests newRequestFormat func TestNewRequestFormat(t *testing.T) { // Construct message - payloadSize := id.ArrIDLen*2 - 1 + payloadSize := id.ArrIDLen*2 - 1 + sidhinterface.PubKeyByteSize + 1 ecrMsg := newEcrFormat(payloadSize) expectedPayload := newPayload(id.ArrIDLen, "ownership") ecrMsg.SetPayload(expectedPayload) @@ -346,7 +342,7 @@ func TestNewRequestFormat(t *testing.T) { // "\n\tReceived: %v", make([]byte, 0), reqMsg.GetPayload()) // } - payloadSize = ownershipSize * 2 + payloadSize = ownershipSize * 2 + sidhinterface.PubKeyByteSize + 1 ecrMsg = newEcrFormat(payloadSize) reqMsg, err = newRequestFormat(ecrMsg) if err == nil { @@ -360,7 +356,7 @@ func TestNewRequestFormat(t *testing.T) { // Unit test for Get/SetID func TestRequestFormat_SetGetID(t *testing.T) { // Construct message - payloadSize := id.ArrIDLen*2 - 1 + payloadSize := id.ArrIDLen*2 - 1 + sidhinterface.PubKeyByteSize + 1 ecrMsg := newEcrFormat(payloadSize) expectedPayload := newPayload(id.ArrIDLen, "ownership") ecrMsg.SetPayload(expectedPayload) @@ -401,10 +397,9 @@ func TestRequestFormat_SetGetID(t *testing.T) { // Unit test for Get/SetMsgPayload func TestRequestFormat_SetGetMsgPayload(t *testing.T) { // Construct message - payloadSize := id.ArrIDLen*3 - 1 + sidhinterface.PubKeyByteSize + payloadSize := id.ArrIDLen*3 - 1 + sidhinterface.PubKeyByteSize + 1 ecrMsg := newEcrFormat(payloadSize) - expectedPayload := newPayload(id.ArrIDLen*2 + - sidhinterface.PubKeyByteSize, "ownership") + expectedPayload := newPayload(id.ArrIDLen*2, "ownership") ecrMsg.SetPayload(expectedPayload) reqMsg, err := newRequestFormat(ecrMsg) if err != nil { @@ -413,7 +408,7 @@ func TestRequestFormat_SetGetMsgPayload(t *testing.T) { } // Test SetMsgPayload - msgPayload := newPayload(id.ArrIDLen*2 + sidhinterface.PubKeyByteSize, + msgPayload := newPayload(id.ArrIDLen*2, "msgPayload") reqMsg.SetPayload(msgPayload) if !bytes.Equal(reqMsg.GetPayload(), msgPayload) { diff --git a/auth/request.go b/auth/request.go index 18d1eea1535fcc48c80204da0ddaacd56734f7b4..e7bb8945073057906d1bf5ed9efed1b3ed5ea0d3 100644 --- a/auth/request.go +++ b/auth/request.go @@ -12,7 +12,6 @@ import ( "github.com/cloudflare/circl/dh/sidh" "github.com/pkg/errors" jww "github.com/spf13/jwalterweatherman" - sidhinterface "gitlab.com/elixxir/client/interfaces/sidh" "gitlab.com/elixxir/client/interfaces" "gitlab.com/elixxir/client/interfaces/params" "gitlab.com/elixxir/client/interfaces/preimage" @@ -76,8 +75,7 @@ func RequestAuth(partner, me contact.Contact, rng io.Reader, /*generate embedded message structures and check payload*/ cmixMsg := format.NewMessage(storage.Cmix().GetGroup().GetP().ByteLen()) - baseFmt := newBaseFormat(cmixMsg.ContentsSize(), grp.GetP().ByteLen(), - sidhinterface.PubKeyByteSize) + baseFmt := newBaseFormat(cmixMsg.ContentsSize(), grp.GetP().ByteLen()) ecrFmt := newEcrFormat(baseFmt.GetEcrPayloadLen()) requestFmt, err := newRequestFormat(ecrFmt) if err != nil { @@ -136,6 +134,7 @@ func RequestAuth(partner, me contact.Contact, rng io.Reader, requestFmt.SetID(storage.GetUser().ReceptionID) requestFmt.SetMsgPayload(msgPayloadBytes) ecrFmt.SetOwnership(ownership) + ecrFmt.SetSidHPubKey(sidHPubKeyA) ecrPayload, mac := cAuth.Encrypt(newPrivKey, partner.DhPubKey, ecrFmt.data, grp) confirmFp := cAuth.MakeOwnershipProofFP(ownership) @@ -143,7 +142,6 @@ func RequestAuth(partner, me contact.Contact, rng io.Reader, /*construct message*/ baseFmt.SetEcrPayload(ecrPayload) - baseFmt.SetSidHPubKey(sidHPubKeyA) baseFmt.SetPubKey(newPubKey) cmixMsg.SetKeyFP(requestfp)