2018-09-20 01:49:47 +02:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
/*
|
2020-01-02 19:52:25 +01:00
|
|
|
* Copyright (C) 2015-2020 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
|
2015-06-05 15:58:00 +02:00
|
|
|
*
|
2017-05-15 23:24:48 +02:00
|
|
|
* This is a specialized constant-time base64/hex implementation that resists side-channel attacks.
|
2015-06-05 15:58:00 +02:00
|
|
|
*/
|
|
|
|
|
2017-04-16 01:20:43 +02:00
|
|
|
#include <string.h>
|
2017-05-15 23:24:48 +02:00
|
|
|
#include "encoding.h"
|
2015-06-05 15:58:00 +02:00
|
|
|
|
2018-06-06 00:05:47 +02:00
|
|
|
static inline void encode_base64(char dest[static 4], const uint8_t src[static 3])
|
2015-06-05 15:58:00 +02:00
|
|
|
{
|
2017-04-16 01:20:43 +02:00
|
|
|
const uint8_t input[] = { (src[0] >> 2) & 63, ((src[0] << 4) | (src[1] >> 4)) & 63, ((src[1] << 2) | (src[2] >> 6)) & 63, src[2] & 63 };
|
2017-10-25 17:56:08 +02:00
|
|
|
|
2017-04-16 01:20:43 +02:00
|
|
|
for (unsigned int i = 0; i < 4; ++i)
|
|
|
|
dest[i] = input[i] + 'A'
|
|
|
|
+ (((25 - input[i]) >> 8) & 6)
|
|
|
|
- (((51 - input[i]) >> 8) & 75)
|
|
|
|
- (((61 - input[i]) >> 8) & 15)
|
|
|
|
+ (((62 - input[i]) >> 8) & 3);
|
2015-06-05 15:58:00 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-04-16 01:20:43 +02:00
|
|
|
void key_to_base64(char base64[static WG_KEY_LEN_BASE64], const uint8_t key[static WG_KEY_LEN])
|
2015-06-05 15:58:00 +02:00
|
|
|
{
|
2017-04-16 01:20:43 +02:00
|
|
|
unsigned int i;
|
2017-10-25 17:56:08 +02:00
|
|
|
|
2017-04-16 01:20:43 +02:00
|
|
|
for (i = 0; i < WG_KEY_LEN / 3; ++i)
|
2017-05-15 23:24:48 +02:00
|
|
|
encode_base64(&base64[i * 4], &key[i * 3]);
|
|
|
|
encode_base64(&base64[i * 4], (const uint8_t[]){ key[i * 3 + 0], key[i * 3 + 1], 0 });
|
2017-04-16 01:20:43 +02:00
|
|
|
base64[WG_KEY_LEN_BASE64 - 2] = '=';
|
|
|
|
base64[WG_KEY_LEN_BASE64 - 1] = '\0';
|
|
|
|
}
|
2015-06-05 15:58:00 +02:00
|
|
|
|
2018-06-06 00:05:47 +02:00
|
|
|
static inline int decode_base64(const char src[static 4])
|
2017-04-16 01:20:43 +02:00
|
|
|
{
|
|
|
|
int val = 0;
|
2017-10-25 17:56:08 +02:00
|
|
|
|
2017-04-16 01:20:43 +02:00
|
|
|
for (unsigned int i = 0; i < 4; ++i)
|
|
|
|
val |= (-1
|
|
|
|
+ ((((('A' - 1) - src[i]) & (src[i] - ('Z' + 1))) >> 8) & (src[i] - 64))
|
|
|
|
+ ((((('a' - 1) - src[i]) & (src[i] - ('z' + 1))) >> 8) & (src[i] - 70))
|
|
|
|
+ ((((('0' - 1) - src[i]) & (src[i] - ('9' + 1))) >> 8) & (src[i] + 5))
|
|
|
|
+ ((((('+' - 1) - src[i]) & (src[i] - ('+' + 1))) >> 8) & 63)
|
|
|
|
+ ((((('/' - 1) - src[i]) & (src[i] - ('/' + 1))) >> 8) & 64)
|
|
|
|
) << (18 - 6 * i);
|
|
|
|
return val;
|
|
|
|
}
|
2015-06-05 15:58:00 +02:00
|
|
|
|
2017-04-16 01:20:43 +02:00
|
|
|
bool key_from_base64(uint8_t key[static WG_KEY_LEN], const char *base64)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
2018-05-30 21:32:27 +02:00
|
|
|
volatile uint8_t ret = 0;
|
2017-04-16 01:20:43 +02:00
|
|
|
int val;
|
2017-10-25 17:56:08 +02:00
|
|
|
|
2017-04-16 01:20:43 +02:00
|
|
|
if (strlen(base64) != WG_KEY_LEN_BASE64 - 1 || base64[WG_KEY_LEN_BASE64 - 2] != '=')
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (i = 0; i < WG_KEY_LEN / 3; ++i) {
|
2017-05-15 23:24:48 +02:00
|
|
|
val = decode_base64(&base64[i * 4]);
|
2018-05-30 21:32:27 +02:00
|
|
|
ret |= (uint32_t)val >> 31;
|
2017-04-16 01:20:43 +02:00
|
|
|
key[i * 3 + 0] = (val >> 16) & 0xff;
|
|
|
|
key[i * 3 + 1] = (val >> 8) & 0xff;
|
|
|
|
key[i * 3 + 2] = val & 0xff;
|
2015-06-05 15:58:00 +02:00
|
|
|
}
|
2017-05-15 23:24:48 +02:00
|
|
|
val = decode_base64((const char[]){ base64[i * 4 + 0], base64[i * 4 + 1], base64[i * 4 + 2], 'A' });
|
2018-05-30 21:32:27 +02:00
|
|
|
ret |= ((uint32_t)val >> 31) | (val & 0xff);
|
2017-04-16 01:20:43 +02:00
|
|
|
key[i * 3 + 0] = (val >> 16) & 0xff;
|
|
|
|
key[i * 3 + 1] = (val >> 8) & 0xff;
|
2018-05-30 21:32:27 +02:00
|
|
|
|
|
|
|
return 1 & ((ret - 1) >> 8);
|
2015-06-05 15:58:00 +02:00
|
|
|
}
|
2017-05-15 23:24:48 +02:00
|
|
|
|
|
|
|
void key_to_hex(char hex[static WG_KEY_LEN_HEX], const uint8_t key[static WG_KEY_LEN])
|
|
|
|
{
|
|
|
|
unsigned int i;
|
2017-10-25 17:56:08 +02:00
|
|
|
|
2017-05-15 23:24:48 +02:00
|
|
|
for (i = 0; i < WG_KEY_LEN; ++i) {
|
|
|
|
hex[i * 2] = 87U + (key[i] >> 4) + ((((key[i] >> 4) - 10U) >> 8) & ~38U);
|
|
|
|
hex[i * 2 + 1] = 87U + (key[i] & 0xf) + ((((key[i] & 0xf) - 10U) >> 8) & ~38U);
|
|
|
|
}
|
|
|
|
hex[i * 2] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
bool key_from_hex(uint8_t key[static WG_KEY_LEN], const char *hex)
|
|
|
|
{
|
2019-02-08 02:32:15 +01:00
|
|
|
uint8_t c, c_acc, c_alpha0, c_alpha, c_num0, c_num, c_val;
|
2018-05-30 21:32:27 +02:00
|
|
|
volatile uint8_t ret = 0;
|
2017-05-15 23:24:48 +02:00
|
|
|
|
|
|
|
if (strlen(hex) != WG_KEY_LEN_HEX - 1)
|
|
|
|
return false;
|
|
|
|
|
2018-05-30 21:32:27 +02:00
|
|
|
for (unsigned int i = 0; i < WG_KEY_LEN_HEX - 1; i += 2) {
|
2017-05-15 23:24:48 +02:00
|
|
|
c = (uint8_t)hex[i];
|
|
|
|
c_num = c ^ 48U;
|
|
|
|
c_num0 = (c_num - 10U) >> 8;
|
|
|
|
c_alpha = (c & ~32U) - 55U;
|
|
|
|
c_alpha0 = ((c_alpha - 10U) ^ (c_alpha - 16U)) >> 8;
|
2018-05-30 21:32:27 +02:00
|
|
|
ret |= ((c_num0 | c_alpha0) - 1) >> 8;
|
2017-05-15 23:24:48 +02:00
|
|
|
c_val = (c_num0 & c_num) | (c_alpha0 & c_alpha);
|
2018-05-30 21:32:27 +02:00
|
|
|
c_acc = c_val * 16U;
|
|
|
|
|
|
|
|
c = (uint8_t)hex[i + 1];
|
|
|
|
c_num = c ^ 48U;
|
|
|
|
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;
|
2017-05-15 23:24:48 +02:00
|
|
|
}
|
2018-05-30 21:32:27 +02:00
|
|
|
|
|
|
|
return 1 & ((ret - 1) >> 8);
|
2017-05-15 23:24:48 +02:00
|
|
|
}
|
2017-09-22 04:04:00 +02:00
|
|
|
|
|
|
|
bool key_is_zero(const uint8_t key[static WG_KEY_LEN])
|
|
|
|
{
|
2017-10-15 21:29:01 +02:00
|
|
|
volatile uint8_t acc = 0;
|
2017-10-25 17:56:08 +02:00
|
|
|
|
2017-09-22 04:04:00 +02:00
|
|
|
for (unsigned int i = 0; i < WG_KEY_LEN; ++i) {
|
|
|
|
acc |= key[i];
|
2019-02-05 01:00:52 +01:00
|
|
|
asm volatile("" : "=r"(acc) : "0"(acc));
|
2017-09-22 04:04:00 +02:00
|
|
|
}
|
2017-10-15 21:29:01 +02:00
|
|
|
return 1 & ((acc - 1) >> 8);
|
2017-09-22 04:04:00 +02:00
|
|
|
}
|