summaryrefslogtreecommitdiff
path: root/vendor/thecodingmachine/safe/generated/sodium.php
blob: d13da0da9381583c735d4d12a393dd320ae80591 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
<?php

namespace Safe;

use Safe\Exceptions\SodiumException;

/**
 * Verify then decrypt with AES-256-GCM.
 * Only available if sodium_crypto_aead_aes256gcm_is_available returns TRUE.
 *
 * @param string $ciphertext Must be in the format provided by sodium_crypto_aead_aes256gcm_encrypt
 * (ciphertext and tag, concatenated).
 * @param string $additional_data Additional, authenticated data. This is used in the verification of the authentication tag
 * appended to the ciphertext, but it is not encrypted or stored in the ciphertext.
 * @param string $nonce A number that must be only used once, per message. 12 bytes long.
 * @param string $key Encryption key (256-bit).
 * @return string Returns the plaintext on success.
 * @throws SodiumException
 *
 */
function sodium_crypto_aead_aes256gcm_decrypt(string $ciphertext, string $additional_data, string $nonce, string $key): string
{
    error_clear_last();
    $result = \sodium_crypto_aead_aes256gcm_decrypt($ciphertext, $additional_data, $nonce, $key);
    if ($result === false) {
        throw SodiumException::createFromPhpError();
    }
    return $result;
}


/**
 * Verify then decrypt with ChaCha20-Poly1305.
 *
 * @param string $ciphertext Must be in the format provided by sodium_crypto_aead_chacha20poly1305_encrypt
 * (ciphertext and tag, concatenated).
 * @param string $additional_data Additional, authenticated data. This is used in the verification of the authentication tag
 * appended to the ciphertext, but it is not encrypted or stored in the ciphertext.
 * @param string $nonce A number that must be only used once, per message. 8 bytes long.
 * @param string $key Encryption key (256-bit).
 * @return string Returns the plaintext on success.
 * @throws SodiumException
 *
 */
function sodium_crypto_aead_chacha20poly1305_decrypt(string $ciphertext, string $additional_data, string $nonce, string $key): string
{
    error_clear_last();
    $result = \sodium_crypto_aead_chacha20poly1305_decrypt($ciphertext, $additional_data, $nonce, $key);
    if ($result === false) {
        throw SodiumException::createFromPhpError();
    }
    return $result;
}


/**
 * Encrypt then authenticate with ChaCha20-Poly1305.
 *
 * @param string $message The plaintext message to encrypt.
 * @param string $additional_data Additional, authenticated data. This is used in the verification of the authentication tag
 * appended to the ciphertext, but it is not encrypted or stored in the ciphertext.
 * @param string $nonce A number that must be only used once, per message. 8 bytes long.
 * @param string $key Encryption key (256-bit).
 * @return string Returns the ciphertext and tag on success.
 * @throws SodiumException
 *
 */
function sodium_crypto_aead_chacha20poly1305_encrypt(string $message, string $additional_data, string $nonce, string $key): string
{
    error_clear_last();
    $result = \sodium_crypto_aead_chacha20poly1305_encrypt($message, $additional_data, $nonce, $key);
    if ($result === false) {
        throw SodiumException::createFromPhpError();
    }
    return $result;
}


/**
 * Verify then decrypt with ChaCha20-Poly1305 (IETF variant).
 *
 * The IETF variant uses 96-bit nonces and 32-bit internal counters, instead of 64-bit for both.
 *
 * @param string $ciphertext Must be in the format provided by sodium_crypto_aead_chacha20poly1305_ietf_encrypt
 * (ciphertext and tag, concatenated).
 * @param string $additional_data Additional, authenticated data. This is used in the verification of the authentication tag
 * appended to the ciphertext, but it is not encrypted or stored in the ciphertext.
 * @param string $nonce A number that must be only used once, per message. 12 bytes long.
 * @param string $key Encryption key (256-bit).
 * @return string Returns the plaintext on success.
 * @throws SodiumException
 *
 */
