1<?php
2
3if (class_exists('ParagonIE_Sodium_Core_HChaCha20', false)) {
4 return;
5}
6
7/**
8 * Class ParagonIE_Sodium_Core_HChaCha20
9 */
10class ParagonIE_Sodium_Core_HChaCha20 extends ParagonIE_Sodium_Core_ChaCha20
11{
12 /**
13 * @param string $in
14 * @param string $key
15 * @param string|null $c
16 * @return string
17 *
18 * @throws SodiumException
19 * @throws TypeError
20 */
21 public static function hChaCha20($in, $key, $c = null)
22 {
23 if (self::strlen($in) !== 16) {
24 throw new SodiumException('Argument 1 must be 16 bytes');
25 }
26 if (self::strlen($key) !== 32) {
27 throw new SodiumException('Argument 2 must be 32 bytes');
28 }
29 $ctx = array();
30
31 if ($c === null) {
32 $ctx[0] = 0x61707865;
33 $ctx[1] = 0x3320646e;
34 $ctx[2] = 0x79622d32;
35 $ctx[3] = 0x6b206574;
36 } else {
37 $ctx[0] = self::load_4(self::substr($c, 0, 4));
38 $ctx[1] = self::load_4(self::substr($c, 4, 4));
39 $ctx[2] = self::load_4(self::substr($c, 8, 4));
40 $ctx[3] = self::load_4(self::substr($c, 12, 4));
41 }
42 $ctx[4] = self::load_4(self::substr($key, 0, 4));
43 $ctx[5] = self::load_4(self::substr($key, 4, 4));
44 $ctx[6] = self::load_4(self::substr($key, 8, 4));
45 $ctx[7] = self::load_4(self::substr($key, 12, 4));
46 $ctx[8] = self::load_4(self::substr($key, 16, 4));
47 $ctx[9] = self::load_4(self::substr($key, 20, 4));
48 $ctx[10] = self::load_4(self::substr($key, 24, 4));
49 $ctx[11] = self::load_4(self::substr($key, 28, 4));
50 $ctx[12] = self::load_4(self::substr($in, 0, 4));
51 $ctx[13] = self::load_4(self::substr($in, 4, 4));
52 $ctx[14] = self::load_4(self::substr($in, 8, 4));
53 $ctx[15] = self::load_4(self::substr($in, 12, 4));
54 return self::hChaCha20Bytes($ctx);
55 }
56
57 /**
58 * @param array $ctx
59 * @return string
60 * @throws TypeError
61 */
62 protected static function hChaCha20Bytes(array $ctx)
63 {
64 $x0 = (int) $ctx[0];
65 $x1 = (int) $ctx[1];
66 $x2 = (int) $ctx[2];
67 $x3 = (int) $ctx[3];
68 $x4 = (int) $ctx[4];
69 $x5 = (int) $ctx[5];
70 $x6 = (int) $ctx[6];
71 $x7 = (int) $ctx[7];
72 $x8 = (int) $ctx[8];
73 $x9 = (int) $ctx[9];
74 $x10 = (int) $ctx[10];
75 $x11 = (int) $ctx[11];
76 $x12 = (int) $ctx[12];
77 $x13 = (int) $ctx[13];
78 $x14 = (int) $ctx[14];
79 $x15 = (int) $ctx[15];
80
81 for ($i = 0; $i < 10; ++$i) {
82 # QUARTERROUND( x0, x4, x8, x12)
83 list($x0, $x4, $x8, $x12) = self::quarterRound($x0, $x4, $x8, $x12);
84
85 # QUARTERROUND( x1, x5, x9, x13)
86 list($x1, $x5, $x9, $x13) = self::quarterRound($x1, $x5, $x9, $x13);
87
88 # QUARTERROUND( x2, x6, x10, x14)
89 list($x2, $x6, $x10, $x14) = self::quarterRound($x2, $x6, $x10, $x14);
90
91 # QUARTERROUND( x3, x7, x11, x15)
92 list($x3, $x7, $x11, $x15) = self::quarterRound($x3, $x7, $x11, $x15);
93
94 # QUARTERROUND( x0, x5, x10, x15)
95 list($x0, $x5, $x10, $x15) = self::quarterRound($x0, $x5, $x10, $x15);
96
97 # QUARTERROUND( x1, x6, x11, x12)
98 list($x1, $x6, $x11, $x12) = self::quarterRound($x1, $x6, $x11, $x12);
99
100 # QUARTERROUND( x2, x7, x8, x13)
101 list($x2, $x7, $x8, $x13) = self::quarterRound($x2, $x7, $x8, $x13);
102
103 # QUARTERROUND( x3, x4, x9, x14)
104 list($x3, $x4, $x9, $x14) = self::quarterRound($x3, $x4, $x9, $x14);
105 }
106
107 return self::store32_le((int) ($x0 & 0xffffffff)) .
108 self::store32_le((int) ($x1 & 0xffffffff)) .
109 self::store32_le((int) ($x2 & 0xffffffff)) .
110 self::store32_le((int) ($x3 & 0xffffffff)) .
111 self::store32_le((int) ($x12 & 0xffffffff)) .
112 self::store32_le((int) ($x13 & 0xffffffff)) .
113 self::store32_le((int) ($x14 & 0xffffffff)) .
114 self::store32_le((int) ($x15 & 0xffffffff));
115 }
116}
117