Closed zhenkyle closed 7 years ago
That OpenSSL::Cipher expects 16 bytes long IV is not a bug. For RFC 7539 compliant operation, prepend the initial counter to the nonce:
...
chacha20_nonce = "000000000000004a00000000" # assuming 9 in your code is a typo
chacha20_initial_counter ="01000000"
...
initial_counter = hex2bin(chacha20_initial_counter)
...
cipher.iv = initial_counter + nonce
...
And I thought it would work, ... but it didn't because of a bug in the OpenSSL library. I will report to the OpenSSL project.
diff --git a/crypto/evp/e_chacha20_poly1305.c b/crypto/evp/e_chacha20_poly1305.c
index 952bd3fca781..befd805e35a5 100644
--- a/crypto/evp/e_chacha20_poly1305.c
+++ b/crypto/evp/e_chacha20_poly1305.c
@@ -127,7 +127,7 @@ static const EVP_CIPHER chacha20 = {
1, /* block_size */
CHACHA_KEY_SIZE, /* key_len */
CHACHA_CTR_SIZE, /* iv_len, 128-bit counter in the context */
- 0, /* flags */
+ EVP_CIPH_CUSTOM_IV | EVP_CIPH_ALWAYS_CALL_INIT,
chacha_init_key,
chacha_cipher,
NULL,
The chacha20_nonce
in my code is a typo, and after applying your patch to OpenSSL library my test did passed.
Thank you for point out the initial_counter's use case for me, you find and solve the real problem really quick.
Please do report the issuse to OpenSSL project.
FYI, the patch has been merged (https://github.com/openssl/openssl/pull/2156) and the next 1.1.0 release will fix the issue.
Got it, thanks.
My environment is
ruby 2.3.3p222 (2016-11-21 revision 56859) [x86_64-linux]
OpenSSL 1.1.0c 10 Nov 2016
openssl (2.0.2)
I got this error
running my little test script
That is because
OpenSSL::Cipher.new("chacha20").iv_len = 16
, while I'm trying to pass in an iv with 12-bytes.Accrodding to
rfc7539
,iv_len
ofchacha20
should be 96-bit, that is 12-bytes.After all
OpenSSL::Cipher.new("chacha20-poly1305").iv_len
equals to12
So I think it's a bug.