I’m working on upgrading Elixir and Erlang for a potential issue and I need to rewrite some code that uses the crypto
module.
I thought I should be able to call the new functions like this:
:crypto.crypto_one_time(:aes_ctr, key, iv, data, encrypt: false)
But I’m seeing an error like this when I call the function with the above code:
** (ErlangError) Erlang error: {:badarg, {'api_ng.c', 141}, 'Unknown cipher'}
(crypto 4.8.3) :crypto.ng_crypto_one_time_nif(:aes_ctr, <<9, 130, 176, 54, 178, 62, 33, 59, 142, 45, 42, 47, 12, 217, 143, 221, 41, 191, 194, 21, 245, 247, 80, 88, 206, 73, 133, 90, 164, 25, 54, 10>>, <<58, 134, 203, 139, 138, 182, 47, 173, 47, 192, 203, 97, 113, 39, 21, 2>>, "This is a test message", true, :undefined)
The docs for the crypto
module:
It sure looks like aes_ctr
(equivalent to :aes_ctr
in Elixir) is one of the specific literal values that type can have.
But the formatting – for the aes_ctr
value specifically, and only that value – is slightly different. Does that different formatting mean something relevant to this?
I did find this GitHub issue, and several related ones in the same project:
Their fix seems to do something like this with the new functions:
key_length = bit_size(key)
cipher = "aes_#{key_length}_ctr" |> String.to_atom()
:crypto.crypto_one_time( cipher, key, iv, data, encrypt: false )
I’m more curious about why the cipher_iv
type in the crypto
docs seem to indicate that :aes_ctr
is or should be a perfectly valid value.