From: Loup Vaillant Date: Thu, 27 Jul 2023 15:21:30 +0000 (+0200) Subject: Fix various documentation typos & oversights X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=5448fa4a1b5cd04c93c6d846c93d5ccc7ca97214;p=Monocypher.git Fix various documentation typos & oversights With the help of a (now updated) `doc_extract_examples.sh` script. Note: We may want to integrate this script in the test suite, if we end up writing more documentation. Fix #260 --- diff --git a/doc/crypto_aead_lock.3monocypher b/doc/crypto_aead_lock.3monocypher index 7bd1ff5..348c3bf 100644 --- a/doc/crypto_aead_lock.3monocypher +++ b/doc/crypto_aead_lock.3monocypher @@ -378,7 +378,7 @@ const uint8_t nonce[24]; /* Same as the above */ const uint8_t mac [16]; /* Received from along with text */ uint8_t text [12]; /* Message to decrypt */ if (crypto_aead_unlock(text, mac, key, nonce, - NULL, 0 + NULL, 0, text, sizeof(text))) { /* The message is corrupted. * Wipe key if it is no longer needed, @@ -406,7 +406,7 @@ crypto_aead_ctx ctx; crypto_aead_init_x(&ctx, key, nonce); crypto_aead_write(&ctx, cipher_text, mac, NULL, 0, - plain_text, sizeof(plain_text)) + plain_text, sizeof(plain_text)); /* Wipe secrets if they are no longer needed */ crypto_wipe(plain_text, 12); crypto_wipe(key, 32); diff --git a/doc/crypto_argon2.3monocypher b/doc/crypto_argon2.3monocypher index 64cc6f5..37bf24f 100644 --- a/doc/crypto_argon2.3monocypher +++ b/doc/crypto_argon2.3monocypher @@ -341,10 +341,11 @@ crypto_argon2_config config = { .nb_passes = 3, /* 3 iterations */ .nb_lanes = 1 /* Single-threaded */ }; +uint8_t password[14] = "Okay Password!"; crypto_argon2_inputs inputs = { - .pass = (const uint8_t *)"Okay Password!", /* User password */ - .pass_size = 14, /* Password length */ - .salt = salt, /* Salt for the password */ + .pass = password, /* User password */ + .pass_size = sizeof(password), /* Password length */ + .salt = salt, /* Salt for the password */ .salt_size = 16 }; crypto_argon2_extras extras = {0}; /* Extra parameters unused */ @@ -353,17 +354,17 @@ crypto_argon2_extras extras = {0}; /* Extra parameters unused */ * Note the conversion to size_t. * Without it we cannot allocate more than 4GiB. */ -void *work_area = malloc((size_t)nb_blocks * 1024); +void *work_area = malloc((size_t)config.nb_blocks * 1024); if (work_area == NULL) { /* Handle malloc() failure */ /* Wipe secrets if they are no longer needed */ - crypto_wipe(password, password_size); + crypto_wipe(password, sizeof(password)); } else { arc4random_buf(salt, 16); - crypto_argon2i(hash, 32, work_area, - config, inputs, extras); + crypto_argon2(hash, 32, work_area, + config, inputs, extras); /* Wipe secrets if they are no longer needed */ - crypto_wipe(password, password_size); + crypto_wipe(password, sizeof(password)); free(work_area); } .Ed diff --git a/doc/crypto_blake2b.3monocypher b/doc/crypto_blake2b.3monocypher index 4ddc79b..86ff25b 100644 --- a/doc/crypto_blake2b.3monocypher +++ b/doc/crypto_blake2b.3monocypher @@ -451,6 +451,8 @@ void xkdf(uint8_t *okm, size_t okm_size, /* unlimited */ Computing key derivation with BLAKE2b alone (a little tedious indeed): .Bd -literal -offset indent +#define MIN(a, b) ((a) < (b) ? (a) : (b)) + void b2kdf(uint8_t *okm, size_t okm_size, /* unlimited */ uint8_t *ikm, size_t ikm_size, /* unlimited */ uint8_t *salt, size_t salt_size, /* <= 64 bytes */ diff --git a/doc/crypto_sha512.3monocypher b/doc/crypto_sha512.3monocypher index ca2ccdd..d03578e 100644 --- a/doc/crypto_sha512.3monocypher +++ b/doc/crypto_sha512.3monocypher @@ -483,10 +483,10 @@ arc4random_buf(salt, sizeof(salt)); /* Extract */ uint8_t prk[64]; /* pseudo-random key */ crypto_sha512_hmac_ctx ctx; -crypto_sha512_hmac_init (&ctx); -crypto_sha512_hmac_update(&ctx, pk_a); -crypto_sha512_hmac_update(&ctx, pk_b); -crypto_sha512_hmac_update(&ctx, skab); +crypto_sha512_hmac_init (&ctx, salt, sizeof(salt)); +crypto_sha512_hmac_update(&ctx, pk_a, sizeof(pk_a)); +crypto_sha512_hmac_update(&ctx, pk_b, sizeof(pk_b)); +crypto_sha512_hmac_update(&ctx, skab, sizeof(skab)); crypto_sha512_hmac_final (&ctx, prk); /* Expand */ diff --git a/doc/crypto_x25519.3monocypher b/doc/crypto_x25519.3monocypher index a4dedd7..d9abbe3 100644 --- a/doc/crypto_x25519.3monocypher +++ b/doc/crypto_x25519.3monocypher @@ -189,14 +189,14 @@ uint8_t your_sk [32]; /* Your secret key */ uint8_t your_pk [32]; /* Your public key */ uint8_t shared_secret[32]; /* Shared secret (NOT a key) */ arc4random_buf(your_sk, 32); -crypto_x25512(your_pk, your_sk); +crypto_x25519_public_key(your_pk, your_sk); crypto_x25519(shared_secret, your_sk, their_pk); /* Wipe secrets if they are no longer needed */ crypto_wipe(your_sk, 32); uint8_t shared_keys[64]; /* Two shared session keys */ crypto_blake2b_ctx ctx; -crypto_blake2b_init (&ctx); +crypto_blake2b_init (&ctx, 64); crypto_blake2b_update(&ctx, shared_secret, 32); crypto_blake2b_update(&ctx, your_pk , 32); crypto_blake2b_update(&ctx, their_pk , 32); diff --git a/doc/doc_extract_examples.sh b/doc/doc_extract_examples.sh index 71ea9b3..8fd0736 100755 --- a/doc/doc_extract_examples.sh +++ b/doc/doc_extract_examples.sh @@ -53,26 +53,21 @@ # cat << END +#include #include #include +#include #include "../src/monocypher.h" #include "../src/optional/monocypher-ed25519.h" -typedef struct SHA2_CTX { void *x; } SHA2_CTX; -void SHA512Init(SHA2_CTX*); -void SHA512Update(SHA2_CTX*, const void*, size_t); -void SHA512Final(uint8_t*, SHA2_CTX*); void arc4random_buf(void*, size_t); - int main() { END -for f in man/man3/*.3monocypher man/man3/optional/*.3monocypher +for f in *.3monocypher do - # crypto_sign_init_first_pass_custom_hash examples are more complicated - # and can't be tested like this - if [ ! -h "$f" ] && [ "$f" != "man/man3/crypto_sign_init_first_pass_custom_hash.3monocypher" ] + if [ ! -h "$f" ] then echo "// $f" cat "$f" | sed -n "/^\.Bd/,/^\.Ed/p" | sed "s/\.Bd.*/{/" | sed "s/\.Ed/}/"