@@ -52,23 +52,27 @@ public static function encrypt($payload, $userPublicKey, $userAuthToken, $native
52
52
// generate salt
53
53
$ salt = openssl_random_pseudo_bytes (16 );
54
54
55
- $ prk = !empty ($ userAuthToken ) ?
56
- self ::hkdf ($ userAuthToken , $ sharedSecret , utf8_decode ('Content-Encoding: auth\0 ' ), 32 ) :
55
+ // section 4.3
56
+ $ ikm = !empty ($ userAuthToken ) ?
57
+ self ::hkdf ($ userAuthToken , $ sharedSecret , 'Content-Encoding: auth ' .chr (0 ), 32 ) :
57
58
$ sharedSecret ;
58
59
60
+ // section 4.2
59
61
$ context = self ::createContext ($ userPublicKey , $ localPublicKey );
60
62
61
63
// derive the Content Encryption Key
62
- $ contentEncryptionKeyInfo = self ::createInfo ('aesgcm ' , $ context );
63
- $ contentEncryptionKey = self ::hkdf ($ salt , $ prk , $ contentEncryptionKeyInfo , 16 );
64
+ // TODO Chrome GCM wants 'aesgcm'?
65
+ $ contentEncryptionKeyInfo = self ::createInfo ('aesgcm128 ' , $ context );
66
+ $ contentEncryptionKey = self ::hkdf ($ salt , $ ikm , $ contentEncryptionKeyInfo , 16 );
64
67
65
- // derive the Nonce
68
+ // section 3.3, derive the nonce
66
69
$ nonceInfo = self ::createInfo ('nonce ' , $ context );
67
- $ nonce = self ::hkdf ($ salt , $ prk , $ nonceInfo , 12 );
70
+ $ nonce = self ::hkdf ($ salt , $ ikm , $ nonceInfo , 12 );
68
71
69
72
// encrypt
73
+ // "The additional data passed to each invocation of AEAD_AES_128_GCM is a zero-length octet sequence."
70
74
if (!$ nativeEncryption ) {
71
- list ($ encryptedText , $ tag ) = \Jose \Util \GCM ::encrypt ($ contentEncryptionKey , $ nonce , $ plaintext , "" );
75
+ list ($ encryptedText , $ tag ) = \Jose \Util \GCM ::encrypt ($ contentEncryptionKey , $ nonce , $ plaintext , null );
72
76
$ cipherText = $ encryptedText .$ tag ;
73
77
} else {
74
78
$ cipherText = openssl_encrypt ($ plaintext , 'aes-128-gcm ' , $ contentEncryptionKey , false , $ nonce ); // base 64 encoded
@@ -150,6 +154,7 @@ private static function createInfo($type, $context) {
150
154
throw new \ErrorException ('Context argument has invalid size ' );
151
155
}
152
156
157
+ // TODO Why 'P-256'?
153
158
return 'Content-Encoding: ' .$ type .chr (0 ).'P-256 ' .$ context ;
154
159
}
155
160
}
0 commit comments