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
- (Optional) message blob is signed: raw message blob is signed with sender's private key using RSASSA-PSS with SHA1 hashing algorithm.
- 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**>, }
- The payload is converted to UTF-8 JSON.
- A random AES-256-CBC key (32 bytes) and IV (16 bytes) are generated.
- JSON payload is encrypted with AES-256-CBC using the previously created key and IV.
- AES Key and IV are both encrypted with recipient's public key using RSA with PKCS#1 OAEP padding.
- 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>, }
- The object is converted to UTF-8 JSON.
- This JSON representation of the Package is then used to make a postMessage(data, recipientKey) call.
Decrypting the message
- Client extracts and decodes the JSON Package object from the message info block obtained through retrieveMessage(id) call.
- 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).
- 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.
- Client decodes the JSON representation of Payload into actual Payload object.
- Client decodes the Base64-encoded blob and signature from the Payload.
- 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.