|
| 1 | +/* |
| 2 | + * Copyright 2015 |
| 3 | + * Daniel Dinu, Dmitry Khovratovich, Jean-Philippe Aumasson, and Samuel Neves |
| 4 | + * |
| 5 | + * You may use this work under the terms of a Creative Commons CC0 1.0 |
| 6 | + * License/Waiver or the Apache Public License 2.0, at your option. The terms of |
| 7 | + * these licenses can be found at: |
| 8 | + * |
| 9 | + * - CC0 1.0 Universal : https://creativecommons.org/publicdomain/zero/1.0 |
| 10 | + * - Apache 2.0 : https://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * You should have received a copy of both of these licenses along with this |
| 13 | + * software. If not, they may be obtained at the above URLs. |
| 14 | + */ |
| 15 | + |
| 16 | +#include <argon2.h> |
| 17 | + |
| 18 | +#include "alg-argon2-encoding.h" |
| 19 | + |
| 20 | +/* |
| 21 | + * Some macros for constant-time comparisons. These work over values in |
| 22 | + * the 0..255 range. Returned value is 0x00 on "false", 0xFF on "true". |
| 23 | + */ |
| 24 | +#define EQ(x, y) ((((0U - ((unsigned)(x) ^ (unsigned)(y))) >> 8) & 0xFF) ^ 0xFF) |
| 25 | +#define GT(x, y) ((((unsigned)(y) - (unsigned)(x)) >> 8) & 0xFF) |
| 26 | +#define GE(x, y) (GT(y, x) ^ 0xFF) |
| 27 | +#define LT(x, y) GT(y, x) |
| 28 | +#define LE(x, y) GE(y, x) |
| 29 | + |
| 30 | +/* |
| 31 | + * Convert value x (0..63) to corresponding Base64 character. |
| 32 | + */ |
| 33 | +static unsigned b64_byte_to_char(unsigned x) { |
| 34 | + return (LT(x, 26) & (x + 'A')) | |
| 35 | + (GE(x, 26) & LT(x, 52) & (x + (unsigned)('a' - 26))) | |
| 36 | + (GE(x, 52) & LT(x, 62) & (x + (unsigned)('0' - 52))) | (EQ(x, 62) & '+') | |
| 37 | + (EQ(x, 63) & '/'); |
| 38 | +} |
| 39 | + |
| 40 | +/* |
| 41 | + * Convert character c to the corresponding 6-bit value. If character c |
| 42 | + * is not a Base64 character, then 0xFF (255) is returned. |
| 43 | + */ |
| 44 | +static unsigned b64_char_to_byte(int c) { |
| 45 | + unsigned x; |
| 46 | + |
| 47 | + x = (GE(c, 'A') & LE(c, 'Z') & (unsigned)(c - 'A')) | |
| 48 | + (GE(c, 'a') & LE(c, 'z') & (unsigned)(c - ('a' - 26))) | |
| 49 | + (GE(c, '0') & LE(c, '9') & (unsigned)(c - ('0' - 52))) | (EQ(c, '+') & 62) | |
| 50 | + (EQ(c, '/') & 63); |
| 51 | + return x | (EQ(x, 0) & (EQ(c, 'A') ^ 0xFF)); |
| 52 | +} |
| 53 | + |
| 54 | +/* |
| 55 | + * Convert some bytes to Base64. 'dst_len' is the length (in characters) |
| 56 | + * of the output buffer 'dst'; if that buffer is not large enough to |
| 57 | + * receive the result (including the terminating 0), then (size_t)-1 |
| 58 | + * is returned. Otherwise, the zero-terminated Base64 string is written |
| 59 | + * in the buffer, and the output length (counted WITHOUT the terminating |
| 60 | + * zero) is returned. |
| 61 | + */ |
| 62 | +size_t argon2_encode64(char *dst, size_t dst_len, const uint8_t *src, |
| 63 | + size_t src_len) { |
| 64 | + size_t olen; |
| 65 | + const unsigned char *buf; |
| 66 | + unsigned acc, acc_len; |
| 67 | + |
| 68 | + olen = (src_len / 3) << 2; |
| 69 | + switch (src_len % 3) { |
| 70 | + case 2: |
| 71 | + olen++; |
| 72 | + /* fall through */ |
| 73 | + case 1: |
| 74 | + olen += 2; |
| 75 | + break; |
| 76 | + } |
| 77 | + if (dst_len <= olen) { |
| 78 | + return (size_t)-1; |
| 79 | + } |
| 80 | + acc = 0; |
| 81 | + acc_len = 0; |
| 82 | + buf = (const unsigned char *)src; |
| 83 | + while (src_len-- > 0) { |
| 84 | + acc = (acc << 8) + (*buf++); |
| 85 | + acc_len += 8; |
| 86 | + while (acc_len >= 6) { |
| 87 | + acc_len -= 6; |
| 88 | + *dst++ = (char)b64_byte_to_char((acc >> acc_len) & 0x3F); |
| 89 | + } |
| 90 | + } |
| 91 | + if (acc_len > 0) { |
| 92 | + *dst++ = (char)b64_byte_to_char((acc << (6 - acc_len)) & 0x3F); |
| 93 | + } |
| 94 | + *dst++ = 0; |
| 95 | + return olen; |
| 96 | +} |
| 97 | + |
| 98 | +/* |
| 99 | + * Decode Base64 chars into bytes. The '*dst_len' value must initially |
| 100 | + * contain the length of the output buffer '*dst'; when the decoding |
| 101 | + * ends, the actual number of decoded bytes is written back in |
| 102 | + * '*dst_len'. |
| 103 | + * |
| 104 | + * Decoding stops when a non-Base64 character is encountered, or when |
| 105 | + * the output buffer capacity is exceeded. If an error occurred (output |
| 106 | + * buffer is too small, invalid last characters leading to unprocessed |
| 107 | + * buffered bits), then NULL is returned; otherwise, the returned value |
| 108 | + * points to the first non-Base64 character in the source stream, which |
| 109 | + * may be the terminating zero. |
| 110 | + */ |
| 111 | +const char *argon2_decode64(uint8_t *dst, size_t *dst_len, const char *src) { |
| 112 | + size_t len; |
| 113 | + unsigned char *buf; |
| 114 | + unsigned acc, acc_len; |
| 115 | + |
| 116 | + buf = (unsigned char *)dst; |
| 117 | + len = 0; |
| 118 | + acc = 0; |
| 119 | + acc_len = 0; |
| 120 | + for (;;) { |
| 121 | + unsigned d; |
| 122 | + |
| 123 | + d = b64_char_to_byte(*src); |
| 124 | + if (d == 0xFF) { |
| 125 | + break; |
| 126 | + } |
| 127 | + src++; |
| 128 | + acc = (acc << 6) + d; |
| 129 | + acc_len += 6; |
| 130 | + if (acc_len >= 8) { |
| 131 | + acc_len -= 8; |
| 132 | + if ((len++) >= *dst_len) { |
| 133 | + return NULL; |
| 134 | + } |
| 135 | + *buf++ = (acc >> acc_len) & 0xFF; |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + /* |
| 140 | + * If the input length is equal to 1 modulo 4 (which is |
| 141 | + * invalid), then there will remain 6 unprocessed bits; |
| 142 | + * otherwise, only 0, 2 or 4 bits are buffered. The buffered |
| 143 | + * bits must also all be zero. |
| 144 | + */ |
| 145 | + if (acc_len > 4 || (acc & (((unsigned)1 << acc_len) - 1)) != 0) { |
| 146 | + return NULL; |
| 147 | + } |
| 148 | + *dst_len = len; |
| 149 | + return src; |
| 150 | +} |
| 151 | + |
0 commit comments