@@ -20,16 +20,19 @@ final class Encryption
20
20
const MAX_PAYLOAD_LENGTH = 4078 ;
21
21
22
22
/**
23
- * @param $payload
24
- * @return string
23
+ * @param string $payload
24
+ * @param bool $automatic
25
+ * @return string padded payload (plaintext)
25
26
*/
26
- public static function automaticPadding ($ payload )
27
+ public static function padPayload ($ payload, $ automatic )
27
28
{
28
- return str_pad ($ payload , self ::MAX_PAYLOAD_LENGTH , chr (0 ), STR_PAD_LEFT );
29
+ $ payloadLen = strlen ($ payload );
30
+ $ padLen = $ automatic ? self ::MAX_PAYLOAD_LENGTH - $ payloadLen : 0 ;
31
+ return chr ($ padLen >> 8 ).chr ($ padLen & 0xFF ).str_pad ($ payload , $ padLen + $ payloadLen , chr (0 ), STR_PAD_LEFT );
29
32
}
30
33
31
34
/**
32
- * @param string $payload
35
+ * @param string $payload With padding
33
36
* @param string $userPublicKey MIME base 64 encoded
34
37
* @param string $userAuthToken MIME base 64 encoded
35
38
* @param bool $nativeEncryption Use OpenSSL (>PHP7.1)
@@ -40,7 +43,6 @@ public static function encrypt($payload, $userPublicKey, $userAuthToken, $native
40
43
{
41
44
$ userPublicKey = base64_decode ($ userPublicKey );
42
45
$ userAuthToken = base64_decode ($ userAuthToken );
43
- $ plaintext = chr (0 ).chr (0 ).$ payload ;
44
46
45
47
// initialize utilities
46
48
$ math = EccFactory::getAdapter ();
@@ -82,9 +84,9 @@ public static function encrypt($payload, $userPublicKey, $userAuthToken, $native
82
84
// encrypt
83
85
// "The additional data passed to each invocation of AEAD_AES_128_GCM is a zero-length octet sequence."
84
86
if (!$ nativeEncryption ) {
85
- list ($ encryptedText , $ tag ) = \Jose \Util \GCM ::encrypt ($ contentEncryptionKey , $ nonce , $ plaintext , "" );
87
+ list ($ encryptedText , $ tag ) = \Jose \Util \GCM ::encrypt ($ contentEncryptionKey , $ nonce , $ payload , "" );
86
88
} else {
87
- $ encryptedText = openssl_encrypt ($ plaintext , 'aes-128-gcm ' , $ contentEncryptionKey , OPENSSL_RAW_DATA , $ nonce , $ tag ); // base 64 encoded
89
+ $ encryptedText = openssl_encrypt ($ payload , 'aes-128-gcm ' , $ contentEncryptionKey , OPENSSL_RAW_DATA , $ nonce , $ tag ); // base 64 encoded
88
90
}
89
91
90
92
// return values in url safe base64
0 commit comments