Skip to content
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

Closed
wants to merge 4 commits into from
Closed

crypto:cmac calculating the Cipher-based Message Authentication Code (ERL-82) #1138

wants to merge 4 commits into from

Conversation

gotthardp
Copy link
Contributor

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 the crypto:hmac/3 and crypto: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.

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.
@gotthardp gotthardp changed the title crypto:cmac calculating the Cipher-based Message Authentication Code crypto:cmac calculating the Cipher-based Message Authentication Code (ERL-82) Jul 30, 2016
@OTP-Maintainer
Copy link

The summary line of the commit message is too long and/or ends with a "."
Make sure the whole message follows the guidelines here: https://github.com/erlang/otp/wiki/Writing-good-commit-messages.

Bad message: Fix building crypto/cmac_nif on 64-bit machines.
crypto:cmac calculating the Cipher-based Message Authentication Code


I am a script, I am not human


@psyeugenic psyeugenic added team:PS Assigned to OTP team PS feature labels Aug 1, 2016
@RaimoNiskanen RaimoNiskanen added testing currently being tested, tag is used by OTP internal CI waiting waiting for changes/input from author labels Aug 1, 2016
@RaimoNiskanen
Copy link
Contributor

Now running in our daily tests.

In a week we have enough people back from vacation to thoroughly review this...

@IngelaAndin
Copy link
Contributor

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.

@gotthardp
Copy link
Contributor Author

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.

@IngelaAndin
Copy link
Contributor

Ok, thank you for the explenation then that should be fine :)

@IngelaAndin
Copy link
Contributor

It would be nice if you could add a reference to cmac RFC in the description part of the man page.

@gotthardp
Copy link
Contributor Author

Done. Thank you for looking onto this. :)

@sverker
Copy link
Contributor

sverker commented Aug 12, 2016

Test case should be skipped on older OpenSSL and not fail with notsup.

@gotthardp
Copy link
Contributor Author

Good point. Done.

@OTP-Maintainer
Copy link

The summary line of the commit message is too long and/or ends with a "."
Make sure the whole message follows the guidelines here: https://github.com/erlang/otp/wiki/Writing-good-commit-messages.

Bad message: Added a reference to cmac RFC in the description part of the man page
Fix building crypto/cmac_nif on 64-bit machines.
crypto:cmac calculating the Cipher-based Message Authentication Code


I am a script, I am not human


@RoadRunnr
Copy link
Contributor

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.

@IngelaAndin
Copy link
Contributor

Thank you for the offer of fall back, however we do not want to open up for Erlang implementations of
of cryptographic functions with all responsibility of testing and making sure they are correct and safe to use. We think it is acceptable that it will not work with older versions of OpenSSL. We are ready to accept this PR now.

@IngelaAndin IngelaAndin removed the waiting waiting for changes/input from author label Aug 30, 2016
@RaimoNiskanen RaimoNiskanen removed the testing currently being tested, tag is used by OTP internal CI label Aug 31, 2016
@RaimoNiskanen
Copy link
Contributor

Merged to 'master'.

@gotthardp
Copy link
Contributor Author

@RaimoNiskanen, it appears unmerged. was it your intention, or not?

@RaimoNiskanen
Copy link
Contributor

RaimoNiskanen commented Sep 1, 2016

Nope. I pushed the 'master' branch to the wrong repository, so here it was unmerged, but not anymore...

@gotthardp gotthardp deleted the otp-crypto-cmac/ERL-82 branch September 1, 2016 06:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature team:PS Assigned to OTP team PS
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants