LIBTOMCRYPT(3)

πŸ’– LIBTOMCRYPT πŸ’–

okay bestie, so this is the libtomcrypt page from the Library Functions Manual (section 3, the "library functions" girlies). it's a public domain open source crypthographic toolkit and yes we are going to read the whole thing together, no skimming, no skipping, periodt ✨


🌸 NAME 🌸

libtomcrypt β€” public domain open source crypthographic toolkit

it's giving "free, open, does cryptography." no cap.


πŸŽ€ SYNOPSIS πŸŽ€

#include <tomcrypt.h>

Link with -ltomcrypt (use pkg-config --libs libtomcrypt) 🫢

like, that's the whole vibe. include the header, link the lib, go be a cryptographer.


πŸ¦‹ DESCRIPTION πŸ¦‹

so libtomcrypt is documented in /usr/share/doc/libtomcrypt-dev/crypt.pdf. that's the real manual, the actual lore, the extended universe. πŸ“–

but to give you a very brief introduction, the following example is provided. very brief. like, appetizer brief. but it slays.


⭐ EXAMPLE ⭐

not me about to hand you the entire AES-XTS example verbatim, because every line of this matters πŸ’…
/* AES-XTS example for libtomcrypt. (c) 2008 Michael Stapelberg, Public Domain */
#include <stdint.h>
#include <string.h>
#include <tomcrypt.h>

static symmetric_xts xts;

/*
 * Initializes AES-XTS for use with encrypt(). Key must be at least 32 bytes long, only
 * the first 32 bytes will be used.
 *
 */
void initialize_xts(unsigned char *key) {
  int idx, err;
  unsigned char aeskey1[16], aeskey2[16];

  /* You can use 32 different ciphers simultaneously. Before using a cipher, you must register it. */
  register_cipher(&aes_desc);

  /* Get the index of the cipher registered before */
  if ((idx = find_cipher("aes")) == -1) {
          fprintf(stderr, "ERROR: AES not available in libtomcrypt. Please upgrade/fix libtomcrypt.\n");
          exit(EXIT_FAILURE);
  }

  /* Set up the two private keys required by AES-XTS (see 3.4.10 of crypt.pdf) */
  strncpy((char*)aeskey1, key, 16);
  strncpy((char*)aeskey2, key+16, 16);

  printf("Initializing with keys \"%.16s\" and \"%.16s\" (AES-XTS)\n", aeskey1, aeskey2);

  /* Initialize AES-XTS */
  if ((err = xts_start(idx, aeskey1, aeskey2, 16, 0, &xts)) != CRYPT_OK) {
          fprintf(stderr, "ERROR starting XTS: %s\n", error_to_string(err));
          exit(EXIT_FAILURE);
  }
}

/*
 * Encrypts the input (of input_size) and stores the result in output. The piece index
 * is required because XTS wants a tweak for each block so that it doesn't generate
 * patterns which would be visible in the encrypted output.
 *
 */
void encrypt(const uint8_t *input, uint8_t *output, int input_size, int piece_idx) {
  unsigned char tweak[256];
  int err;

  memset(tweak, '\0', 256);
  snprintf((char*)tweak, 256, "%d", piece_idx);

  if ((err = xts_encrypt(input, input_size, output, tweak, &xts)) != CRYPT_OK) {
          fprintf(stderr, "ERROR in AES encryption: %d: %s\n", err, error_to_string(err));
          exit(EXIT_FAILURE);
  }
}

like… the way that this does the whole register-cipher, find-cipher, split-the-key-in-two, start-XTS, encrypt-with-a-tweak pipeline? main character energy fr fr πŸ”₯ and the tweak thing is so real β€” XTS needs a per-block tweak so it doesn't leak visible patterns in the encrypted output. iconic. no patterns, no problem. 🧚

also note the key deets: the key must be at least 32 bytes long, and only the first 32 bytes will be used. you can use 32 different ciphers simultaneously, and before using a cipher you must register it. those aren't optional, bestie, that's just how it is. πŸ’œ


🌈 SEE ALSO 🌈

  • pkg-config(1)

πŸ‘‘ AUTHOR πŸ‘‘

libtomcrypt was written by Tom St Denis.

This manual page was written by Michael Stapelberg <michael@stapelberg.de>, for the Debian project (and may be used by others). 🫢


and that's the whole page, bestie!! this came from the June 2009 era of the LIBTOMCRYPT(3) manual page (Library Functions Manual section, obvs πŸ’…) β€” so if you see it floating around, that's where it's from. go forth and encrypt responsibly, mwah πŸ’–βœ¨πŸŽ€

πŸ“¦ where this page comes from