Updated README file.

This commit is contained in:
LoRd_MuldeR 2021-03-14 16:07:47 +01:00
parent 811cf7410c
commit 925e882e03
Signed by: mulder
GPG Key ID: 2B5913365F57E03F

307
README.md
View File

@ -64,6 +64,35 @@ The following options are available:
- **`<length>`**:
* Speicifes the length of the passphrase to be generated, in characters. If *not* specified, defaults to 24.
Examples
--------
Examples on how to use the SlunkCrypt command-line application:
1. Let's generate a new secure password first:
slunkcrypt --make-pw
Example output:
cdG2=fh<C=3[SSCzf[)iDjIV
2. Now, encrypt the plaintext message, using the generated password:
slunkcrypt --encrypt "cdG2=fh<C=3[SSCzf[)iDjIV" plaintext.txt ciphertext.enc
Optionally, let's have a look at the ciphertext:
hexdump -C ciphertext.enc
3. Finally, decrypt the ciphertext, using the same password as before:
slunkcrypt --decrypt "cdG2=fh<C=3[SSCzf[)iDjIV" ciphertext.enc plaintext.out
Optionally, verify that files are actually the same:
cmp --silent plaintext.txt plaintext.out && echo "files are the same"
Programming Interface (API)
===========================
@ -90,7 +119,7 @@ Allocate and initialize a new SlunkCrypt encryption/decryption context.
*Note:* It is recommended to generate a random nonce via the `slunkcrypt_generate_nonce()` function for each message!
* `passwd`
The password to "protect" the message. The password is given as an array of bytes (`uint8_t`), e.g. UTF-8 encoded characters; a terminating NULL character is *not* required, as the length of the password is specified explicitly. The same password **may** be used to encrypt *multiple* messages. Also, the same password **must** be used for both, encryption *and* decryption; it will *only* be possible decrypt the ciphertext, if the "correct" password is known. The password must be kept confidential under all circumstances!
The password to "protect" the message. The password is given as a byte array (`uint8_t`), e.g. UTF-8 encoded characters; a terminating NULL character is *not* required, as the length of the password is specified explicitly. The same password **may** be used to encrypt *multiple* messages. Also, the same password **must** be used for both, encryption *and* decryption; it will *only* be possible decrypt the ciphertext, if the "correct" password is known. The password must be kept confidential under all circumstances!
*Note:* In order to thwart *brute force* attacks, it is recommended to choose a "random" password that is at least 12 characters in length and that consists of upper-case characters, lower-case characters, digits as well as other "special" characters.
@ -174,12 +203,12 @@ Encrypt the next message chunk, using separate input/output buffers.
The existing SlunkCrypt context to be used for encrypting the message chunk. This context will be updated.
* `input`
A pointer to the *input* buffer containing the next chunk of the plaintext to be encrypted. The plaintext is given as an array of bytes (`uint8_t`). This can be arbitrary binary data, e.g. UTF-8 encoded text. NULL bytes are **not** treated specially.
A pointer to the *input* buffer containing the next chunk of the plaintext to be encrypted. The plaintext is given as a byte array (`uint8_t`). This can be arbitrary binary data, e.g. UTF-8 encoded text. NULL bytes are **not** treated specially.
The *input* buffer must contain *at least* `length` bytes of data. If the buffer is longer than `length` bytes, then only the first `length` bytes will be processed and the remainder is ignored!
* `output`
A pointer to the *output* buffer where the ciphertext chunk that corresponds to the given plaintext chunk will be stored. The ciphertext is stored as an array of bytes (`uint8_t`); it has the same length as the plaintext data.
A pointer to the *output* buffer where the ciphertext chunk that corresponds to the given plaintext chunk will be stored. The ciphertext is stored as a byte array (`uint8_t`); it has the same length as the plaintext data.
The *output* buffer must provide sufficient space for storing *at least* `length` bytes of encrypted data. If the buffer is longer than `length` bytes, then only the first `length` bytes of the buffer will be filled with encrypted data!
@ -204,7 +233,7 @@ Encrypt the next message chunk, using a single buffer.
The existing SlunkCrypt context to be used for encrypting the message chunk. This context will be updated.
* `buffer`
A pointer to the buffer initially containing the next chunk of the plaintext to be encrypted. The plaintext is given as an array of bytes (`uint8_t`). This can be arbitrary binary data, e.g. UTF-8 encoded text. NULL bytes are **not** treated specially. The ciphertext chunk that corresponds to the given plaintext chunk will be stored to the *same* buffer, thus replacing the plaintext data.
A pointer to the buffer initially containing the next chunk of the plaintext to be encrypted. The plaintext is given as a byte array (`uint8_t`). This can be arbitrary binary data, e.g. UTF-8 encoded text. NULL bytes are **not** treated specially. The ciphertext chunk that corresponds to the given plaintext chunk will be stored to the *same* buffer, thus replacing the plaintext data.
The buffer must initially contain *at least* `length` bytes of input data; the first `length` bytes of the buffer will be overwritten with the encrypted data. If the buffer is longer than `length` bytes, then only the first `length` bytes will be processed and overwritten.
@ -230,12 +259,12 @@ Decrypt the next ciphertext chunk, using separate input/output buffers.
The existing SlunkCrypt context to be used for decrypting the ciphertext chunk. This context will be updated.
* `input`
A pointer to the *input* buffer containing the next chunk of the ciphertext to be decrypted. The ciphertext is given as an array of bytes (`uint8_t`).
A pointer to the *input* buffer containing the next chunk of the ciphertext to be decrypted. The ciphertext is given as a byte array (`uint8_t`).
The *input* buffer must contain *at least* `length` bytes of data. If the buffer is longer than `length` bytes, then only the first `length` bytes will be processed and the remainder is ignored!
* `output`
A pointer to the *output* buffer where the plaintext chunk that corresponds to the given ciphertext chunk will be stored. The plaintext is stored as an array of bytes (`uint8_t`); it has the same length as the plaintext data.
A pointer to the *output* buffer where the plaintext chunk that corresponds to the given ciphertext chunk will be stored. The plaintext is stored as a byte array (`uint8_t`); it has the same length as the plaintext data.
The *output* buffer must provide sufficient space for storing *at least* `length` bytes of decrypted data. If the buffer is longer than `length` bytes, then only the first `length` bytes of the buffer will be filled with decrypted data!
@ -260,7 +289,7 @@ Decrypt the next ciphertext chunk, using a single buffer.
The existing SlunkCrypt context to be used for decrypting the ciphertext chunk. This context will be updated.
* `buffer`
A pointer to the buffer initially containing the next chunk of the ciphertext to be decrypted. The ciphertext is given as an array of bytes (`uint8_t`). The plaintext chunk that corresponds to the given ciphertext chunk will be stored to the *same* buffer, thus replacing the plaintext data.
A pointer to the buffer initially containing the next chunk of the ciphertext to be decrypted. The ciphertext is given as a byte array (`uint8_t`). The plaintext that corresponds to the given ciphertext will be stored to the *same* buffer, thus replacing the plaintext data.
The buffer must initially contain *at least* `length` bytes of input data; the first `length` bytes of the buffer will be overwritten with the decrypted data. If the buffer is longer than `length` bytes, then only the first `length` bytes will be processed and overwritten.
@ -296,7 +325,7 @@ Generate a sequence of random bytes, using the system's "cryptographically secur
### slunkcrypt_bzero()
Erase the contents of a byte array, by overwriting it with *zero* bytes, guaranteeing that compiler optimizations will **not** remove the erase operation.
Erase the contents of a byte array, by overwriting it with *zero* bytes. Compiler optimizations will **not** remove the erase operation.
void slunkcrypt_bzero(
void* const buffer,
@ -313,6 +342,268 @@ Erase the contents of a byte array, by overwriting it with *zero* bytes, guarant
The size of the buffer to be erased, in bytes.
C++ API
-------
This section describes the "high-level" C++ API of the SlunkCypt library.
### SlunkCryptEncr
Class for *encrypting* data using the SlunkCrypt library.
#### Constructor
Create and initialize a new **``SlunkCryptEncr``** instance. Also generated a new, random nonce.
SlunkCryptEncr::SlunkCryptEncr(
const std::string &passwd
);
***Parameters:***
* `passwd`
The password to "protect" the message. The password is given as an `std::string`, e.g. UTF-8 encoded characters. The same password **may** be used to encrypt *multiple* messages. Also, the same password **must** be used for both, encryption *and* decryption; it will *only* be possible decrypt the ciphertext, if the "correct" password is known. The password must be kept confidential under all circumstances!
*Note:* In order to thwart *brute force* attacks, it is recommended to choose a "random" password that is at least 12 characters in length and that consists of upper-case characters, lower-case characters, digits as well as other "special" characters.
***Exceptions:***
* Throws `std::runtime_error`, if the nonce could not be generated, or if the SlunkCrypt context could not be allocated.
#### SlunkCryptEncr::encrypt() [1]
Encrypt the next message chunk, using separate input/output buffers.
bool encrypt(
const uint8_t* const input,
uint8_t* const output,
size_t length
);
***Parameters:***
* `input`
A pointer to the *input* buffer containing the next chunk of the plaintext to be encrypted. The plaintext is given as a byte array (`uint8_t`). This can be arbitrary binary data, e.g. UTF-8 encoded text. NULL bytes are **not** treated specially.
The *input* buffer must contain *at least* `length` bytes of data. If the buffer is longer than `length` bytes, then only the first `length` bytes will be processed and the remainder is ignored!
* `output`
A pointer to the *output* buffer where the ciphertext chunk that corresponds to the given plaintext chunk will be stored. The ciphertext is stored as a byte array (`uint8_t`); it has the same length as the plaintext data.
The *output* buffer must provide sufficient space for storing *at least* `length` bytes of encrypted data. If the buffer is longer than `length` bytes, then only the first `length` bytes of the buffer will be filled with encrypted data!
* `length`
The length of the plaintext chunk contained in the *input* buffer given by the `input` parameter, in bytes. At the same time, this determines the minimum required size of the *output* buffer given by the `output` parameters, in bytes.
***Return value:***
* If successful, `true` is returned; otherwise `false` is returned.
#### SlunkCryptEncr::encrypt() [2]
Encrypt the next message chunk, using separate input/output containers (`std::vector`).
bool encrypt(
const std::vector<uint8_t> &input,
std::vector<uint8_t> &output
);
***Parameters:***
* `input`
A reference to the `std::vector<uint8_t>` instance containing the next chunk of the plaintext to be encrypted. This can be arbitrary binary data, e.g. UTF-8 encoded text. NULL bytes are **not** treated specially.
* `output`
A reference to the `std::vector<uint8_t>` instance where the ciphertext chunk that corresponds to the given plaintext chunk will be stored.
The `output.size()` must be *greater than or equal* to `input.size()`. If the `output.size()` is larger than the `input.size()`, then only the first `input.size()` elements of `output` will be filled with encrypted data!
***Return value:***
* If successful, `true` is returned; otherwise `false` is returned. The function fails, if the *output* `std::vector` is too small.
#### SlunkCryptEncr::encrypt_inplace() [1]
Encrypt the next message chunk, using a single buffer.
bool encrypt_inplace(
uint8_t* const buffer,
size_t length
);
***Parameters:***
* `buffer`
A pointer to the buffer initially containing the next chunk of the plaintext to be encrypted. The plaintext is given as a byte array (`uint8_t`). This can be arbitrary binary data, e.g. UTF-8 encoded text. NULL bytes are **not** treated specially. The ciphertext chunk that corresponds to the given plaintext chunk will be stored to the *same* buffer, thus replacing the plaintext data.
The buffer must initially contain *at least* `length` bytes of input data; the first `length` bytes of the buffer will be overwritten with the encrypted data. If the buffer is longer than `length` bytes, then only the first `length` bytes will be processed and overwritten.
* `length`
The length of the plaintext chunk initially contained in the input/output buffer given by the `buffer` parameter, in bytes. At the same time, this determines the portion of the input/output buffer that will be overwritten with encrypted data, in bytes.
***Return value:***
* If successful, `true` is returned; otherwise `false` is returned.
#### SlunkCryptEncr::encrypt_inplace() [2]
Encrypt the next message chunk, using a single container (`std::vector`).
bool encrypt_inplace(
std::vector<uint8_t> &buffer
);
***Parameters:***
* `buffer`
A reference to the `std::vector<uint8_t>` initially containing the next chunk of the plaintext to be encrypted. This can be arbitrary binary data, e.g. UTF-8 encoded text. NULL bytes are **not** treated specially. The ciphertext chunk that corresponds to the given plaintext chunk will be stored to the *same* `std::vector<uint8_t>`, thus replacing all the plaintext data.
***Return value:***
* If successful, `true` is returned; otherwise `false` is returned.
#### SlunkCryptEncr::get_nonce()
Retrieve the random nonce that is used to encrypt the message.
uint64_t get_nonce();
***Return value:***
* Returns the nonce that is used to encrypt the message. The purpose of the nonce is to ensure that each message will be encrypted differently, even when the same password is used to encrypt multiple (possibly identical) messages. Therefore, a new random nonce must be chosen for each message! It is not necessary to keep the nonce confidential, but the same nonce must be used for both, encryption and decryption. Typically, the nonce is stored/transmitted alongside the ciphertext.
*Note:* The `SlunkCryptEncr` class automatically generates a new, random nonce for each message to be encrypted. Use *this* function to retrieve that nonce, so that it can be passed to `SlunkCryptDecr` for decryption later.
### SlunkCryptDecr
Class for *decrypting* data using the SlunkCrypt library.
#### Constructor
Create and initialize a new **``SlunkCryptDecr``** instance.
SlunkCryptDecr::SlunkCryptDecr(
const uint64_t nonce,
const std::string& passwd
);
***Parameters:***
* `nonce`
The *nonce* (number used once) to be used for the decryption process. The purpose of the nonce is to ensure that each message will be encrypted differently, even when the same password is used to encrypt *multiple* (possibly identical) messages. Therefore, a new *random* nonce **must** be chosen for each message! It is *not* necessary to keep the nonce confidential, but the same nonce **must** be used for both, encryption *and* decryption. Typically, the nonce is stored/transmitted alongside the ciphertext.
*Note:* The `SlunkCryptEncr` class automatically generates a new, random nonce for each message to be encrypted. Use `SlunkCryptEncr::get_nonce()` to retrieve that nonce, so that it can be passed to `SlunkCryptDecr` for decryption later.
* `passwd`
The password to "protect" the message. The password is given as an `std::string`, e.g. UTF-8 encoded characters. The same password **may** be used to encrypt *multiple* messages. Also, the same password **must** be used for both, encryption *and* decryption; it will *only* be possible decrypt the ciphertext, if the "correct" password is known. The password must be kept confidential under all circumstances!
*Note:* In order to thwart *brute force* attacks, it is recommended to choose a "random" password that is at least 12 characters in length and that consists of upper-case characters, lower-case characters, digits as well as other "special" characters.
***Exceptions:***
* Throws `std::runtime_error`, if the SlunkCrypt context could not be allocated.
#### SlunkCryptDecr::decrypt() [1]
Decrypt the next message chunk, using separate input/output buffers.
bool decrypt(
const uint8_t* const input,
uint8_t* const output,
size_t length
);
***Parameters:***
* `input`
A pointer to the *input* buffer containing the next chunk of the ciphertext to be decrypted. The ciphertext is given as a byte array (`uint8_t`).
The *input* buffer must contain *at least* `length` bytes of data. If the buffer is longer than `length` bytes, then only the first `length` bytes will be processed and the remainder is ignored!
* `output`
A pointer to the *output* buffer where the plaintext chunk that corresponds to the given ciphertext chunk will be stored. The plaintext is stored as a byte array (`uint8_t`); it has the same length as the ciphertext data.
The *output* buffer must provide sufficient space for storing *at least* `length` bytes of decrypted data. If the buffer is longer than `length` bytes, then only the first `length` bytes of the buffer will be filled with decrypted data!
* `length`
The length of the ciphertext chunk contained in the *input* buffer given by the `input` parameter, in bytes. At the same time, this determines the minimum required size of the *output* buffer given by the `output` parameters, in bytes.
***Return value:***
* If successful, `true` is returned; otherwise `false` is returned.
#### SlunkCryptDecr::decrypt() [2]
Decrypt the next message chunk, using separate input/output containers (`std::vector`).
bool decrypt(
const std::vector<uint8_t> &input,
std::vector<uint8_t> &output
);
***Parameters:***
* `input`
A reference to the `std::vector<uint8_t>` instance containing the next chunk of the ciphertext to be decrypted.
* `output`
A reference to the `std::vector<uint8_t>` instance where the plaintext chunk that corresponds to the given ciphertext chunk will be stored.
The `output.size()` must be *greater than or equal* to `input.size()`. If the `output.size()` is greater than the `input.size()`, then only the first `input.size()` elements of `output` will be filled with decrypted data!
***Return value:***
* If successful, `true` is returned; otherwise `false` is returned. The function fails, if the *output* `std::vector` is too small.
#### SlunkCryptDecr::decrypt_inplace() [1]
Decrypt the next message chunk, using a single buffer.
bool decrypt_inplace(
uint8_t* const buffer,
size_t length
);
***Parameters:***
* `buffer`
A pointer to the buffer initially containing the next chunk of the ciphertext to be decrypted. The ciphertext is given as a byte array (`uint8_t`). The plaintext that corresponds to the given ciphertext will be stored to the *same* buffer, replacing the plaintext data.
The buffer must initially contain *at least* `length` bytes of input data; the first `length` bytes of the buffer will be overwritten with the encrypted data. If the buffer is longer than `length` bytes, then only the first `length` bytes will be processed and overwritten.
* `length`
The length of the ciphertext chunk initially contained in the input/output buffer given by the `buffer` parameter, in bytes. At the same time, this determines the portion of the input/output buffer that will be overwritten with decrypted data, in bytes.
***Return value:***
* If successful, `true` is returned; otherwise `false` is returned.
#### SlunkCryptDecr::decrypt_inplace() [2]
Decrypt the next message chunk, using a single container (`std::vector`).
bool decrypt_inplace(
std::vector<uint8_t> &buffer
);
***Parameters:***
* `buffer`
A reference to the `std::vector<uint8_t>` initially containing the next chunk of the ciphertext to be decrypted. The plaintext that corresponds to the given ciphertext will be stored to the *same* `std::vector<uint8_t>`, replacing all the ciphertext data.
***Return value:***
* If successful, `true` is returned; otherwise `false` is returned.
Thread safety
-------------
The following functions are fully "thread-safe" and thus may safely be called by *any* thread at *any* time ***without*** the need for synchronization:
* `slunkcrypt_alloc()`
* `slunkcrypt_random_bytes()`
* `slunkcrypt_bzero()`
* `SlunkCryptEncr::SlunkCryptEncr()`
* `SlunkCryptDecr::SlunkCryptDecr()`
The following functions are "reentrant" and thus may safely be called by *any* thread at *any* time, ***without*** the need for synchronization, provided that each `slunkcrypt_t`, `SlunkCryptEncr` or `SlunkCryptDecr` instance is "owned" (i.e. accessed *exclusively*) and by a *single* thread:
* `slunkcrypt_reset()`
* `slunkcrypt_free()`
* `slunkcrypt_encrypt()`
* `slunkcrypt_encrypt_inplace()`
* `slunkcrypt_decrypt()`
* `slunkcrypt_decrypt_inplace()`
* `SlunkCryptEncr::encrypt()`
* `SlunkCryptEncr::encrypt_inplace()`
* `SlunkCryptEncr::get_nonce()`
* `SlunkCryptDecr::decrypt()`
* `SlunkCryptDecr::decrypt_inplace()`
***Note:*** Iff the same `slunkcrypt_t`, `SlunkCryptEncr` or `SlunkCryptDecr` instance needs to be shared across *multiple* threads, then the application **must** *serialize* any invocations of the above functions (on the shared instance), by an *explicit* "mutex" synchronization!
License
=======