diff --git a/api/client.go b/api/client.go index 5ed98577902bc58054c9694ae1a6d6e8593e20fa..505f25257263ae8c4b78adcf8c89f3e7a452c4e2 100644 --- a/api/client.go +++ b/api/client.go @@ -8,6 +8,7 @@ package api import ( "crypto/rand" + "crypto/sha256" "errors" "fmt" "github.com/golang/protobuf/proto" @@ -25,6 +26,7 @@ import ( pb "gitlab.com/elixxir/comms/mixmessages" "gitlab.com/elixxir/crypto/csprng" "gitlab.com/elixxir/crypto/cyclic" + "gitlab.com/elixxir/crypto/hash" "gitlab.com/elixxir/crypto/registration" "gitlab.com/elixxir/crypto/signature" "gitlab.com/elixxir/primitives/id" @@ -85,18 +87,18 @@ func Register(registrationCode, registrationAddr string, gwAddresses []string, // Generate DSA keypair params := signature.NewDSAParams(rand.Reader, signature.L2048N256) privateKey := params.PrivateKeyGen(rand.Reader) + publicKey := privateKey.PublicKeyGen() // Generate UserID by hashing salt and public key - UID := registration.GenUserID(privateKey.PublicKeyGen(), salt) + UID := registration.GenUserID(publicKey, salt) // Send registration code and public key to RegistrationServer - p, q, g := privateKey.GetParams() response, err := client.SendRegistrationMessage(registrationAddr, &pb.RegisterUserMessage{ - Y: privateKey.GetPublicKey().Bytes(), - P: p.Bytes(), - Q: q.Bytes(), - G: g.Bytes(), + Y: publicKey.GetKey().Bytes(), + P: params.GetP().Bytes(), + Q: params.GetQ().Bytes(), + G: params.GetG().Bytes(), }) if err != nil { globals.Log.ERROR.Printf( @@ -119,10 +121,10 @@ func Register(registrationCode, registrationAddr string, gwAddresses []string, nonceResponse, err := client.SendRequestNonceMessage(gwAddr, &pb.RequestNonceMessage{ Salt: salt, - Y: privateKey.GetPublicKey().Bytes(), - P: p.Bytes(), - Q: q.Bytes(), - G: g.Bytes(), + Y: publicKey.GetKey().Bytes(), + P: params.GetP().Bytes(), + Q: params.GetQ().Bytes(), + G: params.GetG().Bytes(), Hash: regHash, R: regR, S: regS, @@ -177,29 +179,56 @@ func Register(registrationCode, registrationAddr string, gwAddresses []string, } - // FIXME: Add private key to session storage - //nus := user.NewSession(u, gwAddresses[0], nk, cyclic.NewIntFromBytes([]byte( - // "this is not a real public key"))) - // - //_, err = payment.CreateWallet(nus, mint) - //if err != nil { - // return id.ZeroID, err - //} + // Generate cyclic group for key generation + grp := cyclic.NewGroup( + params.GetP(), + cyclic.NewInt(2), + cyclic.NewInt(2), + cyclic.NewRandom(cyclic.NewInt(3), cyclic.NewInt(7)), + ) + + nk := make([]user.NodeKeys, len(gwAddresses)) + + // Initialise blake2b hash for transmission keys and sha256 for reception + // keys + transmissionHash, _ := hash.NewCMixHash() + receptionHash := sha256.New() + + // Loop through all the server public keys + for itr, publicKey := range serverPublicKeys { + // Generate the base keys + nk[itr].TransmissionKey = registration.GenerateBaseKey( + &grp, publicKey, privateKey, transmissionHash, + ) + + nk[itr].ReceptionKey = registration.GenerateBaseKey( + &grp, publicKey, privateKey, receptionHash, + ) + } + + u := user.User{UID, ""} + + nus := user.NewSession(&u, gwAddresses[0], nk, privateKey.PublicKeyGen(), privateKey) + + _, err = payment.CreateWallet(nus, mint) + if err != nil { + return id.ZeroID, err + } // - //errStore := nus.StoreSession() + errStore := nus.StoreSession() // //// FIXME If we have an error here, the session that gets created doesn't get immolated. //// Immolation should happen in a deferred call instead. - //if errStore != nil { - // err = errors.New(fmt.Sprintf( - // "Register: could not register due to failed session save"+ - // ": %s", errStore.Error())) - // globals.Log.ERROR.Printf(err.Error()) - // return id.ZeroID, err - //} - // - //nus.Immolate() - //nus = nil + if errStore != nil { + err = errors.New(fmt.Sprintf( + "Register: could not register due to failed session save"+ + ": %s", errStore.Error())) + globals.Log.ERROR.Printf(err.Error()) + return id.ZeroID, err + } + + nus.Immolate() + nus = nil return UID, err } @@ -361,7 +390,7 @@ func RegisterForUserDiscovery(emailAddress string) error { } publicKey := user.TheSession.GetPublicKey() - publicKeyBytes := publicKey.LeftpadBytes(256) + publicKeyBytes := publicKey.GetKey().LeftpadBytes(256) return bots.Register(valueType, emailAddress, publicKeyBytes) } diff --git a/api/client_test.go b/api/client_test.go index 0b184af65f595dee395063b46d4661930f100b0a..ce39a487cd91f7daae2c739294a3766c66c150ae 100644 --- a/api/client_test.go +++ b/api/client_test.go @@ -13,13 +13,13 @@ import ( "github.com/golang/protobuf/proto" "gitlab.com/elixxir/client/cmixproto" "gitlab.com/elixxir/client/globals" + "gitlab.com/elixxir/client/parse" "gitlab.com/elixxir/client/user" "gitlab.com/elixxir/crypto/cyclic" "gitlab.com/elixxir/primitives/id" + "reflect" "testing" "time" - "reflect" - "gitlab.com/elixxir/client/parse" ) func TestRegistrationGob(t *testing.T) { @@ -27,7 +27,7 @@ func TestRegistrationGob(t *testing.T) { globals.InitStorage(&globals.RamStorage{}, "") // populate a gob in the store - Register("UAV6IWD6", gwAddress, 1, false) + Register("UAV6IWD6", gwAddress, []string{"1"}, false) // get the gob out of there again sessionGob := globals.LocalStorage.Load() @@ -58,77 +58,25 @@ func VerifyRegisterGobUser(t *testing.T) { } func VerifyRegisterGobKeys(t *testing.T) { - if Session.GetPublicKey().Cmp(cyclic.NewIntFromBytes([]byte( - "this is not a real public key"))) != 0 { - t.Errorf("Public key was %v, expected %v", - string(Session.GetPublicKey().Bytes()), - "this is not a real public key") - } + h := sha256.New() - h.Write([]byte(string(30005))) - expectedTransmissionRecursiveKey := cyclic.NewIntFromBytes(h.Sum(nil)) - if Session.GetKeys()[0].TransmissionKeys.Recursive.Cmp( - expectedTransmissionRecursiveKey) != 0 { - t.Errorf("Transmission recursive key was %v, expected %v", - Session.GetKeys()[0].TransmissionKeys.Recursive.Text(16), - expectedTransmissionRecursiveKey.Text(16)) - } - h = sha256.New() h.Write([]byte(string(20005))) expectedTransmissionBaseKey := cyclic.NewIntFromBytes(h.Sum(nil)) - if Session.GetKeys()[0].TransmissionKeys.Base.Cmp( + if Session.GetKeys()[0].TransmissionKey.Cmp( expectedTransmissionBaseKey) != 0 { t.Errorf("Transmission base key was %v, expected %v", - Session.GetKeys()[0].TransmissionKeys.Base.Text(16), + Session.GetKeys()[0].TransmissionKey.Text(16), expectedTransmissionBaseKey.Text(16)) } h = sha256.New() - h.Write([]byte(string(50005))) - expectedReceptionRecursiveKey := cyclic.NewIntFromBytes(h.Sum(nil)) - if Session.GetKeys()[0].ReceptionKeys.Recursive.Cmp( - expectedReceptionRecursiveKey) != 0 { - t.Errorf("Reception recursive key was %v, expected %v", - Session.GetKeys()[0].ReceptionKeys.Recursive.Text(16), - expectedReceptionRecursiveKey.Text(16)) - } - h = sha256.New() h.Write([]byte(string(40005))) expectedReceptionBaseKey := cyclic.NewIntFromBytes(h.Sum(nil)) - if Session.GetKeys()[0].ReceptionKeys.Base.Cmp( + if Session.GetKeys()[0].ReceptionKey.Cmp( expectedReceptionBaseKey) != 0 { t.Errorf("Reception base key was %v, expected %v", - Session.GetKeys()[0].ReceptionKeys.Base.Text(16), + Session.GetKeys()[0].ReceptionKey.Text(16), expectedReceptionBaseKey.Text(16)) } - - if Session.GetKeys()[0].ReturnKeys.Recursive == nil { - t.Logf("warning: return recursive key is nil") - } else { - t.Logf("return recursive key is not nil. " + - "update gob test to ensure that it's serialized to storage, " + - "if needed") - } - if Session.GetKeys()[0].ReturnKeys.Base == nil { - t.Logf("warning: return base key is nil") - } else { - t.Logf("return base key is not nil. " + - "update gob test to ensure that it's serialized to storage, " + - "if needed") - } - if Session.GetKeys()[0].ReceiptKeys.Recursive == nil { - t.Logf("warning: receipt recursive key is nil") - } else { - t.Logf("receipt recursive key is not nil. " + - "update gob test to ensure that it's serialized to storage, " + - "if needed") - } - if Session.GetKeys()[0].ReceiptKeys.Base == nil { - t.Logf("warning: receipt recursive key is nil") - } else { - t.Logf("receipt base key is not nil. " + - "update gob test to ensure that it's serialized to storage, " + - "if needed") - } } // Make sure that a formatted text message can deserialize to the text @@ -160,18 +108,18 @@ func TestParsedMessage_GetSender(t *testing.T) { pm := ParsedMessage{} sndr := pm.GetSender() - if !reflect.DeepEqual(sndr,[]byte{}){ + if !reflect.DeepEqual(sndr, []byte{}) { t.Errorf("Sender not empty from typed message") } } func TestParsedMessage_GetPayload(t *testing.T) { pm := ParsedMessage{} - payload := []byte{0,1,2,3} + payload := []byte{0, 1, 2, 3} pm.Payload = payload pld := pm.GetPayload() - if !reflect.DeepEqual(pld,payload){ + if !reflect.DeepEqual(pld, payload) { t.Errorf("Output payload does not match input payload: %v %v", payload, pld) } } @@ -180,7 +128,7 @@ func TestParsedMessage_GetRecipient(t *testing.T) { pm := ParsedMessage{} rcpt := pm.GetRecipient() - if !reflect.DeepEqual(rcpt,[]byte{}){ + if !reflect.DeepEqual(rcpt, []byte{}) { t.Errorf("Recipient not empty from typed message") } } @@ -192,14 +140,14 @@ func TestParsedMessage_GetType(t *testing.T) { pm.Typed = typeTest typ := pm.GetType() - if typ!=typeTest{ + if typ != typeTest { t.Errorf("Returned type does not match") } } -func TestParse(t *testing.T){ +func TestParse(t *testing.T) { ms := parse.Message{} - ms.Body = []byte{0,1,2} + ms.Body = []byte{0, 1, 2} ms.Type = cmixproto.Type_NO_TYPE ms.Receiver = id.ZeroID ms.Sender = id.ZeroID @@ -208,15 +156,15 @@ func TestParse(t *testing.T){ msOut, err := ParseMessage(messagePacked) - if err!=nil{ + if err != nil { t.Errorf("Message failed to parse: %s", err.Error()) } - if msOut.GetType()!=int32(ms.Type){ + if msOut.GetType() != int32(ms.Type) { t.Errorf("Types do not match after message parse: %v vs %v", msOut.GetType(), ms.Type) } - if !reflect.DeepEqual(ms.Body,msOut.GetPayload()){ + if !reflect.DeepEqual(ms.Body, msOut.GetPayload()) { t.Errorf("Bodies do not match after message parse: %v vs %v", msOut.GetPayload(), ms.Body) } diff --git a/api/mockserver_test.go b/api/mockserver_test.go index 1c6776a91e72a66835ce9b228b17a32f0792596c..654135b4fc5363cf2891f02295b5e11e08c07ec4 100644 --- a/api/mockserver_test.go +++ b/api/mockserver_test.go @@ -64,7 +64,7 @@ func TestRegister(t *testing.T) { registrationCode := "UAV6IWD6" d := DummyStorage{Location: "Blah", LastSave: []byte{'a', 'b', 'c'}} err := InitClient(&d, "hello") - regRes, err := Register(registrationCode, gwAddress, 1, false) + regRes, err := Register(registrationCode, gwAddress, []string{"1", "2", "3"}, false) if err != nil { t.Errorf("Registration failed: %s", err.Error()) } @@ -83,7 +83,7 @@ func TestRegisterBadNumNodes(t *testing.T) { registrationCode := "UAV6IWD6" d := DummyStorage{Location: "Blah", LastSave: []byte{'a', 'b', 'c'}} err := InitClient(&d, "hello") - _, err = Register(registrationCode, gwAddress, 0, false) + _, err = Register(registrationCode, gwAddress, []string{"1", "2", "3"}, false) if err == nil { t.Errorf("Registration worked with bad numnodes! %s", err.Error()) } @@ -99,7 +99,7 @@ func TestRegisterBadHUID(t *testing.T) { registrationCode := "OIF3OJ6I" d := DummyStorage{Location: "Blah", LastSave: []byte{'a', 'b', 'c'}} err := InitClient(&d, "hello") - _, err = Register(registrationCode, gwAddress, 1, false) + _, err = Register(registrationCode, gwAddress, []string{"1", "2", "3"}, false) if err == nil { t.Error("Registration worked with bad registration code!") } @@ -117,7 +117,7 @@ func TestRegisterDeletedUser(t *testing.T) { err := InitClient(&d, "hello") tempUser, _ := user.Users.GetUser(id.NewUserFromUint(5, t)) user.Users.DeleteUser(id.NewUserFromUint(5, t)) - _, err = Register(registrationCode, gwAddress, 1, false) + _, err = Register(registrationCode, gwAddress, []string{"1", "2", "3"}, false) if err == nil { t.Errorf("Registration worked with a deleted user: %s", err.Error()) @@ -131,8 +131,8 @@ func SetNulKeys() { // FIXME: Why doesn't crypto panic when these keys are empty? keys := user.TheSession.GetKeys() for i := range keys { - keys[i].TransmissionKeys.Base = cyclic.NewInt(1) - keys[i].TransmissionKeys.Recursive = cyclic.NewInt(1) + keys[i].TransmissionKey = cyclic.NewInt(1) + keys[i].TransmissionKey = cyclic.NewInt(1) } } @@ -145,7 +145,7 @@ func TestSend(t *testing.T) { d := DummyStorage{Location: "Blah", LastSave: []byte{'a', 'b', 'c'}} err := InitClient(&d, "hello") registrationCode := "UAV6IWD6" - userID, err := Register(registrationCode, gwAddress, 1, false) + userID, err := Register(registrationCode, gwAddress, []string{"1", "2", "3"}, false) session, err2 := Login(userID, gwAddress, "") SetNulKeys() diff --git a/bindings/client.go b/bindings/client.go index c0464089047873ce5e2f5df5e44d27788ef25c4a..129501b8a2da1716f4f890c2803522d8951af34a 100644 --- a/bindings/client.go +++ b/bindings/client.go @@ -17,6 +17,7 @@ import ( "gitlab.com/elixxir/crypto/certs" "gitlab.com/elixxir/primitives/id" "io" + "strings" ) // Copy of the storage interface. @@ -110,17 +111,19 @@ func InitClient(storage Storage, loc string) error { // Registers user and returns the User ID. Returns null if registration fails. // registrationCode is a one time use string // registrationAddr is the address of the registration server -// gwAddresses is a list of gateway addresses +// gwAddresses is CSV of gateway addresses // numNodes is the number of nodes in the system -func Register(registrationCode, registrationAddr string, gwAddresses []string, +func Register(registrationCode, registrationAddr string, gwAddressesList string, mint bool) ([]byte, error) { - if len(gwAddresses) < 1 { + gwList := strings.Split(gwAddressesList, ",") + + if len(gwList) < 1 { return id.ZeroID[:], errors.New("invalid number of nodes") } UID, err := api.Register(registrationCode, registrationAddr, - gwAddresses, mint) + gwList, mint) if err != nil { return id.ZeroID[:], err diff --git a/bindings/client_test.go b/bindings/client_test.go index 57e387d8f2991047aff2e88768086041c962ac56..df05a3bb077ad69e77614dc543d7f140053bb8f3 100644 --- a/bindings/client_test.go +++ b/bindings/client_test.go @@ -130,7 +130,7 @@ func TestRegister(t *testing.T) { d := api.DummyStorage{Location: "Blah", LastSave: []byte{'a', 'b', 'c'}} err := InitClient(&d, "hello") - regRes, err := Register(registrationCode, gwAddress, 1, false) + regRes, err := Register(registrationCode, gwAddress, "1,2,3", false) if err != nil { t.Errorf("Registration failed: %s", err.Error()) } @@ -149,7 +149,7 @@ func TestRegisterBadNumNodes(t *testing.T) { d := api.DummyStorage{Location: "Blah", LastSave: []byte{'a', 'b', 'c'}} err := InitClient(&d, "hello") - _, err = Register(registrationCode, gwAddress, 0, false) + _, err = Register(registrationCode, gwAddress, "1,2,3", false) if err == nil { t.Errorf("Registration worked with bad numnodes! %s", err.Error()) } @@ -165,7 +165,7 @@ func TestLoginLogout(t *testing.T) { d := api.DummyStorage{Location: "Blah", LastSave: []byte{'a', 'b', 'c'}} err := InitClient(&d, "hello") - regRes, err := Register(registrationCode, gwAddress, 1, false) + regRes, err := Register(registrationCode, gwAddress, "1,2,3", false) loginRes, err2 := Login(regRes, gwAddress, "") if err2 != nil { t.Errorf("Login failed: %s", err.Error()) @@ -194,7 +194,7 @@ func TestDisableBlockingTransmission(t *testing.T) { func TestSetRateLimiting(t *testing.T) { u, _ := user.Users.GetUser(id.NewUserFromUint(1, t)) nk := make([]user.NodeKeys, 1) - user.TheSession = user.NewSession(u, gwAddress, nk, nil) + user.TheSession = user.NewSession(u, gwAddress, nk, nil, nil) if io.TransmitDelay != time.Duration(1000)*time.Millisecond { t.Errorf("SetRateLimiting not intilized properly") } diff --git a/cmd/root.go b/cmd/root.go index 434c2676b390be134a3c8da59b8350e9abc5f48a..0bbd404f741356b1e4a9dcb507617cebb7c6b816 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -27,6 +27,7 @@ import ( "log" "math/big" "os" + "strings" "sync/atomic" "time" ) @@ -119,7 +120,8 @@ func sessionInitialization() { // to allow testing with IDs that are long enough to exercise more than // 64 bits regCode := new(id.User).SetUints(&[4]uint64{0, 0, 0, userId}).RegistrationCode() - _, err := bindings.Register(regCode, "", gwAddresses, mint) + + _, err := bindings.Register(regCode, "", strings.Join(gwAddresses, ","), mint) if err != nil { fmt.Printf("Could Not Register User: %s\n", err.Error()) return diff --git a/crypto/encryptdecrypt_test.go b/crypto/encryptdecrypt_test.go index 448154134fd1cd42236cdd89184cb4fb6cd000c6..a401b234dcc354aa80c8c59e0986d006a14e9e1d 100644 --- a/crypto/encryptdecrypt_test.go +++ b/crypto/encryptdecrypt_test.go @@ -58,14 +58,10 @@ func setup(t *testing.T) { // this makes it possible for the reception key to decrypt the // transmission key without spinning up a whole server to decouple them - nk[i].TransmissionKeys.Base = cyclic.NewInt(1) - nk[i].TransmissionKeys.Recursive = cyclic.NewIntFromString( - "ad333f4ccea0ccf2afcab6c1b9aa2384e561aee970046e39b7f2a78c3942a251", 16) - nk[i].ReceptionKeys.Base = cyclic.NewInt(1) - nk[i].ReceptionKeys.Recursive = grp.Inverse( - nk[i].TransmissionKeys.Recursive, cyclic.NewInt(1)) + nk[i].TransmissionKey = cyclic.NewInt(1) + nk[i].ReceptionKey = cyclic.NewInt(1) } - user.TheSession = user.NewSession(u, "", nk, nil) + user.TheSession = user.NewSession(u, "", nk, nil, nil) } func TestEncryptDecrypt(t *testing.T) { @@ -83,7 +79,7 @@ func TestEncryptDecrypt(t *testing.T) { // Generate a compound encryption key encryptionKey := cyclic.NewInt(1) for _, key := range user.TheSession.GetKeys() { - baseKey := key.TransmissionKeys.Base + baseKey := key.TransmissionKey partialEncryptionKey := cmix.NewEncryptionKey(salt, baseKey, Grp) Grp.Mul(encryptionKey, encryptionKey, partialEncryptionKey) //TODO: Add KMAC generation here diff --git a/io/collate_test.go b/io/collate_test.go index 82e60794e78c50f9cec1636968e67578af70b9b5..fd86fc805dfdc9ce31568e4e96a35a781d34f1d7 100644 --- a/io/collate_test.go +++ b/io/collate_test.go @@ -11,7 +11,7 @@ import ( "encoding/hex" "gitlab.com/elixxir/client/parse" "gitlab.com/elixxir/client/user" - "gitlab.com/elixxir/crypto/cyclic" + "gitlab.com/elixxir/crypto/signature" "gitlab.com/elixxir/primitives/format" "gitlab.com/elixxir/primitives/id" "math/rand" @@ -21,9 +21,14 @@ import ( func TestCollator_AddMessage(t *testing.T) { + rng := rand.New(rand.NewSource(42)) + params := signature.NewDSAParams(rng, signature.L3072N256) + privateKey := params.PrivateKeyGen(rng) + publicKey := privateKey.PublicKeyGen() + user.TheSession = user.NewSession(&user.User{id.NewUserFromUint(8, t), "test"}, "", - []user.NodeKeys{}, cyclic.NewInt(0)) + []user.NodeKeys{}, publicKey, privateKey) collator := &collator{ pendingMessages: make(map[PendingMessageKey]*multiPartMessage), @@ -47,8 +52,8 @@ func TestCollator_AddMessage(t *testing.T) { for j := range partitions { fm := format.NewMessage() - fm.SetSender(id.NewUserFromUint(5,t)) - fm.SetRecipient(id.NewUserFromUint(6,t)) + fm.SetSender(id.NewUserFromUint(5, t)) + fm.SetRecipient(id.NewUserFromUint(6, t)) fm.SetPayloadData(partitions[j]) result = collator.AddMessage(fm, time.Minute) @@ -70,9 +75,14 @@ func TestCollator_AddMessage(t *testing.T) { func TestCollator_AddMessage_Timeout(t *testing.T) { + rng := rand.New(rand.NewSource(42)) + params := signature.NewDSAParams(rng, signature.L3072N256) + privateKey := params.PrivateKeyGen(rng) + publicKey := privateKey.PublicKeyGen() + user.TheSession = user.NewSession(&user.User{id.NewUserFromUint(8, t), "test"}, "", - []user.NodeKeys{}, cyclic.NewInt(0)) + []user.NodeKeys{}, publicKey, privateKey) collator := &collator{ pendingMessages: make(map[PendingMessageKey]*multiPartMessage), diff --git a/io/messaging.go b/io/messaging.go index ad75d4f241a1db802a666bd2118e138d9ac9a6f8..9b1ee14741981a5d16109b87ec522a8ab782d579 100644 --- a/io/messaging.go +++ b/io/messaging.go @@ -127,7 +127,7 @@ func send(senderID *id.User, message *format.Message) error { // Generate a compound encryption key encryptionKey := cyclic.NewInt(1) for _, key := range user.TheSession.GetKeys() { - baseKey := key.TransmissionKeys.Base + baseKey := key.TransmissionKey partialEncryptionKey := cmix.NewEncryptionKey(salt, baseKey, crypto.Grp) crypto.Grp.Mul(encryptionKey, partialEncryptionKey, encryptionKey) //TODO: Add KMAC generation here @@ -240,7 +240,7 @@ func (m *messaging) receiveMessagesFromGateway( salt := newMessage.Salt decryptionKey := cyclic.NewInt(1) for _, key := range user.TheSession.GetKeys() { - baseKey := key.ReceptionKeys.Base + baseKey := key.ReceptionKey partialDecryptionKey := cmix.NewDecryptionKey(salt, baseKey, crypto.Grp) crypto.Grp.Mul(decryptionKey, partialDecryptionKey, decryptionKey) diff --git a/payment/orderedStorage_test.go b/payment/orderedStorage_test.go index dfa38fa5b0ee2f3f860b783aff5d433aaa3ca589..a522f5eb1eb9b0cdaaa96ba14dbe8f7e6038832a 100644 --- a/payment/orderedStorage_test.go +++ b/payment/orderedStorage_test.go @@ -11,7 +11,7 @@ import ( "gitlab.com/elixxir/client/globals" "gitlab.com/elixxir/client/user" "gitlab.com/elixxir/crypto/coin" - "gitlab.com/elixxir/crypto/cyclic" + "gitlab.com/elixxir/crypto/signature" "gitlab.com/elixxir/primitives/id" "math/rand" "os" @@ -24,8 +24,14 @@ func TestCreateOrderedStorage_New(t *testing.T) { globals.LocalStorage = nil globals.InitStorage(&globals.RamStorage{}, "") userID := id.NewUserFromUint(1, t) + + rng := rand.New(rand.NewSource(42)) + params := signature.NewDSAParams(rng, signature.L3072N256) + privateKey := params.PrivateKeyGen(rng) + publicKey := privateKey.PublicKeyGen() + s := user.NewSession(&user.User{userID, "test"}, "", []user.NodeKeys{}, - cyclic.NewInt(0)) + publicKey, privateKey) // show that the ordered list does not exist key := "TestOrderedList" @@ -70,8 +76,13 @@ func TestCreateOrderedStorage_Load(t *testing.T) { globals.LocalStorage = nil globals.InitStorage(&globals.RamStorage{}, "") userID := id.NewUserFromUint(1, t) - s := user.NewSession(&user.User{userID, "test"}, "", []user.NodeKeys{}, - cyclic.NewInt(0)) + + rng := rand.New(rand.NewSource(42)) + params := signature.NewDSAParams(rng, signature.L3072N256) + privateKey := params.PrivateKeyGen(rng) + publicKey := privateKey.PublicKeyGen() + + s := user.NewSession(&user.User{userID, "test"}, "", []user.NodeKeys{}, publicKey, privateKey) // show that the ordered list does not exist key := "TestOrderedList" @@ -118,8 +129,13 @@ func TestOrderedCoinStorage_Value(t *testing.T) { globals.LocalStorage = nil globals.InitStorage(&globals.RamStorage{}, "") userID := id.NewUserFromUint(1, t) - s := user.NewSession(&user.User{userID, "test"}, "", []user.NodeKeys{}, - cyclic.NewInt(0)) + + rng1 := rand.New(rand.NewSource(42)) + params := signature.NewDSAParams(rng1, signature.L3072N256) + privateKey := params.PrivateKeyGen(rng1) + publicKey := privateKey.PublicKeyGen() + + s := user.NewSession(&user.User{userID, "test"}, "", []user.NodeKeys{}, publicKey, privateKey) src := rand.NewSource(42) rng := rand.New(src) @@ -142,8 +158,13 @@ func TestOrderedCoinStorage_Add_Empty(t *testing.T) { globals.LocalStorage = nil globals.InitStorage(&globals.RamStorage{}, "") userID := id.NewUserFromUint(1, t) - s := user.NewSession(&user.User{userID, "test"}, "", []user.NodeKeys{}, - cyclic.NewInt(0)) + + rng := rand.New(rand.NewSource(42)) + params := signature.NewDSAParams(rng, signature.L3072N256) + privateKey := params.PrivateKeyGen(rng) + publicKey := privateKey.PublicKeyGen() + + s := user.NewSession(&user.User{userID, "test"}, "", []user.NodeKeys{}, publicKey, privateKey) cs, err := coin.NewSleeve(69) @@ -165,9 +186,13 @@ func TestOrderedCoinStorage_Add_Multi(t *testing.T) { globals.LocalStorage = nil globals.InitStorage(&globals.RamStorage{}, "") userID := id.NewUserFromUint(1, t) - s := user.NewSession(&user.User{userID, "test"}, "", []user.NodeKeys{}, - cyclic.NewInt(0)) + rng := rand.New(rand.NewSource(42)) + params := signature.NewDSAParams(rng, signature.L3072N256) + privateKey := params.PrivateKeyGen(rng) + publicKey := privateKey.PublicKeyGen() + + s := user.NewSession(&user.User{userID, "test"}, "", []user.NodeKeys{}, publicKey, privateKey) ocs := OrderedCoinStorage{&[]coin.Sleeve{}, 0, s} unorderdValues := []uint64{100, 13, 44} @@ -210,8 +235,13 @@ func TestOrderedCoinStorage_Add_Save(t *testing.T) { globals.LocalStorage = nil globals.InitStorage(&globals.RamStorage{}, "") userID := id.NewUserFromUint(1, t) - s := user.NewSession(&user.User{userID, "test"}, "", []user.NodeKeys{}, - cyclic.NewInt(0)) + + rng := rand.New(rand.NewSource(42)) + params := signature.NewDSAParams(rng, signature.L3072N256) + privateKey := params.PrivateKeyGen(rng) + publicKey := privateKey.PublicKeyGen() + + s := user.NewSession(&user.User{userID, "test"}, "", []user.NodeKeys{}, publicKey, privateKey) // show that the ordered list does not exist key := "TestOrderedList" @@ -249,8 +279,13 @@ func TestOrderedCoinStorage_Get(t *testing.T) { globals.LocalStorage = nil globals.InitStorage(&globals.RamStorage{}, "") uid := id.NewUserFromUint(1, t) - s := user.NewSession(&user.User{uid, "test"}, "", []user.NodeKeys{}, - cyclic.NewInt(0)) + + rng := rand.New(rand.NewSource(42)) + params := signature.NewDSAParams(rng, signature.L3072N256) + privateKey := params.PrivateKeyGen(rng) + publicKey := privateKey.PublicKeyGen() + + s := user.NewSession(&user.User{uid, "test"}, "", []user.NodeKeys{}, publicKey, privateKey) key := "TestOrderedList" @@ -289,7 +324,13 @@ func TestOrderedCoinStorage_Pop(t *testing.T) { globals.LocalStorage = nil globals.InitStorage(&globals.RamStorage{}, "") uid := id.NewUserFromUint(1, t) - s := user.NewSession(&user.User{uid, "test"}, "", []user.NodeKeys{}, cyclic.NewInt(0)) + + rng := rand.New(rand.NewSource(42)) + params := signature.NewDSAParams(rng, signature.L3072N256) + privateKey := params.PrivateKeyGen(rng) + publicKey := privateKey.PublicKeyGen() + + s := user.NewSession(&user.User{uid, "test"}, "", []user.NodeKeys{}, publicKey, privateKey) key := "TestOrderedList" @@ -328,8 +369,13 @@ func TestOrderedCoinStorage_Pop_Save(t *testing.T) { globals.LocalStorage = nil globals.InitStorage(&globals.RamStorage{}, "") uid := id.NewUserFromUint(1, t) - s := user.NewSession(&user.User{uid, "test"}, "", []user.NodeKeys{}, - cyclic.NewInt(0)) + + rng := rand.New(rand.NewSource(42)) + params := signature.NewDSAParams(rng, signature.L3072N256) + privateKey := params.PrivateKeyGen(rng) + publicKey := privateKey.PublicKeyGen() + + s := user.NewSession(&user.User{uid, "test"}, "", []user.NodeKeys{}, publicKey, privateKey) key := "TestOrderedList" @@ -386,8 +432,13 @@ func TestOrderedCoinStorage_Fund_Insufficient(t *testing.T) { globals.LocalStorage = nil globals.InitStorage(&globals.RamStorage{}, "") uid := id.NewUserFromUint(1, t) - s := user.NewSession(&user.User{uid, "test"}, "", []user.NodeKeys{}, - cyclic.NewInt(0)) + + rng := rand.New(rand.NewSource(42)) + params := signature.NewDSAParams(rng, signature.L3072N256) + privateKey := params.PrivateKeyGen(rng) + publicKey := privateKey.PublicKeyGen() + + s := user.NewSession(&user.User{uid, "test"}, "", []user.NodeKeys{}, publicKey, privateKey) key := "TestOrderedList" @@ -429,8 +480,13 @@ func TestOrderedCoinStorage_Fund_Single_Exact(t *testing.T) { globals.LocalStorage = nil globals.InitStorage(&globals.RamStorage{}, "") uid := id.NewUserFromUint(1, t) - s := user.NewSession(&user.User{uid, "test"}, "", []user.NodeKeys{}, - cyclic.NewInt(0)) + + rng := rand.New(rand.NewSource(42)) + params := signature.NewDSAParams(rng, signature.L3072N256) + privateKey := params.PrivateKeyGen(rng) + publicKey := privateKey.PublicKeyGen() + + s := user.NewSession(&user.User{uid, "test"}, "", []user.NodeKeys{}, publicKey, privateKey) key := "TestOrderedList" @@ -473,8 +529,13 @@ func TestOrderedCoinStorage_Fund_Multi_Exact(t *testing.T) { globals.LocalStorage = nil globals.InitStorage(&globals.RamStorage{}, "") uid := id.NewUserFromUint(1, t) - s := user.NewSession(&user.User{uid, "test"}, "", []user.NodeKeys{}, - cyclic.NewInt(0)) + + rng := rand.New(rand.NewSource(42)) + params := signature.NewDSAParams(rng, signature.L3072N256) + privateKey := params.PrivateKeyGen(rng) + publicKey := privateKey.PublicKeyGen() + + s := user.NewSession(&user.User{uid, "test"}, "", []user.NodeKeys{}, publicKey, privateKey) key := "TestOrderedList" @@ -529,8 +590,13 @@ func TestOrderedCoinStorage_Fund_Multi_Exact_Split(t *testing.T) { globals.LocalStorage = nil globals.InitStorage(&globals.RamStorage{}, "") uid := id.NewUserFromUint(1, t) - s := user.NewSession(&user.User{uid, "test"}, "", []user.NodeKeys{}, - cyclic.NewInt(0)) + + rng := rand.New(rand.NewSource(42)) + params := signature.NewDSAParams(rng, signature.L3072N256) + privateKey := params.PrivateKeyGen(rng) + publicKey := privateKey.PublicKeyGen() + + s := user.NewSession(&user.User{uid, "test"}, "", []user.NodeKeys{}, publicKey, privateKey) key := "TestOrderedList" @@ -585,8 +651,13 @@ func TestOrderedCoinStorage_Fund_Organization(t *testing.T) { globals.LocalStorage = nil globals.InitStorage(&globals.RamStorage{}, "") uid := id.NewUserFromUint(1, t) - s := user.NewSession(&user.User{uid, "test"}, "", []user.NodeKeys{}, - cyclic.NewInt(0)) + + rng := rand.New(rand.NewSource(42)) + params := signature.NewDSAParams(rng, signature.L3072N256) + privateKey := params.PrivateKeyGen(rng) + publicKey := privateKey.PublicKeyGen() + + s := user.NewSession(&user.User{uid, "test"}, "", []user.NodeKeys{}, publicKey, privateKey) key := "TestOrderedList" @@ -626,8 +697,13 @@ func TestOrderedCoinStorage_Fund_Multi_Exact_Split_Change(t *testing.T) { globals.LocalStorage = nil globals.InitStorage(&globals.RamStorage{}, "") uid := id.NewUserFromUint(1, t) - s := user.NewSession(&user.User{uid, "test"}, "", []user.NodeKeys{}, - cyclic.NewInt(0)) + + rng := rand.New(rand.NewSource(42)) + params := signature.NewDSAParams(rng, signature.L3072N256) + privateKey := params.PrivateKeyGen(rng) + publicKey := privateKey.PublicKeyGen() + + s := user.NewSession(&user.User{uid, "test"}, "", []user.NodeKeys{}, publicKey, privateKey) key := "TestOrderedList" @@ -690,8 +766,13 @@ func TestOrderedStorage_FileLoading(t *testing.T) { } globals.InitStorage(&globals.DefaultStorage{}, storagePath+filename) uid := id.NewUserFromUint(1, t) - s := user.NewSession(&user.User{uid, "test"}, "", []user.NodeKeys{}, - cyclic.NewInt(0)) + + rng := rand.New(rand.NewSource(42)) + params := signature.NewDSAParams(rng, signature.L3072N256) + privateKey := params.PrivateKeyGen(rng) + publicKey := privateKey.PublicKeyGen() + + s := user.NewSession(&user.User{uid, "test"}, "", []user.NodeKeys{}, publicKey, privateKey) // show that the ordered list does not exist key := "TestOrderedList" diff --git a/payment/transactionList_test.go b/payment/transactionList_test.go index bddfa14292ac080ea0be2d0d97073cc73841ece2..ffd42eea044e1b9470752591650bbdbf62b7a353 100644 --- a/payment/transactionList_test.go +++ b/payment/transactionList_test.go @@ -12,7 +12,7 @@ import ( "gitlab.com/elixxir/client/parse" "gitlab.com/elixxir/client/user" "gitlab.com/elixxir/crypto/coin" - "gitlab.com/elixxir/crypto/cyclic" + "gitlab.com/elixxir/crypto/signature" "gitlab.com/elixxir/primitives/id" "math" "math/rand" @@ -25,9 +25,15 @@ import ( func TestCreateTransactionList_New(t *testing.T) { globals.LocalStorage = nil globals.InitStorage(&globals.RamStorage{}, "") + + rng := rand.New(rand.NewSource(42)) + params := signature.NewDSAParams(rng, signature.L3072N256) + privateKey := params.PrivateKeyGen(rng) + publicKey := privateKey.PublicKeyGen() + s := user.NewSession(&user.User{User: id.NewUserFromUint(1, t), Nick: "test"}, "", - []user.NodeKeys{}, cyclic.NewInt(0)) + []user.NodeKeys{}, publicKey, privateKey) // show that the ordered list does not exist key := "TestTransactionList" @@ -72,8 +78,15 @@ func TestCreateTransactionList_New(t *testing.T) { func TestCreateTransactionList_Load(t *testing.T) { globals.LocalStorage = nil globals.InitStorage(&globals.RamStorage{}, "") + + rng := rand.New(rand.NewSource(42)) + params := signature.NewDSAParams(rng, signature.L3072N256) + privateKey := params.PrivateKeyGen(rng) + publicKey := privateKey.PublicKeyGen() + s := user.NewSession(&user.User{User: id.NewUserFromUint(1, t), - Nick: "test"}, "", []user.NodeKeys{}, cyclic.NewInt(0)) + Nick: "test"}, "", + []user.NodeKeys{}, publicKey, privateKey) // show that the transaction list does not exist key := "TestTransactionList" @@ -116,8 +129,15 @@ func TestTransactionList_Value(t *testing.T) { globals.LocalStorage = nil globals.InitStorage(&globals.RamStorage{}, "") + + rng1 := rand.New(rand.NewSource(42)) + params := signature.NewDSAParams(rng1, signature.L3072N256) + privateKey := params.PrivateKeyGen(rng1) + publicKey := privateKey.PublicKeyGen() + s := user.NewSession(&user.User{User: id.NewUserFromUint(1, t), - Nick: "test"}, "", []user.NodeKeys{}, cyclic.NewInt(0)) + Nick: "test"}, "", + []user.NodeKeys{}, publicKey, privateKey) src := rand.NewSource(42) rng := rand.New(src) @@ -138,8 +158,15 @@ func TestTransactionList_Value(t *testing.T) { func TestTransactionList_Upsert_Empty(t *testing.T) { globals.LocalStorage = nil globals.InitStorage(&globals.RamStorage{}, "") + + rng := rand.New(rand.NewSource(42)) + params := signature.NewDSAParams(rng, signature.L3072N256) + privateKey := params.PrivateKeyGen(rng) + publicKey := privateKey.PublicKeyGen() + s := user.NewSession(&user.User{User: id.NewUserFromUint(1, t), - Nick: "test"}, "", []user.NodeKeys{}, cyclic.NewInt(0)) + Nick: "test"}, "", + []user.NodeKeys{}, publicKey, privateKey) tMap := make(map[parse.MessageHash]*Transaction) @@ -164,8 +191,15 @@ func TestTransactionList_Upsert_Empty(t *testing.T) { func TestTransactionList_Upsert_Multi(t *testing.T) { globals.LocalStorage = nil globals.InitStorage(&globals.RamStorage{}, "") + + rng := rand.New(rand.NewSource(42)) + params := signature.NewDSAParams(rng, signature.L3072N256) + privateKey := params.PrivateKeyGen(rng) + publicKey := privateKey.PublicKeyGen() + s := user.NewSession(&user.User{User: id.NewUserFromUint(1, t), - Nick: "test"}, "", []user.NodeKeys{}, cyclic.NewInt(0)) + Nick: "test"}, "", + []user.NodeKeys{}, publicKey, privateKey) t1 := Transaction{Memo: "1"} t1Hash := parse.Message{ @@ -198,8 +232,15 @@ func TestTransactionList_Upsert_Multi(t *testing.T) { func TestTransactionList_Upsert_Save(t *testing.T) { globals.LocalStorage = nil globals.InitStorage(&globals.RamStorage{}, "") + + rng := rand.New(rand.NewSource(42)) + params := signature.NewDSAParams(rng, signature.L3072N256) + privateKey := params.PrivateKeyGen(rng) + publicKey := privateKey.PublicKeyGen() + s := user.NewSession(&user.User{User: id.NewUserFromUint(1, t), - Nick: "test"}, "", []user.NodeKeys{}, cyclic.NewInt(0)) + Nick: "test"}, "", + []user.NodeKeys{}, publicKey, privateKey) key := "TestTransactionList" @@ -241,8 +282,15 @@ func TestTransactionList_Upsert_Save(t *testing.T) { func TestTransactionList_Get(t *testing.T) { globals.LocalStorage = nil globals.InitStorage(&globals.RamStorage{}, "") + + rng := rand.New(rand.NewSource(42)) + params := signature.NewDSAParams(rng, signature.L3072N256) + privateKey := params.PrivateKeyGen(rng) + publicKey := privateKey.PublicKeyGen() + s := user.NewSession(&user.User{User: id.NewUserFromUint(1, t), - Nick: "test"}, "", []user.NodeKeys{}, cyclic.NewInt(0)) + Nick: "test"}, "", + []user.NodeKeys{}, publicKey, privateKey) t1 := Transaction{Memo: "1"} t1Hash := parse.Message{ @@ -286,8 +334,15 @@ func TestTransactionList_Get(t *testing.T) { func TestTransactionList_Pop(t *testing.T) { globals.LocalStorage = nil globals.InitStorage(&globals.RamStorage{}, "") + + rng := rand.New(rand.NewSource(42)) + params := signature.NewDSAParams(rng, signature.L3072N256) + privateKey := params.PrivateKeyGen(rng) + publicKey := privateKey.PublicKeyGen() + s := user.NewSession(&user.User{User: id.NewUserFromUint(1, t), - Nick: "test"}, "", []user.NodeKeys{}, cyclic.NewInt(0)) + Nick: "test"}, "", + []user.NodeKeys{}, publicKey, privateKey) t1 := Transaction{Memo: "1"} t1Hash := parse.Message{ @@ -331,8 +386,15 @@ func TestTransactionList_Pop(t *testing.T) { func TestTransactionList_Pop_Invalid(t *testing.T) { globals.LocalStorage = nil globals.InitStorage(&globals.RamStorage{}, "") + + rng := rand.New(rand.NewSource(42)) + params := signature.NewDSAParams(rng, signature.L3072N256) + privateKey := params.PrivateKeyGen(rng) + publicKey := privateKey.PublicKeyGen() + s := user.NewSession(&user.User{User: id.NewUserFromUint(1, t), - Nick: "test"}, "", []user.NodeKeys{}, cyclic.NewInt(0)) + Nick: "test"}, "", + []user.NodeKeys{}, publicKey, privateKey) t1 := Transaction{Memo: "1"} t1Hash := parse.Message{ @@ -365,8 +427,15 @@ func TestTransactionList_Pop_Invalid(t *testing.T) { func TestTransactionList_Pop_Save(t *testing.T) { globals.LocalStorage = nil globals.InitStorage(&globals.RamStorage{}, "") + + rng := rand.New(rand.NewSource(42)) + params := signature.NewDSAParams(rng, signature.L3072N256) + privateKey := params.PrivateKeyGen(rng) + publicKey := privateKey.PublicKeyGen() + s := user.NewSession(&user.User{User: id.NewUserFromUint(1, t), - Nick: "test"}, "", []user.NodeKeys{}, cyclic.NewInt(0)) + Nick: "test"}, "", + []user.NodeKeys{}, publicKey, privateKey) key := "TestTransactionList" @@ -430,8 +499,16 @@ func TestTransactionList_GetKeysByTimestampDescending(t *testing.T) { // populate a transaction list with some items globals.LocalStorage = nil globals.InitStorage(&globals.RamStorage{}, "") + + rng := rand.New(rand.NewSource(42)) + params := signature.NewDSAParams(rng, signature.L3072N256) + privateKey := params.PrivateKeyGen(rng) + publicKey := privateKey.PublicKeyGen() + s := user.NewSession(&user.User{User: id.NewUserFromUint(1, t), - Nick: "test"}, "", []user.NodeKeys{}, cyclic.NewInt(0)) + Nick: "test"}, "", + []user.NodeKeys{}, publicKey, privateKey) + transactionMap := make(map[parse.MessageHash]*Transaction) transactions := TransactionList{ transactionMap: &transactionMap, diff --git a/payment/wallet_test.go b/payment/wallet_test.go index 9a09402c50626bd401951ced529ce77de476bf07..797a13ef61a35e3006f16afd3a6d19a715063281 100644 --- a/payment/wallet_test.go +++ b/payment/wallet_test.go @@ -17,8 +17,9 @@ import ( "gitlab.com/elixxir/client/switchboard" "gitlab.com/elixxir/client/user" "gitlab.com/elixxir/crypto/coin" - "gitlab.com/elixxir/crypto/cyclic" + "gitlab.com/elixxir/crypto/signature" "gitlab.com/elixxir/primitives/id" + "math/rand" "reflect" "testing" "time" @@ -32,9 +33,15 @@ func TestWallet_registerInvoice(t *testing.T) { value := uint64(85) globals.LocalStorage = nil + + rng := rand.New(rand.NewSource(42)) + params := signature.NewDSAParams(rng, signature.L3072N256) + privateKey := params.PrivateKeyGen(rng) + publicKey := privateKey.PublicKeyGen() + globals.InitStorage(&globals.RamStorage{}, "") s := user.NewSession(&user.User{User: payee, Nick: "Taxman McGee"}, "", - []user.NodeKeys{}, cyclic.NewInt(0)) + []user.NodeKeys{}, publicKey, privateKey) or, err := createTransactionList(OutboundRequestsTag, s) if err != nil { @@ -86,8 +93,14 @@ func TestWallet_registerInvoice(t *testing.T) { func TestCreateWallet(t *testing.T) { globals.LocalStorage = nil globals.InitStorage(&globals.RamStorage{}, "") + + rng := rand.New(rand.NewSource(42)) + params := signature.NewDSAParams(rng, signature.L3072N256) + privateKey := params.PrivateKeyGen(rng) + publicKey := privateKey.PublicKeyGen() + s := user.NewSession(&user.User{User: id.NewUserFromUint(1, t), - Nick: "test"}, "", []user.NodeKeys{}, cyclic.NewInt(0)) + Nick: "test"}, "", []user.NodeKeys{}, publicKey, privateKey) _, err := CreateWallet(s, false) @@ -137,8 +150,14 @@ func TestWallet_Invoice(t *testing.T) { // Set up the wallet and its storage globals.LocalStorage = nil globals.InitStorage(&globals.RamStorage{}, "") + + rng := rand.New(rand.NewSource(42)) + params := signature.NewDSAParams(rng, signature.L3072N256) + privateKey := params.PrivateKeyGen(rng) + publicKey := privateKey.PublicKeyGen() + s := user.NewSession(&user.User{User: payee, Nick: "Taxman McGee"}, "", - []user.NodeKeys{}, cyclic.NewInt(0)) + []user.NodeKeys{}, publicKey, privateKey) or, err := createTransactionList(OutboundRequestsTag, s) if err != nil { @@ -319,12 +338,12 @@ func (ms *MockSession) GetKeys() []user.NodeKeys { return nil } -func (ms *MockSession) GetPrivateKey() *cyclic.Int { +func (ms *MockSession) GetPrivateKey() *signature.DSAPrivateKey { *ms = true return nil } -func (ms *MockSession) GetPublicKey() *cyclic.Int { +func (ms *MockSession) GetPublicKey() *signature.DSAPublicKey { *ms = true return nil } @@ -379,7 +398,7 @@ func TestInvoiceListener_Hear(t *testing.T) { globals.LocalStorage = nil globals.InitStorage(&globals.RamStorage{}, "") s := user.NewSession(&user.User{User: payer, Nick: "CEO MF DOOM"}, "", - []user.NodeKeys{}, cyclic.NewInt(0)) + []user.NodeKeys{}, nil, nil) ir, err := createTransactionList(InboundRequestsTag, s) if err != nil { @@ -467,7 +486,7 @@ func TestWallet_Invoice_Error(t *testing.T) { globals.LocalStorage = nil globals.InitStorage(&globals.RamStorage{}, "") s := user.NewSession(&user.User{User: payee, Nick: "Taxman McGee"}, "", - []user.NodeKeys{}, cyclic.NewInt(0)) + []user.NodeKeys{}, nil, nil) or, err := createTransactionList(OutboundRequestsTag, s) if err != nil { @@ -511,7 +530,7 @@ func TestResponseListener_Hear(t *testing.T) { globals.LocalStorage = nil globals.InitStorage(&globals.RamStorage{}, "") s := user.NewSession(&user.User{User: payer, Nick: "Darth Icky"}, "", - []user.NodeKeys{}, cyclic.NewInt(0)) + []user.NodeKeys{}, nil, nil) walletAmount := uint64(8970) paymentAmount := uint64(962) @@ -637,7 +656,7 @@ func TestResponseListener_Hear_Failure(t *testing.T) { globals.LocalStorage = nil globals.InitStorage(&globals.RamStorage{}, "") s := user.NewSession(&user.User{User: payer, Nick: "Darth Icky"}, "", - []user.NodeKeys{}, cyclic.NewInt(0)) + []user.NodeKeys{}, nil, nil) walletAmount := uint64(8970) paymentAmount := uint64(962) @@ -749,7 +768,7 @@ func TestWallet_Pay_NoChange(t *testing.T) { globals.LocalStorage = nil globals.InitStorage(&globals.RamStorage{}, "") s := user.NewSession(&user.User{User: payer, Nick: "Darth Icky"}, "", - []user.NodeKeys{}, cyclic.NewInt(0)) + []user.NodeKeys{}, nil, nil) paymentAmount := uint64(5008) walletAmount := uint64(5008) @@ -842,7 +861,7 @@ func TestWallet_Pay_YesChange(t *testing.T) { globals.LocalStorage = nil globals.InitStorage(&globals.RamStorage{}, "") s := user.NewSession(&user.User{User: payer, Nick: "Darth Icky"}, "", - []user.NodeKeys{}, cyclic.NewInt(0)) + []user.NodeKeys{}, nil, nil) paymentAmount := uint64(2611) walletAmount := uint64(5008) @@ -968,7 +987,7 @@ func TestReceiptListener_Hear(t *testing.T) { globals.LocalStorage = nil globals.InitStorage(&globals.RamStorage{}, "") s := user.NewSession(&user.User{User: payer, Nick: "Darth Icky"}, "", - []user.NodeKeys{}, cyclic.NewInt(0)) + []user.NodeKeys{}, nil, nil) walletAmount := uint64(8970) paymentAmount := uint64(1234) diff --git a/user/session.go b/user/session.go index a5b2d5a0c54917ecc1c39fe9432d46fe7afc930a..5cdff7917f3174b310d3e741616cd4dc5786d89e 100644 --- a/user/session.go +++ b/user/session.go @@ -11,13 +11,14 @@ import ( "encoding/gob" "errors" "fmt" + jww "github.com/spf13/jwalterweatherman" "gitlab.com/elixxir/client/globals" "gitlab.com/elixxir/crypto/cyclic" + "gitlab.com/elixxir/crypto/signature" "gitlab.com/elixxir/primitives/id" "math/rand" "sync" "time" - jww "github.com/spf13/jwalterweatherman" ) // Errors @@ -33,8 +34,8 @@ type Session interface { GetGWAddress() string SetGWAddress(addr string) GetKeys() []NodeKeys - GetPrivateKey() *cyclic.Int - GetPublicKey() *cyclic.Int + GetPrivateKey() *signature.DSAPrivateKey + GetPublicKey() *signature.DSAPublicKey GetLastMessageID() string SetLastMessageID(id string) StoreSession() error @@ -48,26 +49,19 @@ type Session interface { } type NodeKeys struct { - TransmissionKeys RatchetKey - ReceptionKeys RatchetKey - ReceiptKeys RatchetKey - ReturnKeys RatchetKey -} - -type RatchetKey struct { - Base *cyclic.Int - Recursive *cyclic.Int + TransmissionKey *cyclic.Int + ReceptionKey *cyclic.Int } // Creates a new Session interface for registration -func NewSession(u *User, GatewayAddr string, nk []NodeKeys, publicKey *cyclic.Int) Session { +func NewSession(u *User, GatewayAddr string, nk []NodeKeys, publicKey *signature.DSAPublicKey, privateKey *signature.DSAPrivateKey) Session { // With an underlying Session data structure return Session(&SessionObj{ CurrentUser: u, GWAddress: GatewayAddr, // FIXME: don't store this here Keys: nk, - PrivateKey: cyclic.NewMaxInt(), + PrivateKey: privateKey, PublicKey: publicKey, InterfaceMap: make(map[string]interface{}), }) @@ -112,7 +106,7 @@ func LoadSession(UID *id.User) (Session, error) { } else if UID == nil { jww.ERROR.Panic("Dereferencing nil param UID") } - + // Line of the actual crash if *session.CurrentUser.User != *UID { err = errors.New(fmt.Sprintf( @@ -136,8 +130,8 @@ type SessionObj struct { GWAddress string Keys []NodeKeys - PrivateKey *cyclic.Int - PublicKey *cyclic.Int + PrivateKey *signature.DSAPrivateKey + PublicKey *signature.DSAPublicKey // Last received message ID. Check messages after this on the gateway. LastMessageID string @@ -166,13 +160,13 @@ func (s *SessionObj) GetKeys() []NodeKeys { return s.Keys } -func (s *SessionObj) GetPrivateKey() *cyclic.Int { +func (s *SessionObj) GetPrivateKey() *signature.DSAPrivateKey { s.LockStorage() defer s.UnlockStorage() return s.PrivateKey } -func (s *SessionObj) GetPublicKey() *cyclic.Int { +func (s *SessionObj) GetPublicKey() *signature.DSAPublicKey { s.LockStorage() defer s.UnlockStorage() return s.PublicKey @@ -255,14 +249,6 @@ func (s *SessionObj) Immolate() error { s.GWAddress = burntString(len(s.GWAddress)) s.GWAddress = "" - clearCyclicInt(s.PrivateKey) - clearCyclicInt(s.PublicKey) - - for i := 0; i < len(s.Keys); i++ { - clearRatchetKeys(&s.Keys[i].TransmissionKeys) - clearRatchetKeys(&s.Keys[i].ReceptionKeys) - } - TheSession = nil s.UnlockStorage() @@ -336,11 +322,6 @@ func clearCyclicInt(c *cyclic.Int) { c.SetInt64(0) } -func clearRatchetKeys(r *RatchetKey) { - clearCyclicInt(r.Base) - clearCyclicInt(r.Recursive) -} - // FIXME Shouldn't we just be putting pseudorandom bytes in to obscure the mem? func burntString(length int) string { diff --git a/user/session_test.go b/user/session_test.go index 48913dc1d6a4f95169e176bd0baf56fd8081bab9..1a5c5db2f31f21926d0e01d29c81ce552babd083 100644 --- a/user/session_test.go +++ b/user/session_test.go @@ -10,7 +10,10 @@ import ( "crypto/sha256" "gitlab.com/elixxir/client/globals" "gitlab.com/elixxir/crypto/cyclic" + "gitlab.com/elixxir/crypto/signature" "gitlab.com/elixxir/primitives/id" + "math/rand" + "reflect" "testing" ) @@ -31,10 +34,8 @@ func TestUserSession(t *testing.T) { keys := make([]NodeKeys, 1) keys[0] = NodeKeys{ - TransmissionKeys: RatchetKey{cyclic.NewInt(2), cyclic.NewInt(2)}, - ReceptionKeys: RatchetKey{cyclic.NewInt(2), cyclic.NewInt(2)}, - ReceiptKeys: RatchetKey{cyclic.NewInt(2), cyclic.NewInt(2)}, - ReturnKeys: RatchetKey{cyclic.NewInt(2), cyclic.NewInt(2)}, + TransmissionKey: cyclic.NewInt(2), + ReceptionKey: cyclic.NewInt(2), } err := globals.InitStorage(&globals.RamStorage{}, "") @@ -43,10 +44,12 @@ func TestUserSession(t *testing.T) { t.Errorf("User Session: Local storage could not be created: %s", err.Error()) } - //Ask Ben if there should be a Node Address here! - ses := NewSession(u, "abc", keys, cyclic.NewInt(2)) + rng := rand.New(rand.NewSource(42)) + params := signature.NewDSAParams(rng, signature.L3072N256) + privateKey := params.PrivateKeyGen(rng) + publicKey := privateKey.PublicKeyGen() + ses := NewSession(u, "abc", keys, publicKey, privateKey) - ses.(*SessionObj).PrivateKey.SetInt64(2) ses.SetLastMessageID("totally unique ID") err = ses.StoreSession() @@ -124,32 +127,16 @@ func TestUserSession(t *testing.T) { for i := 0; i < len(TheSession.GetKeys()); i++ { - if TheSession.GetPublicKey().Cmp(cyclic.NewInt(2)) != 0 { + if !reflect.DeepEqual(*TheSession.GetPublicKey(), *publicKey) { t.Errorf("Error: Public key not set correctly!") - } else if TheSession.GetKeys()[i].ReceiptKeys.Base.Cmp(cyclic. + } else if !reflect.DeepEqual(*TheSession.GetPrivateKey(), *privateKey) { + t.Errorf("Error: Private key not set correctly!") + } else if TheSession.GetKeys()[i].ReceptionKey.Cmp(cyclic. NewInt(2)) != 0 { - t.Errorf("Error: Receipt base key not set correctly!") - } else if TheSession.GetKeys()[i].ReceiptKeys.Recursive.Cmp(cyclic. + t.Errorf("Error: Reception key not set correctly!") + } else if TheSession.GetKeys()[i].TransmissionKey.Cmp(cyclic. NewInt(2)) != 0 { - t.Errorf("Error: Receipt base key not set correctly!") - } else if TheSession.GetKeys()[i].ReceptionKeys.Base.Cmp(cyclic. - NewInt(2)) != 0 { - t.Errorf("Error: Receipt base key not set correctly!") - } else if TheSession.GetKeys()[i].ReceptionKeys.Recursive.Cmp( - cyclic.NewInt(2)) != 0 { - t.Errorf("Error: Receipt base key not set correctly!") - } else if TheSession.GetKeys()[i].ReturnKeys.Base.Cmp(cyclic. - NewInt(2)) != 0 { - t.Errorf("Error: Receipt base key not set correctly!") - } else if TheSession.GetKeys()[i].ReturnKeys.Recursive.Cmp(cyclic. - NewInt(2)) != 0 { - t.Errorf("Error: Receipt base key not set correctly!") - } else if TheSession.GetKeys()[i].TransmissionKeys.Base.Cmp(cyclic. - NewInt(2)) != 0 { - t.Errorf("Error: Receipt base key not set correctly!") - } else if TheSession.GetKeys()[i].TransmissionKeys.Recursive.Cmp( - cyclic.NewInt(2)) != 0 { - t.Errorf("Error: Receipt base key not set correctly!") + t.Errorf("Error: Transmission key not set correctly!") } pass++ @@ -235,15 +222,43 @@ func TestGetPubKey(t *testing.T) { keys := make([]NodeKeys, 1) keys[0] = NodeKeys{ - TransmissionKeys: RatchetKey{cyclic.NewInt(2), cyclic.NewInt(2)}, - ReceptionKeys: RatchetKey{cyclic.NewInt(2), cyclic.NewInt(2)}, - ReceiptKeys: RatchetKey{cyclic.NewInt(2), cyclic.NewInt(2)}, - ReturnKeys: RatchetKey{cyclic.NewInt(2), cyclic.NewInt(2)}, + TransmissionKey: cyclic.NewInt(2), + ReceptionKey: cyclic.NewInt(2), } - ses := NewSession(u, "abc", keys, cyclic.NewInt(2)) + rng := rand.New(rand.NewSource(42)) + params := signature.NewDSAParams(rng, signature.L3072N256) + privateKey := params.PrivateKeyGen(rng) + publicKey := privateKey.PublicKeyGen() + ses := NewSession(u, "abc", keys, publicKey, privateKey) + pubKey := ses.GetPublicKey() - if pubKey.Cmp(cyclic.NewInt(2)) != 0 { - t.Errorf("Public key is not set correctly!") + if !reflect.DeepEqual(pubKey, publicKey) { + t.Errorf("Public key not returned correctly!") + } +} + +func TestGetPrivKey(t *testing.T) { + u := new(User) + UID := id.NewUserFromUint(1, t) + + u.User = UID + u.Nick = "Mario" + + keys := make([]NodeKeys, 1) + keys[0] = NodeKeys{ + TransmissionKey: cyclic.NewInt(2), + ReceptionKey: cyclic.NewInt(2), + } + + rng := rand.New(rand.NewSource(42)) + params := signature.NewDSAParams(rng, signature.L3072N256) + privateKey := params.PrivateKeyGen(rng) + publicKey := privateKey.PublicKeyGen() + ses := NewSession(u, "abc", keys, publicKey, privateKey) + + privKey := ses.GetPrivateKey() + if !reflect.DeepEqual(*privKey, *privateKey) { + t.Errorf("Private key is not returned correctly!") } } diff --git a/user/user.go b/user/user.go index 17a58652808ec774994ea9714c945de1ac1f16b2..c107da8ac98d9c7a02a0f7cfccf4736d6ec5b05a 100644 --- a/user/user.go +++ b/user/user.go @@ -70,16 +70,10 @@ func newRegistry() Registry { // TODO We need a better way to generate base/recursive keys h := sha256.New() h.Write([]byte(string(20000 + i))) - k.TransmissionKeys.Base = cyclic.NewIntFromBytes(h.Sum(nil)) - h = sha256.New() - h.Write([]byte(string(30000 + i))) - k.TransmissionKeys.Recursive = cyclic.NewIntFromBytes(h.Sum(nil)) + k.TransmissionKey = cyclic.NewIntFromBytes(h.Sum(nil)) h = sha256.New() h.Write([]byte(string(40000 + i))) - k.ReceptionKeys.Base = cyclic.NewIntFromBytes(h.Sum(nil)) - h = sha256.New() - h.Write([]byte(string(50000 + i))) - k.ReceptionKeys.Recursive = cyclic.NewIntFromBytes(h.Sum(nil)) + k.ReceptionKey = cyclic.NewIntFromBytes(h.Sum(nil)) // Add user to collection and lookup table uc[*t.User] = t diff --git a/user/user_test.go b/user/user_test.go index 9e21a5139d91cf98c0eb6888dfe0d79d680d25fa..88a0d69debced3816122d3373f7144b734b82705 100644 --- a/user/user_test.go +++ b/user/user_test.go @@ -73,35 +73,21 @@ func TestUserRegistry(t *testing.T) { h := sha256.New() h.Write([]byte(string(20001))) key := cyclic.NewIntFromBytes(h.Sum(nil)) - if keys.TransmissionKeys.Base.Text(16) != key.Text(16) { + if keys.TransmissionKey.Text(16) != key.Text(16) { t.Errorf("LookupKeys returned an incorrect key. "+ "Expected:%v \nActual%v", key.Text(16), - keys.TransmissionKeys.Base.Text(16)) - } - h = sha256.New() - h.Write([]byte(string(30001))) - key = cyclic.NewIntFromBytes(h.Sum(nil)) - if keys.TransmissionKeys.Recursive.Text(16) != key.Text(16) { - t.Errorf("LookupKeys returned an incorrect key. "+ - "Expected:%v \nActual%v", key.Text(16), - keys.TransmissionKeys.Recursive.Text(16)) + keys.TransmissionKey.Text(16)) } + h = sha256.New() h.Write([]byte(string(40001))) key = cyclic.NewIntFromBytes(h.Sum(nil)) - if keys.ReceptionKeys.Base.Text(16) != key.Text(16) { + if keys.ReceptionKey.Text(16) != key.Text(16) { t.Errorf("LookupKeys returned an incorrect key. "+ "Expected:%v \nActual%v", key.Text(16), - keys.ReceptionKeys.Base.Text(16)) - } - h = sha256.New() - h.Write([]byte(string(50001))) - key = cyclic.NewIntFromBytes(h.Sum(nil)) - if keys.ReceptionKeys.Recursive.Text(16) != key.Text(16) { - t.Errorf("LookupKeys returned an incorrect key. "+ - "Expected:%v \nActual%v", key.Text(16), - keys.ReceptionKeys.Recursive.Text(16)) + keys.ReceptionKey.Text(16)) } + // Test delete user Users.DeleteUser(id.NewUserFromUint(2, t))