return true;
}
-static inline bool key_is_valid(uint8_t key[WG_KEY_LEN])
-{
- static const uint8_t zero[WG_KEY_LEN] = { 0 };
- return !!memcmp(key, zero, WG_KEY_LEN);
-}
-
bool config_read_finish(struct config_ctx *ctx)
{
size_t i;
struct wgpeer *peer;
- if (ctx->buf.dev->flags & WGDEVICE_REPLACE_PEERS && !key_is_valid(ctx->buf.dev->private_key)) {
+ if (ctx->buf.dev->flags & WGDEVICE_REPLACE_PEERS && key_is_zero(ctx->buf.dev->private_key)) {
fprintf(stderr, "No private key configured\n");
goto err;
}
ctx->buf.dev->flags |= WGDEVICE_REMOVE_FWMARK;
for_each_wgpeer(ctx->buf.dev, peer, i) {
- if (!key_is_valid(peer->public_key)) {
+ if (key_is_zero(peer->public_key)) {
fprintf(stderr, "A peer is missing a public key\n");
goto err;
}
bool key_from_hex(uint8_t key[static WG_KEY_LEN], const char *hex)
{
- uint8_t i, 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, state = 0;
if (strlen(hex) != WG_KEY_LEN_HEX - 1)
return false;
- for (i = 0; i < WG_KEY_LEN_HEX - 1; ++i) {
+ for (unsigned int i = 0; i < WG_KEY_LEN_HEX - 1; ++i) {
c = (uint8_t)hex[i];
c_num = c ^ 48U;
c_num0 = (c_num - 10U) >> 8;
}
return true;
}
+
+bool key_is_zero(const uint8_t key[static WG_KEY_LEN])
+{
+ uint8_t acc = 0;
+ for (unsigned int i = 0; i < WG_KEY_LEN; ++i) {
+ acc |= key[i];
+ __asm__ ("" : "=r" (acc) : "0" (acc));
+ }
+ return acc == 0;
+}
void key_to_hex(char hex[static WG_KEY_LEN_HEX], const uint8_t key[static WG_KEY_LEN]);
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]);
+
#endif
static int userspace_set_device(struct wgdevice *dev)
{
- static const uint8_t zero[WG_KEY_LEN] = { 0 };
char hex[WG_KEY_LEN_HEX], ip[INET6_ADDRSTRLEN], host[4096 + 1], service[512 + 1];
struct wgpeer *peer;
struct wgipmask *ipmask;
if (dev->flags & WGDEVICE_REMOVE_PRIVATE_KEY)
fprintf(f, "private_key=\n");
- else if (memcmp(dev->private_key, zero, WG_KEY_LEN)) {
+ else if (!key_is_zero(dev->private_key)) {
key_to_hex(hex, dev->private_key);
fprintf(f, "private_key=%s\n", hex);
}
}
if (peer->flags & WGPEER_REMOVE_PRESHARED_KEY)
fprintf(f, "preshared_key=\n");
- else if (memcmp(peer->preshared_key, zero, WG_KEY_LEN)) {
+ else if (!key_is_zero(peer->preshared_key)) {
key_to_hex(hex, peer->preshared_key);
fprintf(f, "preshared_key=%s\n", hex);
}
free(new_device);
}
-static const uint8_t zero[WG_KEY_LEN] = { 0 };
-
static char *key(const uint8_t key[static WG_KEY_LEN])
{
static char base64[WG_KEY_LEN_BASE64];
- if (!memcmp(key, zero, WG_KEY_LEN))
+ if (key_is_zero(key))
return "(none)";
key_to_base64(base64, key);
return base64;
terminal_printf(TERMINAL_RESET);
terminal_printf(TERMINAL_FG_GREEN TERMINAL_BOLD "interface" TERMINAL_RESET ": " TERMINAL_FG_GREEN "%s" TERMINAL_RESET "\n", device->interface);
- if (memcmp(device->public_key, zero, WG_KEY_LEN))
+ if (!key_is_zero(device->public_key))
terminal_printf(" " TERMINAL_BOLD "public key" TERMINAL_RESET ": %s\n", key(device->public_key));
- if (memcmp(device->private_key, zero, WG_KEY_LEN))
+ if (!key_is_zero(device->private_key))
terminal_printf(" " TERMINAL_BOLD "private key" TERMINAL_RESET ": %s\n", masked_key(device->private_key));
if (device->port)
terminal_printf(" " TERMINAL_BOLD "listening port" TERMINAL_RESET ": %u\n", device->port);
}
for_each_wgpeer(device, peer, i) {
terminal_printf(TERMINAL_FG_YELLOW TERMINAL_BOLD "peer" TERMINAL_RESET ": " TERMINAL_FG_YELLOW "%s" TERMINAL_RESET "\n", key(peer->public_key));
- if (memcmp(peer->preshared_key, zero, WG_KEY_LEN))
+ if (!key_is_zero(peer->preshared_key))
terminal_printf(" " TERMINAL_BOLD "preshared key" TERMINAL_RESET ": %s\n", masked_key(peer->preshared_key));
if (peer->endpoint.addr.sa_family == AF_INET || peer->endpoint.addr.sa_family == AF_INET6)
terminal_printf(" " TERMINAL_BOLD "endpoint" TERMINAL_RESET ": %s\n", endpoint(&peer->endpoint.addr));
int showconf_main(int argc, char *argv[])
{
- static const uint8_t zero[WG_KEY_LEN] = { 0 };
char base64[WG_KEY_LEN_BASE64];
char ip[INET6_ADDRSTRLEN];
struct wgdevice *device = NULL;
printf("ListenPort = %u\n", device->port);
if (device->fwmark)
printf("FwMark = 0x%x\n", device->fwmark);
- if (memcmp(device->private_key, zero, WG_KEY_LEN)) {
+ if (!key_is_zero(device->private_key)) {
key_to_base64(base64, device->private_key);
printf("PrivateKey = %s\n", base64);
}
for_each_wgpeer(device, peer, i) {
key_to_base64(base64, peer->public_key);
printf("[Peer]\nPublicKey = %s\n", base64);
- if (memcmp(peer->preshared_key, zero, WG_KEY_LEN)) {
+ if (!key_is_zero(peer->preshared_key)) {
key_to_base64(base64, peer->preshared_key);
printf("PresharedKey = %s\n", base64);
}