Skip to content

Commit 0216630

Browse files
committed
Fix bug #79601 (Wrong ciphertext/tag in AES-CCM encryption for a 12 bytes IV)
1 parent 6b8ffdb commit 0216630

File tree

4 files changed

+57
-22
lines changed

4 files changed

+57
-22
lines changed

ext/openssl/openssl.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6391,11 +6391,6 @@ static int php_openssl_validate_iv(char **piv, size_t *piv_len, size_t iv_requir
63916391
{
63926392
char *iv_new;
63936393

6394-
/* Best case scenario, user behaved */
6395-
if (*piv_len == iv_required_len) {
6396-
return SUCCESS;
6397-
}
6398-
63996394
if (mode->is_aead) {
64006395
if (EVP_CIPHER_CTX_ctrl(cipher_ctx, mode->aead_ivlen_flag, *piv_len, NULL) != 1) {
64016396
php_error_docref(NULL, E_WARNING, "Setting of IV length for AEAD mode failed");
@@ -6404,6 +6399,11 @@ static int php_openssl_validate_iv(char **piv, size_t *piv_len, size_t iv_requir
64046399
return SUCCESS;
64056400
}
64066401

6402+
/* Best case scenario, user behaved */
6403+
if (*piv_len == iv_required_len) {
6404+
return SUCCESS;
6405+
}
6406+
64076407
iv_new = ecalloc(1, iv_required_len + 1);
64086408

64096409
if (*piv_len == 0) {

ext/openssl/tests/cipher_tests.inc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
11
<?php
22
$php_openssl_cipher_tests = array(
3+
'aes-128-ccm' => array(
4+
array(
5+
'key' => '404142434445464748494a4b4c4d4e4f',
6+
'iv' => '1011121314151617',
7+
'aad' => '000102030405060708090a0b0c0d0e0f',
8+
'tag' => '1fc64fbfaccd',
9+
'pt' => '202122232425262728292a2b2c2d2e2f',
10+
'ct' => 'd2a1f0e051ea5f62081a7792073d593d',
11+
),
12+
array(
13+
'key' => '404142434445464748494a4b4c4d4e4f',
14+
'iv' => '101112131415161718191a1b',
15+
'aad' => '000102030405060708090a0b0c0d0e0f' .
16+
'10111213',
17+
'tag' => '484392fbc1b09951',
18+
'pt' => '202122232425262728292a2b2c2d2e2f' .
19+
'3031323334353637',
20+
'ct' => 'e3b201a9f5b71a7a9b1ceaeccd97e70b' .
21+
'6176aad9a4428aa5',
22+
),
23+
),
324
'aes-256-ccm' => array(
425
array(
526
'key' => '1bde3251d41a8b5ea013c195ae128b21' .

ext/openssl/tests/openssl_decrypt_ccm.phpt

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,16 @@ if (!in_array('aes-256-ccm', openssl_get_cipher_methods()))
1010
--FILE--
1111
<?php
1212
require_once __DIR__ . "/cipher_tests.inc";
13-
$method = 'aes-256-ccm';
14-
$tests = openssl_get_cipher_tests($method);
13+
$methods = ['aes-128-ccm', 'aes-256-ccm'];
1514

16-
foreach ($tests as $idx => $test) {
17-
echo "TEST $idx\n";
18-
$pt = openssl_decrypt($test['ct'], $method, $test['key'], OPENSSL_RAW_DATA,
19-
$test['iv'], $test['tag'], $test['aad']);
20-
var_dump($test['pt'] === $pt);
15+
foreach ($methods as $method) {
16+
$tests = openssl_get_cipher_tests($method);
17+
foreach ($tests as $idx => $test) {
18+
echo "$method - TEST $idx\n";
19+
$pt = openssl_decrypt($test['ct'], $method, $test['key'], OPENSSL_RAW_DATA,
20+
$test['iv'], $test['tag'], $test['aad']);
21+
var_dump($test['pt'] === $pt);
22+
}
2123
}
2224

2325
// no IV
@@ -32,7 +34,11 @@ var_dump(openssl_decrypt($test['ct'], $method, $test['key'], OPENSSL_RAW_DATA,
3234

3335
?>
3436
--EXPECTF--
35-
TEST 0
37+
aes-128-ccm - TEST 0
38+
bool(true)
39+
aes-128-ccm - TEST 1
40+
bool(true)
41+
aes-256-ccm - TEST 0
3642
bool(true)
3743

3844
Warning: openssl_decrypt(): Setting of IV length for AEAD mode failed in %s on line %d

ext/openssl/tests/openssl_encrypt_ccm.phpt

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,17 @@ if (!in_array('aes-256-ccm', openssl_get_cipher_methods()))
1010
--FILE--
1111
<?php
1212
require_once __DIR__ . "/cipher_tests.inc";
13-
$method = 'aes-256-ccm';
14-
$tests = openssl_get_cipher_tests($method);
13+
$methods = ['aes-128-ccm', 'aes-256-ccm'];
1514

16-
foreach ($tests as $idx => $test) {
17-
echo "TEST $idx\n";
18-
$ct = openssl_encrypt($test['pt'], $method, $test['key'], OPENSSL_RAW_DATA,
19-
$test['iv'], $tag, $test['aad'], strlen($test['tag']));
20-
var_dump($test['ct'] === $ct);
21-
var_dump($test['tag'] === $tag);
15+
foreach ($methods as $method) {
16+
$tests = openssl_get_cipher_tests($method);
17+
foreach ($tests as $idx => $test) {
18+
echo "$method - TEST $idx\n";
19+
$ct = openssl_encrypt($test['pt'], $method, $test['key'], OPENSSL_RAW_DATA,
20+
$test['iv'], $tag, $test['aad'], strlen($test['tag']));
21+
var_dump($test['ct'] === $ct);
22+
var_dump($test['tag'] === $tag);
23+
}
2224
}
2325

2426
// Empty IV error
@@ -32,7 +34,13 @@ var_dump(strlen($tag));
3234
var_dump(openssl_encrypt('data', $method, 'password', 0, str_repeat('x', 16), $tag, '', 1024));
3335
?>
3436
--EXPECTF--
35-
TEST 0
37+
aes-128-ccm - TEST 0
38+
bool(true)
39+
bool(true)
40+
aes-128-ccm - TEST 1
41+
bool(true)
42+
bool(true)
43+
aes-256-ccm - TEST 0
3644
bool(true)
3745
bool(true)
3846

0 commit comments

Comments
 (0)