.Sh EXAMPLES
Simple encryption:
.Bd -literal -offset indent
-const uint8_t key [ 32]; /* Secret random key */
+uint8_t key [ 32]; /* Secret random key */
const uint8_t nonce [ 24]; /* Unique nonce (possibly random) */
-const uint8_t plain_text [500]; /* Message to be encrypted */
+uint8_t plain_text [500]; /* Message to be encrypted */
uint8_t cipher_text[500]; /* Will be the encrypted message */
crypto_xchacha20(cipher_text, plain_text, 500, key, nonce);
/* Wipe secrets if they are no longer needed */
.Pp
To decrypt the above:
.Bd -literal -offset indent
-const uint8_t key [ 32]; /* Same key as above */
+uint8_t key [ 32]; /* Same key as above */
const uint8_t nonce[ 24]; /* Same nonce as above */
uint8_t plain_text [500]; /* Will be the decrypted message */
uint8_t cipher_text[500]; /* Encrypted message */
.Pp
Incremental encryption (in blocks of 64 bytes):
.Bd -literal -offset indent
-const uint8_t key [ 32]; /* Secret random key */
+uint8_t key [ 32]; /* Secret random key */
const uint8_t nonce [ 24]; /* Unique nonce (possibly random) */
-const uint8_t plain_text [500]; /* Message to be encrypted */
+uint8_t plain_text [500]; /* Message to be encrypted */
uint8_t cipher_text[500]; /* Will be the encrypted message */
uint64_t ctr; /* Block counter */
int i;
.Fn crypto_xchacha20_ctr
works):
.Bd -literal -offset indent
-const uint8_t key [ 32]; /* Secret random key */
+uint8_t key [ 32]; /* Secret random key */
const uint8_t nonce [ 24]; /* Unique nonce (possibly random) */
-const uint8_t plain_text [500]; /* Message to be encrypted */
+uint8_t plain_text [500]; /* Message to be encrypted */
uint8_t cipher_text[500]; /* Will be the encrypted message */
/* Encrypt the second part of the message first... */
crypto_chacha20(cipher_text + (3 * 64),
.Sh EXAMPLES
Simple hash:
.Bd -literal -offset indent
-const uint8_t key[32]; /* Must have enough entropy */
-const uint8_t in [16]; /* Does not have to be random */
-uint8_t out[32]; /* Will be random iff the above holds */
+uint8_t key[32]; /* Must have enough entropy */
+uint8_t in [16]; /* Does not have to be random */
+uint8_t out[32]; /* Will be random iff the above holds */
crypto_hchacha20(out, key, in);
/* Wipe secrets if they are no longer needed */
crypto_wipe(key, 32);
.Sh EXAMPLES
Generate a public key from a randomly generated secret key:
.Bd -literal -offset indent
-const uint8_t sk[32]; /* Random secret key */
-uint8_t pk[32]; /* Public key */
+uint8_t sk[32]; /* Random secret key */
+uint8_t pk[32]; /* Public key */
crypto_key_exchange_public_key(pk, sk);
/* Wipe secrets if they are no longer needed */
crypto_wipe(sk, 32);
key and their secret key.)
.Bd -literal -offset indent
const uint8_t their_pk [32]; /* Their public key */
-const uint8_t your_sk [32]; /* Your secret key */
+uint8_t your_sk [32]; /* Your secret key */
uint8_t shared_key[32]; /* Shared session key */
crypto_key_exchange(shared_key, your_sk, their_pk);
/* Wipe secrets if they are no longer needed */
.Sh EXAMPLES
Encryption:
.Bd -literal -offset indent
-const uint8_t key [32]; /* Random, secret session key */
+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 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);
.Pp
To decrypt the above:
.Bd -literal -offset indent
-const uint8_t key [32]; /* Same as the above */
+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 */
.Pp
In-place encryption:
.Bd -literal -offset indent
-const uint8_t key [32]; /* Random, secret session key */
+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 */
.Pp
In-place decryption:
.Bd -literal -offset indent
-const uint8_t key [32]; /* Same as the above */
+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 [500]; /* Message to decrypt */
To authenticate a message:
.Bd -literal -offset indent
const uint8_t msg[500]; /* Message to authenticate */
-const uint8_t key[ 32]; /* Random secret key (use only once) */
+uint8_t key[ 32]; /* Random secret key (use only once) */
uint8_t mac[ 16]; /* Message authentication code (MAC) */
crypto_poly1305(mac, msg, 500, key);
/* Wipe the key */
To verify the above message:
.Bd -literal -offset indent
const uint8_t msg [500]; /* Message to verify */
-const uint8_t key [ 32]; /* The above key */
+uint8_t key [ 32]; /* The above key */
const uint8_t mac [ 16]; /* The above MAC */
uint8_t real_mac[ 16]; /* The actual MAC */
crypto_poly1305(real_mac, msg, 500, key);
Incremental authentication:
.Bd -literal -offset indent
const uint8_t msg[500]; /* Message to authenticate */
-const uint8_t key[ 32]; /* Random secret key (use only once) */
+uint8_t key[ 32]; /* Random secret key (use only once) */
uint8_t mac[ 16]; /* Message authentication code (MAC) */
crypto_poly1305_ctx ctx;
crypto_poly1305_init(&ctx, key);
.Sh EXAMPLES
Generate a public key from a random secret key:
.Bd -literal -offset indent
-const uint8_t sk[32]; /* Random secret key */
+uint8_t sk[32]; /* Random secret key */
uint8_t pk[32]; /* Matching public key */
crypto_sign_public_key(pk, sk);
/* Wipe the secret key if it is no longer needed */
.Pp
Sign a message:
.Bd -literal -offset indent
-const uint8_t sk [ 32]; /* Your secret key */
+uint8_t sk [ 32]; /* Your secret key */
const uint8_t pk [ 32]; /* Matching public key */
const uint8_t message [500]; /* Message to sign */
uint8_t signature[ 64];
.Sh EXAMPLES
Sign a message:
.Bd -literal -offset indent
-const uint8_t sk [ 32]; /* Secret key */
+uint8_t sk [ 32]; /* Secret key */
const uint8_t pk [ 32]; /* Public key (optional) */
const uint8_t message [500]; /* Message to sign */
uint8_t signature[ 64]; /* Output signature */
return 1;
}
puts("ok");
+ return 0;
}
.Ed
.Sh SEE ALSO
(This can help nonce management for full duplex communications.)
.Bd -literal -offset indent
const uint8_t their_pk [32]; /* Their public key */
-const uint8_t your_sk [32]; /* Your secret key */
+uint8_t your_sk [32]; /* Your secret key */
uint8_t shared_secret[32]; /* Shared secret (NOT a key) */
crypto_x25519(shared_secret, your_sk, their_pk);
/* Wipe secrets if they are no longer needed */