Completed
Push — develop ( f0d100...266ee2 )
by J.D.
04:14
created
src/library/sodium_compat/namespaced/Core/X25519.php 1 patch
Braces   +1 added lines, -2 removed lines patch added patch discarded remove patch
@@ -1,7 +1,6 @@
 block discarded – undo
1 1
 <?php
2 2
 namespace ParagonIE\Sodium\Core;
3 3
 
4
-class X25519 extends \ParagonIE_Sodium_Core_X25519
5
-{
4
+class X25519 extends \ParagonIE_Sodium_Core_X25519 {
6 5
 
7 6
 }
Please login to merge, or discard this patch.
src/library/sodium_compat/namespaced/Core/ChaCha20/IetfCtx.php 1 patch
Braces   +1 added lines, -2 removed lines patch added patch discarded remove patch
@@ -1,7 +1,6 @@
 block discarded – undo
1 1
 <?php
2 2
 namespace ParagonIE\Sodium\Core\ChaCha20;
3 3
 
4
-class IetfCtx extends \ParagonIE_Sodium_Core_ChaCha20_IetfCtx
5
-{
4
+class IetfCtx extends \ParagonIE_Sodium_Core_ChaCha20_IetfCtx {
6 5
 
7 6
 }
Please login to merge, or discard this patch.
src/library/sodium_compat/namespaced/Core/ChaCha20/Ctx.php 1 patch
Braces   +1 added lines, -2 removed lines patch added patch discarded remove patch
@@ -1,7 +1,6 @@
 block discarded – undo
1 1
 <?php
2 2
 namespace ParagonIE\Sodium\Core\ChaCha20;
3 3
 
4
-class Ctx extends \ParagonIE_Sodium_Core_ChaCha20_Ctx
5
-{
4
+class Ctx extends \ParagonIE_Sodium_Core_ChaCha20_Ctx {
6 5
 
7 6
 }
Please login to merge, or discard this patch.
src/library/sodium_compat/namespaced/Core/BLAKE2b.php 1 patch
Braces   +1 added lines, -2 removed lines patch added patch discarded remove patch
@@ -1,7 +1,6 @@
 block discarded – undo
1 1
 <?php
2 2
 namespace ParagonIE\Sodium\Core;
3 3
 
4
-class BLAKE2b extends \ParagonIE_Sodium_Core_BLAKE2b
5
-{
4
+class BLAKE2b extends \ParagonIE_Sodium_Core_BLAKE2b {
6 5
 
7 6
 }
Please login to merge, or discard this patch.
src/library/sodium_compat/namespaced/Core/HChaCha20.php 1 patch
Braces   +1 added lines, -2 removed lines patch added patch discarded remove patch
@@ -1,7 +1,6 @@
 block discarded – undo
1 1
 <?php
2 2
 namespace ParagonIE\Sodium\Core;
3 3
 
4
-class HChaCha20 extends \ParagonIE_Sodium_Core_HChaCha20
5
-{
4
+class HChaCha20 extends \ParagonIE_Sodium_Core_HChaCha20 {
6 5
 
7 6
 }
Please login to merge, or discard this patch.
src/library/sodium_compat/src/Crypto32.php 3 patches
Indentation   +1158 added lines, -1158 removed lines patch added patch discarded remove patch
@@ -1,7 +1,7 @@  discard block
 block discarded – undo
1 1
 <?php
2 2
 
3 3
 if (class_exists('ParagonIE_Sodium_Crypto32', false)) {
4
-    return;
4
+	return;
5 5
 }
6 6
 
7 7
 /**
@@ -14,1161 +14,1161 @@  discard block
 block discarded – undo
14 14
  */
15 15
 abstract class ParagonIE_Sodium_Crypto32
16 16
 {
17
-    const aead_chacha20poly1305_KEYBYTES = 32;
18
-    const aead_chacha20poly1305_NSECBYTES = 0;
19
-    const aead_chacha20poly1305_NPUBBYTES = 8;
20
-    const aead_chacha20poly1305_ABYTES = 16;
21
-
22
-    const aead_chacha20poly1305_IETF_KEYBYTES = 32;
23
-    const aead_chacha20poly1305_IETF_NSECBYTES = 0;
24
-    const aead_chacha20poly1305_IETF_NPUBBYTES = 12;
25
-    const aead_chacha20poly1305_IETF_ABYTES = 16;
26
-
27
-    const aead_xchacha20poly1305_IETF_KEYBYTES = 32;
28
-    const aead_xchacha20poly1305_IETF_NSECBYTES = 0;
29
-    const aead_xchacha20poly1305_IETF_NPUBBYTES = 24;
30
-    const aead_xchacha20poly1305_IETF_ABYTES = 16;
31
-
32
-    const box_curve25519xsalsa20poly1305_SEEDBYTES = 32;
33
-    const box_curve25519xsalsa20poly1305_PUBLICKEYBYTES = 32;
34
-    const box_curve25519xsalsa20poly1305_SECRETKEYBYTES = 32;
35
-    const box_curve25519xsalsa20poly1305_BEFORENMBYTES = 32;
36
-    const box_curve25519xsalsa20poly1305_NONCEBYTES = 24;
37
-    const box_curve25519xsalsa20poly1305_MACBYTES = 16;
38
-    const box_curve25519xsalsa20poly1305_BOXZEROBYTES = 16;
39
-    const box_curve25519xsalsa20poly1305_ZEROBYTES = 32;
40
-
41
-    const onetimeauth_poly1305_BYTES = 16;
42
-    const onetimeauth_poly1305_KEYBYTES = 32;
43
-
44
-    const secretbox_xsalsa20poly1305_KEYBYTES = 32;
45
-    const secretbox_xsalsa20poly1305_NONCEBYTES = 24;
46
-    const secretbox_xsalsa20poly1305_MACBYTES = 16;
47
-    const secretbox_xsalsa20poly1305_BOXZEROBYTES = 16;
48
-    const secretbox_xsalsa20poly1305_ZEROBYTES = 32;
49
-
50
-    const secretbox_xchacha20poly1305_KEYBYTES = 32;
51
-    const secretbox_xchacha20poly1305_NONCEBYTES = 24;
52
-    const secretbox_xchacha20poly1305_MACBYTES = 16;
53
-    const secretbox_xchacha20poly1305_BOXZEROBYTES = 16;
54
-    const secretbox_xchacha20poly1305_ZEROBYTES = 32;
55
-
56
-    const stream_salsa20_KEYBYTES = 32;
57
-
58
-    /**
59
-     * AEAD Decryption with ChaCha20-Poly1305
60
-     *
61
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
62
-     *
63
-     * @param string $message
64
-     * @param string $ad
65
-     * @param string $nonce
66
-     * @param string $key
67
-     * @return string
68
-     * @throws Error
69
-     */
70
-    public static function aead_chacha20poly1305_decrypt(
71
-        $message = '',
72
-        $ad = '',
73
-        $nonce = '',
74
-        $key = ''
75
-    ) {
76
-        /** @var int $len - Length of message (ciphertext + MAC) */
77
-        $len = ParagonIE_Sodium_Core32_Util::strlen($message);
78
-
79
-        /** @var int  $clen - Length of ciphertext */
80
-        $clen = $len - self::aead_chacha20poly1305_ABYTES;
81
-
82
-        /** @var int $adlen - Length of associated data */
83
-        $adlen = ParagonIE_Sodium_Core32_Util::strlen($ad);
84
-
85
-        /** @var string $mac - Message authentication code */
86
-        $mac = ParagonIE_Sodium_Core32_Util::substr(
87
-            $message,
88
-            $clen,
89
-            self::aead_chacha20poly1305_ABYTES
90
-        );
91
-
92
-        /** @var string $ciphertext - The encrypted message (sans MAC) */
93
-        $ciphertext = ParagonIE_Sodium_Core32_Util::substr($message, 0, $clen);
94
-
95
-        /** @var string The first block of the chacha20 keystream, used as a poly1305 key */
96
-        $block0 = ParagonIE_Sodium_Core32_ChaCha20::stream(
97
-            32,
98
-            $nonce,
99
-            $key
100
-        );
101
-
102
-        /* Recalculate the Poly1305 authentication tag (MAC): */
103
-        $state = new ParagonIE_Sodium_Core32_Poly1305_State($block0);
104
-        try {
105
-            ParagonIE_Sodium_Compat::memzero($block0);
106
-        } catch (Error $ex) {
107
-            $block0 = null;
108
-        }
109
-        $state->update($ad);
110
-        $state->update(ParagonIE_Sodium_Core32_Util::store64_le($adlen));
111
-        $state->update($ciphertext);
112
-        $state->update(ParagonIE_Sodium_Core32_Util::store64_le($clen));
113
-        $computed_mac = $state->finish();
114
-
115
-        /* Compare the given MAC with the recalculated MAC: */
116
-        if (!ParagonIE_Sodium_Core32_Util::verify_16($computed_mac, $mac)) {
117
-            throw new Error('Invalid MAC');
118
-        }
119
-
120
-        // Here, we know that the MAC is valid, so we decrypt and return the plaintext
121
-        return ParagonIE_Sodium_Core32_ChaCha20::streamXorIc(
122
-            $ciphertext,
123
-            $nonce,
124
-            $key,
125
-            ParagonIE_Sodium_Core32_Util::store64_le(1)
126
-        );
127
-    }
128
-
129
-    /**
130
-     * AEAD Encryption with ChaCha20-Poly1305
131
-     *
132
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
133
-     *
134
-     * @param string $message
135
-     * @param string $ad
136
-     * @param string $nonce
137
-     * @param string $key
138
-     * @return string
139
-     */
140
-    public static function aead_chacha20poly1305_encrypt(
141
-        $message = '',
142
-        $ad = '',
143
-        $nonce = '',
144
-        $key = ''
145
-    ) {
146
-        /** @var int $len - Length of the plaintext message */
147
-        $len = ParagonIE_Sodium_Core32_Util::strlen($message);
148
-
149
-        /** @var int $adlen - Length of the associated data */
150
-        $adlen = ParagonIE_Sodium_Core32_Util::strlen($ad);
151
-
152
-        /** @var string The first block of the chacha20 keystream, used as a poly1305 key */
153
-        $block0 = ParagonIE_Sodium_Core32_ChaCha20::stream(
154
-            32,
155
-            $nonce,
156
-            $key
157
-        );
158
-        $state = new ParagonIE_Sodium_Core32_Poly1305_State($block0);
159
-        try {
160
-            ParagonIE_Sodium_Compat::memzero($block0);
161
-        } catch (Error $ex) {
162
-            $block0 = null;
163
-        }
164
-
165
-        /** @var string $ciphertext - Raw encrypted data */
166
-        $ciphertext = ParagonIE_Sodium_Core32_ChaCha20::streamXorIc(
167
-            $message,
168
-            $nonce,
169
-            $key,
170
-            ParagonIE_Sodium_Core32_Util::store64_le(1)
171
-        );
172
-
173
-        $state->update($ad);
174
-        $state->update(ParagonIE_Sodium_Core32_Util::store64_le($adlen));
175
-        $state->update($ciphertext);
176
-        $state->update(ParagonIE_Sodium_Core32_Util::store64_le($len));
177
-        return $ciphertext . $state->finish();
178
-    }
179
-
180
-    /**
181
-     * AEAD Decryption with ChaCha20-Poly1305, IETF mode (96-bit nonce)
182
-     *
183
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
184
-     *
185
-     * @param string $message
186
-     * @param string $ad
187
-     * @param string $nonce
188
-     * @param string $key
189
-     * @return string
190
-     * @throws Error
191
-     */
192
-    public static function aead_chacha20poly1305_ietf_decrypt(
193
-        $message = '',
194
-        $ad = '',
195
-        $nonce = '',
196
-        $key = ''
197
-    ) {
198
-        /** @var int $adlen - Length of associated data */
199
-        $adlen = ParagonIE_Sodium_Core32_Util::strlen($ad);
200
-
201
-        /** @var int $len - Length of message (ciphertext + MAC) */
202
-        $len = ParagonIE_Sodium_Core32_Util::strlen($message);
203
-
204
-        /** @var int  $clen - Length of ciphertext */
205
-        $clen = $len - self::aead_chacha20poly1305_IETF_ABYTES;
206
-
207
-        /** @var string The first block of the chacha20 keystream, used as a poly1305 key */
208
-        $block0 = ParagonIE_Sodium_Core32_ChaCha20::ietfStream(
209
-            32,
210
-            $nonce,
211
-            $key
212
-        );
213
-
214
-        /** @var string $mac - Message authentication code */
215
-        $mac = ParagonIE_Sodium_Core32_Util::substr(
216
-            $message,
217
-            $len - self::aead_chacha20poly1305_IETF_ABYTES,
218
-            self::aead_chacha20poly1305_IETF_ABYTES
219
-        );
220
-
221
-        /** @var string $ciphertext - The encrypted message (sans MAC) */
222
-        $ciphertext = ParagonIE_Sodium_Core32_Util::substr(
223
-            $message,
224
-            0,
225
-            $len - self::aead_chacha20poly1305_IETF_ABYTES
226
-        );
227
-
228
-        /* Recalculate the Poly1305 authentication tag (MAC): */
229
-        $state = new ParagonIE_Sodium_Core32_Poly1305_State($block0);
230
-        try {
231
-            ParagonIE_Sodium_Compat::memzero($block0);
232
-        } catch (Error $ex) {
233
-            $block0 = null;
234
-        }
235
-        $state->update($ad);
236
-        $state->update(str_repeat("\x00", ((0x10 - $adlen) & 0xf)));
237
-        $state->update($ciphertext);
238
-        $state->update(str_repeat("\x00", (0x10 - $clen) & 0xf));
239
-        $state->update(ParagonIE_Sodium_Core32_Util::store64_le($adlen));
240
-        $state->update(ParagonIE_Sodium_Core32_Util::store64_le($clen));
241
-        $computed_mac = $state->finish();
242
-
243
-        /* Compare the given MAC with the recalculated MAC: */
244
-        if (!ParagonIE_Sodium_Core32_Util::verify_16($computed_mac, $mac)) {
245
-            throw new Error('Invalid MAC');
246
-        }
247
-
248
-        // Here, we know that the MAC is valid, so we decrypt and return the plaintext
249
-        return ParagonIE_Sodium_Core32_ChaCha20::ietfStreamXorIc(
250
-            $ciphertext,
251
-            $nonce,
252
-            $key,
253
-            ParagonIE_Sodium_Core32_Util::store64_le(1)
254
-        );
255
-    }
256
-
257
-    /**
258
-     * AEAD Encryption with ChaCha20-Poly1305, IETF mode (96-bit nonce)
259
-     *
260
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
261
-     *
262
-     * @param string $message
263
-     * @param string $ad
264
-     * @param string $nonce
265
-     * @param string $key
266
-     * @return string
267
-     */
268
-    public static function aead_chacha20poly1305_ietf_encrypt(
269
-        $message = '',
270
-        $ad = '',
271
-        $nonce = '',
272
-        $key = ''
273
-    ) {
274
-        /** @var int $len - Length of the plaintext message */
275
-        $len = ParagonIE_Sodium_Core32_Util::strlen($message);
276
-
277
-        /** @var int $adlen - Length of the associated data */
278
-        $adlen = ParagonIE_Sodium_Core32_Util::strlen($ad);
279
-
280
-        /** @var string The first block of the chacha20 keystream, used as a poly1305 key */
281
-        $block0 = ParagonIE_Sodium_Core32_ChaCha20::ietfStream(
282
-            32,
283
-            $nonce,
284
-            $key
285
-        );
286
-        $state = new ParagonIE_Sodium_Core32_Poly1305_State($block0);
287
-        try {
288
-            ParagonIE_Sodium_Compat::memzero($block0);
289
-        } catch (Error $ex) {
290
-            $block0 = null;
291
-        }
292
-
293
-        /** @var string $ciphertext - Raw encrypted data */
294
-        $ciphertext = ParagonIE_Sodium_Core32_ChaCha20::ietfStreamXorIc(
295
-            $message,
296
-            $nonce,
297
-            $key,
298
-            ParagonIE_Sodium_Core32_Util::store64_le(1)
299
-        );
300
-
301
-        $state->update($ad);
302
-        $state->update(str_repeat("\x00", ((0x10 - $adlen) & 0xf)));
303
-        $state->update($ciphertext);
304
-        $state->update(str_repeat("\x00", ((0x10 - $len) & 0xf)));
305
-        $state->update(ParagonIE_Sodium_Core32_Util::store64_le($adlen));
306
-        $state->update(ParagonIE_Sodium_Core32_Util::store64_le($len));
307
-        return $ciphertext . $state->finish();
308
-    }
309
-
310
-    /**
311
-     * AEAD Decryption with ChaCha20-Poly1305, IETF mode (96-bit nonce)
312
-     *
313
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
314
-     *
315
-     * @param string $message
316
-     * @param string $ad
317
-     * @param string $nonce
318
-     * @param string $key
319
-     * @return string
320
-     * @throws Error
321
-     */
322
-    public static function aead_xchacha20poly1305_ietf_decrypt(
323
-        $message = '',
324
-        $ad = '',
325
-        $nonce = '',
326
-        $key = ''
327
-    ) {
328
-        $subkey = ParagonIE_Sodium_Core32_HChaCha20::hChaCha20(
329
-            ParagonIE_Sodium_Core32_Util::substr($nonce, 0, 16),
330
-            $key
331
-        );
332
-        $nonceLast = "\x00\x00\x00\x00" .
333
-            ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8);
334
-
335
-        return self::aead_chacha20poly1305_ietf_decrypt($message, $ad, $nonceLast, $subkey);
336
-    }
337
-
338
-    /**
339
-     * AEAD Encryption with ChaCha20-Poly1305, IETF mode (96-bit nonce)
340
-     *
341
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
342
-     *
343
-     * @param string $message
344
-     * @param string $ad
345
-     * @param string $nonce
346
-     * @param string $key
347
-     * @return string
348
-     */
349
-    public static function aead_xchacha20poly1305_ietf_encrypt(
350
-        $message = '',
351
-        $ad = '',
352
-        $nonce = '',
353
-        $key = ''
354
-    ) {
355
-        $subkey = ParagonIE_Sodium_Core32_HChaCha20::hChaCha20(
356
-            ParagonIE_Sodium_Core32_Util::substr($nonce, 0, 16),
357
-            $key
358
-        );
359
-        $nonceLast = "\x00\x00\x00\x00" .
360
-            ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8);
361
-
362
-        return self::aead_chacha20poly1305_ietf_encrypt($message, $ad, $nonceLast, $subkey);
363
-    }
364
-
365
-    /**
366
-     * HMAC-SHA-512-256 (a.k.a. the leftmost 256 bits of HMAC-SHA-512)
367
-     *
368
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
369
-     *
370
-     * @param string $message
371
-     * @param string $key
372
-     * @return string
373
-     */
374
-    public static function auth($message, $key)
375
-    {
376
-        return ParagonIE_Sodium_Core32_Util::substr(
377
-            hash_hmac('sha512', $message, $key, true),
378
-            0,
379
-            32
380
-        );
381
-    }
382
-
383
-    /**
384
-     * HMAC-SHA-512-256 validation. Constant-time via hash_equals().
385
-     *
386
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
387
-     *
388
-     * @param string $mac
389
-     * @param string $message
390
-     * @param string $key
391
-     * @return bool
392
-     */
393
-    public static function auth_verify($mac, $message, $key)
394
-    {
395
-        return ParagonIE_Sodium_Core32_Util::hashEquals(
396
-            $mac,
397
-            self::auth($message, $key)
398
-        );
399
-    }
400
-
401
-    /**
402
-     * X25519 key exchange followed by XSalsa20Poly1305 symmetric encryption
403
-     *
404
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
405
-     *
406
-     * @param string $plaintext
407
-     * @param string $nonce
408
-     * @param string $keypair
409
-     * @return string
410
-     */
411
-    public static function box($plaintext, $nonce, $keypair)
412
-    {
413
-        $c = self::secretbox(
414
-            $plaintext,
415
-            $nonce,
416
-            self::box_beforenm(
417
-                self::box_secretkey($keypair),
418
-                self::box_publickey($keypair)
419
-            )
420
-        );
421
-        return $c;
422
-    }
423
-
424
-    /**
425
-     * X25519-XSalsa20-Poly1305 with one ephemeral X25519 keypair.
426
-     *
427
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
428
-     *
429
-     * @param string $message
430
-     * @param string $publicKey
431
-     * @return string
432
-     */
433
-    public static function box_seal($message, $publicKey)
434
-    {
435
-        /** @var string $ephemeralKeypair */
436
-        $ephemeralKeypair = self::box_keypair();
437
-
438
-        /** @var string $ephemeralSK */
439
-        $ephemeralSK = self::box_secretkey($ephemeralKeypair);
440
-
441
-        /** @var string $ephemeralPK */
442
-        $ephemeralPK = self::box_publickey($ephemeralKeypair);
443
-
444
-        /** @var string $nonce */
445
-        $nonce = self::generichash(
446
-            $ephemeralPK . $publicKey,
447
-            '',
448
-            24
449
-        );
450
-
451
-        /** @var string $keypair - The combined keypair used in crypto_box() */
452
-        $keypair = self::box_keypair_from_secretkey_and_publickey($ephemeralSK, $publicKey);
453
-
454
-        /** @var string $ciphertext Ciphertext + MAC from crypto_box */
455
-        $ciphertext = self::box($message, $nonce, $keypair);
456
-        try {
457
-            ParagonIE_Sodium_Compat::memzero($ephemeralKeypair);
458
-            ParagonIE_Sodium_Compat::memzero($ephemeralSK);
459
-            ParagonIE_Sodium_Compat::memzero($nonce);
460
-        } catch (Error $ex) {
461
-            $ephemeralKeypair = null;
462
-            $ephemeralSK = null;
463
-            $nonce = null;
464
-        }
465
-        return $ephemeralPK . $ciphertext;
466
-    }
467
-
468
-    /**
469
-     * Opens a message encrypted via box_seal().
470
-     *
471
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
472
-     *
473
-     * @param string $message
474
-     * @param string $keypair
475
-     * @return string
476
-     */
477
-    public static function box_seal_open($message, $keypair)
478
-    {
479
-        /** @var string $ephemeralPK */
480
-        $ephemeralPK = ParagonIE_Sodium_Core32_Util::substr($message, 0, 32);
481
-
482
-        /** @var string $ciphertext (ciphertext + MAC) */
483
-        $ciphertext = ParagonIE_Sodium_Core32_Util::substr($message, 32);
484
-
485
-        /** @var string $secretKey */
486
-        $secretKey = self::box_secretkey($keypair);
487
-
488
-        /** @var string $publicKey */
489
-        $publicKey = self::box_publickey($keypair);
490
-
491
-        /** @var string $nonce */
492
-        $nonce = self::generichash(
493
-            $ephemeralPK . $publicKey,
494
-            '',
495
-            24
496
-        );
497
-
498
-        /** @var string $keypair */
499
-        $keypair = self::box_keypair_from_secretkey_and_publickey($secretKey, $ephemeralPK);
500
-
501
-        /** @var string $m */
502
-        $m = self::box_open($ciphertext, $nonce, $keypair);
503
-        try {
504
-            ParagonIE_Sodium_Compat::memzero($secretKey);
505
-            ParagonIE_Sodium_Compat::memzero($ephemeralPK);
506
-            ParagonIE_Sodium_Compat::memzero($nonce);
507
-        } catch (Error $ex) {
508
-            $secretKey = null;
509
-            $ephemeralPK = null;
510
-            $nonce = null;
511
-        }
512
-        return $m;
513
-    }
514
-
515
-    /**
516
-     * Used by crypto_box() to get the crypto_secretbox() key.
517
-     *
518
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
519
-     *
520
-     * @param string $sk
521
-     * @param string $pk
522
-     * @return string
523
-     */
524
-    public static function box_beforenm($sk, $pk)
525
-    {
526
-        return ParagonIE_Sodium_Core32_HSalsa20::hsalsa20(
527
-            str_repeat("\x00", 16),
528
-            self::scalarmult($sk, $pk)
529
-        );
530
-    }
531
-
532
-    /**
533
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
534
-     *
535
-     * @return string
536
-     */
537
-    public static function box_keypair()
538
-    {
539
-        $sKey = random_bytes(32);
540
-        $pKey = self::scalarmult_base($sKey);
541
-        return $sKey . $pKey;
542
-    }
543
-
544
-    /**
545
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
546
-     *
547
-     * @param string $sKey
548
-     * @param string $pKey
549
-     * @return string
550
-     */
551
-    public static function box_keypair_from_secretkey_and_publickey($sKey, $pKey)
552
-    {
553
-        return ParagonIE_Sodium_Core32_Util::substr($sKey, 0, 32) .
554
-            ParagonIE_Sodium_Core32_Util::substr($pKey, 0, 32);
555
-    }
556
-
557
-    /**
558
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
559
-     *
560
-     * @param string $keypair
561
-     * @return string
562
-     * @throws RangeException
563
-     */
564
-    public static function box_secretkey($keypair)
565
-    {
566
-        if (ParagonIE_Sodium_Core32_Util::strlen($keypair) !== 64) {
567
-            throw new RangeException('Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES bytes long.');
568
-        }
569
-        return ParagonIE_Sodium_Core32_Util::substr($keypair, 0, 32);
570
-    }
571
-
572
-    /**
573
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
574
-     *
575
-     * @param string $keypair
576
-     * @return string
577
-     * @throws RangeException
578
-     */
579
-    public static function box_publickey($keypair)
580
-    {
581
-        if (ParagonIE_Sodium_Core32_Util::strlen($keypair) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES) {
582
-            throw new RangeException('Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES bytes long.');
583
-        }
584
-        return ParagonIE_Sodium_Core32_Util::substr($keypair, 32, 32);
585
-    }
586
-
587
-    /**
588
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
589
-     *
590
-     * @param string $sKey
591
-     * @return string
592
-     * @throws RangeException
593
-     */
594
-    public static function box_publickey_from_secretkey($sKey)
595
-    {
596
-        if (ParagonIE_Sodium_Core32_Util::strlen($sKey) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_SECRETKEYBYTES) {
597
-            throw new RangeException('Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_SECRETKEYBYTES bytes long.');
598
-        }
599
-        return self::scalarmult_base($sKey);
600
-    }
601
-
602
-    /**
603
-     * Decrypt a message encrypted with box().
604
-     *
605
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
606
-     *
607
-     * @param string $ciphertext
608
-     * @param string $nonce
609
-     * @param string $nonce
610
-     * @param string $keypair
611
-     * @return string
612
-     */
613
-    public static function box_open($ciphertext, $nonce, $keypair)
614
-    {
615
-        return self::secretbox_open(
616
-            $ciphertext,
617
-            $nonce,
618
-            self::box_beforenm(
619
-                self::box_secretkey($keypair),
620
-                self::box_publickey($keypair)
621
-            )
622
-        );
623
-    }
624
-
625
-    /**
626
-     * Calculate a BLAKE2b hash.
627
-     *
628
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
629
-     *
630
-     * @param string $message
631
-     * @param string|null $key
632
-     * @param int $outlen
633
-     * @return string
634
-     * @throws RangeException
635
-     */
636
-    public static function generichash($message, $key = '', $outlen = 32)
637
-    {
638
-        // This ensures that ParagonIE_Sodium_Core32_BLAKE2b::$iv is initialized
639
-        ParagonIE_Sodium_Core32_BLAKE2b::pseudoConstructor();
640
-
641
-        $k = null;
642
-        if (!empty($key)) {
643
-            /** @var SplFixedArray $k */
644
-            $k = ParagonIE_Sodium_Core32_BLAKE2b::stringToSplFixedArray($key);
645
-            if ($k->count() > ParagonIE_Sodium_Core32_BLAKE2b::KEYBYTES) {
646
-                throw new RangeException('Invalid key size');
647
-            }
648
-        }
649
-
650
-        /** @var SplFixedArray $in */
651
-        $in = ParagonIE_Sodium_Core32_BLAKE2b::stringToSplFixedArray($message);
652
-
653
-        /** @var SplFixedArray $ctx */
654
-        $ctx = ParagonIE_Sodium_Core32_BLAKE2b::init($k, $outlen);
655
-        ParagonIE_Sodium_Core32_BLAKE2b::update($ctx, $in, $in->count());
656
-
657
-        /** @var SplFixedArray $out */
658
-        $out = new SplFixedArray($outlen);
659
-        $out = ParagonIE_Sodium_Core32_BLAKE2b::finish($ctx, $out);
660
-
661
-        /** @var array<int, int> */
662
-        $outArray = $out->toArray();
663
-        return ParagonIE_Sodium_Core32_Util::intArrayToString($outArray);
664
-    }
665
-
666
-    /**
667
-     * Finalize a BLAKE2b hashing context, returning the hash.
668
-     *
669
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
670
-     *
671
-     * @param string $ctx
672
-     * @param int $outlen
673
-     * @return string
674
-     * @throws TypeError
675
-     */
676
-    public static function generichash_final($ctx, $outlen = 32)
677
-    {
678
-        if (!is_string($ctx)) {
679
-            throw new TypeError('Context must be a string');
680
-        }
681
-        $out = new SplFixedArray($outlen);
682
-
683
-        /** @var SplFixedArray $context */
684
-        $context = ParagonIE_Sodium_Core32_BLAKE2b::stringToContext($ctx);
685
-
686
-        /** @var SplFixedArray $out */
687
-        $out = ParagonIE_Sodium_Core32_BLAKE2b::finish($context, $out);
688
-
689
-        /** @var array<int, int> */
690
-        $outArray = $out->toArray();
691
-        return ParagonIE_Sodium_Core32_Util::intArrayToString($outArray);
692
-    }
693
-
694
-    /**
695
-     * Initialize a hashing context for BLAKE2b.
696
-     *
697
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
698
-     *
699
-     * @param string $key
700
-     * @param int $outputLength
701
-     * @return string
702
-     * @throws RangeException
703
-     */
704
-    public static function generichash_init($key = '', $outputLength = 32)
705
-    {
706
-        // This ensures that ParagonIE_Sodium_Core32_BLAKE2b::$iv is initialized
707
-        ParagonIE_Sodium_Core32_BLAKE2b::pseudoConstructor();
708
-
709
-        $k = null;
710
-        if (!empty($key)) {
711
-            $k = ParagonIE_Sodium_Core32_BLAKE2b::stringToSplFixedArray($key);
712
-            if ($k->count() > ParagonIE_Sodium_Core32_BLAKE2b::KEYBYTES) {
713
-                throw new RangeException('Invalid key size');
714
-            }
715
-        }
716
-
717
-        /** @var SplFixedArray $ctx */
718
-        $ctx = ParagonIE_Sodium_Core32_BLAKE2b::init($k, $outputLength);
719
-
720
-        return ParagonIE_Sodium_Core32_BLAKE2b::contextToString($ctx);
721
-    }
722
-
723
-    /**
724
-     * Update a hashing context for BLAKE2b with $message
725
-     *
726
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
727
-     *
728
-     * @param string $ctx
729
-     * @param string $message
730
-     * @return string
731
-     */
732
-    public static function generichash_update($ctx, $message)
733
-    {
734
-        // This ensures that ParagonIE_Sodium_Core32_BLAKE2b::$iv is initialized
735
-        ParagonIE_Sodium_Core32_BLAKE2b::pseudoConstructor();
736
-
737
-        /** @var SplFixedArray $context */
738
-        $context = ParagonIE_Sodium_Core32_BLAKE2b::stringToContext($ctx);
739
-
740
-        /** @var SplFixedArray $in */
741
-        $in = ParagonIE_Sodium_Core32_BLAKE2b::stringToSplFixedArray($message);
742
-
743
-        ParagonIE_Sodium_Core32_BLAKE2b::update($context, $in, $in->count());
744
-
745
-        return ParagonIE_Sodium_Core32_BLAKE2b::contextToString($context);
746
-    }
747
-
748
-    /**
749
-     * Libsodium's crypto_kx().
750
-     *
751
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
752
-     *
753
-     * @param string $my_sk
754
-     * @param string $their_pk
755
-     * @param string $client_pk
756
-     * @param string $server_pk
757
-     * @return string
758
-     */
759
-    public static function keyExchange($my_sk, $their_pk, $client_pk, $server_pk)
760
-    {
761
-        return self::generichash(
762
-            self::scalarmult($my_sk, $their_pk) .
763
-            $client_pk .
764
-            $server_pk
765
-        );
766
-    }
767
-
768
-    /**
769
-     * ECDH over Curve25519
770
-     *
771
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
772
-     *
773
-     * @param string $sKey
774
-     * @param string $pKey
775
-     * @return string
776
-     *
777
-     * @throws Error
778
-     */
779
-    public static function scalarmult($sKey, $pKey)
780
-    {
781
-        $q = ParagonIE_Sodium_Core32_X25519::crypto_scalarmult_curve25519_ref10($sKey, $pKey);
782
-        self::scalarmult_throw_if_zero($q);
783
-        return $q;
784
-    }
785
-
786
-    /**
787
-     * ECDH over Curve25519, using the basepoint.
788
-     * Used to get a secret key from a public key.
789
-     *
790
-     * @param string $secret
791
-     * @return string
792
-     *
793
-     * @throws Error
794
-     */
795
-    public static function scalarmult_base($secret)
796
-    {
797
-        $q = ParagonIE_Sodium_Core32_X25519::crypto_scalarmult_curve25519_ref10_base($secret);
798
-        self::scalarmult_throw_if_zero($q);
799
-        return $q;
800
-    }
801
-
802
-    /**
803
-     * This throws an Error if a zero public key was passed to the function.
804
-     *
805
-     * @param string $q
806
-     * @return void
807
-     * @throws Error
808
-     */
809
-    protected static function scalarmult_throw_if_zero($q)
810
-    {
811
-        $d = 0;
812
-        for ($i = 0; $i < self::box_curve25519xsalsa20poly1305_SECRETKEYBYTES; ++$i) {
813
-            $d |= ParagonIE_Sodium_Core32_Util::chrToInt($q[$i]);
814
-        }
815
-
816
-        /* branch-free variant of === 0 */
817
-        if (-(1 & (($d - 1) >> 8))) {
818
-            throw new Error('Zero public key is not allowed');
819
-        }
820
-    }
821
-
822
-    /**
823
-     * XSalsa20-Poly1305 authenticated symmetric-key encryption.
824
-     *
825
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
826
-     *
827
-     * @param string $plaintext
828
-     * @param string $nonce
829
-     * @param string $key
830
-     * @return string
831
-     */
832
-    public static function secretbox($plaintext, $nonce, $key)
833
-    {
834
-        /** @var string $subkey */
835
-        $subkey = ParagonIE_Sodium_Core32_HSalsa20::hsalsa20($nonce, $key);
836
-
837
-        /** @var string $block0 */
838
-        $block0 = str_repeat("\x00", 32);
839
-
840
-        /** @var int $mlen - Length of the plaintext message */
841
-        $mlen = ParagonIE_Sodium_Core32_Util::strlen($plaintext);
842
-        $mlen0 = $mlen;
843
-        if ($mlen0 > 64 - self::secretbox_xsalsa20poly1305_ZEROBYTES) {
844
-            $mlen0 = 64 - self::secretbox_xsalsa20poly1305_ZEROBYTES;
845
-        }
846
-        $block0 .= ParagonIE_Sodium_Core32_Util::substr($plaintext, 0, $mlen0);
847
-
848
-        /** @var string $block0 */
849
-        $block0 = ParagonIE_Sodium_Core32_Salsa20::salsa20_xor(
850
-            $block0,
851
-            ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8),
852
-            $subkey
853
-        );
854
-
855
-        /** @var string $c */
856
-        $c = ParagonIE_Sodium_Core32_Util::substr(
857
-            $block0,
858
-            self::secretbox_xsalsa20poly1305_ZEROBYTES
859
-        );
860
-        if ($mlen > $mlen0) {
861
-            $c .= ParagonIE_Sodium_Core32_Salsa20::salsa20_xor_ic(
862
-                ParagonIE_Sodium_Core32_Util::substr(
863
-                    $plaintext,
864
-                    self::secretbox_xsalsa20poly1305_ZEROBYTES
865
-                ),
866
-                ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8),
867
-                1,
868
-                $subkey
869
-            );
870
-        }
871
-        $state = new ParagonIE_Sodium_Core32_Poly1305_State(
872
-            ParagonIE_Sodium_Core32_Util::substr(
873
-                $block0,
874
-                0,
875
-                self::onetimeauth_poly1305_KEYBYTES
876
-            )
877
-        );
878
-        try {
879
-            ParagonIE_Sodium_Compat::memzero($block0);
880
-            ParagonIE_Sodium_Compat::memzero($subkey);
881
-        } catch (Error $ex) {
882
-            $block0 = null;
883
-            $subkey = null;
884
-        }
885
-
886
-        $state->update($c);
887
-
888
-        /** @var string $c - MAC || ciphertext */
889
-        $c = $state->finish() . $c;
890
-        unset($state);
891
-
892
-        return $c;
893
-    }
894
-
895
-    /**
896
-     * Decrypt a ciphertext generated via secretbox().
897
-     *
898
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
899
-     *
900
-     * @param string $ciphertext
901
-     * @param string $nonce
902
-     * @param string $key
903
-     * @return string
904
-     * @throws Error
905
-     */
906
-    public static function secretbox_open($ciphertext, $nonce, $key)
907
-    {
908
-        /** @var string $mac */
909
-        $mac = ParagonIE_Sodium_Core32_Util::substr(
910
-            $ciphertext,
911
-            0,
912
-            self::secretbox_xsalsa20poly1305_MACBYTES
913
-        );
914
-
915
-        /** @var string $c */
916
-        $c = ParagonIE_Sodium_Core32_Util::substr(
917
-            $ciphertext,
918
-            self::secretbox_xsalsa20poly1305_MACBYTES
919
-        );
920
-
921
-        /** @var int $clen */
922
-        $clen = ParagonIE_Sodium_Core32_Util::strlen($c);
923
-
924
-        /** @var string $subkey */
925
-        $subkey = ParagonIE_Sodium_Core32_HSalsa20::hsalsa20($nonce, $key);
926
-
927
-        /** @var string $block0 */
928
-        $block0 = ParagonIE_Sodium_Core32_Salsa20::salsa20(
929
-            64,
930
-            ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8),
931
-            $subkey
932
-        );
933
-        $verified = ParagonIE_Sodium_Core32_Poly1305::onetimeauth_verify(
934
-            $mac,
935
-            $c,
936
-            ParagonIE_Sodium_Core32_Util::substr($block0, 0, 32)
937
-        );
938
-        if (!$verified) {
939
-            try {
940
-                ParagonIE_Sodium_Compat::memzero($subkey);
941
-            } catch (Error $ex) {
942
-                $subkey = null;
943
-            }
944
-            throw new Error('Invalid MAC');
945
-        }
946
-
947
-        /** @var string $m - Decrypted message */
948
-        $m = ParagonIE_Sodium_Core32_Util::xorStrings(
949
-            ParagonIE_Sodium_Core32_Util::substr($block0, self::secretbox_xsalsa20poly1305_ZEROBYTES),
950
-            ParagonIE_Sodium_Core32_Util::substr($c, 0, self::secretbox_xsalsa20poly1305_ZEROBYTES)
951
-        );
952
-        if ($clen > self::secretbox_xsalsa20poly1305_ZEROBYTES) {
953
-            // We had more than 1 block, so let's continue to decrypt the rest.
954
-            $m .= ParagonIE_Sodium_Core32_Salsa20::salsa20_xor_ic(
955
-                ParagonIE_Sodium_Core32_Util::substr(
956
-                    $c,
957
-                    self::secretbox_xsalsa20poly1305_ZEROBYTES
958
-                ),
959
-                ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8),
960
-                1,
961
-                $subkey
962
-            );
963
-        }
964
-        return $m;
965
-    }
966
-
967
-    /**
968
-     * XChaCha20-Poly1305 authenticated symmetric-key encryption.
969
-     *
970
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
971
-     *
972
-     * @param string $plaintext
973
-     * @param string $nonce
974
-     * @param string $key
975
-     * @return string
976
-     */
977
-    public static function secretbox_xchacha20poly1305($plaintext, $nonce, $key)
978
-    {
979
-        /** @var string $subkey */
980
-        $subkey = ParagonIE_Sodium_Core32_HChaCha20::hChaCha20(
981
-            ParagonIE_Sodium_Core32_Util::substr($nonce, 0, 16),
982
-            $key
983
-        );
984
-        $nonceLast = ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8);
985
-
986
-        /** @var string $block0 */
987
-        $block0 = str_repeat("\x00", 32);
988
-
989
-        /** @var int $mlen - Length of the plaintext message */
990
-        $mlen = ParagonIE_Sodium_Core32_Util::strlen($plaintext);
991
-        $mlen0 = $mlen;
992
-        if ($mlen0 > 64 - self::secretbox_xchacha20poly1305_ZEROBYTES) {
993
-            $mlen0 = 64 - self::secretbox_xchacha20poly1305_ZEROBYTES;
994
-        }
995
-        $block0 .= ParagonIE_Sodium_Core32_Util::substr($plaintext, 0, $mlen0);
996
-
997
-        /** @var string $block0 */
998
-        $block0 = ParagonIE_Sodium_Core32_ChaCha20::streamXorIc(
999
-            $block0,
1000
-            $nonceLast,
1001
-            $subkey
1002
-        );
1003
-
1004
-        /** @var string $c */
1005
-        $c = ParagonIE_Sodium_Core32_Util::substr(
1006
-            $block0,
1007
-            self::secretbox_xchacha20poly1305_ZEROBYTES
1008
-        );
1009
-        if ($mlen > $mlen0) {
1010
-            $c .= ParagonIE_Sodium_Core32_ChaCha20::streamXorIc(
1011
-                ParagonIE_Sodium_Core32_Util::substr(
1012
-                    $plaintext,
1013
-                    self::secretbox_xchacha20poly1305_ZEROBYTES
1014
-                ),
1015
-                $nonceLast,
1016
-                $subkey,
1017
-                ParagonIE_Sodium_Core32_Util::store64_le(1)
1018
-            );
1019
-        }
1020
-        $state = new ParagonIE_Sodium_Core32_Poly1305_State(
1021
-            ParagonIE_Sodium_Core32_Util::substr(
1022
-                $block0,
1023
-                0,
1024
-                self::onetimeauth_poly1305_KEYBYTES
1025
-            )
1026
-        );
1027
-        try {
1028
-            ParagonIE_Sodium_Compat::memzero($block0);
1029
-            ParagonIE_Sodium_Compat::memzero($subkey);
1030
-        } catch (Error $ex) {
1031
-            $block0 = null;
1032
-            $subkey = null;
1033
-        }
1034
-
1035
-        $state->update($c);
1036
-
1037
-        /** @var string $c - MAC || ciphertext */
1038
-        $c = $state->finish() . $c;
1039
-        unset($state);
1040
-
1041
-        return $c;
1042
-    }
1043
-
1044
-    /**
1045
-     * Decrypt a ciphertext generated via secretbox_xchacha20poly1305().
1046
-     *
1047
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
1048
-     *
1049
-     * @param string $ciphertext
1050
-     * @param string $nonce
1051
-     * @param string $key
1052
-     * @return string
1053
-     * @throws Error
1054
-     */
1055
-    public static function secretbox_xchacha20poly1305_open($ciphertext, $nonce, $key)
1056
-    {
1057
-        /** @var string $mac */
1058
-        $mac = ParagonIE_Sodium_Core32_Util::substr(
1059
-            $ciphertext,
1060
-            0,
1061
-            self::secretbox_xchacha20poly1305_MACBYTES
1062
-        );
1063
-
1064
-        /** @var string $c */
1065
-        $c = ParagonIE_Sodium_Core32_Util::substr(
1066
-            $ciphertext,
1067
-            self::secretbox_xchacha20poly1305_MACBYTES
1068
-        );
1069
-
1070
-        /** @var int $clen */
1071
-        $clen = ParagonIE_Sodium_Core32_Util::strlen($c);
1072
-
1073
-        /** @var string $subkey */
1074
-        $subkey = ParagonIE_Sodium_Core32_HChaCha20::hchacha20($nonce, $key);
1075
-
1076
-        /** @var string $block0 */
1077
-        $block0 = ParagonIE_Sodium_Core32_ChaCha20::stream(
1078
-            64,
1079
-            ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8),
1080
-            $subkey
1081
-        );
1082
-        $verified = ParagonIE_Sodium_Core32_Poly1305::onetimeauth_verify(
1083
-            $mac,
1084
-            $c,
1085
-            ParagonIE_Sodium_Core32_Util::substr($block0, 0, 32)
1086
-        );
1087
-
1088
-        if (!$verified) {
1089
-            try {
1090
-                ParagonIE_Sodium_Compat::memzero($subkey);
1091
-            } catch (Error $ex) {
1092
-                $subkey = null;
1093
-            }
1094
-            throw new Error('Invalid MAC');
1095
-        }
1096
-
1097
-        /** @var string $m - Decrypted message */
1098
-        $m = ParagonIE_Sodium_Core32_Util::xorStrings(
1099
-            ParagonIE_Sodium_Core32_Util::substr($block0, self::secretbox_xchacha20poly1305_ZEROBYTES),
1100
-            ParagonIE_Sodium_Core32_Util::substr($c, 0, self::secretbox_xchacha20poly1305_ZEROBYTES)
1101
-        );
1102
-
1103
-        if ($clen > self::secretbox_xchacha20poly1305_ZEROBYTES) {
1104
-            // We had more than 1 block, so let's continue to decrypt the rest.
1105
-            $m .= ParagonIE_Sodium_Core32_ChaCha20::streamXorIc(
1106
-                ParagonIE_Sodium_Core32_Util::substr(
1107
-                    $c,
1108
-                    self::secretbox_xchacha20poly1305_ZEROBYTES
1109
-                ),
1110
-                ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8),
1111
-                $subkey,
1112
-                ParagonIE_Sodium_Core32_Util::store64_le(1)
1113
-            );
1114
-        }
1115
-        return $m;
1116
-    }
1117
-
1118
-    /**
1119
-     * Detached Ed25519 signature.
1120
-     *
1121
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
1122
-     *
1123
-     * @param string $message
1124
-     * @param string $sk
1125
-     * @return string
1126
-     */
1127
-    public static function sign_detached($message, $sk)
1128
-    {
1129
-        return ParagonIE_Sodium_Core32_Ed25519::sign_detached($message, $sk);
1130
-    }
1131
-
1132
-    /**
1133
-     * Attached Ed25519 signature. (Returns a signed message.)
1134
-     *
1135
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
1136
-     *
1137
-     * @param string $message
1138
-     * @param string $sk
1139
-     * @return string
1140
-     */
1141
-    public static function sign($message, $sk)
1142
-    {
1143
-        return ParagonIE_Sodium_Core32_Ed25519::sign($message, $sk);
1144
-    }
1145
-
1146
-    /**
1147
-     * Opens a signed message. If valid, returns the message.
1148
-     *
1149
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
1150
-     *
1151
-     * @param string $signedMessage
1152
-     * @param string $pk
1153
-     * @return string
1154
-     */
1155
-    public static function sign_open($signedMessage, $pk)
1156
-    {
1157
-        return ParagonIE_Sodium_Core32_Ed25519::sign_open($signedMessage, $pk);
1158
-    }
1159
-
1160
-    /**
1161
-     * Verify a detached signature of a given message and public key.
1162
-     *
1163
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
1164
-     *
1165
-     * @param string $signature
1166
-     * @param string $message
1167
-     * @param string $pk
1168
-     * @return bool
1169
-     */
1170
-    public static function sign_verify_detached($signature, $message, $pk)
1171
-    {
1172
-        return ParagonIE_Sodium_Core32_Ed25519::verify_detached($signature, $message, $pk);
1173
-    }
17
+	const aead_chacha20poly1305_KEYBYTES = 32;
18
+	const aead_chacha20poly1305_NSECBYTES = 0;
19
+	const aead_chacha20poly1305_NPUBBYTES = 8;
20
+	const aead_chacha20poly1305_ABYTES = 16;
21
+
22
+	const aead_chacha20poly1305_IETF_KEYBYTES = 32;
23
+	const aead_chacha20poly1305_IETF_NSECBYTES = 0;
24
+	const aead_chacha20poly1305_IETF_NPUBBYTES = 12;
25
+	const aead_chacha20poly1305_IETF_ABYTES = 16;
26
+
27
+	const aead_xchacha20poly1305_IETF_KEYBYTES = 32;
28
+	const aead_xchacha20poly1305_IETF_NSECBYTES = 0;
29
+	const aead_xchacha20poly1305_IETF_NPUBBYTES = 24;
30
+	const aead_xchacha20poly1305_IETF_ABYTES = 16;
31
+
32
+	const box_curve25519xsalsa20poly1305_SEEDBYTES = 32;
33
+	const box_curve25519xsalsa20poly1305_PUBLICKEYBYTES = 32;
34
+	const box_curve25519xsalsa20poly1305_SECRETKEYBYTES = 32;
35
+	const box_curve25519xsalsa20poly1305_BEFORENMBYTES = 32;
36
+	const box_curve25519xsalsa20poly1305_NONCEBYTES = 24;
37
+	const box_curve25519xsalsa20poly1305_MACBYTES = 16;
38
+	const box_curve25519xsalsa20poly1305_BOXZEROBYTES = 16;
39
+	const box_curve25519xsalsa20poly1305_ZEROBYTES = 32;
40
+
41
+	const onetimeauth_poly1305_BYTES = 16;
42
+	const onetimeauth_poly1305_KEYBYTES = 32;
43
+
44
+	const secretbox_xsalsa20poly1305_KEYBYTES = 32;
45
+	const secretbox_xsalsa20poly1305_NONCEBYTES = 24;
46
+	const secretbox_xsalsa20poly1305_MACBYTES = 16;
47
+	const secretbox_xsalsa20poly1305_BOXZEROBYTES = 16;
48
+	const secretbox_xsalsa20poly1305_ZEROBYTES = 32;
49
+
50
+	const secretbox_xchacha20poly1305_KEYBYTES = 32;
51
+	const secretbox_xchacha20poly1305_NONCEBYTES = 24;
52
+	const secretbox_xchacha20poly1305_MACBYTES = 16;
53
+	const secretbox_xchacha20poly1305_BOXZEROBYTES = 16;
54
+	const secretbox_xchacha20poly1305_ZEROBYTES = 32;
55
+
56
+	const stream_salsa20_KEYBYTES = 32;
57
+
58
+	/**
59
+	 * AEAD Decryption with ChaCha20-Poly1305
60
+	 *
61
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
62
+	 *
63
+	 * @param string $message
64
+	 * @param string $ad
65
+	 * @param string $nonce
66
+	 * @param string $key
67
+	 * @return string
68
+	 * @throws Error
69
+	 */
70
+	public static function aead_chacha20poly1305_decrypt(
71
+		$message = '',
72
+		$ad = '',
73
+		$nonce = '',
74
+		$key = ''
75
+	) {
76
+		/** @var int $len - Length of message (ciphertext + MAC) */
77
+		$len = ParagonIE_Sodium_Core32_Util::strlen($message);
78
+
79
+		/** @var int  $clen - Length of ciphertext */
80
+		$clen = $len - self::aead_chacha20poly1305_ABYTES;
81
+
82
+		/** @var int $adlen - Length of associated data */
83
+		$adlen = ParagonIE_Sodium_Core32_Util::strlen($ad);
84
+
85
+		/** @var string $mac - Message authentication code */
86
+		$mac = ParagonIE_Sodium_Core32_Util::substr(
87
+			$message,
88
+			$clen,
89
+			self::aead_chacha20poly1305_ABYTES
90
+		);
91
+
92
+		/** @var string $ciphertext - The encrypted message (sans MAC) */
93
+		$ciphertext = ParagonIE_Sodium_Core32_Util::substr($message, 0, $clen);
94
+
95
+		/** @var string The first block of the chacha20 keystream, used as a poly1305 key */
96
+		$block0 = ParagonIE_Sodium_Core32_ChaCha20::stream(
97
+			32,
98
+			$nonce,
99
+			$key
100
+		);
101
+
102
+		/* Recalculate the Poly1305 authentication tag (MAC): */
103
+		$state = new ParagonIE_Sodium_Core32_Poly1305_State($block0);
104
+		try {
105
+			ParagonIE_Sodium_Compat::memzero($block0);
106
+		} catch (Error $ex) {
107
+			$block0 = null;
108
+		}
109
+		$state->update($ad);
110
+		$state->update(ParagonIE_Sodium_Core32_Util::store64_le($adlen));
111
+		$state->update($ciphertext);
112
+		$state->update(ParagonIE_Sodium_Core32_Util::store64_le($clen));
113
+		$computed_mac = $state->finish();
114
+
115
+		/* Compare the given MAC with the recalculated MAC: */
116
+		if (!ParagonIE_Sodium_Core32_Util::verify_16($computed_mac, $mac)) {
117
+			throw new Error('Invalid MAC');
118
+		}
119
+
120
+		// Here, we know that the MAC is valid, so we decrypt and return the plaintext
121
+		return ParagonIE_Sodium_Core32_ChaCha20::streamXorIc(
122
+			$ciphertext,
123
+			$nonce,
124
+			$key,
125
+			ParagonIE_Sodium_Core32_Util::store64_le(1)
126
+		);
127
+	}
128
+
129
+	/**
130
+	 * AEAD Encryption with ChaCha20-Poly1305
131
+	 *
132
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
133
+	 *
134
+	 * @param string $message
135
+	 * @param string $ad
136
+	 * @param string $nonce
137
+	 * @param string $key
138
+	 * @return string
139
+	 */
140
+	public static function aead_chacha20poly1305_encrypt(
141
+		$message = '',
142
+		$ad = '',
143
+		$nonce = '',
144
+		$key = ''
145
+	) {
146
+		/** @var int $len - Length of the plaintext message */
147
+		$len = ParagonIE_Sodium_Core32_Util::strlen($message);
148
+
149
+		/** @var int $adlen - Length of the associated data */
150
+		$adlen = ParagonIE_Sodium_Core32_Util::strlen($ad);
151
+
152
+		/** @var string The first block of the chacha20 keystream, used as a poly1305 key */
153
+		$block0 = ParagonIE_Sodium_Core32_ChaCha20::stream(
154
+			32,
155
+			$nonce,
156
+			$key
157
+		);
158
+		$state = new ParagonIE_Sodium_Core32_Poly1305_State($block0);
159
+		try {
160
+			ParagonIE_Sodium_Compat::memzero($block0);
161
+		} catch (Error $ex) {
162
+			$block0 = null;
163
+		}
164
+
165
+		/** @var string $ciphertext - Raw encrypted data */
166
+		$ciphertext = ParagonIE_Sodium_Core32_ChaCha20::streamXorIc(
167
+			$message,
168
+			$nonce,
169
+			$key,
170
+			ParagonIE_Sodium_Core32_Util::store64_le(1)
171
+		);
172
+
173
+		$state->update($ad);
174
+		$state->update(ParagonIE_Sodium_Core32_Util::store64_le($adlen));
175
+		$state->update($ciphertext);
176
+		$state->update(ParagonIE_Sodium_Core32_Util::store64_le($len));
177
+		return $ciphertext . $state->finish();
178
+	}
179
+
180
+	/**
181
+	 * AEAD Decryption with ChaCha20-Poly1305, IETF mode (96-bit nonce)
182
+	 *
183
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
184
+	 *
185
+	 * @param string $message
186
+	 * @param string $ad
187
+	 * @param string $nonce
188
+	 * @param string $key
189
+	 * @return string
190
+	 * @throws Error
191
+	 */
192
+	public static function aead_chacha20poly1305_ietf_decrypt(
193
+		$message = '',
194
+		$ad = '',
195
+		$nonce = '',
196
+		$key = ''
197
+	) {
198
+		/** @var int $adlen - Length of associated data */
199
+		$adlen = ParagonIE_Sodium_Core32_Util::strlen($ad);
200
+
201
+		/** @var int $len - Length of message (ciphertext + MAC) */
202
+		$len = ParagonIE_Sodium_Core32_Util::strlen($message);
203
+
204
+		/** @var int  $clen - Length of ciphertext */
205
+		$clen = $len - self::aead_chacha20poly1305_IETF_ABYTES;
206
+
207
+		/** @var string The first block of the chacha20 keystream, used as a poly1305 key */
208
+		$block0 = ParagonIE_Sodium_Core32_ChaCha20::ietfStream(
209
+			32,
210
+			$nonce,
211
+			$key
212
+		);
213
+
214
+		/** @var string $mac - Message authentication code */
215
+		$mac = ParagonIE_Sodium_Core32_Util::substr(
216
+			$message,
217
+			$len - self::aead_chacha20poly1305_IETF_ABYTES,
218
+			self::aead_chacha20poly1305_IETF_ABYTES
219
+		);
220
+
221
+		/** @var string $ciphertext - The encrypted message (sans MAC) */
222
+		$ciphertext = ParagonIE_Sodium_Core32_Util::substr(
223
+			$message,
224
+			0,
225
+			$len - self::aead_chacha20poly1305_IETF_ABYTES
226
+		);
227
+
228
+		/* Recalculate the Poly1305 authentication tag (MAC): */
229
+		$state = new ParagonIE_Sodium_Core32_Poly1305_State($block0);
230
+		try {
231
+			ParagonIE_Sodium_Compat::memzero($block0);
232
+		} catch (Error $ex) {
233
+			$block0 = null;
234
+		}
235
+		$state->update($ad);
236
+		$state->update(str_repeat("\x00", ((0x10 - $adlen) & 0xf)));
237
+		$state->update($ciphertext);
238
+		$state->update(str_repeat("\x00", (0x10 - $clen) & 0xf));
239
+		$state->update(ParagonIE_Sodium_Core32_Util::store64_le($adlen));
240
+		$state->update(ParagonIE_Sodium_Core32_Util::store64_le($clen));
241
+		$computed_mac = $state->finish();
242
+
243
+		/* Compare the given MAC with the recalculated MAC: */
244
+		if (!ParagonIE_Sodium_Core32_Util::verify_16($computed_mac, $mac)) {
245
+			throw new Error('Invalid MAC');
246
+		}
247
+
248
+		// Here, we know that the MAC is valid, so we decrypt and return the plaintext
249
+		return ParagonIE_Sodium_Core32_ChaCha20::ietfStreamXorIc(
250
+			$ciphertext,
251
+			$nonce,
252
+			$key,
253
+			ParagonIE_Sodium_Core32_Util::store64_le(1)
254
+		);
255
+	}
256
+
257
+	/**
258
+	 * AEAD Encryption with ChaCha20-Poly1305, IETF mode (96-bit nonce)
259
+	 *
260
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
261
+	 *
262
+	 * @param string $message
263
+	 * @param string $ad
264
+	 * @param string $nonce
265
+	 * @param string $key
266
+	 * @return string
267
+	 */
268
+	public static function aead_chacha20poly1305_ietf_encrypt(
269
+		$message = '',
270
+		$ad = '',
271
+		$nonce = '',
272
+		$key = ''
273
+	) {
274
+		/** @var int $len - Length of the plaintext message */
275
+		$len = ParagonIE_Sodium_Core32_Util::strlen($message);
276
+
277
+		/** @var int $adlen - Length of the associated data */
278
+		$adlen = ParagonIE_Sodium_Core32_Util::strlen($ad);
279
+
280
+		/** @var string The first block of the chacha20 keystream, used as a poly1305 key */
281
+		$block0 = ParagonIE_Sodium_Core32_ChaCha20::ietfStream(
282
+			32,
283
+			$nonce,
284
+			$key
285
+		);
286
+		$state = new ParagonIE_Sodium_Core32_Poly1305_State($block0);
287
+		try {
288
+			ParagonIE_Sodium_Compat::memzero($block0);
289
+		} catch (Error $ex) {
290
+			$block0 = null;
291
+		}
292
+
293
+		/** @var string $ciphertext - Raw encrypted data */
294
+		$ciphertext = ParagonIE_Sodium_Core32_ChaCha20::ietfStreamXorIc(
295
+			$message,
296
+			$nonce,
297
+			$key,
298
+			ParagonIE_Sodium_Core32_Util::store64_le(1)
299
+		);
300
+
301
+		$state->update($ad);
302
+		$state->update(str_repeat("\x00", ((0x10 - $adlen) & 0xf)));
303
+		$state->update($ciphertext);
304
+		$state->update(str_repeat("\x00", ((0x10 - $len) & 0xf)));
305
+		$state->update(ParagonIE_Sodium_Core32_Util::store64_le($adlen));
306
+		$state->update(ParagonIE_Sodium_Core32_Util::store64_le($len));
307
+		return $ciphertext . $state->finish();
308
+	}
309
+
310
+	/**
311
+	 * AEAD Decryption with ChaCha20-Poly1305, IETF mode (96-bit nonce)
312
+	 *
313
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
314
+	 *
315
+	 * @param string $message
316
+	 * @param string $ad
317
+	 * @param string $nonce
318
+	 * @param string $key
319
+	 * @return string
320
+	 * @throws Error
321
+	 */
322
+	public static function aead_xchacha20poly1305_ietf_decrypt(
323
+		$message = '',
324
+		$ad = '',
325
+		$nonce = '',
326
+		$key = ''
327
+	) {
328
+		$subkey = ParagonIE_Sodium_Core32_HChaCha20::hChaCha20(
329
+			ParagonIE_Sodium_Core32_Util::substr($nonce, 0, 16),
330
+			$key
331
+		);
332
+		$nonceLast = "\x00\x00\x00\x00" .
333
+			ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8);
334
+
335
+		return self::aead_chacha20poly1305_ietf_decrypt($message, $ad, $nonceLast, $subkey);
336
+	}
337
+
338
+	/**
339
+	 * AEAD Encryption with ChaCha20-Poly1305, IETF mode (96-bit nonce)
340
+	 *
341
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
342
+	 *
343
+	 * @param string $message
344
+	 * @param string $ad
345
+	 * @param string $nonce
346
+	 * @param string $key
347
+	 * @return string
348
+	 */
349
+	public static function aead_xchacha20poly1305_ietf_encrypt(
350
+		$message = '',
351
+		$ad = '',
352
+		$nonce = '',
353
+		$key = ''
354
+	) {
355
+		$subkey = ParagonIE_Sodium_Core32_HChaCha20::hChaCha20(
356
+			ParagonIE_Sodium_Core32_Util::substr($nonce, 0, 16),
357
+			$key
358
+		);
359
+		$nonceLast = "\x00\x00\x00\x00" .
360
+			ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8);
361
+
362
+		return self::aead_chacha20poly1305_ietf_encrypt($message, $ad, $nonceLast, $subkey);
363
+	}
364
+
365
+	/**
366
+	 * HMAC-SHA-512-256 (a.k.a. the leftmost 256 bits of HMAC-SHA-512)
367
+	 *
368
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
369
+	 *
370
+	 * @param string $message
371
+	 * @param string $key
372
+	 * @return string
373
+	 */
374
+	public static function auth($message, $key)
375
+	{
376
+		return ParagonIE_Sodium_Core32_Util::substr(
377
+			hash_hmac('sha512', $message, $key, true),
378
+			0,
379
+			32
380
+		);
381
+	}
382
+
383
+	/**
384
+	 * HMAC-SHA-512-256 validation. Constant-time via hash_equals().
385
+	 *
386
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
387
+	 *
388
+	 * @param string $mac
389
+	 * @param string $message
390
+	 * @param string $key
391
+	 * @return bool
392
+	 */
393
+	public static function auth_verify($mac, $message, $key)
394
+	{
395
+		return ParagonIE_Sodium_Core32_Util::hashEquals(
396
+			$mac,
397
+			self::auth($message, $key)
398
+		);
399
+	}
400
+
401
+	/**
402
+	 * X25519 key exchange followed by XSalsa20Poly1305 symmetric encryption
403
+	 *
404
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
405
+	 *
406
+	 * @param string $plaintext
407
+	 * @param string $nonce
408
+	 * @param string $keypair
409
+	 * @return string
410
+	 */
411
+	public static function box($plaintext, $nonce, $keypair)
412
+	{
413
+		$c = self::secretbox(
414
+			$plaintext,
415
+			$nonce,
416
+			self::box_beforenm(
417
+				self::box_secretkey($keypair),
418
+				self::box_publickey($keypair)
419
+			)
420
+		);
421
+		return $c;
422
+	}
423
+
424
+	/**
425
+	 * X25519-XSalsa20-Poly1305 with one ephemeral X25519 keypair.
426
+	 *
427
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
428
+	 *
429
+	 * @param string $message
430
+	 * @param string $publicKey
431
+	 * @return string
432
+	 */
433
+	public static function box_seal($message, $publicKey)
434
+	{
435
+		/** @var string $ephemeralKeypair */
436
+		$ephemeralKeypair = self::box_keypair();
437
+
438
+		/** @var string $ephemeralSK */
439
+		$ephemeralSK = self::box_secretkey($ephemeralKeypair);
440
+
441
+		/** @var string $ephemeralPK */
442
+		$ephemeralPK = self::box_publickey($ephemeralKeypair);
443
+
444
+		/** @var string $nonce */
445
+		$nonce = self::generichash(
446
+			$ephemeralPK . $publicKey,
447
+			'',
448
+			24
449
+		);
450
+
451
+		/** @var string $keypair - The combined keypair used in crypto_box() */
452
+		$keypair = self::box_keypair_from_secretkey_and_publickey($ephemeralSK, $publicKey);
453
+
454
+		/** @var string $ciphertext Ciphertext + MAC from crypto_box */
455
+		$ciphertext = self::box($message, $nonce, $keypair);
456
+		try {
457
+			ParagonIE_Sodium_Compat::memzero($ephemeralKeypair);
458
+			ParagonIE_Sodium_Compat::memzero($ephemeralSK);
459
+			ParagonIE_Sodium_Compat::memzero($nonce);
460
+		} catch (Error $ex) {
461
+			$ephemeralKeypair = null;
462
+			$ephemeralSK = null;
463
+			$nonce = null;
464
+		}
465
+		return $ephemeralPK . $ciphertext;
466
+	}
467
+
468
+	/**
469
+	 * Opens a message encrypted via box_seal().
470
+	 *
471
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
472
+	 *
473
+	 * @param string $message
474
+	 * @param string $keypair
475
+	 * @return string
476
+	 */
477
+	public static function box_seal_open($message, $keypair)
478
+	{
479
+		/** @var string $ephemeralPK */
480
+		$ephemeralPK = ParagonIE_Sodium_Core32_Util::substr($message, 0, 32);
481
+
482
+		/** @var string $ciphertext (ciphertext + MAC) */
483
+		$ciphertext = ParagonIE_Sodium_Core32_Util::substr($message, 32);
484
+
485
+		/** @var string $secretKey */
486
+		$secretKey = self::box_secretkey($keypair);
487
+
488
+		/** @var string $publicKey */
489
+		$publicKey = self::box_publickey($keypair);
490
+
491
+		/** @var string $nonce */
492
+		$nonce = self::generichash(
493
+			$ephemeralPK . $publicKey,
494
+			'',
495
+			24
496
+		);
497
+
498
+		/** @var string $keypair */
499
+		$keypair = self::box_keypair_from_secretkey_and_publickey($secretKey, $ephemeralPK);
500
+
501
+		/** @var string $m */
502
+		$m = self::box_open($ciphertext, $nonce, $keypair);
503
+		try {
504
+			ParagonIE_Sodium_Compat::memzero($secretKey);
505
+			ParagonIE_Sodium_Compat::memzero($ephemeralPK);
506
+			ParagonIE_Sodium_Compat::memzero($nonce);
507
+		} catch (Error $ex) {
508
+			$secretKey = null;
509
+			$ephemeralPK = null;
510
+			$nonce = null;
511
+		}
512
+		return $m;
513
+	}
514
+
515
+	/**
516
+	 * Used by crypto_box() to get the crypto_secretbox() key.
517
+	 *
518
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
519
+	 *
520
+	 * @param string $sk
521
+	 * @param string $pk
522
+	 * @return string
523
+	 */
524
+	public static function box_beforenm($sk, $pk)
525
+	{
526
+		return ParagonIE_Sodium_Core32_HSalsa20::hsalsa20(
527
+			str_repeat("\x00", 16),
528
+			self::scalarmult($sk, $pk)
529
+		);
530
+	}
531
+
532
+	/**
533
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
534
+	 *
535
+	 * @return string
536
+	 */
537
+	public static function box_keypair()
538
+	{
539
+		$sKey = random_bytes(32);
540
+		$pKey = self::scalarmult_base($sKey);
541
+		return $sKey . $pKey;
542
+	}
543
+
544
+	/**
545
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
546
+	 *
547
+	 * @param string $sKey
548
+	 * @param string $pKey
549
+	 * @return string
550
+	 */
551
+	public static function box_keypair_from_secretkey_and_publickey($sKey, $pKey)
552
+	{
553
+		return ParagonIE_Sodium_Core32_Util::substr($sKey, 0, 32) .
554
+			ParagonIE_Sodium_Core32_Util::substr($pKey, 0, 32);
555
+	}
556
+
557
+	/**
558
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
559
+	 *
560
+	 * @param string $keypair
561
+	 * @return string
562
+	 * @throws RangeException
563
+	 */
564
+	public static function box_secretkey($keypair)
565
+	{
566
+		if (ParagonIE_Sodium_Core32_Util::strlen($keypair) !== 64) {
567
+			throw new RangeException('Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES bytes long.');
568
+		}
569
+		return ParagonIE_Sodium_Core32_Util::substr($keypair, 0, 32);
570
+	}
571
+
572
+	/**
573
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
574
+	 *
575
+	 * @param string $keypair
576
+	 * @return string
577
+	 * @throws RangeException
578
+	 */
579
+	public static function box_publickey($keypair)
580
+	{
581
+		if (ParagonIE_Sodium_Core32_Util::strlen($keypair) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES) {
582
+			throw new RangeException('Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES bytes long.');
583
+		}
584
+		return ParagonIE_Sodium_Core32_Util::substr($keypair, 32, 32);
585
+	}
586
+
587
+	/**
588
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
589
+	 *
590
+	 * @param string $sKey
591
+	 * @return string
592
+	 * @throws RangeException
593
+	 */
594
+	public static function box_publickey_from_secretkey($sKey)
595
+	{
596
+		if (ParagonIE_Sodium_Core32_Util::strlen($sKey) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_SECRETKEYBYTES) {
597
+			throw new RangeException('Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_SECRETKEYBYTES bytes long.');
598
+		}
599
+		return self::scalarmult_base($sKey);
600
+	}
601
+
602
+	/**
603
+	 * Decrypt a message encrypted with box().
604
+	 *
605
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
606
+	 *
607
+	 * @param string $ciphertext
608
+	 * @param string $nonce
609
+	 * @param string $nonce
610
+	 * @param string $keypair
611
+	 * @return string
612
+	 */
613
+	public static function box_open($ciphertext, $nonce, $keypair)
614
+	{
615
+		return self::secretbox_open(
616
+			$ciphertext,
617
+			$nonce,
618
+			self::box_beforenm(
619
+				self::box_secretkey($keypair),
620
+				self::box_publickey($keypair)
621
+			)
622
+		);
623
+	}
624
+
625
+	/**
626
+	 * Calculate a BLAKE2b hash.
627
+	 *
628
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
629
+	 *
630
+	 * @param string $message
631
+	 * @param string|null $key
632
+	 * @param int $outlen
633
+	 * @return string
634
+	 * @throws RangeException
635
+	 */
636
+	public static function generichash($message, $key = '', $outlen = 32)
637
+	{
638
+		// This ensures that ParagonIE_Sodium_Core32_BLAKE2b::$iv is initialized
639
+		ParagonIE_Sodium_Core32_BLAKE2b::pseudoConstructor();
640
+
641
+		$k = null;
642
+		if (!empty($key)) {
643
+			/** @var SplFixedArray $k */
644
+			$k = ParagonIE_Sodium_Core32_BLAKE2b::stringToSplFixedArray($key);
645
+			if ($k->count() > ParagonIE_Sodium_Core32_BLAKE2b::KEYBYTES) {
646
+				throw new RangeException('Invalid key size');
647
+			}
648
+		}
649
+
650
+		/** @var SplFixedArray $in */
651
+		$in = ParagonIE_Sodium_Core32_BLAKE2b::stringToSplFixedArray($message);
652
+
653
+		/** @var SplFixedArray $ctx */
654
+		$ctx = ParagonIE_Sodium_Core32_BLAKE2b::init($k, $outlen);
655
+		ParagonIE_Sodium_Core32_BLAKE2b::update($ctx, $in, $in->count());
656
+
657
+		/** @var SplFixedArray $out */
658
+		$out = new SplFixedArray($outlen);
659
+		$out = ParagonIE_Sodium_Core32_BLAKE2b::finish($ctx, $out);
660
+
661
+		/** @var array<int, int> */
662
+		$outArray = $out->toArray();
663
+		return ParagonIE_Sodium_Core32_Util::intArrayToString($outArray);
664
+	}
665
+
666
+	/**
667
+	 * Finalize a BLAKE2b hashing context, returning the hash.
668
+	 *
669
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
670
+	 *
671
+	 * @param string $ctx
672
+	 * @param int $outlen
673
+	 * @return string
674
+	 * @throws TypeError
675
+	 */
676
+	public static function generichash_final($ctx, $outlen = 32)
677
+	{
678
+		if (!is_string($ctx)) {
679
+			throw new TypeError('Context must be a string');
680
+		}
681
+		$out = new SplFixedArray($outlen);
682
+
683
+		/** @var SplFixedArray $context */
684
+		$context = ParagonIE_Sodium_Core32_BLAKE2b::stringToContext($ctx);
685
+
686
+		/** @var SplFixedArray $out */
687
+		$out = ParagonIE_Sodium_Core32_BLAKE2b::finish($context, $out);
688
+
689
+		/** @var array<int, int> */
690
+		$outArray = $out->toArray();
691
+		return ParagonIE_Sodium_Core32_Util::intArrayToString($outArray);
692
+	}
693
+
694
+	/**
695
+	 * Initialize a hashing context for BLAKE2b.
696
+	 *
697
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
698
+	 *
699
+	 * @param string $key
700
+	 * @param int $outputLength
701
+	 * @return string
702
+	 * @throws RangeException
703
+	 */
704
+	public static function generichash_init($key = '', $outputLength = 32)
705
+	{
706
+		// This ensures that ParagonIE_Sodium_Core32_BLAKE2b::$iv is initialized
707
+		ParagonIE_Sodium_Core32_BLAKE2b::pseudoConstructor();
708
+
709
+		$k = null;
710
+		if (!empty($key)) {
711
+			$k = ParagonIE_Sodium_Core32_BLAKE2b::stringToSplFixedArray($key);
712
+			if ($k->count() > ParagonIE_Sodium_Core32_BLAKE2b::KEYBYTES) {
713
+				throw new RangeException('Invalid key size');
714
+			}
715
+		}
716
+
717
+		/** @var SplFixedArray $ctx */
718
+		$ctx = ParagonIE_Sodium_Core32_BLAKE2b::init($k, $outputLength);
719
+
720
+		return ParagonIE_Sodium_Core32_BLAKE2b::contextToString($ctx);
721
+	}
722
+
723
+	/**
724
+	 * Update a hashing context for BLAKE2b with $message
725
+	 *
726
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
727
+	 *
728
+	 * @param string $ctx
729
+	 * @param string $message
730
+	 * @return string
731
+	 */
732
+	public static function generichash_update($ctx, $message)
733
+	{
734
+		// This ensures that ParagonIE_Sodium_Core32_BLAKE2b::$iv is initialized
735
+		ParagonIE_Sodium_Core32_BLAKE2b::pseudoConstructor();
736
+
737
+		/** @var SplFixedArray $context */
738
+		$context = ParagonIE_Sodium_Core32_BLAKE2b::stringToContext($ctx);
739
+
740
+		/** @var SplFixedArray $in */
741
+		$in = ParagonIE_Sodium_Core32_BLAKE2b::stringToSplFixedArray($message);
742
+
743
+		ParagonIE_Sodium_Core32_BLAKE2b::update($context, $in, $in->count());
744
+
745
+		return ParagonIE_Sodium_Core32_BLAKE2b::contextToString($context);
746
+	}
747
+
748
+	/**
749
+	 * Libsodium's crypto_kx().
750
+	 *
751
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
752
+	 *
753
+	 * @param string $my_sk
754
+	 * @param string $their_pk
755
+	 * @param string $client_pk
756
+	 * @param string $server_pk
757
+	 * @return string
758
+	 */
759
+	public static function keyExchange($my_sk, $their_pk, $client_pk, $server_pk)
760
+	{
761
+		return self::generichash(
762
+			self::scalarmult($my_sk, $their_pk) .
763
+			$client_pk .
764
+			$server_pk
765
+		);
766
+	}
767
+
768
+	/**
769
+	 * ECDH over Curve25519
770
+	 *
771
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
772
+	 *
773
+	 * @param string $sKey
774
+	 * @param string $pKey
775
+	 * @return string
776
+	 *
777
+	 * @throws Error
778
+	 */
779
+	public static function scalarmult($sKey, $pKey)
780
+	{
781
+		$q = ParagonIE_Sodium_Core32_X25519::crypto_scalarmult_curve25519_ref10($sKey, $pKey);
782
+		self::scalarmult_throw_if_zero($q);
783
+		return $q;
784
+	}
785
+
786
+	/**
787
+	 * ECDH over Curve25519, using the basepoint.
788
+	 * Used to get a secret key from a public key.
789
+	 *
790
+	 * @param string $secret
791
+	 * @return string
792
+	 *
793
+	 * @throws Error
794
+	 */
795
+	public static function scalarmult_base($secret)
796
+	{
797
+		$q = ParagonIE_Sodium_Core32_X25519::crypto_scalarmult_curve25519_ref10_base($secret);
798
+		self::scalarmult_throw_if_zero($q);
799
+		return $q;
800
+	}
801
+
802
+	/**
803
+	 * This throws an Error if a zero public key was passed to the function.
804
+	 *
805
+	 * @param string $q
806
+	 * @return void
807
+	 * @throws Error
808
+	 */
809
+	protected static function scalarmult_throw_if_zero($q)
810
+	{
811
+		$d = 0;
812
+		for ($i = 0; $i < self::box_curve25519xsalsa20poly1305_SECRETKEYBYTES; ++$i) {
813
+			$d |= ParagonIE_Sodium_Core32_Util::chrToInt($q[$i]);
814
+		}
815
+
816
+		/* branch-free variant of === 0 */
817
+		if (-(1 & (($d - 1) >> 8))) {
818
+			throw new Error('Zero public key is not allowed');
819
+		}
820
+	}
821
+
822
+	/**
823
+	 * XSalsa20-Poly1305 authenticated symmetric-key encryption.
824
+	 *
825
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
826
+	 *
827
+	 * @param string $plaintext
828
+	 * @param string $nonce
829
+	 * @param string $key
830
+	 * @return string
831
+	 */
832
+	public static function secretbox($plaintext, $nonce, $key)
833
+	{
834
+		/** @var string $subkey */
835
+		$subkey = ParagonIE_Sodium_Core32_HSalsa20::hsalsa20($nonce, $key);
836
+
837
+		/** @var string $block0 */
838
+		$block0 = str_repeat("\x00", 32);
839
+
840
+		/** @var int $mlen - Length of the plaintext message */
841
+		$mlen = ParagonIE_Sodium_Core32_Util::strlen($plaintext);
842
+		$mlen0 = $mlen;
843
+		if ($mlen0 > 64 - self::secretbox_xsalsa20poly1305_ZEROBYTES) {
844
+			$mlen0 = 64 - self::secretbox_xsalsa20poly1305_ZEROBYTES;
845
+		}
846
+		$block0 .= ParagonIE_Sodium_Core32_Util::substr($plaintext, 0, $mlen0);
847
+
848
+		/** @var string $block0 */
849
+		$block0 = ParagonIE_Sodium_Core32_Salsa20::salsa20_xor(
850
+			$block0,
851
+			ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8),
852
+			$subkey
853
+		);
854
+
855
+		/** @var string $c */
856
+		$c = ParagonIE_Sodium_Core32_Util::substr(
857
+			$block0,
858
+			self::secretbox_xsalsa20poly1305_ZEROBYTES
859
+		);
860
+		if ($mlen > $mlen0) {
861
+			$c .= ParagonIE_Sodium_Core32_Salsa20::salsa20_xor_ic(
862
+				ParagonIE_Sodium_Core32_Util::substr(
863
+					$plaintext,
864
+					self::secretbox_xsalsa20poly1305_ZEROBYTES
865
+				),
866
+				ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8),
867
+				1,
868
+				$subkey
869
+			);
870
+		}
871
+		$state = new ParagonIE_Sodium_Core32_Poly1305_State(
872
+			ParagonIE_Sodium_Core32_Util::substr(
873
+				$block0,
874
+				0,
875
+				self::onetimeauth_poly1305_KEYBYTES
876
+			)
877
+		);
878
+		try {
879
+			ParagonIE_Sodium_Compat::memzero($block0);
880
+			ParagonIE_Sodium_Compat::memzero($subkey);
881
+		} catch (Error $ex) {
882
+			$block0 = null;
883
+			$subkey = null;
884
+		}
885
+
886
+		$state->update($c);
887
+
888
+		/** @var string $c - MAC || ciphertext */
889
+		$c = $state->finish() . $c;
890
+		unset($state);
891
+
892
+		return $c;
893
+	}
894
+
895
+	/**
896
+	 * Decrypt a ciphertext generated via secretbox().
897
+	 *
898
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
899
+	 *
900
+	 * @param string $ciphertext
901
+	 * @param string $nonce
902
+	 * @param string $key
903
+	 * @return string
904
+	 * @throws Error
905
+	 */
906
+	public static function secretbox_open($ciphertext, $nonce, $key)
907
+	{
908
+		/** @var string $mac */
909
+		$mac = ParagonIE_Sodium_Core32_Util::substr(
910
+			$ciphertext,
911
+			0,
912
+			self::secretbox_xsalsa20poly1305_MACBYTES
913
+		);
914
+
915
+		/** @var string $c */
916
+		$c = ParagonIE_Sodium_Core32_Util::substr(
917
+			$ciphertext,
918
+			self::secretbox_xsalsa20poly1305_MACBYTES
919
+		);
920
+
921
+		/** @var int $clen */
922
+		$clen = ParagonIE_Sodium_Core32_Util::strlen($c);
923
+
924
+		/** @var string $subkey */
925
+		$subkey = ParagonIE_Sodium_Core32_HSalsa20::hsalsa20($nonce, $key);
926
+
927
+		/** @var string $block0 */
928
+		$block0 = ParagonIE_Sodium_Core32_Salsa20::salsa20(
929
+			64,
930
+			ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8),
931
+			$subkey
932
+		);
933
+		$verified = ParagonIE_Sodium_Core32_Poly1305::onetimeauth_verify(
934
+			$mac,
935
+			$c,
936
+			ParagonIE_Sodium_Core32_Util::substr($block0, 0, 32)
937
+		);
938
+		if (!$verified) {
939
+			try {
940
+				ParagonIE_Sodium_Compat::memzero($subkey);
941
+			} catch (Error $ex) {
942
+				$subkey = null;
943
+			}
944
+			throw new Error('Invalid MAC');
945
+		}
946
+
947
+		/** @var string $m - Decrypted message */
948
+		$m = ParagonIE_Sodium_Core32_Util::xorStrings(
949
+			ParagonIE_Sodium_Core32_Util::substr($block0, self::secretbox_xsalsa20poly1305_ZEROBYTES),
950
+			ParagonIE_Sodium_Core32_Util::substr($c, 0, self::secretbox_xsalsa20poly1305_ZEROBYTES)
951
+		);
952
+		if ($clen > self::secretbox_xsalsa20poly1305_ZEROBYTES) {
953
+			// We had more than 1 block, so let's continue to decrypt the rest.
954
+			$m .= ParagonIE_Sodium_Core32_Salsa20::salsa20_xor_ic(
955
+				ParagonIE_Sodium_Core32_Util::substr(
956
+					$c,
957
+					self::secretbox_xsalsa20poly1305_ZEROBYTES
958
+				),
959
+				ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8),
960
+				1,
961
+				$subkey
962
+			);
963
+		}
964
+		return $m;
965
+	}
966
+
967
+	/**
968
+	 * XChaCha20-Poly1305 authenticated symmetric-key encryption.
969
+	 *
970
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
971
+	 *
972
+	 * @param string $plaintext
973
+	 * @param string $nonce
974
+	 * @param string $key
975
+	 * @return string
976
+	 */
977
+	public static function secretbox_xchacha20poly1305($plaintext, $nonce, $key)
978
+	{
979
+		/** @var string $subkey */
980
+		$subkey = ParagonIE_Sodium_Core32_HChaCha20::hChaCha20(
981
+			ParagonIE_Sodium_Core32_Util::substr($nonce, 0, 16),
982
+			$key
983
+		);
984
+		$nonceLast = ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8);
985
+
986
+		/** @var string $block0 */
987
+		$block0 = str_repeat("\x00", 32);
988
+
989
+		/** @var int $mlen - Length of the plaintext message */
990
+		$mlen = ParagonIE_Sodium_Core32_Util::strlen($plaintext);
991
+		$mlen0 = $mlen;
992
+		if ($mlen0 > 64 - self::secretbox_xchacha20poly1305_ZEROBYTES) {
993
+			$mlen0 = 64 - self::secretbox_xchacha20poly1305_ZEROBYTES;
994
+		}
995
+		$block0 .= ParagonIE_Sodium_Core32_Util::substr($plaintext, 0, $mlen0);
996
+
997
+		/** @var string $block0 */
998
+		$block0 = ParagonIE_Sodium_Core32_ChaCha20::streamXorIc(
999
+			$block0,
1000
+			$nonceLast,
1001
+			$subkey
1002
+		);
1003
+
1004
+		/** @var string $c */
1005
+		$c = ParagonIE_Sodium_Core32_Util::substr(
1006
+			$block0,
1007
+			self::secretbox_xchacha20poly1305_ZEROBYTES
1008
+		);
1009
+		if ($mlen > $mlen0) {
1010
+			$c .= ParagonIE_Sodium_Core32_ChaCha20::streamXorIc(
1011
+				ParagonIE_Sodium_Core32_Util::substr(
1012
+					$plaintext,
1013
+					self::secretbox_xchacha20poly1305_ZEROBYTES
1014
+				),
1015
+				$nonceLast,
1016
+				$subkey,
1017
+				ParagonIE_Sodium_Core32_Util::store64_le(1)
1018
+			);
1019
+		}
1020
+		$state = new ParagonIE_Sodium_Core32_Poly1305_State(
1021
+			ParagonIE_Sodium_Core32_Util::substr(
1022
+				$block0,
1023
+				0,
1024
+				self::onetimeauth_poly1305_KEYBYTES
1025
+			)
1026
+		);
1027
+		try {
1028
+			ParagonIE_Sodium_Compat::memzero($block0);
1029
+			ParagonIE_Sodium_Compat::memzero($subkey);
1030
+		} catch (Error $ex) {
1031
+			$block0 = null;
1032
+			$subkey = null;
1033
+		}
1034
+
1035
+		$state->update($c);
1036
+
1037
+		/** @var string $c - MAC || ciphertext */
1038
+		$c = $state->finish() . $c;
1039
+		unset($state);
1040
+
1041
+		return $c;
1042
+	}
1043
+
1044
+	/**
1045
+	 * Decrypt a ciphertext generated via secretbox_xchacha20poly1305().
1046
+	 *
1047
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
1048
+	 *
1049
+	 * @param string $ciphertext
1050
+	 * @param string $nonce
1051
+	 * @param string $key
1052
+	 * @return string
1053
+	 * @throws Error
1054
+	 */
1055
+	public static function secretbox_xchacha20poly1305_open($ciphertext, $nonce, $key)
1056
+	{
1057
+		/** @var string $mac */
1058
+		$mac = ParagonIE_Sodium_Core32_Util::substr(
1059
+			$ciphertext,
1060
+			0,
1061
+			self::secretbox_xchacha20poly1305_MACBYTES
1062
+		);
1063
+
1064
+		/** @var string $c */
1065
+		$c = ParagonIE_Sodium_Core32_Util::substr(
1066
+			$ciphertext,
1067
+			self::secretbox_xchacha20poly1305_MACBYTES
1068
+		);
1069
+
1070
+		/** @var int $clen */
1071
+		$clen = ParagonIE_Sodium_Core32_Util::strlen($c);
1072
+
1073
+		/** @var string $subkey */
1074
+		$subkey = ParagonIE_Sodium_Core32_HChaCha20::hchacha20($nonce, $key);
1075
+
1076
+		/** @var string $block0 */
1077
+		$block0 = ParagonIE_Sodium_Core32_ChaCha20::stream(
1078
+			64,
1079
+			ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8),
1080
+			$subkey
1081
+		);
1082
+		$verified = ParagonIE_Sodium_Core32_Poly1305::onetimeauth_verify(
1083
+			$mac,
1084
+			$c,
1085
+			ParagonIE_Sodium_Core32_Util::substr($block0, 0, 32)
1086
+		);
1087
+
1088
+		if (!$verified) {
1089
+			try {
1090
+				ParagonIE_Sodium_Compat::memzero($subkey);
1091
+			} catch (Error $ex) {
1092
+				$subkey = null;
1093
+			}
1094
+			throw new Error('Invalid MAC');
1095
+		}
1096
+
1097
+		/** @var string $m - Decrypted message */
1098
+		$m = ParagonIE_Sodium_Core32_Util::xorStrings(
1099
+			ParagonIE_Sodium_Core32_Util::substr($block0, self::secretbox_xchacha20poly1305_ZEROBYTES),
1100
+			ParagonIE_Sodium_Core32_Util::substr($c, 0, self::secretbox_xchacha20poly1305_ZEROBYTES)
1101
+		);
1102
+
1103
+		if ($clen > self::secretbox_xchacha20poly1305_ZEROBYTES) {
1104
+			// We had more than 1 block, so let's continue to decrypt the rest.
1105
+			$m .= ParagonIE_Sodium_Core32_ChaCha20::streamXorIc(
1106
+				ParagonIE_Sodium_Core32_Util::substr(
1107
+					$c,
1108
+					self::secretbox_xchacha20poly1305_ZEROBYTES
1109
+				),
1110
+				ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8),
1111
+				$subkey,
1112
+				ParagonIE_Sodium_Core32_Util::store64_le(1)
1113
+			);
1114
+		}
1115
+		return $m;
1116
+	}
1117
+
1118
+	/**
1119
+	 * Detached Ed25519 signature.
1120
+	 *
1121
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
1122
+	 *
1123
+	 * @param string $message
1124
+	 * @param string $sk
1125
+	 * @return string
1126
+	 */
1127
+	public static function sign_detached($message, $sk)
1128
+	{
1129
+		return ParagonIE_Sodium_Core32_Ed25519::sign_detached($message, $sk);
1130
+	}
1131
+
1132
+	/**
1133
+	 * Attached Ed25519 signature. (Returns a signed message.)
1134
+	 *
1135
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
1136
+	 *
1137
+	 * @param string $message
1138
+	 * @param string $sk
1139
+	 * @return string
1140
+	 */
1141
+	public static function sign($message, $sk)
1142
+	{
1143
+		return ParagonIE_Sodium_Core32_Ed25519::sign($message, $sk);
1144
+	}
1145
+
1146
+	/**
1147
+	 * Opens a signed message. If valid, returns the message.
1148
+	 *
1149
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
1150
+	 *
1151
+	 * @param string $signedMessage
1152
+	 * @param string $pk
1153
+	 * @return string
1154
+	 */
1155
+	public static function sign_open($signedMessage, $pk)
1156
+	{
1157
+		return ParagonIE_Sodium_Core32_Ed25519::sign_open($signedMessage, $pk);
1158
+	}
1159
+
1160
+	/**
1161
+	 * Verify a detached signature of a given message and public key.
1162
+	 *
1163
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
1164
+	 *
1165
+	 * @param string $signature
1166
+	 * @param string $message
1167
+	 * @param string $pk
1168
+	 * @return bool
1169
+	 */
1170
+	public static function sign_verify_detached($signature, $message, $pk)
1171
+	{
1172
+		return ParagonIE_Sodium_Core32_Ed25519::verify_detached($signature, $message, $pk);
1173
+	}
1174 1174
 }
