Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 14aa765

Browse files
committedMar 16, 2025
PS-9713 [DOCS] - Document new PS Encryption UDFs functionality (inspired by upstream 8.4.4)
modified: docs/encryption-functions.md
1 parent 3431afe commit 14aa765

File tree

1 file changed

+178
-58
lines changed

1 file changed

+178
-58
lines changed
 

‎docs/encryption-functions.md

+178-58
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,69 @@ When choosing key lengths, consider the following:
1212

1313
* If performance is important and the functions are frequently used, use symmetric encryption. Symmetric encryption functions are faster than asymmetric encryption functions. Moreover, asymmetric encryption has restrictions on the maximum length of a message being encrypted. For example, for RSA the algorithm maximum message size is the key length in bytes (key length in bits / 8) minus 11.
1414

15-
The following table and sections describe the functions. For examples, see function examples.
15+
## Version updates
1616

17-
| Function Name |
18-
|----------------------------------------------------------------------------------------------------------------------------------|
19-
| [asymmetric_decrypt(algorithm, crypt_str, key_str)](#asymmetric_decryptalgorithm-crypt_str-key_str) |
20-
| [asymmetric_derive(pub_key_str, priv_key_str)](#asymmetric_derivepub_key_str-priv_key_str) |
21-
| [asymmetric_encrypt(algorithm, str, key_str)](#asymmetric_encryptalgorithm-str-key_str) |
22-
| [asymmetric_sign(algorithm, digest_str, priv_key_str, digest_type)](#asymmetric_signalgorithm-digest_str-priv_key_str-digest_type) |
23-
| [asymmetric_verify(algorithm, digest_str, sig_str, pub_key_str, digest_type)](#asymmetric_verifyalgorithm-digest_str-sig_str-pub_key_str-digest_type) |
24-
| [create_asymmetric_priv_key(algorithm, (key_len | dh_parameters))](#create_asymmetric_priv_keyalgorithm-key_len--dh_parameters) |
25-
| [create_asymmetric_pub_key(algorithm, priv_key_str)](#create_asymmetric_pub_keyalgorithm-priv_key_str) |
26-
| [create_dh_parameters(key_len)](#create_dh_parameterskey_len) |
27-
| [create_digest(digest_type, str)](#create_digestdigest_type-str) |
28-
29-
The following table describes the Encryption threshold variables which can be used to set the maximum value for a key length based on the type of encryption.
17+
Percona Server for MySQL 8.4.4 adds the following:
3018

31-
| Variable Name |
32-
|-----------------------------------|
33-
| [encryption_udf.dh_bits_threshold](#encryption_udfdh_bits_threshold) |
34-
| [encryption_udf.dsa_bits_threshold](#encryption_udfdsa_bits_threshold) |
35-
| [encryption_udf.rsa_bits_threshold](#encryption_udfrsa_bits_threshold) |
19+
* Support for `pkcs1`, `oaep`, or `no` padding for RSA encrypt and decrypt operations
20+
21+
<details>
22+
<summary> `pkcs1` padding explanation</summary>
23+
[`RSAES-PKCS1-v1_5`](https://en.wikipedia.org/wiki/PKCS_1) RSA encryption padding scheme prevents patterns that attackers could exploit by including a random sequence of bytes, which ensures that the ciphertext is different no matter how many times it is encrypted.
24+
</details>
25+
<details>
26+
<summary> `oaep` padding explanation</summary>
27+
The [`RSAES-OAEP`](https://en.wikipedia.org/wiki/PKCS_1) - [`Optimal Asymmetric Encryption Padding`](https://en.wikipedia.org/wiki/Optimal_asymmetric_encryption_padding) RSA encryption padding scheme adds a randomized mask generation function. This function makes it more difficult for attackers to exploit the encryption algorithm's weaknesses or recover the original message.
28+
</details>
29+
<details>
30+
<summary> `no` padding explanation</summary>
31+
Using `no` padding means the plaintext message is encrypted without adding an extra layer before performing the RSA encryption operation.
32+
</details>
33+
34+
* Support for `pkcs1` or `pkcs1_pss` padding for RSA sign and verify operations
35+
36+
<details>
37+
<summary> `pkcs1` padding explanation</summary>
38+
The [`RSASSA-PKCS1-v1_5`](https://en.wikipedia.org/wiki/PKCS_1) is a deterministic RSA signature padding scheme that hashes a message, pads the hash with a specific structure, and encrypts it with the signer's private key for signature generation.
39+
</details>
40+
<details>
41+
<summary> `pkcs1_pss` padding explanation</summary>
42+
The [`RSASSA-PSS`](https://en.wikipedia.org/wiki/PKCS_1) - [`Probabilistic Signature Scheme'](https://en.wikipedia.org/wiki/Probabilistic_signature_scheme) is an RSA signature padding scheme used to add randomness to a message before signing it with a private key. This randomness helps to increase the security of the signature and makes it more resistant to various attacks.
43+
</details>
44+
45+
* [`encryption_udf.legacy_paddding`](#legacy_padding) system variable
46+
47+
* Character set awareness
48+
49+
## Charset Awareness
50+
51+
All component_encryption_udf functions now handle character sets intelligently:
52+
53+
• Algorithms, digest names, padding schemes, keys, and parameters in PEM format: Automatically converted to the ASCII charset at the MySQL level before passing to the functions.
54+
55+
• Messages, data blocks, and signatures used for digest calculation, encryption, decryption, signing, or verification: Automatically converted to the binary charset at the MySQL level before passing to the functions.
56+
57+
• Function return values in PEM format: Assigned the ASCII charset.
58+
59+
• Function return values for operations like digest calculation, encryption, decryption, and signing: Assigned the binary charset.
60+
61+
## Use user-defined functions
62+
63+
You can also use the user-defined functions with the PEM format keys generated externally by the OpenSSL utility.
64+
65+
A digest uses plaintext and generates a hash value. This hash value can verify if the plaintext is unmodified. You can also sign or verify on digests to ensure that the original plaintext was not modified. You cannot decrypt the original text from the hash value.
66+
67+
When choosing key lengths, consider the following:
68+
69+
* Encryption strength increases with the key size and generation time.
70+
71+
* If performance is essential and the functions are frequently used, use symmetric encryption. Symmetric encryption functions are faster than asymmetric encryption functions. Moreover, asymmetric encryption restricts the maximum length of a message being encrypted. For example, the algorithm's maximum message size for RSA is the key length in bytes (key length in bits / 8) minus 11.
3672

3773
## Install component_encryption_udf
3874

3975
Use the [Install Component Statement] to add the component_encryption_udf component. The functions and variables are available. The user-defined functions and the Encryption threshold variables are auto-registered. There is no requirement to invoke `CREATE FUNCTION ... SONAME ...`.
4076

41-
The `INSERT` privilege on the `mysql.component` system table is required to run the `INSTALL COMPONENT` statement. To register the component, the operation adds a row to this table.
77+
The `INSERT` privilege on the `mysql.component` system table is required to run the `INSTALL COMPONENT` statement. The operation adds a row to this table to register the component.
4278

4379
The following is an example of the installation command:
4480

@@ -48,11 +84,41 @@ mysql> INSTALL COMPONENT 'file://component_encryption_udf';
4884

4985
!!! note
5086

51-
If you are Compiling Percona Server for MySQL from Source, the Encryption UDF component is built by default when Percona Server for MySQL is built. Specify the `-DWITH_ENCRYPTION_UDF=OFF` cmake option to exclude it.
87+
When you build Percona Server for MySQL from source code, the Encryption UDF component is included by default. To exclude it, use the `-DWITH_ENCRYPTION_UDF=OFF` option with cmake.
88+
89+
90+
91+
## Functions
92+
93+
The following table and sections describe the functions. For examples, see function examples.
94+
95+
| Function Name |
96+
|----------------------------------------------------------------------------------------------------------------------------------|
97+
| [asymmetric_decrypt()](#asymmetric_decrypt()) |
98+
| [asymmetric_derive()](#asymmetric_derive()) |
99+
| [asymmetric_encrypt()](#asymmetric_encrypt()) |
100+
| [asymmetric_sign()](#asymmetric_sign()) |
101+
| [asymmetric_verify()](#asymmetric_verify()) |
102+
| [create_asymmetric_priv_key()](#create_asymmetric_priv_key()) |
103+
| [create_asymmetric_pub_key()](#create_asymmetric_pub_key()) |
104+
| [create_dh_parameters()](#create_dh_parameters()) |
105+
| [create_digest()](#create_digest()) |
106+
107+
The following table describes the encryption threshold variables which can be used to set the maximum value for a key length based on the type of encryption used.
108+
109+
| Variable Name |
110+
|-----------------------------------|
111+
| [encryption_udf.dh_bits_threshold](#dh_bits_threshold) |
112+
| [encryption_udf.dsa_bits_threshold](#dsa_bits_threshold) |
113+
| [encryption_udf.legacy_padding](#legacy_padding)|
114+
| [encryption_udf.rsa_bits_threshold](#rsa_bits_threshold) |
115+
116+
117+
52118

53119
## User-defined functions described
54120

55-
## asymmetric_decrypt(*algorithm, crypt_str, key_str*)
121+
## <a name="asymmetric_decrypt()">asymmetric_decrypt(*algorithm, crypt_str, key_str*)</a>
56122

57123
Decrypts an encrypted string using the algorithm and a key string.
58124

@@ -64,15 +130,19 @@ A plaintext as a string.
64130

65131
The following are the function’s parameters:
66132

67-
* algorithm - the encryption algorithm supports RSA to decrypt the string.
133+
* algorithm - the encryption algorithm supports RSA in decrypting the string.
134+
135+
* `crypt_str` - an encrypted string produced by certain encryption functions like AES_ENCRYPT(). This string is typically stored as a binary or blog data type.
68136

69137
* key_str - a string in the PEM format. The key string must have the following attributes:
70138

71139
* Valid
72140

73-
* Public or private key string that corresponds with the private or public key string used with the asymmetric_encrypt function.
141+
* Public or private key string corresponding with the private or public key string used with the [`asymmetric_encrypt`](#asymmetric_encrypt()) function.
142+
143+
* padding - An optional parameter introduced in Percona Server for MySQL 8.4.4. It is used with the RSA algorithm and supports RSA encryption padding schemes like pkcs1, or oaep. If you skip this parameter, the system determines its value based on the [`encryption_udf.legacy_padding`](#legacy_padding) variable.
74144

75-
## asymmetric_derive(*pub_key_str, priv_key_str*)
145+
## <a name="asymmetric_derive()">asymmetric_derive(*pub_key_str, priv_key_str*)</a>
76146

77147
Derives a symmetric key using a public key generated on one side and a private key generated on another.
78148

@@ -86,7 +156,7 @@ The `pub_key_str` must be a public key in the PEM format and generated using the
86156

87157
The `priv_key_str` must be a private key in the PEM format and generated using the Diffie-Hellman (DH) algorithm.
88158

89-
## asymmetric_encrypt(*algorithm, str, key_str*)
159+
## <a name="asymmetric_encrypt()">asymmetric_encrypt(*algorithm, str, key_str*)</a>
90160

91161
Encrypts a string using the algorithm and a key string.
92162

@@ -104,7 +174,9 @@ The parameters are the following:
104174

105175
* key_str - a key (either private or public) in the PEM format
106176

107-
## asymmetric_sign(*algorithm, digest_str, priv_key_str, digest_type*)
177+
* padding - An optional parameter introduced in Percona Server for MySQL 8.4.4. It is used with the RSA algorithm and supports RSA encryption padding schemes like pkcs1, or oaep. If you skip this parameter, the system determines its value based on the [`encryption_udf.legacy_padding`](#legacy_padding) variable.
178+
179+
## <a name=“asymmetric_sign()">asymmetric_sign(*algorithm, digest_str, priv_key_str, digest_type*)</a>
108180

109181
Signs a digest string using a private key string.
110182

@@ -116,7 +188,7 @@ A signature is a binary string.
116188

117189
The parameters are the following:
118190

119-
* algorithm - the encryption algorithm supports either RSA or DSA to encrypt the string.
191+
* algorithm - the encryption algorithm supports either RSA or DSA in encrypting the string.
120192

121193
* digest_str - the digest binary string that is signed. Invoking create_digest generates the digest.
122194

@@ -146,10 +218,13 @@ The parameters are the following:
146218
| | | shake128 | |
147219
| | | shake256 | |
148220

149-
## asymmetric_verify(*algorithm, digest_str, sig_str, pub_key_str, digest_type*)
221+
* padding - An optional parameter introduced in Percona Server for MySQL 8.4.4. It is used with the RSA algorithm and supports RSA signature padding schemes like `pkcs1`, or `pkcs1_pss`. If you skip this parameter, the system determines its value based on the [`encryption_udf.legacy_padding`](#legacy_padding) variable.
222+
223+
## <a name="asymmetric_verify()">asymmetric_verify(*algorithm, digest_str, sig_str, pub_key_str, digest_type*)</a>
150224

151225
Verifies whether the signature string matches the digest string.
152226

227+
153228
### asymmetric_verify output
154229

155230
A `1` (success) or a `0` (failure).
@@ -168,7 +243,9 @@ The parameters are the following:
168243

169244
* digest_type - the supported values are listed in the digest type table of create_digest
170245

171-
## create_asymmetric_priv_key(*algorithm, (key_len | dh_parameters)*)
246+
* padding - An optional parameter introduced in Percona Server for MySQL 8.4.4. It is used with the RSA algorithm and supports RSA signature padding schemes like `pkcs1`, or `pkcs1_pss`. If you skip this parameter, the system determines its value based on the [`encryption_udf.legacy_padding`](#legacy_padding) variable.
247+
248+
## <a name="create_asymmetric_priv_key()">create_asymmetric_priv_key(*algorithm,(key_len | dh_parameters)*)</a>
172249

173250
Generates a private key using the given algorithm and key length for RSA or DSA
174251
or Diffie-Hellman parameters for DH. For RSA or DSA, if needed, execute `KILL
@@ -198,7 +275,7 @@ The parameters are the following:
198275

199276
* dh_parameters - Diffie-Hellman (DH) parameters. Invoking create_dh_parameter creates the DH parameters.
200277

201-
## create_asymmetric_pub_key(*algorithm, priv_key_str*)
278+
## <a name="create_asymmetric_pub_key()">create_asymmetric_pub_key(*algorithm, priv_key_str*)</a>
202279

203280
Derives a public key from the given private key using the given algorithm.
204281

@@ -214,29 +291,28 @@ The parameters are the following:
214291

215292
* priv_key_str - must be a valid key string in the PEM format.
216293

217-
## create_dh_parameters(*key_len*)
294+
## <a name="create_dh_parameters()">create_dh_parameters(*key_len*)</a>
218295

219296
Creates parameters for generating a Diffie-Hellman (DH) private/public key pair.
220297
If needed, execute `KILL [QUERY|CONNECTION] <id>` to terminate the generation of long-lasting parameters.
221298

222299
Generating the DH parameters can take more time than generating the RSA keys or
223300
the DSA keys.
224-
OpenSSL defines the parameter length limits. To change the
225-
maximum parameter length, use encryption_udf.dh_bits_threshold.
301+
OpenSSL defines the parameter length limits. To change the maximum parameter length, use [`encryption_udf.dh_bits_threshold`](#dh_bits_threshold).
226302

227303
### create_dh_parameters output
228304

229-
A string in the PEM format and can be passed to create_asymmetric_private_key.
305+
A string in the PEM format and can be passed to [`create_asymmetric_priv_key`](#create_asymmetric_priv_key()).
230306

231307
### create_dh_parameters parameters
232308

233309
The parameters are the following:
234310

235311
* key_len - the range for the key length is from 1024 to 10,000. The default value is 10,000.
236312

237-
## create_digest(*digest_type, str*)
313+
## <a name="create_digest()">create_digest(*digest_type, str*)</a>
238314

239-
Creates a digest from the given string using the given digest type. The digest string can be used with asymmetric_sign and asymmetric_verify.
315+
Creates a digest from the given string using the given digest type. The digest string can be used with [`asymmetric_sign()`](#asymmetric_sign()) and [`asymmetric_verify()`](#asymmetric_verify()).
240316

241317
### create_digest output
242318

@@ -282,9 +358,9 @@ The variables are automatically registered when component_encryption_udf is inst
282358
|----------------------------------|
283359
| encryption_udf.dh_bits_threshold |
284360

285-
### `encryption_udf.dh_bits_threshold`
361+
### <a name="dh_bits_threshold">`encryption_udf.dh_bits_threshold`</a>
286362

287-
The variable sets the maximum limit for the create_dh_parameters user-defined function and takes precedence over the OpenSSL maximum length value.
363+
The variable sets the maximum limit for the [`create_dh_parameters()`](#create_dh_parameters()) user-defined function and takes precedence over the OpenSSL maximum length value.
288364

289365
| Option | Description |
290366
|--------------|------------------|
@@ -295,9 +371,9 @@ The variable sets the maximum limit for the create_dh_parameters user-defined fu
295371

296372
The range for this variable is from 1024 to 10,000. The default value is 10,000.
297373

298-
### encryption_udf.dsa_bits_threshold
374+
### <a name="dsa_bits_threshold">`encryption_udf.dsa_bits_threshold`</a>
299375

300-
The variable sets the threshold limits for create_asymmetric_priv_key user-defined function when the function is invoked with the DSA parameter and takes precedence over the OpenSSL maximum length value.
376+
The variable sets the threshold limits for [`create_asymmetric_priv_key`](#create_asymmetric_priv_key()) user-defined function when the function is invoked with the DSA parameter and takes precedence over the OpenSSL maximum length value.
301377

302378
| Option | Description |
303379
|--------------|------------------|
@@ -308,9 +384,53 @@ The variable sets the threshold limits for create_asymmetric_priv_key user-defin
308384

309385
The range for this variable is from 1,024 to 9,984. The default value is 9,984.
310386

311-
### encryption_udf.rsa_bits_threshold
387+
### <a name="legacy_padding">`encryption_udf.legacy_padding`</a>
388+
389+
The variable enables or disables the legacy padding scheme for certain encryption operations.
390+
391+
| Option | Description |
392+
|--------------|------------------|
393+
| command-line | Yes |
394+
| scope | Global |
395+
| data type | Boolean |
396+
| default | OFF |
397+
398+
This system variable is a BOOLEAN type and set to `OFF` by default.
399+
400+
This variable controls how the functions [`asymmetric_encrypt()`](#asymmetric_encrypt()), [`asymmetric_decrypt()`](#asymmetric_decrypt()), [`asymmetric_sign()`](#asymmetric_sign()), and [`asymmetric_verify()`](#asymmetric_verify()) behave when you don’t explicitly set the padding parameter.
401+
402+
• When encryption_udf.legacy_padding is OFF:
403+
404+
[asymmetric_encrypt()](#asymmetric_encrypt()) and [asymmetric_decrypt()](#asymmetric_decrypt()) use OAEP encryption padding.
405+
406+
[asymmetric_sign()](#asymmetric_sign()) and [asymmetric_verify()](#asymmetric_verify()) use PKCS1_PSS signature padding.
407+
408+
• When encryption_udf.legacy_padding is ON:
409+
410+
[`asymmetric_encrypt()`](#asymmetric_encrypt()) and [`asymmetric_decrypt()`](#asymmetric_decrypt()) use PKCS1 encryption padding.
411+
412+
[`asymmetric_sign()`](#asymmetric_sign()) and [`asymmetric_verify()`](#asymmetric_verify()) use PKCS1 signature padding.
413+
414+
The [`asymmetric_encrypt()`](#asymmetric_encrypt()) and [`asymmetric_decrypt()`](#asymmetric_decrypt()) functions, when the encryption is `RSA`, can accept an optional parameter, `padding`. You can set this parameter to `no`, `pkcs1`, or `oaep`. If you don’t specify this parameter, it defaults based on the [`encryption_udf.legacy_padding`](#legacy_padding) value.
415+
416+
The padding schemes have the following limitations:
417+
418+
| Padding Scheme | Details |
419+
|-------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------|
420+
| `oaep` | The message you encrypt can be as long as your RSA key size in bytes - 42 bytes.|
421+
| `no` | The message length must exactly match your RSA key size in bytes. For example, if your key is 1024 bits (128 bytes), the message must also be 128 bytes. If it doesn’t match, it will cause an error. |
422+
| `pkcs1` | Your message can be equal to or smaller than the RSA key size - 11 bytes. For instance, with a 1024-bit RSA key, your message can’t be longer than 117 bytes.|
423+
424+
Similarly, [`asymmetric_sign()`](#asymmetric_sign()) and [`asymmetric_verify()`](#asymmetric_verify()) also have an optional `padding` parameter, either `pkcs1` or `pkcs1_pss`. If not explicitly set, it follows the default based on [`encryption_udf.legacy_padding`](#legacy_padding). You can only use the padding parameter with RSA algorithms.
425+
426+
#### Additional resources
427+
428+
For more information, read [`Digital Signatures: Another layer of Data Protection in Percona Server for MySQL`](https://www.percona.com/blog/digital-signatures-another-layer-of-data-protection-in-percona-server-for-mysql/)
429+
312430

313-
The variable sets the threshold limits for the create_asymmetric_priv_key user-defined function when the function is invoked with the RSA parameter and takes precedence over the OpenSSL maximum length value.
431+
### <a name="rsa_bits_threshold">encryption_udf.rsa_bits_threshold</a>
432+
433+
The variable sets the threshold limits for the [`create_asymmetric_priv_key`](#create_asymmetric_priv_key()) user-defined function when the function is invoked with the RSA parameter and takes precedence over the OpenSSL maximum length value.
314434

315435
| Option | Description |
316436
|--------------|------------------|
@@ -325,15 +445,15 @@ The range for this variable is from 1,024 to 16,384. The default value is 16,384
325445

326446
Code examples for the following operations:
327447

328-
* set the threshold variables
448+
* Set the threshold variables
329449

330-
* create a private key
450+
* Create a private key
331451

332-
* create a public key
452+
* Create a public key
333453

334-
* encrypt data
454+
* Encrypt data
335455

336-
* decrypt data
456+
* Decrypt data
337457

338458
```{.bash data-prompt="mysql>"}
339459
-- Set Global variable
@@ -360,11 +480,11 @@ mysql> SET @plaintext = asymmetric_decrypt('RSA', @ciphertext, @public_key);
360480

361481
Code examples for the following operations:
362482

363-
* generate a digest string
483+
* Generate a digest string
364484

365-
* generate a digest signature
485+
* Generate a digest signature
366486

367-
* verify the signature against the digest
487+
* Verify the signature against the digest
368488

369489
```{.bash data-prompt="mysql>"}
370490
-- Generate a digest string
@@ -380,13 +500,13 @@ mysql> SET @verify_signature = asymmetric_verify('RSA', @digest, @signature, @pu
380500

381501
Code examples for the following operations:
382502

383-
* generate a DH parameter
503+
* Generate a DH parameter
384504

385-
* generates two DH key pairs
505+
* Generates two DH key pairs
386506

387-
* generate a symmetric key using the public_1 and the private_2
507+
* Generate a symmetric key using the public_1 and the private_2
388508

389-
* generate a symmetric key using the public_2 and the private_1
509+
* Generate a symmetric key using the public_2 and the private_1
390510

391511
```{.bash data-prompt="mysql>"}
392512
-- Generate a DH parameter
@@ -409,11 +529,11 @@ mysql> SET symmetric_2 = asymmetric_derive(@public_2, @private_1);
409529

410530
Code examples for the following operations:
411531

412-
* create a private key using a `SET` statement
532+
* Create a private key using a `SET` statement
413533

414-
* create a private key using a `SELECT` statement
534+
* Create a private key using a `SELECT` statement
415535

416-
* create a private key using an `INSERT` statement
536+
* Create a private key using an `INSERT` statement
417537

418538
```{.bash data-prompt="mysql>"}
419539
mysql> SET @private_key1 = create_asymmetric_priv_key('RSA', 3072);

0 commit comments

Comments
 (0)
Please sign in to comment.