@@ -1,7 +1,7 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | if (class_exists('ParagonIE_Sodium_Core32_HChaCha20', false)) { |
| 4 | - return; |
|
| 4 | + return; |
|
| 5 | 5 | } |
| 6 | 6 | |
| 7 | 7 | /** |
@@ -9,109 +9,109 @@ discard block |
||
| 9 | 9 | */ |
| 10 | 10 | class ParagonIE_Sodium_Core32_HChaCha20 extends ParagonIE_Sodium_Core32_ChaCha20 |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * @param string $in |
|
| 14 | - * @param string $key |
|
| 15 | - * @param string|null $c |
|
| 16 | - * @return string |
|
| 17 | - */ |
|
| 18 | - public static function hChaCha20($in = '', $key = '', $c = null) |
|
| 19 | - { |
|
| 20 | - $ctx = array(); |
|
| 12 | + /** |
|
| 13 | + * @param string $in |
|
| 14 | + * @param string $key |
|
| 15 | + * @param string|null $c |
|
| 16 | + * @return string |
|
| 17 | + */ |
|
| 18 | + public static function hChaCha20($in = '', $key = '', $c = null) |
|
| 19 | + { |
|
| 20 | + $ctx = array(); |
|
| 21 | 21 | |
| 22 | - if ($c === null) { |
|
| 23 | - $ctx[0] = new ParagonIE_Sodium_Core32_Int32(array(0x6170, 0x7865)); |
|
| 24 | - $ctx[1] = new ParagonIE_Sodium_Core32_Int32(array(0x3320, 0x646e)); |
|
| 25 | - $ctx[2] = new ParagonIE_Sodium_Core32_Int32(array(0x7962, 0x2d32)); |
|
| 26 | - $ctx[3] = new ParagonIE_Sodium_Core32_Int32(array(0x6b20, 0x6574)); |
|
| 27 | - } else { |
|
| 28 | - $ctx[0] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 0, 4)); |
|
| 29 | - $ctx[1] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 4, 4)); |
|
| 30 | - $ctx[2] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 8, 4)); |
|
| 31 | - $ctx[3] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 12, 4)); |
|
| 32 | - } |
|
| 33 | - $ctx[4] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 0, 4)); |
|
| 34 | - $ctx[5] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 4, 4)); |
|
| 35 | - $ctx[6] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 8, 4)); |
|
| 36 | - $ctx[7] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 12, 4)); |
|
| 37 | - $ctx[8] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 16, 4)); |
|
| 38 | - $ctx[9] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 20, 4)); |
|
| 39 | - $ctx[10] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 24, 4)); |
|
| 40 | - $ctx[11] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 28, 4)); |
|
| 41 | - $ctx[12] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 0, 4)); |
|
| 42 | - $ctx[13] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 4, 4)); |
|
| 43 | - $ctx[14] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 8, 4)); |
|
| 44 | - $ctx[15] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 12, 4)); |
|
| 22 | + if ($c === null) { |
|
| 23 | + $ctx[0] = new ParagonIE_Sodium_Core32_Int32(array(0x6170, 0x7865)); |
|
| 24 | + $ctx[1] = new ParagonIE_Sodium_Core32_Int32(array(0x3320, 0x646e)); |
|
| 25 | + $ctx[2] = new ParagonIE_Sodium_Core32_Int32(array(0x7962, 0x2d32)); |
|
| 26 | + $ctx[3] = new ParagonIE_Sodium_Core32_Int32(array(0x6b20, 0x6574)); |
|
| 27 | + } else { |
|
| 28 | + $ctx[0] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 0, 4)); |
|
| 29 | + $ctx[1] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 4, 4)); |
|
| 30 | + $ctx[2] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 8, 4)); |
|
| 31 | + $ctx[3] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 12, 4)); |
|
| 32 | + } |
|
| 33 | + $ctx[4] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 0, 4)); |
|
| 34 | + $ctx[5] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 4, 4)); |
|
| 35 | + $ctx[6] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 8, 4)); |
|
| 36 | + $ctx[7] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 12, 4)); |
|
| 37 | + $ctx[8] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 16, 4)); |
|
| 38 | + $ctx[9] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 20, 4)); |
|
| 39 | + $ctx[10] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 24, 4)); |
|
| 40 | + $ctx[11] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 28, 4)); |
|
| 41 | + $ctx[12] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 0, 4)); |
|
| 42 | + $ctx[13] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 4, 4)); |
|
| 43 | + $ctx[14] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 8, 4)); |
|
| 44 | + $ctx[15] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 12, 4)); |
|
| 45 | 45 | |
| 46 | - return self::hChaCha20Bytes($ctx); |
|
| 47 | - } |
|
| 46 | + return self::hChaCha20Bytes($ctx); |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * @param array $ctx |
|
| 51 | - * @return string |
|
| 52 | - */ |
|
| 53 | - protected static function hChaCha20Bytes(array $ctx) |
|
| 54 | - { |
|
| 55 | - /** |
|
| 56 | - * @var ParagonIE_Sodium_Core32_Int32 $x0 |
|
| 57 | - * @var ParagonIE_Sodium_Core32_Int32 $x1 |
|
| 58 | - * @var ParagonIE_Sodium_Core32_Int32 $x2 |
|
| 59 | - * @var ParagonIE_Sodium_Core32_Int32 $x3 |
|
| 60 | - * @var ParagonIE_Sodium_Core32_Int32 $x12 |
|
| 61 | - * @var ParagonIE_Sodium_Core32_Int32 $x13 |
|
| 62 | - * @var ParagonIE_Sodium_Core32_Int32 $x14 |
|
| 63 | - * @var ParagonIE_Sodium_Core32_Int32 $x15 |
|
| 64 | - */ |
|
| 65 | - $x0 = $ctx[0]; |
|
| 66 | - $x1 = $ctx[1]; |
|
| 67 | - $x2 = $ctx[2]; |
|
| 68 | - $x3 = $ctx[3]; |
|
| 69 | - $x4 = $ctx[4]; |
|
| 70 | - $x5 = $ctx[5]; |
|
| 71 | - $x6 = $ctx[6]; |
|
| 72 | - $x7 = $ctx[7]; |
|
| 73 | - $x8 = $ctx[8]; |
|
| 74 | - $x9 = $ctx[9]; |
|
| 75 | - $x10 = $ctx[10]; |
|
| 76 | - $x11 = $ctx[11]; |
|
| 77 | - $x12 = $ctx[12]; |
|
| 78 | - $x13 = $ctx[13]; |
|
| 79 | - $x14 = $ctx[14]; |
|
| 80 | - $x15 = $ctx[15]; |
|
| 49 | + /** |
|
| 50 | + * @param array $ctx |
|
| 51 | + * @return string |
|
| 52 | + */ |
|
| 53 | + protected static function hChaCha20Bytes(array $ctx) |
|
| 54 | + { |
|
| 55 | + /** |
|
| 56 | + * @var ParagonIE_Sodium_Core32_Int32 $x0 |
|
| 57 | + * @var ParagonIE_Sodium_Core32_Int32 $x1 |
|
| 58 | + * @var ParagonIE_Sodium_Core32_Int32 $x2 |
|
| 59 | + * @var ParagonIE_Sodium_Core32_Int32 $x3 |
|
| 60 | + * @var ParagonIE_Sodium_Core32_Int32 $x12 |
|
| 61 | + * @var ParagonIE_Sodium_Core32_Int32 $x13 |
|
| 62 | + * @var ParagonIE_Sodium_Core32_Int32 $x14 |
|
| 63 | + * @var ParagonIE_Sodium_Core32_Int32 $x15 |
|
| 64 | + */ |
|
| 65 | + $x0 = $ctx[0]; |
|
| 66 | + $x1 = $ctx[1]; |
|
| 67 | + $x2 = $ctx[2]; |
|
| 68 | + $x3 = $ctx[3]; |
|
| 69 | + $x4 = $ctx[4]; |
|
| 70 | + $x5 = $ctx[5]; |
|
| 71 | + $x6 = $ctx[6]; |
|
| 72 | + $x7 = $ctx[7]; |
|
| 73 | + $x8 = $ctx[8]; |
|
| 74 | + $x9 = $ctx[9]; |
|
| 75 | + $x10 = $ctx[10]; |
|
| 76 | + $x11 = $ctx[11]; |
|
| 77 | + $x12 = $ctx[12]; |
|
| 78 | + $x13 = $ctx[13]; |
|
| 79 | + $x14 = $ctx[14]; |
|
| 80 | + $x15 = $ctx[15]; |
|
| 81 | 81 | |
| 82 | - for ($i = 0; $i < 10; ++$i) { |
|
| 83 | - # QUARTERROUND( x0, x4, x8, x12) |
|
| 84 | - list($x0, $x4, $x8, $x12) = self::quarterRound($x0, $x4, $x8, $x12); |
|
| 82 | + for ($i = 0; $i < 10; ++$i) { |
|
| 83 | + # QUARTERROUND( x0, x4, x8, x12) |
|
| 84 | + list($x0, $x4, $x8, $x12) = self::quarterRound($x0, $x4, $x8, $x12); |
|
| 85 | 85 | |
| 86 | - # QUARTERROUND( x1, x5, x9, x13) |
|
| 87 | - list($x1, $x5, $x9, $x13) = self::quarterRound($x1, $x5, $x9, $x13); |
|
| 86 | + # QUARTERROUND( x1, x5, x9, x13) |
|
| 87 | + list($x1, $x5, $x9, $x13) = self::quarterRound($x1, $x5, $x9, $x13); |
|
| 88 | 88 | |
| 89 | - # QUARTERROUND( x2, x6, x10, x14) |
|
| 90 | - list($x2, $x6, $x10, $x14) = self::quarterRound($x2, $x6, $x10, $x14); |
|
| 89 | + # QUARTERROUND( x2, x6, x10, x14) |
|
| 90 | + list($x2, $x6, $x10, $x14) = self::quarterRound($x2, $x6, $x10, $x14); |
|
| 91 | 91 | |
| 92 | - # QUARTERROUND( x3, x7, x11, x15) |
|
| 93 | - list($x3, $x7, $x11, $x15) = self::quarterRound($x3, $x7, $x11, $x15); |
|
| 92 | + # QUARTERROUND( x3, x7, x11, x15) |
|
| 93 | + list($x3, $x7, $x11, $x15) = self::quarterRound($x3, $x7, $x11, $x15); |
|
| 94 | 94 | |
| 95 | - # QUARTERROUND( x0, x5, x10, x15) |
|
| 96 | - list($x0, $x5, $x10, $x15) = self::quarterRound($x0, $x5, $x10, $x15); |
|
| 95 | + # QUARTERROUND( x0, x5, x10, x15) |
|
| 96 | + list($x0, $x5, $x10, $x15) = self::quarterRound($x0, $x5, $x10, $x15); |
|
| 97 | 97 | |
| 98 | - # QUARTERROUND( x1, x6, x11, x12) |
|
| 99 | - list($x1, $x6, $x11, $x12) = self::quarterRound($x1, $x6, $x11, $x12); |
|
| 98 | + # QUARTERROUND( x1, x6, x11, x12) |
|
| 99 | + list($x1, $x6, $x11, $x12) = self::quarterRound($x1, $x6, $x11, $x12); |
|
| 100 | 100 | |
| 101 | - # QUARTERROUND( x2, x7, x8, x13) |
|
| 102 | - list($x2, $x7, $x8, $x13) = self::quarterRound($x2, $x7, $x8, $x13); |
|
| 101 | + # QUARTERROUND( x2, x7, x8, x13) |
|
| 102 | + list($x2, $x7, $x8, $x13) = self::quarterRound($x2, $x7, $x8, $x13); |
|
| 103 | 103 | |
| 104 | - # QUARTERROUND( x3, x4, x9, x14) |
|
| 105 | - list($x3, $x4, $x9, $x14) = self::quarterRound($x3, $x4, $x9, $x14); |
|
| 106 | - } |
|
| 104 | + # QUARTERROUND( x3, x4, x9, x14) |
|
| 105 | + list($x3, $x4, $x9, $x14) = self::quarterRound($x3, $x4, $x9, $x14); |
|
| 106 | + } |
|
| 107 | 107 | |
| 108 | - return $x0->toReverseString() . |
|
| 109 | - $x1->toReverseString() . |
|
| 110 | - $x2->toReverseString() . |
|
| 111 | - $x3->toReverseString() . |
|
| 112 | - $x12->toReverseString() . |
|
| 113 | - $x13->toReverseString() . |
|
| 114 | - $x14->toReverseString() . |
|
| 115 | - $x15->toReverseString(); |
|
| 116 | - } |
|
| 108 | + return $x0->toReverseString() . |
|
| 109 | + $x1->toReverseString() . |
|
| 110 | + $x2->toReverseString() . |
|
| 111 | + $x3->toReverseString() . |
|
| 112 | + $x12->toReverseString() . |
|
| 113 | + $x13->toReverseString() . |
|
| 114 | + $x14->toReverseString() . |
|
| 115 | + $x15->toReverseString(); |
|
| 116 | + } |
|
| 117 | 117 | } |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -if (class_exists('ParagonIE_Sodium_Core32_HChaCha20', false)) { |
|
| 3 | +if ( class_exists( 'ParagonIE_Sodium_Core32_HChaCha20', false ) ) { |
|
| 4 | 4 | return; |
| 5 | 5 | } |
| 6 | 6 | |
@@ -15,42 +15,42 @@ discard block |
||
| 15 | 15 | * @param string|null $c |
| 16 | 16 | * @return string |
| 17 | 17 | */ |
| 18 | - public static function hChaCha20($in = '', $key = '', $c = null) |
|
| 18 | + public static function hChaCha20( $in = '', $key = '', $c = null ) |
|
| 19 | 19 | { |
| 20 | 20 | $ctx = array(); |
| 21 | 21 | |
| 22 | - if ($c === null) { |
|
| 23 | - $ctx[0] = new ParagonIE_Sodium_Core32_Int32(array(0x6170, 0x7865)); |
|
| 24 | - $ctx[1] = new ParagonIE_Sodium_Core32_Int32(array(0x3320, 0x646e)); |
|
| 25 | - $ctx[2] = new ParagonIE_Sodium_Core32_Int32(array(0x7962, 0x2d32)); |
|
| 26 | - $ctx[3] = new ParagonIE_Sodium_Core32_Int32(array(0x6b20, 0x6574)); |
|
| 22 | + if ( $c === null ) { |
|
| 23 | + $ctx[0] = new ParagonIE_Sodium_Core32_Int32( array( 0x6170, 0x7865 ) ); |
|
| 24 | + $ctx[1] = new ParagonIE_Sodium_Core32_Int32( array( 0x3320, 0x646e ) ); |
|
| 25 | + $ctx[2] = new ParagonIE_Sodium_Core32_Int32( array( 0x7962, 0x2d32 ) ); |
|
| 26 | + $ctx[3] = new ParagonIE_Sodium_Core32_Int32( array( 0x6b20, 0x6574 ) ); |
|
| 27 | 27 | } else { |
| 28 | - $ctx[0] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 0, 4)); |
|
| 29 | - $ctx[1] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 4, 4)); |
|
| 30 | - $ctx[2] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 8, 4)); |
|
| 31 | - $ctx[3] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 12, 4)); |
|
| 28 | + $ctx[0] = ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $c, 0, 4 ) ); |
|
| 29 | + $ctx[1] = ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $c, 4, 4 ) ); |
|
| 30 | + $ctx[2] = ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $c, 8, 4 ) ); |
|
| 31 | + $ctx[3] = ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $c, 12, 4 ) ); |
|
| 32 | 32 | } |
| 33 | - $ctx[4] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 0, 4)); |
|
| 34 | - $ctx[5] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 4, 4)); |
|
| 35 | - $ctx[6] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 8, 4)); |
|
| 36 | - $ctx[7] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 12, 4)); |
|
| 37 | - $ctx[8] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 16, 4)); |
|
| 38 | - $ctx[9] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 20, 4)); |
|
| 39 | - $ctx[10] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 24, 4)); |
|
| 40 | - $ctx[11] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 28, 4)); |
|
| 41 | - $ctx[12] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 0, 4)); |
|
| 42 | - $ctx[13] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 4, 4)); |
|
| 43 | - $ctx[14] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 8, 4)); |
|
| 44 | - $ctx[15] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 12, 4)); |
|
| 33 | + $ctx[4] = ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $key, 0, 4 ) ); |
|
| 34 | + $ctx[5] = ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $key, 4, 4 ) ); |
|
| 35 | + $ctx[6] = ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $key, 8, 4 ) ); |
|
| 36 | + $ctx[7] = ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $key, 12, 4 ) ); |
|
| 37 | + $ctx[8] = ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $key, 16, 4 ) ); |
|
| 38 | + $ctx[9] = ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $key, 20, 4 ) ); |
|
| 39 | + $ctx[10] = ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $key, 24, 4 ) ); |
|
| 40 | + $ctx[11] = ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $key, 28, 4 ) ); |
|
| 41 | + $ctx[12] = ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $in, 0, 4 ) ); |
|
| 42 | + $ctx[13] = ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $in, 4, 4 ) ); |
|
| 43 | + $ctx[14] = ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $in, 8, 4 ) ); |
|
| 44 | + $ctx[15] = ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $in, 12, 4 ) ); |
|
| 45 | 45 | |
| 46 | - return self::hChaCha20Bytes($ctx); |
|
| 46 | + return self::hChaCha20Bytes( $ctx ); |
|
| 47 | 47 | } |
| 48 | 48 | |
| 49 | 49 | /** |
| 50 | 50 | * @param array $ctx |
| 51 | 51 | * @return string |
| 52 | 52 | */ |
| 53 | - protected static function hChaCha20Bytes(array $ctx) |
|
| 53 | + protected static function hChaCha20Bytes( array $ctx ) |
|
| 54 | 54 | { |
| 55 | 55 | /** |
| 56 | 56 | * @var ParagonIE_Sodium_Core32_Int32 $x0 |
@@ -79,30 +79,30 @@ discard block |
||
| 79 | 79 | $x14 = $ctx[14]; |
| 80 | 80 | $x15 = $ctx[15]; |
| 81 | 81 | |
| 82 | - for ($i = 0; $i < 10; ++$i) { |
|
| 82 | + for ( $i = 0; $i < 10; ++$i ) { |
|
| 83 | 83 | # QUARTERROUND( x0, x4, x8, x12) |
| 84 | - list($x0, $x4, $x8, $x12) = self::quarterRound($x0, $x4, $x8, $x12); |
|
| 84 | + list( $x0, $x4, $x8, $x12 ) = self::quarterRound( $x0, $x4, $x8, $x12 ); |
|
| 85 | 85 | |
| 86 | 86 | # QUARTERROUND( x1, x5, x9, x13) |
| 87 | - list($x1, $x5, $x9, $x13) = self::quarterRound($x1, $x5, $x9, $x13); |
|
| 87 | + list( $x1, $x5, $x9, $x13 ) = self::quarterRound( $x1, $x5, $x9, $x13 ); |
|
| 88 | 88 | |
| 89 | 89 | # QUARTERROUND( x2, x6, x10, x14) |
| 90 | - list($x2, $x6, $x10, $x14) = self::quarterRound($x2, $x6, $x10, $x14); |
|
| 90 | + list( $x2, $x6, $x10, $x14 ) = self::quarterRound( $x2, $x6, $x10, $x14 ); |
|
| 91 | 91 | |
| 92 | 92 | # QUARTERROUND( x3, x7, x11, x15) |
| 93 | - list($x3, $x7, $x11, $x15) = self::quarterRound($x3, $x7, $x11, $x15); |
|
| 93 | + list( $x3, $x7, $x11, $x15 ) = self::quarterRound( $x3, $x7, $x11, $x15 ); |
|
| 94 | 94 | |
| 95 | 95 | # QUARTERROUND( x0, x5, x10, x15) |
| 96 | - list($x0, $x5, $x10, $x15) = self::quarterRound($x0, $x5, $x10, $x15); |
|
| 96 | + list( $x0, $x5, $x10, $x15 ) = self::quarterRound( $x0, $x5, $x10, $x15 ); |
|
| 97 | 97 | |
| 98 | 98 | # QUARTERROUND( x1, x6, x11, x12) |
| 99 | - list($x1, $x6, $x11, $x12) = self::quarterRound($x1, $x6, $x11, $x12); |
|
| 99 | + list( $x1, $x6, $x11, $x12 ) = self::quarterRound( $x1, $x6, $x11, $x12 ); |
|
| 100 | 100 | |
| 101 | 101 | # QUARTERROUND( x2, x7, x8, x13) |
| 102 | - list($x2, $x7, $x8, $x13) = self::quarterRound($x2, $x7, $x8, $x13); |
|
| 102 | + list( $x2, $x7, $x8, $x13 ) = self::quarterRound( $x2, $x7, $x8, $x13 ); |
|
| 103 | 103 | |
| 104 | 104 | # QUARTERROUND( x3, x4, x9, x14) |
| 105 | - list($x3, $x4, $x9, $x14) = self::quarterRound($x3, $x4, $x9, $x14); |
|
| 105 | + list( $x3, $x4, $x9, $x14 ) = self::quarterRound( $x3, $x4, $x9, $x14 ); |
|
| 106 | 106 | } |
| 107 | 107 | |
| 108 | 108 | return $x0->toReverseString() . |
@@ -7,16 +7,14 @@ discard block |
||
| 7 | 7 | /** |
| 8 | 8 | * Class ParagonIE_Sodium_Core_HChaCha20 |
| 9 | 9 | */ |
| 10 | -class ParagonIE_Sodium_Core32_HChaCha20 extends ParagonIE_Sodium_Core32_ChaCha20 |
|
| 11 | -{ |
|
| 10 | +class ParagonIE_Sodium_Core32_HChaCha20 extends ParagonIE_Sodium_Core32_ChaCha20 { |
|
| 12 | 11 | /** |
| 13 | 12 | * @param string $in |
| 14 | 13 | * @param string $key |
| 15 | 14 | * @param string|null $c |
| 16 | 15 | * @return string |
| 17 | 16 | */ |
| 18 | - public static function hChaCha20($in = '', $key = '', $c = null) |
|
| 19 | - { |
|
| 17 | + public static function hChaCha20($in = '', $key = '', $c = null) { |
|
| 20 | 18 | $ctx = array(); |
| 21 | 19 | |
| 22 | 20 | if ($c === null) { |
@@ -50,8 +48,7 @@ discard block |
||
| 50 | 48 | * @param array $ctx |
| 51 | 49 | * @return string |
| 52 | 50 | */ |
| 53 | - protected static function hChaCha20Bytes(array $ctx) |
|
| 54 | - { |
|
| 51 | + protected static function hChaCha20Bytes(array $ctx) { |
|
| 55 | 52 | /** |
| 56 | 53 | * @var ParagonIE_Sodium_Core32_Int32 $x0 |
| 57 | 54 | * @var ParagonIE_Sodium_Core32_Int32 $x1 |
@@ -1,7 +1,7 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | if (class_exists('ParagonIE_Sodium_Crypto', false)) { |
| 4 | - return; |
|
| 4 | + return; |
|
| 5 | 5 | } |
| 6 | 6 | |
| 7 | 7 | /** |
@@ -14,1176 +14,1176 @@ discard block |
||
| 14 | 14 | */ |
| 15 | 15 | abstract class ParagonIE_Sodium_Crypto |
| 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_Core_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_Core_Util::strlen($ad); |
|
| 84 | - |
|
| 85 | - /** @var string $mac - Message authentication code */ |
|
| 86 | - $mac = ParagonIE_Sodium_Core_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_Core_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_Core_ChaCha20::stream( |
|
| 97 | - 32, |
|
| 98 | - $nonce, |
|
| 99 | - $key |
|
| 100 | - ); |
|
| 101 | - |
|
| 102 | - /* Recalculate the Poly1305 authentication tag (MAC): */ |
|
| 103 | - $state = new ParagonIE_Sodium_Core_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_Core_Util::store64_le($adlen)); |
|
| 111 | - $state->update($ciphertext); |
|
| 112 | - $state->update(ParagonIE_Sodium_Core_Util::store64_le($clen)); |
|
| 113 | - $computed_mac = $state->finish(); |
|
| 114 | - |
|
| 115 | - /* Compare the given MAC with the recalculated MAC: */ |
|
| 116 | - if (!ParagonIE_Sodium_Core_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_Core_ChaCha20::streamXorIc( |
|
| 122 | - $ciphertext, |
|
| 123 | - $nonce, |
|
| 124 | - $key, |
|
| 125 | - ParagonIE_Sodium_Core_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_Core_Util::strlen($message); |
|
| 148 | - |
|
| 149 | - /** @var int $adlen - Length of the associated data */ |
|
| 150 | - $adlen = ParagonIE_Sodium_Core_Util::strlen($ad); |
|
| 151 | - |
|
| 152 | - /** @var string The first block of the chacha20 keystream, used as a poly1305 key */ |
|
| 153 | - $block0 = ParagonIE_Sodium_Core_ChaCha20::stream( |
|
| 154 | - 32, |
|
| 155 | - $nonce, |
|
| 156 | - $key |
|
| 157 | - ); |
|
| 158 | - $state = new ParagonIE_Sodium_Core_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_Core_ChaCha20::streamXorIc( |
|
| 167 | - $message, |
|
| 168 | - $nonce, |
|
| 169 | - $key, |
|
| 170 | - ParagonIE_Sodium_Core_Util::store64_le(1) |
|
| 171 | - ); |
|
| 172 | - |
|
| 173 | - $state->update($ad); |
|
| 174 | - $state->update(ParagonIE_Sodium_Core_Util::store64_le($adlen)); |
|
| 175 | - $state->update($ciphertext); |
|
| 176 | - $state->update(ParagonIE_Sodium_Core_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_Core_Util::strlen($ad); |
|
| 200 | - |
|
| 201 | - /** @var int $len - Length of message (ciphertext + MAC) */ |
|
| 202 | - $len = ParagonIE_Sodium_Core_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_Core_ChaCha20::ietfStream( |
|
| 209 | - 32, |
|
| 210 | - $nonce, |
|
| 211 | - $key |
|
| 212 | - ); |
|
| 213 | - |
|
| 214 | - /** @var string $mac - Message authentication code */ |
|
| 215 | - $mac = ParagonIE_Sodium_Core_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_Core_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_Core_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_Core_Util::store64_le($adlen)); |
|
| 240 | - $state->update(ParagonIE_Sodium_Core_Util::store64_le($clen)); |
|
| 241 | - $computed_mac = $state->finish(); |
|
| 242 | - |
|
| 243 | - /* Compare the given MAC with the recalculated MAC: */ |
|
| 244 | - if (!ParagonIE_Sodium_Core_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_Core_ChaCha20::ietfStreamXorIc( |
|
| 250 | - $ciphertext, |
|
| 251 | - $nonce, |
|
| 252 | - $key, |
|
| 253 | - ParagonIE_Sodium_Core_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_Core_Util::strlen($message); |
|
| 276 | - |
|
| 277 | - /** @var int $adlen - Length of the associated data */ |
|
| 278 | - $adlen = ParagonIE_Sodium_Core_Util::strlen($ad); |
|
| 279 | - |
|
| 280 | - /** @var string The first block of the chacha20 keystream, used as a poly1305 key */ |
|
| 281 | - $block0 = ParagonIE_Sodium_Core_ChaCha20::ietfStream( |
|
| 282 | - 32, |
|
| 283 | - $nonce, |
|
| 284 | - $key |
|
| 285 | - ); |
|
| 286 | - $state = new ParagonIE_Sodium_Core_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_Core_ChaCha20::ietfStreamXorIc( |
|
| 295 | - $message, |
|
| 296 | - $nonce, |
|
| 297 | - $key, |
|
| 298 | - ParagonIE_Sodium_Core_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_Core_Util::store64_le($adlen)); |
|
| 306 | - $state->update(ParagonIE_Sodium_Core_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_Core_HChaCha20::hChaCha20( |
|
| 329 | - ParagonIE_Sodium_Core_Util::substr($nonce, 0, 16), |
|
| 330 | - $key |
|
| 331 | - ); |
|
| 332 | - $nonceLast = "\x00\x00\x00\x00" . |
|
| 333 | - ParagonIE_Sodium_Core_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_Core_HChaCha20::hChaCha20( |
|
| 356 | - ParagonIE_Sodium_Core_Util::substr($nonce, 0, 16), |
|
| 357 | - $key |
|
| 358 | - ); |
|
| 359 | - $nonceLast = "\x00\x00\x00\x00" . |
|
| 360 | - ParagonIE_Sodium_Core_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_Core_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_Core_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_Core_Util::substr($message, 0, 32); |
|
| 481 | - |
|
| 482 | - /** @var string $ciphertext (ciphertext + MAC) */ |
|
| 483 | - $ciphertext = ParagonIE_Sodium_Core_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_Core_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 | - * @param string $seed |
|
| 546 | - * @return string |
|
| 547 | - */ |
|
| 548 | - public static function box_seed_keypair($seed) |
|
| 549 | - { |
|
| 550 | - $sKey = ParagonIE_Sodium_Core_Util::substr( |
|
| 551 | - hash('sha512', $seed, true), |
|
| 552 | - 0, |
|
| 553 | - 32 |
|
| 554 | - ); |
|
| 555 | - $pKey = self::scalarmult_base($sKey); |
|
| 556 | - return $sKey . $pKey; |
|
| 557 | - } |
|
| 558 | - |
|
| 559 | - /** |
|
| 560 | - * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
| 561 | - * |
|
| 562 | - * @param string $sKey |
|
| 563 | - * @param string $pKey |
|
| 564 | - * @return string |
|
| 565 | - */ |
|
| 566 | - public static function box_keypair_from_secretkey_and_publickey($sKey, $pKey) |
|
| 567 | - { |
|
| 568 | - return ParagonIE_Sodium_Core_Util::substr($sKey, 0, 32) . |
|
| 569 | - ParagonIE_Sodium_Core_Util::substr($pKey, 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_secretkey($keypair) |
|
| 580 | - { |
|
| 581 | - if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== 64) { |
|
| 582 | - throw new RangeException('Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES bytes long.'); |
|
| 583 | - } |
|
| 584 | - return ParagonIE_Sodium_Core_Util::substr($keypair, 0, 32); |
|
| 585 | - } |
|
| 586 | - |
|
| 587 | - /** |
|
| 588 | - * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
| 589 | - * |
|
| 590 | - * @param string $keypair |
|
| 591 | - * @return string |
|
| 592 | - * @throws RangeException |
|
| 593 | - */ |
|
| 594 | - public static function box_publickey($keypair) |
|
| 595 | - { |
|
| 596 | - if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES) { |
|
| 597 | - throw new RangeException('Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES bytes long.'); |
|
| 598 | - } |
|
| 599 | - return ParagonIE_Sodium_Core_Util::substr($keypair, 32, 32); |
|
| 600 | - } |
|
| 601 | - |
|
| 602 | - /** |
|
| 603 | - * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
| 604 | - * |
|
| 605 | - * @param string $sKey |
|
| 606 | - * @return string |
|
| 607 | - * @throws RangeException |
|
| 608 | - */ |
|
| 609 | - public static function box_publickey_from_secretkey($sKey) |
|
| 610 | - { |
|
| 611 | - if (ParagonIE_Sodium_Core_Util::strlen($sKey) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_SECRETKEYBYTES) { |
|
| 612 | - throw new RangeException('Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_SECRETKEYBYTES bytes long.'); |
|
| 613 | - } |
|
| 614 | - return self::scalarmult_base($sKey); |
|
| 615 | - } |
|
| 616 | - |
|
| 617 | - /** |
|
| 618 | - * Decrypt a message encrypted with box(). |
|
| 619 | - * |
|
| 620 | - * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
| 621 | - * |
|
| 622 | - * @param string $ciphertext |
|
| 623 | - * @param string $nonce |
|
| 624 | - * @param string $nonce |
|
| 625 | - * @param string $keypair |
|
| 626 | - * @return string |
|
| 627 | - */ |
|
| 628 | - public static function box_open($ciphertext, $nonce, $keypair) |
|
| 629 | - { |
|
| 630 | - return self::secretbox_open( |
|
| 631 | - $ciphertext, |
|
| 632 | - $nonce, |
|
| 633 | - self::box_beforenm( |
|
| 634 | - self::box_secretkey($keypair), |
|
| 635 | - self::box_publickey($keypair) |
|
| 636 | - ) |
|
| 637 | - ); |
|
| 638 | - } |
|
| 639 | - |
|
| 640 | - /** |
|
| 641 | - * Calculate a BLAKE2b hash. |
|
| 642 | - * |
|
| 643 | - * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
| 644 | - * |
|
| 645 | - * @param string $message |
|
| 646 | - * @param string|null $key |
|
| 647 | - * @param int $outlen |
|
| 648 | - * @return string |
|
| 649 | - * @throws RangeException |
|
| 650 | - */ |
|
| 651 | - public static function generichash($message, $key = '', $outlen = 32) |
|
| 652 | - { |
|
| 653 | - // This ensures that ParagonIE_Sodium_Core_BLAKE2b::$iv is initialized |
|
| 654 | - ParagonIE_Sodium_Core_BLAKE2b::pseudoConstructor(); |
|
| 655 | - |
|
| 656 | - $k = null; |
|
| 657 | - if (!empty($key)) { |
|
| 658 | - /** @var SplFixedArray $k */ |
|
| 659 | - $k = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($key); |
|
| 660 | - if ($k->count() > ParagonIE_Sodium_Core_BLAKE2b::KEYBYTES) { |
|
| 661 | - throw new RangeException('Invalid key size'); |
|
| 662 | - } |
|
| 663 | - } |
|
| 664 | - |
|
| 665 | - /** @var SplFixedArray $in */ |
|
| 666 | - $in = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($message); |
|
| 667 | - |
|
| 668 | - /** @var SplFixedArray $ctx */ |
|
| 669 | - $ctx = ParagonIE_Sodium_Core_BLAKE2b::init($k, $outlen); |
|
| 670 | - ParagonIE_Sodium_Core_BLAKE2b::update($ctx, $in, $in->count()); |
|
| 671 | - |
|
| 672 | - /** @var SplFixedArray $out */ |
|
| 673 | - $out = new SplFixedArray($outlen); |
|
| 674 | - $out = ParagonIE_Sodium_Core_BLAKE2b::finish($ctx, $out); |
|
| 675 | - |
|
| 676 | - /** @var array<int, int> */ |
|
| 677 | - $outArray = $out->toArray(); |
|
| 678 | - return ParagonIE_Sodium_Core_Util::intArrayToString($outArray); |
|
| 679 | - } |
|
| 680 | - |
|
| 681 | - /** |
|
| 682 | - * Finalize a BLAKE2b hashing context, returning the hash. |
|
| 683 | - * |
|
| 684 | - * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
| 685 | - * |
|
| 686 | - * @param string $ctx |
|
| 687 | - * @param int $outlen |
|
| 688 | - * @return string |
|
| 689 | - * @throws TypeError |
|
| 690 | - */ |
|
| 691 | - public static function generichash_final($ctx, $outlen = 32) |
|
| 692 | - { |
|
| 693 | - if (!is_string($ctx)) { |
|
| 694 | - throw new TypeError('Context must be a string'); |
|
| 695 | - } |
|
| 696 | - $out = new SplFixedArray($outlen); |
|
| 697 | - |
|
| 698 | - /** @var SplFixedArray $context */ |
|
| 699 | - $context = ParagonIE_Sodium_Core_BLAKE2b::stringToContext($ctx); |
|
| 700 | - |
|
| 701 | - /** @var SplFixedArray $out */ |
|
| 702 | - $out = ParagonIE_Sodium_Core_BLAKE2b::finish($context, $out); |
|
| 703 | - |
|
| 704 | - /** @var array<int, int> */ |
|
| 705 | - $outArray = $out->toArray(); |
|
| 706 | - return ParagonIE_Sodium_Core_Util::intArrayToString($outArray); |
|
| 707 | - } |
|
| 708 | - |
|
| 709 | - /** |
|
| 710 | - * Initialize a hashing context for BLAKE2b. |
|
| 711 | - * |
|
| 712 | - * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
| 713 | - * |
|
| 714 | - * @param string $key |
|
| 715 | - * @param int $outputLength |
|
| 716 | - * @return string |
|
| 717 | - * @throws RangeException |
|
| 718 | - */ |
|
| 719 | - public static function generichash_init($key = '', $outputLength = 32) |
|
| 720 | - { |
|
| 721 | - // This ensures that ParagonIE_Sodium_Core_BLAKE2b::$iv is initialized |
|
| 722 | - ParagonIE_Sodium_Core_BLAKE2b::pseudoConstructor(); |
|
| 723 | - |
|
| 724 | - $k = null; |
|
| 725 | - if (!empty($key)) { |
|
| 726 | - $k = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($key); |
|
| 727 | - if ($k->count() > ParagonIE_Sodium_Core_BLAKE2b::KEYBYTES) { |
|
| 728 | - throw new RangeException('Invalid key size'); |
|
| 729 | - } |
|
| 730 | - } |
|
| 731 | - |
|
| 732 | - /** @var SplFixedArray $ctx */ |
|
| 733 | - $ctx = ParagonIE_Sodium_Core_BLAKE2b::init($k, $outputLength); |
|
| 734 | - |
|
| 735 | - return ParagonIE_Sodium_Core_BLAKE2b::contextToString($ctx); |
|
| 736 | - } |
|
| 737 | - |
|
| 738 | - /** |
|
| 739 | - * Update a hashing context for BLAKE2b with $message |
|
| 740 | - * |
|
| 741 | - * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
| 742 | - * |
|
| 743 | - * @param string $ctx |
|
| 744 | - * @param string $message |
|
| 745 | - * @return string |
|
| 746 | - */ |
|
| 747 | - public static function generichash_update($ctx, $message) |
|
| 748 | - { |
|
| 749 | - // This ensures that ParagonIE_Sodium_Core_BLAKE2b::$iv is initialized |
|
| 750 | - ParagonIE_Sodium_Core_BLAKE2b::pseudoConstructor(); |
|
| 751 | - |
|
| 752 | - /** @var SplFixedArray $context */ |
|
| 753 | - $context = ParagonIE_Sodium_Core_BLAKE2b::stringToContext($ctx); |
|
| 754 | - |
|
| 755 | - /** @var SplFixedArray $in */ |
|
| 756 | - $in = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($message); |
|
| 757 | - |
|
| 758 | - ParagonIE_Sodium_Core_BLAKE2b::update($context, $in, $in->count()); |
|
| 759 | - |
|
| 760 | - return ParagonIE_Sodium_Core_BLAKE2b::contextToString($context); |
|
| 761 | - } |
|
| 762 | - |
|
| 763 | - /** |
|
| 764 | - * Libsodium's crypto_kx(). |
|
| 765 | - * |
|
| 766 | - * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
| 767 | - * |
|
| 768 | - * @param string $my_sk |
|
| 769 | - * @param string $their_pk |
|
| 770 | - * @param string $client_pk |
|
| 771 | - * @param string $server_pk |
|
| 772 | - * @return string |
|
| 773 | - */ |
|
| 774 | - public static function keyExchange($my_sk, $their_pk, $client_pk, $server_pk) |
|
| 775 | - { |
|
| 776 | - return self::generichash( |
|
| 777 | - self::scalarmult($my_sk, $their_pk) . |
|
| 778 | - $client_pk . |
|
| 779 | - $server_pk |
|
| 780 | - ); |
|
| 781 | - } |
|
| 782 | - |
|
| 783 | - /** |
|
| 784 | - * ECDH over Curve25519 |
|
| 785 | - * |
|
| 786 | - * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
| 787 | - * |
|
| 788 | - * @param string $sKey |
|
| 789 | - * @param string $pKey |
|
| 790 | - * @return string |
|
| 791 | - * |
|
| 792 | - * @throws Error |
|
| 793 | - */ |
|
| 794 | - public static function scalarmult($sKey, $pKey) |
|
| 795 | - { |
|
| 796 | - $q = ParagonIE_Sodium_Core_X25519::crypto_scalarmult_curve25519_ref10($sKey, $pKey); |
|
| 797 | - self::scalarmult_throw_if_zero($q); |
|
| 798 | - return $q; |
|
| 799 | - } |
|
| 800 | - |
|
| 801 | - /** |
|
| 802 | - * ECDH over Curve25519, using the basepoint. |
|
| 803 | - * Used to get a secret key from a public key. |
|
| 804 | - * |
|
| 805 | - * @param string $secret |
|
| 806 | - * @return string |
|
| 807 | - * |
|
| 808 | - * @throws Error |
|
| 809 | - */ |
|
| 810 | - public static function scalarmult_base($secret) |
|
| 811 | - { |
|
| 812 | - $q = ParagonIE_Sodium_Core_X25519::crypto_scalarmult_curve25519_ref10_base($secret); |
|
| 813 | - self::scalarmult_throw_if_zero($q); |
|
| 814 | - return $q; |
|
| 815 | - } |
|
| 816 | - |
|
| 817 | - /** |
|
| 818 | - * This throws an Error if a zero public key was passed to the function. |
|
| 819 | - * |
|
| 820 | - * @param string $q |
|
| 821 | - * @return void |
|
| 822 | - * @throws Error |
|
| 823 | - */ |
|
| 824 | - protected static function scalarmult_throw_if_zero($q) |
|
| 825 | - { |
|
| 826 | - $d = 0; |
|
| 827 | - for ($i = 0; $i < self::box_curve25519xsalsa20poly1305_SECRETKEYBYTES; ++$i) { |
|
| 828 | - $d |= ParagonIE_Sodium_Core_Util::chrToInt($q[$i]); |
|
| 829 | - } |
|
| 830 | - |
|
| 831 | - /* branch-free variant of === 0 */ |
|
| 832 | - if (-(1 & (($d - 1) >> 8))) { |
|
| 833 | - throw new Error('Zero public key is not allowed'); |
|
| 834 | - } |
|
| 835 | - } |
|
| 836 | - |
|
| 837 | - /** |
|
| 838 | - * XSalsa20-Poly1305 authenticated symmetric-key encryption. |
|
| 839 | - * |
|
| 840 | - * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
| 841 | - * |
|
| 842 | - * @param string $plaintext |
|
| 843 | - * @param string $nonce |
|
| 844 | - * @param string $key |
|
| 845 | - * @return string |
|
| 846 | - */ |
|
| 847 | - public static function secretbox($plaintext, $nonce, $key) |
|
| 848 | - { |
|
| 849 | - /** @var string $subkey */ |
|
| 850 | - $subkey = ParagonIE_Sodium_Core_HSalsa20::hsalsa20($nonce, $key); |
|
| 851 | - |
|
| 852 | - /** @var string $block0 */ |
|
| 853 | - $block0 = str_repeat("\x00", 32); |
|
| 854 | - |
|
| 855 | - /** @var int $mlen - Length of the plaintext message */ |
|
| 856 | - $mlen = ParagonIE_Sodium_Core_Util::strlen($plaintext); |
|
| 857 | - $mlen0 = $mlen; |
|
| 858 | - if ($mlen0 > 64 - self::secretbox_xsalsa20poly1305_ZEROBYTES) { |
|
| 859 | - $mlen0 = 64 - self::secretbox_xsalsa20poly1305_ZEROBYTES; |
|
| 860 | - } |
|
| 861 | - $block0 .= ParagonIE_Sodium_Core_Util::substr($plaintext, 0, $mlen0); |
|
| 862 | - |
|
| 863 | - /** @var string $block0 */ |
|
| 864 | - $block0 = ParagonIE_Sodium_Core_Salsa20::salsa20_xor( |
|
| 865 | - $block0, |
|
| 866 | - ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8), |
|
| 867 | - $subkey |
|
| 868 | - ); |
|
| 869 | - |
|
| 870 | - /** @var string $c */ |
|
| 871 | - $c = ParagonIE_Sodium_Core_Util::substr( |
|
| 872 | - $block0, |
|
| 873 | - self::secretbox_xsalsa20poly1305_ZEROBYTES |
|
| 874 | - ); |
|
| 875 | - if ($mlen > $mlen0) { |
|
| 876 | - $c .= ParagonIE_Sodium_Core_Salsa20::salsa20_xor_ic( |
|
| 877 | - ParagonIE_Sodium_Core_Util::substr( |
|
| 878 | - $plaintext, |
|
| 879 | - self::secretbox_xsalsa20poly1305_ZEROBYTES |
|
| 880 | - ), |
|
| 881 | - ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8), |
|
| 882 | - 1, |
|
| 883 | - $subkey |
|
| 884 | - ); |
|
| 885 | - } |
|
| 886 | - $state = new ParagonIE_Sodium_Core_Poly1305_State( |
|
| 887 | - ParagonIE_Sodium_Core_Util::substr( |
|
| 888 | - $block0, |
|
| 889 | - 0, |
|
| 890 | - self::onetimeauth_poly1305_KEYBYTES |
|
| 891 | - ) |
|
| 892 | - ); |
|
| 893 | - try { |
|
| 894 | - ParagonIE_Sodium_Compat::memzero($block0); |
|
| 895 | - ParagonIE_Sodium_Compat::memzero($subkey); |
|
| 896 | - } catch (Error $ex) { |
|
| 897 | - $block0 = null; |
|
| 898 | - $subkey = null; |
|
| 899 | - } |
|
| 900 | - |
|
| 901 | - $state->update($c); |
|
| 902 | - |
|
| 903 | - /** @var string $c - MAC || ciphertext */ |
|
| 904 | - $c = $state->finish() . $c; |
|
| 905 | - unset($state); |
|
| 906 | - |
|
| 907 | - return $c; |
|
| 908 | - } |
|
| 909 | - |
|
| 910 | - /** |
|
| 911 | - * Decrypt a ciphertext generated via secretbox(). |
|
| 912 | - * |
|
| 913 | - * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
| 914 | - * |
|
| 915 | - * @param string $ciphertext |
|
| 916 | - * @param string $nonce |
|
| 917 | - * @param string $key |
|
| 918 | - * @return string |
|
| 919 | - * @throws Error |
|
| 920 | - */ |
|
| 921 | - public static function secretbox_open($ciphertext, $nonce, $key) |
|
| 922 | - { |
|
| 923 | - /** @var string $mac */ |
|
| 924 | - $mac = ParagonIE_Sodium_Core_Util::substr( |
|
| 925 | - $ciphertext, |
|
| 926 | - 0, |
|
| 927 | - self::secretbox_xsalsa20poly1305_MACBYTES |
|
| 928 | - ); |
|
| 929 | - |
|
| 930 | - /** @var string $c */ |
|
| 931 | - $c = ParagonIE_Sodium_Core_Util::substr( |
|
| 932 | - $ciphertext, |
|
| 933 | - self::secretbox_xsalsa20poly1305_MACBYTES |
|
| 934 | - ); |
|
| 935 | - |
|
| 936 | - /** @var int $clen */ |
|
| 937 | - $clen = ParagonIE_Sodium_Core_Util::strlen($c); |
|
| 938 | - |
|
| 939 | - /** @var string $subkey */ |
|
| 940 | - $subkey = ParagonIE_Sodium_Core_HSalsa20::hsalsa20($nonce, $key); |
|
| 941 | - |
|
| 942 | - /** @var string $block0 */ |
|
| 943 | - $block0 = ParagonIE_Sodium_Core_Salsa20::salsa20( |
|
| 944 | - 64, |
|
| 945 | - ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8), |
|
| 946 | - $subkey |
|
| 947 | - ); |
|
| 948 | - $verified = ParagonIE_Sodium_Core_Poly1305::onetimeauth_verify( |
|
| 949 | - $mac, |
|
| 950 | - $c, |
|
| 951 | - ParagonIE_Sodium_Core_Util::substr($block0, 0, 32) |
|
| 952 | - ); |
|
| 953 | - if (!$verified) { |
|
| 954 | - try { |
|
| 955 | - ParagonIE_Sodium_Compat::memzero($subkey); |
|
| 956 | - } catch (Error $ex) { |
|
| 957 | - $subkey = null; |
|
| 958 | - } |
|
| 959 | - throw new Error('Invalid MAC'); |
|
| 960 | - } |
|
| 961 | - |
|
| 962 | - /** @var string $m - Decrypted message */ |
|
| 963 | - $m = ParagonIE_Sodium_Core_Util::xorStrings( |
|
| 964 | - ParagonIE_Sodium_Core_Util::substr($block0, self::secretbox_xsalsa20poly1305_ZEROBYTES), |
|
| 965 | - ParagonIE_Sodium_Core_Util::substr($c, 0, self::secretbox_xsalsa20poly1305_ZEROBYTES) |
|
| 966 | - ); |
|
| 967 | - if ($clen > self::secretbox_xsalsa20poly1305_ZEROBYTES) { |
|
| 968 | - // We had more than 1 block, so let's continue to decrypt the rest. |
|
| 969 | - $m .= ParagonIE_Sodium_Core_Salsa20::salsa20_xor_ic( |
|
| 970 | - ParagonIE_Sodium_Core_Util::substr( |
|
| 971 | - $c, |
|
| 972 | - self::secretbox_xsalsa20poly1305_ZEROBYTES |
|
| 973 | - ), |
|
| 974 | - ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8), |
|
| 975 | - 1, |
|
| 976 | - $subkey |
|
| 977 | - ); |
|
| 978 | - } |
|
| 979 | - return $m; |
|
| 980 | - } |
|
| 981 | - |
|
| 982 | - /** |
|
| 983 | - * XChaCha20-Poly1305 authenticated symmetric-key encryption. |
|
| 984 | - * |
|
| 985 | - * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
| 986 | - * |
|
| 987 | - * @param string $plaintext |
|
| 988 | - * @param string $nonce |
|
| 989 | - * @param string $key |
|
| 990 | - * @return string |
|
| 991 | - */ |
|
| 992 | - public static function secretbox_xchacha20poly1305($plaintext, $nonce, $key) |
|
| 993 | - { |
|
| 994 | - /** @var string $subkey */ |
|
| 995 | - $subkey = ParagonIE_Sodium_Core_HChaCha20::hChaCha20( |
|
| 996 | - ParagonIE_Sodium_Core_Util::substr($nonce, 0, 16), |
|
| 997 | - $key |
|
| 998 | - ); |
|
| 999 | - $nonceLast = ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8); |
|
| 1000 | - |
|
| 1001 | - /** @var string $block0 */ |
|
| 1002 | - $block0 = str_repeat("\x00", 32); |
|
| 1003 | - |
|
| 1004 | - /** @var int $mlen - Length of the plaintext message */ |
|
| 1005 | - $mlen = ParagonIE_Sodium_Core_Util::strlen($plaintext); |
|
| 1006 | - $mlen0 = $mlen; |
|
| 1007 | - if ($mlen0 > 64 - self::secretbox_xchacha20poly1305_ZEROBYTES) { |
|
| 1008 | - $mlen0 = 64 - self::secretbox_xchacha20poly1305_ZEROBYTES; |
|
| 1009 | - } |
|
| 1010 | - $block0 .= ParagonIE_Sodium_Core_Util::substr($plaintext, 0, $mlen0); |
|
| 1011 | - |
|
| 1012 | - /** @var string $block0 */ |
|
| 1013 | - $block0 = ParagonIE_Sodium_Core_ChaCha20::streamXorIc( |
|
| 1014 | - $block0, |
|
| 1015 | - $nonceLast, |
|
| 1016 | - $subkey |
|
| 1017 | - ); |
|
| 1018 | - |
|
| 1019 | - /** @var string $c */ |
|
| 1020 | - $c = ParagonIE_Sodium_Core_Util::substr( |
|
| 1021 | - $block0, |
|
| 1022 | - self::secretbox_xchacha20poly1305_ZEROBYTES |
|
| 1023 | - ); |
|
| 1024 | - if ($mlen > $mlen0) { |
|
| 1025 | - $c .= ParagonIE_Sodium_Core_ChaCha20::streamXorIc( |
|
| 1026 | - ParagonIE_Sodium_Core_Util::substr( |
|
| 1027 | - $plaintext, |
|
| 1028 | - self::secretbox_xchacha20poly1305_ZEROBYTES |
|
| 1029 | - ), |
|
| 1030 | - $nonceLast, |
|
| 1031 | - $subkey, |
|
| 1032 | - ParagonIE_Sodium_Core_Util::store64_le(1) |
|
| 1033 | - ); |
|
| 1034 | - } |
|
| 1035 | - $state = new ParagonIE_Sodium_Core_Poly1305_State( |
|
| 1036 | - ParagonIE_Sodium_Core_Util::substr( |
|
| 1037 | - $block0, |
|
| 1038 | - 0, |
|
| 1039 | - self::onetimeauth_poly1305_KEYBYTES |
|
| 1040 | - ) |
|
| 1041 | - ); |
|
| 1042 | - try { |
|
| 1043 | - ParagonIE_Sodium_Compat::memzero($block0); |
|
| 1044 | - ParagonIE_Sodium_Compat::memzero($subkey); |
|
| 1045 | - } catch (Error $ex) { |
|
| 1046 | - $block0 = null; |
|
| 1047 | - $subkey = null; |
|
| 1048 | - } |
|
| 1049 | - |
|
| 1050 | - $state->update($c); |
|
| 1051 | - |
|
| 1052 | - /** @var string $c - MAC || ciphertext */ |
|
| 1053 | - $c = $state->finish() . $c; |
|
| 1054 | - unset($state); |
|
| 1055 | - |
|
| 1056 | - return $c; |
|
| 1057 | - } |
|
| 1058 | - |
|
| 1059 | - /** |
|
| 1060 | - * Decrypt a ciphertext generated via secretbox_xchacha20poly1305(). |
|
| 1061 | - * |
|
| 1062 | - * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
| 1063 | - * |
|
| 1064 | - * @param string $ciphertext |
|
| 1065 | - * @param string $nonce |
|
| 1066 | - * @param string $key |
|
| 1067 | - * @return string |
|
| 1068 | - * @throws Error |
|
| 1069 | - */ |
|
| 1070 | - public static function secretbox_xchacha20poly1305_open($ciphertext, $nonce, $key) |
|
| 1071 | - { |
|
| 1072 | - /** @var string $mac */ |
|
| 1073 | - $mac = ParagonIE_Sodium_Core_Util::substr( |
|
| 1074 | - $ciphertext, |
|
| 1075 | - 0, |
|
| 1076 | - self::secretbox_xchacha20poly1305_MACBYTES |
|
| 1077 | - ); |
|
| 1078 | - |
|
| 1079 | - /** @var string $c */ |
|
| 1080 | - $c = ParagonIE_Sodium_Core_Util::substr( |
|
| 1081 | - $ciphertext, |
|
| 1082 | - self::secretbox_xchacha20poly1305_MACBYTES |
|
| 1083 | - ); |
|
| 1084 | - |
|
| 1085 | - /** @var int $clen */ |
|
| 1086 | - $clen = ParagonIE_Sodium_Core_Util::strlen($c); |
|
| 1087 | - |
|
| 1088 | - /** @var string $subkey */ |
|
| 1089 | - $subkey = ParagonIE_Sodium_Core_HChaCha20::hchacha20($nonce, $key); |
|
| 1090 | - |
|
| 1091 | - /** @var string $block0 */ |
|
| 1092 | - $block0 = ParagonIE_Sodium_Core_ChaCha20::stream( |
|
| 1093 | - 64, |
|
| 1094 | - ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8), |
|
| 1095 | - $subkey |
|
| 1096 | - ); |
|
| 1097 | - $verified = ParagonIE_Sodium_Core_Poly1305::onetimeauth_verify( |
|
| 1098 | - $mac, |
|
| 1099 | - $c, |
|
| 1100 | - ParagonIE_Sodium_Core_Util::substr($block0, 0, 32) |
|
| 1101 | - ); |
|
| 1102 | - |
|
| 1103 | - if (!$verified) { |
|
| 1104 | - try { |
|
| 1105 | - ParagonIE_Sodium_Compat::memzero($subkey); |
|
| 1106 | - } catch (Error $ex) { |
|
| 1107 | - $subkey = null; |
|
| 1108 | - } |
|
| 1109 | - throw new Error('Invalid MAC'); |
|
| 1110 | - } |
|
| 1111 | - |
|
| 1112 | - /** @var string $m - Decrypted message */ |
|
| 1113 | - $m = ParagonIE_Sodium_Core_Util::xorStrings( |
|
| 1114 | - ParagonIE_Sodium_Core_Util::substr($block0, self::secretbox_xchacha20poly1305_ZEROBYTES), |
|
| 1115 | - ParagonIE_Sodium_Core_Util::substr($c, 0, self::secretbox_xchacha20poly1305_ZEROBYTES) |
|
| 1116 | - ); |
|
| 1117 | - |
|
| 1118 | - if ($clen > self::secretbox_xchacha20poly1305_ZEROBYTES) { |
|
| 1119 | - // We had more than 1 block, so let's continue to decrypt the rest. |
|
| 1120 | - $m .= ParagonIE_Sodium_Core_ChaCha20::streamXorIc( |
|
| 1121 | - ParagonIE_Sodium_Core_Util::substr( |
|
| 1122 | - $c, |
|
| 1123 | - self::secretbox_xchacha20poly1305_ZEROBYTES |
|
| 1124 | - ), |
|
| 1125 | - ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8), |
|
| 1126 | - $subkey, |
|
| 1127 | - ParagonIE_Sodium_Core_Util::store64_le(1) |
|
| 1128 | - ); |
|
| 1129 | - } |
|
| 1130 | - return $m; |
|
| 1131 | - } |
|
| 1132 | - |
|
| 1133 | - /** |
|
| 1134 | - * Detached Ed25519 signature. |
|
| 1135 | - * |
|
| 1136 | - * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
| 1137 | - * |
|
| 1138 | - * @param string $message |
|
| 1139 | - * @param string $sk |
|
| 1140 | - * @return string |
|
| 1141 | - */ |
|
| 1142 | - public static function sign_detached($message, $sk) |
|
| 1143 | - { |
|
| 1144 | - return ParagonIE_Sodium_Core_Ed25519::sign_detached($message, $sk); |
|
| 1145 | - } |
|
| 1146 | - |
|
| 1147 | - /** |
|
| 1148 | - * Attached Ed25519 signature. (Returns a signed message.) |
|
| 1149 | - * |
|
| 1150 | - * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
| 1151 | - * |
|
| 1152 | - * @param string $message |
|
| 1153 | - * @param string $sk |
|
| 1154 | - * @return string |
|
| 1155 | - */ |
|
| 1156 | - public static function sign($message, $sk) |
|
| 1157 | - { |
|
| 1158 | - return ParagonIE_Sodium_Core_Ed25519::sign($message, $sk); |
|
| 1159 | - } |
|
| 1160 | - |
|
| 1161 | - /** |
|
| 1162 | - * Opens a signed message. If valid, returns the message. |
|
| 1163 | - * |
|
| 1164 | - * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
| 1165 | - * |
|
| 1166 | - * @param string $signedMessage |
|
| 1167 | - * @param string $pk |
|
| 1168 | - * @return string |
|
| 1169 | - */ |
|
| 1170 | - public static function sign_open($signedMessage, $pk) |
|
| 1171 | - { |
|
| 1172 | - return ParagonIE_Sodium_Core_Ed25519::sign_open($signedMessage, $pk); |
|
| 1173 | - } |
|
| 1174 | - |
|
| 1175 | - /** |
|
| 1176 | - * Verify a detached signature of a given message and public key. |
|
| 1177 | - * |
|
| 1178 | - * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
| 1179 | - * |
|
| 1180 | - * @param string $signature |
|
| 1181 | - * @param string $message |
|
| 1182 | - * @param string $pk |
|
| 1183 | - * @return bool |
|
| 1184 | - */ |
|
| 1185 | - public static function sign_verify_detached($signature, $message, $pk) |
|
| 1186 | - { |
|
| 1187 | - return ParagonIE_Sodium_Core_Ed25519::verify_detached($signature, $message, $pk); |
|
| 1188 | - } |
|
| 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_Core_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_Core_Util::strlen($ad); |
|
| 84 | + |
|
| 85 | + /** @var string $mac - Message authentication code */ |
|
| 86 | + $mac = ParagonIE_Sodium_Core_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_Core_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_Core_ChaCha20::stream( |
|
| 97 | + 32, |
|
| 98 | + $nonce, |
|
| 99 | + $key |
|
| 100 | + ); |
|
| 101 | + |
|
| 102 | + /* Recalculate the Poly1305 authentication tag (MAC): */ |
|
| 103 | + $state = new ParagonIE_Sodium_Core_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_Core_Util::store64_le($adlen)); |
|
| 111 | + $state->update($ciphertext); |
|
| 112 | + $state->update(ParagonIE_Sodium_Core_Util::store64_le($clen)); |
|
| 113 | + $computed_mac = $state->finish(); |
|
| 114 | + |
|
| 115 | + /* Compare the given MAC with the recalculated MAC: */ |
|
| 116 | + if (!ParagonIE_Sodium_Core_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_Core_ChaCha20::streamXorIc( |
|
| 122 | + $ciphertext, |
|
| 123 | + $nonce, |
|
| 124 | + $key, |
|
| 125 | + ParagonIE_Sodium_Core_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_Core_Util::strlen($message); |
|
| 148 | + |
|
| 149 | + /** @var int $adlen - Length of the associated data */ |
|
| 150 | + $adlen = ParagonIE_Sodium_Core_Util::strlen($ad); |
|
| 151 | + |
|
| 152 | + /** @var string The first block of the chacha20 keystream, used as a poly1305 key */ |
|
| 153 | + $block0 = ParagonIE_Sodium_Core_ChaCha20::stream( |
|
| 154 | + 32, |
|
| 155 | + $nonce, |
|
| 156 | + $key |
|
| 157 | + ); |
|
| 158 | + $state = new ParagonIE_Sodium_Core_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_Core_ChaCha20::streamXorIc( |
|
| 167 | + $message, |
|
| 168 | + $nonce, |
|
| 169 | + $key, |
|
| 170 | + ParagonIE_Sodium_Core_Util::store64_le(1) |
|
| 171 | + ); |
|
| 172 | + |
|
| 173 | + $state->update($ad); |
|
| 174 | + $state->update(ParagonIE_Sodium_Core_Util::store64_le($adlen)); |
|
| 175 | + $state->update($ciphertext); |
|
| 176 | + $state->update(ParagonIE_Sodium_Core_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_Core_Util::strlen($ad); |
|
| 200 | + |
|
| 201 | + /** @var int $len - Length of message (ciphertext + MAC) */ |
|
| 202 | + $len = ParagonIE_Sodium_Core_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_Core_ChaCha20::ietfStream( |
|
| 209 | + 32, |
|
| 210 | + $nonce, |
|
| 211 | + $key |
|
| 212 | + ); |
|
| 213 | + |
|
| 214 | + /** @var string $mac - Message authentication code */ |
|
| 215 | + $mac = ParagonIE_Sodium_Core_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_Core_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_Core_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_Core_Util::store64_le($adlen)); |
|
| 240 | + $state->update(ParagonIE_Sodium_Core_Util::store64_le($clen)); |
|
| 241 | + $computed_mac = $state->finish(); |
|
| 242 | + |
|
| 243 | + /* Compare the given MAC with the recalculated MAC: */ |
|
| 244 | + if (!ParagonIE_Sodium_Core_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_Core_ChaCha20::ietfStreamXorIc( |
|
| 250 | + $ciphertext, |
|
| 251 | + $nonce, |
|
| 252 | + $key, |
|
| 253 | + ParagonIE_Sodium_Core_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_Core_Util::strlen($message); |
|
| 276 | + |
|
| 277 | + /** @var int $adlen - Length of the associated data */ |
|
| 278 | + $adlen = ParagonIE_Sodium_Core_Util::strlen($ad); |
|
| 279 | + |
|
| 280 | + /** @var string The first block of the chacha20 keystream, used as a poly1305 key */ |
|
| 281 | + $block0 = ParagonIE_Sodium_Core_ChaCha20::ietfStream( |
|
| 282 | + 32, |
|
| 283 | + $nonce, |
|
| 284 | + $key |
|
| 285 | + ); |
|
| 286 | + $state = new ParagonIE_Sodium_Core_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_Core_ChaCha20::ietfStreamXorIc( |
|
| 295 | + $message, |
|
| 296 | + $nonce, |
|
| 297 | + $key, |
|
| 298 | + ParagonIE_Sodium_Core_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_Core_Util::store64_le($adlen)); |
|
| 306 | + $state->update(ParagonIE_Sodium_Core_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_Core_HChaCha20::hChaCha20( |
|
| 329 | + ParagonIE_Sodium_Core_Util::substr($nonce, 0, 16), |
|
| 330 | + $key |
|
| 331 | + ); |
|
| 332 | + $nonceLast = "\x00\x00\x00\x00" . |
|
| 333 | + ParagonIE_Sodium_Core_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_Core_HChaCha20::hChaCha20( |
|
| 356 | + ParagonIE_Sodium_Core_Util::substr($nonce, 0, 16), |
|
| 357 | + $key |
|
| 358 | + ); |
|
| 359 | + $nonceLast = "\x00\x00\x00\x00" . |
|
| 360 | + ParagonIE_Sodium_Core_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_Core_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_Core_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_Core_Util::substr($message, 0, 32); |
|
| 481 | + |
|
| 482 | + /** @var string $ciphertext (ciphertext + MAC) */ |
|
| 483 | + $ciphertext = ParagonIE_Sodium_Core_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_Core_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 | + * @param string $seed |
|
| 546 | + * @return string |
|
| 547 | + */ |
|
| 548 | + public static function box_seed_keypair($seed) |
|
| 549 | + { |
|
| 550 | + $sKey = ParagonIE_Sodium_Core_Util::substr( |
|
| 551 | + hash('sha512', $seed, true), |
|
| 552 | + 0, |
|
| 553 | + 32 |
|
| 554 | + ); |
|
| 555 | + $pKey = self::scalarmult_base($sKey); |
|
| 556 | + return $sKey . $pKey; |
|
| 557 | + } |
|
| 558 | + |
|
| 559 | + /** |
|
| 560 | + * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
| 561 | + * |
|
| 562 | + * @param string $sKey |
|
| 563 | + * @param string $pKey |
|
| 564 | + * @return string |
|
| 565 | + */ |
|
| 566 | + public static function box_keypair_from_secretkey_and_publickey($sKey, $pKey) |
|
| 567 | + { |
|
| 568 | + return ParagonIE_Sodium_Core_Util::substr($sKey, 0, 32) . |
|
| 569 | + ParagonIE_Sodium_Core_Util::substr($pKey, 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_secretkey($keypair) |
|
| 580 | + { |
|
| 581 | + if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== 64) { |
|
| 582 | + throw new RangeException('Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES bytes long.'); |
|
| 583 | + } |
|
| 584 | + return ParagonIE_Sodium_Core_Util::substr($keypair, 0, 32); |
|
| 585 | + } |
|
| 586 | + |
|
| 587 | + /** |
|
| 588 | + * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
| 589 | + * |
|
| 590 | + * @param string $keypair |
|
| 591 | + * @return string |
|
| 592 | + * @throws RangeException |
|
| 593 | + */ |
|
| 594 | + public static function box_publickey($keypair) |
|
| 595 | + { |
|
| 596 | + if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES) { |
|
| 597 | + throw new RangeException('Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES bytes long.'); |
|
| 598 | + } |
|
| 599 | + return ParagonIE_Sodium_Core_Util::substr($keypair, 32, 32); |
|
| 600 | + } |
|
| 601 | + |
|
| 602 | + /** |
|
| 603 | + * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
| 604 | + * |
|
| 605 | + * @param string $sKey |
|
| 606 | + * @return string |
|
| 607 | + * @throws RangeException |
|
| 608 | + */ |
|
| 609 | + public static function box_publickey_from_secretkey($sKey) |
|
| 610 | + { |
|
| 611 | + if (ParagonIE_Sodium_Core_Util::strlen($sKey) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_SECRETKEYBYTES) { |
|
| 612 | + throw new RangeException('Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_SECRETKEYBYTES bytes long.'); |
|
| 613 | + } |
|
| 614 | + return self::scalarmult_base($sKey); |
|
| 615 | + } |
|
| 616 | + |
|
| 617 | + /** |
|
| 618 | + * Decrypt a message encrypted with box(). |
|
| 619 | + * |
|
| 620 | + * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
| 621 | + * |
|
| 622 | + * @param string $ciphertext |
|
| 623 | + * @param string $nonce |
|
| 624 | + * @param string $nonce |
|
| 625 | + * @param string $keypair |
|
| 626 | + * @return string |
|
| 627 | + */ |
|
| 628 | + public static function box_open($ciphertext, $nonce, $keypair) |
|
| 629 | + { |
|
| 630 | + return self::secretbox_open( |
|
| 631 | + $ciphertext, |
|
| 632 | + $nonce, |
|
| 633 | + self::box_beforenm( |
|
| 634 | + self::box_secretkey($keypair), |
|
| 635 | + self::box_publickey($keypair) |
|
| 636 | + ) |
|
| 637 | + ); |
|
| 638 | + } |
|
| 639 | + |
|
| 640 | + /** |
|
| 641 | + * Calculate a BLAKE2b hash. |
|
| 642 | + * |
|
| 643 | + * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
| 644 | + * |
|
| 645 | + * @param string $message |
|
| 646 | + * @param string|null $key |
|
| 647 | + * @param int $outlen |
|
| 648 | + * @return string |
|
| 649 | + * @throws RangeException |
|
| 650 | + */ |
|
| 651 | + public static function generichash($message, $key = '', $outlen = 32) |
|
| 652 | + { |
|
| 653 | + // This ensures that ParagonIE_Sodium_Core_BLAKE2b::$iv is initialized |
|
| 654 | + ParagonIE_Sodium_Core_BLAKE2b::pseudoConstructor(); |
|
| 655 | + |
|
| 656 | + $k = null; |
|
| 657 | + if (!empty($key)) { |
|
| 658 | + /** @var SplFixedArray $k */ |
|
| 659 | + $k = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($key); |
|
| 660 | + if ($k->count() > ParagonIE_Sodium_Core_BLAKE2b::KEYBYTES) { |
|
| 661 | + throw new RangeException('Invalid key size'); |
|
| 662 | + } |
|
| 663 | + } |
|
| 664 | + |
|
| 665 | + /** @var SplFixedArray $in */ |
|
| 666 | + $in = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($message); |
|
| 667 | + |
|
| 668 | + /** @var SplFixedArray $ctx */ |
|
| 669 | + $ctx = ParagonIE_Sodium_Core_BLAKE2b::init($k, $outlen); |
|
| 670 | + ParagonIE_Sodium_Core_BLAKE2b::update($ctx, $in, $in->count()); |
|
| 671 | + |
|
| 672 | + /** @var SplFixedArray $out */ |
|
| 673 | + $out = new SplFixedArray($outlen); |
|
| 674 | + $out = ParagonIE_Sodium_Core_BLAKE2b::finish($ctx, $out); |
|
| 675 | + |
|
| 676 | + /** @var array<int, int> */ |
|
| 677 | + $outArray = $out->toArray(); |
|
| 678 | + return ParagonIE_Sodium_Core_Util::intArrayToString($outArray); |
|
| 679 | + } |
|
| 680 | + |
|
| 681 | + /** |
|
| 682 | + * Finalize a BLAKE2b hashing context, returning the hash. |
|
| 683 | + * |
|
| 684 | + * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
| 685 | + * |
|
| 686 | + * @param string $ctx |
|
| 687 | + * @param int $outlen |
|
| 688 | + * @return string |
|
| 689 | + * @throws TypeError |
|
| 690 | + */ |
|
| 691 | + public static function generichash_final($ctx, $outlen = 32) |
|
| 692 | + { |
|
| 693 | + if (!is_string($ctx)) { |
|
| 694 | + throw new TypeError('Context must be a string'); |
|
| 695 | + } |
|
| 696 | + $out = new SplFixedArray($outlen); |
|
| 697 | + |
|
| 698 | + /** @var SplFixedArray $context */ |
|
| 699 | + $context = ParagonIE_Sodium_Core_BLAKE2b::stringToContext($ctx); |
|
| 700 | + |
|
| 701 | + /** @var SplFixedArray $out */ |
|
| 702 | + $out = ParagonIE_Sodium_Core_BLAKE2b::finish($context, $out); |
|
| 703 | + |
|
| 704 | + /** @var array<int, int> */ |
|
| 705 | + $outArray = $out->toArray(); |
|
| 706 | + return ParagonIE_Sodium_Core_Util::intArrayToString($outArray); |
|
| 707 | + } |
|
| 708 | + |
|
| 709 | + /** |
|
| 710 | + * Initialize a hashing context for BLAKE2b. |
|
| 711 | + * |
|
| 712 | + * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
| 713 | + * |
|
| 714 | + * @param string $key |
|
| 715 | + * @param int $outputLength |
|
| 716 | + * @return string |
|
| 717 | + * @throws RangeException |
|
| 718 | + */ |
|
| 719 | + public static function generichash_init($key = '', $outputLength = 32) |
|
| 720 | + { |
|
| 721 | + // This ensures that ParagonIE_Sodium_Core_BLAKE2b::$iv is initialized |
|
| 722 | + ParagonIE_Sodium_Core_BLAKE2b::pseudoConstructor(); |
|
| 723 | + |
|
| 724 | + $k = null; |
|
| 725 | + if (!empty($key)) { |
|
| 726 | + $k = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($key); |
|
| 727 | + if ($k->count() > ParagonIE_Sodium_Core_BLAKE2b::KEYBYTES) { |
|
| 728 | + throw new RangeException('Invalid key size'); |
|
| 729 | + } |
|
| 730 | + } |
|
| 731 | + |
|
| 732 | + /** @var SplFixedArray $ctx */ |
|
| 733 | + $ctx = ParagonIE_Sodium_Core_BLAKE2b::init($k, $outputLength); |
|
| 734 | + |
|
| 735 | + return ParagonIE_Sodium_Core_BLAKE2b::contextToString($ctx); |
|
| 736 | + } |
|
| 737 | + |
|
| 738 | + /** |
|
| 739 | + * Update a hashing context for BLAKE2b with $message |
|
| 740 | + * |
|
| 741 | + * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
| 742 | + * |
|
| 743 | + * @param string $ctx |
|
| 744 | + * @param string $message |
|
| 745 | + * @return string |
|
| 746 | + */ |
|
| 747 | + public static function generichash_update($ctx, $message) |
|
| 748 | + { |
|
| 749 | + // This ensures that ParagonIE_Sodium_Core_BLAKE2b::$iv is initialized |
|
| 750 | + ParagonIE_Sodium_Core_BLAKE2b::pseudoConstructor(); |
|
| 751 | + |
|
| 752 | + /** @var SplFixedArray $context */ |
|
| 753 | + $context = ParagonIE_Sodium_Core_BLAKE2b::stringToContext($ctx); |
|
| 754 | + |
|
| 755 | + /** @var SplFixedArray $in */ |
|
| 756 | + $in = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($message); |
|
| 757 | + |
|
| 758 | + ParagonIE_Sodium_Core_BLAKE2b::update($context, $in, $in->count()); |
|
| 759 | + |
|
| 760 | + return ParagonIE_Sodium_Core_BLAKE2b::contextToString($context); |
|
| 761 | + } |
|
| 762 | + |
|
| 763 | + /** |
|
| 764 | + * Libsodium's crypto_kx(). |
|
| 765 | + * |
|
| 766 | + * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
| 767 | + * |
|
| 768 | + * @param string $my_sk |
|
| 769 | + * @param string $their_pk |
|
| 770 | + * @param string $client_pk |
|
| 771 | + * @param string $server_pk |
|
| 772 | + * @return string |
|
| 773 | + */ |
|
| 774 | + public static function keyExchange($my_sk, $their_pk, $client_pk, $server_pk) |
|
| 775 | + { |
|
| 776 | + return self::generichash( |
|
| 777 | + self::scalarmult($my_sk, $their_pk) . |
|
| 778 | + $client_pk . |
|
| 779 | + $server_pk |
|
| 780 | + ); |
|
| 781 | + } |
|
| 782 | + |
|
| 783 | + /** |
|
| 784 | + * ECDH over Curve25519 |
|
| 785 | + * |
|
| 786 | + * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
| 787 | + * |
|
| 788 | + * @param string $sKey |
|
| 789 | + * @param string $pKey |
|
| 790 | + * @return string |
|
| 791 | + * |
|
| 792 | + * @throws Error |
|
| 793 | + */ |
|
| 794 | + public static function scalarmult($sKey, $pKey) |
|
| 795 | + { |
|
| 796 | + $q = ParagonIE_Sodium_Core_X25519::crypto_scalarmult_curve25519_ref10($sKey, $pKey); |
|
| 797 | + self::scalarmult_throw_if_zero($q); |
|
| 798 | + return $q; |
|
| 799 | + } |
|
| 800 | + |
|
| 801 | + /** |
|
| 802 | + * ECDH over Curve25519, using the basepoint. |
|
| 803 | + * Used to get a secret key from a public key. |
|
| 804 | + * |
|
| 805 | + * @param string $secret |
|
| 806 | + * @return string |
|
| 807 | + * |
|
| 808 | + * @throws Error |
|
| 809 | + */ |
|
| 810 | + public static function scalarmult_base($secret) |
|
| 811 | + { |
|
| 812 | + $q = ParagonIE_Sodium_Core_X25519::crypto_scalarmult_curve25519_ref10_base($secret); |
|
| 813 | + self::scalarmult_throw_if_zero($q); |
|
| 814 | + return $q; |
|
| 815 | + } |
|
| 816 | + |
|
| 817 | + /** |
|
| 818 | + * This throws an Error if a zero public key was passed to the function. |
|
| 819 | + * |
|
| 820 | + * @param string $q |
|
| 821 | + * @return void |
|
| 822 | + * @throws Error |
|
| 823 | + */ |
|
| 824 | + protected static function scalarmult_throw_if_zero($q) |
|
| 825 | + { |
|
| 826 | + $d = 0; |
|
| 827 | + for ($i = 0; $i < self::box_curve25519xsalsa20poly1305_SECRETKEYBYTES; ++$i) { |
|
| 828 | + $d |= ParagonIE_Sodium_Core_Util::chrToInt($q[$i]); |
|
| 829 | + } |
|
| 830 | + |
|
| 831 | + /* branch-free variant of === 0 */ |
|
| 832 | + if (-(1 & (($d - 1) >> 8))) { |
|
| 833 | + throw new Error('Zero public key is not allowed'); |
|
| 834 | + } |
|
| 835 | + } |
|
| 836 | + |
|
| 837 | + /** |
|
| 838 | + * XSalsa20-Poly1305 authenticated symmetric-key encryption. |
|
| 839 | + * |
|
| 840 | + * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
| 841 | + * |
|
| 842 | + * @param string $plaintext |
|
| 843 | + * @param string $nonce |
|
| 844 | + * @param string $key |
|
| 845 | + * @return string |
|
| 846 | + */ |
|
| 847 | + public static function secretbox($plaintext, $nonce, $key) |
|
| 848 | + { |
|
| 849 | + /** @var string $subkey */ |
|
| 850 | + $subkey = ParagonIE_Sodium_Core_HSalsa20::hsalsa20($nonce, $key); |
|
| 851 | + |
|
| 852 | + /** @var string $block0 */ |
|
| 853 | + $block0 = str_repeat("\x00", 32); |
|
| 854 | + |
|
| 855 | + /** @var int $mlen - Length of the plaintext message */ |
|
| 856 | + $mlen = ParagonIE_Sodium_Core_Util::strlen($plaintext); |
|
| 857 | + $mlen0 = $mlen; |
|
| 858 | + if ($mlen0 > 64 - self::secretbox_xsalsa20poly1305_ZEROBYTES) { |
|
| 859 | + $mlen0 = 64 - self::secretbox_xsalsa20poly1305_ZEROBYTES; |
|
| 860 | + } |
|
| 861 | + $block0 .= ParagonIE_Sodium_Core_Util::substr($plaintext, 0, $mlen0); |
|
| 862 | + |
|
| 863 | + /** @var string $block0 */ |
|
| 864 | + $block0 = ParagonIE_Sodium_Core_Salsa20::salsa20_xor( |
|
| 865 | + $block0, |
|
| 866 | + ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8), |
|
| 867 | + $subkey |
|
| 868 | + ); |
|
| 869 | + |
|
| 870 | + /** @var string $c */ |
|
| 871 | + $c = ParagonIE_Sodium_Core_Util::substr( |
|
| 872 | + $block0, |
|
| 873 | + self::secretbox_xsalsa20poly1305_ZEROBYTES |
|
| 874 | + ); |
|
| 875 | + if ($mlen > $mlen0) { |
|
| 876 | + $c .= ParagonIE_Sodium_Core_Salsa20::salsa20_xor_ic( |
|
| 877 | + ParagonIE_Sodium_Core_Util::substr( |
|
| 878 | + $plaintext, |
|
| 879 | + self::secretbox_xsalsa20poly1305_ZEROBYTES |
|
| 880 | + ), |
|
| 881 | + ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8), |
|
| 882 | + 1, |
|
| 883 | + $subkey |
|
| 884 | + ); |
|
| 885 | + } |
|
| 886 | + $state = new ParagonIE_Sodium_Core_Poly1305_State( |
|
| 887 | + ParagonIE_Sodium_Core_Util::substr( |
|
| 888 | + $block0, |
|
| 889 | + 0, |
|
| 890 | + self::onetimeauth_poly1305_KEYBYTES |
|
| 891 | + ) |
|
| 892 | + ); |
|
| 893 | + try { |
|
| 894 | + ParagonIE_Sodium_Compat::memzero($block0); |
|
| 895 | + ParagonIE_Sodium_Compat::memzero($subkey); |
|
| 896 | + } catch (Error $ex) { |
|
| 897 | + $block0 = null; |
|
| 898 | + $subkey = null; |
|
| 899 | + } |
|
| 900 | + |
|
| 901 | + $state->update($c); |
|
| 902 | + |
|
| 903 | + /** @var string $c - MAC || ciphertext */ |
|
| 904 | + $c = $state->finish() . $c; |
|
| 905 | + unset($state); |
|
| 906 | + |
|
| 907 | + return $c; |
|
| 908 | + } |
|
| 909 | + |
|
| 910 | + /** |
|
| 911 | + * Decrypt a ciphertext generated via secretbox(). |
|
| 912 | + * |
|
| 913 | + * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
| 914 | + * |
|
| 915 | + * @param string $ciphertext |
|
| 916 | + * @param string $nonce |
|
| 917 | + * @param string $key |
|
| 918 | + * @return string |
|
| 919 | + * @throws Error |
|
| 920 | + */ |
|
| 921 | + public static function secretbox_open($ciphertext, $nonce, $key) |
|
| 922 | + { |
|
| 923 | + /** @var string $mac */ |
|
| 924 | + $mac = ParagonIE_Sodium_Core_Util::substr( |
|
| 925 | + $ciphertext, |
|
| 926 | + 0, |
|
| 927 | + self::secretbox_xsalsa20poly1305_MACBYTES |
|
| 928 | + ); |
|
| 929 | + |
|
| 930 | + /** @var string $c */ |
|
| 931 | + $c = ParagonIE_Sodium_Core_Util::substr( |
|
| 932 | + $ciphertext, |
|
| 933 | + self::secretbox_xsalsa20poly1305_MACBYTES |
|
| 934 | + ); |
|
| 935 | + |
|
| 936 | + /** @var int $clen */ |
|
| 937 | + $clen = ParagonIE_Sodium_Core_Util::strlen($c); |
|
| 938 | + |
|
| 939 | + /** @var string $subkey */ |
|
| 940 | + $subkey = ParagonIE_Sodium_Core_HSalsa20::hsalsa20($nonce, $key); |
|
| 941 | + |
|
| 942 | + /** @var string $block0 */ |
|
| 943 | + $block0 = ParagonIE_Sodium_Core_Salsa20::salsa20( |
|
| 944 | + 64, |
|
| 945 | + ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8), |
|
| 946 | + $subkey |
|
| 947 | + ); |
|
| 948 | + $verified = ParagonIE_Sodium_Core_Poly1305::onetimeauth_verify( |
|
| 949 | + $mac, |
|
| 950 | + $c, |
|
| 951 | + ParagonIE_Sodium_Core_Util::substr($block0, 0, 32) |
|
| 952 | + ); |
|
| 953 | + if (!$verified) { |
|
| 954 | + try { |
|
| 955 | + ParagonIE_Sodium_Compat::memzero($subkey); |
|
| 956 | + } catch (Error $ex) { |
|
| 957 | + $subkey = null; |
|
| 958 | + } |
|
| 959 | + throw new Error('Invalid MAC'); |
|
| 960 | + } |
|
| 961 | + |
|
| 962 | + /** @var string $m - Decrypted message */ |
|
| 963 | + $m = ParagonIE_Sodium_Core_Util::xorStrings( |
|
| 964 | + ParagonIE_Sodium_Core_Util::substr($block0, self::secretbox_xsalsa20poly1305_ZEROBYTES), |
|
| 965 | + ParagonIE_Sodium_Core_Util::substr($c, 0, self::secretbox_xsalsa20poly1305_ZEROBYTES) |
|
| 966 | + ); |
|
| 967 | + if ($clen > self::secretbox_xsalsa20poly1305_ZEROBYTES) { |
|
| 968 | + // We had more than 1 block, so let's continue to decrypt the rest. |
|
| 969 | + $m .= ParagonIE_Sodium_Core_Salsa20::salsa20_xor_ic( |
|
| 970 | + ParagonIE_Sodium_Core_Util::substr( |
|
| 971 | + $c, |
|
| 972 | + self::secretbox_xsalsa20poly1305_ZEROBYTES |
|
| 973 | + ), |
|
| 974 | + ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8), |
|
| 975 | + 1, |
|
| 976 | + $subkey |
|
| 977 | + ); |
|
| 978 | + } |
|
| 979 | + return $m; |
|
| 980 | + } |
|
| 981 | + |
|
| 982 | + /** |
|
| 983 | + * XChaCha20-Poly1305 authenticated symmetric-key encryption. |
|
| 984 | + * |
|
| 985 | + * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
| 986 | + * |
|
| 987 | + * @param string $plaintext |
|
| 988 | + * @param string $nonce |
|
| 989 | + * @param string $key |
|
| 990 | + * @return string |
|
| 991 | + */ |
|
| 992 | + public static function secretbox_xchacha20poly1305($plaintext, $nonce, $key) |
|
| 993 | + { |
|
| 994 | + /** @var string $subkey */ |
|
| 995 | + $subkey = ParagonIE_Sodium_Core_HChaCha20::hChaCha20( |
|
| 996 | + ParagonIE_Sodium_Core_Util::substr($nonce, 0, 16), |
|
| 997 | + $key |
|
| 998 | + ); |
|
| 999 | + $nonceLast = ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8); |
|
| 1000 | + |
|
| 1001 | + /** @var string $block0 */ |
|
| 1002 | + $block0 = str_repeat("\x00", 32); |
|
| 1003 | + |
|
| 1004 | + /** @var int $mlen - Length of the plaintext message */ |
|
| 1005 | + $mlen = ParagonIE_Sodium_Core_Util::strlen($plaintext); |
|
| 1006 | + $mlen0 = $mlen; |
|
| 1007 | + if ($mlen0 > 64 - self::secretbox_xchacha20poly1305_ZEROBYTES) { |
|
| 1008 | + $mlen0 = 64 - self::secretbox_xchacha20poly1305_ZEROBYTES; |
|
| 1009 | + } |
|
| 1010 | + $block0 .= ParagonIE_Sodium_Core_Util::substr($plaintext, 0, $mlen0); |
|
| 1011 | + |
|
| 1012 | + /** @var string $block0 */ |
|
| 1013 | + $block0 = ParagonIE_Sodium_Core_ChaCha20::streamXorIc( |
|
| 1014 | + $block0, |
|
| 1015 | + $nonceLast, |
|
| 1016 | + $subkey |
|
| 1017 | + ); |
|
| 1018 | + |
|
| 1019 | + /** @var string $c */ |
|
| 1020 | + $c = ParagonIE_Sodium_Core_Util::substr( |
|
| 1021 | + $block0, |
|
| 1022 | + self::secretbox_xchacha20poly1305_ZEROBYTES |
|
| 1023 | + ); |
|
| 1024 | + if ($mlen > $mlen0) { |
|
| 1025 | + $c .= ParagonIE_Sodium_Core_ChaCha20::streamXorIc( |
|
| 1026 | + ParagonIE_Sodium_Core_Util::substr( |
|
| 1027 | + $plaintext, |
|
| 1028 | + self::secretbox_xchacha20poly1305_ZEROBYTES |
|
| 1029 | + ), |
|
| 1030 | + $nonceLast, |
|
| 1031 | + $subkey, |
|
| 1032 | + ParagonIE_Sodium_Core_Util::store64_le(1) |
|
| 1033 | + ); |
|
| 1034 | + } |
|
| 1035 | + $state = new ParagonIE_Sodium_Core_Poly1305_State( |
|
| 1036 | + ParagonIE_Sodium_Core_Util::substr( |
|
| 1037 | + $block0, |
|
| 1038 | + 0, |
|
| 1039 | + self::onetimeauth_poly1305_KEYBYTES |
|
| 1040 | + ) |
|
| 1041 | + ); |
|
| 1042 | + try { |
|
| 1043 | + ParagonIE_Sodium_Compat::memzero($block0); |
|
| 1044 | + ParagonIE_Sodium_Compat::memzero($subkey); |
|
| 1045 | + } catch (Error $ex) { |
|
| 1046 | + $block0 = null; |
|
| 1047 | + $subkey = null; |
|
| 1048 | + } |
|
| 1049 | + |
|
| 1050 | + $state->update($c); |
|
| 1051 | + |
|
| 1052 | + /** @var string $c - MAC || ciphertext */ |
|
| 1053 | + $c = $state->finish() . $c; |
|
| 1054 | + unset($state); |
|
| 1055 | + |
|
| 1056 | + return $c; |
|
| 1057 | + } |
|
| 1058 | + |
|
| 1059 | + /** |
|
| 1060 | + * Decrypt a ciphertext generated via secretbox_xchacha20poly1305(). |
|
| 1061 | + * |
|
| 1062 | + * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
| 1063 | + * |
|
| 1064 | + * @param string $ciphertext |
|
| 1065 | + * @param string $nonce |
|
| 1066 | + * @param string $key |
|
| 1067 | + * @return string |
|
| 1068 | + * @throws Error |
|
| 1069 | + */ |
|
| 1070 | + public static function secretbox_xchacha20poly1305_open($ciphertext, $nonce, $key) |
|
| 1071 | + { |
|
| 1072 | + /** @var string $mac */ |
|
| 1073 | + $mac = ParagonIE_Sodium_Core_Util::substr( |
|
| 1074 | + $ciphertext, |
|
| 1075 | + 0, |
|
| 1076 | + self::secretbox_xchacha20poly1305_MACBYTES |
|
| 1077 | + ); |
|
| 1078 | + |
|
| 1079 | + /** @var string $c */ |
|
| 1080 | + $c = ParagonIE_Sodium_Core_Util::substr( |
|
| 1081 | + $ciphertext, |
|
| 1082 | + self::secretbox_xchacha20poly1305_MACBYTES |
|
| 1083 | + ); |
|
| 1084 | + |
|
| 1085 | + /** @var int $clen */ |
|
| 1086 | + $clen = ParagonIE_Sodium_Core_Util::strlen($c); |
|
| 1087 | + |
|
| 1088 | + /** @var string $subkey */ |
|
| 1089 | + $subkey = ParagonIE_Sodium_Core_HChaCha20::hchacha20($nonce, $key); |
|
| 1090 | + |
|
| 1091 | + /** @var string $block0 */ |
|
| 1092 | + $block0 = ParagonIE_Sodium_Core_ChaCha20::stream( |
|
| 1093 | + 64, |
|
| 1094 | + ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8), |
|
| 1095 | + $subkey |
|
| 1096 | + ); |
|
| 1097 | + $verified = ParagonIE_Sodium_Core_Poly1305::onetimeauth_verify( |
|
| 1098 | + $mac, |
|
| 1099 | + $c, |
|
| 1100 | + ParagonIE_Sodium_Core_Util::substr($block0, 0, 32) |
|
| 1101 | + ); |
|
| 1102 | + |
|
| 1103 | + if (!$verified) { |
|
| 1104 | + try { |
|
| 1105 | + ParagonIE_Sodium_Compat::memzero($subkey); |
|
| 1106 | + } catch (Error $ex) { |
|
| 1107 | + $subkey = null; |
|
| 1108 | + } |
|
| 1109 | + throw new Error('Invalid MAC'); |
|
| 1110 | + } |
|
| 1111 | + |
|
| 1112 | + /** @var string $m - Decrypted message */ |
|
| 1113 | + $m = ParagonIE_Sodium_Core_Util::xorStrings( |
|
| 1114 | + ParagonIE_Sodium_Core_Util::substr($block0, self::secretbox_xchacha20poly1305_ZEROBYTES), |
|
| 1115 | + ParagonIE_Sodium_Core_Util::substr($c, 0, self::secretbox_xchacha20poly1305_ZEROBYTES) |
|
| 1116 | + ); |
|
| 1117 | + |
|
| 1118 | + if ($clen > self::secretbox_xchacha20poly1305_ZEROBYTES) { |
|
| 1119 | + // We had more than 1 block, so let's continue to decrypt the rest. |
|
| 1120 | + $m .= ParagonIE_Sodium_Core_ChaCha20::streamXorIc( |
|
| 1121 | + ParagonIE_Sodium_Core_Util::substr( |
|
| 1122 | + $c, |
|
| 1123 | + self::secretbox_xchacha20poly1305_ZEROBYTES |
|
| 1124 | + ), |
|
| 1125 | + ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8), |
|
| 1126 | + $subkey, |
|
| 1127 | + ParagonIE_Sodium_Core_Util::store64_le(1) |
|
| 1128 | + ); |
|
| 1129 | + } |
|
| 1130 | + return $m; |
|
| 1131 | + } |
|
| 1132 | + |
|
| 1133 | + /** |
|
| 1134 | + * Detached Ed25519 signature. |
|
| 1135 | + * |
|
| 1136 | + * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
| 1137 | + * |
|
| 1138 | + * @param string $message |
|
| 1139 | + * @param string $sk |
|
| 1140 | + * @return string |
|
| 1141 | + */ |
|
| 1142 | + public static function sign_detached($message, $sk) |
|
| 1143 | + { |
|
| 1144 | + return ParagonIE_Sodium_Core_Ed25519::sign_detached($message, $sk); |
|
| 1145 | + } |
|
| 1146 | + |
|
| 1147 | + /** |
|
| 1148 | + * Attached Ed25519 signature. (Returns a signed message.) |
|
| 1149 | + * |
|
| 1150 | + * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
| 1151 | + * |
|
| 1152 | + * @param string $message |
|
| 1153 | + * @param string $sk |
|
| 1154 | + * @return string |
|
| 1155 | + */ |
|
| 1156 | + public static function sign($message, $sk) |
|
| 1157 | + { |
|
| 1158 | + return ParagonIE_Sodium_Core_Ed25519::sign($message, $sk); |
|
| 1159 | + } |
|
| 1160 | + |
|
| 1161 | + /** |
|
| 1162 | + * Opens a signed message. If valid, returns the message. |
|
| 1163 | + * |
|
| 1164 | + * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
| 1165 | + * |
|
| 1166 | + * @param string $signedMessage |
|
| 1167 | + * @param string $pk |
|
| 1168 | + * @return string |
|
| 1169 | + */ |
|
| 1170 | + public static function sign_open($signedMessage, $pk) |
|
| 1171 | + { |
|
| 1172 | + return ParagonIE_Sodium_Core_Ed25519::sign_open($signedMessage, $pk); |
|
| 1173 | + } |
|
| 1174 | + |
|
| 1175 | + /** |
|
| 1176 | + * Verify a detached signature of a given message and public key. |
|
| 1177 | + * |
|
| 1178 | + * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
| 1179 | + * |
|
| 1180 | + * @param string $signature |
|
| 1181 | + * @param string $message |
|
| 1182 | + * @param string $pk |
|
| 1183 | + * @return bool |
|
| 1184 | + */ |
|
| 1185 | + public static function sign_verify_detached($signature, $message, $pk) |
|
| 1186 | + { |
|
| 1187 | + return ParagonIE_Sodium_Core_Ed25519::verify_detached($signature, $message, $pk); |
|
| 1188 | + } |
|
| 1189 | 1189 | } |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -if (class_exists('ParagonIE_Sodium_Crypto', false)) { |
|
| 3 | +if ( class_exists( 'ParagonIE_Sodium_Crypto', false ) ) { |
|
| 4 | 4 | return; |
| 5 | 5 | } |
| 6 | 6 | |
@@ -74,13 +74,13 @@ discard block |
||
| 74 | 74 | $key = '' |
| 75 | 75 | ) { |
| 76 | 76 | /** @var int $len - Length of message (ciphertext + MAC) */ |
| 77 | - $len = ParagonIE_Sodium_Core_Util::strlen($message); |
|
| 77 | + $len = ParagonIE_Sodium_Core_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_Core_Util::strlen($ad); |
|
| 83 | + $adlen = ParagonIE_Sodium_Core_Util::strlen( $ad ); |
|
| 84 | 84 | |
| 85 | 85 | /** @var string $mac - Message authentication code */ |
| 86 | 86 | $mac = ParagonIE_Sodium_Core_Util::substr( |
@@ -90,7 +90,7 @@ discard block |
||
| 90 | 90 | ); |
| 91 | 91 | |
| 92 | 92 | /** @var string $ciphertext - The encrypted message (sans MAC) */ |
| 93 | - $ciphertext = ParagonIE_Sodium_Core_Util::substr($message, 0, $clen); |
|
| 93 | + $ciphertext = ParagonIE_Sodium_Core_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_Core_ChaCha20::stream( |
@@ -100,21 +100,21 @@ discard block |
||
| 100 | 100 | ); |
| 101 | 101 | |
| 102 | 102 | /* Recalculate the Poly1305 authentication tag (MAC): */ |
| 103 | - $state = new ParagonIE_Sodium_Core_Poly1305_State($block0); |
|
| 103 | + $state = new ParagonIE_Sodium_Core_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_Core_Util::store64_le($adlen)); |
|
| 111 | - $state->update($ciphertext); |
|
| 112 | - $state->update(ParagonIE_Sodium_Core_Util::store64_le($clen)); |
|
| 109 | + $state->update( $ad ); |
|
| 110 | + $state->update( ParagonIE_Sodium_Core_Util::store64_le( $adlen ) ); |
|
| 111 | + $state->update( $ciphertext ); |
|
| 112 | + $state->update( ParagonIE_Sodium_Core_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_Core_Util::verify_16($computed_mac, $mac)) { |
|
| 117 | - throw new Error('Invalid MAC'); |
|
| 116 | + if ( ! ParagonIE_Sodium_Core_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 |
||
| 122 | 122 | $ciphertext, |
| 123 | 123 | $nonce, |
| 124 | 124 | $key, |
| 125 | - ParagonIE_Sodium_Core_Util::store64_le(1) |
|
| 125 | + ParagonIE_Sodium_Core_Util::store64_le( 1 ) |
|
| 126 | 126 | ); |
| 127 | 127 | } |
| 128 | 128 | |
@@ -144,10 +144,10 @@ discard block |
||
| 144 | 144 | $key = '' |
| 145 | 145 | ) { |
| 146 | 146 | /** @var int $len - Length of the plaintext message */ |
| 147 | - $len = ParagonIE_Sodium_Core_Util::strlen($message); |
|
| 147 | + $len = ParagonIE_Sodium_Core_Util::strlen( $message ); |
|
| 148 | 148 | |
| 149 | 149 | /** @var int $adlen - Length of the associated data */ |
| 150 | - $adlen = ParagonIE_Sodium_Core_Util::strlen($ad); |
|
| 150 | + $adlen = ParagonIE_Sodium_Core_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_Core_ChaCha20::stream( |
@@ -155,10 +155,10 @@ discard block |
||
| 155 | 155 | $nonce, |
| 156 | 156 | $key |
| 157 | 157 | ); |
| 158 | - $state = new ParagonIE_Sodium_Core_Poly1305_State($block0); |
|
| 158 | + $state = new ParagonIE_Sodium_Core_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 |
||
| 167 | 167 | $message, |
| 168 | 168 | $nonce, |
| 169 | 169 | $key, |
| 170 | - ParagonIE_Sodium_Core_Util::store64_le(1) |
|
| 170 | + ParagonIE_Sodium_Core_Util::store64_le( 1 ) |
|
| 171 | 171 | ); |
| 172 | 172 | |
| 173 | - $state->update($ad); |
|
| 174 | - $state->update(ParagonIE_Sodium_Core_Util::store64_le($adlen)); |
|
| 175 | - $state->update($ciphertext); |
|
| 176 | - $state->update(ParagonIE_Sodium_Core_Util::store64_le($len)); |
|
| 173 | + $state->update( $ad ); |
|
| 174 | + $state->update( ParagonIE_Sodium_Core_Util::store64_le( $adlen ) ); |
|
| 175 | + $state->update( $ciphertext ); |
|
| 176 | + $state->update( ParagonIE_Sodium_Core_Util::store64_le( $len ) ); |
|
| 177 | 177 | return $ciphertext . $state->finish(); |
| 178 | 178 | } |
| 179 | 179 | |
@@ -196,10 +196,10 @@ discard block |
||
| 196 | 196 | $key = '' |
| 197 | 197 | ) { |
| 198 | 198 | /** @var int $adlen - Length of associated data */ |
| 199 | - $adlen = ParagonIE_Sodium_Core_Util::strlen($ad); |
|
| 199 | + $adlen = ParagonIE_Sodium_Core_Util::strlen( $ad ); |
|
| 200 | 200 | |
| 201 | 201 | /** @var int $len - Length of message (ciphertext + MAC) */ |
| 202 | - $len = ParagonIE_Sodium_Core_Util::strlen($message); |
|
| 202 | + $len = ParagonIE_Sodium_Core_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 |
||
| 226 | 226 | ); |
| 227 | 227 | |
| 228 | 228 | /* Recalculate the Poly1305 authentication tag (MAC): */ |
| 229 | - $state = new ParagonIE_Sodium_Core_Poly1305_State($block0); |
|
| 229 | + $state = new ParagonIE_Sodium_Core_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_Core_Util::store64_le($adlen)); |
|
| 240 | - $state->update(ParagonIE_Sodium_Core_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_Core_Util::store64_le( $adlen ) ); |
|
| 240 | + $state->update( ParagonIE_Sodium_Core_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_Core_Util::verify_16($computed_mac, $mac)) { |
|
| 245 | - throw new Error('Invalid MAC'); |
|
| 244 | + if ( ! ParagonIE_Sodium_Core_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 |
||
| 250 | 250 | $ciphertext, |
| 251 | 251 | $nonce, |
| 252 | 252 | $key, |
| 253 | - ParagonIE_Sodium_Core_Util::store64_le(1) |
|
| 253 | + ParagonIE_Sodium_Core_Util::store64_le( 1 ) |
|
| 254 | 254 | ); |
| 255 | 255 | } |
| 256 | 256 | |
@@ -272,10 +272,10 @@ discard block |
||
| 272 | 272 | $key = '' |
| 273 | 273 | ) { |
| 274 | 274 | /** @var int $len - Length of the plaintext message */ |
| 275 | - $len = ParagonIE_Sodium_Core_Util::strlen($message); |
|
| 275 | + $len = ParagonIE_Sodium_Core_Util::strlen( $message ); |
|
| 276 | 276 | |
| 277 | 277 | /** @var int $adlen - Length of the associated data */ |
| 278 | - $adlen = ParagonIE_Sodium_Core_Util::strlen($ad); |
|
| 278 | + $adlen = ParagonIE_Sodium_Core_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_Core_ChaCha20::ietfStream( |
@@ -283,10 +283,10 @@ discard block |
||
| 283 | 283 | $nonce, |
| 284 | 284 | $key |
| 285 | 285 | ); |
| 286 | - $state = new ParagonIE_Sodium_Core_Poly1305_State($block0); |
|
| 286 | + $state = new ParagonIE_Sodium_Core_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 |
||
| 295 | 295 | $message, |
| 296 | 296 | $nonce, |
| 297 | 297 | $key, |
| 298 | - ParagonIE_Sodium_Core_Util::store64_le(1) |
|
| 298 | + ParagonIE_Sodium_Core_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_Core_Util::store64_le($adlen)); |
|
| 306 | - $state->update(ParagonIE_Sodium_Core_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_Core_Util::store64_le( $adlen ) ); |
|
| 306 | + $state->update( ParagonIE_Sodium_Core_Util::store64_le( $len ) ); |
|
| 307 | 307 | return $ciphertext . $state->finish(); |
| 308 | 308 | } |
| 309 | 309 | |
@@ -326,13 +326,13 @@ discard block |
||
| 326 | 326 | $key = '' |
| 327 | 327 | ) { |
| 328 | 328 | $subkey = ParagonIE_Sodium_Core_HChaCha20::hChaCha20( |
| 329 | - ParagonIE_Sodium_Core_Util::substr($nonce, 0, 16), |
|
| 329 | + ParagonIE_Sodium_Core_Util::substr( $nonce, 0, 16 ), |
|
| 330 | 330 | $key |
| 331 | 331 | ); |
| 332 | 332 | $nonceLast = "\x00\x00\x00\x00" . |
| 333 | - ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8); |
|
| 333 | + ParagonIE_Sodium_Core_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 |
||
| 353 | 353 | $key = '' |
| 354 | 354 | ) { |
| 355 | 355 | $subkey = ParagonIE_Sodium_Core_HChaCha20::hChaCha20( |
| 356 | - ParagonIE_Sodium_Core_Util::substr($nonce, 0, 16), |
|
| 356 | + ParagonIE_Sodium_Core_Util::substr( $nonce, 0, 16 ), |
|
| 357 | 357 | $key |
| 358 | 358 | ); |
| 359 | 359 | $nonceLast = "\x00\x00\x00\x00" . |
| 360 | - ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8); |
|
| 360 | + ParagonIE_Sodium_Core_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 |
||
| 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_Core_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 |
||
| 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_Core_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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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_Core_Util::substr($message, 0, 32); |
|
| 480 | + $ephemeralPK = ParagonIE_Sodium_Core_Util::substr( $message, 0, 32 ); |
|
| 481 | 481 | |
| 482 | 482 | /** @var string $ciphertext (ciphertext + MAC) */ |
| 483 | - $ciphertext = ParagonIE_Sodium_Core_Util::substr($message, 32); |
|
| 483 | + $ciphertext = ParagonIE_Sodium_Core_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 |
||
| 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 |
||
| 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_Core_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 |
||
| 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 | |
@@ -545,14 +545,14 @@ discard block |
||
| 545 | 545 | * @param string $seed |
| 546 | 546 | * @return string |
| 547 | 547 | */ |
| 548 | - public static function box_seed_keypair($seed) |
|
| 548 | + public static function box_seed_keypair( $seed ) |
|
| 549 | 549 | { |
| 550 | 550 | $sKey = ParagonIE_Sodium_Core_Util::substr( |
| 551 | - hash('sha512', $seed, true), |
|
| 551 | + hash( 'sha512', $seed, true ), |
|
| 552 | 552 | 0, |
| 553 | 553 | 32 |
| 554 | 554 | ); |
| 555 | - $pKey = self::scalarmult_base($sKey); |
|
| 555 | + $pKey = self::scalarmult_base( $sKey ); |
|
| 556 | 556 | return $sKey . $pKey; |
| 557 | 557 | } |
| 558 | 558 | |
@@ -563,10 +563,10 @@ discard block |
||
| 563 | 563 | * @param string $pKey |
| 564 | 564 | * @return string |
| 565 | 565 | */ |
| 566 | - public static function box_keypair_from_secretkey_and_publickey($sKey, $pKey) |
|
| 566 | + public static function box_keypair_from_secretkey_and_publickey( $sKey, $pKey ) |
|
| 567 | 567 | { |
| 568 | - return ParagonIE_Sodium_Core_Util::substr($sKey, 0, 32) . |
|
| 569 | - ParagonIE_Sodium_Core_Util::substr($pKey, 0, 32); |
|
| 568 | + return ParagonIE_Sodium_Core_Util::substr( $sKey, 0, 32 ) . |
|
| 569 | + ParagonIE_Sodium_Core_Util::substr( $pKey, 0, 32 ); |
|
| 570 | 570 | } |
| 571 | 571 | |
| 572 | 572 | /** |
@@ -576,12 +576,12 @@ discard block |
||
| 576 | 576 | * @return string |
| 577 | 577 | * @throws RangeException |
| 578 | 578 | */ |
| 579 | - public static function box_secretkey($keypair) |
|
| 579 | + public static function box_secretkey( $keypair ) |
|
| 580 | 580 | { |
| 581 | - if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== 64) { |
|
| 582 | - throw new RangeException('Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES bytes long.'); |
|
| 581 | + if ( ParagonIE_Sodium_Core_Util::strlen( $keypair ) !== 64 ) { |
|
| 582 | + throw new RangeException( 'Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES bytes long.' ); |
|
| 583 | 583 | } |
| 584 | - return ParagonIE_Sodium_Core_Util::substr($keypair, 0, 32); |
|
| 584 | + return ParagonIE_Sodium_Core_Util::substr( $keypair, 0, 32 ); |
|
| 585 | 585 | } |
| 586 | 586 | |
| 587 | 587 | /** |
@@ -591,12 +591,12 @@ discard block |
||
| 591 | 591 | * @return string |
| 592 | 592 | * @throws RangeException |
| 593 | 593 | */ |
| 594 | - public static function box_publickey($keypair) |
|
| 594 | + public static function box_publickey( $keypair ) |
|
| 595 | 595 | { |
| 596 | - if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES) { |
|
| 597 | - throw new RangeException('Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES bytes long.'); |
|
| 596 | + if ( ParagonIE_Sodium_Core_Util::strlen( $keypair ) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES ) { |
|
| 597 | + throw new RangeException( 'Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES bytes long.' ); |
|
| 598 | 598 | } |
| 599 | - return ParagonIE_Sodium_Core_Util::substr($keypair, 32, 32); |
|
| 599 | + return ParagonIE_Sodium_Core_Util::substr( $keypair, 32, 32 ); |
|
| 600 | 600 | } |
| 601 | 601 | |
| 602 | 602 | /** |
@@ -606,12 +606,12 @@ discard block |
||
| 606 | 606 | * @return string |
| 607 | 607 | * @throws RangeException |
| 608 | 608 | */ |
| 609 | - public static function box_publickey_from_secretkey($sKey) |
|
| 609 | + public static function box_publickey_from_secretkey( $sKey ) |
|
| 610 | 610 | { |
| 611 | - if (ParagonIE_Sodium_Core_Util::strlen($sKey) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_SECRETKEYBYTES) { |
|
| 612 | - throw new RangeException('Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_SECRETKEYBYTES bytes long.'); |
|
| 611 | + if ( ParagonIE_Sodium_Core_Util::strlen( $sKey ) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_SECRETKEYBYTES ) { |
|
| 612 | + throw new RangeException( 'Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_SECRETKEYBYTES bytes long.' ); |
|
| 613 | 613 | } |
| 614 | - return self::scalarmult_base($sKey); |
|
| 614 | + return self::scalarmult_base( $sKey ); |
|
| 615 | 615 | } |
| 616 | 616 | |
| 617 | 617 | /** |
@@ -625,14 +625,14 @@ discard block |
||
| 625 | 625 | * @param string $keypair |
| 626 | 626 | * @return string |
| 627 | 627 | */ |
| 628 | - public static function box_open($ciphertext, $nonce, $keypair) |
|
| 628 | + public static function box_open( $ciphertext, $nonce, $keypair ) |
|
| 629 | 629 | { |
| 630 | 630 | return self::secretbox_open( |
| 631 | 631 | $ciphertext, |
| 632 | 632 | $nonce, |
| 633 | 633 | self::box_beforenm( |
| 634 | - self::box_secretkey($keypair), |
|
| 635 | - self::box_publickey($keypair) |
|
| 634 | + self::box_secretkey( $keypair ), |
|
| 635 | + self::box_publickey( $keypair ) |
|
| 636 | 636 | ) |
| 637 | 637 | ); |
| 638 | 638 | } |
@@ -648,34 +648,34 @@ discard block |
||
| 648 | 648 | * @return string |
| 649 | 649 | * @throws RangeException |
| 650 | 650 | */ |
| 651 | - public static function generichash($message, $key = '', $outlen = 32) |
|
| 651 | + public static function generichash( $message, $key = '', $outlen = 32 ) |
|
| 652 | 652 | { |
| 653 | 653 | // This ensures that ParagonIE_Sodium_Core_BLAKE2b::$iv is initialized |
| 654 | 654 | ParagonIE_Sodium_Core_BLAKE2b::pseudoConstructor(); |
| 655 | 655 | |
| 656 | 656 | $k = null; |
| 657 | - if (!empty($key)) { |
|
| 657 | + if ( ! empty( $key ) ) { |
|
| 658 | 658 | /** @var SplFixedArray $k */ |
| 659 | - $k = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($key); |
|
| 660 | - if ($k->count() > ParagonIE_Sodium_Core_BLAKE2b::KEYBYTES) { |
|
| 661 | - throw new RangeException('Invalid key size'); |
|
| 659 | + $k = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray( $key ); |
|
| 660 | + if ( $k->count() > ParagonIE_Sodium_Core_BLAKE2b::KEYBYTES ) { |
|
| 661 | + throw new RangeException( 'Invalid key size' ); |
|
| 662 | 662 | } |
| 663 | 663 | } |
| 664 | 664 | |
| 665 | 665 | /** @var SplFixedArray $in */ |
| 666 | - $in = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($message); |
|
| 666 | + $in = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray( $message ); |
|
| 667 | 667 | |
| 668 | 668 | /** @var SplFixedArray $ctx */ |
| 669 | - $ctx = ParagonIE_Sodium_Core_BLAKE2b::init($k, $outlen); |
|
| 670 | - ParagonIE_Sodium_Core_BLAKE2b::update($ctx, $in, $in->count()); |
|
| 669 | + $ctx = ParagonIE_Sodium_Core_BLAKE2b::init( $k, $outlen ); |
|
| 670 | + ParagonIE_Sodium_Core_BLAKE2b::update( $ctx, $in, $in->count() ); |
|
| 671 | 671 | |
| 672 | 672 | /** @var SplFixedArray $out */ |
| 673 | - $out = new SplFixedArray($outlen); |
|
| 674 | - $out = ParagonIE_Sodium_Core_BLAKE2b::finish($ctx, $out); |
|
| 673 | + $out = new SplFixedArray( $outlen ); |
|
| 674 | + $out = ParagonIE_Sodium_Core_BLAKE2b::finish( $ctx, $out ); |
|
| 675 | 675 | |
| 676 | 676 | /** @var array<int, int> */ |
| 677 | 677 | $outArray = $out->toArray(); |
| 678 | - return ParagonIE_Sodium_Core_Util::intArrayToString($outArray); |
|
| 678 | + return ParagonIE_Sodium_Core_Util::intArrayToString( $outArray ); |
|
| 679 | 679 | } |
| 680 | 680 | |
| 681 | 681 | /** |
@@ -688,22 +688,22 @@ discard block |
||
| 688 | 688 | * @return string |
| 689 | 689 | * @throws TypeError |
| 690 | 690 | */ |
| 691 | - public static function generichash_final($ctx, $outlen = 32) |
|
| 691 | + public static function generichash_final( $ctx, $outlen = 32 ) |
|
| 692 | 692 | { |
| 693 | - if (!is_string($ctx)) { |
|
| 694 | - throw new TypeError('Context must be a string'); |
|
| 693 | + if ( ! is_string( $ctx ) ) { |
|
| 694 | + throw new TypeError( 'Context must be a string' ); |
|
| 695 | 695 | } |
| 696 | - $out = new SplFixedArray($outlen); |
|
| 696 | + $out = new SplFixedArray( $outlen ); |
|
| 697 | 697 | |
| 698 | 698 | /** @var SplFixedArray $context */ |
| 699 | - $context = ParagonIE_Sodium_Core_BLAKE2b::stringToContext($ctx); |
|
| 699 | + $context = ParagonIE_Sodium_Core_BLAKE2b::stringToContext( $ctx ); |
|
| 700 | 700 | |
| 701 | 701 | /** @var SplFixedArray $out */ |
| 702 | - $out = ParagonIE_Sodium_Core_BLAKE2b::finish($context, $out); |
|
| 702 | + $out = ParagonIE_Sodium_Core_BLAKE2b::finish( $context, $out ); |
|
| 703 | 703 | |
| 704 | 704 | /** @var array<int, int> */ |
| 705 | 705 | $outArray = $out->toArray(); |
| 706 | - return ParagonIE_Sodium_Core_Util::intArrayToString($outArray); |
|
| 706 | + return ParagonIE_Sodium_Core_Util::intArrayToString( $outArray ); |
|
| 707 | 707 | } |
| 708 | 708 | |
| 709 | 709 | /** |
@@ -716,23 +716,23 @@ discard block |
||
| 716 | 716 | * @return string |
| 717 | 717 | * @throws RangeException |
| 718 | 718 | */ |
| 719 | - public static function generichash_init($key = '', $outputLength = 32) |
|
| 719 | + public static function generichash_init( $key = '', $outputLength = 32 ) |
|
| 720 | 720 | { |
| 721 | 721 | // This ensures that ParagonIE_Sodium_Core_BLAKE2b::$iv is initialized |
| 722 | 722 | ParagonIE_Sodium_Core_BLAKE2b::pseudoConstructor(); |
| 723 | 723 | |
| 724 | 724 | $k = null; |
| 725 | - if (!empty($key)) { |
|
| 726 | - $k = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($key); |
|
| 727 | - if ($k->count() > ParagonIE_Sodium_Core_BLAKE2b::KEYBYTES) { |
|
| 728 | - throw new RangeException('Invalid key size'); |
|
| 725 | + if ( ! empty( $key ) ) { |
|
| 726 | + $k = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray( $key ); |
|
| 727 | + if ( $k->count() > ParagonIE_Sodium_Core_BLAKE2b::KEYBYTES ) { |
|
| 728 | + throw new RangeException( 'Invalid key size' ); |
|
| 729 | 729 | } |
| 730 | 730 | } |
| 731 | 731 | |
| 732 | 732 | /** @var SplFixedArray $ctx */ |
| 733 | - $ctx = ParagonIE_Sodium_Core_BLAKE2b::init($k, $outputLength); |
|
| 733 | + $ctx = ParagonIE_Sodium_Core_BLAKE2b::init( $k, $outputLength ); |
|
| 734 | 734 | |
| 735 | - return ParagonIE_Sodium_Core_BLAKE2b::contextToString($ctx); |
|
| 735 | + return ParagonIE_Sodium_Core_BLAKE2b::contextToString( $ctx ); |
|
| 736 | 736 | } |
| 737 | 737 | |
| 738 | 738 | /** |
@@ -744,20 +744,20 @@ discard block |
||
| 744 | 744 | * @param string $message |
| 745 | 745 | * @return string |
| 746 | 746 | */ |
| 747 | - public static function generichash_update($ctx, $message) |
|
| 747 | + public static function generichash_update( $ctx, $message ) |
|
| 748 | 748 | { |
| 749 | 749 | // This ensures that ParagonIE_Sodium_Core_BLAKE2b::$iv is initialized |
| 750 | 750 | ParagonIE_Sodium_Core_BLAKE2b::pseudoConstructor(); |
| 751 | 751 | |
| 752 | 752 | /** @var SplFixedArray $context */ |
| 753 | - $context = ParagonIE_Sodium_Core_BLAKE2b::stringToContext($ctx); |
|
| 753 | + $context = ParagonIE_Sodium_Core_BLAKE2b::stringToContext( $ctx ); |
|
| 754 | 754 | |
| 755 | 755 | /** @var SplFixedArray $in */ |
| 756 | - $in = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($message); |
|
| 756 | + $in = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray( $message ); |
|
| 757 | 757 | |
| 758 | - ParagonIE_Sodium_Core_BLAKE2b::update($context, $in, $in->count()); |
|
| 758 | + ParagonIE_Sodium_Core_BLAKE2b::update( $context, $in, $in->count() ); |
|
| 759 | 759 | |
| 760 | - return ParagonIE_Sodium_Core_BLAKE2b::contextToString($context); |
|
| 760 | + return ParagonIE_Sodium_Core_BLAKE2b::contextToString( $context ); |
|
| 761 | 761 | } |
| 762 | 762 | |
| 763 | 763 | /** |
@@ -771,10 +771,10 @@ discard block |
||
| 771 | 771 | * @param string $server_pk |
| 772 | 772 | * @return string |
| 773 | 773 | */ |
| 774 | - public static function keyExchange($my_sk, $their_pk, $client_pk, $server_pk) |
|
| 774 | + public static function keyExchange( $my_sk, $their_pk, $client_pk, $server_pk ) |
|
| 775 | 775 | { |
| 776 | 776 | return self::generichash( |
| 777 | - self::scalarmult($my_sk, $their_pk) . |
|
| 777 | + self::scalarmult( $my_sk, $their_pk ) . |
|
| 778 | 778 | $client_pk . |
| 779 | 779 | $server_pk |
| 780 | 780 | ); |
@@ -791,10 +791,10 @@ discard block |
||
| 791 | 791 | * |
| 792 | 792 | * @throws Error |
| 793 | 793 | */ |
| 794 | - public static function scalarmult($sKey, $pKey) |
|
| 794 | + public static function scalarmult( $sKey, $pKey ) |
|
| 795 | 795 | { |
| 796 | - $q = ParagonIE_Sodium_Core_X25519::crypto_scalarmult_curve25519_ref10($sKey, $pKey); |
|
| 797 | - self::scalarmult_throw_if_zero($q); |
|
| 796 | + $q = ParagonIE_Sodium_Core_X25519::crypto_scalarmult_curve25519_ref10( $sKey, $pKey ); |
|
| 797 | + self::scalarmult_throw_if_zero( $q ); |
|
| 798 | 798 | return $q; |
| 799 | 799 | } |
| 800 | 800 | |
@@ -807,10 +807,10 @@ discard block |
||
| 807 | 807 | * |
| 808 | 808 | * @throws Error |
| 809 | 809 | */ |
| 810 | - public static function scalarmult_base($secret) |
|
| 810 | + public static function scalarmult_base( $secret ) |
|
| 811 | 811 | { |
| 812 | - $q = ParagonIE_Sodium_Core_X25519::crypto_scalarmult_curve25519_ref10_base($secret); |
|
| 813 | - self::scalarmult_throw_if_zero($q); |
|
| 812 | + $q = ParagonIE_Sodium_Core_X25519::crypto_scalarmult_curve25519_ref10_base( $secret ); |
|
| 813 | + self::scalarmult_throw_if_zero( $q ); |
|
| 814 | 814 | return $q; |
| 815 | 815 | } |
| 816 | 816 | |
@@ -821,16 +821,16 @@ discard block |
||
| 821 | 821 | * @return void |
| 822 | 822 | * @throws Error |
| 823 | 823 | */ |
| 824 | - protected static function scalarmult_throw_if_zero($q) |
|
| 824 | + protected static function scalarmult_throw_if_zero( $q ) |
|
| 825 | 825 | { |
| 826 | 826 | $d = 0; |
| 827 | - for ($i = 0; $i < self::box_curve25519xsalsa20poly1305_SECRETKEYBYTES; ++$i) { |
|
| 828 | - $d |= ParagonIE_Sodium_Core_Util::chrToInt($q[$i]); |
|
| 827 | + for ( $i = 0; $i < self::box_curve25519xsalsa20poly1305_SECRETKEYBYTES; ++$i ) { |
|
| 828 | + $d |= ParagonIE_Sodium_Core_Util::chrToInt( $q[$i] ); |
|
| 829 | 829 | } |
| 830 | 830 | |
| 831 | 831 | /* branch-free variant of === 0 */ |
| 832 | - if (-(1 & (($d - 1) >> 8))) { |
|
| 833 | - throw new Error('Zero public key is not allowed'); |
|
| 832 | + if (-( 1 & ( ( $d - 1 ) >> 8 ) )) { |
|
| 833 | + throw new Error( 'Zero public key is not allowed' ); |
|
| 834 | 834 | } |
| 835 | 835 | } |
| 836 | 836 | |
@@ -844,26 +844,26 @@ discard block |
||
| 844 | 844 | * @param string $key |
| 845 | 845 | * @return string |
| 846 | 846 | */ |
| 847 | - public static function secretbox($plaintext, $nonce, $key) |
|
| 847 | + public static function secretbox( $plaintext, $nonce, $key ) |
|
| 848 | 848 | { |
| 849 | 849 | /** @var string $subkey */ |
| 850 | - $subkey = ParagonIE_Sodium_Core_HSalsa20::hsalsa20($nonce, $key); |
|
| 850 | + $subkey = ParagonIE_Sodium_Core_HSalsa20::hsalsa20( $nonce, $key ); |
|
| 851 | 851 | |
| 852 | 852 | /** @var string $block0 */ |
| 853 | - $block0 = str_repeat("\x00", 32); |
|
| 853 | + $block0 = str_repeat( "\x00", 32 ); |
|
| 854 | 854 | |
| 855 | 855 | /** @var int $mlen - Length of the plaintext message */ |
| 856 | - $mlen = ParagonIE_Sodium_Core_Util::strlen($plaintext); |
|
| 856 | + $mlen = ParagonIE_Sodium_Core_Util::strlen( $plaintext ); |
|
| 857 | 857 | $mlen0 = $mlen; |
| 858 | - if ($mlen0 > 64 - self::secretbox_xsalsa20poly1305_ZEROBYTES) { |
|
| 858 | + if ( $mlen0 > 64 - self::secretbox_xsalsa20poly1305_ZEROBYTES ) { |
|
| 859 | 859 | $mlen0 = 64 - self::secretbox_xsalsa20poly1305_ZEROBYTES; |
| 860 | 860 | } |
| 861 | - $block0 .= ParagonIE_Sodium_Core_Util::substr($plaintext, 0, $mlen0); |
|
| 861 | + $block0 .= ParagonIE_Sodium_Core_Util::substr( $plaintext, 0, $mlen0 ); |
|
| 862 | 862 | |
| 863 | 863 | /** @var string $block0 */ |
| 864 | 864 | $block0 = ParagonIE_Sodium_Core_Salsa20::salsa20_xor( |
| 865 | 865 | $block0, |
| 866 | - ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8), |
|
| 866 | + ParagonIE_Sodium_Core_Util::substr( $nonce, 16, 8 ), |
|
| 867 | 867 | $subkey |
| 868 | 868 | ); |
| 869 | 869 | |
@@ -872,13 +872,13 @@ discard block |
||
| 872 | 872 | $block0, |
| 873 | 873 | self::secretbox_xsalsa20poly1305_ZEROBYTES |
| 874 | 874 | ); |
| 875 | - if ($mlen > $mlen0) { |
|
| 875 | + if ( $mlen > $mlen0 ) { |
|
| 876 | 876 | $c .= ParagonIE_Sodium_Core_Salsa20::salsa20_xor_ic( |
| 877 | 877 | ParagonIE_Sodium_Core_Util::substr( |
| 878 | 878 | $plaintext, |
| 879 | 879 | self::secretbox_xsalsa20poly1305_ZEROBYTES |
| 880 | 880 | ), |
| 881 | - ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8), |
|
| 881 | + ParagonIE_Sodium_Core_Util::substr( $nonce, 16, 8 ), |
|
| 882 | 882 | 1, |
| 883 | 883 | $subkey |
| 884 | 884 | ); |
@@ -891,18 +891,18 @@ discard block |
||
| 891 | 891 | ) |
| 892 | 892 | ); |
| 893 | 893 | try { |
| 894 | - ParagonIE_Sodium_Compat::memzero($block0); |
|
| 895 | - ParagonIE_Sodium_Compat::memzero($subkey); |
|
| 896 | - } catch (Error $ex) { |
|
| 894 | + ParagonIE_Sodium_Compat::memzero( $block0 ); |
|
| 895 | + ParagonIE_Sodium_Compat::memzero( $subkey ); |
|
| 896 | + } catch ( Error $ex ) { |
|
| 897 | 897 | $block0 = null; |
| 898 | 898 | $subkey = null; |
| 899 | 899 | } |
| 900 | 900 | |
| 901 | - $state->update($c); |
|
| 901 | + $state->update( $c ); |
|
| 902 | 902 | |
| 903 | 903 | /** @var string $c - MAC || ciphertext */ |
| 904 | 904 | $c = $state->finish() . $c; |
| 905 | - unset($state); |
|
| 905 | + unset( $state ); |
|
| 906 | 906 | |
| 907 | 907 | return $c; |
| 908 | 908 | } |
@@ -918,7 +918,7 @@ discard block |
||
| 918 | 918 | * @return string |
| 919 | 919 | * @throws Error |
| 920 | 920 | */ |
| 921 | - public static function secretbox_open($ciphertext, $nonce, $key) |
|
| 921 | + public static function secretbox_open( $ciphertext, $nonce, $key ) |
|
| 922 | 922 | { |
| 923 | 923 | /** @var string $mac */ |
| 924 | 924 | $mac = ParagonIE_Sodium_Core_Util::substr( |
@@ -934,44 +934,44 @@ discard block |
||
| 934 | 934 | ); |
| 935 | 935 | |
| 936 | 936 | /** @var int $clen */ |
| 937 | - $clen = ParagonIE_Sodium_Core_Util::strlen($c); |
|
| 937 | + $clen = ParagonIE_Sodium_Core_Util::strlen( $c ); |
|
| 938 | 938 | |
| 939 | 939 | /** @var string $subkey */ |
| 940 | - $subkey = ParagonIE_Sodium_Core_HSalsa20::hsalsa20($nonce, $key); |
|
| 940 | + $subkey = ParagonIE_Sodium_Core_HSalsa20::hsalsa20( $nonce, $key ); |
|
| 941 | 941 | |
| 942 | 942 | /** @var string $block0 */ |
| 943 | 943 | $block0 = ParagonIE_Sodium_Core_Salsa20::salsa20( |
| 944 | 944 | 64, |
| 945 | - ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8), |
|
| 945 | + ParagonIE_Sodium_Core_Util::substr( $nonce, 16, 8 ), |
|
| 946 | 946 | $subkey |
| 947 | 947 | ); |
| 948 | 948 | $verified = ParagonIE_Sodium_Core_Poly1305::onetimeauth_verify( |
| 949 | 949 | $mac, |
| 950 | 950 | $c, |
| 951 | - ParagonIE_Sodium_Core_Util::substr($block0, 0, 32) |
|
| 951 | + ParagonIE_Sodium_Core_Util::substr( $block0, 0, 32 ) |
|
| 952 | 952 | ); |
| 953 | - if (!$verified) { |
|
| 953 | + if ( ! $verified ) { |
|
| 954 | 954 | try { |
| 955 | - ParagonIE_Sodium_Compat::memzero($subkey); |
|
| 956 | - } catch (Error $ex) { |
|
| 955 | + ParagonIE_Sodium_Compat::memzero( $subkey ); |
|
| 956 | + } catch ( Error $ex ) { |
|
| 957 | 957 | $subkey = null; |
| 958 | 958 | } |
| 959 | - throw new Error('Invalid MAC'); |
|
| 959 | + throw new Error( 'Invalid MAC' ); |
|
| 960 | 960 | } |
| 961 | 961 | |
| 962 | 962 | /** @var string $m - Decrypted message */ |
| 963 | 963 | $m = ParagonIE_Sodium_Core_Util::xorStrings( |
| 964 | - ParagonIE_Sodium_Core_Util::substr($block0, self::secretbox_xsalsa20poly1305_ZEROBYTES), |
|
| 965 | - ParagonIE_Sodium_Core_Util::substr($c, 0, self::secretbox_xsalsa20poly1305_ZEROBYTES) |
|
| 964 | + ParagonIE_Sodium_Core_Util::substr( $block0, self::secretbox_xsalsa20poly1305_ZEROBYTES ), |
|
| 965 | + ParagonIE_Sodium_Core_Util::substr( $c, 0, self::secretbox_xsalsa20poly1305_ZEROBYTES ) |
|
| 966 | 966 | ); |
| 967 | - if ($clen > self::secretbox_xsalsa20poly1305_ZEROBYTES) { |
|
| 967 | + if ( $clen > self::secretbox_xsalsa20poly1305_ZEROBYTES ) { |
|
| 968 | 968 | // We had more than 1 block, so let's continue to decrypt the rest. |
| 969 | 969 | $m .= ParagonIE_Sodium_Core_Salsa20::salsa20_xor_ic( |
| 970 | 970 | ParagonIE_Sodium_Core_Util::substr( |
| 971 | 971 | $c, |
| 972 | 972 | self::secretbox_xsalsa20poly1305_ZEROBYTES |
| 973 | 973 | ), |
| 974 | - ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8), |
|
| 974 | + ParagonIE_Sodium_Core_Util::substr( $nonce, 16, 8 ), |
|
| 975 | 975 | 1, |
| 976 | 976 | $subkey |
| 977 | 977 | ); |
@@ -989,25 +989,25 @@ discard block |
||
| 989 | 989 | * @param string $key |
| 990 | 990 | * @return string |
| 991 | 991 | */ |
| 992 | - public static function secretbox_xchacha20poly1305($plaintext, $nonce, $key) |
|
| 992 | + public static function secretbox_xchacha20poly1305( $plaintext, $nonce, $key ) |
|
| 993 | 993 | { |
| 994 | 994 | /** @var string $subkey */ |
| 995 | 995 | $subkey = ParagonIE_Sodium_Core_HChaCha20::hChaCha20( |
| 996 | - ParagonIE_Sodium_Core_Util::substr($nonce, 0, 16), |
|
| 996 | + ParagonIE_Sodium_Core_Util::substr( $nonce, 0, 16 ), |
|
| 997 | 997 | $key |
| 998 | 998 | ); |
| 999 | - $nonceLast = ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8); |
|
| 999 | + $nonceLast = ParagonIE_Sodium_Core_Util::substr( $nonce, 16, 8 ); |
|
| 1000 | 1000 | |
| 1001 | 1001 | /** @var string $block0 */ |
| 1002 | - $block0 = str_repeat("\x00", 32); |
|
| 1002 | + $block0 = str_repeat( "\x00", 32 ); |
|
| 1003 | 1003 | |
| 1004 | 1004 | /** @var int $mlen - Length of the plaintext message */ |
| 1005 | - $mlen = ParagonIE_Sodium_Core_Util::strlen($plaintext); |
|
| 1005 | + $mlen = ParagonIE_Sodium_Core_Util::strlen( $plaintext ); |
|
| 1006 | 1006 | $mlen0 = $mlen; |
| 1007 | - if ($mlen0 > 64 - self::secretbox_xchacha20poly1305_ZEROBYTES) { |
|
| 1007 | + if ( $mlen0 > 64 - self::secretbox_xchacha20poly1305_ZEROBYTES ) { |
|
| 1008 | 1008 | $mlen0 = 64 - self::secretbox_xchacha20poly1305_ZEROBYTES; |
| 1009 | 1009 | } |
| 1010 | - $block0 .= ParagonIE_Sodium_Core_Util::substr($plaintext, 0, $mlen0); |
|
| 1010 | + $block0 .= ParagonIE_Sodium_Core_Util::substr( $plaintext, 0, $mlen0 ); |
|
| 1011 | 1011 | |
| 1012 | 1012 | /** @var string $block0 */ |
| 1013 | 1013 | $block0 = ParagonIE_Sodium_Core_ChaCha20::streamXorIc( |
@@ -1021,7 +1021,7 @@ discard block |
||
| 1021 | 1021 | $block0, |
| 1022 | 1022 | self::secretbox_xchacha20poly1305_ZEROBYTES |
| 1023 | 1023 | ); |
| 1024 | - if ($mlen > $mlen0) { |
|
| 1024 | + if ( $mlen > $mlen0 ) { |
|
| 1025 | 1025 | $c .= ParagonIE_Sodium_Core_ChaCha20::streamXorIc( |
| 1026 | 1026 | ParagonIE_Sodium_Core_Util::substr( |
| 1027 | 1027 | $plaintext, |
@@ -1029,7 +1029,7 @@ discard block |
||
| 1029 | 1029 | ), |
| 1030 | 1030 | $nonceLast, |
| 1031 | 1031 | $subkey, |
| 1032 | - ParagonIE_Sodium_Core_Util::store64_le(1) |
|
| 1032 | + ParagonIE_Sodium_Core_Util::store64_le( 1 ) |
|
| 1033 | 1033 | ); |
| 1034 | 1034 | } |
| 1035 | 1035 | $state = new ParagonIE_Sodium_Core_Poly1305_State( |
@@ -1040,18 +1040,18 @@ discard block |
||
| 1040 | 1040 | ) |
| 1041 | 1041 | ); |
| 1042 | 1042 | try { |
| 1043 | - ParagonIE_Sodium_Compat::memzero($block0); |
|
| 1044 | - ParagonIE_Sodium_Compat::memzero($subkey); |
|
| 1045 | - } catch (Error $ex) { |
|
| 1043 | + ParagonIE_Sodium_Compat::memzero( $block0 ); |
|
| 1044 | + ParagonIE_Sodium_Compat::memzero( $subkey ); |
|
| 1045 | + } catch ( Error $ex ) { |
|
| 1046 | 1046 | $block0 = null; |
| 1047 | 1047 | $subkey = null; |
| 1048 | 1048 | } |
| 1049 | 1049 | |
| 1050 | - $state->update($c); |
|
| 1050 | + $state->update( $c ); |
|
| 1051 | 1051 | |
| 1052 | 1052 | /** @var string $c - MAC || ciphertext */ |
| 1053 | 1053 | $c = $state->finish() . $c; |
| 1054 | - unset($state); |
|
| 1054 | + unset( $state ); |
|
| 1055 | 1055 | |
| 1056 | 1056 | return $c; |
| 1057 | 1057 | } |
@@ -1067,7 +1067,7 @@ discard block |
||
| 1067 | 1067 | * @return string |
| 1068 | 1068 | * @throws Error |
| 1069 | 1069 | */ |
| 1070 | - public static function secretbox_xchacha20poly1305_open($ciphertext, $nonce, $key) |
|
| 1070 | + public static function secretbox_xchacha20poly1305_open( $ciphertext, $nonce, $key ) |
|
| 1071 | 1071 | { |
| 1072 | 1072 | /** @var string $mac */ |
| 1073 | 1073 | $mac = ParagonIE_Sodium_Core_Util::substr( |
@@ -1083,48 +1083,48 @@ discard block |
||
| 1083 | 1083 | ); |
| 1084 | 1084 | |
| 1085 | 1085 | /** @var int $clen */ |
| 1086 | - $clen = ParagonIE_Sodium_Core_Util::strlen($c); |
|
| 1086 | + $clen = ParagonIE_Sodium_Core_Util::strlen( $c ); |
|
| 1087 | 1087 | |
| 1088 | 1088 | /** @var string $subkey */ |
| 1089 | - $subkey = ParagonIE_Sodium_Core_HChaCha20::hchacha20($nonce, $key); |
|
| 1089 | + $subkey = ParagonIE_Sodium_Core_HChaCha20::hchacha20( $nonce, $key ); |
|
| 1090 | 1090 | |
| 1091 | 1091 | /** @var string $block0 */ |
| 1092 | 1092 | $block0 = ParagonIE_Sodium_Core_ChaCha20::stream( |
| 1093 | 1093 | 64, |
| 1094 | - ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8), |
|
| 1094 | + ParagonIE_Sodium_Core_Util::substr( $nonce, 16, 8 ), |
|
| 1095 | 1095 | $subkey |
| 1096 | 1096 | ); |
| 1097 | 1097 | $verified = ParagonIE_Sodium_Core_Poly1305::onetimeauth_verify( |
| 1098 | 1098 | $mac, |
| 1099 | 1099 | $c, |
| 1100 | - ParagonIE_Sodium_Core_Util::substr($block0, 0, 32) |
|
| 1100 | + ParagonIE_Sodium_Core_Util::substr( $block0, 0, 32 ) |
|
| 1101 | 1101 | ); |
| 1102 | 1102 | |
| 1103 | - if (!$verified) { |
|
| 1103 | + if ( ! $verified ) { |
|
| 1104 | 1104 | try { |
| 1105 | - ParagonIE_Sodium_Compat::memzero($subkey); |
|
| 1106 | - } catch (Error $ex) { |
|
| 1105 | + ParagonIE_Sodium_Compat::memzero( $subkey ); |
|
| 1106 | + } catch ( Error $ex ) { |
|
| 1107 | 1107 | $subkey = null; |
| 1108 | 1108 | } |
| 1109 | - throw new Error('Invalid MAC'); |
|
| 1109 | + throw new Error( 'Invalid MAC' ); |
|
| 1110 | 1110 | } |
| 1111 | 1111 | |
| 1112 | 1112 | /** @var string $m - Decrypted message */ |
| 1113 | 1113 | $m = ParagonIE_Sodium_Core_Util::xorStrings( |
| 1114 | - ParagonIE_Sodium_Core_Util::substr($block0, self::secretbox_xchacha20poly1305_ZEROBYTES), |
|
| 1115 | - ParagonIE_Sodium_Core_Util::substr($c, 0, self::secretbox_xchacha20poly1305_ZEROBYTES) |
|
| 1114 | + ParagonIE_Sodium_Core_Util::substr( $block0, self::secretbox_xchacha20poly1305_ZEROBYTES ), |
|
| 1115 | + ParagonIE_Sodium_Core_Util::substr( $c, 0, self::secretbox_xchacha20poly1305_ZEROBYTES ) |
|
| 1116 | 1116 | ); |
| 1117 | 1117 | |
| 1118 | - if ($clen > self::secretbox_xchacha20poly1305_ZEROBYTES) { |
|
| 1118 | + if ( $clen > self::secretbox_xchacha20poly1305_ZEROBYTES ) { |
|
| 1119 | 1119 | // We had more than 1 block, so let's continue to decrypt the rest. |
| 1120 | 1120 | $m .= ParagonIE_Sodium_Core_ChaCha20::streamXorIc( |
| 1121 | 1121 | ParagonIE_Sodium_Core_Util::substr( |
| 1122 | 1122 | $c, |
| 1123 | 1123 | self::secretbox_xchacha20poly1305_ZEROBYTES |
| 1124 | 1124 | ), |
| 1125 | - ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8), |
|
| 1125 | + ParagonIE_Sodium_Core_Util::substr( $nonce, 16, 8 ), |
|
| 1126 | 1126 | $subkey, |
| 1127 | - ParagonIE_Sodium_Core_Util::store64_le(1) |
|
| 1127 | + ParagonIE_Sodium_Core_Util::store64_le( 1 ) |
|
| 1128 | 1128 | ); |
| 1129 | 1129 | } |
| 1130 | 1130 | return $m; |
@@ -1139,9 +1139,9 @@ discard block |
||
| 1139 | 1139 | * @param string $sk |
| 1140 | 1140 | * @return string |
| 1141 | 1141 | */ |
| 1142 | - public static function sign_detached($message, $sk) |
|
| 1142 | + public static function sign_detached( $message, $sk ) |
|
| 1143 | 1143 | { |
| 1144 | - return ParagonIE_Sodium_Core_Ed25519::sign_detached($message, $sk); |
|
| 1144 | + return ParagonIE_Sodium_Core_Ed25519::sign_detached( $message, $sk ); |
|
| 1145 | 1145 | } |
| 1146 | 1146 | |
| 1147 | 1147 | /** |
@@ -1153,9 +1153,9 @@ discard block |
||
| 1153 | 1153 | * @param string $sk |
| 1154 | 1154 | * @return string |
| 1155 | 1155 | */ |
| 1156 | - public static function sign($message, $sk) |
|
| 1156 | + public static function sign( $message, $sk ) |
|
| 1157 | 1157 | { |
| 1158 | - return ParagonIE_Sodium_Core_Ed25519::sign($message, $sk); |
|
| 1158 | + return ParagonIE_Sodium_Core_Ed25519::sign( $message, $sk ); |
|
| 1159 | 1159 | } |
| 1160 | 1160 | |
| 1161 | 1161 | /** |
@@ -1167,9 +1167,9 @@ discard block |
||
| 1167 | 1167 | * @param string $pk |
| 1168 | 1168 | * @return string |
| 1169 | 1169 | */ |
| 1170 | - public static function sign_open($signedMessage, $pk) |
|
| 1170 | + public static function sign_open( $signedMessage, $pk ) |
|
| 1171 | 1171 | { |
| 1172 | - return ParagonIE_Sodium_Core_Ed25519::sign_open($signedMessage, $pk); |
|
| 1172 | + return ParagonIE_Sodium_Core_Ed25519::sign_open( $signedMessage, $pk ); |
|
| 1173 | 1173 | } |
| 1174 | 1174 | |
| 1175 | 1175 | /** |
@@ -1182,8 +1182,8 @@ discard block |
||
| 1182 | 1182 | * @param string $pk |
| 1183 | 1183 | * @return bool |
| 1184 | 1184 | */ |
| 1185 | - public static function sign_verify_detached($signature, $message, $pk) |
|
| 1185 | + public static function sign_verify_detached( $signature, $message, $pk ) |
|
| 1186 | 1186 | { |
| 1187 | - return ParagonIE_Sodium_Core_Ed25519::verify_detached($signature, $message, $pk); |
|
| 1187 | + return ParagonIE_Sodium_Core_Ed25519::verify_detached( $signature, $message, $pk ); |
|
| 1188 | 1188 | } |
| 1189 | 1189 | } |
@@ -12,8 +12,7 @@ discard block |
||
| 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_Crypto |
|
| 16 | -{ |
|
| 15 | +abstract class ParagonIE_Sodium_Crypto { |
|
| 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 |
||
| 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_Core_Util::substr( |
| 377 | 375 | hash_hmac('sha512', $message, $key, true), |
| 378 | 376 | 0, |
@@ -390,8 +388,7 @@ discard block |
||
| 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_Core_Util::hashEquals( |
| 396 | 393 | $mac, |
| 397 | 394 | self::auth($message, $key) |
@@ -408,8 +405,7 @@ discard block |
||
| 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 |
||
| 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 |
||
| 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_Core_Util::substr($message, 0, 32); |
| 481 | 475 | |
@@ -521,8 +515,7 @@ discard block |
||
| 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_Core_HSalsa20::hsalsa20( |
| 527 | 520 | str_repeat("\x00", 16), |
| 528 | 521 | self::scalarmult($sk, $pk) |
@@ -534,8 +527,7 @@ discard block |
||
| 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; |
@@ -545,8 +537,7 @@ discard block |
||
| 545 | 537 | * @param string $seed |
| 546 | 538 | * @return string |
| 547 | 539 | */ |
| 548 | - public static function box_seed_keypair($seed) |
|
| 549 | - { |
|
| 540 | + public static function box_seed_keypair($seed) { |
|
| 550 | 541 | $sKey = ParagonIE_Sodium_Core_Util::substr( |
| 551 | 542 | hash('sha512', $seed, true), |
| 552 | 543 | 0, |
@@ -563,8 +554,7 @@ discard block |
||
| 563 | 554 | * @param string $pKey |
| 564 | 555 | * @return string |
| 565 | 556 | */ |
| 566 | - public static function box_keypair_from_secretkey_and_publickey($sKey, $pKey) |
|
| 567 | - { |
|
| 557 | + public static function box_keypair_from_secretkey_and_publickey($sKey, $pKey) { |
|
| 568 | 558 | return ParagonIE_Sodium_Core_Util::substr($sKey, 0, 32) . |
| 569 | 559 | ParagonIE_Sodium_Core_Util::substr($pKey, 0, 32); |
| 570 | 560 | } |
@@ -576,8 +566,7 @@ discard block |
||
| 576 | 566 | * @return string |
| 577 | 567 | * @throws RangeException |
| 578 | 568 | */ |
| 579 | - public static function box_secretkey($keypair) |
|
| 580 | - { |
|
| 569 | + public static function box_secretkey($keypair) { |
|
| 581 | 570 | if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== 64) { |
| 582 | 571 | throw new RangeException('Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES bytes long.'); |
| 583 | 572 | } |
@@ -591,8 +580,7 @@ discard block |
||
| 591 | 580 | * @return string |
| 592 | 581 | * @throws RangeException |
| 593 | 582 | */ |
| 594 | - public static function box_publickey($keypair) |
|
| 595 | - { |
|
| 583 | + public static function box_publickey($keypair) { |
|
| 596 | 584 | if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES) { |
| 597 | 585 | throw new RangeException('Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES bytes long.'); |
| 598 | 586 | } |
@@ -606,8 +594,7 @@ discard block |
||
| 606 | 594 | * @return string |
| 607 | 595 | * @throws RangeException |
| 608 | 596 | */ |
| 609 | - public static function box_publickey_from_secretkey($sKey) |
|
| 610 | - { |
|
| 597 | + public static function box_publickey_from_secretkey($sKey) { |
|
| 611 | 598 | if (ParagonIE_Sodium_Core_Util::strlen($sKey) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_SECRETKEYBYTES) { |
| 612 | 599 | throw new RangeException('Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_SECRETKEYBYTES bytes long.'); |
| 613 | 600 | } |
@@ -625,8 +612,7 @@ discard block |
||
| 625 | 612 | * @param string $keypair |
| 626 | 613 | * @return string |
| 627 | 614 | */ |
| 628 | - public static function box_open($ciphertext, $nonce, $keypair) |
|
| 629 | - { |
|
| 615 | + public static function box_open($ciphertext, $nonce, $keypair) { |
|
| 630 | 616 | return self::secretbox_open( |
| 631 | 617 | $ciphertext, |
| 632 | 618 | $nonce, |
@@ -648,8 +634,7 @@ discard block |
||
| 648 | 634 | * @return string |
| 649 | 635 | * @throws RangeException |
| 650 | 636 | */ |
| 651 | - public static function generichash($message, $key = '', $outlen = 32) |
|
| 652 | - { |
|
| 637 | + public static function generichash($message, $key = '', $outlen = 32) { |
|
| 653 | 638 | // This ensures that ParagonIE_Sodium_Core_BLAKE2b::$iv is initialized |
| 654 | 639 | ParagonIE_Sodium_Core_BLAKE2b::pseudoConstructor(); |
| 655 | 640 | |
@@ -688,8 +673,7 @@ discard block |
||
| 688 | 673 | * @return string |
| 689 | 674 | * @throws TypeError |
| 690 | 675 | */ |
| 691 | - public static function generichash_final($ctx, $outlen = 32) |
|
| 692 | - { |
|
| 676 | + public static function generichash_final($ctx, $outlen = 32) { |
|
| 693 | 677 | if (!is_string($ctx)) { |
| 694 | 678 | throw new TypeError('Context must be a string'); |
| 695 | 679 | } |
@@ -716,8 +700,7 @@ discard block |
||
| 716 | 700 | * @return string |
| 717 | 701 | * @throws RangeException |
| 718 | 702 | */ |
| 719 | - public static function generichash_init($key = '', $outputLength = 32) |
|
| 720 | - { |
|
| 703 | + public static function generichash_init($key = '', $outputLength = 32) { |
|
| 721 | 704 | // This ensures that ParagonIE_Sodium_Core_BLAKE2b::$iv is initialized |
| 722 | 705 | ParagonIE_Sodium_Core_BLAKE2b::pseudoConstructor(); |
| 723 | 706 | |
@@ -744,8 +727,7 @@ discard block |
||
| 744 | 727 | * @param string $message |
| 745 | 728 | * @return string |
| 746 | 729 | */ |
| 747 | - public static function generichash_update($ctx, $message) |
|
| 748 | - { |
|
| 730 | + public static function generichash_update($ctx, $message) { |
|
| 749 | 731 | // This ensures that ParagonIE_Sodium_Core_BLAKE2b::$iv is initialized |
| 750 | 732 | ParagonIE_Sodium_Core_BLAKE2b::pseudoConstructor(); |
| 751 | 733 | |
@@ -771,8 +753,7 @@ discard block |
||
| 771 | 753 | * @param string $server_pk |
| 772 | 754 | * @return string |
| 773 | 755 | */ |
| 774 | - public static function keyExchange($my_sk, $their_pk, $client_pk, $server_pk) |
|
| 775 | - { |
|
| 756 | + public static function keyExchange($my_sk, $their_pk, $client_pk, $server_pk) { |
|
| 776 | 757 | return self::generichash( |
| 777 | 758 | self::scalarmult($my_sk, $their_pk) . |
| 778 | 759 | $client_pk . |
@@ -791,8 +772,7 @@ discard block |
||
| 791 | 772 | * |
| 792 | 773 | * @throws Error |
| 793 | 774 | */ |
| 794 | - public static function scalarmult($sKey, $pKey) |
|
| 795 | - { |
|
| 775 | + public static function scalarmult($sKey, $pKey) { |
|
| 796 | 776 | $q = ParagonIE_Sodium_Core_X25519::crypto_scalarmult_curve25519_ref10($sKey, $pKey); |
| 797 | 777 | self::scalarmult_throw_if_zero($q); |
| 798 | 778 | return $q; |
@@ -807,8 +787,7 @@ discard block |
||
| 807 | 787 | * |
| 808 | 788 | * @throws Error |
| 809 | 789 | */ |
| 810 | - public static function scalarmult_base($secret) |
|
| 811 | - { |
|
| 790 | + public static function scalarmult_base($secret) { |
|
| 812 | 791 | $q = ParagonIE_Sodium_Core_X25519::crypto_scalarmult_curve25519_ref10_base($secret); |
| 813 | 792 | self::scalarmult_throw_if_zero($q); |
| 814 | 793 | return $q; |
@@ -821,8 +800,7 @@ discard block |
||
| 821 | 800 | * @return void |
| 822 | 801 | * @throws Error |
| 823 | 802 | */ |
| 824 | - protected static function scalarmult_throw_if_zero($q) |
|
| 825 | - { |
|
| 803 | + protected static function scalarmult_throw_if_zero($q) { |
|
| 826 | 804 | $d = 0; |
| 827 | 805 | for ($i = 0; $i < self::box_curve25519xsalsa20poly1305_SECRETKEYBYTES; ++$i) { |
| 828 | 806 | $d |= ParagonIE_Sodium_Core_Util::chrToInt($q[$i]); |
@@ -844,8 +822,7 @@ discard block |
||
| 844 | 822 | * @param string $key |
| 845 | 823 | * @return string |
| 846 | 824 | */ |
| 847 | - public static function secretbox($plaintext, $nonce, $key) |
|
| 848 | - { |
|
| 825 | + public static function secretbox($plaintext, $nonce, $key) { |
|
| 849 | 826 | /** @var string $subkey */ |
| 850 | 827 | $subkey = ParagonIE_Sodium_Core_HSalsa20::hsalsa20($nonce, $key); |
| 851 | 828 | |
@@ -918,8 +895,7 @@ discard block |
||
| 918 | 895 | * @return string |
| 919 | 896 | * @throws Error |
| 920 | 897 | */ |
| 921 | - public static function secretbox_open($ciphertext, $nonce, $key) |
|
| 922 | - { |
|
| 898 | + public static function secretbox_open($ciphertext, $nonce, $key) { |
|
| 923 | 899 | /** @var string $mac */ |
| 924 | 900 | $mac = ParagonIE_Sodium_Core_Util::substr( |
| 925 | 901 | $ciphertext, |
@@ -989,8 +965,7 @@ discard block |
||
| 989 | 965 | * @param string $key |
| 990 | 966 | * @return string |
| 991 | 967 | */ |
| 992 | - public static function secretbox_xchacha20poly1305($plaintext, $nonce, $key) |
|
| 993 | - { |
|
| 968 | + public static function secretbox_xchacha20poly1305($plaintext, $nonce, $key) { |
|
| 994 | 969 | /** @var string $subkey */ |
| 995 | 970 | $subkey = ParagonIE_Sodium_Core_HChaCha20::hChaCha20( |
| 996 | 971 | ParagonIE_Sodium_Core_Util::substr($nonce, 0, 16), |
@@ -1067,8 +1042,7 @@ discard block |
||
| 1067 | 1042 | * @return string |
| 1068 | 1043 | * @throws Error |
| 1069 | 1044 | */ |
| 1070 | - public static function secretbox_xchacha20poly1305_open($ciphertext, $nonce, $key) |
|
| 1071 | - { |
|
| 1045 | + public static function secretbox_xchacha20poly1305_open($ciphertext, $nonce, $key) { |
|
| 1072 | 1046 | /** @var string $mac */ |
| 1073 | 1047 | $mac = ParagonIE_Sodium_Core_Util::substr( |
| 1074 | 1048 | $ciphertext, |
@@ -1139,8 +1113,7 @@ discard block |
||
| 1139 | 1113 | * @param string $sk |
| 1140 | 1114 | * @return string |
| 1141 | 1115 | */ |
| 1142 | - public static function sign_detached($message, $sk) |
|
| 1143 | - { |
|
| 1116 | + public static function sign_detached($message, $sk) { |
|
| 1144 | 1117 | return ParagonIE_Sodium_Core_Ed25519::sign_detached($message, $sk); |
| 1145 | 1118 | } |
| 1146 | 1119 | |
@@ -1153,8 +1126,7 @@ discard block |
||
| 1153 | 1126 | * @param string $sk |
| 1154 | 1127 | * @return string |
| 1155 | 1128 | */ |
| 1156 | - public static function sign($message, $sk) |
|
| 1157 | - { |
|
| 1129 | + public static function sign($message, $sk) { |
|
| 1158 | 1130 | return ParagonIE_Sodium_Core_Ed25519::sign($message, $sk); |
| 1159 | 1131 | } |
| 1160 | 1132 | |
@@ -1167,8 +1139,7 @@ discard block |
||
| 1167 | 1139 | * @param string $pk |
| 1168 | 1140 | * @return string |
| 1169 | 1141 | */ |
| 1170 | - public static function sign_open($signedMessage, $pk) |
|
| 1171 | - { |
|
| 1142 | + public static function sign_open($signedMessage, $pk) { |
|
| 1172 | 1143 | return ParagonIE_Sodium_Core_Ed25519::sign_open($signedMessage, $pk); |
| 1173 | 1144 | } |
| 1174 | 1145 | |
@@ -1182,8 +1153,7 @@ discard block |
||
| 1182 | 1153 | * @param string $pk |
| 1183 | 1154 | * @return bool |
| 1184 | 1155 | */ |
| 1185 | - public static function sign_verify_detached($signature, $message, $pk) |
|
| 1186 | - { |
|
| 1156 | + public static function sign_verify_detached($signature, $message, $pk) { |
|
| 1187 | 1157 | return ParagonIE_Sodium_Core_Ed25519::verify_detached($signature, $message, $pk); |
| 1188 | 1158 | } |
| 1189 | 1159 | } |
@@ -1,7 +1,7 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | if (class_exists('ParagonIE_Sodium_Core_Poly1305', false)) { |
| 4 | - return; |
|
| 4 | + return; |
|
| 5 | 5 | } |
| 6 | 6 | |
| 7 | 7 | /** |
@@ -9,51 +9,51 @@ discard block |
||
| 9 | 9 | */ |
| 10 | 10 | abstract class ParagonIE_Sodium_Core_Poly1305 extends ParagonIE_Sodium_Core_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_Core_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_Core_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_Core_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_Core_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 | } |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -if (class_exists('ParagonIE_Sodium_Core_Poly1305', false)) { |
|
| 3 | +if ( class_exists( 'ParagonIE_Sodium_Core_Poly1305', false ) ) { |
|
| 4 | 4 | return; |
| 5 | 5 | } |
| 6 | 6 | |
@@ -18,18 +18,18 @@ discard block |
||
| 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_Core_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 |
||
| 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_Core_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 | } |
@@ -7,8 +7,7 @@ discard block |
||
| 7 | 7 | /** |
| 8 | 8 | * Class ParagonIE_Sodium_Core_Poly1305 |
| 9 | 9 | */ |
| 10 | -abstract class ParagonIE_Sodium_Core_Poly1305 extends ParagonIE_Sodium_Core_Util |
|
| 11 | -{ |
|
| 10 | +abstract class ParagonIE_Sodium_Core_Poly1305 extends ParagonIE_Sodium_Core_Util { |
|
| 12 | 11 | const BLOCK_SIZE = 16; |
| 13 | 12 | |
| 14 | 13 | /** |
@@ -18,8 +17,7 @@ discard block |
||
| 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 |
||
| 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.' |
@@ -1,7 +1,7 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | if (class_exists('ParagonIE_Sodium_Core_Ed25519', false)) { |
| 4 | - return; |
|
| 4 | + return; |
|
| 5 | 5 | } |
| 6 | 6 | |
| 7 | 7 | /** |
@@ -9,410 +9,410 @@ discard block |
||
| 9 | 9 | */ |
| 10 | 10 | abstract class ParagonIE_Sodium_Core_Ed25519 extends ParagonIE_Sodium_Core_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_Core_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_Core_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_Core_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_Core_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 | } |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -if (class_exists('ParagonIE_Sodium_Core_Ed25519', false)) { |
|
| 3 | +if ( class_exists( 'ParagonIE_Sodium_Core_Ed25519', false ) ) { |
|
| 4 | 4 | return; |
| 5 | 5 | } |
| 6 | 6 | |
@@ -19,10 +19,10 @@ discard block |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 244 | 244 | ParagonIE_Sodium_Compat::$fastMult = true; |
| 245 | 245 | |
| 246 | 246 | /** @var ParagonIE_Sodium_Core_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_Core_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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 | } |
@@ -7,8 +7,7 @@ discard block |
||
| 7 | 7 | /** |
| 8 | 8 | * Class ParagonIE_Sodium_Core_Ed25519 |
| 9 | 9 | */ |
| 10 | -abstract class ParagonIE_Sodium_Core_Ed25519 extends ParagonIE_Sodium_Core_Curve25519 |
|
| 11 | -{ |
|
| 10 | +abstract class ParagonIE_Sodium_Core_Ed25519 extends ParagonIE_Sodium_Core_Curve25519 { |
|
| 12 | 11 | const KEYPAIR_BYTES = 96; |
| 13 | 12 | const SEED_BYTES = 32; |
| 14 | 13 | |
@@ -17,8 +16,7 @@ discard block |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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( |
@@ -1,7 +1,7 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | if (class_exists('ParagonIE_Sodium_Core_Poly1305_State', false)) { |
| 4 | - return; |
|
| 4 | + return; |
|
| 5 | 5 | } |
| 6 | 6 | |
| 7 | 7 | /** |
@@ -9,349 +9,349 @@ discard block |
||
| 9 | 9 | */ |
| 10 | 10 | class ParagonIE_Sodium_Core_Poly1305_State extends ParagonIE_Sodium_Core_Util |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * @var array<int, int> |
|
| 14 | - */ |
|
| 15 | - protected $buffer = array(); |
|
| 16 | - |
|
| 17 | - /** |
|
| 18 | - * @var bool |
|
| 19 | - */ |
|
| 20 | - protected $final = false; |
|
| 21 | - |
|
| 22 | - /** |
|
| 23 | - * @var array<int, int> |
|
| 24 | - */ |
|
| 25 | - public $h; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * @var int |
|
| 29 | - */ |
|
| 30 | - protected $leftover = 0; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * @var int[] |
|
| 34 | - */ |
|
| 35 | - public $r; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * @var int[] |
|
| 39 | - */ |
|
| 40 | - public $pad; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * ParagonIE_Sodium_Core_Poly1305_State constructor. |
|
| 44 | - * |
|
| 45 | - * @internal You should not use this directly from another application |
|
| 46 | - * |
|
| 47 | - * @param string $key |
|
| 48 | - * @throws InvalidArgumentException |
|
| 49 | - */ |
|
| 50 | - public function __construct($key = '') |
|
| 51 | - { |
|
| 52 | - if (self::strlen($key) < 32) { |
|
| 53 | - throw new InvalidArgumentException( |
|
| 54 | - 'Poly1305 requires a 32-byte key' |
|
| 55 | - ); |
|
| 56 | - } |
|
| 57 | - /* r &= 0xffffffc0ffffffc0ffffffc0fffffff */ |
|
| 58 | - $this->r = array( |
|
| 59 | - (int) ((self::load_4(self::substr($key, 0, 4))) & 0x3ffffff), |
|
| 60 | - (int) ((self::load_4(self::substr($key, 3, 4)) >> 2) & 0x3ffff03), |
|
| 61 | - (int) ((self::load_4(self::substr($key, 6, 4)) >> 4) & 0x3ffc0ff), |
|
| 62 | - (int) ((self::load_4(self::substr($key, 9, 4)) >> 6) & 0x3f03fff), |
|
| 63 | - (int) ((self::load_4(self::substr($key, 12, 4)) >> 8) & 0x00fffff) |
|
| 64 | - ); |
|
| 65 | - |
|
| 66 | - /* h = 0 */ |
|
| 67 | - $this->h = array(0, 0, 0, 0, 0); |
|
| 68 | - |
|
| 69 | - /* save pad for later */ |
|
| 70 | - $this->pad = array( |
|
| 71 | - self::load_4(self::substr($key, 16, 4)), |
|
| 72 | - self::load_4(self::substr($key, 20, 4)), |
|
| 73 | - self::load_4(self::substr($key, 24, 4)), |
|
| 74 | - self::load_4(self::substr($key, 28, 4)), |
|
| 75 | - ); |
|
| 76 | - |
|
| 77 | - $this->leftover = 0; |
|
| 78 | - $this->final = false; |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * @internal You should not use this directly from another application |
|
| 83 | - * |
|
| 84 | - * @param string $message |
|
| 85 | - * @return self |
|
| 86 | - */ |
|
| 87 | - public function update($message = '') |
|
| 88 | - { |
|
| 89 | - $bytes = self::strlen($message); |
|
| 90 | - |
|
| 91 | - /* handle leftover */ |
|
| 92 | - if ($this->leftover) { |
|
| 93 | - $want = ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE - $this->leftover; |
|
| 94 | - if ($want > $bytes) { |
|
| 95 | - $want = $bytes; |
|
| 96 | - } |
|
| 97 | - for ($i = 0; $i < $want; ++$i) { |
|
| 98 | - $mi = self::chrToInt($message[$i]); |
|
| 99 | - $this->buffer[$this->leftover + $i] = $mi; |
|
| 100 | - } |
|
| 101 | - // We snip off the leftmost bytes. |
|
| 102 | - $message = self::substr($message, $want); |
|
| 103 | - $bytes = self::strlen($message); |
|
| 104 | - $this->leftover += $want; |
|
| 105 | - if ($this->leftover < ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE) { |
|
| 106 | - // We still don't have enough to run $this->blocks() |
|
| 107 | - return $this; |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - $this->blocks( |
|
| 111 | - static::intArrayToString($this->buffer), |
|
| 112 | - ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE |
|
| 113 | - ); |
|
| 114 | - $this->leftover = 0; |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - /* process full blocks */ |
|
| 118 | - if ($bytes >= ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE) { |
|
| 119 | - $want = $bytes & ~(ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE - 1); |
|
| 120 | - if ($want >= ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE) { |
|
| 121 | - $block = self::substr($message, 0, $want); |
|
| 122 | - if (self::strlen($block) >= ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE) { |
|
| 123 | - $this->blocks($block, $want); |
|
| 124 | - $message = self::substr($message, $want); |
|
| 125 | - $bytes = self::strlen($message); |
|
| 126 | - } |
|
| 127 | - } |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - /* store leftover */ |
|
| 131 | - if ($bytes) { |
|
| 132 | - for ($i = 0; $i < $bytes; ++$i) { |
|
| 133 | - $mi = self::chrToInt($message[$i]); |
|
| 134 | - $this->buffer[$this->leftover + $i] = $mi; |
|
| 135 | - } |
|
| 136 | - $this->leftover = (int) $this->leftover + $bytes; |
|
| 137 | - } |
|
| 138 | - return $this; |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - /** |
|
| 142 | - * @internal You should not use this directly from another application |
|
| 143 | - * |
|
| 144 | - * @param string $message |
|
| 145 | - * @param int $bytes |
|
| 146 | - * @return self |
|
| 147 | - */ |
|
| 148 | - public function blocks($message, $bytes) |
|
| 149 | - { |
|
| 150 | - if (self::strlen($message) < 16) { |
|
| 151 | - $message = str_pad($message, 16, "\x00", STR_PAD_RIGHT); |
|
| 152 | - } |
|
| 153 | - $hibit = $this->final ? 0 : 1 << 24; /* 1 << 128 */ |
|
| 154 | - $r0 = (int) $this->r[0]; |
|
| 155 | - $r1 = (int) $this->r[1]; |
|
| 156 | - $r2 = (int) $this->r[2]; |
|
| 157 | - $r3 = (int) $this->r[3]; |
|
| 158 | - $r4 = (int) $this->r[4]; |
|
| 159 | - |
|
| 160 | - $s1 = self::mul($r1, 5, 3); |
|
| 161 | - $s2 = self::mul($r2, 5, 3); |
|
| 162 | - $s3 = self::mul($r3, 5, 3); |
|
| 163 | - $s4 = self::mul($r4, 5, 3); |
|
| 164 | - |
|
| 165 | - $h0 = $this->h[0]; |
|
| 166 | - $h1 = $this->h[1]; |
|
| 167 | - $h2 = $this->h[2]; |
|
| 168 | - $h3 = $this->h[3]; |
|
| 169 | - $h4 = $this->h[4]; |
|
| 170 | - |
|
| 171 | - while ($bytes >= ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE) { |
|
| 172 | - /* h += m[i] */ |
|
| 173 | - $h0 += self::load_4(self::substr($message, 0, 4)) & 0x3ffffff; |
|
| 174 | - $h1 += (self::load_4(self::substr($message, 3, 4)) >> 2) & 0x3ffffff; |
|
| 175 | - $h2 += (self::load_4(self::substr($message, 6, 4)) >> 4) & 0x3ffffff; |
|
| 176 | - $h3 += (self::load_4(self::substr($message, 9, 4)) >> 6) & 0x3ffffff; |
|
| 177 | - $h4 += (self::load_4(self::substr($message, 12, 4)) >> 8) | $hibit; |
|
| 178 | - |
|
| 179 | - /* h *= r */ |
|
| 180 | - $d0 = ( |
|
| 181 | - self::mul($h0, $r0) + |
|
| 182 | - self::mul($h1, $s4) + |
|
| 183 | - self::mul($h2, $s3) + |
|
| 184 | - self::mul($h3, $s2) + |
|
| 185 | - self::mul($h4, $s1) |
|
| 186 | - ); |
|
| 187 | - |
|
| 188 | - $d1 = ( |
|
| 189 | - self::mul($h0, $r1) + |
|
| 190 | - self::mul($h1, $r0) + |
|
| 191 | - self::mul($h2, $s4) + |
|
| 192 | - self::mul($h3, $s3) + |
|
| 193 | - self::mul($h4, $s2) |
|
| 194 | - ); |
|
| 195 | - |
|
| 196 | - $d2 = ( |
|
| 197 | - self::mul($h0, $r2) + |
|
| 198 | - self::mul($h1, $r1) + |
|
| 199 | - self::mul($h2, $r0) + |
|
| 200 | - self::mul($h3, $s4) + |
|
| 201 | - self::mul($h4, $s3) |
|
| 202 | - ); |
|
| 203 | - |
|
| 204 | - $d3 = ( |
|
| 205 | - self::mul($h0, $r3) + |
|
| 206 | - self::mul($h1, $r2) + |
|
| 207 | - self::mul($h2, $r1) + |
|
| 208 | - self::mul($h3, $r0) + |
|
| 209 | - self::mul($h4, $s4) |
|
| 210 | - ); |
|
| 211 | - |
|
| 212 | - $d4 = ( |
|
| 213 | - self::mul($h0, $r4) + |
|
| 214 | - self::mul($h1, $r3) + |
|
| 215 | - self::mul($h2, $r2) + |
|
| 216 | - self::mul($h3, $r1) + |
|
| 217 | - self::mul($h4, $r0) |
|
| 218 | - ); |
|
| 219 | - |
|
| 220 | - /* (partial) h %= p */ |
|
| 221 | - $c = $d0 >> 26; |
|
| 222 | - $h0 = $d0 & 0x3ffffff; |
|
| 223 | - $d1 += $c; |
|
| 224 | - $c = $d1 >> 26; |
|
| 225 | - $h1 = $d1 & 0x3ffffff; |
|
| 226 | - $d2 += $c; |
|
| 227 | - $c = $d2 >> 26; |
|
| 228 | - $h2 = $d2 & 0x3ffffff; |
|
| 229 | - $d3 += $c; |
|
| 230 | - $c = $d3 >> 26; |
|
| 231 | - $h3 = $d3 & 0x3ffffff; |
|
| 232 | - $d4 += $c; |
|
| 233 | - $c = $d4 >> 26; |
|
| 234 | - $h4 = $d4 & 0x3ffffff; |
|
| 235 | - $h0 += (int) self::mul($c, 5, 3); |
|
| 236 | - $c = $h0 >> 26; |
|
| 237 | - $h0 &= 0x3ffffff; |
|
| 238 | - $h1 += $c; |
|
| 239 | - |
|
| 240 | - // Chop off the left 32 bytes. |
|
| 241 | - $message = self::substr( |
|
| 242 | - $message, |
|
| 243 | - ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE |
|
| 244 | - ); |
|
| 245 | - $bytes -= ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE; |
|
| 246 | - } |
|
| 247 | - |
|
| 248 | - $this->h = array( |
|
| 249 | - (int) ($h0 & 0xffffffff), |
|
| 250 | - (int) ($h1 & 0xffffffff), |
|
| 251 | - (int) ($h2 & 0xffffffff), |
|
| 252 | - (int) ($h3 & 0xffffffff), |
|
| 253 | - (int) ($h4 & 0xffffffff) |
|
| 254 | - ); |
|
| 255 | - return $this; |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - /** |
|
| 259 | - * @internal You should not use this directly from another application |
|
| 260 | - * |
|
| 261 | - * @return string |
|
| 262 | - */ |
|
| 263 | - public function finish() |
|
| 264 | - { |
|
| 265 | - /* process the remaining block */ |
|
| 266 | - if ($this->leftover) { |
|
| 267 | - $i = $this->leftover; |
|
| 268 | - $this->buffer[$i++] = 1; |
|
| 269 | - for (; $i < ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE; ++$i) { |
|
| 270 | - $this->buffer[$i] = 0; |
|
| 271 | - } |
|
| 272 | - $this->final = true; |
|
| 273 | - $this->blocks( |
|
| 274 | - self::substr( |
|
| 275 | - static::intArrayToString($this->buffer), |
|
| 276 | - 0, |
|
| 277 | - ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE |
|
| 278 | - ), |
|
| 279 | - ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE |
|
| 280 | - ); |
|
| 281 | - } |
|
| 282 | - |
|
| 283 | - $h0 = (int) $this->h[0]; |
|
| 284 | - $h1 = (int) $this->h[1]; |
|
| 285 | - $h2 = (int) $this->h[2]; |
|
| 286 | - $h3 = (int) $this->h[3]; |
|
| 287 | - $h4 = (int) $this->h[4]; |
|
| 288 | - |
|
| 289 | - $c = $h1 >> 26; |
|
| 290 | - $h1 &= 0x3ffffff; |
|
| 291 | - $h2 += $c; |
|
| 292 | - $c = $h2 >> 26; |
|
| 293 | - $h2 &= 0x3ffffff; |
|
| 294 | - $h3 += $c; |
|
| 295 | - $c = $h3 >> 26; |
|
| 296 | - $h3 &= 0x3ffffff; |
|
| 297 | - $h4 += $c; |
|
| 298 | - $c = $h4 >> 26; |
|
| 299 | - $h4 &= 0x3ffffff; |
|
| 300 | - $h0 += self::mul($c, 5, 3); |
|
| 301 | - $c = $h0 >> 26; |
|
| 302 | - $h0 &= 0x3ffffff; |
|
| 303 | - $h1 += $c; |
|
| 304 | - |
|
| 305 | - /* compute h + -p */ |
|
| 306 | - $g0 = $h0 + 5; |
|
| 307 | - $c = $g0 >> 26; |
|
| 308 | - $g0 &= 0x3ffffff; |
|
| 309 | - $g1 = $h1 + $c; |
|
| 310 | - $c = $g1 >> 26; |
|
| 311 | - $g1 &= 0x3ffffff; |
|
| 312 | - $g2 = $h2 + $c; |
|
| 313 | - $c = $g2 >> 26; |
|
| 314 | - $g2 &= 0x3ffffff; |
|
| 315 | - $g3 = $h3 + $c; |
|
| 316 | - $c = $g3 >> 26; |
|
| 317 | - $g3 &= 0x3ffffff; |
|
| 318 | - $g4 = ($h4 + $c - (1 << 26)) & 0xffffffff; |
|
| 319 | - |
|
| 320 | - /* select h if h < p, or h + -p if h >= p */ |
|
| 321 | - $mask = ($g4 >> 31) - 1; |
|
| 322 | - |
|
| 323 | - $g0 &= $mask; |
|
| 324 | - $g1 &= $mask; |
|
| 325 | - $g2 &= $mask; |
|
| 326 | - $g3 &= $mask; |
|
| 327 | - $g4 &= $mask; |
|
| 328 | - |
|
| 329 | - $mask = ~$mask & 0xffffffff; |
|
| 330 | - $h0 = ($h0 & $mask) | $g0; |
|
| 331 | - $h1 = ($h1 & $mask) | $g1; |
|
| 332 | - $h2 = ($h2 & $mask) | $g2; |
|
| 333 | - $h3 = ($h3 & $mask) | $g3; |
|
| 334 | - $h4 = ($h4 & $mask) | $g4; |
|
| 335 | - |
|
| 336 | - /* h = h % (2^128) */ |
|
| 337 | - $h0 = (($h0) | ($h1 << 26)) & 0xffffffff; |
|
| 338 | - $h1 = (($h1 >> 6) | ($h2 << 20)) & 0xffffffff; |
|
| 339 | - $h2 = (($h2 >> 12) | ($h3 << 14)) & 0xffffffff; |
|
| 340 | - $h3 = (($h3 >> 18) | ($h4 << 8)) & 0xffffffff; |
|
| 341 | - |
|
| 342 | - /* mac = (h + pad) % (2^128) */ |
|
| 343 | - $f = ($h0 + $this->pad[0]); |
|
| 344 | - $h0 = (int) $f; |
|
| 345 | - $f = ($h1 + $this->pad[1] + ($f >> 32)); |
|
| 346 | - $h1 = (int) $f; |
|
| 347 | - $f = ($h2 + $this->pad[2] + ($f >> 32)); |
|
| 348 | - $h2 = (int) $f; |
|
| 349 | - $f = ($h3 + $this->pad[3] + ($f >> 32)); |
|
| 350 | - $h3 = (int) $f; |
|
| 351 | - |
|
| 352 | - return self::store32_le($h0 & 0xffffffff) . |
|
| 353 | - self::store32_le($h1 & 0xffffffff) . |
|
| 354 | - self::store32_le($h2 & 0xffffffff) . |
|
| 355 | - self::store32_le($h3 & 0xffffffff); |
|
| 356 | - } |
|
| 12 | + /** |
|
| 13 | + * @var array<int, int> |
|
| 14 | + */ |
|
| 15 | + protected $buffer = array(); |
|
| 16 | + |
|
| 17 | + /** |
|
| 18 | + * @var bool |
|
| 19 | + */ |
|
| 20 | + protected $final = false; |
|
| 21 | + |
|
| 22 | + /** |
|
| 23 | + * @var array<int, int> |
|
| 24 | + */ |
|
| 25 | + public $h; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * @var int |
|
| 29 | + */ |
|
| 30 | + protected $leftover = 0; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * @var int[] |
|
| 34 | + */ |
|
| 35 | + public $r; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * @var int[] |
|
| 39 | + */ |
|
| 40 | + public $pad; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * ParagonIE_Sodium_Core_Poly1305_State constructor. |
|
| 44 | + * |
|
| 45 | + * @internal You should not use this directly from another application |
|
| 46 | + * |
|
| 47 | + * @param string $key |
|
| 48 | + * @throws InvalidArgumentException |
|
| 49 | + */ |
|
| 50 | + public function __construct($key = '') |
|
| 51 | + { |
|
| 52 | + if (self::strlen($key) < 32) { |
|
| 53 | + throw new InvalidArgumentException( |
|
| 54 | + 'Poly1305 requires a 32-byte key' |
|
| 55 | + ); |
|
| 56 | + } |
|
| 57 | + /* r &= 0xffffffc0ffffffc0ffffffc0fffffff */ |
|
| 58 | + $this->r = array( |
|
| 59 | + (int) ((self::load_4(self::substr($key, 0, 4))) & 0x3ffffff), |
|
| 60 | + (int) ((self::load_4(self::substr($key, 3, 4)) >> 2) & 0x3ffff03), |
|
| 61 | + (int) ((self::load_4(self::substr($key, 6, 4)) >> 4) & 0x3ffc0ff), |
|
| 62 | + (int) ((self::load_4(self::substr($key, 9, 4)) >> 6) & 0x3f03fff), |
|
| 63 | + (int) ((self::load_4(self::substr($key, 12, 4)) >> 8) & 0x00fffff) |
|
| 64 | + ); |
|
| 65 | + |
|
| 66 | + /* h = 0 */ |
|
| 67 | + $this->h = array(0, 0, 0, 0, 0); |
|
| 68 | + |
|
| 69 | + /* save pad for later */ |
|
| 70 | + $this->pad = array( |
|
| 71 | + self::load_4(self::substr($key, 16, 4)), |
|
| 72 | + self::load_4(self::substr($key, 20, 4)), |
|
| 73 | + self::load_4(self::substr($key, 24, 4)), |
|
| 74 | + self::load_4(self::substr($key, 28, 4)), |
|
| 75 | + ); |
|
| 76 | + |
|
| 77 | + $this->leftover = 0; |
|
| 78 | + $this->final = false; |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * @internal You should not use this directly from another application |
|
| 83 | + * |
|
| 84 | + * @param string $message |
|
| 85 | + * @return self |
|
| 86 | + */ |
|
| 87 | + public function update($message = '') |
|
| 88 | + { |
|
| 89 | + $bytes = self::strlen($message); |
|
| 90 | + |
|
| 91 | + /* handle leftover */ |
|
| 92 | + if ($this->leftover) { |
|
| 93 | + $want = ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE - $this->leftover; |
|
| 94 | + if ($want > $bytes) { |
|
| 95 | + $want = $bytes; |
|
| 96 | + } |
|
| 97 | + for ($i = 0; $i < $want; ++$i) { |
|
| 98 | + $mi = self::chrToInt($message[$i]); |
|
| 99 | + $this->buffer[$this->leftover + $i] = $mi; |
|
| 100 | + } |
|
| 101 | + // We snip off the leftmost bytes. |
|
| 102 | + $message = self::substr($message, $want); |
|
| 103 | + $bytes = self::strlen($message); |
|
| 104 | + $this->leftover += $want; |
|
| 105 | + if ($this->leftover < ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE) { |
|
| 106 | + // We still don't have enough to run $this->blocks() |
|
| 107 | + return $this; |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + $this->blocks( |
|
| 111 | + static::intArrayToString($this->buffer), |
|
| 112 | + ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE |
|
| 113 | + ); |
|
| 114 | + $this->leftover = 0; |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + /* process full blocks */ |
|
| 118 | + if ($bytes >= ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE) { |
|
| 119 | + $want = $bytes & ~(ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE - 1); |
|
| 120 | + if ($want >= ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE) { |
|
| 121 | + $block = self::substr($message, 0, $want); |
|
| 122 | + if (self::strlen($block) >= ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE) { |
|
| 123 | + $this->blocks($block, $want); |
|
| 124 | + $message = self::substr($message, $want); |
|
| 125 | + $bytes = self::strlen($message); |
|
| 126 | + } |
|
| 127 | + } |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + /* store leftover */ |
|
| 131 | + if ($bytes) { |
|
| 132 | + for ($i = 0; $i < $bytes; ++$i) { |
|
| 133 | + $mi = self::chrToInt($message[$i]); |
|
| 134 | + $this->buffer[$this->leftover + $i] = $mi; |
|
| 135 | + } |
|
| 136 | + $this->leftover = (int) $this->leftover + $bytes; |
|
| 137 | + } |
|
| 138 | + return $this; |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + /** |
|
| 142 | + * @internal You should not use this directly from another application |
|
| 143 | + * |
|
| 144 | + * @param string $message |
|
| 145 | + * @param int $bytes |
|
| 146 | + * @return self |
|
| 147 | + */ |
|
| 148 | + public function blocks($message, $bytes) |
|
| 149 | + { |
|
| 150 | + if (self::strlen($message) < 16) { |
|
| 151 | + $message = str_pad($message, 16, "\x00", STR_PAD_RIGHT); |
|
| 152 | + } |
|
| 153 | + $hibit = $this->final ? 0 : 1 << 24; /* 1 << 128 */ |
|
| 154 | + $r0 = (int) $this->r[0]; |
|
| 155 | + $r1 = (int) $this->r[1]; |
|
| 156 | + $r2 = (int) $this->r[2]; |
|
| 157 | + $r3 = (int) $this->r[3]; |
|
| 158 | + $r4 = (int) $this->r[4]; |
|
| 159 | + |
|
| 160 | + $s1 = self::mul($r1, 5, 3); |
|
| 161 | + $s2 = self::mul($r2, 5, 3); |
|
| 162 | + $s3 = self::mul($r3, 5, 3); |
|
| 163 | + $s4 = self::mul($r4, 5, 3); |
|
| 164 | + |
|
| 165 | + $h0 = $this->h[0]; |
|
| 166 | + $h1 = $this->h[1]; |
|
| 167 | + $h2 = $this->h[2]; |
|
| 168 | + $h3 = $this->h[3]; |
|
| 169 | + $h4 = $this->h[4]; |
|
| 170 | + |
|
| 171 | + while ($bytes >= ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE) { |
|
| 172 | + /* h += m[i] */ |
|
| 173 | + $h0 += self::load_4(self::substr($message, 0, 4)) & 0x3ffffff; |
|
| 174 | + $h1 += (self::load_4(self::substr($message, 3, 4)) >> 2) & 0x3ffffff; |
|
| 175 | + $h2 += (self::load_4(self::substr($message, 6, 4)) >> 4) & 0x3ffffff; |
|
| 176 | + $h3 += (self::load_4(self::substr($message, 9, 4)) >> 6) & 0x3ffffff; |
|
| 177 | + $h4 += (self::load_4(self::substr($message, 12, 4)) >> 8) | $hibit; |
|
| 178 | + |
|
| 179 | + /* h *= r */ |
|
| 180 | + $d0 = ( |
|
| 181 | + self::mul($h0, $r0) + |
|
| 182 | + self::mul($h1, $s4) + |
|
| 183 | + self::mul($h2, $s3) + |
|
| 184 | + self::mul($h3, $s2) + |
|
| 185 | + self::mul($h4, $s1) |
|
| 186 | + ); |
|
| 187 | + |
|
| 188 | + $d1 = ( |
|
| 189 | + self::mul($h0, $r1) + |
|
| 190 | + self::mul($h1, $r0) + |
|
| 191 | + self::mul($h2, $s4) + |
|
| 192 | + self::mul($h3, $s3) + |
|
| 193 | + self::mul($h4, $s2) |
|
| 194 | + ); |
|
| 195 | + |
|
| 196 | + $d2 = ( |
|
| 197 | + self::mul($h0, $r2) + |
|
| 198 | + self::mul($h1, $r1) + |
|
| 199 | + self::mul($h2, $r0) + |
|
| 200 | + self::mul($h3, $s4) + |
|
| 201 | + self::mul($h4, $s3) |
|
| 202 | + ); |
|
| 203 | + |
|
| 204 | + $d3 = ( |
|
| 205 | + self::mul($h0, $r3) + |
|
| 206 | + self::mul($h1, $r2) + |
|
| 207 | + self::mul($h2, $r1) + |
|
| 208 | + self::mul($h3, $r0) + |
|
| 209 | + self::mul($h4, $s4) |
|
| 210 | + ); |
|
| 211 | + |
|
| 212 | + $d4 = ( |
|
| 213 | + self::mul($h0, $r4) + |
|
| 214 | + self::mul($h1, $r3) + |
|
| 215 | + self::mul($h2, $r2) + |
|
| 216 | + self::mul($h3, $r1) + |
|
| 217 | + self::mul($h4, $r0) |
|
| 218 | + ); |
|
| 219 | + |
|
| 220 | + /* (partial) h %= p */ |
|
| 221 | + $c = $d0 >> 26; |
|
| 222 | + $h0 = $d0 & 0x3ffffff; |
|
| 223 | + $d1 += $c; |
|
| 224 | + $c = $d1 >> 26; |
|
| 225 | + $h1 = $d1 & 0x3ffffff; |
|
| 226 | + $d2 += $c; |
|
| 227 | + $c = $d2 >> 26; |
|
| 228 | + $h2 = $d2 & 0x3ffffff; |
|
| 229 | + $d3 += $c; |
|
| 230 | + $c = $d3 >> 26; |
|
| 231 | + $h3 = $d3 & 0x3ffffff; |
|
| 232 | + $d4 += $c; |
|
| 233 | + $c = $d4 >> 26; |
|
| 234 | + $h4 = $d4 & 0x3ffffff; |
|
| 235 | + $h0 += (int) self::mul($c, 5, 3); |
|
| 236 | + $c = $h0 >> 26; |
|
| 237 | + $h0 &= 0x3ffffff; |
|
| 238 | + $h1 += $c; |
|
| 239 | + |
|
| 240 | + // Chop off the left 32 bytes. |
|
| 241 | + $message = self::substr( |
|
| 242 | + $message, |
|
| 243 | + ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE |
|
| 244 | + ); |
|
| 245 | + $bytes -= ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE; |
|
| 246 | + } |
|
| 247 | + |
|
| 248 | + $this->h = array( |
|
| 249 | + (int) ($h0 & 0xffffffff), |
|
| 250 | + (int) ($h1 & 0xffffffff), |
|
| 251 | + (int) ($h2 & 0xffffffff), |
|
| 252 | + (int) ($h3 & 0xffffffff), |
|
| 253 | + (int) ($h4 & 0xffffffff) |
|
| 254 | + ); |
|
| 255 | + return $this; |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + /** |
|
| 259 | + * @internal You should not use this directly from another application |
|
| 260 | + * |
|
| 261 | + * @return string |
|
| 262 | + */ |
|
| 263 | + public function finish() |
|
| 264 | + { |
|
| 265 | + /* process the remaining block */ |
|
| 266 | + if ($this->leftover) { |
|
| 267 | + $i = $this->leftover; |
|
| 268 | + $this->buffer[$i++] = 1; |
|
| 269 | + for (; $i < ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE; ++$i) { |
|
| 270 | + $this->buffer[$i] = 0; |
|
| 271 | + } |
|
| 272 | + $this->final = true; |
|
| 273 | + $this->blocks( |
|
| 274 | + self::substr( |
|
| 275 | + static::intArrayToString($this->buffer), |
|
| 276 | + 0, |
|
| 277 | + ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE |
|
| 278 | + ), |
|
| 279 | + ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE |
|
| 280 | + ); |
|
| 281 | + } |
|
| 282 | + |
|
| 283 | + $h0 = (int) $this->h[0]; |
|
| 284 | + $h1 = (int) $this->h[1]; |
|
| 285 | + $h2 = (int) $this->h[2]; |
|
| 286 | + $h3 = (int) $this->h[3]; |
|
| 287 | + $h4 = (int) $this->h[4]; |
|
| 288 | + |
|
| 289 | + $c = $h1 >> 26; |
|
| 290 | + $h1 &= 0x3ffffff; |
|
| 291 | + $h2 += $c; |
|
| 292 | + $c = $h2 >> 26; |
|
| 293 | + $h2 &= 0x3ffffff; |
|
| 294 | + $h3 += $c; |
|
| 295 | + $c = $h3 >> 26; |
|
| 296 | + $h3 &= 0x3ffffff; |
|
| 297 | + $h4 += $c; |
|
| 298 | + $c = $h4 >> 26; |
|
| 299 | + $h4 &= 0x3ffffff; |
|
| 300 | + $h0 += self::mul($c, 5, 3); |
|
| 301 | + $c = $h0 >> 26; |
|
| 302 | + $h0 &= 0x3ffffff; |
|
| 303 | + $h1 += $c; |
|
| 304 | + |
|
| 305 | + /* compute h + -p */ |
|
| 306 | + $g0 = $h0 + 5; |
|
| 307 | + $c = $g0 >> 26; |
|
| 308 | + $g0 &= 0x3ffffff; |
|
| 309 | + $g1 = $h1 + $c; |
|
| 310 | + $c = $g1 >> 26; |
|
| 311 | + $g1 &= 0x3ffffff; |
|
| 312 | + $g2 = $h2 + $c; |
|
| 313 | + $c = $g2 >> 26; |
|
| 314 | + $g2 &= 0x3ffffff; |
|
| 315 | + $g3 = $h3 + $c; |
|
| 316 | + $c = $g3 >> 26; |
|
| 317 | + $g3 &= 0x3ffffff; |
|
| 318 | + $g4 = ($h4 + $c - (1 << 26)) & 0xffffffff; |
|
| 319 | + |
|
| 320 | + /* select h if h < p, or h + -p if h >= p */ |
|
| 321 | + $mask = ($g4 >> 31) - 1; |
|
| 322 | + |
|
| 323 | + $g0 &= $mask; |
|
| 324 | + $g1 &= $mask; |
|
| 325 | + $g2 &= $mask; |
|
| 326 | + $g3 &= $mask; |
|
| 327 | + $g4 &= $mask; |
|
| 328 | + |
|
| 329 | + $mask = ~$mask & 0xffffffff; |
|
| 330 | + $h0 = ($h0 & $mask) | $g0; |
|
| 331 | + $h1 = ($h1 & $mask) | $g1; |
|
| 332 | + $h2 = ($h2 & $mask) | $g2; |
|
| 333 | + $h3 = ($h3 & $mask) | $g3; |
|
| 334 | + $h4 = ($h4 & $mask) | $g4; |
|
| 335 | + |
|
| 336 | + /* h = h % (2^128) */ |
|
| 337 | + $h0 = (($h0) | ($h1 << 26)) & 0xffffffff; |
|
| 338 | + $h1 = (($h1 >> 6) | ($h2 << 20)) & 0xffffffff; |
|
| 339 | + $h2 = (($h2 >> 12) | ($h3 << 14)) & 0xffffffff; |
|
| 340 | + $h3 = (($h3 >> 18) | ($h4 << 8)) & 0xffffffff; |
|
| 341 | + |
|
| 342 | + /* mac = (h + pad) % (2^128) */ |
|
| 343 | + $f = ($h0 + $this->pad[0]); |
|
| 344 | + $h0 = (int) $f; |
|
| 345 | + $f = ($h1 + $this->pad[1] + ($f >> 32)); |
|
| 346 | + $h1 = (int) $f; |
|
| 347 | + $f = ($h2 + $this->pad[2] + ($f >> 32)); |
|
| 348 | + $h2 = (int) $f; |
|
| 349 | + $f = ($h3 + $this->pad[3] + ($f >> 32)); |
|
| 350 | + $h3 = (int) $f; |
|
| 351 | + |
|
| 352 | + return self::store32_le($h0 & 0xffffffff) . |
|
| 353 | + self::store32_le($h1 & 0xffffffff) . |
|
| 354 | + self::store32_le($h2 & 0xffffffff) . |
|
| 355 | + self::store32_le($h3 & 0xffffffff); |
|
| 356 | + } |
|
| 357 | 357 | } |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -if (class_exists('ParagonIE_Sodium_Core_Poly1305_State', false)) { |
|
| 3 | +if ( class_exists( 'ParagonIE_Sodium_Core_Poly1305_State', false ) ) { |
|
| 4 | 4 | return; |
| 5 | 5 | } |
| 6 | 6 | |
@@ -47,31 +47,31 @@ discard block |
||
| 47 | 47 | * @param string $key |
| 48 | 48 | * @throws InvalidArgumentException |
| 49 | 49 | */ |
| 50 | - public function __construct($key = '') |
|
| 50 | + public function __construct( $key = '' ) |
|
| 51 | 51 | { |
| 52 | - if (self::strlen($key) < 32) { |
|
| 52 | + if ( self::strlen( $key ) < 32 ) { |
|
| 53 | 53 | throw new InvalidArgumentException( |
| 54 | 54 | 'Poly1305 requires a 32-byte key' |
| 55 | 55 | ); |
| 56 | 56 | } |
| 57 | 57 | /* r &= 0xffffffc0ffffffc0ffffffc0fffffff */ |
| 58 | 58 | $this->r = array( |
| 59 | - (int) ((self::load_4(self::substr($key, 0, 4))) & 0x3ffffff), |
|
| 60 | - (int) ((self::load_4(self::substr($key, 3, 4)) >> 2) & 0x3ffff03), |
|
| 61 | - (int) ((self::load_4(self::substr($key, 6, 4)) >> 4) & 0x3ffc0ff), |
|
| 62 | - (int) ((self::load_4(self::substr($key, 9, 4)) >> 6) & 0x3f03fff), |
|
| 63 | - (int) ((self::load_4(self::substr($key, 12, 4)) >> 8) & 0x00fffff) |
|
| 59 | + (int) ( ( self::load_4( self::substr( $key, 0, 4 ) ) ) & 0x3ffffff ), |
|
| 60 | + (int) ( ( self::load_4( self::substr( $key, 3, 4 ) ) >> 2 ) & 0x3ffff03 ), |
|
| 61 | + (int) ( ( self::load_4( self::substr( $key, 6, 4 ) ) >> 4 ) & 0x3ffc0ff ), |
|
| 62 | + (int) ( ( self::load_4( self::substr( $key, 9, 4 ) ) >> 6 ) & 0x3f03fff ), |
|
| 63 | + (int) ( ( self::load_4( self::substr( $key, 12, 4 ) ) >> 8 ) & 0x00fffff ) |
|
| 64 | 64 | ); |
| 65 | 65 | |
| 66 | 66 | /* h = 0 */ |
| 67 | - $this->h = array(0, 0, 0, 0, 0); |
|
| 67 | + $this->h = array( 0, 0, 0, 0, 0 ); |
|
| 68 | 68 | |
| 69 | 69 | /* save pad for later */ |
| 70 | 70 | $this->pad = array( |
| 71 | - self::load_4(self::substr($key, 16, 4)), |
|
| 72 | - self::load_4(self::substr($key, 20, 4)), |
|
| 73 | - self::load_4(self::substr($key, 24, 4)), |
|
| 74 | - self::load_4(self::substr($key, 28, 4)), |
|
| 71 | + self::load_4( self::substr( $key, 16, 4 ) ), |
|
| 72 | + self::load_4( self::substr( $key, 20, 4 ) ), |
|
| 73 | + self::load_4( self::substr( $key, 24, 4 ) ), |
|
| 74 | + self::load_4( self::substr( $key, 28, 4 ) ), |
|
| 75 | 75 | ); |
| 76 | 76 | |
| 77 | 77 | $this->leftover = 0; |
@@ -84,53 +84,53 @@ discard block |
||
| 84 | 84 | * @param string $message |
| 85 | 85 | * @return self |
| 86 | 86 | */ |
| 87 | - public function update($message = '') |
|
| 87 | + public function update( $message = '' ) |
|
| 88 | 88 | { |
| 89 | - $bytes = self::strlen($message); |
|
| 89 | + $bytes = self::strlen( $message ); |
|
| 90 | 90 | |
| 91 | 91 | /* handle leftover */ |
| 92 | - if ($this->leftover) { |
|
| 92 | + if ( $this->leftover ) { |
|
| 93 | 93 | $want = ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE - $this->leftover; |
| 94 | - if ($want > $bytes) { |
|
| 94 | + if ( $want > $bytes ) { |
|
| 95 | 95 | $want = $bytes; |
| 96 | 96 | } |
| 97 | - for ($i = 0; $i < $want; ++$i) { |
|
| 98 | - $mi = self::chrToInt($message[$i]); |
|
| 97 | + for ( $i = 0; $i < $want; ++$i ) { |
|
| 98 | + $mi = self::chrToInt( $message[$i] ); |
|
| 99 | 99 | $this->buffer[$this->leftover + $i] = $mi; |
| 100 | 100 | } |
| 101 | 101 | // We snip off the leftmost bytes. |
| 102 | - $message = self::substr($message, $want); |
|
| 103 | - $bytes = self::strlen($message); |
|
| 102 | + $message = self::substr( $message, $want ); |
|
| 103 | + $bytes = self::strlen( $message ); |
|
| 104 | 104 | $this->leftover += $want; |
| 105 | - if ($this->leftover < ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE) { |
|
| 105 | + if ( $this->leftover < ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE ) { |
|
| 106 | 106 | // We still don't have enough to run $this->blocks() |
| 107 | 107 | return $this; |
| 108 | 108 | } |
| 109 | 109 | |
| 110 | 110 | $this->blocks( |
| 111 | - static::intArrayToString($this->buffer), |
|
| 111 | + static::intArrayToString( $this->buffer ), |
|
| 112 | 112 | ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE |
| 113 | 113 | ); |
| 114 | 114 | $this->leftover = 0; |
| 115 | 115 | } |
| 116 | 116 | |
| 117 | 117 | /* process full blocks */ |
| 118 | - if ($bytes >= ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE) { |
|
| 119 | - $want = $bytes & ~(ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE - 1); |
|
| 120 | - if ($want >= ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE) { |
|
| 121 | - $block = self::substr($message, 0, $want); |
|
| 122 | - if (self::strlen($block) >= ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE) { |
|
| 123 | - $this->blocks($block, $want); |
|
| 124 | - $message = self::substr($message, $want); |
|
| 125 | - $bytes = self::strlen($message); |
|
| 118 | + if ( $bytes >= ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE ) { |
|
| 119 | + $want = $bytes & ~( ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE - 1 ); |
|
| 120 | + if ( $want >= ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE ) { |
|
| 121 | + $block = self::substr( $message, 0, $want ); |
|
| 122 | + if ( self::strlen( $block ) >= ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE ) { |
|
| 123 | + $this->blocks( $block, $want ); |
|
| 124 | + $message = self::substr( $message, $want ); |
|
| 125 | + $bytes = self::strlen( $message ); |
|
| 126 | 126 | } |
| 127 | 127 | } |
| 128 | 128 | } |
| 129 | 129 | |
| 130 | 130 | /* store leftover */ |
| 131 | - if ($bytes) { |
|
| 132 | - for ($i = 0; $i < $bytes; ++$i) { |
|
| 133 | - $mi = self::chrToInt($message[$i]); |
|
| 131 | + if ( $bytes ) { |
|
| 132 | + for ( $i = 0; $i < $bytes; ++$i ) { |
|
| 133 | + $mi = self::chrToInt( $message[$i] ); |
|
| 134 | 134 | $this->buffer[$this->leftover + $i] = $mi; |
| 135 | 135 | } |
| 136 | 136 | $this->leftover = (int) $this->leftover + $bytes; |
@@ -145,10 +145,10 @@ discard block |
||
| 145 | 145 | * @param int $bytes |
| 146 | 146 | * @return self |
| 147 | 147 | */ |
| 148 | - public function blocks($message, $bytes) |
|
| 148 | + public function blocks( $message, $bytes ) |
|
| 149 | 149 | { |
| 150 | - if (self::strlen($message) < 16) { |
|
| 151 | - $message = str_pad($message, 16, "\x00", STR_PAD_RIGHT); |
|
| 150 | + if ( self::strlen( $message ) < 16 ) { |
|
| 151 | + $message = str_pad( $message, 16, "\x00", STR_PAD_RIGHT ); |
|
| 152 | 152 | } |
| 153 | 153 | $hibit = $this->final ? 0 : 1 << 24; /* 1 << 128 */ |
| 154 | 154 | $r0 = (int) $this->r[0]; |
@@ -157,10 +157,10 @@ discard block |
||
| 157 | 157 | $r3 = (int) $this->r[3]; |
| 158 | 158 | $r4 = (int) $this->r[4]; |
| 159 | 159 | |
| 160 | - $s1 = self::mul($r1, 5, 3); |
|
| 161 | - $s2 = self::mul($r2, 5, 3); |
|
| 162 | - $s3 = self::mul($r3, 5, 3); |
|
| 163 | - $s4 = self::mul($r4, 5, 3); |
|
| 160 | + $s1 = self::mul( $r1, 5, 3 ); |
|
| 161 | + $s2 = self::mul( $r2, 5, 3 ); |
|
| 162 | + $s3 = self::mul( $r3, 5, 3 ); |
|
| 163 | + $s4 = self::mul( $r4, 5, 3 ); |
|
| 164 | 164 | |
| 165 | 165 | $h0 = $this->h[0]; |
| 166 | 166 | $h1 = $this->h[1]; |
@@ -168,53 +168,53 @@ discard block |
||
| 168 | 168 | $h3 = $this->h[3]; |
| 169 | 169 | $h4 = $this->h[4]; |
| 170 | 170 | |
| 171 | - while ($bytes >= ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE) { |
|
| 171 | + while ( $bytes >= ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE ) { |
|
| 172 | 172 | /* h += m[i] */ |
| 173 | - $h0 += self::load_4(self::substr($message, 0, 4)) & 0x3ffffff; |
|
| 174 | - $h1 += (self::load_4(self::substr($message, 3, 4)) >> 2) & 0x3ffffff; |
|
| 175 | - $h2 += (self::load_4(self::substr($message, 6, 4)) >> 4) & 0x3ffffff; |
|
| 176 | - $h3 += (self::load_4(self::substr($message, 9, 4)) >> 6) & 0x3ffffff; |
|
| 177 | - $h4 += (self::load_4(self::substr($message, 12, 4)) >> 8) | $hibit; |
|
| 173 | + $h0 += self::load_4( self::substr( $message, 0, 4 ) ) & 0x3ffffff; |
|
| 174 | + $h1 += ( self::load_4( self::substr( $message, 3, 4 ) ) >> 2 ) & 0x3ffffff; |
|
| 175 | + $h2 += ( self::load_4( self::substr( $message, 6, 4 ) ) >> 4 ) & 0x3ffffff; |
|
| 176 | + $h3 += ( self::load_4( self::substr( $message, 9, 4 ) ) >> 6 ) & 0x3ffffff; |
|
| 177 | + $h4 += ( self::load_4( self::substr( $message, 12, 4 ) ) >> 8 ) | $hibit; |
|
| 178 | 178 | |
| 179 | 179 | /* h *= r */ |
| 180 | 180 | $d0 = ( |
| 181 | - self::mul($h0, $r0) + |
|
| 182 | - self::mul($h1, $s4) + |
|
| 183 | - self::mul($h2, $s3) + |
|
| 184 | - self::mul($h3, $s2) + |
|
| 185 | - self::mul($h4, $s1) |
|
| 181 | + self::mul( $h0, $r0 ) + |
|
| 182 | + self::mul( $h1, $s4 ) + |
|
| 183 | + self::mul( $h2, $s3 ) + |
|
| 184 | + self::mul( $h3, $s2 ) + |
|
| 185 | + self::mul( $h4, $s1 ) |
|
| 186 | 186 | ); |
| 187 | 187 | |
| 188 | 188 | $d1 = ( |
| 189 | - self::mul($h0, $r1) + |
|
| 190 | - self::mul($h1, $r0) + |
|
| 191 | - self::mul($h2, $s4) + |
|
| 192 | - self::mul($h3, $s3) + |
|
| 193 | - self::mul($h4, $s2) |
|
| 189 | + self::mul( $h0, $r1 ) + |
|
| 190 | + self::mul( $h1, $r0 ) + |
|
| 191 | + self::mul( $h2, $s4 ) + |
|
| 192 | + self::mul( $h3, $s3 ) + |
|
| 193 | + self::mul( $h4, $s2 ) |
|
| 194 | 194 | ); |
| 195 | 195 | |
| 196 | 196 | $d2 = ( |
| 197 | - self::mul($h0, $r2) + |
|
| 198 | - self::mul($h1, $r1) + |
|
| 199 | - self::mul($h2, $r0) + |
|
| 200 | - self::mul($h3, $s4) + |
|
| 201 | - self::mul($h4, $s3) |
|
| 197 | + self::mul( $h0, $r2 ) + |
|
| 198 | + self::mul( $h1, $r1 ) + |
|
| 199 | + self::mul( $h2, $r0 ) + |
|
| 200 | + self::mul( $h3, $s4 ) + |
|
| 201 | + self::mul( $h4, $s3 ) |
|
| 202 | 202 | ); |
| 203 | 203 | |
| 204 | 204 | $d3 = ( |
| 205 | - self::mul($h0, $r3) + |
|
| 206 | - self::mul($h1, $r2) + |
|
| 207 | - self::mul($h2, $r1) + |
|
| 208 | - self::mul($h3, $r0) + |
|
| 209 | - self::mul($h4, $s4) |
|
| 205 | + self::mul( $h0, $r3 ) + |
|
| 206 | + self::mul( $h1, $r2 ) + |
|
| 207 | + self::mul( $h2, $r1 ) + |
|
| 208 | + self::mul( $h3, $r0 ) + |
|
| 209 | + self::mul( $h4, $s4 ) |
|
| 210 | 210 | ); |
| 211 | 211 | |
| 212 | 212 | $d4 = ( |
| 213 | - self::mul($h0, $r4) + |
|
| 214 | - self::mul($h1, $r3) + |
|
| 215 | - self::mul($h2, $r2) + |
|
| 216 | - self::mul($h3, $r1) + |
|
| 217 | - self::mul($h4, $r0) |
|
| 213 | + self::mul( $h0, $r4 ) + |
|
| 214 | + self::mul( $h1, $r3 ) + |
|
| 215 | + self::mul( $h2, $r2 ) + |
|
| 216 | + self::mul( $h3, $r1 ) + |
|
| 217 | + self::mul( $h4, $r0 ) |
|
| 218 | 218 | ); |
| 219 | 219 | |
| 220 | 220 | /* (partial) h %= p */ |
@@ -232,7 +232,7 @@ discard block |
||
| 232 | 232 | $d4 += $c; |
| 233 | 233 | $c = $d4 >> 26; |
| 234 | 234 | $h4 = $d4 & 0x3ffffff; |
| 235 | - $h0 += (int) self::mul($c, 5, 3); |
|
| 235 | + $h0 += (int) self::mul( $c, 5, 3 ); |
|
| 236 | 236 | $c = $h0 >> 26; |
| 237 | 237 | $h0 &= 0x3ffffff; |
| 238 | 238 | $h1 += $c; |
@@ -246,11 +246,11 @@ discard block |
||
| 246 | 246 | } |
| 247 | 247 | |
| 248 | 248 | $this->h = array( |
| 249 | - (int) ($h0 & 0xffffffff), |
|
| 250 | - (int) ($h1 & 0xffffffff), |
|
| 251 | - (int) ($h2 & 0xffffffff), |
|
| 252 | - (int) ($h3 & 0xffffffff), |
|
| 253 | - (int) ($h4 & 0xffffffff) |
|
| 249 | + (int) ( $h0 & 0xffffffff ), |
|
| 250 | + (int) ( $h1 & 0xffffffff ), |
|
| 251 | + (int) ( $h2 & 0xffffffff ), |
|
| 252 | + (int) ( $h3 & 0xffffffff ), |
|
| 253 | + (int) ( $h4 & 0xffffffff ) |
|
| 254 | 254 | ); |
| 255 | 255 | return $this; |
| 256 | 256 | } |
@@ -263,16 +263,16 @@ discard block |
||
| 263 | 263 | public function finish() |
| 264 | 264 | { |
| 265 | 265 | /* process the remaining block */ |
| 266 | - if ($this->leftover) { |
|
| 266 | + if ( $this->leftover ) { |
|
| 267 | 267 | $i = $this->leftover; |
| 268 | 268 | $this->buffer[$i++] = 1; |
| 269 | - for (; $i < ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE; ++$i) { |
|
| 269 | + for ( ; $i < ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE; ++$i ) { |
|
| 270 | 270 | $this->buffer[$i] = 0; |
| 271 | 271 | } |
| 272 | 272 | $this->final = true; |
| 273 | 273 | $this->blocks( |
| 274 | 274 | self::substr( |
| 275 | - static::intArrayToString($this->buffer), |
|
| 275 | + static::intArrayToString( $this->buffer ), |
|
| 276 | 276 | 0, |
| 277 | 277 | ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE |
| 278 | 278 | ), |
@@ -297,7 +297,7 @@ discard block |
||
| 297 | 297 | $h4 += $c; |
| 298 | 298 | $c = $h4 >> 26; |
| 299 | 299 | $h4 &= 0x3ffffff; |
| 300 | - $h0 += self::mul($c, 5, 3); |
|
| 300 | + $h0 += self::mul( $c, 5, 3 ); |
|
| 301 | 301 | $c = $h0 >> 26; |
| 302 | 302 | $h0 &= 0x3ffffff; |
| 303 | 303 | $h1 += $c; |
@@ -315,10 +315,10 @@ discard block |
||
| 315 | 315 | $g3 = $h3 + $c; |
| 316 | 316 | $c = $g3 >> 26; |
| 317 | 317 | $g3 &= 0x3ffffff; |
| 318 | - $g4 = ($h4 + $c - (1 << 26)) & 0xffffffff; |
|
| 318 | + $g4 = ( $h4 + $c - ( 1 << 26 ) ) & 0xffffffff; |
|
| 319 | 319 | |
| 320 | 320 | /* select h if h < p, or h + -p if h >= p */ |
| 321 | - $mask = ($g4 >> 31) - 1; |
|
| 321 | + $mask = ( $g4 >> 31 ) - 1; |
|
| 322 | 322 | |
| 323 | 323 | $g0 &= $mask; |
| 324 | 324 | $g1 &= $mask; |
@@ -327,31 +327,31 @@ discard block |
||
| 327 | 327 | $g4 &= $mask; |
| 328 | 328 | |
| 329 | 329 | $mask = ~$mask & 0xffffffff; |
| 330 | - $h0 = ($h0 & $mask) | $g0; |
|
| 331 | - $h1 = ($h1 & $mask) | $g1; |
|
| 332 | - $h2 = ($h2 & $mask) | $g2; |
|
| 333 | - $h3 = ($h3 & $mask) | $g3; |
|
| 334 | - $h4 = ($h4 & $mask) | $g4; |
|
| 330 | + $h0 = ( $h0 & $mask ) | $g0; |
|
| 331 | + $h1 = ( $h1 & $mask ) | $g1; |
|
| 332 | + $h2 = ( $h2 & $mask ) | $g2; |
|
| 333 | + $h3 = ( $h3 & $mask ) | $g3; |
|
| 334 | + $h4 = ( $h4 & $mask ) | $g4; |
|
| 335 | 335 | |
| 336 | 336 | /* h = h % (2^128) */ |
| 337 | - $h0 = (($h0) | ($h1 << 26)) & 0xffffffff; |
|
| 338 | - $h1 = (($h1 >> 6) | ($h2 << 20)) & 0xffffffff; |
|
| 339 | - $h2 = (($h2 >> 12) | ($h3 << 14)) & 0xffffffff; |
|
| 340 | - $h3 = (($h3 >> 18) | ($h4 << 8)) & 0xffffffff; |
|
| 337 | + $h0 = ( ( $h0 ) | ( $h1 << 26 ) ) & 0xffffffff; |
|
| 338 | + $h1 = ( ( $h1 >> 6 ) | ( $h2 << 20 ) ) & 0xffffffff; |
|
| 339 | + $h2 = ( ( $h2 >> 12 ) | ( $h3 << 14 ) ) & 0xffffffff; |
|
| 340 | + $h3 = ( ( $h3 >> 18 ) | ( $h4 << 8 ) ) & 0xffffffff; |
|
| 341 | 341 | |
| 342 | 342 | /* mac = (h + pad) % (2^128) */ |
| 343 | - $f = ($h0 + $this->pad[0]); |
|
| 343 | + $f = ( $h0 + $this->pad[0] ); |
|
| 344 | 344 | $h0 = (int) $f; |
| 345 | - $f = ($h1 + $this->pad[1] + ($f >> 32)); |
|
| 345 | + $f = ( $h1 + $this->pad[1] + ( $f >> 32 ) ); |
|
| 346 | 346 | $h1 = (int) $f; |
| 347 | - $f = ($h2 + $this->pad[2] + ($f >> 32)); |
|
| 347 | + $f = ( $h2 + $this->pad[2] + ( $f >> 32 ) ); |
|
| 348 | 348 | $h2 = (int) $f; |
| 349 | - $f = ($h3 + $this->pad[3] + ($f >> 32)); |
|
| 349 | + $f = ( $h3 + $this->pad[3] + ( $f >> 32 ) ); |
|
| 350 | 350 | $h3 = (int) $f; |
| 351 | 351 | |
| 352 | - return self::store32_le($h0 & 0xffffffff) . |
|
| 353 | - self::store32_le($h1 & 0xffffffff) . |
|
| 354 | - self::store32_le($h2 & 0xffffffff) . |
|
| 355 | - self::store32_le($h3 & 0xffffffff); |
|
| 352 | + return self::store32_le( $h0 & 0xffffffff ) . |
|
| 353 | + self::store32_le( $h1 & 0xffffffff ) . |
|
| 354 | + self::store32_le( $h2 & 0xffffffff ) . |
|
| 355 | + self::store32_le( $h3 & 0xffffffff ); |
|
| 356 | 356 | } |
| 357 | 357 | } |
@@ -7,8 +7,7 @@ discard block |
||
| 7 | 7 | /** |
| 8 | 8 | * Class ParagonIE_Sodium_Core_Poly1305_State |
| 9 | 9 | */ |
| 10 | -class ParagonIE_Sodium_Core_Poly1305_State extends ParagonIE_Sodium_Core_Util |
|
| 11 | -{ |
|
| 10 | +class ParagonIE_Sodium_Core_Poly1305_State extends ParagonIE_Sodium_Core_Util { |
|
| 12 | 11 | /** |
| 13 | 12 | * @var array<int, int> |
| 14 | 13 | */ |
@@ -47,8 +46,7 @@ discard block |
||
| 47 | 46 | * @param string $key |
| 48 | 47 | * @throws InvalidArgumentException |
| 49 | 48 | */ |
| 50 | - public function __construct($key = '') |
|
| 51 | - { |
|
| 49 | + public function __construct($key = '') { |
|
| 52 | 50 | if (self::strlen($key) < 32) { |
| 53 | 51 | throw new InvalidArgumentException( |
| 54 | 52 | 'Poly1305 requires a 32-byte key' |
@@ -84,8 +82,7 @@ discard block |
||
| 84 | 82 | * @param string $message |
| 85 | 83 | * @return self |
| 86 | 84 | */ |
| 87 | - public function update($message = '') |
|
| 88 | - { |
|
| 85 | + public function update($message = '') { |
|
| 89 | 86 | $bytes = self::strlen($message); |
| 90 | 87 | |
| 91 | 88 | /* handle leftover */ |
@@ -145,8 +142,7 @@ discard block |
||
| 145 | 142 | * @param int $bytes |
| 146 | 143 | * @return self |
| 147 | 144 | */ |
| 148 | - public function blocks($message, $bytes) |
|
| 149 | - { |
|
| 145 | + public function blocks($message, $bytes) { |
|
| 150 | 146 | if (self::strlen($message) < 16) { |
| 151 | 147 | $message = str_pad($message, 16, "\x00", STR_PAD_RIGHT); |
| 152 | 148 | } |
@@ -260,8 +256,7 @@ discard block |
||
| 260 | 256 | * |
| 261 | 257 | * @return string |
| 262 | 258 | */ |
| 263 | - public function finish() |
|
| 264 | - { |
|
| 259 | + public function finish() { |
|
| 265 | 260 | /* process the remaining block */ |
| 266 | 261 | if ($this->leftover) { |
| 267 | 262 | $i = $this->leftover; |
@@ -1,7 +1,7 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | if (class_exists('ParagonIE_Sodium_Core_Curve25519_Ge_P3', false)) { |
| 4 | - return; |
|
| 4 | + return; |
|
| 5 | 5 | } |
| 6 | 6 | |
| 7 | 7 | /** |
@@ -9,57 +9,57 @@ discard block |
||
| 9 | 9 | */ |
| 10 | 10 | class ParagonIE_Sodium_Core_Curve25519_Ge_P3 |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * @var ParagonIE_Sodium_Core_Curve25519_Fe |
|
| 14 | - */ |
|
| 15 | - public $X; |
|
| 12 | + /** |
|
| 13 | + * @var ParagonIE_Sodium_Core_Curve25519_Fe |
|
| 14 | + */ |
|
| 15 | + public $X; |
|
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * @var ParagonIE_Sodium_Core_Curve25519_Fe |
|
| 19 | - */ |
|
| 20 | - public $Y; |
|
| 17 | + /** |
|
| 18 | + * @var ParagonIE_Sodium_Core_Curve25519_Fe |
|
| 19 | + */ |
|
| 20 | + public $Y; |
|
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * @var ParagonIE_Sodium_Core_Curve25519_Fe |
|
| 24 | - */ |
|
| 25 | - public $Z; |
|
| 22 | + /** |
|
| 23 | + * @var ParagonIE_Sodium_Core_Curve25519_Fe |
|
| 24 | + */ |
|
| 25 | + public $Z; |
|
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * @var ParagonIE_Sodium_Core_Curve25519_Fe |
|
| 29 | - */ |
|
| 30 | - public $T; |
|
| 27 | + /** |
|
| 28 | + * @var ParagonIE_Sodium_Core_Curve25519_Fe |
|
| 29 | + */ |
|
| 30 | + public $T; |
|
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * ParagonIE_Sodium_Core_Curve25519_Ge_P3 constructor. |
|
| 34 | - * |
|
| 35 | - * @internal You should not use this directly from another application |
|
| 36 | - * |
|
| 37 | - * @param ParagonIE_Sodium_Core_Curve25519_Fe|null $x |
|
| 38 | - * @param ParagonIE_Sodium_Core_Curve25519_Fe|null $y |
|
| 39 | - * @param ParagonIE_Sodium_Core_Curve25519_Fe|null $z |
|
| 40 | - * @param ParagonIE_Sodium_Core_Curve25519_Fe|null $t |
|
| 41 | - */ |
|
| 42 | - public function __construct( |
|
| 43 | - ParagonIE_Sodium_Core_Curve25519_Fe $x = null, |
|
| 44 | - ParagonIE_Sodium_Core_Curve25519_Fe $y = null, |
|
| 45 | - ParagonIE_Sodium_Core_Curve25519_Fe $z = null, |
|
| 46 | - ParagonIE_Sodium_Core_Curve25519_Fe $t = null |
|
| 47 | - ) { |
|
| 48 | - if ($x === null) { |
|
| 49 | - $x = new ParagonIE_Sodium_Core_Curve25519_Fe(); |
|
| 50 | - } |
|
| 51 | - $this->X = $x; |
|
| 52 | - if ($y === null) { |
|
| 53 | - $y = new ParagonIE_Sodium_Core_Curve25519_Fe(); |
|
| 54 | - } |
|
| 55 | - $this->Y = $y; |
|
| 56 | - if ($z === null) { |
|
| 57 | - $z = new ParagonIE_Sodium_Core_Curve25519_Fe(); |
|
| 58 | - } |
|
| 59 | - $this->Z = $z; |
|
| 60 | - if ($t === null) { |
|
| 61 | - $t = new ParagonIE_Sodium_Core_Curve25519_Fe(); |
|
| 62 | - } |
|
| 63 | - $this->T = $t; |
|
| 64 | - } |
|
| 32 | + /** |
|
| 33 | + * ParagonIE_Sodium_Core_Curve25519_Ge_P3 constructor. |
|
| 34 | + * |
|
| 35 | + * @internal You should not use this directly from another application |
|
| 36 | + * |
|
| 37 | + * @param ParagonIE_Sodium_Core_Curve25519_Fe|null $x |
|
| 38 | + * @param ParagonIE_Sodium_Core_Curve25519_Fe|null $y |
|
| 39 | + * @param ParagonIE_Sodium_Core_Curve25519_Fe|null $z |
|
| 40 | + * @param ParagonIE_Sodium_Core_Curve25519_Fe|null $t |
|
| 41 | + */ |
|
| 42 | + public function __construct( |
|
| 43 | + ParagonIE_Sodium_Core_Curve25519_Fe $x = null, |
|
| 44 | + ParagonIE_Sodium_Core_Curve25519_Fe $y = null, |
|
| 45 | + ParagonIE_Sodium_Core_Curve25519_Fe $z = null, |
|
| 46 | + ParagonIE_Sodium_Core_Curve25519_Fe $t = null |
|
| 47 | + ) { |
|
| 48 | + if ($x === null) { |
|
| 49 | + $x = new ParagonIE_Sodium_Core_Curve25519_Fe(); |
|
| 50 | + } |
|
| 51 | + $this->X = $x; |
|
| 52 | + if ($y === null) { |
|
| 53 | + $y = new ParagonIE_Sodium_Core_Curve25519_Fe(); |
|
| 54 | + } |
|
| 55 | + $this->Y = $y; |
|
| 56 | + if ($z === null) { |
|
| 57 | + $z = new ParagonIE_Sodium_Core_Curve25519_Fe(); |
|
| 58 | + } |
|
| 59 | + $this->Z = $z; |
|
| 60 | + if ($t === null) { |
|
| 61 | + $t = new ParagonIE_Sodium_Core_Curve25519_Fe(); |
|
| 62 | + } |
|
| 63 | + $this->T = $t; |
|
| 64 | + } |
|
| 65 | 65 | } |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -if (class_exists('ParagonIE_Sodium_Core_Curve25519_Ge_P3', false)) { |
|
| 3 | +if ( class_exists( 'ParagonIE_Sodium_Core_Curve25519_Ge_P3', false ) ) { |
|
| 4 | 4 | return; |
| 5 | 5 | } |
| 6 | 6 | |
@@ -45,19 +45,19 @@ discard block |
||
| 45 | 45 | ParagonIE_Sodium_Core_Curve25519_Fe $z = null, |
| 46 | 46 | ParagonIE_Sodium_Core_Curve25519_Fe $t = null |
| 47 | 47 | ) { |
| 48 | - if ($x === null) { |
|
| 48 | + if ( $x === null ) { |
|
| 49 | 49 | $x = new ParagonIE_Sodium_Core_Curve25519_Fe(); |
| 50 | 50 | } |
| 51 | 51 | $this->X = $x; |
| 52 | - if ($y === null) { |
|
| 52 | + if ( $y === null ) { |
|
| 53 | 53 | $y = new ParagonIE_Sodium_Core_Curve25519_Fe(); |
| 54 | 54 | } |
| 55 | 55 | $this->Y = $y; |
| 56 | - if ($z === null) { |
|
| 56 | + if ( $z === null ) { |
|
| 57 | 57 | $z = new ParagonIE_Sodium_Core_Curve25519_Fe(); |
| 58 | 58 | } |
| 59 | 59 | $this->Z = $z; |
| 60 | - if ($t === null) { |
|
| 60 | + if ( $t === null ) { |
|
| 61 | 61 | $t = new ParagonIE_Sodium_Core_Curve25519_Fe(); |
| 62 | 62 | } |
| 63 | 63 | $this->T = $t; |
@@ -7,8 +7,7 @@ |
||
| 7 | 7 | /** |
| 8 | 8 | * Class ParagonIE_Sodium_Core_Curve25519_Ge_P3 |
| 9 | 9 | */ |
| 10 | -class ParagonIE_Sodium_Core_Curve25519_Ge_P3 |
|
| 11 | -{ |
|
| 10 | +class ParagonIE_Sodium_Core_Curve25519_Ge_P3 { |
|
| 12 | 11 | /** |
| 13 | 12 | * @var ParagonIE_Sodium_Core_Curve25519_Fe |
| 14 | 13 | */ |
@@ -2,64 +2,64 @@ |
||
| 2 | 2 | |
| 3 | 3 | |
| 4 | 4 | if (class_exists('ParagonIE_Sodium_Core_Curve25519_Ge_Cached', false)) { |
| 5 | - return; |
|
| 5 | + return; |
|
| 6 | 6 | } |
| 7 | 7 | /** |
| 8 | 8 | * Class ParagonIE_Sodium_Core_Curve25519_Ge_Cached |
| 9 | 9 | */ |
| 10 | 10 | class ParagonIE_Sodium_Core_Curve25519_Ge_Cached |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * @var ParagonIE_Sodium_Core_Curve25519_Fe |
|
| 14 | - */ |
|
| 15 | - public $YplusX; |
|
| 12 | + /** |
|
| 13 | + * @var ParagonIE_Sodium_Core_Curve25519_Fe |
|
| 14 | + */ |
|
| 15 | + public $YplusX; |
|
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * @var ParagonIE_Sodium_Core_Curve25519_Fe |
|
| 19 | - */ |
|
| 20 | - public $YminusX; |
|
| 17 | + /** |
|
| 18 | + * @var ParagonIE_Sodium_Core_Curve25519_Fe |
|
| 19 | + */ |
|
| 20 | + public $YminusX; |
|
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * @var ParagonIE_Sodium_Core_Curve25519_Fe |
|
| 24 | - */ |
|
| 25 | - public $Z; |
|
| 22 | + /** |
|
| 23 | + * @var ParagonIE_Sodium_Core_Curve25519_Fe |
|
| 24 | + */ |
|
| 25 | + public $Z; |
|
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * @var ParagonIE_Sodium_Core_Curve25519_Fe |
|
| 29 | - */ |
|
| 30 | - public $T2d; |
|
| 27 | + /** |
|
| 28 | + * @var ParagonIE_Sodium_Core_Curve25519_Fe |
|
| 29 | + */ |
|
| 30 | + public $T2d; |
|
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * ParagonIE_Sodium_Core_Curve25519_Ge_Cached constructor. |
|
| 34 | - * |
|
| 35 | - * @internal You should not use this directly from another application |
|
| 36 | - * |
|
| 37 | - * @param ParagonIE_Sodium_Core_Curve25519_Fe|null $YplusX |
|
| 38 | - * @param ParagonIE_Sodium_Core_Curve25519_Fe|null $YminusX |
|
| 39 | - * @param ParagonIE_Sodium_Core_Curve25519_Fe|null $Z |
|
| 40 | - * @param ParagonIE_Sodium_Core_Curve25519_Fe|null $T2d |
|
| 41 | - */ |
|
| 42 | - public function __construct( |
|
| 43 | - ParagonIE_Sodium_Core_Curve25519_Fe $YplusX = null, |
|
| 44 | - ParagonIE_Sodium_Core_Curve25519_Fe $YminusX = null, |
|
| 45 | - ParagonIE_Sodium_Core_Curve25519_Fe $Z = null, |
|
| 46 | - ParagonIE_Sodium_Core_Curve25519_Fe $T2d = null |
|
| 47 | - ) { |
|
| 48 | - if ($YplusX === null) { |
|
| 49 | - $YplusX = new ParagonIE_Sodium_Core_Curve25519_Fe(); |
|
| 50 | - } |
|
| 51 | - $this->YplusX = $YplusX; |
|
| 52 | - if ($YminusX === null) { |
|
| 53 | - $YminusX = new ParagonIE_Sodium_Core_Curve25519_Fe(); |
|
| 54 | - } |
|
| 55 | - $this->YminusX = $YminusX; |
|
| 56 | - if ($Z === null) { |
|
| 57 | - $Z = new ParagonIE_Sodium_Core_Curve25519_Fe(); |
|
| 58 | - } |
|
| 59 | - $this->Z = $Z; |
|
| 60 | - if ($T2d === null) { |
|
| 61 | - $T2d = new ParagonIE_Sodium_Core_Curve25519_Fe(); |
|
| 62 | - } |
|
| 63 | - $this->T2d = $T2d; |
|
| 64 | - } |
|
| 32 | + /** |
|
| 33 | + * ParagonIE_Sodium_Core_Curve25519_Ge_Cached constructor. |
|
| 34 | + * |
|
| 35 | + * @internal You should not use this directly from another application |
|
| 36 | + * |
|
| 37 | + * @param ParagonIE_Sodium_Core_Curve25519_Fe|null $YplusX |
|
| 38 | + * @param ParagonIE_Sodium_Core_Curve25519_Fe|null $YminusX |
|
| 39 | + * @param ParagonIE_Sodium_Core_Curve25519_Fe|null $Z |
|
| 40 | + * @param ParagonIE_Sodium_Core_Curve25519_Fe|null $T2d |
|
| 41 | + */ |
|
| 42 | + public function __construct( |
|
| 43 | + ParagonIE_Sodium_Core_Curve25519_Fe $YplusX = null, |
|
| 44 | + ParagonIE_Sodium_Core_Curve25519_Fe $YminusX = null, |
|
| 45 | + ParagonIE_Sodium_Core_Curve25519_Fe $Z = null, |
|
| 46 | + ParagonIE_Sodium_Core_Curve25519_Fe $T2d = null |
|
| 47 | + ) { |
|
| 48 | + if ($YplusX === null) { |
|
| 49 | + $YplusX = new ParagonIE_Sodium_Core_Curve25519_Fe(); |
|
| 50 | + } |
|
| 51 | + $this->YplusX = $YplusX; |
|
| 52 | + if ($YminusX === null) { |
|
| 53 | + $YminusX = new ParagonIE_Sodium_Core_Curve25519_Fe(); |
|
| 54 | + } |
|
| 55 | + $this->YminusX = $YminusX; |
|
| 56 | + if ($Z === null) { |
|
| 57 | + $Z = new ParagonIE_Sodium_Core_Curve25519_Fe(); |
|
| 58 | + } |
|
| 59 | + $this->Z = $Z; |
|
| 60 | + if ($T2d === null) { |
|
| 61 | + $T2d = new ParagonIE_Sodium_Core_Curve25519_Fe(); |
|
| 62 | + } |
|
| 63 | + $this->T2d = $T2d; |
|
| 64 | + } |
|
| 65 | 65 | } |
@@ -1,7 +1,7 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | |
| 4 | -if (class_exists('ParagonIE_Sodium_Core_Curve25519_Ge_Cached', false)) { |
|
| 4 | +if ( class_exists( 'ParagonIE_Sodium_Core_Curve25519_Ge_Cached', false ) ) { |
|
| 5 | 5 | return; |
| 6 | 6 | } |
| 7 | 7 | /** |
@@ -45,19 +45,19 @@ discard block |
||
| 45 | 45 | ParagonIE_Sodium_Core_Curve25519_Fe $Z = null, |
| 46 | 46 | ParagonIE_Sodium_Core_Curve25519_Fe $T2d = null |
| 47 | 47 | ) { |
| 48 | - if ($YplusX === null) { |
|
| 48 | + if ( $YplusX === null ) { |
|
| 49 | 49 | $YplusX = new ParagonIE_Sodium_Core_Curve25519_Fe(); |
| 50 | 50 | } |
| 51 | 51 | $this->YplusX = $YplusX; |
| 52 | - if ($YminusX === null) { |
|
| 52 | + if ( $YminusX === null ) { |
|
| 53 | 53 | $YminusX = new ParagonIE_Sodium_Core_Curve25519_Fe(); |
| 54 | 54 | } |
| 55 | 55 | $this->YminusX = $YminusX; |
| 56 | - if ($Z === null) { |
|
| 56 | + if ( $Z === null ) { |
|
| 57 | 57 | $Z = new ParagonIE_Sodium_Core_Curve25519_Fe(); |
| 58 | 58 | } |
| 59 | 59 | $this->Z = $Z; |
| 60 | - if ($T2d === null) { |
|
| 60 | + if ( $T2d === null ) { |
|
| 61 | 61 | $T2d = new ParagonIE_Sodium_Core_Curve25519_Fe(); |
| 62 | 62 | } |
| 63 | 63 | $this->T2d = $T2d; |
@@ -7,8 +7,7 @@ |
||
| 7 | 7 | /** |
| 8 | 8 | * Class ParagonIE_Sodium_Core_Curve25519_Ge_Cached |
| 9 | 9 | */ |
| 10 | -class ParagonIE_Sodium_Core_Curve25519_Ge_Cached |
|
| 11 | -{ |
|
| 10 | +class ParagonIE_Sodium_Core_Curve25519_Ge_Cached { |
|
| 12 | 11 | /** |
| 13 | 12 | * @var ParagonIE_Sodium_Core_Curve25519_Fe |
| 14 | 13 | */ |
@@ -1,64 +1,64 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | if (class_exists('ParagonIE_Sodium_Core_Curve25519_Ge_P1p1', false)) { |
| 4 | - return; |
|
| 4 | + return; |
|
| 5 | 5 | } |
| 6 | 6 | /** |
| 7 | 7 | * Class ParagonIE_Sodium_Core_Curve25519_Ge_P1p1 |
| 8 | 8 | */ |
| 9 | 9 | class ParagonIE_Sodium_Core_Curve25519_Ge_P1p1 |
| 10 | 10 | { |
| 11 | - /** |
|
| 12 | - * @var ParagonIE_Sodium_Core_Curve25519_Fe |
|
| 13 | - */ |
|
| 14 | - public $X; |
|
| 11 | + /** |
|
| 12 | + * @var ParagonIE_Sodium_Core_Curve25519_Fe |
|
| 13 | + */ |
|
| 14 | + public $X; |
|
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * @var ParagonIE_Sodium_Core_Curve25519_Fe |
|
| 18 | - */ |
|
| 19 | - public $Y; |
|
| 16 | + /** |
|
| 17 | + * @var ParagonIE_Sodium_Core_Curve25519_Fe |
|
| 18 | + */ |
|
| 19 | + public $Y; |
|
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * @var ParagonIE_Sodium_Core_Curve25519_Fe |
|
| 23 | - */ |
|
| 24 | - public $Z; |
|
| 21 | + /** |
|
| 22 | + * @var ParagonIE_Sodium_Core_Curve25519_Fe |
|
| 23 | + */ |
|
| 24 | + public $Z; |
|
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * @var ParagonIE_Sodium_Core_Curve25519_Fe |
|
| 28 | - */ |
|
| 29 | - public $T; |
|
| 26 | + /** |
|
| 27 | + * @var ParagonIE_Sodium_Core_Curve25519_Fe |
|
| 28 | + */ |
|
| 29 | + public $T; |
|
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * ParagonIE_Sodium_Core_Curve25519_Ge_P1p1 constructor. |
|
| 33 | - * |
|
| 34 | - * @internal You should not use this directly from another application |
|
| 35 | - * |
|
| 36 | - * @param ParagonIE_Sodium_Core_Curve25519_Fe|null $x |
|
| 37 | - * @param ParagonIE_Sodium_Core_Curve25519_Fe|null $y |
|
| 38 | - * @param ParagonIE_Sodium_Core_Curve25519_Fe|null $z |
|
| 39 | - * @param ParagonIE_Sodium_Core_Curve25519_Fe|null $t |
|
| 40 | - */ |
|
| 41 | - public function __construct( |
|
| 42 | - ParagonIE_Sodium_Core_Curve25519_Fe $x = null, |
|
| 43 | - ParagonIE_Sodium_Core_Curve25519_Fe $y = null, |
|
| 44 | - ParagonIE_Sodium_Core_Curve25519_Fe $z = null, |
|
| 45 | - ParagonIE_Sodium_Core_Curve25519_Fe $t = null |
|
| 46 | - ) { |
|
| 47 | - if ($x === null) { |
|
| 48 | - $x = new ParagonIE_Sodium_Core_Curve25519_Fe(); |
|
| 49 | - } |
|
| 50 | - $this->X = $x; |
|
| 51 | - if ($y === null) { |
|
| 52 | - $y = new ParagonIE_Sodium_Core_Curve25519_Fe(); |
|
| 53 | - } |
|
| 54 | - $this->Y = $y; |
|
| 55 | - if ($z === null) { |
|
| 56 | - $z = new ParagonIE_Sodium_Core_Curve25519_Fe(); |
|
| 57 | - } |
|
| 58 | - $this->Z = $z; |
|
| 59 | - if ($t === null) { |
|
| 60 | - $t = new ParagonIE_Sodium_Core_Curve25519_Fe(); |
|
| 61 | - } |
|
| 62 | - $this->T = $t; |
|
| 63 | - } |
|
| 31 | + /** |
|
| 32 | + * ParagonIE_Sodium_Core_Curve25519_Ge_P1p1 constructor. |
|
| 33 | + * |
|
| 34 | + * @internal You should not use this directly from another application |
|
| 35 | + * |
|
| 36 | + * @param ParagonIE_Sodium_Core_Curve25519_Fe|null $x |
|
| 37 | + * @param ParagonIE_Sodium_Core_Curve25519_Fe|null $y |
|
| 38 | + * @param ParagonIE_Sodium_Core_Curve25519_Fe|null $z |
|
| 39 | + * @param ParagonIE_Sodium_Core_Curve25519_Fe|null $t |
|
| 40 | + */ |
|
| 41 | + public function __construct( |
|
| 42 | + ParagonIE_Sodium_Core_Curve25519_Fe $x = null, |
|
| 43 | + ParagonIE_Sodium_Core_Curve25519_Fe $y = null, |
|
| 44 | + ParagonIE_Sodium_Core_Curve25519_Fe $z = null, |
|
| 45 | + ParagonIE_Sodium_Core_Curve25519_Fe $t = null |
|
| 46 | + ) { |
|
| 47 | + if ($x === null) { |
|
| 48 | + $x = new ParagonIE_Sodium_Core_Curve25519_Fe(); |
|
| 49 | + } |
|
| 50 | + $this->X = $x; |
|
| 51 | + if ($y === null) { |
|
| 52 | + $y = new ParagonIE_Sodium_Core_Curve25519_Fe(); |
|
| 53 | + } |
|
| 54 | + $this->Y = $y; |
|
| 55 | + if ($z === null) { |
|
| 56 | + $z = new ParagonIE_Sodium_Core_Curve25519_Fe(); |
|
| 57 | + } |
|
| 58 | + $this->Z = $z; |
|
| 59 | + if ($t === null) { |
|
| 60 | + $t = new ParagonIE_Sodium_Core_Curve25519_Fe(); |
|
| 61 | + } |
|
| 62 | + $this->T = $t; |
|
| 63 | + } |
|
| 64 | 64 | } |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -if (class_exists('ParagonIE_Sodium_Core_Curve25519_Ge_P1p1', false)) { |
|
| 3 | +if ( class_exists( 'ParagonIE_Sodium_Core_Curve25519_Ge_P1p1', false ) ) { |
|
| 4 | 4 | return; |
| 5 | 5 | } |
| 6 | 6 | /** |
@@ -44,19 +44,19 @@ discard block |
||
| 44 | 44 | ParagonIE_Sodium_Core_Curve25519_Fe $z = null, |
| 45 | 45 | ParagonIE_Sodium_Core_Curve25519_Fe $t = null |
| 46 | 46 | ) { |
| 47 | - if ($x === null) { |
|
| 47 | + if ( $x === null ) { |
|
| 48 | 48 | $x = new ParagonIE_Sodium_Core_Curve25519_Fe(); |
| 49 | 49 | } |
| 50 | 50 | $this->X = $x; |
| 51 | - if ($y === null) { |
|
| 51 | + if ( $y === null ) { |
|
| 52 | 52 | $y = new ParagonIE_Sodium_Core_Curve25519_Fe(); |
| 53 | 53 | } |
| 54 | 54 | $this->Y = $y; |
| 55 | - if ($z === null) { |
|
| 55 | + if ( $z === null ) { |
|
| 56 | 56 | $z = new ParagonIE_Sodium_Core_Curve25519_Fe(); |
| 57 | 57 | } |
| 58 | 58 | $this->Z = $z; |
| 59 | - if ($t === null) { |
|
| 59 | + if ( $t === null ) { |
|
| 60 | 60 | $t = new ParagonIE_Sodium_Core_Curve25519_Fe(); |
| 61 | 61 | } |
| 62 | 62 | $this->T = $t; |
@@ -6,8 +6,7 @@ |
||
| 6 | 6 | /** |
| 7 | 7 | * Class ParagonIE_Sodium_Core_Curve25519_Ge_P1p1 |
| 8 | 8 | */ |
| 9 | -class ParagonIE_Sodium_Core_Curve25519_Ge_P1p1 |
|
| 10 | -{ |
|
| 9 | +class ParagonIE_Sodium_Core_Curve25519_Ge_P1p1 { |
|
| 11 | 10 | /** |
| 12 | 11 | * @var ParagonIE_Sodium_Core_Curve25519_Fe |
| 13 | 12 | */ |
@@ -1,7 +1,7 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | if (class_exists('ParagonIE_Sodium_Core_Curve25519_Ge_Precomp', false)) { |
| 4 | - return; |
|
| 4 | + return; |
|
| 5 | 5 | } |
| 6 | 6 | |
| 7 | 7 | /** |
@@ -9,46 +9,46 @@ discard block |
||
| 9 | 9 | */ |
| 10 | 10 | class ParagonIE_Sodium_Core_Curve25519_Ge_Precomp |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * @var ParagonIE_Sodium_Core_Curve25519_Fe |
|
| 14 | - */ |
|
| 15 | - public $yplusx; |
|
| 12 | + /** |
|
| 13 | + * @var ParagonIE_Sodium_Core_Curve25519_Fe |
|
| 14 | + */ |
|
| 15 | + public $yplusx; |
|
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * @var ParagonIE_Sodium_Core_Curve25519_Fe |
|
| 19 | - */ |
|
| 20 | - public $yminusx; |
|
| 17 | + /** |
|
| 18 | + * @var ParagonIE_Sodium_Core_Curve25519_Fe |
|
| 19 | + */ |
|
| 20 | + public $yminusx; |
|
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * @var ParagonIE_Sodium_Core_Curve25519_Fe |
|
| 24 | - */ |
|
| 25 | - public $xy2d; |
|
| 22 | + /** |
|
| 23 | + * @var ParagonIE_Sodium_Core_Curve25519_Fe |
|
| 24 | + */ |
|
| 25 | + public $xy2d; |
|
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * ParagonIE_Sodium_Core_Curve25519_Ge_Precomp constructor. |
|
| 29 | - * |
|
| 30 | - * @internal You should not use this directly from another application |
|
| 31 | - * |
|
| 32 | - * @param ParagonIE_Sodium_Core_Curve25519_Fe $yplusx |
|
| 33 | - * @param ParagonIE_Sodium_Core_Curve25519_Fe $yminusx |
|
| 34 | - * @param ParagonIE_Sodium_Core_Curve25519_Fe $xy2d |
|
| 35 | - */ |
|
| 36 | - public function __construct( |
|
| 37 | - ParagonIE_Sodium_Core_Curve25519_Fe $yplusx = null, |
|
| 38 | - ParagonIE_Sodium_Core_Curve25519_Fe $yminusx = null, |
|
| 39 | - ParagonIE_Sodium_Core_Curve25519_Fe $xy2d = null |
|
| 40 | - ) { |
|
| 41 | - if ($yplusx === null) { |
|
| 42 | - $yplusx = new ParagonIE_Sodium_Core_Curve25519_Fe(); |
|
| 43 | - } |
|
| 44 | - $this->yplusx = $yplusx; |
|
| 45 | - if ($yminusx === null) { |
|
| 46 | - $yminusx = new ParagonIE_Sodium_Core_Curve25519_Fe(); |
|
| 47 | - } |
|
| 48 | - $this->yminusx = $yminusx; |
|
| 49 | - if ($xy2d === null) { |
|
| 50 | - $xy2d = new ParagonIE_Sodium_Core_Curve25519_Fe(); |
|
| 51 | - } |
|
| 52 | - $this->xy2d = $xy2d; |
|
| 53 | - } |
|
| 27 | + /** |
|
| 28 | + * ParagonIE_Sodium_Core_Curve25519_Ge_Precomp constructor. |
|
| 29 | + * |
|
| 30 | + * @internal You should not use this directly from another application |
|
| 31 | + * |
|
| 32 | + * @param ParagonIE_Sodium_Core_Curve25519_Fe $yplusx |
|
| 33 | + * @param ParagonIE_Sodium_Core_Curve25519_Fe $yminusx |
|
| 34 | + * @param ParagonIE_Sodium_Core_Curve25519_Fe $xy2d |
|
| 35 | + */ |
|
| 36 | + public function __construct( |
|
| 37 | + ParagonIE_Sodium_Core_Curve25519_Fe $yplusx = null, |
|
| 38 | + ParagonIE_Sodium_Core_Curve25519_Fe $yminusx = null, |
|
| 39 | + ParagonIE_Sodium_Core_Curve25519_Fe $xy2d = null |
|
| 40 | + ) { |
|
| 41 | + if ($yplusx === null) { |
|
| 42 | + $yplusx = new ParagonIE_Sodium_Core_Curve25519_Fe(); |
|
| 43 | + } |
|
| 44 | + $this->yplusx = $yplusx; |
|
| 45 | + if ($yminusx === null) { |
|
| 46 | + $yminusx = new ParagonIE_Sodium_Core_Curve25519_Fe(); |
|
| 47 | + } |
|
| 48 | + $this->yminusx = $yminusx; |
|
| 49 | + if ($xy2d === null) { |
|
| 50 | + $xy2d = new ParagonIE_Sodium_Core_Curve25519_Fe(); |
|
| 51 | + } |
|
| 52 | + $this->xy2d = $xy2d; |
|
| 53 | + } |
|
| 54 | 54 | } |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -if (class_exists('ParagonIE_Sodium_Core_Curve25519_Ge_Precomp', false)) { |
|
| 3 | +if ( class_exists( 'ParagonIE_Sodium_Core_Curve25519_Ge_Precomp', false ) ) { |
|
| 4 | 4 | return; |
| 5 | 5 | } |
| 6 | 6 | |
@@ -38,15 +38,15 @@ discard block |
||
| 38 | 38 | ParagonIE_Sodium_Core_Curve25519_Fe $yminusx = null, |
| 39 | 39 | ParagonIE_Sodium_Core_Curve25519_Fe $xy2d = null |
| 40 | 40 | ) { |
| 41 | - if ($yplusx === null) { |
|
| 41 | + if ( $yplusx === null ) { |
|
| 42 | 42 | $yplusx = new ParagonIE_Sodium_Core_Curve25519_Fe(); |
| 43 | 43 | } |
| 44 | 44 | $this->yplusx = $yplusx; |
| 45 | - if ($yminusx === null) { |
|
| 45 | + if ( $yminusx === null ) { |
|
| 46 | 46 | $yminusx = new ParagonIE_Sodium_Core_Curve25519_Fe(); |
| 47 | 47 | } |
| 48 | 48 | $this->yminusx = $yminusx; |
| 49 | - if ($xy2d === null) { |
|
| 49 | + if ( $xy2d === null ) { |
|
| 50 | 50 | $xy2d = new ParagonIE_Sodium_Core_Curve25519_Fe(); |
| 51 | 51 | } |
| 52 | 52 | $this->xy2d = $xy2d; |
@@ -7,8 +7,7 @@ |
||
| 7 | 7 | /** |
| 8 | 8 | * Class ParagonIE_Sodium_Core_Curve25519_Ge_Precomp |
| 9 | 9 | */ |
| 10 | -class ParagonIE_Sodium_Core_Curve25519_Ge_Precomp |
|
| 11 | -{ |
|
| 10 | +class ParagonIE_Sodium_Core_Curve25519_Ge_Precomp { |
|
| 12 | 11 | /** |
| 13 | 12 | * @var ParagonIE_Sodium_Core_Curve25519_Fe |
| 14 | 13 | */ |