Please login to merge, or discard this patch.
Spacing   +212 added lines, -212 removed lines patch added patch discarded remove patch
@@ -1,6 +1,6 @@  discard block
 block discarded – undo
1 1
 <?php
2 2
 
3
-if (class_exists('ParagonIE_Sodium_Crypto32', false)) {
3
+if ( class_exists( 'ParagonIE_Sodium_Crypto32', false ) ) {
4 4
     return;
5 5
 }
6 6
 
@@ -74,13 +74,13 @@  discard block
 block discarded – undo
74 74
         $key = ''
75 75
     ) {
76 76
         /** @var int $len - Length of message (ciphertext + MAC) */
77
-        $len = ParagonIE_Sodium_Core32_Util::strlen($message);
77
+        $len = ParagonIE_Sodium_Core32_Util::strlen( $message );
78 78
 
79 79
         /** @var int  $clen - Length of ciphertext */
80 80
         $clen = $len - self::aead_chacha20poly1305_ABYTES;
81 81
 
82 82
         /** @var int $adlen - Length of associated data */
83
-        $adlen = ParagonIE_Sodium_Core32_Util::strlen($ad);
83
+        $adlen = ParagonIE_Sodium_Core32_Util::strlen( $ad );
84 84
 
85 85
         /** @var string $mac - Message authentication code */
86 86
         $mac = ParagonIE_Sodium_Core32_Util::substr(
@@ -90,7 +90,7 @@  discard block
 block discarded – undo
90 90
         );
91 91
 
92 92
         /** @var string $ciphertext - The encrypted message (sans MAC) */
93
-        $ciphertext = ParagonIE_Sodium_Core32_Util::substr($message, 0, $clen);
93
+        $ciphertext = ParagonIE_Sodium_Core32_Util::substr( $message, 0, $clen );
94 94
 
95 95
         /** @var string The first block of the chacha20 keystream, used as a poly1305 key */
96 96
         $block0 = ParagonIE_Sodium_Core32_ChaCha20::stream(
@@ -100,21 +100,21 @@  discard block
 block discarded – undo
100 100
         );
101 101
 
102 102
         /* Recalculate the Poly1305 authentication tag (MAC): */
103
-        $state = new ParagonIE_Sodium_Core32_Poly1305_State($block0);
103
+        $state = new ParagonIE_Sodium_Core32_Poly1305_State( $block0 );
104 104
         try {
105
-            ParagonIE_Sodium_Compat::memzero($block0);
106
-        } catch (Error $ex) {
105
+            ParagonIE_Sodium_Compat::memzero( $block0 );
106
+        } catch ( Error $ex ) {
107 107
             $block0 = null;
108 108
         }
109
-        $state->update($ad);
110
-        $state->update(ParagonIE_Sodium_Core32_Util::store64_le($adlen));
111
-        $state->update($ciphertext);
112
-        $state->update(ParagonIE_Sodium_Core32_Util::store64_le($clen));
109
+        $state->update( $ad );
110
+        $state->update( ParagonIE_Sodium_Core32_Util::store64_le( $adlen ) );
111
+        $state->update( $ciphertext );
112
+        $state->update( ParagonIE_Sodium_Core32_Util::store64_le( $clen ) );
113 113
         $computed_mac = $state->finish();
114 114
 
115 115
         /* Compare the given MAC with the recalculated MAC: */
116
-        if (!ParagonIE_Sodium_Core32_Util::verify_16($computed_mac, $mac)) {
117
-            throw new Error('Invalid MAC');
116
+        if ( ! ParagonIE_Sodium_Core32_Util::verify_16( $computed_mac, $mac ) ) {
117
+            throw new Error( 'Invalid MAC' );
118 118
         }
119 119
 
120 120
         // Here, we know that the MAC is valid, so we decrypt and return the plaintext
@@ -122,7 +122,7 @@  discard block
 block discarded – undo
122 122
             $ciphertext,
123 123
             $nonce,
124 124
             $key,
125
-            ParagonIE_Sodium_Core32_Util::store64_le(1)
125
+            ParagonIE_Sodium_Core32_Util::store64_le( 1 )
126 126
         );
127 127
     }
