Encryption

Bitmessage uses the Elliptic Curve Integrated Encryption Scheme (ECIES) to encrypt the payload of the Message and Broadcast objects.

The scheme uses Elliptic Curve Diffie-Hellman (ECDH) to generate a shared secret used to generate the encryption parameters for Advanced Encryption Standard with 256bit key and Cipher-Block Chaining (AES-256-CBC). The encrypted data will be padded to a 16 byte boundary in accordance to PKCS7. This means that the data is padded with N bytes of value N.

The Key Derivation Function (KDF) used to generate the key material for AES is SHA512. The Message Authentication Code (MAC) scheme used is HMACSHA256.

Format
(See also: Protocol specification)

In order to reconstitute a usable (65 byte) public key (starting with 0x04), the X and Y components need to be expanded by prepending them with 0x00 bytes until the individual component lengths are 32 bytes.

Encryption

 * 1) The destination public key is called K.
 * 2) Generate 16 random bytes using a secure random number generator. Call them IV.
 * 3) Generate a new random EC key pair with private key called r and public key called R.
 * 4) Do an EC point multiply with public key K and private key r. This gives you public key P.
 * 5) Use the X component of public key P and calculate the SHA512 hash H.
 * 6) The first 32 bytes of H are called key_e and the last 32 bytes are called key_m.
 * 7) Pad the input text to a multiple of 16 bytes, in accordance to PKCS7.
 * 8) Encrypt the data with AES-256-CBC, using IV as initialization vector, key_e as encryption key and the padded input text as payload. Call the output cipher text.
 * 9) Calculate a 32 byte MAC with HMACSHA256, using key_m as salt and IV + R + cipher text as data. Call the output MAC.

The resulting data is: IV + R + cipher text + MAC

Decryption

 * 1) The private key used to decrypt is called k.
 * 2) Do an EC point multiply with private key k and public key R. This gives you public key P.
 * 3) Use the X component of public key P and calculate the SHA512 hash H.
 * 4) The first 32 bytes of H are called key_e and the last 32 bytes are called key_m.
 * 5) Calculate MAC' with HMACSHA256, using key_m as salt and IV + R + cipher text as data.
 * 6) Compare MAC with MAC'. If not equal, decryption will fail.
 * 7) Decrypt the cipher text with AES-256-CBC, using IV as initialization vector, key_e as decryption key and the cipher text as payload. The output is the padded input text.

Partial Example
Public key K:

Initialization Vector IV:

Randomly generated key pair with private key r and public key R:

Derived public key P (point multiply r with K):

SHA512 of public key P X component (H):

Padded input:

Cipher text: