MySQL 26.10.0
Source Code Documentation
my_kdf.h
Go to the documentation of this file.
1/* Copyright (c) 2022, 2026, Oracle and/or its affiliates.
2
3This program is free software; you can redistribute it and/or modify
4it under the terms of the GNU General Public License, version 2.0,
5as published by the Free Software Foundation.
6
7This program is designed to work with certain software (including
8but not limited to OpenSSL) that is licensed under separate terms,
9as designated in a particular file or component or in included license
10documentation. The authors of MySQL hereby grant you an additional
11permission to link the program and your derivative works with the
12separately licensed software that they have either included with
13the program or referenced in the documentation.
14
15This program is distributed in the hope that it will be useful,
16but WITHOUT ANY WARRANTY; without even the implied warranty of
17MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18GNU General Public License, version 2.0, for more details.
19
20You should have received a copy of the GNU General Public License
21along with this program; if not, write to the Free Software
22Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#include <openssl/opensslv.h>
25#include <string>
26#include <vector>
27
28using std::string;
29using std::vector;
30
31/**
32 Creates the required size of key using supplied key and KDF options.
33
34 KDF: key derivation function (KDF) is a cryptographic algorithm that derives
35 one or more secret keys from a secret value such as a main key, a password, or
36 a passphrase using a pseudorandom function (which typically uses a
37 cryptographic hash function or block cipher)
38
39 @param key Input key
40 @param key_length Input key length
41 @param [out] rkey output key
42 @param rkey_size output key length
43 @param kdf_options KDF function options
44
45 @return 0 on success and 1 on failure
46*/
47int create_kdf_key(const unsigned char *key, const unsigned int key_length,
48 unsigned char *rkey, unsigned int rkey_size,
49 vector<string> *kdf_options);
50
52 protected:
53 vector<string> *kdf_options_{nullptr};
54 bool options_valid_{false};
55
56 public:
58 virtual int derive_key(const unsigned char *key,
59 const unsigned int key_length, unsigned char *rkey,
60 unsigned int key_size) = 0;
61 virtual int validate_options() = 0;
62};
63
64/** Class to implement KDF method hkdf. */
66 string salt_;
67 string info_;
68
69 public:
70 /**
71 hkdf Constructor.
72
73 @param kdf_options options
74
75 kdf_options has following KDF options:
76
77 1. KDF function name
78
79 2. KDF salt: The salt. Salts prevent attacks based on dictionaries of
80 common passwords and attacks based on rainbow tables. It is a public value
81 that can be safely stored along with the encryption key.
82
83 3. KDF info: The context and application specific information.
84 */
85 Key_hkdf_function(vector<string> *kdf_options);
86 virtual ~Key_hkdf_function() override {}
87 int derive_key(const unsigned char *key, const unsigned int key_length,
88 unsigned char *rkey, unsigned int key_size) override;
89 int validate_options() override;
90};
91
92/** Class to implement KDF method pbkdf2_hmac. */
94 string salt_;
96
97 public:
98 /**
99 pbkdf2_hmac Constructor.
100
101 @param kdf_options options
102
103 kdf_options has following KDF options:
104
105 1. KDF function name
106
107 2. KDF salt: The salt. Salts prevent attacks based on dictionaries of
108 common passwords and attacks based on rainbow tables. It is a public value
109 that can be safely stored along with the encryption key.
110
111 3. KDF info: The iteration count.
112 This provides the ability to tune the algorithm.
113 It is better to use the highest count possible for the maximum resistance
114 to brute-force attacks.
115 */
116 Key_pbkdf2_hmac_function(vector<string> *kdf_options);
117 virtual ~Key_pbkdf2_hmac_function() override {}
118 int derive_key(const unsigned char *key, const unsigned int key_length,
119 unsigned char *rkey, unsigned int key_size) override;
120 int validate_options() override;
121};
Definition: my_kdf.h:51
virtual int validate_options()=0
virtual ~Key_derivation_function()
Definition: my_kdf.h:57
bool options_valid_
Definition: my_kdf.h:54
virtual int derive_key(const unsigned char *key, const unsigned int key_length, unsigned char *rkey, unsigned int key_size)=0
vector< string > * kdf_options_
Definition: my_kdf.h:53
Class to implement KDF method hkdf.
Definition: my_kdf.h:65
virtual ~Key_hkdf_function() override
Definition: my_kdf.h:86
Key_hkdf_function(vector< string > *kdf_options)
hkdf Constructor.
Definition: my_kdf.cc:72
string info_
Definition: my_kdf.h:67
int derive_key(const unsigned char *key, const unsigned int key_length, unsigned char *rkey, unsigned int key_size) override
Definition: my_kdf.cc:92
int validate_options() override
Definition: my_kdf.cc:80
string salt_
Definition: my_kdf.h:66
Class to implement KDF method pbkdf2_hmac.
Definition: my_kdf.h:93
virtual ~Key_pbkdf2_hmac_function() override
Definition: my_kdf.h:117
string salt_
Definition: my_kdf.h:94
int validate_options() override
Definition: my_kdf.cc:156
Key_pbkdf2_hmac_function(vector< string > *kdf_options)
pbkdf2_hmac Constructor.
Definition: my_kdf.cc:147
int derive_key(const unsigned char *key, const unsigned int key_length, unsigned char *rkey, unsigned int key_size) override
Definition: my_kdf.cc:179
int iterations_
Definition: my_kdf.h:95
int create_kdf_key(const unsigned char *key, const unsigned int key_length, unsigned char *rkey, unsigned int rkey_size, vector< string > *kdf_options)
Creates the required size of key using supplied key and KDF options.
Definition: my_kdf.cc:47
std::vector< T, ut::allocator< T > > vector
Specialization of vector which uses allocator.
Definition: ut0new.h:2724
required string key
Definition: replication_asynchronous_connection_failover.proto:60