128 128
 
@@ -144,10 +144,10 @@  discard block
 block discarded – undo
144 144
         $key = ''
145 145
     ) {
146 146
         /** @var int $len - Length of the plaintext message */
147
-        $len = ParagonIE_Sodium_Core32_Util::strlen($message);
147
+        $len = ParagonIE_Sodium_Core32_Util::strlen( $message );
148 148
 
149 149
         /** @var int $adlen - Length of the associated data */
150
-        $adlen = ParagonIE_Sodium_Core32_Util::strlen($ad);
150
+        $adlen = ParagonIE_Sodium_Core32_Util::strlen( $ad );
151 151
 
152 152
         /** @var string The first block of the chacha20 keystream, used as a poly1305 key */
153 153
         $block0 = ParagonIE_Sodium_Core32_ChaCha20::stream(
@@ -155,10 +155,10 @@  discard block
 block discarded – undo
155 155
             $nonce,
156 156
             $key
157 157
         );
158
-        $state = new ParagonIE_Sodium_Core32_Poly1305_State($block0);
158
+        $state = new ParagonIE_Sodium_Core32_Poly1305_State( $block0 );
159 159
         try {
160
-            ParagonIE_Sodium_Compat::memzero($block0);
161
-        } catch (Error $ex) {
160
+            ParagonIE_Sodium_Compat::memzero( $block0 );
161
+        } catch ( Error $ex ) {
162 162
             $block0 = null;
163 163
         }
164 164
 
@@ -167,13 +167,13 @@  discard block
 block discarded – undo
167 167
             $message,
168 168
             $nonce,
169 169
             $key,
170
-            ParagonIE_Sodium_Core32_Util::store64_le(1)
170
+            ParagonIE_Sodium_Core32_Util::store64_le( 1 )
171 171
         );
