@@ -21,20 +21,22 @@ final class Encryption
21
21
22
22
/**
23
23
* @param string $payload
24
- * @param bool $automatic
24
+ * @param bool $automatic
25
+ *
25
26
* @return string padded payload (plaintext)
26
27
*/
27
28
public static function padPayload ($ payload , $ automatic )
28
29
{
29
30
$ payloadLen = Utils::safe_strlen ($ payload );
30
31
$ padLen = $ automatic ? self ::MAX_PAYLOAD_LENGTH - $ payloadLen : 0 ;
32
+
31
33
return pack ('n* ' , $ padLen ).str_pad ($ payload , $ padLen + $ payloadLen , chr (0 ), STR_PAD_LEFT );
32
34
}
33
35
34
36
/**
35
- * @param string $payload With padding
36
- * @param string $userPublicKey Base 64 encoded (MIME or URL-safe)
37
- * @param string $userAuthToken Base 64 encoded (MIME or URL-safe)
37
+ * @param string $payload With padding
38
+ * @param string $userPublicKey Base 64 encoded (MIME or URL-safe)
39
+ * @param string $userAuthToken Base 64 encoded (MIME or URL-safe)
38
40
* @param bool $nativeEncryption Use OpenSSL (>PHP7.1)
39
41
*
40
42
* @return array
@@ -84,7 +86,7 @@ public static function encrypt($payload, $userPublicKey, $userAuthToken, $native
84
86
// encrypt
85
87
// "The additional data passed to each invocation of AEAD_AES_128_GCM is a zero-length octet sequence."
86
88
if (!$ nativeEncryption ) {
87
- list ($ encryptedText , $ tag ) = \AESGCM \AESGCM ::encrypt ($ contentEncryptionKey , $ nonce , $ payload , "" );
89
+ list ($ encryptedText , $ tag ) = \AESGCM \AESGCM ::encrypt ($ contentEncryptionKey , $ nonce , $ payload , '' );
88
90
} else {
89
91
$ encryptedText = openssl_encrypt ($ payload , 'aes-128-gcm ' , $ contentEncryptionKey , OPENSSL_RAW_DATA , $ nonce , $ tag ); // base 64 encoded
90
92
}
@@ -98,7 +100,7 @@ public static function encrypt($payload, $userPublicKey, $userAuthToken, $native
98
100
}
99
101
100
102
/**
101
- * HMAC-based Extract-and-Expand Key Derivation Function (HKDF)
103
+ * HMAC-based Extract-and-Expand Key Derivation Function (HKDF).
102
104
*
103
105
* This is used to derive a secure encryption key from a mostly-secure shared
104
106
* secret.
@@ -114,6 +116,7 @@ public static function encrypt($payload, $userPublicKey, $userAuthToken, $native
114
116
* @param $ikm string Input keying material
115
117
* @param $info string Application-specific context
116
118
* @param $length int The length (in bytes) of the required output key
119
+ *
117
120
* @return string
118
121
*/
119
122
private static function hkdf ($ salt , $ ikm , $ info , $ length )
@@ -129,11 +132,13 @@ private static function hkdf($salt, $ikm, $info, $length)
129
132
* Creates a context for deriving encyption parameters.
130
133
* See section 4.2 of
131
134
* {@link https://tools.ietf.org/html/draft-ietf-httpbis-encryption-encoding-00}
132
- * From {@link https://github.com/GoogleChrome/push-encryption-node/blob/master/src/encrypt.js}
135
+ * From {@link https://github.com/GoogleChrome/push-encryption-node/blob/master/src/encrypt.js}.
133
136
*
134
137
* @param $clientPublicKey string The client's public key
135
138
* @param $serverPublicKey string Our public key
139
+ *
136
140
* @return string
141
+ *
137
142
* @throws \ErrorException
138
143
*/
139
144
private static function createContext ($ clientPublicKey , $ serverPublicKey )
@@ -155,14 +160,17 @@ private static function createContext($clientPublicKey, $serverPublicKey)
155
160
/**
156
161
* Returns an info record. See sections 3.2 and 3.3 of
157
162
* {@link https://tools.ietf.org/html/draft-ietf-httpbis-encryption-encoding-00}
158
- * From {@link https://github.com/GoogleChrome/push-encryption-node/blob/master/src/encrypt.js}
163
+ * From {@link https://github.com/GoogleChrome/push-encryption-node/blob/master/src/encrypt.js}.
159
164
*
160
165
* @param $type string The type of the info record
161
166
* @param $context string The context for the record
167
+ *
162
168
* @return string
169
+ *
163
170
* @throws \ErrorException
164
171
*/
165
- private static function createInfo ($ type , $ context ) {
172
+ private static function createInfo ($ type , $ context )
173
+ {
166
174
if (Utils::safe_strlen ($ context ) !== 135 ) {
167
175
throw new \ErrorException ('Context argument has invalid size ' );
168
176
}
0 commit comments