diff --git a/NEWS b/NEWS index 24fd78630de3..bde1f547abfd 100644 --- a/NEWS +++ b/NEWS @@ -82,6 +82,9 @@ PHP NEWS . Fixed OSS-Fuzz #545352966 (default value AST of an SHM-persisted partial). (ndossche) +- OpenSSL: + . Added AES-GCM-SIV support. (Rakdos8) + - PDO: . Fixed PDOStatement::getColumnMeta() reading out of bounds for an invalid column index. (Ilia Alshanetsky) diff --git a/ext/openssl/openssl_backend_common.c b/ext/openssl/openssl_backend_common.c index 995d836deead..e20c2a05579b 100644 --- a/ext/openssl/openssl_backend_common.c +++ b/ext/openssl/openssl_backend_common.c @@ -1682,10 +1682,13 @@ void php_openssl_load_cipher_mode(struct php_openssl_cipher_mode *mode, const EV switch (cipher_mode) { case EVP_CIPH_GCM_MODE: case EVP_CIPH_CCM_MODE: - /* We check for EVP_CIPH_SIV_MODE and EVP_CIPH_SIV_MODE, because older OpenSSL and LibreSSL do not support them. */ + /* We check for EVP_CIPH_SIV_MODE and EVP_CIPH_GCM_SIV_MODE, because older OpenSSL and LibreSSL do not support them. */ #ifdef EVP_CIPH_SIV_MODE case EVP_CIPH_SIV_MODE: #endif +#ifdef EVP_CIPH_GCM_SIV_MODE + case EVP_CIPH_GCM_SIV_MODE: +#endif #ifdef EVP_CIPH_OCB_MODE case EVP_CIPH_OCB_MODE: /* For OCB mode, explicitly set the tag length even when decrypting, @@ -1697,6 +1700,11 @@ void php_openssl_load_cipher_mode(struct php_openssl_cipher_mode *mode, const EV mode->is_single_run_aead = cipher_mode == EVP_CIPH_CCM_MODE; #ifdef EVP_CIPH_SIV_MODE mode->aad_supports_vector = cipher_mode == EVP_CIPH_SIV_MODE; +#endif +#ifdef EVP_CIPH_GCM_SIV_MODE + /* GCM-SIV only takes a 96-bit nonce, but OpenSSL accepts any IV length for it and reads + * 12 bytes from the IV buffer, see https://github.com/openssl/openssl/commit/b8997895c2e8. */ + mode->aead_fixed_iv_len = cipher_mode == EVP_CIPH_GCM_SIV_MODE; #endif break; #ifdef NID_chacha20_poly1305 @@ -1715,6 +1723,12 @@ zend_result php_openssl_validate_iv(const char **piv, size_t *piv_len, size_t iv char *iv_new; if (mode->is_aead) { + if (mode->aead_fixed_iv_len && *piv_len != iv_required_len) { + php_error_docref(NULL, E_WARNING, + "IV passed is %zd bytes long, cipher expects an IV of precisely %zd bytes", + *piv_len, iv_required_len); + return FAILURE; + } if (EVP_CIPHER_CTX_ctrl(cipher_ctx, mode->aead_ivlen_flag, *piv_len, NULL) <= 0) { php_error_docref(NULL, E_WARNING, "Setting of IV length for AEAD mode failed"); return FAILURE; diff --git a/ext/openssl/php_openssl_backend.h b/ext/openssl/php_openssl_backend.h index 364f27eec23e..28e3ac6b9b94 100644 --- a/ext/openssl/php_openssl_backend.h +++ b/ext/openssl/php_openssl_backend.h @@ -359,6 +359,7 @@ struct php_openssl_cipher_mode { bool set_tag_length_always; bool set_tag_length_when_encrypting; bool aad_supports_vector; + bool aead_fixed_iv_len; int aead_get_tag_flag; int aead_set_tag_flag; int aead_ivlen_flag; diff --git a/ext/openssl/tests/cipher_tests.inc b/ext/openssl/tests/cipher_tests.inc index 9d17e847bfa2..ffc57ad21556 100644 --- a/ext/openssl/tests/cipher_tests.inc +++ b/ext/openssl/tests/cipher_tests.inc @@ -213,6 +213,33 @@ $php_openssl_cipher_tests = array( '21ad626dc866cc539f2d0e34b6824fc3', ), ), + /* RFC 8452 Appendix C.2 vectors for AES-256-GCM-SIV. */ + 'aes-256-gcm-siv' => array( + array( + 'key' => '0100000000000000000000000000000000000000000000000000000000000000', + 'iv' => '030000000000000000000000', + 'aad' => '', + 'tag' => '07f5f4169bbf55a8400cd47ea6fd400f', + 'pt' => '', + 'ct' => '', + ), + array( + 'key' => '0100000000000000000000000000000000000000000000000000000000000000', + 'iv' => '030000000000000000000000', + 'aad' => '', + 'tag' => '843122130f7364b761e0b97427e3df28', + 'pt' => '0100000000000000', + 'ct' => 'c2ef328e5c71c83b', + ), + array( + 'key' => '0100000000000000000000000000000000000000000000000000000000000000', + 'iv' => '030000000000000000000000', + 'aad' => '01', + 'tag' => '91213f267e3b452f02d01ae33e4ec854', + 'pt' => '0200000000000000', + 'ct' => '1de22967237a8132', + ), + ), 'chacha20-poly1305' => array( array( 'key' => '808182838485868788898a8b8c8d8e8f' . diff --git a/ext/openssl/tests/openssl_decrypt_gcm_siv.phpt b/ext/openssl/tests/openssl_decrypt_gcm_siv.phpt new file mode 100644 index 000000000000..0173748a9c92 --- /dev/null +++ b/ext/openssl/tests/openssl_decrypt_gcm_siv.phpt @@ -0,0 +1,69 @@ +--TEST-- +openssl_decrypt() with GCM-SIV cipher algorithm tests +--EXTENSIONS-- +openssl +--SKIPIF-- += 3.2)"); +} +?> +--FILE-- + $test) { + echo "TEST $idx\n"; + $pt = openssl_decrypt($test['ct'], $method, $test['key'], OPENSSL_RAW_DATA, + $test['iv'], $test['tag'], $test['aad']); + var_dump($test['pt'] === $pt); +} + +// Use the last vector (non-empty pt + AAD) for tampering checks. +echo "TEST WRONGTAG\n"; +$bad_tag = $test['tag']; +$bad_tag[0] = chr(ord($bad_tag[0]) ^ 0x01); +var_dump(openssl_decrypt($test['ct'], $method, $test['key'], OPENSSL_RAW_DATA, + $test['iv'], $bad_tag, $test['aad'])); + +echo "TEST WRONGCT\n"; +$bad_ct = $test['ct']; +$bad_ct[0] = chr(ord($bad_ct[0]) ^ 0x01); +var_dump(openssl_decrypt($bad_ct, $method, $test['key'], OPENSSL_RAW_DATA, + $test['iv'], $test['tag'], $test['aad'])); + +echo "TEST WRONGNONCE\n"; +$bad_iv = $test['iv']; +$bad_iv[0] = chr(ord($bad_iv[0]) ^ 0x01); +var_dump(openssl_decrypt($test['ct'], $method, $test['key'], OPENSSL_RAW_DATA, + $bad_iv, $test['tag'], $test['aad'])); + +echo "TEST WRONGAAD\n"; +var_dump(openssl_decrypt($test['ct'], $method, $test['key'], OPENSSL_RAW_DATA, + $test['iv'], $test['tag'], 'unexpected aad')); + +echo "TEST SHORTNONCE\n"; +var_dump(openssl_decrypt($test['ct'], $method, $test['key'], OPENSSL_RAW_DATA, + substr($test['iv'], 0, 11), $test['tag'], $test['aad'])); +?> +--EXPECTF-- +TEST 0 +bool(true) +TEST 1 +bool(true) +TEST 2 +bool(true) +TEST WRONGTAG +bool(false) +TEST WRONGCT +bool(false) +TEST WRONGNONCE +bool(false) +TEST WRONGAAD +bool(false) +TEST SHORTNONCE + +Warning: openssl_decrypt(): IV passed is 11 bytes long, cipher expects an IV of precisely 12 bytes in %s on line %d +bool(false) diff --git a/ext/openssl/tests/openssl_encrypt_gcm_siv.phpt b/ext/openssl/tests/openssl_encrypt_gcm_siv.phpt new file mode 100644 index 000000000000..d9cccf29e07a --- /dev/null +++ b/ext/openssl/tests/openssl_encrypt_gcm_siv.phpt @@ -0,0 +1,60 @@ +--TEST-- +openssl_encrypt() with GCM-SIV cipher algorithm tests +--EXTENSIONS-- +openssl +--SKIPIF-- += 3.2)"); +} +?> +--FILE-- + $test) { + echo "TEST $idx\n"; + $ct = openssl_encrypt($test['pt'], $method, $test['key'], OPENSSL_RAW_DATA, + $test['iv'], $tag, $test['aad'], strlen($test['tag'])); + var_dump($test['ct'] === $ct); + var_dump($test['tag'] === $tag); +} + +// Failing when IV is not 12 bytes +var_dump(openssl_encrypt('data', $method, str_repeat('x', 32), 0, '', $tag)); +var_dump(openssl_encrypt('data', $method, str_repeat('x', 32), 0, 'x', $tag)); +var_dump(openssl_encrypt('data', $method, str_repeat('x', 32), 0, str_repeat('x', 13), $tag)); + +// Failing to retrieve tag (max is 16 bytes) +var_dump(openssl_encrypt('data', $method, str_repeat('x', 32), 0, str_repeat('x', 12), $tag, '', 20)); + +// Failing when no tag supplied +var_dump(openssl_encrypt('data', $method, str_repeat('x', 32), 0, str_repeat('x', 12))); +?> +--EXPECTF-- +TEST 0 +bool(true) +bool(true) +TEST 1 +bool(true) +bool(true) +TEST 2 +bool(true) +bool(true) + +Warning: openssl_encrypt(): IV passed is 0 bytes long, cipher expects an IV of precisely 12 bytes in %s on line %d +bool(false) + +Warning: openssl_encrypt(): IV passed is 1 bytes long, cipher expects an IV of precisely 12 bytes in %s on line %d +bool(false) + +Warning: openssl_encrypt(): IV passed is 13 bytes long, cipher expects an IV of precisely 12 bytes in %s on line %d +bool(false) + +Warning: openssl_encrypt(): Retrieving verification tag failed in %s on line %d +bool(false) + +Warning: openssl_encrypt(): A tag should be provided when using AEAD mode in %s on line %d +bool(false)