172 172
 
173
-        $state->update($ad);
174
-        $state->update(ParagonIE_Sodium_Core32_Util::store64_le($adlen));
175
-        $state->update($ciphertext);
176
-        $state->update(ParagonIE_Sodium_Core32_Util::store64_le($len));
173
+        $state->update( $ad );
174
+        $state->update( ParagonIE_Sodium_Core32_Util::store64_le( $adlen ) );
175
+        $state->update( $ciphertext );
176
+        $state->update( ParagonIE_Sodium_Core32_Util::store64_le( $len ) );
177 177
         return $ciphertext . $state->finish();
178 178
     }
179 179
 
@@ -196,10 +196,10 @@  discard block
 block discarded – undo
196 196
         $key = ''
197 197
     ) {
198 198
         /** @var int $adlen - Length of associated data */
199
-        $adlen = ParagonIE_Sodium_Core32_Util::strlen($ad);
199
+        $adlen = ParagonIE_Sodium_Core32_Util::strlen( $ad );
200 200
 
201 201
         /** @var int $len - Length of message (ciphertext + MAC) */
202
-        $len = ParagonIE_Sodium_Core32_Util::strlen($message);
202
+        $len = ParagonIE_Sodium_Core32_Util::strlen( $message );
203 203
 
204 204
         /** @var int  $clen - Length of ciphertext */
205 205
         $clen = $len - self::aead_chacha20poly1305_IETF_ABYTES;
@@ -226,23 +226,23 @@  discard block
 block discarded – undo
226 226
         );
227 227
 
228 228
         /* Recalculate the Poly1305 authentication tag (MAC): */
229
-        $state = new ParagonIE_Sodium_Core32_Poly1305_State($block0);
229
+        $state = new ParagonIE_Sodium_Core32_Poly1305_State( $block0 );
230 230
         try {
231
-            ParagonIE_Sodium_Compat::memzero($block0);
232
-        } catch (Error $ex) {
231
+            ParagonIE_Sodium_Compat::memzero( $block0 );
232
+        } catch ( Error $ex ) {
233 233
             $block0 = null;
234 234
         }
235
-        $state->update($ad);
236
-        $state->update(str_repeat("\x00", ((0x10 - $adlen) & 0xf)));
237
-        $state->update($ciphertext);
238
-        $state->update(str_repeat("\x00", (0x10 - $clen) & 0xf));
239
-        $state->update(ParagonIE_Sodium_Core32_Util::store64_le($adlen));
240
-        $state->update(ParagonIE_Sodium_Core32_Util::store64_le($clen));
235
+        $state->update( $ad );
236
+        $state->update( str_repeat( "\x00", ( ( 0x10 - $adlen ) & 0xf ) ) );
237
+        $state->update( $ciphertext );
238
+        $state->update( str_repeat( "\x00", ( 0x10 - $clen ) & 0xf ) );
239
+        $state->update( ParagonIE_Sodium_Core32_Util::store64_le( $adlen ) );
240
+        $state->update( ParagonIE_Sodium_Core32_Util::store64_le( $clen ) );
241 241
         $computed_mac = $state->finish();
242 242
 
243 243
         /* Compare the given MAC with the recalculated MAC: */
244
-        if (!ParagonIE_Sodium_Core32_Util::verify_16($computed_mac, $mac)) {
245
-            throw new Error('Invalid MAC');
244
+        if ( ! ParagonIE_Sodium_Core32_Util::verify_16( $computed_mac, $mac ) ) {
245
+            throw new Error( 'Invalid MAC' );
246 246
         }
247 247
 
248 248
         // Here, we know that the MAC is valid, so we decrypt and return the plaintext
@@ -250,7 +250,7 @@  discard block
 block discarded – undo
250 250
             $ciphertext,
251 251
             $nonce,
252 252
             $key,
253
-            ParagonIE_Sodium_Core32_Util::store64_le(1)
253
+            ParagonIE_Sodium_Core32_Util::store64_le( 1 )
254 254
         );
255 255
     }
256 256
 
@@ -272,10 +272,10 @@  discard block
 block discarded – undo
272 272
         $key = ''
273 273
     ) {
274 274
         /** @var int $len - Length of the plaintext message */
275
-        $len = ParagonIE_Sodium_Core32_Util::strlen($message);
275
+        $len = ParagonIE_Sodium_Core32_Util::strlen( $message );
276 276
 
277 277
         /** @var int $adlen - Length of the associated data */
278
-        $adlen = ParagonIE_Sodium_Core32_Util::strlen($ad);
278
+        $adlen = ParagonIE_Sodium_Core32_Util::strlen( $ad );
279 279
 
280 280
         /** @var string The first block of the chacha20 keystream, used as a poly1305 key */
281 281
         $block0 = ParagonIE_Sodium_Core32_ChaCha20::ietfStream(
@@ -283,10 +283,10 @@  discard block
 block discarded – undo
283 283
             $nonce,
284 284
             $key
285 285
         );
286
-        $state = new ParagonIE_Sodium_Core32_Poly1305_State($block0);
286
+        $state = new ParagonIE_Sodium_Core32_Poly1305_State( $block0 );
287 287
         try {
288
-            ParagonIE_Sodium_Compat::memzero($block0);
289
-        } catch (Error $ex) {
288
+            ParagonIE_Sodium_Compat::memzero( $block0 );
289
+        } catch ( Error $ex ) {
290 290
             $block0 = null;
291 291
         }
292 292
 
@@ -295,15 +295,15 @@  discard block
 block discarded – undo
295 295
             $message,
296 296
             $nonce,
297 297
             $key,
298
-            ParagonIE_Sodium_Core32_Util::store64_le(1)
298
+            ParagonIE_Sodium_Core32_Util::store64_le( 1 )
299 299
         );
300 300
 
301
-        $state->update($ad);
302
-        $state->update(str_repeat("\x00", ((0x10 - $adlen) & 0xf)));
303
-        $state->update($ciphertext);
304
-        $state->update(str_repeat("\x00", ((0x10 - $len) & 0xf)));
305
-        $state->update(ParagonIE_Sodium_Core32_Util::store64_le($adlen));
306
-        $state->update(ParagonIE_Sodium_Core32_Util::store64_le($len));
301
+        $state->update( $ad );
302
+        $state->update( str_repeat( "\x00", ( ( 0x10 - $adlen ) & 0xf ) ) );
303
+        $state->update( $ciphertext );
304
+        $state->update( str_repeat( "\x00", ( ( 0x10 - $len ) & 0xf ) ) );
305
+        $state->update( ParagonIE_Sodium_Core32_Util::store64_le( $adlen ) );
306
+        $state->update( ParagonIE_Sodium_Core32_Util::store64_le( $len ) );
307 307
         return $ciphertext . $state->finish();
308 308
     }
309 309
 
@@ -326,13 +326,13 @@  discard block
 block discarded – undo
326 326
         $key = ''
327 327
     ) {
328 328
         $subkey = ParagonIE_Sodium_Core32_HChaCha20::hChaCha20(
329
-            ParagonIE_Sodium_Core32_Util::substr($nonce, 0, 16),
329
+            ParagonIE_Sodium_Core32_Util::substr( $nonce, 0, 16 ),
330 330
             $key
331 331
         );
332 332
         $nonceLast = "\x00\x00\x00\x00" .
333
-            ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8);
333
+            ParagonIE_Sodium_Core32_Util::substr( $nonce, 16, 8 );
334 334
 
335
-        return self::aead_chacha20poly1305_ietf_decrypt($message, $ad, $nonceLast, $subkey);
335
+        return self::aead_chacha20poly1305_ietf_decrypt( $message, $ad, $nonceLast, $subkey );
336 336
     }
337 337
 
338 338
     /**
@@ -353,13 +353,13 @@  discard block
 block discarded – undo
353 353
         $key = ''
354 354
     ) {
355 355
         $subkey = ParagonIE_Sodium_Core32_HChaCha20::hChaCha20(
356
-            ParagonIE_Sodium_Core32_Util::substr($nonce, 0, 16),
356
+            ParagonIE_Sodium_Core32_Util::substr( $nonce, 0, 16 ),
357 357
             $key
358 358
         );
359 359
         $nonceLast = "\x00\x00\x00\x00" .
360
-            ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8);
360
+            ParagonIE_Sodium_Core32_Util::substr( $nonce, 16, 8 );
361 361
 
362
-        return self::aead_chacha20poly1305_ietf_encrypt($message, $ad, $nonceLast, $subkey);
362
+        return self::aead_chacha20poly1305_ietf_encrypt( $message, $ad, $nonceLast, $subkey );
363 363
     }
364 364
 
365 365
     /**
@@ -371,10 +371,10 @@  discard block
 block discarded – undo
371 371
      * @param string $key
372 372
      * @return string
373 373
      */