function sodium_crypto_aead_chacha20poly1305_ietf_decrypt(string $ciphertext, string $additional_data, string $nonce, string $key): string
{
    error_clear_last();
    $result = \sodium_crypto_aead_chacha20poly1305_ietf_decrypt($ciphertext, $additional_data, $nonce, $key);
    if ($result === false) {
        throw SodiumException::createFromPhpError();
    }
    return $result;
}


/**
 * Encrypt then authenticate with ChaCha20-Poly1305 (IETF variant).
 *
 * The IETF variant uses 96-bit nonces and 32-bit internal counters, instead of 64-bit for both.
 *
 * @param string $message The plaintext message to encrypt.
 * @param string $additional_data Additional, authenticated data. This is used in the verification of the authentication tag
 * appended to the ciphertext, but it is not encrypted or stored in the ciphertext.
 * @param string $nonce A number that must be only used once, per message. 12 bytes long.
 * @param string $key Encryption key (256-bit).
 * @return string Returns the ciphertext and tag on success.
 * @throws SodiumException
 *
 */
function sodium_crypto_aead_chacha20poly1305_ietf_encrypt(string $message, string $additional_data, string $nonce, string $key): string
{
    error_clear_last();
    $result = \sodium_crypto_aead_chacha20poly1305_ietf_encrypt($message, $additional_data, $nonce, $key);
    if ($result === false) {
        throw SodiumException::createFromPhpError();
    }
    return $result;
}


/**
 * Verify then decrypt with ChaCha20-Poly1305 (eXtended-nonce variant).
 *
 * Generally, XChaCha20-Poly1305 is the best of the provided AEAD modes to use.
 *
 * @param string $ciphertext Must be in the format provided by sodium_crypto_aead_chacha20poly1305_ietf_encrypt
 * (ciphertext and tag, concatenated).
 * @param string $additional_data Additional, authenticated data. This is used in the verification of the authentication tag
 * appended to the ciphertext, but it is not encrypted or stored in the ciphertext.
 * @param string $nonce A number that must be only used once, per message. 24 bytes long.
 * This is a large enough bound to generate randomly (i.e. random_bytes).
 * @param string $key Encryption key (256-bit).
 * @return string Returns the plaintext on success.
 * @throws SodiumException
 *
 */
function sodium_crypto_aead_xchacha20poly1305_ietf_decrypt(string $ciphertext, string $additional_data, string $nonce, string $key): string
{
    error_clear_last();
    $result = \sodium_crypto_aead_xchacha20poly1305_ietf_decrypt($ciphertext, $additional_data, $nonce, $key);
    if ($result === false) {
        throw SodiumException::createFromPhpError();
    }
    return $result;
}


/**
 * Encrypt then authenticate with XChaCha20-Poly1305 (eXtended-nonce variant).
 *
 * Generally, XChaCha20-Poly1305 is the best of the provided AEAD modes to use.
 *
 * @param string $message The plaintext message to encrypt.
 * @param string $additional_data Additional, authenticated data. This is used in the verification of the authentication tag
 * appended to the ciphertext, but it is not encrypted or stored in the ciphertext.
 * @param string $nonce A number that must be only used once, per message. 24 bytes long.
 * This is a large enough bound to generate randomly (i.e. random_bytes).
 * @param string $key Encryption key (256-bit).
 * @return string Returns the ciphertext and tag on success.
 * @throws SodiumException
 *
 */
function sodium_crypto_aead_xchacha20poly1305_ietf_encrypt(string $message, string $additional_data, string $nonce, string $key): string
{
    error_clear_last();
    $result = \sodium_crypto_aead_xchacha20poly1305_ietf_encrypt($message, $additional_data, $nonce, $key);
    if ($result === false) {
        throw SodiumException::createFromPhpError();
    }
    return $result;
}


/**
 * Verify the authentication tag is valid for a given message and key.
 *
 * Unlike with digital signatures (e.g. sodium_crypto_sign_verify_detached),
 * any party capable of verifying a message is also capable of authenticating
 * their own messages. (Hence, symmetric authentication.)
 *
 * @param string $mac Authentication tag produced by sodium_crypto_auth
 * @param string $message Message
 * @param string $key Authentication key
 * @throws SodiumException
 *
 */
