cremalink.crypto package

This module provides a set of cryptographic helper functions for handling encryption, decryption, and hashing, primarily using AES and HMAC.

cremalink.crypto.aes_decrypt(enc: str, key: bytes, iv: bytes) bytes[source]

Decrypts a base64-encoded string using AES-128 in CBC mode.

Parameters:
  • enc – The base64-encoded encrypted string.

  • key – The 16-byte decryption key.

  • iv – The 16-byte initialization vector (IV).

Returns:

The decrypted data as bytes, after removing zero-padding.

cremalink.crypto.aes_encrypt(message: str, key: bytes, iv: bytes) str[source]

Encrypts a string using AES-128 in CBC mode with zero-padding.

Parameters:
  • message – The string message to encrypt.

  • key – The 16-byte encryption key.

  • iv – The 16-byte initialization vector (IV).

Returns:

A base64-encoded string of the encrypted data.

cremalink.crypto.extract_bits(raw: bytes, start: int, end: int) bytearray[source]

Extracts a slice from the hexadecimal representation of raw bytes.

Note: The name is a misnomer; it operates on the hex string, not raw bits.

Parameters:
  • raw – The input bytes.

  • start – The starting index in the hex string representation.

  • end – The ending index in the hex string representation.

Returns:

A bytearray corresponding to the specified hex slice.

cremalink.crypto.hmac_for_key_and_data(key: bytes, data: bytes) bytes[source]

Generates an HMAC-SHA256 digest for the given key and data.

Parameters:
  • key – The secret key for the HMAC operation.

  • data – The message data to hash.

Returns:

The raw HMAC-SHA256 digest as bytes.

cremalink.crypto.pad_zero(data: bytes, block_size: int = 16) bytes[source]

Pads data with zero bytes to a multiple of the specified block size.

Note: This padding scheme is not reversible if the original data can end with zero bytes.

Parameters:
  • data – The bytes to pad.

  • block_size – The block size to align to. Defaults to 16.

Returns:

The zero-padded data.

cremalink.crypto.rotate_iv_from_ciphertext(enc: str) bytes[source]

Extracts the last 16 bytes from a ciphertext to be used as the next IV.

This is a common technique in some protocols to chain IVs.

Parameters:

enc – The base64-encoded ciphertext.

Returns:

The last 16 bytes of the raw ciphertext.

cremalink.crypto.unpad_zero(data: bytes) bytes[source]

Removes trailing zero-byte padding from data.

This works by finding the first null byte and truncating the rest.

Parameters:

data – The padded bytes.

Returns:

The unpadded data.