aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/tools/encoding.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/tools/encoding.c6
1 files changed, 6 insertions, 0 deletions
diff --git a/src/tools/encoding.c b/src/tools/encoding.c
index c407b57..da5ccef 100644
--- a/src/tools/encoding.c
+++ b/src/tools/encoding.c
@@ -9,6 +9,7 @@
static inline void encode_base64(char dest[4], const uint8_t src[3])
{
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 };
+
for (unsigned int i = 0; i < 4; ++i)
dest[i] = input[i] + 'A'
+ (((25 - input[i]) >> 8) & 6)
@@ -21,6 +22,7 @@ static inline void encode_base64(char dest[4], const uint8_t src[3])
void key_to_base64(char base64[static WG_KEY_LEN_BASE64], const uint8_t key[static WG_KEY_LEN])
{
unsigned int i;
+
for (i = 0; i < WG_KEY_LEN / 3; ++i)
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 });
@@ -31,6 +33,7 @@ void key_to_base64(char base64[static WG_KEY_LEN_BASE64], const uint8_t key[stat
static inline int decode_base64(const char src[4])
{
int val = 0;
+
for (unsigned int i = 0; i < 4; ++i)
val |= (-1
+ ((((('A' - 1) - src[i]) & (src[i] - ('Z' + 1))) >> 8) & (src[i] - 64))
@@ -46,6 +49,7 @@ bool key_from_base64(uint8_t key[static WG_KEY_LEN], const char *base64)
{
unsigned int i;
int val;
+
if (strlen(base64) != WG_KEY_LEN_BASE64 - 1 || base64[WG_KEY_LEN_BASE64 - 2] != '=')
return false;
@@ -68,6 +72,7 @@ bool key_from_base64(uint8_t key[static WG_KEY_LEN], const char *base64)
void key_to_hex(char hex[static WG_KEY_LEN_HEX], const uint8_t key[static WG_KEY_LEN])
{
unsigned int i;
+
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);
@@ -103,6 +108,7 @@ bool key_from_hex(uint8_t key[static WG_KEY_LEN], const char *hex)
bool key_is_zero(const uint8_t key[static WG_KEY_LEN])
{
volatile uint8_t acc = 0;
+
for (unsigned int i = 0; i < WG_KEY_LEN; ++i) {
acc |= key[i];
__asm__ ("" : "=r" (acc) : "0" (acc));