-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
crypto:cmac calculating the Cipher-based Message Authentication Code (ERL-82) #1138
Conversation
The ERL-82 issue requests a way to calculate a CMAC in Erlang. The AES128 CMAC is standartized in RFC 4493 and used e.g. for message authentication in the LoRaWAN networks. The CMAC is implemented by OpenSSL since v1.0.1, but as @IngelaAndin stated in response to the ERL-82, the current crypto implementation does not include functions that call those OpenSSL cryptolib functions. This commit introduces a new function `crypto:cmac` that calls the corresponding OpenSSL functions and calculates the CMAC. Only the cmac_nif is implemented. The incremental functions (init, update, final) are not provided because the current OpenSSL does not allow custom memory allocators like `enif_alloc_resource`. The Erlang user guide states that at least OpenSSL 0.9.8 is required, so I added few #ifdefs so the code is compatible with all versions. However, the OpenSSL pages say that the pre-1.0.1 versions (0.9.8 and 1.0.0) are no longer maintained. Even the 1.0.1 will be retired by Dec 2016. Hence I believe that adding a 1.0.1-only function like CMAC should be OK.
The summary line of the commit message is too long and/or ends with a "." Bad message: Fix building crypto/cmac_nif on 64-bit machines. I am a script, I am not human |
Now running in our daily tests. In a week we have enough people back from vacation to thoroughly review this... |
As I understand it you should be able to use the EVP API https://wiki.openssl.org/index.php/EVP_Signing_and_Verifying And we have an ambition to use the EVP API as much as possible. |
Well, the openssl/cmac.c implementation used in my code is just a wrapper around the EVP API. Calculation of CMAC using EVP is not trivial and therefore OpenSSL created these convenience functions. Calling EVP API directly (instead of using the cmac.c wrapper) just means re-implementing the cmac.c wrapper inside the OTP crypto. I think you may not want that either. |
Ok, thank you for the explenation then that should be fine :) |
It would be nice if you could add a reference to cmac RFC in the description part of the man page. |
Done. Thank you for looking onto this. :) |
Test case should be skipped on older OpenSSL and not fail with |
Good point. Done. |
The summary line of the commit message is too long and/or ends with a "." Bad message: Added a reference to cmac RFC in the description part of the man page I am a script, I am not human |
Here is a pur Erlang AES-CMAC implementation: https://gist.github.com/RoadRunnr/fc1cdba514030c0c7f6529ec6222989b That one could be used as fall back on older OpenSSL versions or - if performance is not an issue - instead of the native version. |
Thank you for the offer of fall back, however we do not want to open up for Erlang implementations of |
Merged to 'master'. |
@RaimoNiskanen, it appears unmerged. was it your intention, or not? |
Nope. I pushed the 'master' branch to the wrong repository, so here it was unmerged, but not anymore... |
The ERL-82 issue requests a way to calculate a CMAC in Erlang. The AES128 CMAC is standartized in RFC 4493 and used e.g. for message authentication in the LoRaWAN networks.
The CMAC is implemented by OpenSSL since v1.0.1, but as @IngelaAndin stated in response to the ERL-82, the current crypto implementation does not include functions that call those OpenSSL cryptolib functions.
This commit introduces a new function
crypto:cmac
that calls the corresponding OpenSSL functions and calculates the CMAC.Only the cmac_nif is implemented. The incremental functions (init, update, final) are not provided because the current OpenSSL does not allow custom memory allocators like
enif_alloc_resource
.The
crypto:cmac/4
is implemented for convenience and to keep the API similar to thecrypto:hmac/3
andcrypto:hmac/4
.The Erlang user guide states that at least OpenSSL 0.9.8 is required, so I added few #ifdefs so the code is compatible with all versions. However, the OpenSSL pages say that the pre-1.0.1 versions (0.9.8 and
1.0.0) are no longer maintained. Even the 1.0.1 will be retired by Dec 2016. Hence I believe that adding a 1.0.1-only function like CMAC should be OK.