Peer-To-Peer Social Networking in Shychat: Hatem A. Mahmoud
Peer-To-Peer Social Networking in Shychat: Hatem A. Mahmoud
Peer-To-Peer Social Networking in Shychat: Hatem A. Mahmoud
Hatem A. Mahmoud
Khawarezm
[email protected]
III. HANDSHAKING
In this section we elaborate on our handshaking protocol for exchanging secret keys between two ShyChat users, say Alice
and Bob, before they start having any conversations. Although we use human names like Alice and Bob when referring to
phones, all handshaking operations are done automatically and in the background, on behalf of the users. We make the design
decision of using two secret keys for each pair of users communicating with one another. Think of a private conversation
between Alice and Bob as a two-lane road, with messages sent from Alice to Bob being encrypted and decrypted using a secret
key, while those sent from Bob to Alice are encrypted and decrypted using another secret key. This design decision simplifies
the handshaking protocol, specially since secret keys should be updated frequently. Obviously, each of the two users need to
have both secret keys for any meaningful conversation to take place. Our concern is not just to keep the conversation private,
but also to keep the source and the destination of exchanged messages private; that is, a sniffer should not be able to know who
is sending to whom, so long as the GCM service itself is secure.
To see how this works we begin by explaining the structure of messages exchanged between ShyChat users. Each message
begins by an ENCRYPTED flag indicating whether this message is encrypted or not. Unencrypted messages are typically
broadcast messages sent over BC channels; those are usually digitally-signed but never encrypted. Special types of messages
sent over RCV channels may be unencrypted as well; as we see later in this section. In general, if the ENCRYPTED flag at the
beginning of a message is set, then it must be followed by a decryption key identifier indicating which key should be used by
the recipient phone to decrypt this message. A key identifier of 0 indicates that this message was encrypted using the public key
of the recipient phone, as the case during handshaking for example, and thus should be decrypted using the private key of the
recipient phone. Otherwise, a non-zero key identifier indicates that this message was sent from a phone that had already
completed handshaking with the recipient phone. Let the local (recipient) phone be Alice and the remote (sender) phone be Bob.
When the decryption key identifier is non-zero, Alice uses this identifier to lookup her decryption keys table for a secret key to
decrypt this message. The decryption keys table is maintained locally by Alice and contains secret keys generated during
handshaking. Alice uses the decryption secret key associated with Bob’s key identifier to decrypt this message. Once decrypted
and processed, and if a response (e.g., an acknowledgement) is necessary, ShyChat builds a response message and encrypts it.
Alice, just like any other ShyChat user, maintains besides her decryption keys table an encryption keys table, to be used when
sending encrypted messages over RCV channels. Unlike the decryption keys table, the encryption keys table is indexed by
phone numbers of contacts, and contains for each contact either a public key or a secret key. If Bob’s entry in Alice’s encryption
keys table contains a secret key, then it must also contain a remote decryption key identifier to be placed in the header of the
encrypted response sent to Bob. This remote decryption key identifier is the key identifier that Bob will use to lookup his own
decryption keys table for a secret key to decrypt Alice’s response. Having said that, and before we explain our handshaking
protocol itself, we shall also mention that whenever a message sent over a RCV channel is encrypted via a public key, it must
also be digitally signed using the private key of the sender. Digital signature is applied to the message before encryption but is
still excluded from encryption. First thing to be done after such a message is decrypted at the recipient’s side, using the
recipient’s private key, is to check it the way a broadcast message is checked; that is, to extract the sender’s phone number from
the message and use it to lookup the local encryption keys table for the sender’s public key, then use the sender’s public key to
decrypt the digital signature of the message, and compare the resulting digest against a locally generated digest of the decrypted
message.
Here we explain the handshaking protocol itself. Consider the same example of two phones, Alice and Bob. Alice already has
ShyChat installed, while Bob has just installed the app for the first time. Bob begins by broadcasting his public key in a
PUBLIC_KEY message over BC_Bob. If Alice does not have Bob in her contacts list nothing will happen. Otherwise, if Alice
has Bob in her contacts list, Alice will receive Bob’s PUBLIC_KEY broadcast message, will save it in her encryption keys
table, and will respond by sending her own public key to Bob through the channel RCV_Bob in another PUBLIC_KEY
message, with a flag indicating that this is actually a response so as to avoid an infinite loop of public key exchanges. Next,
Alice generates a secret key, saves it in her decryption keys table, obtains a decryption key identifier, and sends both the secret
key and the identifier in an encrypted SECRET_KET message to Bob, informing him that he should use this key and this
identifier whenever he sends encrypted messages to Alice in the future. This SECRET_KEY message is itself encrypted using
Bob’s public key, and thus the decryption key identifier field in the header of the message itself is set to 0, and the message is
digitally signed using Alice’s private key.
If Bob does not have Alice in his contacts list, he will ignore both her PUBLIC_KEY and her SECRET_KEY messages. So
we focus on the case when Alice is in Bob’s contacts list. Bob receives Alice’s PUBLIC_KEY message first, so he creates an
entry for Alice in his encryption keys table, and saves her public key there. Since Alice’s PUBLIC_KEY message has a flag
indicating that it is actually a response, Bob will not take any further steps. Next, Bob receives Alice’s SECRET_KEY message,
so he extracts from it the secret key and the key identifier that were generated by Alice, and saves them in his encryption keys
table, to be the encryption key and the remote decryption key identifier associated with Alice. Now, and only now, Bob marks
Alice “Available” in his contacts list; this means that Bob can initiate a conversation with Alice. However, immediately after
processing Alice’s SECRET_KEY message, Bob starts building his own SECRET_KEY response to Alice; he generates another
secret key, saves it in his decryption keys table, obtains a decryption key identifier, and sends both his key and his identifier in
an encrypted SECRET_KEY message to Alice, informing her that she should use this key and this identifier whenever she sends
encrypted messages to him in the future. Bob’s SECRET_KEY message must have a flag indicating that it is actually a response
so as to avoid an infinite loop of SECRET_KEY exchanges. Also, this SECRET_KEY message is itself encrypted using Alice’s
public key, and thus the decryption key identifier field in the header of the message itself is set to 0, and the message is digitally
signed using Bob’s private key. Finally, Alice receives Bob’s SECRET_KEY message, extracts from it the secret key and the
key identifier that were generated by Bob, and saves them in her encryption keys table, to be the encryption key and the remote
decryption key identifier associated with Bob. Now, and only now, Alice marks Bob “Available” in her contacts list; this means
that she can initiate conversations with Bob.
Notice that message ordering during handshaking matters. Alice’s PUBLIC_KEY message must reach Bob not after her
SECRET_KEY message so that he can check her digital signature. Also, Bob’s SECRET_KEY message must reach Alice not
after any conversation he may initiate with her so that she can encrypt her responses. Although the GCM service always delivers
messages to the destination ISP in the order they are sent, we do not take chances; messages that has to be delivered in order
(e.g., Alice’s PUBLIC_KEY and SECRET_KEY messages) are packed in a single GCM message and unpacked at the
destination; this approach is safer and also more efficient as each GCM invocation costs us an HTTPS POST request. Notice
also that when we process incoming messages; the two operations of updating local data and sending response messages have to
happen atomically; for example, when Alice generates a secret key and sends it to Bob in response to his PUBLIC_KEY
message, we can not tolerate to have one of these two operations succeeds and the other fails. Thus, all automatic responses to
incoming messages are stored locally in the same database where local data updates are performed, and using the same
transaction. If the response messages could not be delivered successfully for whatever reason, e.g. network outage or GCM
service issues, they remain in the local database until ShyChat detects that the communication issue is resolved then they are
sent again.
Now consider the case when Alice is not in Bob’s contacts list at the time when he installs the app, but she gets added to his
contacts list later. ShyChat monitors the user’s local contacts list in the background. Adding a new contact, Alice in our case,
triggers Bob to send his PUBLIC_KEY message automatically to Alice over RCV_Alice. If Alice have not uninstalled and
reinstalled ShyChat since she last received Bob’s PUBLIC_KEY broadcast, Bob’s entries in Alice’s encryption and decryption
key tables are still there; Alice makes sure that Bob’s public key has not changed since she last received his PUBLIC_KEY
message then proceeds with the handshaking protocol using the secret key she generated for Bob before. Otherwise, if Bob’s
public key has changed, indicating that he has uninstalled and reinstalled the app, Alice generates a new secret key for Bob so
that the protocol remains semantically correct. If, after handshaking has completed successfully, either of the two users, say
Alice, removes the other one, Bob, from her contacts list, such event will only mark Bob “deleted” in Alice’s database and Bob
will be informed; however, neither public keys nor secret keys will be affected. Although Alice and Bob will not be able to
initiate conversations with one another until Alice adds Bob back to her contacts list, another round of handshaking will not be
required unless one or both of the two users uninstalls and reinstalls the app.
V. DEMO
ShyChat is available for free on Google’s play store. In this demo, the user may test the various use cases supported by
ShyChat using two or more Android phones having Google Play APIs installed and internet connection available. Besides the
typical day-to-day use cases mentioned in Section IV, users may also try joining and leaving ShyChat’s social network in
different orders and examine how ShyChat handles corner cases robustly.