2 * Copyright (c) 2020 Doc.ai and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #ifndef __included_wg_messages_h__
17 #define __included_wg_messages_h__
20 #include <wireguard/wireguard_noise.h>
21 #include <wireguard/wireguard_cookie.h>
23 #define WG_TICK 0.01 /**< WG tick period (s) */
24 #define WHZ (u32) (1/WG_TICK) /**< WG tick frequency */
26 #define NOISE_KEY_LEN_BASE64 ((((NOISE_PUBLIC_KEY_LEN) + 2) / 3) * 4 + 1)
27 #define noise_encrypted_len(plain_len) ((plain_len) + NOISE_AUTHTAG_LEN)
32 REKEY_TIMEOUT_JITTER = WHZ / 3,
33 KEEPALIVE_TIMEOUT = 10,
34 MAX_TIMER_HANDSHAKES = 90 / REKEY_TIMEOUT,
38 #define foreach_wg_message_type \
39 _(INVALID, "Invalid") \
40 _(HANDSHAKE_INITIATION, "Handshake initiation") \
41 _(HANDSHAKE_RESPONSE, "Handshake response") \
42 _(HANDSHAKE_COOKIE, "Handshake cookie") \
45 typedef enum message_type
47 #define _(v,s) MESSAGE_##v,
48 foreach_wg_message_type
52 typedef struct message_header
57 typedef struct message_handshake_initiation
59 message_header_t header;
61 u8 unencrypted_ephemeral[NOISE_PUBLIC_KEY_LEN];
62 u8 encrypted_static[noise_encrypted_len (NOISE_PUBLIC_KEY_LEN)];
63 u8 encrypted_timestamp[noise_encrypted_len (NOISE_TIMESTAMP_LEN)];
65 } message_handshake_initiation_t;
67 typedef struct message_handshake_response
69 message_header_t header;
72 u8 unencrypted_ephemeral[NOISE_PUBLIC_KEY_LEN];
73 u8 encrypted_nothing[noise_encrypted_len (0)];
75 } message_handshake_response_t;
77 typedef struct message_handshake_cookie
79 message_header_t header;
81 u8 nonce[COOKIE_NONCE_SIZE];
82 u8 encrypted_cookie[noise_encrypted_len (COOKIE_MAC_SIZE)];
83 } message_handshake_cookie_t;
85 typedef struct message_data
87 message_header_t header;
93 #define message_data_len(plain_len) \
94 (noise_encrypted_len(plain_len) + sizeof(message_data_t))
96 #endif /* __included_wg_messages_h__ */
99 * fd.io coding-style-patch-verification: ON
102 * eval: (c-set-style "gnu")