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
Implement equal_const_time/2 as nif #2749
Closed
Closed
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* %CopyrightBegin% | ||
* | ||
* Copyright Ericsson AB 2010-2020. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* %CopyrightEnd% | ||
*/ | ||
|
||
#include "str.h" | ||
|
||
ERL_NIF_TERM do_equal_const_time(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) | ||
{ | ||
|
||
ErlNifBinary s1, s2; | ||
|
||
ASSERT(argc == 2); | ||
|
||
if (!enif_inspect_binary(env, argv[0], &s1)) | ||
goto bad_arg; | ||
if (!enif_inspect_binary(env, argv[1], &s2)) | ||
goto bad_arg; | ||
|
||
if (s1.size != s2.size) | ||
goto nomatch; | ||
|
||
if (CRYPTO_memcmp(s1.data, s2.data, s1.size) == 0) | ||
goto match; | ||
|
||
goto nomatch; | ||
|
||
match: | ||
return enif_make_atom(env,"true"); | ||
nomatch: | ||
return enif_make_atom(env,"false"); | ||
bad_arg: | ||
return enif_make_badarg(env); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* %CopyrightBegin% | ||
* | ||
* Copyright Ericsson AB 2010-2020. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* %CopyrightEnd% | ||
*/ | ||
|
||
|
||
#ifndef E_STR_H__ | ||
#define E_STR_H__ 1 | ||
|
||
#include "common.h" | ||
|
||
ERL_NIF_TERM do_equal_const_time(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); | ||
|
||
#endif /* E_STR_H__ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,6 +67,8 @@ | |
compute/1, | ||
compute_bug/0, | ||
compute_bug/1, | ||
equal_const_time/0, | ||
equal_const_time/1, | ||
exor/0, | ||
exor/1, | ||
generate/0, | ||
|
@@ -218,6 +220,7 @@ all() -> | |
{group, non_fips}, | ||
cipher_padding, | ||
mod_pow, | ||
equal_const_time, | ||
exor, | ||
rand_uniform, | ||
rand_threads, | ||
|
@@ -1228,6 +1231,24 @@ mod_pow() -> | |
mod_pow(Config) when is_list(Config) -> | ||
mod_pow_aux_test(2, 5, 10, 8). | ||
%%-------------------------------------------------------------------- | ||
equal_const_time() -> | ||
[{doc, "Test equal_const_time"}]. | ||
equal_const_time(Config) when is_list(Config) -> | ||
true = crypto:equal_const_time(<<"">>, <<"">>), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can do better tests surely, but will wait for approval. |
||
true = crypto:equal_const_time(<<"good">>, <<"good">>), | ||
|
||
false = crypto:equal_const_time(<<"good">>, <<"good1">>), | ||
false = crypto:equal_const_time(<<"good">>, <<"bad">>), | ||
false = crypto:equal_const_time(<<"eh?">>, <<"abcdefg">>), | ||
|
||
true = crypto:equal_const_time("", ""), | ||
true = crypto:equal_const_time("good", "good"), | ||
|
||
false = crypto:equal_const_time("good", "bad"), | ||
false = crypto:equal_const_time("good", "bad1"), | ||
false = crypto:equal_const_time("eh?", "abcdefg"), | ||
ok. | ||
%%-------------------------------------------------------------------- | ||
exor() -> | ||
[{doc, "Test the exor function"}]. | ||
exor(Config) when is_list(Config) -> | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is where the erlang list implementation and the nif implementation differ currently (i.e., the length of the strings isn't checked explicitly in the erlang implementation and probably for good reason), but the behavior remains the same for both.