0

I wrote a simple implementation of ChaCha20 encryptor for files in C using MbedTLS implementation. The process of encryption is standard - I set key, then for each block of fixed size I generate new nonce (to prevent nonce reuse) and encrypt this block. Then I write nonce to a new file and then append block of encrypted data. So basically it's just a while loop of reading blocks from file and encrypting each of them with key and newly generated nonce.

Here are results of time command when I encrypt 1Gb file:

./main test.dat test.bin  2.67s user 1.04s system 97% cpu 3.803 total

I generate nonce for each block with this method:

mbedtls_ctr_drbg_random(&ctx->ctr_drbg, nonce, NONCE_SIZE);

Where ctx is my structure where I saved ctr_drbg and nonce is just an uint8_t nonce[12]. I am not building entropy each time, I built the generator only once and then just repeated the code above for each block.

Then I call this code:

mbedtls_chacha20_starts(&ctx->ctx, nonce, 0);
mbedtls_chacha20_update(&ctx->ctx, bytes_read, in_buffer, out_buffer);
  • in_buffer - block of data from file.
  • out_buffer - encrypted data for new file.
  • bytes_read - bytes counter.

And repeat the whole process until there is nothing left in the file.

Encryption and decryption works fine. However, my question is quite simple - is it okay speed for ChaCha20? Is there an option to speed it up? Does generating new nonce for every block slows down the execution? If you need my complete code, I might post it. My question is not about the code, it is about the speed, so I guess it's the right place to ask this.

Thanks in advance.

0 Answers0