Skip to content

Commit c4e6c94

Browse files
author
=
committed
Comment improvements in libsodium secretbox eample
1 parent be6ea56 commit c4e6c94

File tree

3 files changed

+8
-8
lines changed

3 files changed

+8
-8
lines changed

sodium/nacl_decrypt_file.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ int main(int argc, char *argv[])
137137
printf("ERROR: Authenticated ciphertext file is too small to contain a nonce and a MAC\n");
138138
}
139139

140-
// The 192-bit nonce doesn't have to be confidential, so it was stored in the ciphertext file first unencrypted
140+
// The 192-bit (24-byte) nonce doesn't have to be secret, so it was stored in the ciphertext file first unencrypted
141141
unsigned char nonce[crypto_secretbox_NONCEBYTES];
142142

143143
// Allocate buffers big enough to hold the message and the authenticated ciphertext
@@ -155,7 +155,7 @@ int main(int argc, char *argv[])
155155
goto exit;
156156
}
157157

158-
// Read in the authenticated ciphertext from the file
158+
// Read in the authenticated ciphertext from the file (ciphertext + 16-byte authentication tag)
159159
bytes_read = fread( ciphertext, 1, ciphertext_len, file_cipher );
160160
if( ciphertext_len != bytes_read )
161161
{

sodium/nacl_encrypt_file.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,15 +152,15 @@ int main(int argc, char *argv[])
152152
printf("Message is %llu bytes: %s\n", message_len, (char*)message);
153153

154154

155-
// The 192-bit nonce doesn't have to be confidential, but it should never ever be reused with the same key
155+
// The 192-bit (24-byte) nonce doesn't have to be confidential, but it should never ever be reused with the same key
156156
unsigned char nonce[crypto_secretbox_NONCEBYTES];
157157
randombytes_buf(nonce, sizeof nonce);
158158

159-
// Encrypts a message with a key and a nonce in combined mode
159+
// Encrypts a message with key and nonce in combined mode where the ciphertext and a 16-byte tag are stored together
160160
printf("Encrypting message and computing an authentication tag ...");
161161
ret = crypto_secretbox_easy(ciphertext, message, message_len, nonce, key);
162162
if ( ret != 0)
163-
{ // The only I can see for this function to fail is if the message length is too large (> 2^64 - 16)
163+
{ // The only way I can see for this function to fail is if the message length is too large (> 2^64 - 16)
164164
printf(" failed. Message length = %lld\n", message_len);
165165
}
166166
printf(" Done\n");
@@ -175,7 +175,7 @@ int main(int argc, char *argv[])
175175
goto exit;
176176
}
177177

178-
// Save the nonce to the authenticated ciphertext file first (unencrypted)
178+
// Save the 24-byte (192-bit) nonce to the authenticated ciphertext file first (unencrypted)
179179
size_t bytes_written = fwrite( nonce, 1, crypto_secretbox_NONCEBYTES, file_cipher );
180180
if( crypto_secretbox_NONCEBYTES != bytes_written )
181181
{

sodium/nacl_symmetric_gen.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ int main(int argc, char *argv[])
6565
char hex_key[KEY_HEX_BYTES];
6666

6767
// Create a random key - equivalent to calling randombytes_buf() but improves code clarity
68-
// crypto_secretbox_keygen(key); // Function added in libsodium version 1.12
69-
randombytes_buf(key, crypto_secretbox_KEYBYTES);
68+
crypto_secretbox_keygen(key); // Function added in libsodium version 1.12
69+
// randombytes_buf(key, crypto_secretbox_KEYBYTES);
7070

7171
// Convert the binary key into a hexadecimal string
7272
sodium_bin2hex(hex_key, KEY_HEX_BYTES, key, crypto_secretbox_KEYBYTES);

0 commit comments

Comments
 (0)