function sodium_crypto_auth_verify(string $mac, string $message, string $key): void
{
    error_clear_last();
    $result = \sodium_crypto_auth_verify($mac, $message, $key);
    if ($result === false) {
        throw SodiumException::createFromPhpError();
    }
}


/**
 * Decrypt a message using asymmetric (public key) cryptography.
 *
 * @param string $ciphertext The encrypted message to attempt to decrypt.
 * @param string $nonce A number that must be only used once, per message. 24 bytes long.
 * This is a large enough bound to generate randomly (i.e. random_bytes).
 * @param string $key_pair See sodium_crypto_box_keypair_from_secretkey_and_publickey.
 * This should include the sender's public key and the recipient's secret key.
 * @return string Returns the plaintext message on success.
 * @throws SodiumException
 *
 */
function sodium_crypto_box_open(string $ciphertext, string $nonce, string $key_pair): string
{
    error_clear_last();
    $result = \sodium_crypto_box_open($ciphertext, $nonce, $key_pair);
    if ($result === false) {
        throw SodiumException::createFromPhpError();
    }
    return $result;
}


/**
 * Decrypt a message that was encrypted with sodium_crypto_box_seal
 *
 * @param string $ciphertext The encrypted message
 * @param string $key_pair The keypair of the recipient. Must include the secret key.
 * @return string The plaintext on success.
 * @throws SodiumException
 *
 */
function sodium_crypto_box_seal_open(string $ciphertext, string $key_pair): string
{
    error_clear_last();
    $result = \sodium_crypto_box_seal_open($ciphertext, $key_pair);
    if ($result === false) {
        throw SodiumException::createFromPhpError();
    }
    return $result;
}


/**
 * Appends a message to the internal hash state.
 *
 * @param string $state The return value of sodium_crypto_generichash_init.
 * @param string $message Data to append to the hashing state.
 * @throws SodiumException
 *
 */
function sodium_crypto_generichash_update(string &$state, string $message): void
{
    error_clear_last();
    $result = \sodium_crypto_generichash_update($state, $message);
    if ($result === false) {
        throw SodiumException::createFromPhpError();
    }
}


/**
 * Decrypt an encrypted message with a symmetric (shared) key.
 *
 * @param string $ciphertext Must be in the format provided by sodium_crypto_secretbox
 * (ciphertext and tag, concatenated).
 * @param string $nonce A number that must be only used once, per message. 24 bytes long.
 * This is a large enough bound to generate randomly (i.e. random_bytes).
 * @param string $key Encryption key (256-bit).
 * @return string The decrypted string on success.
 * @throws SodiumException
 *
 */
function sodium_crypto_secretbox_open(string $ciphertext, string $nonce, string $key): string
{
    error_clear_last();
    $result = \sodium_crypto_secretbox_open($ciphertext, $nonce, $key);
    if ($result === false) {
        throw SodiumException::createFromPhpError();
    }
    return $result;
}


/**
 * Verify the signature attached to a message and return the message
 *
 * @param string $signed_message A message signed with sodium_crypto_sign
 * @param string $public_key An Ed25519 public key
 * @return string Returns the original signed message on success.
 * @throws SodiumException
 *
 */
function sodium_crypto_sign_open(string $signed_message, string $public_key): string
{
    error_clear_last();
    $result = \sodium_crypto_sign_open($signed_message, $public_key);
    if ($result === false) {
        throw SodiumException::createFromPhpError();
    }
    return $result;
}


/**
 * Verify signature for the message
 *
 * @param string $signature The cryptographic signature obtained from sodium_crypto_sign_detached
 * @param string $message The message being verified
 * @param string $public_key Ed25519 public key
 * @throws SodiumException
 *
 */
function sodium_crypto_sign_verify_detached(string $signature, string $message, string $public_key): void
{
    error_clear_last();
    $result = \sodium_crypto_sign_verify_detached($signature, $message, $public_key);
    if ($result === false) {
        throw SodiumException::createFromPhpError();
    }
}