Chacha-Poly1305

Bindings for the portable (any platform that is 32-bits or higher) implementation of chacha-poly1305.

Danger

The nonce MUST be unique per invocation with the same key and MUST NOT be predicable. Failure to meet those two requirements makes it trivial to find the key.

The nonce CANNOT be random, as there is not enough entropy in 12 bytes to guarantee uniqueness. It CANNOT be a counter as it would be predicable.

RFC 7539 gives guidance to safely generate nonces for chacha-poly1305. pyhacl.drbg.DRBGRandom can be used too, albeit slow.

class pyhacl.aead.chacha_poly1305._EncryptReturn(output: bytes, tag: bytes)

The return type of encrypt().

output: bytes

Alias for field number 0

tag: bytes

Alias for field number 1

pyhacl.aead.chacha_poly1305.decrypt(input_: bytes, data: bytes, key: bytes, nonce: bytes, tag: bytes) bytes

Decrypt input_ and verify tag.

Parameters:
  • input – The encrypted text to be decrypted.

  • data – The associated data over which a tag is computed and compared against tag.

  • key – The 32 bytes private key.

  • nonce – The 12 bytes nonce used by the other side.

  • tag – The 16 bytes tag computed by the other side that is verified.

Returns:

The decrypted text.

Raises:
  • ValueError – When the key or nonce or tag has incorrect length.

  • HACLError – When the underlying C function returned an error.

Binding for Hacl_AEAD_Chacha20Poly1305_decrypt.

pyhacl.aead.chacha_poly1305.encrypt(input_: bytes, data: bytes, key: bytes, nonce: bytes) _EncryptReturn

Encrypt input_ and produce an authenticated tag over data.

Parameters:
  • input – The plain text to be enciphered.

  • data – The associated data for which an authenticated tag is computed.

  • key – The 32 bytes private key.

  • nonce – A unique and unpredictable 12 bytes nonce. See RFC 7538 for guidance.

Returns:

a 2-items named tuple with:

output (bytes, index 0)

The encrypted output of same length as the input.

tag (bytes, index 1)

The 16 bytes authenticated tag.

Raises:

ValueError – When the key or nonce has incorrect length.

Binding for Hacl_AEAD_Chacha20Poly1305_encrypt.