wg: constanter time encoding
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
parent
206e8f08e2
commit
2bca99893f
|
@ -1559,25 +1559,25 @@ int wg_key_from_base64(wg_key key, const wg_key_b64_string base64)
|
||||||
{
|
{
|
||||||
unsigned int i;
|
unsigned int i;
|
||||||
int val;
|
int val;
|
||||||
|
volatile uint8_t ret = 0;
|
||||||
|
|
||||||
errno = EINVAL;
|
if (strlen(base64) != sizeof(wg_key_b64_string) - 1 || base64[sizeof(wg_key_b64_string) - 2] != '=') {
|
||||||
if (strlen(base64) != sizeof(wg_key_b64_string) - 1 || base64[sizeof(wg_key_b64_string) - 2] != '=')
|
errno = EINVAL;
|
||||||
goto out;
|
goto out;
|
||||||
|
}
|
||||||
|
|
||||||
for (i = 0; i < 32 / 3; ++i) {
|
for (i = 0; i < 32 / 3; ++i) {
|
||||||
val = decode_base64(&base64[i * 4]);
|
val = decode_base64(&base64[i * 4]);
|
||||||
if (val < 0)
|
ret |= (uint32_t)val >> 31;
|
||||||
goto out;
|
|
||||||
key[i * 3 + 0] = (val >> 16) & 0xff;
|
key[i * 3 + 0] = (val >> 16) & 0xff;
|
||||||
key[i * 3 + 1] = (val >> 8) & 0xff;
|
key[i * 3 + 1] = (val >> 8) & 0xff;
|
||||||
key[i * 3 + 2] = val & 0xff;
|
key[i * 3 + 2] = val & 0xff;
|
||||||
}
|
}
|
||||||
val = decode_base64((const char[]){ base64[i * 4 + 0], base64[i * 4 + 1], base64[i * 4 + 2], 'A' });
|
val = decode_base64((const char[]){ base64[i * 4 + 0], base64[i * 4 + 1], base64[i * 4 + 2], 'A' });
|
||||||
if (val < 0 || val & 0xff)
|
ret |= ((uint32_t)val >> 31) | (val & 0xff);
|
||||||
goto out;
|
|
||||||
key[i * 3 + 0] = (val >> 16) & 0xff;
|
key[i * 3 + 0] = (val >> 16) & 0xff;
|
||||||
key[i * 3 + 1] = (val >> 8) & 0xff;
|
key[i * 3 + 1] = (val >> 8) & 0xff;
|
||||||
errno = 0;
|
errno = EINVAL & ~((ret - 1) >> 8);
|
||||||
out:
|
out:
|
||||||
return -errno;
|
return -errno;
|
||||||
}
|
}
|
||||||
|
|
|
@ -50,6 +50,7 @@ static inline int decode_base64(const char src[4])
|
||||||
bool key_from_base64(uint8_t key[static WG_KEY_LEN], const char *base64)
|
bool key_from_base64(uint8_t key[static WG_KEY_LEN], const char *base64)
|
||||||
{
|
{
|
||||||
unsigned int i;
|
unsigned int i;
|
||||||
|
volatile uint8_t ret = 0;
|
||||||
int val;
|
int val;
|
||||||
|
|
||||||
if (strlen(base64) != WG_KEY_LEN_BASE64 - 1 || base64[WG_KEY_LEN_BASE64 - 2] != '=')
|
if (strlen(base64) != WG_KEY_LEN_BASE64 - 1 || base64[WG_KEY_LEN_BASE64 - 2] != '=')
|
||||||
|
@ -57,18 +58,17 @@ bool key_from_base64(uint8_t key[static WG_KEY_LEN], const char *base64)
|
||||||
|
|
||||||
for (i = 0; i < WG_KEY_LEN / 3; ++i) {
|
for (i = 0; i < WG_KEY_LEN / 3; ++i) {
|
||||||
val = decode_base64(&base64[i * 4]);
|
val = decode_base64(&base64[i * 4]);
|
||||||
if (val < 0)
|
ret |= (uint32_t)val >> 31;
|
||||||
return false;
|
|
||||||
key[i * 3 + 0] = (val >> 16) & 0xff;
|
key[i * 3 + 0] = (val >> 16) & 0xff;
|
||||||
key[i * 3 + 1] = (val >> 8) & 0xff;
|
key[i * 3 + 1] = (val >> 8) & 0xff;
|
||||||
key[i * 3 + 2] = val & 0xff;
|
key[i * 3 + 2] = val & 0xff;
|
||||||
}
|
}
|
||||||
val = decode_base64((const char[]){ base64[i * 4 + 0], base64[i * 4 + 1], base64[i * 4 + 2], 'A' });
|
val = decode_base64((const char[]){ base64[i * 4 + 0], base64[i * 4 + 1], base64[i * 4 + 2], 'A' });
|
||||||
if (val < 0 || val & 0xff)
|
ret |= ((uint32_t)val >> 31) | (val & 0xff);
|
||||||
return false;
|
|
||||||
key[i * 3 + 0] = (val >> 16) & 0xff;
|
key[i * 3 + 0] = (val >> 16) & 0xff;
|
||||||
key[i * 3 + 1] = (val >> 8) & 0xff;
|
key[i * 3 + 1] = (val >> 8) & 0xff;
|
||||||
return true;
|
|
||||||
|
return 1 & ((ret - 1) >> 8);
|
||||||
}
|
}
|
||||||
|
|
||||||
void key_to_hex(char hex[static WG_KEY_LEN_HEX], const uint8_t key[static WG_KEY_LEN])
|
void key_to_hex(char hex[static WG_KEY_LEN_HEX], const uint8_t key[static WG_KEY_LEN])
|
||||||
|
@ -84,27 +84,33 @@ void key_to_hex(char hex[static WG_KEY_LEN_HEX], const uint8_t key[static WG_KEY
|
||||||
|
|
||||||
bool key_from_hex(uint8_t key[static WG_KEY_LEN], const char *hex)
|
bool key_from_hex(uint8_t key[static WG_KEY_LEN], const char *hex)
|
||||||
{
|
{
|
||||||
uint8_t c, c_acc = 0, c_alpha0, c_alpha, c_num0, c_num, c_val, state = 0;
|
uint8_t c, c_acc = 0, c_alpha0, c_alpha, c_num0, c_num, c_val;
|
||||||
|
volatile uint8_t ret = 0;
|
||||||
|
|
||||||
if (strlen(hex) != WG_KEY_LEN_HEX - 1)
|
if (strlen(hex) != WG_KEY_LEN_HEX - 1)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
for (unsigned int i = 0; i < WG_KEY_LEN_HEX - 1; ++i) {
|
for (unsigned int i = 0; i < WG_KEY_LEN_HEX - 1; i += 2) {
|
||||||
c = (uint8_t)hex[i];
|
c = (uint8_t)hex[i];
|
||||||
c_num = c ^ 48U;
|
c_num = c ^ 48U;
|
||||||
c_num0 = (c_num - 10U) >> 8;
|
c_num0 = (c_num - 10U) >> 8;
|
||||||
c_alpha = (c & ~32U) - 55U;
|
c_alpha = (c & ~32U) - 55U;
|
||||||
c_alpha0 = ((c_alpha - 10U) ^ (c_alpha - 16U)) >> 8;
|
c_alpha0 = ((c_alpha - 10U) ^ (c_alpha - 16U)) >> 8;
|
||||||
if (!(c_num0 | c_alpha0))
|
ret |= ((c_num0 | c_alpha0) - 1) >> 8;
|
||||||
return false;
|
|
||||||
c_val = (c_num0 & c_num) | (c_alpha0 & c_alpha);
|
c_val = (c_num0 & c_num) | (c_alpha0 & c_alpha);
|
||||||
if (!state)
|
c_acc = c_val * 16U;
|
||||||
c_acc = c_val * 16U;
|
|
||||||
else
|
c = (uint8_t)hex[i + 1];
|
||||||
key[i / 2] = c_acc | c_val;
|
c_num = c ^ 48U;
|
||||||
state = ~state;
|
c_num0 = (c_num - 10U) >> 8;
|
||||||
|
c_alpha = (c & ~32U) - 55U;
|
||||||
|
c_alpha0 = ((c_alpha - 10U) ^ (c_alpha - 16U)) >> 8;
|
||||||
|
ret |= ((c_num0 | c_alpha0) - 1) >> 8;
|
||||||
|
c_val = (c_num0 & c_num) | (c_alpha0 & c_alpha);
|
||||||
|
key[i / 2] = c_acc | c_val;
|
||||||
}
|
}
|
||||||
return true;
|
|
||||||
|
return 1 & ((ret - 1) >> 8);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool key_is_zero(const uint8_t key[static WG_KEY_LEN])
|
bool key_is_zero(const uint8_t key[static WG_KEY_LEN])
|
||||||
|
|
Loading…
Reference in a new issue