openssl_seal()/_open() is not able to handle gcm cipers, e.g. aes-256-gcm
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- C
- Star
- 40.4k
- Fork
- 8.2k
- Merge trung bình
- 2 ngày 13 giờ
- Pull request đã merge (30 ngày)
- 96
Mô tả
Description
The following code:
<?php
const NUM_KEYS = 2;
for ($i = 0; $i < NUM_KEYS; $i++) {
$keys[] = $key = openssl_pkey_new();
$details = openssl_pkey_get_details($key);
$pubKeys[] = $details['key'];
$privKeys[] = openssl_pkey_get_private($key);
}
$data = 'Test Data String';
$ciphers = [
'aes-256-ctr',
'aes-256-gcm',
];
foreach ($ciphers as $cipherAlgo) {
echo "*** TESTING $cipherAlgo ***" . PHP_EOL . PHP_EOL;
$iv = \random_bytes(openssl_cipher_iv_length($cipherAlgo));
$result = openssl_seal($data, $sealedData, $sealedKeys, $pubKeys, $cipherAlgo, $iv);
echo "DATA : " . strlen($data) . ' ' . $data . PHP_EOL;
echo "IV-LEN: " . openssl_cipher_iv_length($cipherAlgo) . PHP_EOL;
echo "IV : " . bin2hex($iv) . PHP_EOL;
echo "ENC DATA: " . strlen($sealedData) . ' ' . bin2hex($sealedData) . PHP_EOL;
echo "RESULT: " . ($result ? 'true' : 'false') . PHP_EOL;
echo PHP_EOL;
// Try decrypt
foreach ($keys as $i => $key) {
$decrypted = null; // ;)
$result = openssl_open($sealedData, $decrypted, $sealedKeys[$i], $key, $cipherAlgo, $iv);
echo "OPEN: " . $decrypted . PHP_EOL;
echo "RESULT: " . ($result ? 'true' : 'false') . PHP_EOL;
$result = openssl_private_decrypt($sealedKeys[$i], $unsealedKey, $key);
echo "UNSEAL: " . bin2hex($unsealedKey) . PHP_EOL;
echo "RESULT: " . ($result ? 'true' : 'false') . PHP_EOL;
echo "DECRYPT: " . openssl_decrypt($sealedData, $cipherAlgo, $unsealedKey, OPENSSL_RAW_DATA, $iv) . PHP_EOL;
}
echo PHP_EOL;
}
Resulted in this output:
$ ./openssl-seal-test.php
*** TESTING aes-256-ctr ***
DATA : 16 Test Data String
IV-LEN: 16
IV : 37ff67041864b987b8ec2dcd0879f55e
ENC DATA: 16 2a748aa6ae176caf59cf22868304d65a
RESULT: true
OPEN: Test Data String
RESULT: true
UNSEAL: 8745e00bdf3d88bd1caa8ec0150a9b369eade92e57fc2d26ae283f224c66af14
RESULT: true
DECRYPT: Test Data String
OPEN: Test Data String
RESULT: true
UNSEAL: 8745e00bdf3d88bd1caa8ec0150a9b369eade92e57fc2d26ae283f224c66af14
RESULT: true
DECRYPT: Test Data String
*** TESTING aes-256-gcm ***
DATA : 16 Test Data String
IV-LEN: 12
IV : 033b1589f62bebbfffa3adda
ENC DATA: 16 a531f88fc51e373235c928cae1eb7c29
RESULT: true
OPEN:
RESULT: false
UNSEAL: 600cf6e76cf4ae9d48a881e0649af4682aa9065e63bec515a6b8c9678e398d36
RESULT: true
DECRYPT:
OPEN:
RESULT: false
UNSEAL: 600cf6e76cf4ae9d48a881e0649af4682aa9065e63bec515a6b8c9678e398d36
RESULT: true
DECRYPT:
But I expected this output instead:
[...]
*** TESTING aes-256-gcm ***
DATA : 16 Test Data String
IV-LEN: 12
IV : 033b1589f62bebbfffa3adda
ENC DATA: 16 a531f88fc51e373235c928cae1eb7c29
RESULT: true
OPEN: Test Data String
RESULT: true
UNSEAL: 600cf6e76cf4ae9d48a881e0649af4682aa9065e63bec515a6b8c9678e398d36
RESULT: false
DECRYPT:
OPEN: Test Data String
RESULT: true
UNSEAL: 600cf6e76cf4ae9d48a881e0649af4682aa9065e63bec515a6b8c9678e398d36
RESULT: false
DECRYPT:
The point is that the gcm-cipher need the authentication tag for decrypting the data. This however does not seem to be saved by the openssl_seal() function. I have also placed a question in the OpenSSL issue tracker: https://github.com/openssl/openssl/issues/17235
PHP Version
PHP 8.0.13 (cli) (built: Nov 27 2021 17:17:19) ( ZTS )
Operating System
Gentoo Linux 5.15.6, but should not matter
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Tái hiện script PHP được cung cấp và so sánh các đường dẫn aes-256-ctr và aes-256-gcm thông qua openssl_seal() và openssl_open(). Đọc các phần triển khai của những entry point đó và issue OpenSSL được liên kết để xác định thẻ xác thực được xử lý như thế nào. Được xem là hoàn tất khi hành vi GCM và chế độ lỗi được bao phủ bởi hành vi API mà dự án mong đợi.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- c, php
- Lĩnh vực
- backend-api-design, cryptography, security
- Loại issue
- Lỗi
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 35/100