374
-    public static function auth($message, $key)
374
+    public static function auth( $message, $key )
375 375
     {
376 376
         return ParagonIE_Sodium_Core32_Util::substr(
377
-            hash_hmac('sha512', $message, $key, true),
377
+            hash_hmac( 'sha512', $message, $key, true ),
378 378
             0,
379 379
             32
380 380
         );
@@ -390,11 +390,11 @@  discard block
 block discarded – undo
390 390
      * @param string $key
391 391
      * @return bool
392 392
      */
393
-    public static function auth_verify($mac, $message, $key)
393
+    public static function auth_verify( $mac, $message, $key )
394 394
     {
395 395
         return ParagonIE_Sodium_Core32_Util::hashEquals(
396 396
             $mac,
397
-            self::auth($message, $key)
397
+            self::auth( $message, $key )
398 398
         );
399 399
     }
400 400
 
@@ -408,14 +408,14 @@  discard block
 block discarded – undo
408 408
      * @param string $keypair
409 409
      * @return string
410 410
      */
411
-    public static function box($plaintext, $nonce, $keypair)
411
+    public static function box( $plaintext, $nonce, $keypair )
412 412
     {
413 413
         $c = self::secretbox(
414 414
             $plaintext,
415 415
             $nonce,
416 416
             self::box_beforenm(
417
-                self::box_secretkey($keypair),
418
-                self::box_publickey($keypair)
417
+                self::box_secretkey( $keypair ),
418
+                self::box_publickey( $keypair )
419 419
             )
420 420
         );
421 421
         return $c;
@@ -430,16 +430,16 @@  discard block
 block discarded – undo
430 430
      * @param string $publicKey
431 431
      * @return string
432 432
      */
433
-    public static function box_seal($message, $publicKey)
433
+    public static function box_seal( $message, $publicKey )
434 434
     {
435 435
         /** @var string $ephemeralKeypair */
436 436
         $ephemeralKeypair = self::box_keypair();
437 437
 
438 438
         /** @var string $ephemeralSK */
439
-        $ephemeralSK = self::box_secretkey($ephemeralKeypair);
439
+        $ephemeralSK = self::box_secretkey( $ephemeralKeypair );
440 440
 
441 441
         /** @var string $ephemeralPK */
442
-        $ephemeralPK = self::box_publickey($ephemeralKeypair);
442
+        $ephemeralPK = self::box_publickey( $ephemeralKeypair );
443 443
 
444 444
         /** @var string $nonce */
445 445
         $nonce = self::generichash(
@@ -449,15 +449,15 @@  discard block
 block discarded – undo
449 449
         );
450 450
 
451 451
         /** @var string $keypair - The combined keypair used in crypto_box() */
452
-        $keypair = self::box_keypair_from_secretkey_and_publickey($ephemeralSK, $publicKey);
452
+        $keypair = self::box_keypair_from_secretkey_and_publickey( $ephemeralSK, $publicKey );
453 453
 
454 454
         /** @var string $ciphertext Ciphertext + MAC from crypto_box */
455
-        $ciphertext = self::box($message, $nonce, $keypair);
455
+        $ciphertext = self::box( $message, $nonce, $keypair );
456 456
         try {
457
-            ParagonIE_Sodium_Compat::memzero($ephemeralKeypair);
458
-            ParagonIE_Sodium_Compat::memzero($ephemeralSK);
459
-            ParagonIE_Sodium_Compat::memzero($nonce);
460
-        } catch (Error $ex) {
457
+            ParagonIE_Sodium_Compat::memzero( $ephemeralKeypair );
458
+            ParagonIE_Sodium_Compat::memzero( $ephemeralSK );
459
+            ParagonIE_Sodium_Compat::memzero( $nonce );
460
+        } catch ( Error $ex ) {
461 461
             $ephemeralKeypair = null;
462 462
             $ephemeralSK = null;
463 463
             $nonce = null;
@@ -474,19 +474,19 @@  discard block
 block discarded – undo
474 474
      * @param string $keypair
475 475
      * @return string
476 476
      */
477
-    public static function box_seal_open($message, $keypair)
477
+    public static function box_seal_open( $message, $keypair )
478 478
     {
479 479
         /** @var string $ephemeralPK */
480
-        $ephemeralPK = ParagonIE_Sodium_Core32_Util::substr($message, 0, 32);
480
+        $ephemeralPK = ParagonIE_Sodium_Core32_Util::substr( $message, 0, 32 );
481 481
 
482 482
         /** @var string $ciphertext (ciphertext + MAC) */
483
-        $ciphertext = ParagonIE_Sodium_Core32_Util::substr($message, 32);
483
+        $ciphertext = ParagonIE_Sodium_Core32_Util::substr( $message, 32 );
484 484
 
485 485
         /** @var string $secretKey */
486
-        $secretKey = self::box_secretkey($keypair);
486
+        $secretKey = self::box_secretkey( $keypair );
487 487
 
488 488
         /** @var string $publicKey */
489
-        $publicKey = self::box_publickey($keypair);
489
+        $publicKey = self::box_publickey( $keypair );
490 490
 
491 491
         /** @var string $nonce */
492 492
         $nonce = self::generichash(
@@ -496,15 +496,15 @@  discard block
 block discarded – undo
496 496
         );
497 497
 
498 498
         /** @var string $keypair */
499
-        $keypair = self::box_keypair_from_secretkey_and_publickey($secretKey, $ephemeralPK);
499
+        $keypair = self::box_keypair_from_secretkey_and_publickey( $secretKey, $ephemeralPK );
500 500
 
501 501
         /** @var string $m */
502
-        $m = self::box_open($ciphertext, $nonce, $keypair);
502
+        $m = self::box_open( $ciphertext, $nonce, $keypair );
503 503
         try {
504
-            ParagonIE_Sodium_Compat::memzero($secretKey);
505
-            ParagonIE_Sodium_Compat::memzero($ephemeralPK);
506
-            ParagonIE_Sodium_Compat::memzero($nonce);
507
-        } catch (Error $ex) {
504
+            ParagonIE_Sodium_Compat::memzero( $secretKey );
505
+            ParagonIE_Sodium_Compat::memzero( $ephemeralPK );
506
+            ParagonIE_Sodium_Compat::memzero( $nonce );
507
+        } catch ( Error $ex ) {
508 508
             $secretKey = null;
509 509
             $ephemeralPK = null;
510 510
             $nonce = null;
@@ -521,11 +521,11 @@  discard block
 block discarded – undo
521 521
      * @param string $pk
522 522
      * @return string
523 523
      */
524
-    public static function box_beforenm($sk, $pk)
524
+    public static function box_beforenm( $sk, $pk )
525 525
     {
526 526
         return ParagonIE_Sodium_Core32_HSalsa20::hsalsa20(
527
-            str_repeat("\x00", 16),
528
-            self::scalarmult($sk, $pk)
527
+            str_repeat( "\x00", 16 ),
528
+            self::scalarmult( $sk, $pk )
529 529
         );
530 530
     }
531 531
 
@@ -536,8 +536,8 @@  discard block
 block discarded – undo
536 536
      */
537 537
     public static function box_keypair()
538 538
     {
539
-        $sKey = random_bytes(32);
540
-        $pKey = self::scalarmult_base($sKey);
539
+        $sKey = random_bytes( 32 );
540
+        $pKey = self::scalarmult_base( $sKey );
541 541
         return $sKey . $pKey;
542 542
     }
543 543
 
@@ -548,10 +548,10 @@  discard block
 block discarded – undo
548 548
      * @param string $pKey
549 549
      * @return string
550 550
      */
551
-    public static function box_keypair_from_secretkey_and_publickey($sKey, $pKey)
551
+    public static function box_keypair_from_secretkey_and_publickey( $sKey, $pKey )
552 552
     {
553
-        return ParagonIE_Sodium_Core32_Util::substr($sKey, 0, 32) .
554
-            ParagonIE_Sodium_Core32_Util::substr($pKey, 0, 32);
553
+        return ParagonIE_Sodium_Core32_Util::substr( $sKey, 0, 32 ) .
554
+            ParagonIE_Sodium_Core32_Util::substr( $pKey, 0, 32 );
555 555
     }
556 556
 
557 557
     /**
@@ -561,12 +561,12 @@  discard block
 block discarded – undo
561 561
      * @return string
562 562
      * @throws RangeException
563 563
      */
564
-    public static function box_secretkey($keypair)
564
+    public static function box_secretkey( $keypair )
565 565
     {
566
-        if (ParagonIE_Sodium_Core32_Util::strlen($keypair) !== 64) {
567
-            throw new RangeException('Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES bytes long.');
566
+        if ( ParagonIE_Sodium_Core32_Util::strlen( $keypair ) !== 64 ) {
567
+            throw new RangeException( 'Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES bytes long.' );
568 568
         }
569
-        return ParagonIE_Sodium_Core32_Util::substr($keypair, 0, 32);
569
+        return ParagonIE_Sodium_Core32_Util::substr( $keypair, 0, 32 );
570 570
     }
571 571
 
572 572
     /**
@@ -576,12 +576,12 @@  discard block
 block discarded – undo
576 576
      * @return string
577 577
      * @throws RangeException
578 578
      */
579
-    public static function box_publickey($keypair)
579
+    public static function box_publickey( $keypair )
580 580
     {
581
-        if (ParagonIE_Sodium_Core32_Util::strlen($keypair) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES) {
582
-            throw new RangeException('Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES bytes long.');
581
+        if ( ParagonIE_Sodium_Core32_Util::strlen( $keypair ) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES ) {
582
+            throw new RangeException( 'Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES bytes long.' );
583 583
         }
584
-        return ParagonIE_Sodium_Core32_Util::substr($keypair, 32, 32);
584
+        return ParagonIE_Sodium_Core32_Util::substr( $keypair, 32, 32 );
585 585
     }
586 586
 
587 587
     /**
@@ -591,12 +591,12 @@  discard block
 block discarded – undo
591 591
      * @return string
592 592
      * @throws RangeException
593 593
      */
594
-    public static function box_publickey_from_secretkey($sKey)
594
+    public static function box_publickey_from_secretkey( $sKey )
595 595
     {
596
-        if (ParagonIE_Sodium_Core32_Util::strlen($sKey) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_SECRETKEYBYTES) {
597
-            throw new RangeException('Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_SECRETKEYBYTES bytes long.');
596
+        if ( ParagonIE_Sodium_Core32_Util::strlen( $sKey ) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_SECRETKEYBYTES ) {
597
+            throw new RangeException( 'Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_SECRETKEYBYTES bytes long.' );
598 598
         }
599
-        return self::scalarmult_base($sKey);
599
+        return self::scalarmult_base( $sKey );
600 600
     }
601 601
 
602 602
     /**
@@ -610,14 +610,14 @@  discard block
 block discarded – undo
610 610
      * @param string $keypair
611 611
      * @return string
612 612
      */
613
-    public static function box_open($ciphertext, $nonce, $keypair)
613
+    public static function box_open( $ciphertext, $nonce, $keypair )
614 614
     {
615 615
         return self::secretbox_open(
616 616
             $ciphertext,
617 617
             $nonce,
618 618
             self::box_beforenm(
619
-                self::box_secretkey($keypair),
620
-                self::box_publickey($keypair)
619
+                self::box_secretkey( $keypair ),
620
+                self::box_publickey( $keypair )
621 621
             )
622 622
         );
623 623
     }
@@ -633,34 +633,34 @@  discard block
 block discarded – undo
633 633
      * @return string
634 634
      * @throws RangeException
635 635
      */
636
-    public static function generichash($message, $key = '', $outlen = 32)
636
+    public static function generichash( $message, $key = '', $outlen = 32 )
637 637
     {
638 638
         // This ensures that ParagonIE_Sodium_Core32_BLAKE2b::$iv is initialized
639 639
         ParagonIE_Sodium_Core32_BLAKE2b::pseudoConstructor();
640 640
 
641 641
         $k = null;
642
-        if (!empty($key)) {
642
+        if ( ! empty( $key ) ) {
643 643
             /** @var SplFixedArray $k */
644
-            $k = ParagonIE_Sodium_Core32_BLAKE2b::stringToSplFixedArray($key);
645
-            if ($k->count() > ParagonIE_Sodium_Core32_BLAKE2b::KEYBYTES) {
646
-                throw new RangeException('Invalid key size');
644
+            $k = ParagonIE_Sodium_Core32_BLAKE2b::stringToSplFixedArray( $key );
645
+            if ( $k->count() > ParagonIE_Sodium_Core32_BLAKE2b::KEYBYTES ) {
646
+                throw new RangeException( 'Invalid key size' );
647 647
             }
648 648
         }
649 649
 
650 650
         /** @var SplFixedArray $in */
651
-        $in = ParagonIE_Sodium_Core32_BLAKE2b::stringToSplFixedArray($message);
651
+        $in = ParagonIE_Sodium_Core32_BLAKE2b::stringToSplFixedArray( $message );
652 652
 
653 653
         /** @var SplFixedArray $ctx */
654
-        $ctx = ParagonIE_Sodium_Core32_BLAKE2b::init($k, $outlen);
655
-        ParagonIE_Sodium_Core32_BLAKE2b::update($ctx, $in, $in->count());
654
+        $ctx = ParagonIE_Sodium_Core32_BLAKE2b::init( $k, $outlen );
655
+        ParagonIE_Sodium_Core32_BLAKE2b::update( $ctx, $in, $in->count() );
656 656
 
657 657
         /** @var SplFixedArray $out */
658
-        $out = new SplFixedArray($outlen);
659
-        $out = ParagonIE_Sodium_Core32_BLAKE2b::finish($ctx, $out);
658
+        $out = new SplFixedArray( $outlen );
659
+        $out = ParagonIE_Sodium_Core32_BLAKE2b::finish( $ctx, $out );
660 660
 
661 661
         /** @var array<int, int> */
662 662
         $outArray = $out->toArray();
663
-        return ParagonIE_Sodium_Core32_Util::intArrayToString($outArray);
663
+        return ParagonIE_Sodium_Core32_Util::intArrayToString( $outArray );
664 664
     }
665 665
 
666 666
     /**
@@ -673,22 +673,22 @@  discard block
 block discarded – undo
673 673
      * @return string
674 674
      * @throws TypeError
675 675
      */
676
-    public static function generichash_final($ctx, $outlen = 32)
676
+    public static function generichash_final( $ctx, $outlen = 32 )
677 677
     {
678
-        if (!is_string($ctx)) {
679
-            throw new TypeError('Context must be a string');
678
+        if ( ! is_string( $ctx ) ) {
679
+            throw new TypeError( 'Context must be a string' );
680 680
         }
681
-        $out = new SplFixedArray($outlen);
681
+        $out = new SplFixedArray( $outlen );
682 682
 
683 683
         /** @var SplFixedArray $context */
684
-        $context = ParagonIE_Sodium_Core32_BLAKE2b::stringToContext($ctx);
684
+        $context = ParagonIE_Sodium_Core32_BLAKE2b::stringToContext( $ctx );
685 685
 
686 686
         /** @var SplFixedArray $out */
687
-        $out = ParagonIE_Sodium_Core32_BLAKE2b::finish($context, $out);
687
+        $out = ParagonIE_Sodium_Core32_BLAKE2b::finish( $context, $out );
688 688
 
689 689
         /** @var array<int, int> */
690 690
         $outArray = $out->toArray();
691
-        return ParagonIE_Sodium_Core32_Util::intArrayToString($outArray);
691
+        return ParagonIE_Sodium_Core32_Util::intArrayToString( $outArray );
692 692
     }
693 693
 
694 694
     /**
@@ -701,23 +701,23 @@  discard block
 block discarded – undo
701 701
      * @return string
702 702
      * @throws RangeException
703 703
      */
704
-    public static function generichash_init($key = '', $outputLength = 32)
704
+    public static function generichash_init( $key = '', $outputLength = 32 )
705 705
     {
706 706
         // This ensures that ParagonIE_Sodium_Core32_BLAKE2b::$iv is initialized
707 707
         ParagonIE_Sodium_Core32_BLAKE2b::pseudoConstructor();
708 708
 
709 709
         $k = null;
710
-        if (!empty($key)) {
711
-            $k = ParagonIE_Sodium_Core32_BLAKE2b::stringToSplFixedArray($key);
712
-            if ($k->count() > ParagonIE_Sodium_Core32_BLAKE2b::KEYBYTES) {
713
-                throw new RangeException('Invalid key size');
710
+        if ( ! empty( $key ) ) {
711
+            $k = ParagonIE_Sodium_Core32_BLAKE2b::stringToSplFixedArray( $key );
712
+            if ( $k->count() > ParagonIE_Sodium_Core32_BLAKE2b::KEYBYTES ) {
713
+                throw new RangeException( 'Invalid key size' );
714 714
             }
715 715
         }
716 716
 
717 717
         /** @var SplFixedArray $ctx */
718
-        $ctx = ParagonIE_Sodium_Core32_BLAKE2b::init($k, $outputLength);
718
+        $ctx = ParagonIE_Sodium_Core32_BLAKE2b::init( $k, $outputLength );
719 719
 
720
-        return ParagonIE_Sodium_Core32_BLAKE2b::contextToString($ctx);
720
+        return ParagonIE_Sodium_Core32_BLAKE2b::contextToString( $ctx );
721 721
     }
722 722
 
723 723
     /**
@@ -729,20 +729,20 @@  discard block
 block discarded – undo
729 729
      * @param string $message
730 730
      * @return string
731 731
      */
732
-    public static function generichash_update($ctx, $message)
732
+    public static function generichash_update( $ctx, $message )
733 733
     {
734 734
         // This ensures that ParagonIE_Sodium_Core32_BLAKE2b::$iv is initialized
735 735
         ParagonIE_Sodium_Core32_BLAKE2b::pseudoConstructor();
736 736
 
737 737
         /** @var SplFixedArray $context */
738
-        $context = ParagonIE_Sodium_Core32_BLAKE2b::stringToContext($ctx);
738
+        $context = ParagonIE_Sodium_Core32_BLAKE2b::stringToContext( $ctx );
739 739
 
740 740
         /** @var SplFixedArray $in */
741
-        $in = ParagonIE_Sodium_Core32_BLAKE2b::stringToSplFixedArray($message);
741
+        $in = ParagonIE_Sodium_Core32_BLAKE2b::stringToSplFixedArray( $message );
742 742
 
743
-        ParagonIE_Sodium_Core32_BLAKE2b::update($context, $in, $in->count());
743
+        ParagonIE_Sodium_Core32_BLAKE2b::update( $context, $in, $in->count() );
744 744
 
745
-        return ParagonIE_Sodium_Core32_BLAKE2b::contextToString($context);
745
+        return ParagonIE_Sodium_Core32_BLAKE2b::contextToString( $context );
746 746
     }
747 747
 
748 748
     /**
@@ -756,10 +756,10 @@  discard block
 block discarded – undo
756 756
      * @param string $server_pk
757 757
      * @return string
758 758
      */
759
-    public static function keyExchange($my_sk, $their_pk, $client_pk, $server_pk)
759
+    public static function keyExchange( $my_sk, $their_pk, $client_pk, $server_pk )
760 760
     {
761 761
         return self::generichash(
762
-            self::scalarmult($my_sk, $their_pk) .
762
+            self::scalarmult( $my_sk, $their_pk ) .
763 763
             $client_pk .
764 764
             $server_pk
765 765
         );
@@ -776,10 +776,10 @@  discard block
 block discarded – undo
776 776
      *
777 777
      * @throws Error
778 778
      */
779
-    public static function scalarmult($sKey, $pKey)
779
+    public static function scalarmult( $sKey, $pKey )
780 780
     {
781
-        $q = ParagonIE_Sodium_Core32_X25519::crypto_scalarmult_curve25519_ref10($sKey, $pKey);
782
-        self::scalarmult_throw_if_zero($q);
781
+        $q = ParagonIE_Sodium_Core32_X25519::crypto_scalarmult_curve25519_ref10( $sKey, $pKey );
782
+        self::scalarmult_throw_if_zero( $q );
783 783
         return $q;
784 784
     }
785 785
 
@@ -792,10 +792,10 @@  discard block
 block discarded – undo
792 792
      *
793 793
      * @throws Error
794 794
      */
795
-    public static function scalarmult_base($secret)
795
+    public static function scalarmult_base( $secret )
796 796
     {
797
-        $q = ParagonIE_Sodium_Core32_X25519::crypto_scalarmult_curve25519_ref10_base($secret);
798
-        self::scalarmult_throw_if_zero($q);
797
+        $q = ParagonIE_Sodium_Core32_X25519::crypto_scalarmult_curve25519_ref10_base( $secret );
798
+        self::scalarmult_throw_if_zero( $q );
799 799
         return $q;
800 800
     }
801 801
 
@@ -806,16 +806,16 @@  discard block
 block discarded – undo
806 806
      * @return void
807 807
      * @throws Error
808 808
      */
809
-    protected static function scalarmult_throw_if_zero($q)
809
+    protected static function scalarmult_throw_if_zero( $q )
810 810
     {
811 811
         $d = 0;
812
-        for ($i = 0; $i < self::box_curve25519xsalsa20poly1305_SECRETKEYBYTES; ++$i) {
813
-            $d |= ParagonIE_Sodium_Core32_Util::chrToInt($q[$i]);
812
+        for ( $i = 0; $i < self::box_curve25519xsalsa20poly1305_SECRETKEYBYTES; ++$i ) {
813
+            $d |= ParagonIE_Sodium_Core32_Util::chrToInt( $q[$i] );
814 814
         }
815 815
 
816 816
         /* branch-free variant of === 0 */
817
-        if (-(1 & (($d - 1) >> 8))) {
818
-            throw new Error('Zero public key is not allowed');
817
+        if (-( 1 & ( ( $d - 1 ) >> 8 ) )) {
818
+            throw new Error( 'Zero public key is not allowed' );
819 819
         }
820 820
     }
821 821
 
@@ -829,26 +829,26 @@  discard block
 block discarded – undo
829 829
      * @param string $key
830 830
      * @return string
831 831
      */
832
-    public static function secretbox($plaintext, $nonce, $key)
832
+    public static function secretbox( $plaintext, $nonce, $key )
833 833
     {
834 834
         /** @var string $subkey */
835
-        $subkey = ParagonIE_Sodium_Core32_HSalsa20::hsalsa20($nonce, $key);
835
+        $subkey = ParagonIE_Sodium_Core32_HSalsa20::hsalsa20( $nonce, $key );
836 836
 
837 837
         /** @var string $block0 */
838
-        $block0 = str_repeat("\x00", 32);
838
+        $block0 = str_repeat( "\x00", 32 );
839 839
 
840 840
         /** @var int $mlen - Length of the plaintext message */
841
-        $mlen = ParagonIE_Sodium_Core32_Util::strlen($plaintext);
841
+        $mlen = ParagonIE_Sodium_Core32_Util::strlen( $plaintext );
842 842
         $mlen0 = $mlen;
843
-        if ($mlen0 > 64 - self::secretbox_xsalsa20poly1305_ZEROBYTES) {
843
+        if ( $mlen0 > 64 - self::secretbox_xsalsa20poly1305_ZEROBYTES ) {
844 844
             $mlen0 = 64 - self::secretbox_xsalsa20poly1305_ZEROBYTES;
845 845
         }
846
-        $block0 .= ParagonIE_Sodium_Core32_Util::substr($plaintext, 0, $mlen0);
846
+        $block0 .= ParagonIE_Sodium_Core32_Util::substr( $plaintext, 0, $mlen0 );
847 847
 
848 848
         /** @var string $block0 */
849 849
         $block0 = ParagonIE_Sodium_Core32_Salsa20::salsa20_xor(
850 850
             $block0,
851
-            ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8),
851
+            ParagonIE_Sodium_Core32_Util::substr( $nonce, 16, 8 ),
852 852
             $subkey
853 853
         );
854 854
 
@@ -857,13 +857,13 @@  discard block
 block discarded – undo
857 857
             $block0,
858 858
             self::secretbox_xsalsa20poly1305_ZEROBYTES
859 859
         );
860
-        if ($mlen > $mlen0) {
860
+        if ( $mlen > $mlen0 ) {
861 861
             $c .= ParagonIE_Sodium_Core32_Salsa20::salsa20_xor_ic(
862 862
                 ParagonIE_Sodium_Core32_Util::substr(
863 863
                     $plaintext,
864 864
                     self::secretbox_xsalsa20poly1305_ZEROBYTES
865 865
                 ),
866
-                ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8),
866
+                ParagonIE_Sodium_Core32_Util::substr( $nonce, 16, 8 ),
867 867
                 1,
868 868
                 $subkey
869 869
             );
@@ -876,18 +876,18 @@  discard block
 block discarded – undo
876 876
             )
877 877
         );
878 878
         try {
879
-            ParagonIE_Sodium_Compat::memzero($block0);
880
-            ParagonIE_Sodium_Compat::memzero($subkey);
881
-        } catch (Error $ex) {
879
+            ParagonIE_Sodium_Compat::memzero( $block0 );
880
+            ParagonIE_Sodium_Compat::memzero( $subkey );
881
+        } catch ( Error $ex ) {
882 882
             $block0 = null;
883 883
             $subkey = null;
884 884
         }
885 885
 
886
-        $state->update($c);
886
+        $state->update( $c );
887 887
 
888 888
         /** @var string $c - MAC || ciphertext */
889 889
         $c = $state->finish() . $c;
890
-        unset($state);
890
+        unset( $state );
891 891
 
892 892
         return $c;
893 893
     }
@@ -903,7 +903,7 @@  discard block
 block discarded – undo
903 903
      * @return string
904 904
      * @throws Error
905 905
      */
906
-    public static function secretbox_open($ciphertext, $nonce, $key)
906
+    public static function secretbox_open( $ciphertext, $nonce, $key )
907 907
     {
908 908
         /** @var string $mac */
909 909
         $mac = ParagonIE_Sodium_Core32_Util::substr(
@@ -919,44 +919,44 @@  discard block
 block discarded – undo
919 919
         );
920 920
 
921 921
         /** @var int $clen */
922
-        $clen = ParagonIE_Sodium_Core32_Util::strlen($c);
922
+        $clen = ParagonIE_Sodium_Core32_Util::strlen( $c );
923 923
 
924 924
         /** @var string $subkey */
925
-        $subkey = ParagonIE_Sodium_Core32_HSalsa20::hsalsa20($nonce, $key);
925
+        $subkey = ParagonIE_Sodium_Core32_HSalsa20::hsalsa20( $nonce, $key );
926 926
 
927 927
         /** @var string $block0 */
928 928
         $block0 = ParagonIE_Sodium_Core32_Salsa20::salsa20(
929 929
             64,
930
-            ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8),
930
+            ParagonIE_Sodium_Core32_Util::substr( $nonce, 16, 8 ),
931 931
             $subkey
932 932
         );
933 933
         $verified = ParagonIE_Sodium_Core32_Poly1305::onetimeauth_verify(
934 934
             $mac,
935 935
             $c,
936
-            ParagonIE_Sodium_Core32_Util::substr($block0, 0, 32)
936
+            ParagonIE_Sodium_Core32_Util::substr( $block0, 0, 32 )
937 937
         );
938
-        if (!$verified) {
938
+        if ( ! $verified ) {
939 939
             try {
940
-                ParagonIE_Sodium_Compat::memzero($subkey);
941
-            } catch (Error $ex) {
940
+                ParagonIE_Sodium_Compat::memzero( $subkey );
941
+            } catch ( Error $ex ) {
942 942
                 $subkey = null;
943 943
             }
944
-            throw new Error('Invalid MAC');
944
+            throw new Error( 'Invalid MAC' );
945 945
         }
946 946
 
947 947
         /** @var string $m - Decrypted message */
948 948
         $m = ParagonIE_Sodium_Core32_Util::xorStrings(
949
-            ParagonIE_Sodium_Core32_Util::substr($block0, self::secretbox_xsalsa20poly1305_ZEROBYTES),
950
-            ParagonIE_Sodium_Core32_Util::substr($c, 0, self::secretbox_xsalsa20poly1305_ZEROBYTES)
949
+            ParagonIE_Sodium_Core32_Util::substr( $block0, self::secretbox_xsalsa20poly1305_ZEROBYTES ),
950
+            ParagonIE_Sodium_Core32_Util::substr( $c, 0, self::secretbox_xsalsa20poly1305_ZEROBYTES )
951 951
         );
952
-        if ($clen > self::secretbox_xsalsa20poly1305_ZEROBYTES) {
952
+        if ( $clen > self::secretbox_xsalsa20poly1305_ZEROBYTES ) {
953 953
             // We had more than 1 block, so let's continue to decrypt the rest.
954 954
             $m .= ParagonIE_Sodium_Core32_Salsa20::salsa20_xor_ic(
955 955
                 ParagonIE_Sodium_Core32_Util::substr(
956 956
                     $c,
957 957
                     self::secretbox_xsalsa20poly1305_ZEROBYTES
958 958
                 ),
959
-                ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8),
959
+                ParagonIE_Sodium_Core32_Util::substr( $nonce, 16, 8 ),
960 960
                 1,
961 961
                 $subkey
962 962
             );
@@ -974,25 +974,25 @@  discard block
 block discarded – undo
974 974
      * @param string $key
975 975
      * @return string
976 976
      */
977
-    public static function secretbox_xchacha20poly1305($plaintext, $nonce, $key)
977
+    public static function secretbox_xchacha20poly1305( $plaintext, $nonce, $key )
978 978
     {
979 979
         /** @var string $subkey */
980 980
         $subkey = ParagonIE_Sodium_Core32_HChaCha20::hChaCha20(
981
-            ParagonIE_Sodium_Core32_Util::substr($nonce, 0, 16),
981
+            ParagonIE_Sodium_Core32_Util::substr( $nonce, 0, 16 ),
982 982
             $key
983 983
         );
984
-        $nonceLast = ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8);
984
+        $nonceLast = ParagonIE_Sodium_Core32_Util::substr( $nonce, 16, 8 );
985 985
 
986 986
         /** @var string $block0 */
987
-        $block0 = str_repeat("\x00", 32);
987
+        $block0 = str_repeat( "\x00", 32 );
988 988
 
989 989
         /** @var int $mlen - Length of the plaintext message */
990
-        $mlen = ParagonIE_Sodium_Core32_Util::strlen($plaintext);
990
+        $mlen = ParagonIE_Sodium_Core32_Util::strlen( $plaintext );
991 991
         $mlen0 = $mlen;
992
-        if ($mlen0 > 64 - self::secretbox_xchacha20poly1305_ZEROBYTES) {
992
+        if ( $mlen0 > 64 - self::secretbox_xchacha20poly1305_ZEROBYTES ) {
993 993
             $mlen0 = 64 - self::secretbox_xchacha20poly1305_ZEROBYTES;
994 994
         }
995
-        $block0 .= ParagonIE_Sodium_Core32_Util::substr($plaintext, 0, $mlen0);
995
+        $block0 .= ParagonIE_Sodium_Core32_Util::substr( $plaintext, 0, $mlen0 );
996 996
 
997 997
         /** @var string $block0 */
998 998
         $block0 = ParagonIE_Sodium_Core32_ChaCha20::streamXorIc(
@@ -1006,7 +1006,7 @@  discard block
 block discarded – undo
1006 1006
             $block0,
1007 1007
             self::secretbox_xchacha20poly1305_ZEROBYTES
1008 1008
         );
1009
-        if ($mlen > $mlen0) {
1009
+        if ( $mlen > $mlen0 ) {
1010 1010
             $c .= ParagonIE_Sodium_Core32_ChaCha20::streamXorIc(
1011 1011
                 ParagonIE_Sodium_Core32_Util::substr(
1012 1012
                     $plaintext,
@@ -1014,7 +1014,7 @@  discard block
 block discarded – undo
1014 1014
                 ),
1015 1015
                 $nonceLast,
1016 1016
                 $subkey,
1017
-                ParagonIE_Sodium_Core32_Util::store64_le(1)
1017
+                ParagonIE_Sodium_Core32_Util::store64_le( 1 )
1018 1018
             );
1019 1019
         }
1020 1020
         $state = new ParagonIE_Sodium_Core32_Poly1305_State(
@@ -1025,18 +1025,18 @@  discard block
 block discarded – undo
1025 1025
             )
1026 1026
         );
1027 1027
         try {
1028
-            ParagonIE_Sodium_Compat::memzero($block0);
1029
-            ParagonIE_Sodium_Compat::memzero($subkey);
1030
-        } catch (Error $ex) {
1028
+            ParagonIE_Sodium_Compat::memzero( $block0 );
1029
+            ParagonIE_Sodium_Compat::memzero( $subkey );
1030
+        } catch ( Error $ex ) {
1031 1031
             $block0 = null;
1032 1032
             $subkey = null;
1033 1033
         }
1034 1034
 
1035
-        $state->update($c);
1035
+        $state->update( $c );
1036 1036
 
1037 1037
         /** @var string $c - MAC || ciphertext */
1038 1038
         $c = $state->finish() . $c;
1039
-        unset($state);
1039
+        unset( $state );
1040 1040
 
1041 1041
         return $c;
1042 1042
     }
@@ -1052,7 +1052,7 @@  discard block
 block discarded – undo
1052 1052
      * @return string
1053 1053
      * @throws Error
1054 1054
      */
1055
-    public static function secretbox_xchacha20poly1305_open($ciphertext, $nonce, $key)
1055
+    public static function secretbox_xchacha20poly1305_open( $ciphertext, $nonce, $key )
1056 1056
     {
1057 1057
         /** @var string $mac */
1058 1058
         $mac = ParagonIE_Sodium_Core32_Util::substr(
@@ -1068,48 +1068,48 @@  discard block
 block discarded – undo
1068 1068
         );
1069 1069
 
1070 1070
         /** @var int $clen */
1071
-        $clen = ParagonIE_Sodium_Core32_Util::strlen($c);
1071
+        $clen = ParagonIE_Sodium_Core32_Util::strlen( $c );
1072 1072
 
1073 1073
         /** @var string $subkey */
1074
-        $subkey = ParagonIE_Sodium_Core32_HChaCha20::hchacha20($nonce, $key);
1074
+        $subkey = ParagonIE_Sodium_Core32_HChaCha20::hchacha20( $nonce, $key );
1075 1075
 
1076 1076
         /** @var string $block0 */
1077 1077
         $block0 = ParagonIE_Sodium_Core32_ChaCha20::stream(
1078 1078
             64,
1079
-            ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8),
1079
+            ParagonIE_Sodium_Core32_Util::substr( $nonce, 16, 8 ),
1080 1080
             $subkey
1081 1081
         );
1082 1082
         $verified = ParagonIE_Sodium_Core32_Poly1305::onetimeauth_verify(
1083 1083
             $mac,
1084 1084
             $c,
1085
-            ParagonIE_Sodium_Core32_Util::substr($block0, 0, 32)
1085
+            ParagonIE_Sodium_Core32_Util::substr( $block0, 0, 32 )
1086 1086
         );
1087 1087
 
1088
-        if (!$verified) {
1088
+        if ( ! $verified ) {
1089 1089
             try {
1090
-                ParagonIE_Sodium_Compat::memzero($subkey);
1091
-            } catch (Error $ex) {
1090
+                ParagonIE_Sodium_Compat::memzero( $subkey );
1091
+            } catch ( Error $ex ) {
1092 1092
                 $subkey = null;
1093 1093
             }
1094
-            throw new Error('Invalid MAC');
1094
+            throw new Error( 'Invalid MAC' );
1095 1095
         }
1096 1096
 
1097 1097
         /** @var string $m - Decrypted message */
1098 1098
         $m = ParagonIE_Sodium_Core32_Util::xorStrings(
1099
-            ParagonIE_Sodium_Core32_Util::substr($block0, self::secretbox_xchacha20poly1305_ZEROBYTES),
1100
-            ParagonIE_Sodium_Core32_Util::substr($c, 0, self::secretbox_xchacha20poly1305_ZEROBYTES)
1099
+            ParagonIE_Sodium_Core32_Util::substr( $block0, self::secretbox_xchacha20poly1305_ZEROBYTES ),
1100
+            ParagonIE_Sodium_Core32_Util::substr( $c, 0, self::secretbox_xchacha20poly1305_ZEROBYTES )
1101 1101
         );
1102 1102
 
1103
-        if ($clen > self::secretbox_xchacha20poly1305_ZEROBYTES) {
1103
+        if ( $clen > self::secretbox_xchacha20poly1305_ZEROBYTES ) {
1104 1104
             // We had more than 1 block, so let's continue to decrypt the rest.
1105 1105
             $m .= ParagonIE_Sodium_Core32_ChaCha20::streamXorIc(
1106 1106
                 ParagonIE_Sodium_Core32_Util::substr(
1107 1107
                     $c,
1108 1108
                     self::secretbox_xchacha20poly1305_ZEROBYTES
1109 1109
                 ),
1110
-                ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8),
1110
+                ParagonIE_Sodium_Core32_Util::substr( $nonce, 16, 8 ),
1111 1111
                 $subkey,
1112
-                ParagonIE_Sodium_Core32_Util::store64_le(1)
1112
+                ParagonIE_Sodium_Core32_Util::store64_le( 1 )
1113 1113
             );
1114 1114
         }
1115 1115
         return $m;
@@ -1124,9 +1124,9 @@  discard block
 block discarded – undo
1124 1124
      * @param string $sk
1125 1125
      * @return string
1126 1126
      */
1127
-    public static function sign_detached($message, $sk)
1127
+    public static function sign_detached( $message, $sk )
1128 1128
     {
1129
-        return ParagonIE_Sodium_Core32_Ed25519::sign_detached($message, $sk);
1129
+        return ParagonIE_Sodium_Core32_Ed25519::sign_detached( $message, $sk );
1130 1130
     }
1131 1131
 
1132 1132
     /**
@@ -1138,9 +1138,9 @@  discard block
 block discarded – undo
1138 1138
      * @param string $sk
1139 1139
      * @return string
1140 1140
      */
1141
-    public static function sign($message, $sk)
1141
+    public static function sign( $message, $sk )
1142 1142
     {
1143
-        return ParagonIE_Sodium_Core32_Ed25519::sign($message, $sk);
1143
+        return ParagonIE_Sodium_Core32_Ed25519::sign( $message, $sk );
1144 1144
     }
1145 1145
 
1146 1146
     /**
@@ -1152,9 +1152,9 @@  discard block
 block discarded – undo
1152 1152
      * @param string $pk
1153 1153
      * @return string
1154 1154
      */
1155
-    public static function sign_open($signedMessage, $pk)
1155
+    public static function sign_open( $signedMessage, $pk )
1156 1156
     {
1157
-        return ParagonIE_Sodium_Core32_Ed25519::sign_open($signedMessage, $pk);
1157
+        return ParagonIE_Sodium_Core32_Ed25519::sign_open( $signedMessage, $pk );
1158 1158
     }
1159 1159
 
1160 1160
     /**
@@ -1167,8 +1167,8 @@  discard block
 block discarded – undo
1167 1167
      * @param string $pk
1168 1168
      * @return bool
1169 1169
      */
1170
-    public static function sign_verify_detached($signature, $message, $pk)
1170
+    public static function sign_verify_detached( $signature, $message, $pk )
1171 1171
     {
1172
-        return ParagonIE_Sodium_Core32_Ed25519::verify_detached($signature, $message, $pk);
1172
+        return ParagonIE_Sodium_Core32_Ed25519::verify_detached( $signature, $message, $pk );
1173 1173
     }
1174 1174
 }
Please login to merge, or discard this patch.
Braces   +29 added lines, -58 removed lines patch added patch discarded remove patch
@@ -12,8 +12,7 @@  discard block
 block discarded – undo
12 12
  * If you are using this library, you should be using
13 13
  * ParagonIE_Sodium_Compat in your code, not this class.
14 14
  */
15
-abstract class ParagonIE_Sodium_Crypto32
16
-{
15
+abstract class ParagonIE_Sodium_Crypto32 {
17 16
     const aead_chacha20poly1305_KEYBYTES = 32;
18 17
     const aead_chacha20poly1305_NSECBYTES = 0;
19 18
     const aead_chacha20poly1305_NPUBBYTES = 8;
@@ -371,8 +370,7 @@  discard block
 block discarded – undo
371 370
      * @param string $key
372 371
      * @return string
373 372
      */
374
-    public static function auth($message, $key)
375
-    {
373
+    public static function auth($message, $key) {
376 374
         return ParagonIE_Sodium_Core32_Util::substr(
377 375
             hash_hmac('sha512', $message, $key, true),
378 376
             0,
@@ -390,8 +388,7 @@  discard block
 block discarded – undo
390 388
      * @param string $key
391 389
      * @return bool
392 390
      */
393
-    public static function auth_verify($mac, $message, $key)
394
-    {
391
+    public static function auth_verify($mac, $message, $key) {
395 392
         return ParagonIE_Sodium_Core32_Util::hashEquals(
396 393
             $mac,
397 394
             self::auth($message, $key)
@@ -408,8 +405,7 @@  discard block
 block discarded – undo
408 405
      * @param string $keypair
409 406
      * @return string
410 407
      */
411
-    public static function box($plaintext, $nonce, $keypair)
412
-    {
408
+    public static function box($plaintext, $nonce, $keypair) {
413 409
         $c = self::secretbox(
414 410
             $plaintext,
415 411
             $nonce,
@@ -430,8 +426,7 @@  discard block
 block discarded – undo
430 426
      * @param string $publicKey
431 427
      * @return string
432 428
      */
433
-    public static function box_seal($message, $publicKey)
434
-    {
429
+    public static function box_seal($message, $publicKey) {
435 430
         /** @var string $ephemeralKeypair */
436 431
         $ephemeralKeypair = self::box_keypair();
437 432
 
@@ -474,8 +469,7 @@  discard block
 block discarded – undo
474 469
      * @param string $keypair
475 470
      * @return string
476 471
      */
477
-    public static function box_seal_open($message, $keypair)
478
-    {
472
+    public static function box_seal_open($message, $keypair) {
479 473
         /** @var string $ephemeralPK */
480 474
         $ephemeralPK = ParagonIE_Sodium_Core32_Util::substr($message, 0, 32);
481 475
 
@@ -521,8 +515,7 @@  discard block
 block discarded – undo
521 515
      * @param string $pk
522 516
      * @return string
523 517
      */
524
-    public static function box_beforenm($sk, $pk)
525
-    {
518
+    public static function box_beforenm($sk, $pk) {
526 519
         return ParagonIE_Sodium_Core32_HSalsa20::hsalsa20(
527 520
             str_repeat("\x00", 16),
528 521
             self::scalarmult($sk, $pk)
@@ -534,8 +527,7 @@  discard block
 block discarded – undo
534 527
      *
535 528
      * @return string
536 529
      */
537
-    public static function box_keypair()
538
-    {
530
+    public static function box_keypair() {
539 531
         $sKey = random_bytes(32);
540 532
         $pKey = self::scalarmult_base($sKey);
541 533
         return $sKey . $pKey;
@@ -548,8 +540,7 @@  discard block
 block discarded – undo
548 540
      * @param string $pKey
549 541
      * @return string
550 542
      */
551
-    public static function box_keypair_from_secretkey_and_publickey($sKey, $pKey)
552
-    {
543
+    public static function box_keypair_from_secretkey_and_publickey($sKey, $pKey) {
553 544
         return ParagonIE_Sodium_Core32_Util::substr($sKey, 0, 32) .
554 545
             ParagonIE_Sodium_Core32_Util::substr($pKey, 0, 32);
555 546
     }
@@ -561,8 +552,7 @@  discard block
 block discarded – undo
561 552
      * @return string
562 553
      * @throws RangeException
563 554
      */
564
-    public static function box_secretkey($keypair)
565
-    {
555
+    public static function box_secretkey($keypair) {
566 556
         if (ParagonIE_Sodium_Core32_Util::strlen($keypair) !== 64) {
567 557
             throw new RangeException('Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES bytes long.');
568 558
         }
@@ -576,8 +566,7 @@  discard block
 block discarded – undo
576 566
      * @return string
577 567
      * @throws RangeException
578 568
      */
579
-    public static function box_publickey($keypair)
580
-    {
569
+    public static function box_publickey($keypair) {
581 570
         if (ParagonIE_Sodium_Core32_Util::strlen($keypair) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES) {
582 571
             throw new RangeException('Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES bytes long.');
583 572
         }
@@ -591,8 +580,7 @@  discard block
 block discarded – undo
591 580
      * @return string
592 581
      * @throws RangeException
593 582
      */
594
-    public static function box_publickey_from_secretkey($sKey)
595
-    {
583
+    public static function box_publickey_from_secretkey($sKey) {
596 584
         if (ParagonIE_Sodium_Core32_Util::strlen($sKey) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_SECRETKEYBYTES) {
597 585
             throw new RangeException('Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_SECRETKEYBYTES bytes long.');
598 586
         }
@@ -610,8 +598,7 @@  discard block
 block discarded – undo
610 598
      * @param string $keypair
611 599
      * @return string
612 600
      */
613
-    public static function box_open($ciphertext, $nonce, $keypair)
614
-    {
601
+    public static function box_open($ciphertext, $nonce, $keypair) {
615 602
         return self::secretbox_open(
616 603
             $ciphertext,
617 604
             $nonce,
@@ -633,8 +620,7 @@  discard block
 block discarded – undo
633 620
      * @return string
634 621
      * @throws RangeException
635 622
      */
636
-    public static function generichash($message, $key = '', $outlen = 32)
637
-    {
623
+    public static function generichash($message, $key = '', $outlen = 32) {
638 624
         // This ensures that ParagonIE_Sodium_Core32_BLAKE2b::$iv is initialized
639 625
         ParagonIE_Sodium_Core32_BLAKE2b::pseudoConstructor();
640 626
 
@@ -673,8 +659,7 @@  discard block
 block discarded – undo
673 659
      * @return string
674 660
      * @throws TypeError
675 661
      */
676
-    public static function generichash_final($ctx, $outlen = 32)
677
-    {
662
+    public static function generichash_final($ctx, $outlen = 32) {
678 663
         if (!is_string($ctx)) {
679 664
             throw new TypeError('Context must be a string');
680 665
         }
@@ -701,8 +686,7 @@  discard block
 block discarded – undo
701 686
      * @return string
702 687
      * @throws RangeException
703 688
      */
704
-    public static function generichash_init($key = '', $outputLength = 32)
705
-    {
689
+    public static function generichash_init($key = '', $outputLength = 32) {
706 690
         // This ensures that ParagonIE_Sodium_Core32_BLAKE2b::$iv is initialized
707 691
         ParagonIE_Sodium_Core32_BLAKE2b::pseudoConstructor();
708 692
 
@@ -729,8 +713,7 @@  discard block
 block discarded – undo
729 713
      * @param string $message
730 714
      * @return string
731 715
      */
732
-    public static function generichash_update($ctx, $message)
733
-    {
716
+    public static function generichash_update($ctx, $message) {
734 717
         // This ensures that ParagonIE_Sodium_Core32_BLAKE2b::$iv is initialized
735 718
         ParagonIE_Sodium_Core32_BLAKE2b::pseudoConstructor();
736 719
 
@@ -756,8 +739,7 @@  discard block
 block discarded – undo
756 739
      * @param string $server_pk
757 740
      * @return string
758 741
      */
759
-    public static function keyExchange($my_sk, $their_pk, $client_pk, $server_pk)
760
-    {
742
+    public static function keyExchange($my_sk, $their_pk, $client_pk, $server_pk) {
761 743
         return self::generichash(
762 744
             self::scalarmult($my_sk, $their_pk) .
763 745
             $client_pk .
@@ -776,8 +758,7 @@  discard block
 block discarded – undo
776 758
      *
777 759
      * @throws Error
778 760
      */
779
-    public static function scalarmult($sKey, $pKey)
780
-    {
761
+    public static function scalarmult($sKey, $pKey) {
781 762
         $q = ParagonIE_Sodium_Core32_X25519::crypto_scalarmult_curve25519_ref10($sKey, $pKey);
782 763
         self::scalarmult_throw_if_zero($q);
783 764
         return $q;
@@ -792,8 +773,7 @@  discard block
 block discarded – undo
792 773
      *
793 774
      * @throws Error
794 775
      */
795
-    public static function scalarmult_base($secret)
796
-    {
776
+    public static function scalarmult_base($secret) {
797 777
         $q = ParagonIE_Sodium_Core32_X25519::crypto_scalarmult_curve25519_ref10_base($secret);
798 778
         self::scalarmult_throw_if_zero($q);
799 779
         return $q;
@@ -806,8 +786,7 @@  discard block
 block discarded – undo
806 786
      * @return void
807 787
      * @throws Error
808 788
      */
809
-    protected static function scalarmult_throw_if_zero($q)
810
-    {
789
+    protected static function scalarmult_throw_if_zero($q) {
811 790
         $d = 0;
812 791
         for ($i = 0; $i < self::box_curve25519xsalsa20poly1305_SECRETKEYBYTES; ++$i) {
813 792
             $d |= ParagonIE_Sodium_Core32_Util::chrToInt($q[$i]);
@@ -829,8 +808,7 @@  discard block
 block discarded – undo
829 808
      * @param string $key
830 809
      * @return string
831 810
      */
832
-    public static function secretbox($plaintext, $nonce, $key)
833
-    {
811
+    public static function secretbox($plaintext, $nonce, $key) {
834 812
         /** @var string $subkey */
835 813
         $subkey = ParagonIE_Sodium_Core32_HSalsa20::hsalsa20($nonce, $key);
836 814
 
@@ -903,8 +881,7 @@  discard block
 block discarded – undo
903 881
      * @return string
904 882
      * @throws Error
905 883
      */
906
-    public static function secretbox_open($ciphertext, $nonce, $key)
907
-    {
884
+    public static function secretbox_open($ciphertext, $nonce, $key) {
908 885
         /** @var string $mac */
909 886
         $mac = ParagonIE_Sodium_Core32_Util::substr(
910 887
             $ciphertext,
@@ -974,8 +951,7 @@  discard block
 block discarded – undo
974 951
      * @param string $key
975 952
      * @return string
976 953
      */
977
-    public static function secretbox_xchacha20poly1305($plaintext, $nonce, $key)
978
-    {
954
+    public static function secretbox_xchacha20poly1305($plaintext, $nonce, $key) {
979 955
         /** @var string $subkey */
980 956
         $subkey = ParagonIE_Sodium_Core32_HChaCha20::hChaCha20(
981 957
             ParagonIE_Sodium_Core32_Util::substr($nonce, 0, 16),
@@ -1052,8 +1028,7 @@  discard block
 block discarded – undo
1052 1028
      * @return string
1053 1029
      * @throws Error
1054 1030
      */
1055
-    public static function secretbox_xchacha20poly1305_open($ciphertext, $nonce, $key)
1056
-    {
1031
+    public static function secretbox_xchacha20poly1305_open($ciphertext, $nonce, $key) {
1057 1032
         /** @var string $mac */
1058 1033
         $mac = ParagonIE_Sodium_Core32_Util::substr(
1059 1034
             $ciphertext,
@@ -1124,8 +1099,7 @@  discard block
 block discarded – undo
1124 1099
      * @param string $sk
1125 1100
      * @return string
1126 1101
      */
1127
-    public static function sign_detached($message, $sk)
1128
-    {
1102
+    public static function sign_detached($message, $sk) {
1129 1103
         return ParagonIE_Sodium_Core32_Ed25519::sign_detached($message, $sk);
1130 1104
     }
1131 1105
 
@@ -1138,8 +1112,7 @@  discard block
 block discarded – undo
1138 1112
      * @param string $sk
1139 1113
      * @return string
1140 1114
      */
1141
-    public static function sign($message, $sk)
1142
-    {
1115
+    public static function sign($message, $sk) {
1143 1116
         return ParagonIE_Sodium_Core32_Ed25519::sign($message, $sk);
1144 1117
     }
1145 1118
 
@@ -1152,8 +1125,7 @@  discard block
 block discarded – undo
1152 1125
      * @param string $pk
1153 1126
      * @return string
1154 1127
      */
1155
-    public static function sign_open($signedMessage, $pk)
1156
-    {
1128
+    public static function sign_open($signedMessage, $pk) {
1157 1129
         return ParagonIE_Sodium_Core32_Ed25519::sign_open($signedMessage, $pk);
1158 1130
     }
1159 1131
 
@@ -1167,8 +1139,7 @@  discard block
 block discarded – undo
1167 1139
      * @param string $pk
1168 1140
      * @return bool
1169 1141
      */
1170
-    public static function sign_verify_detached($signature, $message, $pk)
1171
-    {
1142
+    public static function sign_verify_detached($signature, $message, $pk) {
1172 1143
         return ParagonIE_Sodium_Core32_Ed25519::verify_detached($signature, $message, $pk);
1173 1144
     }
1174 1145
 }
Please login to merge, or discard this patch.
src/library/sodium_compat/src/Core32/Util.php 2 patches
Indentation   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -1,7 +1,7 @@
 block discarded – undo
1 1
 <?php
2 2
 
3 3
 if (class_exists('ParagonIE_Sodium_Core32_Util', false)) {
4
-    return;
4
+	return;
5 5
 }
6 6
 
7 7
 /**
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -1,6 +1,6 @@
 block discarded – undo
1 1
 <?php
2 2
 
3
-if (class_exists('ParagonIE_Sodium_Core32_Util', false)) {
3
+if ( class_exists( 'ParagonIE_Sodium_Core32_Util', false ) ) {
4 4
     return;
5 5
 }
6 6
 
Please login to merge, or discard this patch.
src/library/sodium_compat/src/Core32/Poly1305.php 3 patches
Indentation   +46 added lines, -46 removed lines patch added patch discarded remove patch
@@ -1,7 +1,7 @@  discard block
 block discarded – undo
1 1
 <?php
2 2
 
3 3
 if (class_exists('ParagonIE_Sodium_Core32_Poly1305', false)) {
4
-    return;
4
+	return;
5 5
 }
6 6
 
7 7
 /**
@@ -9,51 +9,51 @@  discard block
 block discarded – undo
9 9
  */
10 10
 abstract class ParagonIE_Sodium_Core32_Poly1305 extends ParagonIE_Sodium_Core32_Util
11 11
 {
12
-    const BLOCK_SIZE = 16;
12
+	const BLOCK_SIZE = 16;
13 13
 
14
-    /**
15
-     * @internal You should not use this directly from another application
16
-     *
17
-     * @param string $m
18
-     * @param string $key
19
-     * @return string
20
-     */
21
-    public static function onetimeauth($m, $key)
22
-    {
23
-        if (self::strlen($key) < 32) {
24
-            throw new InvalidArgumentException(
25
-                'Key must be 32 bytes long.'
26
-            );
27
-        }
28
-        $state = new ParagonIE_Sodium_Core32_Poly1305_State(
29
-            self::substr($key, 0, 32)
30
-        );
31
-        return $state
32
-            ->update($m)
33
-            ->finish();
34
-    }
14
+	/**
15
+	 * @internal You should not use this directly from another application
16
+	 *
17
+	 * @param string $m
18
+	 * @param string $key
19
+	 * @return string
20
+	 */
21
+	public static function onetimeauth($m, $key)
22
+	{
23
+		if (self::strlen($key) < 32) {
24
+			throw new InvalidArgumentException(
25
+				'Key must be 32 bytes long.'
26
+			);
27
+		}
28
+		$state = new ParagonIE_Sodium_Core32_Poly1305_State(
29
+			self::substr($key, 0, 32)
30
+		);
31
+		return $state
32
+			->update($m)
33
+			->finish();
34
+	}
35 35
 
36
-    /**
37
-     * @internal You should not use this directly from another application
38
-     *
39
-     * @param string $mac
40
-     * @param string $m
41
-     * @param string $key
42
-     * @return bool
43
-     */
44
-    public static function onetimeauth_verify($mac, $m, $key)
45
-    {
46
-        if (self::strlen($key) < 32) {
47
-            throw new InvalidArgumentException(
48
-                'Key must be 32 bytes long.'
49
-            );
50
-        }
51
-        $state = new ParagonIE_Sodium_Core32_Poly1305_State(
52
-            self::substr($key, 0, 32)
53
-        );
54
-        $calc = $state
55
-            ->update($m)
56
-            ->finish();
57
-        return self::verify_16($calc, $mac);
58
-    }
36
+	/**
37
+	 * @internal You should not use this directly from another application
38
+	 *
39
+	 * @param string $mac
40
+	 * @param string $m
41
+	 * @param string $key
42
+	 * @return bool
43
+	 */
44
+	public static function onetimeauth_verify($mac, $m, $key)
45
+	{
46
+		if (self::strlen($key) < 32) {
47
+			throw new InvalidArgumentException(
48
+				'Key must be 32 bytes long.'
49
+			);
50
+		}
51
+		$state = new ParagonIE_Sodium_Core32_Poly1305_State(
52
+			self::substr($key, 0, 32)
53
+		);
54
+		$calc = $state
55
+			->update($m)
56
+			->finish();
57
+		return self::verify_16($calc, $mac);
58
+	}
59 59
 }
Please login to merge, or discard this patch.
Spacing   +10 added lines, -10 removed lines patch added patch discarded remove patch
@@ -1,6 +1,6 @@  discard block
 block discarded – undo
1 1
 <?php
2 2
 
3
-if (class_exists('ParagonIE_Sodium_Core32_Poly1305', false)) {
3
+if ( class_exists( 'ParagonIE_Sodium_Core32_Poly1305', false ) ) {
4 4
     return;
5 5
 }
6 6
 
@@ -18,18 +18,18 @@  discard block
 block discarded – undo
18 18
      * @param string $key
19 19
      * @return string
20 20
      */
21
-    public static function onetimeauth($m, $key)
21
+    public static function onetimeauth( $m, $key )
22 22
     {
23
-        if (self::strlen($key) < 32) {
23
+        if ( self::strlen( $key ) < 32 ) {
24 24
             throw new InvalidArgumentException(
25 25
                 'Key must be 32 bytes long.'
26 26
             );
27 27
         }
28 28
         $state = new ParagonIE_Sodium_Core32_Poly1305_State(
29
-            self::substr($key, 0, 32)
29
+            self::substr( $key, 0, 32 )
30 30
         );
31 31
         return $state
32
-            ->update($m)
32
+            ->update( $m )
33 33
             ->finish();
34 34
     }
35 35
 
@@ -41,19 +41,19 @@  discard block
 block discarded – undo
41 41
      * @param string $key
42 42
      * @return bool
43 43
      */
44
-    public static function onetimeauth_verify($mac, $m, $key)
44
+    public static function onetimeauth_verify( $mac, $m, $key )
45 45
     {
46
-        if (self::strlen($key) < 32) {
46
+        if ( self::strlen( $key ) < 32 ) {
47 47
             throw new InvalidArgumentException(
48 48
                 'Key must be 32 bytes long.'
49 49
             );
50 50
         }
51 51
         $state = new ParagonIE_Sodium_Core32_Poly1305_State(
52
-            self::substr($key, 0, 32)
52
+            self::substr( $key, 0, 32 )
53 53
         );
54 54
         $calc = $state
55
-            ->update($m)
55
+            ->update( $m )
56 56
             ->finish();
57
-        return self::verify_16($calc, $mac);
57
+        return self::verify_16( $calc, $mac );
58 58
     }
59 59
 }
Please login to merge, or discard this patch.
Braces   +3 added lines, -6 removed lines patch added patch discarded remove patch
@@ -7,8 +7,7 @@  discard block
 block discarded – undo
7 7
 /**
8 8
  * Class ParagonIE_Sodium_Core32_Poly1305
9 9
  */
10
-abstract class ParagonIE_Sodium_Core32_Poly1305 extends ParagonIE_Sodium_Core32_Util
11
-{
10
+abstract class ParagonIE_Sodium_Core32_Poly1305 extends ParagonIE_Sodium_Core32_Util {
12 11
     const BLOCK_SIZE = 16;
13 12
 
14 13
     /**
@@ -18,8 +17,7 @@  discard block
 block discarded – undo
18 17
      * @param string $key
19 18
      * @return string
20 19
      */
21
-    public static function onetimeauth($m, $key)
22
-    {
20
+    public static function onetimeauth($m, $key) {
23 21
         if (self::strlen($key) < 32) {
24 22
             throw new InvalidArgumentException(
25 23
                 'Key must be 32 bytes long.'
@@ -41,8 +39,7 @@  discard block
 block discarded – undo
41 39
      * @param string $key
42 40
      * @return bool
43 41
      */
44
-    public static function onetimeauth_verify($mac, $m, $key)
45
-    {
42
+    public static function onetimeauth_verify($mac, $m, $key) {
46 43
         if (self::strlen($key) < 32) {
47 44
             throw new InvalidArgumentException(
48 45
                 'Key must be 32 bytes long.'
Please login to merge, or discard this patch.
src/library/sodium_compat/src/Core32/Ed25519.php 3 patches
Indentation   +407 added lines, -407 removed lines patch added patch discarded remove patch
@@ -1,7 +1,7 @@  discard block
 block discarded – undo
1 1
 <?php
2 2
 
3 3
 if (class_exists('ParagonIE_Sodium_Core32_Ed25519', false)) {
4
-    return;
4
+	return;
5 5
 }
6 6
 
7 7
 /**
@@ -9,410 +9,410 @@  discard block
 block discarded – undo
9 9
  */
10 10
 abstract class ParagonIE_Sodium_Core32_Ed25519 extends ParagonIE_Sodium_Core32_Curve25519
11 11
 {
12
-    const KEYPAIR_BYTES = 96;
13
-    const SEED_BYTES = 32;
14
-
15
-    /**
16
-     * @internal You should not use this directly from another application
17
-     *
18
-     * @return string (96 bytes)
19
-     */
20
-    public static function keypair()
21
-    {
22
-        $seed = random_bytes(self::SEED_BYTES);
23
-        $pk = '';
24
-        $sk = '';
25
-        self::seed_keypair($pk, $sk, $seed);
26
-        return $sk . $pk;
27
-    }
28
-
29
-    /**
30
-     * @internal You should not use this directly from another application
31
-     *
32
-     * @param string $pk
33
-     * @param string $sk
34
-     * @param string $seed
35
-     * @return string
36
-     */
37
-    public static function seed_keypair(&$pk, &$sk, $seed)
38
-    {
39
-        if (self::strlen($seed) !== self::SEED_BYTES) {
40
-            throw new RangeException('crypto_sign keypair seed must be 32 bytes long');
41
-        }
42
-
43
-        /** @var string $pk */
44
-        $pk = self::publickey_from_secretkey($seed);
45
-        $sk = $seed . $pk;
46
-        return $sk;
47
-    }
48
-
49
-    /**
50
-     * @internal You should not use this directly from another application
51
-     *
52
-     * @param string $keypair
53
-     * @return string
54
-     */
55
-    public static function secretkey($keypair)
56
-    {
57
-        if (self::strlen($keypair) !== self::KEYPAIR_BYTES) {
58
-            throw new RangeException('crypto_sign keypair must be 96 bytes long');
59
-        }
60
-        return self::substr($keypair, 0, 64);
61
-    }
62
-
63
-    /**
64
-     * @internal You should not use this directly from another application
65
-     *
66
-     * @param string $keypair
67
-     * @return string
68
-     */
69
-    public static function publickey($keypair)
70
-    {
71
-        if (self::strlen($keypair) !== self::KEYPAIR_BYTES) {
72
-            throw new RangeException('crypto_sign keypair must be 96 bytes long');
73
-        }
74
-        return self::substr($keypair, 64, 32);
75
-    }
76
-
77
-    /**
78
-     * @internal You should not use this directly from another application
79
-     *
80
-     * @param string $sk
81
-     * @return string
82
-     */
83
-    public static function publickey_from_secretkey($sk)
84
-    {
85
-        /** @var string $sk */
86
-        $sk = hash('sha512', self::substr($sk, 0, 32), true);
87
-        $sk[0] = self::intToChr(
88
-            self::chrToInt($sk[0]) & 248
89
-        );
90
-        $sk[31] = self::intToChr(
91
-            (self::chrToInt($sk[31]) & 63) | 64
92
-        );
93
-        return self::sk_to_pk($sk);
94
-    }
95
-
96
-    /**
97
-     * @internal You should not use this directly from another application
98
-     *
99
-     * @param string $sk
100
-     * @return string
101
-     */
102
-    public static function sk_to_pk($sk)
103
-    {
104
-        return self::ge_p3_tobytes(
105
-            self::ge_scalarmult_base(
106
-                self::substr($sk, 0, 32)
107
-            )
108
-        );
109
-    }
110
-
111
-    /**
112
-     * @internal You should not use this directly from another application
113
-     *
114
-     * @param string $message
115
-     * @param string $sk
116
-     * @return string
117
-     */
118
-    public static function sign($message, $sk)
119
-    {
120
-        /** @var string $signature */
121
-        $signature = self::sign_detached($message, $sk);
122
-        return $signature . $message;
123
-    }
124
-
125
-    /**
126
-     * @internal You should not use this directly from another application
127
-     *
128
-     * @param string $message A signed message
129
-     * @param string $pk      Public key
130
-     * @return string         Message (without signature)
131
-     * @throws Exception
132
-     */
133
-    public static function sign_open($message, $pk)
134
-    {
135
-        /** @var string $signature */
136
-        $signature = self::substr($message, 0, 64);
137
-
138
-        /** @var string $message */
139
-        $message = self::substr($message, 64);
140
-
141
-        if (self::verify_detached($signature, $message, $pk)) {
142
-            return $message;
143
-        }
144
-        throw new Exception('Invalid signature');
145
-    }
146
-
147
-    /**
148
-     * @internal You should not use this directly from another application
149
-     *
150
-     * @param string $message
151
-     * @param string $sk
152
-     * @return string
153
-     */
154
-    public static function sign_detached($message, $sk)
155
-    {
156
-        # crypto_hash_sha512(az, sk, 32);
157
-        $az =  hash('sha512', self::substr($sk, 0, 32), true);
158
-
159
-        # az[0] &= 248;
160
-        # az[31] &= 63;
161
-        # az[31] |= 64;
162
-        $az[0] = self::intToChr(self::chrToInt($az[0]) & 248);
163
-        $az[31] = self::intToChr((self::chrToInt($az[31]) & 63) | 64);
164
-
165
-        # crypto_hash_sha512_init(&hs);
166
-        # crypto_hash_sha512_update(&hs, az + 32, 32);
167
-        # crypto_hash_sha512_update(&hs, m, mlen);
168
-        # crypto_hash_sha512_final(&hs, nonce);
169
-        $hs = hash_init('sha512');
170
-        hash_update($hs, self::substr($az, 32, 32));
171
-        hash_update($hs, $message);
172
-        $nonceHash = hash_final($hs, true);
173
-
174
-        # memmove(sig + 32, sk + 32, 32);
175
-        $pk = self::substr($sk, 32, 32);
176
-
177
-        # sc_reduce(nonce);
178
-        # ge_scalarmult_base(&R, nonce);
179
-        # ge_p3_tobytes(sig, &R);
180
-        $nonce = self::sc_reduce($nonceHash) . self::substr($nonceHash, 32);
181
-        $sig = self::ge_p3_tobytes(
182
-            self::ge_scalarmult_base($nonce)
183
-        );
184
-
185
-        # crypto_hash_sha512_init(&hs);
186
-        # crypto_hash_sha512_update(&hs, sig, 64);
187
-        # crypto_hash_sha512_update(&hs, m, mlen);
188
-        # crypto_hash_sha512_final(&hs, hram);
189
-        $hs = hash_init('sha512');
190
-        hash_update($hs, self::substr($sig, 0, 32));
191
-        hash_update($hs, self::substr($pk, 0, 32));
192
-        hash_update($hs, $message);
193
-        $hramHash = hash_final($hs, true);
194
-
195
-        # sc_reduce(hram);
196
-        # sc_muladd(sig + 32, hram, az, nonce);
197
-        $hram = self::sc_reduce($hramHash);
198
-        $sigAfter = self::sc_muladd($hram, $az, $nonce);
199
-        $sig = self::substr($sig, 0, 32) . self::substr($sigAfter, 0, 32);
200
-
201
-        try {
202
-            ParagonIE_Sodium_Compat::memzero($az);
203
-        } catch (Error $ex) {
204
-            $az = null;
205
-        }
206
-        return $sig;
207
-    }
208
-
209
-    /**
210
-     * @internal You should not use this directly from another application
211
-     *
212
-     * @param string $sig
213
-     * @param string $message
214
-     * @param string $pk
215
-     * @return bool
216
-     * @throws Exception
217
-     */
218
-    public static function verify_detached($sig, $message, $pk)
219
-    {
220
-        if (self::strlen($sig) < 64) {
221
-            throw new Exception('Signature is too short');
222
-        }
223
-        if (self::check_S_lt_L(self::substr($sig, 32, 32))) {
224
-            throw new Exception('S < L - Invalid signature');
225
-        }
226
-        if (self::small_order($sig)) {
227
-            throw new Exception('Signature is on too small of an order');
228
-        }
229
-        if ((self::chrToInt($sig[63]) & 224) !== 0) {
230
-            throw new Exception('Invalid signature');
231
-        }
232
-        $d = 0;
233
-        for ($i = 0; $i < 32; ++$i) {
234
-            $d |= self::chrToInt($pk[$i]);
235
-        }
236
-        if ($d === 0) {
237
-            throw new Exception('All zero public key');
238
-        }
239
-
240
-        /** @var bool The original value of ParagonIE_Sodium_Compat::$fastMult */
241
-        $orig = ParagonIE_Sodium_Compat::$fastMult;
242
-
243
-        // Set ParagonIE_Sodium_Compat::$fastMult to true to speed up verification.
244
-        ParagonIE_Sodium_Compat::$fastMult = true;
245
-
246
-        /** @var ParagonIE_Sodium_Core32_Curve25519_Ge_P3 $A */
247
-        $A = self::ge_frombytes_negate_vartime($pk);
248
-
249
-        /** @var string $hDigest */
250
-        $hDigest = hash(
251
-            'sha512',
252
-            self::substr($sig, 0, 32) .
253
-            self::substr($pk, 0, 32) .
254
-            $message,
255
-            true
256
-        );
257
-
258
-        /** @var string $h */
259
-        $h = self::sc_reduce($hDigest) . self::substr($hDigest, 32);
260
-
261
-        /** @var ParagonIE_Sodium_Core32_Curve25519_Ge_P2 $R */
262
-        $R = self::ge_double_scalarmult_vartime(
263
-            $h,
264
-            $A,
265
-            self::substr($sig, 32)
266
-        );
267
-
268
-        /** @var string $rcheck */
269
-        $rcheck = self::ge_tobytes($R);
270
-
271
-        // Reset ParagonIE_Sodium_Compat::$fastMult to what it was before.
272
-        ParagonIE_Sodium_Compat::$fastMult = $orig;
273
-
274
-        return self::verify_32($rcheck, self::substr($sig, 0, 32));
275
-    }
276
-
277
-    /**
278
-     * @internal You should not use this directly from another application
279
-     *
280
-     * @param string $S
281
-     * @return bool
282
-     * @throws Exception
283
-     */
284
-    public static function check_S_lt_L($S)
285
-    {
286
-        if (self::strlen($S) < 32) {
287
-            throw new Exception('Signature must be 32 bytes');
288
-        }
289
-        static $L = array(
290
-            0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58,
291
-            0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14,
292
-            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
293
-            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10
294
-        );
295
-        $c = 0;
296
-        $n = 1;
297
-        $i = 32;
298
-
299
-        do {
300
-            --$i;
301
-            $x = self::chrToInt($S[$i]);
302
-            $c |= (
303
-                (($x - $L[$i]) >> 8) & $n
304
-            );
305
-            $n &= (
306
-                (($x ^ $L[$i]) - 1) >> 8
307
-            );
308
-        } while ($i !== 0);
309
-
310
-        return $c === 0;
311
-    }
312
-
313
-    /**
314
-     * @param string $R
315
-     * @return bool
316
-     */
317
-    public static function small_order($R)
318
-    {
319
-        static $blacklist = array(
320
-            /* 0 (order 4) */
321
-            array(
322
-                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
323
-                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
324
-                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
325
-                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
326
-            ),
327
-            /* 1 (order 1) */
328
-            array(
329
-                0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
330
-                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
331
-                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
332
-                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
333
-            ),
334
-            /* 2707385501144840649318225287225658788936804267575313519463743609750303402022 (order 8) */
335
-            array(
336
-                0x26, 0xe8, 0x95, 0x8f, 0xc2, 0xb2, 0x27, 0xb0,
337
-                0x45, 0xc3, 0xf4, 0x89, 0xf2, 0xef, 0x98, 0xf0,
338
-                0xd5, 0xdf, 0xac, 0x05, 0xd3, 0xc6, 0x33, 0x39,
339
-                0xb1, 0x38, 0x02, 0x88, 0x6d, 0x53, 0xfc, 0x05
340
-            ),
341
-            /* 55188659117513257062467267217118295137698188065244968500265048394206261417927 (order 8) */
342
-            array(
343
-                0xc7, 0x17, 0x6a, 0x70, 0x3d, 0x4d, 0xd8, 0x4f,
344
-                0xba, 0x3c, 0x0b, 0x76, 0x0d, 0x10, 0x67, 0x0f,
345
-                0x2a, 0x20, 0x53, 0xfa, 0x2c, 0x39, 0xcc, 0xc6,
346
-                0x4e, 0xc7, 0xfd, 0x77, 0x92, 0xac, 0x03, 0x7a
347
-            ),
348
-            /* p-1 (order 2) */
349
-            array(
350
-                0x13, 0xe8, 0x95, 0x8f, 0xc2, 0xb2, 0x27, 0xb0,
351
-                0x45, 0xc3, 0xf4, 0x89, 0xf2, 0xef, 0x98, 0xf0,
352
-                0xd5, 0xdf, 0xac, 0x05, 0xd3, 0xc6, 0x33, 0x39,
353
-                0xb1, 0x38, 0x02, 0x88, 0x6d, 0x53, 0xfc, 0x85
354
-            ),
355
-            /* p (order 4) */
356
-            array(
357
-                0xb4, 0x17, 0x6a, 0x70, 0x3d, 0x4d, 0xd8, 0x4f,
358
-                0xba, 0x3c, 0x0b, 0x76, 0x0d, 0x10, 0x67, 0x0f,
359
-                0x2a, 0x20, 0x53, 0xfa, 0x2c, 0x39, 0xcc, 0xc6,
360
-                0x4e, 0xc7, 0xfd, 0x77, 0x92, 0xac, 0x03, 0xfa
361
-            ),
362
-            /* p+1 (order 1) */
363
-            array(
364
-                0xec, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
365
-                0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
366
-                0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
367
-                0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f
368
-            ),
369
-            /* p+2707385501144840649318225287225658788936804267575313519463743609750303402022 (order 8) */
370
-            array(
371
-                0xed, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
372
-                0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
373
-                0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
374
-                0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f
375
-            ),
376
-            /* p+55188659117513257062467267217118295137698188065244968500265048394206261417927 (order 8) */
377
-            array(
378
-                0xee, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
379
-                0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
380
-                0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
381
-                0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f
382
-            ),
383
-            /* 2p-1 (order 2) */
384
-            array(
385
-                0xd9, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
386
-                0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
387
-                0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
388
-                0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
389
-            ),
390
-            /* 2p (order 4) */
391
-            array(
392
-                0xda, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
393
-                0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
394
-                0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
395
-                0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
396
-            ),
397
-            /* 2p+1 (order 1) */
398
-            array(
399
-                0xdb, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
400
-                0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
401
-                0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
402
-                0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
403
-            )
404
-        );
405
-        $countBlacklist = count($blacklist);
406
-
407
-        for ($i = 0; $i < $countBlacklist; ++$i) {
408
-            $c = 0;
409
-            for ($j = 0; $j < 32; ++$j) {
410
-                $c |= self::chrToInt($R[$j]) ^ $blacklist[$i][$j];
411
-            }
412
-            if ($c === 0) {
413
-                return true;
414
-            }
415
-        }
416
-        return false;
417
-    }
12
+	const KEYPAIR_BYTES = 96;
13
+	const SEED_BYTES = 32;
14
+
15
+	/**
16
+	 * @internal You should not use this directly from another application
17
+	 *
18
+	 * @return string (96 bytes)
19
+	 */
20
+	public static function keypair()
21
+	{
22
+		$seed = random_bytes(self::SEED_BYTES);
23
+		$pk = '';
24
+		$sk = '';
25
+		self::seed_keypair($pk, $sk, $seed);
26
+		return $sk . $pk;
27
+	}
28
+
29
+	/**
30
+	 * @internal You should not use this directly from another application
31
+	 *
32
+	 * @param string $pk
33
+	 * @param string $sk
34
+	 * @param string $seed
35
+	 * @return string
36
+	 */
37
+	public static function seed_keypair(&$pk, &$sk, $seed)
38
+	{
39
+		if (self::strlen($seed) !== self::SEED_BYTES) {
40
+			throw new RangeException('crypto_sign keypair seed must be 32 bytes long');
41
+		}
42
+
43
+		/** @var string $pk */
44
+		$pk = self::publickey_from_secretkey($seed);
45
+		$sk = $seed . $pk;
46
+		return $sk;
47
+	}
48
+
49
+	/**
50
+	 * @internal You should not use this directly from another application
51
+	 *
52
+	 * @param string $keypair
53
+	 * @return string
54
+	 */
55
+	public static function secretkey($keypair)
56
+	{
57
+		if (self::strlen($keypair) !== self::KEYPAIR_BYTES) {
58
+			throw new RangeException('crypto_sign keypair must be 96 bytes long');
59
+		}
60
+		return self::substr($keypair, 0, 64);
61
+	}
62
+
63
+	/**
64
+	 * @internal You should not use this directly from another application
65
+	 *
66
+	 * @param string $keypair
67
+	 * @return string
68
+	 */
69
+	public static function publickey($keypair)
70
+	{
71
+		if (self::strlen($keypair) !== self::KEYPAIR_BYTES) {
72
+			throw new RangeException('crypto_sign keypair must be 96 bytes long');
73
+		}
74
+		return self::substr($keypair, 64, 32);
75
+	}
76
+
77
+	/**
78
+	 * @internal You should not use this directly from another application
79
+	 *
80
+	 * @param string $sk
81
+	 * @return string
82
+	 */
83
+	public static function publickey_from_secretkey($sk)
84
+	{
85
+		/** @var string $sk */
86
+		$sk = hash('sha512', self::substr($sk, 0, 32), true);
87
+		$sk[0] = self::intToChr(
88
+			self::chrToInt($sk[0]) & 248
89
+		);
90
+		$sk[31] = self::intToChr(
91
+			(self::chrToInt($sk[31]) & 63) | 64
92
+		);
93
+		return self::sk_to_pk($sk);
94
+	}
95
+
96
+	/**
97
+	 * @internal You should not use this directly from another application
98
+	 *
99
+	 * @param string $sk
100
+	 * @return string
101
+	 */
102
+	public static function sk_to_pk($sk)
103
+	{
104
+		return self::ge_p3_tobytes(
105
+			self::ge_scalarmult_base(
106
+				self::substr($sk, 0, 32)
107
+			)
108
+		);
109
+	}
110
+
111
+	/**
112
+	 * @internal You should not use this directly from another application
113
+	 *
114
+	 * @param string $message
115
+	 * @param string $sk
116
+	 * @return string
117
+	 */
118
+	public static function sign($message, $sk)
119
+	{
120
+		/** @var string $signature */
121
+		$signature = self::sign_detached($message, $sk);
122
+		return $signature . $message;
123
+	}
124
+
125
+	/**
126
+	 * @internal You should not use this directly from another application
127
+	 *
128
+	 * @param string $message A signed message
129
+	 * @param string $pk      Public key
130
+	 * @return string         Message (without signature)
131
+	 * @throws Exception
132
+	 */
133
+	public static function sign_open($message, $pk)
134
+	{
135
+		/** @var string $signature */
136
+		$signature = self::substr($message, 0, 64);
137
+
138
+		/** @var string $message */
139
+		$message = self::substr($message, 64);
140
+
141
+		if (self::verify_detached($signature, $message, $pk)) {
142
+			return $message;
143
+		}
144
+		throw new Exception('Invalid signature');
145
+	}
146
+
147
+	/**
148
+	 * @internal You should not use this directly from another application
149
+	 *
150
+	 * @param string $message
151
+	 * @param string $sk
152
+	 * @return string
153
+	 */
154
+	public static function sign_detached($message, $sk)
155
+	{
156
+		# crypto_hash_sha512(az, sk, 32);
157
+		$az =  hash('sha512', self::substr($sk, 0, 32), true);
158
+
159
+		# az[0] &= 248;
160
+		# az[31] &= 63;
161
+		# az[31] |= 64;
162
+		$az[0] = self::intToChr(self::chrToInt($az[0]) & 248);
163
+		$az[31] = self::intToChr((self::chrToInt($az[31]) & 63) | 64);
164
+
165
+		# crypto_hash_sha512_init(&hs);
166
+		# crypto_hash_sha512_update(&hs, az + 32, 32);
167
+		# crypto_hash_sha512_update(&hs, m, mlen);
168
+		# crypto_hash_sha512_final(&hs, nonce);
169
+		$hs = hash_init('sha512');
170
+		hash_update($hs, self::substr($az, 32, 32));
171
+		hash_update($hs, $message);
172
+		$nonceHash = hash_final($hs, true);
173
+
174
+		# memmove(sig + 32, sk + 32, 32);
175
+		$pk = self::substr($sk, 32, 32);
176
+
177
+		# sc_reduce(nonce);
178
+		# ge_scalarmult_base(&R, nonce);
179
+		# ge_p3_tobytes(sig, &R);
180
+		$nonce = self::sc_reduce($nonceHash) . self::substr($nonceHash, 32);
181
+		$sig = self::ge_p3_tobytes(
182
+			self::ge_scalarmult_base($nonce)
183
+		);
184
+
185
+		# crypto_hash_sha512_init(&hs);
186
+		# crypto_hash_sha512_update(&hs, sig, 64);
187
+		# crypto_hash_sha512_update(&hs, m, mlen);
188
+		# crypto_hash_sha512_final(&hs, hram);
189
+		$hs = hash_init('sha512');
190
+		hash_update($hs, self::substr($sig, 0, 32));
191
+		hash_update($hs, self::substr($pk, 0, 32));
192
+		hash_update($hs, $message);
193
+		$hramHash = hash_final($hs, true);
194
+
195
+		# sc_reduce(hram);
196
+		# sc_muladd(sig + 32, hram, az, nonce);
197
+		$hram = self::sc_reduce($hramHash);
198
+		$sigAfter = self::sc_muladd($hram, $az, $nonce);
199
+		$sig = self::substr($sig, 0, 32) . self::substr($sigAfter, 0, 32);
200
+
201
+		try {
202
+			ParagonIE_Sodium_Compat::memzero($az);
203
+		} catch (Error $ex) {
204
+			$az = null;
205
+		}
206
+		return $sig;
207
+	}
208
+
209
+	/**
210
+	 * @internal You should not use this directly from another application
211
+	 *
212
+	 * @param string $sig
213
+	 * @param string $message
214
+	 * @param string $pk
215
+	 * @return bool
216
+	 * @throws Exception
217
+	 */
218
+	public static function verify_detached($sig, $message, $pk)
219
+	{
220
+		if (self::strlen($sig) < 64) {
221
+			throw new Exception('Signature is too short');
222
+		}
223
+		if (self::check_S_lt_L(self::substr($sig, 32, 32))) {
224
+			throw new Exception('S < L - Invalid signature');
225
+		}
226
+		if (self::small_order($sig)) {
227
+			throw new Exception('Signature is on too small of an order');
228
+		}
229
+		if ((self::chrToInt($sig[63]) & 224) !== 0) {
230
+			throw new Exception('Invalid signature');
231
+		}
232
+		$d = 0;
233
+		for ($i = 0; $i < 32; ++$i) {
234
+			$d |= self::chrToInt($pk[$i]);
235
+		}
236
+		if ($d === 0) {
237
+			throw new Exception('All zero public key');
238
+		}
239
+
240
+		/** @var bool The original value of ParagonIE_Sodium_Compat::$fastMult */
241
+		$orig = ParagonIE_Sodium_Compat::$fastMult;
242
+
243
+		// Set ParagonIE_Sodium_Compat::$fastMult to true to speed up verification.
244
+		ParagonIE_Sodium_Compat::$fastMult = true;
245
+
246
+		/** @var ParagonIE_Sodium_Core32_Curve25519_Ge_P3 $A */
247
+		$A = self::ge_frombytes_negate_vartime($pk);
248
+
249
+		/** @var string $hDigest */
250
+		$hDigest = hash(
251
+			'sha512',
252
+			self::substr($sig, 0, 32) .
253
+			self::substr($pk, 0, 32) .
254
+			$message,
255
+			true
256
+		);
257
+
258
+		/** @var string $h */
259
+		$h = self::sc_reduce($hDigest) . self::substr($hDigest, 32);
260
+
261
+		/** @var ParagonIE_Sodium_Core32_Curve25519_Ge_P2 $R */
262
+		$R = self::ge_double_scalarmult_vartime(
263
+			$h,
264
+			$A,
265
+			self::substr($sig, 32)
266
+		);
267
+
268
+		/** @var string $rcheck */
269
+		$rcheck = self::ge_tobytes($R);
270
+
271
+		// Reset ParagonIE_Sodium_Compat::$fastMult to what it was before.
272
+		ParagonIE_Sodium_Compat::$fastMult = $orig;
273
+
274
+		return self::verify_32($rcheck, self::substr($sig, 0, 32));
275
+	}
276
+
277
+	/**
278
+	 * @internal You should not use this directly from another application
279
+	 *
280
+	 * @param string $S
281
+	 * @return bool
282
+	 * @throws Exception
283
+	 */
284
+	public static function check_S_lt_L($S)
285
+	{
286
+		if (self::strlen($S) < 32) {
287
+			throw new Exception('Signature must be 32 bytes');
288
+		}
289
+		static $L = array(
290
+			0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58,
291
+			0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14,
292
+			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
293
+			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10
294
+		);
295
+		$c = 0;
296
+		$n = 1;
297
+		$i = 32;
298
+
299
+		do {
300
+			--$i;
301
+			$x = self::chrToInt($S[$i]);
302
+			$c |= (
303
+				(($x - $L[$i]) >> 8) & $n
304
+			);
305
+			$n &= (
306
+				(($x ^ $L[$i]) - 1) >> 8
307
+			);
308
+		} while ($i !== 0);
309
+
310
+		return $c === 0;
311
+	}
312
+
313
+	/**
314
+	 * @param string $R
315
+	 * @return bool
316
+	 */
317
+	public static function small_order($R)
318
+	{
319
+		static $blacklist = array(
320
+			/* 0 (order 4) */
321
+			array(
322
+				0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
323
+				0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
324
+				0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
325
+				0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
326
+			),
327
+			/* 1 (order 1) */
328
+			array(
329
+				0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
330
+				0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
331
+				0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
332
+				0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
333
+			),
334
+			/* 2707385501144840649318225287225658788936804267575313519463743609750303402022 (order 8) */
335
+			array(
336
+				0x26, 0xe8, 0x95, 0x8f, 0xc2, 0xb2, 0x27, 0xb0,
337
+				0x45, 0xc3, 0xf4, 0x89, 0xf2, 0xef, 0x98, 0xf0,
338
+				0xd5, 0xdf, 0xac, 0x05, 0xd3, 0xc6, 0x33, 0x39,
339
+				0xb1, 0x38, 0x02, 0x88, 0x6d, 0x53, 0xfc, 0x05
340
+			),
341
+			/* 55188659117513257062467267217118295137698188065244968500265048394206261417927 (order 8) */
342
+			array(
343
+				0xc7, 0x17, 0x6a, 0x70, 0x3d, 0x4d, 0xd8, 0x4f,
344
+				0xba, 0x3c, 0x0b, 0x76, 0x0d, 0x10, 0x67, 0x0f,
345
+				0x2a, 0x20, 0x53, 0xfa, 0x2c, 0x39, 0xcc, 0xc6,
346
+				0x4e, 0xc7, 0xfd, 0x77, 0x92, 0xac, 0x03, 0x7a
347
+			),
348
+			/* p-1 (order 2) */
349
+			array(
350
+				0x13, 0xe8, 0x95, 0x8f, 0xc2, 0xb2, 0x27, 0xb0,
351
+				0x45, 0xc3, 0xf4, 0x89, 0xf2, 0xef, 0x98, 0xf0,
352
+				0xd5, 0xdf, 0xac, 0x05, 0xd3, 0xc6, 0x33, 0x39,
353
+				0xb1, 0x38, 0x02, 0x88, 0x6d, 0x53, 0xfc, 0x85
354
+			),
355
+			/* p (order 4) */
356
+			array(
357
+				0xb4, 0x17, 0x6a, 0x70, 0x3d, 0x4d, 0xd8, 0x4f,
358
+				0xba, 0x3c, 0x0b, 0x76, 0x0d, 0x10, 0x67, 0x0f,
359
+				0x2a, 0x20, 0x53, 0xfa, 0x2c, 0x39, 0xcc, 0xc6,
360
+				0x4e, 0xc7, 0xfd, 0x77, 0x92, 0xac, 0x03, 0xfa
361
+			),
362
+			/* p+1 (order 1) */
363
+			array(
364
+				0xec, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
365
+				0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
366
+				0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
367
+				0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f
368
+			),
369
+			/* p+2707385501144840649318225287225658788936804267575313519463743609750303402022 (order 8) */
370
+			array(
371
+				0xed, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
372
+				0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
373
+				0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
374
+				0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f
375
+			),
376
+			/* p+55188659117513257062467267217118295137698188065244968500265048394206261417927 (order 8) */
377
+			array(
378
+				0xee, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
379
+				0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
380
+				0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
381
+				0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f
382
+			),
383
+			/* 2p-1 (order 2) */
384
+			array(
385
+				0xd9, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
386
+				0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
387
+				0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
388
+				0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
389
+			),
390
+			/* 2p (order 4) */
391
+			array(
392
+				0xda, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
393
+				0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
394
+				0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
395
+				0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
396
+			),
397
+			/* 2p+1 (order 1) */
398
+			array(
399
+				0xdb, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
400
+				0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
401
+				0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
402
+				0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
403
+			)
404
+		);
405
+		$countBlacklist = count($blacklist);
406
+
407
+		for ($i = 0; $i < $countBlacklist; ++$i) {
408
+			$c = 0;
409
+			for ($j = 0; $j < 32; ++$j) {
410
+				$c |= self::chrToInt($R[$j]) ^ $blacklist[$i][$j];
411
+			}
412
+			if ($c === 0) {
413
+				return true;
414
+			}
415
+		}
416
+		return false;
417
+	}
418 418
 }
Please login to merge, or discard this patch.
Spacing   +83 added lines, -83 removed lines patch added patch discarded remove patch
@@ -1,6 +1,6 @@  discard block
 block discarded – undo
1 1
 <?php
2 2
 
3
-if (class_exists('ParagonIE_Sodium_Core32_Ed25519', false)) {
3
+if ( class_exists( 'ParagonIE_Sodium_Core32_Ed25519', false ) ) {
4 4
     return;
5 5
 }
6 6
 
@@ -19,10 +19,10 @@  discard block
 block discarded – undo
19 19
      */
20 20
     public static function keypair()
21 21
     {
22
-        $seed = random_bytes(self::SEED_BYTES);
22
+        $seed = random_bytes( self::SEED_BYTES );
23 23
         $pk = '';
24 24
         $sk = '';
25
-        self::seed_keypair($pk, $sk, $seed);
25
+        self::seed_keypair( $pk, $sk, $seed );
26 26
         return $sk . $pk;
27 27
     }
28 28
 
@@ -34,14 +34,14 @@  discard block
 block discarded – undo
34 34
      * @param string $seed
35 35
      * @return string
36 36
      */
37
-    public static function seed_keypair(&$pk, &$sk, $seed)
37
+    public static function seed_keypair( &$pk, &$sk, $seed )
38 38
     {
39
-        if (self::strlen($seed) !== self::SEED_BYTES) {
40
-            throw new RangeException('crypto_sign keypair seed must be 32 bytes long');
39
+        if ( self::strlen( $seed ) !== self::SEED_BYTES ) {
40
+            throw new RangeException( 'crypto_sign keypair seed must be 32 bytes long' );
41 41
         }
42 42
 
43 43
         /** @var string $pk */
44
-        $pk = self::publickey_from_secretkey($seed);
44
+        $pk = self::publickey_from_secretkey( $seed );
45 45
         $sk = $seed . $pk;
46 46
         return $sk;
47 47
     }
@@ -52,12 +52,12 @@  discard block
 block discarded – undo
52 52
      * @param string $keypair
53 53
      * @return string
54 54
      */
55
-    public static function secretkey($keypair)
55
+    public static function secretkey( $keypair )
56 56
     {
57
-        if (self::strlen($keypair) !== self::KEYPAIR_BYTES) {
58
-            throw new RangeException('crypto_sign keypair must be 96 bytes long');
57
+        if ( self::strlen( $keypair ) !== self::KEYPAIR_BYTES ) {
58
+            throw new RangeException( 'crypto_sign keypair must be 96 bytes long' );
59 59
         }
60
-        return self::substr($keypair, 0, 64);
60
+        return self::substr( $keypair, 0, 64 );
61 61
     }
62 62
 
63 63
     /**
@@ -66,12 +66,12 @@  discard block
 block discarded – undo
66 66
      * @param string $keypair
67 67
      * @return string
68 68
      */
69
-    public static function publickey($keypair)
69
+    public static function publickey( $keypair )
70 70
     {
71
-        if (self::strlen($keypair) !== self::KEYPAIR_BYTES) {
72
-            throw new RangeException('crypto_sign keypair must be 96 bytes long');
71
+        if ( self::strlen( $keypair ) !== self::KEYPAIR_BYTES ) {
72
+            throw new RangeException( 'crypto_sign keypair must be 96 bytes long' );
73 73
         }
74
-        return self::substr($keypair, 64, 32);
74
+        return self::substr( $keypair, 64, 32 );
75 75
     }
76 76
 
77 77
     /**
@@ -80,17 +80,17 @@  discard block
 block discarded – undo
80 80
      * @param string $sk
81 81
      * @return string
82 82
      */
83
-    public static function publickey_from_secretkey($sk)
83
+    public static function publickey_from_secretkey( $sk )
84 84
     {
85 85
         /** @var string $sk */
86
-        $sk = hash('sha512', self::substr($sk, 0, 32), true);
86
+        $sk = hash( 'sha512', self::substr( $sk, 0, 32 ), true );
87 87
         $sk[0] = self::intToChr(
88
-            self::chrToInt($sk[0]) & 248
88
+            self::chrToInt( $sk[0] ) & 248
89 89
         );
90 90
         $sk[31] = self::intToChr(
91
-            (self::chrToInt($sk[31]) & 63) | 64
91
+            ( self::chrToInt( $sk[31] ) & 63 ) | 64
92 92
         );
93
-        return self::sk_to_pk($sk);
93
+        return self::sk_to_pk( $sk );
94 94
     }
95 95
 
96 96
     /**
@@ -99,11 +99,11 @@  discard block
 block discarded – undo
99 99
      * @param string $sk
100 100
      * @return string
101 101
      */
102
-    public static function sk_to_pk($sk)
102
+    public static function sk_to_pk( $sk )
103 103
     {
104 104
         return self::ge_p3_tobytes(
105 105
             self::ge_scalarmult_base(
106
-                self::substr($sk, 0, 32)
106
+                self::substr( $sk, 0, 32 )
107 107
             )
108 108
         );
109 109
     }
@@ -115,10 +115,10 @@  discard block
 block discarded – undo
115 115
      * @param string $sk
116 116
      * @return string
117 117
      */
118
-    public static function sign($message, $sk)
118
+    public static function sign( $message, $sk )
119 119
     {
120 120
         /** @var string $signature */
121
-        $signature = self::sign_detached($message, $sk);
121
+        $signature = self::sign_detached( $message, $sk );
122 122
         return $signature . $message;
123 123
     }
124 124
 
@@ -130,18 +130,18 @@  discard block
 block discarded – undo
130 130
      * @return string         Message (without signature)
131 131
      * @throws Exception
132 132
      */
133
-    public static function sign_open($message, $pk)
133
+    public static function sign_open( $message, $pk )
134 134
     {
135 135
         /** @var string $signature */
136
-        $signature = self::substr($message, 0, 64);
136
+        $signature = self::substr( $message, 0, 64 );
137 137
 
138 138
         /** @var string $message */
139
-        $message = self::substr($message, 64);
139
+        $message = self::substr( $message, 64 );
140 140
 
141
-        if (self::verify_detached($signature, $message, $pk)) {
141
+        if ( self::verify_detached( $signature, $message, $pk ) ) {
142 142
             return $message;
143 143
         }
144
-        throw new Exception('Invalid signature');
144
+        throw new Exception( 'Invalid signature' );
145 145
     }
146 146
 
147 147
     /**
@@ -151,56 +151,56 @@  discard block
 block discarded – undo
151 151
      * @param string $sk
152 152
      * @return string
153 153
      */
154
-    public static function sign_detached($message, $sk)
154
+    public static function sign_detached( $message, $sk )
155 155
     {
156 156
         # crypto_hash_sha512(az, sk, 32);
157
-        $az =  hash('sha512', self::substr($sk, 0, 32), true);
157
+        $az = hash( 'sha512', self::substr( $sk, 0, 32 ), true );
158 158
 
159 159
         # az[0] &= 248;
160 160
         # az[31] &= 63;
161 161
         # az[31] |= 64;
162
-        $az[0] = self::intToChr(self::chrToInt($az[0]) & 248);
163
-        $az[31] = self::intToChr((self::chrToInt($az[31]) & 63) | 64);
162
+        $az[0] = self::intToChr( self::chrToInt( $az[0] ) & 248 );
163
+        $az[31] = self::intToChr( ( self::chrToInt( $az[31] ) & 63 ) | 64 );
164 164
 
165 165
         # crypto_hash_sha512_init(&hs);
166 166
         # crypto_hash_sha512_update(&hs, az + 32, 32);
167 167
         # crypto_hash_sha512_update(&hs, m, mlen);
168 168
         # crypto_hash_sha512_final(&hs, nonce);
169
-        $hs = hash_init('sha512');
170
-        hash_update($hs, self::substr($az, 32, 32));
171
-        hash_update($hs, $message);
172
-        $nonceHash = hash_final($hs, true);
169
+        $hs = hash_init( 'sha512' );
170
+        hash_update( $hs, self::substr( $az, 32, 32 ) );
171
+        hash_update( $hs, $message );
172
+        $nonceHash = hash_final( $hs, true );
173 173
 
174 174
         # memmove(sig + 32, sk + 32, 32);
175
-        $pk = self::substr($sk, 32, 32);
175
+        $pk = self::substr( $sk, 32, 32 );
176 176
 
177 177
         # sc_reduce(nonce);
178 178
         # ge_scalarmult_base(&R, nonce);
179 179
         # ge_p3_tobytes(sig, &R);
180
-        $nonce = self::sc_reduce($nonceHash) . self::substr($nonceHash, 32);
180
+        $nonce = self::sc_reduce( $nonceHash ) . self::substr( $nonceHash, 32 );
181 181
         $sig = self::ge_p3_tobytes(
182
-            self::ge_scalarmult_base($nonce)
182
+            self::ge_scalarmult_base( $nonce )
183 183
         );
184 184
 
185 185
         # crypto_hash_sha512_init(&hs);
186 186
         # crypto_hash_sha512_update(&hs, sig, 64);
187 187
         # crypto_hash_sha512_update(&hs, m, mlen);
188 188
         # crypto_hash_sha512_final(&hs, hram);
189
-        $hs = hash_init('sha512');
190
-        hash_update($hs, self::substr($sig, 0, 32));
191
-        hash_update($hs, self::substr($pk, 0, 32));
192
-        hash_update($hs, $message);
193
-        $hramHash = hash_final($hs, true);
189
+        $hs = hash_init( 'sha512' );
190
+        hash_update( $hs, self::substr( $sig, 0, 32 ) );
191
+        hash_update( $hs, self::substr( $pk, 0, 32 ) );
192
+        hash_update( $hs, $message );
193
+        $hramHash = hash_final( $hs, true );
194 194
 
195 195
         # sc_reduce(hram);
196 196
         # sc_muladd(sig + 32, hram, az, nonce);
197
-        $hram = self::sc_reduce($hramHash);
198
-        $sigAfter = self::sc_muladd($hram, $az, $nonce);
199
-        $sig = self::substr($sig, 0, 32) . self::substr($sigAfter, 0, 32);
197
+        $hram = self::sc_reduce( $hramHash );
198
+        $sigAfter = self::sc_muladd( $hram, $az, $nonce );
199
+        $sig = self::substr( $sig, 0, 32 ) . self::substr( $sigAfter, 0, 32 );
200 200
 
201 201
         try {
202
-            ParagonIE_Sodium_Compat::memzero($az);
203
-        } catch (Error $ex) {
202
+            ParagonIE_Sodium_Compat::memzero( $az );
203
+        } catch ( Error $ex ) {
204 204
             $az = null;
205 205
         }
206 206
         return $sig;
@@ -215,26 +215,26 @@  discard block
 block discarded – undo
215 215
      * @return bool
216 216
      * @throws Exception
217 217
      */
218
-    public static function verify_detached($sig, $message, $pk)
218
+    public static function verify_detached( $sig, $message, $pk )
219 219
     {
220
-        if (self::strlen($sig) < 64) {
221
-            throw new Exception('Signature is too short');
220
+        if ( self::strlen( $sig ) < 64 ) {
221
+            throw new Exception( 'Signature is too short' );
222 222
         }
223
-        if (self::check_S_lt_L(self::substr($sig, 32, 32))) {
224
-            throw new Exception('S < L - Invalid signature');
223
+        if ( self::check_S_lt_L( self::substr( $sig, 32, 32 ) ) ) {
224
+            throw new Exception( 'S < L - Invalid signature' );
225 225
         }
226
-        if (self::small_order($sig)) {
227
-            throw new Exception('Signature is on too small of an order');
226
+        if ( self::small_order( $sig ) ) {
227
+            throw new Exception( 'Signature is on too small of an order' );
228 228
         }
229
-        if ((self::chrToInt($sig[63]) & 224) !== 0) {
230
-            throw new Exception('Invalid signature');
229
+        if ( ( self::chrToInt( $sig[63] ) & 224 ) !== 0 ) {
230
+            throw new Exception( 'Invalid signature' );
231 231
         }
232 232
         $d = 0;
233
-        for ($i = 0; $i < 32; ++$i) {
234
-            $d |= self::chrToInt($pk[$i]);
233
+        for ( $i = 0; $i < 32; ++$i ) {
234
+            $d |= self::chrToInt( $pk[$i] );
235 235
         }
236
-        if ($d === 0) {
237
-            throw new Exception('All zero public key');
236
+        if ( $d === 0 ) {
237
+            throw new Exception( 'All zero public key' );
238 238
         }
239 239
 
240 240
         /** @var bool The original value of ParagonIE_Sodium_Compat::$fastMult */
@@ -244,34 +244,34 @@  discard block
 block discarded – undo
244 244
         ParagonIE_Sodium_Compat::$fastMult = true;
245 245
 
246 246
         /** @var ParagonIE_Sodium_Core32_Curve25519_Ge_P3 $A */
247
-        $A = self::ge_frombytes_negate_vartime($pk);
247
+        $A = self::ge_frombytes_negate_vartime( $pk );
248 248
 
249 249
         /** @var string $hDigest */
250 250
         $hDigest = hash(
251 251
             'sha512',
252
-            self::substr($sig, 0, 32) .
253
-            self::substr($pk, 0, 32) .
252
+            self::substr( $sig, 0, 32 ) .
253
+            self::substr( $pk, 0, 32 ) .
254 254
             $message,
255 255
             true
256 256
         );
257 257
 
258 258
         /** @var string $h */
259
-        $h = self::sc_reduce($hDigest) . self::substr($hDigest, 32);
259
+        $h = self::sc_reduce( $hDigest ) . self::substr( $hDigest, 32 );
260 260
 
261 261
         /** @var ParagonIE_Sodium_Core32_Curve25519_Ge_P2 $R */
262 262
         $R = self::ge_double_scalarmult_vartime(
263 263
             $h,
264 264
             $A,
265
-            self::substr($sig, 32)
265
+            self::substr( $sig, 32 )
266 266
         );
267 267
 
268 268
         /** @var string $rcheck */
269
-        $rcheck = self::ge_tobytes($R);
269
+        $rcheck = self::ge_tobytes( $R );
270 270
 
271 271
         // Reset ParagonIE_Sodium_Compat::$fastMult to what it was before.
272 272
         ParagonIE_Sodium_Compat::$fastMult = $orig;
273 273
 
274
-        return self::verify_32($rcheck, self::substr($sig, 0, 32));
274
+        return self::verify_32( $rcheck, self::substr( $sig, 0, 32 ) );
275 275
     }
276 276
 
277 277
     /**
@@ -281,10 +281,10 @@  discard block
 block discarded – undo
281 281
      * @return bool
282 282
      * @throws Exception
283 283
      */
284
-    public static function check_S_lt_L($S)
284
+    public static function check_S_lt_L( $S )
285 285
     {
286
-        if (self::strlen($S) < 32) {
287
-            throw new Exception('Signature must be 32 bytes');
286
+        if ( self::strlen( $S ) < 32 ) {
287
+            throw new Exception( 'Signature must be 32 bytes' );
288 288
         }
289 289
         static $L = array(
290 290
             0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58,
@@ -298,14 +298,14 @@  discard block
 block discarded – undo
298 298
 
299 299
         do {
300 300
             --$i;
301
-            $x = self::chrToInt($S[$i]);
301
+            $x = self::chrToInt( $S[$i] );
302 302
             $c |= (
303
-                (($x - $L[$i]) >> 8) & $n
303
+                ( ( $x - $L[$i] ) >> 8 ) & $n
304 304
             );
305 305
             $n &= (
306
-                (($x ^ $L[$i]) - 1) >> 8
306
+                ( ( $x ^ $L[$i] ) - 1 ) >> 8
307 307
             );
308
-        } while ($i !== 0);
308
+        } while ( $i !== 0 );
309 309
 
310 310
         return $c === 0;
311 311
     }
@@ -314,7 +314,7 @@  discard block
 block discarded – undo
314 314
      * @param string $R
315 315
      * @return bool
316 316
      */
317
-    public static function small_order($R)
317
+    public static function small_order( $R )
318 318
     {
319 319
         static $blacklist = array(
320 320
             /* 0 (order 4) */
@@ -402,14 +402,14 @@  discard block
 block discarded – undo
402 402
                 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
403 403
             )
404 404
         );
405
-        $countBlacklist = count($blacklist);
405
+        $countBlacklist = count( $blacklist );
406 406
 
407
-        for ($i = 0; $i < $countBlacklist; ++$i) {
407
+        for ( $i = 0; $i < $countBlacklist; ++$i ) {
408 408
             $c = 0;
409
-            for ($j = 0; $j < 32; ++$j) {
410
-                $c |= self::chrToInt($R[$j]) ^ $blacklist[$i][$j];
409
+            for ( $j = 0; $j < 32; ++$j ) {
410
+                $c |= self::chrToInt( $R[$j] ) ^ $blacklist[$i][$j];
411 411
             }
412
-            if ($c === 0) {
412
+            if ( $c === 0 ) {
413 413
                 return true;
414 414
             }
415 415
         }
Please login to merge, or discard this patch.
Braces   +13 added lines, -26 removed lines patch added patch discarded remove patch
@@ -7,8 +7,7 @@  discard block
 block discarded – undo
7 7
 /**
8 8
  * Class ParagonIE_Sodium_Core32_Ed25519
9 9
  */
10
-abstract class ParagonIE_Sodium_Core32_Ed25519 extends ParagonIE_Sodium_Core32_Curve25519
11
-{
10
+abstract class ParagonIE_Sodium_Core32_Ed25519 extends ParagonIE_Sodium_Core32_Curve25519 {
12 11
     const KEYPAIR_BYTES = 96;
13 12
     const SEED_BYTES = 32;
14 13
 
@@ -17,8 +16,7 @@  discard block
 block discarded – undo
17 16
      *
18 17
      * @return string (96 bytes)
19 18
      */
20
-    public static function keypair()
21
-    {
19
+    public static function keypair() {
22 20
         $seed = random_bytes(self::SEED_BYTES);
23 21
         $pk = '';
24 22
         $sk = '';
@@ -34,8 +32,7 @@  discard block
 block discarded – undo
34 32
      * @param string $seed
35 33
      * @return string
36 34
      */
37
-    public static function seed_keypair(&$pk, &$sk, $seed)
38
-    {
35
+    public static function seed_keypair(&$pk, &$sk, $seed) {
39 36
         if (self::strlen($seed) !== self::SEED_BYTES) {
40 37
             throw new RangeException('crypto_sign keypair seed must be 32 bytes long');
41 38
         }
@@ -52,8 +49,7 @@  discard block
 block discarded – undo
52 49
      * @param string $keypair
53 50
      * @return string
54 51
      */
55
-    public static function secretkey($keypair)
56
-    {
52
+    public static function secretkey($keypair) {
57 53
         if (self::strlen($keypair) !== self::KEYPAIR_BYTES) {
58 54
             throw new RangeException('crypto_sign keypair must be 96 bytes long');
59 55
         }
@@ -66,8 +62,7 @@  discard block
 block discarded – undo
66 62
      * @param string $keypair
67 63
      * @return string
68 64
      */
69
-    public static function publickey($keypair)
70
-    {
65
+    public static function publickey($keypair) {
71 66
         if (self::strlen($keypair) !== self::KEYPAIR_BYTES) {
72 67
             throw new RangeException('crypto_sign keypair must be 96 bytes long');
73 68
         }
@@ -80,8 +75,7 @@  discard block
 block discarded – undo
80 75
      * @param string $sk
81 76
      * @return string
82 77
      */
83
-    public static function publickey_from_secretkey($sk)
84
-    {
78
+    public static function publickey_from_secretkey($sk) {
85 79
         /** @var string $sk */
86 80
         $sk = hash('sha512', self::substr($sk, 0, 32), true);
87 81
         $sk[0] = self::intToChr(
@@ -99,8 +93,7 @@  discard block
 block discarded – undo
99 93
      * @param string $sk
100 94
      * @return string
101 95
      */
102
-    public static function sk_to_pk($sk)
103
-    {
96
+    public static function sk_to_pk($sk) {
104 97
         return self::ge_p3_tobytes(
105 98
             self::ge_scalarmult_base(
106 99
                 self::substr($sk, 0, 32)
@@ -115,8 +108,7 @@  discard block
 block discarded – undo
115 108
      * @param string $sk
116 109
      * @return string
117 110
      */
118
-    public static function sign($message, $sk)
119
-    {
111
+    public static function sign($message, $sk) {
120 112
         /** @var string $signature */
121 113
         $signature = self::sign_detached($message, $sk);
122 114
         return $signature . $message;
@@ -130,8 +122,7 @@  discard block
 block discarded – undo
130 122
      * @return string         Message (without signature)
131 123
      * @throws Exception
132 124
      */
133
-    public static function sign_open($message, $pk)
134
-    {
125
+    public static function sign_open($message, $pk) {
135 126
         /** @var string $signature */
136 127
         $signature = self::substr($message, 0, 64);
137 128
 
@@ -151,8 +142,7 @@  discard block
 block discarded – undo
151 142
      * @param string $sk
152 143
      * @return string
153 144
      */
154
-    public static function sign_detached($message, $sk)
155
-    {
145
+    public static function sign_detached($message, $sk) {
156 146
         # crypto_hash_sha512(az, sk, 32);
157 147
         $az =  hash('sha512', self::substr($sk, 0, 32), true);
158 148
 
@@ -215,8 +205,7 @@  discard block
 block discarded – undo
215 205
      * @return bool
216 206
      * @throws Exception
217 207
      */
218
-    public static function verify_detached($sig, $message, $pk)
219
-    {
208
+    public static function verify_detached($sig, $message, $pk) {
220 209
         if (self::strlen($sig) < 64) {
221 210
             throw new Exception('Signature is too short');
222 211
         }
@@ -281,8 +270,7 @@  discard block
 block discarded – undo
281 270
      * @return bool
282 271
      * @throws Exception
283 272
      */
284
-    public static function check_S_lt_L($S)
285
-    {
273
+    public static function check_S_lt_L($S) {
286 274
         if (self::strlen($S) < 32) {
287 275
             throw new Exception('Signature must be 32 bytes');
288 276
         }
@@ -314,8 +302,7 @@  discard block
 block discarded – undo
314 302
      * @param string $R
315 303
      * @return bool
316 304
      */
317
-    public static function small_order($R)
318
-    {
305
+    public static function small_order($R) {
319 306
         static $blacklist = array(
320 307
             /* 0 (order 4) */
321 308
             array(
Please login to merge, or discard this patch.