Message Encryption - AtlasNet/Protocols GitHub Wiki

General terms

Message consists of the following parts:

  • Type field: currently the only supported type is 'text'
  • Blob: the binary content of the message. For 'text' type, the blob is UTF-8 encoded text.
  • Timestamp: UNIX timestamp of the message creation

Encrypting the message

  1. (Optional) message blob is signed: raw message blob is signed with sender's private key using RSASSA-PSS with SHA1 hashing algorithm.
  2. Following object ( Payload ) is created:
{
    'type': <string identifier of message type>,
    'blob': <binary content of the message, encoded in Base64>,
    'timestamp': <integer UNIX timestamp>,
    'signature': <if the message is not anonymous, signature described above, encoded in Base64, otherwise **null**>,
    'sender_key': <if the message is not anonymous, PEM-encoded sender public RSA key, otherwise **null**>,
}
  1. The payload is converted to UTF-8 JSON.
  2. A random AES-256-CBC key (32 bytes) and IV (16 bytes) are generated.
  3. JSON payload is encrypted with AES-256-CBC using the previously created key and IV.
  4. AES Key and IV are both encrypted with recipient's public key using RSA with PKCS#1 OAEP padding.
  5. Following object (Package) is created:
{
    "data": <Base64-encoded AES-encrypted JSON payload>,
    "key": <Base64-encoded RSA-encrypted AES key>,
    "iv": <Base64-encoded RSA-encrypted AES IV>,
    "recipient_key": <PEM-encoded recipient's public key>,
}
  1. The object is converted to UTF-8 JSON.
  2. This JSON representation of the Package is then used to make a postMessage(data, recipientKey) call.

Decrypting the message

  1. Client extracts and decodes the JSON Package object from the message info block obtained through retrieveMessage(id) call.
  2. Client uses his private RSA key to decrypt (RSA with PKCS#1 OAEP padding) the Base64-encoded AES key and IV ('key' and 'iv' properties of the Package).
  3. Client uses the decrypted key and IV to decrypt the AES-256-CBC-encrypted, Base64-encoded data property of the Package, obtaining the JSON-encoded Payload object.
  4. Client decodes the JSON representation of Payload into actual Payload object.
  5. Client decodes the Base64-encoded blob and signature from the Payload.
  6. Client uses the recipient_key from the Payload to search for a known contact in his contact database. 6a. If such contact was found, client performs a RSASSA-PSS signature verification of the Blob using contact's public key.