.It Fa hash
The output hash.
.It Fa hash_size
-Length of the output hash, in bytes.
+Length of
+.Fa hash ,
+in bytes.
Must be between 1 and 64.
64 is recommended.
Anything below 32 is discouraged.
.Xr crypto_wipe 3monocypher
once they are done with it.
.It Fa key_size
-Length of the secret key, in bytes.
+Length of
+.Fa key ,
+in bytes.
Must be between 0 and 64.
32 is a good default.
.It Fa message
.Fa hash
argument.
.It Fa message_size
-The length of the message, in bytes.
+Length of
+.Fa message ,
+in bytes.
.El
.Ss Direct interface
The direct interface has two functions,
uint8_t hash [ 64]; /* Output hash (64 bytes) */
uint8_t message[500]; /* Message to hash */
crypto_blake2b(hash, message, 500);
-/* Wipe secrets if they are no longer needed */
-crypto_wipe(message, 500);
.Ed
.Pp
Computing a message authentication code all at once:
crypto_blake2b_init(&ctx);
for (size_t i = 0; i < 500; i += 100) {
crypto_blake2b_update(&ctx, message + i, 100);
- /* Wipe secrets if they are no longer needed */
- crypto_wipe(message + i, 100);
}
crypto_blake2b_final(&ctx, hash);
.Ed
.Sh EXAMPLES
Encryption:
.Bd -literal -offset indent
-const uint8_t key [32]; /* Random, secret session key */
-const uint8_t nonce[24]; /* Use only once per key */
-const uint8_t *plain_text; /* Secret message */
-size_t text_size; /* Message size (NOT secret) */
-uint8_t mac [16]; /* Message authentication code */
-uint8_t *cipher_text; /* Encrypted message */
-crypto_lock(mac, cipher_text, key, nonce, plain_text, text_size);
+const uint8_t key [32]; /* Random, secret session key */
+const uint8_t nonce [24]; /* Use only once per key */
+const uint8_t plain_text [500]; /* Secret message */
+uint8_t mac [16]; /* Message authentication code */
+uint8_t cipher_text[500]; /* Encrypted message */
+crypto_lock(mac, cipher_text, key, nonce, plain_text, 500);
/* Wipe secrets if they are no longer needed */
-crypto_wipe(plain_text, text_size);
+crypto_wipe(plain_text, 500);
crypto_wipe(key, 32);
/* Transmit cipher_text, nonce, and mac over the network */
.Ed
.Pp
To decrypt the above:
.Bd -literal -offset indent
-const uint8_t key [32]; /* Same as the above */
-const uint8_t nonce[24]; /* Same as the above */
-const uint8_t mac [16]; /* Received from the network */
-const uint8_t *cipher_text; /* Encrypted message */
-size_t text_size; /* Message size (NOT secret) */
-uint8_t *plain_text; /* Secret message */
-if (crypto_unlock(plain_text, key, nonce, mac,
- cipher_text, text_size)) {
+const uint8_t key [32]; /* Same as the above */
+const uint8_t nonce [24]; /* Same as the above */
+const uint8_t cipher_text[500]; /* Encrypted message */
+const uint8_t mac [16]; /* Received from the network */
+uint8_t plain_text [500]; /* Secret message */
+if (crypto_unlock(plain_text, key, nonce, mac, cipher_text, 500)) {
/* The message is corrupted.
* Wipe key if it is no longer needed,
* and abort the decryption.
crypto_wipe(key, 32);
}
/* Wipe secrets if they are no longer needed */
-crypto_wipe(plain_text, text_size);
+crypto_wipe(plain_text, 500);
crypto_wipe(key, 32);
.Ed
.Pp
In-place encryption:
.Bd -literal -offset indent
-const uint8_t key [32]; /* Random, secret session key */
-const uint8_t nonce[24]; /* Use only once per key */
-uint8_t *plain_text; /* Secret message */
-size_t text_size; /* Message size (NOT secret) */
-uint8_t mac [16]; /* Message authentication code */
-crypto_lock(mac, plain_text, key, nonce, plain_text, text_size);
+const uint8_t key [32]; /* Random, secret session key */
+const uint8_t nonce[24]; /* Use only once per key */
+uint8_t text [500]; /* Secret message */
+uint8_t mac [16]; /* Message authentication code */
+crypto_lock(mac, text, key, nonce, text, 500);
/* Wipe secrets if they are no longer needed */
crypto_wipe(key, 32);
-/* Transmit plain_text, nonce, and mac over the network */
+/* Transmit text, nonce, and mac over the network */
.Ed
.Pp
In-place decryption:
.Bd -literal -offset indent
-const uint8_t key [32]; /* Same as the above */
-const uint8_t nonce[24]; /* Same as the above */
-const uint8_t mac [16]; /* Received from the network */
-uint8_t *text; /* Message to decrypt */
-size_t text_size; /* Message size (NOT secret) */
-if (crypto_unlock(text, key, nonce, mac, text, text_size)) {
+const uint8_t key [32]; /* Same as the above */
+const uint8_t nonce[24]; /* Same as the above */
+const uint8_t mac [16]; /* Reived from the network */
+uint8_t text [500]; /* Message to decrypt */
+if (crypto_unlock(text, key, nonce, mac, text, 500)) {
/* The message is corrupted.
* Wipe key if it is no longer needed,
* and abort the decryption.
crypto_wipe(key, 32);
}
/* Wipe secrets if they are no longer needed */
-crypto_wipe(text, text_size);
+crypto_wipe(text, 500);
crypto_wipe(key, 32);
.Ed
.Sh SEE ALSO