Encrypt0

Enc0Message messages are used when there is a single receiver. The sender assumes that the receiver knows how to find/derive the CEK to decrypt the payload. In case there are multiple receivers (each using a different key) or the receiver requires more information to find/derive the CEK a EncMessage should be used.

../../_images/encrypt0.png
class pycose.messages.enc0message.Enc0Message(phdr=None, uhdr=None, payload=b'', external_aad=b'', key=None, *args, **kwargs)

Create a COSE_encrypt0 message.

Parameters:
  • phdr – Protected header bucket.

  • uhdr – Unprotected header bucket.

  • payload – The payload (will be encrypted and authenticated).

  • external_aad – External data (is authenticated but not transported in the message).

  • key – The Symmetric COSE key for encryption/decryption of the message

Returns:

Returns a COSE Encrypt0 message object.

encode(tag=True, encrypt=True, *args, **kwargs)

Encode and protect the COSE_Encrypt0 message.

Parameters:
  • tag – Boolean value which indicates if the COSE message will have a CBOR tag.

  • encrypt – Boolean which activates or deactivates the payload protection.

Returns:

Returns a CBOR-encoded COSE Encrypt0 message.

classmethod decode(received, *args, **kwargs)

Decode received COSE message based on the CBOR tag.

If called on CoseMessage, this function can decode any supported message type. Otherwise, if called on a sub-class of CoseMessage, only messages of that type will be allowed to be decoded.

Parameters:

received – COSE messages encoded as bytes

Raises:
  • AttributeError – When the COSE message, it cannot be decoded properly

  • ValueError – The received parameter must be bytes

  • KeyError – thrown when the CBOR tag, identifying the COSE message is unrecognized

  • TypeError – thrown when the messages cannot be decoded properly

  • TypeError – The message’s tag does not match the expected type.

Returns:

An initialized CoseMessage

decrypt(*args, **kwargs)

Decrypts the payload.

Raises:

CoseException – When the key is not of type ‘SymmetricKey’.

Returns:

plaintext as bytes

encrypt(*args, **kwargs)

Encrypts the payload.

Raises:

CoseException – When the key is not of type ‘SymmetricKey’.

Returns:

ciphertext as bytes

get_attr(attribute, default=None)

Fetches an header attribute from the COSE header buckets.

Parameters:
  • attribute – A header parameter to fetch from the buckets.

  • default – A default return value in case the attribute was not found

Raises:

CoseException – When the same attribute is found in both the protected and unprotected header.

Returns:

If found returns a header attribute else ‘None’ or the default value

property phdr_encoded

Encode the protected header.

Returns:

Returns the encoded protected header.

property uhdr_encoded

Encode the unprotected header.

Returns:

Returns the encoded unprotected header.