-
Notifications
You must be signed in to change notification settings - Fork 66
[RFC] Add argon2 backend. #113
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
Open
ferivoz
wants to merge
2
commits into
besser82:develop
Choose a base branch
from
ferivoz:argon2
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,151 @@ | ||
| /* | ||
| * Copyright 2015 | ||
| * Daniel Dinu, Dmitry Khovratovich, Jean-Philippe Aumasson, and Samuel Neves | ||
| * | ||
| * You may use this work under the terms of a Creative Commons CC0 1.0 | ||
| * License/Waiver or the Apache Public License 2.0, at your option. The terms of | ||
| * these licenses can be found at: | ||
| * | ||
| * - CC0 1.0 Universal : https://creativecommons.org/publicdomain/zero/1.0 | ||
| * - Apache 2.0 : https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * You should have received a copy of both of these licenses along with this | ||
| * software. If not, they may be obtained at the above URLs. | ||
| */ | ||
|
|
||
| #include <argon2.h> | ||
|
|
||
| #include "alg-argon2-encoding.h" | ||
|
|
||
| /* | ||
| * Some macros for constant-time comparisons. These work over values in | ||
| * the 0..255 range. Returned value is 0x00 on "false", 0xFF on "true". | ||
| */ | ||
| #define EQ(x, y) ((((0U - ((unsigned)(x) ^ (unsigned)(y))) >> 8) & 0xFF) ^ 0xFF) | ||
| #define GT(x, y) ((((unsigned)(y) - (unsigned)(x)) >> 8) & 0xFF) | ||
| #define GE(x, y) (GT(y, x) ^ 0xFF) | ||
| #define LT(x, y) GT(y, x) | ||
| #define LE(x, y) GE(y, x) | ||
|
|
||
| /* | ||
| * Convert value x (0..63) to corresponding Base64 character. | ||
| */ | ||
| static unsigned b64_byte_to_char(unsigned x) { | ||
| return (LT(x, 26) & (x + 'A')) | | ||
| (GE(x, 26) & LT(x, 52) & (x + (unsigned)('a' - 26))) | | ||
| (GE(x, 52) & LT(x, 62) & (x + (unsigned)('0' - 52))) | (EQ(x, 62) & '+') | | ||
| (EQ(x, 63) & '/'); | ||
| } | ||
|
|
||
| /* | ||
| * Convert character c to the corresponding 6-bit value. If character c | ||
| * is not a Base64 character, then 0xFF (255) is returned. | ||
| */ | ||
| static unsigned b64_char_to_byte(int c) { | ||
| unsigned x; | ||
|
|
||
| x = (GE(c, 'A') & LE(c, 'Z') & (unsigned)(c - 'A')) | | ||
| (GE(c, 'a') & LE(c, 'z') & (unsigned)(c - ('a' - 26))) | | ||
| (GE(c, '0') & LE(c, '9') & (unsigned)(c - ('0' - 52))) | (EQ(c, '+') & 62) | | ||
| (EQ(c, '/') & 63); | ||
| return x | (EQ(x, 0) & (EQ(c, 'A') ^ 0xFF)); | ||
| } | ||
|
|
||
| /* | ||
| * Convert some bytes to Base64. 'dst_len' is the length (in characters) | ||
| * of the output buffer 'dst'; if that buffer is not large enough to | ||
| * receive the result (including the terminating 0), then (size_t)-1 | ||
| * is returned. Otherwise, the zero-terminated Base64 string is written | ||
| * in the buffer, and the output length (counted WITHOUT the terminating | ||
| * zero) is returned. | ||
| */ | ||
| size_t argon2_encode64(char *dst, size_t dst_len, const uint8_t *src, | ||
| size_t src_len) { | ||
| size_t olen; | ||
| const unsigned char *buf; | ||
| unsigned acc, acc_len; | ||
|
|
||
| olen = (src_len / 3) << 2; | ||
| switch (src_len % 3) { | ||
| case 2: | ||
| olen++; | ||
| /* fall through */ | ||
| case 1: | ||
| olen += 2; | ||
| break; | ||
| } | ||
| if (dst_len <= olen) { | ||
| return (size_t)-1; | ||
| } | ||
| acc = 0; | ||
| acc_len = 0; | ||
| buf = (const unsigned char *)src; | ||
| while (src_len-- > 0) { | ||
| acc = (acc << 8) + (*buf++); | ||
| acc_len += 8; | ||
| while (acc_len >= 6) { | ||
| acc_len -= 6; | ||
| *dst++ = (char)b64_byte_to_char((acc >> acc_len) & 0x3F); | ||
| } | ||
| } | ||
| if (acc_len > 0) { | ||
| *dst++ = (char)b64_byte_to_char((acc << (6 - acc_len)) & 0x3F); | ||
| } | ||
| *dst++ = 0; | ||
| return olen; | ||
| } | ||
|
|
||
| /* | ||
| * Decode Base64 chars into bytes. The '*dst_len' value must initially | ||
| * contain the length of the output buffer '*dst'; when the decoding | ||
| * ends, the actual number of decoded bytes is written back in | ||
| * '*dst_len'. | ||
| * | ||
| * Decoding stops when a non-Base64 character is encountered, or when | ||
| * the output buffer capacity is exceeded. If an error occurred (output | ||
| * buffer is too small, invalid last characters leading to unprocessed | ||
| * buffered bits), then NULL is returned; otherwise, the returned value | ||
| * points to the first non-Base64 character in the source stream, which | ||
| * may be the terminating zero. | ||
| */ | ||
| const char *argon2_decode64(uint8_t *dst, size_t *dst_len, const char *src) { | ||
| size_t len; | ||
| unsigned char *buf; | ||
| unsigned acc, acc_len; | ||
|
|
||
| buf = (unsigned char *)dst; | ||
| len = 0; | ||
| acc = 0; | ||
| acc_len = 0; | ||
| for (;;) { | ||
| unsigned d; | ||
|
|
||
| d = b64_char_to_byte(*src); | ||
| if (d == 0xFF) { | ||
| break; | ||
| } | ||
| src++; | ||
| acc = (acc << 6) + d; | ||
| acc_len += 6; | ||
| if (acc_len >= 8) { | ||
| acc_len -= 8; | ||
| if ((len++) >= *dst_len) { | ||
| return NULL; | ||
| } | ||
| *buf++ = (acc >> acc_len) & 0xFF; | ||
| } | ||
| } | ||
|
|
||
| /* | ||
| * If the input length is equal to 1 modulo 4 (which is | ||
| * invalid), then there will remain 6 unprocessed bits; | ||
| * otherwise, only 0, 2 or 4 bits are buffered. The buffered | ||
| * bits must also all be zero. | ||
| */ | ||
| if (acc_len > 4 || (acc & (((unsigned)1 << acc_len) - 1)) != 0) { | ||
| return NULL; | ||
| } | ||
| *dst_len = len; | ||
| return src; | ||
| } | ||
|
|
||
This file contains hidden or 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,25 @@ | ||
| /* | ||
| * Copyright 2015 | ||
| * Daniel Dinu, Dmitry Khovratovich, Jean-Philippe Aumasson, and Samuel Neves | ||
| * | ||
| * You may use this work under the terms of a Creative Commons CC0 1.0 | ||
| * License/Waiver or the Apache Public License 2.0, at your option. The terms of | ||
| * these licenses can be found at: | ||
| * | ||
| * - CC0 1.0 Universal : https://creativecommons.org/publicdomain/zero/1.0 | ||
| * - Apache 2.0 : https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * You should have received a copy of both of these licenses along with this | ||
| * software. If not, they may be obtained at the above URLs. | ||
| */ | ||
|
|
||
| #ifndef ALG_ARGON2_ENCODING_H | ||
| #define ALG_ARGON2_ENCODING_H | ||
|
|
||
| #include <stdint.h> | ||
|
|
||
| size_t argon2_encode64 (char *dst, size_t dst_len, const uint8_t *src, | ||
| size_t src_len); | ||
| const char *argon2_decode64 (uint8_t *dst, size_t *dst_len, const char *src); | ||
|
|
||
| #endif |
Oops, something went wrong.
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.
Could you please explain why these conversion functions need to use constant-time comparisons?
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.
In an earlier version of my code I have used a lookup table just like ascii64 in crypto-common.c. Eventually I gave up that code for these reasons:
I would not mind adjusting it if it is requested. Especially since a lookup table is easier to review and complies with rest of the code base.