VLink 2.0.0
A high-performance communication middleware
Loading...
Searching...
No Matches
security.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2026 by Thun Lu. All rights reserved.
3 * Author: Thun Lu <thun.lu@zohomail.cn>
4 * Repo: https://github.com/thun-res/vlink
5 * _ __ __ _ __
6 * | | / / / / (_) ____ / /__
7 * | | / / / / / / / __ \ / //_/
8 * | |/ / / /___ / / / / / / / ,<
9 * |___/ /_____/ /_/ /_/ /_/ /_/|_|
10 *
11 * Licensed under the Apache License, Version 2.0 (the "License");
12 * you may not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
14 *
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
22 */
23
24/**
25 * @file security.h
26 * @brief AES-128-CBC encryption/decryption with optional custom callback override.
27 *
28 * @details
29 * @c Security provides message-level encryption and decryption for VLink transports.
30 * When compiled with @c VLINK_ENABLE_SECURITY (requires OpenSSL), it uses
31 * AES-128-CBC via the EVP API with PKCS7 padding. The default AES key is
32 * @c "vlink" and the IV is @c "thun.lu@zohomail.cn" (OpenSSL uses the first
33 * 16 bytes of each).
34 *
35 * Custom crypto implementations can replace the built-in algorithm by registering
36 * a pair of @c Callback functions via @c set_callbacks(). When custom callbacks
37 * are installed, the AES implementation is bypassed entirely.
38 *
39 * @par Compile requirements
40 * - Built-in AES: link with @c -lssl @c -lcrypto and define @c VLINK_ENABLE_SECURITY.
41 * - Custom callback: no additional dependencies.
42 *
43 * @par Typical usage
44 * @code
45 * vlink::Security security;
46 * security.set_key("my_secret_key");
47 *
48 * vlink::Bytes cipher;
49 * security.encrypt(plain_bytes, cipher);
50 *
51 * vlink::Bytes recovered;
52 * security.decrypt(cipher, recovered);
53 * @endcode
54 *
55 * @note
56 * - All public methods are thread-safe (protected by an internal mutex).
57 * - @c encrypt() and @c decrypt() return @c true on success, @c false on failure.
58 * - If @c VLINK_ENABLE_SECURITY is not defined, @c encrypt() and @c decrypt()
59 * log a warning and return @c false.
60 * - Passing an empty @c Bytes to @c encrypt() or @c decrypt() is a no-op that
61 * returns @c true immediately.
62 */
63
64#pragma once
65
66#include <functional>
67#include <memory>
68#include <string>
69
70#include "../base/bytes.h"
71#include "../base/macros.h"
72
73namespace vlink {
74
75/**
76 * @class Security
77 * @brief Thread-safe AES-128-CBC encryption/decryption utility with custom callback support.
78 *
79 * @details
80 * Each @c Security instance holds its own EVP context and key material.
81 * Copy and assignment are disabled; instances are intended to be owned by a single
82 * transport endpoint.
83 */
85 public:
86 /**
87 * @brief Callback type for custom encryption or decryption.
88 *
89 * @details
90 * Called with the input @c Bytes; the implementation must write the result into
91 * @p out and return @c true on success. When a custom callback is installed via
92 * @c set_callbacks(), the built-in AES implementation is bypassed.
93 */
94 using Callback = std::function<bool(const Bytes& in, Bytes& out)>;
95
96 /**
97 * @brief Constructs a @c Security object with the default AES key and IV.
98 *
99 * @details
100 * Initialises an OpenSSL EVP context with AES-128-CBC and PKCS7 padding
101 * when @c VLINK_ENABLE_SECURITY is defined. Otherwise logs a warning.
102 */
104
105 /**
106 * @brief Destroys the @c Security object and frees the EVP context.
107 */
109
110 /**
111 * @brief Sets the AES encryption key.
112 *
113 * @details
114 * The key string is stored as @c Bytes. If @p key is empty, the default
115 * built-in key @c "vlink" is restored. The key length is not validated here;
116 * OpenSSL will use the first 16 bytes for AES-128.
117 *
118 * @param key Key string. Pass an empty string to restore the default.
119 */
120 void set_key(const std::string& key);
121
122 /**
123 * @brief Installs custom encrypt and decrypt callbacks, bypassing the built-in AES.
124 *
125 * @details
126 * When both callbacks are set, @c encrypt() delegates to @p encrypt_callback and
127 * @c decrypt() delegates to @p decrypt_callback. Set to @c nullptr to fall back
128 * to the built-in AES implementation.
129 *
130 * @param encrypt_callback Custom encryption function.
131 * @param decrypt_callback Custom decryption function.
132 */
133 void set_callbacks(Callback&& encrypt_callback, Callback&& decrypt_callback);
134
135 /**
136 * @brief Encrypts @p in and writes the ciphertext into @p out.
137 *
138 * @details
139 * Uses the custom encrypt callback if installed, otherwise uses AES-128-CBC.
140 * If @p in is empty, @p out is unchanged and @c true is returned immediately.
141 *
142 * @param in Plaintext bytes.
143 * @param out Output buffer for ciphertext. Overwritten on success.
144 * @return @c true on success; @c false if encryption fails or the feature is disabled.
145 */
146 bool encrypt(const Bytes& in, Bytes& out);
147
148 /**
149 * @brief Decrypts @p in and writes the plaintext into @p out.
150 *
151 * @details
152 * Uses the custom decrypt callback if installed, otherwise uses AES-128-CBC.
153 * If @p in is empty, @p out is unchanged and @c true is returned immediately.
154 *
155 * @param in Ciphertext bytes.
156 * @param out Output buffer for plaintext. Overwritten on success.
157 * @return @c true on success; @c false if decryption fails or the feature is disabled.
158 */
159 bool decrypt(const Bytes& in, Bytes& out);
160
161 private:
162 std::unique_ptr<struct SecurityImpl> impl_;
163
165};
166
167} // namespace vlink
Versatile byte buffer with small-buffer optimisation, ownership semantics and compression.
Platform-independent macro definitions for the VLink library.
#define VLINK_EXPORT
Definition macros.h:85
#define VLINK_DISALLOW_COPY_AND_ASSIGN(classname)
Deletes the copy constructor and copy-assignment operator of classname.
Definition macros.h:184