@@ -1,7 +1,7 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | if (class_exists('ParagonIE_Sodium_Core32_Salsa20', false)) { |
4 | - return; |
|
4 | + return; |
|
5 | 5 | } |
6 | 6 | |
7 | 7 | /** |
@@ -9,290 +9,290 @@ discard block |
||
9 | 9 | */ |
10 | 10 | abstract class ParagonIE_Sodium_Core32_Salsa20 extends ParagonIE_Sodium_Core32_Util |
11 | 11 | { |
12 | - const ROUNDS = 20; |
|
12 | + const ROUNDS = 20; |
|
13 | 13 | |
14 | - /** |
|
15 | - * Calculate an salsa20 hash of a single block |
|
16 | - * |
|
17 | - * @internal You should not use this directly from another application |
|
18 | - * |
|
19 | - * @param string $in |
|
20 | - * @param string $k |
|
21 | - * @param string|null $c |
|
22 | - * @return string |
|
23 | - */ |
|
24 | - public static function core_salsa20($in, $k, $c = null) |
|
25 | - { |
|
26 | - /** |
|
27 | - * @var ParagonIE_Sodium_Core32_Int32 $x0 |
|
28 | - * @var ParagonIE_Sodium_Core32_Int32 $x1 |
|
29 | - * @var ParagonIE_Sodium_Core32_Int32 $x2 |
|
30 | - * @var ParagonIE_Sodium_Core32_Int32 $x3 |
|
31 | - * @var ParagonIE_Sodium_Core32_Int32 $x4 |
|
32 | - * @var ParagonIE_Sodium_Core32_Int32 $x5 |
|
33 | - * @var ParagonIE_Sodium_Core32_Int32 $x6 |
|
34 | - * @var ParagonIE_Sodium_Core32_Int32 $x7 |
|
35 | - * @var ParagonIE_Sodium_Core32_Int32 $x8 |
|
36 | - * @var ParagonIE_Sodium_Core32_Int32 $x9 |
|
37 | - * @var ParagonIE_Sodium_Core32_Int32 $x10 |
|
38 | - * @var ParagonIE_Sodium_Core32_Int32 $x11 |
|
39 | - * @var ParagonIE_Sodium_Core32_Int32 $x12 |
|
40 | - * @var ParagonIE_Sodium_Core32_Int32 $x13 |
|
41 | - * @var ParagonIE_Sodium_Core32_Int32 $x14 |
|
42 | - * @var ParagonIE_Sodium_Core32_Int32 $x15 |
|
43 | - * @var ParagonIE_Sodium_Core32_Int32 $j0 |
|
44 | - * @var ParagonIE_Sodium_Core32_Int32 $j1 |
|
45 | - * @var ParagonIE_Sodium_Core32_Int32 $j2 |
|
46 | - * @var ParagonIE_Sodium_Core32_Int32 $j3 |
|
47 | - * @var ParagonIE_Sodium_Core32_Int32 $j4 |
|
48 | - * @var ParagonIE_Sodium_Core32_Int32 $j5 |
|
49 | - * @var ParagonIE_Sodium_Core32_Int32 $j6 |
|
50 | - * @var ParagonIE_Sodium_Core32_Int32 $j7 |
|
51 | - * @var ParagonIE_Sodium_Core32_Int32 $j8 |
|
52 | - * @var ParagonIE_Sodium_Core32_Int32 $j9 |
|
53 | - * @var ParagonIE_Sodium_Core32_Int32 $j10 |
|
54 | - * @var ParagonIE_Sodium_Core32_Int32 $j11 |
|
55 | - * @var ParagonIE_Sodium_Core32_Int32 $j12 |
|
56 | - * @var ParagonIE_Sodium_Core32_Int32 $j13 |
|
57 | - * @var ParagonIE_Sodium_Core32_Int32 $j14 |
|
58 | - * @var ParagonIE_Sodium_Core32_Int32 $j15 |
|
59 | - */ |
|
60 | - if (self::strlen($k) < 32) { |
|
61 | - throw new RangeException('Key must be 32 bytes long'); |
|
62 | - } |
|
63 | - if ($c === null) { |
|
64 | - $x0 = new ParagonIE_Sodium_Core32_Int32(array(0x6170, 0x7865)); |
|
65 | - $x5 = new ParagonIE_Sodium_Core32_Int32(array(0x3320, 0x646e)); |
|
66 | - $x10 = new ParagonIE_Sodium_Core32_Int32(array(0x7962, 0x2d32)); |
|
67 | - $x15 = new ParagonIE_Sodium_Core32_Int32(array(0x6b20, 0x6574)); |
|
68 | - } else { |
|
69 | - $x0 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 0, 4)); |
|
70 | - $x5 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 4, 4)); |
|
71 | - $x10 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 8, 4)); |
|
72 | - $x15 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 12, 4)); |
|
73 | - } |
|
74 | - $x1 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 0, 4)); |
|
75 | - $x2 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 4, 4)); |
|
76 | - $x3 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 8, 4)); |
|
77 | - $x4 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 12, 4)); |
|
78 | - $x6 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 0, 4)); |
|
79 | - $x7 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 4, 4)); |
|
80 | - $x8 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 8, 4)); |
|
81 | - $x9 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 12, 4)); |
|
82 | - $x11 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 16, 4)); |
|
83 | - $x12 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 20, 4)); |
|
84 | - $x13 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 24, 4)); |
|
85 | - $x14 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 28, 4)); |
|
14 | + /** |
|
15 | + * Calculate an salsa20 hash of a single block |
|
16 | + * |
|
17 | + * @internal You should not use this directly from another application |
|
18 | + * |
|
19 | + * @param string $in |
|
20 | + * @param string $k |
|
21 | + * @param string|null $c |
|
22 | + * @return string |
|
23 | + */ |
|
24 | + public static function core_salsa20($in, $k, $c = null) |
|
25 | + { |
|
26 | + /** |
|
27 | + * @var ParagonIE_Sodium_Core32_Int32 $x0 |
|
28 | + * @var ParagonIE_Sodium_Core32_Int32 $x1 |
|
29 | + * @var ParagonIE_Sodium_Core32_Int32 $x2 |
|
30 | + * @var ParagonIE_Sodium_Core32_Int32 $x3 |
|
31 | + * @var ParagonIE_Sodium_Core32_Int32 $x4 |
|
32 | + * @var ParagonIE_Sodium_Core32_Int32 $x5 |
|
33 | + * @var ParagonIE_Sodium_Core32_Int32 $x6 |
|
34 | + * @var ParagonIE_Sodium_Core32_Int32 $x7 |
|
35 | + * @var ParagonIE_Sodium_Core32_Int32 $x8 |
|
36 | + * @var ParagonIE_Sodium_Core32_Int32 $x9 |
|
37 | + * @var ParagonIE_Sodium_Core32_Int32 $x10 |
|
38 | + * @var ParagonIE_Sodium_Core32_Int32 $x11 |
|
39 | + * @var ParagonIE_Sodium_Core32_Int32 $x12 |
|
40 | + * @var ParagonIE_Sodium_Core32_Int32 $x13 |
|
41 | + * @var ParagonIE_Sodium_Core32_Int32 $x14 |
|
42 | + * @var ParagonIE_Sodium_Core32_Int32 $x15 |
|
43 | + * @var ParagonIE_Sodium_Core32_Int32 $j0 |
|
44 | + * @var ParagonIE_Sodium_Core32_Int32 $j1 |
|
45 | + * @var ParagonIE_Sodium_Core32_Int32 $j2 |
|
46 | + * @var ParagonIE_Sodium_Core32_Int32 $j3 |
|
47 | + * @var ParagonIE_Sodium_Core32_Int32 $j4 |
|
48 | + * @var ParagonIE_Sodium_Core32_Int32 $j5 |
|
49 | + * @var ParagonIE_Sodium_Core32_Int32 $j6 |
|
50 | + * @var ParagonIE_Sodium_Core32_Int32 $j7 |
|
51 | + * @var ParagonIE_Sodium_Core32_Int32 $j8 |
|
52 | + * @var ParagonIE_Sodium_Core32_Int32 $j9 |
|
53 | + * @var ParagonIE_Sodium_Core32_Int32 $j10 |
|
54 | + * @var ParagonIE_Sodium_Core32_Int32 $j11 |
|
55 | + * @var ParagonIE_Sodium_Core32_Int32 $j12 |
|
56 | + * @var ParagonIE_Sodium_Core32_Int32 $j13 |
|
57 | + * @var ParagonIE_Sodium_Core32_Int32 $j14 |
|
58 | + * @var ParagonIE_Sodium_Core32_Int32 $j15 |
|
59 | + */ |
|
60 | + if (self::strlen($k) < 32) { |
|
61 | + throw new RangeException('Key must be 32 bytes long'); |
|
62 | + } |
|
63 | + if ($c === null) { |
|
64 | + $x0 = new ParagonIE_Sodium_Core32_Int32(array(0x6170, 0x7865)); |
|
65 | + $x5 = new ParagonIE_Sodium_Core32_Int32(array(0x3320, 0x646e)); |
|
66 | + $x10 = new ParagonIE_Sodium_Core32_Int32(array(0x7962, 0x2d32)); |
|
67 | + $x15 = new ParagonIE_Sodium_Core32_Int32(array(0x6b20, 0x6574)); |
|
68 | + } else { |
|
69 | + $x0 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 0, 4)); |
|
70 | + $x5 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 4, 4)); |
|
71 | + $x10 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 8, 4)); |
|
72 | + $x15 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 12, 4)); |
|
73 | + } |
|
74 | + $x1 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 0, 4)); |
|
75 | + $x2 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 4, 4)); |
|
76 | + $x3 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 8, 4)); |
|
77 | + $x4 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 12, 4)); |
|
78 | + $x6 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 0, 4)); |
|
79 | + $x7 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 4, 4)); |
|
80 | + $x8 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 8, 4)); |
|
81 | + $x9 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 12, 4)); |
|
82 | + $x11 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 16, 4)); |
|
83 | + $x12 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 20, 4)); |
|
84 | + $x13 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 24, 4)); |
|
85 | + $x14 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 28, 4)); |
|
86 | 86 | |
87 | - $j0 = clone $x0; |
|
88 | - $j1 = clone $x1; |
|
89 | - $j2 = clone $x2; |
|
90 | - $j3 = clone $x3; |
|
91 | - $j4 = clone $x4; |
|
92 | - $j5 = clone $x5; |
|
93 | - $j6 = clone $x6; |
|
94 | - $j7 = clone $x7; |
|
95 | - $j8 = clone $x8; |
|
96 | - $j9 = clone $x9; |
|
97 | - $j10 = clone $x10; |
|
98 | - $j11 = clone $x11; |
|
99 | - $j12 = clone $x12; |
|
100 | - $j13 = clone $x13; |
|
101 | - $j14 = clone $x14; |
|
102 | - $j15 = clone $x15; |
|
87 | + $j0 = clone $x0; |
|
88 | + $j1 = clone $x1; |
|
89 | + $j2 = clone $x2; |
|
90 | + $j3 = clone $x3; |
|
91 | + $j4 = clone $x4; |
|
92 | + $j5 = clone $x5; |
|
93 | + $j6 = clone $x6; |
|
94 | + $j7 = clone $x7; |
|
95 | + $j8 = clone $x8; |
|
96 | + $j9 = clone $x9; |
|
97 | + $j10 = clone $x10; |
|
98 | + $j11 = clone $x11; |
|
99 | + $j12 = clone $x12; |
|
100 | + $j13 = clone $x13; |
|
101 | + $j14 = clone $x14; |
|
102 | + $j15 = clone $x15; |
|
103 | 103 | |
104 | - for ($i = self::ROUNDS; $i > 0; $i -= 2) { |
|
105 | - $x4 = $x4->xorInt32($x0->addInt32($x12)->rotateLeft(7)); |
|
106 | - $x8 = $x8->xorInt32($x4->addInt32($x0)->rotateLeft(9)); |
|
107 | - $x12 = $x12->xorInt32($x8->addInt32($x4)->rotateLeft(13)); |
|
108 | - $x0 = $x0->xorInt32($x12->addInt32($x8)->rotateLeft(18)); |
|
104 | + for ($i = self::ROUNDS; $i > 0; $i -= 2) { |
|
105 | + $x4 = $x4->xorInt32($x0->addInt32($x12)->rotateLeft(7)); |
|
106 | + $x8 = $x8->xorInt32($x4->addInt32($x0)->rotateLeft(9)); |
|
107 | + $x12 = $x12->xorInt32($x8->addInt32($x4)->rotateLeft(13)); |
|
108 | + $x0 = $x0->xorInt32($x12->addInt32($x8)->rotateLeft(18)); |
|
109 | 109 | |
110 | - $x9 = $x9->xorInt32($x5->addInt32($x1)->rotateLeft(7)); |
|
111 | - $x13 = $x13->xorInt32($x9->addInt32($x5)->rotateLeft(9)); |
|
112 | - $x1 = $x1->xorInt32($x13->addInt32($x9)->rotateLeft(13)); |
|
113 | - $x5 = $x5->xorInt32($x1->addInt32($x13)->rotateLeft(18)); |
|
110 | + $x9 = $x9->xorInt32($x5->addInt32($x1)->rotateLeft(7)); |
|
111 | + $x13 = $x13->xorInt32($x9->addInt32($x5)->rotateLeft(9)); |
|
112 | + $x1 = $x1->xorInt32($x13->addInt32($x9)->rotateLeft(13)); |
|
113 | + $x5 = $x5->xorInt32($x1->addInt32($x13)->rotateLeft(18)); |
|
114 | 114 | |
115 | - $x14 = $x14->xorInt32($x10->addInt32($x6)->rotateLeft(7)); |
|
116 | - $x2 = $x2->xorInt32($x14->addInt32($x10)->rotateLeft(9)); |
|
117 | - $x6 = $x6->xorInt32($x2->addInt32($x14)->rotateLeft(13)); |
|
118 | - $x10 = $x10->xorInt32($x6->addInt32($x2)->rotateLeft(18)); |
|
115 | + $x14 = $x14->xorInt32($x10->addInt32($x6)->rotateLeft(7)); |
|
116 | + $x2 = $x2->xorInt32($x14->addInt32($x10)->rotateLeft(9)); |
|
117 | + $x6 = $x6->xorInt32($x2->addInt32($x14)->rotateLeft(13)); |
|
118 | + $x10 = $x10->xorInt32($x6->addInt32($x2)->rotateLeft(18)); |
|
119 | 119 | |
120 | - $x3 = $x3->xorInt32($x15->addInt32($x11)->rotateLeft(7)); |
|
121 | - $x7 = $x7->xorInt32($x3->addInt32($x15)->rotateLeft(9)); |
|
122 | - $x11 = $x11->xorInt32($x7->addInt32($x3)->rotateLeft(13)); |
|
123 | - $x15 = $x15->xorInt32($x11->addInt32($x7)->rotateLeft(18)); |
|
120 | + $x3 = $x3->xorInt32($x15->addInt32($x11)->rotateLeft(7)); |
|
121 | + $x7 = $x7->xorInt32($x3->addInt32($x15)->rotateLeft(9)); |
|
122 | + $x11 = $x11->xorInt32($x7->addInt32($x3)->rotateLeft(13)); |
|
123 | + $x15 = $x15->xorInt32($x11->addInt32($x7)->rotateLeft(18)); |
|
124 | 124 | |
125 | - $x1 = $x1->xorInt32($x0->addInt32($x3)->rotateLeft(7)); |
|
126 | - $x2 = $x2->xorInt32($x1->addInt32($x0)->rotateLeft(9)); |
|
127 | - $x3 = $x3->xorInt32($x2->addInt32($x1)->rotateLeft(13)); |
|
128 | - $x0 = $x0->xorInt32($x3->addInt32($x2)->rotateLeft(18)); |
|
125 | + $x1 = $x1->xorInt32($x0->addInt32($x3)->rotateLeft(7)); |
|
126 | + $x2 = $x2->xorInt32($x1->addInt32($x0)->rotateLeft(9)); |
|
127 | + $x3 = $x3->xorInt32($x2->addInt32($x1)->rotateLeft(13)); |
|
128 | + $x0 = $x0->xorInt32($x3->addInt32($x2)->rotateLeft(18)); |
|
129 | 129 | |
130 | - $x6 = $x6->xorInt32($x5->addInt32($x4)->rotateLeft(7)); |
|
131 | - $x7 = $x7->xorInt32($x6->addInt32($x5)->rotateLeft(9)); |
|
132 | - $x4 = $x4->xorInt32($x7->addInt32($x6)->rotateLeft(13)); |
|
133 | - $x5 = $x5->xorInt32($x4->addInt32($x7)->rotateLeft(18)); |
|
130 | + $x6 = $x6->xorInt32($x5->addInt32($x4)->rotateLeft(7)); |
|
131 | + $x7 = $x7->xorInt32($x6->addInt32($x5)->rotateLeft(9)); |
|
132 | + $x4 = $x4->xorInt32($x7->addInt32($x6)->rotateLeft(13)); |
|
133 | + $x5 = $x5->xorInt32($x4->addInt32($x7)->rotateLeft(18)); |
|
134 | 134 | |
135 | - $x11 = $x11->xorInt32($x10->addInt32($x9)->rotateLeft(7)); |
|
136 | - $x8 = $x8->xorInt32($x11->addInt32($x10)->rotateLeft(9)); |
|
137 | - $x9 = $x9->xorInt32($x8->addInt32($x11)->rotateLeft(13)); |
|
138 | - $x10 = $x10->xorInt32($x9->addInt32($x8)->rotateLeft(18)); |
|
135 | + $x11 = $x11->xorInt32($x10->addInt32($x9)->rotateLeft(7)); |
|
136 | + $x8 = $x8->xorInt32($x11->addInt32($x10)->rotateLeft(9)); |
|
137 | + $x9 = $x9->xorInt32($x8->addInt32($x11)->rotateLeft(13)); |
|
138 | + $x10 = $x10->xorInt32($x9->addInt32($x8)->rotateLeft(18)); |
|
139 | 139 | |
140 | - $x12 = $x12->xorInt32($x15->addInt32($x14)->rotateLeft(7)); |
|
141 | - $x13 = $x13->xorInt32($x12->addInt32($x15)->rotateLeft(9)); |
|
142 | - $x14 = $x14->xorInt32($x13->addInt32($x12)->rotateLeft(13)); |
|
143 | - $x15 = $x15->xorInt32($x14->addInt32($x13)->rotateLeft(18)); |
|
144 | - } |
|
140 | + $x12 = $x12->xorInt32($x15->addInt32($x14)->rotateLeft(7)); |
|
141 | + $x13 = $x13->xorInt32($x12->addInt32($x15)->rotateLeft(9)); |
|
142 | + $x14 = $x14->xorInt32($x13->addInt32($x12)->rotateLeft(13)); |
|
143 | + $x15 = $x15->xorInt32($x14->addInt32($x13)->rotateLeft(18)); |
|
144 | + } |
|
145 | 145 | |
146 | - $x0 = $x0->addInt32($j0); |
|
147 | - $x1 = $x1->addInt32($j1); |
|
148 | - $x2 = $x2->addInt32($j2); |
|
149 | - $x3 = $x3->addInt32($j3); |
|
150 | - $x4 = $x4->addInt32($j4); |
|
151 | - $x5 = $x5->addInt32($j5); |
|
152 | - $x6 = $x6->addInt32($j6); |
|
153 | - $x7 = $x7->addInt32($j7); |
|
154 | - $x8 = $x8->addInt32($j8); |
|
155 | - $x9 = $x9->addInt32($j9); |
|
156 | - $x10 = $x10->addInt32($j10); |
|
157 | - $x11 = $x11->addInt32($j11); |
|
158 | - $x12 = $x12->addInt32($j12); |
|
159 | - $x13 = $x13->addInt32($j13); |
|
160 | - $x14 = $x14->addInt32($j14); |
|
161 | - $x15 = $x15->addInt32($j15); |
|
146 | + $x0 = $x0->addInt32($j0); |
|
147 | + $x1 = $x1->addInt32($j1); |
|
148 | + $x2 = $x2->addInt32($j2); |
|
149 | + $x3 = $x3->addInt32($j3); |
|
150 | + $x4 = $x4->addInt32($j4); |
|
151 | + $x5 = $x5->addInt32($j5); |
|
152 | + $x6 = $x6->addInt32($j6); |
|
153 | + $x7 = $x7->addInt32($j7); |
|
154 | + $x8 = $x8->addInt32($j8); |
|
155 | + $x9 = $x9->addInt32($j9); |
|
156 | + $x10 = $x10->addInt32($j10); |
|
157 | + $x11 = $x11->addInt32($j11); |
|
158 | + $x12 = $x12->addInt32($j12); |
|
159 | + $x13 = $x13->addInt32($j13); |
|
160 | + $x14 = $x14->addInt32($j14); |
|
161 | + $x15 = $x15->addInt32($j15); |
|
162 | 162 | |
163 | - return $x0->toReverseString() . |
|
164 | - $x1->toReverseString() . |
|
165 | - $x2->toReverseString() . |
|
166 | - $x3->toReverseString() . |
|
167 | - $x4->toReverseString() . |
|
168 | - $x5->toReverseString() . |
|
169 | - $x6->toReverseString() . |
|
170 | - $x7->toReverseString() . |
|
171 | - $x8->toReverseString() . |
|
172 | - $x9->toReverseString() . |
|
173 | - $x10->toReverseString() . |
|
174 | - $x11->toReverseString() . |
|
175 | - $x12->toReverseString() . |
|
176 | - $x13->toReverseString() . |
|
177 | - $x14->toReverseString() . |
|
178 | - $x15->toReverseString(); |
|
179 | - } |
|
163 | + return $x0->toReverseString() . |
|
164 | + $x1->toReverseString() . |
|
165 | + $x2->toReverseString() . |
|
166 | + $x3->toReverseString() . |
|
167 | + $x4->toReverseString() . |
|
168 | + $x5->toReverseString() . |
|
169 | + $x6->toReverseString() . |
|
170 | + $x7->toReverseString() . |
|
171 | + $x8->toReverseString() . |
|
172 | + $x9->toReverseString() . |
|
173 | + $x10->toReverseString() . |
|
174 | + $x11->toReverseString() . |
|
175 | + $x12->toReverseString() . |
|
176 | + $x13->toReverseString() . |
|
177 | + $x14->toReverseString() . |
|
178 | + $x15->toReverseString(); |
|
179 | + } |
|
180 | 180 | |
181 | - /** |
|
182 | - * @internal You should not use this directly from another application |
|
183 | - * |
|
184 | - * @param int $len |
|
185 | - * @param string $nonce |
|
186 | - * @param string $key |
|
187 | - * @return string |
|
188 | - */ |
|
189 | - public static function salsa20($len, $nonce, $key) |
|
190 | - { |
|
191 | - if (self::strlen($key) !== 32) { |
|
192 | - throw new RangeException('Key must be 32 bytes long'); |
|
193 | - } |
|
194 | - $kcopy = '' . $key; |
|
195 | - $in = self::substr($nonce, 0, 8) . str_repeat("\0", 8); |
|
196 | - $c = ''; |
|
197 | - while ($len >= 64) { |
|
198 | - $c .= self::core_salsa20($in, $kcopy, null); |
|
199 | - $u = 1; |
|
200 | - // Internal counter. |
|
201 | - for ($i = 8; $i < 16; ++$i) { |
|
202 | - $u += self::chrToInt($in[$i]); |
|
203 | - $in[$i] = self::intToChr($u & 0xff); |
|
204 | - $u >>= 8; |
|
205 | - } |
|
206 | - $len -= 64; |
|
207 | - } |
|
208 | - if ($len > 0) { |
|
209 | - $c .= self::substr( |
|
210 | - self::core_salsa20($in, $kcopy, null), |
|
211 | - 0, |
|
212 | - $len |
|
213 | - ); |
|
214 | - } |
|
215 | - try { |
|
216 | - ParagonIE_Sodium_Compat::memzero($kcopy); |
|
217 | - } catch (Error $ex) { |
|
218 | - $kcopy = null; |
|
219 | - } |
|
220 | - return $c; |
|
221 | - } |
|
181 | + /** |
|
182 | + * @internal You should not use this directly from another application |
|
183 | + * |
|
184 | + * @param int $len |
|
185 | + * @param string $nonce |
|
186 | + * @param string $key |
|
187 | + * @return string |
|
188 | + */ |
|
189 | + public static function salsa20($len, $nonce, $key) |
|
190 | + { |
|
191 | + if (self::strlen($key) !== 32) { |
|
192 | + throw new RangeException('Key must be 32 bytes long'); |
|
193 | + } |
|
194 | + $kcopy = '' . $key; |
|
195 | + $in = self::substr($nonce, 0, 8) . str_repeat("\0", 8); |
|
196 | + $c = ''; |
|
197 | + while ($len >= 64) { |
|
198 | + $c .= self::core_salsa20($in, $kcopy, null); |
|
199 | + $u = 1; |
|
200 | + // Internal counter. |
|
201 | + for ($i = 8; $i < 16; ++$i) { |
|
202 | + $u += self::chrToInt($in[$i]); |
|
203 | + $in[$i] = self::intToChr($u & 0xff); |
|
204 | + $u >>= 8; |
|
205 | + } |
|
206 | + $len -= 64; |
|
207 | + } |
|
208 | + if ($len > 0) { |
|
209 | + $c .= self::substr( |
|
210 | + self::core_salsa20($in, $kcopy, null), |
|
211 | + 0, |
|
212 | + $len |
|
213 | + ); |
|
214 | + } |
|
215 | + try { |
|
216 | + ParagonIE_Sodium_Compat::memzero($kcopy); |
|
217 | + } catch (Error $ex) { |
|
218 | + $kcopy = null; |
|
219 | + } |
|
220 | + return $c; |
|
221 | + } |
|
222 | 222 | |
223 | - /** |
|
224 | - * @internal You should not use this directly from another application |
|
225 | - * |
|
226 | - * @param string $m |
|
227 | - * @param string $n |
|
228 | - * @param int $ic |
|
229 | - * @param string $k |
|
230 | - * @return string |
|
231 | - */ |
|
232 | - public static function salsa20_xor_ic($m, $n, $ic, $k) |
|
233 | - { |
|
234 | - $mlen = self::strlen($m); |
|
235 | - if ($mlen < 1) { |
|
236 | - return ''; |
|
237 | - } |
|
238 | - $kcopy = self::substr($k, 0, 32); |
|
239 | - $in = self::substr($n, 0, 8); |
|
240 | - // Initialize the counter |
|
241 | - $in .= ParagonIE_Sodium_Core32_Util::store64_le($ic); |
|
223 | + /** |
|
224 | + * @internal You should not use this directly from another application |
|
225 | + * |
|
226 | + * @param string $m |
|
227 | + * @param string $n |
|
228 | + * @param int $ic |
|
229 | + * @param string $k |
|
230 | + * @return string |
|
231 | + */ |
|
232 | + public static function salsa20_xor_ic($m, $n, $ic, $k) |
|
233 | + { |
|
234 | + $mlen = self::strlen($m); |
|
235 | + if ($mlen < 1) { |
|
236 | + return ''; |
|
237 | + } |
|
238 | + $kcopy = self::substr($k, 0, 32); |
|
239 | + $in = self::substr($n, 0, 8); |
|
240 | + // Initialize the counter |
|
241 | + $in .= ParagonIE_Sodium_Core32_Util::store64_le($ic); |
|
242 | 242 | |
243 | - $c = ''; |
|
244 | - while ($mlen >= 64) { |
|
245 | - $block = self::core_salsa20($in, $kcopy, null); |
|
246 | - $c .= self::xorStrings( |
|
247 | - self::substr($m, 0, 64), |
|
248 | - self::substr($block, 0, 64) |
|
249 | - ); |
|
250 | - $u = 1; |
|
251 | - for ($i = 8; $i < 16; ++$i) { |
|
252 | - $u += self::chrToInt($in[$i]); |
|
253 | - $in[$i] = self::intToChr($u & 0xff); |
|
254 | - $u >>= 8; |
|
255 | - } |
|
243 | + $c = ''; |
|
244 | + while ($mlen >= 64) { |
|
245 | + $block = self::core_salsa20($in, $kcopy, null); |
|
246 | + $c .= self::xorStrings( |
|
247 | + self::substr($m, 0, 64), |
|
248 | + self::substr($block, 0, 64) |
|
249 | + ); |
|
250 | + $u = 1; |
|
251 | + for ($i = 8; $i < 16; ++$i) { |
|
252 | + $u += self::chrToInt($in[$i]); |
|
253 | + $in[$i] = self::intToChr($u & 0xff); |
|
254 | + $u >>= 8; |
|
255 | + } |
|
256 | 256 | |
257 | - $mlen -= 64; |
|
258 | - $m = self::substr($m, 64); |
|
259 | - } |
|
257 | + $mlen -= 64; |
|
258 | + $m = self::substr($m, 64); |
|
259 | + } |
|
260 | 260 | |
261 | - if ($mlen) { |
|
262 | - $block = self::core_salsa20($in, $kcopy, null); |
|
263 | - $c .= self::xorStrings( |
|
264 | - self::substr($m, 0, $mlen), |
|
265 | - self::substr($block, 0, $mlen) |
|
266 | - ); |
|
267 | - } |
|
268 | - try { |
|
269 | - ParagonIE_Sodium_Compat::memzero($block); |
|
270 | - ParagonIE_Sodium_Compat::memzero($kcopy); |
|
271 | - } catch (Error $ex) { |
|
272 | - $block = null; |
|
273 | - $kcopy = null; |
|
274 | - } |
|
261 | + if ($mlen) { |
|
262 | + $block = self::core_salsa20($in, $kcopy, null); |
|
263 | + $c .= self::xorStrings( |
|
264 | + self::substr($m, 0, $mlen), |
|
265 | + self::substr($block, 0, $mlen) |
|
266 | + ); |
|
267 | + } |
|
268 | + try { |
|
269 | + ParagonIE_Sodium_Compat::memzero($block); |
|
270 | + ParagonIE_Sodium_Compat::memzero($kcopy); |
|
271 | + } catch (Error $ex) { |
|
272 | + $block = null; |
|
273 | + $kcopy = null; |
|
274 | + } |
|
275 | 275 | |
276 | - return $c; |
|
277 | - } |
|
276 | + return $c; |
|
277 | + } |
|
278 | 278 | |
279 | - /** |
|
280 | - * @internal You should not use this directly from another application |
|
281 | - * |
|
282 | - * @param string $message |
|
283 | - * @param string $nonce |
|
284 | - * @param string $key |
|
285 | - * @return string |
|
286 | - */ |
|
287 | - public static function salsa20_xor($message, $nonce, $key) |
|
288 | - { |
|
289 | - return self::xorStrings( |
|
290 | - $message, |
|
291 | - self::salsa20( |
|
292 | - self::strlen($message), |
|
293 | - $nonce, |
|
294 | - $key |
|
295 | - ) |
|
296 | - ); |
|
297 | - } |
|
279 | + /** |
|
280 | + * @internal You should not use this directly from another application |
|
281 | + * |
|
282 | + * @param string $message |
|
283 | + * @param string $nonce |
|
284 | + * @param string $key |
|
285 | + * @return string |
|
286 | + */ |
|
287 | + public static function salsa20_xor($message, $nonce, $key) |
|
288 | + { |
|
289 | + return self::xorStrings( |
|
290 | + $message, |
|
291 | + self::salsa20( |
|
292 | + self::strlen($message), |
|
293 | + $nonce, |
|
294 | + $key |
|
295 | + ) |
|
296 | + ); |
|
297 | + } |
|
298 | 298 | } |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -if (class_exists('ParagonIE_Sodium_Core32_Salsa20', false)) { |
|
3 | +if ( class_exists( 'ParagonIE_Sodium_Core32_Salsa20', false ) ) { |
|
4 | 4 | return; |
5 | 5 | } |
6 | 6 | |
@@ -21,7 +21,7 @@ discard block |
||
21 | 21 | * @param string|null $c |
22 | 22 | * @return string |
23 | 23 | */ |
24 | - public static function core_salsa20($in, $k, $c = null) |
|
24 | + public static function core_salsa20( $in, $k, $c = null ) |
|
25 | 25 | { |
26 | 26 | /** |
27 | 27 | * @var ParagonIE_Sodium_Core32_Int32 $x0 |
@@ -57,32 +57,32 @@ discard block |
||
57 | 57 | * @var ParagonIE_Sodium_Core32_Int32 $j14 |
58 | 58 | * @var ParagonIE_Sodium_Core32_Int32 $j15 |
59 | 59 | */ |
60 | - if (self::strlen($k) < 32) { |
|
61 | - throw new RangeException('Key must be 32 bytes long'); |
|
60 | + if ( self::strlen( $k ) < 32 ) { |
|
61 | + throw new RangeException( 'Key must be 32 bytes long' ); |
|
62 | 62 | } |
63 | - if ($c === null) { |
|
64 | - $x0 = new ParagonIE_Sodium_Core32_Int32(array(0x6170, 0x7865)); |
|
65 | - $x5 = new ParagonIE_Sodium_Core32_Int32(array(0x3320, 0x646e)); |
|
66 | - $x10 = new ParagonIE_Sodium_Core32_Int32(array(0x7962, 0x2d32)); |
|
67 | - $x15 = new ParagonIE_Sodium_Core32_Int32(array(0x6b20, 0x6574)); |
|
63 | + if ( $c === null ) { |
|
64 | + $x0 = new ParagonIE_Sodium_Core32_Int32( array( 0x6170, 0x7865 ) ); |
|
65 | + $x5 = new ParagonIE_Sodium_Core32_Int32( array( 0x3320, 0x646e ) ); |
|
66 | + $x10 = new ParagonIE_Sodium_Core32_Int32( array( 0x7962, 0x2d32 ) ); |
|
67 | + $x15 = new ParagonIE_Sodium_Core32_Int32( array( 0x6b20, 0x6574 ) ); |
|
68 | 68 | } else { |
69 | - $x0 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 0, 4)); |
|
70 | - $x5 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 4, 4)); |
|
71 | - $x10 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 8, 4)); |
|
72 | - $x15 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 12, 4)); |
|
69 | + $x0 = ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $c, 0, 4 ) ); |
|
70 | + $x5 = ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $c, 4, 4 ) ); |
|
71 | + $x10 = ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $c, 8, 4 ) ); |
|
72 | + $x15 = ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $c, 12, 4 ) ); |
|
73 | 73 | } |
74 | - $x1 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 0, 4)); |
|
75 | - $x2 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 4, 4)); |
|
76 | - $x3 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 8, 4)); |
|
77 | - $x4 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 12, 4)); |
|
78 | - $x6 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 0, 4)); |
|
79 | - $x7 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 4, 4)); |
|
80 | - $x8 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 8, 4)); |
|
81 | - $x9 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 12, 4)); |
|
82 | - $x11 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 16, 4)); |
|
83 | - $x12 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 20, 4)); |
|
84 | - $x13 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 24, 4)); |
|
85 | - $x14 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 28, 4)); |
|
74 | + $x1 = ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $k, 0, 4 ) ); |
|
75 | + $x2 = ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $k, 4, 4 ) ); |
|
76 | + $x3 = ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $k, 8, 4 ) ); |
|
77 | + $x4 = ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $k, 12, 4 ) ); |
|
78 | + $x6 = ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $in, 0, 4 ) ); |
|
79 | + $x7 = ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $in, 4, 4 ) ); |
|
80 | + $x8 = ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $in, 8, 4 ) ); |
|
81 | + $x9 = ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $in, 12, 4 ) ); |
|
82 | + $x11 = ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $k, 16, 4 ) ); |
|
83 | + $x12 = ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $k, 20, 4 ) ); |
|
84 | + $x13 = ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $k, 24, 4 ) ); |
|
85 | + $x14 = ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $k, 28, 4 ) ); |
|
86 | 86 | |
87 | 87 | $j0 = clone $x0; |
88 | 88 | $j1 = clone $x1; |
@@ -101,64 +101,64 @@ discard block |
||
101 | 101 | $j14 = clone $x14; |
102 | 102 | $j15 = clone $x15; |
103 | 103 | |
104 | - for ($i = self::ROUNDS; $i > 0; $i -= 2) { |
|
105 | - $x4 = $x4->xorInt32($x0->addInt32($x12)->rotateLeft(7)); |
|
106 | - $x8 = $x8->xorInt32($x4->addInt32($x0)->rotateLeft(9)); |
|
107 | - $x12 = $x12->xorInt32($x8->addInt32($x4)->rotateLeft(13)); |
|
108 | - $x0 = $x0->xorInt32($x12->addInt32($x8)->rotateLeft(18)); |
|
104 | + for ( $i = self::ROUNDS; $i > 0; $i -= 2 ) { |
|
105 | + $x4 = $x4->xorInt32( $x0->addInt32( $x12 )->rotateLeft( 7 ) ); |
|
106 | + $x8 = $x8->xorInt32( $x4->addInt32( $x0 )->rotateLeft( 9 ) ); |
|
107 | + $x12 = $x12->xorInt32( $x8->addInt32( $x4 )->rotateLeft( 13 ) ); |
|
108 | + $x0 = $x0->xorInt32( $x12->addInt32( $x8 )->rotateLeft( 18 ) ); |
|
109 | 109 | |
110 | - $x9 = $x9->xorInt32($x5->addInt32($x1)->rotateLeft(7)); |
|
111 | - $x13 = $x13->xorInt32($x9->addInt32($x5)->rotateLeft(9)); |
|
112 | - $x1 = $x1->xorInt32($x13->addInt32($x9)->rotateLeft(13)); |
|
113 | - $x5 = $x5->xorInt32($x1->addInt32($x13)->rotateLeft(18)); |
|
110 | + $x9 = $x9->xorInt32( $x5->addInt32( $x1 )->rotateLeft( 7 ) ); |
|
111 | + $x13 = $x13->xorInt32( $x9->addInt32( $x5 )->rotateLeft( 9 ) ); |
|
112 | + $x1 = $x1->xorInt32( $x13->addInt32( $x9 )->rotateLeft( 13 ) ); |
|
113 | + $x5 = $x5->xorInt32( $x1->addInt32( $x13 )->rotateLeft( 18 ) ); |
|
114 | 114 | |
115 | - $x14 = $x14->xorInt32($x10->addInt32($x6)->rotateLeft(7)); |
|
116 | - $x2 = $x2->xorInt32($x14->addInt32($x10)->rotateLeft(9)); |
|
117 | - $x6 = $x6->xorInt32($x2->addInt32($x14)->rotateLeft(13)); |
|
118 | - $x10 = $x10->xorInt32($x6->addInt32($x2)->rotateLeft(18)); |
|
115 | + $x14 = $x14->xorInt32( $x10->addInt32( $x6 )->rotateLeft( 7 ) ); |
|
116 | + $x2 = $x2->xorInt32( $x14->addInt32( $x10 )->rotateLeft( 9 ) ); |
|
117 | + $x6 = $x6->xorInt32( $x2->addInt32( $x14 )->rotateLeft( 13 ) ); |
|
118 | + $x10 = $x10->xorInt32( $x6->addInt32( $x2 )->rotateLeft( 18 ) ); |
|
119 | 119 | |
120 | - $x3 = $x3->xorInt32($x15->addInt32($x11)->rotateLeft(7)); |
|
121 | - $x7 = $x7->xorInt32($x3->addInt32($x15)->rotateLeft(9)); |
|
122 | - $x11 = $x11->xorInt32($x7->addInt32($x3)->rotateLeft(13)); |
|
123 | - $x15 = $x15->xorInt32($x11->addInt32($x7)->rotateLeft(18)); |
|
120 | + $x3 = $x3->xorInt32( $x15->addInt32( $x11 )->rotateLeft( 7 ) ); |
|
121 | + $x7 = $x7->xorInt32( $x3->addInt32( $x15 )->rotateLeft( 9 ) ); |
|
122 | + $x11 = $x11->xorInt32( $x7->addInt32( $x3 )->rotateLeft( 13 ) ); |
|
123 | + $x15 = $x15->xorInt32( $x11->addInt32( $x7 )->rotateLeft( 18 ) ); |
|
124 | 124 | |
125 | - $x1 = $x1->xorInt32($x0->addInt32($x3)->rotateLeft(7)); |
|
126 | - $x2 = $x2->xorInt32($x1->addInt32($x0)->rotateLeft(9)); |
|
127 | - $x3 = $x3->xorInt32($x2->addInt32($x1)->rotateLeft(13)); |
|
128 | - $x0 = $x0->xorInt32($x3->addInt32($x2)->rotateLeft(18)); |
|
125 | + $x1 = $x1->xorInt32( $x0->addInt32( $x3 )->rotateLeft( 7 ) ); |
|
126 | + $x2 = $x2->xorInt32( $x1->addInt32( $x0 )->rotateLeft( 9 ) ); |
|
127 | + $x3 = $x3->xorInt32( $x2->addInt32( $x1 )->rotateLeft( 13 ) ); |
|
128 | + $x0 = $x0->xorInt32( $x3->addInt32( $x2 )->rotateLeft( 18 ) ); |
|
129 | 129 | |
130 | - $x6 = $x6->xorInt32($x5->addInt32($x4)->rotateLeft(7)); |
|
131 | - $x7 = $x7->xorInt32($x6->addInt32($x5)->rotateLeft(9)); |
|
132 | - $x4 = $x4->xorInt32($x7->addInt32($x6)->rotateLeft(13)); |
|
133 | - $x5 = $x5->xorInt32($x4->addInt32($x7)->rotateLeft(18)); |
|
130 | + $x6 = $x6->xorInt32( $x5->addInt32( $x4 )->rotateLeft( 7 ) ); |
|
131 | + $x7 = $x7->xorInt32( $x6->addInt32( $x5 )->rotateLeft( 9 ) ); |
|
132 | + $x4 = $x4->xorInt32( $x7->addInt32( $x6 )->rotateLeft( 13 ) ); |
|
133 | + $x5 = $x5->xorInt32( $x4->addInt32( $x7 )->rotateLeft( 18 ) ); |
|
134 | 134 | |
135 | - $x11 = $x11->xorInt32($x10->addInt32($x9)->rotateLeft(7)); |
|
136 | - $x8 = $x8->xorInt32($x11->addInt32($x10)->rotateLeft(9)); |
|
137 | - $x9 = $x9->xorInt32($x8->addInt32($x11)->rotateLeft(13)); |
|
138 | - $x10 = $x10->xorInt32($x9->addInt32($x8)->rotateLeft(18)); |
|
135 | + $x11 = $x11->xorInt32( $x10->addInt32( $x9 )->rotateLeft( 7 ) ); |
|
136 | + $x8 = $x8->xorInt32( $x11->addInt32( $x10 )->rotateLeft( 9 ) ); |
|
137 | + $x9 = $x9->xorInt32( $x8->addInt32( $x11 )->rotateLeft( 13 ) ); |
|
138 | + $x10 = $x10->xorInt32( $x9->addInt32( $x8 )->rotateLeft( 18 ) ); |
|
139 | 139 | |
140 | - $x12 = $x12->xorInt32($x15->addInt32($x14)->rotateLeft(7)); |
|
141 | - $x13 = $x13->xorInt32($x12->addInt32($x15)->rotateLeft(9)); |
|
142 | - $x14 = $x14->xorInt32($x13->addInt32($x12)->rotateLeft(13)); |
|
143 | - $x15 = $x15->xorInt32($x14->addInt32($x13)->rotateLeft(18)); |
|
140 | + $x12 = $x12->xorInt32( $x15->addInt32( $x14 )->rotateLeft( 7 ) ); |
|
141 | + $x13 = $x13->xorInt32( $x12->addInt32( $x15 )->rotateLeft( 9 ) ); |
|
142 | + $x14 = $x14->xorInt32( $x13->addInt32( $x12 )->rotateLeft( 13 ) ); |
|
143 | + $x15 = $x15->xorInt32( $x14->addInt32( $x13 )->rotateLeft( 18 ) ); |
|
144 | 144 | } |
145 | 145 | |
146 | - $x0 = $x0->addInt32($j0); |
|
147 | - $x1 = $x1->addInt32($j1); |
|
148 | - $x2 = $x2->addInt32($j2); |
|
149 | - $x3 = $x3->addInt32($j3); |
|
150 | - $x4 = $x4->addInt32($j4); |
|
151 | - $x5 = $x5->addInt32($j5); |
|
152 | - $x6 = $x6->addInt32($j6); |
|
153 | - $x7 = $x7->addInt32($j7); |
|
154 | - $x8 = $x8->addInt32($j8); |
|
155 | - $x9 = $x9->addInt32($j9); |
|
156 | - $x10 = $x10->addInt32($j10); |
|
157 | - $x11 = $x11->addInt32($j11); |
|
158 | - $x12 = $x12->addInt32($j12); |
|
159 | - $x13 = $x13->addInt32($j13); |
|
160 | - $x14 = $x14->addInt32($j14); |
|
161 | - $x15 = $x15->addInt32($j15); |
|
146 | + $x0 = $x0->addInt32( $j0 ); |
|
147 | + $x1 = $x1->addInt32( $j1 ); |
|
148 | + $x2 = $x2->addInt32( $j2 ); |
|
149 | + $x3 = $x3->addInt32( $j3 ); |
|
150 | + $x4 = $x4->addInt32( $j4 ); |
|
151 | + $x5 = $x5->addInt32( $j5 ); |
|
152 | + $x6 = $x6->addInt32( $j6 ); |
|
153 | + $x7 = $x7->addInt32( $j7 ); |
|
154 | + $x8 = $x8->addInt32( $j8 ); |
|
155 | + $x9 = $x9->addInt32( $j9 ); |
|
156 | + $x10 = $x10->addInt32( $j10 ); |
|
157 | + $x11 = $x11->addInt32( $j11 ); |
|
158 | + $x12 = $x12->addInt32( $j12 ); |
|
159 | + $x13 = $x13->addInt32( $j13 ); |
|
160 | + $x14 = $x14->addInt32( $j14 ); |
|
161 | + $x15 = $x15->addInt32( $j15 ); |
|
162 | 162 | |
163 | 163 | return $x0->toReverseString() . |
164 | 164 | $x1->toReverseString() . |
@@ -186,35 +186,35 @@ discard block |
||
186 | 186 | * @param string $key |
187 | 187 | * @return string |
188 | 188 | */ |
189 | - public static function salsa20($len, $nonce, $key) |
|
189 | + public static function salsa20( $len, $nonce, $key ) |
|
190 | 190 | { |
191 | - if (self::strlen($key) !== 32) { |
|
192 | - throw new RangeException('Key must be 32 bytes long'); |
|
191 | + if ( self::strlen( $key ) !== 32 ) { |
|
192 | + throw new RangeException( 'Key must be 32 bytes long' ); |
|
193 | 193 | } |
194 | 194 | $kcopy = '' . $key; |
195 | - $in = self::substr($nonce, 0, 8) . str_repeat("\0", 8); |
|
195 | + $in = self::substr( $nonce, 0, 8 ) . str_repeat( "\0", 8 ); |
|
196 | 196 | $c = ''; |
197 | - while ($len >= 64) { |
|
198 | - $c .= self::core_salsa20($in, $kcopy, null); |
|
197 | + while ( $len >= 64 ) { |
|
198 | + $c .= self::core_salsa20( $in, $kcopy, null ); |
|
199 | 199 | $u = 1; |
200 | 200 | // Internal counter. |
201 | - for ($i = 8; $i < 16; ++$i) { |
|
202 | - $u += self::chrToInt($in[$i]); |
|
203 | - $in[$i] = self::intToChr($u & 0xff); |
|
201 | + for ( $i = 8; $i < 16; ++$i ) { |
|
202 | + $u += self::chrToInt( $in[$i] ); |
|
203 | + $in[$i] = self::intToChr( $u & 0xff ); |
|
204 | 204 | $u >>= 8; |
205 | 205 | } |
206 | 206 | $len -= 64; |
207 | 207 | } |
208 | - if ($len > 0) { |
|
208 | + if ( $len > 0 ) { |
|
209 | 209 | $c .= self::substr( |
210 | - self::core_salsa20($in, $kcopy, null), |
|
210 | + self::core_salsa20( $in, $kcopy, null ), |
|
211 | 211 | 0, |
212 | 212 | $len |
213 | 213 | ); |
214 | 214 | } |
215 | 215 | try { |
216 | - ParagonIE_Sodium_Compat::memzero($kcopy); |
|
217 | - } catch (Error $ex) { |
|
216 | + ParagonIE_Sodium_Compat::memzero( $kcopy ); |
|
217 | + } catch ( Error $ex ) { |
|
218 | 218 | $kcopy = null; |
219 | 219 | } |
220 | 220 | return $c; |
@@ -229,46 +229,46 @@ discard block |
||
229 | 229 | * @param string $k |
230 | 230 | * @return string |
231 | 231 | */ |
232 | - public static function salsa20_xor_ic($m, $n, $ic, $k) |
|
232 | + public static function salsa20_xor_ic( $m, $n, $ic, $k ) |
|
233 | 233 | { |
234 | - $mlen = self::strlen($m); |
|
235 | - if ($mlen < 1) { |
|
234 | + $mlen = self::strlen( $m ); |
|
235 | + if ( $mlen < 1 ) { |
|
236 | 236 | return ''; |
237 | 237 | } |
238 | - $kcopy = self::substr($k, 0, 32); |
|
239 | - $in = self::substr($n, 0, 8); |
|
238 | + $kcopy = self::substr( $k, 0, 32 ); |
|
239 | + $in = self::substr( $n, 0, 8 ); |
|
240 | 240 | // Initialize the counter |
241 | - $in .= ParagonIE_Sodium_Core32_Util::store64_le($ic); |
|
241 | + $in .= ParagonIE_Sodium_Core32_Util::store64_le( $ic ); |
|
242 | 242 | |
243 | 243 | $c = ''; |
244 | - while ($mlen >= 64) { |
|
245 | - $block = self::core_salsa20($in, $kcopy, null); |
|
244 | + while ( $mlen >= 64 ) { |
|
245 | + $block = self::core_salsa20( $in, $kcopy, null ); |
|
246 | 246 | $c .= self::xorStrings( |
247 | - self::substr($m, 0, 64), |
|
248 | - self::substr($block, 0, 64) |
|
247 | + self::substr( $m, 0, 64 ), |
|
248 | + self::substr( $block, 0, 64 ) |
|
249 | 249 | ); |
250 | 250 | $u = 1; |
251 | - for ($i = 8; $i < 16; ++$i) { |
|
252 | - $u += self::chrToInt($in[$i]); |
|
253 | - $in[$i] = self::intToChr($u & 0xff); |
|
251 | + for ( $i = 8; $i < 16; ++$i ) { |
|
252 | + $u += self::chrToInt( $in[$i] ); |
|
253 | + $in[$i] = self::intToChr( $u & 0xff ); |
|
254 | 254 | $u >>= 8; |
255 | 255 | } |
256 | 256 | |
257 | 257 | $mlen -= 64; |
258 | - $m = self::substr($m, 64); |
|
258 | + $m = self::substr( $m, 64 ); |
|
259 | 259 | } |
260 | 260 | |
261 | - if ($mlen) { |
|
262 | - $block = self::core_salsa20($in, $kcopy, null); |
|
261 | + if ( $mlen ) { |
|
262 | + $block = self::core_salsa20( $in, $kcopy, null ); |
|
263 | 263 | $c .= self::xorStrings( |
264 | - self::substr($m, 0, $mlen), |
|
265 | - self::substr($block, 0, $mlen) |
|
264 | + self::substr( $m, 0, $mlen ), |
|
265 | + self::substr( $block, 0, $mlen ) |
|
266 | 266 | ); |
267 | 267 | } |
268 | 268 | try { |
269 | - ParagonIE_Sodium_Compat::memzero($block); |
|
270 | - ParagonIE_Sodium_Compat::memzero($kcopy); |
|
271 | - } catch (Error $ex) { |
|
269 | + ParagonIE_Sodium_Compat::memzero( $block ); |
|
270 | + ParagonIE_Sodium_Compat::memzero( $kcopy ); |
|
271 | + } catch ( Error $ex ) { |
|
272 | 272 | $block = null; |
273 | 273 | $kcopy = null; |
274 | 274 | } |
@@ -284,12 +284,12 @@ discard block |
||
284 | 284 | * @param string $key |
285 | 285 | * @return string |
286 | 286 | */ |
287 | - public static function salsa20_xor($message, $nonce, $key) |
|
287 | + public static function salsa20_xor( $message, $nonce, $key ) |
|
288 | 288 | { |
289 | 289 | return self::xorStrings( |
290 | 290 | $message, |
291 | 291 | self::salsa20( |
292 | - self::strlen($message), |
|
292 | + self::strlen( $message ), |
|
293 | 293 | $nonce, |
294 | 294 | $key |
295 | 295 | ) |
@@ -7,8 +7,7 @@ discard block |
||
7 | 7 | /** |
8 | 8 | * Class ParagonIE_Sodium_Core32_Salsa20 |
9 | 9 | */ |
10 | -abstract class ParagonIE_Sodium_Core32_Salsa20 extends ParagonIE_Sodium_Core32_Util |
|
11 | -{ |
|
10 | +abstract class ParagonIE_Sodium_Core32_Salsa20 extends ParagonIE_Sodium_Core32_Util { |
|
12 | 11 | const ROUNDS = 20; |
13 | 12 | |
14 | 13 | /** |
@@ -21,8 +20,7 @@ discard block |
||
21 | 20 | * @param string|null $c |
22 | 21 | * @return string |
23 | 22 | */ |
24 | - public static function core_salsa20($in, $k, $c = null) |
|
25 | - { |
|
23 | + public static function core_salsa20($in, $k, $c = null) { |
|
26 | 24 | /** |
27 | 25 | * @var ParagonIE_Sodium_Core32_Int32 $x0 |
28 | 26 | * @var ParagonIE_Sodium_Core32_Int32 $x1 |
@@ -186,8 +184,7 @@ discard block |
||
186 | 184 | * @param string $key |
187 | 185 | * @return string |
188 | 186 | */ |
189 | - public static function salsa20($len, $nonce, $key) |
|
190 | - { |
|
187 | + public static function salsa20($len, $nonce, $key) { |
|
191 | 188 | if (self::strlen($key) !== 32) { |
192 | 189 | throw new RangeException('Key must be 32 bytes long'); |
193 | 190 | } |
@@ -229,8 +226,7 @@ discard block |
||
229 | 226 | * @param string $k |
230 | 227 | * @return string |
231 | 228 | */ |
232 | - public static function salsa20_xor_ic($m, $n, $ic, $k) |
|
233 | - { |
|
229 | + public static function salsa20_xor_ic($m, $n, $ic, $k) { |
|
234 | 230 | $mlen = self::strlen($m); |
235 | 231 | if ($mlen < 1) { |
236 | 232 | return ''; |
@@ -284,8 +280,7 @@ discard block |
||
284 | 280 | * @param string $key |
285 | 281 | * @return string |
286 | 282 | */ |
287 | - public static function salsa20_xor($message, $nonce, $key) |
|
288 | - { |
|
283 | + public static function salsa20_xor($message, $nonce, $key) { |
|
289 | 284 | return self::xorStrings( |
290 | 285 | $message, |
291 | 286 | self::salsa20( |
@@ -1,7 +1,7 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | if (class_exists('ParagonIE_Sodium_Core32_XSalsa20', false)) { |
4 | - return; |
|
4 | + return; |
|
5 | 5 | } |
6 | 6 | |
7 | 7 | /** |
@@ -9,45 +9,45 @@ discard block |
||
9 | 9 | */ |
10 | 10 | abstract class ParagonIE_Sodium_Core32_XSalsa20 extends ParagonIE_Sodium_Core32_HSalsa20 |
11 | 11 | { |
12 | - /** |
|
13 | - * Expand a key and nonce into an xsalsa20 keystream. |
|
14 | - * |
|
15 | - * @internal You should not use this directly from another application |
|
16 | - * |
|
17 | - * @param int $len |
|
18 | - * @param string $nonce |
|
19 | - * @param string $key |
|
20 | - * @return string |
|
21 | - */ |
|
22 | - public static function xsalsa20($len, $nonce, $key) |
|
23 | - { |
|
24 | - $ret = self::salsa20( |
|
25 | - $len, |
|
26 | - self::substr($nonce, 16, 8), |
|
27 | - self::hsalsa20($nonce, $key) |
|
28 | - ); |
|
29 | - return $ret; |
|
30 | - } |
|
12 | + /** |
|
13 | + * Expand a key and nonce into an xsalsa20 keystream. |
|
14 | + * |
|
15 | + * @internal You should not use this directly from another application |
|
16 | + * |
|
17 | + * @param int $len |
|
18 | + * @param string $nonce |
|
19 | + * @param string $key |
|
20 | + * @return string |
|
21 | + */ |
|
22 | + public static function xsalsa20($len, $nonce, $key) |
|
23 | + { |
|
24 | + $ret = self::salsa20( |
|
25 | + $len, |
|
26 | + self::substr($nonce, 16, 8), |
|
27 | + self::hsalsa20($nonce, $key) |
|
28 | + ); |
|
29 | + return $ret; |
|
30 | + } |
|
31 | 31 | |
32 | - /** |
|
33 | - * Encrypt a string with XSalsa20. Doesn't provide integrity. |
|
34 | - * |
|
35 | - * @internal You should not use this directly from another application |
|
36 | - * |
|
37 | - * @param string $message |
|
38 | - * @param string $nonce |
|
39 | - * @param string $key |
|
40 | - * @return string |
|
41 | - */ |
|
42 | - public static function xsalsa20_xor($message, $nonce, $key) |
|
43 | - { |
|
44 | - return self::xorStrings( |
|
45 | - $message, |
|
46 | - self::xsalsa20( |
|
47 | - self::strlen($message), |
|
48 | - $nonce, |
|
49 | - $key |
|
50 | - ) |
|
51 | - ); |
|
52 | - } |
|
32 | + /** |
|
33 | + * Encrypt a string with XSalsa20. Doesn't provide integrity. |
|
34 | + * |
|
35 | + * @internal You should not use this directly from another application |
|
36 | + * |
|
37 | + * @param string $message |
|
38 | + * @param string $nonce |
|
39 | + * @param string $key |
|
40 | + * @return string |
|
41 | + */ |
|
42 | + public static function xsalsa20_xor($message, $nonce, $key) |
|
43 | + { |
|
44 | + return self::xorStrings( |
|
45 | + $message, |
|
46 | + self::xsalsa20( |
|
47 | + self::strlen($message), |
|
48 | + $nonce, |
|
49 | + $key |
|
50 | + ) |
|
51 | + ); |
|
52 | + } |
|
53 | 53 | } |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -if (class_exists('ParagonIE_Sodium_Core32_XSalsa20', false)) { |
|
3 | +if ( class_exists( 'ParagonIE_Sodium_Core32_XSalsa20', false ) ) { |
|
4 | 4 | return; |
5 | 5 | } |
6 | 6 | |
@@ -19,12 +19,12 @@ discard block |
||
19 | 19 | * @param string $key |
20 | 20 | * @return string |
21 | 21 | */ |
22 | - public static function xsalsa20($len, $nonce, $key) |
|
22 | + public static function xsalsa20( $len, $nonce, $key ) |
|
23 | 23 | { |
24 | 24 | $ret = self::salsa20( |
25 | 25 | $len, |
26 | - self::substr($nonce, 16, 8), |
|
27 | - self::hsalsa20($nonce, $key) |
|
26 | + self::substr( $nonce, 16, 8 ), |
|
27 | + self::hsalsa20( $nonce, $key ) |
|
28 | 28 | ); |
29 | 29 | return $ret; |
30 | 30 | } |
@@ -39,12 +39,12 @@ discard block |
||
39 | 39 | * @param string $key |
40 | 40 | * @return string |
41 | 41 | */ |
42 | - public static function xsalsa20_xor($message, $nonce, $key) |
|
42 | + public static function xsalsa20_xor( $message, $nonce, $key ) |
|
43 | 43 | { |
44 | 44 | return self::xorStrings( |
45 | 45 | $message, |
46 | 46 | self::xsalsa20( |
47 | - self::strlen($message), |
|
47 | + self::strlen( $message ), |
|
48 | 48 | $nonce, |
49 | 49 | $key |
50 | 50 | ) |
@@ -7,8 +7,7 @@ discard block |
||
7 | 7 | /** |
8 | 8 | * Class ParagonIE_Sodium_Core32_XSalsa20 |
9 | 9 | */ |
10 | -abstract class ParagonIE_Sodium_Core32_XSalsa20 extends ParagonIE_Sodium_Core32_HSalsa20 |
|
11 | -{ |
|
10 | +abstract class ParagonIE_Sodium_Core32_XSalsa20 extends ParagonIE_Sodium_Core32_HSalsa20 { |
|
12 | 11 | /** |
13 | 12 | * Expand a key and nonce into an xsalsa20 keystream. |
14 | 13 | * |
@@ -19,8 +18,7 @@ discard block |
||
19 | 18 | * @param string $key |
20 | 19 | * @return string |
21 | 20 | */ |
22 | - public static function xsalsa20($len, $nonce, $key) |
|
23 | - { |
|
21 | + public static function xsalsa20($len, $nonce, $key) { |
|
24 | 22 | $ret = self::salsa20( |
25 | 23 | $len, |
26 | 24 | self::substr($nonce, 16, 8), |
@@ -39,8 +37,7 @@ discard block |
||
39 | 37 | * @param string $key |
40 | 38 | * @return string |
41 | 39 | */ |
42 | - public static function xsalsa20_xor($message, $nonce, $key) |
|
43 | - { |
|
40 | + public static function xsalsa20_xor($message, $nonce, $key) { |
|
44 | 41 | return self::xorStrings( |
45 | 42 | $message, |
46 | 43 | self::xsalsa20( |
@@ -1,7 +1,7 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | if (class_exists('ParagonIE_Sodium_Core32_ChaCha20', false)) { |
4 | - return; |
|
4 | + return; |
|
5 | 5 | } |
6 | 6 | |
7 | 7 | /** |
@@ -9,82 +9,82 @@ discard block |
||
9 | 9 | */ |
10 | 10 | class ParagonIE_Sodium_Core32_ChaCha20 extends ParagonIE_Sodium_Core32_Util |
11 | 11 | { |
12 | - /** |
|
13 | - * The ChaCha20 quarter round function. Works on four 32-bit integers. |
|
14 | - * |
|
15 | - * @internal You should not use this directly from another application |
|
16 | - * |
|
17 | - * @param ParagonIE_Sodium_Core32_Int32 $a |
|
18 | - * @param ParagonIE_Sodium_Core32_Int32 $b |
|
19 | - * @param ParagonIE_Sodium_Core32_Int32 $c |
|
20 | - * @param ParagonIE_Sodium_Core32_Int32 $d |
|
21 | - * @return array<int, ParagonIE_Sodium_Core32_Int32> |
|
22 | - */ |
|
23 | - protected static function quarterRound( |
|
24 | - ParagonIE_Sodium_Core32_Int32 $a, |
|
25 | - ParagonIE_Sodium_Core32_Int32 $b, |
|
26 | - ParagonIE_Sodium_Core32_Int32 $c, |
|
27 | - ParagonIE_Sodium_Core32_Int32 $d |
|
28 | - ) { |
|
29 | - /** @var ParagonIE_Sodium_Core32_Int32 $a */ |
|
30 | - /** @var ParagonIE_Sodium_Core32_Int32 $b */ |
|
31 | - /** @var ParagonIE_Sodium_Core32_Int32 $c */ |
|
32 | - /** @var ParagonIE_Sodium_Core32_Int32 $d */ |
|
12 | + /** |
|
13 | + * The ChaCha20 quarter round function. Works on four 32-bit integers. |
|
14 | + * |
|
15 | + * @internal You should not use this directly from another application |
|
16 | + * |
|
17 | + * @param ParagonIE_Sodium_Core32_Int32 $a |
|
18 | + * @param ParagonIE_Sodium_Core32_Int32 $b |
|
19 | + * @param ParagonIE_Sodium_Core32_Int32 $c |
|
20 | + * @param ParagonIE_Sodium_Core32_Int32 $d |
|
21 | + * @return array<int, ParagonIE_Sodium_Core32_Int32> |
|
22 | + */ |
|
23 | + protected static function quarterRound( |
|
24 | + ParagonIE_Sodium_Core32_Int32 $a, |
|
25 | + ParagonIE_Sodium_Core32_Int32 $b, |
|
26 | + ParagonIE_Sodium_Core32_Int32 $c, |
|
27 | + ParagonIE_Sodium_Core32_Int32 $d |
|
28 | + ) { |
|
29 | + /** @var ParagonIE_Sodium_Core32_Int32 $a */ |
|
30 | + /** @var ParagonIE_Sodium_Core32_Int32 $b */ |
|
31 | + /** @var ParagonIE_Sodium_Core32_Int32 $c */ |
|
32 | + /** @var ParagonIE_Sodium_Core32_Int32 $d */ |
|
33 | 33 | |
34 | - # a = PLUS(a,b); d = ROTATE(XOR(d,a),16); |
|
35 | - $a = $a->addInt32($b); |
|
36 | - $d = $d->xorInt32($a)->rotateLeft(16); |
|
34 | + # a = PLUS(a,b); d = ROTATE(XOR(d,a),16); |
|
35 | + $a = $a->addInt32($b); |
|
36 | + $d = $d->xorInt32($a)->rotateLeft(16); |
|
37 | 37 | |
38 | - # c = PLUS(c,d); b = ROTATE(XOR(b,c),12); |
|
39 | - $c = $c->addInt32($d); |
|
40 | - $b = $b->xorInt32($c)->rotateLeft(12); |
|
38 | + # c = PLUS(c,d); b = ROTATE(XOR(b,c),12); |
|
39 | + $c = $c->addInt32($d); |
|
40 | + $b = $b->xorInt32($c)->rotateLeft(12); |
|
41 | 41 | |
42 | - # a = PLUS(a,b); d = ROTATE(XOR(d,a), 8); |
|
43 | - $a = $a->addInt32($b); |
|
44 | - $d = $d->xorInt32($a)->rotateLeft(8); |
|
42 | + # a = PLUS(a,b); d = ROTATE(XOR(d,a), 8); |
|
43 | + $a = $a->addInt32($b); |
|
44 | + $d = $d->xorInt32($a)->rotateLeft(8); |
|
45 | 45 | |
46 | - # c = PLUS(c,d); b = ROTATE(XOR(b,c), 7); |
|
47 | - $c = $c->addInt32($d); |
|
48 | - $b = $b->xorInt32($c)->rotateLeft(7); |
|
46 | + # c = PLUS(c,d); b = ROTATE(XOR(b,c), 7); |
|
47 | + $c = $c->addInt32($d); |
|
48 | + $b = $b->xorInt32($c)->rotateLeft(7); |
|
49 | 49 | |
50 | - return array($a, $b, $c, $d); |
|
51 | - } |
|
50 | + return array($a, $b, $c, $d); |
|
51 | + } |
|
52 | 52 | |
53 | - /** |
|
54 | - * @internal You should not use this directly from another application |
|
55 | - * |
|
56 | - * @param ParagonIE_Sodium_Core32_ChaCha20_Ctx $ctx |
|
57 | - * @param string $message |
|
58 | - * |
|
59 | - * @return string |
|
60 | - * @throws Exception |
|
61 | - */ |
|
62 | - public static function encryptBytes( |
|
63 | - ParagonIE_Sodium_Core32_ChaCha20_Ctx $ctx, |
|
64 | - $message = '' |
|
65 | - ) { |
|
66 | - $bytes = self::strlen($message); |
|
53 | + /** |
|
54 | + * @internal You should not use this directly from another application |
|
55 | + * |
|
56 | + * @param ParagonIE_Sodium_Core32_ChaCha20_Ctx $ctx |
|
57 | + * @param string $message |
|
58 | + * |
|
59 | + * @return string |
|
60 | + * @throws Exception |
|
61 | + */ |
|
62 | + public static function encryptBytes( |
|
63 | + ParagonIE_Sodium_Core32_ChaCha20_Ctx $ctx, |
|
64 | + $message = '' |
|
65 | + ) { |
|
66 | + $bytes = self::strlen($message); |
|
67 | 67 | |
68 | - /** |
|
69 | - * @var ParagonIE_Sodium_Core32_Int32 $x0 |
|
70 | - * @var ParagonIE_Sodium_Core32_Int32 $x1 |
|
71 | - * @var ParagonIE_Sodium_Core32_Int32 $x2 |
|
72 | - * @var ParagonIE_Sodium_Core32_Int32 $x3 |
|
73 | - * @var ParagonIE_Sodium_Core32_Int32 $x4 |
|
74 | - * @var ParagonIE_Sodium_Core32_Int32 $x5 |
|
75 | - * @var ParagonIE_Sodium_Core32_Int32 $x6 |
|
76 | - * @var ParagonIE_Sodium_Core32_Int32 $x7 |
|
77 | - * @var ParagonIE_Sodium_Core32_Int32 $x8 |
|
78 | - * @var ParagonIE_Sodium_Core32_Int32 $x9 |
|
79 | - * @var ParagonIE_Sodium_Core32_Int32 $x10 |
|
80 | - * @var ParagonIE_Sodium_Core32_Int32 $x11 |
|
81 | - * @var ParagonIE_Sodium_Core32_Int32 $x12 |
|
82 | - * @var ParagonIE_Sodium_Core32_Int32 $x13 |
|
83 | - * @var ParagonIE_Sodium_Core32_Int32 $x14 |
|
84 | - * @var ParagonIE_Sodium_Core32_Int32 $x15 |
|
85 | - */ |
|
68 | + /** |
|
69 | + * @var ParagonIE_Sodium_Core32_Int32 $x0 |
|
70 | + * @var ParagonIE_Sodium_Core32_Int32 $x1 |
|
71 | + * @var ParagonIE_Sodium_Core32_Int32 $x2 |
|
72 | + * @var ParagonIE_Sodium_Core32_Int32 $x3 |
|
73 | + * @var ParagonIE_Sodium_Core32_Int32 $x4 |
|
74 | + * @var ParagonIE_Sodium_Core32_Int32 $x5 |
|
75 | + * @var ParagonIE_Sodium_Core32_Int32 $x6 |
|
76 | + * @var ParagonIE_Sodium_Core32_Int32 $x7 |
|
77 | + * @var ParagonIE_Sodium_Core32_Int32 $x8 |
|
78 | + * @var ParagonIE_Sodium_Core32_Int32 $x9 |
|
79 | + * @var ParagonIE_Sodium_Core32_Int32 $x10 |
|
80 | + * @var ParagonIE_Sodium_Core32_Int32 $x11 |
|
81 | + * @var ParagonIE_Sodium_Core32_Int32 $x12 |
|
82 | + * @var ParagonIE_Sodium_Core32_Int32 $x13 |
|
83 | + * @var ParagonIE_Sodium_Core32_Int32 $x14 |
|
84 | + * @var ParagonIE_Sodium_Core32_Int32 $x15 |
|
85 | + */ |
|
86 | 86 | |
87 | - /* |
|
87 | + /* |
|
88 | 88 | j0 = ctx->input[0]; |
89 | 89 | j1 = ctx->input[1]; |
90 | 90 | j2 = ctx->input[2]; |
@@ -102,73 +102,73 @@ discard block |
||
102 | 102 | j14 = ctx->input[14]; |
103 | 103 | j15 = ctx->input[15]; |
104 | 104 | */ |
105 | - $j0 = $ctx[0]; |
|
106 | - $j1 = $ctx[1]; |
|
107 | - $j2 = $ctx[2]; |
|
108 | - $j3 = $ctx[3]; |
|
109 | - $j4 = $ctx[4]; |
|
110 | - $j5 = $ctx[5]; |
|
111 | - $j6 = $ctx[6]; |
|
112 | - $j7 = $ctx[7]; |
|
113 | - $j8 = $ctx[8]; |
|
114 | - $j9 = $ctx[9]; |
|
115 | - $j10 = $ctx[10]; |
|
116 | - $j11 = $ctx[11]; |
|
117 | - $j12 = $ctx[12]; |
|
118 | - $j13 = $ctx[13]; |
|
119 | - $j14 = $ctx[14]; |
|
120 | - $j15 = $ctx[15]; |
|
105 | + $j0 = $ctx[0]; |
|
106 | + $j1 = $ctx[1]; |
|
107 | + $j2 = $ctx[2]; |
|
108 | + $j3 = $ctx[3]; |
|
109 | + $j4 = $ctx[4]; |
|
110 | + $j5 = $ctx[5]; |
|
111 | + $j6 = $ctx[6]; |
|
112 | + $j7 = $ctx[7]; |
|
113 | + $j8 = $ctx[8]; |
|
114 | + $j9 = $ctx[9]; |
|
115 | + $j10 = $ctx[10]; |
|
116 | + $j11 = $ctx[11]; |
|
117 | + $j12 = $ctx[12]; |
|
118 | + $j13 = $ctx[13]; |
|
119 | + $j14 = $ctx[14]; |
|
120 | + $j15 = $ctx[15]; |
|
121 | 121 | |
122 | - $c = ''; |
|
123 | - for (;;) { |
|
124 | - if ($bytes < 64) { |
|
125 | - $message .= str_repeat("\x00", 64 - $bytes); |
|
126 | - } |
|
122 | + $c = ''; |
|
123 | + for (;;) { |
|
124 | + if ($bytes < 64) { |
|
125 | + $message .= str_repeat("\x00", 64 - $bytes); |
|
126 | + } |
|
127 | 127 | |
128 | - $x0 = clone $j0; |
|
129 | - $x1 = clone $j1; |
|
130 | - $x2 = clone $j2; |
|
131 | - $x3 = clone $j3; |
|
132 | - $x4 = clone $j4; |
|
133 | - $x5 = clone $j5; |
|
134 | - $x6 = clone $j6; |
|
135 | - $x7 = clone $j7; |
|
136 | - $x8 = clone $j8; |
|
137 | - $x9 = clone $j9; |
|
138 | - $x10 = clone $j10; |
|
139 | - $x11 = clone $j11; |
|
140 | - $x12 = clone $j12; |
|
141 | - $x13 = clone $j13; |
|
142 | - $x14 = clone $j14; |
|
143 | - $x15 = clone $j15; |
|
128 | + $x0 = clone $j0; |
|
129 | + $x1 = clone $j1; |
|
130 | + $x2 = clone $j2; |
|
131 | + $x3 = clone $j3; |
|
132 | + $x4 = clone $j4; |
|
133 | + $x5 = clone $j5; |
|
134 | + $x6 = clone $j6; |
|
135 | + $x7 = clone $j7; |
|
136 | + $x8 = clone $j8; |
|
137 | + $x9 = clone $j9; |
|
138 | + $x10 = clone $j10; |
|
139 | + $x11 = clone $j11; |
|
140 | + $x12 = clone $j12; |
|
141 | + $x13 = clone $j13; |
|
142 | + $x14 = clone $j14; |
|
143 | + $x15 = clone $j15; |
|
144 | 144 | |
145 | - # for (i = 20; i > 0; i -= 2) { |
|
146 | - for ($i = 20; $i > 0; $i -= 2) { |
|
147 | - # QUARTERROUND( x0, x4, x8, x12) |
|
148 | - list($x0, $x4, $x8, $x12) = self::quarterRound($x0, $x4, $x8, $x12); |
|
145 | + # for (i = 20; i > 0; i -= 2) { |
|
146 | + for ($i = 20; $i > 0; $i -= 2) { |
|
147 | + # QUARTERROUND( x0, x4, x8, x12) |
|
148 | + list($x0, $x4, $x8, $x12) = self::quarterRound($x0, $x4, $x8, $x12); |
|
149 | 149 | |
150 | - # QUARTERROUND( x1, x5, x9, x13) |
|
151 | - list($x1, $x5, $x9, $x13) = self::quarterRound($x1, $x5, $x9, $x13); |
|
150 | + # QUARTERROUND( x1, x5, x9, x13) |
|
151 | + list($x1, $x5, $x9, $x13) = self::quarterRound($x1, $x5, $x9, $x13); |
|
152 | 152 | |
153 | - # QUARTERROUND( x2, x6, x10, x14) |
|
154 | - list($x2, $x6, $x10, $x14) = self::quarterRound($x2, $x6, $x10, $x14); |
|
153 | + # QUARTERROUND( x2, x6, x10, x14) |
|
154 | + list($x2, $x6, $x10, $x14) = self::quarterRound($x2, $x6, $x10, $x14); |
|
155 | 155 | |
156 | - # QUARTERROUND( x3, x7, x11, x15) |
|
157 | - list($x3, $x7, $x11, $x15) = self::quarterRound($x3, $x7, $x11, $x15); |
|
156 | + # QUARTERROUND( x3, x7, x11, x15) |
|
157 | + list($x3, $x7, $x11, $x15) = self::quarterRound($x3, $x7, $x11, $x15); |
|
158 | 158 | |
159 | - # QUARTERROUND( x0, x5, x10, x15) |
|
160 | - list($x0, $x5, $x10, $x15) = self::quarterRound($x0, $x5, $x10, $x15); |
|
159 | + # QUARTERROUND( x0, x5, x10, x15) |
|
160 | + list($x0, $x5, $x10, $x15) = self::quarterRound($x0, $x5, $x10, $x15); |
|
161 | 161 | |
162 | - # QUARTERROUND( x1, x6, x11, x12) |
|
163 | - list($x1, $x6, $x11, $x12) = self::quarterRound($x1, $x6, $x11, $x12); |
|
162 | + # QUARTERROUND( x1, x6, x11, x12) |
|
163 | + list($x1, $x6, $x11, $x12) = self::quarterRound($x1, $x6, $x11, $x12); |
|
164 | 164 | |
165 | - # QUARTERROUND( x2, x7, x8, x13) |
|
166 | - list($x2, $x7, $x8, $x13) = self::quarterRound($x2, $x7, $x8, $x13); |
|
165 | + # QUARTERROUND( x2, x7, x8, x13) |
|
166 | + list($x2, $x7, $x8, $x13) = self::quarterRound($x2, $x7, $x8, $x13); |
|
167 | 167 | |
168 | - # QUARTERROUND( x3, x4, x9, x14) |
|
169 | - list($x3, $x4, $x9, $x14) = self::quarterRound($x3, $x4, $x9, $x14); |
|
170 | - } |
|
171 | - /* |
|
168 | + # QUARTERROUND( x3, x4, x9, x14) |
|
169 | + list($x3, $x4, $x9, $x14) = self::quarterRound($x3, $x4, $x9, $x14); |
|
170 | + } |
|
171 | + /* |
|
172 | 172 | x0 = PLUS(x0, j0); |
173 | 173 | x1 = PLUS(x1, j1); |
174 | 174 | x2 = PLUS(x2, j2); |
@@ -186,24 +186,24 @@ discard block |
||
186 | 186 | x14 = PLUS(x14, j14); |
187 | 187 | x15 = PLUS(x15, j15); |
188 | 188 | */ |
189 | - $x0 = $x0->addInt32($j0); |
|
190 | - $x1 = $x1->addInt32($j1); |
|
191 | - $x2 = $x2->addInt32($j2); |
|
192 | - $x3 = $x3->addInt32($j3); |
|
193 | - $x4 = $x4->addInt32($j4); |
|
194 | - $x5 = $x5->addInt32($j5); |
|
195 | - $x6 = $x6->addInt32($j6); |
|
196 | - $x7 = $x7->addInt32($j7); |
|
197 | - $x8 = $x8->addInt32($j8); |
|
198 | - $x9 = $x9->addInt32($j9); |
|
199 | - $x10 = $x10->addInt32($j10); |
|
200 | - $x11 = $x11->addInt32($j11); |
|
201 | - $x12 = $x12->addInt32($j12); |
|
202 | - $x13 = $x13->addInt32($j13); |
|
203 | - $x14 = $x14->addInt32($j14); |
|
204 | - $x15 = $x15->addInt32($j15); |
|
189 | + $x0 = $x0->addInt32($j0); |
|
190 | + $x1 = $x1->addInt32($j1); |
|
191 | + $x2 = $x2->addInt32($j2); |
|
192 | + $x3 = $x3->addInt32($j3); |
|
193 | + $x4 = $x4->addInt32($j4); |
|
194 | + $x5 = $x5->addInt32($j5); |
|
195 | + $x6 = $x6->addInt32($j6); |
|
196 | + $x7 = $x7->addInt32($j7); |
|
197 | + $x8 = $x8->addInt32($j8); |
|
198 | + $x9 = $x9->addInt32($j9); |
|
199 | + $x10 = $x10->addInt32($j10); |
|
200 | + $x11 = $x11->addInt32($j11); |
|
201 | + $x12 = $x12->addInt32($j12); |
|
202 | + $x13 = $x13->addInt32($j13); |
|
203 | + $x14 = $x14->addInt32($j14); |
|
204 | + $x15 = $x15->addInt32($j15); |
|
205 | 205 | |
206 | - /* |
|
206 | + /* |
|
207 | 207 | x0 = XOR(x0, LOAD32_LE(m + 0)); |
208 | 208 | x1 = XOR(x1, LOAD32_LE(m + 4)); |
209 | 209 | x2 = XOR(x2, LOAD32_LE(m + 8)); |
@@ -221,36 +221,36 @@ discard block |
||
221 | 221 | x14 = XOR(x14, LOAD32_LE(m + 56)); |
222 | 222 | x15 = XOR(x15, LOAD32_LE(m + 60)); |
223 | 223 | */ |
224 | - $x0 = $x0->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 0, 4))); |
|
225 | - $x1 = $x1->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 4, 4))); |
|
226 | - $x2 = $x2->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 8, 4))); |
|
227 | - $x3 = $x3->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 12, 4))); |
|
228 | - $x4 = $x4->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 16, 4))); |
|
229 | - $x5 = $x5->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 20, 4))); |
|
230 | - $x6 = $x6->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 24, 4))); |
|
231 | - $x7 = $x7->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 28, 4))); |
|
232 | - $x8 = $x8->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 32, 4))); |
|
233 | - $x9 = $x9->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 36, 4))); |
|
234 | - $x10 = $x10->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 40, 4))); |
|
235 | - $x11 = $x11->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 44, 4))); |
|
236 | - $x12 = $x12->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 48, 4))); |
|
237 | - $x13 = $x13->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 52, 4))); |
|
238 | - $x14 = $x14->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 56, 4))); |
|
239 | - $x15 = $x15->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 60, 4))); |
|
224 | + $x0 = $x0->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 0, 4))); |
|
225 | + $x1 = $x1->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 4, 4))); |
|
226 | + $x2 = $x2->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 8, 4))); |
|
227 | + $x3 = $x3->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 12, 4))); |
|
228 | + $x4 = $x4->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 16, 4))); |
|
229 | + $x5 = $x5->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 20, 4))); |
|
230 | + $x6 = $x6->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 24, 4))); |
|
231 | + $x7 = $x7->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 28, 4))); |
|
232 | + $x8 = $x8->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 32, 4))); |
|
233 | + $x9 = $x9->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 36, 4))); |
|
234 | + $x10 = $x10->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 40, 4))); |
|
235 | + $x11 = $x11->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 44, 4))); |
|
236 | + $x12 = $x12->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 48, 4))); |
|
237 | + $x13 = $x13->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 52, 4))); |
|
238 | + $x14 = $x14->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 56, 4))); |
|
239 | + $x15 = $x15->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 60, 4))); |
|
240 | 240 | |
241 | - /* |
|
241 | + /* |
|
242 | 242 | j12 = PLUSONE(j12); |
243 | 243 | if (!j12) { |
244 | 244 | j13 = PLUSONE(j13); |
245 | 245 | } |
246 | 246 | */ |
247 | - /** @var ParagonIE_Sodium_Core32_Int32 $j12 */ |
|
248 | - $j12 = $j12->addInt(1); |
|
249 | - if ($j12->limbs[0] === 0 && $j12->limbs[1] === 0) { |
|
250 | - $j13 = $j13->addInt(1); |
|
251 | - } |
|
247 | + /** @var ParagonIE_Sodium_Core32_Int32 $j12 */ |
|
248 | + $j12 = $j12->addInt(1); |
|
249 | + if ($j12->limbs[0] === 0 && $j12->limbs[1] === 0) { |
|
250 | + $j13 = $j13->addInt(1); |
|
251 | + } |
|
252 | 252 | |
253 | - /* |
|
253 | + /* |
|
254 | 254 | STORE32_LE(c + 0, x0); |
255 | 255 | STORE32_LE(c + 4, x1); |
256 | 256 | STORE32_LE(c + 8, x2); |
@@ -269,107 +269,107 @@ discard block |
||
269 | 269 | STORE32_LE(c + 60, x15); |
270 | 270 | */ |
271 | 271 | |
272 | - $block = $x0->toReverseString() . |
|
273 | - $x1->toReverseString() . |
|
274 | - $x2->toReverseString() . |
|
275 | - $x3->toReverseString() . |
|
276 | - $x4->toReverseString() . |
|
277 | - $x5->toReverseString() . |
|
278 | - $x6->toReverseString() . |
|
279 | - $x7->toReverseString() . |
|
280 | - $x8->toReverseString() . |
|
281 | - $x9->toReverseString() . |
|
282 | - $x10->toReverseString() . |
|
283 | - $x11->toReverseString() . |
|
284 | - $x12->toReverseString() . |
|
285 | - $x13->toReverseString() . |
|
286 | - $x14->toReverseString() . |
|
287 | - $x15->toReverseString(); |
|
272 | + $block = $x0->toReverseString() . |
|
273 | + $x1->toReverseString() . |
|
274 | + $x2->toReverseString() . |
|
275 | + $x3->toReverseString() . |
|
276 | + $x4->toReverseString() . |
|
277 | + $x5->toReverseString() . |
|
278 | + $x6->toReverseString() . |
|
279 | + $x7->toReverseString() . |
|
280 | + $x8->toReverseString() . |
|
281 | + $x9->toReverseString() . |
|
282 | + $x10->toReverseString() . |
|
283 | + $x11->toReverseString() . |
|
284 | + $x12->toReverseString() . |
|
285 | + $x13->toReverseString() . |
|
286 | + $x14->toReverseString() . |
|
287 | + $x15->toReverseString(); |
|
288 | 288 | |
289 | - /* Partial block */ |
|
290 | - if ($bytes < 64) { |
|
291 | - $c .= self::substr($block, 0, $bytes); |
|
292 | - break; |
|
293 | - } |
|
289 | + /* Partial block */ |
|
290 | + if ($bytes < 64) { |
|
291 | + $c .= self::substr($block, 0, $bytes); |
|
292 | + break; |
|
293 | + } |
|
294 | 294 | |
295 | - /* Full block */ |
|
296 | - $c .= $block; |
|
297 | - $bytes -= 64; |
|
298 | - if ($bytes <= 0) { |
|
299 | - break; |
|
300 | - } |
|
301 | - $message = self::substr($message, 64); |
|
302 | - } |
|
303 | - /* end for(;;) loop */ |
|
295 | + /* Full block */ |
|
296 | + $c .= $block; |
|
297 | + $bytes -= 64; |
|
298 | + if ($bytes <= 0) { |
|
299 | + break; |
|
300 | + } |
|
301 | + $message = self::substr($message, 64); |
|
302 | + } |
|
303 | + /* end for(;;) loop */ |
|
304 | 304 | |
305 | - $ctx[12] = $j12; |
|
306 | - $ctx[13] = $j13; |
|
307 | - return $c; |
|
308 | - } |
|
305 | + $ctx[12] = $j12; |
|
306 | + $ctx[13] = $j13; |
|
307 | + return $c; |
|
308 | + } |
|
309 | 309 | |
310 | - /** |
|
311 | - * @internal You should not use this directly from another application |
|
312 | - * |
|
313 | - * @param int $len |
|
314 | - * @param string $nonce |
|
315 | - * @param string $key |
|
316 | - * @return string |
|
317 | - */ |
|
318 | - public static function stream($len = 64, $nonce = '', $key = '') |
|
319 | - { |
|
320 | - return self::encryptBytes( |
|
321 | - new ParagonIE_Sodium_Core32_ChaCha20_Ctx($key, $nonce), |
|
322 | - str_repeat("\x00", $len) |
|
323 | - ); |
|
324 | - } |
|
310 | + /** |
|
311 | + * @internal You should not use this directly from another application |
|
312 | + * |
|
313 | + * @param int $len |
|
314 | + * @param string $nonce |
|
315 | + * @param string $key |
|
316 | + * @return string |
|
317 | + */ |
|
318 | + public static function stream($len = 64, $nonce = '', $key = '') |
|
319 | + { |
|
320 | + return self::encryptBytes( |
|
321 | + new ParagonIE_Sodium_Core32_ChaCha20_Ctx($key, $nonce), |
|
322 | + str_repeat("\x00", $len) |
|
323 | + ); |
|
324 | + } |
|
325 | 325 | |
326 | - /** |
|
327 | - * @internal You should not use this directly from another application |
|
328 | - * |
|
329 | - * @param int $len |
|
330 | - * @param string $nonce |
|
331 | - * @param string $key |
|
332 | - * @return string |
|
333 | - */ |
|
334 | - public static function ietfStream($len, $nonce = '', $key = '') |
|
335 | - { |
|
336 | - return self::encryptBytes( |
|
337 | - new ParagonIE_Sodium_Core32_ChaCha20_IetfCtx($key, $nonce), |
|
338 | - str_repeat("\x00", $len) |
|
339 | - ); |
|
340 | - } |
|
326 | + /** |
|
327 | + * @internal You should not use this directly from another application |
|
328 | + * |
|
329 | + * @param int $len |
|
330 | + * @param string $nonce |
|
331 | + * @param string $key |
|
332 | + * @return string |
|
333 | + */ |
|
334 | + public static function ietfStream($len, $nonce = '', $key = '') |
|
335 | + { |
|
336 | + return self::encryptBytes( |
|
337 | + new ParagonIE_Sodium_Core32_ChaCha20_IetfCtx($key, $nonce), |
|
338 | + str_repeat("\x00", $len) |
|
339 | + ); |
|
340 | + } |
|
341 | 341 | |
342 | - /** |
|
343 | - * @internal You should not use this directly from another application |
|
344 | - * |
|
345 | - * @param string $message |
|
346 | - * @param string $nonce |
|
347 | - * @param string $key |
|
348 | - * @param string $ic |
|
349 | - * @return string |
|
350 | - */ |
|
351 | - public static function ietfStreamXorIc($message, $nonce = '', $key = '', $ic = '') |
|
352 | - { |
|
353 | - return self::encryptBytes( |
|
354 | - new ParagonIE_Sodium_Core32_ChaCha20_IetfCtx($key, $nonce, $ic), |
|
355 | - $message |
|
356 | - ); |
|
357 | - } |
|
342 | + /** |
|
343 | + * @internal You should not use this directly from another application |
|
344 | + * |
|
345 | + * @param string $message |
|
346 | + * @param string $nonce |
|
347 | + * @param string $key |
|
348 | + * @param string $ic |
|
349 | + * @return string |
|
350 | + */ |
|
351 | + public static function ietfStreamXorIc($message, $nonce = '', $key = '', $ic = '') |
|
352 | + { |
|
353 | + return self::encryptBytes( |
|
354 | + new ParagonIE_Sodium_Core32_ChaCha20_IetfCtx($key, $nonce, $ic), |
|
355 | + $message |
|
356 | + ); |
|
357 | + } |
|
358 | 358 | |
359 | - /** |
|
360 | - * @internal You should not use this directly from another application |
|
361 | - * |
|
362 | - * @param string $message |
|
363 | - * @param string $nonce |
|
364 | - * @param string $key |
|
365 | - * @param string $ic |
|
366 | - * @return string |
|
367 | - */ |
|
368 | - public static function streamXorIc($message, $nonce = '', $key = '', $ic = '') |
|
369 | - { |
|
370 | - return self::encryptBytes( |
|
371 | - new ParagonIE_Sodium_Core32_ChaCha20_Ctx($key, $nonce, $ic), |
|
372 | - $message |
|
373 | - ); |
|
374 | - } |
|
359 | + /** |
|
360 | + * @internal You should not use this directly from another application |
|
361 | + * |
|
362 | + * @param string $message |
|
363 | + * @param string $nonce |
|
364 | + * @param string $key |
|
365 | + * @param string $ic |
|
366 | + * @return string |
|
367 | + */ |
|
368 | + public static function streamXorIc($message, $nonce = '', $key = '', $ic = '') |
|
369 | + { |
|
370 | + return self::encryptBytes( |
|
371 | + new ParagonIE_Sodium_Core32_ChaCha20_Ctx($key, $nonce, $ic), |
|
372 | + $message |
|
373 | + ); |
|
374 | + } |
|
375 | 375 | } |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -if (class_exists('ParagonIE_Sodium_Core32_ChaCha20', false)) { |
|
3 | +if ( class_exists( 'ParagonIE_Sodium_Core32_ChaCha20', false ) ) { |
|
4 | 4 | return; |
5 | 5 | } |
6 | 6 | |
@@ -32,22 +32,22 @@ discard block |
||
32 | 32 | /** @var ParagonIE_Sodium_Core32_Int32 $d */ |
33 | 33 | |
34 | 34 | # a = PLUS(a,b); d = ROTATE(XOR(d,a),16); |
35 | - $a = $a->addInt32($b); |
|
36 | - $d = $d->xorInt32($a)->rotateLeft(16); |
|
35 | + $a = $a->addInt32( $b ); |
|
36 | + $d = $d->xorInt32( $a )->rotateLeft( 16 ); |
|
37 | 37 | |
38 | 38 | # c = PLUS(c,d); b = ROTATE(XOR(b,c),12); |
39 | - $c = $c->addInt32($d); |
|
40 | - $b = $b->xorInt32($c)->rotateLeft(12); |
|
39 | + $c = $c->addInt32( $d ); |
|
40 | + $b = $b->xorInt32( $c )->rotateLeft( 12 ); |
|
41 | 41 | |
42 | 42 | # a = PLUS(a,b); d = ROTATE(XOR(d,a), 8); |
43 | - $a = $a->addInt32($b); |
|
44 | - $d = $d->xorInt32($a)->rotateLeft(8); |
|
43 | + $a = $a->addInt32( $b ); |
|
44 | + $d = $d->xorInt32( $a )->rotateLeft( 8 ); |
|
45 | 45 | |
46 | 46 | # c = PLUS(c,d); b = ROTATE(XOR(b,c), 7); |
47 | - $c = $c->addInt32($d); |
|
48 | - $b = $b->xorInt32($c)->rotateLeft(7); |
|
47 | + $c = $c->addInt32( $d ); |
|
48 | + $b = $b->xorInt32( $c )->rotateLeft( 7 ); |
|
49 | 49 | |
50 | - return array($a, $b, $c, $d); |
|
50 | + return array( $a, $b, $c, $d ); |
|
51 | 51 | } |
52 | 52 | |
53 | 53 | /** |
@@ -63,7 +63,7 @@ discard block |
||
63 | 63 | ParagonIE_Sodium_Core32_ChaCha20_Ctx $ctx, |
64 | 64 | $message = '' |
65 | 65 | ) { |
66 | - $bytes = self::strlen($message); |
|
66 | + $bytes = self::strlen( $message ); |
|
67 | 67 | |
68 | 68 | /** |
69 | 69 | * @var ParagonIE_Sodium_Core32_Int32 $x0 |
@@ -120,21 +120,21 @@ discard block |
||
120 | 120 | $j15 = $ctx[15]; |
121 | 121 | |
122 | 122 | $c = ''; |
123 | - for (;;) { |
|
124 | - if ($bytes < 64) { |
|
125 | - $message .= str_repeat("\x00", 64 - $bytes); |
|
123 | + for ( ;; ) { |
|
124 | + if ( $bytes < 64 ) { |
|
125 | + $message .= str_repeat( "\x00", 64 - $bytes ); |
|
126 | 126 | } |
127 | 127 | |
128 | - $x0 = clone $j0; |
|
129 | - $x1 = clone $j1; |
|
130 | - $x2 = clone $j2; |
|
131 | - $x3 = clone $j3; |
|
132 | - $x4 = clone $j4; |
|
133 | - $x5 = clone $j5; |
|
134 | - $x6 = clone $j6; |
|
135 | - $x7 = clone $j7; |
|
136 | - $x8 = clone $j8; |
|
137 | - $x9 = clone $j9; |
|
128 | + $x0 = clone $j0; |
|
129 | + $x1 = clone $j1; |
|
130 | + $x2 = clone $j2; |
|
131 | + $x3 = clone $j3; |
|
132 | + $x4 = clone $j4; |
|
133 | + $x5 = clone $j5; |
|
134 | + $x6 = clone $j6; |
|
135 | + $x7 = clone $j7; |
|
136 | + $x8 = clone $j8; |
|
137 | + $x9 = clone $j9; |
|
138 | 138 | $x10 = clone $j10; |
139 | 139 | $x11 = clone $j11; |
140 | 140 | $x12 = clone $j12; |
@@ -143,30 +143,30 @@ discard block |
||
143 | 143 | $x15 = clone $j15; |
144 | 144 | |
145 | 145 | # for (i = 20; i > 0; i -= 2) { |
146 | - for ($i = 20; $i > 0; $i -= 2) { |
|
146 | + for ( $i = 20; $i > 0; $i -= 2 ) { |
|
147 | 147 | # QUARTERROUND( x0, x4, x8, x12) |
148 | - list($x0, $x4, $x8, $x12) = self::quarterRound($x0, $x4, $x8, $x12); |
|
148 | + list( $x0, $x4, $x8, $x12 ) = self::quarterRound( $x0, $x4, $x8, $x12 ); |
|
149 | 149 | |
150 | 150 | # QUARTERROUND( x1, x5, x9, x13) |
151 | - list($x1, $x5, $x9, $x13) = self::quarterRound($x1, $x5, $x9, $x13); |
|
151 | + list( $x1, $x5, $x9, $x13 ) = self::quarterRound( $x1, $x5, $x9, $x13 ); |
|
152 | 152 | |
153 | 153 | # QUARTERROUND( x2, x6, x10, x14) |
154 | - list($x2, $x6, $x10, $x14) = self::quarterRound($x2, $x6, $x10, $x14); |
|
154 | + list( $x2, $x6, $x10, $x14 ) = self::quarterRound( $x2, $x6, $x10, $x14 ); |
|
155 | 155 | |
156 | 156 | # QUARTERROUND( x3, x7, x11, x15) |
157 | - list($x3, $x7, $x11, $x15) = self::quarterRound($x3, $x7, $x11, $x15); |
|
157 | + list( $x3, $x7, $x11, $x15 ) = self::quarterRound( $x3, $x7, $x11, $x15 ); |
|
158 | 158 | |
159 | 159 | # QUARTERROUND( x0, x5, x10, x15) |
160 | - list($x0, $x5, $x10, $x15) = self::quarterRound($x0, $x5, $x10, $x15); |
|
160 | + list( $x0, $x5, $x10, $x15 ) = self::quarterRound( $x0, $x5, $x10, $x15 ); |
|
161 | 161 | |
162 | 162 | # QUARTERROUND( x1, x6, x11, x12) |
163 | - list($x1, $x6, $x11, $x12) = self::quarterRound($x1, $x6, $x11, $x12); |
|
163 | + list( $x1, $x6, $x11, $x12 ) = self::quarterRound( $x1, $x6, $x11, $x12 ); |
|
164 | 164 | |
165 | 165 | # QUARTERROUND( x2, x7, x8, x13) |
166 | - list($x2, $x7, $x8, $x13) = self::quarterRound($x2, $x7, $x8, $x13); |
|
166 | + list( $x2, $x7, $x8, $x13 ) = self::quarterRound( $x2, $x7, $x8, $x13 ); |
|
167 | 167 | |
168 | 168 | # QUARTERROUND( x3, x4, x9, x14) |
169 | - list($x3, $x4, $x9, $x14) = self::quarterRound($x3, $x4, $x9, $x14); |
|
169 | + list( $x3, $x4, $x9, $x14 ) = self::quarterRound( $x3, $x4, $x9, $x14 ); |
|
170 | 170 | } |
171 | 171 | /* |
172 | 172 | x0 = PLUS(x0, j0); |
@@ -186,22 +186,22 @@ discard block |
||
186 | 186 | x14 = PLUS(x14, j14); |
187 | 187 | x15 = PLUS(x15, j15); |
188 | 188 | */ |
189 | - $x0 = $x0->addInt32($j0); |
|
190 | - $x1 = $x1->addInt32($j1); |
|
191 | - $x2 = $x2->addInt32($j2); |
|
192 | - $x3 = $x3->addInt32($j3); |
|
193 | - $x4 = $x4->addInt32($j4); |
|
194 | - $x5 = $x5->addInt32($j5); |
|
195 | - $x6 = $x6->addInt32($j6); |
|
196 | - $x7 = $x7->addInt32($j7); |
|
197 | - $x8 = $x8->addInt32($j8); |
|
198 | - $x9 = $x9->addInt32($j9); |
|
199 | - $x10 = $x10->addInt32($j10); |
|
200 | - $x11 = $x11->addInt32($j11); |
|
201 | - $x12 = $x12->addInt32($j12); |
|
202 | - $x13 = $x13->addInt32($j13); |
|
203 | - $x14 = $x14->addInt32($j14); |
|
204 | - $x15 = $x15->addInt32($j15); |
|
189 | + $x0 = $x0->addInt32( $j0 ); |
|
190 | + $x1 = $x1->addInt32( $j1 ); |
|
191 | + $x2 = $x2->addInt32( $j2 ); |
|
192 | + $x3 = $x3->addInt32( $j3 ); |
|
193 | + $x4 = $x4->addInt32( $j4 ); |
|
194 | + $x5 = $x5->addInt32( $j5 ); |
|
195 | + $x6 = $x6->addInt32( $j6 ); |
|
196 | + $x7 = $x7->addInt32( $j7 ); |
|
197 | + $x8 = $x8->addInt32( $j8 ); |
|
198 | + $x9 = $x9->addInt32( $j9 ); |
|
199 | + $x10 = $x10->addInt32( $j10 ); |
|
200 | + $x11 = $x11->addInt32( $j11 ); |
|
201 | + $x12 = $x12->addInt32( $j12 ); |
|
202 | + $x13 = $x13->addInt32( $j13 ); |
|
203 | + $x14 = $x14->addInt32( $j14 ); |
|
204 | + $x15 = $x15->addInt32( $j15 ); |
|
205 | 205 | |
206 | 206 | /* |
207 | 207 | x0 = XOR(x0, LOAD32_LE(m + 0)); |
@@ -221,22 +221,22 @@ discard block |
||
221 | 221 | x14 = XOR(x14, LOAD32_LE(m + 56)); |
222 | 222 | x15 = XOR(x15, LOAD32_LE(m + 60)); |
223 | 223 | */ |
224 | - $x0 = $x0->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 0, 4))); |
|
225 | - $x1 = $x1->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 4, 4))); |
|
226 | - $x2 = $x2->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 8, 4))); |
|
227 | - $x3 = $x3->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 12, 4))); |
|
228 | - $x4 = $x4->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 16, 4))); |
|
229 | - $x5 = $x5->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 20, 4))); |
|
230 | - $x6 = $x6->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 24, 4))); |
|
231 | - $x7 = $x7->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 28, 4))); |
|
232 | - $x8 = $x8->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 32, 4))); |
|
233 | - $x9 = $x9->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 36, 4))); |
|
234 | - $x10 = $x10->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 40, 4))); |
|
235 | - $x11 = $x11->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 44, 4))); |
|
236 | - $x12 = $x12->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 48, 4))); |
|
237 | - $x13 = $x13->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 52, 4))); |
|
238 | - $x14 = $x14->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 56, 4))); |
|
239 | - $x15 = $x15->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 60, 4))); |
|
224 | + $x0 = $x0->xorInt32( ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $message, 0, 4 ) ) ); |
|
225 | + $x1 = $x1->xorInt32( ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $message, 4, 4 ) ) ); |
|
226 | + $x2 = $x2->xorInt32( ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $message, 8, 4 ) ) ); |
|
227 | + $x3 = $x3->xorInt32( ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $message, 12, 4 ) ) ); |
|
228 | + $x4 = $x4->xorInt32( ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $message, 16, 4 ) ) ); |
|
229 | + $x5 = $x5->xorInt32( ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $message, 20, 4 ) ) ); |
|
230 | + $x6 = $x6->xorInt32( ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $message, 24, 4 ) ) ); |
|
231 | + $x7 = $x7->xorInt32( ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $message, 28, 4 ) ) ); |
|
232 | + $x8 = $x8->xorInt32( ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $message, 32, 4 ) ) ); |
|
233 | + $x9 = $x9->xorInt32( ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $message, 36, 4 ) ) ); |
|
234 | + $x10 = $x10->xorInt32( ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $message, 40, 4 ) ) ); |
|
235 | + $x11 = $x11->xorInt32( ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $message, 44, 4 ) ) ); |
|
236 | + $x12 = $x12->xorInt32( ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $message, 48, 4 ) ) ); |
|
237 | + $x13 = $x13->xorInt32( ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $message, 52, 4 ) ) ); |
|
238 | + $x14 = $x14->xorInt32( ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $message, 56, 4 ) ) ); |
|
239 | + $x15 = $x15->xorInt32( ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $message, 60, 4 ) ) ); |
|
240 | 240 | |
241 | 241 | /* |
242 | 242 | j12 = PLUSONE(j12); |
@@ -245,9 +245,9 @@ discard block |
||
245 | 245 | } |
246 | 246 | */ |
247 | 247 | /** @var ParagonIE_Sodium_Core32_Int32 $j12 */ |
248 | - $j12 = $j12->addInt(1); |
|
249 | - if ($j12->limbs[0] === 0 && $j12->limbs[1] === 0) { |
|
250 | - $j13 = $j13->addInt(1); |
|
248 | + $j12 = $j12->addInt( 1 ); |
|
249 | + if ( $j12->limbs[0] === 0 && $j12->limbs[1] === 0 ) { |
|
250 | + $j13 = $j13->addInt( 1 ); |
|
251 | 251 | } |
252 | 252 | |
253 | 253 | /* |
@@ -287,18 +287,18 @@ discard block |
||
287 | 287 | $x15->toReverseString(); |
288 | 288 | |
289 | 289 | /* Partial block */ |
290 | - if ($bytes < 64) { |
|
291 | - $c .= self::substr($block, 0, $bytes); |
|
290 | + if ( $bytes < 64 ) { |
|
291 | + $c .= self::substr( $block, 0, $bytes ); |
|
292 | 292 | break; |
293 | 293 | } |
294 | 294 | |
295 | 295 | /* Full block */ |
296 | 296 | $c .= $block; |
297 | 297 | $bytes -= 64; |
298 | - if ($bytes <= 0) { |
|
298 | + if ( $bytes <= 0 ) { |
|
299 | 299 | break; |
300 | 300 | } |
301 | - $message = self::substr($message, 64); |
|
301 | + $message = self::substr( $message, 64 ); |
|
302 | 302 | } |
303 | 303 | /* end for(;;) loop */ |
304 | 304 | |
@@ -315,11 +315,11 @@ discard block |
||
315 | 315 | * @param string $key |
316 | 316 | * @return string |
317 | 317 | */ |
318 | - public static function stream($len = 64, $nonce = '', $key = '') |
|
318 | + public static function stream( $len = 64, $nonce = '', $key = '' ) |
|
319 | 319 | { |
320 | 320 | return self::encryptBytes( |
321 | - new ParagonIE_Sodium_Core32_ChaCha20_Ctx($key, $nonce), |
|
322 | - str_repeat("\x00", $len) |
|
321 | + new ParagonIE_Sodium_Core32_ChaCha20_Ctx( $key, $nonce ), |
|
322 | + str_repeat( "\x00", $len ) |
|
323 | 323 | ); |
324 | 324 | } |
325 | 325 | |
@@ -331,11 +331,11 @@ discard block |
||
331 | 331 | * @param string $key |
332 | 332 | * @return string |
333 | 333 | */ |
334 | - public static function ietfStream($len, $nonce = '', $key = '') |
|
334 | + public static function ietfStream( $len, $nonce = '', $key = '' ) |
|
335 | 335 | { |
336 | 336 | return self::encryptBytes( |
337 | - new ParagonIE_Sodium_Core32_ChaCha20_IetfCtx($key, $nonce), |
|
338 | - str_repeat("\x00", $len) |
|
337 | + new ParagonIE_Sodium_Core32_ChaCha20_IetfCtx( $key, $nonce ), |
|
338 | + str_repeat( "\x00", $len ) |
|
339 | 339 | ); |
340 | 340 | } |
341 | 341 | |
@@ -348,10 +348,10 @@ discard block |
||
348 | 348 | * @param string $ic |
349 | 349 | * @return string |
350 | 350 | */ |
351 | - public static function ietfStreamXorIc($message, $nonce = '', $key = '', $ic = '') |
|
351 | + public static function ietfStreamXorIc( $message, $nonce = '', $key = '', $ic = '' ) |
|
352 | 352 | { |
353 | 353 | return self::encryptBytes( |
354 | - new ParagonIE_Sodium_Core32_ChaCha20_IetfCtx($key, $nonce, $ic), |
|
354 | + new ParagonIE_Sodium_Core32_ChaCha20_IetfCtx( $key, $nonce, $ic ), |
|
355 | 355 | $message |
356 | 356 | ); |
357 | 357 | } |
@@ -365,10 +365,10 @@ discard block |
||
365 | 365 | * @param string $ic |
366 | 366 | * @return string |
367 | 367 | */ |
368 | - public static function streamXorIc($message, $nonce = '', $key = '', $ic = '') |
|
368 | + public static function streamXorIc( $message, $nonce = '', $key = '', $ic = '' ) |
|
369 | 369 | { |
370 | 370 | return self::encryptBytes( |
371 | - new ParagonIE_Sodium_Core32_ChaCha20_Ctx($key, $nonce, $ic), |
|
371 | + new ParagonIE_Sodium_Core32_ChaCha20_Ctx( $key, $nonce, $ic ), |
|
372 | 372 | $message |
373 | 373 | ); |
374 | 374 | } |
@@ -7,8 +7,7 @@ discard block |
||
7 | 7 | /** |
8 | 8 | * Class ParagonIE_Sodium_Core32_ChaCha20 |
9 | 9 | */ |
10 | -class ParagonIE_Sodium_Core32_ChaCha20 extends ParagonIE_Sodium_Core32_Util |
|
11 | -{ |
|
10 | +class ParagonIE_Sodium_Core32_ChaCha20 extends ParagonIE_Sodium_Core32_Util { |
|
12 | 11 | /** |
13 | 12 | * The ChaCha20 quarter round function. Works on four 32-bit integers. |
14 | 13 | * |
@@ -315,8 +314,7 @@ discard block |
||
315 | 314 | * @param string $key |
316 | 315 | * @return string |
317 | 316 | */ |
318 | - public static function stream($len = 64, $nonce = '', $key = '') |
|
319 | - { |
|
317 | + public static function stream($len = 64, $nonce = '', $key = '') { |
|
320 | 318 | return self::encryptBytes( |
321 | 319 | new ParagonIE_Sodium_Core32_ChaCha20_Ctx($key, $nonce), |
322 | 320 | str_repeat("\x00", $len) |
@@ -331,8 +329,7 @@ discard block |
||
331 | 329 | * @param string $key |
332 | 330 | * @return string |
333 | 331 | */ |
334 | - public static function ietfStream($len, $nonce = '', $key = '') |
|
335 | - { |
|
332 | + public static function ietfStream($len, $nonce = '', $key = '') { |
|
336 | 333 | return self::encryptBytes( |
337 | 334 | new ParagonIE_Sodium_Core32_ChaCha20_IetfCtx($key, $nonce), |
338 | 335 | str_repeat("\x00", $len) |
@@ -348,8 +345,7 @@ discard block |
||
348 | 345 | * @param string $ic |
349 | 346 | * @return string |
350 | 347 | */ |
351 | - public static function ietfStreamXorIc($message, $nonce = '', $key = '', $ic = '') |
|
352 | - { |
|
348 | + public static function ietfStreamXorIc($message, $nonce = '', $key = '', $ic = '') { |
|
353 | 349 | return self::encryptBytes( |
354 | 350 | new ParagonIE_Sodium_Core32_ChaCha20_IetfCtx($key, $nonce, $ic), |
355 | 351 | $message |
@@ -365,8 +361,7 @@ discard block |
||
365 | 361 | * @param string $ic |
366 | 362 | * @return string |
367 | 363 | */ |
368 | - public static function streamXorIc($message, $nonce = '', $key = '', $ic = '') |
|
369 | - { |
|
364 | + public static function streamXorIc($message, $nonce = '', $key = '', $ic = '') { |
|
370 | 365 | return self::encryptBytes( |
371 | 366 | new ParagonIE_Sodium_Core32_ChaCha20_Ctx($key, $nonce, $ic), |
372 | 367 | $message |
@@ -1,7 +1,7 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | if (class_exists('ParagonIE_Sodium_Core32_X25519', false)) { |
4 | - return; |
|
4 | + return; |
|
5 | 5 | } |
6 | 6 | |
7 | 7 | /** |
@@ -9,118 +9,118 @@ discard block |
||
9 | 9 | */ |
10 | 10 | abstract class ParagonIE_Sodium_Core32_X25519 extends ParagonIE_Sodium_Core32_Curve25519 |
11 | 11 | { |
12 | - /** |
|
13 | - * Alters the objects passed to this method in place. |
|
14 | - * |
|
15 | - * @internal You should not use this directly from another application |
|
16 | - * |
|
17 | - * @param ParagonIE_Sodium_Core32_Curve25519_Fe $f |
|
18 | - * @param ParagonIE_Sodium_Core32_Curve25519_Fe $g |
|
19 | - * @param int $b |
|
20 | - * @return void |
|
21 | - */ |
|
22 | - public static function fe_cswap( |
|
23 | - ParagonIE_Sodium_Core32_Curve25519_Fe $f, |
|
24 | - ParagonIE_Sodium_Core32_Curve25519_Fe $g, |
|
25 | - $b = 0 |
|
26 | - ) { |
|
27 | - $f0 = (int) $f[0]->toInt(); |
|
28 | - $f1 = (int) $f[1]->toInt(); |
|
29 | - $f2 = (int) $f[2]->toInt(); |
|
30 | - $f3 = (int) $f[3]->toInt(); |
|
31 | - $f4 = (int) $f[4]->toInt(); |
|
32 | - $f5 = (int) $f[5]->toInt(); |
|
33 | - $f6 = (int) $f[6]->toInt(); |
|
34 | - $f7 = (int) $f[7]->toInt(); |
|
35 | - $f8 = (int) $f[8]->toInt(); |
|
36 | - $f9 = (int) $f[9]->toInt(); |
|
37 | - $g0 = (int) $g[0]->toInt(); |
|
38 | - $g1 = (int) $g[1]->toInt(); |
|
39 | - $g2 = (int) $g[2]->toInt(); |
|
40 | - $g3 = (int) $g[3]->toInt(); |
|
41 | - $g4 = (int) $g[4]->toInt(); |
|
42 | - $g5 = (int) $g[5]->toInt(); |
|
43 | - $g6 = (int) $g[6]->toInt(); |
|
44 | - $g7 = (int) $g[7]->toInt(); |
|
45 | - $g8 = (int) $g[8]->toInt(); |
|
46 | - $g9 = (int) $g[9]->toInt(); |
|
47 | - $b = -$b; |
|
48 | - $x0 = ($f0 ^ $g0) & $b; |
|
49 | - $x1 = ($f1 ^ $g1) & $b; |
|
50 | - $x2 = ($f2 ^ $g2) & $b; |
|
51 | - $x3 = ($f3 ^ $g3) & $b; |
|
52 | - $x4 = ($f4 ^ $g4) & $b; |
|
53 | - $x5 = ($f5 ^ $g5) & $b; |
|
54 | - $x6 = ($f6 ^ $g6) & $b; |
|
55 | - $x7 = ($f7 ^ $g7) & $b; |
|
56 | - $x8 = ($f8 ^ $g8) & $b; |
|
57 | - $x9 = ($f9 ^ $g9) & $b; |
|
58 | - $f[0] = ParagonIE_Sodium_Core32_Int32::fromInt($f0 ^ $x0); |
|
59 | - $f[1] = ParagonIE_Sodium_Core32_Int32::fromInt($f1 ^ $x1); |
|
60 | - $f[2] = ParagonIE_Sodium_Core32_Int32::fromInt($f2 ^ $x2); |
|
61 | - $f[3] = ParagonIE_Sodium_Core32_Int32::fromInt($f3 ^ $x3); |
|
62 | - $f[4] = ParagonIE_Sodium_Core32_Int32::fromInt($f4 ^ $x4); |
|
63 | - $f[5] = ParagonIE_Sodium_Core32_Int32::fromInt($f5 ^ $x5); |
|
64 | - $f[6] = ParagonIE_Sodium_Core32_Int32::fromInt($f6 ^ $x6); |
|
65 | - $f[7] = ParagonIE_Sodium_Core32_Int32::fromInt($f7 ^ $x7); |
|
66 | - $f[8] = ParagonIE_Sodium_Core32_Int32::fromInt($f8 ^ $x8); |
|
67 | - $f[9] = ParagonIE_Sodium_Core32_Int32::fromInt($f9 ^ $x9); |
|
68 | - $g[0] = ParagonIE_Sodium_Core32_Int32::fromInt($g0 ^ $x0); |
|
69 | - $g[1] = ParagonIE_Sodium_Core32_Int32::fromInt($g1 ^ $x1); |
|
70 | - $g[2] = ParagonIE_Sodium_Core32_Int32::fromInt($g2 ^ $x2); |
|
71 | - $g[3] = ParagonIE_Sodium_Core32_Int32::fromInt($g3 ^ $x3); |
|
72 | - $g[4] = ParagonIE_Sodium_Core32_Int32::fromInt($g4 ^ $x4); |
|
73 | - $g[5] = ParagonIE_Sodium_Core32_Int32::fromInt($g5 ^ $x5); |
|
74 | - $g[6] = ParagonIE_Sodium_Core32_Int32::fromInt($g6 ^ $x6); |
|
75 | - $g[7] = ParagonIE_Sodium_Core32_Int32::fromInt($g7 ^ $x7); |
|
76 | - $g[8] = ParagonIE_Sodium_Core32_Int32::fromInt($g8 ^ $x8); |
|
77 | - $g[9] = ParagonIE_Sodium_Core32_Int32::fromInt($g9 ^ $x9); |
|
78 | - } |
|
79 | - |
|
80 | - /** |
|
81 | - * @internal You should not use this directly from another application |
|
82 | - * |
|
83 | - * @param ParagonIE_Sodium_Core32_Curve25519_Fe $f |
|
84 | - * @return ParagonIE_Sodium_Core32_Curve25519_Fe |
|
85 | - */ |
|
86 | - public static function fe_mul121666(ParagonIE_Sodium_Core32_Curve25519_Fe $f) |
|
87 | - { |
|
88 | - /** @var ParagonIE_Sodium_Core32_Int32[] $h */ |
|
89 | - $h = array( |
|
90 | - $f[0]->mulInt(121666), |
|
91 | - $f[1]->mulInt(121666), |
|
92 | - $f[2]->mulInt(121666), |
|
93 | - $f[3]->mulInt(121666), |
|
94 | - $f[4]->mulInt(121666), |
|
95 | - $f[5]->mulInt(121666), |
|
96 | - $f[6]->mulInt(121666), |
|
97 | - $f[7]->mulInt(121666), |
|
98 | - $f[8]->mulInt(121666), |
|
99 | - $f[9]->mulInt(121666), |
|
100 | - ); |
|
101 | - |
|
102 | - /** @var ParagonIE_Sodium_Core32_Int32 $carry9 */ |
|
103 | - $carry9 = $h[9]->addInt(1 << 24)->shiftRight(25); |
|
104 | - $h[0] = $carry9->mulInt(19); |
|
105 | - $h[9] = $h[9]->subInt32($carry9->shiftLeft(25)); |
|
106 | - |
|
107 | - $carry1 = $h[1]->addInt(1 << 24)->shiftRight(25); |
|
108 | - $h[2] = $h[2]->addInt32($carry1); |
|
109 | - $h[1] = $h[1]->subInt32($carry1->shiftLeft(25)); |
|
110 | - |
|
111 | - $carry3 = $h[3]->addInt(1 << 24)->shiftRight(25); |
|
112 | - $h[4] = $h[4]->addInt32($carry3); |
|
113 | - $h[3] = $h[3]->subInt32($carry3->shiftLeft(25)); |
|
114 | - |
|
115 | - $carry5 = $h[3]->addInt(1 << 24)->shiftRight(25); |
|
116 | - $h[6] = $h[6]->addInt32($carry5); |
|
117 | - $h[5] = $h[5]->subInt32($carry5->shiftLeft(25)); |
|
118 | - |
|
119 | - $carry7 = $h[3]->addInt(1 << 24)->shiftRight(25); |
|
120 | - $h[8] = $h[8]->addInt32($carry7); |
|
121 | - $h[7] = $h[7]->subInt32($carry7->shiftLeft(25)); |
|
122 | - |
|
123 | - /* |
|
12 | + /** |
|
13 | + * Alters the objects passed to this method in place. |
|
14 | + * |
|
15 | + * @internal You should not use this directly from another application |
|
16 | + * |
|
17 | + * @param ParagonIE_Sodium_Core32_Curve25519_Fe $f |
|
18 | + * @param ParagonIE_Sodium_Core32_Curve25519_Fe $g |
|
19 | + * @param int $b |
|
20 | + * @return void |
|
21 | + */ |
|
22 | + public static function fe_cswap( |
|
23 | + ParagonIE_Sodium_Core32_Curve25519_Fe $f, |
|
24 | + ParagonIE_Sodium_Core32_Curve25519_Fe $g, |
|
25 | + $b = 0 |
|
26 | + ) { |
|
27 | + $f0 = (int) $f[0]->toInt(); |
|
28 | + $f1 = (int) $f[1]->toInt(); |
|
29 | + $f2 = (int) $f[2]->toInt(); |
|
30 | + $f3 = (int) $f[3]->toInt(); |
|
31 | + $f4 = (int) $f[4]->toInt(); |
|
32 | + $f5 = (int) $f[5]->toInt(); |
|
33 | + $f6 = (int) $f[6]->toInt(); |
|
34 | + $f7 = (int) $f[7]->toInt(); |
|
35 | + $f8 = (int) $f[8]->toInt(); |
|
36 | + $f9 = (int) $f[9]->toInt(); |
|
37 | + $g0 = (int) $g[0]->toInt(); |
|
38 | + $g1 = (int) $g[1]->toInt(); |
|
39 | + $g2 = (int) $g[2]->toInt(); |
|
40 | + $g3 = (int) $g[3]->toInt(); |
|
41 | + $g4 = (int) $g[4]->toInt(); |
|
42 | + $g5 = (int) $g[5]->toInt(); |
|
43 | + $g6 = (int) $g[6]->toInt(); |
|
44 | + $g7 = (int) $g[7]->toInt(); |
|
45 | + $g8 = (int) $g[8]->toInt(); |
|
46 | + $g9 = (int) $g[9]->toInt(); |
|
47 | + $b = -$b; |
|
48 | + $x0 = ($f0 ^ $g0) & $b; |
|
49 | + $x1 = ($f1 ^ $g1) & $b; |
|
50 | + $x2 = ($f2 ^ $g2) & $b; |
|
51 | + $x3 = ($f3 ^ $g3) & $b; |
|
52 | + $x4 = ($f4 ^ $g4) & $b; |
|
53 | + $x5 = ($f5 ^ $g5) & $b; |
|
54 | + $x6 = ($f6 ^ $g6) & $b; |
|
55 | + $x7 = ($f7 ^ $g7) & $b; |
|
56 | + $x8 = ($f8 ^ $g8) & $b; |
|
57 | + $x9 = ($f9 ^ $g9) & $b; |
|
58 | + $f[0] = ParagonIE_Sodium_Core32_Int32::fromInt($f0 ^ $x0); |
|
59 | + $f[1] = ParagonIE_Sodium_Core32_Int32::fromInt($f1 ^ $x1); |
|
60 | + $f[2] = ParagonIE_Sodium_Core32_Int32::fromInt($f2 ^ $x2); |
|
61 | + $f[3] = ParagonIE_Sodium_Core32_Int32::fromInt($f3 ^ $x3); |
|
62 | + $f[4] = ParagonIE_Sodium_Core32_Int32::fromInt($f4 ^ $x4); |
|
63 | + $f[5] = ParagonIE_Sodium_Core32_Int32::fromInt($f5 ^ $x5); |
|
64 | + $f[6] = ParagonIE_Sodium_Core32_Int32::fromInt($f6 ^ $x6); |
|
65 | + $f[7] = ParagonIE_Sodium_Core32_Int32::fromInt($f7 ^ $x7); |
|
66 | + $f[8] = ParagonIE_Sodium_Core32_Int32::fromInt($f8 ^ $x8); |
|
67 | + $f[9] = ParagonIE_Sodium_Core32_Int32::fromInt($f9 ^ $x9); |
|
68 | + $g[0] = ParagonIE_Sodium_Core32_Int32::fromInt($g0 ^ $x0); |
|
69 | + $g[1] = ParagonIE_Sodium_Core32_Int32::fromInt($g1 ^ $x1); |
|
70 | + $g[2] = ParagonIE_Sodium_Core32_Int32::fromInt($g2 ^ $x2); |
|
71 | + $g[3] = ParagonIE_Sodium_Core32_Int32::fromInt($g3 ^ $x3); |
|
72 | + $g[4] = ParagonIE_Sodium_Core32_Int32::fromInt($g4 ^ $x4); |
|
73 | + $g[5] = ParagonIE_Sodium_Core32_Int32::fromInt($g5 ^ $x5); |
|
74 | + $g[6] = ParagonIE_Sodium_Core32_Int32::fromInt($g6 ^ $x6); |
|
75 | + $g[7] = ParagonIE_Sodium_Core32_Int32::fromInt($g7 ^ $x7); |
|
76 | + $g[8] = ParagonIE_Sodium_Core32_Int32::fromInt($g8 ^ $x8); |
|
77 | + $g[9] = ParagonIE_Sodium_Core32_Int32::fromInt($g9 ^ $x9); |
|
78 | + } |
|
79 | + |
|
80 | + /** |
|
81 | + * @internal You should not use this directly from another application |
|
82 | + * |
|
83 | + * @param ParagonIE_Sodium_Core32_Curve25519_Fe $f |
|
84 | + * @return ParagonIE_Sodium_Core32_Curve25519_Fe |
|
85 | + */ |
|
86 | + public static function fe_mul121666(ParagonIE_Sodium_Core32_Curve25519_Fe $f) |
|
87 | + { |
|
88 | + /** @var ParagonIE_Sodium_Core32_Int32[] $h */ |
|
89 | + $h = array( |
|
90 | + $f[0]->mulInt(121666), |
|
91 | + $f[1]->mulInt(121666), |
|
92 | + $f[2]->mulInt(121666), |
|
93 | + $f[3]->mulInt(121666), |
|
94 | + $f[4]->mulInt(121666), |
|
95 | + $f[5]->mulInt(121666), |
|
96 | + $f[6]->mulInt(121666), |
|
97 | + $f[7]->mulInt(121666), |
|
98 | + $f[8]->mulInt(121666), |
|
99 | + $f[9]->mulInt(121666), |
|
100 | + ); |
|
101 | + |
|
102 | + /** @var ParagonIE_Sodium_Core32_Int32 $carry9 */ |
|
103 | + $carry9 = $h[9]->addInt(1 << 24)->shiftRight(25); |
|
104 | + $h[0] = $carry9->mulInt(19); |
|
105 | + $h[9] = $h[9]->subInt32($carry9->shiftLeft(25)); |
|
106 | + |
|
107 | + $carry1 = $h[1]->addInt(1 << 24)->shiftRight(25); |
|
108 | + $h[2] = $h[2]->addInt32($carry1); |
|
109 | + $h[1] = $h[1]->subInt32($carry1->shiftLeft(25)); |
|
110 | + |
|
111 | + $carry3 = $h[3]->addInt(1 << 24)->shiftRight(25); |
|
112 | + $h[4] = $h[4]->addInt32($carry3); |
|
113 | + $h[3] = $h[3]->subInt32($carry3->shiftLeft(25)); |
|
114 | + |
|
115 | + $carry5 = $h[3]->addInt(1 << 24)->shiftRight(25); |
|
116 | + $h[6] = $h[6]->addInt32($carry5); |
|
117 | + $h[5] = $h[5]->subInt32($carry5->shiftLeft(25)); |
|
118 | + |
|
119 | + $carry7 = $h[3]->addInt(1 << 24)->shiftRight(25); |
|
120 | + $h[8] = $h[8]->addInt32($carry7); |
|
121 | + $h[7] = $h[7]->subInt32($carry7->shiftLeft(25)); |
|
122 | + |
|
123 | + /* |
|
124 | 124 | $carry9 = ($h[9] + (1 << 24)) >> 25; |
125 | 125 | $h[0] += self::mul($carry9, 19); |
126 | 126 | $h[9] -= $carry9 << 25; |
@@ -138,27 +138,27 @@ discard block |
||
138 | 138 | $h[7] -= $carry7 << 25; |
139 | 139 | */ |
140 | 140 | |
141 | - $carry0 = $h[0]->addInt(1 << 25)->shiftRight(26); |
|
142 | - $h[1] = $h[1]->addInt32($carry0); |
|
143 | - $h[0] = $h[0]->subInt32($carry0->shiftLeft(26)); |
|
141 | + $carry0 = $h[0]->addInt(1 << 25)->shiftRight(26); |
|
142 | + $h[1] = $h[1]->addInt32($carry0); |
|
143 | + $h[0] = $h[0]->subInt32($carry0->shiftLeft(26)); |
|
144 | 144 | |
145 | - $carry2 = $h[2]->addInt(1 << 25)->shiftRight(26); |
|
146 | - $h[3] = $h[3]->addInt32($carry2); |
|
147 | - $h[2] = $h[2]->subInt32($carry2->shiftLeft(26)); |
|
145 | + $carry2 = $h[2]->addInt(1 << 25)->shiftRight(26); |
|
146 | + $h[3] = $h[3]->addInt32($carry2); |
|
147 | + $h[2] = $h[2]->subInt32($carry2->shiftLeft(26)); |
|
148 | 148 | |
149 | - $carry4 = $h[4]->addInt(1 << 25)->shiftRight(26); |
|
150 | - $h[5] = $h[5]->addInt32($carry4); |
|
151 | - $h[4] = $h[4]->subInt32($carry4->shiftLeft(26)); |
|
149 | + $carry4 = $h[4]->addInt(1 << 25)->shiftRight(26); |
|
150 | + $h[5] = $h[5]->addInt32($carry4); |
|
151 | + $h[4] = $h[4]->subInt32($carry4->shiftLeft(26)); |
|
152 | 152 | |
153 | - $carry6 = $h[6]->addInt(1 << 25)->shiftRight(26); |
|
154 | - $h[7] = $h[7]->addInt32($carry6); |
|
155 | - $h[6] = $h[6]->subInt32($carry6->shiftLeft(26)); |
|
153 | + $carry6 = $h[6]->addInt(1 << 25)->shiftRight(26); |
|
154 | + $h[7] = $h[7]->addInt32($carry6); |
|
155 | + $h[6] = $h[6]->subInt32($carry6->shiftLeft(26)); |
|
156 | 156 | |
157 | - $carry8 = $h[8]->addInt(1 << 25)->shiftRight(26); |
|
158 | - $h[9] = $h[9]->addInt32($carry8); |
|
159 | - $h[8] = $h[8]->subInt32($carry8->shiftLeft(26)); |
|
157 | + $carry8 = $h[8]->addInt(1 << 25)->shiftRight(26); |
|
158 | + $h[9] = $h[9]->addInt32($carry8); |
|
159 | + $h[8] = $h[8]->subInt32($carry8->shiftLeft(26)); |
|
160 | 160 | |
161 | - /* |
|
161 | + /* |
|
162 | 162 | $carry0 = ($h[0] + (1 << 25)) >> 26; |
163 | 163 | $h[1] += $carry0; |
164 | 164 | $h[0] -= $carry0 << 26; |
@@ -176,179 +176,179 @@ discard block |
||
176 | 176 | $h[8] -= $carry8 << 26; |
177 | 177 | */ |
178 | 178 | |
179 | - return ParagonIE_Sodium_Core32_Curve25519_Fe::fromArray($h); |
|
180 | - } |
|
181 | - |
|
182 | - /** |
|
183 | - * @internal You should not use this directly from another application |
|
184 | - * |
|
185 | - * Inline comments preceded by # are from libsodium's ref10 code. |
|
186 | - * |
|
187 | - * @param string $n |
|
188 | - * @param string $p |
|
189 | - * @return string |
|
190 | - */ |
|
191 | - public static function crypto_scalarmult_curve25519_ref10($n, $p) |
|
192 | - { |
|
193 | - # for (i = 0;i < 32;++i) e[i] = n[i]; |
|
194 | - $e = '' . $n; |
|
195 | - # e[0] &= 248; |
|
196 | - $e[0] = self::intToChr( |
|
197 | - self::chrToInt($e[0]) & 248 |
|
198 | - ); |
|
199 | - # e[31] &= 127; |
|
200 | - # e[31] |= 64; |
|
201 | - $e[31] = self::intToChr( |
|
202 | - (self::chrToInt($e[31]) & 127) | 64 |
|
203 | - ); |
|
204 | - # fe_frombytes(x1,p); |
|
205 | - $x1 = self::fe_frombytes($p); |
|
206 | - # fe_1(x2); |
|
207 | - $x2 = self::fe_1(); |
|
208 | - # fe_0(z2); |
|
209 | - $z2 = self::fe_0(); |
|
210 | - # fe_copy(x3,x1); |
|
211 | - $x3 = self::fe_copy($x1); |
|
212 | - # fe_1(z3); |
|
213 | - $z3 = self::fe_1(); |
|
214 | - |
|
215 | - # swap = 0; |
|
216 | - $swap = 0; |
|
217 | - |
|
218 | - # for (pos = 254;pos >= 0;--pos) { |
|
219 | - for ($pos = 254; $pos >= 0; --$pos) { |
|
220 | - # b = e[pos / 8] >> (pos & 7); |
|
221 | - $b = self::chrToInt( |
|
222 | - $e[(int) floor($pos / 8)] |
|
223 | - ) >> ($pos & 7); |
|
224 | - # b &= 1; |
|
225 | - $b &= 1; |
|
226 | - # swap ^= b; |
|
227 | - $swap ^= $b; |
|
228 | - # fe_cswap(x2,x3,swap); |
|
229 | - self::fe_cswap($x2, $x3, $swap); |
|
230 | - # fe_cswap(z2,z3,swap); |
|
231 | - self::fe_cswap($z2, $z3, $swap); |
|
232 | - # swap = b; |
|
233 | - $swap = $b; |
|
234 | - # fe_sub(tmp0,x3,z3); |
|
235 | - $tmp0 = self::fe_sub($x3, $z3); |
|
236 | - # fe_sub(tmp1,x2,z2); |
|
237 | - $tmp1 = self::fe_sub($x2, $z2); |
|
238 | - |
|
239 | - # fe_add(x2,x2,z2); |
|
240 | - $x2 = self::fe_add($x2, $z2); |
|
241 | - |
|
242 | - # fe_add(z2,x3,z3); |
|
243 | - $z2 = self::fe_add($x3, $z3); |
|
244 | - |
|
245 | - # fe_mul(z3,tmp0,x2); |
|
246 | - $z3 = self::fe_mul($tmp0, $x2); |
|
247 | - |
|
248 | - # fe_mul(z2,z2,tmp1); |
|
249 | - $z2 = self::fe_mul($z2, $tmp1); |
|
250 | - |
|
251 | - # fe_sq(tmp0,tmp1); |
|
252 | - $tmp0 = self::fe_sq($tmp1); |
|
253 | - |
|
254 | - # fe_sq(tmp1,x2); |
|
255 | - $tmp1 = self::fe_sq($x2); |
|
256 | - |
|
257 | - # fe_add(x3,z3,z2); |
|
258 | - $x3 = self::fe_add($z3, $z2); |
|
259 | - |
|
260 | - # fe_sub(z2,z3,z2); |
|
261 | - $z2 = self::fe_sub($z3, $z2); |
|
262 | - |
|
263 | - # fe_mul(x2,tmp1,tmp0); |
|
264 | - $x2 = self::fe_mul($tmp1, $tmp0); |
|
265 | - |
|
266 | - # fe_sub(tmp1,tmp1,tmp0); |
|
267 | - $tmp1 = self::fe_sub($tmp1, $tmp0); |
|
268 | - |
|
269 | - # fe_sq(z2,z2); |
|
270 | - $z2 = self::fe_sq($z2); |
|
271 | - |
|
272 | - # fe_mul121666(z3,tmp1); |
|
273 | - $z3 = self::fe_mul121666($tmp1); |
|
274 | - |
|
275 | - # fe_sq(x3,x3); |
|
276 | - $x3 = self::fe_sq($x3); |
|
277 | - |
|
278 | - # fe_add(tmp0,tmp0,z3); |
|
279 | - $tmp0 = self::fe_add($tmp0, $z3); |
|
280 | - |
|
281 | - # fe_mul(z3,x1,z2); |
|
282 | - $z3 = self::fe_mul($x1, $z2); |
|
283 | - |
|
284 | - # fe_mul(z2,tmp1,tmp0); |
|
285 | - $z2 = self::fe_mul($tmp1, $tmp0); |
|
286 | - } |
|
287 | - |
|
288 | - # fe_cswap(x2,x3,swap); |
|
289 | - self::fe_cswap($x2, $x3, $swap); |
|
290 | - |
|
291 | - # fe_cswap(z2,z3,swap); |
|
292 | - self::fe_cswap($z2, $z3, $swap); |
|
293 | - |
|
294 | - # fe_invert(z2,z2); |
|
295 | - $z2 = self::fe_invert($z2); |
|
296 | - |
|
297 | - # fe_mul(x2,x2,z2); |
|
298 | - $x2 = self::fe_mul($x2, $z2); |
|
299 | - # fe_tobytes(q,x2); |
|
300 | - return self::fe_tobytes($x2); |
|
301 | - } |
|
302 | - |
|
303 | - /** |
|
304 | - * @internal You should not use this directly from another application |
|
305 | - * |
|
306 | - * @param ParagonIE_Sodium_Core32_Curve25519_Fe $edwardsY |
|
307 | - * @param ParagonIE_Sodium_Core32_Curve25519_Fe $edwardsZ |
|
308 | - * @return ParagonIE_Sodium_Core32_Curve25519_Fe |
|
309 | - */ |
|
310 | - public static function edwards_to_montgomery( |
|
311 | - ParagonIE_Sodium_Core32_Curve25519_Fe $edwardsY, |
|
312 | - ParagonIE_Sodium_Core32_Curve25519_Fe $edwardsZ |
|
313 | - ) { |
|
314 | - $tempX = self::fe_add($edwardsZ, $edwardsY); |
|
315 | - $tempZ = self::fe_sub($edwardsZ, $edwardsY); |
|
316 | - $tempZ = self::fe_invert($tempZ); |
|
317 | - return self::fe_mul($tempX, $tempZ); |
|
318 | - } |
|
319 | - |
|
320 | - /** |
|
321 | - * @internal You should not use this directly from another application |
|
322 | - * |
|
323 | - * @param string $n |
|
324 | - * @return string |
|
325 | - * @throws TypeError |
|
326 | - */ |
|
327 | - public static function crypto_scalarmult_curve25519_ref10_base($n) |
|
328 | - { |
|
329 | - # for (i = 0;i < 32;++i) e[i] = n[i]; |
|
330 | - $e = '' . $n; |
|
331 | - |
|
332 | - # e[0] &= 248; |
|
333 | - $e[0] = self::intToChr( |
|
334 | - self::chrToInt($e[0]) & 248 |
|
335 | - ); |
|
336 | - |
|
337 | - # e[31] &= 127; |
|
338 | - # e[31] |= 64; |
|
339 | - $e[31] = self::intToChr( |
|
340 | - (self::chrToInt($e[31]) & 127) | 64 |
|
341 | - ); |
|
342 | - |
|
343 | - $A = self::ge_scalarmult_base($e); |
|
344 | - if ( |
|
345 | - !($A->Y instanceof ParagonIE_Sodium_Core32_Curve25519_Fe) |
|
346 | - || |
|
347 | - !($A->Z instanceof ParagonIE_Sodium_Core32_Curve25519_Fe) |
|
348 | - ) { |
|
349 | - throw new TypeError('Null points encountered'); |
|
350 | - } |
|
351 | - $pk = self::edwards_to_montgomery($A->Y, $A->Z); |
|
352 | - return self::fe_tobytes($pk); |
|
353 | - } |
|
179 | + return ParagonIE_Sodium_Core32_Curve25519_Fe::fromArray($h); |
|
180 | + } |
|
181 | + |
|
182 | + /** |
|
183 | + * @internal You should not use this directly from another application |
|
184 | + * |
|
185 | + * Inline comments preceded by # are from libsodium's ref10 code. |
|
186 | + * |
|
187 | + * @param string $n |
|
188 | + * @param string $p |
|
189 | + * @return string |
|
190 | + */ |
|
191 | + public static function crypto_scalarmult_curve25519_ref10($n, $p) |
|
192 | + { |
|
193 | + # for (i = 0;i < 32;++i) e[i] = n[i]; |
|
194 | + $e = '' . $n; |
|
195 | + # e[0] &= 248; |
|
196 | + $e[0] = self::intToChr( |
|
197 | + self::chrToInt($e[0]) & 248 |
|
198 | + ); |
|
199 | + # e[31] &= 127; |
|
200 | + # e[31] |= 64; |
|
201 | + $e[31] = self::intToChr( |
|
202 | + (self::chrToInt($e[31]) & 127) | 64 |
|
203 | + ); |
|
204 | + # fe_frombytes(x1,p); |
|
205 | + $x1 = self::fe_frombytes($p); |
|
206 | + # fe_1(x2); |
|
207 | + $x2 = self::fe_1(); |
|
208 | + # fe_0(z2); |
|
209 | + $z2 = self::fe_0(); |
|
210 | + # fe_copy(x3,x1); |
|
211 | + $x3 = self::fe_copy($x1); |
|
212 | + # fe_1(z3); |
|
213 | + $z3 = self::fe_1(); |
|
214 | + |
|
215 | + # swap = 0; |
|
216 | + $swap = 0; |
|
217 | + |
|
218 | + # for (pos = 254;pos >= 0;--pos) { |
|
219 | + for ($pos = 254; $pos >= 0; --$pos) { |
|
220 | + # b = e[pos / 8] >> (pos & 7); |
|
221 | + $b = self::chrToInt( |
|
222 | + $e[(int) floor($pos / 8)] |
|
223 | + ) >> ($pos & 7); |
|
224 | + # b &= 1; |
|
225 | + $b &= 1; |
|
226 | + # swap ^= b; |
|
227 | + $swap ^= $b; |
|
228 | + # fe_cswap(x2,x3,swap); |
|
229 | + self::fe_cswap($x2, $x3, $swap); |
|
230 | + # fe_cswap(z2,z3,swap); |
|
231 | + self::fe_cswap($z2, $z3, $swap); |
|
232 | + # swap = b; |
|
233 | + $swap = $b; |
|
234 | + # fe_sub(tmp0,x3,z3); |
|
235 | + $tmp0 = self::fe_sub($x3, $z3); |
|
236 | + # fe_sub(tmp1,x2,z2); |
|
237 | + $tmp1 = self::fe_sub($x2, $z2); |
|
238 | + |
|
239 | + # fe_add(x2,x2,z2); |
|
240 | + $x2 = self::fe_add($x2, $z2); |
|
241 | + |
|
242 | + # fe_add(z2,x3,z3); |
|
243 | + $z2 = self::fe_add($x3, $z3); |
|
244 | + |
|
245 | + # fe_mul(z3,tmp0,x2); |
|
246 | + $z3 = self::fe_mul($tmp0, $x2); |
|
247 | + |
|
248 | + # fe_mul(z2,z2,tmp1); |
|
249 | + $z2 = self::fe_mul($z2, $tmp1); |
|
250 | + |
|
251 | + # fe_sq(tmp0,tmp1); |
|
252 | + $tmp0 = self::fe_sq($tmp1); |
|
253 | + |
|
254 | + # fe_sq(tmp1,x2); |
|
255 | + $tmp1 = self::fe_sq($x2); |
|
256 | + |
|
257 | + # fe_add(x3,z3,z2); |
|
258 | + $x3 = self::fe_add($z3, $z2); |
|
259 | + |
|
260 | + # fe_sub(z2,z3,z2); |
|
261 | + $z2 = self::fe_sub($z3, $z2); |
|
262 | + |
|
263 | + # fe_mul(x2,tmp1,tmp0); |
|
264 | + $x2 = self::fe_mul($tmp1, $tmp0); |
|
265 | + |
|
266 | + # fe_sub(tmp1,tmp1,tmp0); |
|
267 | + $tmp1 = self::fe_sub($tmp1, $tmp0); |
|
268 | + |
|
269 | + # fe_sq(z2,z2); |
|
270 | + $z2 = self::fe_sq($z2); |
|
271 | + |
|
272 | + # fe_mul121666(z3,tmp1); |
|
273 | + $z3 = self::fe_mul121666($tmp1); |
|
274 | + |
|
275 | + # fe_sq(x3,x3); |
|
276 | + $x3 = self::fe_sq($x3); |
|
277 | + |
|
278 | + # fe_add(tmp0,tmp0,z3); |
|
279 | + $tmp0 = self::fe_add($tmp0, $z3); |
|
280 | + |
|
281 | + # fe_mul(z3,x1,z2); |
|
282 | + $z3 = self::fe_mul($x1, $z2); |
|
283 | + |
|
284 | + # fe_mul(z2,tmp1,tmp0); |
|
285 | + $z2 = self::fe_mul($tmp1, $tmp0); |
|
286 | + } |
|
287 | + |
|
288 | + # fe_cswap(x2,x3,swap); |
|
289 | + self::fe_cswap($x2, $x3, $swap); |
|
290 | + |
|
291 | + # fe_cswap(z2,z3,swap); |
|
292 | + self::fe_cswap($z2, $z3, $swap); |
|
293 | + |
|
294 | + # fe_invert(z2,z2); |
|
295 | + $z2 = self::fe_invert($z2); |
|
296 | + |
|
297 | + # fe_mul(x2,x2,z2); |
|
298 | + $x2 = self::fe_mul($x2, $z2); |
|
299 | + # fe_tobytes(q,x2); |
|
300 | + return self::fe_tobytes($x2); |
|
301 | + } |
|
302 | + |
|
303 | + /** |
|
304 | + * @internal You should not use this directly from another application |
|
305 | + * |
|
306 | + * @param ParagonIE_Sodium_Core32_Curve25519_Fe $edwardsY |
|
307 | + * @param ParagonIE_Sodium_Core32_Curve25519_Fe $edwardsZ |
|
308 | + * @return ParagonIE_Sodium_Core32_Curve25519_Fe |
|
309 | + */ |
|
310 | + public static function edwards_to_montgomery( |
|
311 | + ParagonIE_Sodium_Core32_Curve25519_Fe $edwardsY, |
|
312 | + ParagonIE_Sodium_Core32_Curve25519_Fe $edwardsZ |
|
313 | + ) { |
|
314 | + $tempX = self::fe_add($edwardsZ, $edwardsY); |
|
315 | + $tempZ = self::fe_sub($edwardsZ, $edwardsY); |
|
316 | + $tempZ = self::fe_invert($tempZ); |
|
317 | + return self::fe_mul($tempX, $tempZ); |
|
318 | + } |
|
319 | + |
|
320 | + /** |
|
321 | + * @internal You should not use this directly from another application |
|
322 | + * |
|
323 | + * @param string $n |
|
324 | + * @return string |
|
325 | + * @throws TypeError |
|
326 | + */ |
|
327 | + public static function crypto_scalarmult_curve25519_ref10_base($n) |
|
328 | + { |
|
329 | + # for (i = 0;i < 32;++i) e[i] = n[i]; |
|
330 | + $e = '' . $n; |
|
331 | + |
|
332 | + # e[0] &= 248; |
|
333 | + $e[0] = self::intToChr( |
|
334 | + self::chrToInt($e[0]) & 248 |
|
335 | + ); |
|
336 | + |
|
337 | + # e[31] &= 127; |
|
338 | + # e[31] |= 64; |
|
339 | + $e[31] = self::intToChr( |
|
340 | + (self::chrToInt($e[31]) & 127) | 64 |
|
341 | + ); |
|
342 | + |
|
343 | + $A = self::ge_scalarmult_base($e); |
|
344 | + if ( |
|
345 | + !($A->Y instanceof ParagonIE_Sodium_Core32_Curve25519_Fe) |
|
346 | + || |
|
347 | + !($A->Z instanceof ParagonIE_Sodium_Core32_Curve25519_Fe) |
|
348 | + ) { |
|
349 | + throw new TypeError('Null points encountered'); |
|
350 | + } |
|
351 | + $pk = self::edwards_to_montgomery($A->Y, $A->Z); |
|
352 | + return self::fe_tobytes($pk); |
|
353 | + } |
|
354 | 354 | } |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -if (class_exists('ParagonIE_Sodium_Core32_X25519', false)) { |
|
3 | +if ( class_exists( 'ParagonIE_Sodium_Core32_X25519', false ) ) { |
|
4 | 4 | return; |
5 | 5 | } |
6 | 6 | |
@@ -45,36 +45,36 @@ discard block |
||
45 | 45 | $g8 = (int) $g[8]->toInt(); |
46 | 46 | $g9 = (int) $g[9]->toInt(); |
47 | 47 | $b = -$b; |
48 | - $x0 = ($f0 ^ $g0) & $b; |
|
49 | - $x1 = ($f1 ^ $g1) & $b; |
|
50 | - $x2 = ($f2 ^ $g2) & $b; |
|
51 | - $x3 = ($f3 ^ $g3) & $b; |
|
52 | - $x4 = ($f4 ^ $g4) & $b; |
|
53 | - $x5 = ($f5 ^ $g5) & $b; |
|
54 | - $x6 = ($f6 ^ $g6) & $b; |
|
55 | - $x7 = ($f7 ^ $g7) & $b; |
|
56 | - $x8 = ($f8 ^ $g8) & $b; |
|
57 | - $x9 = ($f9 ^ $g9) & $b; |
|
58 | - $f[0] = ParagonIE_Sodium_Core32_Int32::fromInt($f0 ^ $x0); |
|
59 | - $f[1] = ParagonIE_Sodium_Core32_Int32::fromInt($f1 ^ $x1); |
|
60 | - $f[2] = ParagonIE_Sodium_Core32_Int32::fromInt($f2 ^ $x2); |
|
61 | - $f[3] = ParagonIE_Sodium_Core32_Int32::fromInt($f3 ^ $x3); |
|
62 | - $f[4] = ParagonIE_Sodium_Core32_Int32::fromInt($f4 ^ $x4); |
|
63 | - $f[5] = ParagonIE_Sodium_Core32_Int32::fromInt($f5 ^ $x5); |
|
64 | - $f[6] = ParagonIE_Sodium_Core32_Int32::fromInt($f6 ^ $x6); |
|
65 | - $f[7] = ParagonIE_Sodium_Core32_Int32::fromInt($f7 ^ $x7); |
|
66 | - $f[8] = ParagonIE_Sodium_Core32_Int32::fromInt($f8 ^ $x8); |
|
67 | - $f[9] = ParagonIE_Sodium_Core32_Int32::fromInt($f9 ^ $x9); |
|
68 | - $g[0] = ParagonIE_Sodium_Core32_Int32::fromInt($g0 ^ $x0); |
|
69 | - $g[1] = ParagonIE_Sodium_Core32_Int32::fromInt($g1 ^ $x1); |
|
70 | - $g[2] = ParagonIE_Sodium_Core32_Int32::fromInt($g2 ^ $x2); |
|
71 | - $g[3] = ParagonIE_Sodium_Core32_Int32::fromInt($g3 ^ $x3); |
|
72 | - $g[4] = ParagonIE_Sodium_Core32_Int32::fromInt($g4 ^ $x4); |
|
73 | - $g[5] = ParagonIE_Sodium_Core32_Int32::fromInt($g5 ^ $x5); |
|
74 | - $g[6] = ParagonIE_Sodium_Core32_Int32::fromInt($g6 ^ $x6); |
|
75 | - $g[7] = ParagonIE_Sodium_Core32_Int32::fromInt($g7 ^ $x7); |
|
76 | - $g[8] = ParagonIE_Sodium_Core32_Int32::fromInt($g8 ^ $x8); |
|
77 | - $g[9] = ParagonIE_Sodium_Core32_Int32::fromInt($g9 ^ $x9); |
|
48 | + $x0 = ( $f0 ^ $g0 ) & $b; |
|
49 | + $x1 = ( $f1 ^ $g1 ) & $b; |
|
50 | + $x2 = ( $f2 ^ $g2 ) & $b; |
|
51 | + $x3 = ( $f3 ^ $g3 ) & $b; |
|
52 | + $x4 = ( $f4 ^ $g4 ) & $b; |
|
53 | + $x5 = ( $f5 ^ $g5 ) & $b; |
|
54 | + $x6 = ( $f6 ^ $g6 ) & $b; |
|
55 | + $x7 = ( $f7 ^ $g7 ) & $b; |
|
56 | + $x8 = ( $f8 ^ $g8 ) & $b; |
|
57 | + $x9 = ( $f9 ^ $g9 ) & $b; |
|
58 | + $f[0] = ParagonIE_Sodium_Core32_Int32::fromInt( $f0 ^ $x0 ); |
|
59 | + $f[1] = ParagonIE_Sodium_Core32_Int32::fromInt( $f1 ^ $x1 ); |
|
60 | + $f[2] = ParagonIE_Sodium_Core32_Int32::fromInt( $f2 ^ $x2 ); |
|
61 | + $f[3] = ParagonIE_Sodium_Core32_Int32::fromInt( $f3 ^ $x3 ); |
|
62 | + $f[4] = ParagonIE_Sodium_Core32_Int32::fromInt( $f4 ^ $x4 ); |
|
63 | + $f[5] = ParagonIE_Sodium_Core32_Int32::fromInt( $f5 ^ $x5 ); |
|
64 | + $f[6] = ParagonIE_Sodium_Core32_Int32::fromInt( $f6 ^ $x6 ); |
|
65 | + $f[7] = ParagonIE_Sodium_Core32_Int32::fromInt( $f7 ^ $x7 ); |
|
66 | + $f[8] = ParagonIE_Sodium_Core32_Int32::fromInt( $f8 ^ $x8 ); |
|
67 | + $f[9] = ParagonIE_Sodium_Core32_Int32::fromInt( $f9 ^ $x9 ); |
|
68 | + $g[0] = ParagonIE_Sodium_Core32_Int32::fromInt( $g0 ^ $x0 ); |
|
69 | + $g[1] = ParagonIE_Sodium_Core32_Int32::fromInt( $g1 ^ $x1 ); |
|
70 | + $g[2] = ParagonIE_Sodium_Core32_Int32::fromInt( $g2 ^ $x2 ); |
|
71 | + $g[3] = ParagonIE_Sodium_Core32_Int32::fromInt( $g3 ^ $x3 ); |
|
72 | + $g[4] = ParagonIE_Sodium_Core32_Int32::fromInt( $g4 ^ $x4 ); |
|
73 | + $g[5] = ParagonIE_Sodium_Core32_Int32::fromInt( $g5 ^ $x5 ); |
|
74 | + $g[6] = ParagonIE_Sodium_Core32_Int32::fromInt( $g6 ^ $x6 ); |
|
75 | + $g[7] = ParagonIE_Sodium_Core32_Int32::fromInt( $g7 ^ $x7 ); |
|
76 | + $g[8] = ParagonIE_Sodium_Core32_Int32::fromInt( $g8 ^ $x8 ); |
|
77 | + $g[9] = ParagonIE_Sodium_Core32_Int32::fromInt( $g9 ^ $x9 ); |
|
78 | 78 | } |
79 | 79 | |
80 | 80 | /** |
@@ -83,42 +83,42 @@ discard block |
||
83 | 83 | * @param ParagonIE_Sodium_Core32_Curve25519_Fe $f |
84 | 84 | * @return ParagonIE_Sodium_Core32_Curve25519_Fe |
85 | 85 | */ |
86 | - public static function fe_mul121666(ParagonIE_Sodium_Core32_Curve25519_Fe $f) |
|
86 | + public static function fe_mul121666( ParagonIE_Sodium_Core32_Curve25519_Fe $f ) |
|
87 | 87 | { |
88 | 88 | /** @var ParagonIE_Sodium_Core32_Int32[] $h */ |
89 | 89 | $h = array( |
90 | - $f[0]->mulInt(121666), |
|
91 | - $f[1]->mulInt(121666), |
|
92 | - $f[2]->mulInt(121666), |
|
93 | - $f[3]->mulInt(121666), |
|
94 | - $f[4]->mulInt(121666), |
|
95 | - $f[5]->mulInt(121666), |
|
96 | - $f[6]->mulInt(121666), |
|
97 | - $f[7]->mulInt(121666), |
|
98 | - $f[8]->mulInt(121666), |
|
99 | - $f[9]->mulInt(121666), |
|
90 | + $f[0]->mulInt( 121666 ), |
|
91 | + $f[1]->mulInt( 121666 ), |
|
92 | + $f[2]->mulInt( 121666 ), |
|
93 | + $f[3]->mulInt( 121666 ), |
|
94 | + $f[4]->mulInt( 121666 ), |
|
95 | + $f[5]->mulInt( 121666 ), |
|
96 | + $f[6]->mulInt( 121666 ), |
|
97 | + $f[7]->mulInt( 121666 ), |
|
98 | + $f[8]->mulInt( 121666 ), |
|
99 | + $f[9]->mulInt( 121666 ), |
|
100 | 100 | ); |
101 | 101 | |
102 | 102 | /** @var ParagonIE_Sodium_Core32_Int32 $carry9 */ |
103 | - $carry9 = $h[9]->addInt(1 << 24)->shiftRight(25); |
|
104 | - $h[0] = $carry9->mulInt(19); |
|
105 | - $h[9] = $h[9]->subInt32($carry9->shiftLeft(25)); |
|
103 | + $carry9 = $h[9]->addInt( 1 << 24 )->shiftRight( 25 ); |
|
104 | + $h[0] = $carry9->mulInt( 19 ); |
|
105 | + $h[9] = $h[9]->subInt32( $carry9->shiftLeft( 25 ) ); |
|
106 | 106 | |
107 | - $carry1 = $h[1]->addInt(1 << 24)->shiftRight(25); |
|
108 | - $h[2] = $h[2]->addInt32($carry1); |
|
109 | - $h[1] = $h[1]->subInt32($carry1->shiftLeft(25)); |
|
107 | + $carry1 = $h[1]->addInt( 1 << 24 )->shiftRight( 25 ); |
|
108 | + $h[2] = $h[2]->addInt32( $carry1 ); |
|
109 | + $h[1] = $h[1]->subInt32( $carry1->shiftLeft( 25 ) ); |
|
110 | 110 | |
111 | - $carry3 = $h[3]->addInt(1 << 24)->shiftRight(25); |
|
112 | - $h[4] = $h[4]->addInt32($carry3); |
|
113 | - $h[3] = $h[3]->subInt32($carry3->shiftLeft(25)); |
|
111 | + $carry3 = $h[3]->addInt( 1 << 24 )->shiftRight( 25 ); |
|
112 | + $h[4] = $h[4]->addInt32( $carry3 ); |
|
113 | + $h[3] = $h[3]->subInt32( $carry3->shiftLeft( 25 ) ); |
|
114 | 114 | |
115 | - $carry5 = $h[3]->addInt(1 << 24)->shiftRight(25); |
|
116 | - $h[6] = $h[6]->addInt32($carry5); |
|
117 | - $h[5] = $h[5]->subInt32($carry5->shiftLeft(25)); |
|
115 | + $carry5 = $h[3]->addInt( 1 << 24 )->shiftRight( 25 ); |
|
116 | + $h[6] = $h[6]->addInt32( $carry5 ); |
|
117 | + $h[5] = $h[5]->subInt32( $carry5->shiftLeft( 25 ) ); |
|
118 | 118 | |
119 | - $carry7 = $h[3]->addInt(1 << 24)->shiftRight(25); |
|
120 | - $h[8] = $h[8]->addInt32($carry7); |
|
121 | - $h[7] = $h[7]->subInt32($carry7->shiftLeft(25)); |
|
119 | + $carry7 = $h[3]->addInt( 1 << 24 )->shiftRight( 25 ); |
|
120 | + $h[8] = $h[8]->addInt32( $carry7 ); |
|
121 | + $h[7] = $h[7]->subInt32( $carry7->shiftLeft( 25 ) ); |
|
122 | 122 | |
123 | 123 | /* |
124 | 124 | $carry9 = ($h[9] + (1 << 24)) >> 25; |
@@ -138,25 +138,25 @@ discard block |
||
138 | 138 | $h[7] -= $carry7 << 25; |
139 | 139 | */ |
140 | 140 | |
141 | - $carry0 = $h[0]->addInt(1 << 25)->shiftRight(26); |
|
142 | - $h[1] = $h[1]->addInt32($carry0); |
|
143 | - $h[0] = $h[0]->subInt32($carry0->shiftLeft(26)); |
|
141 | + $carry0 = $h[0]->addInt( 1 << 25 )->shiftRight( 26 ); |
|
142 | + $h[1] = $h[1]->addInt32( $carry0 ); |
|
143 | + $h[0] = $h[0]->subInt32( $carry0->shiftLeft( 26 ) ); |
|
144 | 144 | |
145 | - $carry2 = $h[2]->addInt(1 << 25)->shiftRight(26); |
|
146 | - $h[3] = $h[3]->addInt32($carry2); |
|
147 | - $h[2] = $h[2]->subInt32($carry2->shiftLeft(26)); |
|
145 | + $carry2 = $h[2]->addInt( 1 << 25 )->shiftRight( 26 ); |
|
146 | + $h[3] = $h[3]->addInt32( $carry2 ); |
|
147 | + $h[2] = $h[2]->subInt32( $carry2->shiftLeft( 26 ) ); |
|
148 | 148 | |
149 | - $carry4 = $h[4]->addInt(1 << 25)->shiftRight(26); |
|
150 | - $h[5] = $h[5]->addInt32($carry4); |
|
151 | - $h[4] = $h[4]->subInt32($carry4->shiftLeft(26)); |
|
149 | + $carry4 = $h[4]->addInt( 1 << 25 )->shiftRight( 26 ); |
|
150 | + $h[5] = $h[5]->addInt32( $carry4 ); |
|
151 | + $h[4] = $h[4]->subInt32( $carry4->shiftLeft( 26 ) ); |
|
152 | 152 | |
153 | - $carry6 = $h[6]->addInt(1 << 25)->shiftRight(26); |
|
154 | - $h[7] = $h[7]->addInt32($carry6); |
|
155 | - $h[6] = $h[6]->subInt32($carry6->shiftLeft(26)); |
|
153 | + $carry6 = $h[6]->addInt( 1 << 25 )->shiftRight( 26 ); |
|
154 | + $h[7] = $h[7]->addInt32( $carry6 ); |
|
155 | + $h[6] = $h[6]->subInt32( $carry6->shiftLeft( 26 ) ); |
|
156 | 156 | |
157 | - $carry8 = $h[8]->addInt(1 << 25)->shiftRight(26); |
|
158 | - $h[9] = $h[9]->addInt32($carry8); |
|
159 | - $h[8] = $h[8]->subInt32($carry8->shiftLeft(26)); |
|
157 | + $carry8 = $h[8]->addInt( 1 << 25 )->shiftRight( 26 ); |
|
158 | + $h[9] = $h[9]->addInt32( $carry8 ); |
|
159 | + $h[8] = $h[8]->subInt32( $carry8->shiftLeft( 26 ) ); |
|
160 | 160 | |
161 | 161 | /* |
162 | 162 | $carry0 = ($h[0] + (1 << 25)) >> 26; |
@@ -176,7 +176,7 @@ discard block |
||
176 | 176 | $h[8] -= $carry8 << 26; |
177 | 177 | */ |
178 | 178 | |
179 | - return ParagonIE_Sodium_Core32_Curve25519_Fe::fromArray($h); |
|
179 | + return ParagonIE_Sodium_Core32_Curve25519_Fe::fromArray( $h ); |
|
180 | 180 | } |
181 | 181 | |
182 | 182 | /** |
@@ -188,27 +188,27 @@ discard block |
||
188 | 188 | * @param string $p |
189 | 189 | * @return string |
190 | 190 | */ |
191 | - public static function crypto_scalarmult_curve25519_ref10($n, $p) |
|
191 | + public static function crypto_scalarmult_curve25519_ref10( $n, $p ) |
|
192 | 192 | { |
193 | 193 | # for (i = 0;i < 32;++i) e[i] = n[i]; |
194 | 194 | $e = '' . $n; |
195 | 195 | # e[0] &= 248; |
196 | 196 | $e[0] = self::intToChr( |
197 | - self::chrToInt($e[0]) & 248 |
|
197 | + self::chrToInt( $e[0] ) & 248 |
|
198 | 198 | ); |
199 | 199 | # e[31] &= 127; |
200 | 200 | # e[31] |= 64; |
201 | 201 | $e[31] = self::intToChr( |
202 | - (self::chrToInt($e[31]) & 127) | 64 |
|
202 | + ( self::chrToInt( $e[31] ) & 127 ) | 64 |
|
203 | 203 | ); |
204 | 204 | # fe_frombytes(x1,p); |
205 | - $x1 = self::fe_frombytes($p); |
|
205 | + $x1 = self::fe_frombytes( $p ); |
|
206 | 206 | # fe_1(x2); |
207 | 207 | $x2 = self::fe_1(); |
208 | 208 | # fe_0(z2); |
209 | 209 | $z2 = self::fe_0(); |
210 | 210 | # fe_copy(x3,x1); |
211 | - $x3 = self::fe_copy($x1); |
|
211 | + $x3 = self::fe_copy( $x1 ); |
|
212 | 212 | # fe_1(z3); |
213 | 213 | $z3 = self::fe_1(); |
214 | 214 | |
@@ -216,88 +216,88 @@ discard block |
||
216 | 216 | $swap = 0; |
217 | 217 | |
218 | 218 | # for (pos = 254;pos >= 0;--pos) { |
219 | - for ($pos = 254; $pos >= 0; --$pos) { |
|
219 | + for ( $pos = 254; $pos >= 0; --$pos ) { |
|
220 | 220 | # b = e[pos / 8] >> (pos & 7); |
221 | 221 | $b = self::chrToInt( |
222 | - $e[(int) floor($pos / 8)] |
|
223 | - ) >> ($pos & 7); |
|
222 | + $e[(int) floor( $pos / 8 )] |
|
223 | + ) >> ( $pos & 7 ); |
|
224 | 224 | # b &= 1; |
225 | 225 | $b &= 1; |
226 | 226 | # swap ^= b; |
227 | 227 | $swap ^= $b; |
228 | 228 | # fe_cswap(x2,x3,swap); |
229 | - self::fe_cswap($x2, $x3, $swap); |
|
229 | + self::fe_cswap( $x2, $x3, $swap ); |
|
230 | 230 | # fe_cswap(z2,z3,swap); |
231 | - self::fe_cswap($z2, $z3, $swap); |
|
231 | + self::fe_cswap( $z2, $z3, $swap ); |
|
232 | 232 | # swap = b; |
233 | 233 | $swap = $b; |
234 | 234 | # fe_sub(tmp0,x3,z3); |
235 | - $tmp0 = self::fe_sub($x3, $z3); |
|
235 | + $tmp0 = self::fe_sub( $x3, $z3 ); |
|
236 | 236 | # fe_sub(tmp1,x2,z2); |
237 | - $tmp1 = self::fe_sub($x2, $z2); |
|
237 | + $tmp1 = self::fe_sub( $x2, $z2 ); |
|
238 | 238 | |
239 | 239 | # fe_add(x2,x2,z2); |
240 | - $x2 = self::fe_add($x2, $z2); |
|
240 | + $x2 = self::fe_add( $x2, $z2 ); |
|
241 | 241 | |
242 | 242 | # fe_add(z2,x3,z3); |
243 | - $z2 = self::fe_add($x3, $z3); |
|
243 | + $z2 = self::fe_add( $x3, $z3 ); |
|
244 | 244 | |
245 | 245 | # fe_mul(z3,tmp0,x2); |
246 | - $z3 = self::fe_mul($tmp0, $x2); |
|
246 | + $z3 = self::fe_mul( $tmp0, $x2 ); |
|
247 | 247 | |
248 | 248 | # fe_mul(z2,z2,tmp1); |
249 | - $z2 = self::fe_mul($z2, $tmp1); |
|
249 | + $z2 = self::fe_mul( $z2, $tmp1 ); |
|
250 | 250 | |
251 | 251 | # fe_sq(tmp0,tmp1); |
252 | - $tmp0 = self::fe_sq($tmp1); |
|
252 | + $tmp0 = self::fe_sq( $tmp1 ); |
|
253 | 253 | |
254 | 254 | # fe_sq(tmp1,x2); |
255 | - $tmp1 = self::fe_sq($x2); |
|
255 | + $tmp1 = self::fe_sq( $x2 ); |
|
256 | 256 | |
257 | 257 | # fe_add(x3,z3,z2); |
258 | - $x3 = self::fe_add($z3, $z2); |
|
258 | + $x3 = self::fe_add( $z3, $z2 ); |
|
259 | 259 | |
260 | 260 | # fe_sub(z2,z3,z2); |
261 | - $z2 = self::fe_sub($z3, $z2); |
|
261 | + $z2 = self::fe_sub( $z3, $z2 ); |
|
262 | 262 | |
263 | 263 | # fe_mul(x2,tmp1,tmp0); |
264 | - $x2 = self::fe_mul($tmp1, $tmp0); |
|
264 | + $x2 = self::fe_mul( $tmp1, $tmp0 ); |
|
265 | 265 | |
266 | 266 | # fe_sub(tmp1,tmp1,tmp0); |
267 | - $tmp1 = self::fe_sub($tmp1, $tmp0); |
|
267 | + $tmp1 = self::fe_sub( $tmp1, $tmp0 ); |
|
268 | 268 | |
269 | 269 | # fe_sq(z2,z2); |
270 | - $z2 = self::fe_sq($z2); |
|
270 | + $z2 = self::fe_sq( $z2 ); |
|
271 | 271 | |
272 | 272 | # fe_mul121666(z3,tmp1); |
273 | - $z3 = self::fe_mul121666($tmp1); |
|
273 | + $z3 = self::fe_mul121666( $tmp1 ); |
|
274 | 274 | |
275 | 275 | # fe_sq(x3,x3); |
276 | - $x3 = self::fe_sq($x3); |
|
276 | + $x3 = self::fe_sq( $x3 ); |
|
277 | 277 | |
278 | 278 | # fe_add(tmp0,tmp0,z3); |
279 | - $tmp0 = self::fe_add($tmp0, $z3); |
|
279 | + $tmp0 = self::fe_add( $tmp0, $z3 ); |
|
280 | 280 | |
281 | 281 | # fe_mul(z3,x1,z2); |
282 | - $z3 = self::fe_mul($x1, $z2); |
|
282 | + $z3 = self::fe_mul( $x1, $z2 ); |
|
283 | 283 | |
284 | 284 | # fe_mul(z2,tmp1,tmp0); |
285 | - $z2 = self::fe_mul($tmp1, $tmp0); |
|
285 | + $z2 = self::fe_mul( $tmp1, $tmp0 ); |
|
286 | 286 | } |
287 | 287 | |
288 | 288 | # fe_cswap(x2,x3,swap); |
289 | - self::fe_cswap($x2, $x3, $swap); |
|
289 | + self::fe_cswap( $x2, $x3, $swap ); |
|
290 | 290 | |
291 | 291 | # fe_cswap(z2,z3,swap); |
292 | - self::fe_cswap($z2, $z3, $swap); |
|
292 | + self::fe_cswap( $z2, $z3, $swap ); |
|
293 | 293 | |
294 | 294 | # fe_invert(z2,z2); |
295 | - $z2 = self::fe_invert($z2); |
|
295 | + $z2 = self::fe_invert( $z2 ); |
|
296 | 296 | |
297 | 297 | # fe_mul(x2,x2,z2); |
298 | - $x2 = self::fe_mul($x2, $z2); |
|
298 | + $x2 = self::fe_mul( $x2, $z2 ); |
|
299 | 299 | # fe_tobytes(q,x2); |
300 | - return self::fe_tobytes($x2); |
|
300 | + return self::fe_tobytes( $x2 ); |
|
301 | 301 | } |
302 | 302 | |
303 | 303 | /** |
@@ -311,10 +311,10 @@ discard block |
||
311 | 311 | ParagonIE_Sodium_Core32_Curve25519_Fe $edwardsY, |
312 | 312 | ParagonIE_Sodium_Core32_Curve25519_Fe $edwardsZ |
313 | 313 | ) { |
314 | - $tempX = self::fe_add($edwardsZ, $edwardsY); |
|
315 | - $tempZ = self::fe_sub($edwardsZ, $edwardsY); |
|
316 | - $tempZ = self::fe_invert($tempZ); |
|
317 | - return self::fe_mul($tempX, $tempZ); |
|
314 | + $tempX = self::fe_add( $edwardsZ, $edwardsY ); |
|
315 | + $tempZ = self::fe_sub( $edwardsZ, $edwardsY ); |
|
316 | + $tempZ = self::fe_invert( $tempZ ); |
|
317 | + return self::fe_mul( $tempX, $tempZ ); |
|
318 | 318 | } |
319 | 319 | |
320 | 320 | /** |
@@ -324,31 +324,31 @@ discard block |
||
324 | 324 | * @return string |
325 | 325 | * @throws TypeError |
326 | 326 | */ |
327 | - public static function crypto_scalarmult_curve25519_ref10_base($n) |
|
327 | + public static function crypto_scalarmult_curve25519_ref10_base( $n ) |
|
328 | 328 | { |
329 | 329 | # for (i = 0;i < 32;++i) e[i] = n[i]; |
330 | 330 | $e = '' . $n; |
331 | 331 | |
332 | 332 | # e[0] &= 248; |
333 | 333 | $e[0] = self::intToChr( |
334 | - self::chrToInt($e[0]) & 248 |
|
334 | + self::chrToInt( $e[0] ) & 248 |
|
335 | 335 | ); |
336 | 336 | |
337 | 337 | # e[31] &= 127; |
338 | 338 | # e[31] |= 64; |
339 | 339 | $e[31] = self::intToChr( |
340 | - (self::chrToInt($e[31]) & 127) | 64 |
|
340 | + ( self::chrToInt( $e[31] ) & 127 ) | 64 |
|
341 | 341 | ); |
342 | 342 | |
343 | - $A = self::ge_scalarmult_base($e); |
|
343 | + $A = self::ge_scalarmult_base( $e ); |
|
344 | 344 | if ( |
345 | - !($A->Y instanceof ParagonIE_Sodium_Core32_Curve25519_Fe) |
|
345 | + ! ( $A->Y instanceof ParagonIE_Sodium_Core32_Curve25519_Fe ) |
|
346 | 346 | || |
347 | - !($A->Z instanceof ParagonIE_Sodium_Core32_Curve25519_Fe) |
|
347 | + ! ( $A->Z instanceof ParagonIE_Sodium_Core32_Curve25519_Fe ) |
|
348 | 348 | ) { |
349 | - throw new TypeError('Null points encountered'); |
|
349 | + throw new TypeError( 'Null points encountered' ); |
|
350 | 350 | } |
351 | - $pk = self::edwards_to_montgomery($A->Y, $A->Z); |
|
352 | - return self::fe_tobytes($pk); |
|
351 | + $pk = self::edwards_to_montgomery( $A->Y, $A->Z ); |
|
352 | + return self::fe_tobytes( $pk ); |
|
353 | 353 | } |
354 | 354 | } |
@@ -7,8 +7,7 @@ discard block |
||
7 | 7 | /** |
8 | 8 | * Class ParagonIE_Sodium_Core32_X25519 |
9 | 9 | */ |
10 | -abstract class ParagonIE_Sodium_Core32_X25519 extends ParagonIE_Sodium_Core32_Curve25519 |
|
11 | -{ |
|
10 | +abstract class ParagonIE_Sodium_Core32_X25519 extends ParagonIE_Sodium_Core32_Curve25519 { |
|
12 | 11 | /** |
13 | 12 | * Alters the objects passed to this method in place. |
14 | 13 | * |
@@ -83,8 +82,7 @@ discard block |
||
83 | 82 | * @param ParagonIE_Sodium_Core32_Curve25519_Fe $f |
84 | 83 | * @return ParagonIE_Sodium_Core32_Curve25519_Fe |
85 | 84 | */ |
86 | - public static function fe_mul121666(ParagonIE_Sodium_Core32_Curve25519_Fe $f) |
|
87 | - { |
|
85 | + public static function fe_mul121666(ParagonIE_Sodium_Core32_Curve25519_Fe $f) { |
|
88 | 86 | /** @var ParagonIE_Sodium_Core32_Int32[] $h */ |
89 | 87 | $h = array( |
90 | 88 | $f[0]->mulInt(121666), |
@@ -188,8 +186,7 @@ discard block |
||
188 | 186 | * @param string $p |
189 | 187 | * @return string |
190 | 188 | */ |
191 | - public static function crypto_scalarmult_curve25519_ref10($n, $p) |
|
192 | - { |
|
189 | + public static function crypto_scalarmult_curve25519_ref10($n, $p) { |
|
193 | 190 | # for (i = 0;i < 32;++i) e[i] = n[i]; |
194 | 191 | $e = '' . $n; |
195 | 192 | # e[0] &= 248; |
@@ -324,8 +321,7 @@ discard block |
||
324 | 321 | * @return string |
325 | 322 | * @throws TypeError |
326 | 323 | */ |
327 | - public static function crypto_scalarmult_curve25519_ref10_base($n) |
|
328 | - { |
|
324 | + public static function crypto_scalarmult_curve25519_ref10_base($n) { |
|
329 | 325 | # for (i = 0;i < 32;++i) e[i] = n[i]; |
330 | 326 | $e = '' . $n; |
331 | 327 |
@@ -1,7 +1,7 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | if (class_exists('ParagonIE_Sodium_Core_ChaCha20_IetfCtx', false)) { |
4 | - return; |
|
4 | + return; |
|
5 | 5 | } |
6 | 6 | |
7 | 7 | /** |
@@ -9,29 +9,29 @@ discard block |
||
9 | 9 | */ |
10 | 10 | class ParagonIE_Sodium_Core32_ChaCha20_IetfCtx extends ParagonIE_Sodium_Core32_ChaCha20_Ctx |
11 | 11 | { |
12 | - /** |
|
13 | - * ParagonIE_Sodium_Core_ChaCha20_IetfCtx constructor. |
|
14 | - * |
|
15 | - * @internal You should not use this directly from another application |
|
16 | - * |
|
17 | - * @param string $key ChaCha20 key. |
|
18 | - * @param string $iv Initialization Vector (a.k.a. nonce). |
|
19 | - * @param string $counter The initial counter value. |
|
20 | - * Defaults to 4 0x00 bytes. |
|
21 | - * @throws InvalidArgumentException |
|
22 | - */ |
|
23 | - public function __construct($key = '', $iv = '', $counter = '') |
|
24 | - { |
|
25 | - if (self::strlen($iv) !== 12) { |
|
26 | - throw new InvalidArgumentException('ChaCha20 expects a 96-bit nonce in IETF mode.'); |
|
27 | - } |
|
28 | - parent::__construct($key, self::substr($iv, 0, 8), $counter); |
|
12 | + /** |
|
13 | + * ParagonIE_Sodium_Core_ChaCha20_IetfCtx constructor. |
|
14 | + * |
|
15 | + * @internal You should not use this directly from another application |
|
16 | + * |
|
17 | + * @param string $key ChaCha20 key. |
|
18 | + * @param string $iv Initialization Vector (a.k.a. nonce). |
|
19 | + * @param string $counter The initial counter value. |
|
20 | + * Defaults to 4 0x00 bytes. |
|
21 | + * @throws InvalidArgumentException |
|
22 | + */ |
|
23 | + public function __construct($key = '', $iv = '', $counter = '') |
|
24 | + { |
|
25 | + if (self::strlen($iv) !== 12) { |
|
26 | + throw new InvalidArgumentException('ChaCha20 expects a 96-bit nonce in IETF mode.'); |
|
27 | + } |
|
28 | + parent::__construct($key, self::substr($iv, 0, 8), $counter); |
|
29 | 29 | |
30 | - if (!empty($counter)) { |
|
31 | - $this->container[12] = ParagonIE_Sodium_Core32_Int32::fromString(self::substr($counter, 0, 4)); |
|
32 | - } |
|
33 | - $this->container[13] = ParagonIE_Sodium_Core32_Int32::fromString(self::substr($iv, 0, 4)); |
|
34 | - $this->container[14] = ParagonIE_Sodium_Core32_Int32::fromString(self::substr($iv, 4, 4)); |
|
35 | - $this->container[15] = ParagonIE_Sodium_Core32_Int32::fromString(self::substr($iv, 4, 4)); |
|
36 | - } |
|
30 | + if (!empty($counter)) { |
|
31 | + $this->container[12] = ParagonIE_Sodium_Core32_Int32::fromString(self::substr($counter, 0, 4)); |
|
32 | + } |
|
33 | + $this->container[13] = ParagonIE_Sodium_Core32_Int32::fromString(self::substr($iv, 0, 4)); |
|
34 | + $this->container[14] = ParagonIE_Sodium_Core32_Int32::fromString(self::substr($iv, 4, 4)); |
|
35 | + $this->container[15] = ParagonIE_Sodium_Core32_Int32::fromString(self::substr($iv, 4, 4)); |
|
36 | + } |
|
37 | 37 | } |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -if (class_exists('ParagonIE_Sodium_Core_ChaCha20_IetfCtx', false)) { |
|
3 | +if ( class_exists( 'ParagonIE_Sodium_Core_ChaCha20_IetfCtx', false ) ) { |
|
4 | 4 | return; |
5 | 5 | } |
6 | 6 | |
@@ -20,18 +20,18 @@ discard block |
||
20 | 20 | * Defaults to 4 0x00 bytes. |
21 | 21 | * @throws InvalidArgumentException |
22 | 22 | */ |
23 | - public function __construct($key = '', $iv = '', $counter = '') |
|
23 | + public function __construct( $key = '', $iv = '', $counter = '' ) |
|
24 | 24 | { |
25 | - if (self::strlen($iv) !== 12) { |
|
26 | - throw new InvalidArgumentException('ChaCha20 expects a 96-bit nonce in IETF mode.'); |
|
25 | + if ( self::strlen( $iv ) !== 12 ) { |
|
26 | + throw new InvalidArgumentException( 'ChaCha20 expects a 96-bit nonce in IETF mode.' ); |
|
27 | 27 | } |
28 | - parent::__construct($key, self::substr($iv, 0, 8), $counter); |
|
28 | + parent::__construct( $key, self::substr( $iv, 0, 8 ), $counter ); |
|
29 | 29 | |
30 | - if (!empty($counter)) { |
|
31 | - $this->container[12] = ParagonIE_Sodium_Core32_Int32::fromString(self::substr($counter, 0, 4)); |
|
30 | + if ( ! empty( $counter ) ) { |
|
31 | + $this->container[12] = ParagonIE_Sodium_Core32_Int32::fromString( self::substr( $counter, 0, 4 ) ); |
|
32 | 32 | } |
33 | - $this->container[13] = ParagonIE_Sodium_Core32_Int32::fromString(self::substr($iv, 0, 4)); |
|
34 | - $this->container[14] = ParagonIE_Sodium_Core32_Int32::fromString(self::substr($iv, 4, 4)); |
|
35 | - $this->container[15] = ParagonIE_Sodium_Core32_Int32::fromString(self::substr($iv, 4, 4)); |
|
33 | + $this->container[13] = ParagonIE_Sodium_Core32_Int32::fromString( self::substr( $iv, 0, 4 ) ); |
|
34 | + $this->container[14] = ParagonIE_Sodium_Core32_Int32::fromString( self::substr( $iv, 4, 4 ) ); |
|
35 | + $this->container[15] = ParagonIE_Sodium_Core32_Int32::fromString( self::substr( $iv, 4, 4 ) ); |
|
36 | 36 | } |
37 | 37 | } |
@@ -7,8 +7,7 @@ discard block |
||
7 | 7 | /** |
8 | 8 | * Class ParagonIE_Sodium_Core32_ChaCha20_IetfCtx |
9 | 9 | */ |
10 | -class ParagonIE_Sodium_Core32_ChaCha20_IetfCtx extends ParagonIE_Sodium_Core32_ChaCha20_Ctx |
|
11 | -{ |
|
10 | +class ParagonIE_Sodium_Core32_ChaCha20_IetfCtx extends ParagonIE_Sodium_Core32_ChaCha20_Ctx { |
|
12 | 11 | /** |
13 | 12 | * ParagonIE_Sodium_Core_ChaCha20_IetfCtx constructor. |
14 | 13 | * |
@@ -20,8 +19,7 @@ discard block |
||
20 | 19 | * Defaults to 4 0x00 bytes. |
21 | 20 | * @throws InvalidArgumentException |
22 | 21 | */ |
23 | - public function __construct($key = '', $iv = '', $counter = '') |
|
24 | - { |
|
22 | + public function __construct($key = '', $iv = '', $counter = '') { |
|
25 | 23 | if (self::strlen($iv) !== 12) { |
26 | 24 | throw new InvalidArgumentException('ChaCha20 expects a 96-bit nonce in IETF mode.'); |
27 | 25 | } |
@@ -1,7 +1,7 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | if (class_exists('ParagonIE_Sodium_Core_ChaCha20_Ctx', false)) { |
4 | - return; |
|
4 | + return; |
|
5 | 5 | } |
6 | 6 | |
7 | 7 | /** |
@@ -9,113 +9,113 @@ discard block |
||
9 | 9 | */ |
10 | 10 | class ParagonIE_Sodium_Core32_ChaCha20_Ctx extends ParagonIE_Sodium_Core32_Util implements ArrayAccess |
11 | 11 | { |
12 | - /** |
|
13 | - * @var SplFixedArray<int, ParagonIE_Sodium_Core32_Int32> |
|
14 | - */ |
|
15 | - protected $container; |
|
12 | + /** |
|
13 | + * @var SplFixedArray<int, ParagonIE_Sodium_Core32_Int32> |
|
14 | + */ |
|
15 | + protected $container; |
|
16 | 16 | |
17 | - /** |
|
18 | - * ParagonIE_Sodium_Core_ChaCha20_Ctx constructor. |
|
19 | - * |
|
20 | - * @internal You should not use this directly from another application |
|
21 | - * |
|
22 | - * @param string $key ChaCha20 key. |
|
23 | - * @param string $iv Initialization Vector (a.k.a. nonce). |
|
24 | - * @param string $counter The initial counter value. |
|
25 | - * Defaults to 8 0x00 bytes. |
|
26 | - * @throws InvalidArgumentException |
|
27 | - */ |
|
28 | - public function __construct($key = '', $iv = '', $counter = '') |
|
29 | - { |
|
30 | - if (self::strlen($key) !== 32) { |
|
31 | - throw new InvalidArgumentException('ChaCha20 expects a 256-bit key.'); |
|
32 | - } |
|
33 | - if (self::strlen($iv) !== 8) { |
|
34 | - throw new InvalidArgumentException('ChaCha20 expects a 64-bit nonce.'); |
|
35 | - } |
|
36 | - $this->container = new SplFixedArray(16); |
|
17 | + /** |
|
18 | + * ParagonIE_Sodium_Core_ChaCha20_Ctx constructor. |
|
19 | + * |
|
20 | + * @internal You should not use this directly from another application |
|
21 | + * |
|
22 | + * @param string $key ChaCha20 key. |
|
23 | + * @param string $iv Initialization Vector (a.k.a. nonce). |
|
24 | + * @param string $counter The initial counter value. |
|
25 | + * Defaults to 8 0x00 bytes. |
|
26 | + * @throws InvalidArgumentException |
|
27 | + */ |
|
28 | + public function __construct($key = '', $iv = '', $counter = '') |
|
29 | + { |
|
30 | + if (self::strlen($key) !== 32) { |
|
31 | + throw new InvalidArgumentException('ChaCha20 expects a 256-bit key.'); |
|
32 | + } |
|
33 | + if (self::strlen($iv) !== 8) { |
|
34 | + throw new InvalidArgumentException('ChaCha20 expects a 64-bit nonce.'); |
|
35 | + } |
|
36 | + $this->container = new SplFixedArray(16); |
|
37 | 37 | |
38 | - /* "expand 32-byte k" as per ChaCha20 spec */ |
|
39 | - $this->container[0] = new ParagonIE_Sodium_Core32_Int32(array(0x6170, 0x7865)); |
|
40 | - $this->container[1] = new ParagonIE_Sodium_Core32_Int32(array(0x3320, 0x646e)); |
|
41 | - $this->container[2] = new ParagonIE_Sodium_Core32_Int32(array(0x7962, 0x2d32)); |
|
42 | - $this->container[3] = new ParagonIE_Sodium_Core32_Int32(array(0x6b20, 0x6574)); |
|
38 | + /* "expand 32-byte k" as per ChaCha20 spec */ |
|
39 | + $this->container[0] = new ParagonIE_Sodium_Core32_Int32(array(0x6170, 0x7865)); |
|
40 | + $this->container[1] = new ParagonIE_Sodium_Core32_Int32(array(0x3320, 0x646e)); |
|
41 | + $this->container[2] = new ParagonIE_Sodium_Core32_Int32(array(0x7962, 0x2d32)); |
|
42 | + $this->container[3] = new ParagonIE_Sodium_Core32_Int32(array(0x6b20, 0x6574)); |
|
43 | 43 | |
44 | - $this->container[4] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 0, 4)); |
|
45 | - $this->container[5] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 4, 4)); |
|
46 | - $this->container[6] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 8, 4)); |
|
47 | - $this->container[7] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 12, 4)); |
|
48 | - $this->container[8] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 16, 4)); |
|
49 | - $this->container[9] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 20, 4)); |
|
50 | - $this->container[10] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 24, 4)); |
|
51 | - $this->container[11] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 28, 4)); |
|
44 | + $this->container[4] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 0, 4)); |
|
45 | + $this->container[5] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 4, 4)); |
|
46 | + $this->container[6] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 8, 4)); |
|
47 | + $this->container[7] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 12, 4)); |
|
48 | + $this->container[8] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 16, 4)); |
|
49 | + $this->container[9] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 20, 4)); |
|
50 | + $this->container[10] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 24, 4)); |
|
51 | + $this->container[11] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 28, 4)); |
|
52 | 52 | |
53 | - if (empty($counter)) { |
|
54 | - $this->container[12] = new ParagonIE_Sodium_Core32_Int32(); |
|
55 | - $this->container[13] = new ParagonIE_Sodium_Core32_Int32(); |
|
56 | - } else { |
|
57 | - $this->container[12] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($counter, 0, 4)); |
|
58 | - $this->container[13] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($counter, 4, 4)); |
|
59 | - } |
|
60 | - $this->container[14] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($iv, 0, 4)); |
|
61 | - $this->container[15] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($iv, 4, 4)); |
|
62 | - } |
|
53 | + if (empty($counter)) { |
|
54 | + $this->container[12] = new ParagonIE_Sodium_Core32_Int32(); |
|
55 | + $this->container[13] = new ParagonIE_Sodium_Core32_Int32(); |
|
56 | + } else { |
|
57 | + $this->container[12] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($counter, 0, 4)); |
|
58 | + $this->container[13] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($counter, 4, 4)); |
|
59 | + } |
|
60 | + $this->container[14] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($iv, 0, 4)); |
|
61 | + $this->container[15] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($iv, 4, 4)); |
|
62 | + } |
|
63 | 63 | |
64 | - /** |
|
65 | - * @internal You should not use this directly from another application |
|
66 | - * |
|
67 | - * @param int $offset |
|
68 | - * @param int|ParagonIE_Sodium_Core32_Int32 $value |
|
69 | - * @return void |
|
70 | - */ |
|
71 | - public function offsetSet($offset, $value) |
|
72 | - { |
|
73 | - if (!is_int($offset)) { |
|
74 | - throw new InvalidArgumentException('Expected an integer'); |
|
75 | - } |
|
76 | - if ($value instanceof ParagonIE_Sodium_Core32_Int32) { |
|
77 | - /* |
|
64 | + /** |
|
65 | + * @internal You should not use this directly from another application |
|
66 | + * |
|
67 | + * @param int $offset |
|
68 | + * @param int|ParagonIE_Sodium_Core32_Int32 $value |
|
69 | + * @return void |
|
70 | + */ |
|
71 | + public function offsetSet($offset, $value) |
|
72 | + { |
|
73 | + if (!is_int($offset)) { |
|
74 | + throw new InvalidArgumentException('Expected an integer'); |
|
75 | + } |
|
76 | + if ($value instanceof ParagonIE_Sodium_Core32_Int32) { |
|
77 | + /* |
|
78 | 78 | } elseif (is_int($value)) { |
79 | 79 | $value = ParagonIE_Sodium_Core32_Int32::fromInt($value); |
80 | 80 | */ |
81 | - } else { |
|
82 | - throw new InvalidArgumentException('Expected an integer'); |
|
83 | - } |
|
84 | - $this->container[$offset] = $value; |
|
85 | - } |
|
81 | + } else { |
|
82 | + throw new InvalidArgumentException('Expected an integer'); |
|
83 | + } |
|
84 | + $this->container[$offset] = $value; |
|
85 | + } |
|
86 | 86 | |
87 | - /** |
|
88 | - * @internal You should not use this directly from another application |
|
89 | - * |
|
90 | - * @param mixed $offset |
|
91 | - * @return bool |
|
92 | - */ |
|
93 | - public function offsetExists($offset) |
|
94 | - { |
|
95 | - return isset($this->container[$offset]); |
|
96 | - } |
|
87 | + /** |
|
88 | + * @internal You should not use this directly from another application |
|
89 | + * |
|
90 | + * @param mixed $offset |
|
91 | + * @return bool |
|
92 | + */ |
|
93 | + public function offsetExists($offset) |
|
94 | + { |
|
95 | + return isset($this->container[$offset]); |
|
96 | + } |
|
97 | 97 | |
98 | - /** |
|
99 | - * @internal You should not use this directly from another application |
|
100 | - * |
|
101 | - * @param mixed $offset |
|
102 | - * @return void |
|
103 | - */ |
|
104 | - public function offsetUnset($offset) |
|
105 | - { |
|
106 | - unset($this->container[$offset]); |
|
107 | - } |
|
98 | + /** |
|
99 | + * @internal You should not use this directly from another application |
|
100 | + * |
|
101 | + * @param mixed $offset |
|
102 | + * @return void |
|
103 | + */ |
|
104 | + public function offsetUnset($offset) |
|
105 | + { |
|
106 | + unset($this->container[$offset]); |
|
107 | + } |
|
108 | 108 | |
109 | - /** |
|
110 | - * @internal You should not use this directly from another application |
|
111 | - * |
|
112 | - * @param mixed $offset |
|
113 | - * @return mixed|null |
|
114 | - */ |
|
115 | - public function offsetGet($offset) |
|
116 | - { |
|
117 | - return isset($this->container[$offset]) |
|
118 | - ? $this->container[$offset] |
|
119 | - : null; |
|
120 | - } |
|
109 | + /** |
|
110 | + * @internal You should not use this directly from another application |
|
111 | + * |
|
112 | + * @param mixed $offset |
|
113 | + * @return mixed|null |
|
114 | + */ |
|
115 | + public function offsetGet($offset) |
|
116 | + { |
|
117 | + return isset($this->container[$offset]) |
|
118 | + ? $this->container[$offset] |
|
119 | + : null; |
|
120 | + } |
|
121 | 121 | } |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -if (class_exists('ParagonIE_Sodium_Core_ChaCha20_Ctx', false)) { |
|
3 | +if ( class_exists( 'ParagonIE_Sodium_Core_ChaCha20_Ctx', false ) ) { |
|
4 | 4 | return; |
5 | 5 | } |
6 | 6 | |
@@ -25,40 +25,40 @@ discard block |
||
25 | 25 | * Defaults to 8 0x00 bytes. |
26 | 26 | * @throws InvalidArgumentException |
27 | 27 | */ |
28 | - public function __construct($key = '', $iv = '', $counter = '') |
|
28 | + public function __construct( $key = '', $iv = '', $counter = '' ) |
|
29 | 29 | { |
30 | - if (self::strlen($key) !== 32) { |
|
31 | - throw new InvalidArgumentException('ChaCha20 expects a 256-bit key.'); |
|
30 | + if ( self::strlen( $key ) !== 32 ) { |
|
31 | + throw new InvalidArgumentException( 'ChaCha20 expects a 256-bit key.' ); |
|
32 | 32 | } |
33 | - if (self::strlen($iv) !== 8) { |
|
34 | - throw new InvalidArgumentException('ChaCha20 expects a 64-bit nonce.'); |
|
33 | + if ( self::strlen( $iv ) !== 8 ) { |
|
34 | + throw new InvalidArgumentException( 'ChaCha20 expects a 64-bit nonce.' ); |
|
35 | 35 | } |
36 | - $this->container = new SplFixedArray(16); |
|
36 | + $this->container = new SplFixedArray( 16 ); |
|
37 | 37 | |
38 | 38 | /* "expand 32-byte k" as per ChaCha20 spec */ |
39 | - $this->container[0] = new ParagonIE_Sodium_Core32_Int32(array(0x6170, 0x7865)); |
|
40 | - $this->container[1] = new ParagonIE_Sodium_Core32_Int32(array(0x3320, 0x646e)); |
|
41 | - $this->container[2] = new ParagonIE_Sodium_Core32_Int32(array(0x7962, 0x2d32)); |
|
42 | - $this->container[3] = new ParagonIE_Sodium_Core32_Int32(array(0x6b20, 0x6574)); |
|
39 | + $this->container[0] = new ParagonIE_Sodium_Core32_Int32( array( 0x6170, 0x7865 ) ); |
|
40 | + $this->container[1] = new ParagonIE_Sodium_Core32_Int32( array( 0x3320, 0x646e ) ); |
|
41 | + $this->container[2] = new ParagonIE_Sodium_Core32_Int32( array( 0x7962, 0x2d32 ) ); |
|
42 | + $this->container[3] = new ParagonIE_Sodium_Core32_Int32( array( 0x6b20, 0x6574 ) ); |
|
43 | 43 | |
44 | - $this->container[4] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 0, 4)); |
|
45 | - $this->container[5] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 4, 4)); |
|
46 | - $this->container[6] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 8, 4)); |
|
47 | - $this->container[7] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 12, 4)); |
|
48 | - $this->container[8] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 16, 4)); |
|
49 | - $this->container[9] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 20, 4)); |
|
50 | - $this->container[10] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 24, 4)); |
|
51 | - $this->container[11] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 28, 4)); |
|
44 | + $this->container[4] = ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $key, 0, 4 ) ); |
|
45 | + $this->container[5] = ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $key, 4, 4 ) ); |
|
46 | + $this->container[6] = ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $key, 8, 4 ) ); |
|
47 | + $this->container[7] = ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $key, 12, 4 ) ); |
|
48 | + $this->container[8] = ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $key, 16, 4 ) ); |
|
49 | + $this->container[9] = ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $key, 20, 4 ) ); |
|
50 | + $this->container[10] = ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $key, 24, 4 ) ); |
|
51 | + $this->container[11] = ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $key, 28, 4 ) ); |
|
52 | 52 | |
53 | - if (empty($counter)) { |
|
53 | + if ( empty( $counter ) ) { |
|
54 | 54 | $this->container[12] = new ParagonIE_Sodium_Core32_Int32(); |
55 | 55 | $this->container[13] = new ParagonIE_Sodium_Core32_Int32(); |
56 | 56 | } else { |
57 | - $this->container[12] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($counter, 0, 4)); |
|
58 | - $this->container[13] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($counter, 4, 4)); |
|
57 | + $this->container[12] = ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $counter, 0, 4 ) ); |
|
58 | + $this->container[13] = ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $counter, 4, 4 ) ); |
|
59 | 59 | } |
60 | - $this->container[14] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($iv, 0, 4)); |
|
61 | - $this->container[15] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($iv, 4, 4)); |
|
60 | + $this->container[14] = ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $iv, 0, 4 ) ); |
|
61 | + $this->container[15] = ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $iv, 4, 4 ) ); |
|
62 | 62 | } |
63 | 63 | |
64 | 64 | /** |
@@ -68,18 +68,18 @@ discard block |
||
68 | 68 | * @param int|ParagonIE_Sodium_Core32_Int32 $value |
69 | 69 | * @return void |
70 | 70 | */ |
71 | - public function offsetSet($offset, $value) |
|
71 | + public function offsetSet( $offset, $value ) |
|
72 | 72 | { |
73 | - if (!is_int($offset)) { |
|
74 | - throw new InvalidArgumentException('Expected an integer'); |
|
73 | + if ( ! is_int( $offset ) ) { |
|
74 | + throw new InvalidArgumentException( 'Expected an integer' ); |
|
75 | 75 | } |
76 | - if ($value instanceof ParagonIE_Sodium_Core32_Int32) { |
|
76 | + if ( $value instanceof ParagonIE_Sodium_Core32_Int32 ) { |
|
77 | 77 | /* |
78 | 78 | } elseif (is_int($value)) { |
79 | 79 | $value = ParagonIE_Sodium_Core32_Int32::fromInt($value); |
80 | 80 | */ |
81 | 81 | } else { |
82 | - throw new InvalidArgumentException('Expected an integer'); |
|
82 | + throw new InvalidArgumentException( 'Expected an integer' ); |
|
83 | 83 | } |
84 | 84 | $this->container[$offset] = $value; |
85 | 85 | } |
@@ -90,9 +90,9 @@ discard block |
||
90 | 90 | * @param mixed $offset |
91 | 91 | * @return bool |
92 | 92 | */ |
93 | - public function offsetExists($offset) |
|
93 | + public function offsetExists( $offset ) |
|
94 | 94 | { |
95 | - return isset($this->container[$offset]); |
|
95 | + return isset( $this->container[$offset] ); |
|
96 | 96 | } |
97 | 97 | |
98 | 98 | /** |
@@ -101,9 +101,9 @@ discard block |
||
101 | 101 | * @param mixed $offset |
102 | 102 | * @return void |
103 | 103 | */ |
104 | - public function offsetUnset($offset) |
|
104 | + public function offsetUnset( $offset ) |
|
105 | 105 | { |
106 | - unset($this->container[$offset]); |
|
106 | + unset( $this->container[$offset] ); |
|
107 | 107 | } |
108 | 108 | |
109 | 109 | /** |
@@ -112,9 +112,9 @@ discard block |
||
112 | 112 | * @param mixed $offset |
113 | 113 | * @return mixed|null |
114 | 114 | */ |
115 | - public function offsetGet($offset) |
|
115 | + public function offsetGet( $offset ) |
|
116 | 116 | { |
117 | - return isset($this->container[$offset]) |
|
117 | + return isset( $this->container[$offset] ) |
|
118 | 118 | ? $this->container[$offset] |
119 | 119 | : null; |
120 | 120 | } |
@@ -7,8 +7,7 @@ discard block |
||
7 | 7 | /** |
8 | 8 | * Class ParagonIE_Sodium_Core32_ChaCha20_Ctx |
9 | 9 | */ |
10 | -class ParagonIE_Sodium_Core32_ChaCha20_Ctx extends ParagonIE_Sodium_Core32_Util implements ArrayAccess |
|
11 | -{ |
|
10 | +class ParagonIE_Sodium_Core32_ChaCha20_Ctx extends ParagonIE_Sodium_Core32_Util implements ArrayAccess { |
|
12 | 11 | /** |
13 | 12 | * @var SplFixedArray<int, ParagonIE_Sodium_Core32_Int32> |
14 | 13 | */ |
@@ -25,8 +24,7 @@ discard block |
||
25 | 24 | * Defaults to 8 0x00 bytes. |
26 | 25 | * @throws InvalidArgumentException |
27 | 26 | */ |
28 | - public function __construct($key = '', $iv = '', $counter = '') |
|
29 | - { |
|
27 | + public function __construct($key = '', $iv = '', $counter = '') { |
|
30 | 28 | if (self::strlen($key) !== 32) { |
31 | 29 | throw new InvalidArgumentException('ChaCha20 expects a 256-bit key.'); |
32 | 30 | } |
@@ -68,8 +66,7 @@ discard block |
||
68 | 66 | * @param int|ParagonIE_Sodium_Core32_Int32 $value |
69 | 67 | * @return void |
70 | 68 | */ |
71 | - public function offsetSet($offset, $value) |
|
72 | - { |
|
69 | + public function offsetSet($offset, $value) { |
|
73 | 70 | if (!is_int($offset)) { |
74 | 71 | throw new InvalidArgumentException('Expected an integer'); |
75 | 72 | } |
@@ -90,8 +87,7 @@ discard block |
||
90 | 87 | * @param mixed $offset |
91 | 88 | * @return bool |
92 | 89 | */ |
93 | - public function offsetExists($offset) |
|
94 | - { |
|
90 | + public function offsetExists($offset) { |
|
95 | 91 | return isset($this->container[$offset]); |
96 | 92 | } |
97 | 93 | |
@@ -101,8 +97,7 @@ discard block |
||
101 | 97 | * @param mixed $offset |
102 | 98 | * @return void |
103 | 99 | */ |
104 | - public function offsetUnset($offset) |
|
105 | - { |
|
100 | + public function offsetUnset($offset) { |
|
106 | 101 | unset($this->container[$offset]); |
107 | 102 | } |
108 | 103 | |
@@ -112,8 +107,7 @@ discard block |
||
112 | 107 | * @param mixed $offset |
113 | 108 | * @return mixed|null |
114 | 109 | */ |
115 | - public function offsetGet($offset) |
|
116 | - { |
|
110 | + public function offsetGet($offset) { |
|
117 | 111 | return isset($this->container[$offset]) |
118 | 112 | ? $this->container[$offset] |
119 | 113 | : null; |
@@ -1,7 +1,7 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | if (class_exists('ParagonIE_Sodium_Core_BLAKE2b', false)) { |
4 | - return; |
|
4 | + return; |
|
5 | 5 | } |
6 | 6 | |
7 | 7 | /** |
@@ -11,591 +11,591 @@ discard block |
||
11 | 11 | */ |
12 | 12 | abstract class ParagonIE_Sodium_Core32_BLAKE2b extends ParagonIE_Sodium_Core_Util |
13 | 13 | { |
14 | - /** |
|
15 | - * @var SplFixedArray |
|
16 | - */ |
|
17 | - public static $iv; |
|
18 | - |
|
19 | - /** |
|
20 | - * @var int[][] |
|
21 | - */ |
|
22 | - public static $sigma = array( |
|
23 | - array( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15), |
|
24 | - array( 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3), |
|
25 | - array( 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4), |
|
26 | - array( 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8), |
|
27 | - array( 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13), |
|
28 | - array( 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9), |
|
29 | - array( 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11), |
|
30 | - array( 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10), |
|
31 | - array( 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5), |
|
32 | - array( 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13 , 0), |
|
33 | - array( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15), |
|
34 | - array( 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3) |
|
35 | - ); |
|
36 | - |
|
37 | - const BLOCKBYTES = 128; |
|
38 | - const OUTBYTES = 64; |
|
39 | - const KEYBYTES = 64; |
|
40 | - |
|
41 | - /** |
|
42 | - * Turn two 32-bit integers into a fixed array representing a 64-bit integer. |
|
43 | - * |
|
44 | - * @internal You should not use this directly from another application |
|
45 | - * |
|
46 | - * @param int $high |
|
47 | - * @param int $low |
|
48 | - * @return ParagonIE_Sodium_Core32_Int64 |
|
49 | - */ |
|
50 | - public static function new64($high, $low) |
|
51 | - { |
|
52 | - return ParagonIE_Sodium_Core32_Int64::fromInts($low, $high); |
|
53 | - } |
|
54 | - |
|
55 | - /** |
|
56 | - * Convert an arbitrary number into an SplFixedArray of two 32-bit integers |
|
57 | - * that represents a 64-bit integer. |
|
58 | - * |
|
59 | - * @internal You should not use this directly from another application |
|
60 | - * |
|
61 | - * @param int $num |
|
62 | - * @return ParagonIE_Sodium_Core32_Int64 |
|
63 | - */ |
|
64 | - protected static function to64($num) |
|
65 | - { |
|
66 | - list($hi, $lo) = self::numericTo64BitInteger($num); |
|
67 | - return self::new64($hi, $lo); |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * Adds two 64-bit integers together, returning their sum as a SplFixedArray |
|
72 | - * containing two 32-bit integers (representing a 64-bit integer). |
|
73 | - * |
|
74 | - * @internal You should not use this directly from another application |
|
75 | - * |
|
76 | - * @param ParagonIE_Sodium_Core32_Int64 $x |
|
77 | - * @param ParagonIE_Sodium_Core32_Int64 $y |
|
78 | - * @return ParagonIE_Sodium_Core32_Int64 |
|
79 | - */ |
|
80 | - protected static function add64($x, $y) |
|
81 | - { |
|
82 | - return $x->addInt64($y); |
|
83 | - } |
|
84 | - |
|
85 | - /** |
|
86 | - * @internal You should not use this directly from another application |
|
87 | - * |
|
88 | - * @param ParagonIE_Sodium_Core32_Int64 $x |
|
89 | - * @param ParagonIE_Sodium_Core32_Int64 $y |
|
90 | - * @param ParagonIE_Sodium_Core32_Int64 $z |
|
91 | - * @return ParagonIE_Sodium_Core32_Int64 |
|
92 | - */ |
|
93 | - public static function add364($x, $y, $z) |
|
94 | - { |
|
95 | - return $x->addInt64($y)->addInt64($z); |
|
96 | - } |
|
97 | - |
|
98 | - /** |
|
99 | - * @internal You should not use this directly from another application |
|
100 | - * |
|
101 | - * @param ParagonIE_Sodium_Core32_Int64 $x |
|
102 | - * @param ParagonIE_Sodium_Core32_Int64 $y |
|
103 | - * @return ParagonIE_Sodium_Core32_Int64 |
|
104 | - * @throws Exception |
|
105 | - */ |
|
106 | - public static function xor64(ParagonIE_Sodium_Core32_Int64 $x, ParagonIE_Sodium_Core32_Int64 $y) |
|
107 | - { |
|
108 | - return $x->xorInt64($y); |
|
109 | - } |
|
110 | - |
|
111 | - /** |
|
112 | - * @internal You should not use this directly from another application |
|
113 | - * |
|
114 | - * @param ParagonIE_Sodium_Core32_Int64 $x |
|
115 | - * @param int $c |
|
116 | - * @return ParagonIE_Sodium_Core32_Int64 |
|
117 | - */ |
|
118 | - public static function rotr64(ParagonIE_Sodium_Core32_Int64 $x, $c) |
|
119 | - { |
|
120 | - return $x->rotateRight($c); |
|
121 | - } |
|
122 | - |
|
123 | - /** |
|
124 | - * @internal You should not use this directly from another application |
|
125 | - * |
|
126 | - * @param SplFixedArray $x |
|
127 | - * @param int $i |
|
128 | - * @return ParagonIE_Sodium_Core32_Int64 |
|
129 | - */ |
|
130 | - public static function load64($x, $i) |
|
131 | - { |
|
132 | - $l = $x[$i] | ($x[$i+1]<<8) | ($x[$i+2]<<16) | ($x[$i+3]<<24); |
|
133 | - $h = $x[$i+4] | ($x[$i+5]<<8) | ($x[$i+6]<<16) | ($x[$i+7]<<24); |
|
134 | - return self::new64($h, $l); |
|
135 | - } |
|
136 | - |
|
137 | - /** |
|
138 | - * @internal You should not use this directly from another application |
|
139 | - * |
|
140 | - * @param SplFixedArray $x |
|
141 | - * @param int $i |
|
142 | - * @param ParagonIE_Sodium_Core32_Int64 $u |
|
143 | - * @return void |
|
144 | - */ |
|
145 | - public static function store64(SplFixedArray $x, $i, ParagonIE_Sodium_Core32_Int64 $u) |
|
146 | - { |
|
147 | - $v = clone $u; |
|
148 | - $maxLength = $x->getSize() - 1; |
|
149 | - for ($j = 0; $j < 8; ++$j) { |
|
150 | - $k = 3 - ($j >> 1); |
|
151 | - $x[$i] = $v->limbs[$k] & 0xff; |
|
152 | - if (++$i > $maxLength) { |
|
153 | - return; |
|
154 | - } |
|
155 | - $v->limbs[$k] >>= 8; |
|
156 | - } |
|
157 | - } |
|
158 | - |
|
159 | - /** |
|
160 | - * This just sets the $iv static variable. |
|
161 | - * |
|
162 | - * @internal You should not use this directly from another application |
|
163 | - * |
|
164 | - * @return void |
|
165 | - */ |
|
166 | - public static function pseudoConstructor() |
|
167 | - { |
|
168 | - static $called = false; |
|
169 | - if ($called) { |
|
170 | - return; |
|
171 | - } |
|
172 | - self::$iv = new SplFixedArray(8); |
|
173 | - self::$iv[0] = self::new64(0x6a09e667, 0xf3bcc908); |
|
174 | - self::$iv[1] = self::new64(0xbb67ae85, 0x84caa73b); |
|
175 | - self::$iv[2] = self::new64(0x3c6ef372, 0xfe94f82b); |
|
176 | - self::$iv[3] = self::new64(0xa54ff53a, 0x5f1d36f1); |
|
177 | - self::$iv[4] = self::new64(0x510e527f, 0xade682d1); |
|
178 | - self::$iv[5] = self::new64(0x9b05688c, 0x2b3e6c1f); |
|
179 | - self::$iv[6] = self::new64(0x1f83d9ab, 0xfb41bd6b); |
|
180 | - self::$iv[7] = self::new64(0x5be0cd19, 0x137e2179); |
|
181 | - |
|
182 | - $called = true; |
|
183 | - } |
|
184 | - |
|
185 | - /** |
|
186 | - * Returns a fresh BLAKE2 context. |
|
187 | - * |
|
188 | - * @internal You should not use this directly from another application |
|
189 | - * |
|
190 | - * @return SplFixedArray |
|
191 | - */ |
|
192 | - protected static function context() |
|
193 | - { |
|
194 | - $ctx = new SplFixedArray(5); |
|
195 | - $ctx[0] = new SplFixedArray(8); // h |
|
196 | - $ctx[1] = new SplFixedArray(2); // t |
|
197 | - $ctx[2] = new SplFixedArray(2); // f |
|
198 | - $ctx[3] = new SplFixedArray(256); // buf |
|
199 | - $ctx[4] = 0; // buflen |
|
200 | - |
|
201 | - for ($i = 8; $i--;) { |
|
202 | - $ctx[0][$i] = self::$iv[$i]; |
|
203 | - } |
|
204 | - for ($i = 256; $i--;) { |
|
205 | - $ctx[3][$i] = 0; |
|
206 | - } |
|
207 | - |
|
208 | - $zero = self::new64(0, 0); |
|
209 | - $ctx[1][0] = $zero; |
|
210 | - $ctx[1][1] = $zero; |
|
211 | - $ctx[2][0] = $zero; |
|
212 | - $ctx[2][1] = $zero; |
|
213 | - |
|
214 | - return $ctx; |
|
215 | - } |
|
216 | - |
|
217 | - /** |
|
218 | - * @internal You should not use this directly from another application |
|
219 | - * |
|
220 | - * @param SplFixedArray $ctx |
|
221 | - * @param SplFixedArray $buf |
|
222 | - * @return void |
|
223 | - */ |
|
224 | - protected static function compress(SplFixedArray $ctx, SplFixedArray $buf) |
|
225 | - { |
|
226 | - $m = new SplFixedArray(16); |
|
227 | - $v = new SplFixedArray(16); |
|
228 | - |
|
229 | - for ($i = 16; $i--;) { |
|
230 | - $m[$i] = self::load64($buf, $i << 3); |
|
231 | - } |
|
232 | - |
|
233 | - for ($i = 8; $i--;) { |
|
234 | - $v[$i] = $ctx[0][$i]; |
|
235 | - } |
|
236 | - |
|
237 | - $v[ 8] = self::$iv[0]; |
|
238 | - $v[ 9] = self::$iv[1]; |
|
239 | - $v[10] = self::$iv[2]; |
|
240 | - $v[11] = self::$iv[3]; |
|
241 | - |
|
242 | - $v[12] = self::xor64($ctx[1][0], self::$iv[4]); |
|
243 | - $v[13] = self::xor64($ctx[1][1], self::$iv[5]); |
|
244 | - $v[14] = self::xor64($ctx[2][0], self::$iv[6]); |
|
245 | - $v[15] = self::xor64($ctx[2][1], self::$iv[7]); |
|
246 | - |
|
247 | - for ($r = 0; $r < 12; ++$r) { |
|
248 | - $v = self::G($r, 0, 0, 4, 8, 12, $v, $m); |
|
249 | - $v = self::G($r, 1, 1, 5, 9, 13, $v, $m); |
|
250 | - $v = self::G($r, 2, 2, 6, 10, 14, $v, $m); |
|
251 | - $v = self::G($r, 3, 3, 7, 11, 15, $v, $m); |
|
252 | - $v = self::G($r, 4, 0, 5, 10, 15, $v, $m); |
|
253 | - $v = self::G($r, 5, 1, 6, 11, 12, $v, $m); |
|
254 | - $v = self::G($r, 6, 2, 7, 8, 13, $v, $m); |
|
255 | - $v = self::G($r, 7, 3, 4, 9, 14, $v, $m); |
|
256 | - } |
|
257 | - |
|
258 | - for ($i = 8; $i--;) { |
|
259 | - $ctx[0][$i] = self::xor64( |
|
260 | - $ctx[0][$i], self::xor64($v[$i], $v[$i+8]) |
|
261 | - ); |
|
262 | - } |
|
263 | - } |
|
264 | - |
|
265 | - /** |
|
266 | - * @internal You should not use this directly from another application |
|
267 | - * |
|
268 | - * @param int $r |
|
269 | - * @param int $i |
|
270 | - * @param int $a |
|
271 | - * @param int $b |
|
272 | - * @param int $c |
|
273 | - * @param int $d |
|
274 | - * @param SplFixedArray $v |
|
275 | - * @param SplFixedArray $m |
|
276 | - * @return SplFixedArray |
|
277 | - */ |
|
278 | - public static function G($r, $i, $a, $b, $c, $d, SplFixedArray $v, SplFixedArray $m) |
|
279 | - { |
|
280 | - $v[$a] = self::add364($v[$a], $v[$b], $m[self::$sigma[$r][$i << 1]]); |
|
281 | - $v[$d] = self::rotr64(self::xor64($v[$d], $v[$a]), 32); |
|
282 | - $v[$c] = self::add64($v[$c], $v[$d]); |
|
283 | - $v[$b] = self::rotr64(self::xor64($v[$b], $v[$c]), 24); |
|
284 | - $v[$a] = self::add364($v[$a], $v[$b], $m[self::$sigma[$r][($i << 1) + 1]]); |
|
285 | - $v[$d] = self::rotr64(self::xor64($v[$d], $v[$a]), 16); |
|
286 | - $v[$c] = self::add64($v[$c], $v[$d]); |
|
287 | - $v[$b] = self::rotr64(self::xor64($v[$b], $v[$c]), 63); |
|
288 | - return $v; |
|
289 | - } |
|
290 | - |
|
291 | - /** |
|
292 | - * @internal You should not use this directly from another application |
|
293 | - * |
|
294 | - * @param SplFixedArray $ctx |
|
295 | - * @param int $inc |
|
296 | - * @return void |
|
297 | - * @throws Error |
|
298 | - */ |
|
299 | - public static function increment_counter($ctx, $inc) |
|
300 | - { |
|
301 | - if ($inc < 0) { |
|
302 | - throw new Error('Increasing by a negative number makes no sense.'); |
|
303 | - } |
|
304 | - $t = self::to64($inc); |
|
305 | - # S->t is $ctx[1] in our implementation |
|
306 | - |
|
307 | - # S->t[0] = ( uint64_t )( t >> 0 ); |
|
308 | - $ctx[1][0] = self::add64($ctx[1][0], $t); |
|
309 | - |
|
310 | - # S->t[1] += ( S->t[0] < inc ); |
|
311 | - if (!($ctx[1][0] instanceof ParagonIE_Sodium_Core32_Int64)) { |
|
312 | - throw new TypeError('Not an int64'); |
|
313 | - } |
|
314 | - /** @var ParagonIE_Sodium_Core32_Int64 $c*/ |
|
315 | - $c = $ctx[1][0]; |
|
316 | - if ($c->isLessThanInt($inc)) { |
|
317 | - $ctx[1][1] = self::add64($ctx[1][1], self::to64(1)); |
|
318 | - } |
|
319 | - } |
|
320 | - |
|
321 | - /** |
|
322 | - * @internal You should not use this directly from another application |
|
323 | - * |
|
324 | - * @param SplFixedArray $ctx |
|
325 | - * @param SplFixedArray $p |
|
326 | - * @param int $plen |
|
327 | - * @return void |
|
328 | - */ |
|
329 | - public static function update(SplFixedArray $ctx, SplFixedArray $p, $plen) |
|
330 | - { |
|
331 | - self::pseudoConstructor(); |
|
332 | - |
|
333 | - $offset = 0; |
|
334 | - while ($plen > 0) { |
|
335 | - $left = $ctx[4]; |
|
336 | - $fill = 256 - $left; |
|
337 | - |
|
338 | - if ($plen > $fill) { |
|
339 | - # memcpy( S->buf + left, in, fill ); /* Fill buffer */ |
|
340 | - for ($i = $fill; $i--;) { |
|
341 | - $ctx[3][$i + $left] = $p[$i + $offset]; |
|
342 | - } |
|
343 | - |
|
344 | - # S->buflen += fill; |
|
345 | - $ctx[4] += $fill; |
|
346 | - |
|
347 | - # blake2b_increment_counter( S, BLAKE2B_BLOCKBYTES ); |
|
348 | - self::increment_counter($ctx, 128); |
|
349 | - |
|
350 | - # blake2b_compress( S, S->buf ); /* Compress */ |
|
351 | - self::compress($ctx, $ctx[3]); |
|
352 | - |
|
353 | - # memcpy( S->buf, S->buf + BLAKE2B_BLOCKBYTES, BLAKE2B_BLOCKBYTES ); /* Shift buffer left */ |
|
354 | - for ($i = 128; $i--;) { |
|
355 | - $ctx[3][$i] = $ctx[3][$i + 128]; |
|
356 | - } |
|
357 | - |
|
358 | - # S->buflen -= BLAKE2B_BLOCKBYTES; |
|
359 | - $ctx[4] -= 128; |
|
360 | - |
|
361 | - # in += fill; |
|
362 | - $offset += $fill; |
|
363 | - |
|
364 | - # inlen -= fill; |
|
365 | - $plen -= $fill; |
|
366 | - } else { |
|
367 | - for ($i = $plen; $i--;) { |
|
368 | - $ctx[3][$i + $left] = $p[$i + $offset]; |
|
369 | - } |
|
370 | - $ctx[4] += $plen; |
|
371 | - $offset += $plen; |
|
372 | - $plen -= $plen; |
|
373 | - } |
|
374 | - } |
|
375 | - } |
|
376 | - |
|
377 | - /** |
|
378 | - * @internal You should not use this directly from another application |
|
379 | - * |
|
380 | - * @param SplFixedArray $ctx |
|
381 | - * @param SplFixedArray $out |
|
382 | - * @return SplFixedArray |
|
383 | - * @throws Error |
|
384 | - */ |
|
385 | - public static function finish(SplFixedArray $ctx, SplFixedArray $out) |
|
386 | - { |
|
387 | - self::pseudoConstructor(); |
|
388 | - if ($ctx[4] > 128) { |
|
389 | - self::increment_counter($ctx, 128); |
|
390 | - self::compress($ctx, $ctx[3]); |
|
391 | - $ctx[4] -= 128; |
|
392 | - if ($ctx[4] > 128) { |
|
393 | - throw new Error('Failed to assert that buflen <= 128 bytes'); |
|
394 | - } |
|
395 | - for ($i = $ctx[4]; $i--;) { |
|
396 | - $ctx[3][$i] = $ctx[3][$i + 128]; |
|
397 | - } |
|
398 | - } |
|
399 | - |
|
400 | - self::increment_counter($ctx, $ctx[4]); |
|
401 | - $ctx[2][0] = self::new64(0xffffffff, 0xffffffff); |
|
402 | - |
|
403 | - for ($i = 256 - $ctx[4]; $i--;) { |
|
404 | - $ctx[3][$i+$ctx[4]] = 0; |
|
405 | - } |
|
406 | - |
|
407 | - self::compress($ctx, $ctx[3]); |
|
408 | - |
|
409 | - $i = (int) (($out->getSize() - 1) / 8); |
|
410 | - for (; $i >= 0; --$i) { |
|
411 | - self::store64($out, $i << 3, $ctx[0][$i]); |
|
412 | - } |
|
413 | - return $out; |
|
414 | - } |
|
415 | - |
|
416 | - /** |
|
417 | - * @internal You should not use this directly from another application |
|
418 | - * |
|
419 | - * @param SplFixedArray|null $key |
|
420 | - * @param int $outlen |
|
421 | - * @return SplFixedArray |
|
422 | - * @throws Exception |
|
423 | - */ |
|
424 | - public static function init($key = null, $outlen = 64) |
|
425 | - { |
|
426 | - self::pseudoConstructor(); |
|
427 | - $klen = 0; |
|
428 | - |
|
429 | - if ($key !== null) { |
|
430 | - if (count($key) > 64) { |
|
431 | - throw new Exception('Invalid key size'); |
|
432 | - } |
|
433 | - $klen = count($key); |
|
434 | - } |
|
435 | - |
|
436 | - if ($outlen > 64) { |
|
437 | - throw new Exception('Invalid output size'); |
|
438 | - } |
|
439 | - |
|
440 | - $ctx = self::context(); |
|
441 | - |
|
442 | - $p = new SplFixedArray(64); |
|
443 | - for ($i = 64; --$i;) { |
|
444 | - $p[$i] = 0; |
|
445 | - } |
|
446 | - |
|
447 | - $p[0] = $outlen; // digest_length |
|
448 | - $p[1] = $klen; // key_length |
|
449 | - $p[2] = 1; // fanout |
|
450 | - $p[3] = 1; // depth |
|
451 | - |
|
452 | - $ctx[0][0] = self::xor64( |
|
453 | - $ctx[0][0], |
|
454 | - self::load64($p, 0) |
|
455 | - ); |
|
456 | - |
|
457 | - if ($klen > 0 && $key instanceof SplFixedArray) { |
|
458 | - $block = new SplFixedArray(128); |
|
459 | - for ($i = 128; $i--;) { |
|
460 | - $block[$i] = 0; |
|
461 | - } |
|
462 | - for ($i = $klen; $i--;) { |
|
463 | - $block[$i] = $key[$i]; |
|
464 | - } |
|
465 | - self::update($ctx, $block, 128); |
|
466 | - } |
|
467 | - |
|
468 | - return $ctx; |
|
469 | - } |
|
470 | - |
|
471 | - /** |
|
472 | - * Convert a string into an SplFixedArray of integers |
|
473 | - * |
|
474 | - * @internal You should not use this directly from another application |
|
475 | - * |
|
476 | - * @param string $str |
|
477 | - * @return SplFixedArray |
|
478 | - */ |
|
479 | - public static function stringToSplFixedArray($str = '') |
|
480 | - { |
|
481 | - $values = unpack('C*', $str); |
|
482 | - return SplFixedArray::fromArray(array_values($values)); |
|
483 | - } |
|
484 | - |
|
485 | - /** |
|
486 | - * Convert an SplFixedArray of integers into a string |
|
487 | - * |
|
488 | - * @internal You should not use this directly from another application |
|
489 | - * |
|
490 | - * @param SplFixedArray $a |
|
491 | - * @return string |
|
492 | - */ |
|
493 | - public static function SplFixedArrayToString(SplFixedArray $a) |
|
494 | - { |
|
495 | - /** |
|
496 | - * @var array<mixed, int> |
|
497 | - */ |
|
498 | - $arr = $a->toArray(); |
|
499 | - $c = $a->count(); |
|
500 | - array_unshift($arr, str_repeat('C', $c)); |
|
501 | - return call_user_func_array('pack', $arr); |
|
502 | - } |
|
503 | - |
|
504 | - /** |
|
505 | - * @internal You should not use this directly from another application |
|
506 | - * |
|
507 | - * @param SplFixedArray[SplFixedArray] $ctx |
|
508 | - * @return string |
|
509 | - * @throws TypeError |
|
510 | - */ |
|
511 | - public static function contextToString(SplFixedArray $ctx) |
|
512 | - { |
|
513 | - $str = ''; |
|
514 | - $ctxA = $ctx[0]->toArray(); |
|
515 | - |
|
516 | - # uint64_t h[8]; |
|
517 | - for ($i = 0; $i < 8; ++$i) { |
|
518 | - if (!($ctxA[$i] instanceof ParagonIE_Sodium_Core32_Int64)) { |
|
519 | - throw new TypeError('Not an instance of Int64'); |
|
520 | - } |
|
521 | - /** @var ParagonIE_Sodium_Core32_Int64 $ctxAi */ |
|
522 | - $ctxAi = $ctxA[$i]; |
|
523 | - $str .= $ctxAi->toString(); |
|
524 | - } |
|
525 | - |
|
526 | - # uint64_t t[2]; |
|
527 | - # uint64_t f[2]; |
|
528 | - for ($i = 1; $i < 3; ++$i) { |
|
529 | - $ctxA = $ctx[$i]->toArray(); |
|
530 | - /** @var ParagonIE_Sodium_Core32_Int64 $ctxA1 */ |
|
531 | - $ctxA1 = $ctxA[0]; |
|
532 | - /** @var ParagonIE_Sodium_Core32_Int64 $ctxA2 */ |
|
533 | - $ctxA2 = $ctxA[1]; |
|
534 | - |
|
535 | - $str .= $ctxA1->toString(); |
|
536 | - $str .= $ctxA2->toString(); |
|
537 | - } |
|
538 | - |
|
539 | - # uint8_t buf[2 * 128]; |
|
540 | - $str .= self::SplFixedArrayToString($ctx[3]); |
|
541 | - |
|
542 | - # size_t buflen; |
|
543 | - $str .= implode('', array( |
|
544 | - self::intToChr($ctx[4] & 0xff), |
|
545 | - self::intToChr(($ctx[4] >> 8) & 0xff), |
|
546 | - self::intToChr(($ctx[4] >> 16) & 0xff), |
|
547 | - self::intToChr(($ctx[4] >> 24) & 0xff), |
|
548 | - self::intToChr(($ctx[4] >> 32) & 0xff), |
|
549 | - self::intToChr(($ctx[4] >> 40) & 0xff), |
|
550 | - self::intToChr(($ctx[4] >> 48) & 0xff), |
|
551 | - self::intToChr(($ctx[4] >> 56) & 0xff) |
|
552 | - )); |
|
553 | - # uint8_t last_node; |
|
554 | - return $str . "\x00"; |
|
555 | - } |
|
556 | - |
|
557 | - /** |
|
558 | - * Creates an SplFixedArray containing other SplFixedArray elements, from |
|
559 | - * a string (compatible with \Sodium\crypto_generichash_{init, update, final}) |
|
560 | - * |
|
561 | - * @internal You should not use this directly from another application |
|
562 | - * |
|
563 | - * @param string $string |
|
564 | - * @return SplFixedArray |
|
565 | - */ |
|
566 | - public static function stringToContext($string) |
|
567 | - { |
|
568 | - $ctx = self::context(); |
|
569 | - |
|
570 | - # uint64_t h[8]; |
|
571 | - for ($i = 0; $i < 8; ++$i) { |
|
572 | - $ctx[0][$i] = ParagonIE_Sodium_Core32_Int64::fromString( |
|
573 | - self::substr($string, (($i << 3) + 0), 8) |
|
574 | - ); |
|
575 | - } |
|
576 | - |
|
577 | - # uint64_t t[2]; |
|
578 | - # uint64_t f[2]; |
|
579 | - for ($i = 1; $i < 3; ++$i) { |
|
580 | - $ctx[$i][1] = ParagonIE_Sodium_Core32_Int64::fromString( |
|
581 | - self::substr($string, 72 + (($i - 1) << 4), 8) |
|
582 | - ); |
|
583 | - $ctx[$i][0] = ParagonIE_Sodium_Core32_Int64::fromString( |
|
584 | - self::substr($string, 64 + (($i - 1) << 4), 8) |
|
585 | - ); |
|
586 | - } |
|
587 | - |
|
588 | - # uint8_t buf[2 * 128]; |
|
589 | - $ctx[3] = self::stringToSplFixedArray(self::substr($string, 96, 256)); |
|
590 | - |
|
591 | - |
|
592 | - # uint8_t buf[2 * 128]; |
|
593 | - $int = 0; |
|
594 | - for ($i = 0; $i < 8; ++$i) { |
|
595 | - $int |= self::chrToInt($string[352 + $i]) << ($i << 3); |
|
596 | - } |
|
597 | - $ctx[4] = $int; |
|
598 | - |
|
599 | - return $ctx; |
|
600 | - } |
|
14 | + /** |
|
15 | + * @var SplFixedArray |
|
16 | + */ |
|
17 | + public static $iv; |
|
18 | + |
|
19 | + /** |
|
20 | + * @var int[][] |
|
21 | + */ |
|
22 | + public static $sigma = array( |
|
23 | + array( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15), |
|
24 | + array( 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3), |
|
25 | + array( 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4), |
|
26 | + array( 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8), |
|
27 | + array( 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13), |
|
28 | + array( 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9), |
|
29 | + array( 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11), |
|
30 | + array( 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10), |
|
31 | + array( 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5), |
|
32 | + array( 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13 , 0), |
|
33 | + array( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15), |
|
34 | + array( 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3) |
|
35 | + ); |
|
36 | + |
|
37 | + const BLOCKBYTES = 128; |
|
38 | + const OUTBYTES = 64; |
|
39 | + const KEYBYTES = 64; |
|
40 | + |
|
41 | + /** |
|
42 | + * Turn two 32-bit integers into a fixed array representing a 64-bit integer. |
|
43 | + * |
|
44 | + * @internal You should not use this directly from another application |
|
45 | + * |
|
46 | + * @param int $high |
|
47 | + * @param int $low |
|
48 | + * @return ParagonIE_Sodium_Core32_Int64 |
|
49 | + */ |
|
50 | + public static function new64($high, $low) |
|
51 | + { |
|
52 | + return ParagonIE_Sodium_Core32_Int64::fromInts($low, $high); |
|
53 | + } |
|
54 | + |
|
55 | + /** |
|
56 | + * Convert an arbitrary number into an SplFixedArray of two 32-bit integers |
|
57 | + * that represents a 64-bit integer. |
|
58 | + * |
|
59 | + * @internal You should not use this directly from another application |
|
60 | + * |
|
61 | + * @param int $num |
|
62 | + * @return ParagonIE_Sodium_Core32_Int64 |
|
63 | + */ |
|
64 | + protected static function to64($num) |
|
65 | + { |
|
66 | + list($hi, $lo) = self::numericTo64BitInteger($num); |
|
67 | + return self::new64($hi, $lo); |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * Adds two 64-bit integers together, returning their sum as a SplFixedArray |
|
72 | + * containing two 32-bit integers (representing a 64-bit integer). |
|
73 | + * |
|
74 | + * @internal You should not use this directly from another application |
|
75 | + * |
|
76 | + * @param ParagonIE_Sodium_Core32_Int64 $x |
|
77 | + * @param ParagonIE_Sodium_Core32_Int64 $y |
|
78 | + * @return ParagonIE_Sodium_Core32_Int64 |
|
79 | + */ |
|
80 | + protected static function add64($x, $y) |
|
81 | + { |
|
82 | + return $x->addInt64($y); |
|
83 | + } |
|
84 | + |
|
85 | + /** |
|
86 | + * @internal You should not use this directly from another application |
|
87 | + * |
|
88 | + * @param ParagonIE_Sodium_Core32_Int64 $x |
|
89 | + * @param ParagonIE_Sodium_Core32_Int64 $y |
|
90 | + * @param ParagonIE_Sodium_Core32_Int64 $z |
|
91 | + * @return ParagonIE_Sodium_Core32_Int64 |
|
92 | + */ |
|
93 | + public static function add364($x, $y, $z) |
|
94 | + { |
|
95 | + return $x->addInt64($y)->addInt64($z); |
|
96 | + } |
|
97 | + |
|
98 | + /** |
|
99 | + * @internal You should not use this directly from another application |
|
100 | + * |
|
101 | + * @param ParagonIE_Sodium_Core32_Int64 $x |
|
102 | + * @param ParagonIE_Sodium_Core32_Int64 $y |
|
103 | + * @return ParagonIE_Sodium_Core32_Int64 |
|
104 | + * @throws Exception |
|
105 | + */ |
|
106 | + public static function xor64(ParagonIE_Sodium_Core32_Int64 $x, ParagonIE_Sodium_Core32_Int64 $y) |
|
107 | + { |
|
108 | + return $x->xorInt64($y); |
|
109 | + } |
|
110 | + |
|
111 | + /** |
|
112 | + * @internal You should not use this directly from another application |
|
113 | + * |
|
114 | + * @param ParagonIE_Sodium_Core32_Int64 $x |
|
115 | + * @param int $c |
|
116 | + * @return ParagonIE_Sodium_Core32_Int64 |
|
117 | + */ |
|
118 | + public static function rotr64(ParagonIE_Sodium_Core32_Int64 $x, $c) |
|
119 | + { |
|
120 | + return $x->rotateRight($c); |
|
121 | + } |
|
122 | + |
|
123 | + /** |
|
124 | + * @internal You should not use this directly from another application |
|
125 | + * |
|
126 | + * @param SplFixedArray $x |
|
127 | + * @param int $i |
|
128 | + * @return ParagonIE_Sodium_Core32_Int64 |
|
129 | + */ |
|
130 | + public static function load64($x, $i) |
|
131 | + { |
|
132 | + $l = $x[$i] | ($x[$i+1]<<8) | ($x[$i+2]<<16) | ($x[$i+3]<<24); |
|
133 | + $h = $x[$i+4] | ($x[$i+5]<<8) | ($x[$i+6]<<16) | ($x[$i+7]<<24); |
|
134 | + return self::new64($h, $l); |
|
135 | + } |
|
136 | + |
|
137 | + /** |
|
138 | + * @internal You should not use this directly from another application |
|
139 | + * |
|
140 | + * @param SplFixedArray $x |
|
141 | + * @param int $i |
|
142 | + * @param ParagonIE_Sodium_Core32_Int64 $u |
|
143 | + * @return void |
|
144 | + */ |
|
145 | + public static function store64(SplFixedArray $x, $i, ParagonIE_Sodium_Core32_Int64 $u) |
|
146 | + { |
|
147 | + $v = clone $u; |
|
148 | + $maxLength = $x->getSize() - 1; |
|
149 | + for ($j = 0; $j < 8; ++$j) { |
|
150 | + $k = 3 - ($j >> 1); |
|
151 | + $x[$i] = $v->limbs[$k] & 0xff; |
|
152 | + if (++$i > $maxLength) { |
|
153 | + return; |
|
154 | + } |
|
155 | + $v->limbs[$k] >>= 8; |
|
156 | + } |
|
157 | + } |
|
158 | + |
|
159 | + /** |
|
160 | + * This just sets the $iv static variable. |
|
161 | + * |
|
162 | + * @internal You should not use this directly from another application |
|
163 | + * |
|
164 | + * @return void |
|
165 | + */ |
|
166 | + public static function pseudoConstructor() |
|
167 | + { |
|
168 | + static $called = false; |
|
169 | + if ($called) { |
|
170 | + return; |
|
171 | + } |
|
172 | + self::$iv = new SplFixedArray(8); |
|
173 | + self::$iv[0] = self::new64(0x6a09e667, 0xf3bcc908); |
|
174 | + self::$iv[1] = self::new64(0xbb67ae85, 0x84caa73b); |
|
175 | + self::$iv[2] = self::new64(0x3c6ef372, 0xfe94f82b); |
|
176 | + self::$iv[3] = self::new64(0xa54ff53a, 0x5f1d36f1); |
|
177 | + self::$iv[4] = self::new64(0x510e527f, 0xade682d1); |
|
178 | + self::$iv[5] = self::new64(0x9b05688c, 0x2b3e6c1f); |
|
179 | + self::$iv[6] = self::new64(0x1f83d9ab, 0xfb41bd6b); |
|
180 | + self::$iv[7] = self::new64(0x5be0cd19, 0x137e2179); |
|
181 | + |
|
182 | + $called = true; |
|
183 | + } |
|
184 | + |
|
185 | + /** |
|
186 | + * Returns a fresh BLAKE2 context. |
|
187 | + * |
|
188 | + * @internal You should not use this directly from another application |
|
189 | + * |
|
190 | + * @return SplFixedArray |
|
191 | + */ |
|
192 | + protected static function context() |
|
193 | + { |
|
194 | + $ctx = new SplFixedArray(5); |
|
195 | + $ctx[0] = new SplFixedArray(8); // h |
|
196 | + $ctx[1] = new SplFixedArray(2); // t |
|
197 | + $ctx[2] = new SplFixedArray(2); // f |
|
198 | + $ctx[3] = new SplFixedArray(256); // buf |
|
199 | + $ctx[4] = 0; // buflen |
|
200 | + |
|
201 | + for ($i = 8; $i--;) { |
|
202 | + $ctx[0][$i] = self::$iv[$i]; |
|
203 | + } |
|
204 | + for ($i = 256; $i--;) { |
|
205 | + $ctx[3][$i] = 0; |
|
206 | + } |
|
207 | + |
|
208 | + $zero = self::new64(0, 0); |
|
209 | + $ctx[1][0] = $zero; |
|
210 | + $ctx[1][1] = $zero; |
|
211 | + $ctx[2][0] = $zero; |
|
212 | + $ctx[2][1] = $zero; |
|
213 | + |
|
214 | + return $ctx; |
|
215 | + } |
|
216 | + |
|
217 | + /** |
|
218 | + * @internal You should not use this directly from another application |
|
219 | + * |
|
220 | + * @param SplFixedArray $ctx |
|
221 | + * @param SplFixedArray $buf |
|
222 | + * @return void |
|
223 | + */ |
|
224 | + protected static function compress(SplFixedArray $ctx, SplFixedArray $buf) |
|
225 | + { |
|
226 | + $m = new SplFixedArray(16); |
|
227 | + $v = new SplFixedArray(16); |
|
228 | + |
|
229 | + for ($i = 16; $i--;) { |
|
230 | + $m[$i] = self::load64($buf, $i << 3); |
|
231 | + } |
|
232 | + |
|
233 | + for ($i = 8; $i--;) { |
|
234 | + $v[$i] = $ctx[0][$i]; |
|
235 | + } |
|
236 | + |
|
237 | + $v[ 8] = self::$iv[0]; |
|
238 | + $v[ 9] = self::$iv[1]; |
|
239 | + $v[10] = self::$iv[2]; |
|
240 | + $v[11] = self::$iv[3]; |
|
241 | + |
|
242 | + $v[12] = self::xor64($ctx[1][0], self::$iv[4]); |
|
243 | + $v[13] = self::xor64($ctx[1][1], self::$iv[5]); |
|
244 | + $v[14] = self::xor64($ctx[2][0], self::$iv[6]); |
|
245 | + $v[15] = self::xor64($ctx[2][1], self::$iv[7]); |
|
246 | + |
|
247 | + for ($r = 0; $r < 12; ++$r) { |
|
248 | + $v = self::G($r, 0, 0, 4, 8, 12, $v, $m); |
|
249 | + $v = self::G($r, 1, 1, 5, 9, 13, $v, $m); |
|
250 | + $v = self::G($r, 2, 2, 6, 10, 14, $v, $m); |
|
251 | + $v = self::G($r, 3, 3, 7, 11, 15, $v, $m); |
|
252 | + $v = self::G($r, 4, 0, 5, 10, 15, $v, $m); |
|
253 | + $v = self::G($r, 5, 1, 6, 11, 12, $v, $m); |
|
254 | + $v = self::G($r, 6, 2, 7, 8, 13, $v, $m); |
|
255 | + $v = self::G($r, 7, 3, 4, 9, 14, $v, $m); |
|
256 | + } |
|
257 | + |
|
258 | + for ($i = 8; $i--;) { |
|
259 | + $ctx[0][$i] = self::xor64( |
|
260 | + $ctx[0][$i], self::xor64($v[$i], $v[$i+8]) |
|
261 | + ); |
|
262 | + } |
|
263 | + } |
|
264 | + |
|
265 | + /** |
|
266 | + * @internal You should not use this directly from another application |
|
267 | + * |
|
268 | + * @param int $r |
|
269 | + * @param int $i |
|
270 | + * @param int $a |
|
271 | + * @param int $b |
|
272 | + * @param int $c |
|
273 | + * @param int $d |
|
274 | + * @param SplFixedArray $v |
|
275 | + * @param SplFixedArray $m |
|
276 | + * @return SplFixedArray |
|
277 | + */ |
|
278 | + public static function G($r, $i, $a, $b, $c, $d, SplFixedArray $v, SplFixedArray $m) |
|
279 | + { |
|
280 | + $v[$a] = self::add364($v[$a], $v[$b], $m[self::$sigma[$r][$i << 1]]); |
|
281 | + $v[$d] = self::rotr64(self::xor64($v[$d], $v[$a]), 32); |
|
282 | + $v[$c] = self::add64($v[$c], $v[$d]); |
|
283 | + $v[$b] = self::rotr64(self::xor64($v[$b], $v[$c]), 24); |
|
284 | + $v[$a] = self::add364($v[$a], $v[$b], $m[self::$sigma[$r][($i << 1) + 1]]); |
|
285 | + $v[$d] = self::rotr64(self::xor64($v[$d], $v[$a]), 16); |
|
286 | + $v[$c] = self::add64($v[$c], $v[$d]); |
|
287 | + $v[$b] = self::rotr64(self::xor64($v[$b], $v[$c]), 63); |
|
288 | + return $v; |
|
289 | + } |
|
290 | + |
|
291 | + /** |
|
292 | + * @internal You should not use this directly from another application |
|
293 | + * |
|
294 | + * @param SplFixedArray $ctx |
|
295 | + * @param int $inc |
|
296 | + * @return void |
|
297 | + * @throws Error |
|
298 | + */ |
|
299 | + public static function increment_counter($ctx, $inc) |
|
300 | + { |
|
301 | + if ($inc < 0) { |
|
302 | + throw new Error('Increasing by a negative number makes no sense.'); |
|
303 | + } |
|
304 | + $t = self::to64($inc); |
|
305 | + # S->t is $ctx[1] in our implementation |
|
306 | + |
|
307 | + # S->t[0] = ( uint64_t )( t >> 0 ); |
|
308 | + $ctx[1][0] = self::add64($ctx[1][0], $t); |
|
309 | + |
|
310 | + # S->t[1] += ( S->t[0] < inc ); |
|
311 | + if (!($ctx[1][0] instanceof ParagonIE_Sodium_Core32_Int64)) { |
|
312 | + throw new TypeError('Not an int64'); |
|
313 | + } |
|
314 | + /** @var ParagonIE_Sodium_Core32_Int64 $c*/ |
|
315 | + $c = $ctx[1][0]; |
|
316 | + if ($c->isLessThanInt($inc)) { |
|
317 | + $ctx[1][1] = self::add64($ctx[1][1], self::to64(1)); |
|
318 | + } |
|
319 | + } |
|
320 | + |
|
321 | + /** |
|
322 | + * @internal You should not use this directly from another application |
|
323 | + * |
|
324 | + * @param SplFixedArray $ctx |
|
325 | + * @param SplFixedArray $p |
|
326 | + * @param int $plen |
|
327 | + * @return void |
|
328 | + */ |
|
329 | + public static function update(SplFixedArray $ctx, SplFixedArray $p, $plen) |
|
330 | + { |
|
331 | + self::pseudoConstructor(); |
|
332 | + |
|
333 | + $offset = 0; |
|
334 | + while ($plen > 0) { |
|
335 | + $left = $ctx[4]; |
|
336 | + $fill = 256 - $left; |
|
337 | + |
|
338 | + if ($plen > $fill) { |
|
339 | + # memcpy( S->buf + left, in, fill ); /* Fill buffer */ |
|
340 | + for ($i = $fill; $i--;) { |
|
341 | + $ctx[3][$i + $left] = $p[$i + $offset]; |
|
342 | + } |
|
343 | + |
|
344 | + # S->buflen += fill; |
|
345 | + $ctx[4] += $fill; |
|
346 | + |
|
347 | + # blake2b_increment_counter( S, BLAKE2B_BLOCKBYTES ); |
|
348 | + self::increment_counter($ctx, 128); |
|
349 | + |
|
350 | + # blake2b_compress( S, S->buf ); /* Compress */ |
|
351 | + self::compress($ctx, $ctx[3]); |
|
352 | + |
|
353 | + # memcpy( S->buf, S->buf + BLAKE2B_BLOCKBYTES, BLAKE2B_BLOCKBYTES ); /* Shift buffer left */ |
|
354 | + for ($i = 128; $i--;) { |
|
355 | + $ctx[3][$i] = $ctx[3][$i + 128]; |
|
356 | + } |
|
357 | + |
|
358 | + # S->buflen -= BLAKE2B_BLOCKBYTES; |
|
359 | + $ctx[4] -= 128; |
|
360 | + |
|
361 | + # in += fill; |
|
362 | + $offset += $fill; |
|
363 | + |
|
364 | + # inlen -= fill; |
|
365 | + $plen -= $fill; |
|
366 | + } else { |
|
367 | + for ($i = $plen; $i--;) { |
|
368 | + $ctx[3][$i + $left] = $p[$i + $offset]; |
|
369 | + } |
|
370 | + $ctx[4] += $plen; |
|
371 | + $offset += $plen; |
|
372 | + $plen -= $plen; |
|
373 | + } |
|
374 | + } |
|
375 | + } |
|
376 | + |
|
377 | + /** |
|
378 | + * @internal You should not use this directly from another application |
|
379 | + * |
|
380 | + * @param SplFixedArray $ctx |
|
381 | + * @param SplFixedArray $out |
|
382 | + * @return SplFixedArray |
|
383 | + * @throws Error |
|
384 | + */ |
|
385 | + public static function finish(SplFixedArray $ctx, SplFixedArray $out) |
|
386 | + { |
|
387 | + self::pseudoConstructor(); |
|
388 | + if ($ctx[4] > 128) { |
|
389 | + self::increment_counter($ctx, 128); |
|
390 | + self::compress($ctx, $ctx[3]); |
|
391 | + $ctx[4] -= 128; |
|
392 | + if ($ctx[4] > 128) { |
|
393 | + throw new Error('Failed to assert that buflen <= 128 bytes'); |
|
394 | + } |
|
395 | + for ($i = $ctx[4]; $i--;) { |
|
396 | + $ctx[3][$i] = $ctx[3][$i + 128]; |
|
397 | + } |
|
398 | + } |
|
399 | + |
|
400 | + self::increment_counter($ctx, $ctx[4]); |
|
401 | + $ctx[2][0] = self::new64(0xffffffff, 0xffffffff); |
|
402 | + |
|
403 | + for ($i = 256 - $ctx[4]; $i--;) { |
|
404 | + $ctx[3][$i+$ctx[4]] = 0; |
|
405 | + } |
|
406 | + |
|
407 | + self::compress($ctx, $ctx[3]); |
|
408 | + |
|
409 | + $i = (int) (($out->getSize() - 1) / 8); |
|
410 | + for (; $i >= 0; --$i) { |
|
411 | + self::store64($out, $i << 3, $ctx[0][$i]); |
|
412 | + } |
|
413 | + return $out; |
|
414 | + } |
|
415 | + |
|
416 | + /** |
|
417 | + * @internal You should not use this directly from another application |
|
418 | + * |
|
419 | + * @param SplFixedArray|null $key |
|
420 | + * @param int $outlen |
|
421 | + * @return SplFixedArray |
|
422 | + * @throws Exception |
|
423 | + */ |
|
424 | + public static function init($key = null, $outlen = 64) |
|
425 | + { |
|
426 | + self::pseudoConstructor(); |
|
427 | + $klen = 0; |
|
428 | + |
|
429 | + if ($key !== null) { |
|
430 | + if (count($key) > 64) { |
|
431 | + throw new Exception('Invalid key size'); |
|
432 | + } |
|
433 | + $klen = count($key); |
|
434 | + } |
|
435 | + |
|
436 | + if ($outlen > 64) { |
|
437 | + throw new Exception('Invalid output size'); |
|
438 | + } |
|
439 | + |
|
440 | + $ctx = self::context(); |
|
441 | + |
|
442 | + $p = new SplFixedArray(64); |
|
443 | + for ($i = 64; --$i;) { |
|
444 | + $p[$i] = 0; |
|
445 | + } |
|
446 | + |
|
447 | + $p[0] = $outlen; // digest_length |
|
448 | + $p[1] = $klen; // key_length |
|
449 | + $p[2] = 1; // fanout |
|
450 | + $p[3] = 1; // depth |
|
451 | + |
|
452 | + $ctx[0][0] = self::xor64( |
|
453 | + $ctx[0][0], |
|
454 | + self::load64($p, 0) |
|
455 | + ); |
|
456 | + |
|
457 | + if ($klen > 0 && $key instanceof SplFixedArray) { |
|
458 | + $block = new SplFixedArray(128); |
|
459 | + for ($i = 128; $i--;) { |
|
460 | + $block[$i] = 0; |
|
461 | + } |
|
462 | + for ($i = $klen; $i--;) { |
|
463 | + $block[$i] = $key[$i]; |
|
464 | + } |
|
465 | + self::update($ctx, $block, 128); |
|
466 | + } |
|
467 | + |
|
468 | + return $ctx; |
|
469 | + } |
|
470 | + |
|
471 | + /** |
|
472 | + * Convert a string into an SplFixedArray of integers |
|
473 | + * |
|
474 | + * @internal You should not use this directly from another application |
|
475 | + * |
|
476 | + * @param string $str |
|
477 | + * @return SplFixedArray |
|
478 | + */ |
|
479 | + public static function stringToSplFixedArray($str = '') |
|
480 | + { |
|
481 | + $values = unpack('C*', $str); |
|
482 | + return SplFixedArray::fromArray(array_values($values)); |
|
483 | + } |
|
484 | + |
|
485 | + /** |
|
486 | + * Convert an SplFixedArray of integers into a string |
|
487 | + * |
|
488 | + * @internal You should not use this directly from another application |
|
489 | + * |
|
490 | + * @param SplFixedArray $a |
|
491 | + * @return string |
|
492 | + */ |
|
493 | + public static function SplFixedArrayToString(SplFixedArray $a) |
|
494 | + { |
|
495 | + /** |
|
496 | + * @var array<mixed, int> |
|
497 | + */ |
|
498 | + $arr = $a->toArray(); |
|
499 | + $c = $a->count(); |
|
500 | + array_unshift($arr, str_repeat('C', $c)); |
|
501 | + return call_user_func_array('pack', $arr); |
|
502 | + } |
|
503 | + |
|
504 | + /** |
|
505 | + * @internal You should not use this directly from another application |
|
506 | + * |
|
507 | + * @param SplFixedArray[SplFixedArray] $ctx |
|
508 | + * @return string |
|
509 | + * @throws TypeError |
|
510 | + */ |
|
511 | + public static function contextToString(SplFixedArray $ctx) |
|
512 | + { |
|
513 | + $str = ''; |
|
514 | + $ctxA = $ctx[0]->toArray(); |
|
515 | + |
|
516 | + # uint64_t h[8]; |
|
517 | + for ($i = 0; $i < 8; ++$i) { |
|
518 | + if (!($ctxA[$i] instanceof ParagonIE_Sodium_Core32_Int64)) { |
|
519 | + throw new TypeError('Not an instance of Int64'); |
|
520 | + } |
|
521 | + /** @var ParagonIE_Sodium_Core32_Int64 $ctxAi */ |
|
522 | + $ctxAi = $ctxA[$i]; |
|
523 | + $str .= $ctxAi->toString(); |
|
524 | + } |
|
525 | + |
|
526 | + # uint64_t t[2]; |
|
527 | + # uint64_t f[2]; |
|
528 | + for ($i = 1; $i < 3; ++$i) { |
|
529 | + $ctxA = $ctx[$i]->toArray(); |
|
530 | + /** @var ParagonIE_Sodium_Core32_Int64 $ctxA1 */ |
|
531 | + $ctxA1 = $ctxA[0]; |
|
532 | + /** @var ParagonIE_Sodium_Core32_Int64 $ctxA2 */ |
|
533 | + $ctxA2 = $ctxA[1]; |
|
534 | + |
|
535 | + $str .= $ctxA1->toString(); |
|
536 | + $str .= $ctxA2->toString(); |
|
537 | + } |
|
538 | + |
|
539 | + # uint8_t buf[2 * 128]; |
|
540 | + $str .= self::SplFixedArrayToString($ctx[3]); |
|
541 | + |
|
542 | + # size_t buflen; |
|
543 | + $str .= implode('', array( |
|
544 | + self::intToChr($ctx[4] & 0xff), |
|
545 | + self::intToChr(($ctx[4] >> 8) & 0xff), |
|
546 | + self::intToChr(($ctx[4] >> 16) & 0xff), |
|
547 | + self::intToChr(($ctx[4] >> 24) & 0xff), |
|
548 | + self::intToChr(($ctx[4] >> 32) & 0xff), |
|
549 | + self::intToChr(($ctx[4] >> 40) & 0xff), |
|
550 | + self::intToChr(($ctx[4] >> 48) & 0xff), |
|
551 | + self::intToChr(($ctx[4] >> 56) & 0xff) |
|
552 | + )); |
|
553 | + # uint8_t last_node; |
|
554 | + return $str . "\x00"; |
|
555 | + } |
|
556 | + |
|
557 | + /** |
|
558 | + * Creates an SplFixedArray containing other SplFixedArray elements, from |
|
559 | + * a string (compatible with \Sodium\crypto_generichash_{init, update, final}) |
|
560 | + * |
|
561 | + * @internal You should not use this directly from another application |
|
562 | + * |
|
563 | + * @param string $string |
|
564 | + * @return SplFixedArray |
|
565 | + */ |
|
566 | + public static function stringToContext($string) |
|
567 | + { |
|
568 | + $ctx = self::context(); |
|
569 | + |
|
570 | + # uint64_t h[8]; |
|
571 | + for ($i = 0; $i < 8; ++$i) { |
|
572 | + $ctx[0][$i] = ParagonIE_Sodium_Core32_Int64::fromString( |
|
573 | + self::substr($string, (($i << 3) + 0), 8) |
|
574 | + ); |
|
575 | + } |
|
576 | + |
|
577 | + # uint64_t t[2]; |
|
578 | + # uint64_t f[2]; |
|
579 | + for ($i = 1; $i < 3; ++$i) { |
|
580 | + $ctx[$i][1] = ParagonIE_Sodium_Core32_Int64::fromString( |
|
581 | + self::substr($string, 72 + (($i - 1) << 4), 8) |
|
582 | + ); |
|
583 | + $ctx[$i][0] = ParagonIE_Sodium_Core32_Int64::fromString( |
|
584 | + self::substr($string, 64 + (($i - 1) << 4), 8) |
|
585 | + ); |
|
586 | + } |
|
587 | + |
|
588 | + # uint8_t buf[2 * 128]; |
|
589 | + $ctx[3] = self::stringToSplFixedArray(self::substr($string, 96, 256)); |
|
590 | + |
|
591 | + |
|
592 | + # uint8_t buf[2 * 128]; |
|
593 | + $int = 0; |
|
594 | + for ($i = 0; $i < 8; ++$i) { |
|
595 | + $int |= self::chrToInt($string[352 + $i]) << ($i << 3); |
|
596 | + } |
|
597 | + $ctx[4] = $int; |
|
598 | + |
|
599 | + return $ctx; |
|
600 | + } |
|
601 | 601 | } |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -if (class_exists('ParagonIE_Sodium_Core_BLAKE2b', false)) { |
|
3 | +if ( class_exists( 'ParagonIE_Sodium_Core_BLAKE2b', false ) ) { |
|
4 | 4 | return; |
5 | 5 | } |
6 | 6 | |
@@ -20,18 +20,18 @@ discard block |
||
20 | 20 | * @var int[][] |
21 | 21 | */ |
22 | 22 | public static $sigma = array( |
23 | - array( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15), |
|
24 | - array( 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3), |
|
25 | - array( 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4), |
|
26 | - array( 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8), |
|
27 | - array( 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13), |
|
28 | - array( 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9), |
|
29 | - array( 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11), |
|
30 | - array( 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10), |
|
31 | - array( 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5), |
|
32 | - array( 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13 , 0), |
|
33 | - array( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15), |
|
34 | - array( 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3) |
|
23 | + array( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 ), |
|
24 | + array( 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 ), |
|
25 | + array( 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 ), |
|
26 | + array( 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 ), |
|
27 | + array( 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 ), |
|
28 | + array( 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 ), |
|
29 | + array( 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11 ), |
|
30 | + array( 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 ), |
|
31 | + array( 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5 ), |
|
32 | + array( 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0 ), |
|
33 | + array( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 ), |
|
34 | + array( 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 ) |
|
35 | 35 | ); |
36 | 36 | |
37 | 37 | const BLOCKBYTES = 128; |
@@ -47,9 +47,9 @@ discard block |
||
47 | 47 | * @param int $low |
48 | 48 | * @return ParagonIE_Sodium_Core32_Int64 |
49 | 49 | */ |
50 | - public static function new64($high, $low) |
|
50 | + public static function new64( $high, $low ) |
|
51 | 51 | { |
52 | - return ParagonIE_Sodium_Core32_Int64::fromInts($low, $high); |
|
52 | + return ParagonIE_Sodium_Core32_Int64::fromInts( $low, $high ); |
|
53 | 53 | } |
54 | 54 | |
55 | 55 | /** |
@@ -61,10 +61,10 @@ discard block |
||
61 | 61 | * @param int $num |
62 | 62 | * @return ParagonIE_Sodium_Core32_Int64 |
63 | 63 | */ |
64 | - protected static function to64($num) |
|
64 | + protected static function to64( $num ) |
|
65 | 65 | { |
66 | - list($hi, $lo) = self::numericTo64BitInteger($num); |
|
67 | - return self::new64($hi, $lo); |
|
66 | + list( $hi, $lo ) = self::numericTo64BitInteger( $num ); |
|
67 | + return self::new64( $hi, $lo ); |
|
68 | 68 | } |
69 | 69 | |
70 | 70 | /** |
@@ -77,9 +77,9 @@ discard block |
||
77 | 77 | * @param ParagonIE_Sodium_Core32_Int64 $y |
78 | 78 | * @return ParagonIE_Sodium_Core32_Int64 |
79 | 79 | */ |
80 | - protected static function add64($x, $y) |
|
80 | + protected static function add64( $x, $y ) |
|
81 | 81 | { |
82 | - return $x->addInt64($y); |
|
82 | + return $x->addInt64( $y ); |
|
83 | 83 | } |
84 | 84 | |
85 | 85 | /** |
@@ -90,9 +90,9 @@ discard block |
||
90 | 90 | * @param ParagonIE_Sodium_Core32_Int64 $z |
91 | 91 | * @return ParagonIE_Sodium_Core32_Int64 |
92 | 92 | */ |
93 | - public static function add364($x, $y, $z) |
|
93 | + public static function add364( $x, $y, $z ) |
|
94 | 94 | { |
95 | - return $x->addInt64($y)->addInt64($z); |
|
95 | + return $x->addInt64( $y )->addInt64( $z ); |
|
96 | 96 | } |
97 | 97 | |
98 | 98 | /** |
@@ -103,9 +103,9 @@ discard block |
||
103 | 103 | * @return ParagonIE_Sodium_Core32_Int64 |
104 | 104 | * @throws Exception |
105 | 105 | */ |
106 | - public static function xor64(ParagonIE_Sodium_Core32_Int64 $x, ParagonIE_Sodium_Core32_Int64 $y) |
|
106 | + public static function xor64( ParagonIE_Sodium_Core32_Int64 $x, ParagonIE_Sodium_Core32_Int64 $y ) |
|
107 | 107 | { |
108 | - return $x->xorInt64($y); |
|
108 | + return $x->xorInt64( $y ); |
|
109 | 109 | } |
110 | 110 | |
111 | 111 | /** |
@@ -115,9 +115,9 @@ discard block |
||
115 | 115 | * @param int $c |
116 | 116 | * @return ParagonIE_Sodium_Core32_Int64 |
117 | 117 | */ |
118 | - public static function rotr64(ParagonIE_Sodium_Core32_Int64 $x, $c) |
|
118 | + public static function rotr64( ParagonIE_Sodium_Core32_Int64 $x, $c ) |
|
119 | 119 | { |
120 | - return $x->rotateRight($c); |
|
120 | + return $x->rotateRight( $c ); |
|
121 | 121 | } |
122 | 122 | |
123 | 123 | /** |
@@ -127,11 +127,11 @@ discard block |
||
127 | 127 | * @param int $i |
128 | 128 | * @return ParagonIE_Sodium_Core32_Int64 |
129 | 129 | */ |
130 | - public static function load64($x, $i) |
|
130 | + public static function load64( $x, $i ) |
|
131 | 131 | { |
132 | - $l = $x[$i] | ($x[$i+1]<<8) | ($x[$i+2]<<16) | ($x[$i+3]<<24); |
|
133 | - $h = $x[$i+4] | ($x[$i+5]<<8) | ($x[$i+6]<<16) | ($x[$i+7]<<24); |
|
134 | - return self::new64($h, $l); |
|
132 | + $l = $x[$i] | ( $x[$i + 1] << 8 ) | ( $x[$i + 2] << 16 ) | ( $x[$i + 3] << 24 ); |
|
133 | + $h = $x[$i + 4] | ( $x[$i + 5] << 8 ) | ( $x[$i + 6] << 16 ) | ( $x[$i + 7] << 24 ); |
|
134 | + return self::new64( $h, $l ); |
|
135 | 135 | } |
136 | 136 | |
137 | 137 | /** |
@@ -142,12 +142,12 @@ discard block |
||
142 | 142 | * @param ParagonIE_Sodium_Core32_Int64 $u |
143 | 143 | * @return void |
144 | 144 | */ |
145 | - public static function store64(SplFixedArray $x, $i, ParagonIE_Sodium_Core32_Int64 $u) |
|
145 | + public static function store64( SplFixedArray $x, $i, ParagonIE_Sodium_Core32_Int64 $u ) |
|
146 | 146 | { |
147 | 147 | $v = clone $u; |
148 | 148 | $maxLength = $x->getSize() - 1; |
149 | - for ($j = 0; $j < 8; ++$j) { |
|
150 | - $k = 3 - ($j >> 1); |
|
149 | + for ( $j = 0; $j < 8; ++$j ) { |
|
150 | + $k = 3 - ( $j >> 1 ); |
|
151 | 151 | $x[$i] = $v->limbs[$k] & 0xff; |
152 | 152 | if (++$i > $maxLength) { |
153 | 153 | return; |
@@ -166,18 +166,18 @@ discard block |
||
166 | 166 | public static function pseudoConstructor() |
167 | 167 | { |
168 | 168 | static $called = false; |
169 | - if ($called) { |
|
169 | + if ( $called ) { |
|
170 | 170 | return; |
171 | 171 | } |
172 | - self::$iv = new SplFixedArray(8); |
|
173 | - self::$iv[0] = self::new64(0x6a09e667, 0xf3bcc908); |
|
174 | - self::$iv[1] = self::new64(0xbb67ae85, 0x84caa73b); |
|
175 | - self::$iv[2] = self::new64(0x3c6ef372, 0xfe94f82b); |
|
176 | - self::$iv[3] = self::new64(0xa54ff53a, 0x5f1d36f1); |
|
177 | - self::$iv[4] = self::new64(0x510e527f, 0xade682d1); |
|
178 | - self::$iv[5] = self::new64(0x9b05688c, 0x2b3e6c1f); |
|
179 | - self::$iv[6] = self::new64(0x1f83d9ab, 0xfb41bd6b); |
|
180 | - self::$iv[7] = self::new64(0x5be0cd19, 0x137e2179); |
|
172 | + self::$iv = new SplFixedArray( 8 ); |
|
173 | + self::$iv[0] = self::new64( 0x6a09e667, 0xf3bcc908 ); |
|
174 | + self::$iv[1] = self::new64( 0xbb67ae85, 0x84caa73b ); |
|
175 | + self::$iv[2] = self::new64( 0x3c6ef372, 0xfe94f82b ); |
|
176 | + self::$iv[3] = self::new64( 0xa54ff53a, 0x5f1d36f1 ); |
|
177 | + self::$iv[4] = self::new64( 0x510e527f, 0xade682d1 ); |
|
178 | + self::$iv[5] = self::new64( 0x9b05688c, 0x2b3e6c1f ); |
|
179 | + self::$iv[6] = self::new64( 0x1f83d9ab, 0xfb41bd6b ); |
|
180 | + self::$iv[7] = self::new64( 0x5be0cd19, 0x137e2179 ); |
|
181 | 181 | |
182 | 182 | $called = true; |
183 | 183 | } |
@@ -191,21 +191,21 @@ discard block |
||
191 | 191 | */ |
192 | 192 | protected static function context() |
193 | 193 | { |
194 | - $ctx = new SplFixedArray(5); |
|
195 | - $ctx[0] = new SplFixedArray(8); // h |
|
196 | - $ctx[1] = new SplFixedArray(2); // t |
|
197 | - $ctx[2] = new SplFixedArray(2); // f |
|
198 | - $ctx[3] = new SplFixedArray(256); // buf |
|
199 | - $ctx[4] = 0; // buflen |
|
200 | - |
|
201 | - for ($i = 8; $i--;) { |
|
194 | + $ctx = new SplFixedArray( 5 ); |
|
195 | + $ctx[0] = new SplFixedArray( 8 ); // h |
|
196 | + $ctx[1] = new SplFixedArray( 2 ); // t |
|
197 | + $ctx[2] = new SplFixedArray( 2 ); // f |
|
198 | + $ctx[3] = new SplFixedArray( 256 ); // buf |
|
199 | + $ctx[4] = 0; // buflen |
|
200 | + |
|
201 | + for ( $i = 8; $i--; ) { |
|
202 | 202 | $ctx[0][$i] = self::$iv[$i]; |
203 | 203 | } |
204 | - for ($i = 256; $i--;) { |
|
204 | + for ( $i = 256; $i--; ) { |
|
205 | 205 | $ctx[3][$i] = 0; |
206 | 206 | } |
207 | 207 | |
208 | - $zero = self::new64(0, 0); |
|
208 | + $zero = self::new64( 0, 0 ); |
|
209 | 209 | $ctx[1][0] = $zero; |
210 | 210 | $ctx[1][1] = $zero; |
211 | 211 | $ctx[2][0] = $zero; |
@@ -221,43 +221,43 @@ discard block |
||
221 | 221 | * @param SplFixedArray $buf |
222 | 222 | * @return void |
223 | 223 | */ |
224 | - protected static function compress(SplFixedArray $ctx, SplFixedArray $buf) |
|
224 | + protected static function compress( SplFixedArray $ctx, SplFixedArray $buf ) |
|
225 | 225 | { |
226 | - $m = new SplFixedArray(16); |
|
227 | - $v = new SplFixedArray(16); |
|
226 | + $m = new SplFixedArray( 16 ); |
|
227 | + $v = new SplFixedArray( 16 ); |
|
228 | 228 | |
229 | - for ($i = 16; $i--;) { |
|
230 | - $m[$i] = self::load64($buf, $i << 3); |
|
229 | + for ( $i = 16; $i--; ) { |
|
230 | + $m[$i] = self::load64( $buf, $i << 3 ); |
|
231 | 231 | } |
232 | 232 | |
233 | - for ($i = 8; $i--;) { |
|
233 | + for ( $i = 8; $i--; ) { |
|
234 | 234 | $v[$i] = $ctx[0][$i]; |
235 | 235 | } |
236 | 236 | |
237 | - $v[ 8] = self::$iv[0]; |
|
238 | - $v[ 9] = self::$iv[1]; |
|
237 | + $v[8] = self::$iv[0]; |
|
238 | + $v[9] = self::$iv[1]; |
|
239 | 239 | $v[10] = self::$iv[2]; |
240 | 240 | $v[11] = self::$iv[3]; |
241 | 241 | |
242 | - $v[12] = self::xor64($ctx[1][0], self::$iv[4]); |
|
243 | - $v[13] = self::xor64($ctx[1][1], self::$iv[5]); |
|
244 | - $v[14] = self::xor64($ctx[2][0], self::$iv[6]); |
|
245 | - $v[15] = self::xor64($ctx[2][1], self::$iv[7]); |
|
246 | - |
|
247 | - for ($r = 0; $r < 12; ++$r) { |
|
248 | - $v = self::G($r, 0, 0, 4, 8, 12, $v, $m); |
|
249 | - $v = self::G($r, 1, 1, 5, 9, 13, $v, $m); |
|
250 | - $v = self::G($r, 2, 2, 6, 10, 14, $v, $m); |
|
251 | - $v = self::G($r, 3, 3, 7, 11, 15, $v, $m); |
|
252 | - $v = self::G($r, 4, 0, 5, 10, 15, $v, $m); |
|
253 | - $v = self::G($r, 5, 1, 6, 11, 12, $v, $m); |
|
254 | - $v = self::G($r, 6, 2, 7, 8, 13, $v, $m); |
|
255 | - $v = self::G($r, 7, 3, 4, 9, 14, $v, $m); |
|
242 | + $v[12] = self::xor64( $ctx[1][0], self::$iv[4] ); |
|
243 | + $v[13] = self::xor64( $ctx[1][1], self::$iv[5] ); |
|
244 | + $v[14] = self::xor64( $ctx[2][0], self::$iv[6] ); |
|
245 | + $v[15] = self::xor64( $ctx[2][1], self::$iv[7] ); |
|
246 | + |
|
247 | + for ( $r = 0; $r < 12; ++$r ) { |
|
248 | + $v = self::G( $r, 0, 0, 4, 8, 12, $v, $m ); |
|
249 | + $v = self::G( $r, 1, 1, 5, 9, 13, $v, $m ); |
|
250 | + $v = self::G( $r, 2, 2, 6, 10, 14, $v, $m ); |
|
251 | + $v = self::G( $r, 3, 3, 7, 11, 15, $v, $m ); |
|
252 | + $v = self::G( $r, 4, 0, 5, 10, 15, $v, $m ); |
|
253 | + $v = self::G( $r, 5, 1, 6, 11, 12, $v, $m ); |
|
254 | + $v = self::G( $r, 6, 2, 7, 8, 13, $v, $m ); |
|
255 | + $v = self::G( $r, 7, 3, 4, 9, 14, $v, $m ); |
|
256 | 256 | } |
257 | 257 | |
258 | - for ($i = 8; $i--;) { |
|
258 | + for ( $i = 8; $i--; ) { |
|
259 | 259 | $ctx[0][$i] = self::xor64( |
260 | - $ctx[0][$i], self::xor64($v[$i], $v[$i+8]) |
|
260 | + $ctx[0][$i], self::xor64( $v[$i], $v[$i + 8] ) |
|
261 | 261 | ); |
262 | 262 | } |
263 | 263 | } |
@@ -275,16 +275,16 @@ discard block |
||
275 | 275 | * @param SplFixedArray $m |
276 | 276 | * @return SplFixedArray |
277 | 277 | */ |
278 | - public static function G($r, $i, $a, $b, $c, $d, SplFixedArray $v, SplFixedArray $m) |
|
278 | + public static function G( $r, $i, $a, $b, $c, $d, SplFixedArray $v, SplFixedArray $m ) |
|
279 | 279 | { |
280 | - $v[$a] = self::add364($v[$a], $v[$b], $m[self::$sigma[$r][$i << 1]]); |
|
281 | - $v[$d] = self::rotr64(self::xor64($v[$d], $v[$a]), 32); |
|
282 | - $v[$c] = self::add64($v[$c], $v[$d]); |
|
283 | - $v[$b] = self::rotr64(self::xor64($v[$b], $v[$c]), 24); |
|
284 | - $v[$a] = self::add364($v[$a], $v[$b], $m[self::$sigma[$r][($i << 1) + 1]]); |
|
285 | - $v[$d] = self::rotr64(self::xor64($v[$d], $v[$a]), 16); |
|
286 | - $v[$c] = self::add64($v[$c], $v[$d]); |
|
287 | - $v[$b] = self::rotr64(self::xor64($v[$b], $v[$c]), 63); |
|
280 | + $v[$a] = self::add364( $v[$a], $v[$b], $m[self::$sigma[$r][$i << 1]] ); |
|
281 | + $v[$d] = self::rotr64( self::xor64( $v[$d], $v[$a] ), 32 ); |
|
282 | + $v[$c] = self::add64( $v[$c], $v[$d] ); |
|
283 | + $v[$b] = self::rotr64( self::xor64( $v[$b], $v[$c] ), 24 ); |
|
284 | + $v[$a] = self::add364( $v[$a], $v[$b], $m[self::$sigma[$r][( $i << 1 ) + 1]] ); |
|
285 | + $v[$d] = self::rotr64( self::xor64( $v[$d], $v[$a] ), 16 ); |
|
286 | + $v[$c] = self::add64( $v[$c], $v[$d] ); |
|
287 | + $v[$b] = self::rotr64( self::xor64( $v[$b], $v[$c] ), 63 ); |
|
288 | 288 | return $v; |
289 | 289 | } |
290 | 290 | |
@@ -296,25 +296,25 @@ discard block |
||
296 | 296 | * @return void |
297 | 297 | * @throws Error |
298 | 298 | */ |
299 | - public static function increment_counter($ctx, $inc) |
|
299 | + public static function increment_counter( $ctx, $inc ) |
|
300 | 300 | { |
301 | - if ($inc < 0) { |
|
302 | - throw new Error('Increasing by a negative number makes no sense.'); |
|
301 | + if ( $inc < 0 ) { |
|
302 | + throw new Error( 'Increasing by a negative number makes no sense.' ); |
|
303 | 303 | } |
304 | - $t = self::to64($inc); |
|
304 | + $t = self::to64( $inc ); |
|
305 | 305 | # S->t is $ctx[1] in our implementation |
306 | 306 | |
307 | 307 | # S->t[0] = ( uint64_t )( t >> 0 ); |
308 | - $ctx[1][0] = self::add64($ctx[1][0], $t); |
|
308 | + $ctx[1][0] = self::add64( $ctx[1][0], $t ); |
|
309 | 309 | |
310 | 310 | # S->t[1] += ( S->t[0] < inc ); |
311 | - if (!($ctx[1][0] instanceof ParagonIE_Sodium_Core32_Int64)) { |
|
312 | - throw new TypeError('Not an int64'); |
|
311 | + if ( ! ( $ctx[1][0] instanceof ParagonIE_Sodium_Core32_Int64 ) ) { |
|
312 | + throw new TypeError( 'Not an int64' ); |
|
313 | 313 | } |
314 | 314 | /** @var ParagonIE_Sodium_Core32_Int64 $c*/ |
315 | 315 | $c = $ctx[1][0]; |
316 | - if ($c->isLessThanInt($inc)) { |
|
317 | - $ctx[1][1] = self::add64($ctx[1][1], self::to64(1)); |
|
316 | + if ( $c->isLessThanInt( $inc ) ) { |
|
317 | + $ctx[1][1] = self::add64( $ctx[1][1], self::to64( 1 ) ); |
|
318 | 318 | } |
319 | 319 | } |
320 | 320 | |
@@ -326,18 +326,18 @@ discard block |
||
326 | 326 | * @param int $plen |
327 | 327 | * @return void |
328 | 328 | */ |
329 | - public static function update(SplFixedArray $ctx, SplFixedArray $p, $plen) |
|
329 | + public static function update( SplFixedArray $ctx, SplFixedArray $p, $plen ) |
|
330 | 330 | { |
331 | 331 | self::pseudoConstructor(); |
332 | 332 | |
333 | 333 | $offset = 0; |
334 | - while ($plen > 0) { |
|
334 | + while ( $plen > 0 ) { |
|
335 | 335 | $left = $ctx[4]; |
336 | 336 | $fill = 256 - $left; |
337 | 337 | |
338 | - if ($plen > $fill) { |
|
338 | + if ( $plen > $fill ) { |
|
339 | 339 | # memcpy( S->buf + left, in, fill ); /* Fill buffer */ |
340 | - for ($i = $fill; $i--;) { |
|
340 | + for ( $i = $fill; $i--; ) { |
|
341 | 341 | $ctx[3][$i + $left] = $p[$i + $offset]; |
342 | 342 | } |
343 | 343 | |
@@ -345,13 +345,13 @@ discard block |
||
345 | 345 | $ctx[4] += $fill; |
346 | 346 | |
347 | 347 | # blake2b_increment_counter( S, BLAKE2B_BLOCKBYTES ); |
348 | - self::increment_counter($ctx, 128); |
|
348 | + self::increment_counter( $ctx, 128 ); |
|
349 | 349 | |
350 | 350 | # blake2b_compress( S, S->buf ); /* Compress */ |
351 | - self::compress($ctx, $ctx[3]); |
|
351 | + self::compress( $ctx, $ctx[3] ); |
|
352 | 352 | |
353 | 353 | # memcpy( S->buf, S->buf + BLAKE2B_BLOCKBYTES, BLAKE2B_BLOCKBYTES ); /* Shift buffer left */ |
354 | - for ($i = 128; $i--;) { |
|
354 | + for ( $i = 128; $i--; ) { |
|
355 | 355 | $ctx[3][$i] = $ctx[3][$i + 128]; |
356 | 356 | } |
357 | 357 | |
@@ -364,7 +364,7 @@ discard block |
||
364 | 364 | # inlen -= fill; |
365 | 365 | $plen -= $fill; |
366 | 366 | } else { |
367 | - for ($i = $plen; $i--;) { |
|
367 | + for ( $i = $plen; $i--; ) { |
|
368 | 368 | $ctx[3][$i + $left] = $p[$i + $offset]; |
369 | 369 | } |
370 | 370 | $ctx[4] += $plen; |
@@ -382,33 +382,33 @@ discard block |
||
382 | 382 | * @return SplFixedArray |
383 | 383 | * @throws Error |
384 | 384 | */ |
385 | - public static function finish(SplFixedArray $ctx, SplFixedArray $out) |
|
385 | + public static function finish( SplFixedArray $ctx, SplFixedArray $out ) |
|
386 | 386 | { |
387 | 387 | self::pseudoConstructor(); |
388 | - if ($ctx[4] > 128) { |
|
389 | - self::increment_counter($ctx, 128); |
|
390 | - self::compress($ctx, $ctx[3]); |
|
388 | + if ( $ctx[4] > 128 ) { |
|
389 | + self::increment_counter( $ctx, 128 ); |
|
390 | + self::compress( $ctx, $ctx[3] ); |
|
391 | 391 | $ctx[4] -= 128; |
392 | - if ($ctx[4] > 128) { |
|
393 | - throw new Error('Failed to assert that buflen <= 128 bytes'); |
|
392 | + if ( $ctx[4] > 128 ) { |
|
393 | + throw new Error( 'Failed to assert that buflen <= 128 bytes' ); |
|
394 | 394 | } |
395 | - for ($i = $ctx[4]; $i--;) { |
|
395 | + for ( $i = $ctx[4]; $i--; ) { |
|
396 | 396 | $ctx[3][$i] = $ctx[3][$i + 128]; |
397 | 397 | } |
398 | 398 | } |
399 | 399 | |
400 | - self::increment_counter($ctx, $ctx[4]); |
|
401 | - $ctx[2][0] = self::new64(0xffffffff, 0xffffffff); |
|
400 | + self::increment_counter( $ctx, $ctx[4] ); |
|
401 | + $ctx[2][0] = self::new64( 0xffffffff, 0xffffffff ); |
|
402 | 402 | |
403 | - for ($i = 256 - $ctx[4]; $i--;) { |
|
404 | - $ctx[3][$i+$ctx[4]] = 0; |
|
403 | + for ( $i = 256 - $ctx[4]; $i--; ) { |
|
404 | + $ctx[3][$i + $ctx[4]] = 0; |
|
405 | 405 | } |
406 | 406 | |
407 | - self::compress($ctx, $ctx[3]); |
|
407 | + self::compress( $ctx, $ctx[3] ); |
|
408 | 408 | |
409 | - $i = (int) (($out->getSize() - 1) / 8); |
|
410 | - for (; $i >= 0; --$i) { |
|
411 | - self::store64($out, $i << 3, $ctx[0][$i]); |
|
409 | + $i = (int) ( ( $out->getSize() - 1 ) / 8 ); |
|
410 | + for ( ; $i >= 0; --$i ) { |
|
411 | + self::store64( $out, $i << 3, $ctx[0][$i] ); |
|
412 | 412 | } |
413 | 413 | return $out; |
414 | 414 | } |
@@ -421,48 +421,48 @@ discard block |
||
421 | 421 | * @return SplFixedArray |
422 | 422 | * @throws Exception |
423 | 423 | */ |
424 | - public static function init($key = null, $outlen = 64) |
|
424 | + public static function init( $key = null, $outlen = 64 ) |
|
425 | 425 | { |
426 | 426 | self::pseudoConstructor(); |
427 | 427 | $klen = 0; |
428 | 428 | |
429 | - if ($key !== null) { |
|
430 | - if (count($key) > 64) { |
|
431 | - throw new Exception('Invalid key size'); |
|
429 | + if ( $key !== null ) { |
|
430 | + if ( count( $key ) > 64 ) { |
|
431 | + throw new Exception( 'Invalid key size' ); |
|
432 | 432 | } |
433 | - $klen = count($key); |
|
433 | + $klen = count( $key ); |
|
434 | 434 | } |
435 | 435 | |
436 | - if ($outlen > 64) { |
|
437 | - throw new Exception('Invalid output size'); |
|
436 | + if ( $outlen > 64 ) { |
|
437 | + throw new Exception( 'Invalid output size' ); |
|
438 | 438 | } |
439 | 439 | |
440 | 440 | $ctx = self::context(); |
441 | 441 | |
442 | - $p = new SplFixedArray(64); |
|
443 | - for ($i = 64; --$i;) { |
|
442 | + $p = new SplFixedArray( 64 ); |
|
443 | + for ( $i = 64; --$i; ) { |
|
444 | 444 | $p[$i] = 0; |
445 | 445 | } |
446 | 446 | |
447 | 447 | $p[0] = $outlen; // digest_length |
448 | - $p[1] = $klen; // key_length |
|
449 | - $p[2] = 1; // fanout |
|
450 | - $p[3] = 1; // depth |
|
448 | + $p[1] = $klen; // key_length |
|
449 | + $p[2] = 1; // fanout |
|
450 | + $p[3] = 1; // depth |
|
451 | 451 | |
452 | 452 | $ctx[0][0] = self::xor64( |
453 | 453 | $ctx[0][0], |
454 | - self::load64($p, 0) |
|
454 | + self::load64( $p, 0 ) |
|
455 | 455 | ); |
456 | 456 | |
457 | - if ($klen > 0 && $key instanceof SplFixedArray) { |
|
458 | - $block = new SplFixedArray(128); |
|
459 | - for ($i = 128; $i--;) { |
|
457 | + if ( $klen > 0 && $key instanceof SplFixedArray ) { |
|
458 | + $block = new SplFixedArray( 128 ); |
|
459 | + for ( $i = 128; $i--; ) { |
|
460 | 460 | $block[$i] = 0; |
461 | 461 | } |
462 | - for ($i = $klen; $i--;) { |
|
462 | + for ( $i = $klen; $i--; ) { |
|
463 | 463 | $block[$i] = $key[$i]; |
464 | 464 | } |
465 | - self::update($ctx, $block, 128); |
|
465 | + self::update( $ctx, $block, 128 ); |
|
466 | 466 | } |
467 | 467 | |
468 | 468 | return $ctx; |
@@ -476,10 +476,10 @@ discard block |
||
476 | 476 | * @param string $str |
477 | 477 | * @return SplFixedArray |
478 | 478 | */ |
479 | - public static function stringToSplFixedArray($str = '') |
|
479 | + public static function stringToSplFixedArray( $str = '' ) |
|
480 | 480 | { |
481 | - $values = unpack('C*', $str); |
|
482 | - return SplFixedArray::fromArray(array_values($values)); |
|
481 | + $values = unpack( 'C*', $str ); |
|
482 | + return SplFixedArray::fromArray( array_values( $values ) ); |
|
483 | 483 | } |
484 | 484 | |
485 | 485 | /** |
@@ -490,15 +490,15 @@ discard block |
||
490 | 490 | * @param SplFixedArray $a |
491 | 491 | * @return string |
492 | 492 | */ |
493 | - public static function SplFixedArrayToString(SplFixedArray $a) |
|
493 | + public static function SplFixedArrayToString( SplFixedArray $a ) |
|
494 | 494 | { |
495 | 495 | /** |
496 | 496 | * @var array<mixed, int> |
497 | 497 | */ |
498 | 498 | $arr = $a->toArray(); |
499 | 499 | $c = $a->count(); |
500 | - array_unshift($arr, str_repeat('C', $c)); |
|
501 | - return call_user_func_array('pack', $arr); |
|
500 | + array_unshift( $arr, str_repeat( 'C', $c ) ); |
|
501 | + return call_user_func_array( 'pack', $arr ); |
|
502 | 502 | } |
503 | 503 | |
504 | 504 | /** |
@@ -508,15 +508,15 @@ discard block |
||
508 | 508 | * @return string |
509 | 509 | * @throws TypeError |
510 | 510 | */ |
511 | - public static function contextToString(SplFixedArray $ctx) |
|
511 | + public static function contextToString( SplFixedArray $ctx ) |
|
512 | 512 | { |
513 | 513 | $str = ''; |
514 | 514 | $ctxA = $ctx[0]->toArray(); |
515 | 515 | |
516 | 516 | # uint64_t h[8]; |
517 | - for ($i = 0; $i < 8; ++$i) { |
|
518 | - if (!($ctxA[$i] instanceof ParagonIE_Sodium_Core32_Int64)) { |
|
519 | - throw new TypeError('Not an instance of Int64'); |
|
517 | + for ( $i = 0; $i < 8; ++$i ) { |
|
518 | + if ( ! ( $ctxA[$i] instanceof ParagonIE_Sodium_Core32_Int64 ) ) { |
|
519 | + throw new TypeError( 'Not an instance of Int64' ); |
|
520 | 520 | } |
521 | 521 | /** @var ParagonIE_Sodium_Core32_Int64 $ctxAi */ |
522 | 522 | $ctxAi = $ctxA[$i]; |
@@ -525,7 +525,7 @@ discard block |
||
525 | 525 | |
526 | 526 | # uint64_t t[2]; |
527 | 527 | # uint64_t f[2]; |
528 | - for ($i = 1; $i < 3; ++$i) { |
|
528 | + for ( $i = 1; $i < 3; ++$i ) { |
|
529 | 529 | $ctxA = $ctx[$i]->toArray(); |
530 | 530 | /** @var ParagonIE_Sodium_Core32_Int64 $ctxA1 */ |
531 | 531 | $ctxA1 = $ctxA[0]; |
@@ -537,19 +537,19 @@ discard block |
||
537 | 537 | } |
538 | 538 | |
539 | 539 | # uint8_t buf[2 * 128]; |
540 | - $str .= self::SplFixedArrayToString($ctx[3]); |
|
540 | + $str .= self::SplFixedArrayToString( $ctx[3] ); |
|
541 | 541 | |
542 | 542 | # size_t buflen; |
543 | - $str .= implode('', array( |
|
544 | - self::intToChr($ctx[4] & 0xff), |
|
545 | - self::intToChr(($ctx[4] >> 8) & 0xff), |
|
546 | - self::intToChr(($ctx[4] >> 16) & 0xff), |
|
547 | - self::intToChr(($ctx[4] >> 24) & 0xff), |
|
548 | - self::intToChr(($ctx[4] >> 32) & 0xff), |
|
549 | - self::intToChr(($ctx[4] >> 40) & 0xff), |
|
550 | - self::intToChr(($ctx[4] >> 48) & 0xff), |
|
551 | - self::intToChr(($ctx[4] >> 56) & 0xff) |
|
552 | - )); |
|
543 | + $str .= implode( '', array( |
|
544 | + self::intToChr( $ctx[4] & 0xff ), |
|
545 | + self::intToChr( ( $ctx[4] >> 8 ) & 0xff ), |
|
546 | + self::intToChr( ( $ctx[4] >> 16 ) & 0xff ), |
|
547 | + self::intToChr( ( $ctx[4] >> 24 ) & 0xff ), |
|
548 | + self::intToChr( ( $ctx[4] >> 32 ) & 0xff ), |
|
549 | + self::intToChr( ( $ctx[4] >> 40 ) & 0xff ), |
|
550 | + self::intToChr( ( $ctx[4] >> 48 ) & 0xff ), |
|
551 | + self::intToChr( ( $ctx[4] >> 56 ) & 0xff ) |
|
552 | + ) ); |
|
553 | 553 | # uint8_t last_node; |
554 | 554 | return $str . "\x00"; |
555 | 555 | } |
@@ -563,36 +563,36 @@ discard block |
||
563 | 563 | * @param string $string |
564 | 564 | * @return SplFixedArray |
565 | 565 | */ |
566 | - public static function stringToContext($string) |
|
566 | + public static function stringToContext( $string ) |
|
567 | 567 | { |
568 | 568 | $ctx = self::context(); |
569 | 569 | |
570 | 570 | # uint64_t h[8]; |
571 | - for ($i = 0; $i < 8; ++$i) { |
|
571 | + for ( $i = 0; $i < 8; ++$i ) { |
|
572 | 572 | $ctx[0][$i] = ParagonIE_Sodium_Core32_Int64::fromString( |
573 | - self::substr($string, (($i << 3) + 0), 8) |
|
573 | + self::substr( $string, ( ( $i << 3 ) + 0 ), 8 ) |
|
574 | 574 | ); |
575 | 575 | } |
576 | 576 | |
577 | 577 | # uint64_t t[2]; |
578 | 578 | # uint64_t f[2]; |
579 | - for ($i = 1; $i < 3; ++$i) { |
|
579 | + for ( $i = 1; $i < 3; ++$i ) { |
|
580 | 580 | $ctx[$i][1] = ParagonIE_Sodium_Core32_Int64::fromString( |
581 | - self::substr($string, 72 + (($i - 1) << 4), 8) |
|
581 | + self::substr( $string, 72 + ( ( $i - 1 ) << 4 ), 8 ) |
|
582 | 582 | ); |
583 | 583 | $ctx[$i][0] = ParagonIE_Sodium_Core32_Int64::fromString( |
584 | - self::substr($string, 64 + (($i - 1) << 4), 8) |
|
584 | + self::substr( $string, 64 + ( ( $i - 1 ) << 4 ), 8 ) |
|
585 | 585 | ); |
586 | 586 | } |
587 | 587 | |
588 | 588 | # uint8_t buf[2 * 128]; |
589 | - $ctx[3] = self::stringToSplFixedArray(self::substr($string, 96, 256)); |
|
589 | + $ctx[3] = self::stringToSplFixedArray( self::substr( $string, 96, 256 ) ); |
|
590 | 590 | |
591 | 591 | |
592 | 592 | # uint8_t buf[2 * 128]; |
593 | 593 | $int = 0; |
594 | - for ($i = 0; $i < 8; ++$i) { |
|
595 | - $int |= self::chrToInt($string[352 + $i]) << ($i << 3); |
|
594 | + for ( $i = 0; $i < 8; ++$i ) { |
|
595 | + $int |= self::chrToInt( $string[352 + $i] ) << ( $i << 3 ); |
|
596 | 596 | } |
597 | 597 | $ctx[4] = $int; |
598 | 598 |
@@ -9,8 +9,7 @@ discard block |
||
9 | 9 | * |
10 | 10 | * Based on the work of Devi Mandiri in devi/salt. |
11 | 11 | */ |
12 | -abstract class ParagonIE_Sodium_Core32_BLAKE2b extends ParagonIE_Sodium_Core_Util |
|
13 | -{ |
|
12 | +abstract class ParagonIE_Sodium_Core32_BLAKE2b extends ParagonIE_Sodium_Core_Util { |
|
14 | 13 | /** |
15 | 14 | * @var SplFixedArray |
16 | 15 | */ |
@@ -47,8 +46,7 @@ discard block |
||
47 | 46 | * @param int $low |
48 | 47 | * @return ParagonIE_Sodium_Core32_Int64 |
49 | 48 | */ |
50 | - public static function new64($high, $low) |
|
51 | - { |
|
49 | + public static function new64($high, $low) { |
|
52 | 50 | return ParagonIE_Sodium_Core32_Int64::fromInts($low, $high); |
53 | 51 | } |
54 | 52 | |
@@ -61,8 +59,7 @@ discard block |
||
61 | 59 | * @param int $num |
62 | 60 | * @return ParagonIE_Sodium_Core32_Int64 |
63 | 61 | */ |
64 | - protected static function to64($num) |
|
65 | - { |
|
62 | + protected static function to64($num) { |
|
66 | 63 | list($hi, $lo) = self::numericTo64BitInteger($num); |
67 | 64 | return self::new64($hi, $lo); |
68 | 65 | } |
@@ -77,8 +74,7 @@ discard block |
||
77 | 74 | * @param ParagonIE_Sodium_Core32_Int64 $y |
78 | 75 | * @return ParagonIE_Sodium_Core32_Int64 |
79 | 76 | */ |
80 | - protected static function add64($x, $y) |
|
81 | - { |
|
77 | + protected static function add64($x, $y) { |
|
82 | 78 | return $x->addInt64($y); |
83 | 79 | } |
84 | 80 | |
@@ -90,8 +86,7 @@ discard block |
||
90 | 86 | * @param ParagonIE_Sodium_Core32_Int64 $z |
91 | 87 | * @return ParagonIE_Sodium_Core32_Int64 |
92 | 88 | */ |
93 | - public static function add364($x, $y, $z) |
|
94 | - { |
|
89 | + public static function add364($x, $y, $z) { |
|
95 | 90 | return $x->addInt64($y)->addInt64($z); |
96 | 91 | } |
97 | 92 | |
@@ -103,8 +98,7 @@ discard block |
||
103 | 98 | * @return ParagonIE_Sodium_Core32_Int64 |
104 | 99 | * @throws Exception |
105 | 100 | */ |
106 | - public static function xor64(ParagonIE_Sodium_Core32_Int64 $x, ParagonIE_Sodium_Core32_Int64 $y) |
|
107 | - { |
|
101 | + public static function xor64(ParagonIE_Sodium_Core32_Int64 $x, ParagonIE_Sodium_Core32_Int64 $y) { |
|
108 | 102 | return $x->xorInt64($y); |
109 | 103 | } |
110 | 104 | |
@@ -115,8 +109,7 @@ discard block |
||
115 | 109 | * @param int $c |
116 | 110 | * @return ParagonIE_Sodium_Core32_Int64 |
117 | 111 | */ |
118 | - public static function rotr64(ParagonIE_Sodium_Core32_Int64 $x, $c) |
|
119 | - { |
|
112 | + public static function rotr64(ParagonIE_Sodium_Core32_Int64 $x, $c) { |
|
120 | 113 | return $x->rotateRight($c); |
121 | 114 | } |
122 | 115 | |
@@ -127,8 +120,7 @@ discard block |
||
127 | 120 | * @param int $i |
128 | 121 | * @return ParagonIE_Sodium_Core32_Int64 |
129 | 122 | */ |
130 | - public static function load64($x, $i) |
|
131 | - { |
|
123 | + public static function load64($x, $i) { |
|
132 | 124 | $l = $x[$i] | ($x[$i+1]<<8) | ($x[$i+2]<<16) | ($x[$i+3]<<24); |
133 | 125 | $h = $x[$i+4] | ($x[$i+5]<<8) | ($x[$i+6]<<16) | ($x[$i+7]<<24); |
134 | 126 | return self::new64($h, $l); |
@@ -142,8 +134,7 @@ discard block |
||
142 | 134 | * @param ParagonIE_Sodium_Core32_Int64 $u |
143 | 135 | * @return void |
144 | 136 | */ |
145 | - public static function store64(SplFixedArray $x, $i, ParagonIE_Sodium_Core32_Int64 $u) |
|
146 | - { |
|
137 | + public static function store64(SplFixedArray $x, $i, ParagonIE_Sodium_Core32_Int64 $u) { |
|
147 | 138 | $v = clone $u; |
148 | 139 | $maxLength = $x->getSize() - 1; |
149 | 140 | for ($j = 0; $j < 8; ++$j) { |
@@ -163,8 +154,7 @@ discard block |
||
163 | 154 | * |
164 | 155 | * @return void |
165 | 156 | */ |
166 | - public static function pseudoConstructor() |
|
167 | - { |
|
157 | + public static function pseudoConstructor() { |
|
168 | 158 | static $called = false; |
169 | 159 | if ($called) { |
170 | 160 | return; |
@@ -189,8 +179,7 @@ discard block |
||
189 | 179 | * |
190 | 180 | * @return SplFixedArray |
191 | 181 | */ |
192 | - protected static function context() |
|
193 | - { |
|
182 | + protected static function context() { |
|
194 | 183 | $ctx = new SplFixedArray(5); |
195 | 184 | $ctx[0] = new SplFixedArray(8); // h |
196 | 185 | $ctx[1] = new SplFixedArray(2); // t |
@@ -221,8 +210,7 @@ discard block |
||
221 | 210 | * @param SplFixedArray $buf |
222 | 211 | * @return void |
223 | 212 | */ |
224 | - protected static function compress(SplFixedArray $ctx, SplFixedArray $buf) |
|
225 | - { |
|
213 | + protected static function compress(SplFixedArray $ctx, SplFixedArray $buf) { |
|
226 | 214 | $m = new SplFixedArray(16); |
227 | 215 | $v = new SplFixedArray(16); |
228 | 216 | |
@@ -275,8 +263,7 @@ discard block |
||
275 | 263 | * @param SplFixedArray $m |
276 | 264 | * @return SplFixedArray |
277 | 265 | */ |
278 | - public static function G($r, $i, $a, $b, $c, $d, SplFixedArray $v, SplFixedArray $m) |
|
279 | - { |
|
266 | + public static function G($r, $i, $a, $b, $c, $d, SplFixedArray $v, SplFixedArray $m) { |
|
280 | 267 | $v[$a] = self::add364($v[$a], $v[$b], $m[self::$sigma[$r][$i << 1]]); |
281 | 268 | $v[$d] = self::rotr64(self::xor64($v[$d], $v[$a]), 32); |
282 | 269 | $v[$c] = self::add64($v[$c], $v[$d]); |
@@ -296,8 +283,7 @@ discard block |
||
296 | 283 | * @return void |
297 | 284 | * @throws Error |
298 | 285 | */ |
299 | - public static function increment_counter($ctx, $inc) |
|
300 | - { |
|
286 | + public static function increment_counter($ctx, $inc) { |
|
301 | 287 | if ($inc < 0) { |
302 | 288 | throw new Error('Increasing by a negative number makes no sense.'); |
303 | 289 | } |
@@ -326,8 +312,7 @@ discard block |
||
326 | 312 | * @param int $plen |
327 | 313 | * @return void |
328 | 314 | */ |
329 | - public static function update(SplFixedArray $ctx, SplFixedArray $p, $plen) |
|
330 | - { |
|
315 | + public static function update(SplFixedArray $ctx, SplFixedArray $p, $plen) { |
|
331 | 316 | self::pseudoConstructor(); |
332 | 317 | |
333 | 318 | $offset = 0; |
@@ -382,8 +367,7 @@ discard block |
||
382 | 367 | * @return SplFixedArray |
383 | 368 | * @throws Error |
384 | 369 | */ |
385 | - public static function finish(SplFixedArray $ctx, SplFixedArray $out) |
|
386 | - { |
|
370 | + public static function finish(SplFixedArray $ctx, SplFixedArray $out) { |
|
387 | 371 | self::pseudoConstructor(); |
388 | 372 | if ($ctx[4] > 128) { |
389 | 373 | self::increment_counter($ctx, 128); |
@@ -421,8 +405,7 @@ discard block |
||
421 | 405 | * @return SplFixedArray |
422 | 406 | * @throws Exception |
423 | 407 | */ |
424 | - public static function init($key = null, $outlen = 64) |
|
425 | - { |
|
408 | + public static function init($key = null, $outlen = 64) { |
|
426 | 409 | self::pseudoConstructor(); |
427 | 410 | $klen = 0; |
428 | 411 | |
@@ -476,8 +459,7 @@ discard block |
||
476 | 459 | * @param string $str |
477 | 460 | * @return SplFixedArray |
478 | 461 | */ |
479 | - public static function stringToSplFixedArray($str = '') |
|
480 | - { |
|
462 | + public static function stringToSplFixedArray($str = '') { |
|
481 | 463 | $values = unpack('C*', $str); |
482 | 464 | return SplFixedArray::fromArray(array_values($values)); |
483 | 465 | } |
@@ -490,8 +472,7 @@ discard block |
||
490 | 472 | * @param SplFixedArray $a |
491 | 473 | * @return string |
492 | 474 | */ |
493 | - public static function SplFixedArrayToString(SplFixedArray $a) |
|
494 | - { |
|
475 | + public static function SplFixedArrayToString(SplFixedArray $a) { |
|
495 | 476 | /** |
496 | 477 | * @var array<mixed, int> |
497 | 478 | */ |
@@ -508,8 +489,7 @@ discard block |
||
508 | 489 | * @return string |
509 | 490 | * @throws TypeError |
510 | 491 | */ |
511 | - public static function contextToString(SplFixedArray $ctx) |
|
512 | - { |
|
492 | + public static function contextToString(SplFixedArray $ctx) { |
|
513 | 493 | $str = ''; |
514 | 494 | $ctxA = $ctx[0]->toArray(); |
515 | 495 | |
@@ -563,8 +543,7 @@ discard block |
||
563 | 543 | * @param string $string |
564 | 544 | * @return SplFixedArray |
565 | 545 | */ |
566 | - public static function stringToContext($string) |
|
567 | - { |
|
546 | + public static function stringToContext($string) { |
|
568 | 547 | $ctx = self::context(); |
569 | 548 | |
570 | 549 | # uint64_t h[8]; |
@@ -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 | } |